Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

emacs

elisp

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?

like image 310
louxiu Avatar asked Nov 29 '12 11:11

louxiu


1 Answers

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))))))))
like image 76
user4815162342 Avatar answered Oct 13 '22 00:10

user4815162342