]> git.eshelyaron.com Git - emacs.git/commitdiff
Make blink-cursor-mode use new focus functions
authorDaniel Colascione <dancol@dancol.org>
Mon, 11 Jun 2018 23:08:29 +0000 (16:08 -0700)
committerDaniel Colascione <dancol@dancol.org>
Mon, 11 Jun 2018 23:10:34 +0000 (16:10 -0700)
* lisp/frame.el (blink-cursor--should-blink): New function.
(blink-cursor-check): Call it.
(blink-cursor--rescan-frames): New function.
(blink-cursor-mode): Wire up `blink-cursor--rescan-frames`; stop
using `focus-in-hook' and `focus-out-hook'.

lisp/frame.el

index 2a2391e8a530de59305c3ac4a86fd3b77facfa56..a0e7b35a138892a59aae5862182b18e8f5712600 100644 (file)
@@ -2416,15 +2416,41 @@ frame receives focus."
     (cancel-timer blink-cursor-idle-timer)
     (setq blink-cursor-idle-timer nil)))
 
-(defun blink-cursor-check ()
+(defun blink-cursor--should-blink (&optional ignored-frame)
+  "Determine whether we should be blinking.
+Returns whether we have any focused non-TTY frame.  IGNORED-FRAME
+is a frame to ignore during the scan, used when we want to ignore
+a frame about to be deleted."
+  (and blink-cursor-mode
+       (let ((frame-list (frame-list))
+             (any-graphical-focused nil))
+         (while frame-list
+           (let ((frame (pop frame-list)))
+             (when (and (not (eq frame ignored-frame))
+                        (display-graphic-p frame)
+                        (frame-focus-state frame))
+               (setf any-graphical-focused t)
+               (setf frame-list nil))))
+         any-graphical-focused)))
+
+(defun blink-cursor-check (&optional ignored-frame)
   "Check if cursor blinking shall be restarted.
-This is done when a frame gets focus.  Blink timers may be stopped by
-`blink-cursor-suspend'."
-  (when (and blink-cursor-mode
-            (not blink-cursor-idle-timer)
-             (display-graphic-p))
-    (remove-hook 'post-command-hook 'blink-cursor-check)
-    (blink-cursor--start-idle-timer)))
+This is done when a frame gets focus.  Blink timers may be
+stopped by `blink-cursor-suspend'.  Internally calls
+`blink-cursor--should-blink' and returns its result."
+  (let ((should-blink (blink-cursor--should-blink ignored-frame)))
+    (when (and should-blink (not blink-cursor-idle-timer))
+      (remove-hook 'post-command-hook 'blink-cursor-check)
+      (blink-cursor--start-idle-timer))
+    should-blink))
+
+(defun blink-cursor--rescan-frames (&optional ignored-frame)
+  "Called when the set of focused frames changes or when we
+delete a frame.  Re-check whether we want to enable blinking.
+IGNORED-FRAME is there so we ignore a frame about to be deleted
+when we're called under via `delete-frame-functions'."
+  (unless (blink-cursor-check ignored-frame)
+    (blink-cursor-suspend)))
 
 (define-minor-mode blink-cursor-mode
   "Toggle cursor blinking (Blink Cursor mode).
@@ -2448,11 +2474,11 @@ terminals, cursor blinking is controlled by the terminal."
   :group 'cursor
   :global t
   (blink-cursor-suspend)
-  (remove-hook 'focus-in-hook #'blink-cursor-check)
-  (remove-hook 'focus-out-hook #'blink-cursor-suspend)
+  (remove-hook 'delete-frame-functions #'blink-cursor--rescan-frames)
+  (remove-function after-focus-change-function #'blink-cursor--rescan-frames)
   (when blink-cursor-mode
-    (add-hook 'focus-in-hook #'blink-cursor-check)
-    (add-hook 'focus-out-hook #'blink-cursor-suspend)
+    (add-function :after after-focus-change-function #'blink-cursor--rescan-frames)
+    (add-hook 'delete-frame-functions #'blink-cursor--rescan-frames)
     (blink-cursor--start-idle-timer)))
 
 \f