25639
|
1 ;;; winner.el --- Restore old window configurations
|
17469
|
2
|
20968
|
3 ;; Copyright (C) 1997, 1998 Free Software Foundation. Inc.
|
17469
|
4
|
28071
|
5 ;; Author: Ivar Rummelhoff <ivarru@math.uio.no>
|
17469
|
6 ;; Created: 27 Feb 1997
|
25639
|
7 ;; Time-stamp: <1998-08-21 19:51:02 ivarr>
|
28023
|
8 ;; Keywords: convenience frames
|
17469
|
9
|
17470
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
|
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
17469
|
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.
|
|
16
|
17470
|
17 ;; GNU Emacs is distributed in the hope that it will be useful,
|
17469
|
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.
|
|
21
|
|
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.
|
|
26
|
|
27 ;;; Commentary:
|
19564
|
28
|
21125
|
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
|
25639
|
31 ;; windows) so that the changes can be "undone" using the command
|
21125
|
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.
|
17469
|
38
|
25639
|
39 ;; Winner mode was improved august 1998.
|
|
40
|
|
41 ;;; Code:
|
|
42
|
|
43 (eval-when-compile
|
28023
|
44 (require 'cl))
|
17469
|
45
|
28023
|
46 (eval-when-compile
|
25639
|
47 (cond
|
25733
|
48 ((eq (aref (emacs-version) 0) ?X)
|
25639
|
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)))) )
|
|
58
|
19564
|
59 (require 'ring)
|
17469
|
60
|
21125
|
61 (when (fboundp 'defgroup)
|
25639
|
62 (defgroup winner nil
|
21125
|
63 "Restoring window configurations."
|
|
64 :group 'windows))
|
|
65
|
|
66 (unless (fboundp 'defcustom)
|
|
67 (defmacro defcustom (symbol &optional initvalue docs &rest rest)
|
|
68 (list 'defvar symbol initvalue docs)))
|
|
69
|
28023
|
70
|
|
71 ;;;###autoload
|
20968
|
72 (defcustom winner-mode nil
|
|
73 "Toggle winner-mode.
|
24637
|
74 Setting this variable directly does not take effect;
|
|
75 use either \\[customize] or the function `winner-mode'."
|
21125
|
76 :set #'(lambda (symbol value)
|
|
77 (winner-mode (or value 0)))
|
20968
|
78 :initialize 'custom-initialize-default
|
21125
|
79 :type 'boolean
|
|
80 :group 'winner
|
20968
|
81 :require 'winner)
|
|
82
|
|
83 (defcustom winner-dont-bind-my-keys nil
|
|
84 "If non-nil: Do not use `winner-mode-map' in Winner mode."
|
21125
|
85 :type 'boolean
|
|
86 :group 'winner)
|
|
87
|
|
88 (defcustom winner-ring-size 200
|
|
89 "Maximum number of stored window configurations per frame."
|
|
90 :type 'integer
|
20968
|
91 :group 'winner)
|
17469
|
92
|
21125
|
93
|
|
94
|
17469
|
95
|
25639
|
96 ;;;; Saving old configurations (internal variables and subroutines)
|
|
97
|
|
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)
|
|
102
|
|
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))))
|
|
111
|
|
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))))
|
|
119
|
|
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))))
|
|
125
|
21125
|
126
|
17469
|
127
|
21125
|
128 ;; This variable contains the window cofiguration rings.
|
|
129 ;; The key in this alist is the frame.
|
19564
|
130 (defvar winner-ring-alist nil)
|
|
131
|
21125
|
132 ;; Find the right ring. If it does not exist, create one.
|
19564
|
133 (defsubst winner-ring (frame)
|
|
134 (or (cdr (assq frame winner-ring-alist))
|
|
135 (progn
|
21125
|
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))))
|
|
140
|
25639
|
141 ;; If the same command is called several times in a row,
|
|
142 ;; we only save one window configuration.
|
|
143 (defvar winner-last-command nil)
|
21125
|
144
|
25639
|
145 ;; Frames affected by the previous command.
|
|
146 (defvar winner-last-frames nil)
|
|
147
|
|
148 ;; Save the current window configuration, if it has changed.
|
|
149 ;; Then return frame, else return nil.
|
21125
|
150 (defun winner-insert-if-new (frame)
|
25639
|
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)))
|
21125
|
161
|
25639
|
162 ;; Frames affected by the current command.
|
|
163 (defvar winner-modified-list nil)
|
19564
|
164
|
25639
|
165 ;; Called whenever the window configuration changes
|
|
166 ;; (a `window-configuration-change-hook').
|
19564
|
167 (defun winner-change-fun ()
|
21125
|
168 (unless (memq (selected-frame) winner-modified-list)
|
|
169 (push (selected-frame) winner-modified-list)))
|
|
170
|
|
171
|
25639
|
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))
|
|
186
|
|
187 ;; For compatibility with other emacsen
|
|
188 ;; and called by `winner-undo' before "undoing".
|
21125
|
189 (defun winner-save-unconditionally ()
|
25639
|
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))
|
|
195
|
21125
|
196
|
25639
|
197
|
19564
|
198
|
25639
|
199 ;;;; Restoring configurations
|
|
200
|
|
201 ;; Works almost as `set-window-configuration',
|
|
202 ;; but doesn't change the contents or the size of the minibuffer.
|
21125
|
203 (defun winner-set-conf (winconf)
|
25639
|
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))))))
|
|
220
|
|
221
|
|
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.
|
|
226 ;;
|
|
227 ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
|
21125
|
228
|
25639
|
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)))
|
|
246
|
19564
|
247
|
25639
|
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)))))))
|
|
265
|
|
266 ;; Make sure point doesn't end up in the minibuffer and
|
21125
|
267 ;; delete windows displaying dead buffers. Return nil
|
|
268 ;; if and only if all the windows should have been deleted.
|
25639
|
269 ;; Do not move neither points nor marks.
|
19564
|
270 (defun winner-set (conf)
|
25639
|
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)))
|
21125
|
278 (winner-set-conf (car conf))
|
25639
|
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)))))))
|
19564
|
304
|
|
305
|
21125
|
306
|
25639
|
307 ;;;; Winner mode (a minor mode)
|
17469
|
308
|
20968
|
309 (defcustom winner-mode-hook nil
|
|
310 "Functions to run whenever Winner mode is turned on."
|
|
311 :type 'hook
|
20969
|
312 :group 'winner)
|
17469
|
313
|
20969
|
314 (defcustom winner-mode-leave-hook nil
|
20968
|
315 "Functions to run whenever Winner mode is turned off."
|
|
316 :type 'hook
|
20969
|
317 :group 'winner)
|
17469
|
318
|
|
319 (defvar winner-mode-map nil "Keymap for Winner mode.")
|
17470
|
320
|
21125
|
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)))
|
|
329
|
28023
|
330
|
|
331 ;;;###autoload
|
17469
|
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
|
19564
|
339 ;; Turn mode on
|
|
340 (on-p
|
|
341 (setq winner-mode t)
|
21125
|
342 (cond
|
|
343 ((winner-hook-installed-p)
|
|
344 (add-hook 'window-configuration-change-hook 'winner-change-fun)
|
25639
|
345 (add-hook 'post-command-hook 'winner-save-old-configurations))
|
21125
|
346 (t (add-hook 'post-command-hook 'winner-save-unconditionally)))
|
19564
|
347 (setq winner-modified-list (frame-list))
|
25639
|
348 (winner-save-old-configurations)
|
19564
|
349 (run-hooks 'winner-mode-hook))
|
|
350 ;; Turn mode off
|
|
351 (winner-mode
|
|
352 (setq winner-mode nil)
|
21125
|
353 (remove-hook 'window-configuration-change-hook 'winner-change-fun)
|
25639
|
354 (remove-hook 'post-command-hook 'winner-save-old-configurations)
|
21125
|
355 (remove-hook 'post-command-hook 'winner-save-unconditionally)
|
19564
|
356 (run-hooks 'winner-mode-leave-hook)))
|
17469
|
357 (force-mode-line-update)))
|
|
358
|
21125
|
359 ;; Inspired by undo (simple.el)
|
24590
|
360
|
25639
|
361 (defvar winner-undo-frame nil)
|
|
362
|
24590
|
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.
|
|
367
|
25639
|
368 (defun winner-undo ()
|
19564
|
369 "Switch back to an earlier window configuration saved by Winner mode.
|
25639
|
370 In other words, \"undo\" changes in window configuration."
|
|
371 (interactive)
|
19564
|
372 (cond
|
|
373 ((not winner-mode) (error "Winner mode is turned off"))
|
25639
|
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)))))))
|
|
388
|
|
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))))
|
|
396
|
|
397
|
|
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))
|
|
404
|
|
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)))))
|
|
415
|
17469
|
416
|
25639
|
417 (defun winner-redo () ; If you change your mind.
|
19564
|
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))
|
25639
|
424 (unless (eq (selected-window) (minibuffer-window))
|
|
425 (message "Winner undid undo")))
|
19564
|
426 (t (error "Previous command was not a winner-undo"))))
|
25639
|
427
|
|
428 ;;; To be evaluated when the package is loaded:
|
17469
|
429
|
21125
|
430 (if (fboundp 'compare-window-configurations)
|
|
431 (defalias 'winner-equal 'compare-window-configurations)
|
|
432 (defalias 'winner-equal 'equal))
|
17469
|
433
|
|
434 (unless winner-mode-map
|
|
435 (setq winner-mode-map (make-sparse-keymap))
|
21125
|
436 (define-key winner-mode-map [(control x) left] 'winner-undo)
|
|
437 (define-key winner-mode-map [(control x) right] 'winner-redo))
|
17469
|
438
|
|
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))
|
|
443
|
|
444 (unless (assq 'winner-mode minor-mode-alist)
|
|
445 (push '(winner-mode " Win") minor-mode-alist))
|
|
446
|
|
447 (provide 'winner)
|
|
448
|
17470
|
449 ;;; winner.el ends here
|