1 ;;; winner.el --- Restore old window configurations
3 ;; Copyright (C) 1997, 1998, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation. Inc.
6 ;; Author: Ivar Rummelhoff <ivarru@math.uio.no>
7 ;; Created: 27 Feb 1997
8 ;; Keywords: convenience frames
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Winner mode is a global minor mode that records the changes in the
30 ;; window configuration (i.e. how the frames are partitioned into
31 ;; windows) so that the changes can be "undone" using the command
32 ;; `winner-undo'. By default this one is bound to the key sequence
33 ;; ctrl-c left. If you change your mind (while undoing), you can
34 ;; press ctrl-c right (calling `winner-redo'). Even though it uses
35 ;; some features of Emacs20.3, winner.el should also work with
36 ;; Emacs19.34 and XEmacs20, provided that the installed version of
37 ;; custom is not obsolete.
39 ;; Winner mode was improved August 1998.
40 ;; Further improvements February 2002.
48 (defmacro winner-active-region
()
49 (if (boundp 'mark-active
)
53 (defsetf winner-active-region
() (store)
54 (if (featurep 'xemacs
)
55 `(if ,store
(zmacs-activate-region)
56 (zmacs-deactivate-region))
57 `(setq mark-active
,store
)))
59 (defalias 'winner-edges
60 (if (featurep 'xemacs
) 'window-pixel-edges
'window-edges
))
61 (defalias 'winner-window-list
62 (if (featurep 'xemacs
)
63 (lambda () (delq (minibuffer-window) (window-list nil
0)))
64 (lambda () (window-list nil
0))))
69 "Restoring window configurations."
73 (defcustom winner-mode nil
75 Setting this variable directly does not take effect;
76 use either \\[customize] or the function `winner-mode'."
77 :set
#'(lambda (symbol value
) (funcall symbol
(or value
0)))
78 :initialize
'custom-initialize-default
83 (defcustom winner-dont-bind-my-keys nil
84 "If non-nil: Do not use `winner-mode-map' in Winner mode."
88 (defcustom winner-ring-size
200
89 "Maximum number of stored window configurations per frame."
93 (defcustom winner-boring-buffers
'("*Completions*")
94 "`winner-undo' will not restore windows displaying any of these buffers.
95 You may want to include buffer names such as *Help*, *Apropos*,
96 *Buffer List*, *info* and *Compile-Log*."
97 :type
'(repeat string
)
104 ;;;; Saving old configurations (internal variables and subroutines)
107 ;;; Current configuration
109 ;; List the windows according to their edges.
110 (defun winner-sorted-window-list ()
111 (sort (winner-window-list)
113 (loop for a in
(winner-edges x
)
114 for b in
(winner-edges y
)
116 finally return
(< a b
)))))
118 (defun winner-win-data ()
119 ;; Essential properties of the windows in the selected frame.
120 (loop for win in
(winner-sorted-window-list)
121 collect
(cons (winner-edges win
) (window-buffer win
))))
123 ;; This variable is updated with the current window configuration
124 ;; every time it changes.
125 (defvar winner-currents nil
)
127 ;; The current configuration (+ the buffers involved).
128 (defsubst winner-conf
()
129 (cons (current-window-configuration)
133 ;; Save current configuration.
134 ;; (Called below by `winner-save-old-configurations').
135 (defun winner-remember ()
136 (let ((entry (assq (selected-frame) winner-currents
)))
137 (if entry
(setcdr entry
(winner-conf))
138 (push (cons (selected-frame) (winner-conf))
141 ;; Consult `winner-currents'.
142 (defun winner-configuration (&optional frame
)
143 (or (cdr (assq (or frame
(selected-frame)) winner-currents
))
144 (letf (((selected-frame) frame
))
149 ;;; Saved configurations
151 ;; This variable contains the window cofiguration rings.
152 ;; The key in this alist is the frame.
153 (defvar winner-ring-alist nil
)
155 ;; Find the right ring. If it does not exist, create one.
156 (defsubst winner-ring
(frame)
157 (or (cdr (assq frame winner-ring-alist
))
158 (let ((ring (make-ring winner-ring-size
)))
159 (ring-insert ring
(winner-configuration frame
))
160 (push (cons frame ring
) winner-ring-alist
)
164 ;; If the same command is called several times in a row,
165 ;; we only save one window configuration.
166 (defvar winner-last-command nil
)
168 ;; Frames affected by the previous command.
169 (defvar winner-last-frames nil
)
172 (defsubst winner-equal
(a b
)
173 "Check whether two Winner configurations (as produced by
174 `winner-conf') are equal."
175 (equal (cdr a
) (cdr b
)))
178 ;; Save the current window configuration, if it has changed.
179 ;; If so return frame, otherwise return nil.
180 (defun winner-insert-if-new (frame)
181 (unless (or (memq frame winner-last-frames
)
182 (eq this-command
'winner-redo
))
183 (let ((conf (winner-configuration frame
))
184 (ring (winner-ring frame
)))
185 (when (and (not (ring-empty-p ring
))
186 (winner-equal conf
(ring-ref ring
0)))
187 ;; When the previous configuration was very similar,
188 ;; keep only the latest.
189 (ring-remove ring
0))
190 (ring-insert ring conf
)
191 (push frame winner-last-frames
)
198 ;; Frames affected by the current command.
199 (defvar winner-modified-list nil
)
201 ;; Called whenever the window configuration changes
202 ;; (a `window-configuration-change-hook').
203 (defun winner-change-fun ()
204 (unless (or (memq (selected-frame) winner-modified-list
)
205 (/= 0 (minibuffer-depth)))
206 (push (selected-frame) winner-modified-list
)))
208 ;; A `post-command-hook' for emacsen with
209 ;; `window-configuration-change-hook'.
210 (defun winner-save-old-configurations ()
211 (when (zerop (minibuffer-depth))
212 (unless (eq this-command winner-last-command
)
213 (setq winner-last-frames nil
)
214 (setq winner-last-command this-command
))
215 (dolist (frame winner-modified-list
)
216 (winner-insert-if-new frame
))
217 (setq winner-modified-list nil
)
220 ;; A `minibuffer-setup-hook'.
221 (defun winner-save-unconditionally ()
222 (unless (eq this-command winner-last-command
)
223 (setq winner-last-frames nil
)
224 (setq winner-last-command this-command
))
225 (winner-insert-if-new (selected-frame))
228 ;; A `post-command-hook' for other emacsen.
229 ;; Also called by `winner-undo' before "undoing".
230 (defun winner-save-conditionally ()
231 (when (zerop (minibuffer-depth))
232 (winner-save-unconditionally)))
237 ;;;; Restoring configurations
239 ;; Works almost as `set-window-configuration',
240 ;; but does not change the contents or the size of the minibuffer,
241 ;; and tries to preserve the selected window.
242 (defun winner-set-conf (winconf)
243 (let* ((miniwin (minibuffer-window))
244 (chosen (selected-window))
245 (minisize (window-height miniwin
)))
246 (letf (((window-buffer miniwin
))
247 ((window-point miniwin
)))
248 (set-window-configuration winconf
))
250 ((window-live-p chosen
) (select-window chosen
))
251 ((window-minibuffer-p (selected-window))
253 (when (/= minisize
(window-height miniwin
))
254 (letf (((selected-window) miniwin
) )
255 (setf (window-height) minisize
)))))
259 (defvar winner-point-alist nil
)
260 ;; `set-window-configuration' restores old points and marks. This is
261 ;; not what we want, so we make a list of the "real" (i.e. new) points
262 ;; and marks before undoing window configurations.
264 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
266 (defun winner-make-point-alist ()
267 (letf (((current-buffer)))
269 for win in
(winner-window-list)
271 (or (assq (window-buffer win
) alist
)
272 (car (push (list (set-buffer (window-buffer win
))
273 (cons (mark t
) (winner-active-region)))
275 do
(push (cons win
(window-point win
))
277 finally return alist
)))
279 (defun winner-get-point (buf win
)
280 ;; Consult (and possibly extend) `winner-point-alist'.
281 ;; Returns nil if buf no longer exists.
282 (when (buffer-name buf
)
283 (let ((entry (assq buf winner-point-alist
)))
286 (or (cdr (assq win
(cddr entry
)))
287 (cdr (assq nil
(cddr entry
)))
288 (letf (((current-buffer) buf
))
289 (push (cons nil
(point)) (cddr entry
))
291 (t (letf (((current-buffer) buf
))
293 (cons (mark t
) (winner-active-region))
299 ;; Make sure point does not end up in the minibuffer and delete
300 ;; windows displaying dead or boring buffers
301 ;; (c.f. `winner-boring-buffers'). Return nil if all the windows
302 ;; should be deleted. Preserve correct points and marks.
303 (defun winner-set (conf)
304 ;; For the format of `conf', see `winner-conf'.
307 ;; Possibly update `winner-point-alist'
308 (loop for buf in
(mapcar 'cdr
(cdr conf
))
309 for pos
= (winner-get-point buf nil
)
310 if
(and pos
(not (memq buf buffers
)))
311 do
(push buf buffers
)
313 (winner-set-conf (car conf
))
314 (let (xwins) ; to be deleted
317 (dolist (win (winner-sorted-window-list))
318 (unless (and (pop alive
)
319 (setf (window-point win
)
320 (winner-get-point (window-buffer win
) win
))
321 (not (member (buffer-name (window-buffer win
))
322 winner-boring-buffers
)))
323 (push win xwins
))) ; delete this window
326 (letf (((current-buffer)))
327 (loop for buf in buffers
328 for entry
= (cadr (assq buf winner-point-alist
))
329 do
(progn (set-buffer buf
)
330 (set-mark (car entry
))
331 (setf (winner-active-region) (cdr entry
)))))
332 ;; Delete windows, whose buffers are dead or boring.
333 ;; Return t if this is still a possible configuration.
336 (mapc 'delete-window
(cdr xwins
)) ; delete all but one
337 (unless (one-window-p t
)
338 (delete-window (car xwins
))
343 ;;;; Winner mode (a minor mode)
345 (defcustom winner-mode-hook nil
346 "Functions to run whenever Winner mode is turned on."
350 (defcustom winner-mode-leave-hook nil
351 "Functions to run whenever Winner mode is turned off."
355 (defvar winner-mode-map
356 (let ((map (make-sparse-keymap)))
357 (define-key map
[(control c
) left
] 'winner-undo
)
358 (define-key map
[(control c
) right
] 'winner-redo
)
360 "Keymap for Winner mode.")
362 ;; Check if `window-configuration-change-hook' is working.
363 (defun winner-hook-installed-p ()
364 (save-window-excursion
365 (let ((winner-var nil
)
366 (window-configuration-change-hook
367 '((lambda () (setq winner-var t
)))))
373 (defun winner-mode (&optional arg
)
375 With arg, turn Winner mode on if and only if arg is positive."
377 (let ((on-p (if arg
(> (prefix-numeric-value arg
) 0)
384 ((winner-hook-installed-p)
385 (add-hook 'window-configuration-change-hook
'winner-change-fun
)
386 (add-hook 'post-command-hook
'winner-save-old-configurations
))
387 (t (add-hook 'post-command-hook
'winner-save-conditionally
)))
388 (add-hook 'minibuffer-setup-hook
'winner-save-unconditionally
)
389 (setq winner-modified-list
(frame-list))
390 (winner-save-old-configurations)
391 (run-hooks 'winner-mode-hook
)
392 (when (interactive-p) (message "Winner mode enabled")))
395 (setq winner-mode nil
)
396 (remove-hook 'window-configuration-change-hook
'winner-change-fun
)
397 (remove-hook 'post-command-hook
'winner-save-old-configurations
)
398 (remove-hook 'post-command-hook
'winner-save-conditionally
)
399 (remove-hook 'minibuffer-setup-hook
'winner-save-unconditionally
)
400 (run-hooks 'winner-mode-leave-hook
)
401 (when (interactive-p) (message "Winner mode disabled"))))))
403 ;; Inspired by undo (simple.el)
405 (defvar winner-undo-frame nil
)
407 (defvar winner-pending-undo-ring nil
408 "The ring currently used by `winner-undo'.")
409 (defvar winner-undo-counter nil
)
410 (defvar winner-undone-data nil
) ; There confs have been passed.
412 (defun winner-undo ()
413 "Switch back to an earlier window configuration saved by Winner mode.
414 In other words, \"undo\" changes in window configuration."
417 ((not winner-mode
) (error "Winner mode is turned off"))
418 (t (unless (and (eq last-command
'winner-undo
)
419 (eq winner-undo-frame
(selected-frame)))
420 (winner-save-conditionally) ; current configuration->stack
421 (setq winner-undo-frame
(selected-frame))
422 (setq winner-point-alist
(winner-make-point-alist))
423 (setq winner-pending-undo-ring
(winner-ring (selected-frame)))
424 (setq winner-undo-counter
0)
425 (setq winner-undone-data
(list (winner-win-data))))
426 (incf winner-undo-counter
) ; starting at 1
427 (when (and (winner-undo-this)
428 (not (window-minibuffer-p (selected-window))))
429 (message "Winner undo (%d / %d)"
431 (1- (ring-length winner-pending-undo-ring
)))))))
436 (defun winner-undo-this () ; The heart of winner undo.
439 ((>= winner-undo-counter
(ring-length winner-pending-undo-ring
))
440 (message "No further window configuration undo information")
443 ((and ; If possible configuration
444 (winner-set (ring-ref winner-pending-undo-ring
445 winner-undo-counter
))
446 ; .. and new configuration
447 (let ((data (winner-win-data)))
448 (and (not (member data winner-undone-data
))
449 (push data winner-undone-data
))))
450 (return t
)) ; .. then everything is fine.
451 (t ;; Otherwise, discharge it (and try the next one).
452 (ring-remove winner-pending-undo-ring winner-undo-counter
)))))
455 (defun winner-redo () ; If you change your mind.
456 "Restore a more recent window configuration saved by Winner mode."
459 ((eq last-command
'winner-undo
)
461 (if (zerop (minibuffer-depth))
462 (ring-remove winner-pending-undo-ring
0)
463 (ring-ref winner-pending-undo-ring
0)))
464 (unless (eq (selected-window) (minibuffer-window))
465 (message "Winner undid undo")))
466 (t (error "Previous command was not a `winner-undo'"))))
468 ;;; To be evaluated when the package is loaded:
470 (unless (or (assq 'winner-mode minor-mode-map-alist
)
471 winner-dont-bind-my-keys
)
472 (push (cons 'winner-mode winner-mode-map
)
473 minor-mode-map-alist
))
476 ;; arch-tag: 686d1c1b-010e-42ca-a192-b5685112418f
477 ;;; winner.el ends here