2017-06-08 11:47:56 +02:00
|
|
|
;;; core/autoload/buffers.el -*- lexical-binding: t; -*-
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
(defvar-local doom-buffer--narrowed-origin nil)
|
|
|
|
|
2017-03-06 19:07:41 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defvar doom-real-buffer-functions '()
|
|
|
|
"A list of functions that are run to determine if a buffer is real.")
|
|
|
|
|
2017-07-08 21:08:14 +02:00
|
|
|
(defvar-local doom-real-buffer-p nil
|
|
|
|
"If non-nil, this buffer should be considered real no matter what.")
|
|
|
|
|
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-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-31 19:47:58 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-narrow-buffer (beg end &optional clone-p)
|
2017-02-03 08:03:48 -05:00
|
|
|
"Restrict editing in this buffer to the current region, indirectly. With CLONE-P,
|
2017-01-22 20:16:34 -05:00
|
|
|
clone the buffer and hard-narrow the selection. If mark isn't active, then widen
|
|
|
|
the buffer (if narrowed).
|
|
|
|
|
|
|
|
Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/"
|
2017-01-31 19:47:58 -05:00
|
|
|
(interactive "r")
|
2017-06-08 11:47:56 +02:00
|
|
|
(cond ((region-active-p)
|
|
|
|
(deactivate-mark)
|
|
|
|
(when clone-p
|
|
|
|
(let ((old-buf (current-buffer)))
|
|
|
|
(switch-to-buffer (clone-indirect-buffer nil nil))
|
|
|
|
(setq doom-buffer--narrowed-origin old-buf)))
|
|
|
|
(narrow-to-region beg end))
|
|
|
|
(doom-buffer--narrowed-origin
|
|
|
|
(kill-this-buffer)
|
|
|
|
(switch-to-buffer doom-buffer--narrowed-origin)
|
|
|
|
(setq doom-buffer--narrowed-origin nil))
|
|
|
|
(t
|
|
|
|
(widen))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
;; Buffer Life and Death ;;;;;;;;;;;;;;;
|
|
|
|
;;;###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
|
|
|
|
|
|
|
;;;###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'."
|
2017-06-09 01:18:31 +02:00
|
|
|
(cl-loop for buf in (or buffer-list (doom-buffer-list))
|
|
|
|
if (doom-real-buffer-p buf)
|
|
|
|
collect buf))
|
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."
|
2017-06-09 01:18:31 +02:00
|
|
|
(cl-loop for win in (or window-list (window-list))
|
|
|
|
unless (doom-popup-p win)
|
|
|
|
collect win))
|
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."
|
2017-06-09 01:18:31 +02:00
|
|
|
(cl-loop for buf in (or buffer-list (doom-buffer-list))
|
|
|
|
unless (get-buffer-window buf)
|
|
|
|
collect buf))
|
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
|
|
|
|
|
|
|
(defun doom--cycle-real-buffers (&optional 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."
|
2017-12-27 13:36:20 -05:00
|
|
|
(let ((buffers (delq (current-buffer) (doom-real-buffer-list))))
|
2017-02-24 03:11:28 -05:00
|
|
|
(cond ((or (not buffers)
|
2017-03-01 19:15:45 -05:00
|
|
|
(zerop (% n (1+ (length buffers)))))
|
2017-09-26 19:39:06 +02:00
|
|
|
(switch-to-buffer (doom-fallback-buffer) nil t))
|
2017-02-24 03:11:28 -05:00
|
|
|
((= (length buffers) 1)
|
2017-09-26 19:39:06 +02:00
|
|
|
(switch-to-buffer (car buffers) nil t))
|
2017-02-24 03:11:28 -05:00
|
|
|
(t
|
2017-06-27 01:49:04 +02:00
|
|
|
;; 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)
|
2017-07-12 23:54:56 +02:00
|
|
|
for i to 20
|
2017-06-27 01:49:04 +02:00
|
|
|
while (not (memq (current-buffer) buffers))
|
|
|
|
do
|
|
|
|
(dotimes (_i (abs n))
|
|
|
|
(funcall move-func)))))
|
2017-12-28 21:42:33 -05:00
|
|
|
(force-mode-line-update)
|
2017-02-24 03:11:28 -05:00
|
|
|
(current-buffer)))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-02-08 02:18:31 -05:00
|
|
|
(defun doom-real-buffer-p (&optional buffer-or-name)
|
2017-09-24 19:18:26 +02:00
|
|
|
"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."
|
2017-12-10 14:49:52 -05:00
|
|
|
(when-let* ((buf (ignore-errors (window-normalize-buffer buffer-or-name))))
|
2017-07-08 21:08:14 +02:00
|
|
|
(or (buffer-local-value 'doom-real-buffer-p buf)
|
|
|
|
(run-hook-with-args-until-success 'doom-real-buffer-functions buf)
|
2017-03-06 19:07:41 -05:00
|
|
|
(not (or (doom-popup-p buf)
|
2017-03-09 00:28:04 -05:00
|
|
|
(minibufferp buf)
|
2017-06-12 14:28:59 +02:00
|
|
|
(string-match-p "^\\s-*\\*" (buffer-name buf))
|
|
|
|
(not (buffer-file-name buf)))))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/next-buffer ()
|
2017-09-24 19:18:26 +02:00
|
|
|
"Switch to the next real buffer, skipping non-real buffers. See
|
2017-01-22 20:16:34 -05:00
|
|
|
`doom-real-buffer-p' for what 'real' means."
|
|
|
|
(interactive)
|
|
|
|
(doom--cycle-real-buffers +1))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/previous-buffer ()
|
2017-09-24 19:18:26 +02:00
|
|
|
"Switch to the previous real buffer, skipping non-real buffers. See
|
2017-01-22 20:16:34 -05:00
|
|
|
`doom-real-buffer-p' for what 'real' means."
|
|
|
|
(interactive)
|
|
|
|
(doom--cycle-real-buffers -1))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-kill-buffer (&optional buffer dont-save)
|
|
|
|
"Kill BUFFER (falls back to current buffer if omitted) then switch to a real
|
2017-09-24 19:18:26 +02:00
|
|
|
buffer. If the buffer is present in another window, only bury it.
|
|
|
|
|
|
|
|
Will prompt to save unsaved buffers when attempting to kill them, unless
|
|
|
|
DONT-SAVE is non-nil.
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
See `doom-real-buffer-p' for what 'real' means."
|
2017-06-08 02:01:07 +02:00
|
|
|
(setq buffer (or buffer (current-buffer)))
|
|
|
|
(when (and (bufferp buffer) (buffer-live-p buffer))
|
2017-12-28 21:43:48 -05:00
|
|
|
(let ((buffer-win (get-buffer-window buffer)))
|
2017-06-08 02:01:07 +02:00
|
|
|
;; deal with unsaved buffers
|
2017-12-28 21:43:48 -05:00
|
|
|
(when (and (buffer-file-name buffer)
|
2017-06-08 02:01:07 +02:00
|
|
|
(buffer-modified-p buffer))
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(if (and (not dont-save)
|
|
|
|
(yes-or-no-p "Buffer is unsaved, save it?"))
|
|
|
|
(save-buffer)
|
|
|
|
(set-buffer-modified-p nil))))
|
|
|
|
(if buffer-win
|
2017-06-27 01:49:04 +02:00
|
|
|
;; deal with dedicated windows
|
2017-06-08 02:01:07 +02:00
|
|
|
(if (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
|
|
|
|
(with-selected-window buffer-win
|
|
|
|
(doom--cycle-real-buffers -1)
|
|
|
|
(when buffer-win
|
|
|
|
(unrecord-window-buffer buffer-win buffer))
|
2017-12-28 21:43:48 -05:00
|
|
|
(kill-buffer buffer)))
|
|
|
|
(kill-buffer buffer)))
|
|
|
|
(not (eq (current-buffer) buffer))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
2017-05-12 12:06:56 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-force-kill-buffer (&optional buffer dont-save)
|
|
|
|
"Kill BUFFER globally and ensure all windows previously showing BUFFER have
|
|
|
|
switched to a real buffer."
|
|
|
|
(interactive)
|
|
|
|
(let* ((buffer (or buffer (current-buffer)))
|
|
|
|
(windows (get-buffer-window-list buffer nil t)))
|
2017-06-08 02:01:07 +02:00
|
|
|
(doom-kill-buffer buffer dont-save)
|
2017-05-12 12:06:56 +02:00
|
|
|
(dolist (win windows)
|
|
|
|
(with-selected-window win
|
|
|
|
(unless (doom-real-buffer-p)
|
|
|
|
(doom/previous-buffer))))))
|
|
|
|
|
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-process-buffers ()
|
2017-06-08 11:47:56 +02:00
|
|
|
"Kill all processes that have no visible associated buffers. Return number of
|
|
|
|
processes killed."
|
2017-01-22 20:16:34 -05:00
|
|
|
(interactive)
|
2017-02-24 03:10:18 -05:00
|
|
|
(let ((n 0))
|
2017-01-22 20:16:34 -05:00
|
|
|
(dolist (p (process-list))
|
2017-02-24 03:10:18 -05:00
|
|
|
(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)
|
2017-04-08 01:29:53 -04:00
|
|
|
(not (buffer-live-p process-buffer)))))
|
2017-01-22 20:16:34 -05:00
|
|
|
(delete-process p)
|
2017-06-08 11:47:56 +02:00
|
|
|
(cl-incf n))))
|
2017-01-22 20:16:34 -05:00
|
|
|
n))
|
|
|
|
|
|
|
|
;;;###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))
|
|
|
|
(doom-kill-buffer buf t))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/kill-this-buffer ()
|
2017-06-27 01:49:04 +02:00
|
|
|
"Use `doom-kill-buffer' on the current buffer."
|
2017-01-22 20:16:34 -05:00
|
|
|
(interactive)
|
2017-07-12 14:54:21 +02:00
|
|
|
(when (and (not (doom-kill-buffer)) (called-interactively-p 'interactive))
|
2017-01-22 20:16:34 -05:00
|
|
|
(message "Nowhere left to go!")))
|
|
|
|
|
|
|
|
;;;###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")
|
2017-09-26 19:41:11 +02:00
|
|
|
(doom/popup-kill-all)
|
2017-06-27 01:49:04 +02:00
|
|
|
(let ((buffers (if project-p (doom-project-buffer-list) (doom-buffer-list))))
|
2017-04-17 02:17:10 -04:00
|
|
|
(mapc #'doom-kill-buffer-and-windows buffers)
|
2017-09-13 12:49:36 +02:00
|
|
|
(unless (doom-real-buffer-p)
|
|
|
|
(switch-to-buffer (doom-fallback-buffer)))
|
|
|
|
(message "Killed %s buffers" (length 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
|
2017-04-12 08:52:22 -04:00
|
|
|
(defun doom/cleanup-buffers (&optional all-p)
|
2017-09-24 19:18:26 +02:00
|
|
|
"Clean up buried and inactive process buffers in the current workspace."
|
2017-04-12 08:52:22 -04:00
|
|
|
(interactive "P")
|
2017-12-26 19:27:14 -05:00
|
|
|
(run-hooks 'doom-cleanup-hook)
|
2017-06-27 01:49:04 +02:00
|
|
|
(let ((buffers (doom-buried-buffers (if all-p (buffer-list))))
|
|
|
|
(n 0))
|
2017-04-17 02:17:10 -04:00
|
|
|
(mapc #'kill-buffer buffers)
|
2017-06-27 01:49:04 +02:00
|
|
|
(setq n (+ n (length buffers) (doom-kill-process-buffers)))
|
2017-01-22 20:16:34 -05:00
|
|
|
(when (called-interactively-p 'interactive)
|
|
|
|
(message "Cleaned up %s buffers" n))))
|
|
|
|
|
2017-07-08 21:08:14 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-set-buffer-real (buffer flag)
|
2017-12-27 18:19:33 -05:00
|
|
|
"Forcibly mark BUFFER as FLAG (non-nil = real)."
|
2017-07-08 21:08:14 +02:00
|
|
|
(with-current-buffer buffer
|
|
|
|
(setq doom-real-buffer-p flag)))
|