Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs - set different intervals for cursor blinking on and off

I want to set different intervals for blinking ON and for blinking OFF. I mean, I want the cursor to stay visible for 1 second and OFF for 0.2 second. I read the cursor documentation but closest I found is the blink-cursor-interval that changes both ON and OFF blinking.

How can I customize this in Emacs?

like image 395
Jesse Avatar asked Apr 14 '13 21:04

Jesse


2 Answers

There's no such functionality built into Emacs, but you can hack it by adding the following lines to your .emacs file:

(defvar blink-cursor-interval-visible 1)
(defvar blink-cursor-interval-invisible 0.2)

(defadvice internal-show-cursor (before unsymmetric-blink-cursor-interval)
  (when blink-cursor-timer
    (setf (timer--repeat-delay blink-cursor-timer)
          (if (internal-show-cursor-p)
              blink-cursor-interval-visible
            blink-cursor-interval-invisible))))
(ad-activate 'internal-show-cursor)

Emacs implements the blinking of the cursor with a toggle function called by a timer. Each time the function is called, it either hides the cursor if it is currently visible, or shows it if it is invisible. Unfortunately, the timer calls this function at a fixed interval.

In order to realize different delay times depending on the state of the cursor, the above code advises the internal function that shows or hides the cursor. Each time that function is called, the advice changes the delay time of the timer to either 1 or 0.2, depending on whether the cursor is visible or not. That is, every time the cursor is hidden or shown, the timer's delay time is changed.

Quite hackish, but it does the trick.

like image 151
Thomas Avatar answered Nov 13 '22 18:11

Thomas


I was able to modify the blink-cursor-timer-function function to support what you want I believe.

First, you'll need to modify the value of blink-cursor-interval to .2

then this code should do the trick: blink-cursor-timer-function is called every blink-cursor-interval seconds. So this function will be called every .2 seconds, It will keep the cursor ON for 5 calls then turn it off for 1. So 5 calls at .2 seconds per call will give you 1 second of ON time, then only .2 seconds of OFF time.

;; change the interval time to .2
(setq blink-cursor-interval .2)

;; create a variable that counts the timer ticks
(defvar blink-tick-counter 0)

;; this function will be called every .2 seconds
(defun blink-cursor-timer-function ()
  "Timer function of timer `blink-cursor-timer'."
  (if (internal-show-cursor-p)
      (progn
    (if (> blink-tick-counter 4)
        (progn
          (internal-show-cursor nil nil)
          (setq blink-tick-counter 0))
      (setq blink-tick-counter (1+ blink-tick-counter))))
    (internal-show-cursor nil t)))
like image 22
Jordon Biondo Avatar answered Nov 13 '22 18:11

Jordon Biondo