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
|
|
|
|
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
|
|
|
|
2017-06-08 11:47:56 +02:00
|
|
|
(def-package! persp-mode
|
2019-04-05 05:26:54 -04:00
|
|
|
:commands (persp-switch-to-buffer)
|
2018-01-20 02:44:12 -05:00
|
|
|
:init
|
2018-01-28 20:32:08 -05:00
|
|
|
(defun +workspaces|init ()
|
2018-02-03 18:06:44 -05:00
|
|
|
;; Remove default buffer predicate so persp-mode can put in its own
|
2018-06-23 16:48:58 +02:00
|
|
|
(setq default-frame-alist
|
|
|
|
(delq (assq 'buffer-predicate default-frame-alist)
|
|
|
|
default-frame-alist))
|
2018-01-30 16:17:46 -05:00
|
|
|
(add-hook 'after-make-frame-functions #'+workspaces|init-frame)
|
2018-01-20 02:44:12 -05:00
|
|
|
(require 'persp-mode)
|
2018-01-30 16:17:46 -05:00
|
|
|
(unless (daemonp)
|
|
|
|
(+workspaces|init-frame (selected-frame))))
|
2018-01-28 20:32:08 -05:00
|
|
|
|
|
|
|
(defun +workspaces|init-frame (frame)
|
2018-07-09 15:33:31 +02:00
|
|
|
"Ensure a main workspace exists and is switched to, if FRAME isn't in any
|
2018-01-31 01:26:01 -05:00
|
|
|
workspace. Also ensures that the *Warnings* buffer will be visible in main.
|
|
|
|
|
|
|
|
Uses `+workspaces-main' to determine the name of the main workspace."
|
2018-01-30 16:17:46 -05:00
|
|
|
(unless persp-mode
|
2019-03-02 01:04:41 -05:00
|
|
|
(persp-mode +1)
|
|
|
|
(unless noninteractive
|
2019-03-24 12:35:38 -04:00
|
|
|
(let (persp-before-switch-functions)
|
2019-03-02 01:04:41 -05:00
|
|
|
(with-selected-frame frame
|
|
|
|
;; 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 aren't auto-loading the last session
|
|
|
|
(when (and (string= (safe-persp-name (get-current-persp)) persp-nil-name)
|
|
|
|
(= persp-auto-resume-time -1))
|
|
|
|
(persp-frame-switch +workspaces-main frame)
|
|
|
|
;; We want to know where we are in every new daemon frame
|
|
|
|
(when (daemonp)
|
|
|
|
(run-at-time 0.1 nil #'+workspace/display))
|
|
|
|
;; 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)))))))))))
|
2018-01-20 02:44:12 -05:00
|
|
|
|
2019-03-04 20:35:47 -05:00
|
|
|
(add-hook 'doom-init-modules-hook #'+workspaces|init t)
|
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
|
|
|
|
2018-07-09 15:33:31 +02:00
|
|
|
(advice-add #'persp-asave-on-exit :around #'+workspaces*autosave-real-buffers)
|
|
|
|
|
2018-06-30 00:57:41 +02:00
|
|
|
;; Ensure buffers we've opened/switched to are auto-added to the current
|
|
|
|
;; perspective
|
|
|
|
(setq persp-add-buffer-on-find-file t
|
|
|
|
persp-add-buffer-on-after-change-major-mode t)
|
|
|
|
(add-hook 'persp-add-buffer-on-after-change-major-mode-filter-functions #'doom-unreal-buffer-p)
|
|
|
|
|
2018-03-22 06:30:49 -04:00
|
|
|
(defun +workspaces|init-persp-mode ()
|
|
|
|
(cond (persp-mode
|
2018-07-09 15:33:31 +02:00
|
|
|
;; `persp-kill-buffer-query-function' must be last
|
2018-03-22 06:30:49 -04:00
|
|
|
(remove-hook 'kill-buffer-query-functions 'persp-kill-buffer-query-function)
|
|
|
|
(add-hook 'kill-buffer-query-functions 'persp-kill-buffer-query-function t)
|
2018-07-09 15:33:31 +02:00
|
|
|
;; Restrict buffer list to workspace
|
2018-03-22 06:30:49 -04:00
|
|
|
(advice-add #'doom-buffer-list :override #'+workspace-buffer-list))
|
2018-06-30 00:57:41 +02:00
|
|
|
((advice-remove #'doom-buffer-list #'+workspace-buffer-list))))
|
2018-01-03 13:24:11 -05:00
|
|
|
(add-hook 'persp-mode-hook #'+workspaces|init-persp-mode)
|
2018-03-22 06:30:49 -04:00
|
|
|
|
2018-05-13 23:16:06 +02:00
|
|
|
(defun +workspaces|leave-nil-perspective (&rest _)
|
|
|
|
(when (string= (+workspace-current-name) persp-nil-name)
|
2018-06-19 14:52:33 +02:00
|
|
|
(+workspace-switch (or (if (+workspace-p +workspace--last) +workspace--last)
|
|
|
|
(car (+workspace-list-names))
|
|
|
|
+workspaces-main)
|
|
|
|
'auto-create)))
|
2018-05-13 23:16:06 +02:00
|
|
|
(add-hook 'persp-after-load-state-functions #'+workspaces|leave-nil-perspective)
|
|
|
|
|
2019-05-18 21:27:23 -04:00
|
|
|
(defun +workspaces*evil-alternate-buffer (&optional window)
|
|
|
|
"Make `evil-alternate-buffer' ignore buffers outside the current workspace."
|
|
|
|
(let* ((prev-buffers (cl-remove-if-not #'persp-contain-buffer-p (window-prev-buffers)
|
|
|
|
:key #'car))
|
|
|
|
(head (car prev-buffers)))
|
|
|
|
(if (eq (car head) (window-buffer window))
|
|
|
|
(cadr prev-buffers)
|
|
|
|
head)))
|
|
|
|
(advice-add #'evil-alternate-buffer :override #'+workspaces*evil-alternate-buffer)
|
|
|
|
|
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
|
|
|
|
[remap evil-delete-window] #'+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
|
2018-01-28 18:26:32 -05:00
|
|
|
persp-interactive-init-frame-behaviour-override #'+workspaces|associate-frame
|
|
|
|
persp-emacsclient-init-frame-behaviour-override #'+workspaces|associate-frame)
|
2018-01-20 02:44:12 -05:00
|
|
|
(add-hook 'delete-frame-functions #'+workspaces|delete-associated-workspace)
|
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
|
2018-02-20 17:56:38 -05:00
|
|
|
(setq projectile-switch-project-action #'+workspaces|set-project-action
|
2018-07-04 23:59:36 +08:00
|
|
|
counsel-projectile-switch-project-action
|
|
|
|
'(1 ("o" +workspaces|switch-to-project "open project in new workspace")
|
|
|
|
("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")
|
|
|
|
("s" (lambda (project) (let ((projectile-switch-project-action (lambda () (call-interactively #'+ivy/project-search))))
|
|
|
|
(counsel-projectile-switch-project-by-name project))) "search project")
|
|
|
|
("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")))
|
|
|
|
|
2018-04-21 21:04:34 -04:00
|
|
|
(add-hook 'projectile-after-switch-project-hook #'+workspaces|switch-to-project)
|
|
|
|
|
2018-07-09 15:33:31 +02:00
|
|
|
;; In some scenarios, persp-mode throws error when Emacs tries to die,
|
|
|
|
;; preventing its death and trapping us in Emacs.
|
2018-06-29 22:23:44 +02:00
|
|
|
(defun +workspaces*ignore-errors-on-kill-emacs (orig-fn)
|
|
|
|
(ignore-errors (funcall orig-fn)))
|
|
|
|
(advice-add #'persp-kill-emacs-h :around #'+workspaces*ignore-errors-on-kill-emacs)
|
|
|
|
|
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
|
|
|
|
(buffer-name (buffer-base-buffer))))
|
|
|
|
: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)))
|
|
|
|
(defun +workspaces|reload-indirect-buffers (&rest _)
|
|
|
|
(dolist (ibc +workspaces--indirect-buffers-to-restore)
|
|
|
|
(let* ((nbn (car ibc))
|
|
|
|
(bbn (cdr ibc))
|
|
|
|
(bb (get-buffer bbn)))
|
|
|
|
(when bb
|
|
|
|
(when (get-buffer nbn)
|
|
|
|
(setq nbn (generate-new-buffer-name nbn)))
|
|
|
|
(make-indirect-buffer bb nbn t))))
|
|
|
|
(setq +workspaces--indirect-buffers-to-restore nil))
|
|
|
|
(add-hook 'persp-after-load-state-functions #'+workspaces|reload-indirect-buffers))
|