请教:有没有 buffer change hook, 或者如何实现

我想在buffer变化时执行一行代码,保存输入法状态现在是用 pre-command hook和post command hook,但是有可能buffer切换不是因为commandh导致的,所以问一下

(defun imbot--pre-command-switcher ()
(setq imbot--last-buffer (current-buffer)))

(defun imbot--post-command-switcher ()
;; (message "2 this command %s" this-command)
(cond
    ;; restore im state after prefix sequence completed 
    ((and (not imbot--prefix-override) this-command)
    (setq imbot--prefix-override t)
    (imbot--maybe-activate-im))
    ;; save im state on buffer switch and restore im state
    ((not (eq imbot--last-buffer (current-buffer)))
    (when (buffer-live-p imbot--last-buffer)
    (with-current-buffer imbot--last-buffer
        (imbot--save-state)))
    (imbot--maybe-activate-im))
    (t nil)))

after-change-functions

豆知识:-functions结尾的变量往往代表这是一个会被带参数调用的hook变量(具体什么参数看docstring)

不是指这个,应该是切换focused buffer,就是当前buffer发生变化的时候

感觉和 pre-redisplay-functions 一样,不能知道之前的buffer,而这个命令是要在之前的buffer执行

先试试 last-buffer

切换到其它buffer,不能知道之前的buffer是那个? 这个问题我是在执行切换成其它buffer前定义一个变量存放上一个buffer的名字解决(setq my-before-buffer (buffer-file-name))

嗯 那就放到pre-command-hook进行保存,就是担心一些忽然冒出的buffer

感觉这个可能行

(defun redisplay--pre-redisplay-functions (windows)
(with-demoted-errors "redisplay--pre-redisplay-functions: %S"
    ;; 在这里保存
    (setq my-last-buffer (current-buffer))
    (if (null windows)
        (with-current-buffer (window-buffer (selected-window))
        (run-hook-with-args 'pre-redisplay-functions (selected-window)))
    (dolist (win (if (listp windows) windows (window-list-1 nil nil t)))
        (with-current-buffer (window-buffer win)
        (run-hook-with-args 'pre-redisplay-functions win))))))

这个函数使用太频繁了