louxiu
louxiu

Reputation: 2915

Looking forward a way to make cursor blinks like a heartbeat in Emacs

How can I make the cursor in Emacs blink like a heartbeat. Like the LED on the front panel of laptop when the computer is suspended.

There is variable blink-cursor-alist that control the blink of cursor, but I do not know how to use it to satisfy my requirement.

Is it possible?

Upvotes: 5

Views: 1941

Answers (2)

user4815162342
user4815162342

Reputation: 154836

This simple minor mode implements a heartbeat-style blinking cursor. You can tweak heartbeat-cursor-colors to get different hue or variations thereof.

The code is tested in Emacs 24.2.1, but would be easy to port to older Emacsen.

(require 'cl)
(require 'color)

(defvar heartbeat-fps 16)
(defvar heartbeat-period 5)

(defun heartbeat-range (from to cnt)
  (let ((step (/ (- to from) (float cnt))))
    (loop for i below cnt collect (+ from (* step i)))))

(defun heartbeat-cursor-colors ()
  (let ((cnt (* heartbeat-period heartbeat-fps)))
    (mapcar (lambda (r)
              (color-rgb-to-hex r 0 0))
            (nconc (heartbeat-range .2 1 (/ cnt 2))
                   (heartbeat-range 1 .2 (/ cnt 2))))))

(defvar heartbeat-cursor-timer nil)
(defvar heartbeat-cursor-old-color)

(define-minor-mode heartbeat-cursor-mode
  "Change cursor color with the heartbeat effect."
  nil "" nil
  :global t
  (when heartbeat-cursor-timer
    (cancel-timer heartbeat-cursor-timer)
    (setq heartbeat-cursor-timer nil)
    (set-face-background 'cursor heartbeat-cursor-old-color))
  (when heartbeat-cursor-mode
    (setq heartbeat-cursor-old-color (face-background 'cursor)
          heartbeat-cursor-timer
          (run-with-timer
           0 (/ 1 (float heartbeat-fps))
           (lexical-let ((colors (heartbeat-cursor-colors)) tail)
             (lambda ()
               (setq tail (or (cdr tail) colors))
               (set-face-background 'cursor (car tail))))))))

Upvotes: 8

Omri Barel
Omri Barel

Reputation: 9480

You guess that the option has something to do with the word "blink". So you press C-h a (for apropos) and type "blink". On my emacs, I get two options: blink-cursor-mode and blink-matching-open. The first one looks right. The description says: "Toggle blinking cursor mode".

The shortcut on my emacs says: <menu-bar> <options> <blink-cursor-mode>. So I guess that the option is somewhere in the menu, maybe under "options". I open the Options menu, and there it is: "Blinking Cursor" with a tick box.

This also sounds like an option that can be customised. So I type M-x customize-option and then blink-cursor-mode. This allows me to toggle the value and also save it for future sessions.

EDIT: To set the interval between ON and OFF for the cursor, there is a variable called blink-cursor-interval. You can use M-x customize-variable and then blink-cursor-interval to set the interval. The variable blink-cursor-alist matches an OFF state cursor type to the ON state cursor type, and is not related to the speed of blinking.

EDIT2: As far as I know there is no way to make the cursor gradually turn off and on, because the shape of the cursor for the ON state could be different to the shape in the OFF state (so a gradual change in shape will be required).

Upvotes: 4

Related Questions