]> code.delx.au - gnu-emacs-elpa/blobdiff - beacon.el
Fix scrolling outside command loop
[gnu-emacs-elpa] / beacon.el
index 8a997a1c5dcc392ced9dfaf6322e085f42337c61..ba47f18eaf2dde9a566190c9fb77048f23da3e47 100644 (file)
--- a/beacon.el
+++ b/beacon.el
@@ -204,8 +204,8 @@ Only returns `beacon-size' elements."
 
 \f
 ;;; Movement detection
+(defvar beacon--window-scrolled nil)
 (defvar beacon--previous-place nil)
-(defvar beacon--previous-window-start nil)
 (defvar beacon--previous-mark-head nil)
 
 (defun beacon--movement-> (delta)
@@ -243,9 +243,10 @@ If DELTA is nil, return nil."
         (beacon-blink))))
    ;; Blink for scrolling.
    ((and beacon-blink-when-window-scrolls
-         (progn (redisplay)
-                (not (equal beacon--previous-window-start (window-start)))))
-    (beacon-blink))
+         beacon--window-scrolled)
+    (with-selected-window beacon--window-scrolled
+      (beacon-blink))
+    (setq beacon--window-scrolled nil))
    ;; Blink for movement
    ((beacon--movement-> beacon-blink-when-point-moves)
     (beacon-blink))
@@ -253,16 +254,29 @@ If DELTA is nil, return nil."
    (t (beacon--vanish)))
   (beacon--maybe-push-mark)
   (unless (window-minibuffer-p)
-    (setq beacon--previous-window-start (window-start))
     (setq beacon--previous-mark-head (car mark-ring))
     (setq beacon--previous-place (point-marker))))
 
+(defun beacon--window-scroll-function (win _start-pos)
+  "Blink the beacon or record that window has been scrolled.
+If invoked during the command loop, record the current window so
+that it may be blinked on post-command.  This is because the
+scrolled window might not be active, but we only know that at
+`post-command-hook'.
+
+If invoked outside the command loop, `post-command-hook' would be
+unreliable, so just blink immediately."
+  (if this-command
+      (setq beacon--window-scrolled win)
+    (beacon-blink)))
+
 \f
 ;;; Minor-mode
-(defcustom beacon-lighter (cond
-                     ((char-displayable-p ?💡) " ðŸ’¡")
-                     ((char-displayable-p ?Λ) " Î›")
-                     (t " *"))
+(defcustom beacon-lighter
+  (cond
+   ((char-displayable-p ?💡) " ðŸ’¡")
+   ((char-displayable-p ?Λ) " Î›")
+   (t " *"))
   "Lighter string used on the mode-line."
   :type 'string)
 
@@ -271,7 +285,10 @@ If DELTA is nil, return nil."
   nil nil beacon-lighter nil
   :global t
   (if beacon-mode
-      (add-hook 'post-command-hook #'beacon--post-command)
+      (progn
+        (add-hook 'window-scroll-functions #'beacon--window-scroll-function)
+        (add-hook 'post-command-hook #'beacon--post-command))
+    (remove-hook 'window-scroll-functions #'beacon--window-scroll-function)
     (remove-hook 'post-command-hook #'beacon--post-command)))
 
 (provide 'beacon)