1 ;;; semantic-ctxt.el --- Context calculations for Semantic tools.
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Eric M. Ludlam
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; X-RCS: $Id: semantic-ctxt.el,v 1.43 2007/02/19 13:48:19 zappo Exp $
9 ;; This file is not part of GNU Emacs.
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)
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.
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.
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.
32 ;; This library provides the hooks needed for a language to specify how
33 ;; the current context is calculated.
36 (eval-when-compile (require 'semanticdb))
41 (defvar semantic-command-separation-character
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)
47 (defvar semantic-function-argument-separation-character
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)
53 ;;; Local variable parsing.
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))))
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-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'.")
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-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
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
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")
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 ())))))
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)))
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))
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
188 (save-excursion (semantic-end-of-context) (point))
194 ;; Modify the tags in place.
196 (semantic--tag-put-property (car vars2) :filename (buffer-file-name))
197 (setq vars2 (cdr vars2)))
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
209 (if point (goto-char point))
210 (let* ((case-fold-search semantic-case-fold)
211 (args (:override-with-args ()))
213 ;; Convert unsafe arguments to the right thing.
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))
223 (semantic--tag-put-property
224 (semantic-tag-new-variable arg nil nil)
225 :filename (buffer-file-name)))
227 (error "Unknown parameter element %S" arg)))
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))))
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.")
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:
249 - LOCAL-ARGUMENTS is collected by `semantic-get-local-arguments'.
250 - LOCAL-VARIABLES is collected by `semantic-get-local-variables'."
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)))))
257 ;;; Local context parsing
259 ;; Context parsing assumes a series of language independent commonalities.
260 ;; These terms are used to describe those contexts:
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.
270 (define-overload semantic-end-of-command ()
271 "Move to the end of the current command.
272 Be default, uses `semantic-command-separation-character'.")
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
282 (if (re-search-forward (regexp-quote semantic-command-separation-character)
285 ;; If there wasn't a command after this, we are the last
286 ;; command, and we are incomplete.
287 (goto-char (point-max)))))))
289 (define-overload semantic-beginning-of-command ()
290 "Move to the beginning of the current command.
291 Be default, uses `semantic-command-separation-character'.")
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)
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")
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)))
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)))
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)))
324 (defmacro semantic-with-buffer-narrowed-to-command (&rest body)
325 "Execute BODY with the buffer narrowed to the current command."
327 (semantic-narrow-to-command)
329 (put 'semantic-with-buffer-narrowed-to-command 'lisp-indent-function 0)
330 (add-hook 'edebug-setup-hook
332 (def-edebug-spec semantic-with-buffer-narrowed-to-command
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.")
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
348 (fieldsep (concat "\\(" fieldsep1 "\\)\\(\\w\\|\\s_\\)"))
349 (case-fold-search semantic-case-fold)
352 (with-syntax-table semantic-lex-syntax-table
354 (if (looking-at "\\w\\|\\s_")
356 ;; Not on a sym, are we at a separator char with no field
358 (when (or (looking-at fieldsep1)
360 (and (condition-case nil
361 (progn (forward-sexp -1)
365 (looking-at fieldsep1))))
366 (setq symlist (list ""))
368 ;; Skip array expressions.
369 (while (looking-at "\\s(") (forward-sexp -1))
373 (while (save-excursion
375 (looking-at "\\w\\|\\s_"))
376 ;; We have a symbol.. Do symbol things
378 (setq symlist (cons (buffer-substring-no-properties (point) end)
380 ;; Skip the next syntactic expression backwards, then go forwards.
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
387 (if (<= cp (point)) (error nil)))
388 (if (looking-at fieldsep)
391 ;; Skip array expressions.
392 (while (looking-at "\\s(") (forward-sexp -1))
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.")
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
412 (semantic-with-buffer-narrowed-to-command
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
420 (semantic-ctxt-current-symbol)))
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.")
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
433 (if point (goto-char point))
434 (let ((case-fold-search semantic-case-fold))
435 (with-syntax-table semantic-lex-syntax-table
437 (semantic-up-context)
438 (when (looking-at "(")
439 (semantic-ctxt-current-symbol))))
442 (define-overload semantic-ctxt-current-argument (&optional point)
443 "Return the index of the argument position the cursor is on at POINT.")
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)
453 ;; Only get the current arg index if we are in function args.
456 (semantic-up-context)
457 (while (re-search-forward
458 (regexp-quote semantic-function-argument-separation-character)
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)))
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.")
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."
484 (if point (goto-char point))
486 (let ((tag (semantic-current-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))
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.")
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."
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.)")
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
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.
528 (provide 'semantic-ctxt)
530 ;;; semantic-ctxt.el ends here