2015-11-30 10:52:12 -05:00
|
|
|
|
;;; which-func.el --- print current function in mode line -*- lexical-binding:t -*-
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
2023-01-01 05:31:12 -05:00
|
|
|
|
;; Copyright (C) 1994-2023 Free Software Foundation, Inc.
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
2022-11-25 15:01:03 +01:00
|
|
|
|
;; Author: Alex Rezinsky <alexr@msil.sps.mot.com>
|
|
|
|
|
;; Maintainer: emacs-devel@gnu.org
|
2003-05-30 23:31:15 +00:00
|
|
|
|
;; Keywords: mode-line, imenu, tools
|
|
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
2008-05-06 07:25:26 +00:00
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2003-05-30 23:31:15 +00:00
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 07:25:26 +00:00
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 15:52:52 -07:00
|
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; This package prints name of function where your current point is
|
2021-04-22 18:02:06 +02:00
|
|
|
|
;; located in mode line. It assumes that you work with the imenu
|
|
|
|
|
;; package and `imenu--index-alist' is up to date.
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
|
|
|
|
;; TODO LIST
|
|
|
|
|
;; ---------
|
|
|
|
|
;; 1. Dependence on imenu package should be removed. Separate
|
|
|
|
|
;; function determination mechanism should be used to determine the end
|
|
|
|
|
;; of a function as well as the beginning of a function.
|
|
|
|
|
;; 2. This package should be realized with the help of overlay
|
2021-04-22 18:02:06 +02:00
|
|
|
|
;; properties instead of the `imenu--index-alist' variable.
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
|
|
|
|
;;; History:
|
|
|
|
|
|
|
|
|
|
;; THANKS TO
|
|
|
|
|
;; ---------
|
|
|
|
|
;; Per Abrahamsen <abraham@iesd.auc.dk>
|
2022-11-25 15:01:03 +01:00
|
|
|
|
;; Some ideas (inserting in mode-line, using of post-command hook
|
|
|
|
|
;; and toggling this mode) have been borrowed from his package
|
2003-05-30 23:31:15 +00:00
|
|
|
|
;; column.el
|
|
|
|
|
;; Peter Eisenhauer <pipe@fzi.de>
|
|
|
|
|
;; Bug fixing in case nested indexes.
|
|
|
|
|
;; Terry Tateyama <ttt@ursa0.cs.utah.edu>
|
|
|
|
|
;; Suggestion to use find-file-hook for first imenu
|
|
|
|
|
;; index building.
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
;; Variables for customization
|
|
|
|
|
;; ---------------------------
|
|
|
|
|
;;
|
2022-05-24 18:19:57 -04:00
|
|
|
|
(defvar which-func-unknown "n/a"
|
2003-05-30 23:31:15 +00:00
|
|
|
|
"String to display in the mode line when current function is unknown.")
|
|
|
|
|
|
|
|
|
|
(defgroup which-func nil
|
2012-06-02 18:56:09 +08:00
|
|
|
|
"Display the current function name in the mode line."
|
2003-05-30 23:31:15 +00:00
|
|
|
|
:group 'tools
|
|
|
|
|
:version "20.3")
|
|
|
|
|
|
2012-04-13 22:31:50 -04:00
|
|
|
|
(defcustom which-func-modes t
|
|
|
|
|
;; '(emacs-lisp-mode c-mode c++-mode objc-mode perl-mode cperl-mode python-mode
|
|
|
|
|
;; makefile-mode sh-mode fortran-mode f90-mode ada-mode
|
|
|
|
|
;; diff-mode)
|
2003-05-30 23:31:15 +00:00
|
|
|
|
"List of major modes for which Which Function mode should be used.
|
|
|
|
|
For other modes it is disabled. If this is equal to t,
|
|
|
|
|
then Which Function mode is enabled in any major mode that supports it."
|
2012-08-15 09:29:11 -07:00
|
|
|
|
:version "24.3" ; explicit list -> t
|
2003-05-30 23:31:15 +00:00
|
|
|
|
:type '(choice (const :tag "All modes" t)
|
|
|
|
|
(repeat (symbol :tag "Major mode"))))
|
|
|
|
|
|
|
|
|
|
(defcustom which-func-non-auto-modes nil
|
|
|
|
|
"List of major modes where Which Function mode is inactive till Imenu is used.
|
|
|
|
|
This means that Which Function mode won't really do anything
|
|
|
|
|
until you use Imenu, in these modes. Note that files
|
|
|
|
|
larger than `which-func-maxout' behave in this way too;
|
2022-12-16 17:43:46 +02:00
|
|
|
|
Which Function mode doesn't do anything until you use Imenu.
|
|
|
|
|
|
|
|
|
|
If Which Function delays the initial display of buffers too much,
|
|
|
|
|
e.g., when it is used with Eglot, and the language server takes a
|
|
|
|
|
long time to send the information, you can use this option to delay
|
|
|
|
|
activation of Which Function until Imenu is used for the first time."
|
2003-05-30 23:31:15 +00:00
|
|
|
|
:type '(repeat (symbol :tag "Major mode")))
|
|
|
|
|
|
|
|
|
|
(defcustom which-func-maxout 500000
|
|
|
|
|
"Don't automatically compute the Imenu menu if buffer is this big or bigger.
|
2022-12-16 17:43:46 +02:00
|
|
|
|
Zero means compute the Imenu menu regardless of size.
|
|
|
|
|
|
|
|
|
|
If Which Function delays the initial display of buffers too much,
|
|
|
|
|
e.g., when it is used with Eglot, and the language server takes a
|
|
|
|
|
long time to send the information, you can use this option to delay
|
|
|
|
|
activation of Which Function until Imenu is used for the first time."
|
2003-05-30 23:31:15 +00:00
|
|
|
|
:type 'integer)
|
|
|
|
|
|
2004-07-17 08:10:28 +00:00
|
|
|
|
(defvar which-func-keymap
|
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
|
(define-key map [mode-line mouse-1] 'beginning-of-defun)
|
|
|
|
|
(define-key map [mode-line mouse-2]
|
|
|
|
|
(lambda ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(if (eq (point-min) 1)
|
|
|
|
|
(narrow-to-defun)
|
|
|
|
|
(widen))))
|
|
|
|
|
(define-key map [mode-line mouse-3] 'end-of-defun)
|
|
|
|
|
map)
|
|
|
|
|
"Keymap to display on mode line which-func.")
|
|
|
|
|
|
2005-06-15 00:14:41 +00:00
|
|
|
|
(defface which-func
|
2005-06-15 04:35:23 +00:00
|
|
|
|
;; Whether `font-lock-function-name-face' is an appropriate face to
|
|
|
|
|
;; inherit depends on the mode-line face; define several variants based
|
|
|
|
|
;; on the default mode-line face.
|
|
|
|
|
'(;; The default mode-line face on a high-color display is a relatively
|
|
|
|
|
;; light color ("grey75"), and only the light-background variant of
|
|
|
|
|
;; `font-lock-function-name-face' is visible against it.
|
|
|
|
|
(((class color) (min-colors 88) (background light))
|
|
|
|
|
:inherit font-lock-function-name-face)
|
|
|
|
|
;; The default mode-line face on other display types is inverse-video;
|
|
|
|
|
;; it seems that only in the dark-background case is
|
|
|
|
|
;; `font-lock-function-name-face' visible against it.
|
|
|
|
|
(((class grayscale mono) (background dark))
|
|
|
|
|
:inherit font-lock-function-name-face)
|
|
|
|
|
(((class color) (background light))
|
|
|
|
|
:inherit font-lock-function-name-face)
|
|
|
|
|
;; If none of the above cases, use an explicit color chosen to contrast
|
|
|
|
|
;; well with the default mode-line face.
|
|
|
|
|
(((class color) (min-colors 88) (background dark))
|
|
|
|
|
:foreground "Blue1")
|
|
|
|
|
(((background dark))
|
|
|
|
|
:foreground "Blue1")
|
|
|
|
|
(t
|
|
|
|
|
:foreground "LightSkyBlue"))
|
2015-11-30 10:52:12 -05:00
|
|
|
|
"Face used to highlight mode line function names.")
|
2004-07-17 08:10:28 +00:00
|
|
|
|
|
|
|
|
|
(defcustom which-func-format
|
|
|
|
|
`("["
|
|
|
|
|
(:propertize which-func-current
|
|
|
|
|
local-map ,which-func-keymap
|
2005-06-15 00:14:41 +00:00
|
|
|
|
face which-func
|
2012-06-25 00:11:03 -07:00
|
|
|
|
mouse-face mode-line-highlight
|
2021-09-23 13:20:55 +02:00
|
|
|
|
help-echo ,(concat
|
|
|
|
|
"Current function\n"
|
|
|
|
|
"mouse-1: go to beginning\n"
|
|
|
|
|
"mouse-2: toggle rest visibility\n"
|
|
|
|
|
"mouse-3: go to end"))
|
2004-07-17 08:10:28 +00:00
|
|
|
|
"]")
|
2003-05-30 23:31:15 +00:00
|
|
|
|
"Format for displaying the function in the mode line."
|
2021-09-23 13:20:55 +02:00
|
|
|
|
:version "28.1"
|
2003-05-30 23:31:15 +00:00
|
|
|
|
:type 'sexp)
|
|
|
|
|
;;;###autoload (put 'which-func-format 'risky-local-variable t)
|
|
|
|
|
|
2010-11-23 21:21:55 -05:00
|
|
|
|
(defvar which-func-imenu-joiner-function (lambda (x) (car (last x)))
|
2008-09-30 03:42:56 +00:00
|
|
|
|
"Function to join together multiple levels of imenu nomenclature.
|
|
|
|
|
Called with a single argument, a list of strings giving the names
|
|
|
|
|
of the menus we had to traverse to get to the item. Returns a
|
|
|
|
|
single string, the new name of the item.")
|
2008-09-30 03:41:35 +00:00
|
|
|
|
|
2003-05-30 23:31:15 +00:00
|
|
|
|
(defvar which-func-cleanup-function nil
|
|
|
|
|
"Function to transform a string before displaying it in the mode line.
|
|
|
|
|
The function is called with one argument, the string to display.
|
2012-06-02 18:56:09 +08:00
|
|
|
|
Its return value is displayed in the mode line.
|
2003-05-30 23:31:15 +00:00
|
|
|
|
If nil, no function is called. The default value is nil.
|
|
|
|
|
|
|
|
|
|
This feature can be useful if Imenu is set up to make more
|
|
|
|
|
detailed entries (e.g., containing the argument list of a function),
|
|
|
|
|
and you want to simplify them for the mode line
|
|
|
|
|
\(e.g., removing the parameter list to just have the function name.)")
|
|
|
|
|
|
|
|
|
|
;;; Code, nothing to customize below here
|
|
|
|
|
;;; -------------------------------------
|
|
|
|
|
;;;
|
|
|
|
|
(require 'imenu)
|
|
|
|
|
|
|
|
|
|
(defvar which-func-table (make-hash-table :test 'eq :weakness 'key))
|
|
|
|
|
|
|
|
|
|
(defconst which-func-current
|
Use string-replace instead of replace-regexp-in-string
`string-replace` is easier to understand, less error-prone, much
faster, and results in shorter Lisp and byte code. Use it where
applicable and obviously safe (erring on the conservative side).
* admin/authors.el (authors-scan-change-log):
* lisp/autoinsert.el (auto-insert-alist):
* lisp/calc/calc-prog.el (calc-edit-macro-combine-alg-ent)
(calc-edit-macro-combine-ext-command)
(calc-edit-macro-combine-var-name):
* lisp/calc/calc-units.el (math-make-unit-string):
* lisp/calendar/cal-html.el (cal-html-comment):
* lisp/calendar/cal-tex.el (cal-tex-comment):
* lisp/calendar/icalendar.el (icalendar--convert-string-for-export)
(icalendar--convert-string-for-import):
* lisp/calendar/iso8601.el (iso8601--concat-regexps)
(iso8601--full-time-match, iso8601--combined-match):
* lisp/calendar/time-date.el (format-seconds):
* lisp/calendar/todo-mode.el (todo-filter-items-filename):
* lisp/cedet/cedet-files.el (cedet-directory-name-to-file-name)
(cedet-file-name-to-directory-name):
* lisp/comint.el (comint-watch-for-password-prompt):
* lisp/dired-aux.el (dired-do-chmod):
* lisp/dired-x.el (dired-man):
* lisp/dired.el (dired-insert-directory, dired-goto-file-1):
* lisp/emacs-lisp/comp.el (comp-c-func-name):
* lisp/emacs-lisp/re-builder.el (reb-copy):
* lisp/erc/erc-dcc.el (erc-dcc-unquote-filename):
* lisp/erc/erc.el (erc-quit-reason-zippy, erc-part-reason-zippy)
(erc-update-mode-line-buffer, erc-message-english-PART):
* lisp/files.el (make-backup-file-name-1, files--transform-file-name)
(read-file-modes):
* lisp/fringe.el (fringe-mode):
* lisp/gnus/gnus-art.el (gnus-button-handle-info-url):
* lisp/gnus/gnus-group.el (gnus-group-completing-read):
* lisp/gnus/gnus-icalendar.el (gnus-icalendar-event-from-ical):
* lisp/gnus/gnus-mlspl.el (gnus-group-split-fancy):
* lisp/gnus/gnus-search.el (gnus-search-query-parse-date)
(gnus-search-transform-expression, gnus-search-run-search):
* lisp/gnus/gnus-start.el (gnus-dribble-enter):
* lisp/gnus/gnus-sum.el (gnus-summary-refer-article):
* lisp/gnus/gnus-util.el (gnus-mode-string-quote):
* lisp/gnus/message.el (message-put-addresses-in-ecomplete)
(message-parse-mailto-url, message-mailto-1):
* lisp/gnus/mml-sec.el (mml-secure-epg-sign):
* lisp/gnus/mml-smime.el (mml-smime-epg-verify):
* lisp/gnus/mml2015.el (mml2015-epg-verify):
* lisp/gnus/nnmaildir.el (nnmaildir--system-name)
(nnmaildir-request-list, nnmaildir-retrieve-groups)
(nnmaildir-request-group, nnmaildir-retrieve-headers):
* lisp/gnus/nnrss.el (nnrss-node-text):
* lisp/gnus/spam-report.el (spam-report-gmane-internal)
(spam-report-user-mail-address):
* lisp/ibuffer.el (name):
* lisp/image-dired.el (image-dired-pngnq-thumb)
(image-dired-pngcrush-thumb, image-dired-optipng-thumb)
(image-dired-create-thumb-1):
* lisp/info.el (Info-set-mode-line):
* lisp/international/mule-cmds.el (describe-language-environment):
* lisp/mail/rfc2231.el (rfc2231-parse-string):
* lisp/mail/rfc2368.el (rfc2368-parse-mailto-url):
* lisp/mail/rmail.el (rmail-insert-inbox-text)
(rmail-simplified-subject-regexp):
* lisp/mail/rmailout.el (rmail-output-body-to-file):
* lisp/mail/undigest.el (rmail-digest-rfc1153):
* lisp/man.el (Man-default-man-entry):
* lisp/mouse.el (minor-mode-menu-from-indicator):
* lisp/mpc.el (mpc--debug):
* lisp/net/browse-url.el (browse-url-mail):
* lisp/net/eww.el (eww-update-header-line-format):
* lisp/net/newst-backend.el (newsticker-save-item):
* lisp/net/rcirc.el (rcirc-sentinel):
* lisp/net/soap-client.el (soap-decode-date-time):
* lisp/nxml/rng-cmpct.el (rng-c-literal-2-re):
* lisp/nxml/xmltok.el (let*):
* lisp/obsolete/nnir.el (nnir-run-swish-e, nnir-run-hyrex)
(nnir-run-find-grep):
* lisp/play/dunnet.el (dun-doassign):
* lisp/play/handwrite.el (handwrite):
* lisp/proced.el (proced-format-args):
* lisp/profiler.el (profiler-report-header-line-format):
* lisp/progmodes/gdb-mi.el (gdb-mi-quote):
* lisp/progmodes/make-mode.el (makefile-bsdmake-rule-action-regex)
(makefile-make-font-lock-keywords):
* lisp/progmodes/prolog.el (prolog-guess-fill-prefix):
* lisp/progmodes/ruby-mode.el (ruby-toggle-string-quotes):
* lisp/progmodes/sql.el (sql-remove-tabs-filter, sql-str-literal):
* lisp/progmodes/which-func.el (which-func-current):
* lisp/replace.el (query-replace-read-from)
(occur-engine, replace-quote):
* lisp/select.el (xselect--encode-string):
* lisp/ses.el (ses-export-tab):
* lisp/subr.el (shell-quote-argument):
* lisp/term/pc-win.el (msdos-show-help):
* lisp/term/w32-win.el (w32--set-selection):
* lisp/term/xterm.el (gui-backend-set-selection):
* lisp/textmodes/picture.el (picture-tab-search):
* lisp/thumbs.el (thumbs-call-setroot-command):
* lisp/tooltip.el (tooltip-show-help-non-mode):
* lisp/transient.el (transient-format-key):
* lisp/url/url-mailto.el (url-mailto):
* lisp/vc/log-edit.el (log-edit-changelog-ours-p):
* lisp/vc/vc-bzr.el (vc-bzr-status):
* lisp/vc/vc-hg.el (vc-hg--glob-to-pcre):
* lisp/vc/vc-svn.el (vc-svn-after-dir-status):
* lisp/xdg.el (xdg-desktop-strings):
* test/lisp/electric-tests.el (defun):
* test/lisp/term-tests.el (term-simple-lines):
* test/lisp/time-stamp-tests.el (formatz-mod-del-colons):
* test/lisp/wdired-tests.el (wdired-test-bug32173-01)
(wdired-test-unfinished-edit-01):
* test/src/json-tests.el (json-parse-with-custom-null-and-false-objects):
Use `string-replace` instead of `replace-regexp-in-string`.
2021-08-08 18:58:46 +02:00
|
|
|
|
'(:eval (string-replace
|
2012-04-21 20:25:20 +02:00
|
|
|
|
"%" "%%"
|
2012-10-22 09:29:28 -04:00
|
|
|
|
(or (gethash (selected-window) which-func-table)
|
|
|
|
|
which-func-unknown))))
|
2003-05-30 23:31:15 +00:00
|
|
|
|
;;;###autoload (put 'which-func-current 'risky-local-variable t)
|
|
|
|
|
|
2014-09-03 15:10:15 -04:00
|
|
|
|
(defvar-local which-func-mode nil
|
2003-05-30 23:31:15 +00:00
|
|
|
|
"Non-nil means display current function name in mode line.
|
2021-10-04 01:08:03 +02:00
|
|
|
|
This makes a difference only if variable `which-function-mode' is
|
|
|
|
|
non-nil.")
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
2020-12-18 09:16:29 -05:00
|
|
|
|
(add-hook 'after-change-major-mode-hook #'which-func-ff-hook t)
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
2015-11-30 10:52:12 -05:00
|
|
|
|
(defun which-func-try-to-enable ()
|
|
|
|
|
(unless (or (not which-function-mode)
|
|
|
|
|
(local-variable-p 'which-func-mode))
|
|
|
|
|
(setq which-func-mode (or (eq which-func-modes t)
|
|
|
|
|
(member major-mode which-func-modes)))))
|
|
|
|
|
|
2003-05-30 23:31:15 +00:00
|
|
|
|
(defun which-func-ff-hook ()
|
2020-05-20 18:02:13 +00:00
|
|
|
|
"`after-change-major-mode-hook' for Which Function mode.
|
2003-05-30 23:31:15 +00:00
|
|
|
|
It creates the Imenu index for the buffer, if necessary."
|
2015-11-30 10:52:12 -05:00
|
|
|
|
(which-func-try-to-enable)
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
2010-12-03 19:49:49 -05:00
|
|
|
|
(condition-case err
|
2003-05-30 23:31:15 +00:00
|
|
|
|
(if (and which-func-mode
|
|
|
|
|
(not (member major-mode which-func-non-auto-modes))
|
|
|
|
|
(or (null which-func-maxout)
|
|
|
|
|
(< buffer-saved-size which-func-maxout)
|
|
|
|
|
(= which-func-maxout 0)))
|
|
|
|
|
(setq imenu--index-alist
|
2015-08-29 18:06:13 -07:00
|
|
|
|
(save-excursion (funcall imenu-create-index-function))))
|
|
|
|
|
(imenu-unavailable
|
|
|
|
|
(setq which-func-mode nil))
|
2003-05-30 23:31:15 +00:00
|
|
|
|
(error
|
2015-08-29 18:06:13 -07:00
|
|
|
|
(message "which-func-ff-hook error: %S" err)
|
2003-05-30 23:31:15 +00:00
|
|
|
|
(setq which-func-mode nil))))
|
|
|
|
|
|
|
|
|
|
(defun which-func-update ()
|
2022-03-07 16:19:19 +01:00
|
|
|
|
"Update the Which-Function mode display in the current window."
|
2003-05-30 23:31:15 +00:00
|
|
|
|
;; (walk-windows 'which-func-update-1 nil 'visible))
|
2020-12-18 09:16:29 -05:00
|
|
|
|
(let ((non-essential t))
|
|
|
|
|
(which-func-update-1 (selected-window))))
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
|
|
|
|
(defun which-func-update-1 (window)
|
2004-08-16 22:46:26 +00:00
|
|
|
|
"Update the Which Function mode display for window WINDOW."
|
2003-05-30 23:31:15 +00:00
|
|
|
|
(with-selected-window window
|
|
|
|
|
(when which-func-mode
|
|
|
|
|
(condition-case info
|
|
|
|
|
(let ((current (which-function)))
|
|
|
|
|
(unless (equal current (gethash window which-func-table))
|
|
|
|
|
(puthash window current which-func-table)
|
|
|
|
|
(force-mode-line-update)))
|
|
|
|
|
(error
|
2005-05-29 08:31:54 +00:00
|
|
|
|
(setq which-func-mode nil)
|
2010-12-03 19:49:49 -05:00
|
|
|
|
(error "Error in which-func-update: %S" info))))))
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
|
|
|
|
(defvar which-func-update-timer nil)
|
|
|
|
|
|
2015-11-30 10:52:12 -05:00
|
|
|
|
(unless (or (assq 'which-func-mode mode-line-misc-info)
|
|
|
|
|
(assq 'which-function-mode mode-line-misc-info))
|
|
|
|
|
(add-to-list 'mode-line-misc-info
|
|
|
|
|
'(which-function-mode ;Only display if mode is enabled.
|
|
|
|
|
(which-func-mode ;Only display if buffer supports it.
|
|
|
|
|
("" which-func-format " ")))))
|
|
|
|
|
|
2003-05-30 23:31:15 +00:00
|
|
|
|
;; This is the name people would normally expect.
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(define-minor-mode which-function-mode
|
2011-10-19 20:26:14 -04:00
|
|
|
|
"Toggle mode line display of current function (Which Function mode).
|
|
|
|
|
|
|
|
|
|
Which Function mode is a global minor mode. When enabled, the
|
|
|
|
|
current function name is continuously displayed in the mode line,
|
|
|
|
|
in certain major modes."
|
2003-05-30 23:31:15 +00:00
|
|
|
|
:global t :group 'which-func
|
2010-11-23 21:21:55 -05:00
|
|
|
|
(when (timerp which-func-update-timer)
|
|
|
|
|
(cancel-timer which-func-update-timer))
|
|
|
|
|
(setq which-func-update-timer nil)
|
2015-11-30 10:52:12 -05:00
|
|
|
|
(when which-function-mode
|
|
|
|
|
;;Turn it on.
|
|
|
|
|
(setq which-func-update-timer
|
|
|
|
|
(run-with-idle-timer idle-update-delay t #'which-func-update))
|
|
|
|
|
(dolist (buf (buffer-list))
|
|
|
|
|
(with-current-buffer buf (which-func-try-to-enable)))))
|
2003-05-30 23:31:15 +00:00
|
|
|
|
|
|
|
|
|
(defvar which-function-imenu-failed nil
|
|
|
|
|
"Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
|
|
|
|
|
|
2003-09-30 12:53:26 +00:00
|
|
|
|
(defvar which-func-functions nil
|
|
|
|
|
"List of functions for `which-function' to call with no arguments.
|
|
|
|
|
It calls them sequentially, and if any returns non-nil,
|
|
|
|
|
`which-function' uses that name and stops looking for the name.")
|
|
|
|
|
|
2003-05-30 23:31:15 +00:00
|
|
|
|
(defun which-function ()
|
|
|
|
|
"Return current function name based on point.
|
2018-12-11 08:29:50 -06:00
|
|
|
|
Uses `which-func-functions', `add-log-current-defun'.
|
|
|
|
|
or `imenu--index-alist'
|
2003-05-30 23:31:15 +00:00
|
|
|
|
If no function name is found, return nil."
|
2004-12-05 16:58:13 +00:00
|
|
|
|
(let ((name
|
2006-07-27 12:18:03 +00:00
|
|
|
|
;; Try the `which-func-functions' functions first.
|
2004-12-05 16:58:13 +00:00
|
|
|
|
(run-hook-with-args-until-success 'which-func-functions)))
|
2018-12-11 08:29:50 -06:00
|
|
|
|
;; Try using add-log support.
|
|
|
|
|
(when (null name)
|
|
|
|
|
(setq name (add-log-current-defun)))
|
2003-05-30 23:31:15 +00:00
|
|
|
|
;; If Imenu is loaded, try to make an index alist with it.
|
2020-05-20 18:02:13 +00:00
|
|
|
|
;; If `add-log-current-defun' ran and gave nil, accept that.
|
2003-09-30 12:53:26 +00:00
|
|
|
|
(when (and (null name)
|
2020-05-20 18:02:13 +00:00
|
|
|
|
(null add-log-current-defun-function))
|
|
|
|
|
(when (and (null name)
|
|
|
|
|
(boundp 'imenu--index-alist)
|
|
|
|
|
(or (null imenu--index-alist)
|
|
|
|
|
;; Update if outdated
|
|
|
|
|
(/= (buffer-chars-modified-tick) imenu-menubar-modified-tick))
|
|
|
|
|
(null which-function-imenu-failed))
|
|
|
|
|
(ignore-errors (imenu--make-index-alist t))
|
|
|
|
|
(unless imenu--index-alist
|
2020-12-06 08:57:54 +01:00
|
|
|
|
(setq-local which-function-imenu-failed t)))
|
2020-05-20 18:02:13 +00:00
|
|
|
|
;; If we have an index alist, use it.
|
|
|
|
|
(when (and (null name)
|
|
|
|
|
(boundp 'imenu--index-alist) imenu--index-alist)
|
|
|
|
|
(let ((alist imenu--index-alist)
|
|
|
|
|
(minoffset (point-max))
|
|
|
|
|
offset pair mark imstack namestack)
|
|
|
|
|
;; Elements of alist are either ("name" . marker), or
|
|
|
|
|
;; ("submenu" ("name" . marker) ... ). The list can be
|
|
|
|
|
;; arbitrarily nested.
|
|
|
|
|
(while (or alist imstack)
|
|
|
|
|
(if (null alist)
|
|
|
|
|
(setq alist (car imstack)
|
|
|
|
|
namestack (cdr namestack)
|
|
|
|
|
imstack (cdr imstack))
|
|
|
|
|
|
|
|
|
|
(setq pair (car-safe alist)
|
|
|
|
|
alist (cdr-safe alist))
|
|
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
((atom pair)) ; Skip anything not a cons.
|
|
|
|
|
|
|
|
|
|
((imenu--subalist-p pair)
|
|
|
|
|
(setq imstack (cons alist imstack)
|
|
|
|
|
namestack (cons (car pair) namestack)
|
|
|
|
|
alist (cdr pair)))
|
|
|
|
|
|
|
|
|
|
((or (number-or-marker-p (setq mark (cdr pair)))
|
|
|
|
|
(and (overlayp mark)
|
|
|
|
|
(setq mark (overlay-start mark))))
|
|
|
|
|
(when (and (>= (setq offset (- (point) mark)) 0)
|
|
|
|
|
(< offset minoffset)) ; Find the closest item.
|
|
|
|
|
(setq minoffset offset
|
|
|
|
|
name (if (null which-func-imenu-joiner-function)
|
|
|
|
|
(car pair)
|
|
|
|
|
(funcall
|
|
|
|
|
which-func-imenu-joiner-function
|
|
|
|
|
(reverse (cons (car pair) namestack)))))))))))))
|
2003-05-30 23:31:15 +00:00
|
|
|
|
;; Filter the name if requested.
|
|
|
|
|
(when name
|
|
|
|
|
(if which-func-cleanup-function
|
|
|
|
|
(funcall which-func-cleanup-function name)
|
|
|
|
|
name))))
|
|
|
|
|
|
2012-06-03 19:10:44 +08:00
|
|
|
|
|
|
|
|
|
;;; Integration with other packages
|
|
|
|
|
|
2013-05-23 20:49:17 -07:00
|
|
|
|
(defvar ediff-window-A)
|
|
|
|
|
(defvar ediff-window-B)
|
|
|
|
|
(defvar ediff-window-C)
|
|
|
|
|
|
2014-09-03 15:10:15 -04:00
|
|
|
|
;; FIXME: Why does ediff require special support?
|
2012-06-03 19:10:44 +08:00
|
|
|
|
(defun which-func-update-ediff-windows ()
|
|
|
|
|
"Update Which-Function mode display for Ediff windows.
|
|
|
|
|
This function is meant to be called from `ediff-select-hook'."
|
2014-09-03 15:10:15 -04:00
|
|
|
|
(when (and (derived-mode-p 'ediff-mode) which-function-mode)
|
2012-06-03 19:10:44 +08:00
|
|
|
|
(when ediff-window-A
|
|
|
|
|
(which-func-update-1 ediff-window-A))
|
|
|
|
|
(when ediff-window-B
|
|
|
|
|
(which-func-update-1 ediff-window-B))
|
|
|
|
|
(when ediff-window-C
|
|
|
|
|
(which-func-update-1 ediff-window-C))))
|
|
|
|
|
|
2020-12-18 09:16:29 -05:00
|
|
|
|
(add-hook 'ediff-select-hook #'which-func-update-ediff-windows)
|
2012-06-03 19:10:44 +08:00
|
|
|
|
|
2003-05-30 23:31:15 +00:00
|
|
|
|
(provide 'which-func)
|
|
|
|
|
|
|
|
|
|
;;; which-func.el ends here
|