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; -*- ;;; completion/vertico/autoload/workspaces.el -*- lexical-binding: t; -*-
;;;###if (featurep! :ui workspaces) ;;;###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 () (defun +vertico--workspace-generate-sources ()
"Generate list of consult buffer sources for all workspaces" "Generate list of consult buffer sources for all workspaces"
(let* ((active-workspace (+workspace-current-name)) (let* ((active-workspace (+workspace-current-name))
@ -14,9 +36,9 @@
(cl-incf i) (cl-incf i)
`(:name ,name `(:name ,name
:hidden ,(not (string= active-workspace name)) :hidden ,(not (string= active-workspace name))
:narrow ,(nth i key-range) :narrow ,(nth (1- i) key-range)
:category buffer :category buffer
:state consult--buffer-state :state +vertico--workspace-buffer-state
:items ,(lambda () :items ,(lambda ()
(consult--buffer-query (consult--buffer-query
:sort 'visibility :sort 'visibility
@ -29,13 +51,14 @@
(autoload 'consult--multi "consult") (autoload 'consult--multi "consult")
;;;###autoload ;;;###autoload
(defun +vertico/switch-workspace-buffer () (defun +vertico/switch-workspace-buffer (buffers &optional force-same-workspace)
"Switch to another buffer in the 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 Type the workspace's number (starting from 1) followed by a space to display its
buffer list." buffer list. Selecting a buffer in another workspace will switch to that
(interactive) workspace instead. If FORCE-SAME-WORKSPACE (the prefix arg) is non-nil, that
;; FIXME Open buffers in other workspaces in their respective workspace buffer will be opened in the current workspace instead."
(interactive "P")
(when-let (buffer (consult--multi (+vertico--workspace-generate-sources) (when-let (buffer (consult--multi (+vertico--workspace-generate-sources)
:require-match :require-match
(confirm-nonexistent-file-or-buffer) (confirm-nonexistent-file-or-buffer)
@ -43,10 +66,16 @@ buffer list."
(+workspace-current-name)) (+workspace-current-name))
:history 'consult--buffer-history :history 'consult--buffer-history
:sort nil)) :sort nil))
;; When the buffer does not belong to a source, (let ((origin-workspace (plist-get (cdr buffer) :name)))
;; create a new buffer with the name. ;; Switch to the workspace the buffer belongs to, maybe
(unless (cdr buffer) (if (or (equal origin-workspace (+workspace-current-name))
(funcall consult--buffer-display (car buffer))))) 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 ;;;###autoload
(defun +vertico/embark-open-in-new-workspace (x) (defun +vertico/embark-open-in-new-workspace (x)