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