which-func.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. ;;; which-func.el --- print current function in mode line
  2. ;; Copyright (C) 1994, 1997-1998, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Alex Rezinsky <alexr@msil.sps.mot.com>
  4. ;; (doesn't seem to be responsive any more)
  5. ;; Keywords: mode-line, imenu, tools
  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. ;; This package prints name of function where your current point is
  19. ;; located in mode line. It assumes that you work with imenu package
  20. ;; and imenu--index-alist is up to date.
  21. ;; KNOWN BUGS
  22. ;; ----------
  23. ;; Really this package shows not "function where the current point is
  24. ;; located now", but "nearest function which defined above the current
  25. ;; point". So if your current point is located after end of function
  26. ;; FOO but before begin of function BAR, FOO will be displayed in mode
  27. ;; line.
  28. ;; - if two windows display the same buffer, both windows
  29. ;; show the same `which-func' information.
  30. ;; TODO LIST
  31. ;; ---------
  32. ;; 1. Dependence on imenu package should be removed. Separate
  33. ;; function determination mechanism should be used to determine the end
  34. ;; of a function as well as the beginning of a function.
  35. ;; 2. This package should be realized with the help of overlay
  36. ;; properties instead of imenu--index-alist variable.
  37. ;;; History:
  38. ;; THANKS TO
  39. ;; ---------
  40. ;; Per Abrahamsen <abraham@iesd.auc.dk>
  41. ;; Some ideas (inserting in mode-line, using of post-command hook
  42. ;; and toggling this mode) have been borrowed from his package
  43. ;; column.el
  44. ;; Peter Eisenhauer <pipe@fzi.de>
  45. ;; Bug fixing in case nested indexes.
  46. ;; Terry Tateyama <ttt@ursa0.cs.utah.edu>
  47. ;; Suggestion to use find-file-hook for first imenu
  48. ;; index building.
  49. ;;; Code:
  50. ;; Variables for customization
  51. ;; ---------------------------
  52. ;;
  53. (defvar which-func-unknown "???"
  54. "String to display in the mode line when current function is unknown.")
  55. (defgroup which-func nil
  56. "Display the current function name in the mode line."
  57. :group 'tools
  58. :version "20.3")
  59. (defcustom which-func-modes t
  60. ;; '(emacs-lisp-mode c-mode c++-mode objc-mode perl-mode cperl-mode python-mode
  61. ;; makefile-mode sh-mode fortran-mode f90-mode ada-mode
  62. ;; diff-mode)
  63. "List of major modes for which Which Function mode should be used.
  64. For other modes it is disabled. If this is equal to t,
  65. then Which Function mode is enabled in any major mode that supports it."
  66. :group 'which-func
  67. :version "24.2" ; explicit list -> t
  68. :type '(choice (const :tag "All modes" t)
  69. (repeat (symbol :tag "Major mode"))))
  70. (defcustom which-func-non-auto-modes nil
  71. "List of major modes where Which Function mode is inactive till Imenu is used.
  72. This means that Which Function mode won't really do anything
  73. until you use Imenu, in these modes. Note that files
  74. larger than `which-func-maxout' behave in this way too;
  75. Which Function mode doesn't do anything until you use Imenu."
  76. :group 'which-func
  77. :type '(repeat (symbol :tag "Major mode")))
  78. (defcustom which-func-maxout 500000
  79. "Don't automatically compute the Imenu menu if buffer is this big or bigger.
  80. Zero means compute the Imenu menu regardless of size."
  81. :group 'which-func
  82. :type 'integer)
  83. (defvar which-func-keymap
  84. (let ((map (make-sparse-keymap)))
  85. (define-key map [mode-line mouse-1] 'beginning-of-defun)
  86. (define-key map [mode-line mouse-2]
  87. (lambda ()
  88. (interactive)
  89. (if (eq (point-min) 1)
  90. (narrow-to-defun)
  91. (widen))))
  92. (define-key map [mode-line mouse-3] 'end-of-defun)
  93. map)
  94. "Keymap to display on mode line which-func.")
  95. (defface which-func
  96. ;; Whether `font-lock-function-name-face' is an appropriate face to
  97. ;; inherit depends on the mode-line face; define several variants based
  98. ;; on the default mode-line face.
  99. '(;; The default mode-line face on a high-color display is a relatively
  100. ;; light color ("grey75"), and only the light-background variant of
  101. ;; `font-lock-function-name-face' is visible against it.
  102. (((class color) (min-colors 88) (background light))
  103. :inherit font-lock-function-name-face)
  104. ;; The default mode-line face on other display types is inverse-video;
  105. ;; it seems that only in the dark-background case is
  106. ;; `font-lock-function-name-face' visible against it.
  107. (((class grayscale mono) (background dark))
  108. :inherit font-lock-function-name-face)
  109. (((class color) (background light))
  110. :inherit font-lock-function-name-face)
  111. ;; If none of the above cases, use an explicit color chosen to contrast
  112. ;; well with the default mode-line face.
  113. (((class color) (min-colors 88) (background dark))
  114. :foreground "Blue1")
  115. (((background dark))
  116. :foreground "Blue1")
  117. (t
  118. :foreground "LightSkyBlue"))
  119. "Face used to highlight mode line function names."
  120. :group 'which-func)
  121. (defcustom which-func-format
  122. `("["
  123. (:propertize which-func-current
  124. local-map ,which-func-keymap
  125. face which-func
  126. ;;mouse-face highlight ; currently not evaluated :-(
  127. help-echo "mouse-1: go to beginning\n\
  128. mouse-2: toggle rest visibility\n\
  129. mouse-3: go to end")
  130. "]")
  131. "Format for displaying the function in the mode line."
  132. :group 'which-func
  133. :type 'sexp)
  134. ;;;###autoload (put 'which-func-format 'risky-local-variable t)
  135. (defvar which-func-imenu-joiner-function (lambda (x) (car (last x)))
  136. "Function to join together multiple levels of imenu nomenclature.
  137. Called with a single argument, a list of strings giving the names
  138. of the menus we had to traverse to get to the item. Returns a
  139. single string, the new name of the item.")
  140. (defvar which-func-cleanup-function nil
  141. "Function to transform a string before displaying it in the mode line.
  142. The function is called with one argument, the string to display.
  143. Its return value is displayed in the mode line.
  144. If nil, no function is called. The default value is nil.
  145. This feature can be useful if Imenu is set up to make more
  146. detailed entries (e.g., containing the argument list of a function),
  147. and you want to simplify them for the mode line
  148. \(e.g., removing the parameter list to just have the function name.)")
  149. ;;; Code, nothing to customize below here
  150. ;;; -------------------------------------
  151. ;;;
  152. (require 'imenu)
  153. (defvar which-func-table (make-hash-table :test 'eq :weakness 'key))
  154. (defconst which-func-current
  155. '(:eval (replace-regexp-in-string
  156. "%" "%%"
  157. (gethash (selected-window) which-func-table which-func-unknown))))
  158. ;;;###autoload (put 'which-func-current 'risky-local-variable t)
  159. (defvar which-func-mode nil
  160. "Non-nil means display current function name in mode line.
  161. This makes a difference only if `which-function-mode' is non-nil.")
  162. (make-variable-buffer-local 'which-func-mode)
  163. ;;(put 'which-func-mode 'permanent-local t)
  164. (add-hook 'find-file-hook 'which-func-ff-hook t)
  165. (defun which-func-ff-hook ()
  166. "File find hook for Which Function mode.
  167. It creates the Imenu index for the buffer, if necessary."
  168. (setq which-func-mode
  169. (and which-function-mode
  170. (or (eq which-func-modes t)
  171. (member major-mode which-func-modes))))
  172. (condition-case err
  173. (if (and which-func-mode
  174. (not (member major-mode which-func-non-auto-modes))
  175. (or (null which-func-maxout)
  176. (< buffer-saved-size which-func-maxout)
  177. (= which-func-maxout 0)))
  178. (setq imenu--index-alist
  179. (save-excursion (funcall imenu-create-index-function))))
  180. (error
  181. (unless (equal err
  182. '(user-error "This buffer cannot use `imenu-default-create-index-function'"))
  183. (message "which-func-ff-hook error: %S" err))
  184. (setq which-func-mode nil))))
  185. (defun which-func-update ()
  186. ;; "Update the Which-Function mode display for all windows."
  187. ;; (walk-windows 'which-func-update-1 nil 'visible))
  188. (which-func-update-1 (selected-window)))
  189. (defun which-func-update-1 (window)
  190. "Update the Which Function mode display for window WINDOW."
  191. (with-selected-window window
  192. (when which-func-mode
  193. (condition-case info
  194. (let ((current (which-function)))
  195. (unless (equal current (gethash window which-func-table))
  196. (puthash window current which-func-table)
  197. (force-mode-line-update)))
  198. (error
  199. (setq which-func-mode nil)
  200. (error "Error in which-func-update: %S" info))))))
  201. ;;;###autoload
  202. (defun which-func-mode (&optional arg)
  203. (which-function-mode arg))
  204. (make-obsolete 'which-func-mode 'which-function-mode "24.1")
  205. (defvar which-func-update-timer nil)
  206. ;; This is the name people would normally expect.
  207. ;;;###autoload
  208. (define-minor-mode which-function-mode
  209. "Toggle mode line display of current function (Which Function mode).
  210. With a prefix argument ARG, enable Which Function mode if ARG is
  211. positive, and disable it otherwise. If called from Lisp, enable
  212. the mode if ARG is omitted or nil.
  213. Which Function mode is a global minor mode. When enabled, the
  214. current function name is continuously displayed in the mode line,
  215. in certain major modes."
  216. :global t :group 'which-func
  217. (when (timerp which-func-update-timer)
  218. (cancel-timer which-func-update-timer))
  219. (setq which-func-update-timer nil)
  220. (if which-function-mode
  221. ;;Turn it on
  222. (progn
  223. (setq which-func-update-timer
  224. (run-with-idle-timer idle-update-delay t 'which-func-update))
  225. (dolist (buf (buffer-list))
  226. (with-current-buffer buf
  227. (setq which-func-mode
  228. (or (eq which-func-modes t)
  229. (member major-mode which-func-modes))))))
  230. ;; Turn it off
  231. (dolist (buf (buffer-list))
  232. (with-current-buffer buf (setq which-func-mode nil)))))
  233. (defvar which-function-imenu-failed nil
  234. "Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
  235. (defvar which-func-functions nil
  236. "List of functions for `which-function' to call with no arguments.
  237. It calls them sequentially, and if any returns non-nil,
  238. `which-function' uses that name and stops looking for the name.")
  239. (defun which-function ()
  240. "Return current function name based on point.
  241. Uses `which-func-functions', `imenu--index-alist'
  242. or `add-log-current-defun'.
  243. If no function name is found, return nil."
  244. (let ((name
  245. ;; Try the `which-func-functions' functions first.
  246. (run-hook-with-args-until-success 'which-func-functions)))
  247. ;; If Imenu is loaded, try to make an index alist with it.
  248. (when (and (null name)
  249. (boundp 'imenu--index-alist) (null imenu--index-alist)
  250. (null which-function-imenu-failed))
  251. (imenu--make-index-alist t)
  252. (unless imenu--index-alist
  253. (set (make-local-variable 'which-function-imenu-failed) t)))
  254. ;; If we have an index alist, use it.
  255. (when (and (null name)
  256. (boundp 'imenu--index-alist) imenu--index-alist)
  257. (let ((alist imenu--index-alist)
  258. (minoffset (point-max))
  259. offset pair mark imstack namestack)
  260. ;; Elements of alist are either ("name" . marker), or
  261. ;; ("submenu" ("name" . marker) ... ). The list can be
  262. ;; arbitrarily nested.
  263. (while (or alist imstack)
  264. (if (null alist)
  265. (setq alist (car imstack)
  266. namestack (cdr namestack)
  267. imstack (cdr imstack))
  268. (setq pair (car-safe alist)
  269. alist (cdr-safe alist))
  270. (cond
  271. ((atom pair)) ; Skip anything not a cons.
  272. ((imenu--subalist-p pair)
  273. (setq imstack (cons alist imstack)
  274. namestack (cons (car pair) namestack)
  275. alist (cdr pair)))
  276. ((number-or-marker-p (setq mark (cdr pair)))
  277. (when (and (>= (setq offset (- (point) mark)) 0)
  278. (< offset minoffset)) ; Find the closest item.
  279. (setq minoffset offset
  280. name (if (null which-func-imenu-joiner-function)
  281. (car pair)
  282. (funcall
  283. which-func-imenu-joiner-function
  284. (reverse (cons (car pair) namestack))))))))))))
  285. ;; Try using add-log support.
  286. (when (null name)
  287. (setq name (add-log-current-defun)))
  288. ;; Filter the name if requested.
  289. (when name
  290. (if which-func-cleanup-function
  291. (funcall which-func-cleanup-function name)
  292. name))))
  293. ;;; Integration with other packages
  294. (defun which-func-update-ediff-windows ()
  295. "Update Which-Function mode display for Ediff windows.
  296. This function is meant to be called from `ediff-select-hook'."
  297. (when (eq major-mode 'ediff-mode)
  298. (when ediff-window-A
  299. (which-func-update-1 ediff-window-A))
  300. (when ediff-window-B
  301. (which-func-update-1 ediff-window-B))
  302. (when ediff-window-C
  303. (which-func-update-1 ediff-window-C))))
  304. (add-hook 'ediff-select-hook 'which-func-update-ediff-windows)
  305. (provide 'which-func)
  306. ;;; which-func.el ends here