which-func.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. "Mode to display the current function name in the modeline."
  57. :group 'tools
  58. :version "20.3")
  59. (defcustom which-func-modes
  60. '(emacs-lisp-mode c-mode c++-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. :type '(choice (const :tag "All modes" t)
  68. (repeat (symbol :tag "Major mode"))))
  69. (defcustom which-func-non-auto-modes nil
  70. "List of major modes where Which Function mode is inactive till Imenu is used.
  71. This means that Which Function mode won't really do anything
  72. until you use Imenu, in these modes. Note that files
  73. larger than `which-func-maxout' behave in this way too;
  74. Which Function mode doesn't do anything until you use Imenu."
  75. :group 'which-func
  76. :type '(repeat (symbol :tag "Major mode")))
  77. (defcustom which-func-maxout 500000
  78. "Don't automatically compute the Imenu menu if buffer is this big or bigger.
  79. Zero means compute the Imenu menu regardless of size."
  80. :group 'which-func
  81. :type 'integer)
  82. (defvar which-func-keymap
  83. (let ((map (make-sparse-keymap)))
  84. (define-key map [mode-line mouse-1] 'beginning-of-defun)
  85. (define-key map [mode-line mouse-2]
  86. (lambda ()
  87. (interactive)
  88. (if (eq (point-min) 1)
  89. (narrow-to-defun)
  90. (widen))))
  91. (define-key map [mode-line mouse-3] 'end-of-defun)
  92. map)
  93. "Keymap to display on mode line which-func.")
  94. (defface which-func
  95. ;; Whether `font-lock-function-name-face' is an appropriate face to
  96. ;; inherit depends on the mode-line face; define several variants based
  97. ;; on the default mode-line face.
  98. '(;; The default mode-line face on a high-color display is a relatively
  99. ;; light color ("grey75"), and only the light-background variant of
  100. ;; `font-lock-function-name-face' is visible against it.
  101. (((class color) (min-colors 88) (background light))
  102. :inherit font-lock-function-name-face)
  103. ;; The default mode-line face on other display types is inverse-video;
  104. ;; it seems that only in the dark-background case is
  105. ;; `font-lock-function-name-face' visible against it.
  106. (((class grayscale mono) (background dark))
  107. :inherit font-lock-function-name-face)
  108. (((class color) (background light))
  109. :inherit font-lock-function-name-face)
  110. ;; If none of the above cases, use an explicit color chosen to contrast
  111. ;; well with the default mode-line face.
  112. (((class color) (min-colors 88) (background dark))
  113. :foreground "Blue1")
  114. (((background dark))
  115. :foreground "Blue1")
  116. (t
  117. :foreground "LightSkyBlue"))
  118. "Face used to highlight mode line function names."
  119. :group 'which-func)
  120. (defcustom which-func-format
  121. `("["
  122. (:propertize which-func-current
  123. local-map ,which-func-keymap
  124. face which-func
  125. mouse-face mode-line-highlight
  126. help-echo "mouse-1: go to beginning\n\
  127. mouse-2: toggle rest visibility\n\
  128. mouse-3: go to end")
  129. "]")
  130. "Format for displaying the function in the mode line."
  131. :version "24.2" ; added mouse-face
  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 modeline.
  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 '(error "This buffer cannot use `imenu-default-create-index-function'"))
  182. (message "which-func-ff-hook error: %S" err))
  183. (setq which-func-mode nil))))
  184. (defun which-func-update ()
  185. ;; "Update the Which-Function mode display for all windows."
  186. ;; (walk-windows 'which-func-update-1 nil 'visible))
  187. (which-func-update-1 (selected-window)))
  188. (defun which-func-update-1 (window)
  189. "Update the Which Function mode display for window WINDOW."
  190. (with-selected-window window
  191. (when which-func-mode
  192. (condition-case info
  193. (let ((current (which-function)))
  194. (unless (equal current (gethash window which-func-table))
  195. (puthash window current which-func-table)
  196. (force-mode-line-update)))
  197. (error
  198. (setq which-func-mode nil)
  199. (error "Error in which-func-update: %S" info))))))
  200. ;;;###autoload
  201. (defun which-func-mode (&optional arg)
  202. (which-function-mode arg))
  203. (make-obsolete 'which-func-mode 'which-function-mode "24.1")
  204. (defvar which-func-update-timer nil)
  205. ;; This is the name people would normally expect.
  206. ;;;###autoload
  207. (define-minor-mode which-function-mode
  208. "Toggle mode line display of current function (Which Function mode).
  209. With a prefix argument ARG, enable Which Function mode if ARG is
  210. positive, and disable it otherwise. If called from Lisp, enable
  211. the mode if ARG is omitted or nil.
  212. Which Function mode is a global minor mode. When enabled, the
  213. current function name is continuously displayed in the mode line,
  214. in certain major modes."
  215. :global t :group 'which-func
  216. (when (timerp which-func-update-timer)
  217. (cancel-timer which-func-update-timer))
  218. (setq which-func-update-timer nil)
  219. (if which-function-mode
  220. ;;Turn it on
  221. (progn
  222. (setq which-func-update-timer
  223. (run-with-idle-timer idle-update-delay t 'which-func-update))
  224. (dolist (buf (buffer-list))
  225. (with-current-buffer buf
  226. (setq which-func-mode
  227. (or (eq which-func-modes t)
  228. (member major-mode which-func-modes))))))
  229. ;; Turn it off
  230. (dolist (buf (buffer-list))
  231. (with-current-buffer buf (setq which-func-mode nil)))))
  232. (defvar which-function-imenu-failed nil
  233. "Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
  234. (defvar which-func-functions nil
  235. "List of functions for `which-function' to call with no arguments.
  236. It calls them sequentially, and if any returns non-nil,
  237. `which-function' uses that name and stops looking for the name.")
  238. (defun which-function ()
  239. "Return current function name based on point.
  240. Uses `which-func-functions', `imenu--index-alist'
  241. or `add-log-current-defun'.
  242. If no function name is found, return nil."
  243. (let ((name
  244. ;; Try the `which-func-functions' functions first.
  245. (run-hook-with-args-until-success 'which-func-functions)))
  246. ;; If Imenu is loaded, try to make an index alist with it.
  247. (when (and (null name)
  248. (boundp 'imenu--index-alist) (null imenu--index-alist)
  249. (null which-function-imenu-failed))
  250. (imenu--make-index-alist t)
  251. (unless imenu--index-alist
  252. (set (make-local-variable 'which-function-imenu-failed) t)))
  253. ;; If we have an index alist, use it.
  254. (when (and (null name)
  255. (boundp 'imenu--index-alist) imenu--index-alist)
  256. (let ((alist imenu--index-alist)
  257. (minoffset (point-max))
  258. offset pair mark imstack namestack)
  259. ;; Elements of alist are either ("name" . marker), or
  260. ;; ("submenu" ("name" . marker) ... ). The list can be
  261. ;; arbitrarily nested.
  262. (while (or alist imstack)
  263. (if (null alist)
  264. (setq alist (car imstack)
  265. namestack (cdr namestack)
  266. imstack (cdr imstack))
  267. (setq pair (car-safe alist)
  268. alist (cdr-safe alist))
  269. (cond
  270. ((atom pair)) ; Skip anything not a cons.
  271. ((imenu--subalist-p pair)
  272. (setq imstack (cons alist imstack)
  273. namestack (cons (car pair) namestack)
  274. alist (cdr pair)))
  275. ((number-or-marker-p (setq mark (cdr pair)))
  276. (when (and (>= (setq offset (- (point) mark)) 0)
  277. (< offset minoffset)) ; Find the closest item.
  278. (setq minoffset offset
  279. name (if (null which-func-imenu-joiner-function)
  280. (car pair)
  281. (funcall
  282. which-func-imenu-joiner-function
  283. (reverse (cons (car pair) namestack))))))))))))
  284. ;; Try using add-log support.
  285. (when (null name)
  286. (setq name (add-log-current-defun)))
  287. ;; Filter the name if requested.
  288. (when name
  289. (if which-func-cleanup-function
  290. (funcall which-func-cleanup-function name)
  291. name))))
  292. (provide 'which-func)
  293. ;;; which-func.el ends here