]> git.eshelyaron.com Git - emacs.git/commitdiff
Use single post-command-hook on hl-line modes
authorGabriel do Nascimento Ribeiro <gabriel.nascimento@nubank.com.br>
Sat, 23 Jan 2021 18:38:42 +0000 (15:38 -0300)
committerEli Zaretskii <eliz@gnu.org>
Sat, 23 Jan 2021 19:35:00 +0000 (21:35 +0200)
* lisp/hl-line.el (hl-line-mode, global-hl-line-mode): Ensure
that 'maybe-unhighlight' is called after line is highlighted.
(Bug#45946)
(hl-line-unhighlight, global-hl-line-unhighlight): Set overlay
variable to nil after overlay is deleted.

lisp/hl-line.el

index 73870f9579ec3a7cb9eacb58f096a68ce635db3e..82952e934b62f738be8d14176198d2f1279bac3b 100644 (file)
 ;; An overlay is used.  In the non-sticky cases, this overlay is
 ;; active only on the selected window.  A hook is added to
 ;; `post-command-hook' to activate the overlay and move it to the line
-;; about point.  To get the non-sticky behavior, `hl-line-unhighlight'
-;; is added to `pre-command-hook' as well.  This function deactivates
-;; the overlay unconditionally in case the command changes the
-;; selected window.  (It does so rather than keeping track of changes
-;; in the selected window).
+;; about point.
 
 ;; You could make variable `global-hl-line-mode' buffer-local and set
 ;; it to nil to avoid highlighting specific buffers, when the global
@@ -91,9 +87,9 @@ when `global-hl-line-sticky-flag' is non-nil.")
         (set symbol value)
         (dolist (buffer (buffer-list))
           (with-current-buffer buffer
-            (when hl-line-overlay
+            (when (overlayp hl-line-overlay)
               (overlay-put hl-line-overlay 'face hl-line-face))))
-        (when global-hl-line-overlay
+        (when (overlayp global-hl-line-overlay)
           (overlay-put global-hl-line-overlay 'face hl-line-face))))
 
 (defcustom hl-line-sticky-flag t
@@ -141,9 +137,7 @@ non-selected window.  Hl-Line mode uses the function
 `hl-line-highlight' on `post-command-hook' in this case.
 
 When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
-line about point in the selected window only.  In this case, it
-uses the function `hl-line-maybe-unhighlight' in
-addition to `hl-line-highlight' on `post-command-hook'."
+line about point in the selected window only."
   :group 'hl-line
   (if hl-line-mode
       (progn
@@ -151,12 +145,10 @@ addition to `hl-line-highlight' on `post-command-hook'."
         (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t)
         (hl-line-highlight)
         (setq hl-line-overlay-buffer (current-buffer))
-       (add-hook 'post-command-hook #'hl-line-highlight nil t)
-        (add-hook 'post-command-hook #'hl-line-maybe-unhighlight nil t))
+       (add-hook 'post-command-hook #'hl-line-highlight nil t))
     (remove-hook 'post-command-hook #'hl-line-highlight t)
     (hl-line-unhighlight)
-    (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)
-    (remove-hook 'post-command-hook #'hl-line-maybe-unhighlight t)))
+    (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)))
 
 (defun hl-line-make-overlay ()
   (let ((ol (make-overlay (point) (point))))
@@ -168,17 +160,19 @@ addition to `hl-line-highlight' on `post-command-hook'."
   "Activate the Hl-Line overlay on the current line."
   (if hl-line-mode     ; Might be changed outside the mode function.
       (progn
-        (unless hl-line-overlay
+        (unless (overlayp hl-line-overlay)
           (setq hl-line-overlay (hl-line-make-overlay))) ; To be moved.
         (overlay-put hl-line-overlay
                      'window (unless hl-line-sticky-flag (selected-window)))
-       (hl-line-move hl-line-overlay))
+       (hl-line-move hl-line-overlay)
+        (hl-line-maybe-unhighlight))
     (hl-line-unhighlight)))
 
 (defun hl-line-unhighlight ()
   "Deactivate the Hl-Line overlay on the current line."
-  (when hl-line-overlay
-    (delete-overlay hl-line-overlay)))
+  (when (overlayp hl-line-overlay)
+    (delete-overlay hl-line-overlay)
+    (setq hl-line-overlay nil)))
 
 (defun hl-line-maybe-unhighlight ()
   "Maybe deactivate the Hl-Line overlay on the current line.
@@ -191,8 +185,7 @@ such overlays in all buffers except the current one."
                (not (eq curbuf hlob))
                (not (minibufferp)))
       (with-current-buffer hlob
-        (when (overlayp hl-line-overlay)
-          (delete-overlay hl-line-overlay))))
+        (hl-line-unhighlight)))
     (when (and (overlayp hl-line-overlay)
                (eq (overlay-buffer hl-line-overlay) curbuf))
       (setq hl-line-overlay-buffer curbuf))))
