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.
34 (require 'semantic
/find
)
37 (defvar semantic-command-separation-character
39 "String which indicates the end of a command.
40 Used for identifying the end of a single command.")
41 (make-variable-buffer-local 'semantic-command-separation-character
)
43 (defvar semantic-function-argument-separation-character
45 "String which indicates the end of an argument.
46 Used for identifying arguments to functions.")
47 (make-variable-buffer-local 'semantic-function-argument-separation-character
)
51 ;; These context are nested blocks of code, such as code in an
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 (if point
(goto-char point
))
63 (let ((nar (semantic-current-tag-of-class (or bounds-type
'function
))))
65 (semantic-with-buffer-narrowed-to-tag nar
(:override-with-args
()))
67 (error "No context of type %s to advance in" bounds-type
))
68 (:override-with-args
()))))
70 (defun semantic-up-context-default ()
71 "Move the point up and out one context level.
72 Works with languages that use parenthetical grouping."
73 ;; By default, assume that the language uses some form of parenthetical
74 ;; do dads for their context.
81 (define-overloadable-function semantic-beginning-of-context
(&optional point
)
82 "Move POINT to the beginning of the current context.
83 Return non-nil if there is no upper context.
84 The default behavior uses `semantic-up-context'.")
86 (defun semantic-beginning-of-context-default (&optional point
)
87 "Move POINT to the beginning of the current context via parenthisis.
88 Return non-nil if there is no upper context."
89 (if point
(goto-char point
))
90 (if (semantic-up-context)
95 (define-overloadable-function semantic-end-of-context
(&optional point
)
96 "Move POINT to the end of the current context.
97 Return non-nil if there is no upper context.
98 Be default, this uses `semantic-up-context', and assumes parenthetical
101 (defun semantic-end-of-context-default (&optional point
)
102 "Move POINT to the end of the current context via parenthisis.
103 Return non-nil if there is no upper context."
104 (if point
(goto-char point
))
105 (let ((start (point)))
106 (if (semantic-up-context)
108 ;; Go over the list, and back over the end parenthisis.
114 ;; If an error occurs, get the current tag from the cache,
115 ;; and just go to the end of that. Make sure we end up at least
116 ;; where start was so parse-region type calls work.
117 (if (semantic-current-tag)
119 (goto-char (semantic-tag-end (semantic-current-tag)))
120 (when (< (point) start
)
126 (defun semantic-narrow-to-context ()
127 "Narrow the buffer to the extent of the current context."
130 (if (semantic-beginning-of-context)
134 (if (semantic-end-of-context)
137 (if (and b e
) (narrow-to-region b e
))))
139 (defmacro semantic-with-buffer-narrowed-to-context
(&rest body
)
140 "Execute BODY with the buffer narrowed to the current context."
142 (semantic-narrow-to-context)
144 (put 'semantic-with-buffer-narrowed-to-context
'lisp-indent-function
0)
145 (add-hook 'edebug-setup-hook
147 (def-edebug-spec semantic-with-buffer-narrowed-to-context
153 (define-overloadable-function semantic-get-local-variables
(&optional point
)
154 "Get the local variables based on POINT's context.
155 Local variables are returned in Semantic tag format.
156 This can be overriden with `get-local-variables'."
157 ;; The working status is to let the parser work properly
158 (let ((semantic--progress-reporter
159 (make-progress-reporter (semantic-parser-working-message "Local")
162 (if point
(goto-char point
))
163 (let* ((semantic-working-type nil
)
164 ;; Disable parsing messages
165 (case-fold-search semantic-case-fold
))
166 (:override-with-args
())))))
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 (not (semantic-parse-tree-unparseable-p)))
176 (let ((vars (semantic-get-cache-data 'get-local-variables
)))
179 ;;(message "Found cached vars.")
182 ;; We want nothing to do with funny syntaxing while doing this.
183 (semantic-unmatched-syntax-hook nil
)
185 (firstusefulstart nil
)
187 (while (not (semantic-up-context (point) 'function
))
189 (setq firstusefulstart
(point)))
193 ;; Note to self: semantic-parse-region returns cooked
194 ;; but unlinked tags. File information is lost here
195 ;; and is added next.
196 (append (semantic-parse-region
198 (save-excursion (semantic-end-of-context) (point))
203 ;; Modify the tags in place.
206 (semantic--tag-put-property (car vars2
) :filename
(buffer-file-name))
207 (setq vars2
(cdr vars2
)))
208 ;; Hash our value into the first context that produced useful results.
209 (when (and vars firstusefulstart
)
210 (let ((end (save-excursion
211 (goto-char firstusefulstart
)
213 (unless (semantic-end-of-context)
215 ;;(message "Caching values %d->%d." firstusefulstart end)
216 (semantic-cache-data-to-buffer
217 (current-buffer) firstusefulstart
219 ;; If the end-of-context fails,
220 ;; just use our cursor starting
223 vars
'get-local-variables
'exit-cache-zone
))
228 (define-overloadable-function semantic-get-local-arguments
(&optional point
)
229 "Get arguments (variables) from the current context at POINT.
230 Parameters are available if the point is in a function or method.
231 Return a list of tags unlinked from the originating buffer.
232 Arguments are obtained by overriding `get-local-arguments', or by the
233 default function `semantic-get-local-arguments-default'. This, must
234 return a list of tags, or a list of strings that will be converted to
237 (if point
(goto-char point
))
238 (let* ((case-fold-search semantic-case-fold
)
239 (args (:override-with-args
()))
241 ;; Convert unsafe arguments to the right thing.
246 ((semantic-tag-p arg
)
247 ;; Return a copy of tag without overlay.
248 ;; The overlay is preserved.
249 (semantic-tag-copy arg nil t
))
251 (semantic--tag-put-property
252 (semantic-tag-new-variable arg nil nil
)
253 :filename
(buffer-file-name)))
255 (error "Unknown parameter element %S" arg
)))
259 (defun semantic-get-local-arguments-default ()
260 "Get arguments (variables) from the current context.
261 Parameters are available if the point is in a function or method."
262 (let ((tag (semantic-current-tag)))
263 (if (and tag
(semantic-tag-of-class-p tag
'function
))
264 (semantic-tag-function-arguments tag
))))
266 (define-overloadable-function semantic-get-all-local-variables
(&optional point
)
267 "Get all local variables for this context, and parent contexts.
268 Local variables are returned in Semantic tag format.
269 Be default, this gets local variables, and local arguments.
270 Optional argument POINT is the location to start getting the variables from.")
272 (defun semantic-get-all-local-variables-default (&optional point
)
273 "Get all local variables for this context.
274 Optional argument POINT is the location to start getting the variables from.
275 That is a cons (LOCAL-ARGUMENTS . LOCAL-VARIABLES) where:
277 - LOCAL-ARGUMENTS is collected by `semantic-get-local-arguments'.
278 - LOCAL-VARIABLES is collected by `semantic-get-local-variables'."
280 (if point
(goto-char point
))
281 (let ((case-fold-search semantic-case-fold
))
282 (append (semantic-get-local-arguments)
283 (semantic-get-local-variables)))))
285 ;;; Local context parsing
287 ;; Context parsing assumes a series of language independent commonalities.
288 ;; These terms are used to describe those contexts:
290 ;; command - One command in the language.
291 ;; symbol - The symbol the cursor is on.
292 ;; This would include a series of type/field when applicable.
293 ;; assignment - The variable currently being assigned to
294 ;; function - The function call the cursor is on/in
295 ;; argument - The index to the argument the cursor is on.
298 (define-overloadable-function semantic-end-of-command
()
299 "Move to the end of the current command.
300 Be default, uses `semantic-command-separation-character'.")
302 (defun semantic-end-of-command-default ()
303 "Move to the end of the current command.
304 Depends on `semantic-command-separation-character' to find the
305 beginning and end of a command."
306 (semantic-with-buffer-narrowed-to-context
307 (let ((case-fold-search semantic-case-fold
))
308 (with-syntax-table semantic-lex-syntax-table
310 (if (re-search-forward (regexp-quote semantic-command-separation-character
)
313 ;; If there wasn't a command after this, we are the last
314 ;; command, and we are incomplete.
315 (goto-char (point-max)))))))
317 (define-overloadable-function semantic-beginning-of-command
()
318 "Move to the beginning of the current command.
319 Be default, uses `semantic-command-separation-character'.")
321 (defun semantic-beginning-of-command-default ()
322 "Move to the beginning of the current command.
323 Depends on `semantic-command-separation-character' to find the
324 beginning and end of a command."
325 (semantic-with-buffer-narrowed-to-context
326 (with-syntax-table semantic-lex-syntax-table
327 (let ((case-fold-search semantic-case-fold
))
328 (skip-chars-backward semantic-command-separation-character
)
329 (if (re-search-backward (regexp-quote semantic-command-separation-character
)
331 (goto-char (match-end 0))
332 ;; If there wasn't a command after this, we are the last
333 ;; command, and we are incomplete.
334 (goto-char (point-min)))
335 (skip-chars-forward " \t\n")
339 (defsubst semantic-point-at-beginning-of-command
()
340 "Return the point at the beginning of the current command."
341 (save-excursion (semantic-beginning-of-command) (point)))
343 (defsubst semantic-point-at-end-of-command
()
344 "Return the point at the beginning of the current command."
345 (save-excursion (semantic-end-of-command) (point)))
347 (defsubst semantic-narrow-to-command
()
348 "Narrow the current buffer to the current command."
349 (narrow-to-region (semantic-point-at-beginning-of-command)
350 (semantic-point-at-end-of-command)))
352 (defmacro semantic-with-buffer-narrowed-to-command
(&rest body
)
353 "Execute BODY with the buffer narrowed to the current command."
355 (semantic-narrow-to-command)
357 (put 'semantic-with-buffer-narrowed-to-command
'lisp-indent-function
0)
358 (add-hook 'edebug-setup-hook
360 (def-edebug-spec semantic-with-buffer-narrowed-to-command
364 (define-overloadable-function semantic-ctxt-current-symbol
(&optional point
)
365 "Return the current symbol the cursor is on at POINT in a list.
366 The symbol includes all logical parts of a complex reference.
367 For example, in C the statement:
370 Would be object `this' calling method `that' which returns some structure
371 whose field `entry' is being reference. In this case, this function
372 would return the list:
373 ( \"this\" \"that\" \"entry\" )")
375 (defun semantic-ctxt-current-symbol-default (&optional point
)
376 "Return the current symbol the cursor is on at POINT in a list.
377 This will include a list of type/field names when applicable.
378 Depends on `semantic-type-relation-separator-character'."
380 (if point
(goto-char point
))
381 (let* ((fieldsep1 (mapconcat (lambda (a) (regexp-quote a
))
382 semantic-type-relation-separator-character
384 ;; NOTE: The [ \n] expression below should used \\s-, but that
385 ;; doesn't work in C since \n means end-of-comment, and isn't
386 ;; really whitespace.
387 (fieldsep (concat "[ \t\n\r]*\\(" fieldsep1
"\\)[ \t\n\r]*\\(\\w\\|\\s_\\)"))
388 (case-fold-search semantic-case-fold
)
391 (with-syntax-table semantic-lex-syntax-table
393 (cond ((looking-at "\\w\\|\\s_")
394 ;; In the middle of a symbol, move to the end.
396 ((looking-at fieldsep1
)
397 ;; We are in a find spot.. do nothing.
401 (and (condition-case nil
402 (progn (forward-sexp -
1)
406 (looking-at fieldsep1
)))
407 (setq symlist
(list ""))
409 ;; Skip array expressions.
410 (while (looking-at "\\s(") (forward-sexp -
1))
413 ;; Set our end point.
416 ;; Now that we have gotten started, lets do the rest.
418 (while (save-excursion
420 (looking-at "\\w\\|\\s_"))
421 ;; We have a symbol.. Do symbol things
423 (setq symlist
(cons (buffer-substring-no-properties (point) end
)
425 ;; Skip the next syntactic expression backwards, then go forwards.
429 ;; If we end up at the same place we started, we are at the
430 ;; beginning of a buffer, or narrowed to a command and
432 (if (<= cp
(point)) (error nil
)))
433 (if (looking-at fieldsep
)
436 ;; Skip array expressions.
437 (while (and (looking-at "\\s(") (not (bobp)))
447 (define-overloadable-function semantic-ctxt-current-symbol-and-bounds
(&optional point
)
448 "Return the current symbol and bounds the cursor is on at POINT.
449 The symbol should be the same as returned by `semantic-ctxt-current-symbol'.
450 Return (PREFIX ENDSYM BOUNDS).")
452 (defun semantic-ctxt-current-symbol-and-bounds-default (&optional point
)
453 "Return the current symbol and bounds the cursor is on at POINT.
454 Uses `semantic-ctxt-current-symbol' to calculate the symbol.
455 Return (PREFIX ENDSYM BOUNDS)."
457 (when point
(goto-char (point)))
458 (let* ((prefix (semantic-ctxt-current-symbol))
459 (endsym (car (reverse prefix
)))
460 ;; @todo - Can we get this data direct from ctxt-current-symbol?
461 (bounds (save-excursion
462 (cond ((string= endsym
"")
463 (cons (point) (point))
465 ((and prefix
(looking-at endsym
))
473 (cons (progn (forward-sexp -
1) (point))
474 (progn (forward-sexp 1) (point)))
478 (list prefix endsym bounds
))))
480 (define-overloadable-function semantic-ctxt-current-assignment
(&optional point
)
481 "Return the current assignment near the cursor at POINT.
482 Return a list as per `semantic-ctxt-current-symbol'.
483 Return nil if there is nothing relevant.")
485 (defun semantic-ctxt-current-assignment-default (&optional point
)
486 "Return the current assignment near the cursor at POINT.
487 By default, assume that \"=\" indicates an assignment."
488 (if point
(goto-char point
))
489 (let ((case-fold-search semantic-case-fold
))
490 (with-syntax-table semantic-lex-syntax-table
492 (semantic-with-buffer-narrowed-to-command
494 (skip-chars-forward " \t=")
495 (condition-case nil
(forward-char 1) (error nil
))
496 (re-search-backward "[^=]=\\([^=]\\|$\\)")
497 ;; We are at an equals sign. Go backwards a sexp, and
498 ;; we'll have the variable. Otherwise we threw an error
500 (semantic-ctxt-current-symbol)))
503 (define-overloadable-function semantic-ctxt-current-function
(&optional point
)
504 "Return the current function call the cursor is in at POINT.
505 The function returned is the one accepting the arguments that
506 the cursor is currently in. It will not return function symbol if the
507 cursor is on the text representing that function.")
509 (defun semantic-ctxt-current-function-default (&optional point
)
510 "Return the current function call the cursor is in at POINT.
511 The call will be identifed for C like langauges with the form
513 (if point
(goto-char point
))
514 (let ((case-fold-search semantic-case-fold
))
515 (with-syntax-table semantic-lex-syntax-table
517 (semantic-up-context)
518 (when (looking-at "(")
519 (semantic-ctxt-current-symbol))))
522 (define-overloadable-function semantic-ctxt-current-argument
(&optional point
)
523 "Return the index of the argument position the cursor is on at POINT.")
525 (defun semantic-ctxt-current-argument-default (&optional point
)
526 "Return the index of the argument the cursor is on at POINT.
527 Depends on `semantic-function-argument-separation-character'."
528 (if point
(goto-char point
))
529 (let ((case-fold-search semantic-case-fold
))
530 (with-syntax-table semantic-lex-syntax-table
531 (when (semantic-ctxt-current-function)
533 ;; Only get the current arg index if we are in function args.
536 (semantic-up-context)
537 (while (re-search-forward
538 (regexp-quote semantic-function-argument-separation-character
)
543 (defun semantic-ctxt-current-thing ()
544 "Calculate a thing identified by the current cursor position.
545 Calls previously defined `semantic-ctxt-current-...' calls until something
546 gets a match. See `semantic-ctxt-current-symbol',
547 `semantic-ctxt-current-function', and `semantic-ctxt-current-assignment'
548 for details on the return value."
549 (or (semantic-ctxt-current-symbol)
550 (semantic-ctxt-current-function)
551 (semantic-ctxt-current-assignment)))
553 (define-overloadable-function semantic-ctxt-current-class-list
(&optional point
)
554 "Return a list of tag classes that are allowed at POINT.
555 If POINT is nil, the current buffer location is used.
556 For example, in Emacs Lisp, the symbol after a ( is most likely
557 a function. In a makefile, symbols after a : are rules, and symbols
558 after a $( are variables.")
560 (defun semantic-ctxt-current-class-list-default (&optional point
)
561 "Return a list of tag classes that are allowed at POINT.
562 Assume a functional typed language. Uses very simple rules."
564 (if point
(goto-char point
))
566 (let ((tag (semantic-current-tag)))
568 (cond ((semantic-tag-of-class-p tag
'function
)
569 '(function variable type
))
570 ((or (semantic-tag-of-class-p tag
'type
)
571 (semantic-tag-of-class-p tag
'variable
))
577 (define-overloadable-function semantic-ctxt-current-mode
(&optional point
)
578 "Return the major mode active at POINT.
579 POINT defaults to the value of point in current buffer.
580 You should override this function in multiple mode buffers to
581 determine which major mode apply at point.")
583 (defun semantic-ctxt-current-mode-default (&optional point
)
584 "Return the major mode active at POINT.
585 POINT defaults to the value of point in current buffer.
586 This default implementation returns the current major mode."
591 ;; Scoped types are types that the current code would have access to.
592 ;; The come from the global namespace or from special commands such as "using"
593 (define-overloadable-function semantic-ctxt-scoped-types
(&optional point
)
594 "Return a list of type names currently in scope at POINT.
595 The return value can be a mixed list of either strings (names of
596 types that are in scope) or actual tags (type declared locally
597 that may or may not have a name.)")
599 (defun semantic-ctxt-scoped-types-default (&optional point
)
600 "Return a list of scoped types by name for the current context at POINT.
601 This is very different for various languages, and does nothing unless
603 (if point
(goto-char point
))
604 (let ((case-fold-search semantic-case-fold
))
605 ;; We need to look at TYPES within the bounds of locally parse arguments.
606 ;; C needs to find using statements and the like too. Bleh.
610 (provide 'semantic
/ctxt
)
612 ;;; semantic/ctxt.el ends here