PascalVKooten
PascalVKooten

Reputation: 21451

Changing cursor color based on a minor mode

I have written a minor-mode, it defines some key bindings and does some initialization. The mode properly sets up Navi-mode and Navi-mode-map.

How do I enhance this minor-mode to change the color of the cursor when Navi-mode is enabled, and change it back whenever the mode is disabled? Can I use the hook Navi-mode-hook?

Upvotes: 0

Views: 339

Answers (2)

Either you have total control of the minor mode (because you wrote it), and you can embed this behaviour directly in your minor-mode function as explained in Dmitry's answer:

(define-minor-mode navi-mode
  "Navi mode does wonderful things"
  :lighter " Navi"
  :global t
  :init-value 0

  (if navi-mode
      (progn
        (setq old-cursor-color (cdr (assoc 'cursor-color (frame-parameters))))
        (set-cursor-color "red"))
    (set-cursor-color old-cursor-color)))

Or you don't control the minor mode definition and you'll have to use a hook:

(defun navi-change-cursor-color ()
  (if navi-mode
      (progn
        (setq old-cursor-color (cdr (assoc 'cursor-color (frame-parameters))))
        (set-cursor-color "red"))
    (set-cursor-color old-cursor-color)))

(add-hook 'navi-mode-hook 'navi-change-cursor-color)

Upvotes: 3

Dmitry
Dmitry

Reputation: 3665

Try this:

(define-minor-mode foo-mode "doodad" :lighter ""
  (if foo-mode
      (setq cursor-type 'bar)
    (setq cursor-type (default-value 'cursor-type))))

Or, if you anticipate cursor-type to already have a non-default value, you can save it when the mode is enabled and restore the saved value when it's disabled.

Upvotes: 4

Related Questions