对光标离开当前 buffer 的操作有没有 hook?

想要实现光标切换到别的 buffer 时,让 evil 退出 insert 状态到 normal 状态。

可以参考super-save的实现 super-save/super-save.el at master · bbatsov/super-save · GitHub 关键的几处如下,如果想要的触发事件不够还可以自己加

(defcustom super-save-triggers
  '(switch-to-buffer other-window windmove-up windmove-down windmove-left windmove-right next-buffer previous-buffer)
  "A list of commands which would trigger `super-save-command'."
  :group 'super-save
  :type '(repeat symbol)
  :package-version '(super-save . "0.1.0"))
(defun super-save-advise-trigger-commands ()
  "Apply super-save advice to the commands listed in `super-save-triggers'."
  (mapc
   (lambda (command)
     (advice-add command :before #'super-save-command-advice))
   super-save-triggers))
(defun super-save-command-advice (&rest _args)
  "A simple wrapper around `super-save-command' that's advice-friendly."
  (super-save-command))
1 个赞

也可以用 window-selection-change-functions, 注意其函数参数要求

1 个赞

下面这些配置大概可以解决我的问题了:

(add-hook 'focus-in-hook #'(lambda () (evil-normal-state)))

(advice-add 'switch-window :before
	    #'evil-normal-state)

(advice-add 'switch-to-buffer :before
	    #'(lambda (buffer-or-name &optional norecord force-same-window)
		(evil-normal-state)))
1 个赞