From d5adf4ccbc70be9b30d33f3cef10ffaaf92fbc4f Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 19 Jun 2018 14:59:41 +0200 Subject: [PATCH] Minor refactor of feature/workspaces Improve error handling and update docstrings+comments --- .../feature/workspaces/autoload/workspaces.el | 25 +++++++++++-------- modules/feature/workspaces/config.el | 5 ++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/modules/feature/workspaces/autoload/workspaces.el b/modules/feature/workspaces/autoload/workspaces.el index 3994c5517..e249f60e4 100644 --- a/modules/feature/workspaces/autoload/workspaces.el +++ b/modules/feature/workspaces/autoload/workspaces.el @@ -38,7 +38,7 @@ ;;;###autoload (defun +workspace-exists-p (name) "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))) ;;;###autoload @@ -57,6 +57,7 @@ (defun +workspace-get (name &optional noerror) "Return a workspace named NAME. Unless NOERROR is non-nil, this throws an error if NAME doesn't exist." + (cl-check-type name string) (when-let* ((persp (persp-get-by-name name))) (cond ((+workspace-p persp) persp) ((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 `+workspace-p'). If nil or omitted, it defaults to the current workspace." - (unless persp - (setq persp (+workspace-current))) - (unless (+workspace-p persp) - (error "You're in the nil perspective")) - (cl-loop for buf in (buffer-list) - if (+workspace-contains-buffer-p buf persp) - collect buf)) + (let ((persp (or persp (+workspace-current)))) + (unless (+workspace-p persp) + (user-error "Not in a valid workspace (%s)" persp)) + (cl-loop for buf in (buffer-list) + if (+workspace-contains-buffer-p buf persp) + collect buf))) ;;;###autoload (defun +workspace-orphaned-buffer-list () @@ -109,7 +109,7 @@ retrieve perspectives that were explicitly saved with `+workspace-save'. Returns t if successful, nil otherwise." (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 (expand-file-name +workspaces-data-file persp-save-dir) *persp-hash* (list name)) @@ -353,7 +353,7 @@ end of the workspace list." (when (and (stringp index) (string-match-p "^[0-9]+$" index)) (setq index (string-to-number index))) - (condition-case ex + (condition-case-unless-debug ex (let ((names (+workspace-list-names)) (old-name (+workspace-current-name))) (cond ((numberp index) @@ -544,7 +544,10 @@ created." to it. If in the main workspace and it's empty, recycle that workspace, without 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 (setq +workspaces--project-dir dir)) (when (and persp-mode +workspaces--project-dir) diff --git a/modules/feature/workspaces/config.el b/modules/feature/workspaces/config.el index 97d37de1e..bbc80b14d 100644 --- a/modules/feature/workspaces/config.el +++ b/modules/feature/workspaces/config.el @@ -26,10 +26,11 @@ new project directory.") "The basename of the file to store single workspace perspectives. Will be 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 _) (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