2017-06-08 11:47:56 +02:00
|
|
|
;;; core/autoload/buffers.el -*- lexical-binding: t; -*-
|
2017-01-22 20:16:34 -05:00
|
|
|
|
2017-03-06 19:07:41 -05:00
|
|
|
;;;###autoload
|
2018-01-28 20:37:40 -05:00
|
|
|
(defvar doom-real-buffer-functions
|
|
|
|
'(doom-dired-buffer-p)
|
2018-01-03 03:38:35 -05:00
|
|
|
"A list of predicate functions run to determine if a buffer is real, unlike
|
|
|
|
`doom-unreal-buffer-functions'. They are passed one argument: the buffer to be
|
|
|
|
tested.
|
|
|
|
|
|
|
|
Should any of its function returns non-nil, the rest of the functions are
|
2018-02-01 20:04:54 -05:00
|
|
|
ignored and the buffer is considered real.
|
|
|
|
|
|
|
|
See `doom-real-buffer-p' for more information.")
|
2018-01-03 03:38:35 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defvar doom-unreal-buffer-functions
|
|
|
|
'(minibufferp doom-special-buffer-p doom-non-file-visiting-buffer-p)
|
|
|
|
"A list of predicate functions run to determine if a buffer is *not* real,
|
|
|
|
unlike `doom-real-buffer-functions'. They are passed one argument: the buffer to
|
|
|
|
be tested.
|
|
|
|
|
|
|
|
Should any of these functions return non-nil, the rest of the functions are
|
2018-02-01 20:04:54 -05:00
|
|
|
ignored and the buffer is considered unreal.
|
|
|
|
|
|
|
|
See `doom-real-buffer-p' for more information.")
|
2017-03-06 19:07:41 -05:00
|
|
|
|
2017-12-30 00:58:58 -05:00
|
|
|
;;;###autoload
|
2017-07-08 21:08:14 +02:00
|
|
|
(defvar-local doom-real-buffer-p nil
|
2018-02-01 20:04:54 -05:00
|
|
|
"If non-nil, this buffer should be considered real no matter what. See
|
|
|
|
`doom-real-buffer-p' for more information.")
|
2017-07-08 21:08:14 +02:00
|
|
|
|
2017-02-08 02:02:51 -05:00
|
|
|
;;;###autoload
|
2017-01-22 20:16:34 -05:00
|
|
|
(defvar doom-fallback-buffer "*scratch*"
|
|
|
|
"The name of the buffer to fall back to if no other buffers exist (will create
|
|
|
|
it if it doesn't exist).")
|
|
|
|
|
2017-12-29 22:29:57 -05:00
|
|
|
|
|
|
|
;;
|
|
|
|
;; Functions
|
|
|
|
;;
|
|
|
|
|
2018-02-01 19:01:49 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-buffer-frame-predicate (buf)
|
|
|
|
"To be used as the default frame buffer-predicate parameter. Returns nil if
|
|
|
|
BUF should be skipped over by functions like `next-buffer' and `other-buffer'."
|
|
|
|
(or (doom-real-buffer-p buf)
|
|
|
|
(eq buf (doom-fallback-buffer))))
|
|
|
|
|
2017-02-19 18:03:42 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-fallback-buffer ()
|
2017-02-23 00:14:20 -05:00
|
|
|
"Returns the fallback buffer, creating it if necessary. By default this is the
|
|
|
|
scratch buffer."
|
2017-02-19 18:03:42 -05:00
|
|
|
(get-buffer-create doom-fallback-buffer))
|
|
|
|
|
2017-01-22 20:16:34 -05:00
|
|
|
;;;###autoload
|
2017-06-27 01:49:04 +02:00
|
|
|
(defalias 'doom-buffer-list #'buffer-list)
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-project-buffer-list ()
|
|
|
|
"Return a list of buffers belonging to the current project.
|
|
|
|
|
|
|
|
If no project is active, return all buffers."
|
|
|
|
(let ((buffers (doom-buffer-list)))
|
2017-12-10 14:49:52 -05:00
|
|
|
(if-let* ((project-root (if (doom-project-p) (doom-project-root))))
|
2017-06-27 01:49:04 +02:00
|
|
|
(cl-loop for buf in buffers
|
|
|
|
if (projectile-project-buffer-p buf project-root)
|
|
|
|
collect buf)
|
|
|
|
buffers)))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
2018-01-28 20:37:40 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-dired-buffer-p (buf)
|
|
|
|
"Returns non-nil if BUF is a dired buffer."
|
|
|
|
(with-current-buffer buf (derived-mode-p 'dired-mode)))
|
|
|
|
|
2018-01-03 03:38:35 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-special-buffer-p (buf)
|
|
|
|
"Returns non-nil if BUF's name starts and ends with an *."
|
|
|
|
(string-match-p "^\\s-*\\*" (buffer-name buf)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-non-file-visiting-buffer-p (buf)
|
|
|
|
"Returns non-nil if BUF does not have a value for `buffer-file-name'."
|
|
|
|
(not (buffer-file-name buf)))
|
|
|
|
|
2017-01-22 20:16:34 -05:00
|
|
|
;;;###autoload
|
2017-06-27 01:49:04 +02:00
|
|
|
(defun doom-real-buffer-list (&optional buffer-list)
|
|
|
|
"Return a list of buffers that satify `doom-real-buffer-p'."
|
2018-01-03 13:43:33 -05:00
|
|
|
(cl-remove-if-not #'doom-real-buffer-p (or buffer-list (doom-buffer-list))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
2017-12-30 00:58:58 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-real-buffer-p (&optional buffer-or-name)
|
2018-02-01 20:04:54 -05:00
|
|
|
"Returns t if BUFFER-OR-NAME is a 'real' buffer.
|
|
|
|
|
|
|
|
A real buffer is a useful buffer; a first class citizen in Doom. Real ones
|
|
|
|
should get special treatment, because we will be spending most of our time in
|
|
|
|
them. Unreal ones should be low-profile and easy to cast aside, so we can focus
|
|
|
|
on real ones.
|
|
|
|
|
|
|
|
The exact criteria for a real buffer is:
|
2017-12-30 00:58:58 -05:00
|
|
|
|
2018-01-03 03:38:35 -05:00
|
|
|
1. A non-nil value for the buffer-local value of the `doom-real-buffer-p'
|
|
|
|
variable OR
|
|
|
|
2. Any function in `doom-real-buffer-functions' returns non-nil OR
|
|
|
|
3. None of the functions in `doom-unreal-buffer-functions' must return
|
|
|
|
non-nil.
|
2017-12-30 00:58:58 -05:00
|
|
|
|
|
|
|
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)
|
2018-01-03 03:38:35 -05:00
|
|
|
(not (run-hook-with-args-until-success 'doom-unreal-buffer-functions buf)))))
|
2017-12-30 00:58:58 -05:00
|
|
|
|
2017-01-22 20:16:34 -05:00
|
|
|
;;;###autoload
|
2017-05-17 17:28:04 +02:00
|
|
|
(defun doom-buffers-in-mode (modes &optional buffer-list derived-p)
|
2017-06-27 01:49:04 +02:00
|
|
|
"Return a list of buffers whose `major-mode' is `eq' to MODE(S).
|
|
|
|
|
|
|
|
If DERIVED-P, test with `derived-mode-p', otherwise use `eq'."
|
|
|
|
(let ((modes (doom-enlist modes)))
|
2017-05-17 17:28:04 +02:00
|
|
|
(cl-remove-if-not (if derived-p
|
|
|
|
(lambda (buf)
|
|
|
|
(with-current-buffer buf
|
|
|
|
(apply #'derived-mode-p modes)))
|
|
|
|
(lambda (buf)
|
|
|
|
(memq (buffer-local-value 'major-mode buf) modes)))
|
2017-04-04 22:16:08 -04:00
|
|
|
(or buffer-list (doom-buffer-list)))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-visible-windows (&optional window-list)
|
2017-06-27 01:49:04 +02:00
|
|
|
"Return a list of the visible, non-popup windows."
|
2018-01-03 13:43:08 -05:00
|
|
|
(cl-loop for window in (or window-list (window-list))
|
|
|
|
unless (eq (window-dedicated-p window) 'side)
|
|
|
|
collect window))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-visible-buffers (&optional buffer-list)
|
2017-06-27 01:49:04 +02:00
|
|
|
"Return a list of visible buffers (i.e. not buried)."
|
2017-06-09 01:18:31 +02:00
|
|
|
(cl-loop for buf in (or buffer-list (doom-buffer-list))
|
|
|
|
when (get-buffer-window buf)
|
|
|
|
collect buf))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-buried-buffers (&optional buffer-list)
|
2017-06-27 01:49:04 +02:00
|
|
|
"Get a list of buffers that are buried."
|
2018-01-03 13:43:33 -05:00
|
|
|
(cl-remove-if #'get-buffer-window (or buffer-list (doom-buffer-list))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-matching-buffers (pattern &optional buffer-list)
|
2017-06-27 01:49:04 +02:00
|
|
|
"Get a list of all buffers that match the regex PATTERN."
|
2017-06-08 11:47:56 +02:00
|
|
|
(cl-loop for buf in (or buffer-list (doom-buffer-list))
|
|
|
|
when (string-match-p pattern (buffer-name buf))
|
2017-06-09 01:18:31 +02:00
|
|
|
collect buf))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
2018-01-03 13:44:14 -05:00
|
|
|
(defun doom--cycle-real-buffers (n)
|
2017-02-24 03:11:28 -05:00
|
|
|
"Switch to the next buffer N times (previous, if N < 0), skipping over unreal
|
|
|
|
buffers. If there's nothing left, switch to `doom-fallback-buffer'. See
|
|
|
|
`doom-real-buffer-p' for what 'real' means."
|
2018-01-03 13:44:14 -05:00
|
|
|
(if (null n)
|
|
|
|
(switch-to-buffer (doom-fallback-buffer) nil t)
|
|
|
|
(let ((buffers (delq (current-buffer) (doom-real-buffer-list))))
|
|
|
|
(cond ((or (not buffers)
|
|
|
|
(zerop (% n (1+ (length buffers)))))
|
|
|
|
(switch-to-buffer (doom-fallback-buffer) nil t))
|
|
|
|
((= (length buffers) 1)
|
|
|
|
(switch-to-buffer (car buffers) nil t))
|
|
|
|
(t
|
|
|
|
;; Why this instead of switching straight to the Nth buffer in
|
|
|
|
;; BUFFERS? Because `switch-to-next-buffer' and
|
|
|
|
;; `switch-to-prev-buffer' properly update buffer list order.
|
|
|
|
(cl-loop with move-func =
|
|
|
|
(if (> n 0) #'switch-to-next-buffer #'switch-to-prev-buffer)
|
|
|
|
for i to 20
|
|
|
|
while (not (memq (current-buffer) buffers))
|
|
|
|
do
|
|
|
|
(dotimes (_i (abs n))
|
|
|
|
(funcall move-func)))))))
|
|
|
|
(force-mode-line-update)
|
|
|
|
(current-buffer))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-12-30 00:58:58 -05:00
|
|
|
(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)))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-kill-buffer-and-windows (buffer)
|
|
|
|
"Kill the buffer and delete all the windows it's displayed in."
|
2017-06-08 11:47:56 +02:00
|
|
|
(dolist (window (get-buffer-window-list buffer))
|
|
|
|
(unless (one-window-p t)
|
|
|
|
(delete-window window)))
|
2017-02-19 18:03:42 -05:00
|
|
|
(kill-buffer buffer))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-kill-matching-buffers (pattern &optional buffer-list)
|
2017-02-23 00:14:20 -05:00
|
|
|
"Kill all buffers (in current workspace OR in BUFFER-LIST) that match the
|
2017-01-22 20:16:34 -05:00
|
|
|
regex PATTERN. Returns the number of killed buffers."
|
|
|
|
(let ((buffers (doom-matching-buffers pattern buffer-list)))
|
2017-06-27 01:49:04 +02:00
|
|
|
(dolist (buf buffers (length buffers))
|
2018-01-03 13:52:17 -05:00
|
|
|
(kill-buffer buf t))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
2017-12-30 00:58:58 -05:00
|
|
|
|
|
|
|
;;
|
|
|
|
;; Interactive commands
|
|
|
|
;;
|
|
|
|
|
2017-12-30 00:55:11 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/kill-this-buffer-in-all-windows (buffer &optional dont-save)
|
|
|
|
"Kill BUFFER globally and ensure all windows previously showing this buffer
|
|
|
|
have switched to a real buffer.
|
|
|
|
|
|
|
|
If DONT-SAVE, don't prompt to save modified buffers (discarding their changes)."
|
|
|
|
(interactive
|
|
|
|
(list (current-buffer) current-prefix-arg))
|
|
|
|
(cl-assert (bufferp buffer) t)
|
|
|
|
(let ((windows (get-buffer-window-list buffer nil t)))
|
2018-01-03 13:52:17 -05:00
|
|
|
(when (and (buffer-modified-p buffer) dont-save)
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(set-buffer-modified-p nil)))
|
|
|
|
(kill-buffer buffer)
|
2017-12-30 00:55:11 -05:00
|
|
|
(cl-loop for win in windows
|
|
|
|
if (doom-real-buffer-p (window-buffer win))
|
|
|
|
do (with-selected-window win (doom/previous-buffer)))))
|
|
|
|
|
2017-01-22 20:16:34 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/kill-all-buffers (&optional project-p)
|
2017-09-24 19:18:26 +02:00
|
|
|
"Kill all buffers and closes their windows.
|
2017-06-27 01:49:04 +02:00
|
|
|
|
2017-09-24 19:18:26 +02:00
|
|
|
If PROJECT-P (universal argument), kill only buffers that belong to the current
|
|
|
|
project."
|
2017-02-08 02:19:34 -05:00
|
|
|
(interactive "P")
|
2018-01-03 13:48:58 -05:00
|
|
|
(let ((buffers (if project-p (doom-project-buffer-list) (doom-buffer-list)))
|
|
|
|
(ignore-window-parameters t))
|
|
|
|
(delete-other-windows)
|
|
|
|
(switch-to-buffer (doom-fallback-buffer))
|
|
|
|
(let (kill-buffer-query-functions)
|
|
|
|
(message "Killed %s buffers"
|
|
|
|
(length (delq nil (mapcar #'kill-buffer buffers)))))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/kill-other-buffers (&optional project-p)
|
2017-09-24 19:18:26 +02:00
|
|
|
"Kill all other buffers (besides the current one).
|
2017-06-27 01:49:04 +02:00
|
|
|
|
2017-09-24 19:18:26 +02:00
|
|
|
If PROJECT-P (universal argument), kill only buffers that belong to the current
|
|
|
|
project."
|
2017-02-08 02:19:34 -05:00
|
|
|
(interactive "P")
|
2017-06-27 01:49:04 +02:00
|
|
|
(let ((buffers (if project-p (doom-project-buffer-list) (doom-buffer-list)))
|
2017-06-08 11:47:56 +02:00
|
|
|
(current-buffer (current-buffer)))
|
|
|
|
(dolist (buf buffers)
|
|
|
|
(unless (eq buf current-buffer)
|
|
|
|
(doom-kill-buffer-and-windows buf)))
|
2017-01-22 20:16:34 -05:00
|
|
|
(when (called-interactively-p 'interactive)
|
|
|
|
(message "Killed %s buffers" (length buffers)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/kill-matching-buffers (pattern &optional project-p)
|
2017-06-27 01:49:04 +02:00
|
|
|
"Kill buffers that match PATTERN in BUFFER-LIST.
|
|
|
|
|
|
|
|
If PROJECT-P (universal argument), only kill matching buffers in the current
|
|
|
|
project."
|
|
|
|
(interactive
|
|
|
|
(list (read-regexp "Buffer pattern: ")
|
|
|
|
current-prefix-arg))
|
|
|
|
(let* ((buffers (if project-p (doom-project-buffer-list) (doom-buffer-list)))
|
2017-01-22 20:16:34 -05:00
|
|
|
(n (doom-kill-matching-buffers pattern buffers)))
|
|
|
|
(when (called-interactively-p 'interactive)
|
|
|
|
(message "Killed %s buffers" n))))
|
|
|
|
|
|
|
|
;;;###autoload
|
2018-01-04 03:55:25 -05:00
|
|
|
(defun doom/cleanup-session (&optional all-p)
|
2017-12-30 00:59:44 -05:00
|
|
|
"Clean up buried buries and orphaned processes in the current workspace. If
|
|
|
|
ALL-P (universal argument), clean them up globally."
|
2018-01-04 03:55:25 -05:00
|
|
|
(interactive (list current-prefix-arg))
|
2017-06-27 01:49:04 +02:00
|
|
|
(let ((buffers (doom-buried-buffers (if all-p (buffer-list))))
|
2018-01-08 19:36:00 -05:00
|
|
|
(n 0))
|
2017-04-17 02:17:10 -04:00
|
|
|
(mapc #'kill-buffer buffers)
|
2017-12-30 00:59:44 -05:00
|
|
|
(setq n (+ n (length buffers) (doom/cleanup-processes)))
|
2018-01-04 03:55:25 -05:00
|
|
|
(dolist (hook doom-cleanup-hook)
|
2018-01-07 15:03:45 -05:00
|
|
|
(let ((m (funcall hook)))
|
|
|
|
(when (integerp m)
|
|
|
|
(setq n (+ n m)))))
|
2018-01-04 03:55:25 -05:00
|
|
|
(message "Cleaned up %s buffers" n)
|
2018-01-03 14:08:41 -05:00
|
|
|
n))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
2017-12-30 00:59:44 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/cleanup-processes ()
|
|
|
|
"Kill all processes that have no visible associated buffers. Return number of
|
|
|
|
processes killed."
|
|
|
|
(interactive)
|
|
|
|
(let ((n 0))
|
|
|
|
(dolist (p (process-list))
|
|
|
|
(let ((process-buffer (process-buffer p)))
|
|
|
|
(when (and (process-live-p p)
|
|
|
|
(not (string= (process-name p) "server"))
|
|
|
|
(or (not process-buffer)
|
|
|
|
(and (bufferp process-buffer)
|
|
|
|
(not (buffer-live-p process-buffer)))))
|
|
|
|
(delete-process p)
|
|
|
|
(cl-incf n))))
|
|
|
|
n))
|