Refactor core/autoload/buffers.el
This commit is contained in:
parent
7eb1198172
commit
61df57b86a
1 changed files with 62 additions and 58 deletions
|
@ -6,6 +6,7 @@
|
||||||
(defvar doom-real-buffer-functions '()
|
(defvar doom-real-buffer-functions '()
|
||||||
"A list of functions that are run to determine if a buffer is real.")
|
"A list of functions that are run to determine if a buffer is real.")
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
(defvar-local doom-real-buffer-p nil
|
(defvar-local doom-real-buffer-p nil
|
||||||
"If non-nil, this buffer should be considered real no matter what.")
|
"If non-nil, this buffer should be considered real no matter what.")
|
||||||
|
|
||||||
|
@ -25,8 +26,6 @@ it if it doesn't exist).")
|
||||||
scratch buffer."
|
scratch buffer."
|
||||||
(get-buffer-create doom-fallback-buffer))
|
(get-buffer-create doom-fallback-buffer))
|
||||||
|
|
||||||
|
|
||||||
;; Buffer Life and Death ;;;;;;;;;;;;;;;
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defalias 'doom-buffer-list #'buffer-list)
|
(defalias 'doom-buffer-list #'buffer-list)
|
||||||
|
|
||||||
|
@ -49,6 +48,28 @@ If no project is active, return all buffers."
|
||||||
if (doom-real-buffer-p buf)
|
if (doom-real-buffer-p buf)
|
||||||
collect buf))
|
collect buf))
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun doom-real-buffer-p (&optional buffer-or-name)
|
||||||
|
"Returns t if BUFFER-OR-NAME is a 'real' buffer. The complete criteria for a
|
||||||
|
real buffer is:
|
||||||
|
|
||||||
|
1. The buffer-local value of `doom-real-buffer-p' (variable) is non-nil OR
|
||||||
|
2. Any function in `doom-real-buffer-functions' must return non-nil when
|
||||||
|
passed this buffer OR
|
||||||
|
3. The current buffer:
|
||||||
|
a) has a `buffer-file-name' defined AND
|
||||||
|
b) is not in a popup window (see `doom-popup-p') AND
|
||||||
|
c) is not a special buffer (its name isn't something like *Help*)
|
||||||
|
|
||||||
|
If BUFFER-OR-NAME is omitted or nil, the current buffer is tested."
|
||||||
|
(when-let* ((buf (ignore-errors (window-normalize-buffer buffer-or-name))))
|
||||||
|
(or (buffer-local-value 'doom-real-buffer-p buf)
|
||||||
|
(run-hook-with-args-until-success 'doom-real-buffer-functions buf)
|
||||||
|
(not (or (doom-popup-p buf)
|
||||||
|
(minibufferp buf)
|
||||||
|
(string-match-p "^\\s-*\\*" (buffer-name buf))
|
||||||
|
(not (buffer-file-name buf)))))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom-buffers-in-mode (modes &optional buffer-list derived-p)
|
(defun doom-buffers-in-mode (modes &optional buffer-list derived-p)
|
||||||
"Return a list of buffers whose `major-mode' is `eq' to MODE(S).
|
"Return a list of buffers whose `major-mode' is `eq' to MODE(S).
|
||||||
|
@ -116,54 +137,26 @@ buffers. If there's nothing left, switch to `doom-fallback-buffer'. See
|
||||||
(current-buffer)))
|
(current-buffer)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom-real-buffer-p (&optional buffer-or-name)
|
(defun doom-set-buffer-real (buffer flag)
|
||||||
"Returns t if BUFFER-OR-NAME is a 'real' buffer. The complete criteria for a
|
"Forcibly mark BUFFER as FLAG (non-nil = real)."
|
||||||
real buffer is:
|
(with-current-buffer buffer
|
||||||
|
(setq doom-real-buffer-p flag)))
|
||||||
1. The buffer-local value of `doom-real-buffer-p' (variable) is non-nil OR
|
|
||||||
2. Any function in `doom-real-buffer-functions' must return non-nil when
|
|
||||||
passed this buffer OR
|
|
||||||
3. The current buffer:
|
|
||||||
a) has a `buffer-file-name' defined AND
|
|
||||||
b) is not in a popup window (see `doom-popup-p') AND
|
|
||||||
c) is not a special buffer (its name isn't something like *Help*)
|
|
||||||
|
|
||||||
If BUFFER-OR-NAME is omitted or nil, the current buffer is tested."
|
|
||||||
(when-let* ((buf (ignore-errors (window-normalize-buffer buffer-or-name))))
|
|
||||||
(or (buffer-local-value 'doom-real-buffer-p buf)
|
|
||||||
(run-hook-with-args-until-success 'doom-real-buffer-functions buf)
|
|
||||||
(not (or (doom-popup-p buf)
|
|
||||||
(minibufferp buf)
|
|
||||||
(string-match-p "^\\s-*\\*" (buffer-name buf))
|
|
||||||
(not (buffer-file-name buf)))))))
|
|
||||||
|
|
||||||
;;;###autoload
|
|
||||||
(defun doom/next-buffer ()
|
|
||||||
"Switch to the next real buffer, skipping non-real buffers. See
|
|
||||||
`doom-real-buffer-p' for what 'real' means."
|
|
||||||
(interactive)
|
|
||||||
(doom--cycle-real-buffers +1))
|
|
||||||
|
|
||||||
;;;###autoload
|
|
||||||
(defun doom/previous-buffer ()
|
|
||||||
"Switch to the previous real buffer, skipping non-real buffers. See
|
|
||||||
`doom-real-buffer-p' for what 'real' means."
|
|
||||||
(interactive)
|
|
||||||
(doom--cycle-real-buffers -1))
|
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom-kill-buffer (&optional buffer dont-save)
|
(defun doom-kill-buffer (&optional buffer dont-save)
|
||||||
"Kill BUFFER (falls back to current buffer if omitted) then switch to a real
|
"Kill BUFFER (defaults to current buffer), but make sure we land on a real
|
||||||
buffer. If the buffer is present in another window, only bury it.
|
buffer. Bury the buffer if the buffer is present in another window.
|
||||||
|
|
||||||
Will prompt to save unsaved buffers when attempting to kill them, unless
|
Will prompt to save unsaved buffers when attempting to kill them, unless
|
||||||
DONT-SAVE is non-nil.
|
DONT-SAVE is non-nil.
|
||||||
|
|
||||||
See `doom-real-buffer-p' for what 'real' means."
|
See `doom-real-buffer-p' for what 'real' means."
|
||||||
(setq buffer (or buffer (current-buffer)))
|
(unless buffer
|
||||||
(when (and (bufferp buffer) (buffer-live-p buffer))
|
(setq buffer (current-buffer)))
|
||||||
|
(when (and (bufferp buffer)
|
||||||
|
(buffer-live-p buffer))
|
||||||
(let ((buffer-win (get-buffer-window buffer)))
|
(let ((buffer-win (get-buffer-window buffer)))
|
||||||
;; deal with unsaved buffers
|
;; deal with modified buffers
|
||||||
(when (and (buffer-file-name buffer)
|
(when (and (buffer-file-name buffer)
|
||||||
(buffer-modified-p buffer))
|
(buffer-modified-p buffer))
|
||||||
(with-current-buffer buffer
|
(with-current-buffer buffer
|
||||||
|
@ -171,19 +164,17 @@ See `doom-real-buffer-p' for what 'real' means."
|
||||||
(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 buffer-win
|
;; kill the buffer (or close dedicated window)
|
||||||
;; deal with dedicated windows
|
(cond ((not buffer-win)
|
||||||
(if (window-dedicated-p buffer-win)
|
(kill-buffer buffer))
|
||||||
(unless (window--delete buffer-win t t)
|
((window-dedicated-p buffer-win)
|
||||||
(split-window buffer-win)
|
(unless (window--delete buffer-win t t)
|
||||||
(window--delete buffer-win t t))
|
(split-window buffer-win)
|
||||||
;; cycle to a real buffer
|
(window--delete buffer-win t t)))
|
||||||
(with-selected-window buffer-win
|
(t ; cycle to a real buffer
|
||||||
(doom--cycle-real-buffers -1)
|
(with-selected-window buffer-win
|
||||||
(when buffer-win
|
(doom--cycle-real-buffers -1)
|
||||||
(unrecord-window-buffer buffer-win buffer))
|
(kill-buffer buffer)))))
|
||||||
(kill-buffer buffer)))
|
|
||||||
(kill-buffer buffer)))
|
|
||||||
(not (eq (current-buffer) buffer))))
|
(not (eq (current-buffer) buffer))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
|
@ -219,6 +210,11 @@ regex PATTERN. Returns the number of killed buffers."
|
||||||
(dolist (buf buffers (length buffers))
|
(dolist (buf buffers (length buffers))
|
||||||
(doom-kill-buffer buf t))))
|
(doom-kill-buffer buf t))))
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; Interactive commands
|
||||||
|
;;
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom/kill-this-buffer (&optional interactive-p)
|
(defun doom/kill-this-buffer (&optional interactive-p)
|
||||||
"Use `doom-kill-buffer' on the current buffer."
|
"Use `doom-kill-buffer' on the current buffer."
|
||||||
|
@ -297,7 +293,15 @@ project."
|
||||||
(message "Cleaned up %s buffers" n))))
|
(message "Cleaned up %s buffers" n))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom-set-buffer-real (buffer flag)
|
(defun doom/next-buffer ()
|
||||||
"Forcibly mark BUFFER as FLAG (non-nil = real)."
|
"Switch to the next real buffer, skipping non-real buffers. See
|
||||||
(with-current-buffer buffer
|
`doom-real-buffer-p' for what 'real' means."
|
||||||
(setq doom-real-buffer-p flag)))
|
(interactive)
|
||||||
|
(doom--cycle-real-buffers +1))
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun doom/previous-buffer ()
|
||||||
|
"Switch to the previous real buffer, skipping non-real buffers. See
|
||||||
|
`doom-real-buffer-p' for what 'real' means."
|
||||||
|
(interactive)
|
||||||
|
(doom--cycle-real-buffers -1))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue