]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/imenu.el
9fd0343faef234f434dfc051e46a6e4a744e3af7
[gnu-emacs] / lisp / cedet / semantic / imenu.el
1 ;;; semantic/imenu.el --- Use Semantic as an imenu tag generator
2
3 ;; Copyright (C) 2000-2005, 2007-2008, 2010-2014 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Maintainer: Eric Ludlam
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 ;; This support function can be used in any buffer which supports
26 ;; the bovinator to create the imenu index.
27 ;;
28 ;; To use this in a buffer, do this in a hook.
29 ;;
30 ;; (add-hook 'mode-hook
31 ;; (lambda ()
32 ;; (setq imenu-create-index-function 'semantic-create-imenu-index)
33 ;; ))
34
35 (require 'semantic)
36 (require 'semantic/format)
37 (require 'semantic/db)
38 (require 'semantic/db-file)
39 (require 'semantic/sort)
40 (require 'imenu)
41
42 (declare-function pulse-momentary-highlight-one-line "pulse" (o &optional face))
43 (declare-function semanticdb-semantic-init-hook-fcn "db-mode")
44
45 ;; Because semantic imenu tags will hose the current imenu handling
46 ;; code in speedbar, force semantic/sb in.
47 (if (featurep 'speedbar)
48 (require 'semantic/sb)
49 (add-hook 'speedbar-load-hook (lambda () (require 'semantic/sb))))
50
51 (defgroup semantic-imenu nil
52 "Semantic interface to Imenu."
53 :group 'semantic
54 :group 'imenu
55 )
56
57 ;;;###autoload
58 (defcustom semantic-imenu-summary-function 'semantic-format-tag-abbreviate
59 "*Function to use when creating items in Imenu.
60 Some useful functions are found in `semantic-format-tag-functions'."
61 :group 'semantic-imenu
62 :type semantic-format-tag-custom-list)
63 (make-variable-buffer-local 'semantic-imenu-summary-function)
64
65 ;;;###autoload
66 (defcustom semantic-imenu-bucketize-file t
67 "*Non-nil if tags in a file are to be grouped into buckets."
68 :group 'semantic-imenu
69 :type 'boolean)
70 (make-variable-buffer-local 'semantic-imenu-bucketize-file)
71
72 (defcustom semantic-imenu-adopt-external-members t
73 "*Non-nil if types in a file should adopt externally defined members.
74 C++ and CLOS can define methods that are not in the body of a class
75 definition."
76 :group 'semantic-imenu
77 :type 'boolean)
78
79 (defcustom semantic-imenu-buckets-to-submenu t
80 "*Non-nil if buckets of tags are to be turned into submenus.
81 This option is ignored if `semantic-imenu-bucketize-file' is nil."
82 :group 'semantic-imenu
83 :type 'boolean)
84 (make-variable-buffer-local 'semantic-imenu-buckets-to-submenu)
85
86 ;;;###autoload
87 (defcustom semantic-imenu-expand-type-members t
88 "*Non-nil if types should have submenus with members in them."
89 :group 'semantic-imenu
90 :type 'boolean)
91 (make-variable-buffer-local 'semantic-imenu-expand-type-members)
92 (semantic-varalias-obsolete 'semantic-imenu-expand-type-parts
93 'semantic-imenu-expand-type-members "23.2")
94
95 (defcustom semantic-imenu-bucketize-type-members t
96 "*Non-nil if members of a type should be grouped into buckets.
97 A nil value means to keep them in the same order.
98 Overridden to nil if `semantic-imenu-bucketize-file' is nil."
99 :group 'semantic-imenu
100 :type 'boolean)
101 (make-variable-buffer-local 'semantic-imenu-bucketize-type-members)
102 (semantic-varalias-obsolete 'semantic-imenu-bucketize-type-parts
103 'semantic-imenu-bucketize-type-members "23.2")
104
105 (defcustom semantic-imenu-sort-bucket-function nil
106 "*Function to use when sorting tags in the buckets of functions.
107 See `semantic-bucketize' and the FILTER argument for more details on this function."
108 :group 'semantic-imenu
109 :type '(radio (const :tag "No Sorting" nil)
110 (const semantic-sort-tags-by-name-increasing)
111 (const semantic-sort-tags-by-name-decreasing)
112 (const semantic-sort-tags-by-type-increasing)
113 (const semantic-sort-tags-by-type-decreasing)
114 (const semantic-sort-tags-by-name-increasing-ci)
115 (const semantic-sort-tags-by-name-decreasing-ci)
116 (const semantic-sort-tags-by-type-increasing-ci)
117 (const semantic-sort-tags-by-type-decreasing-ci)
118 (function)))
119 (make-variable-buffer-local 'semantic-imenu-sort-bucket-function)
120
121 (defcustom semantic-imenu-index-directory nil
122 "*Non nil to index the entire directory for tags.
123 Doesn't actually parse the entire directory, but displays tags for all files
124 currently listed in the current Semantic database.
125 This variable has no meaning if semanticdb is not active."
126 :group 'semantic-imenu
127 :type 'boolean)
128
129 (defcustom semantic-imenu-auto-rebuild-directory-indexes nil
130 "*If non-nil automatically rebuild directory index imenus.
131 That is when a directory index imenu is updated, automatically rebuild
132 other buffer local ones based on the same semanticdb."
133 :group 'semantic-imenu
134 :type 'boolean)
135
136 (defvar semantic-imenu-directory-current-file nil
137 "When building a file index, this is the file name currently being built.")
138
139 (defvar semantic-imenu-auto-rebuild-running nil
140 "Non-nil if `semantic-imenu-rebuild-directory-indexes' is running.")
141
142 ;;;###autoload
143 (defvar semantic-imenu-expandable-tag-classes '(type)
144 "List of expandable tag classes.
145 Tags of those classes will be given submenu with children.
146 By default, a `type' has interesting children. In Texinfo, however, a
147 `section' has interesting children.")
148 (make-variable-buffer-local 'semantic-imenu-expandable-tag-classes)
149 (semantic-varalias-obsolete 'semantic-imenu-expandable-token
150 'semantic-imenu-expandable-tag-classes "23.2")
151
152 ;;; Code:
153 (defun semantic-imenu-tag-overlay (tag)
154 "Return the overlay belonging to tag.
155 If TAG doesn't have an overlay, and instead as a vector of positions,
156 concoct a combination of file name, and position."
157 (let ((o (semantic-tag-overlay tag)))
158 (if (not (semantic-overlay-p o))
159 (let ((v (make-vector 3 nil)))
160 (aset v 0 semantic-imenu-directory-current-file)
161 (aset v 1 (aref o 0))
162 (aset v 2 (aref o 1))
163 v)
164 o)))
165
166
167 (defun semantic-imenu-goto-function (name position &optional rest)
168 "Move point associated with NAME to POSITION.
169 Used to override function `imenu-default-goto-function' so that
170 we can continue to use overlays to maintain the current position.
171 Optional argument REST is some extra stuff."
172 (require 'pulse)
173 (if (semantic-overlay-p position)
174 (let ((os (semantic-overlay-start position))
175 (ob (semantic-overlay-buffer position)))
176 (if os
177 (progn
178 (if (not (eq ob (current-buffer)))
179 (switch-to-buffer ob))
180 (imenu-default-goto-function name os rest)
181 (pulse-momentary-highlight-one-line (point))
182 )
183 ;; This should never happen, but check anyway.
184 (message "Imenu is out of date, try again. (internal bug)")
185 (setq imenu--index-alist nil)))
186 ;; When the POSITION is actually a pair of numbers in an array, then
187 ;; the file isn't loaded into the current buffer.
188 (if (vectorp position)
189 (let ((file (aref position 0))
190 (pos (aref position 1)))
191 (and file (find-file file))
192 (imenu-default-goto-function name pos rest)
193 (pulse-momentary-highlight-one-line (point))
194 )
195 ;; When the POSITION is the symbol 'file-only' it means that this
196 ;; is a directory index entry and there is no tags in this
197 ;; file. So just jump to the beginning of the file.
198 (if (eq position 'file-only)
199 (progn
200 (find-file name)
201 (imenu-default-goto-function name (point-min) rest)
202 (pulse-momentary-highlight-one-line (point))
203 )
204 ;; Probably POSITION don't came from a semantic imenu. Try
205 ;; the default imenu goto function.
206 (condition-case nil
207 (progn
208 (imenu-default-goto-function name position rest)
209 (pulse-momentary-highlight-one-line (point))
210 )
211 (error
212 (message "Semantic Imenu override problem. (Internal bug)")
213 (setq imenu--index-alist nil)))))
214 ))
215
216 (defun semantic-imenu-flush-fcn (&optional ignore)
217 "This function is called as a hook to clear the imenu cache.
218 It is cleared after any parsing.
219 IGNORE arguments."
220 (if (eq imenu-create-index-function 'semantic-create-imenu-index)
221 (setq imenu--index-alist nil
222 imenu-menubar-modified-tick 0))
223 (remove-hook 'semantic-after-toplevel-cache-change-hook
224 'semantic-imenu-flush-fcn t)
225 (remove-hook 'semantic-after-partial-cache-change-hook
226 'semantic-imenu-flush-fcn t)
227 )
228
229 ;;;###autoload
230 (defun semantic-create-imenu-index (&optional stream)
231 "Create an imenu index for any buffer which supports Semantic.
232 Uses the output of the Semantic parser to create the index.
233 Optional argument STREAM is an optional stream of tags used to create menus."
234 (setq imenu-default-goto-function 'semantic-imenu-goto-function)
235 (prog1
236 (if (and semantic-imenu-index-directory
237 (featurep 'semantic/db)
238 (semanticdb-minor-mode-p))
239 (semantic-create-imenu-directory-index
240 (or stream (semantic-fetch-tags-fast)))
241 (semantic-create-imenu-index-1
242 (or stream (semantic-fetch-tags-fast)) nil))
243 (semantic-make-local-hook 'semantic-after-toplevel-cache-change-hook)
244 (add-hook 'semantic-after-toplevel-cache-change-hook
245 'semantic-imenu-flush-fcn nil t)
246 (semantic-make-local-hook 'semantic-after-partial-cache-change-hook)
247 (add-hook 'semantic-after-partial-cache-change-hook
248 'semantic-imenu-flush-fcn nil t)))
249
250 (defun semantic-create-imenu-directory-index (&optional stream)
251 "Create an imenu tag index based on all files active in semanticdb.
252 Optional argument STREAM is the stream of tags for the current buffer."
253 (if (not semanticdb-current-database)
254 (semantic-create-imenu-index-1 stream nil)
255 ;; We have a database, list all files, with the current file on top.
256 (let ((index (list
257 (cons (oref semanticdb-current-table file)
258 (or (semantic-create-imenu-index-1 stream nil)
259 ;; No tags in this file
260 'file-only))))
261 (tables (semanticdb-get-database-tables semanticdb-current-database)))
262 (while tables
263 (let ((semantic-imenu-directory-current-file
264 (oref (car tables) file))
265 tags)
266 (when (and (not (eq (car tables) semanticdb-current-table))
267 (semanticdb-live-p (car tables))
268 (semanticdb-equivalent-mode (car tables))
269 )
270 (setq tags (oref (car tables) tags)
271 index (cons (cons semantic-imenu-directory-current-file
272 (or (and tags
273 ;; don't pass nil stream because
274 ;; it will use the current
275 ;; buffer
276 (semantic-create-imenu-index-1
277 (oref (car tables) tags)
278 nil))
279 ;; no tags in the file
280 'file-only))
281 index)))
282 (setq tables (cdr tables))))
283
284 ;; If enabled automatically rebuild other imenu directory
285 ;; indexes based on the same Semantic database
286 (or (not semantic-imenu-auto-rebuild-directory-indexes)
287 ;; If auto rebuild already in progress does nothing
288 semantic-imenu-auto-rebuild-running
289 (unwind-protect
290 (progn
291 (setq semantic-imenu-auto-rebuild-running t)
292 (semantic-imenu-rebuild-directory-indexes
293 semanticdb-current-database))
294 (setq semantic-imenu-auto-rebuild-running nil)))
295
296 (nreverse index))))
297
298 (defun semantic-create-imenu-index-1 (stream &optional parent)
299 "Create an imenu index for any buffer which supports Semantic.
300 Uses the output of the Semantic parser to create the index.
301 STREAM is a stream of tags used to create menus.
302 Optional argument PARENT is a tag parent of STREAM."
303 (let ((tags stream)
304 (semantic-imenu-adopt-external-members
305 semantic-imenu-adopt-external-members))
306 ;; If we should regroup, do so.
307 (if semantic-imenu-adopt-external-members
308 (setq tags (semantic-adopt-external-members tags)
309 ;; Don't allow recursion here.
310 semantic-imenu-adopt-external-members nil))
311 ;; Test for bucketing vs not.
312 (if semantic-imenu-bucketize-file
313 (let ((buckets (semantic-bucketize
314 tags parent
315 semantic-imenu-sort-bucket-function))
316 item name
317 index)
318 (cond
319 ((null buckets)
320 nil)
321 ((or (cdr-safe buckets) ;; if buckets has more than one item in it.
322 (not semantic-imenu-buckets-to-submenu)) ;; to force separators between buckets
323 (while buckets
324 (setq name (car (car buckets))
325 item (cdr (car buckets)))
326 (if semantic-imenu-buckets-to-submenu
327 (progn
328 ;; Make submenus
329 (if item
330 (setq index
331 (cons (cons name
332 (semantic-create-imenu-subindex item))
333 index))))
334 ;; Glom everything together with "---" between
335 (if item
336 (setq index
337 (append index
338 ;; do not create a menu separator in the parent menu
339 ;; when creating a sub-menu
340 (if (memq (semantic-tag-class (car item))
341 semantic-imenu-expandable-tag-classes)
342 (semantic-create-imenu-subindex item)
343 (cons
344 '("---")
345 (semantic-create-imenu-subindex item)))))
346 ))
347 (setq buckets (cdr buckets)))
348 (if semantic-imenu-buckets-to-submenu
349 (nreverse index)
350 index))
351 (t
352 (setq name (car (car buckets))
353 item (cdr (car buckets)))
354 (semantic-create-imenu-subindex item))))
355 ;; Else, group everything together
356 (semantic-create-imenu-subindex tags))))
357
358 (defun semantic-create-imenu-subindex (tags)
359 "From TAGS, create an imenu index of interesting things."
360 (let ((notypecheck (not semantic-imenu-expand-type-members))
361 children index tag parts)
362 (while tags
363 (setq tag (car tags)
364 children (semantic-tag-components-with-overlays tag))
365 (if (and (not notypecheck)
366 (memq (semantic-tag-class tag)
367 semantic-imenu-expandable-tag-classes)
368 children
369 )
370 ;; to keep an homogeneous menu organization, type menu items
371 ;; always have a sub-menu with at least the *definition*
372 ;; item (even if the tag has no type components)
373 (progn
374 (setq parts children)
375 ;; There is options which create the submenu
376 ;; * Type has an overlay, but children do.
377 ;; The type doesn't have to have it's own overlay,
378 ;; but a type with no overlay and no children should be
379 ;; invalid.
380 (setq index
381 (cons
382 (cons
383 (funcall semantic-imenu-summary-function tag)
384 ;; Add a menu for getting at the type definitions
385 (if (and parts
386 ;; Note to self: enable menu items for
387 ;; sub parts even if they are not proper
388 ;; tags.
389 (semantic-tag-p (car parts)))
390 (let ((submenu
391 (if (and semantic-imenu-bucketize-type-members
392 semantic-imenu-bucketize-file)
393 (semantic-create-imenu-index-1 parts tag)
394 (semantic-create-imenu-subindex parts))))
395 ;; Only add a *definition* if we have a position
396 ;; in that type tag.
397 (if (semantic-tag-with-position-p tag)
398 (cons
399 (cons "*definition*"
400 (semantic-imenu-tag-overlay tag))
401 submenu)
402 submenu))
403 ;; There were no parts, or something like that, so
404 ;; instead just put the definition here.
405 (if (semantic-tag-with-position-p tag)
406 (semantic-imenu-tag-overlay tag)
407 nil)
408 ))
409 index)))
410 (if (semantic-tag-with-position-p tag)
411 (setq index (cons
412 (cons
413 (funcall semantic-imenu-summary-function tag)
414 (semantic-imenu-tag-overlay tag))
415 index))))
416 (setq tags (cdr tags)))
417 ;; `imenu--split-submenus' sort submenus according to
418 ;; `imenu-sort-function' setting and split them up if they are
419 ;; longer than `imenu-max-items'.
420 (imenu--split-submenus (nreverse index))))
421
422 ;;; directory imenu rebuilding.
423 ;;
424 (defun semantic-imenu-rebuild-directory-indexes (db)
425 "Rebuild directory index imenus based on Semantic database DB."
426 (let ((l (buffer-list))
427 b)
428 (while l
429 (setq b (car l)
430 l (cdr l))
431 (if (and (not (eq b (current-buffer)))
432 (buffer-live-p b))
433 (with-current-buffer b
434 ;; If there is a buffer local Semantic index directory
435 ;; imenu
436 (when (and (eq imenu-create-index-function
437 'semantic-create-imenu-index)
438 semanticdb-current-database
439 (eq semanticdb-current-database db))
440 ;; Rebuild the imenu
441 (imenu--cleanup)
442 (setq imenu--index-alist nil)
443 (funcall
444 (if (fboundp 'imenu-menu-filter)
445 ;; XEmacs imenu
446 'imenu-menu-filter
447 ;; Emacs imenu
448 'imenu-update-menubar))))))))
449
450 (defun semantic-imenu-semanticdb-hook ()
451 "Function to be called from `semanticdb-mode-hook'.
452 Clears all imenu menus that may be depending on the database."
453 (require 'semantic/db-mode)
454 (semantic-map-buffers
455 #'(lambda ()
456 ;; Set up semanticdb environment if enabled.
457 (if (semanticdb-minor-mode-p)
458 (semanticdb-semantic-init-hook-fcn))
459 ;; Clear imenu cache to redraw the imenu.
460 (semantic-imenu-flush-fcn))))
461
462 (add-hook 'semanticdb-mode-hook 'semantic-imenu-semanticdb-hook)
463
464 ;;; Interactive Utilities
465 ;;
466 (defun semantic-imenu-toggle-bucketize-file ()
467 "Toggle the ability of imenu to bucketize the current file."
468 (interactive)
469 (setq semantic-imenu-bucketize-file (not semantic-imenu-bucketize-file))
470 ;; Force a rescan
471 (setq imenu--index-alist nil))
472
473 (defun semantic-imenu-toggle-buckets-to-submenu ()
474 "Toggle the ability of imenu to turn buckets into submenus."
475 (interactive)
476 (setq semantic-imenu-buckets-to-submenu (not semantic-imenu-buckets-to-submenu))
477 ;; Force a rescan
478 (setq imenu--index-alist nil))
479
480 (defun semantic-imenu-toggle-bucketize-type-parts ()
481 "Toggle the ability of imenu to bucketize the current file."
482 (interactive)
483 (setq semantic-imenu-bucketize-type-members (not semantic-imenu-bucketize-type-members))
484 ;; Force a rescan
485 (setq imenu--index-alist nil))
486
487 ;;; Which function support
488 ;;
489 ;; The which-function library will display the current function in the
490 ;; mode line. It tries do do this through imenu. With a semantic parsed
491 ;; buffer, there is a much more efficient way of doing this.
492 ;; Advise `which-function' so that we optionally use semantic tags
493 ;; instead, and get better stuff.
494 (require 'advice)
495
496 (defvar semantic-which-function 'semantic-default-which-function
497 "Function to convert semantic tags into `which-function' text.")
498
499 (defcustom semantic-which-function-use-color nil
500 "*Use color when displaying the current function with `which-function'."
501 :group 'semantic-imenu
502 :type 'boolean)
503
504 (defun semantic-default-which-function (taglist)
505 "Convert TAGLIST into a string usable by `which-function'.
506 Returns the first tag name in the list, unless it is a type,
507 in which case it concatenates them together."
508 (cond ((eq (length taglist) 1)
509 (semantic-format-tag-abbreviate
510 (car taglist) nil semantic-which-function-use-color))
511 ((memq (semantic-tag-class (car taglist))
512 semantic-imenu-expandable-tag-classes)
513 (concat (semantic-format-tag-name
514 (car taglist) nil semantic-which-function-use-color)
515 (car semantic-type-relation-separator-character)
516 ;; recurse until we no longer have a type
517 ;; or any tags left.
518 (semantic-default-which-function (cdr taglist))))
519 (t (semantic-format-tag-abbreviate
520 (car taglist) nil semantic-which-function-use-color))))
521
522 ;; (defadvice which-function (around semantic-which activate)
523 ;; "Choose the function to display via semantic if it is currently active."
524 ;; (if (and (featurep 'semantic) semantic--buffer-cache)
525 ;; (let ((ol (semantic-find-tag-by-overlay)))
526 ;; (setq ad-return-value (funcall semantic-which-function ol)))
527 ;; ad-do-it))
528
529 (provide 'semantic/imenu)
530
531 ;; Local variables:
532 ;; generated-autoload-file: "loaddefs.el"
533 ;; generated-autoload-load-name: "semantic/imenu"
534 ;; End:
535
536 ;;; semantic/imenu.el ends here