1 ;;; semantic/ctxt.el --- Context calculations for Semantic tools.
3 ;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Semantic, as a tool, provides a nice list of searchable tags.
26 ;; That information can provide some very accurate answers if the current
27 ;; context of a position is known.
29 ;; This library provides the hooks needed for a language to specify how
30 ;; the current context is calculated.
35 (defvar semantic-command-separation-character
37 "String which indicates the end of a command.
38 Used for identifying the end of a single command.")
39 (make-variable-buffer-local 'semantic-command-separation-character
)
41 (defvar semantic-function-argument-separation-character
43 "String which indicates the end of an argument.
44 Used for identifying arguments to functions.")
45 (make-variable-buffer-local 'semantic-function-argument-separation-character
)
49 ;; These context are nested blocks of code, such as code in an
51 (declare-function semantic-current-tag-of-class
"semantic/find")
53 (define-overloadable-function semantic-up-context
(&optional point bounds-type
)
54 "Move point up one context from POINT.
55 Return non-nil if there are no more context levels.
56 Overloaded functions using `up-context' take no parameters.
57 BOUNDS-TYPE is a symbol representing a tag class to restrict
58 movement to. If this is nil, 'function is used.
59 This will find the smallest tag of that class (function, variable,
60 type, etc) and make sure non-nil is returned if you cannot
61 go up past the bounds of that tag."
62 (require 'semantic
/find
)
63 (if point
(goto-char point
))
64 (let ((nar (semantic-current-tag-of-class (or bounds-type
'function
))))
66 (semantic-with-buffer-narrowed-to-tag nar
(:override-with-args
()))
68 (error "No context of type %s to advance in" bounds-type
))
69 (:override-with-args
()))))
71 (defun semantic-up-context-default ()
72 "Move the point up and out one context level.
73 Works with languages that use parenthetical grouping."
74 ;; By default, assume that the language uses some form of parenthetical
75 ;; do dads for their context.
82 (define-overloadable-function semantic-beginning-of-context
(&optional point
)
83 "Move POINT to the beginning of the current context.
84 Return non-nil if there is no upper context.
85 The default behavior uses `semantic-up-context'.")
87 (defun semantic-beginning-of-context-default (&optional point
)
88 "Move POINT to the beginning of the current context via parenthesis.
89 Return non-nil if there is no upper context."
90 (if point
(goto-char point
))
91 (if (semantic-up-context)
96 (define-overloadable-function semantic-end-of-context
(&optional point
)
97 "Move POINT to the end of the current context.
98 Return non-nil if there is no upper context.
99 Be default, this uses `semantic-up-context', and assumes parenthetical
102 (defun semantic-end-of-context-default (&optional point
)
103 "Move POINT to the end of the current context via parenthesis.
104 Return non-nil if there is no upper context."
105 (if point
(goto-char point
))
106 (let ((start (point)))
107 (if (semantic-up-context)
109 ;; Go over the list, and back over the end parenthesis.
115 ;; If an error occurs, get the current tag from the cache,
116 ;; and just go to the end of that. Make sure we end up at least
117 ;; where start was so parse-region type calls work.
118 (if (semantic-current-tag)
120 (goto-char (semantic-tag-end (semantic-current-tag)))
121 (when (< (point) start
)
127 (defun semantic-narrow-to-context ()
128 "Narrow the buffer to the extent of the current context."
131 (if (semantic-beginning-of-context)
135 (if (semantic-end-of-context)
138 (if (and b e
) (narrow-to-region b e
))))
140 (defmacro semantic-with-buffer-narrowed-to-context
(&rest body
)
141 "Execute BODY with the buffer narrowed to the current context."
143 (semantic-narrow-to-context)
145 (put 'semantic-with-buffer-narrowed-to-context
'lisp-indent-function
0)
146 (add-hook 'edebug-setup-hook
148 (def-edebug-spec semantic-with-buffer-narrowed-to-context
154 (define-overloadable-function semantic-get-local-variables
(&optional point
)
155 "Get the local variables based on POINT's context.
156 Local variables are returned in Semantic tag format.
157 This can be overridden with `get-local-variables'."
158 ;; Disable parsing messages
159 (let ((semantic--progress-reporter nil
))
161 (if point
(goto-char point
))
162 (let* ((case-fold-search semantic-case-fold
))
163 (:override-with-args
())))))
165 (defun semantic-get-local-variables-default ()
166 "Get local values from a specific context.
167 Uses the bovinator with the special top-symbol `bovine-inner-scope'
168 to collect tags, such as local variables or prototypes."
169 ;; This assumes a bovine parser. Make sure we don't do
170 ;; anything in that case.
171 (when (and semantic--parse-table
(not (eq semantic--parse-table t
)))
172 (let ((vars (semantic-get-cache-data 'get-local-variables
)))
175 ;;(message "Found cached vars.")
178 ;; We want nothing to do with funny syntaxing while doing this.
179 (semantic-unmatched-syntax-hook nil
)
181 (firstusefulstart nil
)
183 (while (not (semantic-up-context (point) 'function
))
185 (setq firstusefulstart
(point)))
189 ;; Note to self: semantic-parse-region returns cooked
190 ;; but unlinked tags. File information is lost here
191 ;; and is added next.
192 (append (semantic-parse-region
194 (save-excursion (semantic-end-of-context) (point))
199 ;; Modify the tags in place.
202 (semantic--tag-put-property (car vars2
) :filename
(buffer-file-name))
203 (setq vars2
(cdr vars2
)))
204 ;; Hash our value into the first context that produced useful results.
205 (when (and vars firstusefulstart
)
206 (let ((end (save-excursion
207 (goto-char firstusefulstart
)
209 (unless (semantic-end-of-context)
211 ;;(message "Caching values %d->%d." firstusefulstart end)
212 (semantic-cache-data-to-buffer
213 (current-buffer) firstusefulstart
215 ;; If the end-of-context fails,
216 ;; just use our cursor starting
219 vars
'get-local-variables
'exit-cache-zone
))
224 (define-overloadable-function semantic-get-local-arguments
(&optional point
)
225 "Get arguments (variables) from the current context at POINT.
226 Parameters are available if the point is in a function or method.
227 Return a list of tags unlinked from the originating buffer.
228 Arguments are obtained by overriding `get-local-arguments', or by the
229 default function `semantic-get-local-arguments-default'. This, must
230 return a list of tags, or a list of strings that will be converted to
233 (if point
(goto-char point
))
234 (let* ((case-fold-search semantic-case-fold
)
235 (args (:override-with-args
()))
237 ;; Convert unsafe arguments to the right thing.
242 ((semantic-tag-p arg
)
243 ;; Return a copy of tag without overlay.
244 ;; The overlay is preserved.
245 (semantic-tag-copy arg nil t
))
247 (semantic--tag-put-property
248 (semantic-tag-new-variable arg nil nil
)
249 :filename
(buffer-file-name)))
251 (error "Unknown parameter element %S" arg
)))
255 (defun semantic-get-local-arguments-default ()
256 "Get arguments (variables) from the current context.
257 Parameters are available if the point is in a function or method."
258 (let ((tag (semantic-current-tag)))
259 (if (and tag
(semantic-tag-of-class-p tag
'function
))
260 (semantic-tag-function-arguments tag
))))
262 (define-overloadable-function semantic-get-all-local-variables
(&optional point
)
263 "Get all local variables for this context, and parent contexts.
264 Local variables are returned in Semantic tag format.
265 Be default, this gets local variables, and local arguments.
266 Optional argument POINT is the location to start getting the variables from.")
268 (defun semantic-get-all-local-variables-default (&optional point
)
269 "Get all local variables for this context.
270 Optional argument POINT is the location to start getting the variables from.
271 That is a cons (LOCAL-ARGUMENTS . LOCAL-VARIABLES) where:
273 - LOCAL-ARGUMENTS is collected by `semantic-get-local-arguments'.
274 - LOCAL-VARIABLES is collected by `semantic-get-local-variables'."
276 (if point
(goto-char point
))
277 (let ((case-fold-search semantic-case-fold
))
278 (append (semantic-get-local-arguments)
279 (semantic-get-local-variables)))))
281 ;;; Local context parsing
283 ;; Context parsing assumes a series of language independent commonalities.
284 ;; These terms are used to describe those contexts:
286 ;; command - One command in the language.
287 ;; symbol - The symbol the cursor is on.
288 ;; This would include a series of type/field when applicable.
289 ;; assignment - The variable currently being assigned to
290 ;; function - The function call the cursor is on/in
291 ;; argument - The index to the argument the cursor is on.
294 (define-overloadable-function semantic-end-of-command
()
295 "Move to the end of the current command.
296 Be default, uses `semantic-command-separation-character'.")
298 (defun semantic-end-of-command-default ()
299 "Move to the end of the current command.
300 Depends on `semantic-command-separation-character' to find the
301 beginning and end of a command."
302 (semantic-with-buffer-narrowed-to-context
303 (let ((case-fold-search semantic-case-fold
))
304 (with-syntax-table semantic-lex-syntax-table
306 (if (re-search-forward (regexp-quote semantic-command-separation-character
)
309 ;; If there wasn't a command after this, we are the last
310 ;; command, and we are incomplete.
311 (goto-char (point-max)))))))
313 (define-overloadable-function semantic-beginning-of-command
()
314 "Move to the beginning of the current command.
315 Be default, uses `semantic-command-separation-character'.")
317 (defun semantic-beginning-of-command-default ()
318 "Move to the beginning of the current command.
319 Depends on `semantic-command-separation-character' to find the
320 beginning and end of a command."
321 (semantic-with-buffer-narrowed-to-context
322 (with-syntax-table semantic-lex-syntax-table
323 (let ((case-fold-search semantic-case-fold
))
324 (skip-chars-backward semantic-command-separation-character
)
325 (if (re-search-backward (regexp-quote semantic-command-separation-character
)
327 (goto-char (match-end 0))
328 ;; If there wasn't a command after this, we are the last
329 ;; command, and we are incomplete.
330 (goto-char (point-min)))
331 (skip-chars-forward " \t\n")
335 (defsubst semantic-point-at-beginning-of-command
()
336 "Return the point at the beginning of the current command."
337 (save-excursion (semantic-beginning-of-command) (point)))
339 (defsubst semantic-point-at-end-of-command
()
340 "Return the point at the beginning of the current command."
341 (save-excursion (semantic-end-of-command) (point)))
343 (defsubst semantic-narrow-to-command
()
344 "Narrow the current buffer to the current command."
345 (narrow-to-region (semantic-point-at-beginning-of-command)
346 (semantic-point-at-end-of-command)))
348 (defmacro semantic-with-buffer-narrowed-to-command
(&rest body
)
349 "Execute BODY with the buffer narrowed to the current command."
351 (semantic-narrow-to-command)
353 (put 'semantic-with-buffer-narrowed-to-command
'lisp-indent-function
0)
354 (add-hook 'edebug-setup-hook
356 (def-edebug-spec semantic-with-buffer-narrowed-to-command
359 (define-overloadable-function semantic-ctxt-end-of-symbol
(&optional point
)
360 "Move point to the end of the current symbol under POINT.
361 This skips forward over symbols in a complex reference.
362 For example, in the C statement:
365 If the cursor is on 'this', will move point to the ; after entry.")
367 (defun semantic-ctxt-end-of-symbol-default (&optional point
)
368 "Move point to the end of the current symbol under POINT.
369 This will move past type/field names when applicable.
370 Depends on `semantic-type-relation-separator-character', and will
371 work on C like languages."
372 (if point
(goto-char point
))
373 (let* ((fieldsep1 (mapconcat (lambda (a) (regexp-quote a
))
374 semantic-type-relation-separator-character
376 ;; NOTE: The [ \n] expression below should used \\s-, but that
377 ;; doesn't work in C since \n means end-of-comment, and isn't
378 ;; really whitespace.
379 (fieldsep (concat "[ \t\n\r]*\\(" fieldsep1
"\\)[ \t\n\r]*\\(\\w\\|\\s_\\)"))
380 (case-fold-search semantic-case-fold
)
384 (with-syntax-table semantic-lex-syntax-table
385 (cond ((looking-at "\\w\\|\\s_")
386 ;; In the middle of a symbol, move to the end.
388 ((looking-at fieldsep1
)
389 ;; We are in a fine spot.. do nothing.
393 (and (condition-case nil
394 (progn (forward-sexp -
1)
398 (looking-at fieldsep1
)))
400 ;; Skip array expressions.
401 (while (looking-at "\\s(") (forward-sexp -
1))
404 ;; Set the current end marker.
407 ;; Cursor is at the safe end of some symbol. Look until we
408 ;; find the logical end of this current complex symbol.
410 (while continuesearch
411 ;; If there are functional arguments, arrays, etc, skip them.
412 (when (looking-at "\\s(")
415 ;; If there is a field separator, then skip that, plus
416 ;; the next expected symbol.
417 (if (not (looking-at fieldsep1
))
421 ;; Skip the separator and the symbol.
422 (goto-char (match-end 0))
424 (if (looking-at "\\w\\|\\s_")
427 ;; No symbol, exit the search...
428 (setq continuesearch nil
))
435 ;; Restore position if we go to far....
436 (error (goto-char end
)) )
440 (define-overloadable-function semantic-ctxt-current-symbol
(&optional point
)
441 "Return the current symbol the cursor is on at POINT in a list.
442 The symbol includes all logical parts of a complex reference.
443 For example, in C the statement:
446 Would be object `this' calling method `that' which returns some structure
447 whose field `entry' is being reference. In this case, this function
448 would return the list:
449 ( \"this\" \"that\" \"entry\" )")
451 (defun semantic-ctxt-current-symbol-default (&optional point
)
452 "Return the current symbol the cursor is on at POINT in a list.
453 This will include a list of type/field names when applicable.
454 Depends on `semantic-type-relation-separator-character'."
456 (if point
(goto-char point
))
457 (let* ((fieldsep1 (mapconcat (lambda (a) (regexp-quote a
))
458 semantic-type-relation-separator-character
460 ;; NOTE: The [ \n] expression below should used \\s-, but that
461 ;; doesn't work in C since \n means end-of-comment, and isn't
462 ;; really whitespace.
463 (fieldsep (concat "[ \t\n\r]*\\(" fieldsep1
"\\)[ \t\n\r]*\\(\\w\\|\\s_\\)"))
464 (case-fold-search semantic-case-fold
)
467 (with-syntax-table semantic-lex-syntax-table
469 (cond ((looking-at "\\w\\|\\s_")
470 ;; In the middle of a symbol, move to the end.
472 ((looking-at fieldsep1
)
473 ;; We are in a fine spot.. do nothing.
477 (and (condition-case nil
478 (progn (forward-sexp -
1)
482 (looking-at fieldsep1
)))
483 (setq symlist
(list ""))
485 ;; Skip array expressions.
486 (while (looking-at "\\s(") (forward-sexp -
1))
489 ;; Set our end point.
492 ;; Now that we have gotten started, let's do the rest.
494 (while (save-excursion
496 (looking-at "\\w\\|\\s_"))
497 ;; We have a symbol.. Do symbol things
499 (setq symlist
(cons (buffer-substring-no-properties (point) end
)
501 ;; Skip the next syntactic expression backwards, then go forwards.
505 ;; If we end up at the same place we started, we are at the
506 ;; beginning of a buffer, or narrowed to a command and
508 (if (<= cp
(point)) (error nil
)))
509 (if (looking-at fieldsep
)
512 ;; Skip array expressions.
513 (while (and (looking-at "\\s(") (not (bobp)))
523 (define-overloadable-function semantic-ctxt-current-symbol-and-bounds
(&optional point
)
524 "Return the current symbol and bounds the cursor is on at POINT.
525 The symbol should be the same as returned by `semantic-ctxt-current-symbol'.
526 Return (PREFIX ENDSYM BOUNDS).")
528 (defun semantic-ctxt-current-symbol-and-bounds-default (&optional point
)
529 "Return the current symbol and bounds the cursor is on at POINT.
530 Uses `semantic-ctxt-current-symbol' to calculate the symbol.
531 Return (PREFIX ENDSYM BOUNDS)."
533 (when point
(goto-char (point)))
534 (let* ((prefix (semantic-ctxt-current-symbol))
535 (endsym (car (reverse prefix
)))
536 ;; @todo - Can we get this data direct from ctxt-current-symbol?
537 (bounds (save-excursion
538 (cond ((string= endsym
"")
539 (cons (point) (point))
541 ((and prefix
(looking-at endsym
))
549 (cons (progn (forward-sexp -
1) (point))
550 (progn (forward-sexp 1) (point)))
554 (list prefix endsym bounds
))))
556 (define-overloadable-function semantic-ctxt-current-assignment
(&optional point
)
557 "Return the current assignment near the cursor at POINT.
558 Return a list as per `semantic-ctxt-current-symbol'.
559 Return nil if there is nothing relevant.")
561 (defun semantic-ctxt-current-assignment-default (&optional point
)
562 "Return the current assignment near the cursor at POINT.
563 By default, assume that \"=\" indicates an assignment."
564 (if point
(goto-char point
))
565 (let ((case-fold-search semantic-case-fold
))
566 (with-syntax-table semantic-lex-syntax-table
568 (semantic-with-buffer-narrowed-to-command
570 (skip-chars-forward " \t=")
571 (condition-case nil
(forward-char 1) (error nil
))
572 (re-search-backward "[^=]=\\([^=]\\|$\\)")
573 ;; We are at an equals sign. Go backwards a sexp, and
574 ;; we'll have the variable. Otherwise we threw an error
576 (semantic-ctxt-current-symbol)))
579 (define-overloadable-function semantic-ctxt-current-function
(&optional point
)
580 "Return the current function call the cursor is in at POINT.
581 The function returned is the one accepting the arguments that
582 the cursor is currently in. It will not return function symbol if the
583 cursor is on the text representing that function.")
585 (defun semantic-ctxt-current-function-default (&optional point
)
586 "Return the current function call the cursor is in at POINT.
587 The call will be identified for C like languages with the form
589 (if point
(goto-char point
))
590 (let ((case-fold-search semantic-case-fold
))
591 (with-syntax-table semantic-lex-syntax-table
593 (semantic-up-context)
594 (when (looking-at "(")
595 (semantic-ctxt-current-symbol))))
598 (define-overloadable-function semantic-ctxt-current-argument
(&optional point
)
599 "Return the index of the argument position the cursor is on at POINT.")
601 (defun semantic-ctxt-current-argument-default (&optional point
)
602 "Return the index of the argument the cursor is on at POINT.
603 Depends on `semantic-function-argument-separation-character'."
604 (if point
(goto-char point
))
605 (let ((case-fold-search semantic-case-fold
))
606 (with-syntax-table semantic-lex-syntax-table
607 (when (semantic-ctxt-current-function)
609 ;; Only get the current arg index if we are in function args.
612 (semantic-up-context)
613 (while (re-search-forward
614 (regexp-quote semantic-function-argument-separation-character
)
619 (defun semantic-ctxt-current-thing ()
620 "Calculate a thing identified by the current cursor position.
621 Calls previously defined `semantic-ctxt-current-...' calls until something
622 gets a match. See `semantic-ctxt-current-symbol',
623 `semantic-ctxt-current-function', and `semantic-ctxt-current-assignment'
624 for details on the return value."
625 (or (semantic-ctxt-current-symbol)
626 (semantic-ctxt-current-function)
627 (semantic-ctxt-current-assignment)))
629 (define-overloadable-function semantic-ctxt-current-class-list
(&optional point
)
630 "Return a list of tag classes that are allowed at POINT.
631 If POINT is nil, the current buffer location is used.
632 For example, in Emacs Lisp, the symbol after a ( is most likely
633 a function. In a makefile, symbols after a : are rules, and symbols
634 after a $( are variables.")
636 (defun semantic-ctxt-current-class-list-default (&optional point
)
637 "Return a list of tag classes that are allowed at POINT.
638 Assume a functional typed language. Uses very simple rules."
640 (if point
(goto-char point
))
642 (let ((tag (semantic-current-tag)))
644 (cond ((semantic-tag-of-class-p tag
'function
)
645 '(function variable type
))
646 ((or (semantic-tag-of-class-p tag
'type
)
647 (semantic-tag-of-class-p tag
'variable
))
654 (define-overloadable-function semantic-ctxt-current-mode
(&optional point
)
655 "Return the major mode active at POINT.
656 POINT defaults to the value of point in current buffer.
657 You should override this function in multiple mode buffers to
658 determine which major mode apply at point.")
660 (defun semantic-ctxt-current-mode-default (&optional point
)
661 "Return the major mode active at POINT.
662 POINT defaults to the value of point in current buffer.
663 This default implementation returns the current major mode."
668 ;; Scoped types are types that the current code would have access to.
669 ;; The come from the global namespace or from special commands such as "using"
670 (define-overloadable-function semantic-ctxt-scoped-types
(&optional point
)
671 "Return a list of type names currently in scope at POINT.
672 The return value can be a mixed list of either strings (names of
673 types that are in scope) or actual tags (type declared locally
674 that may or may not have a name.)")
676 (defun semantic-ctxt-scoped-types-default (&optional point
)
677 "Return a list of scoped types by name for the current context at POINT.
678 This is very different for various languages, and does nothing unless
682 (define-overloadable-function semantic-ctxt-imported-packages
(&optional point
)
683 "Return a list of package tags or names which are being imported at POINT.
684 The return value is a list of strings which are package names
685 that are implied in code. Thus a C++ symbol:
687 where there is a statement such as:
689 means that the first symbol might be:
693 (provide 'semantic
/ctxt
)
696 ;; generated-autoload-file: "loaddefs.el"
697 ;; generated-autoload-load-name: "semantic/ctxt"
700 ;;; semantic/ctxt.el ends here