1 ;;; semantic/ctxt.el --- Context calculations for Semantic tools.
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010, 2011 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 parenthesis.
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 parenthesis.
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 ;; Disable parsing messages
160 (let ((semantic--progress-reporter nil
))
162 (if point
(goto-char point
))
163 (let* ((case-fold-search semantic-case-fold
))
164 (:override-with-args
())))))
166 (defun semantic-get-local-variables-default ()
167 "Get local values from a specific context.
168 Uses the bovinator with the special top-symbol `bovine-inner-scope'
169 to collect tags, such as local variables or prototypes."
170 ;; This assumes a bovine parser. Make sure we don't do
171 ;; anything in that case.
172 (when (and semantic--parse-table
(not (eq semantic--parse-table t
))
173 (not (semantic-parse-tree-unparseable-p)))
174 (let ((vars (semantic-get-cache-data 'get-local-variables
)))
177 ;;(message "Found cached vars.")
180 ;; We want nothing to do with funny syntaxing while doing this.
181 (semantic-unmatched-syntax-hook nil
)
183 (firstusefulstart nil
)
185 (while (not (semantic-up-context (point) 'function
))
187 (setq firstusefulstart
(point)))
191 ;; Note to self: semantic-parse-region returns cooked
192 ;; but unlinked tags. File information is lost here
193 ;; and is added next.
194 (append (semantic-parse-region
196 (save-excursion (semantic-end-of-context) (point))
201 ;; Modify the tags in place.
204 (semantic--tag-put-property (car vars2
) :filename
(buffer-file-name))
205 (setq vars2
(cdr vars2
)))
206 ;; Hash our value into the first context that produced useful results.
207 (when (and vars firstusefulstart
)
208 (let ((end (save-excursion
209 (goto-char firstusefulstart
)
211 (unless (semantic-end-of-context)
213 ;;(message "Caching values %d->%d." firstusefulstart end)
214 (semantic-cache-data-to-buffer
215 (current-buffer) firstusefulstart
217 ;; If the end-of-context fails,
218 ;; just use our cursor starting
221 vars
'get-local-variables
'exit-cache-zone
))
226 (define-overloadable-function semantic-get-local-arguments
(&optional point
)
227 "Get arguments (variables) from the current context at POINT.
228 Parameters are available if the point is in a function or method.
229 Return a list of tags unlinked from the originating buffer.
230 Arguments are obtained by overriding `get-local-arguments', or by the
231 default function `semantic-get-local-arguments-default'. This, must
232 return a list of tags, or a list of strings that will be converted to
235 (if point
(goto-char point
))
236 (let* ((case-fold-search semantic-case-fold
)
237 (args (:override-with-args
()))
239 ;; Convert unsafe arguments to the right thing.
244 ((semantic-tag-p arg
)
245 ;; Return a copy of tag without overlay.
246 ;; The overlay is preserved.
247 (semantic-tag-copy arg nil t
))
249 (semantic--tag-put-property
250 (semantic-tag-new-variable arg nil nil
)
251 :filename
(buffer-file-name)))
253 (error "Unknown parameter element %S" arg
)))
257 (defun semantic-get-local-arguments-default ()
258 "Get arguments (variables) from the current context.
259 Parameters are available if the point is in a function or method."
260 (let ((tag (semantic-current-tag)))
261 (if (and tag
(semantic-tag-of-class-p tag
'function
))
262 (semantic-tag-function-arguments tag
))))
264 (define-overloadable-function semantic-get-all-local-variables
(&optional point
)
265 "Get all local variables for this context, and parent contexts.
266 Local variables are returned in Semantic tag format.
267 Be default, this gets local variables, and local arguments.
268 Optional argument POINT is the location to start getting the variables from.")
270 (defun semantic-get-all-local-variables-default (&optional point
)
271 "Get all local variables for this context.
272 Optional argument POINT is the location to start getting the variables from.
273 That is a cons (LOCAL-ARGUMENTS . LOCAL-VARIABLES) where:
275 - LOCAL-ARGUMENTS is collected by `semantic-get-local-arguments'.
276 - LOCAL-VARIABLES is collected by `semantic-get-local-variables'."
278 (if point
(goto-char point
))
279 (let ((case-fold-search semantic-case-fold
))
280 (append (semantic-get-local-arguments)
281 (semantic-get-local-variables)))))
283 ;;; Local context parsing
285 ;; Context parsing assumes a series of language independent commonalities.
286 ;; These terms are used to describe those contexts:
288 ;; command - One command in the language.
289 ;; symbol - The symbol the cursor is on.
290 ;; This would include a series of type/field when applicable.
291 ;; assignment - The variable currently being assigned to
292 ;; function - The function call the cursor is on/in
293 ;; argument - The index to the argument the cursor is on.
296 (define-overloadable-function semantic-end-of-command
()
297 "Move to the end of the current command.
298 Be default, uses `semantic-command-separation-character'.")
300 (defun semantic-end-of-command-default ()
301 "Move to the end of the current command.
302 Depends on `semantic-command-separation-character' to find the
303 beginning and end of a command."
304 (semantic-with-buffer-narrowed-to-context
305 (let ((case-fold-search semantic-case-fold
))
306 (with-syntax-table semantic-lex-syntax-table
308 (if (re-search-forward (regexp-quote semantic-command-separation-character
)
311 ;; If there wasn't a command after this, we are the last
312 ;; command, and we are incomplete.
313 (goto-char (point-max)))))))
315 (define-overloadable-function semantic-beginning-of-command
()
316 "Move to the beginning of the current command.
317 Be default, uses `semantic-command-separation-character'.")
319 (defun semantic-beginning-of-command-default ()
320 "Move to the beginning of the current command.
321 Depends on `semantic-command-separation-character' to find the
322 beginning and end of a command."
323 (semantic-with-buffer-narrowed-to-context
324 (with-syntax-table semantic-lex-syntax-table
325 (let ((case-fold-search semantic-case-fold
))
326 (skip-chars-backward semantic-command-separation-character
)
327 (if (re-search-backward (regexp-quote semantic-command-separation-character
)
329 (goto-char (match-end 0))
330 ;; If there wasn't a command after this, we are the last
331 ;; command, and we are incomplete.
332 (goto-char (point-min)))
333 (skip-chars-forward " \t\n")
337 (defsubst semantic-point-at-beginning-of-command
()
338 "Return the point at the beginning of the current command."
339 (save-excursion (semantic-beginning-of-command) (point)))
341 (defsubst semantic-point-at-end-of-command
()
342 "Return the point at the beginning of the current command."
343 (save-excursion (semantic-end-of-command) (point)))
345 (defsubst semantic-narrow-to-command
()
346 "Narrow the current buffer to the current command."
347 (narrow-to-region (semantic-point-at-beginning-of-command)
348 (semantic-point-at-end-of-command)))
350 (defmacro semantic-with-buffer-narrowed-to-command
(&rest body
)
351 "Execute BODY with the buffer narrowed to the current command."
353 (semantic-narrow-to-command)
355 (put 'semantic-with-buffer-narrowed-to-command
'lisp-indent-function
0)
356 (add-hook 'edebug-setup-hook
358 (def-edebug-spec semantic-with-buffer-narrowed-to-command
362 (define-overloadable-function semantic-ctxt-current-symbol
(&optional point
)
363 "Return the current symbol the cursor is on at POINT in a list.
364 The symbol includes all logical parts of a complex reference.
365 For example, in C the statement:
368 Would be object `this' calling method `that' which returns some structure
369 whose field `entry' is being reference. In this case, this function
370 would return the list:
371 ( \"this\" \"that\" \"entry\" )")
373 (defun semantic-ctxt-current-symbol-default (&optional point
)
374 "Return the current symbol the cursor is on at POINT in a list.
375 This will include a list of type/field names when applicable.
376 Depends on `semantic-type-relation-separator-character'."
378 (if point
(goto-char point
))
379 (let* ((fieldsep1 (mapconcat (lambda (a) (regexp-quote a
))
380 semantic-type-relation-separator-character
382 ;; NOTE: The [ \n] expression below should used \\s-, but that
383 ;; doesn't work in C since \n means end-of-comment, and isn't
384 ;; really whitespace.
385 (fieldsep (concat "[ \t\n\r]*\\(" fieldsep1
"\\)[ \t\n\r]*\\(\\w\\|\\s_\\)"))
386 (case-fold-search semantic-case-fold
)
389 (with-syntax-table semantic-lex-syntax-table
391 (cond ((looking-at "\\w\\|\\s_")
392 ;; In the middle of a symbol, move to the end.
394 ((looking-at fieldsep1
)
395 ;; We are in a find spot.. do nothing.
399 (and (condition-case nil
400 (progn (forward-sexp -
1)
404 (looking-at fieldsep1
)))
405 (setq symlist
(list ""))
407 ;; Skip array expressions.
408 (while (looking-at "\\s(") (forward-sexp -
1))
411 ;; Set our end point.
414 ;; Now that we have gotten started, lets do the rest.
416 (while (save-excursion
418 (looking-at "\\w\\|\\s_"))
419 ;; We have a symbol.. Do symbol things
421 (setq symlist
(cons (buffer-substring-no-properties (point) end
)
423 ;; Skip the next syntactic expression backwards, then go forwards.
427 ;; If we end up at the same place we started, we are at the
428 ;; beginning of a buffer, or narrowed to a command and
430 (if (<= cp
(point)) (error nil
)))
431 (if (looking-at fieldsep
)
434 ;; Skip array expressions.
435 (while (and (looking-at "\\s(") (not (bobp)))
445 (define-overloadable-function semantic-ctxt-current-symbol-and-bounds
(&optional point
)
446 "Return the current symbol and bounds the cursor is on at POINT.
447 The symbol should be the same as returned by `semantic-ctxt-current-symbol'.
448 Return (PREFIX ENDSYM BOUNDS).")
450 (defun semantic-ctxt-current-symbol-and-bounds-default (&optional point
)
451 "Return the current symbol and bounds the cursor is on at POINT.
452 Uses `semantic-ctxt-current-symbol' to calculate the symbol.
453 Return (PREFIX ENDSYM BOUNDS)."
455 (when point
(goto-char (point)))
456 (let* ((prefix (semantic-ctxt-current-symbol))
457 (endsym (car (reverse prefix
)))
458 ;; @todo - Can we get this data direct from ctxt-current-symbol?
459 (bounds (save-excursion
460 (cond ((string= endsym
"")
461 (cons (point) (point))
463 ((and prefix
(looking-at endsym
))
471 (cons (progn (forward-sexp -
1) (point))
472 (progn (forward-sexp 1) (point)))
476 (list prefix endsym bounds
))))
478 (define-overloadable-function semantic-ctxt-current-assignment
(&optional point
)
479 "Return the current assignment near the cursor at POINT.
480 Return a list as per `semantic-ctxt-current-symbol'.
481 Return nil if there is nothing relevant.")
483 (defun semantic-ctxt-current-assignment-default (&optional point
)
484 "Return the current assignment near the cursor at POINT.
485 By default, assume that \"=\" indicates an assignment."
486 (if point
(goto-char point
))
487 (let ((case-fold-search semantic-case-fold
))
488 (with-syntax-table semantic-lex-syntax-table
490 (semantic-with-buffer-narrowed-to-command
492 (skip-chars-forward " \t=")
493 (condition-case nil
(forward-char 1) (error nil
))
494 (re-search-backward "[^=]=\\([^=]\\|$\\)")
495 ;; We are at an equals sign. Go backwards a sexp, and
496 ;; we'll have the variable. Otherwise we threw an error
498 (semantic-ctxt-current-symbol)))
501 (define-overloadable-function semantic-ctxt-current-function
(&optional point
)
502 "Return the current function call the cursor is in at POINT.
503 The function returned is the one accepting the arguments that
504 the cursor is currently in. It will not return function symbol if the
505 cursor is on the text representing that function.")
507 (defun semantic-ctxt-current-function-default (&optional point
)
508 "Return the current function call the cursor is in at POINT.
509 The call will be identified for C like languages with the form
511 (if point
(goto-char point
))
512 (let ((case-fold-search semantic-case-fold
))
513 (with-syntax-table semantic-lex-syntax-table
515 (semantic-up-context)
516 (when (looking-at "(")
517 (semantic-ctxt-current-symbol))))
520 (define-overloadable-function semantic-ctxt-current-argument
(&optional point
)
521 "Return the index of the argument position the cursor is on at POINT.")
523 (defun semantic-ctxt-current-argument-default (&optional point
)
524 "Return the index of the argument the cursor is on at POINT.
525 Depends on `semantic-function-argument-separation-character'."
526 (if point
(goto-char point
))
527 (let ((case-fold-search semantic-case-fold
))
528 (with-syntax-table semantic-lex-syntax-table
529 (when (semantic-ctxt-current-function)
531 ;; Only get the current arg index if we are in function args.
534 (semantic-up-context)
535 (while (re-search-forward
536 (regexp-quote semantic-function-argument-separation-character
)
541 (defun semantic-ctxt-current-thing ()
542 "Calculate a thing identified by the current cursor position.
543 Calls previously defined `semantic-ctxt-current-...' calls until something
544 gets a match. See `semantic-ctxt-current-symbol',
545 `semantic-ctxt-current-function', and `semantic-ctxt-current-assignment'
546 for details on the return value."
547 (or (semantic-ctxt-current-symbol)
548 (semantic-ctxt-current-function)
549 (semantic-ctxt-current-assignment)))
551 (define-overloadable-function semantic-ctxt-current-class-list
(&optional point
)
552 "Return a list of tag classes that are allowed at POINT.
553 If POINT is nil, the current buffer location is used.
554 For example, in Emacs Lisp, the symbol after a ( is most likely
555 a function. In a makefile, symbols after a : are rules, and symbols
556 after a $( are variables.")
558 (defun semantic-ctxt-current-class-list-default (&optional point
)
559 "Return a list of tag classes that are allowed at POINT.
560 Assume a functional typed language. Uses very simple rules."
562 (if point
(goto-char point
))
564 (let ((tag (semantic-current-tag)))
566 (cond ((semantic-tag-of-class-p tag
'function
)
567 '(function variable type
))
568 ((or (semantic-tag-of-class-p tag
'type
)
569 (semantic-tag-of-class-p tag
'variable
))
576 (define-overloadable-function semantic-ctxt-current-mode
(&optional point
)
577 "Return the major mode active at POINT.
578 POINT defaults to the value of point in current buffer.
579 You should override this function in multiple mode buffers to
580 determine which major mode apply at point.")
582 (defun semantic-ctxt-current-mode-default (&optional point
)
583 "Return the major mode active at POINT.
584 POINT defaults to the value of point in current buffer.
585 This default implementation returns the current major mode."
590 ;; Scoped types are types that the current code would have access to.
591 ;; The come from the global namespace or from special commands such as "using"
592 (define-overloadable-function semantic-ctxt-scoped-types
(&optional point
)
593 "Return a list of type names currently in scope at POINT.
594 The return value can be a mixed list of either strings (names of
595 types that are in scope) or actual tags (type declared locally
596 that may or may not have a name.)")
598 (defun semantic-ctxt-scoped-types-default (&optional point
)
599 "Return a list of scoped types by name for the current context at POINT.
600 This is very different for various languages, and does nothing unless
604 (define-overloadable-function semantic-ctxt-imported-packages
(&optional point
)
605 "Return a list of package tags or names which are being imported at POINT.
606 The return value is a list of strings which are package names
607 that are implied in code. Thus a C++ symbol:
609 where there is a statement such as:
611 means that the first symbol might be:
615 (provide 'semantic
/ctxt
)
618 ;; generated-autoload-file: "loaddefs.el"
619 ;; generated-autoload-load-name: "semantic/ctxt"
622 ;; arch-tag: 04f3ae3c-78bb-40ca-b112-ba77f5e4ea88
623 ;;; semantic/ctxt.el ends here