Jousimies/.emacs.d

cursor color

Closed this issue · 0 comments

使用 Evil,通过光标的颜色来提示当前的输入法状态,结合使用[[http://zzzm.ysepan.com/][三码郑码 / 至至郑码 (至简·至爱)zhengma.plus]] 输入法,具有很好的使用体验,相当的明确感。

关于光标自动更换颜色,这里有个包可以使用:[[https://github.com/Eason0210/im-cursor-chg][Eason0210/im-cursor-chg]].
#+begin_src emacs-lisp :tangle no
(defun im--chinese-p ()
"Check if the current input state is Chinese."
(if (featurep 'rime)
(and (rime--should-enable-p)
(not (rime--should-inline-ascii-p))
current-input-method)
current-input-method))

(defun im-change-cursor-color ()
"Set cursor color depending on input method."
(interactive)
(set-cursor-color (if (im--chinese-p)
"red"
(foreground-color-at-point))))

(add-hook 'post-command-hook 'im-change-cursor-color)
#+end_src

[[https://emacs-china.org/t/topic/17717/42][切换输入法时自动更换光标颜色 - Emacs-general - Emacs China]] 提到了 post-command-hook 的开销比较大,调用频繁。

使用 evil 内置的 hook 也可以达到切换光标颜色的功能,同时避免 post-command-hook 的巨大开销。

#+begin_src emacs-lisp :tangle no
(defun im--chinese-p ()
"Check if the current input state is Chinese."
(if (featurep 'rime)
(and (rime--should-enable-p)
(not (rime--should-inline-ascii-p))
current-input-method)
current-input-method))

(add-hook 'evil-insert-state-entry-hook
(lambda ()
(when (im--chinese-p)
(set-cursor-color "red"))))

(add-hook 'evil-insert-state-exit-hook
(lambda ()
(set-cursor-color (foreground-color-at-point))))
#+end_src