Make buffer killing/cycling more reliable

This commit is contained in:
Henrik Lissner 2017-02-19 18:03:42 -05:00
parent 2d5d826177
commit 7682ce3ba4

View file

@ -24,6 +24,10 @@ killed by `doom:kill-old-buffers', or after `doom-kill-buffer').")
checks before killing processes. If there are no buffers with matching checks before killing processes. If there are no buffers with matching
major-modes, the process gets killed.") major-modes, the process gets killed.")
;;;###autoload
(defun doom-fallback-buffer ()
(get-buffer-create doom-fallback-buffer))
;;;###autoload ;;;###autoload
(defun doom-narrow-buffer (beg end &optional clone-p) (defun doom-narrow-buffer (beg end &optional clone-p)
"Restrict editing in this buffer to the current region, indirectly. With CLONE-P, "Restrict editing in this buffer to the current region, indirectly. With CLONE-P,
@ -114,30 +118,30 @@ See `doom-real-buffer-p' for what 'real' means."
(move-func (if (> n 0) 'switch-to-next-buffer 'switch-to-prev-buffer)) (move-func (if (> n 0) 'switch-to-next-buffer 'switch-to-prev-buffer))
(max 25) (max 25)
(i 0) (i 0)
(project-dir (doom-project-root))
(buffers (doom-real-buffers-list)) (buffers (doom-real-buffers-list))
(fail-buffer (cond ((> (length (get-buffer-window-list doom-fallback-buffer nil t)) 1)
start-buffer)
((bufferp doom-fallback-buffer)
doom-fallback-buffer)
(t doom-fallback-buffer)))
destbuf) destbuf)
(setq destbuf (setq destbuf
(catch 'goto (catch 'goto
(if (or (not buffers) (if (or (not buffers)
(= (length buffers) 1)) (= (length buffers) 1))
(progn (message "No other buffers in workspace") (progn (message "No other buffers in workspace")
(throw 'goto fail-buffer)) (throw 'goto t))
(funcall move-func) (funcall move-func)
(while (not (memq (current-buffer) buffers)) (while (not (memq (current-buffer) buffers))
(if (or (eq (current-buffer) start-buffer) (if (or (eq (current-buffer) start-buffer)
(>= i max)) (>= i max))
(throw 'goto fail-buffer) (throw 'goto t)
(funcall move-func)) (funcall move-func))
(cl-incf i)) (cl-incf i))
(current-buffer)))) (current-buffer))))
(when (eq destbuf fail-buffer) (when (eq destbuf t)
(setq destbuf (doom-fallback-buffer))
(message "Nowhere to go")) (message "Nowhere to go"))
(set-window-buffer (selected-window) destbuf))) (prog1
(set-buffer destbuf)
(when (eq destbuf (doom-fallback-buffer))
(cd project-dir)))))
;;;###autoload ;;;###autoload
(defun doom-real-buffer-p (&optional buffer-or-name) (defun doom-real-buffer-p (&optional buffer-or-name)
@ -176,38 +180,36 @@ c) and its major-mode or buffer-name-matching regexp isn't in
buffer, but buries the buffer if it is present in another window. buffer, but buries the buffer if it is present in another window.
See `doom-real-buffer-p' for what 'real' means." See `doom-real-buffer-p' for what 'real' means."
(let* ((old-project (doom-project-root)) (let* ((buffer (or buffer (current-buffer)))
(buffer (or buffer (current-buffer)))
(buffer-win (get-buffer-window buffer)) (buffer-win (get-buffer-window buffer))
(only-buffer-window-p (= (length (get-buffer-window-list buffer nil t)) 1))) (only-buffer-window-p (= 1 (length (get-buffer-window-list buffer nil t)))))
(with-current-buffer buffer
(when (and only-buffer-window-p (when (and only-buffer-window-p
(buffer-file-name buffer) (buffer-file-name buffer)
(buffer-modified-p buffer)) (buffer-modified-p buffer))
(with-current-buffer buffer
(if (and (not dont-save) (if (and (not dont-save)
(yes-or-no-p "Buffer is unsaved, save it?")) (yes-or-no-p "Buffer is unsaved, save it?"))
(save-buffer) (save-buffer)
(set-buffer-modified-p nil))) (set-buffer-modified-p nil))))
(if (window-dedicated-p buffer-win) (if (window-dedicated-p buffer-win)
(unless (window--delete buffer-win t t) (unless (window--delete buffer-win t t)
(split-window buffer-win) (split-window buffer-win)
(window--delete buffer-win t t)) (window--delete buffer-win t t))
(doom--cycle-real-buffers -1) (doom--cycle-real-buffers -1)
(unless (eq (current-buffer) buffer)
(when only-buffer-window-p
(kill-buffer buffer)
(unless (doom-real-buffer-p)
(doom--cycle-real-buffers -1))))
(when buffer-win (when buffer-win
(unrecord-window-buffer buffer-win buffer)) (unrecord-window-buffer buffer-win buffer))
(eq (current-buffer) buffer))))) (when only-buffer-window-p
(unless (eq buffer (doom-fallback-buffer))
(kill-buffer buffer))))
(eq (current-buffer) buffer)))
;;;###autoload ;;;###autoload
(defun doom-kill-buffer-and-windows (buffer) (defun doom-kill-buffer-and-windows (buffer)
"Kill the buffer and delete all the windows it's displayed in." "Kill the buffer and delete all the windows it's displayed in."
(unless (one-window-p t) (unless (one-window-p t)
(mapc (lambda (win) (unless (one-window-p t) (delete-window win))) (mapc (lambda (win) (unless (one-window-p t) (delete-window win)))
(get-buffer-window-list buf)))) (get-buffer-window-list buffer)))
(kill-buffer buffer))
;;;###autoload ;;;###autoload
(defun doom-kill-process-buffers () (defun doom-kill-process-buffers ()