aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSpencer Baugh2023-10-21 10:41:42 -0400
committerEli Zaretskii2023-10-29 13:21:03 +0200
commit3dca52dd422c50ebf24a304e7c3d36cf5f1c55cf (patch)
treed6d7d6fa292027a2328adbac2d65f28cecf5f82c
parent27ccf2230bced7248a86e3741b45734bde77cb42 (diff)
downloademacs-3dca52dd422c50ebf24a304e7c3d36cf5f1c55cf.tar.gz
emacs-3dca52dd422c50ebf24a304e7c3d36cf5f1c55cf.zip
Remove the header line after disabling 'which-function-mode'
Previously, the header line would stay around even when after disabling 'which-function-mode', although it may be empty. Now the 'which-function-mode' element is properly removed from 'header-line-format', so the header line will disappear if there's nothing else in 'header-line-format'. Also, previously, when we ran (which-function-mode), we would enable 'which-function-mode' for all buffers even if they didn't support imenu. We didn't run the normal logic in 'which-func-ff-hook' to disable 'which-func-mode' if imenu wasn't present. Now we do run that logic, by just calling 'which-func-ff-hook'. This is especially important when the header line is enabled, because otherwise there's a very noticeable header line added to every buffer, including e.g. *Help* and *Buffer List*. Also, we now check that 'header-line-format' is a list before trying to add to it; this makes us work properly when enabling and disabling 'which-function-mode' for modes which set 'header-line-format' to a string or symbol, such as eww. * lisp/progmodes/which-func.el (which-func-try-to-enable): Re-add 'which-func-format' to the header line. (which-func--header-line-remove): New function. (which-func--disable): Call 'which-func--header-line-remove'. (which-function-mode): Call 'which-func-ff-hook' and 'which-func--header-line-remove'. (bug#66283) * test/lisp/progmodes/which-func-tests.el: New test.
-rw-r--r--lisp/progmodes/which-func.el39
-rw-r--r--test/lisp/progmodes/which-func-tests.el58
2 files changed, 82 insertions, 15 deletions
diff --git a/lisp/progmodes/which-func.el b/lisp/progmodes/which-func.el
index 09d0250515f..0e04bab6ea4 100644
--- a/lisp/progmodes/which-func.el
+++ b/lisp/progmodes/which-func.el
@@ -208,21 +208,28 @@ non-nil.")
208(add-hook 'after-change-major-mode-hook #'which-func-ff-hook t) 208(add-hook 'after-change-major-mode-hook #'which-func-ff-hook t)
209 209
210(defun which-func-try-to-enable () 210(defun which-func-try-to-enable ()
211 (unless (or (not which-function-mode) 211 (when which-function-mode
212 (local-variable-p 'which-func-mode)) 212 (unless (local-variable-p 'which-func-mode)
213 (setq which-func-mode (or (eq which-func-modes t) 213 (setq which-func-mode (or (eq which-func-modes t)
214 (member major-mode which-func-modes))) 214 (member major-mode which-func-modes)))
215 (setq which-func--use-mode-line 215 (setq which-func--use-mode-line
216 (member which-func-display '(mode mode-and-header))) 216 (member which-func-display '(mode mode-and-header)))
217 (setq which-func--use-header-line 217 (setq which-func--use-header-line
218 (member which-func-display '(header mode-and-header))) 218 (member which-func-display '(header mode-and-header))))
219 (when (and which-func-mode which-func--use-header-line) 219 ;; We might need to re-add which-func-format to the header line,
220 ;; if which-function-mode was toggled off and on.
221 (when (and which-func-mode which-func--use-header-line
222 (listp header-line-format))
220 (add-to-list 'header-line-format '("" which-func-format " "))))) 223 (add-to-list 'header-line-format '("" which-func-format " ")))))
221 224
222(defun which-func--disable () 225(defun which-func--header-line-remove ()
223 (when (and which-func-mode which-func--use-header-line) 226 (when (and which-func-mode which-func--use-header-line
227 (listp header-line-format))
224 (setq header-line-format 228 (setq header-line-format
225 (delete '("" which-func-format " ") header-line-format))) 229 (delete '("" which-func-format " ") header-line-format))))
230
231(defun which-func--disable ()
232 (which-func--header-line-remove)
226 (setq which-func-mode nil)) 233 (setq which-func-mode nil))
227 234
228(defun which-func-ff-hook () 235(defun which-func-ff-hook ()
@@ -288,9 +295,11 @@ in certain major modes."
288 (when which-function-mode 295 (when which-function-mode
289 ;;Turn it on. 296 ;;Turn it on.
290 (setq which-func-update-timer 297 (setq which-func-update-timer
291 (run-with-idle-timer idle-update-delay t #'which-func-update)) 298 (run-with-idle-timer idle-update-delay t #'which-func-update)))
292 (dolist (buf (buffer-list)) 299 (dolist (buf (buffer-list))
293 (with-current-buffer buf (which-func-try-to-enable))))) 300 (with-current-buffer buf
301 (which-func--header-line-remove)
302 (which-func-ff-hook))))
294 303
295(defvar which-function-imenu-failed nil 304(defvar which-function-imenu-failed nil
296 "Locally t in a buffer if `imenu--make-index-alist' found nothing there.") 305 "Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
diff --git a/test/lisp/progmodes/which-func-tests.el b/test/lisp/progmodes/which-func-tests.el
new file mode 100644
index 00000000000..73709f1c5e5
--- /dev/null
+++ b/test/lisp/progmodes/which-func-tests.el
@@ -0,0 +1,58 @@
1;;; which-func-tests.el --- tests for which-func -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2023 Free Software Foundation, Inc.
4
5;; Author: Spencer Baugh <sbaugh@catern.com>
6
7;; This file is part of GNU Emacs.
8
9;; This program is free software; you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
13
14;; This program is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
21
22;;; Commentary:
23
24;;; Code:
25(require 'ert)
26(require 'which-func)
27
28(ert-deftest which-func-tests-toggle ()
29 (let ((which-func-display 'mode-and-header) buf-code buf-not)
30 (setq buf-code (find-file-noselect "which-func-tests.el"))
31 (setq buf-not (get-buffer-create "fundamental"))
32 (with-current-buffer buf-code
33 (should-not which-func-mode) (should-not header-line-format))
34 (with-current-buffer buf-not
35 (should-not which-func-mode) (should-not header-line-format))
36 (which-function-mode 1)
37 (with-current-buffer buf-code
38 (should which-func-mode) (should header-line-format))
39 (with-current-buffer buf-not
40 (should-not which-func-mode) (should-not header-line-format))
41 (which-function-mode -1)
42 ;; which-func-mode stays set even when which-function-mode is off.
43 (with-current-buffer buf-code
44 (should which-func-mode) (should-not header-line-format))
45 (with-current-buffer buf-not
46 (should-not which-func-mode) (should-not header-line-format))
47 (kill-buffer buf-code)
48 (kill-buffer buf-not)
49 (which-function-mode 1)
50 (setq buf-code (find-file-noselect "which-func-tests.el"))
51 (setq buf-not (get-buffer-create "fundamental"))
52 (with-current-buffer buf-code
53 (should which-func-mode) (should header-line-format))
54 (with-current-buffer buf-not
55 (should-not which-func-mode) (should-not header-line-format))))
56
57(provide 'which-func-tests)
58;;; which-func-tests.el ends here