feat(vertico): switch to buffer's workspace

When selecting a buffer in another workspace with
+vertico/switch-workspace-buffer, that workspace will be switched to,
instead of opening the buffer in the current workspace.
This commit is contained in:
Henrik Lissner 2022-02-17 21:11:21 +01:00
parent 72c7d07fb4
commit 81629dc2ad
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -1,6 +1,28 @@
;;; completion/vertico/autoload/workspaces.el -*- lexical-binding: t; -*-
;;;###if (featurep! :ui workspaces)
(defun +vertico--workspace-buffer-state ()
(let ((preview
(let ((orig-buf (current-buffer))
cleanup-buffers)
(lambda (cand restore)
(when (and (not restore)
;; Only preview in current window and other window.
;; Preview in frames and tabs is not possible since
;; these don't get cleaned up.
(or (eq consult--buffer-display #'switch-to-buffer)
(eq consult--buffer-display #'switch-to-buffer-other-window)))
(cond
((and cand (get-buffer cand))
(unless (+workspace-contains-buffer-p cand)
(cl-pushnew cand cleanup-buffers))
(consult--buffer-action cand 'norecord))
((buffer-live-p orig-buf)
(consult--buffer-action orig-buf 'norecord)
(mapc #'persp-remove-buffer cleanup-buffers))))))))
(lambda (cand restore)
(funcall preview cand restore))))
(defun +vertico--workspace-generate-sources ()
"Generate list of consult buffer sources for all workspaces"
(let* ((active-workspace (+workspace-current-name))
@ -14,9 +36,9 @@
(cl-incf i)
`(:name ,name
:hidden ,(not (string= active-workspace name))
:narrow ,(nth i key-range)
:narrow ,(nth (1- i) key-range)
:category buffer
:state consult--buffer-state
:state +vertico--workspace-buffer-state
:items ,(lambda ()
(consult--buffer-query
:sort 'visibility
@ -29,13 +51,14 @@
(autoload 'consult--multi "consult")
;;;###autoload
(defun +vertico/switch-workspace-buffer ()
(defun +vertico/switch-workspace-buffer (buffers &optional force-same-workspace)
"Switch to another buffer in the same workspace.
Type the workspace's number (starting from 1) followed by a space to display its
buffer list."
(interactive)
;; FIXME Open buffers in other workspaces in their respective workspace
buffer list. Selecting a buffer in another workspace will switch to that
workspace instead. If FORCE-SAME-WORKSPACE (the prefix arg) is non-nil, that
buffer will be opened in the current workspace instead."
(interactive "P")
(when-let (buffer (consult--multi (+vertico--workspace-generate-sources)
:require-match
(confirm-nonexistent-file-or-buffer)
@ -43,10 +66,16 @@ buffer list."
(+workspace-current-name))
:history 'consult--buffer-history
:sort nil))
;; When the buffer does not belong to a source,
;; create a new buffer with the name.
(unless (cdr buffer)
(funcall consult--buffer-display (car buffer)))))
(let ((origin-workspace (plist-get (cdr buffer) :name)))
;; Switch to the workspace the buffer belongs to, maybe
(if (or (equal origin-workspace (+workspace-current-name))
force-same-workspace)
(funcall consult--buffer-display (car buffer))
(+workspace-switch origin-workspace)
(message "Switched to %S workspace" origin-workspace)
(if-let (window (get-buffer-window (car buffer)))
(select-window window)
(funcall consult--buffer-display (car buffer)))))))
;;;###autoload
(defun +vertico/embark-open-in-new-workspace (x)