Subject: Re: bug#73637: [PATCH] New hook 'help-setup-hook'

From: Dmitry Gutov <[email-masked]>
Date: Sat, 5 Oct 2024 15:19:29 +0300

Hi again!

On 05/10/2024 10:14, Eshel Yaron via Bug reports for GNU Emacs, the 
Swiss army knife of text editors wrote:
> Add a new hook that allows commands that make use of bespoke variable
> settings in the*Help* buffer to clean up after themselves.  This
> generalizes and improves the accuracy of the hard-coded clean up that
> help-setup-xref currently performs.

Thanks for looking into it.

Have you tried going in the other direction? Meaning, killing all locals 
by default but keeping the ones that are really needed.

This is less backward compatible, but ultimately a more predictable and 
common design. This seems to work okay here:

diff --git a/lisp/help-mode.el b/lisp/help-mode.el
index 7bb69a9389f..8e37c138588 100644
--- a/lisp/help-mode.el
+++ b/lisp/help-mode.el
@@ -505,30 +505,19 @@ help-setup-xref
  because we want to record the \"previous\" position of point so we can
  restore it properly when going back."
    (with-current-buffer (help-buffer)
-    ;; Disable `outline-minor-mode' in a reused Help buffer
-    ;; created by `describe-bindings' that enables this mode.
-    (when (bound-and-true-p outline-minor-mode)
-      (outline-minor-mode -1)
-      (mapc #'kill-local-variable
-            '(outline-search-function
-              outline-regexp
-              outline-heading-end-regexp
-              outline-level
-              outline-minor-mode-cycle
-              outline-minor-mode-highlight
-              outline-minor-mode-use-buttons
-              outline-default-state
-              outline-default-rules)))
-    (kill-local-variable 'xref-backend-functions)
-    (kill-local-variable 'semantic-symref-filepattern-alist)
-    (when help-xref-stack-item
-      (push (cons (point) help-xref-stack-item) help-xref-stack)
-      (setq help-xref-forward-stack nil))
-    (when interactive-p
-      (let ((tail (nthcdr 10 help-xref-stack)))
-	;; Truncate the stack.
-	(if tail (setcdr tail nil))))
-    (setq help-xref-stack-item item)))
+    (let ((stack-item help-xref-stack-item)
+          (stack help-xref-stack))
+      (kill-all-local-variables)
+      (setq help-xref-stack-item stack-item
+            help-xref-stack stack)
+      (when help-xref-stack-item
+        (push (cons (point) help-xref-stack-item) help-xref-stack)
+        (setq help-xref-forward-stack nil))
+      (when interactive-p
+        (let ((tail (nthcdr 10 help-xref-stack)))
+	  ;; Truncate the stack.
+	  (if tail (setcdr tail nil))))
+      (setq help-xref-stack-item item))))

  (defvar help-xref-following nil
    "Non-nil when following a help cross-reference.")