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: <1998-08-21 19:51:02 ivarr>
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.
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
)))
72 (defcustom winner-mode nil
74 Setting this variable directly does not take effect;
75 use either \\[customize] or the function `winner-mode'."
76 :set
#'(lambda (symbol value
)
77 (winner-mode (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."
96 ;;;; Saving old configurations (internal variables and subroutines)
98 ;; This variable is updated with the current window configuration
99 ;; after every command, so that when command make changes in the
100 ;; window configuration, the last configuration can be saved.
101 (defvar winner-currents nil
)
103 ;; The current configuration (+ the buffers involved).
104 (defsubst winner-conf
()
105 (list (current-window-configuration)
106 (loop for w being the windows
107 unless
(window-minibuffer-p w
)
108 collect
(window-buffer w
)) ))
109 ;; (if winner-testvar (incf winner-testvar) ; For debugging purposes
110 ;; (setq winner-testvar 0))))
112 ;; Save current configuration.
113 ;; (Called by `winner-save-old-configurations' below).
114 (defun winner-remember ()
115 (let ((entry (assq (selected-frame) winner-currents
)))
116 (if entry
(setcdr entry
(winner-conf))
117 (push (cons (selected-frame) (winner-conf))
120 ;; Consult `winner-currents'.
121 (defun winner-configuration (&optional frame
)
122 (or (cdr (assq (or frame
(selected-frame)) winner-currents
))
123 (letf (((selected-frame) frame
))
128 ;; This variable contains the window cofiguration rings.
129 ;; The key in this alist is the frame.
130 (defvar winner-ring-alist nil
)
132 ;; Find the right ring. If it does not exist, create one.
133 (defsubst winner-ring
(frame)
134 (or (cdr (assq frame winner-ring-alist
))
136 (let ((ring (make-ring winner-ring-size
)))
137 (ring-insert ring
(winner-configuration frame
))
138 (push (cons frame ring
) winner-ring-alist
)
141 \f;; If the same command is called several times in a row,
142 ;; we only save one window configuration.
143 (defvar winner-last-command nil
)
145 ;; Frames affected by the previous command.
146 (defvar winner-last-frames nil
)
149 (defun winner-equal (a b
)
150 "Check two Winner configurations A and B for equality.
151 Winner configurations are of the form (CONFIG BUFFERS),
152 where CONFIG is a window configuration and BUFFERS is a list of
154 (and (compare-window-configurations (car a
) (car b
))
155 (equal (cdr a
) (cdr b
))))
158 ;; Save the current window configuration, if it has changed.
159 ;; Then return frame, else return nil.
160 (defun winner-insert-if-new (frame)
161 (unless (or (memq frame winner-last-frames
)
162 (eq this-command
'winner-redo
))
163 (let ((conf (winner-configuration frame
))
164 (ring (winner-ring frame
)))
165 (when (and (not (ring-empty-p ring
))
166 (winner-equal conf
(ring-ref ring
0)))
167 (ring-remove ring
0))
168 (ring-insert ring conf
)
169 (push frame winner-last-frames
)
172 ;; Frames affected by the current command.
173 (defvar winner-modified-list nil
)
175 ;; Called whenever the window configuration changes
176 ;; (a `window-configuration-change-hook').
177 (defun winner-change-fun ()
178 (unless (memq (selected-frame) winner-modified-list
)
179 (push (selected-frame) winner-modified-list
)))
182 ;; For Emacs20 (a `post-command-hook').
183 (defun winner-save-old-configurations ()
184 (unless (eq this-command winner-last-command
)
185 (setq winner-last-frames nil
)
186 (setq winner-last-command this-command
))
187 (dolist (frame winner-modified-list
)
188 (winner-insert-if-new frame
))
189 (setq winner-modified-list nil
)
190 ;; (ir-trace ; For debugging purposes
192 ;; (loop with ring = (winner-ring (selected-frame))
193 ;; for i from 0 to (1- (ring-length ring))
194 ;; collect (caddr (ring-ref ring i))))
197 ;; For compatibility with other emacsen
198 ;; and called by `winner-undo' before "undoing".
199 (defun winner-save-unconditionally ()
200 (unless (eq this-command winner-last-command
)
201 (setq winner-last-frames nil
)
202 (setq winner-last-command this-command
))
203 (winner-insert-if-new (selected-frame))
209 \f;;;; Restoring configurations
211 ;; Works almost as `set-window-configuration',
212 ;; but doesn't change the contents or the size of the minibuffer.
213 (defun winner-set-conf (winconf)
214 (let ((miniwin (minibuffer-window))
215 (minisel (window-minibuffer-p (selected-window))))
216 (let ((minibuf (window-buffer miniwin
))
217 (minipoint (window-point miniwin
))
218 (minisize (window-height miniwin
)))
219 (set-window-configuration winconf
)
220 (setf (window-buffer miniwin
) minibuf
221 (window-point miniwin
) minipoint
)
222 (when (/= minisize
(window-height miniwin
))
223 (letf (((selected-window) miniwin
) )
224 ;; Clumsy due to cl-macs-limitation
225 (setf (window-height) minisize
)))
227 (minisel (select-window miniwin
))
228 ((window-minibuffer-p (selected-window))
229 (other-window 1))))))
232 (defvar winner-point-alist nil
)
233 ;; `set-window-configuration' restores old points and marks. This is
234 ;; not what we want, so we make a list of the "real" (i.e. new) points
235 ;; and marks before undoing window configurations.
237 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
239 (defun winner-make-point-alist ()
240 (letf (((current-buffer)))
243 for win being the windows
245 ((window-minibuffer-p win
))
246 ((setq entry
(assq win alist
))
247 ;; Update existing entry
248 (push (cons win
(window-point win
))
250 (t;; Else create new entry
251 (push (list (set-buffer (window-buffer win
))
252 (cons (mark t
) (winner-active-region))
253 (cons win
(window-point win
)))
255 finally return alist
)))
258 (defun winner-get-point (buf win
)
259 ;; Consult (and possibly extend) `winner-point-alist'.
260 (when (buffer-name buf
)
261 (let ((entry (assq buf winner-point-alist
)))
264 (or (cdr (assq win
(cddr entry
)))
265 (cdr (assq nil
(cddr entry
)))
266 (letf (((current-buffer) buf
))
267 (push (cons nil
(point)) (cddr entry
))
269 (t (letf (((current-buffer) buf
))
271 (cons (mark t
) (winner-active-region))
276 \f;; Make sure point doesn't end up in the minibuffer and
277 ;; delete windows displaying dead buffers. Return nil
278 ;; if and only if all the windows should have been deleted.
279 ;; Do not move neither points nor marks.
280 (defun winner-set (conf)
283 (loop for buf in
(cadr 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) ; These windows should be deleted
290 (loop for win being the windows
291 unless
(window-minibuffer-p win
)
292 do
(if (pop origpoints
)
293 (setf (window-point win
)
298 (push win xwins
))) ; delete this window
300 (letf (((current-buffer)))
301 (loop for buf in buffers
302 for entry
= (cadr (assq buf winner-point-alist
))
303 do
(progn (set-buffer buf
)
304 (set-mark (car entry
))
305 (setf (winner-active-region) (cdr entry
)))))
306 ;; Delete windows, whose buffers are dead.
307 ;; Return t if this is still a possible configuration.
309 (progn (mapcar 'delete-window
(cdr xwins
))
311 nil
; No windows left
312 (progn (delete-window (car xwins
))
317 ;;;; Winner mode (a minor mode)
319 (defcustom winner-mode-hook nil
320 "Functions to run whenever Winner mode is turned on."
324 (defcustom winner-mode-leave-hook nil
325 "Functions to run whenever Winner mode is turned off."
329 (defvar winner-mode-map nil
"Keymap for Winner mode.")
331 ;; Is `window-configuration-change-hook' working?
332 (defun winner-hook-installed-p ()
333 (save-window-excursion
334 (let ((winner-var nil
)
335 (window-configuration-change-hook
336 '((lambda () (setq winner-var t
)))))
342 (defun winner-mode (&optional arg
)
344 With arg, turn Winner mode on if and only if arg is positive."
346 (let ((on-p (if arg
(> (prefix-numeric-value arg
) 0)
353 ((winner-hook-installed-p)
354 (add-hook 'window-configuration-change-hook
'winner-change-fun
)
355 (add-hook 'post-command-hook
'winner-save-old-configurations
))
356 (t (add-hook 'post-command-hook
'winner-save-unconditionally
)))
357 (setq winner-modified-list
(frame-list))
358 (winner-save-old-configurations)
359 (run-hooks 'winner-mode-hook
))
362 (setq winner-mode nil
)
363 (remove-hook 'window-configuration-change-hook
'winner-change-fun
)
364 (remove-hook 'post-command-hook
'winner-save-old-configurations
)
365 (remove-hook 'post-command-hook
'winner-save-unconditionally
)
366 (run-hooks 'winner-mode-leave-hook
)))
367 (force-mode-line-update)))
369 \f;; Inspired by undo (simple.el)
371 (defvar winner-undo-frame nil
)
373 (defvar winner-pending-undo-ring nil
374 "The ring currently used by winner undo.")
375 (defvar winner-undo-counter nil
)
376 (defvar winner-undone-data nil
) ; There confs have been passed.
378 (defun winner-undo ()
379 "Switch back to an earlier window configuration saved by Winner mode.
380 In other words, \"undo\" changes in window configuration."
383 ((not winner-mode
) (error "Winner mode is turned off"))
384 (t (unless (and (eq last-command
'winner-undo
)
385 (eq winner-undo-frame
(selected-frame)))
386 (winner-save-unconditionally) ; current configuration->stack
387 (setq winner-undo-frame
(selected-frame))
388 (setq winner-point-alist
(winner-make-point-alist))
389 (setq winner-pending-undo-ring
(winner-ring (selected-frame)))
390 (setq winner-undo-counter
0)
391 (setq winner-undone-data
(list (winner-win-data))))
392 (incf winner-undo-counter
) ; starting at 1
393 (when (and (winner-undo-this)
394 (not (window-minibuffer-p (selected-window))))
395 (message "Winner undo (%d / %d)"
397 (1- (ring-length winner-pending-undo-ring
)))))))
399 (defun winner-win-data ()
400 ;; Essential properties of the windows in the selected frame.
401 (loop for win being the windows
402 unless
(window-minibuffer-p win
)
403 collect
(list (window-buffer win
)
405 (window-height win
))))
408 (defun winner-undo-this () ; The heart of winner undo.
411 ((>= winner-undo-counter
(ring-length winner-pending-undo-ring
))
412 (message "No further window configuration undo information")
415 ((and ; If possible configuration
416 (winner-set (ring-ref winner-pending-undo-ring
417 winner-undo-counter
))
418 ;; .. and new configuration
419 (let ((data (winner-win-data)))
420 (and (not (member data winner-undone-data
))
421 (push data winner-undone-data
))))
422 (return t
)) ; .. then everything is all right.
423 (t ; Else; discharge it and try another one.
424 (ring-remove winner-pending-undo-ring winner-undo-counter
)))))
427 (defun winner-redo () ; If you change your mind.
428 "Restore a more recent window configuration saved by Winner mode."
431 ((eq last-command
'winner-undo
)
433 (ring-remove winner-pending-undo-ring
0))
434 (unless (eq (selected-window) (minibuffer-window))
435 (message "Winner undid undo")))
436 (t (error "Previous command was not a winner-undo"))))
438 ;;; To be evaluated when the package is loaded:
440 (unless winner-mode-map
441 (setq winner-mode-map
(make-sparse-keymap))
442 (define-key winner-mode-map
[(control x
) left
] 'winner-undo
)
443 (define-key winner-mode-map
[(control x
) right
] 'winner-redo
))
445 (unless (or (assq 'winner-mode minor-mode-map-alist
)
446 winner-dont-bind-my-keys
)
447 (push (cons 'winner-mode winner-mode-map
)
448 minor-mode-map-alist
))
450 (unless (assq 'winner-mode minor-mode-alist
)
451 (push '(winner-mode " Win") minor-mode-alist
))
455 ;;; winner.el ends here