Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option: auto-generate layout names #7323

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/core-dotspacemacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ if used there.")
"If non nil then the last auto saved layouts are resume automatically upon
start.")

(defvar dotspacemacs-auto-generate-layout-names nil
"If non-nil, auto-generate layout name when creating new layouts.
Only has effect when using the \"jump to layout by number\" commands.")

(defvar dotspacemacs-max-rollback-slots 5
"Maximum number of rollback slots to keep in the cache.")

Expand Down
3 changes: 3 additions & 0 deletions core/templates/.spacemacs.template
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ values."
;; If non nil then the last auto saved layouts are resume automatically upon
;; start. (default nil)
dotspacemacs-auto-resume-layouts nil
;; If non-nil, auto-generate layout name when creating new layouts. Only has
;; effect when using the "jump to layout by number" commands.
dotspacemacs-auto-generate-layout-names nil
;; Size (in MB) above which spacemacs will prompt to open the large file
;; literally to avoid performance issues. Opening a file literally means that
;; no major mode or minor modes are active. (default is 1)
Expand Down
22 changes: 22 additions & 0 deletions layers/+spacemacs/spacemacs-layouts/config.el
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,25 @@

(defvar spacemacs--layouts-autosave-timer nil
"Timer for layouts auto-save.")

(defvar spacemacs-generic-layout-names
'(("zebra" "zucchini" "zen" "yellow" "yeti" "yard") ; grab-bag
("baboon" "banana" "blue") ; 2nd layout
("crab" "cabbage" "crayon") ; 3rd
("deer" "doughnut" "door") ; 4th
("elephant" "eggplant" "extreme") ; 5th
("falcon" "fig" "fjord") ; 6th
("gnu" "garlic" "guardian") ; 7th
("horse" "honey" "hallelujah") ; 8th
("iguana" "ice-cream" "internet") ; 9th
("jellyfish" "jalapeno" "jolt")) ; 10th (aka 0th)
"Names for auto-generated layout names.
Used by `spacemacs//generate-layout-name'.

Must be a list with 10 entries, where each entry is a list of
names. The 2nd list contains possible names for the 2nd
layout (or 10th) layout, the 3rd list contains names for the 3rd
layout, the 4th for the 4th, and so on until the 10th (aka layout
number 0). The first list is sepcial - it is a grab-bag for names
in case none of the regular names can be used for a new layout.")

40 changes: 33 additions & 7 deletions layers/+spacemacs/spacemacs-layouts/funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,49 @@ current perspective."
(propertize "?" 'face 'hydra-face-red)
"] help)")))))

(defun spacemacs//generate-layout-name (pos)
"Generate name for layout of position POS.
POS should be a number between 1 and 9, where 1 represents the
2nd layout, 2 represents the 3rd and so on. 9 represents the 10th
layout, which is also knows as the 0th layout.

If no name can be generated, return nil."
(catch 'found
;; return 1st available name
(dolist (name (nth pos spacemacs-generic-layout-names))
(unless (persp-get-by-name name)
(throw 'found name)))

;; return 1st available name from grab-bag
(dolist (name (car spacemacs-generic-layout-names))
(unless (persp-get-by-name name)
(throw 'found name)))))

(defun spacemacs/layout-switch-by-pos (pos)
"Switch to perspective of position POS."
"Switch to perspective of position POS.
If POS has no layout, and `dotspacemacs-auto-generate-layout-names'
is non-nil, create layout with auto-generated name. Otherwise,
ask the user if a new layout should be created."
(let ((persp-to-switch
(nth pos (persp-names-current-frame-fast-ordered))))
(if persp-to-switch
(persp-switch persp-to-switch)
(when (y-or-n-p
(concat "Perspective in this position doesn't exist.\n"
"Do you want to create one? "))
(let ((persp-reset-windows-on-nil-window-conf t))
(let ((persp-reset-windows-on-nil-window-conf t)
(generated-name (and dotspacemacs-auto-generate-layout-names
(spacemacs//generate-layout-name pos))))
(cond
(generated-name
(persp-switch generated-name))
((y-or-n-p (concat "Layout in this position doesn't exist. "
"Do you want to create one? "))
(persp-switch nil)
(spacemacs/home-delete-other-windows))))))
(spacemacs/home-delete-other-windows)))))))

;; Define all `spacemacs/persp-switch-to-X' functions
(dolist (i (number-sequence 9 0 -1))
(eval `(defun ,(intern (format "spacemacs/persp-switch-to-%s" i)) nil
,(format "Switch to layout %s." i)
,(format "Switch to layout %s.\n%s"
i "See `spacemacs/layout-switch-by-pos' for details.")
(interactive)
(spacemacs/layout-switch-by-pos ,(if (eq 0 i) 9 (1- i))))))

Expand Down