1 ;;; winner.el --- Restore old window configurations
3 ;; Copyright (C) 1997, 1998, 2001 Free Software Foundation. Inc.
5 ;; Author: Ivar Rummelhoff <ivarru@math.uio.no>
6 ;; Created: 27 Feb 1997
7 ;; Time-stamp: <2002-02-20 22:06:58 ivarru>
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 2, 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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, 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-x left. If you change your mind (while undoing), you can
34 ;; press ctrl-x 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.
49 ((eq (aref (emacs-version) 0) ?X)
50 (defmacro winner-active-region ()
52 (defsetf winner-active-region () (store)
53 `(if ,store (zmacs-activate-region)
54 (zmacs-deactivate-region))))
55 (t (defmacro winner-active-region ()
57 (defsetf winner-active-region () (store)
58 `(setq mark-active ,store)))) )
62 ((eq (aref (emacs-version) 0) ?X)
63 (defalias 'winner-edges 'window-pixel-edges)
64 (defsubst winner-window-list ()
65 (remq (minibuffer-window)
66 (window-list nil 0))))
67 (t (defalias 'winner-edges 'window-edges)
68 (defsubst winner-window-list ()
69 (window-list nil 0)))) )
73 (when (fboundp 'defgroup)
75 "Restoring window configurations."
78 (unless (fboundp 'defcustom)
79 (defmacro defcustom (symbol &optional initvalue docs &rest rest)
80 (list 'defvar symbol initvalue docs)))
83 (defcustom winner-mode nil
85 Setting this variable directly does not take effect;
86 use either \\[customize] or the function `winner-mode'."
87 :set #'(lambda (symbol value)
88 (winner-mode (or value 0)))
89 :initialize 'custom-initialize-default
94 (defcustom winner-dont-bind-my-keys nil
95 "If non-nil: Do not use `winner-mode-map' in Winner mode."
99 (defcustom winner-ring-size 200
100 "Maximum number of stored window configurations per frame."
104 (defcustom winner-boring-buffers '("*Completions*")
105 "`winner-undo' will not restore windows displaying any of these \
107 You may want to include buffer names such as *Help*, *Apropos*,
108 *Buffer List*, *info* and *Compile-Log*."
109 :type '(repeat string)
115 \f;;;; Saving old configurations (internal variables and subroutines)
118 ;;; Current configuration
120 ;; List the windows according to their edges.
121 (defun winner-sorted-window-list ()
122 (sort (winner-window-list)
124 (loop for a in (winner-edges x)
125 for b in (winner-edges y)
127 finally return (< a b)))))
129 (defun winner-win-data ()
130 ;; Essential properties of the windows in the selected frame.
131 (loop for win in (winner-sorted-window-list)
132 collect (cons (winner-edges win) (window-buffer win))))
134 ;; This variable is updated with the current window configuration
135 ;; every time it changes.
136 (defvar winner-currents nil)
138 ;; The current configuration (+ the buffers involved).
139 (defsubst winner-conf ()
140 (cons (current-window-configuration)
144 ;; Save current configuration.
145 ;; (Called below by `winner-save-old-configurations').
146 (defun winner-remember ()
147 (let ((entry (assq (selected-frame) winner-currents)))
148 (if entry (setcdr entry (winner-conf))
149 (push (cons (selected-frame) (winner-conf))
152 ;; Consult `winner-currents'.
153 (defun winner-configuration (&optional frame)
154 (or (cdr (assq (or frame (selected-frame)) winner-currents))
155 (letf (((selected-frame) frame))
160 ;;; Saved configurations
162 ;; This variable contains the window cofiguration rings.
163 ;; The key in this alist is the frame.
164 (defvar winner-ring-alist nil)
166 ;; Find the right ring. If it does not exist, create one.
167 (defsubst winner-ring (frame)
168 (or (cdr (assq frame winner-ring-alist))
170 (let ((ring (make-ring winner-ring-size)))
171 (ring-insert ring (winner-configuration frame))
172 (push (cons frame ring) winner-ring-alist)
175 \f;; If the same command is called several times in a row,
176 ;; we only save one window configuration.
177 (defvar winner-last-command nil)
179 ;; Frames affected by the previous command.
180 (defvar winner-last-frames nil)
183 (defun winner-equal (a b)
184 "Check whether two Winner configurations (as produced by
185 `winner-conf') are equal."
186 (equal (cdr a) (cdr b)))
189 ;; Save the current window configuration, if it has changed.
190 ;; If so return frame, otherwise return nil.
191 (defun winner-insert-if-new (frame)
192 (unless (or (memq frame winner-last-frames)
193 (eq this-command 'winner-redo))
194 (let ((conf (winner-configuration frame))
195 (ring (winner-ring frame)))
196 (when (and (not (ring-empty-p ring))
197 (winner-equal conf (ring-ref ring 0)))
198 ;; When the previous configuration was very similar,
199 ;; keep only the latest.
200 (ring-remove ring 0))
201 (ring-insert ring conf)
202 (push frame winner-last-frames)
209 ;; Frames affected by the current command.
210 (defvar winner-modified-list nil)
212 ;; Called whenever the window configuration changes
213 ;; (a `window-configuration-change-hook').
214 (defun winner-change-fun ()
215 (unless (or (memq (selected-frame) winner-modified-list)
216 (/= 0 (minibuffer-depth)))
217 (push (selected-frame) winner-modified-list)))
219 ;; A `post-command-hook' for emacsen with
220 ;; `window-configuration-change-hook'.
221 (defun winner-save-old-configurations ()
222 (when (zerop (minibuffer-depth))
223 (unless (eq this-command winner-last-command)
224 (setq winner-last-frames nil)
225 (setq winner-last-command this-command))
226 (dolist (frame winner-modified-list)
227 (winner-insert-if-new frame))
228 (setq winner-modified-list nil)
231 ;; A `minibuffer-setup-hook'.
232 (defun winner-save-unconditionally ()
233 (unless (eq this-command winner-last-command)
234 (setq winner-last-frames nil)
235 (setq winner-last-command this-command))
236 (winner-insert-if-new (selected-frame))
239 ;; A `post-command-hook' for other emacsen.
240 ;; Also called by `winner-undo' before "undoing".
241 (defun winner-save-conditionally ()
242 (when (zerop (minibuffer-depth))
243 (winner-save-unconditionally)))
247 \f;;;; Restoring configurations
249 ;; Works almost as `set-window-configuration',
250 ;; but does not change the contents or the size of the minibuffer,
251 ;; and tries to preserve the selected window.
252 (defun winner-set-conf (winconf)
253 (let* ((miniwin (minibuffer-window))
254 (chosen (selected-window))
255 (minisize (window-height miniwin)))
256 (letf (((window-buffer miniwin))
257 ((window-point miniwin)))
258 (set-window-configuration winconf))
260 ((window-live-p chosen) (select-window chosen))
261 ((window-minibuffer-p (selected-window))
263 (when (/= minisize (window-height miniwin))
264 (letf (((selected-window) miniwin) )
265 (setf (window-height) minisize)))))
269 (defvar winner-point-alist nil)
270 ;; `set-window-configuration' restores old points and marks. This is
271 ;; not what we want, so we make a list of the "real" (i.e. new) points
272 ;; and marks before undoing window configurations.
274 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
276 (defun winner-make-point-alist ()
277 (letf (((current-buffer)))
279 for win in (winner-window-list)
281 (or (assq (window-buffer win) alist)
282 (car (push (list (set-buffer (window-buffer win))
283 (cons (mark t) (winner-active-region)))
285 do (push (cons win (window-point win))
287 finally return alist)))
289 (defun winner-get-point (buf win)
290 ;; Consult (and possibly extend) `winner-point-alist'.
291 ;; Returns nil iff buf no longer exists.
292 (when (buffer-name buf)
293 (let ((entry (assq buf winner-point-alist)))
296 (or (cdr (assq win (cddr entry)))
297 (cdr (assq nil (cddr entry)))
298 (letf (((current-buffer) buf))
299 (push (cons nil (point)) (cddr entry))
301 (t (letf (((current-buffer) buf))
303 (cons (mark t) (winner-active-region))
308 \f;; Make sure point does not end up in the minibuffer and delete
309 ;; windows displaying dead or boring buffers
310 ;; (c.f. `winner-boring-buffers'). Return nil iff all the windows
311 ;; should be deleted. Preserve correct points and marks.
312 (defun winner-set (conf)
313 ;; For the format of `conf', see `winner-conf'.
316 ;; Possibly update `winner-point-alist'
317 (loop for buf in (mapcar 'cdr (cdr conf))
318 for pos = (winner-get-point buf nil)
319 if (and pos (not (memq buf buffers)))
320 do (push buf buffers)
322 (winner-set-conf (car conf))
323 (let (xwins) ; to be deleted
326 (dolist (win (winner-sorted-window-list))
327 (unless (and (pop alive)
328 (setf (window-point win)
329 (winner-get-point (window-buffer win) win))
330 (not (member (buffer-name (window-buffer win))
331 winner-boring-buffers)))
332 (push win xwins))) ; delete this window
335 (letf (((current-buffer)))
336 (loop for buf in buffers
337 for entry = (cadr (assq buf winner-point-alist))
338 do (progn (set-buffer buf)
339 (set-mark (car entry))
340 (setf (winner-active-region) (cdr entry)))))
341 ;; Delete windows, whose buffers are dead or boring.
342 ;; Return t if this is still a possible configuration.
345 (mapc 'delete-window (cdr xwins)) ; delete all but one
346 (unless (one-window-p t)
347 (delete-window (car xwins))
352 ;;;; Winner mode (a minor mode)
354 (defcustom winner-mode-hook nil
355 "Functions to run whenever Winner mode is turned on."
359 (defcustom winner-mode-leave-hook nil
360 "Functions to run whenever Winner mode is turned off."
364 (defvar winner-mode-map nil "Keymap for Winner mode.")
366 ;; Check if `window-configuration-change-hook' is working.
367 (defun winner-hook-installed-p ()
368 (save-window-excursion
369 (let ((winner-var nil)
370 (window-configuration-change-hook
371 '((lambda () (setq winner-var t)))))
377 (defun winner-mode (&optional arg)
379 With arg, turn Winner mode on if and only if arg is positive."
381 (let ((on-p (if arg (> (prefix-numeric-value arg) 0)
388 ((winner-hook-installed-p)
389 (add-hook 'window-configuration-change-hook 'winner-change-fun)
390 (add-hook 'post-command-hook 'winner-save-old-configurations))
391 (t (add-hook 'post-command-hook 'winner-save-conditionally)))
392 (add-hook 'minibuffer-setup-hook 'winner-save-unconditionally)
393 (setq winner-modified-list (frame-list))
394 (winner-save-old-configurations)
395 (run-hooks 'winner-mode-hook)
396 (when (interactive-p) (message "Winner mode enabled")))
399 (setq winner-mode nil)
400 (remove-hook 'window-configuration-change-hook 'winner-change-fun)
401 (remove-hook 'post-command-hook 'winner-save-old-configurations)
402 (remove-hook 'post-command-hook 'winner-save-conditionally)
403 (remove-hook 'minibuffer-setup-hook 'winner-save-unconditionally)
404 (run-hooks 'winner-mode-leave-hook)
405 (when (interactive-p) (message "Winner mode disabled"))))))
407 ;; Inspired by undo (simple.el)
409 (defvar winner-undo-frame nil)
411 (defvar winner-pending-undo-ring nil
412 "The ring currently used by winner undo.")
413 (defvar winner-undo-counter nil)
414 (defvar winner-undone-data nil) ; There confs have been passed.
416 (defun winner-undo ()
417 "Switch back to an earlier window configuration saved by Winner mode.
418 In other words, \"undo\" changes in window configuration."
421 ((not winner-mode) (error "Winner mode is turned off"))
422 (t (unless (and (eq last-command 'winner-undo)
423 (eq winner-undo-frame (selected-frame)))
424 (winner-save-conditionally) ; current configuration->stack
425 (setq winner-undo-frame (selected-frame))
426 (setq winner-point-alist (winner-make-point-alist))
427 (setq winner-pending-undo-ring (winner-ring (selected-frame)))
428 (setq winner-undo-counter 0)
429 (setq winner-undone-data (list (winner-win-data))))
430 (incf winner-undo-counter) ; starting at 1
431 (when (and (winner-undo-this)
432 (not (window-minibuffer-p (selected-window))))
433 (message "Winner undo (%d / %d)"
435 (1- (ring-length winner-pending-undo-ring)))))))
439 \f(defun winner-undo-this () ; The heart of winner undo.
442 ((>= winner-undo-counter (ring-length winner-pending-undo-ring))
443 (message "No further window configuration undo information")
446 ((and ; If possible configuration
447 (winner-set (ring-ref winner-pending-undo-ring
448 winner-undo-counter))
449 ; .. and new configuration
450 (let ((data (winner-win-data)))
451 (and (not (member data winner-undone-data))
452 (push data winner-undone-data))))
453 (return t)) ; .. then everything is fine.
454 (t ;; Otherwise, discharge it (and try the next one).
455 (ring-remove winner-pending-undo-ring winner-undo-counter)))))
458 (defun winner-redo () ; If you change your mind.
459 "Restore a more recent window configuration saved by Winner mode."
462 ((eq last-command 'winner-undo)
464 (if (zerop (minibuffer-depth))
465 (ring-remove winner-pending-undo-ring 0)
466 (ring-ref winner-pending-undo-ring 0)))
467 (unless (eq (selected-window) (minibuffer-window))
468 (message "Winner undid undo")))
469 (t (error "Previous command was not a winner-undo"))))
471 ;;; To be evaluated when the package is loaded:
473 (unless winner-mode-map
474 (setq winner-mode-map (make-sparse-keymap))
475 (define-key winner-mode-map [(control x) left] 'winner-undo)
476 (define-key winner-mode-map [(control x) right] 'winner-redo))
478 (unless (or (assq 'winner-mode minor-mode-map-alist)
479 winner-dont-bind-my-keys)
480 (push (cons 'winner-mode winner-mode-map)
481 minor-mode-map-alist))
485 ;;; winner.el ends here