Merge from emacs-24; up to 2012-12-27T08:21:08Z!rgm@gnu.org
[bpt/emacs.git] / lisp / winner.el
1 ;;; winner.el --- Restore old window configurations
2
3 ;; Copyright (C) 1997-1998, 2001-2013 Free Software Foundation, Inc.
4
5 ;; Author: Ivar Rummelhoff <ivarru@math.uio.no>
6 ;; Created: 27 Feb 1997
7 ;; Keywords: convenience frames
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
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.
35
36 ;; Winner mode was improved August 1998.
37 ;; Further improvements February 2002.
38
39 ;;; Code:
40
41 (eval-when-compile (require 'cl-lib))
42
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 `(if ,store (activate-mark) (deactivate-mark))))))
49 (region-active-p))
50
51 (defalias 'winner-edges
52 (if (featurep 'xemacs) 'window-pixel-edges 'window-edges))
53 (defalias 'winner-window-list
54 (if (featurep 'xemacs)
55 (lambda () (delq (minibuffer-window) (window-list nil 0)))
56 (lambda () (window-list nil 0))))
57
58 (require 'ring)
59
60 (defgroup winner nil
61 "Restoring window configurations."
62 :group 'windows)
63
64 (defcustom winner-dont-bind-my-keys nil
65 "Non-nil means do not bind keys in Winner mode."
66 :type 'boolean
67 :group 'winner)
68
69 (defcustom winner-ring-size 200
70 "Maximum number of stored window configurations per frame."
71 :type 'integer
72 :group 'winner)
73
74 (defcustom winner-boring-buffers '("*Completions*")
75 "List of buffer names whose windows `winner-undo' will not restore.
76 You may want to include buffer names such as *Help*, *Apropos*,
77 *Buffer List*, *info* and *Compile-Log*."
78 :type '(repeat string)
79 :group 'winner)
80
81
82 \f
83 ;;;; Saving old configurations (internal variables and subroutines)
84
85
86 ;;; Current configuration
87
88 ;; List the windows according to their edges.
89 (defun winner-sorted-window-list ()
90 (sort (winner-window-list)
91 (lambda (x y)
92 (cl-loop for a in (winner-edges x)
93 for b in (winner-edges y)
94 while (= a b)
95 finally return (< a b)))))
96
97 (defun winner-win-data ()
98 ;; Essential properties of the windows in the selected frame.
99 (cl-loop for win in (winner-sorted-window-list)
100 collect (cons (winner-edges win) (window-buffer win))))
101
102 ;; This variable is updated with the current window configuration
103 ;; every time it changes.
104 (defvar winner-currents nil)
105
106 ;; The current configuration (+ the buffers involved).
107 (defsubst winner-conf ()
108 (cons (current-window-configuration)
109 (winner-win-data)))
110
111
112 ;; Save current configuration.
113 ;; (Called below by `winner-save-old-configurations').
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))
118 winner-currents))))
119
120 ;; Consult `winner-currents'.
121 (defun winner-configuration (&optional frame)
122 (or (cdr (assq (or frame (selected-frame)) winner-currents))
123 (with-selected-frame frame
124 (winner-conf))))
125
126
127
128 ;;; Saved configurations
129
130 ;; This variable contains the window configuration rings.
131 ;; The key in this alist is the frame.
132 (defvar winner-ring-alist nil)
133
134 ;; Find the right ring. If it does not exist, create one.
135 (defsubst winner-ring (frame)
136 (or (cdr (assq frame winner-ring-alist))
137 (let ((ring (make-ring winner-ring-size)))
138 (ring-insert ring (winner-configuration frame))
139 (push (cons frame ring) winner-ring-alist)
140 ring)))
141
142 \f
143 ;; If the same command is called several times in a row,
144 ;; we only save one window configuration.
145 (defvar winner-last-command nil)
146
147 ;; Frames affected by the previous command.
148 (defvar winner-last-frames nil)
149
150
151 (defsubst winner-equal (a b)
152 "Check whether two Winner configurations (as produced by
153 `winner-conf') are equal."
154 (equal (cdr a) (cdr b)))
155
156
157 ;; Save the current window configuration, if it has changed.
158 ;; If so return frame, otherwise return nil.
159 (defun winner-insert-if-new (frame)
160 (unless (or (memq frame winner-last-frames)
161 (eq this-command 'winner-redo))
162 (let ((conf (winner-configuration frame))
163 (ring (winner-ring frame)))
164 (when (and (not (ring-empty-p ring))
165 (winner-equal conf (ring-ref ring 0)))
166 ;; When the previous configuration was very similar,
167 ;; keep only the latest.
168 (ring-remove ring 0))
169 (ring-insert ring conf)
170 (push frame winner-last-frames)
171 frame)))
172
173
174
175 ;;; Hooks
176
177 ;; Frames affected by the current command.
178 (defvar winner-modified-list nil)
179
180 ;; Called whenever the window configuration changes
181 ;; (a `window-configuration-change-hook').
182 (defun winner-change-fun ()
183 (unless (or (memq (selected-frame) winner-modified-list)
184 (/= 0 (minibuffer-depth)))
185 (push (selected-frame) winner-modified-list)))
186
187 ;; A `post-command-hook' for emacsen with
188 ;; `window-configuration-change-hook'.
189 (defun winner-save-old-configurations ()
190 (when (zerop (minibuffer-depth))
191 (unless (eq this-command winner-last-command)
192 (setq winner-last-frames nil)
193 (setq winner-last-command this-command))
194 (dolist (frame winner-modified-list)
195 (winner-insert-if-new frame))
196 (setq winner-modified-list nil)
197 (winner-remember)))
198
199 ;; A `minibuffer-setup-hook'.
200 (defun winner-save-unconditionally ()
201 (unless (eq this-command winner-last-command)
202 (setq winner-last-frames nil)
203 (setq winner-last-command this-command))
204 (winner-insert-if-new (selected-frame))
205 (winner-remember))
206
207 ;; A `post-command-hook' for other emacsen.
208 ;; Also called by `winner-undo' before "undoing".
209 (defun winner-save-conditionally ()
210 (when (zerop (minibuffer-depth))
211 (winner-save-unconditionally)))
212
213
214
215 \f
216 ;;;; Restoring configurations
217
218 ;; Works almost as `set-window-configuration',
219 ;; but does not change the contents or the size of the minibuffer,
220 ;; and tries to preserve the selected window.
221 (defun winner-set-conf (winconf)
222 (let* ((miniwin (minibuffer-window))
223 (chosen (selected-window))
224 (minisize (window-height miniwin)))
225 (cl-letf (((window-buffer miniwin))
226 ((window-point miniwin)))
227 (set-window-configuration winconf))
228 (cond
229 ((window-live-p chosen) (select-window chosen))
230 ((window-minibuffer-p (selected-window))
231 (other-window 1)))
232 (when (/= minisize (window-height miniwin))
233 (with-selected-window miniwin
234 (setf (window-height) minisize)))))
235
236
237
238 (defvar winner-point-alist nil)
239 ;; `set-window-configuration' restores old points and marks. This is
240 ;; not what we want, so we make a list of the "real" (i.e. new) points
241 ;; and marks before undoing window configurations.
242 ;;
243 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
244
245 (defun winner-make-point-alist ()
246 (save-current-buffer
247 (cl-loop with alist
248 for win in (winner-window-list)
249 for entry =
250 (or (assq (window-buffer win) alist)
251 (car (push (list (set-buffer (window-buffer win))
252 (cons (mark t) (winner-active-region)))
253 alist)))
254 do (push (cons win (window-point win))
255 (cddr entry))
256 finally return alist)))
257
258 (defun winner-get-point (buf win)
259 ;; Consult (and possibly extend) `winner-point-alist'.
260 ;; Returns nil if buf no longer exists.
261 (when (buffer-name buf)
262 (let ((entry (assq buf winner-point-alist)))
263 (cond
264 (entry
265 (or (cdr (assq win (cddr entry)))
266 (cdr (assq nil (cddr entry)))
267 (with-current-buffer buf
268 (push (cons nil (point)) (cddr entry))
269 (point))))
270 (t (with-current-buffer buf
271 (push (list buf
272 (cons (mark t) (winner-active-region))
273 (cons nil (point)))
274 winner-point-alist)
275 (point)))))))
276
277 \f
278 ;; Make sure point does not end up in the minibuffer and delete
279 ;; windows displaying dead or boring buffers
280 ;; (c.f. `winner-boring-buffers'). Return nil if all the windows
281 ;; should be deleted. Preserve correct points and marks.
282 (defun winner-set (conf)
283 ;; For the format of `conf', see `winner-conf'.
284 (let* ((buffers nil)
285 (alive
286 ;; Possibly update `winner-point-alist'
287 (cl-loop for buf in (mapcar 'cdr (cdr conf))
288 for pos = (winner-get-point buf nil)
289 if (and pos (not (memq buf buffers)))
290 do (push buf buffers)
291 collect pos)))
292 (winner-set-conf (car conf))
293 (let (xwins) ; to be deleted
294
295 ;; Restore points
296 (dolist (win (winner-sorted-window-list))
297 (unless (and (pop alive)
298 (setf (window-point win)
299 (winner-get-point (window-buffer win) win))
300 (not (member (buffer-name (window-buffer win))
301 winner-boring-buffers)))
302 (push win xwins))) ; delete this window
303
304 ;; Restore marks
305 (save-current-buffer
306 (cl-loop for buf in buffers
307 for entry = (cadr (assq buf winner-point-alist))
308 do (progn (set-buffer buf)
309 (set-mark (car entry))
310 (setf (winner-active-region) (cdr entry)))))
311 ;; Delete windows, whose buffers are dead or boring.
312 ;; Return t if this is still a possible configuration.
313 (or (null xwins)
314 (progn
315 (mapc 'delete-window (cdr xwins)) ; delete all but one
316 (unless (one-window-p t)
317 (delete-window (car xwins))
318 t))))))
319
320
321
322 ;;;; Winner mode (a minor mode)
323
324 (defcustom winner-mode-hook nil
325 "Functions to run whenever Winner mode is turned on or off."
326 :type 'hook
327 :group 'winner)
328
329 (define-obsolete-variable-alias 'winner-mode-leave-hook
330 'winner-mode-off-hook "24.3")
331
332 (defcustom winner-mode-off-hook nil
333 "Functions to run whenever Winner mode is turned off."
334 :type 'hook
335 :group 'winner)
336
337 (defvar winner-mode-map
338 (let ((map (make-sparse-keymap)))
339 (unless winner-dont-bind-my-keys
340 (define-key map [(control c) left] 'winner-undo)
341 (define-key map [(control c) right] 'winner-redo))
342 map)
343 "Keymap for Winner mode.")
344
345 ;; Check if `window-configuration-change-hook' is working.
346 (defun winner-hook-installed-p ()
347 (save-window-excursion
348 (let ((winner-var nil)
349 (window-configuration-change-hook
350 '((lambda () (setq winner-var t)))))
351 (split-window)
352 winner-var)))
353
354 \f
355 ;;;###autoload
356 (define-minor-mode winner-mode nil :global t ; let d-m-m make the doc
357 (if winner-mode
358 (progn
359 (if (winner-hook-installed-p)
360 (progn
361 (add-hook 'window-configuration-change-hook 'winner-change-fun)
362 (add-hook 'post-command-hook 'winner-save-old-configurations))
363 (add-hook 'post-command-hook 'winner-save-conditionally))
364 (add-hook 'minibuffer-setup-hook 'winner-save-unconditionally)
365 (setq winner-modified-list (frame-list))
366 (winner-save-old-configurations))
367 (remove-hook 'window-configuration-change-hook 'winner-change-fun)
368 (remove-hook 'post-command-hook 'winner-save-old-configurations)
369 (remove-hook 'post-command-hook 'winner-save-conditionally)
370 (remove-hook 'minibuffer-setup-hook 'winner-save-unconditionally)))
371
372 ;; Inspired by undo (simple.el)
373
374 (defvar winner-undo-frame nil)
375
376 (defvar winner-pending-undo-ring nil
377 "The ring currently used by `winner-undo'.")
378 (defvar winner-undo-counter nil)
379 (defvar winner-undone-data nil) ; There confs have been passed.
380
381 (defun winner-undo ()
382 "Switch back to an earlier window configuration saved by Winner mode.
383 In other words, \"undo\" changes in window configuration."
384 (interactive)
385 (cond
386 ((not winner-mode) (error "Winner mode is turned off"))
387 (t (unless (and (eq last-command 'winner-undo)
388 (eq winner-undo-frame (selected-frame)))
389 (winner-save-conditionally) ; current configuration->stack
390 (setq winner-undo-frame (selected-frame))
391 (setq winner-point-alist (winner-make-point-alist))
392 (setq winner-pending-undo-ring (winner-ring (selected-frame)))
393 (setq winner-undo-counter 0)
394 (setq winner-undone-data (list (winner-win-data))))
395 (cl-incf winner-undo-counter) ; starting at 1
396 (when (and (winner-undo-this)
397 (not (window-minibuffer-p (selected-window))))
398 (message "Winner undo (%d / %d)"
399 winner-undo-counter
400 (1- (ring-length winner-pending-undo-ring)))))))
401
402
403
404 \f
405 (defun winner-undo-this () ; The heart of winner undo.
406 (cl-loop
407 (cond
408 ((>= winner-undo-counter (ring-length winner-pending-undo-ring))
409 (message "No further window configuration undo information")
410 (cl-return nil))
411
412 ((and ; If possible configuration
413 (winner-set (ring-ref winner-pending-undo-ring
414 winner-undo-counter))
415 ; .. and new configuration
416 (let ((data (winner-win-data)))
417 (and (not (member data winner-undone-data))
418 (push data winner-undone-data))))
419 (cl-return t)) ; .. then everything is fine.
420 (t ;; Otherwise, discharge it (and try the next one).
421 (ring-remove winner-pending-undo-ring winner-undo-counter)))))
422
423
424 (defun winner-redo () ; If you change your mind.
425 "Restore a more recent window configuration saved by Winner mode."
426 (interactive)
427 (cond
428 ((eq last-command 'winner-undo)
429 (winner-set
430 (if (zerop (minibuffer-depth))
431 (ring-remove winner-pending-undo-ring 0)
432 (ring-ref winner-pending-undo-ring 0)))
433 (unless (eq (selected-window) (minibuffer-window))
434 (message "Winner undid undo")))
435 (t (error "Previous command was not a `winner-undo'"))))
436
437 (provide 'winner)
438 ;;; winner.el ends here