imenu.el 20 KB

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