Update modules/feature/workspaces

This commit is contained in:
Henrik Lissner 2017-02-19 18:40:39 -05:00
parent 2f75a81716
commit 43d0247d61
3 changed files with 117 additions and 125 deletions

View file

@ -0,0 +1,48 @@
;;; feature/workspaces/autoload/evil.el
;;;###autoload (autoload '+workspace:save-session "feature/workspaces/autoload/evil" nil t)
(evil-define-command +workspace:save-session (&optional name)
"Ex wrapper around `+workspace/save-session'."
(interactive "<a>") (+workspace/save-session name))
;;;###autoload (autoload '+workspace:load-session "feature/workspaces/autoload/evil" nil t)
(evil-define-command +workspace:load-session (&optional name)
"Ex wrapper around `+workspace/load-session'."
(interactive "<a>") (+workspace/load-session name))
;;;###autoload (autoload '+workspace:save "feature/workspaces/autoload/evil" nil t)
(evil-define-command +workspace:save (&optional name)
"Ex wrapper around `+workspace/save-session'."
(interactive "<a>") (+workspace/save name))
;;;###autoload (autoload '+workspace:load "feature/workspaces/autoload/evil" nil t)
(evil-define-command +workspace:load (&optional name)
"Ex wrapper around `+workspace/load-session'."
(interactive "<a>") (+workspace/load name))
;;;###autoload (autoload '+workspace:new "feature/workspaces/autoload/evil" nil t)
(evil-define-command +workspace:new (bang name)
"Ex wrapper around `+workspace/new'. If BANG, clone the current workspace."
(interactive "<!><a>") (+workspace/new name bang))
;;;###autoload (autoload '+workspace:rename "feature/workspaces/autoload/evil" nil t)
(evil-define-command +workspace:rename (new-name)
"Ex wrapper around `+workspace/rename'."
(interactive "<a>") (+workspace/rename new-name))
;;;###autoload (autoload '+workspace:delete "feature/workspaces/autoload/evil" nil t)
(evil-define-command +workspace:delete ()
"Ex wrapper around `+workspace/delete'."
(interactive "<a>") (+workspace/delete (+workspace-current-name)))
;;;###autoload (autoload '+workspace:switch-next "feature/workspaces/autoload/evil" nil t)
(evil-define-command +workspace:switch-next (&optional count)
"Switch to next workspace. If COUNT, switch to COUNT-th workspace."
(interactive "<c>")
(if count (+workspace/switch-to count) (+workspace/cycle +1)))
;;;###autoload (autoload '+workspace:switch-previous "feature/workspaces/autoload/evil" nil t)
(evil-define-command +workspace:switch-previous (&optional count)
"Switch to previous workspace. If COUNT, switch to COUNT-th workspace."
(interactive "<c>")
(if count (+workspace/switch-to count) (+workspace/cycle -1)))

View file

