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 '()
|
||||
"A list of functions that are run to determine if a buffer is real.")
|
||||
|
||||
;;;###autoload
|
||||
(defvar-local doom-real-buffer-p nil
|
||||
"If non-nil, this buffer should be considered real no matter what.")
|
||||
|
||||
|
@ -25,8 +26,6 @@ it if it doesn't exist).")
|
|||
scratch buffer."
|
||||
(get-buffer-create doom-fallback-buffer))
|
||||
|
||||
|
||||
;; Buffer Life and Death ;;;;;;;;;;;;;;;
|
||||
;;;###autoload
|
||||
(defalias 'doom-buffer-list #'buffer-list)
|
||||
|
||||
|
@ -49,6 +48,28 @@ If no project is active, return all buffers."
|
|||
if (doom-real-buffer-p 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
|
||||
(defun doom-buffers-in-mode (modes &optional buffer-list derived-p)
|
||||
"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)))
|
||||
|
||||
;;;###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
|
||||
(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))
|
||||
(defun doom-set-buffer-real (buffer flag)
|
||||
"Forcibly mark BUFFER as FLAG (non-nil = real)."
|
||||
(with-current-buffer buffer
|
||||
(setq doom-real-buffer-p flag)))
|
||||
|
||||
;;;###autoload
|
||||
(defun doom-kill-buffer (&optional buffer dont-save)
|
||||
"Kill BUFFER (falls back to current buffer if omitted) then switch to a real
|
||||
buffer. If the buffer is present in another window, only bury it.
|
||||
"Kill BUFFER (defaults to current buffer), but make sure we land on a real
|
||||
buffer. Bury the buffer if the buffer is present in another window.
|
||||
|
||||
Will prompt to save unsaved buffers when attempting to kill them, unless
|
||||
DONT-SAVE is non-nil.
|
||||
|
||||
See `doom-real-buffer-p' for what 'real' means."
|
||||
(setq buffer (or buffer (current-buffer)))
|
||||
(when (and (bufferp buffer) (buffer-live-p buffer))
|
||||
(unless buffer
|
||||
(setq buffer (current-buffer)))
|
||||
(when (and (bufferp buffer)
|
||||
(buffer-live-p buffer))
|
||||
(let ((buffer-win (get-buffer-window buffer)))
|
||||
;; deal with unsaved buffers
|
||||
;; deal with modified buffers
|
||||
(when (and (buffer-file-name buffer)
|
||||
(buffer-modified-p 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?"))
|
||||
(save-buffer)
|
||||
(set-buffer-modified-p nil))))
|
||||
(if buffer-win
|
||||
;; deal with dedicated windows
|
||||
(if (window-dedicated-p buffer-win)
|
||||
;; kill the buffer (or close dedicated window)
|
||||
(cond ((not buffer-win)
|
||||
(kill-buffer buffer))
|
||||
((window-dedicated-p buffer-win)
|
||||
(unless (window--delete buffer-win t t)
|
||||
(split-window buffer-win)
|
||||
(window--delete buffer-win t t))
|
||||
;; cycle to a real buffer
|
||||
(window--delete buffer-win t t)))
|
||||
(t ; cycle to a real buffer
|
||||
(with-selected-window buffer-win
|
||||
(doom--cycle-real-buffers -1)
|
||||
(when buffer-win
|
||||
(unrecord-window-buffer buffer-win buffer))
|
||||
(kill-buffer buffer)))
|
||||
(kill-buffer buffer)))
|
||||
(kill-buffer buffer)))))
|
||||
(not (eq (current-buffer) buffer))))
|
||||
|
||||
;;;###autoload
|
||||
|
@ -219,6 +210,11 @@ regex PATTERN. Returns the number of killed buffers."
|
|||
(dolist (buf buffers (length buffers))
|
||||
(doom-kill-buffer buf t))))
|
||||
|
||||
|
||||
;;
|
||||
;; Interactive commands
|
||||
;;
|
||||
|
||||
;;;###autoload
|
||||
(defun doom/kill-this-buffer (&optional interactive-p)
|
||||
"Use `doom-kill-buffer' on the current buffer."
|
||||
|
@ -297,7 +293,15 @@ project."
|
|||
(message "Cleaned up %s buffers" n))))
|
||||
|
||||
;;;###autoload
|
||||
(defun doom-set-buffer-real (buffer flag)
|
||||
"Forcibly mark BUFFER as FLAG (non-nil = real)."
|
||||
(with-current-buffer buffer
|
||||
(setq doom-real-buffer-p flag)))
|
||||
(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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue