changing the cursor color and shape dynamically










When typing some text, it's often useful to know what mode we're in – are we in

overwrite-mode, in read-only-mode, or in normal insert-mode. The
information is available in the mode-line – but wouldn't it be nicer to get
some small visual cue, for example by changing the cursor color or style?



That indeed is possible. There are some existing ways to do this, described in EmacsWiki. However, I want to be able to control both the cursor color
and cursor shape, and also distinguish between overwrite, read-only and
'normal' mode. Below is my attempt.



By putting the following snippet in your .emacs, the cursor becomes a
yellow vertical bar during normal mode, it becomes a red block when you're
in overwrite-mode and it becomes a gray vertical bar when you're in
read-only mode.





;; Change cursor color according to mode; inspired by
;; http://www.emacswiki.org/emacs/ChangingCursorDynamically
(setq djcb-read-only-color "gray")
;; valid values are t, nil, box, hollow, bar, (bar . WIDTH), hbar,
;; (hbar. HEIGHT); see the docs for set-cursor-type

(setq djcb-read-only-cursor-type 'hbar)
(setq djcb-overwrite-color "red")
(setq djcb-overwrite-cursor-type 'box)
(setq djcb-normal-color "yellow")
(setq djcb-normal-cursor-type 'bar)

(defun djcb-set-cursor-according-to-mode ()
"change cursor color and type according to some minor modes."

(cond
(buffer-read-only
(set-cursor-color djcb-read-only-color)
(setq cursor-type djcb-read-only-cursor-type))
(overwrite-mode
(set-cursor-color djcb-overwrite-color)
(setq cursor-type djcb-overwrite-cursor-type))
(t
(set-cursor-color djcb-normal-color)
(setq cursor-type djcb-normal-cursor-type))))

(add-hook 'post-command-hook 'djcb-set-cursor-according-to-mode)







You can change the colors and cursor types by modifying the various
variables.



I should probably turn this into a proper minor mode, but for now this seems
to work well.



No comments:

Post a Comment

Followers

Popular Posts