Refactor doom/kill-*-buffer commands

This commit is contained in:
Henrik Lissner 2019-10-10 12:34:10 -04:00
parent 400dae27cd
commit a3fa1e07b1
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 61 additions and 55 deletions

View file

@ -273,74 +273,78 @@ If DONT-SAVE, don't prompt to save modified buffers (discarding their changes)."
(doom-kill-buffer-fixup-windows buffer)) (doom-kill-buffer-fixup-windows buffer))
;;;###autoload ;;;###autoload
(defun doom/kill-all-buffers (&optional project-p) (defun doom/kill-all-buffers (&optional buffer-list interactive)
"Kill all buffers and closes their windows. "Kill all buffers and closes their windows.
If PROJECT-P (universal argument), don't close windows and only kill buffers If the prefix arg is passed, doesn't close windows and only kill buffers that
that belong to the current project." belong to the current project."
(interactive "P") (interactive
(list (if current-prefix-arg
(doom-project-buffer-list)
(doom-buffer-list))
t))
(save-some-buffers) (save-some-buffers)
(unless project-p (delete-other-windows)
(delete-other-windows)) (when (memq (current-buffer) buffer-list)
(switch-to-buffer (doom-fallback-buffer)) (switch-to-buffer (doom-fallback-buffer)))
(let ((buffers (if project-p (doom-project-buffer-list) (doom-buffer-list)))) (mapc #'kill-buffer buffer-list)
(mapc #'kill-buffer buffers) (when interactive
(when (called-interactively-p 'interactive) (message "Killed %s buffers"
(message "Killed %s buffers" (- (length buffer-list)
(- (length buffers) (length (cl-remove-if-not #'buffer-live-p buffer-list))))))
(length (cl-remove-if-not #'buffer-live-p buffers)))))))
;;;###autoload ;;;###autoload
(defun doom/kill-other-buffers (&optional project-p) (defun doom/kill-other-buffers (&optional buffer-list interactive)
"Kill all other buffers (besides the current one). "Kill all other buffers (besides the current one).
If PROJECT-P (universal argument), kill only buffers that belong to the current If the prefix arg is passed, kill only buffers that belong to the current
project."
(interactive "P")
(let ((buffers
(delq (current-buffer)
(if project-p (doom-project-buffer-list) (doom-buffer-list)))))
(mapc #'doom-kill-buffer-and-windows buffers)
(when (called-interactively-p 'interactive)
(message "Killed %s buffers"
(- (length buffers)
(length (cl-remove-if-not #'buffer-live-p buffers)))))))
;;;###autoload
(defun doom/kill-matching-buffers (pattern &optional project-p)
"Kill buffers that match PATTERN in BUFFER-LIST.
If PROJECT-P (universal argument), only kill matching buffers in the current
project." project."
(interactive (interactive
(list (read-regexp "Buffer pattern: ") (list (delq (current-buffer)
current-prefix-arg)) (if current-prefix-arg
(let* ((buffers (if project-p (doom-project-buffer-list) (doom-buffer-list)))) (doom-project-buffer-list)
(doom-kill-matching-buffers pattern buffers) (doom-buffer-list)))
(when (called-interactively-p 'interactive) t))
(message "Killed %d buffer(s)" (mapc #'doom-kill-buffer-and-windows buffer-list)
(- (length buffers) (when interactive
(length (cl-remove-if-not #'buffer-live-p buffers))))))) (message "Killed %s buffers"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list))))))
;;;###autoload ;;;###autoload
(defun doom/kill-buried-buffers (&optional project-p) (defun doom/kill-matching-buffers (pattern &optional buffer-list interactive)
"Kill buffers that match PATTERN in BUFFER-LIST.
If the prefix arg is passed, only kill matching buffers in the current project."
(interactive
(list (read-regexp "Buffer pattern: ")
(if current-prefix-arg
(doom-project-buffer-list)
(doom-buffer-list))
t))
(doom-kill-matching-buffers pattern buffer-list)
(when interactive
(message "Killed %d buffer(s)"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list))))))
;;;###autoload
(defun doom/kill-buried-buffers (&optional buffer-list interactive)
"Kill buffers that are buried. "Kill buffers that are buried.
If PROJECT-P (universal argument), only kill buried buffers belonging to the If PROJECT-P (universal argument), only kill buried buffers belonging to the
current project." current project."
(interactive "P") (interactive
(let ((buffers (doom-buried-buffers (if project-p (doom-project-buffer-list))))) (list (doom-buried-buffers
(mapc #'kill-buffer buffers) (if current-prefix-arg (doom-project-buffer-list)))
(when (called-interactively-p 'interactive) t))
(message "Killed %d buffer(s)" (doom/kill-all-buffers buffer-list interactive))
(- (length buffers)
(length (cl-remove-if-not #'buffer-live-p buffers)))))))
;;;###autoload ;;;###autoload
(defun doom/kill-project-buffers (project) (defun doom/kill-project-buffers (project &optional interactive)
"Kill buffers for the specified PROJECT." "Kill buffers for the specified PROJECT."
(interactive (interactive
(list (if-let* ((open-projects (doom-open-projects))) (list (if-let (open-projects (doom-open-projects))
(completing-read (completing-read
"Kill buffers for project: " open-projects "Kill buffers for project: " open-projects
nil t nil nil nil t nil nil
@ -349,11 +353,12 @@ current project."
((member project-root open-projects))) ((member project-root open-projects)))
project-root)) project-root))
(message "No projects are open!") (message "No projects are open!")
nil))) nil)
t))
(when project (when project
(let ((buffers (doom-project-buffer-list project))) (let ((buffers (doom-project-buffer-list project)))
(doom-kill-buffers-fixup-windows buffers) (doom-kill-buffers-fixup-windows buffers)
(when (called-interactively-p 'interactive) (when interactive
(message "Killed %d buffer(s)" (message "Killed %d buffer(s)"
(- (length buffers) (- (length buffers)
(length (cl-remove-if-not #'buffer-live-p buffers)))))))) (length (cl-remove-if-not #'buffer-live-p buffers))))))))

View file

@ -143,14 +143,15 @@ This command understands vim file modifiers (like %:p:h). See
(interactive "<!>") (interactive "<!>")
(if (and bang (fboundp '+workspace/kill-session)) (if (and bang (fboundp '+workspace/kill-session))
(+workspace/kill-session) (+workspace/kill-session)
(doom/kill-all-buffers))) (call-interactively #'doom/kill-all-buffers)))
;;;###autoload (autoload '+evil:kill-matching-buffers "editor/evil/autoload/ex" nil t) ;;;###autoload (autoload '+evil:kill-matching-buffers "editor/evil/autoload/ex" nil t)
(evil-define-command +evil:kill-matching-buffers (&optional bang pattern) (evil-define-command +evil:kill-matching-buffers (&optional bang pattern)
"Kill all buffers matching PATTERN regexp. If BANG, only match project "Kill all buffers matching PATTERN regexp. If BANG, only match project
buffers." buffers."
(interactive "<a>") (interactive "<a>")
(doom/kill-matching-buffers pattern bang)) (doom/kill-matching-buffers
pattern (if bang (doom-project-buffer-list))))
;;;###autoload (autoload '+evil:help "editor/evil/autoload/ex" nil t) ;;;###autoload (autoload '+evil:help "editor/evil/autoload/ex" nil t)
(evil-define-command +evil:help (&optional bang query) (evil-define-command +evil:help (&optional bang query)

View file

@ -263,7 +263,7 @@ workspace to delete."
(+workspace-switch +workspaces-main t) (+workspace-switch +workspaces-main t)
(unless (string= (car workspaces) +workspaces-main) (unless (string= (car workspaces) +workspaces-main)
(+workspace-delete name)) (+workspace-delete name))
(doom/kill-all-buffers))) (doom/kill-all-buffers (doom-buffer-list))))
(+workspace-message (format "Deleted '%s' workspace" name) 'success))) (+workspace-message (format "Deleted '%s' workspace" name) 'success)))
('error (+workspace-error ex t)))) ('error (+workspace-error ex t))))
@ -274,7 +274,7 @@ workspace to delete."
(unless (cl-every #'+workspace-delete (+workspace-list-names)) (unless (cl-every #'+workspace-delete (+workspace-list-names))
(+workspace-error "Could not clear session")) (+workspace-error "Could not clear session"))
(+workspace-switch +workspaces-main t) (+workspace-switch +workspaces-main t)
(doom/kill-all-buffers)) (doom/kill-all-buffers (buffer-list)))
;;;###autoload ;;;###autoload
(defun +workspace/kill-session-and-quit () (defun +workspace/kill-session-and-quit ()