Subject: bug#66283: 30.0.50; which-function-mode: When configured to display in header, and toggling off, then does not remove header

From: Spencer Baugh <[email-masked]>
Date: Wed, 04 Oct 2023 11:13:06 -0400

Two fixes to the previous patch:

- There was always a bug with which-function-mode where if you enabled
  it when there were already existing buffers, it would behave
  differently than if you enabled it and then created those buffers.
  Namely it would enable which-func-mode for every buffer, even if the
  buffer didn't support imenu.  This is especially noticeable when
  toggling which-function-mode and using the header line, so I fixed it.

- Also, I accidentally dropped a paren before submitting.  Now that's
  fixed :)

>From 9918722015e52510b12b0eeee093f11e231d14fe Mon Sep 17 00:00:00 2001
From: Spencer Baugh <[email-masked]>
Date: Wed, 4 Oct 2023 09:53:47 -0400
Subject: [PATCH] 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): Add.
(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)
---
 lisp/progmodes/which-func.el | 39 ++++++++++++++++++++++--------------
 1 file changed, 24 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 @@ which-func--use-mode-line
 (add-hook 'after-change-major-mode-hook #'which-func-ff-hook t)
 
 (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)))
-    (setq which-func--use-mode-line
-          (member which-func-display '(mode mode-and-header)))
-    (setq which-func--use-header-line
-          (member which-func-display '(header mode-and-header)))
-    (when (and which-func-mode which-func--use-header-line)
+  (when which-function-mode
+    (unless (local-variable-p 'which-func-mode)
+      (setq which-func-mode (or (eq which-func-modes t)
+                                (member major-mode which-func-modes)))
+      (setq which-func--use-mode-line
+            (member which-func-display '(mode mode-and-header)))
+      (setq which-func--use-header-line
+            (member which-func-display '(header mode-and-header))))
+    ;; We might need to re-add which-func-format to the header line,
+    ;; if which-function-mode was toggled off and on.
+    (when (and which-func-mode which-func--use-header-line
+               (listp header-line-format))
       (add-to-list 'header-line-format '("" which-func-format " ")))))
 
-(defun which-func--disable ()
-  (when (and which-func-mode which-func--use-header-line)
+(defun which-func--header-line-remove ()
+  (when (and which-func-mode which-func--use-header-line
+             (listp header-line-format))
     (setq header-line-format
-          (delete '("" which-func-format " ") header-line-format)))
+          (delete '("" which-func-format " ") header-line-format))))
+
+(defun which-func--disable ()
+  (which-func--header-line-remove)
   (setq which-func-mode nil))
 
 (defun which-func-ff-hook ()
@@ -288,9 +295,11 @@ which-function-mode
   (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)))))
+          (run-with-idle-timer idle-update-delay t #'which-func-update)))
+  (dolist (buf (buffer-list))
+    (with-current-buffer buf
+      (which-func--header-line-remove)
+      (which-func-ff-hook))))
 
 (defvar which-function-imenu-failed nil
   "Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
-- 
2.39.3