which-func.el 13 KB

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