Fix window issues due to switch-buffer hooks

- Fixes the issue that 45873615 was trying to address with frameworks
  like ivy, helm and hydra (where they would manipulate the wrong
  windows),
- Fixes an issue where notmuch couldn't find its buffers ("no buffer
  named *notmuch-X*" errors),
This commit is contained in:
Henrik Lissner 2019-05-16 11:42:30 -04:00
parent a247f00a45
commit 5d2610fc31
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -86,12 +86,13 @@ behavior). Do not set this directly, this is let-bound in `doom|init-theme'.")
(defvar doom--last-frame nil) (defvar doom--last-frame nil)
(defun doom|run-switch-window-hooks () (defun doom|run-switch-window-hooks ()
(unless (or doom-inhibit-switch-window-hooks (let ((gc-cons-threshold doom-gc-cons-upper-limit))
(eq doom--last-window (selected-window)) (unless (or doom-inhibit-switch-window-hooks
(minibufferp)) (eq doom--last-window (selected-window))
(let ((doom-inhibit-switch-window-hooks t)) (minibufferp))
(run-hooks 'doom-switch-window-hook) (let ((doom-inhibit-switch-window-hooks t))
(setq doom--last-window (selected-window))))) (run-hooks 'doom-switch-window-hook)
(setq doom--last-window (selected-window))))))
(defun doom|run-switch-frame-hooks (&rest _) (defun doom|run-switch-frame-hooks (&rest _)
(unless (or doom-inhibit-switch-frame-hooks (unless (or doom-inhibit-switch-frame-hooks
@ -102,28 +103,28 @@ behavior). Do not set this directly, this is let-bound in `doom|init-theme'.")
(setq doom--last-frame (selected-frame))))) (setq doom--last-frame (selected-frame)))))
(defun doom*run-switch-buffer-hooks (orig-fn buffer-or-name &rest args) (defun doom*run-switch-buffer-hooks (orig-fn buffer-or-name &rest args)
(if (or doom-inhibit-switch-buffer-hooks (let ((gc-cons-threshold doom-gc-cons-upper-limit))
(eq (setq buffer-or-name (get-buffer buffer-or-name)) (if (or doom-inhibit-switch-buffer-hooks
(current-buffer)) (eq (current-buffer) (get-buffer buffer-or-name))
(and (eq orig-fn 'switch-to-buffer) (car args))) (and (eq orig-fn #'switch-to-buffer) (car args)))
(apply orig-fn buffer-or-name args) (apply orig-fn buffer-or-name args)
(let ((doom-inhibit-switch-buffer-hooks t)) (let ((doom-inhibit-switch-buffer-hooks t))
(when-let* ((buffer (apply orig-fn buffer-or-name args))) (when-let* ((buffer (apply orig-fn buffer-or-name args)))
(with-current-buffer (with-current-buffer (if (windowp buffer)
(if (windowp buffer) (window-buffer buffer)
(window-buffer buffer) buffer)
buffer) (run-hooks 'doom-switch-buffer-hook))
(run-hooks 'doom-switch-buffer-hook)) buffer)))))
buffer))))
(defun doom*run-switch-to-next-prev-buffer-hooks (orig-fn &rest args) (defun doom*run-switch-to-next-prev-buffer-hooks (orig-fn &rest args)
(if doom-inhibit-switch-buffer-hooks (let ((gc-cons-threshold doom-gc-cons-upper-limit))
(apply orig-fn args) (if doom-inhibit-switch-buffer-hooks
(let ((doom-inhibit-switch-buffer-hooks t)) (apply orig-fn args)
(when-let* ((buffer (apply orig-fn args))) (let ((doom-inhibit-switch-buffer-hooks t))
(with-current-buffer buffer (when-let* ((buffer (apply orig-fn args)))
(run-hooks 'doom-switch-buffer-hook)) (with-current-buffer buffer
buffer)))) (run-hooks 'doom-switch-buffer-hook))
buffer)))))
(defun doom*run-load-theme-hooks (theme &optional _no-confirm no-enable) (defun doom*run-load-theme-hooks (theme &optional _no-confirm no-enable)
"Set up `doom-load-theme-hook' to run after `load-theme' is called." "Set up `doom-load-theme-hook' to run after `load-theme' is called."
@ -131,18 +132,6 @@ behavior). Do not set this directly, this is let-bound in `doom|init-theme'.")
(setq doom-theme theme) (setq doom-theme theme)
(run-hooks 'doom-load-theme-hook))) (run-hooks 'doom-load-theme-hook)))
(defun doom|inhibit-switch-hooks ()
"Inhibit all switch hooks (frames, buffers and windows)."
(setq doom-inhibit-switch-buffer-hooks t
doom-inhibit-switch-window-hooks t
doom-inhibit-switch-frame-hooks t))
(defun doom|restore-switch-hooks ()
"Uninhibit all switch hooks (frames, buffers and windows)."
(setq doom-inhibit-switch-buffer-hooks nil
doom-inhibit-switch-window-hooks nil
doom-inhibit-switch-frame-hooks nil))
(defun doom|protect-fallback-buffer () (defun doom|protect-fallback-buffer ()
"Don't kill the scratch buffer. Meant for `kill-buffer-query-functions'." "Don't kill the scratch buffer. Meant for `kill-buffer-query-functions'."
(not (eq (current-buffer) (doom-fallback-buffer)))) (not (eq (current-buffer) (doom-fallback-buffer))))
@ -522,11 +511,7 @@ By default, this uses Apple Color Emoji on MacOS and Symbola on Linux."
(advice-add! '(switch-to-next-buffer switch-to-prev-buffer) (advice-add! '(switch-to-next-buffer switch-to-prev-buffer)
:around #'doom*run-switch-to-next-prev-buffer-hooks) :around #'doom*run-switch-to-next-prev-buffer-hooks)
(advice-add! '(switch-to-buffer display-buffer) (advice-add! '(switch-to-buffer display-buffer)
:around #'doom*run-switch-buffer-hooks) :around #'doom*run-switch-buffer-hooks))
;; Inhibit switch hookswhile the minibuffer is active, because they can
;; interfere with interactive completion frameworks like ivy, helm, and hydra.
(add-hook 'minibuffer-setup-hook #'doom|inhibit-switch-hooks)
(add-hook 'minibuffer-exit-hook #'doom|restore-switch-hooks))
;; Apply `doom-theme' ;; Apply `doom-theme'
(if (daemonp) (if (daemonp)