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

Preserve perspectives when deleting a frame #42

Closed
wants to merge 1 commit into from
Closed
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
54 changes: 48 additions & 6 deletions perspective.el
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ named collections of buffers and window configurations."
(ad-activate 'exit-recursive-edit)
(add-hook 'after-make-frame-functions 'persp-init-frame)
(add-hook 'ido-make-buffer-list-hook 'persp-set-ido-buffers)
(add-to-list 'delete-frame-functions 'persp-frame-deleted)
(setq read-buffer-function 'persp-read-buffer)
(mapc 'persp-init-frame (frame-list))
(setf (persp-buffers persp-curr) (buffer-list))
Expand All @@ -719,10 +720,43 @@ named collections of buffers and window configurations."
(ad-deactivate-regexp "^persp-.*")
(remove-hook 'after-make-frame-functions 'persp-init-frame)
(remove-hook 'ido-make-buffer-list-hook 'persp-set-ido-buffers)
(delete 'persp-frame-deleted 'delete-frame-functions)
(setq read-buffer-function nil)
(setq perspectives-hash nil)
(setq global-mode-string (delq 'persp-modestring global-mode-string))))

(defvar persp-saved-hash nil
"Stores perspective from the last deleted frame.")

(defun persp-frame-deleted (frame)
"Import or save perspectives from a deleted frame.

When a frame is being deleted, import its perspectives to another
live frame if it exists. Otherwise, if the frame being deleted
is the last live frame, and emacs is running as a daemon, then
store the perspectives of the frame being deleted so that they
can be restored when a new frame is later created.
"
(let ((min-num-frames (if (daemonp) 2 1)))
;; Check whether the frame being deleted is the last visible frame. In
;; daemon mode, a fake fame is used, so the last visible frame occurs when
;; the length of the frame list is 2.
(if (= (length (frame-list)) min-num-frames)
(when (daemonp)
(persp-save)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this?

(setq persp-saved-hash perspectives-hash))
(persp-import-all-from-frame (get-other-frame) frame))))

(defun persp-import-all-from-frame (to-frame from-frame)
"Import all perspectives from FROM-FRAME to TO-FRAME."
(let ((from-hash (with-selected-frame from-frame perspectives-hash))
(to-hash (with-selected-frame to-frame perspectives-hash)))
(cl-loop for name being the hash-keys of from-hash
when (not (gethash name to-hash)) do
(puthash name (gethash name from-hash)
to-hash)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than just ditching the old perspective if there already exists one with the same name, I think we should merge them as best we can.

(with-selected-frame to-frame (persp-update-modestring)))

(defun persp-init-frame (frame)
"Initialize the perspectives system in FRAME.
By default, this uses the current frame."
Expand All @@ -731,9 +765,16 @@ By default, this uses the current frame."
frame
'((perspectives-hash) (persp-curr) (persp-last) (persp-recursive) (persp-modestring)))

;; Don't set these variables in modify-frame-parameters
;; because that won't do anything if they've already been accessed
(setq perspectives-hash (make-hash-table :test 'equal :size 10))
;; Restore perspectives from the last deleted frame in daemon mode.
(if persp-saved-hash
(progn
(setq perspectives-hash persp-saved-hash)
(setq persp-saved-hash nil))

;; Don't set these variables in modify-frame-parameters because that
;; won't do anything if they've already been accessed (setq
;; perspectives-hash (make-hash-table :test 'equal :size 10))
(setq perspectives-hash (make-hash-table :test 'equal :size 10)))

(when persp-show-modestring
(if (eq persp-show-modestring 'header)
Expand All @@ -742,12 +783,13 @@ By default, this uses the current frame."
(set-default 'header-line-format (append val '(persp-modestring)))))
(setq global-mode-string (or global-mode-string '("")))
(unless (memq 'persp-modestring global-mode-string)
(setq global-mode-string (append global-mode-string '(persp-modestring)))))
(persp-update-modestring))
(setq global-mode-string (append global-mode-string '(persp-modestring))))))

(persp-activate
(make-persp :name persp-initial-frame-name :buffers (list (current-buffer))
:window-configuration (current-window-configuration)))))
:window-configuration (current-window-configuration)))
))


(defun persp-make-variable-persp-local (variable)
"Make VARIABLE become perspective-local.
Expand Down