Minor refactor of feature/workspaces

Improve error handling and update docstrings+comments
This commit is contained in:
Henrik Lissner 2018-06-19 14:59:41 +02:00
parent d979c46ea2
commit d5adf4ccbc
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 17 additions and 13 deletions

View file

@ -38,7 +38,7 @@
;;;###autoload ;;;###autoload
(defun +workspace-exists-p (name) (defun +workspace-exists-p (name)
"Returns t if NAME is the name of an existing workspace." "Returns t if NAME is the name of an existing workspace."
(cl-assert (stringp name) t) (cl-check-type name string)
(member name (+workspace-list-names))) (member name (+workspace-list-names)))
;;;###autoload ;;;###autoload
@ -57,6 +57,7 @@
(defun +workspace-get (name &optional noerror) (defun +workspace-get (name &optional noerror)
"Return a workspace named NAME. Unless NOERROR is non-nil, this throws an "Return a workspace named NAME. Unless NOERROR is non-nil, this throws an
error if NAME doesn't exist." error if NAME doesn't exist."
(cl-check-type name string)
(when-let* ((persp (persp-get-by-name name))) (when-let* ((persp (persp-get-by-name name)))
(cond ((+workspace-p persp) persp) (cond ((+workspace-p persp) persp)
((not noerror) ((not noerror)
@ -86,13 +87,12 @@ The buffer list is ordered by recency (same as `buffer-list').
PERSP can be a string (name of a workspace) or a workspace (satisfies 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." `+workspace-p'). If nil or omitted, it defaults to the current workspace."
(unless persp (let ((persp (or persp (+workspace-current))))
(setq persp (+workspace-current))) (unless (+workspace-p persp)
(unless (+workspace-p persp) (user-error "Not in a valid workspace (%s)" persp))
(error "You're in the nil perspective")) (cl-loop for buf in (buffer-list)
(cl-loop for buf in (buffer-list) if (+workspace-contains-buffer-p buf persp)
if (+workspace-contains-buffer-p buf persp) collect buf)))
collect buf))
;;;###autoload ;;;###autoload
(defun +workspace-orphaned-buffer-list () (defun +workspace-orphaned-buffer-list ()
@ -109,7 +109,7 @@ retrieve perspectives that were explicitly saved with `+workspace-save'.
Returns t if successful, nil otherwise." Returns t if successful, nil otherwise."
(when (+workspace-exists-p name) (when (+workspace-exists-p name)
(error "A workspace named '%s' already exists." name)) (user-error "A workspace named '%s' already exists." name))
(persp-load-from-file-by-names (persp-load-from-file-by-names
(expand-file-name +workspaces-data-file persp-save-dir) (expand-file-name +workspaces-data-file persp-save-dir)
*persp-hash* (list name)) *persp-hash* (list name))
@ -353,7 +353,7 @@ end of the workspace list."
(when (and (stringp index) (when (and (stringp index)
(string-match-p "^[0-9]+$" index)) (string-match-p "^[0-9]+$" index))
(setq index (string-to-number index))) (setq index (string-to-number index)))
(condition-case ex (condition-case-unless-debug ex
(let ((names (+workspace-list-names)) (let ((names (+workspace-list-names))
(old-name (+workspace-current-name))) (old-name (+workspace-current-name)))
(cond ((numberp index) (cond ((numberp index)
@ -544,7 +544,10 @@ created."
to it. If in the main workspace and it's empty, recycle that workspace, without to it. If in the main workspace and it's empty, recycle that workspace, without
renaming it. renaming it.
Should be hooked to `projectile-after-switch-project-hook'." 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'."
(when dir (when dir
(setq +workspaces--project-dir dir)) (setq +workspaces--project-dir dir))
(when (and persp-mode +workspaces--project-dir) (when (and persp-mode +workspaces--project-dir)

View file

@ -26,10 +26,11 @@ new project directory.")
"The basename of the file to store single workspace perspectives. Will be "The basename of the file to store single workspace perspectives. Will be
stored in `persp-save-dir'.") stored in `persp-save-dir'.")
;; If emacs is passed --restore, restore the last session on startup. This is
;; particularly useful for the `+workspace/restart-emacs-then-restore' command.
(defun +workspaces-restore-last-session (&rest _) (defun +workspaces-restore-last-session (&rest _)
(add-hook 'doom-post-init-hook #'+workspace/load-session 'append)) (add-hook 'doom-post-init-hook #'+workspace/load-session 'append))
(map-put command-switch-alist '"--restore" #'+workspaces-restore-last-session) (map-put command-switch-alist "--restore" #'+workspaces-restore-last-session)
;; ;;
;; Plugins ;; Plugins