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