2017-06-08 11:47:56 +02:00
|
|
|
;;; feature/workspaces/autoload/workspaces.el -*- lexical-binding: t; -*-
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-09-29 01:50:37 +02:00
|
|
|
(defvar +workspace-data-file "_workspaces"
|
2017-02-08 01:54:24 -05:00
|
|
|
"The file basename in which to store single workspace perspectives.")
|
|
|
|
|
2018-01-03 14:22:27 -05:00
|
|
|
(defvar +workspace-change-hook ()
|
|
|
|
"Hooks run when workspaces are added, removed, renamed or switched to.")
|
|
|
|
|
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
|
|
|
|
2017-09-29 01:50:37 +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'"
|
|
|
|
:group 'doom)
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2017-09-29 01:50:37 +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'"
|
|
|
|
:group 'doom)
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2017-06-27 23:22:27 +02:00
|
|
|
|
2017-09-29 01:50:37 +02: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
|
|
|
|
2017-09-29 01:50:37 +02:00
|
|
|
;; --- Predicates -------------------------
|
2017-06-27 23:22:27 +02:00
|
|
|
|
2017-02-04 03:21:04 -05:00
|
|
|
;;;###autoload
|
2017-09-29 01:50:37 +02:00
|
|
|
(defalias #'+workspace-p #'persp-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-09-29 01:50:37 +02:00
|
|
|
(cl-assert (stringp name) t)
|
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
|
|
|
|
(defun +workspace-contains-buffer-p (buffer &optional workspace)
|
|
|
|
"Return non-nil if buffer is in workspace (defaults to current workspace)."
|
|
|
|
(persp-contain-buffer-p buffer (or workspace (+workspace-current)) nil))
|
|
|
|
|
|
|
|
|
|
|
|
;; --- Getters ----------------------------
|
|
|
|
|
2017-02-04 03:21:04 -05:00
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace-get (name &optional noerror)
|
|
|
|
"Returns a workspace (perspective hash table) named NAME."
|
2017-12-10 14:49:52 -05:00
|
|
|
(when-let* ((persp (persp-get-by-name name)))
|
2017-09-29 01:50:37 +02:00
|
|
|
(cond ((+workspace-p persp) persp)
|
|
|
|
((not noerror) (error "'%s' is an invalid workspace" name)))))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-07-08 13:47:17 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defalias '+workspace-current #'get-current-persp)
|
|
|
|
|
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."
|
2017-02-08 01:54:24 -05:00
|
|
|
(safe-persp-name (get-current-persp)))
|
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 ()
|
|
|
|
"Return a list of workspace structs."
|
|
|
|
(mapcar #'persp-get-by-name (+workspace-list-names)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace-list-names ()
|
|
|
|
"Return a list of workspace names (strings)."
|
|
|
|
(delete persp-nil-name (persp-names-current-frame-fast-ordered)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace-buffer-list (&optional persp)
|
|
|
|
"Return a list of buffers in PERSP (defaults to the current perspective).
|
|
|
|
|
|
|
|
The buffer list is ordered by recency (same as `buffer-list').
|
|
|
|
|
|
|
|
PERSP can be a string (name of a workspace) or a perspective hash (satisfies
|
|
|
|
`+workspace-p').
|
|
|
|
|
|
|
|
If PERSP is t, then return a list of orphaned buffers associated with no
|
|
|
|
perspectives."
|
|
|
|
(let ((persp (or persp (+workspace-current))))
|
|
|
|
(if (eq persp t)
|
|
|
|
(cl-remove-if #'persp--buffer-in-persps (buffer-list))
|
|
|
|
(cl-assert (+workspace-p persp) t)
|
|
|
|
(cl-loop for buf in (buffer-list)
|
|
|
|
if (+workspace-contains-buffer-p buf persp)
|
|
|
|
collect buf))))
|
|
|
|
|
|
|
|
|
|
|
|
;; --- Actions ----------------------------
|
2017-07-08 21:08:42 +02:00
|
|
|
|
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)
|
|
|
|
(error "A workspace named '%s' already exists." name))
|
|
|
|
(persp-load-from-file-by-names
|
|
|
|
(expand-file-name +workspace-data-file persp-save-dir)
|
|
|
|
*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-load-session (&optional name)
|
|
|
|
"Replace current session with the entire session named NAME. If NAME is nil,
|
|
|
|
use `persp-auto-save-fname'."
|
2017-09-29 01:50:37 +02:00
|
|
|
(persp-load-state-from-file
|
|
|
|
(expand-file-name (or name persp-auto-save-fname) persp-save-dir)))
|
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))
|
|
|
|
(let ((fname (expand-file-name +workspace-data-file persp-save-dir)))
|
|
|
|
(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
|
|
|
|
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace-save-session (&optional name)
|
|
|
|
"Save a whole session as NAME. If NAME is nil, use `persp-auto-save-fname'.
|
|
|
|
Return t on success, nil otherwise."
|
2017-09-29 01:50:37 +02:00
|
|
|
(let ((fname (expand-file-name (or name persp-auto-save-fname)
|
|
|
|
persp-save-dir))
|
|
|
|
(persp-auto-save-opt
|
|
|
|
(if (or (not name)
|
|
|
|
(equal name persp-auto-save-fname))
|
|
|
|
0
|
|
|
|
persp-auto-save-opt)))
|
|
|
|
(and (persp-save-state-to-file fname) t)))
|
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-01-03 14:22:27 -05:00
|
|
|
(when (persp-add-new name)
|
|
|
|
(run-hooks '+workspace-change-hook)
|
|
|
|
t))
|
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-03 14:22:27 -05:00
|
|
|
(when (persp-rename new-name (+workspace-get name))
|
|
|
|
(run-hooks '+workspace-change-hook)
|
|
|
|
name))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-03-06 19:23:44 -05:00
|
|
|
(defun +workspace-delete (name &optional inhibit-kill-p)
|
2017-09-29 01:50:37 +02:00
|
|
|
"Delete the workspace denoted by NAME, which can be the name of a perspective
|
|
|
|
or its hash table. If INHIBIT-KILL-P is non-nil, don't kill this workspace's
|
|
|
|
buffers."
|
|
|
|
(when (+workspace--protected-p name)
|
2017-04-10 18:21:42 -04:00
|
|
|
(error "Can't delete '%s' workspace" name))
|
2017-09-29 01:50:37 +02:00
|
|
|
(+workspace-get name) ; error checking
|
|
|
|
(persp-kill name inhibit-kill-p)
|
2018-01-03 14:22:27 -05:00
|
|
|
(unless (+workspace-exists-p name)
|
|
|
|
(run-hooks '+workspace-change-hook)
|
|
|
|
t))
|
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)
|
2017-02-08 01:54:24 -05:00
|
|
|
"Switch to another workspace."
|
|
|
|
(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)))
|
|
|
|
(setq +workspace--last
|
|
|
|
(or (and (not (string= old-name persp-nil-name))
|
|
|
|
old-name)
|
|
|
|
+workspaces-main)))
|
2018-01-03 14:22:27 -05:00
|
|
|
(persp-frame-switch name)
|
|
|
|
(when (equal (+workspace-current-name) name)
|
|
|
|
(run-hooks '+workspace-change-hook)
|
|
|
|
t))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2018-01-03 13:24:11 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace-on-new-frame (frame &optional _new-frame-p)
|
|
|
|
"Spawn a perspective for each new frame."
|
|
|
|
(select-frame frame)
|
|
|
|
(+workspace/new)
|
|
|
|
(set-frame-parameter frame 'assoc-persp (+workspace-current-name)))
|
|
|
|
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
;;
|
|
|
|
;; Interactive commands
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;;###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)
|
2017-09-29 01:50:37 +02:00
|
|
|
(completing-read
|
|
|
|
"Workspace to load: "
|
|
|
|
(persp-list-persp-names-in-file
|
|
|
|
(expand-file-name +workspace-data-file persp-save-dir))))))
|
2017-02-08 01:54:24 -05:00
|
|
|
(if (not (+workspace-load name))
|
|
|
|
(+workspace-error (format "Couldn't load workspace %s" name))
|
|
|
|
(+workspace/switch-to name)
|
|
|
|
(+workspace/display)))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-02-22 04:28:20 -05:00
|
|
|
(defun +workspace/load-session (&optional name)
|
2017-02-08 01:54:24 -05:00
|
|
|
"Load a session and switch to it. If called with C-u, try to load the last
|
|
|
|
session."
|
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(unless current-prefix-arg
|
|
|
|
(completing-read
|
|
|
|
"Session to load: "
|
2017-02-22 04:28:20 -05:00
|
|
|
(directory-files persp-save-dir nil "^[^_.]")
|
2017-02-11 00:52:25 -05:00
|
|
|
nil t))))
|
2017-03-13 14:21:42 -04:00
|
|
|
(condition-case ex
|
|
|
|
(let ((name (or name persp-auto-save-fname)))
|
|
|
|
(+workspace-load-session name)
|
|
|
|
(+workspace-message (format "'%s' workspace loaded" name) 'success))
|
|
|
|
'(error (+workspace-error (cadr ex) t))))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
;;;###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)
|
2017-06-27 23:22:27 +02:00
|
|
|
(completing-read "Workspace to save: " (+workspace-list-names)))))
|
2017-02-08 01:54:24 -05:00
|
|
|
(if (+workspace-save name)
|
2017-03-04 00:01:48 -05:00
|
|
|
(+workspace-message (format "'%s' workspace saved" name) 'success)
|
2017-02-08 01:54:24 -05:00
|
|
|
(+workspace-error (format "Couldn't save workspace %s" name))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/save-session (&optional name)
|
|
|
|
"Save the current session. If called with C-u, prompt you for the name to save
|
|
|
|
the session as."
|
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(when current-prefix-arg
|
|
|
|
(completing-read
|
|
|
|
"Save session as: "
|
2017-02-22 04:28:20 -05:00
|
|
|
(directory-files persp-save-dir nil "^[^_.]")))))
|
2017-02-08 01:54:24 -05:00
|
|
|
(condition-case ex
|
2017-03-13 14:21:42 -04:00
|
|
|
(let ((name (or name persp-auto-save-fname)))
|
2017-02-11 00:52:25 -05:00
|
|
|
(if (+workspace-save-session name)
|
2017-03-13 14:21:42 -04:00
|
|
|
(+workspace-message (format "Saved session as '%s'" name) 'success)
|
|
|
|
(error "Couldn't save session as '%s'" name)))
|
2017-02-08 01:54:24 -05:00
|
|
|
'(error (+workspace-error (cadr ex) t))))
|
|
|
|
|
|
|
|
;;;###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: ")))
|
2017-02-08 01:54:24 -05:00
|
|
|
(condition-case ex
|
|
|
|
(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))
|
|
|
|
('error (+workspace-error (cadr 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)
|
2017-02-08 01:54:24 -05:00
|
|
|
nil nil current-name)
|
|
|
|
current-name))))
|
|
|
|
(condition-case ex
|
2017-09-29 01:50:37 +02:00
|
|
|
(+workspace-message
|
|
|
|
(let ((workspaces (length (+workspace-list-names))))
|
|
|
|
(cond ((> workspaces 1)
|
|
|
|
(+workspace-delete name)
|
|
|
|
(+workspace-switch
|
|
|
|
(if (+workspace-exists-p +workspace--last)
|
|
|
|
+workspace--last
|
|
|
|
(car (+workspace-list-names))))
|
|
|
|
(format "Deleted '%s' workspace" name))
|
|
|
|
((= workspaces 1)
|
|
|
|
(format "Can't delete the last workspace!"))
|
|
|
|
(t
|
|
|
|
(+workspace-delete name)
|
|
|
|
(+workspace-switch +workspaces-main t)
|
|
|
|
(switch-to-buffer (doom-fallback-buffer))
|
|
|
|
(format "No workspaces detected! Auto-creating '%s' workspace" +workspaces-main))))
|
|
|
|
'success)
|
2017-02-08 01:54:24 -05:00
|
|
|
('error (+workspace-error (cadr ex) t))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/kill-session ()
|
|
|
|
"Delete the current session, clears all workspaces, windows and buffers."
|
2017-02-04 03:21:04 -05:00
|
|
|
(interactive)
|
2017-09-29 01:50:37 +02:00
|
|
|
(unless (cl-every #'+workspace-delete (+workspace-list-names))
|
2017-02-08 01:54:24 -05:00
|
|
|
(+workspace-error "Could not clear session"))
|
2017-04-10 18:21:42 -04:00
|
|
|
(+workspace-switch +workspaces-main t)
|
2017-02-19 18:40:39 -05:00
|
|
|
(doom/kill-all-buffers)
|
2017-02-20 16:33:52 -05:00
|
|
|
(let ((fallback-buf (doom-fallback-buffer)))
|
|
|
|
(switch-to-buffer fallback-buf)
|
2017-12-30 00:59:44 -05:00
|
|
|
(doom/cleanup-session)))
|
2017-02-19 18:40:39 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/kill-session-and-quit ()
|
|
|
|
"Forgets current session and quits."
|
2017-11-08 22:49:19 +03:00
|
|
|
(interactive)
|
2017-02-19 18:40:39 -05:00
|
|
|
(+workspace/kill-session)
|
|
|
|
(save-buffers-kill-terminal))
|
2017-02-04 03:21:04 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace/new (&optional name clone-p)
|
|
|
|
"Create a new workspace named NAME. If OVERWRITE-P is non-nil, clear any
|
|
|
|
pre-existing workspace."
|
|
|
|
(interactive "iP")
|
|
|
|
(unless name
|
|
|
|
(setq name (format "#%s" (+workspace--generate-id))))
|
|
|
|
(condition-case ex
|
|
|
|
(let ((exists-p (+workspace-exists-p name)))
|
|
|
|
(if exists-p
|
|
|
|
(error "%s already exists" name)
|
2017-04-10 18:21:42 -04:00
|
|
|
(+workspace-switch name t)
|
2017-02-08 01:54:24 -05:00
|
|
|
(if clone-p
|
2017-02-19 18:40:39 -05:00
|
|
|
(dolist (window (window-list))
|
|
|
|
(persp-add-buffer (window-buffer window) persp nil))
|
2017-02-08 01:54:24 -05:00
|
|
|
(delete-other-windows-internal)
|
2017-02-20 16:33:52 -05:00
|
|
|
(switch-to-buffer (doom-fallback-buffer)))
|
2017-02-08 01:54:24 -05:00
|
|
|
(+workspace/display)))
|
|
|
|
('error (+workspace-error (cadr ex) t))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/switch-to (index)
|
2017-02-04 03:21:04 -05:00
|
|
|
"Switch to a workspace at a given INDEX. A negative number will start from the
|
|
|
|
end of the workspace list."
|
2017-02-08 01:54:24 -05:00
|
|
|
(interactive
|
|
|
|
(list (or current-prefix-arg
|
2017-06-27 23:22:27 +02:00
|
|
|
(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)))
|
|
|
|
(condition-case 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)
|
|
|
|
(unless (member index names)
|
|
|
|
(error "No workspace named %s" index))
|
2017-04-10 18:21:42 -04:00
|
|
|
(+workspace-switch index))
|
|
|
|
(t
|
|
|
|
(error "Not a valid index: %s" index)))
|
2017-02-08 01:54:24 -05:00
|
|
|
(unless (called-interactively-p 'interactive)
|
2017-03-01 21:41:16 -05:00
|
|
|
(if (equal (+workspace-current-name) old-name)
|
|
|
|
(+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
|
2017-02-08 01:54:24 -05:00
|
|
|
(defun +workspace/switch-to-last ()
|
|
|
|
"Switch to the last workspace."
|
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
|
|
|
|
|
|
|
;;;###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)
|
|
|
|
(condition-case 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"))
|
|
|
|
(+workspace/switch-to (% (+ index n) perspc))
|
|
|
|
(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 ()
|
|
|
|
"Close the selected window. If it's the last window in the workspace, close
|
|
|
|
the workspace 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))
|
|
|
|
((cdr (+workspace-list-names))
|
|
|
|
(+workspace/delete current-persp-name)))))))
|
2017-02-08 01:54:24 -05:00
|
|
|
|
2017-09-29 01:50:37 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspace/close-workspace-or-frame ()
|
|
|
|
"Close the current workspace. If it's the last, delete the frame instead."
|
|
|
|
(interactive)
|
|
|
|
(let ((frames (length (frame-list)))
|
|
|
|
(workspaces (length (+workspace-list-names))))
|
|
|
|
(cond ((> workspaces 1)
|
|
|
|
(call-interactively #'+workspace/delete))
|
|
|
|
((> frames 1)
|
|
|
|
(call-interactively #'delete-frame))
|
|
|
|
(t
|
|
|
|
(error "Can't delete last frame.")))))
|
|
|
|
|
2017-02-08 01:54:24 -05:00
|
|
|
|
|
|
|
;;
|
|
|
|
;; Tabs display in minibuffer
|
|
|
|
;;
|
|
|
|
|
|
|
|
(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
|
|
|
|
|
|
|
(defun +workspace--message-body (message &optional type)
|
|
|
|
(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."
|
2017-04-17 02:17:10 -04: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)
|
2017-02-08 01:54:24 -05:00
|
|
|
(message "%s" (+workspace--tabline)))
|
|
|
|
|
2018-01-03 13:24:11 -05:00
|
|
|
|
|
|
|
;;
|
|
|
|
;; Hooks
|
|
|
|
;;
|
2017-06-28 15:16:30 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspaces|delete-associated-workspace-maybe (frame)
|
|
|
|
"Delete workspace associated with current frame IF it has no real buffers."
|
|
|
|
(when persp-mode
|
|
|
|
(let ((frame-persp (frame-parameter frame 'assoc-persp)))
|
|
|
|
(when (and (equal frame-persp (+workspace-current-name))
|
|
|
|
(not (equal frame-persp +workspaces-main)))
|
|
|
|
(+workspace/delete frame-persp)))))
|
|
|
|
|
2018-01-03 13:24:11 -05:00
|
|
|
;;;###autoload
|
2018-01-06 04:24:37 -05:00
|
|
|
(defun +workspaces|per-project ()
|
2018-01-03 13:24:11 -05:00
|
|
|
"Open a new workspace when switching to another project.
|
|
|
|
|
|
|
|
Ensures the scratch (or dashboard) buffers are CDed into the project's root."
|
|
|
|
(when persp-mode
|
|
|
|
(let ((cwd default-directory))
|
|
|
|
(+workspace-switch (projectile-project-name) t)
|
|
|
|
(switch-to-buffer (doom-fallback-buffer))
|
|
|
|
(setq default-directory cwd)
|
|
|
|
(+workspace-message
|
|
|
|
(format "Switched to '%s' in new workspace" (+workspace-current-name))
|
|
|
|
'success))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspaces|cleanup-unassociated-buffers ()
|
|
|
|
(cl-loop for buf in (buffer-list)
|
|
|
|
unless (persp--buffer-in-persps buf)
|
|
|
|
if (kill-buffer buf)
|
2018-01-04 03:55:25 -05:00
|
|
|
sum 1))
|
2018-01-03 13:24:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; Advice
|
|
|
|
;;
|
|
|
|
|
2017-06-28 15:16:30 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun +workspaces*autosave-real-buffers (orig-fn &rest args)
|
|
|
|
"Don't autosave if no real buffers are open."
|
|
|
|
(when (doom-real-buffer-list)
|
|
|
|
(apply orig-fn args))
|
|
|
|
t)
|
2017-08-06 18:38:19 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +workspaces*switch-project-by-name (orig-fn &rest args)
|
|
|
|
"Switch to a project and prompt for a file to open.
|
|
|
|
|
|
|
|
Ensures the scratch (or dashboard) buffers are CDed into the project's root."
|
|
|
|
(when persp-mode
|
|
|
|
(+workspace-switch (car args) t)
|
|
|
|
(with-current-buffer (switch-to-buffer (doom-fallback-buffer))
|
|
|
|
(setq default-directory (car args))))
|
|
|
|
(apply orig-fn args))
|
|
|
|
|