(tpu-cursor-free-mode): Emit message.
[emacs.git] / lisp / winner.el
blob1a8a0952aa21f1826c1b4e483f1da90495414767
1 ;;; winner.el --- Restore old window configurations
3 ;; Copyright (C) 1997, 1998, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation. Inc.
6 ;; Author: Ivar Rummelhoff <ivarru@math.uio.no>
7 ;; Created: 27 Feb 1997
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 3 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Winner mode is a global minor mode that records the changes in the
28 ;; window configuration (i.e. how the frames are partitioned into
29 ;; windows) so that the changes can be "undone" using the command
30 ;; `winner-undo'. By default this one is bound to the key sequence
31 ;; ctrl-c left. If you change your mind (while undoing), you can
32 ;; press ctrl-c right (calling `winner-redo'). Even though it uses
33 ;; some features of Emacs20.3, winner.el should also work with
34 ;; Emacs19.34 and XEmacs20, provided that the installed version of
35 ;; custom is not obsolete.
37 ;; Winner mode was improved August 1998.
38 ;; Further improvements February 2002.
40 ;;; Code:
42 (eval-when-compile
43 (require 'cl))
46 (defmacro winner-active-region ()
47 (if (boundp 'mark-active)
48 'mark-active
49 '(region-active-p)))
51 (defsetf winner-active-region () (store)
52 (if (featurep 'xemacs)
53 `(if ,store (zmacs-activate-region)
54 (zmacs-deactivate-region))
55 `(setq mark-active ,store)))
57 (defalias 'winner-edges
58 (if (featurep 'xemacs) 'window-pixel-edges 'window-edges))
59 (defalias 'winner-window-list
60 (if (featurep 'xemacs)
61 (lambda () (delq (minibuffer-window) (window-list nil 0)))
62 (lambda () (window-list nil 0))))
64 (require 'ring)
66 (defgroup winner nil
67 "Restoring window configurations."
68 :group 'windows)
70 ;;;###autoload
71 (defcustom winner-mode nil
72 "Toggle Winner mode.
73 Setting this variable directly does not take effect;
74 use either \\[customize] or the function `winner-mode'."
75 :set #'(lambda (symbol value) (funcall symbol (or value 0)))
76 :initialize 'custom-initialize-default
77 :type 'boolean
78 :group 'winner
79 :require 'winner)
81 (defcustom winner-dont-bind-my-keys nil
82 "If non-nil: Do not use `winner-mode-map' in Winner mode."
83 :type 'boolean
84 :group 'winner)
86 (defcustom winner-ring-size 200
87 "Maximum number of stored window configurations per frame."
88 :type 'integer
89 :group 'winner)
91 (defcustom winner-boring-buffers '("*Completions*")
92 "`winner-undo' will not restore windows displaying any of these buffers.
93 You may want to include buffer names such as *Help*, *Apropos*,
94 *Buffer List*, *info* and *Compile-Log*."
95 :type '(repeat string)
96 :group 'winner)
102 ;;;; Saving old configurations (internal variables and subroutines)
105 ;;; Current configuration
107 ;; List the windows according to their edges.
108 (defun winner-sorted-window-list ()
109 (sort (winner-window-list)
110 (lambda (x y)
111 (loop for a in (winner-edges x)
112 for b in (winner-edges y)
113 while (= a b)
114 finally return (< a b)))))
116 (defun winner-win-data ()
117 ;; Essential properties of the windows in the selected frame.
118 (loop for win in (winner-sorted-window-list)
119 collect (cons (winner-edges win) (window-buffer win))))
121 ;; This variable is updated with the current window configuration
122 ;; every time it changes.
123 (defvar winner-currents nil)
125 ;; The current configuration (+ the buffers involved).
126 (defsubst winner-conf ()
127 (cons (current-window-configuration)
128 (winner-win-data)))
131 ;; Save current configuration.
132 ;; (Called below by `winner-save-old-configurations').
133 (defun winner-remember ()
134 (let ((entry (assq (selected-frame) winner-currents)))
135 (if entry (setcdr entry (winner-conf))
136 (push (cons (selected-frame) (winner-conf))
137 winner-currents))))
139 ;; Consult `winner-currents'.
140 (defun winner-configuration (&optional frame)
141 (or (cdr (assq (or frame (selected-frame)) winner-currents))
142 (letf (((selected-frame) frame))
143 (winner-conf))))
147 ;;; Saved configurations
149 ;; This variable contains the window cofiguration rings.
150 ;; The key in this alist is the frame.
151 (defvar winner-ring-alist nil)
153 ;; Find the right ring. If it does not exist, create one.
154 (defsubst winner-ring (frame)
155 (or (cdr (assq frame winner-ring-alist))
156 (let ((ring (make-ring winner-ring-size)))
157 (ring-insert ring (winner-configuration frame))
158 (push (cons frame ring) winner-ring-alist)
159 ring)))
162 ;; If the same command is called several times in a row,
163 ;; we only save one window configuration.
164 (defvar winner-last-command nil)
166 ;; Frames affected by the previous command.
167 (defvar winner-last-frames nil)
170 (defsubst winner-equal (a b)
171 "Check whether two Winner configurations (as produced by
172 `winner-conf') are equal."
173 (equal (cdr a) (cdr b)))
176 ;; Save the current window configuration, if it has changed.
177 ;; If so return frame, otherwise return nil.
178 (defun winner-insert-if-new (frame)
179 (unless (or (memq frame winner-last-frames)
180 (eq this-command 'winner-redo))
181 (let ((conf (winner-configuration frame))
182 (ring (winner-ring frame)))
183 (when (and (not (ring-empty-p ring))
184 (winner-equal conf (ring-ref ring 0)))
185 ;; When the previous configuration was very similar,
186 ;; keep only the latest.
187 (ring-remove ring 0))
188 (ring-insert ring conf)
189 (push frame winner-last-frames)
190 frame)))
194 ;;; Hooks
196 ;; Frames affected by the current command.
197 (defvar winner-modified-list nil)
199 ;; Called whenever the window configuration changes
200 ;; (a `window-configuration-change-hook').
201 (defun winner-change-fun ()
202 (unless (or (memq (selected-frame) winner-modified-list)
203 (/= 0 (minibuffer-depth)))
204 (push (selected-frame) winner-modified-list)))
206 ;; A `post-command-hook' for emacsen with
207 ;; `window-configuration-change-hook'.
208 (defun winner-save-old-configurations ()
209 (when (zerop (minibuffer-depth))
210 (unless (eq this-command winner-last-command)
211 (setq winner-last-frames nil)
212 (setq winner-last-command this-command))
213 (dolist (frame winner-modified-list)
214 (winner-insert-if-new frame))
215 (setq winner-modified-list nil)
216 (winner-remember)))
218 ;; A `minibuffer-setup-hook'.
219 (defun winner-save-unconditionally ()
220 (unless (eq this-command winner-last-command)
221 (setq winner-last-frames nil)
222 (setq winner-last-command this-command))
223 (winner-insert-if-new (selected-frame))
224 (winner-remember))
226 ;; A `post-command-hook' for other emacsen.
227 ;; Also called by `winner-undo' before "undoing".
228 (defun winner-save-conditionally ()
229 (when (zerop (minibuffer-depth))
230 (winner-save-unconditionally)))
235 ;;;; Restoring configurations
237 ;; Works almost as `set-window-configuration',
238 ;; but does not change the contents or the size of the minibuffer,
239 ;; and tries to preserve the selected window.
240 (defun winner-set-conf (winconf)
241 (let* ((miniwin (minibuffer-window))
242 (chosen (selected-window))
243 (minisize (window-height miniwin)))
244 (letf (((window-buffer miniwin))
245 ((window-point miniwin)))
246 (set-window-configuration winconf))
247 (cond
248 ((window-live-p chosen) (select-window chosen))
249 ((window-minibuffer-p (selected-window))
250 (other-window 1)))
251 (when (/= minisize (window-height miniwin))
252 (letf (((selected-window) miniwin) )
253 (setf (window-height) minisize)))))
257 (defvar winner-point-alist nil)
258 ;; `set-window-configuration' restores old points and marks. This is
259 ;; not what we want, so we make a list of the "real" (i.e. new) points
260 ;; and marks before undoing window configurations.
262 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
264 (defun winner-make-point-alist ()
265 (letf (((current-buffer)))
266 (loop with alist
267 for win in (winner-window-list)
268 for entry =
269 (or (assq (window-buffer win) alist)
270 (car (push (list (set-buffer (window-buffer win))
271 (cons (mark t) (winner-active-region)))
272 alist)))
273 do (push (cons win (window-point win))
274 (cddr entry))
275 finally return alist)))
277 (defun winner-get-point (buf win)
278 ;; Consult (and possibly extend) `winner-point-alist'.
279 ;; Returns nil if buf no longer exists.
280 (when (buffer-name buf)
281 (let ((entry (assq buf winner-point-alist)))
282 (cond
283 (entry
284 (or (cdr (assq win (cddr entry)))
285 (cdr (assq nil (cddr entry)))
286 (letf (((current-buffer) buf))
287 (push (cons nil (point)) (cddr entry))
288 (point))))
289 (t (letf (((current-buffer) buf))
290 (push (list buf
291 (cons (mark t) (winner-active-region))
292 (cons nil (point)))
293 winner-point-alist)
294 (point)))))))
297 ;; Make sure point does not end up in the minibuffer and delete
298 ;; windows displaying dead or boring buffers
299 ;; (c.f. `winner-boring-buffers'). Return nil if all the windows
300 ;; should be deleted. Preserve correct points and marks.
301 (defun winner-set (conf)
302 ;; For the format of `conf', see `winner-conf'.
303 (let* ((buffers nil)
304 (alive
305 ;; Possibly update `winner-point-alist'
306 (loop for buf in (mapcar 'cdr (cdr conf))
307 for pos = (winner-get-point buf nil)
308 if (and pos (not (memq buf buffers)))
309 do (push buf buffers)
310 collect pos)))
311 (winner-set-conf (car conf))
312 (let (xwins) ; to be deleted
314 ;; Restore points
315 (dolist (win (winner-sorted-window-list))
316 (unless (and (pop alive)
317 (setf (window-point win)
318 (winner-get-point (window-buffer win) win))
319 (not (member (buffer-name (window-buffer win))
320 winner-boring-buffers)))
321 (push win xwins))) ; delete this window
323 ;; Restore marks
324 (letf (((current-buffer)))
325 (loop for buf in buffers
326 for entry = (cadr (assq buf winner-point-alist))
327 do (progn (set-buffer buf)
328 (set-mark (car entry))
329 (setf (winner-active-region) (cdr entry)))))
330 ;; Delete windows, whose buffers are dead or boring.
331 ;; Return t if this is still a possible configuration.
332 (or (null xwins)
333 (progn
334 (mapc 'delete-window (cdr xwins)) ; delete all but one
335 (unless (one-window-p t)
336 (delete-window (car xwins))
337 t))))))
341 ;;;; Winner mode (a minor mode)
343 (defcustom winner-mode-hook nil
344 "Functions to run whenever Winner mode is turned on."
345 :type 'hook
346 :group 'winner)
348 (defcustom winner-mode-leave-hook nil
349 "Functions to run whenever Winner mode is turned off."
350 :type 'hook
351 :group 'winner)
353 (defvar winner-mode-map
354 (let ((map (make-sparse-keymap)))
355 (define-key map [(control c) left] 'winner-undo)
356 (define-key map [(control c) right] 'winner-redo)
357 map)
358 "Keymap for Winner mode.")
360 ;; Check if `window-configuration-change-hook' is working.
361 (defun winner-hook-installed-p ()
362 (save-window-excursion
363 (let ((winner-var nil)
364 (window-configuration-change-hook
365 '((lambda () (setq winner-var t)))))
366 (split-window)
367 winner-var)))
370 ;;;###autoload
371 (defun winner-mode (&optional arg)
372 "Toggle Winner mode.
373 With arg, turn Winner mode on if and only if arg is positive."
374 (interactive "P")
375 (let ((on-p (if arg (> (prefix-numeric-value arg) 0)
376 (not winner-mode))))
377 (cond
378 ;; Turn mode on
379 (on-p
380 (setq winner-mode t)
381 (cond
382 ((winner-hook-installed-p)
383 (add-hook 'window-configuration-change-hook 'winner-change-fun)
384 (add-hook 'post-command-hook 'winner-save-old-configurations))
385 (t (add-hook 'post-command-hook 'winner-save-conditionally)))
386 (add-hook 'minibuffer-setup-hook 'winner-save-unconditionally)
387 (setq winner-modified-list (frame-list))
388 (winner-save-old-configurations)
389 (run-hooks 'winner-mode-hook)
390 (when (called-interactively-p 'interactive)
391 (message "Winner mode enabled")))
392 ;; Turn mode off
393 (winner-mode
394 (setq winner-mode nil)
395 (remove-hook 'window-configuration-change-hook 'winner-change-fun)
396 (remove-hook 'post-command-hook 'winner-save-old-configurations)
397 (remove-hook 'post-command-hook 'winner-save-conditionally)
398 (remove-hook 'minibuffer-setup-hook 'winner-save-unconditionally)
399 (run-hooks 'winner-mode-leave-hook)
400 (when (called-interactively-p 'interactive)
401 (message "Winner mode disabled"))))))
403 ;; Inspired by undo (simple.el)
405 (defvar winner-undo-frame nil)
407 (defvar winner-pending-undo-ring nil
408 "The ring currently used by `winner-undo'.")
409 (defvar winner-undo-counter nil)
410 (defvar winner-undone-data nil) ; There confs have been passed.
412 (defun winner-undo ()
413 "Switch back to an earlier window configuration saved by Winner mode.
414 In other words, \"undo\" changes in window configuration."
415 (interactive)
416 (cond
417 ((not winner-mode) (error "Winner mode is turned off"))
418 (t (unless (and (eq last-command 'winner-undo)
419 (eq winner-undo-frame (selected-frame)))
420 (winner-save-conditionally) ; current configuration->stack
421 (setq winner-undo-frame (selected-frame))
422 (setq winner-point-alist (winner-make-point-alist))
423 (setq winner-pending-undo-ring (winner-ring (selected-frame)))
424 (setq winner-undo-counter 0)
425 (setq winner-undone-data (list (winner-win-data))))
426 (incf winner-undo-counter) ; starting at 1
427 (when (and (winner-undo-this)
428 (not (window-minibuffer-p (selected-window))))
429 (message "Winner undo (%d / %d)"
430 winner-undo-counter
431 (1- (ring-length winner-pending-undo-ring)))))))
436 (defun winner-undo-this () ; The heart of winner undo.
437 (loop
438 (cond
439 ((>= winner-undo-counter (ring-length winner-pending-undo-ring))
440 (message "No further window configuration undo information")
441 (return nil))
443 ((and ; If possible configuration
444 (winner-set (ring-ref winner-pending-undo-ring
445 winner-undo-counter))
446 ; .. and new configuration
447 (let ((data (winner-win-data)))
448 (and (not (member data winner-undone-data))
449 (push data winner-undone-data))))
450 (return t)) ; .. then everything is fine.
451 (t ;; Otherwise, discharge it (and try the next one).
452 (ring-remove winner-pending-undo-ring winner-undo-counter)))))
455 (defun winner-redo () ; If you change your mind.
456 "Restore a more recent window configuration saved by Winner mode."
457 (interactive)
458 (cond
459 ((eq last-command 'winner-undo)
460 (winner-set
461 (if (zerop (minibuffer-depth))
462 (ring-remove winner-pending-undo-ring 0)
463 (ring-ref winner-pending-undo-ring 0)))
464 (unless (eq (selected-window) (minibuffer-window))
465 (message "Winner undid undo")))
466 (t (error "Previous command was not a `winner-undo'"))))
468 ;;; To be evaluated when the package is loaded:
470 (unless (or (assq 'winner-mode minor-mode-map-alist)
471 winner-dont-bind-my-keys)
472 (push (cons 'winner-mode winner-mode-map)
473 minor-mode-map-alist))
475 (provide 'winner)
476 ;; arch-tag: 686d1c1b-010e-42ca-a192-b5685112418f
477 ;;; winner.el ends here