Autoload more carefully from Lisp. Follow aliases for function properties.
[emacs.git] / lisp / winner.el
blobd808a54a10e8ec2cf934d51abe84e8b10ea08fec
1 ;;; winner.el --- Restore old window configurations
3 ;; Copyright (C) 1997-1998, 2001-2012 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/>.
24 ;;; Commentary:
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.
39 ;;; Code:
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)
50 mark-active
51 (region-active-p)))
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))))
60 (require 'ring)
62 (defgroup winner nil
63 "Restoring window configurations."
64 :group 'windows)
66 ;;;###autoload
67 (defcustom winner-mode nil
68 "Toggle Winner mode.
69 Setting this variable directly does not take effect;
70 use either \\[customize] or the function `winner-mode'."
71 :set #'(lambda (symbol value) (funcall symbol (or value 0)))
72 :initialize 'custom-initialize-default
73 :type 'boolean
74 :group 'winner
75 :require 'winner)
77 (defcustom winner-dont-bind-my-keys nil
78 "If non-nil: Do not use `winner-mode-map' in Winner mode."
79 :type 'boolean
80 :group 'winner)
82 (defcustom winner-ring-size 200
83 "Maximum number of stored window configurations per frame."
84 :type 'integer
85 :group 'winner)
87 (defcustom winner-boring-buffers '("*Completions*")
88 "`winner-undo' will not restore windows displaying any of these buffers.
89 You may want to include buffer names such as *Help*, *Apropos*,
90 *Buffer List*, *info* and *Compile-Log*."
91 :type '(repeat string)
92 :group 'winner)
98 ;;;; Saving old configurations (internal variables and subroutines)
101 ;;; Current configuration
103 ;; List the windows according to their edges.
104 (defun winner-sorted-window-list ()
105 (sort (winner-window-list)
106 (lambda (x y)
107 (cl-loop for a in (winner-edges x)
108 for b in (winner-edges y)
109 while (= a b)
110 finally return (< a b)))))
112 (defun winner-win-data ()
113 ;; Essential properties of the windows in the selected frame.
114 (cl-loop for win in (winner-sorted-window-list)
115 collect (cons (winner-edges win) (window-buffer win))))
117 ;; This variable is updated with the current window configuration
118 ;; every time it changes.
119 (defvar winner-currents nil)
121 ;; The current configuration (+ the buffers involved).
122 (defsubst winner-conf ()
123 (cons (current-window-configuration)
124 (winner-win-data)))
127 ;; Save current configuration.
128 ;; (Called below by `winner-save-old-configurations').
129 (defun winner-remember ()
130 (let ((entry (assq (selected-frame) winner-currents)))
131 (if entry (setcdr entry (winner-conf))
132 (push (cons (selected-frame) (winner-conf))
133 winner-currents))))
135 ;; Consult `winner-currents'.
136 (defun winner-configuration (&optional frame)
137 (or (cdr (assq (or frame (selected-frame)) winner-currents))
138 (with-selected-frame frame
139 (winner-conf))))
143 ;;; Saved configurations
145 ;; This variable contains the window configuration rings.
146 ;; The key in this alist is the frame.
147 (defvar winner-ring-alist nil)
149 ;; Find the right ring. If it does not exist, create one.
150 (defsubst winner-ring (frame)
151 (or (cdr (assq frame winner-ring-alist))
152 (let ((ring (make-ring winner-ring-size)))
153 (ring-insert ring (winner-configuration frame))
154 (push (cons frame ring) winner-ring-alist)
155 ring)))
158 ;; If the same command is called several times in a row,
159 ;; we only save one window configuration.
160 (defvar winner-last-command nil)
162 ;; Frames affected by the previous command.
163 (defvar winner-last-frames nil)
166 (defsubst winner-equal (a b)
167 "Check whether two Winner configurations (as produced by
168 `winner-conf') are equal."
169 (equal (cdr a) (cdr b)))
172 ;; Save the current window configuration, if it has changed.
173 ;; If so return frame, otherwise return nil.
174 (defun winner-insert-if-new (frame)
175 (unless (or (memq frame winner-last-frames)
176 (eq this-command 'winner-redo))
177 (let ((conf (winner-configuration frame))
178 (ring (winner-ring frame)))
179 (when (and (not (ring-empty-p ring))
180 (winner-equal conf (ring-ref ring 0)))
181 ;; When the previous configuration was very similar,
182 ;; keep only the latest.
183 (ring-remove ring 0))
184 (ring-insert ring conf)
185 (push frame winner-last-frames)
186 frame)))
190 ;;; Hooks
192 ;; Frames affected by the current command.
193 (defvar winner-modified-list nil)
195 ;; Called whenever the window configuration changes
196 ;; (a `window-configuration-change-hook').
197 (defun winner-change-fun ()
198 (unless (or (memq (selected-frame) winner-modified-list)
199 (/= 0 (minibuffer-depth)))
200 (push (selected-frame) winner-modified-list)))
202 ;; A `post-command-hook' for emacsen with
203 ;; `window-configuration-change-hook'.
204 (defun winner-save-old-configurations ()
205 (when (zerop (minibuffer-depth))
206 (unless (eq this-command winner-last-command)
207 (setq winner-last-frames nil)
208 (setq winner-last-command this-command))
209 (dolist (frame winner-modified-list)
210 (winner-insert-if-new frame))
211 (setq winner-modified-list nil)
212 (winner-remember)))
214 ;; A `minibuffer-setup-hook'.
215 (defun winner-save-unconditionally ()
216 (unless (eq this-command winner-last-command)
217 (setq winner-last-frames nil)
218 (setq winner-last-command this-command))
219 (winner-insert-if-new (selected-frame))
220 (winner-remember))
222 ;; A `post-command-hook' for other emacsen.
223 ;; Also called by `winner-undo' before "undoing".
224 (defun winner-save-conditionally ()
225 (when (zerop (minibuffer-depth))
226 (winner-save-unconditionally)))
231 ;;;; Restoring configurations
233 ;; Works almost as `set-window-configuration',
234 ;; but does not change the contents or the size of the minibuffer,
235 ;; and tries to preserve the selected window.
236 (defun winner-set-conf (winconf)
237 (let* ((miniwin (minibuffer-window))
238 (chosen (selected-window))
239 (minisize (window-height miniwin)))
240 (cl-letf (((window-buffer miniwin))
241 ((window-point miniwin)))
242 (set-window-configuration winconf))
243 (cond
244 ((window-live-p chosen) (select-window chosen))
245 ((window-minibuffer-p (selected-window))
246 (other-window 1)))
247 (when (/= minisize (window-height miniwin))
248 (with-selected-window miniwin
249 (setf (window-height) minisize)))))
253 (defvar winner-point-alist nil)
254 ;; `set-window-configuration' restores old points and marks. This is
255 ;; not what we want, so we make a list of the "real" (i.e. new) points
256 ;; and marks before undoing window configurations.
258 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
260 (defun winner-make-point-alist ()
261 (save-current-buffer
262 (cl-loop with alist
263 for win in (winner-window-list)
264 for entry =
265 (or (assq (window-buffer win) alist)
266 (car (push (list (set-buffer (window-buffer win))
267 (cons (mark t) (winner-active-region)))
268 alist)))
269 do (push (cons win (window-point win))
270 (cddr entry))
271 finally return alist)))
273 (defun winner-get-point (buf win)
274 ;; Consult (and possibly extend) `winner-point-alist'.
275 ;; Returns nil if buf no longer exists.
276 (when (buffer-name buf)
277 (let ((entry (assq buf winner-point-alist)))
278 (cond
279 (entry
280 (or (cdr (assq win (cddr entry)))
281 (cdr (assq nil (cddr entry)))
282 (with-current-buffer buf
283 (push (cons nil (point)) (cddr entry))
284 (point))))
285 (t (with-current-buffer buf
286 (push (list buf
287 (cons (mark t) (winner-active-region))
288 (cons nil (point)))
289 winner-point-alist)
290 (point)))))))
293 ;; Make sure point does not end up in the minibuffer and delete
294 ;; windows displaying dead or boring buffers
295 ;; (c.f. `winner-boring-buffers'). Return nil if all the windows
296 ;; should be deleted. Preserve correct points and marks.
297 (defun winner-set (conf)
298 ;; For the format of `conf', see `winner-conf'.
299 (let* ((buffers nil)
300 (alive
301 ;; Possibly update `winner-point-alist'
302 (cl-loop for buf in (mapcar 'cdr (cdr conf))
303 for pos = (winner-get-point buf nil)
304 if (and pos (not (memq buf buffers)))
305 do (push buf buffers)
306 collect pos)))
307 (winner-set-conf (car conf))
308 (let (xwins) ; to be deleted
310 ;; Restore points
311 (dolist (win (winner-sorted-window-list))
312 (unless (and (pop alive)
313 (setf (window-point win)
314 (winner-get-point (window-buffer win) win))
315 (not (member (buffer-name (window-buffer win))
316 winner-boring-buffers)))
317 (push win xwins))) ; delete this window
319 ;; Restore marks
320 (save-current-buffer
321 (cl-loop for buf in buffers
322 for entry = (cadr (assq buf winner-point-alist))
323 do (progn (set-buffer buf)
324 (set-mark (car entry))
325 (setf (winner-active-region) (cdr entry)))))
326 ;; Delete windows, whose buffers are dead or boring.
327 ;; Return t if this is still a possible configuration.
328 (or (null xwins)
329 (progn
330 (mapc 'delete-window (cdr xwins)) ; delete all but one
331 (unless (one-window-p t)
332 (delete-window (car xwins))
333 t))))))
337 ;;;; Winner mode (a minor mode)
339 (defcustom winner-mode-hook nil
340 "Functions to run whenever Winner mode is turned on."
341 :type 'hook
342 :group 'winner)
344 (defcustom winner-mode-leave-hook nil
345 "Functions to run whenever Winner mode is turned off."
346 :type 'hook
347 :group 'winner)
349 (defvar winner-mode-map
350 (let ((map (make-sparse-keymap)))
351 (define-key map [(control c) left] 'winner-undo)
352 (define-key map [(control c) right] 'winner-redo)
353 map)
354 "Keymap for Winner mode.")
356 ;; Check if `window-configuration-change-hook' is working.
357 (defun winner-hook-installed-p ()
358 (save-window-excursion
359 (let ((winner-var nil)
360 (window-configuration-change-hook
361 '((lambda () (setq winner-var t)))))
362 (split-window)
363 winner-var)))
366 ;;;###autoload
367 (defun winner-mode (&optional arg)
368 "Toggle Winner mode.
369 With arg, turn Winner mode on if and only if arg is positive."
370 (interactive "P")
371 (let ((on-p (if arg (> (prefix-numeric-value arg) 0)
372 (not winner-mode))))
373 (cond
374 ;; Turn mode on
375 (on-p
376 (setq winner-mode t)
377 (cond
378 ((winner-hook-installed-p)
379 (add-hook 'window-configuration-change-hook 'winner-change-fun)
380 (add-hook 'post-command-hook 'winner-save-old-configurations))
381 (t (add-hook 'post-command-hook 'winner-save-conditionally)))
382 (add-hook 'minibuffer-setup-hook 'winner-save-unconditionally)
383 (setq winner-modified-list (frame-list))
384 (winner-save-old-configurations)
385 (run-hooks 'winner-mode-hook)
386 (when (called-interactively-p 'interactive)
387 (message "Winner mode enabled")))
388 ;; Turn mode off
389 (winner-mode
390 (setq winner-mode nil)
391 (remove-hook 'window-configuration-change-hook 'winner-change-fun)
392 (remove-hook 'post-command-hook 'winner-save-old-configurations)
393 (remove-hook 'post-command-hook 'winner-save-conditionally)
394 (remove-hook 'minibuffer-setup-hook 'winner-save-unconditionally)
395 (run-hooks 'winner-mode-leave-hook)
396 (when (called-interactively-p 'interactive)
397 (message "Winner mode disabled"))))))
399 ;; Inspired by undo (simple.el)
401 (defvar winner-undo-frame nil)
403 (defvar winner-pending-undo-ring nil
404 "The ring currently used by `winner-undo'.")
405 (defvar winner-undo-counter nil)
406 (defvar winner-undone-data nil) ; There confs have been passed.
408 (defun winner-undo ()
409 "Switch back to an earlier window configuration saved by Winner mode.
410 In other words, \"undo\" changes in window configuration."
411 (interactive)
412 (cond
413 ((not winner-mode) (error "Winner mode is turned off"))
414 (t (unless (and (eq last-command 'winner-undo)
415 (eq winner-undo-frame (selected-frame)))
416 (winner-save-conditionally) ; current configuration->stack
417 (setq winner-undo-frame (selected-frame))
418 (setq winner-point-alist (winner-make-point-alist))
419 (setq winner-pending-undo-ring (winner-ring (selected-frame)))
420 (setq winner-undo-counter 0)
421 (setq winner-undone-data (list (winner-win-data))))
422 (cl-incf winner-undo-counter) ; starting at 1
423 (when (and (winner-undo-this)
424 (not (window-minibuffer-p (selected-window))))
425 (message "Winner undo (%d / %d)"
426 winner-undo-counter
427 (1- (ring-length winner-pending-undo-ring)))))))
432 (defun winner-undo-this () ; The heart of winner undo.
433 (cl-loop
434 (cond
435 ((>= winner-undo-counter (ring-length winner-pending-undo-ring))
436 (message "No further window configuration undo information")
437 (cl-return nil))
439 ((and ; If possible configuration
440 (winner-set (ring-ref winner-pending-undo-ring
441 winner-undo-counter))
442 ; .. and new configuration
443 (let ((data (winner-win-data)))
444 (and (not (member data winner-undone-data))
445 (push data winner-undone-data))))
446 (cl-return t)) ; .. then everything is fine.
447 (t ;; Otherwise, discharge it (and try the next one).
448 (ring-remove winner-pending-undo-ring winner-undo-counter)))))
451 (defun winner-redo () ; If you change your mind.
452 "Restore a more recent window configuration saved by Winner mode."
453 (interactive)
454 (cond
455 ((eq last-command 'winner-undo)
456 (winner-set
457 (if (zerop (minibuffer-depth))
458 (ring-remove winner-pending-undo-ring 0)
459 (ring-ref winner-pending-undo-ring 0)))
460 (unless (eq (selected-window) (minibuffer-window))
461 (message "Winner undid undo")))
462 (t (error "Previous command was not a `winner-undo'"))))
464 ;;; To be evaluated when the package is loaded:
466 (unless (or (assq 'winner-mode minor-mode-map-alist)
467 winner-dont-bind-my-keys)
468 (push (cons 'winner-mode winner-mode-map)
469 minor-mode-map-alist))
471 (provide 'winner)
472 ;;; winner.el ends here