1 ;;; winner.el --- Restore old window configurations
3 ;; Copyright (C) 1997-1998, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Ivar Rummelhoff <ivarru@math.uio.no>
6 ;; Created: 27 Feb 1997
7 ;; Keywords: convenience frames
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Winner mode is a global minor mode that records the changes in the
27 ;; window configuration (i.e. how the frames are partitioned into
28 ;; windows) so that the changes can be "undone" using the command
29 ;; `winner-undo'. By default this one is bound to the key sequence
30 ;; ctrl-c left. If you change your mind (while undoing), you can
31 ;; press ctrl-c right (calling `winner-redo'). Even though it uses
32 ;; some features of Emacs20.3, winner.el should also work with
33 ;; Emacs19.34 and XEmacs20, provided that the installed version of
34 ;; custom is not obsolete.
36 ;; Winner mode was improved August 1998.
37 ;; Further improvements February 2002.
41 (eval-when-compile (require 'cl-lib
))
43 (defun winner-active-region ()
44 (declare (gv-setter (lambda (store)
45 (if (featurep 'xemacs
)
46 `(if ,store
(zmacs-activate-region)
47 (zmacs-deactivate-region))
48 `(if ,store
(activate-mark) (deactivate-mark))))))
51 (defalias 'winner-edges
52 (if (featurep 'xemacs
) 'window-pixel-edges
'window-edges
))
53 (defalias 'winner-window-list
54 (if (featurep 'xemacs
)
55 (lambda () (delq (minibuffer-window) (window-list nil
0)))
56 (lambda () (window-list nil
0))))
61 "Restoring window configurations."
64 (defcustom winner-dont-bind-my-keys nil
65 "Non-nil means do not bind keys in Winner mode."
69 (defcustom winner-ring-size
200
70 "Maximum number of stored window configurations per frame."
74 (defcustom winner-boring-buffers
'("*Completions*")
75 "List of buffer names whose windows `winner-undo' will not restore.
76 You may want to include buffer names such as *Help*, *Apropos*,
77 *Buffer List*, *info* and *Compile-Log*."
78 :type
'(repeat string
)
83 ;;;; Saving old configurations (internal variables and subroutines)
86 ;;; Current configuration
88 ;; List the windows according to their edges.
89 (defun winner-sorted-window-list ()
90 (sort (winner-window-list)
92 (cl-loop for a in
(winner-edges x
)
93 for b in
(winner-edges y
)
95 finally return
(< a b
)))))
97 (defun winner-win-data ()
98 ;; Essential properties of the windows in the selected frame.
99 (cl-loop for win in
(winner-sorted-window-list)
100 collect
(cons (winner-edges win
) (window-buffer win
))))
102 ;; This variable is updated with the current window configuration
103 ;; every time it changes.
104 (defvar winner-currents nil
)
106 ;; The current configuration (+ the buffers involved).
107 (defsubst winner-conf
()
108 (cons (current-window-configuration)
112 ;; Save current configuration.
113 ;; (Called below by `winner-save-old-configurations').
114 (defun winner-remember ()
115 (setf (alist-get (selected-frame) winner-currents
) (winner-conf)))
117 ;; Consult `winner-currents'.
118 (defun winner-configuration (&optional frame
)
119 (or (cdr (assq (or frame
(selected-frame)) winner-currents
))
120 (with-selected-frame frame
125 ;;; Saved configurations
127 ;; This variable contains the window configuration rings.
128 ;; The key in this alist is the frame.
129 (defvar winner-ring-alist nil
)
131 ;; Find the right ring. If it does not exist, create one.
132 (defsubst winner-ring
(frame)
133 (or (cdr (assq frame winner-ring-alist
))
134 (let ((ring (make-ring winner-ring-size
)))
135 (ring-insert ring
(winner-configuration frame
))
136 (push (cons frame ring
) winner-ring-alist
)
140 ;; If the same command is called several times in a row,
141 ;; we only save one window configuration.
142 (defvar winner-last-command nil
)
144 ;; Frames affected by the previous command.
145 (defvar winner-last-frames nil
)
148 (defsubst winner-equal
(a b
)
149 "Check whether two Winner configurations (as produced by
150 `winner-conf') are equal."
151 (equal (cdr a
) (cdr b
)))
154 ;; Save the current window configuration, if it has changed.
155 ;; If so return frame, otherwise return nil.
156 (defun winner-insert-if-new (frame)
157 (unless (or (memq frame winner-last-frames
)
158 (eq this-command
'winner-redo
))
159 (let ((conf (winner-configuration frame
))
160 (ring (winner-ring frame
)))
161 (when (and (not (ring-empty-p ring
))
162 (winner-equal conf
(ring-ref ring
0)))
163 ;; When the previous configuration was very similar,
164 ;; keep only the latest.
165 (ring-remove ring
0))
166 (ring-insert ring conf
)
167 (push frame winner-last-frames
)
174 ;; Frames affected by the current command.
175 (defvar winner-modified-list nil
)
177 ;; Called whenever the window configuration changes
178 ;; (a `window-configuration-change-hook').
179 (defun winner-change-fun ()
180 (unless (or (memq (selected-frame) winner-modified-list
)
181 (/= 0 (minibuffer-depth)))
182 (push (selected-frame) winner-modified-list
)))
184 ;; A `post-command-hook' for emacsen with
185 ;; `window-configuration-change-hook'.
186 (defun winner-save-old-configurations ()
187 (when (zerop (minibuffer-depth))
188 (unless (eq this-command winner-last-command
)
189 (setq winner-last-frames nil
)
190 (setq winner-last-command this-command
))
191 (dolist (frame winner-modified-list
)
192 (winner-insert-if-new frame
))
193 (setq winner-modified-list nil
)
196 ;; A `minibuffer-setup-hook'.
197 (defun winner-save-unconditionally ()
198 (unless (eq this-command winner-last-command
)
199 (setq winner-last-frames nil
)
200 (setq winner-last-command this-command
))
201 (winner-insert-if-new (selected-frame))
204 ;; A `post-command-hook' for other emacsen.
205 ;; Also called by `winner-undo' before "undoing".
206 (defun winner-save-conditionally ()
207 (when (zerop (minibuffer-depth))
208 (winner-save-unconditionally)))
213 ;;;; Restoring configurations
215 ;; Works almost as `set-window-configuration',
216 ;; but does not change the contents or the size of the minibuffer,
217 ;; and tries to preserve the selected window.
218 (defun winner-set-conf (winconf)
219 (let* ((miniwin (minibuffer-window))
220 (chosen (selected-window))
221 (minisize (window-height miniwin
)))
222 (cl-letf (((window-buffer miniwin
))
223 ((window-point miniwin
)))
224 (set-window-configuration winconf
))
226 ((window-live-p chosen
) (select-window chosen
))
227 ((window-minibuffer-p) (other-window 1)))
228 (when (/= minisize
(window-height miniwin
))
229 (with-selected-window miniwin
230 (setf (window-height) minisize
)))))
234 (defvar winner-point-alist nil
)
235 ;; `set-window-configuration' restores old points and marks. This is
236 ;; not what we want, so we make a list of the "real" (i.e. new) points
237 ;; and marks before undoing window configurations.
239 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
241 (defun winner-make-point-alist ()
244 for win in
(winner-window-list)
246 (or (assq (window-buffer win
) alist
)
247 (car (push (list (set-buffer (window-buffer win
))
248 (cons (mark t
) (winner-active-region)))
250 do
(push (cons win
(window-point win
))
252 finally return alist
)))
254 (defun winner-get-point (buf win
)
255 ;; Consult (and possibly extend) `winner-point-alist'.
256 ;; Returns nil if buf no longer exists.
257 (when (buffer-name buf
)
258 (let ((entry (assq buf winner-point-alist
)))
261 (or (cdr (assq win
(cddr entry
)))
262 (cdr (assq nil
(cddr entry
)))
263 (with-current-buffer buf
264 (push (cons nil
(point)) (cddr entry
))
266 (t (with-current-buffer buf
268 (cons (mark t
) (winner-active-region))
274 ;; Make sure point does not end up in the minibuffer and delete
275 ;; windows displaying dead or boring buffers
276 ;; (c.f. `winner-boring-buffers'). Return nil if all the windows
277 ;; should be deleted. Preserve correct points and marks.
278 (defun winner-set (conf)
279 ;; For the format of `conf', see `winner-conf'.
282 ;; Possibly update `winner-point-alist'
283 (cl-loop for buf in
(mapcar 'cdr
(cdr conf
))
284 for pos
= (winner-get-point buf nil
)
285 if
(and pos
(not (memq buf buffers
)))
286 do
(push buf buffers
)
288 (winner-set-conf (car conf
))
289 (let (xwins) ; to be deleted
292 (dolist (win (winner-sorted-window-list))
293 (unless (and (pop alive
)
294 (setf (window-point win
)
295 (winner-get-point (window-buffer win
) win
))
296 (not (member (buffer-name (window-buffer win
))
297 winner-boring-buffers
)))
298 (push win xwins
))) ; delete this window
302 (cl-loop for buf in buffers
303 for entry
= (cadr (assq buf winner-point-alist
))
304 do
(progn (set-buffer buf
)
305 (set-mark (car entry
))
306 (setf (winner-active-region) (cdr entry
)))))
307 ;; Delete windows, whose buffers are dead or boring.
308 ;; Return t if this is still a possible configuration.
311 (mapc 'delete-window
(cdr xwins
)) ; delete all but one
312 (unless (one-window-p t
)
313 (delete-window (car xwins
))
318 ;;;; Winner mode (a minor mode)
320 (defcustom winner-mode-hook nil
321 "Functions to run whenever Winner mode is turned on or off."
325 (define-obsolete-variable-alias 'winner-mode-leave-hook
326 'winner-mode-off-hook
"24.3")
328 (defcustom winner-mode-off-hook nil
329 "Functions to run whenever Winner mode is turned off."
333 (defvar winner-mode-map
334 (let ((map (make-sparse-keymap)))
335 (unless winner-dont-bind-my-keys
336 (define-key map
[(control c
) left
] 'winner-undo
)
337 (define-key map
[(control c
) right
] 'winner-redo
))
339 "Keymap for Winner mode.")
343 (define-minor-mode winner-mode nil
:global t
; let d-m-m make the doc
346 (add-hook 'window-configuration-change-hook
'winner-change-fun
)
347 (add-hook 'post-command-hook
'winner-save-old-configurations
)
348 (add-hook 'minibuffer-setup-hook
'winner-save-unconditionally
)
349 (setq winner-modified-list
(frame-list))
350 (winner-save-old-configurations))
351 (remove-hook 'window-configuration-change-hook
'winner-change-fun
)
352 (remove-hook 'post-command-hook
'winner-save-old-configurations
)
353 (remove-hook 'minibuffer-setup-hook
'winner-save-unconditionally
)))
355 ;; Inspired by undo (simple.el)
357 (defvar winner-undo-frame nil
)
359 (defvar winner-pending-undo-ring nil
360 "The ring currently used by `winner-undo'.")
361 (defvar winner-undo-counter nil
)
362 (defvar winner-undone-data nil
) ; There confs have been passed.
364 (defun winner-undo ()
365 "Switch back to an earlier window configuration saved by Winner mode.
366 In other words, \"undo\" changes in window configuration."
369 ((not winner-mode
) (error "Winner mode is turned off"))
370 (t (unless (and (eq last-command
'winner-undo
)
371 (eq winner-undo-frame
(selected-frame)))
372 (winner-save-conditionally) ; current configuration->stack
373 (setq winner-undo-frame
(selected-frame))
374 (setq winner-point-alist
(winner-make-point-alist))
375 (setq winner-pending-undo-ring
(winner-ring (selected-frame)))
376 (setq winner-undo-counter
0)
377 (setq winner-undone-data
(list (winner-win-data))))
378 (cl-incf winner-undo-counter
) ; starting at 1
379 (when (and (winner-undo-this)
380 (not (window-minibuffer-p)))
381 (message "Winner undo (%d / %d)"
383 (1- (ring-length winner-pending-undo-ring
)))))))
388 (defun winner-undo-this () ; The heart of winner undo.
391 ((>= winner-undo-counter
(ring-length winner-pending-undo-ring
))
392 (message "No further window configuration undo information")
395 ((and ; If possible configuration
396 (winner-set (ring-ref winner-pending-undo-ring
397 winner-undo-counter
))
398 ; .. and new configuration
399 (let ((data (winner-win-data)))
400 (and (not (member data winner-undone-data
))
401 (push data winner-undone-data
))))
402 (cl-return t
)) ; .. then everything is fine.
403 (t ;; Otherwise, discharge it (and try the next one).
404 (ring-remove winner-pending-undo-ring winner-undo-counter
)))))
407 (defun winner-redo () ; If you change your mind.
408 "Restore a more recent window configuration saved by Winner mode."
411 ((eq last-command
'winner-undo
)
413 (if (zerop (minibuffer-depth))
414 (ring-remove winner-pending-undo-ring
0)
415 (ring-ref winner-pending-undo-ring
0)))
416 (unless (eq (selected-window) (minibuffer-window))
417 (message "Winner undid undo")))
418 (t (error "Previous command was not a `winner-undo'"))))
421 ;;; winner.el ends here