Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / cedet / semantic / ctxt.el
CommitLineData
aa8724ae 1;;; semantic/ctxt.el --- Context calculations for Semantic tools.
1bd95535 2
acaf905b 3;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
1bd95535
CY
4
5;; Author: Eric M. Ludlam <zappo@gnu.org>
6;; Keywords: syntax
7
8;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23;;; Commentary:
24;;
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.
28;;
29;; This library provides the hooks needed for a language to specify how
30;; the current context is calculated.
31;;
32(require 'semantic)
1bd95535
CY
33
34;;; Code:
35(defvar semantic-command-separation-character
36 ";"
37 "String which indicates the end of a command.
38Used for identifying the end of a single command.")
39(make-variable-buffer-local 'semantic-command-separation-character)
40
41(defvar semantic-function-argument-separation-character
42 ","
43 "String which indicates the end of an argument.
44Used for identifying arguments to functions.")
45(make-variable-buffer-local 'semantic-function-argument-separation-character)
46
47;;; Local Contexts
48;;
49;; These context are nested blocks of code, such as code in an
50;; if clause
3d9d8486
CY
51(declare-function semantic-current-tag-of-class "semantic/find")
52
1bd95535
CY
53(define-overloadable-function semantic-up-context (&optional point bounds-type)
54 "Move point up one context from POINT.
55Return non-nil if there are no more context levels.
56Overloaded functions using `up-context' take no parameters.
57BOUNDS-TYPE is a symbol representing a tag class to restrict
58movement to. If this is nil, 'function is used.
59This will find the smallest tag of that class (function, variable,
60type, etc) and make sure non-nil is returned if you cannot
61go up past the bounds of that tag."
3d9d8486 62 (require 'semantic/find)
1bd95535
CY
63 (if point (goto-char point))
64 (let ((nar (semantic-current-tag-of-class (or bounds-type 'function))))
65 (if nar
66 (semantic-with-buffer-narrowed-to-tag nar (:override-with-args ()))
67 (when bounds-type
68 (error "No context of type %s to advance in" bounds-type))
69 (:override-with-args ()))))
70
71(defun semantic-up-context-default ()
72 "Move the point up and out one context level.
73Works with languages that use parenthetical grouping."
74 ;; By default, assume that the language uses some form of parenthetical
75 ;; do dads for their context.
76 (condition-case nil
77 (progn
78 (up-list -1)
79 nil)
80 (error t)))
81
82(define-overloadable-function semantic-beginning-of-context (&optional point)
83 "Move POINT to the beginning of the current context.
84Return non-nil if there is no upper context.
85The default behavior uses `semantic-up-context'.")
86
87(defun semantic-beginning-of-context-default (&optional point)
9bf6c65c 88 "Move POINT to the beginning of the current context via parenthesis.
1bd95535
CY
89Return non-nil if there is no upper context."
90 (if point (goto-char point))
91 (if (semantic-up-context)
92 t
93 (forward-char 1)
94 nil))
95
96(define-overloadable-function semantic-end-of-context (&optional point)
97 "Move POINT to the end of the current context.
98Return non-nil if there is no upper context.
99Be default, this uses `semantic-up-context', and assumes parenthetical
100block delimiters.")
101
102(defun semantic-end-of-context-default (&optional point)
9bf6c65c 103 "Move POINT to the end of the current context via parenthesis.
1bd95535
CY
104Return non-nil if there is no upper context."
105 (if point (goto-char point))
106 (let ((start (point)))
107 (if (semantic-up-context)
108 t
07a79ce4 109 ;; Go over the list, and back over the end parenthesis.
1bd95535
CY
110 (condition-case nil
111 (progn
112 (forward-sexp 1)
113 (forward-char -1))
114 (error
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)
119 (progn
120 (goto-char (semantic-tag-end (semantic-current-tag)))
121 (when (< (point) start)
122 (goto-char start)))
123 (goto-char start))
124 t)))
125 nil))
126
127(defun semantic-narrow-to-context ()
128 "Narrow the buffer to the extent of the current context."
129 (let (b e)
130 (save-excursion
131 (if (semantic-beginning-of-context)
132 nil
133 (setq b (point))))
134 (save-excursion
135 (if (semantic-end-of-context)
136 nil
137 (setq e (point))))
138 (if (and b e) (narrow-to-region b e))))
139
140(defmacro semantic-with-buffer-narrowed-to-context (&rest body)
141 "Execute BODY with the buffer narrowed to the current context."
142 `(save-restriction
143 (semantic-narrow-to-context)
144 ,@body))
145(put 'semantic-with-buffer-narrowed-to-context 'lisp-indent-function 0)
146(add-hook 'edebug-setup-hook
147 (lambda ()
148 (def-edebug-spec semantic-with-buffer-narrowed-to-context
149 (def-body))))
150
151;;; Local Variables
152;;
153;;
154(define-overloadable-function semantic-get-local-variables (&optional point)
155 "Get the local variables based on POINT's context.
156Local variables are returned in Semantic tag format.
0b381c7e 157This can be overridden with `get-local-variables'."
9bf7dc8a
CY
158 ;; Disable parsing messages
159 (let ((semantic--progress-reporter nil))
aa8724ae
CY
160 (save-excursion
161 (if point (goto-char point))
9bf7dc8a 162 (let* ((case-fold-search semantic-case-fold))
aa8724ae 163 (:override-with-args ())))))
1bd95535
CY
164
165(defun semantic-get-local-variables-default ()
166 "Get local values from a specific context.
167Uses the bovinator with the special top-symbol `bovine-inner-scope'
168to 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 (not (semantic-parse-tree-unparseable-p)))
173 (let ((vars (semantic-get-cache-data 'get-local-variables)))
174 (if vars
175 (progn
176 ;;(message "Found cached vars.")
177 vars)
178 (let ((vars2 nil)
179 ;; We want nothing to do with funny syntaxing while doing this.
180 (semantic-unmatched-syntax-hook nil)
181 (start (point))
182 (firstusefulstart nil)
183 )
184 (while (not (semantic-up-context (point) 'function))
185 (when (not vars)
186 (setq firstusefulstart (point)))
187 (save-excursion
188 (forward-char 1)
189 (setq vars
190 ;; Note to self: semantic-parse-region returns cooked
191 ;; but unlinked tags. File information is lost here
192 ;; and is added next.
193 (append (semantic-parse-region
194 (point)
195 (save-excursion (semantic-end-of-context) (point))
196 'bovine-inner-scope
197 nil
198 t)
199 vars))))
200 ;; Modify the tags in place.
201 (setq vars2 vars)
202 (while vars2
203 (semantic--tag-put-property (car vars2) :filename (buffer-file-name))
204 (setq vars2 (cdr vars2)))
205 ;; Hash our value into the first context that produced useful results.
206 (when (and vars firstusefulstart)
207 (let ((end (save-excursion
208 (goto-char firstusefulstart)
209 (save-excursion
210 (unless (semantic-end-of-context)
211 (point))))))
212 ;;(message "Caching values %d->%d." firstusefulstart end)
213 (semantic-cache-data-to-buffer
214 (current-buffer) firstusefulstart
215 (or end
216 ;; If the end-of-context fails,
217 ;; just use our cursor starting
218 ;; position.
219 start)
220 vars 'get-local-variables 'exit-cache-zone))
221 )
222 ;; Return our list.
223 vars)))))
224
225(define-overloadable-function semantic-get-local-arguments (&optional point)
226 "Get arguments (variables) from the current context at POINT.
227Parameters are available if the point is in a function or method.
228Return a list of tags unlinked from the originating buffer.
229Arguments are obtained by overriding `get-local-arguments', or by the
230default function `semantic-get-local-arguments-default'. This, must
231return a list of tags, or a list of strings that will be converted to
232tags."
233 (save-excursion
234 (if point (goto-char point))
235 (let* ((case-fold-search semantic-case-fold)
236 (args (:override-with-args ()))
237 arg tags)
238 ;; Convert unsafe arguments to the right thing.
239 (while args
240 (setq arg (car args)
241 args (cdr args)
242 tags (cons (cond
243 ((semantic-tag-p arg)
244 ;; Return a copy of tag without overlay.
245 ;; The overlay is preserved.
246 (semantic-tag-copy arg nil t))
247 ((stringp arg)
248 (semantic--tag-put-property
249 (semantic-tag-new-variable arg nil nil)
250 :filename (buffer-file-name)))
251 (t
252 (error "Unknown parameter element %S" arg)))
253 tags)))
254 (nreverse tags))))
255
256(defun semantic-get-local-arguments-default ()
257 "Get arguments (variables) from the current context.
258Parameters are available if the point is in a function or method."
259 (let ((tag (semantic-current-tag)))
260 (if (and tag (semantic-tag-of-class-p tag 'function))
261 (semantic-tag-function-arguments tag))))
262
263(define-overloadable-function semantic-get-all-local-variables (&optional point)
264 "Get all local variables for this context, and parent contexts.
265Local variables are returned in Semantic tag format.
266Be default, this gets local variables, and local arguments.
267Optional argument POINT is the location to start getting the variables from.")
268
269(defun semantic-get-all-local-variables-default (&optional point)
270 "Get all local variables for this context.
271Optional argument POINT is the location to start getting the variables from.
272That is a cons (LOCAL-ARGUMENTS . LOCAL-VARIABLES) where:
273
274- LOCAL-ARGUMENTS is collected by `semantic-get-local-arguments'.
275- LOCAL-VARIABLES is collected by `semantic-get-local-variables'."
276 (save-excursion
277 (if point (goto-char point))
278 (let ((case-fold-search semantic-case-fold))
279 (append (semantic-get-local-arguments)
280 (semantic-get-local-variables)))))
281
282;;; Local context parsing
283;;
284;; Context parsing assumes a series of language independent commonalities.
285;; These terms are used to describe those contexts:
286;;
287;; command - One command in the language.
288;; symbol - The symbol the cursor is on.
289;; This would include a series of type/field when applicable.
290;; assignment - The variable currently being assigned to
291;; function - The function call the cursor is on/in
292;; argument - The index to the argument the cursor is on.
293;;
294;;
295(define-overloadable-function semantic-end-of-command ()
296 "Move to the end of the current command.
297Be default, uses `semantic-command-separation-character'.")
298
299(defun semantic-end-of-command-default ()
300 "Move to the end of the current command.
301Depends on `semantic-command-separation-character' to find the
302beginning and end of a command."
303 (semantic-with-buffer-narrowed-to-context
304 (let ((case-fold-search semantic-case-fold))
305 (with-syntax-table semantic-lex-syntax-table
306
307 (if (re-search-forward (regexp-quote semantic-command-separation-character)
308 nil t)
309 (forward-char -1)
310 ;; If there wasn't a command after this, we are the last
311 ;; command, and we are incomplete.
312 (goto-char (point-max)))))))
313
314(define-overloadable-function semantic-beginning-of-command ()
315 "Move to the beginning of the current command.
316Be default, uses `semantic-command-separation-character'.")
317
318(defun semantic-beginning-of-command-default ()
319 "Move to the beginning of the current command.
320Depends on `semantic-command-separation-character' to find the
321beginning and end of a command."
322 (semantic-with-buffer-narrowed-to-context
323 (with-syntax-table semantic-lex-syntax-table
324 (let ((case-fold-search semantic-case-fold))
325 (skip-chars-backward semantic-command-separation-character)
326 (if (re-search-backward (regexp-quote semantic-command-separation-character)
327 nil t)
328 (goto-char (match-end 0))
329 ;; If there wasn't a command after this, we are the last
330 ;; command, and we are incomplete.
331 (goto-char (point-min)))
332 (skip-chars-forward " \t\n")
333 ))))
334
335
336(defsubst semantic-point-at-beginning-of-command ()
337 "Return the point at the beginning of the current command."
338 (save-excursion (semantic-beginning-of-command) (point)))
339
340(defsubst semantic-point-at-end-of-command ()
341 "Return the point at the beginning of the current command."
342 (save-excursion (semantic-end-of-command) (point)))
343
344(defsubst semantic-narrow-to-command ()
345 "Narrow the current buffer to the current command."
346 (narrow-to-region (semantic-point-at-beginning-of-command)
347 (semantic-point-at-end-of-command)))
348
349(defmacro semantic-with-buffer-narrowed-to-command (&rest body)
350 "Execute BODY with the buffer narrowed to the current command."
351 `(save-restriction
352 (semantic-narrow-to-command)
353 ,@body))
354(put 'semantic-with-buffer-narrowed-to-command 'lisp-indent-function 0)
355(add-hook 'edebug-setup-hook
356 (lambda ()
357 (def-edebug-spec semantic-with-buffer-narrowed-to-command
358 (def-body))))
359
360
361(define-overloadable-function semantic-ctxt-current-symbol (&optional point)
362 "Return the current symbol the cursor is on at POINT in a list.
363The symbol includes all logical parts of a complex reference.
364For example, in C the statement:
365 this.that().entry
366
367Would be object `this' calling method `that' which returns some structure
368whose field `entry' is being reference. In this case, this function
369would return the list:
370 ( \"this\" \"that\" \"entry\" )")
371
372(defun semantic-ctxt-current-symbol-default (&optional point)
373 "Return the current symbol the cursor is on at POINT in a list.
374This will include a list of type/field names when applicable.
375Depends on `semantic-type-relation-separator-character'."
376 (save-excursion
377 (if point (goto-char point))
378 (let* ((fieldsep1 (mapconcat (lambda (a) (regexp-quote a))
379 semantic-type-relation-separator-character
380 "\\|"))
381 ;; NOTE: The [ \n] expression below should used \\s-, but that
382 ;; doesn't work in C since \n means end-of-comment, and isn't
383 ;; really whitespace.
384 (fieldsep (concat "[ \t\n\r]*\\(" fieldsep1 "\\)[ \t\n\r]*\\(\\w\\|\\s_\\)"))
385 (case-fold-search semantic-case-fold)
386 (symlist nil)
387 end)
388 (with-syntax-table semantic-lex-syntax-table
389 (save-excursion
390 (cond ((looking-at "\\w\\|\\s_")
391 ;; In the middle of a symbol, move to the end.
392 (forward-sexp 1))
393 ((looking-at fieldsep1)
394 ;; We are in a find spot.. do nothing.
395 nil
396 )
397 ((save-excursion
398 (and (condition-case nil
399 (progn (forward-sexp -1)
400 (forward-sexp 1)
401 t)
402 (error nil))
403 (looking-at fieldsep1)))
404 (setq symlist (list ""))
405 (forward-sexp -1)
406 ;; Skip array expressions.
407 (while (looking-at "\\s(") (forward-sexp -1))
408 (forward-sexp 1))
409 )
410 ;; Set our end point.
411 (setq end (point))
412
c7015153 413 ;; Now that we have gotten started, let's do the rest.
1bd95535
CY
414 (condition-case nil
415 (while (save-excursion
416 (forward-char -1)
417 (looking-at "\\w\\|\\s_"))
418 ;; We have a symbol.. Do symbol things
419 (forward-sexp -1)
420 (setq symlist (cons (buffer-substring-no-properties (point) end)
421 symlist))
422 ;; Skip the next syntactic expression backwards, then go forwards.
423 (let ((cp (point)))
424 (forward-sexp -1)
425 (forward-sexp 1)
426 ;; If we end up at the same place we started, we are at the
427 ;; beginning of a buffer, or narrowed to a command and
428 ;; have to stop.
429 (if (<= cp (point)) (error nil)))
430 (if (looking-at fieldsep)
431 (progn
432 (forward-sexp -1)
433 ;; Skip array expressions.
434 (while (and (looking-at "\\s(") (not (bobp)))
435 (forward-sexp -1))
436 (forward-sexp 1)
437 (setq end (point)))
438 (error nil))
439 )
440 (error nil)))
441 symlist))))
442
443
444(define-overloadable-function semantic-ctxt-current-symbol-and-bounds (&optional point)
445 "Return the current symbol and bounds the cursor is on at POINT.
446The symbol should be the same as returned by `semantic-ctxt-current-symbol'.
447Return (PREFIX ENDSYM BOUNDS).")
448
449(defun semantic-ctxt-current-symbol-and-bounds-default (&optional point)
450 "Return the current symbol and bounds the cursor is on at POINT.
451Uses `semantic-ctxt-current-symbol' to calculate the symbol.
452Return (PREFIX ENDSYM BOUNDS)."
453 (save-excursion
454 (when point (goto-char (point)))
455 (let* ((prefix (semantic-ctxt-current-symbol))
456 (endsym (car (reverse prefix)))
457 ;; @todo - Can we get this data direct from ctxt-current-symbol?
458 (bounds (save-excursion
459 (cond ((string= endsym "")
460 (cons (point) (point))
461 )
462 ((and prefix (looking-at endsym))
463 (cons (point) (progn
464 (condition-case nil
465 (forward-sexp 1)
466 (error nil))
467 (point))))
468 (prefix
469 (condition-case nil
470 (cons (progn (forward-sexp -1) (point))
471 (progn (forward-sexp 1) (point)))
472 (error nil)))
473 (t nil))))
474 )
475 (list prefix endsym bounds))))
476
477(define-overloadable-function semantic-ctxt-current-assignment (&optional point)
478 "Return the current assignment near the cursor at POINT.
479Return a list as per `semantic-ctxt-current-symbol'.
480Return nil if there is nothing relevant.")
481
482(defun semantic-ctxt-current-assignment-default (&optional point)
483 "Return the current assignment near the cursor at POINT.
484By default, assume that \"=\" indicates an assignment."
485 (if point (goto-char point))
486 (let ((case-fold-search semantic-case-fold))
487 (with-syntax-table semantic-lex-syntax-table
488 (condition-case nil
489 (semantic-with-buffer-narrowed-to-command
490 (save-excursion
491 (skip-chars-forward " \t=")
492 (condition-case nil (forward-char 1) (error nil))
493 (re-search-backward "[^=]=\\([^=]\\|$\\)")
494 ;; We are at an equals sign. Go backwards a sexp, and
495 ;; we'll have the variable. Otherwise we threw an error
496 (forward-sexp -1)
497 (semantic-ctxt-current-symbol)))
498 (error nil)))))
499
500(define-overloadable-function semantic-ctxt-current-function (&optional point)
501 "Return the current function call the cursor is in at POINT.
502The function returned is the one accepting the arguments that
503the cursor is currently in. It will not return function symbol if the
504cursor is on the text representing that function.")
505
506(defun semantic-ctxt-current-function-default (&optional point)
507 "Return the current function call the cursor is in at POINT.
9bf6c65c 508The call will be identified for C like languages with the form
1bd95535
CY
509 NAME ( args ... )"
510 (if point (goto-char point))
511 (let ((case-fold-search semantic-case-fold))
512 (with-syntax-table semantic-lex-syntax-table
513 (save-excursion
514 (semantic-up-context)
515 (when (looking-at "(")
516 (semantic-ctxt-current-symbol))))
517 ))
518
519(define-overloadable-function semantic-ctxt-current-argument (&optional point)
520 "Return the index of the argument position the cursor is on at POINT.")
521
522(defun semantic-ctxt-current-argument-default (&optional point)
523 "Return the index of the argument the cursor is on at POINT.
524Depends on `semantic-function-argument-separation-character'."
525 (if point (goto-char point))
526 (let ((case-fold-search semantic-case-fold))
527 (with-syntax-table semantic-lex-syntax-table
528 (when (semantic-ctxt-current-function)
529 (save-excursion
530 ;; Only get the current arg index if we are in function args.
531 (let ((p (point))
532 (idx 1))
533 (semantic-up-context)
534 (while (re-search-forward
535 (regexp-quote semantic-function-argument-separation-character)
536 p t)
537 (setq idx (1+ idx)))
538 idx))))))
539
540(defun semantic-ctxt-current-thing ()
541 "Calculate a thing identified by the current cursor position.
542Calls previously defined `semantic-ctxt-current-...' calls until something
543gets a match. See `semantic-ctxt-current-symbol',
544`semantic-ctxt-current-function', and `semantic-ctxt-current-assignment'
545for details on the return value."
546 (or (semantic-ctxt-current-symbol)
547 (semantic-ctxt-current-function)
548 (semantic-ctxt-current-assignment)))
549
550(define-overloadable-function semantic-ctxt-current-class-list (&optional point)
551 "Return a list of tag classes that are allowed at POINT.
552If POINT is nil, the current buffer location is used.
553For example, in Emacs Lisp, the symbol after a ( is most likely
554a function. In a makefile, symbols after a : are rules, and symbols
555after a $( are variables.")
556
557(defun semantic-ctxt-current-class-list-default (&optional point)
558 "Return a list of tag classes that are allowed at POINT.
559Assume a functional typed language. Uses very simple rules."
560 (save-excursion
561 (if point (goto-char point))
562
563 (let ((tag (semantic-current-tag)))
564 (if tag
565 (cond ((semantic-tag-of-class-p tag 'function)
566 '(function variable type))
567 ((or (semantic-tag-of-class-p tag 'type)
568 (semantic-tag-of-class-p tag 'variable))
569 '(type))
570 (t nil))
571 '(type)
572 ))))
573
55b522b2 574;;;###autoload
1bd95535
CY
575(define-overloadable-function semantic-ctxt-current-mode (&optional point)
576 "Return the major mode active at POINT.
577POINT defaults to the value of point in current buffer.
578You should override this function in multiple mode buffers to
579determine which major mode apply at point.")
580
581(defun semantic-ctxt-current-mode-default (&optional point)
582 "Return the major mode active at POINT.
583POINT defaults to the value of point in current buffer.
584This default implementation returns the current major mode."
585 major-mode)
586\f
587;;; Scoped Types
588;;
589;; Scoped types are types that the current code would have access to.
590;; The come from the global namespace or from special commands such as "using"
591(define-overloadable-function semantic-ctxt-scoped-types (&optional point)
592 "Return a list of type names currently in scope at POINT.
593The return value can be a mixed list of either strings (names of
594types that are in scope) or actual tags (type declared locally
595that may or may not have a name.)")
596
597(defun semantic-ctxt-scoped-types-default (&optional point)
598 "Return a list of scoped types by name for the current context at POINT.
599This is very different for various languages, and does nothing unless
9bf6c65c 600overridden."
dd9af436
CY
601 nil)
602
603(define-overloadable-function semantic-ctxt-imported-packages (&optional point)
604 "Return a list of package tags or names which are being imported at POINT.
605The return value is a list of strings which are package names
606that are implied in code. Thus a C++ symbol:
607 foo::bar();
608where there is a statement such as:
609 using baz;
610means that the first symbol might be:
611 baz::foo::bar();"
612 nil)
1bd95535
CY
613
614(provide 'semantic/ctxt)
615
55b522b2
CY
616;; Local variables:
617;; generated-autoload-file: "loaddefs.el"
996bc9bf 618;; generated-autoload-load-name: "semantic/ctxt"
55b522b2
CY
619;; End:
620
aa8724ae 621;;; semantic/ctxt.el ends here