Skip to content

Commit

Permalink
Fix Perspectives lazy loading
Browse files Browse the repository at this point in the history
Now that I understand better lazy loading and elisp I figured I'd give
the perspectives layer another go. I try to replicate the microstate
from Eyebrowse, but still working on the custom-perspectives.
  • Loading branch information
CestDiego committed Sep 1, 2015
1 parent fba874d commit 1933564
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 104 deletions.
50 changes: 29 additions & 21 deletions contrib/!window-management/perspectives/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
* Table of Contents :TOC@4:
- [[#description][Description]]
- [[#install][Install]]
- [[#layer][Layer]]
- [[#custom-perspective-macro][Custom Perspective Macro]]
- [[#layer][Layer]]
- [[#removing-additional-help][Removing additional help]]
- [[#custom-perspective-macro][Custom Perspective Macro]]
- [[#predefined-custom-perspectives][Predefined custom perspectives]]
- [[#per-project-custom-perpsective][Per project custom perpsective]]
- [[#org-agenda-custom-perspective][Org-agenda custom perspective]]
- [[#rcirc-custom-perspective][RCIRC custom perspective]]
- [[#per-project-custom-perpsective][Per project custom perpsective]]
- [[#org-agenda-custom-perspective][Org-agenda custom perspective]]
- [[#rcirc-custom-perspective][RCIRC custom perspective]]
- [[#key-bindings][Key Bindings]]
- [[#custom-perspectives-key-bindings][Custom Perspectives Key Bindings]]
- [[#custom-perspectives-key-bindings][Custom Perspectives Key Bindings]]

* Description

Expand All @@ -27,6 +28,18 @@ To use this contribution add it to your =~/.spacemacs=
(setq-default dotspacemacs-configuration-layers '(perspectives))
#+END_SRC


** Removing additional help

Once you know the key bindings to navigate between the workspaces you
may want to disable the exhaustive help in the workspace micro-state.
Set the variable =perspective-display-help= to =nil=

#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
'((perspectives :variables perspective-display-help nil)))
#+END_SRC

** Custom Perspective Macro

If you want to add a new custom-persp (for example if you want to have
Expand Down Expand Up @@ -56,12 +69,7 @@ to define a perspective with a layout.
)))
#+END_SRC

Then you just need to add a keybinding to your custom persp, we use
~SPC L o [your key here]~ (Perspective open <key>)

#+BEGIN_SRC emacs-lisp
(evil-leader/set-key "Lo<your key here>" 'custom-persp/<persp-function-name>)
#+END_SRC
Then you can add this custom perspective to a binding of your choice.

* Predefined custom perspectives

Expand Down Expand Up @@ -103,15 +111,15 @@ You will have to use the perspective layer as well as the rcirc layer:

Prefix command for perspective commands is ~SPC L~ (for Layout).

| Key Binding | Description |
|-------------+----------------------------------------------------|
| ~SPC L A~ | Add current buffer to the current perspective |
| ~SPC L c~ | Close a perspective (kill it) |
| ~SPC L k~ | Remove current buffer from the current perspective |
| ~SPC L n~ | Next perspective |
| ~SPC L p~ | Previous perspective |
| ~SPC L r~ | Rename current perspective |
| ~SPC L s~ | Switch to a perspective |
| Key Binding | Description |
|------------------------+-----------------------------------|
| ~SPC L TAB~ | switch to last active perspective |
| ~SPC L c~ | kill current perspective |
| ~SPC L C~ | kill selected perspective |
| ~SPC L n~ | switch to next perspective |
| ~SPC L N~ or ~SPC W p~ | switch to previous perspective |
| ~SPC L r~ | rename perspective |
| ~SPC L s~ | switch perspective |

** Custom Perspectives Key Bindings

Expand Down
3 changes: 3 additions & 0 deletions contrib/!window-management/perspectives/config.el
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
(defvar perspective-enable-persp-projectile nil
"If non nil the helm-projectile-swtich-project command will create
a new perspective for each new project.")

(defvar perspective-display-help t
"If non-nil additional help is displayed when selecting a perspective.")
222 changes: 139 additions & 83 deletions contrib/!window-management/perspectives/packages.el
Original file line number Diff line number Diff line change
@@ -1,107 +1,163 @@
(setq perspectives-packages
'(
perspective
persp-projectile
))
'(perspective
persp-projectile))

(defun perspectives/init-perspective ()
(use-package perspective
:defer
:commands (custom-persp
persp-add-buffer
persp-set-buffer
persp-kill
persp-remove-buffer
persp-mode
;; persp-cycle-next
;; persp-cycle-prev
persp-rename
persp-switch
projectile-persp-bridge
)
projectile-persp-bridge)
:init
(progn
;; muh perspectives
(defun custom-persp/emacs ()
(interactive)
(custom-persp ".emacs.d"
(find-file (locate-user-emacs-file "init.el"))))
(persp-mode)

(setq persp-show-modestring nil)
(defun spacemacs/persp-change-to-number (number)
(let* ((persps (persp-names))
(target-persp (nth number persps)))
(persp-switch target-persp)
(nth 1 (persp-names))))

(defun spacemacs/perspective-number ()
"Return the number of the current perspective."
(let* ((num (eyebrowse--get 'current-slot))
(str (if num (int-to-string num))))
(cond
((not (dotspacemacs|symbol-value
dotspacemacs-mode-line-unicode-symbols)) str)
((equal str "1") "")
((equal str "2") "")
((equal str "3") "")
((equal str "4") "")
((equal str "5") "")
((equal str "6") "")
((equal str "7") "")
((equal str "8") "")
((equal str "9") "")
((equal str "0") ""))))

(defun spacemacs/perspectives-ms-rename ()
"Rename a perspective and get back to micro-state."
(interactive)
(call-interactively #'persp-rename)
(spacemacs/perspectives-micro-state))

(defun spacemacs//perspectives-ms-get-name-string (window-config)
"Return the name for the given window-config"
(let ((slot (car window-config))
(caption (eyebrowse-format-slot window-config)))
(if (= slot current-slot)
(format "[%s]" caption)
caption)))

(defun spacemacs//perspectives-ms-documentation ()
"Return the docstring for the perspectives micro-state."
(let* ((current-persp (persp-name persp-curr))
(frame-perspectives (persp-names))
(all-perspectives (persp-all-names)))
(concat
"<" (when frame-perspectives
(concat
(mapconcat
(lambda (persp) (persp-format-name persp))
frame-perspectives "> <")
">"
(when perspective-display-help
(concat
"\n[n] next, [p/N] previous, [TAB] back and forth, "
"[c] close, [r] rename")))))))

(defun custom-persp/org ()
(interactive)
(custom-persp "@org"
(find-file (first org-agenda-files))))
(spacemacs|define-micro-state perspectives
:doc (spacemacs//perspectives-ms-documentation)
:use-minibuffer t
:evil-leader "L"
:bindings
("<tab>" custom-persp-last)
("c" persp-kill)
("n" persp-next)
("N" persp-prev)
("p" persp-prev)
("r" spacemacs/perspectives-ms-rename :exit t)
("s" persp-switch :exit t))

(defun custom-persp/rcirc ()
(interactive)
(custom-persp "@RCIRC" (spacemacs/rcirc nil)))
;; Loading code for our custom perspectives
;; Taken from Magnar Sveen
(defmacro custom-persp (name &rest body)
`(let ((initialize (not (gethash ,name perspectives-hash)))
(current-perspective persp-curr))
(persp-switch ,name)
(when initialize ,@body)
(setq persp-last current-perspective)))

(spacemacs/declare-prefix "L" "layouts")
(spacemacs/declare-prefix "Lo" "custom-perspectives")
(evil-leader/set-key
"La" 'persp-add-buffer
"LA" 'persp-set-buffer
"Lc" 'persp-kill
"Lk" 'persp-remove-buffer
"Ln" 'persp-next
"Loe" 'custom-persp/emacs
"Loi" 'custom-persp/rcirc
"Loo" 'custom-persp/org
"Lp" 'persp-prev
"Lr" 'persp-rename
"Ls" 'persp-switch))
:config
(progn
(persp-mode t)
;; loading code for our custom perspectives
;; taken from Magnar Sveen
(defmacro custom-persp (name &rest body)
`(let ((initialize (not (gethash ,name perspectives-hash)))
(current-perspective persp-curr))
(persp-switch ,name)
(when initialize ,@body)
(setq persp-last current-perspective)))
(defun custom-persp/emacs ()
(interactive)
(custom-persp ".emacs.d"
(find-file (locate-user-emacs-file "init.el"))))

(add-hook 'emacs-startup-hook '(lambda ()
(persp-rename "@spacemacs")))
;; Jump to last perspective
;; taken from Magnar Sveen
(defun custom-persp-last ()
(interactive)
(persp-switch (persp-name persp-last)))
(defun custom-persp/org ()
(interactive)
(custom-persp "@org"
(find-file (first org-agenda-files))))

;; (defun persp-cycle-next ()
;; "Cycle throught the available perspectives."
;; (interactive)
;; (let ((next-pos (1+ (persp-name persp-curr)))
;; (list-size (length (persp-all-names))))
;; (cond ((eq 1 list-size) (persp-switch nil))
;; ((>= next-pos list-size) (persp-switch (nth 0 (persp-all-names))))
;; (t (persp-next)))))
;; (defun persp-cycle-prev ()
;; "Cycle throught the available perspectives."
;; (interactive)
;; (let ((next-pos (- (persp-name persp-curr) 1))
;; (list-size (length (persp-all-names))))
;; (cond ((eq 1 list-size) (persp-switch nil))
;; ((< next-pos 0) (persp-switch (nth (- list-size 1) (persp-all-names))))
;; (t (persp-prev)))))
)
(defun custom-persp/rcirc ()
(interactive)
(custom-persp "@RCIRC" (spacemacs/rcirc nil)))

;; Jump to last perspective
;; taken from Magnar Sveen
(defun custom-persp-last ()
(interactive)
(persp-switch (persp-name persp-last)))

;; (defun persp-cycle-next ()
;; "Cycle throught the available perspectives."
;; (interactive)
;; (let ((next-pos (1+ (persp-name persp-curr)))
;; (list-size (length (persp-all-names))))
;; (cond ((eq 1 list-size) (persp-switch nil))
;; ((>= next-pos list-size) (persp-switch (nth 0 (persp-all-names))))
;; (t (persp-next)))))
;; (defun persp-cycle-prev ()
;; "Cycle throught the available perspectives."
;; (interactive)
;; (let ((next-pos (- (persp-name persp-curr) 1))
;; (list-size (length (persp-all-names))))
;; (cond ((eq 1 list-size) (persp-switch nil))
;; ((< next-pos 0) (persp-switch (nth (- list-size 1) (persp-all-names))))
;; (t (persp-prev)))))
))

(defun perspectives/init-persp-projectile ()
(use-package persp-projectile
:commands (projectile-persp-bridge)
:if perspective-enable-persp-projectile
:config
(progn
(projectile-persp-bridge helm-projectile-switch-project)

(evil-leader/set-key
"pp" 'spacemacs/persp-switch-project)

(defun spacemacs/persp-switch-project ()
(interactive)
(evil-leader/set-key
"pp" 'helm-projectile-switch-project)
(find-file "~/.spacemacs")
(helm-projectile-switch-project)
(persp-add-buffer "*spacemacs*")
(persp-kill "@spacemacs")))))
:init
(defvar persp-projectile-helm-source-projects
(helm-build-in-buffer-source "Projectile projects"
:data (lambda ()
(if (projectile-project-p)
(cons (abbreviate-file-name (projectile-project-root))
(projectile-relevant-known-projects))
projectile-known-projects))
:fuzzy-match helm-projectile-fuzzy-match
:mode-line helm-read-file-name-mode-line-string
:action '(("Switch to project" .
(lambda (project)
(projectile-persp-switch-project project)))))
"Helm source for known projectile projects.")
(defun helm-projectile-persp-switch-project (arg)
(interactive "P")
(if arg
(helm-projectile-switch-project)
(helm :sources persp-projectile-helm-source-projects
:buffer "*persp projectile helm*")))
(evil-leader/set-key
"pp" 'helm-projectile-persp-switch-project)))

0 comments on commit 1933564

Please sign in to comment.