Commit | Line | Data |
---|---|---|
1ffece3a | 1 | ;;; winner.el --- Restore window configuration (or switch buffer) |
a14fb2b1 | 2 | |
21f3d1d3 | 3 | ;; Copyright (C) 1997 Free Software Foundation. Inc. |
a14fb2b1 RS |
4 | |
5 | ;; Author: Ivar Rummelhoff <ivarr@ifi.uio.no> | |
6 | ;; Maintainer: Ivar Rummelhoff <ivarr@ifi.uio.no> | |
7 | ;; Created: 27 Feb 1997 | |
1ffece3a | 8 | ;; Keywords: extensions, windows |
a14fb2b1 | 9 | |
21f3d1d3 RS |
10 | ;; This file is part of GNU Emacs. |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
a14fb2b1 RS |
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 | ||
21f3d1d3 | 17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
a14fb2b1 RS |
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: | |
1ffece3a RS |
28 | |
29 | ;; Winner mode is a global minor mode that when turned on records | |
30 | ;; changes in window configuration. This way the changes can be | |
31 | ;; "undone" using the function `winner-undo'. By default this one is | |
32 | ;; bound to the key sequence ctrl-x left. If you change your mind | |
33 | ;; (while undoing), you can press ctrl-x right (calling | |
34 | ;; `winner-redo'). Unlike the normal undo, you may have to skip | |
35 | ;; through several identical window configurations in order to find | |
36 | ;; the one you want. This is a bug due to some techical limitations | |
37 | ;; in Emacs and can maybe be fixed in the future. | |
a14fb2b1 | 38 | ;; |
1ffece3a RS |
39 | ;; In addition to this I have added `winner-switch' which is a program |
40 | ;; that switches to other buffers without disturbing Winner mode. If | |
41 | ;; you bind this command to a key sequence, you may step through all | |
42 | ;; your buffers (except the ones mentioned in `winner-skip-buffers' or | |
43 | ;; matched by `winner-skip-regexps'). With a numeric prefix argument | |
44 | ;; skip several buffers at a time. | |
a14fb2b1 RS |
45 | |
46 | ;;; Code: | |
47 | ||
1ffece3a RS |
48 | (eval-when-compile (require 'cl)) |
49 | (require 'ring) | |
a14fb2b1 | 50 | |
1ffece3a RS |
51 | (defvar winner-dont-bind-my-keys nil |
52 | "If non-nil: Do not use `winner-mode-map' in Winner mode.") | |
a14fb2b1 | 53 | |
1ffece3a RS |
54 | (defvar winner-ring-size 100 |
55 | "Maximum number of stored window configurations per frame.") | |
a14fb2b1 RS |
56 | |
57 | (defvar winner-skip-buffers | |
58 | '("*Messages*", | |
59 | "*Compile-Log*", | |
60 | ".newsrc-dribble", | |
61 | "*Completions*", | |
62 | "*Buffer list*") | |
1ffece3a | 63 | "Exclude these buffer names from any \(Winner switch\) list of buffers.") |
a14fb2b1 RS |
64 | |
65 | (defvar winner-skip-regexps '("^ ") | |
79fd9b2f RS |
66 | "Winner excludes buffers with names matching any of these regexps. |
67 | They are not included in any Winner mode list of buffers. | |
a14fb2b1 RS |
68 | |
69 | By default `winner-skip-regexps' is set to \(\"^ \"\), | |
70 | which excludes \"invisible buffers\".") | |
71 | ||
1ffece3a | 72 | (defvar winner-ring-alist nil) |
a14fb2b1 | 73 | |
1ffece3a RS |
74 | (defsubst winner-ring (frame) |
75 | (or (cdr (assq frame winner-ring-alist)) | |
76 | (progn | |
77 | (push (cons frame (make-ring winner-ring-size)) | |
78 | winner-ring-alist) | |
79 | (cdar winner-ring-alist)))) | |
a14fb2b1 | 80 | |
1ffece3a | 81 | (defvar winner-modified-list nil) |
a14fb2b1 | 82 | |
1ffece3a | 83 | (defun winner-change-fun () |
d2f27357 KH |
84 | (or (memq (selected-frame) winner-modified-list) |
85 | (push (selected-frame) winner-modified-list))) | |
a14fb2b1 | 86 | |
1ffece3a RS |
87 | (defun winner-save-new-configurations () |
88 | (while winner-modified-list | |
89 | (ring-insert | |
90 | (winner-ring (car winner-modified-list)) | |
91 | (current-window-configuration (pop winner-modified-list))))) | |
a14fb2b1 | 92 | |
1ffece3a RS |
93 | (defun winner-set (conf) |
94 | (set-window-configuration conf) | |
95 | (if (eq (selected-window) (minibuffer-window)) | |
96 | (other-window 1))) | |
a14fb2b1 | 97 | |
a14fb2b1 | 98 | |
1ffece3a | 99 | ;;; Winner mode (a minor mode) |
a14fb2b1 | 100 | |
1ffece3a RS |
101 | (defvar winner-mode-hook nil |
102 | "Functions to run whenever Winner mode is turned on.") | |
a14fb2b1 | 103 | |
1ffece3a RS |
104 | (defvar winner-mode-leave-hook nil |
105 | "Functions to run whenever Winner mode is turned off.") | |
106 | ||
107 | (defvar winner-mode nil) ; mode variable | |
a14fb2b1 | 108 | (defvar winner-mode-map nil "Keymap for Winner mode.") |
21f3d1d3 | 109 | |
a14fb2b1 RS |
110 | (defun winner-mode (&optional arg) |
111 | "Toggle Winner mode. | |
112 | With arg, turn Winner mode on if and only if arg is positive." | |
113 | (interactive "P") | |
114 | (let ((on-p (if arg (> (prefix-numeric-value arg) 0) | |
115 | (not winner-mode)))) | |
116 | (cond | |
1ffece3a RS |
117 | ;; Turn mode on |
118 | (on-p | |
119 | (setq winner-mode t) | |
120 | (add-hook 'window-configuration-change-hook 'winner-change-fun) | |
121 | (add-hook 'post-command-hook 'winner-save-new-configurations) | |
122 | (setq winner-modified-list (frame-list)) | |
123 | (winner-save-new-configurations) | |
124 | (run-hooks 'winner-mode-hook)) | |
125 | ;; Turn mode off | |
126 | (winner-mode | |
127 | (setq winner-mode nil) | |
128 | (run-hooks 'winner-mode-leave-hook))) | |
a14fb2b1 RS |
129 | (force-mode-line-update))) |
130 | ||
1ffece3a RS |
131 | ;; Inspired by undo (simple.el) |
132 | (defun winner-undo (arg) | |
133 | "Switch back to an earlier window configuration saved by Winner mode. | |
134 | In other words, \"undo\" changes in window configuration." | |
135 | (interactive "p") | |
136 | (cond | |
137 | ((not winner-mode) (error "Winner mode is turned off")) | |
138 | ((eq (selected-window) (minibuffer-window)) | |
139 | (error "No winner undo from minibuffer.")) | |
140 | (t (setq this-command t) | |
141 | (if (eq last-command 'winner-undo) | |
142 | ;; This was no new window configuration after all. | |
143 | (ring-remove winner-pending-undo-ring 0) | |
144 | (setq winner-pending-undo-ring (winner-ring (selected-frame))) | |
145 | (setq winner-undo-counter 0)) | |
146 | (winner-undo-more (or arg 1)) | |
147 | (message "Winner undo (%d)!" winner-undo-counter) | |
148 | (setq this-command 'winner-undo)))) | |
149 | ||
150 | (defvar winner-pending-undo-ring nil) | |
151 | ||
152 | (defvar winner-undo-counter nil) | |
153 | ||
154 | (defun winner-undo-more (count) | |
155 | "Undo N window configuration changes beyond what was already undone. | |
156 | Call `winner-undo-start' to get ready to undo recent changes, | |
157 | then call `winner-undo-more' one or more times to undo them." | |
158 | (let ((len (ring-length winner-pending-undo-ring))) | |
159 | (incf winner-undo-counter count) | |
160 | (if (>= winner-undo-counter len) | |
161 | (error "No further window configuration undo information") | |
162 | (winner-set | |
163 | (ring-ref winner-pending-undo-ring | |
164 | winner-undo-counter))))) | |
165 | ||
166 | (defun winner-redo () | |
167 | "Restore a more recent window configuration saved by Winner mode." | |
168 | (interactive) | |
169 | (cond | |
170 | ((eq last-command 'winner-undo) | |
171 | (ring-remove winner-pending-undo-ring 0) | |
172 | (winner-set | |
173 | (ring-remove winner-pending-undo-ring 0)) | |
174 | (or (eq (selected-window) (minibuffer-window)) | |
175 | (message "Winner undid undo!"))) | |
176 | (t (error "Previous command was not a winner-undo")))) | |
177 | ||
178 | ;;; Winner switch | |
179 | ||
180 | (defun winner-switch-buffer-list () | |
a14fb2b1 RS |
181 | (loop for buf in (buffer-list) |
182 | for name = (buffer-name buf) | |
183 | unless (or (eq (current-buffer) buf) | |
184 | (member name winner-skip-buffers) | |
185 | (loop for regexp in winner-skip-regexps | |
186 | if (string-match regexp name) return t | |
187 | finally return nil)) | |
188 | collect name)) | |
1ffece3a RS |
189 | |
190 | (defvar winner-switch-list nil) | |
191 | ||
192 | (defun winner-switch (count) | |
193 | "Step through your buffers without disturbing `winner-mode'. | |
194 | `winner-switch' does not consider buffers mentioned in the list | |
195 | `winner-skip-buffers' or matched by `winner-skip-regexps'." | |
196 | (interactive "p") | |
197 | (decf count) | |
198 | (setq this-command t) | |
199 | (cond | |
200 | ((eq last-command 'winner-switch) | |
201 | (if winner-mode (ring-remove (winner-ring (selected-frame)) 0)) | |
202 | (bury-buffer (current-buffer)) | |
203 | (mapcar 'bury-buffer winner-switch-list)) | |
204 | (t (setq winner-switch-list (winner-switch-buffer-list)))) | |
205 | (setq winner-switch-list (nthcdr count winner-switch-list)) | |
206 | (or winner-switch-list | |
207 | (setq winner-switch-list (winner-switch-buffer-list)) | |
208 | (error "No more buffers")) | |
209 | (switch-to-buffer (pop winner-switch-list)) | |
210 | (message (concat "Winner: [%s] " | |
211 | (mapconcat 'identity winner-switch-list " ")) | |
212 | (buffer-name)) | |
213 | (setq this-command 'winner-switch)) | |
a14fb2b1 | 214 | |
a14fb2b1 RS |
215 | ;;;; To be evaluated when the package is loaded: |
216 | ||
217 | (unless winner-mode-map | |
218 | (setq winner-mode-map (make-sparse-keymap)) | |
1ffece3a RS |
219 | (define-key winner-mode-map [?\C-x left] 'winner-undo) |
220 | (define-key winner-mode-map [?\C-x right] 'winner-redo)) | |
a14fb2b1 RS |
221 | |
222 | (unless (or (assq 'winner-mode minor-mode-map-alist) | |
223 | winner-dont-bind-my-keys) | |
224 | (push (cons 'winner-mode winner-mode-map) | |
225 | minor-mode-map-alist)) | |
226 | ||
227 | (unless (assq 'winner-mode minor-mode-alist) | |
228 | (push '(winner-mode " Win") minor-mode-alist)) | |
229 | ||
230 | (provide 'winner) | |
231 | ||
21f3d1d3 | 232 | ;;; winner.el ends here |