Make buffer library (and ivy switch-buffer commands) workspace-centric, rather than project centric

This commit is contained in:
Henrik Lissner 2017-02-21 00:43:15 -05:00
parent 8ded4819e2
commit 5374d96781
2 changed files with 49 additions and 46 deletions

View file

@ -54,22 +54,26 @@ Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/"
;; Buffer Life and Death ;;;;;;;;;;;;;;; ;; Buffer Life and Death ;;;;;;;;;;;;;;;
;;;###autoload ;;;###autoload
(defun doom-buffer-list (&optional all-p) (defun doom-buffer-list (&optional project-p)
"Get all buffers in the current project, in the current workgroup. "Get all buffers in the current project, in the current workgroup.
If ALL-P is non-nil, get all buffers across all projects in current If PROJECT-P is non-nil, get all buffers associated with the current project in
workgroup." the current workspace."
(let ((buffers (cond ((and (featurep 'workgroups2) workgroups-mode) (let* ((buffers (cond ((and (featurep 'workgroups2) workgroups-mode)
(wg-workgroup-associated-buffers nil)) (wg-workgroup-associated-buffers nil))
((and (featurep 'persp-mode) persp-mode) ((and (featurep 'persp-mode) persp-mode)
(persp-buffer-list-restricted)) (persp-buffer-list-restricted))
(t (buffer-list)))) (t (buffer-list))))
(project-root (and (not all-p) (doom-project-root t)))) (project-root (and project-p (doom-project-root t)))
(if project-root (buffer-list (if project-root
(funcall (if (eq all-p 'not) 'cl-remove-if 'cl-remove-if-not) (funcall (if (eq project-p 'not) 'cl-remove-if 'cl-remove-if-not)
(lambda (b) (projectile-project-buffer-p b project-root)) (lambda (b) (projectile-project-buffer-p b project-root))
buffers) buffers)
buffers))) buffers))
(fallback-buffer (doom-fallback-buffer)))
(unless (memq fallback-buffer buffer-list)
(nconc buffer-list (list (doom-fallback-buffer))))
buffer-list))
;;;###autoload ;;;###autoload
(defun doom-real-buffers-list (&optional buffer-list) (defun doom-real-buffers-list (&optional buffer-list)
@ -251,7 +255,7 @@ regex PATTERN. Returns the number of killed buffers."
"Kill all buffers in this workspace. If PROJECT-P, kill all buffers that "Kill all buffers in this workspace. If PROJECT-P, kill all buffers that
belong to the current project in this workspace." belong to the current project in this workspace."
(interactive "P") (interactive "P")
(let ((buffers (doom-buffer-list (not project-p)))) (let ((buffers (doom-buffer-list project-p)))
(mapc 'doom-kill-buffer-and-windows buffers) (mapc 'doom-kill-buffer-and-windows buffers)
(when (called-interactively-p 'interactive) (when (called-interactively-p 'interactive)
(message "Killed %s buffers" (length buffers))))) (message "Killed %s buffers" (length buffers)))))
@ -261,7 +265,7 @@ belong to the current project in this workspace."
"Kill all other buffers in this workgroup. If PROJECT-P, kill only the other "Kill all other buffers in this workgroup. If PROJECT-P, kill only the other
buffers that belong to the current project." buffers that belong to the current project."
(interactive "P") (interactive "P")
(let ((buffers (doom-buffer-list (not project-p)))) (let ((buffers (doom-buffer-list project-p)))
(mapc (lambda (buf) (mapc (lambda (buf)
(unless (eq buf (current-buffer)) (unless (eq buf (current-buffer))
(doom-kill-buffer-and-windows buf))) (doom-kill-buffer-and-windows buf)))
@ -274,7 +278,7 @@ buffers that belong to the current project."
"Kill buffers in current workgroup that match regex PATTERN. If BANG, then "Kill buffers in current workgroup that match regex PATTERN. If BANG, then
exclude buffers that aren't part of the current project." exclude buffers that aren't part of the current project."
(interactive "sP") (interactive "sP")
(let* ((buffers (doom-buffer-list (not project-p))) (let* ((buffers (doom-buffer-list project-p))
(n (doom-kill-matching-buffers pattern buffers))) (n (doom-kill-matching-buffers pattern buffers)))
(when (called-interactively-p 'interactive) (when (called-interactively-p 'interactive)
(message "Killed %s buffers" n)))) (message "Killed %s buffers" n))))
@ -283,7 +287,7 @@ exclude buffers that aren't part of the current project."
(defun doom/cleanup-buffers () (defun doom/cleanup-buffers ()
"Clean up buried, unreal buffers." "Clean up buried, unreal buffers."
(interactive) (interactive)
(let ((buffers (doom-buried-buffers (doom-buffer-list t)))) (let ((buffers (doom-buried-buffers (doom-buffer-list))))
(mapc 'doom-kill-buffer buffers) (mapc 'doom-kill-buffer buffers)
(setq n (+ (doom-kill-process-buffers) (length buffers))) (setq n (+ (doom-kill-process-buffers) (length buffers)))
(when (called-interactively-p 'interactive) (when (called-interactively-p 'interactive)

View file

@ -1,7 +1,7 @@
;;; completion/ivy/autoload/ivy.el ;;; completion/ivy/autoload/ivy.el
;; Show more information in ivy-switch-buffer; and only display ;; Show more information in ivy-switch-buffer; and only display
;; project/workgroup-relevant buffers. ;; workgroup-relevant buffers.
(defun +ivy--get-buffers (&optional buffer-list) (defun +ivy--get-buffers (&optional buffer-list)
(let ((min-name 5) (let ((min-name 5)
(min-mode 5) (min-mode 5)
@ -19,8 +19,7 @@
(setq min-name (+ (length buffer-name) 15))) (setq min-name (+ (length buffer-name) 15)))
(when (> (length mode-name) min-mode) (when (> (length mode-name) min-mode)
(setq min-mode (+ (length mode-name) 3))) (setq min-mode (+ (length mode-name) 3)))
(list (list (concat
(concat
(propertize buffer-name (propertize buffer-name
'face (cond ((string-match-p "^ ?\\*" buffer-name) 'face (cond ((string-match-p "^ ?\\*" buffer-name)
'font-lock-comment-face) 'font-lock-comment-face)
@ -33,7 +32,7 @@
(propertize mode-name 'face 'font-lock-constant-face) (propertize mode-name 'face 'font-lock-constant-face)
(when buffer-file-name (when buffer-file-name
(abbreviate-file-name (file-name-directory buffer-file-name))))))) (abbreviate-file-name (file-name-directory buffer-file-name)))))))
(or buffer-list (doom-buffer-list t)))))) (or buffer-list (doom-buffer-list))))))
(defun +ivy--select-buffer-action (buffer) (defun +ivy--select-buffer-action (buffer)
(ivy--switch-buffer-action (ivy--switch-buffer-action
@ -42,22 +41,22 @@
(substring buffer 0 (s-index-of " " buffer))))) (substring buffer 0 (s-index-of " " buffer)))))
;;;###autoload ;;;###autoload
(defun +ivy/switch-project-buffer (&optional all-p) (defun +ivy/switch-workspace-buffer ()
"Displays open buffers in current project and workspace. If ALL-P, then show "Switch to an open buffer in the current workspace."
all open buffers."
(interactive) (interactive)
(ivy-read (format "%s buffers: " (if all-p "All" "Project")) (+ivy/switch-buffer t))
(+ivy--get-buffers (if all-p (buffer-list)))
;;;###autoload
(defun +ivy/switch-buffer (&optional workspace-only-p)
"Switch to an open buffer in the global buffer list. If WORKSPACE-ONLY-P,
limit to buffers in the current workspace."
(interactive)
(ivy-read (format "%s buffers: " (if workspace-only-p "Workspace" "Global"))
(+ivy--get-buffers (unless workspace-only-p (buffer-list)))
:matcher #'ivy--switch-buffer-matcher :matcher #'ivy--switch-buffer-matcher
:action #'+ivy--select-buffer-action :action #'+ivy--select-buffer-action
:keymap ivy-switch-buffer-map :keymap ivy-switch-buffer-map
:caller '+ivy/switch-project-buffer)) :caller '+ivy/switch-workspace-buffer))
;;;###autoload
(defun +ivy/switch-buffer ()
"Displays all open buffers, across projects and workspaces."
(interactive)
(+ivy/switch-project-buffer t))
;;;###autoload ;;;###autoload
(defun +ivy/kill-ring () (defun +ivy/kill-ring ()