1 ;;; winner.el --- Restore old window configurations
3 ;; Copyright (C) 1997, 1998 Free Software Foundation. Inc.
5 ;; Author: Ivar Rummelhoff <ivarr@ifi.uio.no>
6 ;; Maintainer: Ivar Rummelhoff <ivarr@ifi.uio.no>
7 ;; Created: 27 Feb 1997
8 ;; Time-stamp: <1998-08-21 19:51:02 ivarr>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; Winner mode is a global minor mode that records the changes in the
31 ;; window configuration (i.e. how the frames are partitioned into
32 ;; windows) so that the changes can be "undone" using the command
33 ;; `winner-undo'. By default this one is bound to the key sequence
34 ;; ctrl-x left. If you change your mind (while undoing), you can
35 ;; press ctrl-x right (calling `winner-redo'). Even though it uses
36 ;; some features of Emacs20.3, winner.el should also work with
37 ;; Emacs19.34 and XEmacs20, provided that the installed version of
38 ;; custom is not obsolete.
40 ;; Winner mode was improved august 1998.
48 ((eq (aref (emacs-version) 0) ?X
)
49 (defmacro winner-active-region
()
51 (defsetf winner-active-region
() (store)
52 `(if ,store
(zmacs-activate-region)
53 (zmacs-deactivate-region))))
54 (t (defmacro winner-active-region
()
56 (defsetf winner-active-region
() (store)
57 `(setq mark-active
,store
)))) )
61 (when (fboundp 'defgroup
)
63 "Restoring window configurations."
66 (unless (fboundp 'defcustom
)
67 (defmacro defcustom
(symbol &optional initvalue docs
&rest rest
)
68 (list 'defvar symbol initvalue docs
)))
71 (defcustom winner-mode nil
73 Setting this variable directly does not take effect;
74 use either \\[customize] or the function `winner-mode'."
75 :set
#'(lambda (symbol value
)
76 (winner-mode (or value
0)))
77 :initialize
'custom-initialize-default
82 (defcustom winner-dont-bind-my-keys nil
83 "If non-nil: Do not use `winner-mode-map' in Winner mode."
87 (defcustom winner-ring-size
200
88 "Maximum number of stored window configurations per frame."
95 ;;;; Saving old configurations (internal variables and subroutines)
97 ;; This variable is updated with the current window configuration
98 ;; after every command, so that when command make changes in the
99 ;; window configuration, the last configuration can be saved.
100 (defvar winner-currents nil
)
102 ;; The current configuration (+ the buffers involved).
103 (defsubst winner-conf
()
104 (list (current-window-configuration)
105 (loop for w being the windows
106 unless
(window-minibuffer-p w
)
107 collect
(window-buffer w
)) ))
108 ;; (if winner-testvar (incf winner-testvar) ; For debugging purposes
109 ;; (setq winner-testvar 0))))
111 ;; Save current configuration.
112 ;; (Called by `winner-save-old-configurations' below).
113 (defun winner-remember ()
114 (let ((entry (assq (selected-frame) winner-currents
)))
115 (if entry
(setcdr entry
(winner-conf))
116 (push (cons (selected-frame) (winner-conf))
119 ;; Consult `winner-currents'.
120 (defun winner-configuration (&optional frame
)
121 (or (cdr (assq (or frame
(selected-frame)) winner-currents
))
122 (letf (((selected-frame) frame
))
127 ;; This variable contains the window cofiguration 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
))
135 (let ((ring (make-ring winner-ring-size
)))
136 (ring-insert ring
(winner-configuration frame
))
137 (push (cons frame ring
) winner-ring-alist
)
140 \f;; 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
)
147 ;; Save the current window configuration, if it has changed.
148 ;; Then return frame, else return nil.
149 (defun winner-insert-if-new (frame)
150 (unless (or (memq frame winner-last-frames
)
151 (eq this-command
'winner-redo
))
152 (let ((conf (winner-configuration frame
))
153 (ring (winner-ring frame
)))
154 (when (and (not (ring-empty-p ring
))
155 (winner-equal conf
(ring-ref ring
0)))
156 (ring-remove ring
0))
157 (ring-insert ring conf
)
158 (push frame winner-last-frames
)
161 ;; Frames affected by the current command.
162 (defvar winner-modified-list nil
)
164 ;; Called whenever the window configuration changes
165 ;; (a `window-configuration-change-hook').
166 (defun winner-change-fun ()
167 (unless (memq (selected-frame) winner-modified-list
)
168 (push (selected-frame) winner-modified-list
)))
171 ;; For Emacs20 (a `post-command-hook').
172 (defun winner-save-old-configurations ()
173 (unless (eq this-command winner-last-command
)
174 (setq winner-last-frames nil
)
175 (setq winner-last-command this-command
))
176 (dolist (frame winner-modified-list
)
177 (winner-insert-if-new frame
))
178 (setq winner-modified-list nil
)
179 ;; (ir-trace ; For debugging purposes
181 ;; (loop with ring = (winner-ring (selected-frame))
182 ;; for i from 0 to (1- (ring-length ring))
183 ;; collect (caddr (ring-ref ring i))))
186 ;; For compatibility with other emacsen
187 ;; and called by `winner-undo' before "undoing".
188 (defun winner-save-unconditionally ()
189 (unless (eq this-command winner-last-command
)
190 (setq winner-last-frames nil
)
191 (setq winner-last-command this-command
))
192 (winner-insert-if-new (selected-frame))
198 \f;;;; Restoring configurations
200 ;; Works almost as `set-window-configuration',
201 ;; but doesn't change the contents or the size of the minibuffer.
202 (defun winner-set-conf (winconf)
203 (let ((miniwin (minibuffer-window))
204 (minisel (window-minibuffer-p (selected-window))))
205 (let ((minibuf (window-buffer miniwin
))
206 (minipoint (window-point miniwin
))
207 (minisize (window-height miniwin
)))
208 (set-window-configuration winconf
)
209 (setf (window-buffer miniwin
) minibuf
210 (window-point miniwin
) minipoint
)
211 (when (/= minisize
(window-height miniwin
))
212 (letf (((selected-window) miniwin
) )
213 ;; Clumsy due to cl-macs-limitation
214 (setf (window-height) minisize
)))
216 (minisel (select-window miniwin
))
217 ((window-minibuffer-p (selected-window))
218 (other-window 1))))))
221 (defvar winner-point-alist nil
)
222 ;; `set-window-configuration' restores old points and marks. This is
223 ;; not what we want, so we make a list of the "real" (i.e. new) points
224 ;; and marks before undoing window configurations.
226 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
228 (defun winner-make-point-alist ()
229 (letf (((current-buffer)))
232 for win being the windows
234 ((window-minibuffer-p win
))
235 ((setq entry
(assq win alist
))
236 ;; Update existing entry
237 (push (cons win
(window-point win
))
239 (t;; Else create new entry
240 (push (list (set-buffer (window-buffer win
))
241 (cons (mark t
) (winner-active-region))
242 (cons win
(window-point win
)))
244 finally return alist
)))
247 (defun winner-get-point (buf win
)
248 ;; Consult (and possibly extend) `winner-point-alist'.
249 (when (buffer-name buf
)
250 (let ((entry (assq buf winner-point-alist
)))
253 (or (cdr (assq win
(cddr entry
)))
254 (cdr (assq nil
(cddr entry
)))
255 (letf (((current-buffer) buf
))
256 (push (cons nil
(point)) (cddr entry
))
258 (t (letf (((current-buffer) buf
))
260 (cons (mark t
) (winner-active-region))
265 \f;; Make sure point doesn't end up in the minibuffer and
266 ;; delete windows displaying dead buffers. Return nil
267 ;; if and only if all the windows should have been deleted.
268 ;; Do not move neither points nor marks.
269 (defun winner-set (conf)
272 (loop for buf in
(cadr conf
)
273 for pos
= (winner-get-point buf nil
)
274 if
(and pos
(not (memq buf buffers
)))
275 do
(push buf buffers
)
277 (winner-set-conf (car conf
))
278 (let (xwins) ; These windows should be deleted
279 (loop for win being the windows
280 unless
(window-minibuffer-p win
)
281 do
(if (pop origpoints
)
282 (setf (window-point win
)
287 (push win xwins
))) ; delete this window
289 (letf (((current-buffer)))
290 (loop for buf in buffers
291 for entry
= (cadr (assq buf winner-point-alist
))
292 do
(progn (set-buffer buf
)
293 (set-mark (car entry
))
294 (setf (winner-active-region) (cdr entry
)))))
295 ;; Delete windows, whose buffers are dead.
296 ;; Return t if this is still a possible configuration.
298 (progn (mapcar 'delete-window
(cdr xwins
))
300 nil
; No windows left
301 (progn (delete-window (car xwins
))
306 ;;;; Winner mode (a minor mode)
308 (defcustom winner-mode-hook nil
309 "Functions to run whenever Winner mode is turned on."
313 (defcustom winner-mode-leave-hook nil
314 "Functions to run whenever Winner mode is turned off."
318 (defvar winner-mode-map nil
"Keymap for Winner mode.")
320 ;; Is `window-configuration-change-hook' working?
321 (defun winner-hook-installed-p ()
322 (save-window-excursion
323 (let ((winner-var nil
)
324 (window-configuration-change-hook
325 '((lambda () (setq winner-var t
)))))
330 (defun winner-mode (&optional arg
)
332 With arg, turn Winner mode on if and only if arg is positive."
334 (let ((on-p (if arg
(> (prefix-numeric-value arg
) 0)
341 ((winner-hook-installed-p)
342 (add-hook 'window-configuration-change-hook
'winner-change-fun
)
343 (add-hook 'post-command-hook
'winner-save-old-configurations
))
344 (t (add-hook 'post-command-hook
'winner-save-unconditionally
)))
345 (setq winner-modified-list
(frame-list))
346 (winner-save-old-configurations)
347 (run-hooks 'winner-mode-hook
))
350 (setq winner-mode nil
)
351 (remove-hook 'window-configuration-change-hook
'winner-change-fun
)
352 (remove-hook 'post-command-hook
'winner-save-old-configurations
)
353 (remove-hook 'post-command-hook
'winner-save-unconditionally
)
354 (run-hooks 'winner-mode-leave-hook
)))
355 (force-mode-line-update)))
357 \f;; Inspired by undo (simple.el)
359 (defvar winner-undo-frame nil
)
361 (defvar winner-pending-undo-ring nil
362 "The ring currently used by winner undo.")
363 (defvar winner-undo-counter nil
)
364 (defvar winner-undone-data nil
) ; There confs have been passed.
366 (defun winner-undo ()
367 "Switch back to an earlier window configuration saved by Winner mode.
368 In other words, \"undo\" changes in window configuration."
371 ((not winner-mode
) (error "Winner mode is turned off"))
372 (t (unless (and (eq last-command
'winner-undo
)
373 (eq winner-undo-frame
(selected-frame)))
374 (winner-save-unconditionally) ; current configuration->stack
375 (setq winner-undo-frame
(selected-frame))
376 (setq winner-point-alist
(winner-make-point-alist))
377 (setq winner-pending-undo-ring
(winner-ring (selected-frame)))
378 (setq winner-undo-counter
0)
379 (setq winner-undone-data
(list (winner-win-data))))
380 (incf winner-undo-counter
) ; starting at 1
381 (when (and (winner-undo-this)
382 (not (window-minibuffer-p (selected-window))))
383 (message "Winner undo (%d / %d)"
385 (1- (ring-length winner-pending-undo-ring
)))))))
387 (defun winner-win-data ()
388 ;; Essential properties of the windows in the selected frame.
389 (loop for win being the windows
390 unless
(window-minibuffer-p win
)
391 collect
(list (window-buffer win
)
393 (window-height win
))))
396 (defun winner-undo-this () ; The heart of winner undo.
399 ((>= winner-undo-counter
(ring-length winner-pending-undo-ring
))
400 (message "No further window configuration undo information")
403 ((and ; If possible configuration
404 (winner-set (ring-ref winner-pending-undo-ring
405 winner-undo-counter
))
406 ;; .. and new configuration
407 (let ((data (winner-win-data)))
408 (and (not (member data winner-undone-data
))
409 (push data winner-undone-data
))))
410 (return t
)) ; .. then everything is all right.
411 (t ; Else; discharge it and try another one.
412 (ring-remove winner-pending-undo-ring winner-undo-counter
)))))
415 (defun winner-redo () ; If you change your mind.
416 "Restore a more recent window configuration saved by Winner mode."
419 ((eq last-command
'winner-undo
)
421 (ring-remove winner-pending-undo-ring
0))
422 (unless (eq (selected-window) (minibuffer-window))
423 (message "Winner undid undo")))
424 (t (error "Previous command was not a winner-undo"))))
426 ;;; To be evaluated when the package is loaded:
428 (if (fboundp 'compare-window-configurations
)
429 (defalias 'winner-equal
'compare-window-configurations
)
430 (defalias 'winner-equal
'equal
))
432 (unless winner-mode-map
433 (setq winner-mode-map
(make-sparse-keymap))
434 (define-key winner-mode-map
[(control x
) left
] 'winner-undo
)
435 (define-key winner-mode-map
[(control x
) right
] 'winner-redo
))
437 (unless (or (assq 'winner-mode minor-mode-map-alist
)
438 winner-dont-bind-my-keys
)
439 (push (cons 'winner-mode winner-mode-map
)
440 minor-mode-map-alist
))
442 (unless (assq 'winner-mode minor-mode-alist
)
443 (push '(winner-mode " Win") minor-mode-alist
))
447 ;;; winner.el ends here