1 ;;; winner.el --- Restore old window configurations
3 ;; Copyright (C) 1997-1998, 2001-2013 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 `(setq mark-active
,store
)))))
49 (if (boundp 'mark-active
)
53 (defalias 'winner-edges
54 (if (featurep 'xemacs
) 'window-pixel-edges
'window-edges
))
55 (defalias 'winner-window-list
56 (if (featurep 'xemacs
)
57 (lambda () (delq (minibuffer-window) (window-list nil
0)))
58 (lambda () (window-list nil
0))))
63 "Restoring window configurations."
66 (defcustom winner-dont-bind-my-keys nil
67 "Non-nil means do not bind keys in Winner mode."
71 (defcustom winner-ring-size
200
72 "Maximum number of stored window configurations per frame."
76 (defcustom winner-boring-buffers
'("*Completions*")
77 "List of buffer names whose windows `winner-undo' will not restore.
78 You may want to include buffer names such as *Help*, *Apropos*,
79 *Buffer List*, *info* and *Compile-Log*."
80 :type
'(repeat string
)
85 ;;;; Saving old configurations (internal variables and subroutines)
88 ;;; Current configuration
90 ;; List the windows according to their edges.
91 (defun winner-sorted-window-list ()
92 (sort (winner-window-list)
94 (cl-loop for a in
(winner-edges x
)
95 for b in
(winner-edges y
)
97 finally return
(< a b
)))))
99 (defun winner-win-data ()
100 ;; Essential properties of the windows in the selected frame.
101 (cl-loop for win in
(winner-sorted-window-list)
102 collect
(cons (winner-edges win
) (window-buffer win
))))
104 ;; This variable is updated with the current window configuration
105 ;; every time it changes.
106 (defvar winner-currents nil
)
108 ;; The current configuration (+ the buffers involved).
109 (defsubst winner-conf
()
110 (cons (current-window-configuration)
114 ;; Save current configuration.
115 ;; (Called below by `winner-save-old-configurations').
116 (defun winner-remember ()
117 (let ((entry (assq (selected-frame) winner-currents
)))
118 (if entry
(setcdr entry
(winner-conf))
119 (push (cons (selected-frame) (winner-conf))
122 ;; Consult `winner-currents'.
123 (defun winner-configuration (&optional frame
)
124 (or (cdr (assq (or frame
(selected-frame)) winner-currents
))
125 (with-selected-frame frame
130 ;;; Saved configurations
132 ;; This variable contains the window configuration rings.
133 ;; The key in this alist is the frame.
134 (defvar winner-ring-alist nil
)
136 ;; Find the right ring. If it does not exist, create one.
137 (defsubst winner-ring
(frame)
138 (or (cdr (assq frame winner-ring-alist
))
139 (let ((ring (make-ring winner-ring-size
)))
140 (ring-insert ring
(winner-configuration frame
))
141 (push (cons frame ring
) winner-ring-alist
)
145 ;; If the same command is called several times in a row,
146 ;; we only save one window configuration.
147 (defvar winner-last-command nil
)
149 ;; Frames affected by the previous command.
150 (defvar winner-last-frames nil
)
153 (defsubst winner-equal
(a b
)
154 "Check whether two Winner configurations (as produced by
155 `winner-conf') are equal."
156 (equal (cdr a
) (cdr b
)))
159 ;; Save the current window configuration, if it has changed.
160 ;; If so return frame, otherwise return nil.
161 (defun winner-insert-if-new (frame)
162 (unless (or (memq frame winner-last-frames
)
163 (eq this-command
'winner-redo
))
164 (let ((conf (winner-configuration frame
))
165 (ring (winner-ring frame
)))
166 (when (and (not (ring-empty-p ring
))
167 (winner-equal conf
(ring-ref ring
0)))
168 ;; When the previous configuration was very similar,
169 ;; keep only the latest.
170 (ring-remove ring
0))
171 (ring-insert ring conf
)
172 (push frame winner-last-frames
)
179 ;; Frames affected by the current command.
180 (defvar winner-modified-list nil
)
182 ;; Called whenever the window configuration changes
183 ;; (a `window-configuration-change-hook').
184 (defun winner-change-fun ()
185 (unless (or (memq (selected-frame) winner-modified-list
)
186 (/= 0 (minibuffer-depth)))
187 (push (selected-frame) winner-modified-list
)))
189 ;; A `post-command-hook' for emacsen with
190 ;; `window-configuration-change-hook'.
191 (defun winner-save-old-configurations ()
192 (when (zerop (minibuffer-depth))
193 (unless (eq this-command winner-last-command
)
194 (setq winner-last-frames nil
)
195 (setq winner-last-command this-command
))
196 (dolist (frame winner-modified-list
)
197 (winner-insert-if-new frame
))
198 (setq winner-modified-list nil
)
201 ;; A `minibuffer-setup-hook'.
202 (defun winner-save-unconditionally ()
203 (unless (eq this-command winner-last-command
)
204 (setq winner-last-frames nil
)
205 (setq winner-last-command this-command
))
206 (winner-insert-if-new (selected-frame))
209 ;; A `post-command-hook' for other emacsen.
210 ;; Also called by `winner-undo' before "undoing".
211 (defun winner-save-conditionally ()
212 (when (zerop (minibuffer-depth))
213 (winner-save-unconditionally)))
218 ;;;; Restoring configurations
220 ;; Works almost as `set-window-configuration',
221 ;; but does not change the contents or the size of the minibuffer,
222 ;; and tries to preserve the selected window.
223 (defun winner-set-conf (winconf)
224 (let* ((miniwin (minibuffer-window))
225 (chosen (selected-window))
226 (minisize (window-height miniwin
)))
227 (cl-letf (((window-buffer miniwin
))
228 ((window-point miniwin
)))
229 (set-window-configuration winconf
))
231 ((window-live-p chosen
) (select-window chosen
))
232 ((window-minibuffer-p (selected-window))
234 (when (/= minisize
(window-height miniwin
))
235 (with-selected-window miniwin
236 (setf (window-height) minisize
)))))
240 (defvar winner-point-alist nil
)
241 ;; `set-window-configuration' restores old points and marks. This is
242 ;; not what we want, so we make a list of the "real" (i.e. new) points
243 ;; and marks before undoing window configurations.
245 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
247 (defun winner-make-point-alist ()
250 for win in
(winner-window-list)
252 (or (assq (window-buffer win
) alist
)
253 (car (push (list (set-buffer (window-buffer win
))
254 (cons (mark t
) (winner-active-region)))
256 do
(push (cons win
(window-point win
))
258 finally return alist
)))
260 (defun winner-get-point (buf win
)
261 ;; Consult (and possibly extend) `winner-point-alist'.
262 ;; Returns nil if buf no longer exists.
263 (when (buffer-name buf
)
264 (let ((entry (assq buf winner-point-alist
)))
267 (or (cdr (assq win
(cddr entry
)))
268 (cdr (assq nil
(cddr entry
)))
269 (with-current-buffer buf
270 (push (cons nil
(point)) (cddr entry
))
272 (t (with-current-buffer buf
274 (cons (mark t
) (winner-active-region))
280 ;; Make sure point does not end up in the minibuffer and delete
281 ;; windows displaying dead or boring buffers
282 ;; (c.f. `winner-boring-buffers'). Return nil if all the windows
283 ;; should be deleted. Preserve correct points and marks.
284 (defun winner-set (conf)
285 ;; For the format of `conf', see `winner-conf'.
288 ;; Possibly update `winner-point-alist'
289 (cl-loop for buf in
(mapcar 'cdr
(cdr conf
))
290 for pos
= (winner-get-point buf nil
)
291 if
(and pos
(not (memq buf buffers
)))
292 do
(push buf buffers
)
294 (winner-set-conf (car conf
))
295 (let (xwins) ; to be deleted
298 (dolist (win (winner-sorted-window-list))
299 (unless (and (pop alive
)
300 (setf (window-point win
)
301 (winner-get-point (window-buffer win
) win
))
302 (not (member (buffer-name (window-buffer win
))
303 winner-boring-buffers
)))
304 (push win xwins
))) ; delete this window
308 (cl-loop for buf in buffers
309 for entry
= (cadr (assq buf winner-point-alist
))
310 do
(progn (set-buffer buf
)
311 (set-mark (car entry
))
312 (setf (winner-active-region) (cdr entry
)))))
313 ;; Delete windows, whose buffers are dead or boring.
314 ;; Return t if this is still a possible configuration.
317 (mapc 'delete-window
(cdr xwins
)) ; delete all but one
318 (unless (one-window-p t
)
319 (delete-window (car xwins
))
324 ;;;; Winner mode (a minor mode)
326 (defcustom winner-mode-hook nil
327 "Functions to run whenever Winner mode is turned on or off."
331 (define-obsolete-variable-alias 'winner-mode-leave-hook
332 'winner-mode-off-hook
"24.3")
334 (defcustom winner-mode-off-hook nil
335 "Functions to run whenever Winner mode is turned off."
339 (defvar winner-mode-map
340 (let ((map (make-sparse-keymap)))
341 (unless winner-dont-bind-my-keys
342 (define-key map
[(control c
) left
] 'winner-undo
)
343 (define-key map
[(control c
) right
] 'winner-redo
))
345 "Keymap for Winner mode.")
347 ;; Check if `window-configuration-change-hook' is working.
348 (defun winner-hook-installed-p ()
349 (save-window-excursion
350 (let ((winner-var nil
)
351 (window-configuration-change-hook
352 '((lambda () (setq winner-var t
)))))
358 (define-minor-mode winner-mode nil
:global t
; let d-m-m make the doc
361 (if (winner-hook-installed-p)
363 (add-hook 'window-configuration-change-hook
'winner-change-fun
)
364 (add-hook 'post-command-hook
'winner-save-old-configurations
))
365 (add-hook 'post-command-hook
'winner-save-conditionally
))
366 (add-hook 'minibuffer-setup-hook
'winner-save-unconditionally
)
367 (setq winner-modified-list
(frame-list))
368 (winner-save-old-configurations))
369 (remove-hook 'window-configuration-change-hook
'winner-change-fun
)
370 (remove-hook 'post-command-hook
'winner-save-old-configurations
)
371 (remove-hook 'post-command-hook
'winner-save-conditionally
)
372 (remove-hook 'minibuffer-setup-hook
'winner-save-unconditionally
)))
374 ;; Inspired by undo (simple.el)
376 (defvar winner-undo-frame nil
)
378 (defvar winner-pending-undo-ring nil
379 "The ring currently used by `winner-undo'.")
380 (defvar winner-undo-counter nil
)
381 (defvar winner-undone-data nil
) ; There confs have been passed.
383 (defun winner-undo ()
384 "Switch back to an earlier window configuration saved by Winner mode.
385 In other words, \"undo\" changes in window configuration."
388 ((not winner-mode
) (error "Winner mode is turned off"))
389 (t (unless (and (eq last-command
'winner-undo
)
390 (eq winner-undo-frame
(selected-frame)))
391 (winner-save-conditionally) ; current configuration->stack
392 (setq winner-undo-frame
(selected-frame))
393 (setq winner-point-alist
(winner-make-point-alist))
394 (setq winner-pending-undo-ring
(winner-ring (selected-frame)))
395 (setq winner-undo-counter
0)
396 (setq winner-undone-data
(list (winner-win-data))))
397 (cl-incf winner-undo-counter
) ; starting at 1
398 (when (and (winner-undo-this)
399 (not (window-minibuffer-p (selected-window))))
400 (message "Winner undo (%d / %d)"
402 (1- (ring-length winner-pending-undo-ring
)))))))
407 (defun winner-undo-this () ; The heart of winner undo.
410 ((>= winner-undo-counter
(ring-length winner-pending-undo-ring
))
411 (message "No further window configuration undo information")
414 ((and ; If possible configuration
415 (winner-set (ring-ref winner-pending-undo-ring
416 winner-undo-counter
))
417 ; .. and new configuration
418 (let ((data (winner-win-data)))
419 (and (not (member data winner-undone-data
))
420 (push data winner-undone-data
))))
421 (cl-return t
)) ; .. then everything is fine.
422 (t ;; Otherwise, discharge it (and try the next one).
423 (ring-remove winner-pending-undo-ring winner-undo-counter
)))))
426 (defun winner-redo () ; If you change your mind.
427 "Restore a more recent window configuration saved by Winner mode."
430 ((eq last-command
'winner-undo
)
432 (if (zerop (minibuffer-depth))
433 (ring-remove winner-pending-undo-ring
0)
434 (ring-ref winner-pending-undo-ring
0)))
435 (unless (eq (selected-window) (minibuffer-window))
436 (message "Winner undid undo")))
437 (t (error "Previous command was not a `winner-undo'"))))
440 ;;; winner.el ends here