Changing cursor color based on a minor mode

ぃ、小莉子 提交于 2019-12-13 04:23:07

问题


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?


回答1:


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.




回答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)


来源:https://stackoverflow.com/questions/14142495/changing-cursor-color-based-on-a-minor-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!