(outline-discard-overlays): Missing paren.
[emacs.git] / lisp / winner.el
blob2e359959434884512906a854c30d633521813565
1 ;;; winner.el --- Restore old window configurations
3 ;; Copyright (C) 1997, 1998 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)
15 ;; 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; 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.
27 ;;; Commentary:
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.
41 ;;; Code:
43 (eval-when-compile
44 (require 'cl))
46 (eval-when-compile
47 (cond
48 ((eq (aref (emacs-version) 0) ?X)
49 (defmacro winner-active-region ()
50 '(region-active-p))
51 (defsetf winner-active-region () (store)
52 `(if ,store (zmacs-activate-region)
53 (zmacs-deactivate-region))))
54 (t (defmacro winner-active-region ()
55 'mark-active)
56 (defsetf winner-active-region () (store)
57 `(setq mark-active ,store)))) )
59 (require 'ring)
61 (when (fboundp 'defgroup)
62 (defgroup winner nil
63 "Restoring window configurations."
64 :group 'windows))
66 (unless (fboundp 'defcustom)
67 (defmacro defcustom (symbol &optional initvalue docs &rest rest)
68 (list 'defvar symbol initvalue docs)))
71 ;;;###autoload
72 (defcustom winner-mode nil
73 "Toggle winner-mode.
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
79 :type 'boolean
80 :group 'winner
81 :require 'winner)
83 (defcustom winner-dont-bind-my-keys nil
84 "If non-nil: Do not use `winner-mode-map' in Winner mode."
85 :type 'boolean
86 :group 'winner)
88 (defcustom winner-ring-size 200
89 "Maximum number of stored window configurations per frame."
90 :type 'integer
91 :group 'winner)
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))
118 winner-currents))))
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))
124 (winner-conf))))
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))
135 (progn
136 (let ((ring (make-ring winner-ring-size)))
137 (ring-insert ring (winner-configuration frame))
138 (push (cons frame ring) winner-ring-alist)
139 ring))))
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)
148 ;; Save the current window configuration, if it has changed.
149 ;; Then return frame, else return nil.
150 (defun winner-insert-if-new (frame)
151 (unless (or (memq frame winner-last-frames)
152 (eq this-command 'winner-redo))
153 (let ((conf (winner-configuration frame))
154 (ring (winner-ring frame)))
155 (when (and (not (ring-empty-p ring))
156 (winner-equal conf (ring-ref ring 0)))
157 (ring-remove ring 0))
158 (ring-insert ring conf)
159 (push frame winner-last-frames)
160 frame)))
162 ;; Frames affected by the current command.
163 (defvar winner-modified-list nil)
165 ;; Called whenever the window configuration changes
166 ;; (a `window-configuration-change-hook').
167 (defun winner-change-fun ()
168 (unless (memq (selected-frame) winner-modified-list)
169 (push (selected-frame) winner-modified-list)))
172 ;; For Emacs20 (a `post-command-hook').
173 (defun winner-save-old-configurations ()
174 (unless (eq this-command winner-last-command)
175 (setq winner-last-frames nil)
176 (setq winner-last-command this-command))
177 (dolist (frame winner-modified-list)
178 (winner-insert-if-new frame))
179 (setq winner-modified-list nil)
180 ;; (ir-trace ; For debugging purposes
181 ;; "%S"
182 ;; (loop with ring = (winner-ring (selected-frame))
183 ;; for i from 0 to (1- (ring-length ring))
184 ;; collect (caddr (ring-ref ring i))))
185 (winner-remember))
187 ;; For compatibility with other emacsen
188 ;; and called by `winner-undo' before "undoing".
189 (defun winner-save-unconditionally ()
190 (unless (eq this-command winner-last-command)
191 (setq winner-last-frames nil)
192 (setq winner-last-command this-command))
193 (winner-insert-if-new (selected-frame))
194 (winner-remember))
199 \f;;;; Restoring configurations
201 ;; Works almost as `set-window-configuration',
202 ;; but doesn't change the contents or the size of the minibuffer.
203 (defun winner-set-conf (winconf)
204 (let ((miniwin (minibuffer-window))
205 (minisel (window-minibuffer-p (selected-window))))
206 (let ((minibuf (window-buffer miniwin))
207 (minipoint (window-point miniwin))
208 (minisize (window-height miniwin)))
209 (set-window-configuration winconf)
210 (setf (window-buffer miniwin) minibuf
211 (window-point miniwin) minipoint)
212 (when (/= minisize (window-height miniwin))
213 (letf (((selected-window) miniwin) )
214 ;; Clumsy due to cl-macs-limitation
215 (setf (window-height) minisize)))
216 (cond
217 (minisel (select-window miniwin))
218 ((window-minibuffer-p (selected-window))
219 (other-window 1))))))
222 (defvar winner-point-alist nil)
223 ;; `set-window-configuration' restores old points and marks. This is
224 ;; not what we want, so we make a list of the "real" (i.e. new) points
225 ;; and marks before undoing window configurations.
227 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
229 (defun winner-make-point-alist ()
230 (letf (((current-buffer)))
231 (loop with alist
232 with entry
233 for win being the windows
234 do (cond
235 ((window-minibuffer-p win))
236 ((setq entry (assq win alist))
237 ;; Update existing entry
238 (push (cons win (window-point win))
239 (cddr entry)))
240 (t;; Else create new entry
241 (push (list (set-buffer (window-buffer win))
242 (cons (mark t) (winner-active-region))
243 (cons win (window-point win)))
244 alist)))
245 finally return alist)))
248 (defun winner-get-point (buf win)
249 ;; Consult (and possibly extend) `winner-point-alist'.
250 (when (buffer-name buf)
251 (let ((entry (assq buf winner-point-alist)))
252 (cond
253 (entry
254 (or (cdr (assq win (cddr entry)))
255 (cdr (assq nil (cddr entry)))
256 (letf (((current-buffer) buf))
257 (push (cons nil (point)) (cddr entry))
258 (point))))
259 (t (letf (((current-buffer) buf))
260 (push (list buf
261 (cons (mark t) (winner-active-region))
262 (cons nil (point)))
263 winner-point-alist)
264 (point)))))))
266 \f;; Make sure point doesn't end up in the minibuffer and
267 ;; delete windows displaying dead buffers. Return nil
268 ;; if and only if all the windows should have been deleted.
269 ;; Do not move neither points nor marks.
270 (defun winner-set (conf)
271 (let* ((buffers nil)
272 (origpoints
273 (loop for buf in (cadr conf)
274 for pos = (winner-get-point buf nil)
275 if (and pos (not (memq buf buffers)))
276 do (push buf buffers)
277 collect pos)))
278 (winner-set-conf (car conf))
279 (let (xwins) ; These windows should be deleted
280 (loop for win being the windows
281 unless (window-minibuffer-p win)
282 do (if (pop origpoints)
283 (setf (window-point win)
284 ;; Restore point
285 (winner-get-point
286 (window-buffer win)
287 win))
288 (push win xwins))) ; delete this window
289 ;; Restore mark
290 (letf (((current-buffer)))
291 (loop for buf in buffers
292 for entry = (cadr (assq buf winner-point-alist))
293 do (progn (set-buffer buf)
294 (set-mark (car entry))
295 (setf (winner-active-region) (cdr entry)))))
296 ;; Delete windows, whose buffers are dead.
297 ;; Return t if this is still a possible configuration.
298 (or (null xwins)
299 (progn (mapcar 'delete-window (cdr xwins))
300 (if (one-window-p t)
301 nil ; No windows left
302 (progn (delete-window (car xwins))
303 t)))))))
307 ;;;; Winner mode (a minor mode)
309 (defcustom winner-mode-hook nil
310 "Functions to run whenever Winner mode is turned on."
311 :type 'hook
312 :group 'winner)
314 (defcustom winner-mode-leave-hook nil
315 "Functions to run whenever Winner mode is turned off."
316 :type 'hook
317 :group 'winner)
319 (defvar winner-mode-map nil "Keymap for Winner mode.")
321 ;; Is `window-configuration-change-hook' working?
322 (defun winner-hook-installed-p ()
323 (save-window-excursion
324 (let ((winner-var nil)
325 (window-configuration-change-hook
326 '((lambda () (setq winner-var t)))))
327 (split-window)
328 winner-var)))
331 ;;;###autoload
332 (defun winner-mode (&optional arg)
333 "Toggle Winner mode.
334 With arg, turn Winner mode on if and only if arg is positive."
335 (interactive "P")
336 (let ((on-p (if arg (> (prefix-numeric-value arg) 0)
337 (not winner-mode))))
338 (cond
339 ;; Turn mode on
340 (on-p
341 (setq winner-mode t)
342 (cond
343 ((winner-hook-installed-p)
344 (add-hook 'window-configuration-change-hook 'winner-change-fun)
345 (add-hook 'post-command-hook 'winner-save-old-configurations))
346 (t (add-hook 'post-command-hook 'winner-save-unconditionally)))
347 (setq winner-modified-list (frame-list))
348 (winner-save-old-configurations)
349 (run-hooks 'winner-mode-hook))
350 ;; Turn mode off
351 (winner-mode
352 (setq winner-mode nil)
353 (remove-hook 'window-configuration-change-hook 'winner-change-fun)
354 (remove-hook 'post-command-hook 'winner-save-old-configurations)
355 (remove-hook 'post-command-hook 'winner-save-unconditionally)
356 (run-hooks 'winner-mode-leave-hook)))
357 (force-mode-line-update)))
359 \f;; Inspired by undo (simple.el)
361 (defvar winner-undo-frame nil)
363 (defvar winner-pending-undo-ring nil
364 "The ring currently used by winner undo.")
365 (defvar winner-undo-counter nil)
366 (defvar winner-undone-data nil) ; There confs have been passed.
368 (defun winner-undo ()
369 "Switch back to an earlier window configuration saved by Winner mode.
370 In other words, \"undo\" changes in window configuration."
371 (interactive)
372 (cond
373 ((not winner-mode) (error "Winner mode is turned off"))
374 (t (unless (and (eq last-command 'winner-undo)
375 (eq winner-undo-frame (selected-frame)))
376 (winner-save-unconditionally) ; current configuration->stack
377 (setq winner-undo-frame (selected-frame))
378 (setq winner-point-alist (winner-make-point-alist))
379 (setq winner-pending-undo-ring (winner-ring (selected-frame)))
380 (setq winner-undo-counter 0)
381 (setq winner-undone-data (list (winner-win-data))))
382 (incf winner-undo-counter) ; starting at 1
383 (when (and (winner-undo-this)
384 (not (window-minibuffer-p (selected-window))))
385 (message "Winner undo (%d / %d)"
386 winner-undo-counter
387 (1- (ring-length winner-pending-undo-ring)))))))
389 (defun winner-win-data ()
390 ;; Essential properties of the windows in the selected frame.
391 (loop for win being the windows
392 unless (window-minibuffer-p win)
393 collect (list (window-buffer win)
394 (window-width win)
395 (window-height win))))
398 (defun winner-undo-this () ; The heart of winner undo.
399 (loop
400 (cond
401 ((>= winner-undo-counter (ring-length winner-pending-undo-ring))
402 (message "No further window configuration undo information")
403 (return nil))
405 ((and ; If possible configuration
406 (winner-set (ring-ref winner-pending-undo-ring
407 winner-undo-counter))
408 ;; .. and new configuration
409 (let ((data (winner-win-data)))
410 (and (not (member data winner-undone-data))
411 (push data winner-undone-data))))
412 (return t)) ; .. then everything is all right.
413 (t ; Else; discharge it and try another one.
414 (ring-remove winner-pending-undo-ring winner-undo-counter)))))
417 (defun winner-redo () ; If you change your mind.
418 "Restore a more recent window configuration saved by Winner mode."
419 (interactive)
420 (cond
421 ((eq last-command 'winner-undo)
422 (winner-set
423 (ring-remove winner-pending-undo-ring 0))
424 (unless (eq (selected-window) (minibuffer-window))
425 (message "Winner undid undo")))
426 (t (error "Previous command was not a winner-undo"))))
428 ;;; To be evaluated when the package is loaded:
430 (if (fboundp 'compare-window-configurations)
431 (defalias 'winner-equal 'compare-window-configurations)
432 (defalias 'winner-equal 'equal))
434 (unless winner-mode-map
435 (setq winner-mode-map (make-sparse-keymap))
436 (define-key winner-mode-map [(control x) left] 'winner-undo)
437 (define-key winner-mode-map [(control x) right] 'winner-redo))
439 (unless (or (assq 'winner-mode minor-mode-map-alist)
440 winner-dont-bind-my-keys)
441 (push (cons 'winner-mode winner-mode-map)
442 minor-mode-map-alist))
444 (unless (assq 'winner-mode minor-mode-alist)
445 (push '(winner-mode " Win") minor-mode-alist))
447 (provide 'winner)
449 ;;; winner.el ends here