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