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