* lisp/emacs-lisp/cconv.el: New file.
[emacs.git] / lisp / ebuff-menu.el
blobdd589cb58f70260aa7d59c27dc02379cf4be22e2
1 ;;; ebuff-menu.el --- electric-buffer-list mode
3 ;; Copyright (C) 1985-1986, 1994, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Richard Mlynarik <mly@ai.mit.edu>
6 ;; Maintainer: FSF
7 ;; Keywords: convenience
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 ;; Who says one can't have typeout windows in GNU Emacs? The entry
27 ;; point, `electric-buffer-list' works like ^r select buffer from the
28 ;; ITS Emacs lunar or tmacs libraries.
30 ;;; Code:
32 (require 'electric)
34 ;; this depends on the format of list-buffers (from src/buffer.c) and
35 ;; on stuff in lisp/buff-menu.el
37 (defvar electric-buffer-menu-mode-map nil)
39 (defvar electric-buffer-menu-mode-hook nil
40 "Normal hook run by `electric-buffer-list'.")
42 ;;;###autoload
43 (defun electric-buffer-list (arg)
44 "Pop up a buffer describing the set of Emacs buffers.
45 Vaguely like ITS lunar select buffer; combining typeoutoid buffer
46 listing with menuoid buffer selection.
48 If the very next character typed is a space then the buffer list
49 window disappears. Otherwise, one may move around in the buffer list
50 window, marking buffers to be selected, saved or deleted.
52 To exit and select a new buffer, type a space when the cursor is on
53 the appropriate line of the buffer-list window. Other commands are
54 much like those of `Buffer-menu-mode'.
56 Run hooks in `electric-buffer-menu-mode-hook' on entry.
58 \\{electric-buffer-menu-mode-map}"
59 (interactive "P")
60 (let (select buffer)
61 (save-window-excursion
62 (setq buffer (list-buffers-noselect arg))
63 (Electric-pop-up-window buffer)
64 (unwind-protect
65 (progn
66 (set-buffer buffer)
67 (Electric-buffer-menu-mode)
68 (electric-buffer-update-highlight)
69 (setq select
70 (catch 'electric-buffer-menu-select
71 (message "<<< Press Return to bury the buffer list >>>")
72 (if (eq (setq unread-command-events (list (read-event)))
73 ?\s)
74 (progn (setq unread-command-events nil)
75 (throw 'electric-buffer-menu-select nil)))
76 (let ((start-point (point))
77 (first (progn (goto-char (point-min))
78 (unless Buffer-menu-use-header-line
79 (forward-line 2))
80 (point)))
81 (last (progn (goto-char (point-max))
82 (forward-line -1)
83 (point)))
84 (goal-column 0))
85 ;; Use start-point if it is meaningful.
86 (goto-char (if (or (< start-point first)
87 (> start-point last))
88 first
89 start-point))
90 (Electric-command-loop 'electric-buffer-menu-select
91 nil
93 'electric-buffer-menu-looper
94 (cons first last))))))
95 (set-buffer buffer)
96 (Buffer-menu-mode)
97 (bury-buffer) ;Get rid of window, if dedicated.
98 (message "")))
99 (if select
100 (progn (set-buffer buffer)
101 (let ((opoint (point-marker)))
102 (Buffer-menu-execute)
103 (goto-char (point-min))
104 (if (prog1 (search-forward "\n>" nil t)
105 (goto-char opoint) (set-marker opoint nil))
106 (Buffer-menu-select)
107 (switch-to-buffer (Buffer-menu-buffer t))))))))
109 (defun electric-buffer-menu-looper (state condition)
110 (cond ((and condition
111 (not (memq (car condition) '(buffer-read-only
112 end-of-buffer
113 beginning-of-buffer))))
114 (signal (car condition) (cdr condition)))
115 ((< (point) (car state))
116 (goto-char (point-min))
117 (unless Buffer-menu-use-header-line
118 (forward-line 2)))
119 ((> (point) (cdr state))
120 (goto-char (point-max))
121 (forward-line -1)
122 (if (pos-visible-in-window-p (point-max))
123 (recenter -1))))
124 (electric-buffer-update-highlight))
126 (defvar Helper-return-blurb)
128 (put 'Electric-buffer-menu-mode 'mode-class 'special)
129 (defun Electric-buffer-menu-mode ()
130 "Major mode for editing a list of buffers.
131 Each line describes one of the buffers in Emacs.
132 Letters do not insert themselves; instead, they are commands.
133 \\<electric-buffer-menu-mode-map>
134 \\[keyboard-quit] or \\[Electric-buffer-menu-quit] -- exit buffer menu, returning to previous window and buffer
135 configuration. If the very first character typed is a space, it
136 also has this effect.
137 \\[Electric-buffer-menu-select] -- select buffer of line point is on.
138 Also show buffers marked with m in other windows,
139 deletes buffers marked with \"D\", and saves those marked with \"S\".
140 \\[Buffer-menu-mark] -- mark buffer to be displayed.
141 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
142 \\[Buffer-menu-save] -- mark that buffer to be saved.
143 \\[Buffer-menu-delete] or \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted.
144 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
145 \\[Electric-buffer-menu-mode-view-buffer] -- view buffer, returning when done.
146 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
148 \\{electric-buffer-menu-mode-map}
150 Entry to this mode via command `electric-buffer-list' calls the value of
151 `electric-buffer-menu-mode-hook'."
152 (let ((saved header-line-format))
153 (kill-all-local-variables)
154 (setq header-line-format saved))
155 (use-local-map electric-buffer-menu-mode-map)
156 (setq mode-name "Electric Buffer Menu")
157 (setq mode-line-buffer-identification "Electric Buffer List")
158 (make-local-variable 'Helper-return-blurb)
159 (setq Helper-return-blurb "return to buffer editing")
160 (setq truncate-lines t)
161 (setq buffer-read-only t)
162 (setq major-mode 'Electric-buffer-menu-mode)
163 (goto-char (point-min))
164 (if (search-forward "\n." nil t) (forward-char -1))
165 (run-mode-hooks 'electric-buffer-menu-mode-hook))
167 ;; generally the same as Buffer-menu-mode-map
168 ;; (except we don't indirect to global-map)
169 (put 'Electric-buffer-menu-undefined 'suppress-keymap t)
170 (if electric-buffer-menu-mode-map
172 (let ((map (make-keymap)))
173 (fillarray (car (cdr map)) 'Electric-buffer-menu-undefined)
174 (define-key map "\e" nil)
175 (define-key map "\C-z" 'suspend-frame)
176 (define-key map "v" 'Electric-buffer-menu-mode-view-buffer)
177 (define-key map (char-to-string help-char) 'Helper-help)
178 (define-key map "?" 'Helper-describe-bindings)
179 (define-key map "\C-c" nil)
180 (define-key map "\C-c\C-c" 'Electric-buffer-menu-quit)
181 (define-key map "\C-]" 'Electric-buffer-menu-quit)
182 (define-key map "q" 'Electric-buffer-menu-quit)
183 (define-key map " " 'Electric-buffer-menu-select)
184 (define-key map "\C-m" 'Electric-buffer-menu-select)
185 (define-key map "\C-l" 'recenter)
186 (define-key map "s" 'Buffer-menu-save)
187 (define-key map "d" 'Buffer-menu-delete)
188 (define-key map "k" 'Buffer-menu-delete)
189 (define-key map "\C-d" 'Buffer-menu-delete-backwards)
190 ;(define-key map "\C-k" 'Buffer-menu-delete)
191 (define-key map "\177" 'Buffer-menu-backup-unmark)
192 (define-key map "~" 'Buffer-menu-not-modified)
193 (define-key map "u" 'Buffer-menu-unmark)
194 (let ((i ?0))
195 (while (<= i ?9)
196 (define-key map (char-to-string i) 'digit-argument)
197 (define-key map (concat "\e" (char-to-string i)) 'digit-argument)
198 (setq i (1+ i))))
199 (define-key map "-" 'negative-argument)
200 (define-key map "\e-" 'negative-argument)
201 (define-key map "m" 'Buffer-menu-mark)
202 (define-key map "\C-u" 'universal-argument)
203 (define-key map "\C-p" 'previous-line)
204 (define-key map "\C-n" 'next-line)
205 (define-key map "p" 'previous-line)
206 (define-key map "n" 'next-line)
207 (define-key map "\C-v" 'scroll-up)
208 (define-key map "\ev" 'scroll-down)
209 (define-key map ">" 'scroll-right)
210 (define-key map "<" 'scroll-left)
211 (define-key map "\e\C-v" 'scroll-other-window)
212 (define-key map "\e>" 'end-of-buffer)
213 (define-key map "\e<" 'beginning-of-buffer)
214 (define-key map "\e\e" nil)
215 (define-key map "\e\e\e" 'Electric-buffer-menu-quit)
216 (define-key map [escape escape escape] 'Electric-buffer-menu-quit)
217 (define-key map [mouse-2] 'Electric-buffer-menu-mouse-select)
218 (setq electric-buffer-menu-mode-map map)))
220 (defun Electric-buffer-menu-exit ()
221 (interactive)
222 (setq unread-command-events (listify-key-sequence (this-command-keys)))
223 ;; for robustness
224 (condition-case ()
225 (throw 'electric-buffer-menu-select nil)
226 (error (Buffer-menu-mode)
227 (other-buffer))))
229 (defun Electric-buffer-menu-select ()
230 "Leave Electric Buffer Menu, selecting buffers and executing changes.
231 Save buffers marked \"S\". Delete buffers marked \"K\".
232 Select buffer at point and display buffers marked \">\" in other windows."
233 (interactive)
234 (throw 'electric-buffer-menu-select (point)))
236 (defun Electric-buffer-menu-mouse-select (event)
237 (interactive "e")
238 (select-window (posn-window (event-end event)))
239 (set-buffer (window-buffer (selected-window)))
240 (goto-char (posn-point (event-end event)))
241 (throw 'electric-buffer-menu-select (point)))
243 (defun Electric-buffer-menu-quit ()
244 "Leave Electric Buffer Menu, restoring previous window configuration.
245 Skip execution of select, save, and delete commands."
246 (interactive)
247 (throw 'electric-buffer-menu-select nil))
249 (defun Electric-buffer-menu-undefined ()
250 (interactive)
251 (ding)
252 (message "%s"
253 (if (and (eq (key-binding "\C-c\C-c") 'Electric-buffer-menu-quit)
254 (eq (key-binding " ") 'Electric-buffer-menu-select)
255 (eq (key-binding (char-to-string help-char)) 'Helper-help)
256 (eq (key-binding "?") 'Helper-describe-bindings))
257 (substitute-command-keys "Type C-c C-c to exit, Space to select, \\[Helper-help] for help, ? for commands")
258 (substitute-command-keys "\
259 Type \\[Electric-buffer-menu-quit] to exit, \
260 \\[Electric-buffer-menu-select] to select, \
261 \\[Helper-help] for help, \\[Helper-describe-bindings] for commands.")))
262 (sit-for 4))
264 (defun Electric-buffer-menu-mode-view-buffer ()
265 "View buffer on current line in Electric Buffer Menu.
266 Return to Electric Buffer Menu when done."
267 (interactive)
268 (let ((bufnam (Buffer-menu-buffer nil)))
269 (if bufnam
270 (view-buffer bufnam)
271 (ding)
272 (message "Buffer %s does not exist!" bufnam)
273 (sit-for 4))))
275 (defvar electric-buffer-overlay nil)
276 (defun electric-buffer-update-highlight ()
277 (when (eq major-mode 'Electric-buffer-menu-mode)
278 ;; Make sure we have an overlay to use.
279 (or electric-buffer-overlay
280 (progn
281 (make-local-variable 'electric-buffer-overlay)
282 (setq electric-buffer-overlay (make-overlay (point) (point)))))
283 (move-overlay electric-buffer-overlay
284 (line-beginning-position)
285 (line-end-position))
286 (overlay-put electric-buffer-overlay 'face 'highlight)))
288 (provide 'ebuff-menu)
290 ;;; ebuff-menu.el ends here