@@ -205,8 +198,8 @@ If `global-hl-line-sticky-flag' is non-nil, Global Hl-Line mode
 highlights the line about the current buffer's point in all live
 windows.
 
-Global-Hl-Line mode uses the functions `global-hl-line-highlight'
-and `global-hl-line-maybe-unhighlight' on `post-command-hook'."
+Global-Hl-Line mode uses the function `global-hl-line-highlight'
+on `post-command-hook'."
   :global t
   :group 'hl-line
   (if global-hl-line-mode
@@ -214,25 +207,24 @@ and `global-hl-line-maybe-unhighlight' on `post-command-hook'."
         ;; In case `kill-all-local-variables' is called.
         (add-hook 'change-major-mode-hook #'global-hl-line-unhighlight)
         (global-hl-line-highlight-all)
-       (add-hook 'post-command-hook #'global-hl-line-highlight)
-        (add-hook 'post-command-hook #'global-hl-line-maybe-unhighlight))
+       (add-hook 'post-command-hook #'global-hl-line-highlight))
     (global-hl-line-unhighlight-all)
     (remove-hook 'post-command-hook #'global-hl-line-highlight)
-    (remove-hook 'change-major-mode-hook #'global-hl-line-unhighlight)
-    (remove-hook 'post-command-hook #'global-hl-line-maybe-unhighlight)))
+    (remove-hook 'change-major-mode-hook #'global-hl-line-unhighlight)))
 
 (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.
     (unless (window-minibuffer-p)
-      (unless global-hl-line-overlay
+      (unless (overlayp global-hl-line-overlay)
         (setq global-hl-line-overlay (hl-line-make-overlay))) ; To be moved.
       (unless (member global-hl-line-overlay global-hl-line-overlays)
        (push global-hl-line-overlay global-hl-line-overlays))
       (overlay-put global-hl-line-overlay 'window
                   (unless global-hl-line-sticky-flag
                     (selected-window)))
-      (hl-line-move global-hl-line-overlay))))
+      (hl-line-move global-hl-line-overlay)
+      (global-hl-line-maybe-unhighlight))))
 
 (defun global-hl-line-highlight-all ()
   "Highlight the current line in all live windows."
@@ -243,8 +235,9 @@ and `global-hl-line-maybe-unhighlight' on `post-command-hook'."
 
 (defun global-hl-line-unhighlight ()
   "Deactivate the Global-Hl-Line overlay on the current line."
-  (when global-hl-line-overlay
-    (delete-overlay global-hl-line-overlay)))
+  (when (overlayp global-hl-line-overlay)
+    (delete-overlay global-hl-line-overlay)
+    (setq global-hl-line-overlay nil)))
 
 (defun global-hl-line-maybe-unhighlight ()
   "Maybe deactivate the Global-Hl-Line overlay on the current line.
@@ -256,9 +249,8 @@ all such overlays in all buffers except the current one."
                        (bufferp ovb)
                        (not (eq ovb (current-buffer)))
                        (not (minibufferp)))
-               (with-current-buffer ovb
-                  (when (overlayp global-hl-line-overlay)
-                    (delete-overlay global-hl-line-overlay))))))
+             (with-current-buffer ovb
+                (global-hl-line-unhighlight)))))
         global-hl-line-overlays))
 
 (defun global-hl-line-unhighlight-all ()