Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: Saving and restoring original frame layout (e.g. when working with ediff)

Tags:

emacs

In a typical Emacs session I often have only one frame open, and I have it divided into 4 windows forming a 2x2 grid with some specific buffers (files) in each window.

Every time I use ediff-buffers to compare two buffers, Emacs takes my existing frame, and re-splits it into two windows vertically (which I can choose to hortizontal by subsequentially pressing -). However, when I quit the ediff session, Emacs does not automatically restore the original layout of windows in my frame.

With this my questions are:

  1. Is there any way to automatically restore my original layout?'
  2. Even better, how can I have ediff-buffers use a new separate frame just for the ediff session and close it automatically when I quit the ediff session?
like image 775
Amelio Vazquez-Reina Avatar asked Aug 24 '13 22:08

Amelio Vazquez-Reina


4 Answers

You can set up functions on the entrance/exit hooks for ediff to save/restore the window configuration, as well as create a new frame. This seemed to do the trick in Emacs 24.3 - I don't see why it wouldn't work in older versions:

(defvar pre-ediff-window-configuration nil
  "window configuration to use")
(defvar new-ediff-frame-to-use nil
  "new frame for ediff to use")
(defun save-my-window-configuration ()
  (interactive)
  (setq pre-ediff-window-configuration (current-window-configuration))
  (select-frame-set-input-focus (setq new-ediff-frame-to-use (new-frame))))
(add-hook 'ediff-before-setup-hook 'save-my-window-configuration)
(defun restore-my-window-configuration ()
  (interactive)
  (when (framep new-ediff-frame-to-use)
    (delete-frame new-ediff-frame-to-use)
    (setq new-ediff-frame-to-use nil))
  (when (window-configuration-p pre-ediff-window-configuration)
    (set-window-configuration pre-ediff-window-configuration)))
(add-hook 'ediff-after-quit-hook-internal 'restore-my-window-configuration)
like image 153
Trey Jackson Avatar answered Oct 26 '22 13:10

Trey Jackson


On a related note (though you asked about restoring a window configuration and not a frame configuration): Recent development snapshots of Emacs 24 let you persistently save and restore the current set of frames. See the new library frameset.el and the updated library desktop.el. Be aware that this is currently being worked on, so it is liable to change in the immediate.

like image 31
Drew Avatar answered Oct 26 '22 14:10

Drew


At least in emacs >= 25, you can use C-x r f <register> to store the frame layout into a register. C-x r j <register> can restore it (Note: after working with a restored frame, you might need C-x 5 0 to quit, rather than the normal C-x C-c).

This is extremely helpful when I use emacsclient to connect to a long-living emacs daemon. The stored frame layout can live through the daemon's life time.

You can also use C-x r w <register>. But stored window layouts can only live within the same emacsclient session. After you quit emacsclient, they are gone.

like image 28
ShellayLee Avatar answered Oct 26 '22 13:10

ShellayLee


See the Emacs manual section on registers. You can save your window configurations to a register, run ediff, and then restore the configuration. The default bindings are C-x r w R to 'write' the register, and C-x r j R to 'jump' to the register. You could rebind them accordingly if you plan on using this feature frequently.

You can also write your own function that creates a new frame and runs ediff. It will take some tweaking because ediff reads the file names from the minibuffer, but it should be straightforward.

like image 35
seanmcl Avatar answered Oct 26 '22 14:10

seanmcl