Optimize switch buffer/window hooks

Significantly reduces nested triggering of these hooks.
This commit is contained in:
Henrik Lissner 2018-06-01 16:40:14 +02:00
parent 8e38209394
commit 36c36ca271
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -356,6 +356,9 @@ from the default."
;; Custom hooks ;; Custom hooks
;; ;;
(defvar doom-inhibit-switch-buffer-hooks nil)
(defvar doom-inhibit-switch-window-hooks nil)
(defun doom*switch-frame-hooks (orig-fn frame &optional norecord) (defun doom*switch-frame-hooks (orig-fn frame &optional norecord)
(if (eq frame (selected-frame)) (if (eq frame (selected-frame))
(funcall orig-fn frame norecord) (funcall orig-fn frame norecord)
@ -363,17 +366,26 @@ from the default."
(prog1 (funcall orig-fn frame norecord) (prog1 (funcall orig-fn frame norecord)
(run-hooks 'doom-after-switch-frame-hook)))) (run-hooks 'doom-after-switch-frame-hook))))
(defun doom*switch-window-hooks (orig-fn window &optional norecord) (defun doom*switch-window-hooks (orig-fn window &optional norecord)
(if (or (eq window (selected-window)) (if (or doom-inhibit-switch-window-hooks
(eq window (selected-window))
(window-minibuffer-p) (window-minibuffer-p)
(window-minibuffer-p window)) (window-minibuffer-p window))
(funcall orig-fn window norecord) (funcall orig-fn window norecord)
(run-hooks 'doom-before-switch-window-hook) (run-hooks 'doom-before-switch-window-hook)
(prog1 (funcall orig-fn window norecord) (prog1
(let ((doom-inhibit-switch-window-hooks t))
(funcall orig-fn window norecord))
(run-hooks 'doom-after-switch-window-hook)))) (run-hooks 'doom-after-switch-window-hook))))
(defun doom*switch-buffer-hooks (orig-fn &rest args) (defun doom*switch-buffer-hooks (orig-fn buffer-or-name &rest args)
(run-hooks 'doom-before-switch-buffer-hook) (let ((buf (window-normalize-buffer-to-switch-to buffer-or-name)))
(prog1 (apply orig-fn args) (if (or doom-inhibit-switch-buffer-hooks
(run-hooks 'doom-after-switch-buffer-hook))) (eq buf (current-buffer)))
(apply orig-fn buf args)
(run-hooks 'doom-before-switch-buffer-hook)
(prog1
(let ((doom-inhibit-switch-buffer-hooks t))
(apply orig-fn buf args))
(run-hooks 'doom-after-switch-buffer-hook)))))
(defun doom|init-custom-hooks () (defun doom|init-custom-hooks ()
(advice-add #'select-frame :around #'doom*switch-frame-hooks) (advice-add #'select-frame :around #'doom*switch-frame-hooks)