Remove non-free old and crusty clearcase pkg
[packages] / xemacs-packages / semantic / semantic-ctxt.el
1 ;;; semantic-ctxt.el --- Context calculations for Semantic tools.
2
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Eric M. Ludlam
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: syntax
7 ;; X-RCS: $Id: semantic-ctxt.el,v 1.43 2007/02/19 13:48:19 zappo Exp $
8
9 ;; This file is not part of GNU Emacs.
10
11 ;; Semantic is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This software is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27 ;;
28 ;; Semantic, as a tool, provides a nice list of searchable tags.
29 ;; That information can provide some very accurate answers if the current
30 ;; context of a position is known.
31 ;;
32 ;; This library provides the hooks needed for a language to specify how
33 ;; the current context is calculated.
34 ;;
35 (require 'semantic)
36 (eval-when-compile (require 'semanticdb))
37
38 ;;; Code:
39 ;;
40 ;;;###autoload
41 (defvar semantic-command-separation-character
42  ";"
43   "String which indicates the end of a command.
44 Used for identifying the end of a single command.")
45 (make-variable-buffer-local 'semantic-command-separation-character)
46
47 (defvar semantic-function-argument-separation-character
48  ","
49   "String which indicates the end of an argument.
50 Used for identifying arguments to functions.")
51 (make-variable-buffer-local 'semantic-function-argument-separation-character)
52
53 ;;; Local variable parsing.
54 ;;
55 (define-overload semantic-up-context (&optional point bounds-type)
56   "Move point up one context from POINT.
57 Return non-nil if there are no more context levels.
58 Overloaded functions using `up-context' take no parameters.
59 BOUNDS-TYPE is a symbol representing a tag class to restrict
60 movement to.  If this is nil, 'function is used.
61 This will find the smallest tag of that class (function, variable,
62 type, etc) and make sure non-nil is returned if you cannot
63 go up past the bounds of that tag."
64   (if point (goto-char point))
65   (let ((nar (semantic-current-tag-of-class (or bounds-type 'function))))
66     (if nar
67         (semantic-with-buffer-narrowed-to-tag nar (:override-with-args ()))
68       (when bounds-type
69         (error "No context of type %s to advance in" bounds-type))
70       (:override-with-args ()))))
71
72 (defun semantic-up-context-default ()
73   "Move the point up and out one context level.
74 Works with languages that use parenthetical grouping."
75   ;; By default, assume that the language uses some form of parenthetical
76   ;; do dads for their context.
77   (condition-case nil
78       (progn
79         (up-list -1)
80         nil)
81     (error t)))
82
83 (define-overload semantic-beginning-of-context (&optional point)
84   "Move POINT to the beginning of the current context.
85 Return non-nil if there is no upper context.
86 The default behavior uses `semantic-up-context'.")
87
88 (defun semantic-beginning-of-context-default (&optional point)
89   "Move POINT to the beginning of the current context via parenthisis.
90 Return non-nil if there is no upper context."
91   (if point (goto-char point))
92   (if (semantic-up-context)
93       t
94     (forward-char 1)
95     nil))
96
97 (define-overload semantic-end-of-context (&optional point)
98   "Move POINT to the end of the current context.
99 Return non-nil if there is no upper context.
100 Be default, this uses `semantic-up-context', and assumes parenthetical
101 block delimiters.")
102
103 (defun semantic-end-of-context-default (&optional point)
104   "Move POINT to the end of the current context via parenthisis.
105 Return non-nil if there is no upper context."
106   (if point (goto-char point))
107   (let ((start (point)))
108     (if (semantic-up-context)
109         t
110       ;; Go over the list, and back over the end parenthisis.
111       (condition-case nil
112           (progn
113             (forward-sexp 1)
114             (forward-char -1))
115         (error
116          ;; If an error occurs, get the current tag from the cache,
117          ;; and just go to the end of that.  Make sure we end up at least
118          ;; where start was so parse-region type calls work.
119          (if (semantic-current-tag)
120              (progn
121                (goto-char (semantic-tag-end (semantic-current-tag)))
122                (when (< (point) start)
123                  (goto-char start)))
124            (goto-char start))
125          t)))
126     nil))
127
128 (defun semantic-narrow-to-context ()
129   "Narrow the buffer to the extent of the current context."
130   (let (b e)
131     (save-excursion
132       (if (semantic-beginning-of-context)
133           nil
134         (setq b (point))))
135     (save-excursion
136       (if (semantic-end-of-context)
137           nil
138         (setq e (point))))
139     (if (and b e) (narrow-to-region b e))))
140
141 (defmacro semantic-with-buffer-narrowed-to-context (&rest body)
142   "Execute BODY with the buffer narrowed to the current context."
143   `(save-restriction
144      (semantic-narrow-to-context)
145      ,@body))
146 (put 'semantic-with-buffer-narrowed-to-context 'lisp-indent-function 0)
147 (add-hook 'edebug-setup-hook
148           (lambda ()
149             (def-edebug-spec semantic-with-buffer-narrowed-to-context
150               (def-body))))
151
152 (define-overload semantic-get-local-variables (&optional point)
153   "Get the local variables based on POINT's context.
154 Local variables are returned in Semantic tag format.
155 This can be overriden with `get-local-variables'."
156   ;; The working status is to let the parser work properly
157   (working-status-forms
158       (semantic-parser-working-message "Local")
159       "done"
160     (save-excursion
161       (if point (goto-char point))
162       (let* ((semantic-working-type nil)
163              ;; Disable parsing messages
164              (working-status-dynamic-type nil)
165              (case-fold-search semantic-case-fold))
166         (:override-with-args ())))))
167
168 (defun semantic-get-local-variables-default ()
169   "Get local values from a specific context.
170 Uses the bovinator with the special top-symbol `bovine-inner-scope'
171 to collect tags, such as local variables or prototypes."
172   ;; This assumes a bovine parser.  Make sure we don't do
173   ;; anything in that case.
174   (when (and semantic--parse-table (not (eq semantic--parse-table t)))
175     (let ((vars nil)
176           (vars2 nil)
177           ;; We want nothing to do with funny syntaxing while doing this.
178           (semantic-unmatched-syntax-hook nil))
179       (while (not (semantic-up-context (point) 'function))
180         (save-excursion
181           (forward-char 1)
182           (setq vars
183                 ;; Note to self: semantic-parse-region returns cooked
184                 ;; but unlinked tags.  File information is lost here
185                 ;; and is added next.
186                 (append (semantic-parse-region
187                          (point)
188                          (save-excursion (semantic-end-of-context) (point))
189                          'bovine-inner-scope
190                          nil
191                          t)
192                         vars))))
193       (setq vars2 vars)
194       ;; Modify the tags in place.
195       (while vars2
196         (semantic--tag-put-property (car vars2) :filename (buffer-file-name))
197         (setq vars2 (cdr vars2)))
198       vars)))
199
200 (define-overload semantic-get-local-arguments (&optional point)
201   "Get arguments (variables) from the current context at POINT.
202 Parameters are available if the point is in a function or method.
203 Return a list of tags unlinked from the originating buffer.
204 Arguments are obtained by overriding `get-local-arguments', or by the
205 default function `semantic-get-local-arguments-default'.  This, must
206 return a list of tags, or a list of strings that will be converted to
207 tags."
208   (save-excursion
209     (if point (goto-char point))
210     (let* ((case-fold-search semantic-case-fold)
211            (args (:override-with-args ()))
212            arg tags)
213       ;; Convert unsafe arguments to the right thing.
214       (while args
215         (setq arg  (car args)
216               args (cdr args)
217               tags (cons (cond
218                           ((semantic-tag-p arg)
219                            ;; Return a copy of tag without overlay.
220                            ;; The overlay is preserved.
221                            (semantic-tag-copy arg nil t))
222                           ((stringp arg)
223                            (semantic--tag-put-property
224                             (semantic-tag-new-variable arg nil nil)
225                             :filename (buffer-file-name)))
226                           (t
227                            (error "Unknown parameter element %S" arg)))
228                          tags)))
229       (nreverse tags))))
230
231 (defun semantic-get-local-arguments-default ()
232   "Get arguments (variables) from the current context.
233 Parameters are available if the point is in a function or method."
234   (let ((tag (semantic-current-tag)))
235     (if (and tag (semantic-tag-of-class-p tag 'function))
236         (semantic-tag-function-arguments tag))))
237
238 (define-overload semantic-get-all-local-variables (&optional point)
239   "Get all local variables for this context, and parent contexts.
240 Local variables are returned in Semantic tag format.
241 Be default, this gets local variables, and local arguments.
242 Optional argument POINT is the location to start getting the variables from.")
243
244 (defun semantic-get-all-local-variables-default (&optional point)
245   "Get all local variables for this context.
246 Optional argument POINT is the location to start getting the variables from.
247 That is a cons (LOCAL-ARGUMENTS . LOCAL-VARIABLES) where:
248
249 - LOCAL-ARGUMENTS is collected by `semantic-get-local-arguments'.
250 - LOCAL-VARIABLES is collected by `semantic-get-local-variables'."
251   (save-excursion
252     (if point (goto-char point))
253     (let ((case-fold-search semantic-case-fold))
254       (cons (semantic-get-local-arguments)
255             (semantic-get-local-variables)))))
256
257 ;;; Local context parsing
258 ;;
259 ;; Context parsing assumes a series of language independent commonalities.
260 ;; These terms are used to describe those contexts:
261 ;;
262 ;; command      - One command in the language.
263 ;; symbol       - The symbol the cursor is on.
264 ;;                This would include a series of type/field when applicable.
265 ;; assignment   - The variable currently being assigned to
266 ;; function     - The function call the cursor is on/in
267 ;; argument     - The index to the argument the cursor is on.
268 ;;
269 ;;
270 (define-overload semantic-end-of-command ()
271   "Move to the end of the current command.
272 Be default, uses `semantic-command-separation-character'.")
273
274 (defun semantic-end-of-command-default ()
275   "Move to the beginning of the current command.
276 Depends on `semantic-command-separation-character' to find the
277 beginning and end of a command."
278   (semantic-with-buffer-narrowed-to-context
279     (let ((case-fold-search semantic-case-fold))
280       (with-syntax-table semantic-lex-syntax-table
281
282         (if (re-search-forward (regexp-quote semantic-command-separation-character)
283                                nil t)
284             (forward-char -1)
285           ;; If there wasn't a command after this, we are the last
286           ;; command, and we are incomplete.
287           (goto-char (point-max)))))))
288
289 (define-overload semantic-beginning-of-command ()
290   "Move to the beginning of the current command.
291 Be default, uses `semantic-command-separation-character'.")
292
293 (defun semantic-beginning-of-command-default ()
294   "Move to the beginning of the current command.
295 Depends on `semantic-command-separation-character' to find the
296 beginning and end of a command."
297   (semantic-with-buffer-narrowed-to-context
298     (with-syntax-table semantic-lex-syntax-table
299       (let ((case-fold-search semantic-case-fold))
300         (skip-chars-backward semantic-command-separation-character)
301         (if (re-search-backward (regexp-quote semantic-command-separation-character)
302                                 nil t)
303             (goto-char (match-end 0))
304           ;; If there wasn't a command after this, we are the last
305           ;; command, and we are incomplete.
306           (goto-char (point-min)))
307         (skip-chars-forward " \t\n")
308         ))))
309
310
311 (defsubst semantic-point-at-beginning-of-command ()
312   "Return the point at the beginning of the current command."
313   (save-excursion (semantic-beginning-of-command) (point)))
314
315 (defsubst semantic-point-at-end-of-command ()
316   "Return the point at the beginning of the current command."
317   (save-excursion (semantic-end-of-command) (point)))
318
319 (defsubst semantic-narrow-to-command ()
320   "Narrow the current buffer to the current command."
321   (narrow-to-region (semantic-point-at-beginning-of-command)
322                     (semantic-point-at-end-of-command)))
323
324 (defmacro semantic-with-buffer-narrowed-to-command (&rest body)
325   "Execute BODY with the buffer narrowed to the current command."
326   `(save-restriction
327      (semantic-narrow-to-command)
328      ,@body))
329 (put 'semantic-with-buffer-narrowed-to-command 'lisp-indent-function 0)
330 (add-hook 'edebug-setup-hook
331           (lambda ()
332             (def-edebug-spec semantic-with-buffer-narrowed-to-command
333               (def-body))))
334
335
336 (define-overload semantic-ctxt-current-symbol (&optional point)
337   "Return the current symbol the cursor is on at POINT in a list.
338 This will include a list of type/field names when applicable.")
339
340 (defun semantic-ctxt-current-symbol-default (&optional point)
341   "Return the current symbol the cursor is on at POINT in a list.
342 This will include a list of type/field names when applicable.
343 Depends on `semantic-type-relation-separator-character'."
344   (if point (goto-char point))
345   (let* ((fieldsep1 (mapconcat (lambda (a) (regexp-quote a))
346                                semantic-type-relation-separator-character
347                                "\\|"))
348          (fieldsep (concat "\\(" fieldsep1 "\\)\\(\\w\\|\\s_\\)"))
349          (case-fold-search semantic-case-fold)
350          (symlist nil)
351          end)
352     (with-syntax-table semantic-lex-syntax-table
353       (save-excursion
354         (if (looking-at "\\w\\|\\s_")
355             (forward-sexp 1)
356           ;; Not on a sym, are we at a separator char with no field
357           ;; specified yet?
358           (when (or (looking-at fieldsep1)
359                     (save-excursion
360                       (and (condition-case nil
361                                (progn (forward-sexp -1)
362                                       (forward-sexp 1)
363                                       t)
364                              (error nil))
365                            (looking-at fieldsep1))))
366             (setq symlist (list ""))
367             (forward-sexp -1)
368             ;; Skip array expressions.
369             (while (looking-at "\\s(") (forward-sexp -1))
370             (forward-sexp 1)))
371         (setq end (point))
372         (condition-case nil
373             (while (save-excursion
374                      (forward-char -1)
375                      (looking-at "\\w\\|\\s_"))
376               ;; We have a symbol.. Do symbol things
377               (forward-sexp -1)
378               (setq symlist (cons (buffer-substring-no-properties (point) end)
379                                   symlist))
380               ;; Skip the next syntactic expression backwards, then go forwards.
381               (let ((cp (point)))
382                 (forward-sexp -1)
383                 (forward-sexp 1)
384                 ;; If we end up at the same place we started, we are at the
385                 ;; beginning of a buffer, or narrowed to a command and
386                 ;; have to stop.
387                 (if (<= cp (point)) (error nil)))
388               (if (looking-at fieldsep)
389                   (progn
390                     (forward-sexp -1)
391                     ;; Skip array expressions.
392                     (while (looking-at "\\s(") (forward-sexp -1))
393                     (forward-sexp 1)
394                     (setq end (point)))
395                 (error nil))
396               )
397           (error nil)))
398       symlist)))
399
400 (define-overload semantic-ctxt-current-assignment (&optional point)
401   "Return the current assignment near the cursor at POINT.
402 Return a list as per `semantic-ctxt-current-symbol'.
403 Return nil if there is nothing relevant.")
404
405 (defun semantic-ctxt-current-assignment-default (&optional point)
406   "Return the current assignment near the cursor at POINT.
407 By default, assume that \"=\" indicates an assignment."
408   (if point (goto-char point))
409   (let ((case-fold-search semantic-case-fold))
410     (with-syntax-table semantic-lex-syntax-table
411       (condition-case nil
412           (semantic-with-buffer-narrowed-to-command
413             (save-excursion
414               (skip-chars-forward " \t=")
415               (condition-case nil (forward-char 1) (error nil))
416               (re-search-backward "[^=]=\\([^=]\\|$\\)")
417               ;; We are at an equals sign.  Go backwards a sexp, and
418               ;; we'll have the variable.  Otherwise we threw an error
419               (forward-sexp -1)
420               (semantic-ctxt-current-symbol)))
421         (error nil)))))
422
423 (define-overload semantic-ctxt-current-function (&optional point)
424   "Return the current function call the cursor is in at POINT.
425 The function returned is the one accepting the arguments that
426 the cursor is currently in.  It will not return function symbol if the
427 cursor is on the text representing that function.")
428
429 (defun semantic-ctxt-current-function-default (&optional point)
430   "Return the current function call the cursor is in at POINT.
431 The call will be identifed for C like langauges with the form
432  NAME ( args ... )"
433   (if point (goto-char point))
434   (let ((case-fold-search semantic-case-fold))
435     (with-syntax-table semantic-lex-syntax-table
436       (save-excursion
437         (semantic-up-context)
438         (when (looking-at "(")
439           (semantic-ctxt-current-symbol))))
440     ))
441
442 (define-overload semantic-ctxt-current-argument (&optional point)
443   "Return the index of the argument position the cursor is on at POINT.")
444
445 (defun semantic-ctxt-current-argument-default (&optional point)
446   "Return the index of the argument the cursor is on at POINT.
447 Depends on `semantic-function-argument-separation-character'."
448   (if point (goto-char point))
449   (let ((case-fold-search semantic-case-fold))
450     (with-syntax-table semantic-lex-syntax-table
451       (when (semantic-ctxt-current-function)
452         (save-excursion
453           ;; Only get the current arg index if we are in function args.
454           (let ((p (point))
455                 (idx 1))
456             (semantic-up-context)
457             (while (re-search-forward
458                     (regexp-quote semantic-function-argument-separation-character)
459                     p t)
460               (setq idx (1+ idx)))
461             idx))))))
462
463 (defun semantic-ctxt-current-thing ()
464   "Calculate a thing identified by the current cursor position.
465 Calls previously defined `semantic-ctxt-current-...' calls until something
466 gets a match.  See `semantic-ctxt-current-symbol',
467 `semantic-ctxt-current-function', and `semantic-ctxt-current-assignment'
468 for details on the return value."
469   (or (semantic-ctxt-current-symbol)
470       (semantic-ctxt-current-function)
471       (semantic-ctxt-current-assignment)))
472
473 (define-overload semantic-ctxt-current-class-list (&optional point)
474   "Return a list of tag classes that are allowed at POINT.
475 If POINT is nil, the current buffer location is used.
476 For example, in Emacs Lisp, the symbol after a ( is most likely
477 a function.  In a makefile, symbols after a : are rules, and symbols
478 after a $( are variables.")
479
480 (defun semantic-ctxt-current-class-list-default (&optional point)
481   "Return a list of tag classes that are allowed at POINT.
482 Assume a functional typed language.  Uses very simple rules."
483   (save-excursion
484     (if point (goto-char point))
485
486     (let ((tag (semantic-current-tag)))
487       (when tag
488         (cond ((semantic-tag-of-class-p tag 'function)
489                '(function variable))
490               ((or (semantic-tag-of-class-p tag 'type)
491                    (semantic-tag-of-class-p tag 'variable))
492                '(type))
493               (t nil))))))
494
495 (define-overload semantic-ctxt-current-mode (&optional point)
496   "Return the major mode active at POINT.
497 POINT defaults to the value of point in current buffer.
498 You should override this function in multiple mode buffers to
499 determine which major mode apply at point.")
500
501 (defun semantic-ctxt-current-mode-default (&optional point)
502   "Return the major mode active at POINT.
503 POINT defaults to the value of point in current buffer.
504 This default implementation returns the current major mode."
505   major-mode)
506 \f
507 ;;; Scoped Types
508 ;;
509 ;; Scoped types are types that the current code would have access to.
510 ;; The come from the global namespace or from special commands such as "using"
511 (define-overload semantic-ctxt-scoped-types (&optional point)
512   "Return a list of type names currently in scope at POINT.
513 The return value can be a mixed list of either strings (names of
514 types that are in scope) or actual tags (type declared locally
515 that may or may not have a name.)")
516
517 (defun semantic-ctxt-scoped-types-default (&optional point)
518   "Return a list of scoped types by name for the current context at POINT.
519 This is very different for various languages, and does nothing unless
520 overriden."
521   (if point (goto-char point))
522   (let ((case-fold-search semantic-case-fold))
523     ;; We need to look at TYPES within the bounds of locally parse arguments.
524     ;; C needs to find using statements and the like too.  Bleh.
525     nil
526     ))
527
528 (provide 'semantic-ctxt)
529
530 ;;; semantic-ctxt.el ends here