cedet/cedet.el (cedet-packages): Bump srecode version.
[bpt/emacs.git] / lisp / cedet / semantic / ctxt.el
1 ;;; semantic/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 (require 'semantic/find)
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 (let ((semantic--progress-reporter
159 (make-progress-reporter (semantic-parser-working-message "Local")
160 0 100)))
161 (save-excursion
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 ())))))
167
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)))
177 (if vars
178 (progn
179 ;;(message "Found cached vars.")
180 vars)
181 (let ((vars2 nil)
182 ;; We want nothing to do with funny syntaxing while doing this.
183 (semantic-unmatched-syntax-hook nil)
184 (start (point))
185 (firstusefulstart nil)
186 )
187 (while (not (semantic-up-context (point) 'function))
188 (when (not vars)
189 (setq firstusefulstart (point)))
190 (save-excursion
191 (forward-char 1)
192 (setq vars
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
197 (point)
198 (save-excursion (semantic-end-of-context) (point))
199 'bovine-inner-scope
200 nil
201 t)
202 vars))))
203 ;; Modify the tags in place.
204 (setq vars2 vars)
205 (while vars2
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)
212 (save-excursion
213 (unless (semantic-end-of-context)
214 (point))))))
215 ;;(message "Caching values %d->%d." firstusefulstart end)
216 (semantic-cache-data-to-buffer
217 (current-buffer) firstusefulstart
218 (or end
219 ;; If the end-of-context fails,
220 ;; just use our cursor starting
221 ;; position.
222 start)
223 vars 'get-local-variables 'exit-cache-zone))
224 )
225 ;; Return our list.
226 vars)))))
227
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
235 tags."
236 (save-excursion
237 (if point (goto-char point))
238 (let* ((case-fold-search semantic-case-fold)
239 (args (:override-with-args ()))
240 arg tags)
241 ;; Convert unsafe arguments to the right thing.
242 (while args
243 (setq arg (car args)
244 args (cdr args)
245 tags (cons (cond
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))
250 ((stringp arg)
251 (semantic--tag-put-property
252 (semantic-tag-new-variable arg nil nil)
253 :filename (buffer-file-name)))
254 (t
255 (error "Unknown parameter element %S" arg)))
256 tags)))
257 (nreverse tags))))
258
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))))
265
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.")
271
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:
276
277 - LOCAL-ARGUMENTS is collected by `semantic-get-local-arguments'.
278 - LOCAL-VARIABLES is collected by `semantic-get-local-variables'."
279 (save-excursion
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)))))
284
285 ;;; Local context parsing
286 ;;
287 ;; Context parsing assumes a series of language independent commonalities.
288 ;; These terms are used to describe those contexts:
289 ;;
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.
296 ;;
297 ;;
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'.")
301
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
309
310 (if (re-search-forward (regexp-quote semantic-command-separation-character)
311 nil t)
312 (forward-char -1)
313 ;; If there wasn't a command after this, we are the last
314 ;; command, and we are incomplete.
315 (goto-char (point-max)))))))
316
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'.")
320
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)
330 nil t)
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")
336 ))))
337
338
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)))
342
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)))
346
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)))
351
352 (defmacro semantic-with-buffer-narrowed-to-command (&rest body)
353 "Execute BODY with the buffer narrowed to the current command."
354 `(save-restriction
355 (semantic-narrow-to-command)
356 ,@body))
357 (put 'semantic-with-buffer-narrowed-to-command 'lisp-indent-function 0)
358 (add-hook 'edebug-setup-hook
359 (lambda ()
360 (def-edebug-spec semantic-with-buffer-narrowed-to-command
361 (def-body))))
362
363
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:
368 this.that().entry
369
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\" )")
374
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'."
379 (save-excursion
380 (if point (goto-char point))
381 (let* ((fieldsep1 (mapconcat (lambda (a) (regexp-quote a))
382 semantic-type-relation-separator-character
383 "\\|"))
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)
389 (symlist nil)
390 end)
391 (with-syntax-table semantic-lex-syntax-table
392 (save-excursion
393 (cond ((looking-at "\\w\\|\\s_")
394 ;; In the middle of a symbol, move to the end.
395 (forward-sexp 1))
396 ((looking-at fieldsep1)
397 ;; We are in a find spot.. do nothing.
398 nil
399 )
400 ((save-excursion
401 (and (condition-case nil
402 (progn (forward-sexp -1)
403 (forward-sexp 1)
404 t)
405 (error nil))
406 (looking-at fieldsep1)))
407 (setq symlist (list ""))
408 (forward-sexp -1)
409 ;; Skip array expressions.
410 (while (looking-at "\\s(") (forward-sexp -1))
411 (forward-sexp 1))
412 )
413 ;; Set our end point.
414 (setq end (point))
415
416 ;; Now that we have gotten started, lets do the rest.
417 (condition-case nil
418 (while (save-excursion
419 (forward-char -1)
420 (looking-at "\\w\\|\\s_"))
421 ;; We have a symbol.. Do symbol things
422 (forward-sexp -1)
423 (setq symlist (cons (buffer-substring-no-properties (point) end)
424 symlist))
425 ;; Skip the next syntactic expression backwards, then go forwards.
426 (let ((cp (point)))
427 (forward-sexp -1)
428 (forward-sexp 1)
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
431 ;; have to stop.
432 (if (<= cp (point)) (error nil)))
433 (if (looking-at fieldsep)
434 (progn
435 (forward-sexp -1)
436 ;; Skip array expressions.
437 (while (and (looking-at "\\s(") (not (bobp)))
438 (forward-sexp -1))
439 (forward-sexp 1)
440 (setq end (point)))
441 (error nil))
442 )
443 (error nil)))
444 symlist))))
445
446
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).")
451
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)."
456 (save-excursion
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))
464 )
465 ((and prefix (looking-at endsym))
466 (cons (point) (progn
467 (condition-case nil
468 (forward-sexp 1)
469 (error nil))
470 (point))))
471 (prefix
472 (condition-case nil
473 (cons (progn (forward-sexp -1) (point))
474 (progn (forward-sexp 1) (point)))
475 (error nil)))
476 (t nil))))
477 )
478 (list prefix endsym bounds))))
479
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.")
484
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
491 (condition-case nil
492 (semantic-with-buffer-narrowed-to-command
493 (save-excursion
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
499 (forward-sexp -1)
500 (semantic-ctxt-current-symbol)))
501 (error nil)))))
502
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.")
508
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
512 NAME ( args ... )"
513 (if point (goto-char point))
514 (let ((case-fold-search semantic-case-fold))
515 (with-syntax-table semantic-lex-syntax-table
516 (save-excursion
517 (semantic-up-context)
518 (when (looking-at "(")
519 (semantic-ctxt-current-symbol))))
520 ))
521
522 (define-overloadable-function semantic-ctxt-current-argument (&optional point)
523 "Return the index of the argument position the cursor is on at POINT.")
524
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)
532 (save-excursion
533 ;; Only get the current arg index if we are in function args.
534 (let ((p (point))
535 (idx 1))
536 (semantic-up-context)
537 (while (re-search-forward
538 (regexp-quote semantic-function-argument-separation-character)
539 p t)
540 (setq idx (1+ idx)))
541 idx))))))
542
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)))
552
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.")
559
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."
563 (save-excursion
564 (if point (goto-char point))
565
566 (let ((tag (semantic-current-tag)))
567 (if 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))
572 '(type))
573 (t nil))
574 '(type)
575 ))))
576
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.")
582
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."
587 major-mode)
588 \f
589 ;;; Scoped Types
590 ;;
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.)")
598
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
602 overriden."
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.
607 nil
608 ))
609
610 (provide 'semantic/ctxt)
611
612 ;;; semantic/ctxt.el ends here