在emacs

时间:2018-05-28 17:34:47

标签: emacs

当我按退格键时我想更改我的光标颜色,但它仍然应该删除后向字符然后如果我按任意键(退格除外)颜色应该更改回更改之前的颜色。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

这是一个奇怪的请求,但是,只是为了展示出令人敬畏的力量 Emacs,我会为你做的:

(defvar cursor-color-original nil
  "The original cursor color, saved while backspacing.")
(defvar cursor-color-while-backspace "Red"
  "The cursor color while backspacing.")
(defvar cursor-color-backspace-commands
  '(backward-delete-char-untabify delete-backward-char)
  "The commands that switch cursor color.")
(defvar cursor-color-backspace-keys
  (list (kbd "<DEL>"))
  "The keys that switch cursor color.")

(defun cursor-color-note-backspace ()
  "Switch cursor color when backspacing."
  (if (or (memq this-command cursor-color-backspace-commands)
          (member (this-command-keys) cursor-color-backspace-keys))
      (unless (equal (frame-parameter nil 'cursor-color)
                     cursor-color-after-backspace)
        (setq original-cursor-color (frame-parameter nil 'cursor-color))
        (set-cursor-color cursor-color-after-backspace))
    (when (equal (frame-parameter nil 'cursor-color)
                 cursor-color-after-backspace)
      (set-cursor-color original-cursor-color))))

启用:

(add-hook 'post-command-hook 'cursor-color-note-backspace)

要禁用:

(remove-hook 'post-command-hook 'cursor-color-note-backspace)

请注意,如果这对您不起作用,则应执行 C-h c DEL 并将DEL调用的函数添加到cursor-color-backspace-commands。 您还可以向cursor-color-backspace-keys添加密钥。

快乐的黑客攻击!

PS。请对你不知道的每个函数执行 C-h f RET 。这将有助于您下次自己编写这样的代码段。