Remove still more cal-tex code duplication
[emacs.git] / lisp / ebuff-menu.el
blob5d9ddc1a318ad991628cd1297aa42088f3839ada
1 ;;; ebuff-menu.el --- electric-buffer-list mode
3 ;; Copyright (C) 1985-1986, 1994, 2001-2012 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 (defvar electric-buffer-menu-mode-map
35 (let ((map (make-keymap)))
36 (fillarray (car (cdr map)) 'Electric-buffer-menu-undefined)
37 (define-key map "\e" nil)
38 (define-key map "\C-z" 'suspend-frame)
39 (define-key map "v" 'Electric-buffer-menu-mode-view-buffer)
40 (define-key map (char-to-string help-char) 'Helper-help)
41 (define-key map "?" 'Helper-describe-bindings)
42 (define-key map "\C-c" nil)
43 (define-key map "\C-c\C-c" 'Electric-buffer-menu-quit)
44 (define-key map "\C-]" 'Electric-buffer-menu-quit)
45 (define-key map "q" 'Electric-buffer-menu-quit)
46 (define-key map " " 'Electric-buffer-menu-select)
47 (define-key map "\C-m" 'Electric-buffer-menu-select)
48 (define-key map "\C-l" 'recenter)
49 (define-key map "s" 'Buffer-menu-save)
50 (define-key map "d" 'Buffer-menu-delete)
51 (define-key map "k" 'Buffer-menu-delete)
52 (define-key map "\C-d" 'Buffer-menu-delete-backwards)
53 ;; (define-key map "\C-k" 'Buffer-menu-delete)
54 (define-key map "\177" 'Buffer-menu-backup-unmark)
55 (define-key map "~" 'Buffer-menu-not-modified)
56 (define-key map "u" 'Buffer-menu-unmark)
57 (let ((i ?0))
58 (while (<= i ?9)
59 (define-key map (char-to-string i) 'digit-argument)
60 (define-key map (concat "\e" (char-to-string i)) 'digit-argument)
61 (setq i (1+ i))))
62 (define-key map "-" 'negative-argument)
63 (define-key map "\e-" 'negative-argument)
64 (define-key map "m" 'Buffer-menu-mark)
65 (define-key map "\C-u" 'universal-argument)
66 (define-key map "\C-p" 'previous-line)
67 (define-key map "\C-n" 'next-line)
68 (define-key map "p" 'previous-line)
69 (define-key map "n" 'next-line)
70 (define-key map "\C-v" 'scroll-up-command)
71 (define-key map "\ev" 'scroll-down-command)
72 (define-key map ">" 'scroll-right)
73 (define-key map "<" 'scroll-left)
74 (define-key map "\e\C-v" 'scroll-other-window)
75 (define-key map "\e>" 'end-of-buffer)
76 (define-key map "\e<" 'beginning-of-buffer)
77 (define-key map "\e\e" nil)
78 (define-key map "\e\e\e" 'Electric-buffer-menu-quit)
79 ;; This binding prevents the "escape => ESC" function-key-map mapping from
80 ;; kicking in!
81 ;; (define-key map [escape escape escape] 'Electric-buffer-menu-quit)
82 (define-key map [mouse-2] 'Electric-buffer-menu-mouse-select)
83 map))
85 (put 'Electric-buffer-menu-quit :advertised-binding "\C-c\C-c")
86 (put 'Electric-buffer-menu-select :advertised-binding " ")
87 (put 'Helper-help :advertised-binding (char-to-string help-char))
88 (put 'Helper-describe-bindings :advertised-binding "?")
90 (defvar electric-buffer-menu-mode-hook nil
91 "Normal hook run by `electric-buffer-menu-mode'.")
93 ;;;###autoload
94 (defun electric-buffer-list (arg)
95 "Pop up the Buffer Menu in an \"electric\" window.
96 If you type SPC or RET (`Electric-buffer-menu-select'), that
97 selects the buffer at point and quits the \"electric\" window.
98 Otherwise, you can move around in the Buffer Menu, marking
99 buffers to be selected, saved or deleted; these other commands
100 are much like those of `Buffer-menu-mode'.
102 Run hooks in `electric-buffer-menu-mode-hook' on entry.
104 \\<electric-buffer-menu-mode-map>
105 \\[keyboard-quit] or \\[Electric-buffer-menu-quit] -- exit buffer menu, returning to previous window and buffer
106 configuration. If the very first character typed is a space, it
107 also has this effect.
108 \\[Electric-buffer-menu-select] -- select buffer of line point is on.
109 Also show buffers marked with m in other windows,
110 deletes buffers marked with \"D\", and saves those marked with \"S\".
111 \\[Buffer-menu-mark] -- mark buffer to be displayed.
112 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
113 \\[Buffer-menu-save] -- mark that buffer to be saved.
114 \\[Buffer-menu-delete] or \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted.
115 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
116 \\[Electric-buffer-menu-mode-view-buffer] -- view buffer, returning when done.
117 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks."
118 (interactive "P")
119 (let (select buffer)
120 (save-window-excursion
121 (setq buffer (list-buffers-noselect arg))
122 (Electric-pop-up-window buffer)
123 (unwind-protect
124 (let ((header header-line-format))
125 (set-buffer buffer)
126 (electric-buffer-menu-mode)
127 (setq header-line-format header)
128 (goto-char (point-min))
129 (if (search-forward "\n." nil t)
130 (forward-char -1))
131 (electric-buffer-update-highlight)
132 (setq select
133 (catch 'electric-buffer-menu-select
134 (message "<<< Type SPC or RET to bury the buffer list >>>")
135 (setq unread-command-events (list (read-event)))
136 (let ((start-point (point))
137 (first (progn (goto-char (point-min))
138 (unless Buffer-menu-use-header-line
139 (forward-line 2))
140 (point)))
141 (last (progn (goto-char (point-max))
142 (forward-line -1)
143 (point)))
144 (goal-column 0))
145 ;; Use start-point if it is meaningful.
146 (goto-char (if (or (< start-point first)
147 (> start-point last))
148 first
149 start-point))
150 (Electric-command-loop 'electric-buffer-menu-select
153 'electric-buffer-menu-looper
154 (cons first last))))))
155 (set-buffer buffer)
156 (Buffer-menu-mode)
157 (bury-buffer) ;Get rid of window, if dedicated.
158 (message "")))
159 (when select
160 (set-buffer buffer)
161 (let ((opoint (point-marker)))
162 (Buffer-menu-execute)
163 (goto-char (point-min))
164 (if (prog1 (search-forward "\n>" nil t)
165 (goto-char opoint)
166 (set-marker opoint nil))
167 (Buffer-menu-select)
168 (switch-to-buffer (Buffer-menu-buffer t)))))))
170 (defun electric-buffer-menu-looper (state condition)
171 (cond ((and condition
172 (not (memq (car condition) '(buffer-read-only
173 end-of-buffer
174 beginning-of-buffer))))
175 (signal (car condition) (cdr condition)))
176 ((< (point) (car state))
177 (goto-char (point-min))
178 (unless Buffer-menu-use-header-line
179 (forward-line 2)))
180 ((> (point) (cdr state))
181 (goto-char (point-max))
182 (forward-line -1)
183 (if (pos-visible-in-window-p (point-max))
184 (recenter -1))))
185 (electric-buffer-update-highlight))
187 (defvar Helper-return-blurb)
189 (define-derived-mode electric-buffer-menu-mode Buffer-menu-mode
190 "Electric Buffer Menu"
191 "Toggle Electric Buffer Menu mode in this buffer.
192 With a prefix argument ARG, enable Long Lines mode if ARG is
193 positive, and disable it otherwise. If called from Lisp, enable
194 the mode if ARG is omitted or nil.
196 Electric Buffer Menu mode is a minor mode which is automatically
197 enabled and disabled by the \\[electric-buffer-list] command.
198 See the documentation of `electric-buffer-list' for details."
199 (setq mode-line-buffer-identification "Electric Buffer List")
200 (set (make-local-variable 'Helper-return-blurb)
201 "return to buffer editing"))
203 (define-obsolete-function-alias 'Electric-buffer-menu-mode
204 'electric-buffer-menu-mode "24.3")
206 ;; generally the same as Buffer-menu-mode-map
207 ;; (except we don't indirect to global-map)
208 (put 'Electric-buffer-menu-undefined 'suppress-keymap t)
210 (defun Electric-buffer-menu-exit ()
211 (interactive)
212 (setq unread-command-events (listify-key-sequence (this-command-keys)))
213 ;; for robustness
214 (condition-case ()
215 (throw 'electric-buffer-menu-select nil)
216 (error (Buffer-menu-mode)
217 (other-buffer))))
219 (defun Electric-buffer-menu-select ()
220 "Leave Electric Buffer Menu, selecting buffers and executing changes.
221 Save buffers marked \"S\". Delete buffers marked \"K\".
222 Select buffer at point and display buffers marked \">\" in other windows."
223 (interactive)
224 (throw 'electric-buffer-menu-select (point)))
226 (defun Electric-buffer-menu-mouse-select (event)
227 (interactive "e")
228 (select-window (posn-window (event-end event)))
229 (set-buffer (window-buffer (selected-window)))
230 (goto-char (posn-point (event-end event)))
231 (throw 'electric-buffer-menu-select (point)))
233 (defun Electric-buffer-menu-quit ()
234 "Leave Electric Buffer Menu, restoring previous window configuration.
235 Skip execution of select, save, and delete commands."
236 (interactive)
237 (throw 'electric-buffer-menu-select nil))
239 (defun Electric-buffer-menu-undefined ()
240 (interactive)
241 (ding)
242 (message "%s"
243 (substitute-command-keys "\
244 Type \\[Electric-buffer-menu-quit] to exit, \
245 \\[Electric-buffer-menu-select] to select, \
246 \\[Helper-help] for help, \\[Helper-describe-bindings] for commands."))
247 (sit-for 4))
249 (defun Electric-buffer-menu-mode-view-buffer ()
250 "View buffer on current line in Electric Buffer Menu.
251 Return to Electric Buffer Menu when done."
252 (interactive)
253 (let ((bufnam (Buffer-menu-buffer nil)))
254 (if bufnam
255 (view-buffer bufnam)
256 (ding)
257 (message "Buffer %s does not exist!" bufnam)
258 (sit-for 4))))
260 (defvar electric-buffer-overlay nil)
262 (defun electric-buffer-update-highlight ()
263 (when (derived-mode-p 'electric-buffer-menu-mode)
264 ;; Make sure we have an overlay to use.
265 (or electric-buffer-overlay
266 (set (make-local-variable 'electric-buffer-overlay)
267 (make-overlay (point) (point))))
268 (move-overlay electric-buffer-overlay
269 (line-beginning-position)
270 (line-end-position))
271 (overlay-put electric-buffer-overlay 'face 'highlight)))
273 (provide 'ebuff-menu)
275 ;;; ebuff-menu.el ends here