feature/popup: make extra sure popups die safely

A buffer can find other, unexpected ways to kill itself, so we set up
a kill-buffer-hook to make sure we're there to catch them. Not all
heroes wear capes.
This commit is contained in:
Henrik Lissner 2018-01-07 22:13:42 -05:00
parent 395af731d9
commit bf7055a7db
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -14,16 +14,18 @@
"Tries to kill BUFFER, as was requested by a transient timer. If it fails, eg. "Tries to kill BUFFER, as was requested by a transient timer. If it fails, eg.
the buffer is visible, then set another timer and try again later." the buffer is visible, then set another timer and try again later."
(when (buffer-live-p buffer) (when (buffer-live-p buffer)
(if (get-buffer-window buffer) (let ((kill-buffer-hook (delq '+popup|kill-buffer-hook kill-buffer-hook)))
(with-current-buffer buffer (cond ((eq ttl 0)
(setq +popup--timer (kill-buffer buffer))
(run-at-time ttl nil #'+popup--kill-buffer buffer ttl))) ((get-buffer-window buffer)
(with-demoted-errors "Error killing transient buffer: %s" (with-current-buffer buffer
(let ((inhibit-message (not doom-debug-mode))) (setq +popup--timer
(message "Cleaned up transient buffer: %s" buffer)) (run-at-time ttl nil #'+popup--kill-buffer buffer ttl))))
(when-let* ((process (get-buffer-process (current-buffer)))) (t
(kill-process process)) (with-demoted-errors "Error killing transient buffer: %s"
(kill-buffer buffer))))) (when-let* ((process (get-buffer-process (current-buffer))))
(kill-process process))
(kill-buffer buffer)))))))
(defun +popup--init (window alist) (defun +popup--init (window alist)
"Initializes a popup window. Run any time a popup is opened. It sets the "Initializes a popup window. Run any time a popup is opened. It sets the
@ -76,6 +78,7 @@ and enables `+popup-buffer-mode'."
(cl-assert (integerp ttl) t) (cl-assert (integerp ttl) t)
(if (= ttl 0) (if (= ttl 0)
(+popup--kill-buffer buffer 0) (+popup--kill-buffer buffer 0)
(add-hook 'kill-buffer-hook #'+popup|kill-buffer-hook nil t)
(setq +popup--timer (setq +popup--timer
(run-at-time ttl nil #'+popup--kill-buffer (run-at-time ttl nil #'+popup--kill-buffer
buffer ttl))))))))) buffer ttl)))))))))
@ -194,8 +197,7 @@ Uses `shrink-window-if-larger-than-buffer'."
:init-value nil :init-value nil
:keymap +popup-buffer-mode-map :keymap +popup-buffer-mode-map
(when (and +popup-buffer-mode (timerp +popup--timer)) (when (and +popup-buffer-mode (timerp +popup--timer))
(let ((inhibit-message (not doom-debug-mode))) (remove-hook 'kill-buffer-hook #'+popup|kill-buffer-hook t)
(message "Cancelled timer in %s" (current-buffer)))
(cancel-timer +popup--timer) (cancel-timer +popup--timer)
(setq +popup--timer nil))) (setq +popup--timer nil)))
@ -254,6 +256,16 @@ disabled."
(when +popup-mode (when +popup-mode
(setq display-buffer-alist +popup--display-buffer-alist))) (setq display-buffer-alist +popup--display-buffer-alist)))
;;;###autoload
(defun +popup|kill-buffer-hook ()
"TODO"
(let ((buf (current-buffer))
(+popup--inhibit-transient t))
(when (+popup-buffer-p buf)
(when-let* ((window (get-buffer-window buf)))
(when (+popup-window-p window)
(+popup--destroy window))))))
;; ;;
;; Commands ;; Commands