 ;;; 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"


(autoload 'consult--multi "consult")
;;;###autoload
(defun +vertico/switch-workspace-buffer ()
  "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
  (when-let (buffer (consult--multi (+vertico--workspace-generate-sources)
                                    :require-match
                                    (confirm-nonexistent-file-or-buffer)

                                                    (+workspace-current-name))
                                    :history 'consult--buffer-history
                                    :sort nil))
    (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)