@ -1,12 +1,8 @@
;;; feature/workspaces/autoload.el ;;; feature/workspaces/autoload/workspaces.el
(defvar +workspace-workspace-file "_workspaces" (defvar +workspace-workspace-file "_workspaces"
"The file basename in which to store single workspace perspectives.") "The file basename in which to store single workspace perspectives.")
(defvar +workspace--counter 1
"")
(add-to-list 'savehist-additional-variables '+workspace-names)
(defface +workspace-tab-selected-face (defface +workspace-tab-selected-face
'((((class color) (background light)) '((((class color) (background light))
(:background "#333333" :foreground "#000000")) ;; FIXME (:background "#333333" :foreground "#000000")) ;; FIXME
@ -131,11 +127,11 @@ perspective or its hash table."
(persp-frame-switch name)) (persp-frame-switch name))
(defun +workspace--generate-id () (defun +workspace--generate-id ()
(let ((numbers (--map (string-to-number (substring it 1)) (let ((numbers (mapcar (lambda (it) (string-to-number (substring it 1)))
(--filter (string-match-p "^#[0-9]+$" it) (cl-remove-if-not (lambda (it) (string-match-p "^#[0-9]+$" it))
(+workspace-list))))) (+workspace-list)))))
(if numbers (if numbers
(1+ (-max numbers)) (1+ (car (sort numbers (lambda (it other) (> it other)))))
1))) 1)))
@ -168,9 +164,7 @@ session."
(unless current-prefix-arg (unless current-prefix-arg
(completing-read (completing-read
"Session to load: " "Session to load: "
(-map 'f-filename (directory-files persp-save-dir nil "^[^_]")
(f--files persp-save-dir
(not (string-prefix-p "_" (f-filename it)))))
nil t)))) nil t))))
(+workspace-load-session name) (+workspace-load-session name)
(+workspace/display)) (+workspace/display))
@ -197,9 +191,7 @@ the session as."
(when current-prefix-arg (when current-prefix-arg
(completing-read (completing-read
"Save session as: " "Save session as: "
(-map 'f-filename (directory-files persp-save-dir nil "^[^_]")))))
(f--files persp-save-dir
(not (string-prefix-p "_" (f-filename it)))))))))
(condition-case ex (condition-case ex
(let ((name (or name (+workspace-current-name)))) (let ((name (or name (+workspace-current-name))))
(if (+workspace-save-session name) (if (+workspace-save-session name)
@ -241,14 +233,21 @@ workspace to delete."
(defun +workspace/kill-session () (defun +workspace/kill-session ()
"Delete the current session, clears all workspaces, windows and buffers." "Delete the current session, clears all workspaces, windows and buffers."
(interactive) (interactive)
(unless (-all-p '+workspace-delete (+workspace-list t)) (unless (cl-every '+workspace-delete (+workspace-list t))
(+workspace-error "Could not clear session")) (+workspace-error "Could not clear session"))
(+workspace-switch persp-nil-name) (+workspace-switch persp-nil-name)
(delete-other-windows-internal) (doom/kill-all-buffers)
(switch-to-buffer doom-fallback-buffer) (switch-to-buffer doom-fallback-buffer)
(--each (buffer-list) (dolist (buf (buffer-list))
(unless (eq (buffer-name it) doom-fallback-buffer) (unless (eq (buffer-name buf) doom-fallback-buffer)
(kill-buffer it)))) (persp-remove-buffer buf)
(kill-buffer buf))))
;;;###autoload
(defun +workspace/kill-session-and-quit ()
"Forgets current session and quits."
(+workspace/kill-session)
(save-buffers-kill-terminal))
;;;###autoload ;;;###autoload
(defun +workspace/new (&optional name clone-p) (defun +workspace/new (&optional name clone-p)
@ -263,8 +262,8 @@ pre-existing workspace."
(error "%s already exists" name) (error "%s already exists" name)
(persp-frame-switch name) (persp-frame-switch name)
(if clone-p (if clone-p
(--each (persp-add-buffer (window-buffer it) persp nil) (dolist (window (window-list))
(window-list)) (persp-add-buffer (window-buffer window) persp nil))
(delete-other-windows-internal) (delete-other-windows-internal)
(switch-to-buffer doom-fallback-buffer)) (switch-to-buffer doom-fallback-buffer))
(+workspace/display))) (+workspace/display)))
@ -277,7 +276,8 @@ end of the workspace list."
(interactive (interactive
(list (or current-prefix-arg (list (or current-prefix-arg
(completing-read "Switch to workspace: " (+workspace-list))))) (completing-read "Switch to workspace: " (+workspace-list)))))
(when (and (stringp index) (s-numeric? index)) (when (and (stringp index)
(string-match-p "^[0-9]+$" index))
(setq index (string-to-number index))) (setq index (string-to-number index)))
(condition-case ex (condition-case ex
(let ((names (+workspace-list))) (let ((names (+workspace-list)))
@ -305,15 +305,11 @@ end of the workspace list."
"Cycle n workspaces to the right (default) or left." "Cycle n workspaces to the right (default) or left."
(interactive (list 1)) (interactive (list 1))
(condition-case ex (condition-case ex
(let ((spaces (+workspace-list)) (let ((persp-switch-wrap t))
(current-name (+workspace-current-name)) (dotimes (i n)
index new-index) (if (> n 0)
(unless (and space current-name) (persp-next)
(error "No spaces or current workspace to cycle to/from")) (persp-prev)))
(setq index (--find-index (eq it current-name) spaces)
new-index (mod (+ index n) (length spaces)))
(unless (= index new-index)
(+workspace-switch new-index))
(unless (called-interactively-p 'interactive) (unless (called-interactively-p 'interactive)
(+workpace/display))) (+workpace/display)))
('error (+workspace-error (cadr ex) t)))) ('error (+workspace-error (cadr ex) t))))
@ -332,10 +328,7 @@ the workspace and move to the next."
(evil-window-delete) (evil-window-delete)
(delete-window))) (delete-window)))
((> (length (+workspace-list)) 1) ((> (length (+workspace-list)) 1)
(let* ((names (+workspace-list)) (+workspace/delete current-persp-name))))))
(index (--find-index (equal current-persp-name it) names)))
(+workspace/delete current-persp-name)
(+workspace-switch (nth (1- index) names))))))))
;; ;;
@ -344,19 +337,24 @@ the workspace and move to the next."
(defun +workspace--tabline (&optional names) (defun +workspace--tabline (&optional names)
(let ((names (or names (+workspace-list))) (let ((names (or names (+workspace-list)))
(current-name (+workspace-current-name))) (current-name (+workspace-current-name))
(s-join (i 0))
" " (--map-indexed (mapconcat
(propertize (format " [%s] %s " (1+ it-index) it) 'identity
(mapcar (lambda (it)
(cl-incf i)
(propertize (format " [%d] %s " i it)
'face (if (equal current-name it) 'face (if (equal current-name it)
'+workspace-tab-selected-face '+workspace-tab-selected-face
'+workspace-tab-face)) '+workspace-tab-face)))
names)))) names)
" ")))
(defun +workspace--message-body (message &optional type) (defun +workspace--message-body (message &optional type)
(concat (+workspace--tabline) (concat (+workspace--tabline)
(propertize " | " 'face 'font-lock-comment-face) (propertize " | " 'face 'font-lock-comment-face)
(propertize message 'face (pcase type (propertize (format "%s" message)
'face (pcase type
('error 'error) ('error 'error)
('warn 'warning) ('warn 'warning)
('success 'success) ('success 'success)
@ -375,57 +373,3 @@ the workspace and move to the next."
(interactive) (interactive)
(message "%s" (+workspace--tabline))) (message "%s" (+workspace--tabline)))
;;
;; Evil commands
;;
;;;###autoload (autoload '+workspace:save-session "feature/workspaces/autoload" nil t)
;;;###autoload (autoload '+workspace:load-session "feature/workspaces/autoload" nil t)
;;;###autoload (autoload '+workspace:save "feature/workspaces/autoload" nil t)
;;;###autoload (autoload '+workspace:load "feature/workspaces/autoload" nil t)
;;;###autoload (autoload '+workspace:new "feature/workspaces/autoload" nil t)
;;;###autoload (autoload '+workspace:rename "feature/workspaces/autoload" nil t)
;;;###autoload (autoload '+workspace:delete "feature/workspaces/autoload" nil t)
;;;###autoload (autoload '+workspace:switch-next "feature/workspaces/autoload" nil t)
;;;###autoload (autoload '+workspace:switch-previous "feature/workspaces/autoload" nil t)
(@after evil
(evil-define-command +workspace:save-session (&optional name)
"Ex wrapper around `+workspace/save-session'."
(interactive "<a>") (+workspace/save-session name))
(evil-define-command +workspace:load-session (&optional name)
"Ex wrapper around `+workspace/load-session'."
(interactive "<a>") (+workspace/load-session name))
(evil-define-command +workspace:save (&optional name)
"Ex wrapper around `+workspace/save-session'."
(interactive "<a>") (+workspace/save name))
(evil-define-command +workspace:load (&optional name)
"Ex wrapper around `+workspace/load-session'."
(interactive "<a>") (+workspace/load name))
(evil-define-command +workspace:new (bang name)
"Ex wrapper around `+workspace/new'. If BANG, clone the current workspace."
(interactive "<!><a>") (+workspace/new name bang))
(evil-define-command +workspace:rename (new-name)
"Ex wrapper around `+workspace/rename'."
(interactive "<a>") (+workspace/rename new-name))
(evil-define-command +workspace:delete ()
"Ex wrapper around `+workspace/delete'."
(interactive "<a>") (+workspace/delete (+workspace-current-name)))
(evil-define-command +workspace:switch-next (&optional count)
"Switch to next workspace. If COUNT, switch to COUNT-th workspace."
(interactive "<c>")
(if count (+workspace/switch-to count) (+workspace/cycle +1)))
(evil-define-command +workspace:switch-previous (&optional count)
"Switch to previous workspace. If COUNT, switch to COUNT-th workspace."
(interactive "<c>")
(if count (+workspace/switch-to count) (+workspace/cycle -1))))

View file

@ -12,18 +12,18 @@
(@def-package persp-mode :demand t (@def-package persp-mode :demand t
:init :config
(setq persp-autokill-buffer-on-remove 'kill-weak (setq persp-autokill-buffer-on-remove 'kill-weak
persp-nil-name "main" persp-nil-name "main"
persp-auto-save-fname "autosave" persp-auto-save-fname "autosave"
persp-save-dir (concat doom-cache-dir "workspaces/") persp-save-dir (concat doom-cache-dir "workspaces/")
persp-set-last-persp-for-new-frames nil persp-set-last-persp-for-new-frames nil
persp-auto-resume-time (if (display-graphic-p) 0.01 -1) persp-auto-resume-time -1
persp-auto-save-opt 0
persp-switch-to-added-buffer nil) persp-switch-to-added-buffer nil)
:config (add-hook 'after-init-hook 'persp-mode)
(@add-hook emacs-startup (persp-mode 1))
(define-key persp-mode-map [remap delete-window] '+workspace/close-window-or-workspace)
;; Ensure unreal/popup buffers aren't saved ;; Ensure unreal/popup buffers aren't saved
(push (lambda (buf) (doom-popup-p (get-buffer-window buf))) (push (lambda (buf) (doom-popup-p (get-buffer-window buf)))
@ -33,8 +33,11 @@
;; Auto-add buffers when opening them. Allows a perspective-specific buffer list. ;; Auto-add buffers when opening them. Allows a perspective-specific buffer list.
(defun +workspaces*auto-add-buffer (buffer &rest _) (defun +workspaces*auto-add-buffer (buffer &rest _)
(when (and persp-mode (not persp-temporarily-display-buffer)) (when (and persp-mode
(persp-add-buffer buffer (get-current-persp) nil))) (not persp-temporarily-display-buffer)
(doom-real-buffer-p buffer))
(persp-add-buffer buffer (get-current-persp) nil)
(redisplay)))
(advice-add 'switch-to-buffer :after '+workspaces*auto-add-buffer) (advice-add 'switch-to-buffer :after '+workspaces*auto-add-buffer)
(advice-add 'display-buffer :after '+workspaces*auto-add-buffer) (advice-add 'display-buffer :after '+workspaces*auto-add-buffer)
@ -46,14 +49,11 @@
;; TODO Test per-frame perspectives ;; TODO Test per-frame perspectives
;; We use this instead of persp's native autosave. Why? So the "Wrote ;; Be quiet when saving
;; .../_autosave" message appears AFTER the quit confirmation prompt! (defun +workspace*silence (orig-fn &rest args) (@quiet (apply orig-fn args)))
;; (advice-add 'persp-save-state-to-file :around '+workspace*silence)
;; ...I need help.
(defun +workspace|save-on-quit ()
(when persp-mode (@quiet (persp-save-state-to-file))))
(add-hook 'kill-emacs-hook '+workspace|save-on-quit)
;; Add a hook to session loading
(defun +workspaces*reinit-popups (&rest _) (defun +workspaces*reinit-popups (&rest _)
(run-hook-with-args '+workspaces-load-session-hook (window-list))) (run-hook-with-args '+workspaces-load-session-hook (window-list)))
(advice-add 'persp-load-state-from-file :after '+workspaces*reinit-popups) (advice-add 'persp-load-state-from-file :after '+workspaces*reinit-popups)
@ -61,16 +61,16 @@
;; Restore popups on load ;; Restore popups on load
(defun +workspaces|restore-popups (windows) (defun +workspaces|restore-popups (windows)
(dolist (window windows) (dolist (window windows)
(let ((plist (window-parameter window 'popup))) (when-let (plist (window-parameter window 'popup))
(when plist (doom-popup--init window plist))))) (doom-popup--init window plist))))
(add-hook '+workspaces-load-session-hook '+workspaces|restore-popups)) (add-hook '+workspaces-load-session-hook '+workspaces|restore-popups))
(@after ivy (@after ivy
(defun +workspaces|ivy-ignore-non-persp-buffers (b) ;; (defun +workspaces|ivy-ignore-non-persp-buffers (b)
(when persp-mode ;; (when persp-mode
(let ((persp (get-current-persp))) ;; (let ((persp (get-current-persp)))
(and persp (not (persp-contain-buffer-p b persp)))))) ;; (and persp (not (persp-contain-buffer-p b persp))))))
(pushnew '+workspaces|ivy-ignore-non-persp-buffers ivy-ignore-buffers) ;; (pushnew '+workspaces|ivy-ignore-non-persp-buffers ivy-ignore-buffers)
(setq ivy-sort-functions-alist (setq ivy-sort-functions-alist
(append ivy-sort-functions-alist (append ivy-sort-functions-alist