2019-04-21 19:59:44 -04:00
|
|
|
;;; ui/workspaces/config.el -*- lexical-binding: t; -*-
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-02-08 01:54:24 -05:00
|
|
|
;; `persp-mode' gives me workspaces, a workspace-restricted `buffer-list', and
|
2017-06-14 21:03:20 +02:00
|
|
|
;; file-based session persistence. I used workgroups2 before this, but abandoned
|
|
|
|
;; it because it was unstable and slow; `persp-mode' is neither (and still
|
|
|
|
;; maintained).
|
2017-02-11 00:52:25 -05:00
|
|
|
;;
|
2017-09-02 16:39:51 +02:00
|
|
|
;; NOTE persp-mode requires `workgroups' for file persistence in Emacs 24.4.
|
2017-02-11 00:52:25 -05:00
|
|
|
|
2017-04-10 18:21:42 -04:00
|
|
|
(defvar +workspaces-main "main"
|
2018-07-09 15:33:31 +02:00
|
|
|
"The name of the primary and initial workspace, which cannot be deleted.")
|
2017-04-10 18:21:42 -04:00
|
|
|
|
2018-02-20 17:56:38 -05:00
|
|
|
(defvar +workspaces-switch-project-function #'doom-project-find-file
|
|
|
|
"The function to run after `projectile-switch-project' or
|
|
|
|
`counsel-projectile-switch-project'. This function must take one argument: the
|
|
|
|
new project directory.")
|
|
|
|
|
2018-10-03 00:00:56 -04:00
|
|
|
(defvar +workspaces-on-switch-project-behavior 'non-empty
|
|
|
|
"Controls the behavior of workspaces when switching to a new project.
|
|
|
|
|
|
|
|
Can be one of the following:
|
|
|
|
|
|
|
|
t Always create a new workspace for the project
|
2019-03-08 12:49:15 +10:00
|
|
|
'non-empty Only create a new workspace if the current one already has buffers
|
2018-10-03 00:00:56 -04:00
|
|
|
associated with it.
|
|
|
|
nil Never create a new workspace on project switch.")
|
|
|
|
|
2019-03-02 01:04:41 -05:00
|
|
|
;; FIXME actually use this for wconf bookmark system
|
|
|
|
(defvar +workspaces-data-file "_workspaces"
|
|
|
|
"The basename of the file to store single workspace perspectives. Will be
|
|
|
|
stored in `persp-save-dir'.")
|
2018-06-23 16:48:58 +02:00
|
|
|
|
2019-06-25 11:21:39 +02:00
|
|
|
(defvar +workspace--old-uniquify-style nil)
|
|
|
|
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2017-04-11 08:31:00 -04:00
|
|
|
;;
|
2018-09-07 19:36:16 -04:00
|
|
|
;; Packages
|
2017-04-11 08:31:00 -04:00
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! persp-mode
|
2019-07-22 00:52:05 +02:00
|
|
|
:commands persp-switch-to-buffer
|
2018-01-20 02:44:12 -05:00
|
|
|
:init
|
2019-07-27 17:08:03 +02:00
|
|
|
(add-hook! 'doom-init-modules-hook
|
2019-07-22 00:52:05 +02:00
|
|
|
(defun +workspaces-init-h ()
|
|
|
|
(unless noninteractive
|
|
|
|
;; Remove default buffer predicate so persp-mode can put in its own
|
|
|
|
(delq! 'buffer-predicate default-frame-alist 'assq)
|
|
|
|
(require 'persp-mode)
|
|
|
|
(if (daemonp)
|
|
|
|
(add-hook 'after-make-frame-functions #'persp-mode-start-and-remove-from-make-frame-hook)
|
|
|
|
(persp-mode +1)))))
|
2017-02-19 18:40:39 -05:00
|
|
|
:config
|
2017-02-08 01:54:24 -05:00
|
|
|
(setq persp-autokill-buffer-on-remove 'kill-weak
|
2017-04-12 11:27:31 -04:00
|
|
|
persp-nil-hidden t
|
2017-02-11 00:52:25 -05:00
|
|
|
persp-auto-save-fname "autosave"
|
2017-11-04 22:34:55 +01:00
|
|
|
persp-save-dir (concat doom-etc-dir "workspaces/")
|
2018-01-20 02:44:12 -05:00
|
|
|
persp-set-last-persp-for-new-frames t
|
2017-02-22 04:28:20 -05:00
|
|
|
persp-switch-to-added-buffer nil
|
2017-04-10 18:21:42 -04:00
|
|
|
persp-remove-buffers-from-nil-persp-behaviour nil
|
2018-02-01 19:01:49 -05:00
|
|
|
persp-auto-resume-time -1 ; Don't auto-load on startup
|
|
|
|
persp-auto-save-opt (if noninteractive 0 1)) ; auto-save on kill
|
2017-02-04 03:21:04 -05:00
|
|
|
|
2019-07-22 00:52:05 +02:00
|
|
|
(advice-add #'persp-asave-on-exit :around #'+workspaces-autosave-real-buffers-a)
|
|
|
|
|
|
|
|
(add-hook! '(persp-mode-hook persp-after-load-state-functions)
|
|
|
|
(defun +workspaces-ensure-main-workspace-h (&rest _)
|
|
|
|
"Ensure the main workspace exists and the nil workspace is never active."
|
|
|
|
(when persp-mode
|
|
|
|
(let (persp-before-switch-functions)
|
|
|
|
;; The default perspective persp-mode creates (`persp-nil-name') is
|
|
|
|
;; special and doesn't represent a real persp object, so buffers can't
|
|
|
|
;; really be assigned to it, among other quirks. We create a *real* main
|
|
|
|
;; workspace to fill this role.
|
|
|
|
(unless (persp-get-by-name +workspaces-main)
|
|
|
|
(persp-add-new +workspaces-main))
|
|
|
|
;; Switch to it if we're in the nil perspective
|
|
|
|
(dolist (frame (frame-list))
|
|
|
|
(when (string= (safe-persp-name (get-current-persp frame)) persp-nil-name)
|
|
|
|
(persp-frame-switch +workspaces-main frame)
|
|
|
|
;; Fix #319: the warnings buffer gets swallowed by creating
|
|
|
|
;; `+workspaces-main', so we display it manually, if it exists.
|
|
|
|
(when-let (warnings (get-buffer "*Warnings*"))
|
|
|
|
(save-excursion
|
|
|
|
(display-buffer-in-side-window
|
|
|
|
warnings '((window-height . shrink-window-if-larger-than-buffer)))))))))))
|
|
|
|
|
2019-07-26 19:57:13 +02:00
|
|
|
(add-hook! 'persp-mode-hook
|
2019-07-22 00:52:05 +02:00
|
|
|
(defun +workspaces-init-persp-mode-h ()
|
|
|
|
(cond (persp-mode
|
|
|
|
;; `uniquify' breaks persp-mode. It renames old buffers, which causes
|
|
|
|
;; errors when switching between perspective (their buffers are
|
|
|
|
;; serialized by name and persp-mode expects them to have the same
|
|
|
|
;; name when restored).
|
|
|
|
(when uniquify-buffer-name-style
|
|
|
|
(setq +workspace--old-uniquify-style uniquify-buffer-name-style))
|
|
|
|
(setq uniquify-buffer-name-style nil)
|
|
|
|
;; Ensure `persp-kill-buffer-query-function' is last
|
|
|
|
(remove-hook 'kill-buffer-query-functions #'persp-kill-buffer-query-function)
|
|
|
|
(add-hook 'kill-buffer-query-functions #'persp-kill-buffer-query-function t)
|
|
|
|
;; Restrict buffer list to workspace
|
|
|
|
(advice-add #'doom-buffer-list :override #'+workspace-buffer-list))
|
|
|
|
(t
|
|
|
|
(when +workspace--old-uniquify-style
|
|
|
|
(setq uniquify-buffer-name-style +workspace--old-uniquify-style))
|
|
|
|
(advice-remove #'doom-buffer-list #'+workspace-buffer-list)))))
|
2018-03-22 06:30:49 -04:00
|
|
|
|
2019-06-25 11:21:39 +02:00
|
|
|
;; We don't rely on the built-in mechanism for auto-registering a buffer to
|
|
|
|
;; the current workspace; some buffers slip through the cracks. Instead, we
|
|
|
|
;; add buffers when they are switched to.
|
|
|
|
(setq persp-add-buffer-on-find-file nil
|
|
|
|
persp-add-buffer-on-after-change-major-mode nil)
|
|
|
|
|
2019-09-14 18:28:12 -04:00
|
|
|
(add-hook! '(doom-switch-buffer-hook server-visit-hook)
|
2019-07-22 00:52:05 +02:00
|
|
|
(defun +workspaces-add-current-buffer-h ()
|
|
|
|
"Add current buffer to focused perspective."
|
2019-08-22 14:45:40 -04:00
|
|
|
(and persp-mode
|
|
|
|
(not (persp-buffer-filtered-out-p
|
|
|
|
(current-buffer)
|
|
|
|
persp-add-buffer-on-after-change-major-mode-filter-functions))
|
|
|
|
(persp-add-buffer (current-buffer) (get-current-persp) nil nil))))
|
2019-06-25 11:21:39 +02:00
|
|
|
|
2019-07-27 17:08:03 +02:00
|
|
|
(add-hook 'persp-add-buffer-on-after-change-major-mode-filter-functions
|
|
|
|
#'doom-unreal-buffer-p)
|
2018-05-13 23:16:06 +02:00
|
|
|
|
2019-07-23 17:24:56 +02:00
|
|
|
(defadvice! +workspaces--evil-alternate-buffer-a (&optional window)
|
2019-05-18 21:27:23 -04:00
|
|
|
"Make `evil-alternate-buffer' ignore buffers outside the current workspace."
|
2019-07-22 00:52:05 +02:00
|
|
|
:override #'evil-alternate-buffer
|
|
|
|
(let* ((prev-buffers
|
|
|
|
(if persp-mode
|
|
|
|
(cl-remove-if-not #'persp-contain-buffer-p (window-prev-buffers)
|
|
|
|
:key #'car)
|
|
|
|
(window-prev-buffers)))
|
2019-05-18 21:27:23 -04:00
|
|
|
(head (car prev-buffers)))
|
|
|
|
(if (eq (car head) (window-buffer window))
|
|
|
|
(cadr prev-buffers)
|
|
|
|
head)))
|
|
|
|
|
2018-07-09 15:33:31 +02:00
|
|
|
;; Delete the current workspace if closing the last open window
|
2018-06-05 19:50:56 +02:00
|
|
|
(define-key! persp-mode-map
|
|
|
|
[remap delete-window] #'+workspace/close-window-or-workspace
|
2019-08-08 23:58:41 -04:00
|
|
|
[remap evil-window-delete] #'+workspace/close-window-or-workspace)
|
2017-06-28 15:16:30 +02:00
|
|
|
|
2018-01-03 13:24:11 -05:00
|
|
|
;; per-frame workspaces
|
2018-01-31 01:13:56 -05:00
|
|
|
(setq persp-init-frame-behaviour t
|
|
|
|
persp-init-new-frame-behaviour-override nil
|
2019-07-22 00:52:05 +02:00
|
|
|
persp-interactive-init-frame-behaviour-override #'+workspaces-associate-frame-fn
|
|
|
|
persp-emacsclient-init-frame-behaviour-override #'+workspaces-associate-frame-fn)
|
|
|
|
(add-hook 'delete-frame-functions #'+workspaces-delete-associated-workspace-h)
|
2017-06-28 15:16:30 +02:00
|
|
|
|
2018-03-22 06:30:49 -04:00
|
|
|
;; per-project workspaces, but reuse current workspace if empty
|
2019-07-22 00:52:05 +02:00
|
|
|
(setq projectile-switch-project-action #'+workspaces-set-project-action-fn
|
2018-07-04 23:59:36 +08:00
|
|
|
counsel-projectile-switch-project-action
|
2019-07-22 00:52:05 +02:00
|
|
|
'(1 ("o" +workspaces-switch-to-project-h "open project in new workspace")
|
2018-07-04 23:59:36 +08:00
|
|
|
("O" counsel-projectile-switch-project-action "jump to a project buffer or file")
|
|
|
|
("f" counsel-projectile-switch-project-action-find-file "jump to a project file")
|
|
|
|
("d" counsel-projectile-switch-project-action-find-dir "jump to a project directory")
|
|
|
|
("b" counsel-projectile-switch-project-action-switch-to-buffer "jump to a project buffer")
|
|
|
|
("m" counsel-projectile-switch-project-action-find-file-manually "find file manually from project root")
|
|
|
|
("w" counsel-projectile-switch-project-action-save-all-buffers "save all project buffers")
|
|
|
|
("k" counsel-projectile-switch-project-action-kill-buffers "kill all project buffers")
|
|
|
|
("r" counsel-projectile-switch-project-action-remove-known-project "remove project from known projects")
|
|
|
|
("c" counsel-projectile-switch-project-action-compile "run project compilation command")
|
|
|
|
("C" counsel-projectile-switch-project-action-configure "run project configure command")
|
|
|
|
("e" counsel-projectile-switch-project-action-edit-dir-locals "edit project dir-locals")
|
|
|
|
("v" counsel-projectile-switch-project-action-vc "open project in vc-dir / magit / monky")
|
2019-12-05 14:56:16 -05:00
|
|
|
("s" (lambda (project)
|
|
|
|
(let ((projectile-switch-project-action
|
|
|
|
(lambda () (call-interactively #'+ivy/project-search))))
|
|
|
|
(counsel-projectile-switch-project-by-name project))) "search project")
|
2018-07-04 23:59:36 +08:00
|
|
|
("xs" counsel-projectile-switch-project-action-run-shell "invoke shell from project root")
|
|
|
|
("xe" counsel-projectile-switch-project-action-run-eshell "invoke eshell from project root")
|
|
|
|
("xt" counsel-projectile-switch-project-action-run-term "invoke term from project root")
|
|
|
|
("X" counsel-projectile-switch-project-action-org-capture "org-capture into project")))
|
|
|
|
|
2019-07-22 00:52:05 +02:00
|
|
|
(add-hook 'projectile-after-switch-project-hook #'+workspaces-switch-to-project-h)
|
2018-04-21 21:04:34 -04:00
|
|
|
|
2019-10-28 12:13:58 -04:00
|
|
|
;; Fix #1973: visual selection surviving workspace changes
|
|
|
|
(add-hook 'persp-before-deactivate-functions #'deactivate-mark)
|
|
|
|
|
2019-06-25 11:55:00 +02:00
|
|
|
;; Fix #1017: stop session persistence from restoring a broken posframe
|
|
|
|
(after! posframe
|
2019-07-28 14:52:59 +02:00
|
|
|
(add-hook! 'persp-after-load-state-functions
|
2019-07-22 00:52:05 +02:00
|
|
|
(defun +workspaces-delete-all-posframes-h (&rest _)
|
|
|
|
(posframe-delete-all))))
|
2019-06-25 11:55:00 +02:00
|
|
|
|
2019-08-21 00:22:52 -04:00
|
|
|
;; Fix #1525: Ignore dead buffers in PERSP's buffer list
|
2019-08-28 12:40:10 -04:00
|
|
|
(defun +workspaces-dead-buffer-p (buf)
|
|
|
|
(not (buffer-live-p buf)))
|
|
|
|
(add-hook 'persp-filter-save-buffers-functions #'+workspaces-dead-buffer-p)
|
2019-08-21 00:22:52 -04:00
|
|
|
|
2018-04-21 21:04:34 -04:00
|
|
|
;;
|
|
|
|
;; eshell
|
|
|
|
(persp-def-buffer-save/load
|
|
|
|
:mode 'eshell-mode :tag-symbol 'def-eshell-buffer
|
|
|
|
:save-vars '(major-mode default-directory))
|
|
|
|
;; compile
|
|
|
|
(persp-def-buffer-save/load
|
|
|
|
:mode 'compilation-mode :tag-symbol 'def-compilation-buffer
|
|
|
|
:save-vars
|
|
|
|
'(major-mode default-directory compilation-directory compilation-environment compilation-arguments))
|
2018-06-19 14:50:27 +02:00
|
|
|
;; Restore indirect buffers
|
|
|
|
(defvar +workspaces--indirect-buffers-to-restore nil)
|
|
|
|
(persp-def-buffer-save/load
|
|
|
|
:tag-symbol 'def-indirect-buffer
|
|
|
|
:predicate #'buffer-base-buffer
|
|
|
|
:save-function (lambda (buf tag vars)
|
|
|
|
(list tag (buffer-name buf) vars
|
2019-07-08 21:21:02 +02:00
|
|
|
(buffer-name (buffer-base-buffer buf))))
|
2018-06-19 14:50:27 +02:00
|
|
|
:load-function (lambda (savelist &rest _rest)
|
2018-06-30 02:53:35 +02:00
|
|
|
(cl-destructuring-bind (buf-name _vars base-buf-name &rest _)
|
2018-06-26 01:46:34 +02:00
|
|
|
(cdr savelist)
|
2018-06-19 14:50:27 +02:00
|
|
|
(push (cons buf-name base-buf-name)
|
|
|
|
+workspaces--indirect-buffers-to-restore)
|
|
|
|
nil)))
|
2019-08-17 16:04:41 -04:00
|
|
|
(add-hook! 'persp-after-load-state-functions
|
2019-07-22 00:52:05 +02:00
|
|
|
(defun +workspaces-reload-indirect-buffers-h (&rest _)
|
|
|
|
(dolist (ibc +workspaces--indirect-buffers-to-restore)
|
|
|
|
(cl-destructuring-bind (buffer-name . base-buffer-name) ibc
|
|
|
|
(when (buffer-live-p (get-buffer base-buffer-name))
|
|
|
|
(when (get-buffer buffer-name)
|
|
|
|
(setq buffer-name (generate-new-buffer-name buffer-name)))
|
|
|
|
(make-indirect-buffer bb buffer-name t))))
|
|
|
|
(setq +workspaces--indirect-buffers-to-restore nil))))
|