2017-01-22 20:16:34 -05:00
|
|
|
;;; autoload.el
|
2017-02-06 00:25:54 -05:00
|
|
|
(provide 'doom-lib-buffers)
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
(defvar-local doom-buffer--narrowed-origin nil)
|
|
|
|
|
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-01-22 20:16:34 -05:00
|
|
|
(if (region-active-p)
|
2017-01-31 19:47:58 -05:00
|
|
|
(progn
|
|
|
|
(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))
|
2017-01-22 20:16:34 -05:00
|
|
|
(if doom-buffer--narrowed-origin
|
2017-01-31 19:47:58 -05:00
|
|
|
(progn
|
|
|
|
(kill-this-buffer)
|
|
|
|
(switch-to-buffer doom-buffer--narrowed-origin)
|
|
|
|
(setq doom-buffer--narrowed-origin nil))
|
2017-01-22 20:16:34 -05:00
|
|
|
(widen))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Buffer Life and Death ;;;;;;;;;;;;;;;
|
|
|
|
;;;###autoload
|
2017-02-21 00:43:15 -05:00
|
|
|
(defun doom-buffer-list (&optional project-p)
|
2017-02-23 00:14:20 -05:00
|
|
|
"Get all buffers in the current project, in the current workspace.
|
2017-01-22 20:16:34 -05:00
|
|
|
|
2017-02-21 00:43:15 -05:00
|
|
|
If PROJECT-P is non-nil, get all buffers associated with the current project in
|
|
|
|
the current workspace."
|
2017-02-24 03:08:48 -05:00
|
|
|
(let ((buffers (if (and (featurep 'persp-mode) persp-mode)
|
|
|
|
(persp-buffer-list-restricted)
|
|
|
|
(buffer-list)))
|
|
|
|
(project-root (and project-p (doom-project-root t))))
|
|
|
|
(if project-root
|
|
|
|
(funcall (if (eq project-p 'not) 'cl-remove-if 'cl-remove-if-not)
|
|
|
|
(lambda (b) (projectile-project-buffer-p b project-root))
|
|
|
|
buffers)
|
|
|
|
buffers)))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-real-buffers-list (&optional buffer-list)
|
2017-02-23 00:14:20 -05:00
|
|
|
"Get a list of all buffers (in the current workspace OR in BUFFER-LIST) that
|
2017-01-22 20:16:34 -05:00
|
|
|
`doom-real-buffer-p' returns non-nil for."
|
2017-02-19 18:02:40 -05:00
|
|
|
(cl-remove-if-not 'doom-real-buffer-p (or buffer-list (doom-buffer-list))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-buffers-in-mode (modes &optional buffer-list)
|
2017-02-23 00:14:20 -05:00
|
|
|
"Get a list of all buffers (in the current workspace OR in BUFFER-LIST) whose
|
2017-01-22 20:16:34 -05:00
|
|
|
`major-mode' is one of MODES."
|
2017-02-19 18:02:40 -05:00
|
|
|
(cl-remove-if-not (lambda (buf) (memq (buffer-local-value 'major-mode it) modes))
|
|
|
|
(or buffer-list (doom-buffer-list))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-visible-windows (&optional window-list)
|
|
|
|
"Get a list of the visible windows in the current frame (that aren't popups),
|
|
|
|
OR return only the visible windows in WINDOW-LIST."
|
2017-02-19 18:02:40 -05:00
|
|
|
(cl-remove-if 'doom-popup-p (or window-list (window-list))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-visible-buffers (&optional buffer-list)
|
2017-02-23 00:14:20 -05:00
|
|
|
"Get a list of unburied buffers in the current project and workspace, OR
|
2017-01-22 20:16:34 -05:00
|
|
|
return only the unburied buffers in BUFFER-LIST (a list of BUFFER-OR-NAMEs)."
|
2017-02-19 18:02:40 -05:00
|
|
|
(cl-remove-if-not 'get-buffer-window (or buffer-list (doom-buffer-list))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-buried-buffers (&optional buffer-list)
|
2017-02-23 00:14:20 -05:00
|
|
|
"Get a list of buried buffers in the current project and workspace, OR return
|
2017-01-22 20:16:34 -05:00
|
|
|
only the buried buffers in BUFFER-LIST (a list of BUFFER-OR-NAMEs)."
|
2017-02-19 18:02:40 -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-02-23 00:14:20 -05:00
|
|
|
"Get a list of all buffers (in the current workspace OR in BUFFER-LIST) that
|
2017-01-22 20:16:34 -05:00
|
|
|
match the regex PATTERN."
|
2017-02-19 18:02:40 -05:00
|
|
|
(cl-remove-if-not (lambda (buf) (string-match-p pattern (buffer-name buf)))
|
|
|
|
(or buffer-list (doom-buffer-list))))
|
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."
|
|
|
|
(let ((buffers (delq (current-buffer) (doom-real-buffers-list)))
|
|
|
|
(project-dir (doom-project-root)))
|
|
|
|
(cond ((or (not buffers)
|
|
|
|
(zerop (mod n (1+ (length buffers)))))
|
|
|
|
(set-window-buffer nil (doom-fallback-buffer)))
|
|
|
|
((= (length buffers) 1)
|
|
|
|
(set-window-buffer nil (car buffers)))
|
|
|
|
(t
|
|
|
|
(let ((move-func (if (> n 0) 'switch-to-next-buffer 'switch-to-prev-buffer)))
|
|
|
|
;; 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.
|
|
|
|
(while (not (memq (current-buffer) buffers))
|
|
|
|
(dotimes (i (abs n))
|
|
|
|
(funcall move-func))))))
|
|
|
|
(when (eq (current-buffer) (doom-fallback-buffer))
|
|
|
|
(cd project-dir))
|
|
|
|
(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-02-24 03:09:57 -05:00
|
|
|
"Returns t if BUFFER-OR-NAME is a 'real' buffer. Real means it a) isn't a
|
|
|
|
popup window/buffer and b) isn't a special buffer."
|
2017-02-19 18:02:40 -05:00
|
|
|
(when-let (buffer (ignore-errors (window-normalize-buffer buffer-or-name)))
|
2017-02-24 03:09:57 -05:00
|
|
|
(not (or (doom-popup-p buffer)
|
|
|
|
(string-match-p "^ ?\\*" (buffer-name buffer))))))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/next-buffer ()
|
|
|
|
"Switch to the next real buffer, skipping special 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 special buffers. See
|
|
|
|
`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
|
|
|
|
buffer, but buries the buffer if it is present in another window.
|
|
|
|
|
|
|
|
See `doom-real-buffer-p' for what 'real' means."
|
2017-02-19 18:03:42 -05:00
|
|
|
(let* ((buffer (or buffer (current-buffer)))
|
2017-02-08 02:19:01 -05:00
|
|
|
(buffer-win (get-buffer-window buffer))
|
2017-02-19 18:03:42 -05:00
|
|
|
(only-buffer-window-p (= 1 (length (get-buffer-window-list buffer nil t)))))
|
|
|
|
(when (and only-buffer-window-p
|
|
|
|
(buffer-file-name buffer)
|
|
|
|
(buffer-modified-p buffer))
|
|
|
|
(with-current-buffer buffer
|
2017-01-22 20:16:34 -05:00
|
|
|
(if (and (not dont-save)
|
|
|
|
(yes-or-no-p "Buffer is unsaved, save it?"))
|
|
|
|
(save-buffer)
|
2017-02-19 18:03:42 -05:00
|
|
|
(set-buffer-modified-p nil))))
|
|
|
|
(if (window-dedicated-p buffer-win)
|
|
|
|
(unless (window--delete buffer-win t t)
|
|
|
|
(split-window buffer-win)
|
|
|
|
(window--delete buffer-win t t))
|
|
|
|
(doom--cycle-real-buffers -1)
|
|
|
|
(when buffer-win
|
|
|
|
(unrecord-window-buffer buffer-win buffer))
|
|
|
|
(when only-buffer-window-p
|
2017-02-20 19:43:57 -05:00
|
|
|
(kill-buffer buffer)))
|
2017-02-19 18:03:42 -05:00
|
|
|
(eq (current-buffer) 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."
|
|
|
|
(unless (one-window-p t)
|
|
|
|
(mapc (lambda (win) (unless (one-window-p t) (delete-window win)))
|
2017-02-19 18:03:42 -05:00
|
|
|
(get-buffer-window-list buffer)))
|
|
|
|
(kill-buffer buffer))
|
2017-01-22 20:16:34 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-kill-process-buffers ()
|
2017-02-24 03:10:18 -05:00
|
|
|
"Kill all processes that have no visible associated buffers."
|
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)
|
|
|
|
(get-buffer-window-list process-buffer nil t))))
|
2017-01-22 20:16:34 -05:00
|
|
|
(delete-process p)
|
|
|
|
(setq n (1+ n)))))
|
|
|
|
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)))
|
|
|
|
(mapc 'doom-kill-buffer buffers)
|
|
|
|
(length buffers)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/kill-this-buffer ()
|
|
|
|
"Uses `doom-kill-buffer' on the current buffer."
|
|
|
|
(interactive)
|
2017-01-31 19:47:58 -05:00
|
|
|
(when (and (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)
|
|
|
|
"Kill all buffers in this workspace. If PROJECT-P, kill all buffers that
|
|
|
|
belong to the current project in this workspace."
|
2017-02-08 02:19:34 -05:00
|
|
|
(interactive "P")
|
2017-02-21 00:43:15 -05:00
|
|
|
(let ((buffers (doom-buffer-list project-p)))
|
2017-01-22 20:16:34 -05:00
|
|
|
(mapc 'doom-kill-buffer-and-windows buffers)
|
|
|
|
(when (called-interactively-p 'interactive)
|
|
|
|
(message "Killed %s buffers" (length buffers)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/kill-other-buffers (&optional project-p)
|
2017-02-23 00:14:20 -05:00
|
|
|
"Kill all other buffers in this workspace. If PROJECT-P, kill only the other
|
2017-01-22 20:16:34 -05:00
|
|
|
buffers that belong to the current project."
|
2017-02-08 02:19:34 -05:00
|
|
|
(interactive "P")
|
2017-02-21 00:43:15 -05:00
|
|
|
(let ((buffers (doom-buffer-list project-p)))
|
2017-01-22 20:16:34 -05:00
|
|
|
(mapc (lambda (buf)
|
|
|
|
(unless (eq buf (current-buffer))
|
|
|
|
(doom-kill-buffer-and-windows buf)))
|
|
|
|
buffers)
|
|
|
|
(when (called-interactively-p 'interactive)
|
|
|
|
(message "Killed %s buffers" (length buffers)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/kill-matching-buffers (pattern &optional project-p)
|
2017-02-23 00:14:20 -05:00
|
|
|
"Kill buffers in current workspace that match regex PATTERN. If BANG, then
|
2017-01-22 20:16:34 -05:00
|
|
|
exclude buffers that aren't part of the current project."
|
2017-02-08 02:19:34 -05:00
|
|
|
(interactive "sP")
|
2017-02-21 00:43:15 -05:00
|
|
|
(let* ((buffers (doom-buffer-list project-p))
|
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
|
|
|
|
(defun doom/cleanup-buffers ()
|
2017-02-23 00:14:20 -05:00
|
|
|
"Clean up buried and process buffers in the current workspace."
|
2017-01-22 20:16:34 -05:00
|
|
|
(interactive)
|
2017-02-23 00:14:20 -05:00
|
|
|
(let ((buffers (doom-buried-buffers)))
|
2017-02-22 23:57:05 -05:00
|
|
|
(mapc 'kill-buffer buffers)
|
2017-01-22 20:16:34 -05:00
|
|
|
(setq n (+ (doom-kill-process-buffers) (length buffers)))
|
|
|
|
(when (called-interactively-p 'interactive)
|
|
|
|
(message "Cleaned up %s buffers" n))))
|
|
|
|
|