2021-07-09 20:16:11 +03:00
|
|
|
;;; completion/vertico/autoload/workspaces.el -*- lexical-binding: t; -*-
|
2021-05-21 16:38:54 +03:00
|
|
|
;;;###if (featurep! :ui workspaces)
|
|
|
|
|
2022-02-17 21:11:21 +01:00
|
|
|
(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))))
|
|
|
|
|
2021-07-09 20:28:40 +03:00
|
|
|
(defun +vertico--workspace-generate-sources ()
|
2021-05-21 16:38:54 +03:00
|
|
|
"Generate list of consult buffer sources for all workspaces"
|
2022-02-17 19:37:05 +01:00
|
|
|
(let* ((active-workspace (+workspace-current-name))
|
|
|
|
(workspaces (+workspace-list-names))
|
|
|
|
(key-range (append (cl-loop for i from ?1 to ?9 collect i)
|
|
|
|
(cl-loop for i from ?a to ?z collect i)
|
|
|
|
(cl-loop for i from ?A to ?Z collect i)))
|
|
|
|
(last-i (length workspaces))
|
|
|
|
(i 0))
|
|
|
|
(mapcar (lambda (name)
|
|
|
|
(cl-incf i)
|
|
|
|
`(:name ,name
|
|
|
|
:hidden ,(not (string= active-workspace name))
|
2022-02-17 21:11:21 +01:00
|
|
|
:narrow ,(nth (1- i) key-range)
|
2022-02-17 19:37:05 +01:00
|
|
|
:category buffer
|
2022-02-17 21:11:21 +01:00
|
|
|
:state +vertico--workspace-buffer-state
|
2022-02-17 19:37:05 +01:00
|
|
|
:items ,(lambda ()
|
|
|
|
(consult--buffer-query
|
|
|
|
:sort 'visibility
|
|
|
|
:as #'buffer-name
|
|
|
|
:predicate
|
|
|
|
(lambda (buf)
|
|
|
|
(when-let (workspace (+workspace-get name t))
|
|
|
|
(+workspace-contains-buffer-p buf workspace)))))))
|
|
|
|
(+workspace-list-names))))
|
2021-05-21 16:38:54 +03:00
|
|
|
|
|
|
|
(autoload 'consult--multi "consult")
|
|
|
|
;;;###autoload
|
2022-02-17 21:17:39 +01:00
|
|
|
(defun +vertico/switch-workspace-buffer (&optional force-same-workspace)
|
2021-05-21 16:38:54 +03:00
|
|
|
"Switch to another buffer in the same workspace.
|
|
|
|
|
2022-02-17 19:37:05 +01:00
|
|
|
Type the workspace's number (starting from 1) followed by a space to display its
|
2022-02-17 21:11:21 +01:00
|
|
|
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")
|
2021-07-09 20:28:40 +03:00
|
|
|
(when-let (buffer (consult--multi (+vertico--workspace-generate-sources)
|
2021-05-21 16:38:54 +03:00
|
|
|
:require-match
|
|
|
|
(confirm-nonexistent-file-or-buffer)
|
|
|
|
:prompt (format "Switch to buffer (%s): "
|
2021-09-21 17:57:50 +03:00
|
|
|
(+workspace-current-name))
|
|
|
|
:history 'consult--buffer-history
|
|
|
|
:sort nil))
|
2022-02-17 21:11:21 +01:00
|
|
|
(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)))))))
|
2021-06-08 03:58:15 +03:00
|
|
|
|
|
|
|
;;;###autoload
|
2021-07-28 13:09:44 -04:00
|
|
|
(defun +vertico/embark-open-in-new-workspace (x)
|
2021-06-08 03:58:15 +03:00
|
|
|
"Open X (a file) in a new workspace."
|
2021-07-28 13:09:44 -04:00
|
|
|
(interactive)
|
2021-06-08 03:58:15 +03:00
|
|
|
(+workspace/new)
|
|
|
|
(find-file x))
|