Subject: Re: bug#77022: [PATCH] Allow specify in which major modes global-hl-line-mode must be enabled.

From: Elijah Gabe Pérez <[email-masked]>
Date: Sat, 15 Mar 2025 12:35:37 -0600

Eli Zaretskii <[email-masked]> writes:

>> +(defcustom global-hl-line-modes t
>> +  "Which major modes `hl-line-mode' is switched on in.
>> +This variable can be either t (all major modes), nil (no major modes),
>> +or a list of modes and (not modes) to switch use this minor mode or
>> +not.  For instance
>> +
>> +  (c-mode (not message-mode mail-mode) text-mode)
>> +
>> +means \"use this mode in all modes derived from `c-mode', don't use in
>> +modes derived from `message-mode' or `mail-mode', but do use in other
>> +modes derived from `text-mode'\".  An element with value t means \"use\"
>> +and nil means \"don't use\".  There's an implicit nil at the end of the
>> +list."
>> +  :type
>> +  '(choice (const :tag "Enable in all major modes" t)
>> +           (const :tag "Don't enable in any major mode" nil)
> Why is it useful to have a value of nil here?  Why would anyone
> activate this mode when the variable has the value nil?

Well, this is extracted by expanding define-globalized-minor-mode macro,
I've deleted them /(i'm wondering why that macro adds them since i find them unnecessaries)/

>> @@ -224,7 +254,8 @@ global-hl-line-mode
>>  on `post-command-hook'."
>>    :global t
>>    :group 'hl-line
>> -  (if global-hl-line-mode
>> +  (if (and global-hl-line-mode
>> +           (easy-mmode--globalized-predicate-p global-hl-line-modes))
>>        (progn
>>          ;; In case `kill-all-local-variables' is called.
>>          (add-hook 'change-major-mode-hook #'global-hl-line-unhighlight)
> Maybe I'm missing something, but don't you need to consult this new
> variable in global-hl-line-highlight-all?

I've moved it to =global-hl-line-highlight=, which should work better.

>From 293468956984a8fbb92076803b75519b1f1667d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Fri, 14 Mar 2025 19:02:51 -0600
Subject: [PATCH] Allow specify in which major modes global-hl-line-mode must
 be enabled.

* lisp/hl-line.el (global-hl-line-modes): New user option.
(global-hl-line-mode, global-hl-line-highlight-all)
(global-hl-line-highlight): Changes.

* etc/NEWS: Document changes.
---
 etc/NEWS        |  4 ++++
 lisp/hl-line.el | 30 +++++++++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index b33f7cec73f..93716cef1de 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1532,6 +1532,10 @@ commands.  When nil, clicking on an inactive Emacs frame will only
 activate it.  When t (the default), the click will both activate the
 frame and be interpreted as a command.
 
+*** New variable 'global-hl-line-modes'.
+This specifies in which major modes should the 'global-hl-line-mode' be
+switched on.
+
 ---
 *** New user option 'display-fill-column-indicator-warning'.
 Customize it to a non-nil value to have the fill-column indicators
diff --git a/lisp/hl-line.el b/lisp/hl-line.el
index 61bdf40222c..0f36b2458ab 100644
--- a/lisp/hl-line.el
+++ b/lisp/hl-line.el
@@ -122,6 +122,33 @@ global-hl-line-sticky-flag
   :version "24.1"
   :group 'hl-line)
 
+(defcustom global-hl-line-modes t
+  "Which major modes `hl-line-mode' is switched on in.
+This variable can be either t (all major modes), nil (no major modes),
+or a list of modes and (not modes) to switch use this minor mode or
+not.  For instance
+
+  (c-mode (not message-mode mail-mode) text-mode)
+
+means \"use this mode in all modes derived from `c-mode', don't use in
+modes derived from `message-mode' or `mail-mode', but do use in other
+modes derived from `text-mode'\".  An element with value t means \"use\"
+and nil means \"don't use\".  There's an implicit nil at the end of the
+list."
+  :type
+  '(choice (const :tag "Enable in all major modes" t)
+           (repeat :tag "Rules (earlier takes precedence)..."
+                   (choice
+                    (const :tag "Enable in all (other) modes" t)
+                    (symbol :value fundamental-mode :tag
+                            "Enable in major mode")
+                    (cons :tag "Don't enable in major modes"
+                          (const :tag "Don't enable in..." not)
+                          (repeat
+                           (symbol :value fundamental-mode :tag
+                                   "Major mode"))))))
+  :version "31.1")
+
 (defvar hl-line-range-function nil
   "If non-nil, function to call to return highlight range.
 The function of no args should return a cons cell; its car value
@@ -236,7 +263,8 @@ global-hl-line-mode
 
 (defun global-hl-line-highlight ()
   "Highlight the current line in the current window."
-  (when global-hl-line-mode	; Might be changed outside the mode function.
+  (when (and global-hl-line-mode 	; Might be changed outside the mode function.
+             (easy-mmode--globalized-predicate-p global-hl-line-modes))
     (unless (window-minibuffer-p)
       (unless (overlayp global-hl-line-overlay)
         (setq global-hl-line-overlay (hl-line-make-overlay))) ; To be moved.
-- 
2.47.0

-- 
                                          - E.G via GNU Emacs and Org.