2021-03-06 00:32:28 -05:00
|
|
|
;;; ui/workspaces/autoload/workspaces.el -*- lexical-binding: t; -*-
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-04-10 18:21:42 -04:00
|
|
|
(defvar +workspace--last nil)
|
2017-09-29 01:50:37 +02:00
|
|
|
(defvar +workspace--index 0)
|
2017-04-10 18:21:42 -04:00
|
|
|
|
2018-05-10 21:09:54 +02:00
|
|
|
;;;###autoload
|
2018-06-19 14:18:31 +02:00
|
|
|
(defface +workspace-tab-selected-face '((t (:inherit highlight)))
|
2017-04-17 02:20:07 -04:00
|
|
|
"The face for selected tabs displayed by `+workspace/display'"
|
2018-02-18 03:04:36 -05:00
|
|
|
:group 'persp-mode)
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2018-05-10 21:09:54 +02:00
|
|
|
;;;###autoload
|
2018-06-19 14:18:31 +02:00
|
|
|
(defface +workspace-tab-face '((t (:inherit default)))
|
2017-04-17 02:20:07 -04:00
|
|
|
"The face for selected tabs displayed by `+workspace/display'"
|
2018-02-18 03:04:36 -05:00
|
|
|
:group 'persp-mode)
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2017-06-27 23:22:27 +02:00
|
|
|
|
2017-09-29 01:50:37 +02:00
|
|
|
;;
|
2019-05-21 00:34:32 -04:00
|
|
|
;;; Library
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-09-29 01:50:37 +02:00
|
|
|
(defun +workspace--protected-p (name)
|
|
|
|
(equal name persp-nil-name))
|
2017-06-27 23:22:27 +02:00
|
|
|
|
2017-09-29 01:50:37 +02:00
|
|
|
(defun +workspace--generate-id ()
|
|
|
|
(or (cl-loop for name in (+workspace-list-names)
|
|
|
|
when (string-match-p "^#[0-9]+$" name)
|
|
|
|
maximize (string-to-number (substring name 1)) into max
|
|
|
|
finally return (if max (1+ max)))
|
|
|
|
1))
|
2017-07-08 13:44:41 +02:00
|
|
|
|
2017-06-27 23:22:27 +02:00
|
|
|
|
2019-05-21 00:34:32 -04:00
|
|
|
;;; Predicates
|
2017-02-04 03:21:04 -05:00
|
|
|
;;;###autoload
|
2018-01-20 02:44:12 -05:00
|
|
|
(defalias #'+workspace-p #'perspective-p
|
|
|
|
"Return t if OBJ is a perspective hash table.")
|
2017-02-04 03:21:04 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace-exists-p (name)
|
|
|
|
"Returns t if NAME is the name of an existing workspace."
|
2017-06-27 23:22:27 +02:00
|
|
|
(member name (+workspace-list-names)))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-09-29 01:50:37 +02:00
|
|
|
;;;###autoload
|
2018-07-09 21:53:50 +02:00
|
|
|
(defalias #'+workspace-contains-buffer-p #'persp-contain-buffer-p
|
|
|
|
"Return non-nil if BUFFER is in WORKSPACE (defaults to current workspace).")
|
2017-09-29 01:50:37 +02:00
|
|
|
|
|
|
|
|
2019-05-21 00:34:32 -04:00
|
|
|
;;; Getters
|
2018-01-20 02:44:12 -05:00
|
|
|
;;;###autoload
|
2018-03-22 06:28:00 -04:00
|
|
|
(defalias #'+workspace-current #'get-current-persp
|
2018-01-20 02:44:12 -05:00
|
|
|
"Return the currently active workspace.")
|
|
|
|
|
2017-02-04 03:21:04 -05:00
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace-get (name &optional noerror)
|
2018-01-20 02:44:12 -05:00
|
|
|
"Return a workspace named NAME. Unless NOERROR is non-nil, this throws an
|
|
|
|
error if NAME doesn't exist."
|
2018-06-19 14:59:41 +02:00
|
|
|
(cl-check-type name string)
|
2019-06-25 21:38:16 +02:00
|
|
|
(when-let (persp (persp-get-by-name name))
|
2017-09-29 01:50:37 +02:00
|
|
|
(cond ((+workspace-p persp) persp)
|
2018-01-20 02:44:12 -05:00
|
|
|
((not noerror)
|
|
|
|
(error "No workspace called '%s' was found" name)))))
|
2017-07-08 13:47:17 +02:00
|
|
|
|
2017-02-04 03:21:04 -05:00
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace-current-name ()
|
2017-09-29 01:50:37 +02:00
|
|
|
"Get the name of the current workspace."
|
2018-01-20 02:44:12 -05:00
|
|
|
(safe-persp-name (+workspace-current)))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-07-08 21:08:42 +02:00
|
|
|
;;;###autoload
|
2017-09-29 01:50:37 +02:00
|
|
|
(defun +workspace-list ()
|
2018-01-20 02:44:12 -05:00
|
|
|
"Return a list of workspace structs (satisifes `+workspace-p')."
|
2019-04-24 18:27:40 -04:00
|
|
|
;; We don't use `hash-table-values' because it doesn't ensure order in older
|
|
|
|
;; versions of Emacs
|
2020-02-06 13:39:21 -05:00
|
|
|
(cl-loop for name in persp-names-cache
|
|
|
|
if (gethash name *persp-hash*)
|
|
|
|
collect it))
|
2017-09-29 01:50:37 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace-list-names ()
|
2018-01-20 02:44:12 -05:00
|
|
|
"Return the list of names of open workspaces."
|
2020-02-06 13:39:21 -05:00
|
|
|
persp-names-cache)
|
2017-09-29 01:50:37 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace-buffer-list (&optional persp)
|
2018-01-20 02:44:12 -05:00
|
|
|
"Return a list of buffers in PERSP.
|
2017-09-29 01:50:37 +02:00
|
|
|
|
2018-01-20 02:44:12 -05:00
|
|
|
PERSP can be a string (name of a workspace) or a workspace (satisfies
|
|
|
|
`+workspace-p'). If nil or omitted, it defaults to the current workspace."
|
2018-06-19 14:59:41 +02:00
|
|
|
(let ((persp (or persp (+workspace-current))))
|
|
|
|
(unless (+workspace-p persp)
|
|
|
|
(user-error "Not in a valid workspace (%s)" persp))
|
2019-04-24 18:27:40 -04:00
|
|
|
(persp-buffers persp)))
|
2018-01-20 02:44:12 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace-orphaned-buffer-list ()
|
|
|
|
"Return a list of buffers that aren't associated with any perspective."
|
|
|
|
(cl-remove-if #'persp--buffer-in-persps (buffer-list)))
|
2017-09-29 01:50:37 +02:00
|
|
|
|
|
|
|
|
2019-05-21 00:34:32 -04:00
|
|
|
;;; Actions
|
2017-02-04 03:21:04 -05:00
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace-load (name)
|
2017-09-29 01:50:37 +02:00
|
|
|
"Loads a single workspace (named NAME) into the current session. Can only
|
|
|
|
retrieve perspectives that were explicitly saved with `+workspace-save'.
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-02-08 01:54:24 -05:00
|
|
|
Returns t if successful, nil otherwise."
|
2017-09-29 01:50:37 +02:00
|
|
|
(when (+workspace-exists-p name)
|
2018-06-19 14:59:41 +02:00
|
|
|
(user-error "A workspace named '%s' already exists." name))
|
2017-09-29 01:50:37 +02:00
|
|
|
(persp-load-from-file-by-names
|
2018-02-02 19:50:25 -05:00
|
|
|
(expand-file-name +workspaces-data-file persp-save-dir)
|
2017-09-29 01:50:37 +02:00
|
|
|
*persp-hash* (list name))
|
2017-02-08 01:54:24 -05:00
|
|
|
(+workspace-exists-p name))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-02-08 01:54:24 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace-save (name)
|
2017-09-29 01:50:37 +02:00
|
|
|
"Saves a single workspace (NAME) from the current session. Can be loaded again
|
|
|
|
with `+workspace-load'. NAME can be the string name of a workspace or its
|
|
|
|
perspective hash table.
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-02-08 01:54:24 -05:00
|
|
|
Returns t on success, nil otherwise."
|
|
|
|
(unless (+workspace-exists-p name)
|
2017-09-29 01:50:37 +02:00
|
|
|
(error "'%s' is an invalid workspace" name))
|
2018-02-02 19:50:25 -05:00
|
|
|
(let ((fname (expand-file-name +workspaces-data-file persp-save-dir)))
|
2017-09-29 01:50:37 +02:00
|
|
|
(persp-save-to-file-by-names fname *persp-hash* (list name))
|
|
|
|
(and (member name (persp-list-persp-names-in-file fname))
|
|
|
|
t)))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-02-08 01:54:24 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace-new (name)
|
|
|
|
"Create a new workspace named NAME. If one already exists, return nil.
|
|
|
|
Otherwise return t on success, nil otherwise."
|
2017-09-29 01:50:37 +02:00
|
|
|
(when (+workspace--protected-p name)
|
2017-04-10 18:21:42 -04:00
|
|
|
(error "Can't create a new '%s' workspace" name))
|
2017-09-29 01:50:37 +02:00
|
|
|
(when (+workspace-exists-p name)
|
|
|
|
(error "A workspace named '%s' already exists" name))
|
2018-08-26 14:12:50 +02:00
|
|
|
(let ((persp (persp-add-new name))
|
|
|
|
(+popup--inhibit-transient t))
|
|
|
|
(save-window-excursion
|
2019-04-21 00:03:44 -04:00
|
|
|
(let ((ignore-window-parameters t)
|
|
|
|
(+popup--inhibit-transient t))
|
2019-06-14 12:34:23 +02:00
|
|
|
(persp-delete-other-windows))
|
2018-08-26 14:12:50 +02:00
|
|
|
(switch-to-buffer (doom-fallback-buffer))
|
|
|
|
(setf (persp-window-conf persp)
|
2018-08-27 20:04:50 +02:00
|
|
|
(funcall persp-window-state-get-function (selected-frame))))
|
|
|
|
persp))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-02-08 01:54:24 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace-rename (name new-name)
|
|
|
|
"Rename the current workspace named NAME to NEW-NAME. Returns old name on
|
|
|
|
success, nil otherwise."
|
2017-09-29 01:50:37 +02:00
|
|
|
(when (+workspace--protected-p name)
|
2017-04-17 02:20:07 -04:00
|
|
|
(error "Can't rename '%s' workspace" name))
|
2018-01-14 16:21:18 -05:00
|
|
|
(persp-rename new-name (+workspace-get name)))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2018-10-27 00:00:13 +02:00
|
|
|
(defun +workspace-delete (workspace &optional inhibit-kill-p)
|
|
|
|
"Delete the workspace denoted by WORKSPACE, which can be the name of a perspective
|
2017-09-29 01:50:37 +02:00
|
|
|
or its hash table. If INHIBIT-KILL-P is non-nil, don't kill this workspace's
|
|
|
|
buffers."
|
2018-10-27 00:00:13 +02:00
|
|
|
(unless (stringp workspace)
|
|
|
|
(setq workspace (persp-name workspace)))
|
|
|
|
(when (+workspace--protected-p workspace)
|
|
|
|
(error "Can't delete '%s' workspace" workspace))
|
|
|
|
(+workspace-get workspace) ; error checking
|
|
|
|
(persp-kill workspace inhibit-kill-p)
|
|
|
|
(not (+workspace-exists-p workspace)))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-03-06 19:24:25 -05:00
|
|
|
(defun +workspace-switch (name &optional auto-create-p)
|
2018-01-20 02:44:12 -05:00
|
|
|
"Switch to another workspace named NAME (a string).
|
|
|
|
|
|
|
|
If AUTO-CREATE-P is non-nil, create the workspace if it doesn't exist, otherwise
|
|
|
|
throws an error."
|
2017-02-08 01:54:24 -05:00
|
|
|
(unless (+workspace-exists-p name)
|
2017-03-06 19:24:25 -05:00
|
|
|
(if auto-create-p
|
|
|
|
(+workspace-new name)
|
|
|
|
(error "%s is not an available workspace" name)))
|
2017-04-10 18:21:42 -04:00
|
|
|
(let ((old-name (+workspace-current-name)))
|
2020-01-02 21:13:31 -05:00
|
|
|
(unless (equal old-name name)
|
|
|
|
(setq +workspace--last
|
|
|
|
(or (and (not (string= old-name persp-nil-name))
|
|
|
|
old-name)
|
|
|
|
+workspaces-main))
|
|
|
|
(persp-frame-switch name))
|
2018-01-14 16:21:18 -05:00
|
|
|
(equal (+workspace-current-name) name)))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
2019-05-21 00:34:32 -04:00
|
|
|
;;; Commands
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2019-03-02 01:04:41 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defalias '+workspace/restore-last-session #'doom/quickload-session)
|
|
|
|
|
2018-04-25 16:56:49 -04:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/load (name)
|
|
|
|
"Load a workspace and switch to it. If called with C-u, try to reload the
|
|
|
|
current workspace (by name) from session files."
|
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(if current-prefix-arg
|
|
|
|
(+workspace-current-name)
|
|
|
|
(completing-read
|
|
|
|
"Workspace to load: "
|
|
|
|
(persp-list-persp-names-in-file
|
2018-05-19 15:02:54 +01:00
|
|
|
(expand-file-name +workspaces-data-file persp-save-dir))))))
|
2018-04-25 16:56:49 -04:00
|
|
|
(if (not (+workspace-load name))
|
|
|
|
(+workspace-error (format "Couldn't load workspace %s" name))
|
|
|
|
(+workspace/switch-to name)
|
|
|
|
(+workspace/display)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/save (name)
|
|
|
|
"Save the current workspace. If called with C-u, autosave the current
|
|
|
|
workspace."
|
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(if current-prefix-arg
|
|
|
|
(+workspace-current-name)
|
|
|
|
(completing-read "Workspace to save: " (+workspace-list-names)))))
|
|
|
|
(if (+workspace-save name)
|
|
|
|
(+workspace-message (format "'%s' workspace saved" name) 'success)
|
|
|
|
(+workspace-error (format "Couldn't save workspace %s" name))))
|
|
|
|
|
2017-02-08 01:54:24 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/rename (new-name)
|
|
|
|
"Rename the current workspace."
|
2017-09-09 19:30:08 -04:00
|
|
|
(interactive (list (read-from-minibuffer "New workspace name: ")))
|
2018-01-20 02:44:12 -05:00
|
|
|
(condition-case-unless-debug ex
|
2017-02-08 01:54:24 -05:00
|
|
|
(let* ((current-name (+workspace-current-name))
|
|
|
|
(old-name (+workspace-rename current-name new-name)))
|
|
|
|
(unless old-name
|
|
|
|
(error "Failed to rename %s" current-name))
|
|
|
|
(+workspace-message (format "Renamed '%s'->'%s'" old-name new-name) 'success))
|
2018-01-20 02:44:12 -05:00
|
|
|
('error (+workspace-error ex t))))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace/delete (name)
|
|
|
|
"Delete this workspace. If called with C-u, prompts you for the name of the
|
|
|
|
workspace to delete."
|
|
|
|
(interactive
|
|
|
|
(let ((current-name (+workspace-current-name)))
|
|
|
|
(list
|
|
|
|
(if current-prefix-arg
|
|
|
|
(completing-read (format "Delete workspace (default: %s): " current-name)
|
2017-06-27 23:22:27 +02:00
|
|
|
(+workspace-list-names)
|
2019-09-29 12:50:20 -04:00
|
|
|
nil nil nil nil current-name)
|
2017-02-08 01:54:24 -05:00
|
|
|
current-name))))
|
2018-01-20 02:44:12 -05:00
|
|
|
(condition-case-unless-debug ex
|
2019-09-29 23:12:43 -04:00
|
|
|
;; REVIEW refactor me
|
2018-05-29 00:42:33 +02:00
|
|
|
(let ((workspaces (+workspace-list-names)))
|
2018-08-26 14:14:53 +02:00
|
|
|
(if (not (member name workspaces))
|
|
|
|
(+workspace-message (format "'%s' workspace doesn't exist" name) 'warn)
|
|
|
|
(cond ((delq (selected-frame) (persp-frames-with-persp (get-frame-persp)))
|
|
|
|
(user-error "Can't close workspace, it's visible in another frame"))
|
2019-09-29 23:12:43 -04:00
|
|
|
((not (equal (+workspace-current-name) name))
|
|
|
|
(+workspace-delete name))
|
2019-10-03 23:33:59 -04:00
|
|
|
((cdr workspaces)
|
2018-08-26 14:14:53 +02:00
|
|
|
(+workspace-delete name)
|
|
|
|
(+workspace-switch
|
|
|
|
(if (+workspace-exists-p +workspace--last)
|
|
|
|
+workspace--last
|
|
|
|
(car (+workspace-list-names))))
|
2019-03-27 14:34:30 -04:00
|
|
|
(unless (doom-buffer-frame-predicate (window-buffer))
|
2018-08-26 14:14:53 +02:00
|
|
|
(switch-to-buffer (doom-fallback-buffer))))
|
|
|
|
(t
|
|
|
|
(+workspace-switch +workspaces-main t)
|
|
|
|
(unless (string= (car workspaces) +workspaces-main)
|
|
|
|
(+workspace-delete name))
|
2019-10-10 12:34:10 -04:00
|
|
|
(doom/kill-all-buffers (doom-buffer-list))))
|
2018-08-26 14:14:53 +02:00
|
|
|
(+workspace-message (format "Deleted '%s' workspace" name) 'success)))
|
2018-01-20 02:44:12 -05:00
|
|
|
('error (+workspace-error ex t))))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2019-12-21 02:24:11 -05:00
|
|
|
(defun +workspace/kill-session (&optional interactive)
|
2018-01-20 02:44:12 -05:00
|
|
|
"Delete the current session, all workspaces, windows and their buffers."
|
2019-12-21 02:24:11 -05:00
|
|
|
(interactive (list t))
|
|
|
|
(let ((windows (length (window-list)))
|
|
|
|
(persps (length (+workspace-list-names)))
|
|
|
|
(buffers 0))
|
|
|
|
(let ((persp-autokill-buffer-on-remove t))
|
|
|
|
(unless (cl-every #'+workspace-delete (+workspace-list-names))
|
|
|
|
(+workspace-error "Could not clear session")))
|
|
|
|
(+workspace-switch +workspaces-main t)
|
|
|
|
(setq buffers (doom/kill-all-buffers (buffer-list)))
|
|
|
|
(when interactive
|
|
|
|
(message "Killed %d workspace(s), %d window(s) & %d buffer(s)"
|
|
|
|
persps windows buffers))))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2018-02-07 19:27:41 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/kill-session-and-quit ()
|
|
|
|
"Kill emacs without saving anything."
|
|
|
|
(interactive)
|
|
|
|
(let ((persp-auto-save-opt 0))
|
|
|
|
(kill-emacs)))
|
|
|
|
|
2017-02-04 03:21:04 -05:00
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace/new (&optional name clone-p)
|
2018-01-20 02:44:12 -05:00
|
|
|
"Create a new workspace named NAME. If CLONE-P is non-nil, clone the current
|
|
|
|
workspace, otherwise the new workspace is blank."
|
2020-10-04 23:17:50 -04:00
|
|
|
(interactive (list nil current-prefix-arg))
|
2017-02-08 01:54:24 -05:00
|
|
|
(unless name
|
|
|
|
(setq name (format "#%s" (+workspace--generate-id))))
|
2018-06-23 19:59:17 +02:00
|
|
|
(condition-case e
|
|
|
|
(cond ((+workspace-exists-p name)
|
|
|
|
(error "%s already exists" name))
|
|
|
|
(clone-p (persp-copy name t))
|
|
|
|
(t
|
|
|
|
(+workspace-switch name t)
|
|
|
|
(+workspace/display)))
|
|
|
|
((debug error) (+workspace-error (cadr e) t))))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2021-07-08 22:13:13 -07:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/new-named (name)
|
|
|
|
"Create a new workspace with a given NAME."
|
|
|
|
(interactive "sWorkspace Name: ")
|
|
|
|
(+workspace/new name))
|
|
|
|
|
2019-12-01 05:10:40 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/switch-to (index)
|
|
|
|
"Switch to a workspace at a given INDEX. A negative number will start from the
|
|
|
|
end of the workspace list."
|
|
|
|
(interactive
|
|
|
|
(list (or current-prefix-arg
|
2022-08-12 20:29:19 +02:00
|
|
|
(if (modulep! :completion ivy)
|
2019-12-01 05:10:40 -05:00
|
|
|
(ivy-read "Switch to workspace: "
|
|
|
|
(+workspace-list-names)
|
|
|
|
:caller #'+workspace/switch-to
|
|
|
|
:preselect (+workspace-current-name))
|
|
|
|
(completing-read "Switch to workspace: " (+workspace-list-names))))))
|
2017-02-19 18:40:39 -05:00
|
|
|
(when (and (stringp index)
|
|
|
|
(string-match-p "^[0-9]+$" index))
|
2017-02-08 01:54:24 -05:00
|
|
|
(setq index (string-to-number index)))
|
2018-06-19 14:59:41 +02:00
|
|
|
(condition-case-unless-debug ex
|
2017-06-27 23:22:27 +02:00
|
|
|
(let ((names (+workspace-list-names))
|
2017-03-01 21:41:16 -05:00
|
|
|
(old-name (+workspace-current-name)))
|
2017-02-08 01:54:24 -05:00
|
|
|
(cond ((numberp index)
|
|
|
|
(let ((dest (nth index names)))
|
|
|
|
(unless dest
|
|
|
|
(error "No workspace at #%s" (1+ index)))
|
2017-04-10 18:21:42 -04:00
|
|
|
(+workspace-switch dest)))
|
2017-02-08 01:54:24 -05:00
|
|
|
((stringp index)
|
2019-12-01 15:02:38 +07:00
|
|
|
(+workspace-switch index t))
|
2017-04-10 18:21:42 -04:00
|
|
|
(t
|
|
|
|
(error "Not a valid index: %s" index)))
|
2017-02-08 01:54:24 -05:00
|
|
|
(unless (called-interactively-p 'interactive)
|
2019-12-01 05:10:40 -05:00
|
|
|
(if (equal (+workspace-current-name) old-name)
|
2017-03-01 21:41:16 -05:00
|
|
|
(+workspace-message (format "Already in %s" old-name) 'warn)
|
|
|
|
(+workspace/display))))
|
2017-02-08 01:54:24 -05:00
|
|
|
('error (+workspace-error (cadr ex) t))))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2019-06-11 16:09:29 +02:00
|
|
|
(dotimes (i 9)
|
2019-09-26 17:55:33 -04:00
|
|
|
(defalias (intern (format "+workspace/switch-to-%d" i))
|
2019-12-25 21:04:52 -05:00
|
|
|
(lambda () (interactive) (+workspace/switch-to i))
|
|
|
|
(format "Switch to workspace #%d" (1+ i))))
|
2019-06-11 16:09:29 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/switch-to-final ()
|
|
|
|
"Switch to the final workspace in open workspaces."
|
2017-02-04 03:21:04 -05:00
|
|
|
(interactive)
|
2017-06-27 23:22:27 +02:00
|
|
|
(+workspace/switch-to (car (last (+workspace-list-names)))))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2019-06-11 16:09:29 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/other ()
|
|
|
|
"Switch to the last activated workspace."
|
|
|
|
(interactive)
|
|
|
|
(+workspace/switch-to +workspace--last))
|
|
|
|
|
2017-02-04 03:21:04 -05:00
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace/cycle (n)
|
|
|
|
"Cycle n workspaces to the right (default) or left."
|
|
|
|
(interactive (list 1))
|
2017-04-12 11:27:31 -04:00
|
|
|
(let ((current-name (+workspace-current-name)))
|
|
|
|
(if (equal current-name persp-nil-name)
|
|
|
|
(+workspace-switch +workspaces-main t)
|
2018-01-20 02:44:12 -05:00
|
|
|
(condition-case-unless-debug ex
|
2017-06-27 23:22:27 +02:00
|
|
|
(let* ((persps (+workspace-list-names))
|
2017-04-12 11:27:31 -04:00
|
|
|
(perspc (length persps))
|
2017-04-17 02:19:20 -04:00
|
|
|
(index (cl-position current-name persps)))
|
2017-04-12 11:27:31 -04:00
|
|
|
(when (= perspc 1)
|
|
|
|
(user-error "No other workspaces"))
|
2018-04-05 17:05:11 +08:00
|
|
|
(+workspace/switch-to (% (+ index n perspc) perspc))
|
2017-04-12 11:27:31 -04:00
|
|
|
(unless (called-interactively-p 'interactive)
|
|
|
|
(+workspace/display)))
|
|
|
|
('user-error (+workspace-error (cadr ex) t))
|
|
|
|
('error (+workspace-error ex t))))))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-04-08 23:28:06 -04:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/switch-left () (interactive) (+workspace/cycle -1))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/switch-right () (interactive) (+workspace/cycle +1))
|
|
|
|
|
2017-02-04 03:21:04 -05:00
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace/close-window-or-workspace ()
|
2018-01-20 02:44:12 -05:00
|
|
|
"Close the selected window. If it's the last window in the workspace, either
|
|
|
|
close the workspace (as well as its associated frame, if one exists) and move to
|
|
|
|
the next."
|
2017-02-04 03:21:04 -05:00
|
|
|
(interactive)
|
2018-01-03 03:44:32 -05:00
|
|
|
(let ((delete-window-fn (if (featurep 'evil) #'evil-window-delete #'delete-window)))
|
2018-01-03 20:10:00 -05:00
|
|
|
(if (window-dedicated-p)
|
2018-01-03 03:44:32 -05:00
|
|
|
(funcall delete-window-fn)
|
|
|
|
(let ((current-persp-name (+workspace-current-name)))
|
|
|
|
(cond ((or (+workspace--protected-p current-persp-name)
|
|
|
|
(cdr (doom-visible-windows)))
|
|
|
|
(funcall delete-window-fn))
|
2018-01-20 02:44:12 -05:00
|
|
|
|
2018-01-03 03:44:32 -05:00
|
|
|
((cdr (+workspace-list-names))
|
2018-01-20 02:44:12 -05:00
|
|
|
(let ((frame-persp (frame-parameter nil 'workspace)))
|
|
|
|
(if (string= frame-persp (+workspace-current-name))
|
|
|
|
(delete-frame)
|
|
|
|
(+workspace/delete current-persp-name))))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2019-04-24 18:27:40 -04:00
|
|
|
((+workspace-error "Can't delete last workspace" t)))))))
|
2017-09-29 01:50:37 +02:00
|
|
|
|
2020-02-06 15:17:29 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/swap-left (&optional count)
|
|
|
|
"Swap the current workspace with the COUNTth workspace on its left."
|
|
|
|
(interactive "p")
|
|
|
|
(let* ((current-name (+workspace-current-name))
|
|
|
|
(count (or count 1))
|
|
|
|
(index (- (cl-position current-name persp-names-cache :test #'equal)
|
|
|
|
count))
|
|
|
|
(names (remove current-name persp-names-cache)))
|
|
|
|
(unless names
|
|
|
|
(user-error "Only one workspace"))
|
|
|
|
(let ((index (min (max 0 index) (length names))))
|
|
|
|
(setq persp-names-cache
|
|
|
|
(append (cl-subseq names 0 index)
|
|
|
|
(list current-name)
|
|
|
|
(cl-subseq names index))))
|
|
|
|
(when (called-interactively-p 'any)
|
|
|
|
(+workspace/display))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/swap-right (&optional count)
|
|
|
|
"Swap the current workspace with the COUNTth workspace on its right."
|
|
|
|
(interactive "p")
|
|
|
|
(funcall-interactively #'+workspace/swap-left (- count)))
|
|
|
|
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
;;
|
2019-05-21 00:34:32 -04:00
|
|
|
;;; Tabs display in minibuffer
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
(defun +workspace--tabline (&optional names)
|
2017-06-27 23:22:27 +02:00
|
|
|
(let ((names (or names (+workspace-list-names)))
|
2017-06-08 11:47:56 +02:00
|
|
|
(current-name (+workspace-current-name)))
|
2017-02-19 18:40:39 -05:00
|
|
|
(mapconcat
|
2017-04-17 02:17:10 -04:00
|
|
|
#'identity
|
2017-06-08 11:47:56 +02:00
|
|
|
(cl-loop for name in names
|
|
|
|
for i to (length names)
|
|
|
|
collect
|
2017-11-07 14:47:15 +01:00
|
|
|
(propertize (format " [%d] %s " (1+ i) name)
|
2017-06-08 11:47:56 +02:00
|
|
|
'face (if (equal current-name name)
|
|
|
|
'+workspace-tab-selected-face
|
|
|
|
'+workspace-tab-face)))
|
2017-02-19 18:40:39 -05:00
|
|
|
" ")))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2018-01-20 02:44:12 -05:00
|
|
|
(defun +workspace--message-body (message &optional type)
|
2017-02-08 01:54:24 -05:00
|
|
|
(concat (+workspace--tabline)
|
2017-02-19 18:40:39 -05:00
|
|
|
(propertize " | " 'face 'font-lock-comment-face)
|
|
|
|
(propertize (format "%s" message)
|
|
|
|
'face (pcase type
|
|
|
|
('error 'error)
|
|
|
|
('warn 'warning)
|
|
|
|
('success 'success)
|
|
|
|
('info 'font-lock-comment-face)))))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace-message (message &optional type)
|
2017-02-21 03:45:24 -05:00
|
|
|
"Show an 'elegant' message in the echo area next to a listing of workspaces."
|
2017-02-08 01:54:24 -05:00
|
|
|
(message "%s" (+workspace--message-body message type)))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace-error (message &optional noerror)
|
2017-02-21 03:45:24 -05:00
|
|
|
"Show an 'elegant' error in the echo area next to a listing of workspaces."
|
2018-01-20 02:44:12 -05:00
|
|
|
(funcall (if noerror #'message #'error)
|
|
|
|
"%s" (+workspace--message-body message 'error)))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/display ()
|
2017-02-21 03:45:24 -05:00
|
|
|
"Display a list of workspaces (like tabs) in the echo area."
|
2017-02-04 03:21:04 -05:00
|
|
|
(interactive)
|
2018-02-02 04:25:39 -05:00
|
|
|
(let (message-log-max)
|
2019-06-01 00:18:08 -04:00
|
|
|
(message "%s" (+workspace--tabline))))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2018-01-03 13:24:11 -05:00
|
|
|
|
|
|
|
;;
|
2019-05-21 00:34:32 -04:00
|
|
|
;;; Hooks
|
2017-06-28 15:16:30 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2019-07-22 00:52:05 +02:00
|
|
|
(defun +workspaces-delete-associated-workspace-h (&optional frame)
|
2018-01-20 02:44:12 -05:00
|
|
|
"Delete workspace associated with current frame.
|
|
|
|
A workspace gets associated with a frame when a new frame is interactively
|
|
|
|
created."
|
2021-07-09 18:01:02 -04:00
|
|
|
(when (and persp-mode (not (bound-and-true-p with-editor-mode)))
|
2018-01-31 01:13:56 -05:00
|
|
|
(unless frame
|
|
|
|
(setq frame (selected-frame)))
|
2018-01-20 02:44:12 -05:00
|
|
|
(let ((frame-persp (frame-parameter frame 'workspace)))
|
|
|
|
(when (string= frame-persp (+workspace-current-name))
|
2017-06-28 15:16:30 +02:00
|
|
|
(+workspace/delete frame-persp)))))
|
|
|
|
|
2018-01-03 13:24:11 -05:00
|
|
|
;;;###autoload
|
2019-07-22 00:52:05 +02:00
|
|
|
(defun +workspaces-associate-frame-fn (frame &optional _new-frame-p)
|
2018-01-20 02:44:12 -05:00
|
|
|
"Create a blank, new perspective and associate it with FRAME."
|
|
|
|
(when persp-mode
|
2018-06-30 02:21:57 +02:00
|
|
|
(if (not (persp-frame-list-without-daemon))
|
|
|
|
(+workspace-switch +workspaces-main t)
|
|
|
|
(with-selected-frame frame
|
2018-01-31 01:13:56 -05:00
|
|
|
(+workspace-switch (format "#%s" (+workspace--generate-id)) t)
|
2018-06-30 01:55:07 +02:00
|
|
|
(unless (doom-real-buffer-p (current-buffer))
|
2018-02-04 02:21:35 -05:00
|
|
|
(switch-to-buffer (doom-fallback-buffer)))
|
|
|
|
(set-frame-parameter frame 'workspace (+workspace-current-name))
|
|
|
|
;; ensure every buffer has a buffer-predicate
|
|
|
|
(persp-set-frame-buffer-predicate frame))
|
2018-01-31 01:13:56 -05:00
|
|
|
(run-at-time 0.1 nil #'+workspace/display))))
|
2018-01-20 02:44:12 -05:00
|
|
|
|
|
|
|
(defvar +workspaces--project-dir nil)
|
|
|
|
;;;###autoload
|
2019-07-22 00:52:05 +02:00
|
|
|
(defun +workspaces-set-project-action-fn ()
|
2018-01-20 02:44:12 -05:00
|
|
|
"A `projectile-switch-project-action' that sets the project directory for
|
2019-07-22 00:52:05 +02:00
|
|
|
`+workspaces-switch-to-project-h'."
|
2018-01-20 02:44:12 -05:00
|
|
|
(setq +workspaces--project-dir default-directory))
|
|
|
|
|
2018-01-28 03:34:18 -05:00
|
|
|
;;;###autoload
|
2019-07-22 00:52:05 +02:00
|
|
|
(defun +workspaces-switch-to-project-h (&optional dir)
|
2018-01-31 04:54:42 -05:00
|
|
|
"Creates a workspace dedicated to a new project. If one already exists, switch
|
2018-02-06 17:49:51 -05:00
|
|
|
to it. If in the main workspace and it's empty, recycle that workspace, without
|
|
|
|
renaming it.
|
|
|
|
|
2018-06-19 14:59:41 +02:00
|
|
|
Afterwords, runs `+workspaces-switch-project-function'. By default, this prompts
|
|
|
|
the user to open a file in the new project.
|
|
|
|
|
|
|
|
This be hooked to `projectile-after-switch-project-hook'."
|
2018-02-20 17:56:38 -05:00
|
|
|
(when dir
|
|
|
|
(setq +workspaces--project-dir dir))
|
2020-06-04 19:15:25 -04:00
|
|
|
;; HACK Clear projectile-project-root, otherwise cached roots may interfere
|
|
|
|
;; with project switch (see #3166)
|
|
|
|
(let (projectile-project-root)
|
|
|
|
(when (and persp-mode +workspaces--project-dir)
|
|
|
|
(when projectile-before-switch-project-hook
|
|
|
|
(with-temp-buffer
|
|
|
|
;; Load the project dir-local variables into the switch buffer, so the
|
|
|
|
;; action can make use of them
|
|
|
|
(setq default-directory +workspaces--project-dir)
|
|
|
|
(hack-dir-local-variables-non-file-buffer)
|
|
|
|
(run-hooks 'projectile-before-switch-project-hook)))
|
|
|
|
(unwind-protect
|
|
|
|
(if (and (not (null +workspaces-on-switch-project-behavior))
|
|
|
|
(or (eq +workspaces-on-switch-project-behavior t)
|
|
|
|
(equal (safe-persp-name (get-current-persp)) persp-nil-name)
|
|
|
|
(+workspace-buffer-list)))
|
|
|
|
(let* ((persp
|
|
|
|
(let ((project-name (doom-project-name +workspaces--project-dir)))
|
|
|
|
(or (+workspace-get project-name t)
|
|
|
|
(+workspace-new project-name))))
|
|
|
|
(new-name (persp-name persp)))
|
|
|
|
(+workspace-switch new-name)
|
|
|
|
(with-current-buffer (doom-fallback-buffer)
|
|
|
|
(setq default-directory +workspaces--project-dir)
|
|
|
|
(hack-dir-local-variables-non-file-buffer))
|
|
|
|
(unless current-prefix-arg
|
|
|
|
(funcall +workspaces-switch-project-function +workspaces--project-dir))
|
|
|
|
(+workspace-message
|
|
|
|
(format "Switched to '%s' in new workspace" new-name)
|
|
|
|
'success))
|
|
|
|
(with-current-buffer (doom-fallback-buffer)
|
|
|
|
(setq default-directory +workspaces--project-dir)
|
|
|
|
(hack-dir-local-variables-non-file-buffer)
|
|
|
|
(message "Switched to '%s'" (doom-project-name +workspaces--project-dir)))
|
|
|
|
(with-demoted-errors "Workspace error: %s"
|
|
|
|
(+workspace-rename (+workspace-current-name) (doom-project-name +workspaces--project-dir)))
|
|
|
|
(unless current-prefix-arg
|
|
|
|
(funcall +workspaces-switch-project-function +workspaces--project-dir)))
|
|
|
|
(run-hooks 'projectile-after-switch-project-hook)
|
|
|
|
(setq +workspaces--project-dir nil)))))
|
2018-01-03 13:24:11 -05:00
|
|
|
|
2022-08-31 19:59:03 -04:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspaces-save-tab-bar-data-h (_)
|
|
|
|
"Save the current workspace's tab bar data."
|
|
|
|
(when (get-current-persp)
|
|
|
|
(set-persp-parameter
|
|
|
|
'tab-bar-tabs (tab-bar-tabs))
|
|
|
|
(set-persp-parameter 'tab-bar-closed-tabs tab-bar-closed-tabs)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspaces-load-tab-bar-data-h (_)
|
|
|
|
"Restores the tab bar data of the workspace we have just switched to."
|
|
|
|
(tab-bar-tabs-set (persp-parameter 'tab-bar-tabs))
|
|
|
|
(setq tab-bar-closed-tabs (persp-parameter 'tab-bar-closed-tabs))
|
|
|
|
(tab-bar--update-tab-bar-lines t))
|
2018-01-03 13:24:11 -05:00
|
|
|
|
|
|
|
;;
|
2019-05-21 00:34:32 -04:00
|
|
|
;;; Advice
|
2018-01-03 13:24:11 -05:00
|
|
|
|
2017-06-28 15:16:30 +02:00
|
|
|
;;;###autoload
|
2021-08-04 01:18:06 -04:00
|
|
|
(defun +workspaces-autosave-real-buffers-a (fn &rest args)
|
2018-01-20 02:44:12 -05:00
|
|
|
"Don't autosave if no real buffers are open."
|
|
|
|
(when (doom-real-buffer-list)
|
2021-08-04 01:18:06 -04:00
|
|
|
(apply fn args))
|
2018-01-20 02:44:12 -05:00
|
|
|
t)
|