Bad-ptr / persp-mode.el

named perspectives(set of buffers/window configs) for emacs
402 stars 44 forks source link

Switch to last active perspective on layout autoresume #141

Open GreenRecycleBin opened 1 year ago

GreenRecycleBin commented 1 year ago

Spacemacs supports autoresuming layouts on startup by setting dotspacemacs-auto-resume-layouts to t. However, the last active perspective is not switched to automatically. Instead, the default, i.e., nil perspective is.

This PR:

I tested it manually and it worked. On startup, Spacemacs autoresumes the layouts, and additionally switched to the last active perspective. The only extra step, specific to Spacemacs, is to close the Spacemacs buffer because there's no option to disable it on startup except for changing the code.

(I also tried reusing persp-special-last-buffer here, but that didn't work because it's cleared and only set on mode activation.)

GreenRecycleBin commented 1 year ago

@Bad-ptr Would you be able to review this? 👋

Bad-ptr commented 1 year ago

Adds an active field to the perspective struct

I think that you can utilize the "perspective parameters" feature. By using persp-parameter, set-persp-parameter, delete-persp-parameter functions. So that when you switch perspectives the last-active parameter will be deleted for a currently active perspective and set to t for a perspective you are switching to. Then it will be save/loaded for free.

Add to the persp-after-load-state-functions a function like

(defun persp-switch-to-last-active-after-load (file phash persp-names)
  (when (eq phash *persp-hash*) ;; this 'when' is important
    (when-let ((la-persp (find-if (apply-partially #'persp-parameter 'last-active)
                                  (persp-persps))))
      (persp-switch (safe-persp-name la-persp)))))

And to the persp-before-switch-functions:

(defun persp-before-switch-track-last-active (name f-or-w)
  (delete-persp-parameter 'last-active (current-persp))
  (when-let ((new-a-p (persp-add-new name)))
    (set-persp-parameter 'last-active t new-a-p)))