1 ;;; semantic/ctxt.el --- Context calculations for Semantic tools.
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;;; 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs 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 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs 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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Semantic, as a tool, provides a nice list of searchable tags.
27 ;; That information can provide some very accurate answers if the current
28 ;; context of a position is known.
30 ;; This library provides the hooks needed for a language to specify how
31 ;; the current context is calculated.
36 (defvar semantic-command-separation-character
38 "String which indicates the end of a command.
39 Used for identifying the end of a single command.")
40 (make-variable-buffer-local 'semantic-command-separation-character
)
42 (defvar semantic-function-argument-separation-character
44 "String which indicates the end of an argument.
45 Used for identifying arguments to functions.")
46 (make-variable-buffer-local 'semantic-function-argument-separation-character
)
50 ;; These context are nested blocks of code, such as code in an
52 (declare-function semantic-current-tag-of-class
"semantic/find")
54 (define-overloadable-function semantic-up-context
(&optional point bounds-type
)
55 "Move point up one context from POINT.
56 Return non-nil if there are no more context levels.
57 Overloaded functions using `up-context' take no parameters.
58 BOUNDS-TYPE is a symbol representing a tag class to restrict
59 movement to. If this is nil, 'function is used.
60 This will find the smallest tag of that class (function, variable,
61 type, etc) and make sure non-nil is returned if you cannot
62 go up past the bounds of that tag."
63 (require 'semantic
/find
)
64 (if point
(goto-char point
))
65 (let ((nar (semantic-current-tag-of-class (or bounds-type
'function
))))
67 (semantic-with-buffer-narrowed-to-tag nar
(:override-with-args
()))
69 (error "No context of type %s to advance in" bounds-type
))
70 (:override-with-args
()))))
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.
83 (define-overloadable-function 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'.")
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)
97 (define-overloadable-function 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
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)
110 ;; Go over the list, and back over the end parenthisis.
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)
121 (goto-char (semantic-tag-end (semantic-current-tag)))
122 (when (< (point) start
)
128 (defun semantic-narrow-to-context ()
129 "Narrow the buffer to the extent of the current context."
132 (if (semantic-beginning-of-context)
136 (if (semantic-end-of-context)
139 (if (and b e
) (narrow-to-region b e
))))
141 (defmacro semantic-with-buffer-narrowed-to-context
(&rest body
)
142 "Execute BODY with the buffer narrowed to the current context."
144 (semantic-narrow-to-context)
146 (put 'semantic-with-buffer-narrowed-to-context
'lisp-indent-function
0)
147 (add-hook 'edebug-setup-hook
149 (def-edebug-spec semantic-with-buffer-narrowed-to-context
155 (define-overloadable-function semantic-get-local-variables
(&optional point
)
156 "Get the local variables based on POINT's context.
157 Local variables are returned in Semantic tag format.
158 This can be overriden with `get-local-variables'."
159 ;; The working status is to let the parser work properly
160 (let ((semantic--progress-reporter
161 (make-progress-reporter (semantic-parser-working-message "Local")
164 (if point
(goto-char point
))
165 (let* ((semantic-working-type nil
)
166 ;; Disable parsing messages
167 (case-fold-search semantic-case-fold
))
168 (:override-with-args
())))))
170 (defun semantic-get-local-variables-default ()
171 "Get local values from a specific context.
172 Uses the bovinator with the special top-symbol `bovine-inner-scope'
173 to collect tags, such as local variables or prototypes."
174 ;; This assumes a bovine parser. Make sure we don't do
175 ;; anything in that case.
176 (when (and semantic--parse-table
(not (eq semantic--parse-table t
))
177 (not (semantic-parse-tree-unparseable-p)))
178 (let ((vars (semantic-get-cache-data 'get-local-variables
)))
181 ;;(message "Found cached vars.")
184 ;; We want nothing to do with funny syntaxing while doing this.
185 (semantic-unmatched-syntax-hook nil
)
187 (firstusefulstart nil
)
189 (while (not (semantic-up-context (point) 'function
))
191 (setq firstusefulstart
(point)))
195 ;; Note to self: semantic-parse-region returns cooked
196 ;; but unlinked tags. File information is lost here
197 ;; and is added next.
198 (append (semantic-parse-region
200 (save-excursion (semantic-end-of-context) (point))
205 ;; Modify the tags in place.
208 (semantic--tag-put-property (car vars2
) :filename
(buffer-file-name))
209 (setq vars2
(cdr vars2
)))
210 ;; Hash our value into the first context that produced useful results.
211 (when (and vars firstusefulstart
)
212 (let ((end (save-excursion
213 (goto-char firstusefulstart
)
215 (unless (semantic-end-of-context)
217 ;;(message "Caching values %d->%d." firstusefulstart end)
218 (semantic-cache-data-to-buffer
219 (current-buffer) firstusefulstart
221 ;; If the end-of-context fails,
222 ;; just use our cursor starting
225 vars
'get-local-variables
'exit-cache-zone
))
230 (define-overloadable-function semantic-get-local-arguments
(&optional point
)
231 "Get arguments (variables) from the current context at POINT.
232 Parameters are available if the point is in a function or method.
233 Return a list of tags unlinked from the originating buffer.
234 Arguments are obtained by overriding `get-local-arguments', or by the
235 default function `semantic-get-local-arguments-default'. This, must
236 return a list of tags, or a list of strings that will be converted to
239 (if point
(goto-char point
))
240 (let* ((case-fold-search semantic-case-fold
)
241 (args (:override-with-args
()))
243 ;; Convert unsafe arguments to the right thing.
248 ((semantic-tag-p arg
)
249 ;; Return a copy of tag without overlay.
250 ;; The overlay is preserved.
251 (semantic-tag-copy arg nil t
))
253 (semantic--tag-put-property
254 (semantic-tag-new-variable arg nil nil
)
255 :filename
(buffer-file-name)))
257 (error "Unknown parameter element %S" arg
)))
261 (defun semantic-get-local-arguments-default ()
262 "Get arguments (variables) from the current context.
263 Parameters are available if the point is in a function or method."
264 (let ((tag (semantic-current-tag)))
265 (if (and tag
(semantic-tag-of-class-p tag
'function
))
266 (semantic-tag-function-arguments tag
))))
268 (define-overloadable-function semantic-get-all-local-variables
(&optional point
)
269 "Get all local variables for this context, and parent contexts.
270 Local variables are returned in Semantic tag format.
271 Be default, this gets local variables, and local arguments.
272 Optional argument POINT is the location to start getting the variables from.")
274 (defun semantic-get-all-local-variables-default (&optional point
)
275 "Get all local variables for this context.
276 Optional argument POINT is the location to start getting the variables from.
277 That is a cons (LOCAL-ARGUMENTS . LOCAL-VARIABLES) where:
279 - LOCAL-ARGUMENTS is collected by `semantic-get-local-arguments'.
280 - LOCAL-VARIABLES is collected by `semantic-get-local-variables'."
282 (if point
(goto-char point
))
283 (let ((case-fold-search semantic-case-fold
))
284 (append (semantic-get-local-arguments)
285 (semantic-get-local-variables)))))
287 ;;; Local context parsing
289 ;; Context parsing assumes a series of language independent commonalities.
290 ;; These terms are used to describe those contexts:
292 ;; command - One command in the language.
293 ;; symbol - The symbol the cursor is on.
294 ;; This would include a series of type/field when applicable.
295 ;; assignment - The variable currently being assigned to
296 ;; function - The function call the cursor is on/in
297 ;; argument - The index to the argument the cursor is on.
300 (define-overloadable-function semantic-end-of-command
()
301 "Move to the end of the current command.
302 Be default, uses `semantic-command-separation-character'.")
304 (defun semantic-end-of-command-default ()
305 "Move to the end of the current command.
306 Depends on `semantic-command-separation-character' to find the
307 beginning and end of a command."
308 (semantic-with-buffer-narrowed-to-context
309 (let ((case-fold-search semantic-case-fold
))
310 (with-syntax-table semantic-lex-syntax-table
312 (if (re-search-forward (regexp-quote semantic-command-separation-character
)
315 ;; If there wasn't a command after this, we are the last
316 ;; command, and we are incomplete.
317 (goto-char (point-max)))))))
319 (define-overloadable-function semantic-beginning-of-command
()
320 "Move to the beginning of the current command.
321 Be default, uses `semantic-command-separation-character'.")
323 (defun semantic-beginning-of-command-default ()
324 "Move to the beginning of the current command.
325 Depends on `semantic-command-separation-character' to find the
326 beginning and end of a command."
327 (semantic-with-buffer-narrowed-to-context
328 (with-syntax-table semantic-lex-syntax-table
329 (let ((case-fold-search semantic-case-fold
))
330 (skip-chars-backward semantic-command-separation-character
)
331 (if (re-search-backward (regexp-quote semantic-command-separation-character
)
333 (goto-char (match-end 0))
334 ;; If there wasn't a command after this, we are the last
335 ;; command, and we are incomplete.
336 (goto-char (point-min)))
337 (skip-chars-forward " \t\n")
341 (defsubst semantic-point-at-beginning-of-command
()
342 "Return the point at the beginning of the current command."
343 (save-excursion (semantic-beginning-of-command) (point)))
345 (defsubst semantic-point-at-end-of-command
()
346 "Return the point at the beginning of the current command."
347 (save-excursion (semantic-end-of-command) (point)))
349 (defsubst semantic-narrow-to-command
()
350 "Narrow the current buffer to the current command."
351 (narrow-to-region (semantic-point-at-beginning-of-command)
352 (semantic-point-at-end-of-command)))
354 (defmacro semantic-with-buffer-narrowed-to-command
(&rest body
)
355 "Execute BODY with the buffer narrowed to the current command."
357 (semantic-narrow-to-command)
359 (put 'semantic-with-buffer-narrowed-to-command
'lisp-indent-function
0)
360 (add-hook 'edebug-setup-hook
362 (def-edebug-spec semantic-with-buffer-narrowed-to-command
366 (define-overloadable-function semantic-ctxt-current-symbol
(&optional point
)
367 "Return the current symbol the cursor is on at POINT in a list.
368 The symbol includes all logical parts of a complex reference.
369 For example, in C the statement:
372 Would be object `this' calling method `that' which returns some structure
373 whose field `entry' is being reference. In this case, this function
374 would return the list:
375 ( \"this\" \"that\" \"entry\" )")
377 (defun semantic-ctxt-current-symbol-default (&optional point
)
378 "Return the current symbol the cursor is on at POINT in a list.
379 This will include a list of type/field names when applicable.
380 Depends on `semantic-type-relation-separator-character'."
382 (if point
(goto-char point
))
383 (let* ((fieldsep1 (mapconcat (lambda (a) (regexp-quote a
))
384 semantic-type-relation-separator-character
386 ;; NOTE: The [ \n] expression below should used \\s-, but that
387 ;; doesn't work in C since \n means end-of-comment, and isn't
388 ;; really whitespace.
389 (fieldsep (concat "[ \t\n\r]*\\(" fieldsep1
"\\)[ \t\n\r]*\\(\\w\\|\\s_\\)"))
390 (case-fold-search semantic-case-fold
)
393 (with-syntax-table semantic-lex-syntax-table
395 (cond ((looking-at "\\w\\|\\s_")
396 ;; In the middle of a symbol, move to the end.
398 ((looking-at fieldsep1
)
399 ;; We are in a find spot.. do nothing.
403 (and (condition-case nil
404 (progn (forward-sexp -
1)
408 (looking-at fieldsep1
)))
409 (setq symlist
(list ""))
411 ;; Skip array expressions.
412 (while (looking-at "\\s(") (forward-sexp -
1))
415 ;; Set our end point.
418 ;; Now that we have gotten started, lets do the rest.
420 (while (save-excursion
422 (looking-at "\\w\\|\\s_"))
423 ;; We have a symbol.. Do symbol things
425 (setq symlist
(cons (buffer-substring-no-properties (point) end
)
427 ;; Skip the next syntactic expression backwards, then go forwards.
431 ;; If we end up at the same place we started, we are at the
432 ;; beginning of a buffer, or narrowed to a command and
434 (if (<= cp
(point)) (error nil
)))
435 (if (looking-at fieldsep
)
438 ;; Skip array expressions.
439 (while (and (looking-at "\\s(") (not (bobp)))
449 (define-overloadable-function semantic-ctxt-current-symbol-and-bounds
(&optional point
)
450 "Return the current symbol and bounds the cursor is on at POINT.
451 The symbol should be the same as returned by `semantic-ctxt-current-symbol'.
452 Return (PREFIX ENDSYM BOUNDS).")
454 (defun semantic-ctxt-current-symbol-and-bounds-default (&optional point
)
455 "Return the current symbol and bounds the cursor is on at POINT.
456 Uses `semantic-ctxt-current-symbol' to calculate the symbol.
457 Return (PREFIX ENDSYM BOUNDS)."
459 (when point
(goto-char (point)))
460 (let* ((prefix (semantic-ctxt-current-symbol))
461 (endsym (car (reverse prefix
)))
462 ;; @todo - Can we get this data direct from ctxt-current-symbol?
463 (bounds (save-excursion
464 (cond ((string= endsym
"")
465 (cons (point) (point))
467 ((and prefix
(looking-at endsym
))
475 (cons (progn (forward-sexp -
1) (point))
476 (progn (forward-sexp 1) (point)))
480 (list prefix endsym bounds
))))
482 (define-overloadable-function semantic-ctxt-current-assignment
(&optional point
)
483 "Return the current assignment near the cursor at POINT.
484 Return a list as per `semantic-ctxt-current-symbol'.
485 Return nil if there is nothing relevant.")
487 (defun semantic-ctxt-current-assignment-default (&optional point
)
488 "Return the current assignment near the cursor at POINT.
489 By default, assume that \"=\" indicates an assignment."
490 (if point
(goto-char point
))
491 (let ((case-fold-search semantic-case-fold
))
492 (with-syntax-table semantic-lex-syntax-table
494 (semantic-with-buffer-narrowed-to-command
496 (skip-chars-forward " \t=")
497 (condition-case nil
(forward-char 1) (error nil
))
498 (re-search-backward "[^=]=\\([^=]\\|$\\)")
499 ;; We are at an equals sign. Go backwards a sexp, and
500 ;; we'll have the variable. Otherwise we threw an error
502 (semantic-ctxt-current-symbol)))
505 (define-overloadable-function semantic-ctxt-current-function
(&optional point
)
506 "Return the current function call the cursor is in at POINT.
507 The function returned is the one accepting the arguments that
508 the cursor is currently in. It will not return function symbol if the
509 cursor is on the text representing that function.")
511 (defun semantic-ctxt-current-function-default (&optional point
)
512 "Return the current function call the cursor is in at POINT.
513 The call will be identifed for C like langauges with the form
515 (if point
(goto-char point
))
516 (let ((case-fold-search semantic-case-fold
))
517 (with-syntax-table semantic-lex-syntax-table
519 (semantic-up-context)
520 (when (looking-at "(")
521 (semantic-ctxt-current-symbol))))
524 (define-overloadable-function semantic-ctxt-current-argument
(&optional point
)
525 "Return the index of the argument position the cursor is on at POINT.")
527 (defun semantic-ctxt-current-argument-default (&optional point
)
528 "Return the index of the argument the cursor is on at POINT.
529 Depends on `semantic-function-argument-separation-character'."
530 (if point
(goto-char point
))
531 (let ((case-fold-search semantic-case-fold
))
532 (with-syntax-table semantic-lex-syntax-table
533 (when (semantic-ctxt-current-function)
535 ;; Only get the current arg index if we are in function args.
538 (semantic-up-context)
539 (while (re-search-forward
540 (regexp-quote semantic-function-argument-separation-character
)
545 (defun semantic-ctxt-current-thing ()
546 "Calculate a thing identified by the current cursor position.
547 Calls previously defined `semantic-ctxt-current-...' calls until something
548 gets a match. See `semantic-ctxt-current-symbol',
549 `semantic-ctxt-current-function', and `semantic-ctxt-current-assignment'
550 for details on the return value."
551 (or (semantic-ctxt-current-symbol)
552 (semantic-ctxt-current-function)
553 (semantic-ctxt-current-assignment)))
555 (define-overloadable-function semantic-ctxt-current-class-list
(&optional point
)
556 "Return a list of tag classes that are allowed at POINT.
557 If POINT is nil, the current buffer location is used.
558 For example, in Emacs Lisp, the symbol after a ( is most likely
559 a function. In a makefile, symbols after a : are rules, and symbols
560 after a $( are variables.")
562 (defun semantic-ctxt-current-class-list-default (&optional point
)
563 "Return a list of tag classes that are allowed at POINT.
564 Assume a functional typed language. Uses very simple rules."
566 (if point
(goto-char point
))
568 (let ((tag (semantic-current-tag)))
570 (cond ((semantic-tag-of-class-p tag
'function
)
571 '(function variable type
))
572 ((or (semantic-tag-of-class-p tag
'type
)
573 (semantic-tag-of-class-p tag
'variable
))
579 (define-overloadable-function semantic-ctxt-current-mode
(&optional point
)
580 "Return the major mode active at POINT.
581 POINT defaults to the value of point in current buffer.
582 You should override this function in multiple mode buffers to
583 determine which major mode apply at point.")
585 (defun semantic-ctxt-current-mode-default (&optional point
)
586 "Return the major mode active at POINT.
587 POINT defaults to the value of point in current buffer.
588 This default implementation returns the current major mode."
593 ;; Scoped types are types that the current code would have access to.
594 ;; The come from the global namespace or from special commands such as "using"
595 (define-overloadable-function semantic-ctxt-scoped-types
(&optional point
)
596 "Return a list of type names currently in scope at POINT.
597 The return value can be a mixed list of either strings (names of
598 types that are in scope) or actual tags (type declared locally
599 that may or may not have a name.)")
601 (defun semantic-ctxt-scoped-types-default (&optional point
)
602 "Return a list of scoped types by name for the current context at POINT.
603 This is very different for various languages, and does nothing unless
605 (if point
(goto-char point
))
606 (let ((case-fold-search semantic-case-fold
))
607 ;; We need to look at TYPES within the bounds of locally parse arguments.
608 ;; C needs to find using statements and the like too. Bleh.
612 (provide 'semantic
/ctxt
)
614 ;;; semantic/ctxt.el ends here