1 ;;; ebuff-menu.el --- electric-buffer-list mode
3 ;; Copyright (C) 1985-1986, 1994, 2001-2014 Free Software Foundation,
6 ;; Author: Richard Mlynarik <mly@ai.mit.edu>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: convenience
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Who says one can't have typeout windows in GNU Emacs? The entry
28 ;; point, `electric-buffer-list' works like ^r select buffer from the
29 ;; ITS Emacs lunar or tmacs libraries.
35 (defvar electric-buffer-menu-mode-map
36 (let ((map (make-keymap)))
37 (fillarray (car (cdr map
)) 'Electric-buffer-menu-undefined
)
38 (define-key map
"\e" nil
)
39 (define-key map
"\C-z" 'suspend-frame
)
40 (define-key map
"v" 'Electric-buffer-menu-mode-view-buffer
)
41 (define-key map
(char-to-string help-char
) 'Helper-help
)
42 (define-key map
"?" 'Helper-describe-bindings
)
43 (define-key map
"\C-c" nil
)
44 (define-key map
"\C-c\C-c" 'Electric-buffer-menu-quit
)
45 (define-key map
"\C-]" 'Electric-buffer-menu-quit
)
46 (define-key map
"q" 'Electric-buffer-menu-quit
)
47 (define-key map
" " 'Electric-buffer-menu-select
)
48 (define-key map
"\C-m" 'Electric-buffer-menu-select
)
49 (define-key map
"\C-l" 'recenter
)
50 (define-key map
"s" 'Buffer-menu-save
)
51 (define-key map
"d" 'Buffer-menu-delete
)
52 (define-key map
"k" 'Buffer-menu-delete
)
53 (define-key map
"\C-d" 'Buffer-menu-delete-backwards
)
54 ;; (define-key map "\C-k" 'Buffer-menu-delete)
55 (define-key map
"\177" 'Buffer-menu-backup-unmark
)
56 (define-key map
"~" 'Buffer-menu-not-modified
)
57 (define-key map
"u" 'Buffer-menu-unmark
)
60 (define-key map
(char-to-string i
) 'digit-argument
)
61 (define-key map
(concat "\e" (char-to-string i
)) 'digit-argument
)
63 (define-key map
"-" 'negative-argument
)
64 (define-key map
"\e-" 'negative-argument
)
65 (define-key map
"m" 'Buffer-menu-mark
)
66 (define-key map
"\C-u" 'universal-argument
)
67 (define-key map
"\C-p" 'previous-line
)
68 (define-key map
"\C-n" 'next-line
)
69 (define-key map
"p" 'previous-line
)
70 (define-key map
"n" 'next-line
)
71 (define-key map
"\C-v" 'scroll-up-command
)
72 (define-key map
"\ev" 'scroll-down-command
)
73 (define-key map
">" 'scroll-right
)
74 (define-key map
"<" 'scroll-left
)
75 (define-key map
"\e\C-v" 'scroll-other-window
)
76 (define-key map
"\e>" 'end-of-buffer
)
77 (define-key map
"\e<" 'beginning-of-buffer
)
78 (define-key map
"\e\e" nil
)
79 (define-key map
"\e\e\e" 'Electric-buffer-menu-quit
)
80 ;; This binding prevents the "escape => ESC" function-key-map mapping from
82 ;; (define-key map [escape escape escape] 'Electric-buffer-menu-quit)
83 (define-key map
[mouse-2
] 'Electric-buffer-menu-mouse-select
)
86 (put 'Electric-buffer-menu-quit
:advertised-binding
"\C-c\C-c")
87 (put 'Electric-buffer-menu-select
:advertised-binding
" ")
88 (put 'Helper-help
:advertised-binding
(char-to-string help-char
))
89 (put 'Helper-describe-bindings
:advertised-binding
"?")
91 (defvar electric-buffer-menu-mode-hook nil
92 "Normal hook run by `electric-buffer-menu-mode'.")
95 (defun electric-buffer-list (arg)
96 "Pop up the Buffer Menu in an \"electric\" window.
97 If you type SPC or RET (`Electric-buffer-menu-select'), that
98 selects the buffer at point and quits the \"electric\" window.
99 Otherwise, you can move around in the Buffer Menu, marking
100 buffers to be selected, saved or deleted; these other commands
101 are much like those of `Buffer-menu-mode'.
103 Run hooks in `electric-buffer-menu-mode-hook' on entry.
105 \\<electric-buffer-menu-mode-map>
106 \\[keyboard-quit] or \\[Electric-buffer-menu-quit] -- exit buffer menu, returning to previous window and buffer
107 configuration. If the very first character typed is a space, it
108 also has this effect.
109 \\[Electric-buffer-menu-select] -- select buffer of line point is on.
110 Also show buffers marked with m in other windows,
111 deletes buffers marked with \"D\", and saves those marked with \"S\".
112 \\[Buffer-menu-mark] -- mark buffer to be displayed.
113 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
114 \\[Buffer-menu-save] -- mark that buffer to be saved.
115 \\[Buffer-menu-delete] or \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted.
116 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
117 \\[Electric-buffer-menu-mode-view-buffer] -- view buffer, returning when done.
118 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks."
121 (save-window-excursion
122 (setq buffer
(list-buffers-noselect arg
))
123 (Electric-pop-up-window buffer
)
125 (let ((header header-line-format
))
127 (electric-buffer-menu-mode)
128 (setq header-line-format header
)
129 (goto-char (point-min))
130 (if (search-forward "\n." nil t
)
132 (electric-buffer-update-highlight)
134 (catch 'electric-buffer-menu-select
135 (message "<<< Type SPC or RET to bury the buffer list >>>")
136 (setq unread-command-events
(list (read-event)))
137 (let ((start-point (point))
138 (first (progn (goto-char (point-min))
139 (unless Buffer-menu-use-header-line
142 (last (progn (goto-char (point-max))
146 ;; Use start-point if it is meaningful.
147 (goto-char (if (or (< start-point first
)
148 (> start-point last
))
151 (Electric-command-loop 'electric-buffer-menu-select
154 'electric-buffer-menu-looper
155 (cons first last
))))))
158 (bury-buffer) ;Get rid of window, if dedicated.
162 (let ((opoint (point-marker)))
163 (Buffer-menu-execute)
164 (goto-char (point-min))
165 (if (prog1 (search-forward "\n>" nil t
)
167 (set-marker opoint nil
))
169 (switch-to-buffer (Buffer-menu-buffer t
)))))))
171 (defun electric-buffer-menu-looper (state condition
)
172 (cond ((and condition
173 (not (memq (car condition
) '(buffer-read-only
175 beginning-of-buffer
))))
176 (signal (car condition
) (cdr condition
)))
177 ((< (point) (car state
))
178 (goto-char (point-min))
179 (unless Buffer-menu-use-header-line
181 ((> (point) (cdr state
))
182 (goto-char (point-max))
184 (if (pos-visible-in-window-p (point-max))
186 (electric-buffer-update-highlight))
188 (defvar Helper-return-blurb
)
190 (define-derived-mode electric-buffer-menu-mode Buffer-menu-mode
191 "Electric Buffer Menu"
192 "Toggle Electric Buffer Menu mode in this buffer.
193 With a prefix argument ARG, enable Long Lines mode if ARG is
194 positive, and disable it otherwise. If called from Lisp, enable
195 the mode if ARG is omitted or nil.
197 Electric Buffer Menu mode is a minor mode which is automatically
198 enabled and disabled by the \\[electric-buffer-list] command.
199 See the documentation of `electric-buffer-list' for details."
200 (setq mode-line-buffer-identification
"Electric Buffer List")
201 (set (make-local-variable 'Helper-return-blurb
)
202 "return to buffer editing"))
204 (define-obsolete-function-alias 'Electric-buffer-menu-mode
205 'electric-buffer-menu-mode
"24.3")
207 ;; generally the same as Buffer-menu-mode-map
208 ;; (except we don't indirect to global-map)
209 (put 'Electric-buffer-menu-undefined
'suppress-keymap t
)
211 (defun Electric-buffer-menu-exit ()
213 (setq unread-command-events
(listify-key-sequence (this-command-keys)))
216 (throw 'electric-buffer-menu-select nil
)
217 (error (Buffer-menu-mode)
220 (defun Electric-buffer-menu-select ()
221 "Leave Electric Buffer Menu, selecting buffers and executing changes.
222 Save buffers marked \"S\". Delete buffers marked \"K\".
223 Select buffer at point and display buffers marked \">\" in other windows."
225 (throw 'electric-buffer-menu-select
(point)))
227 (defun Electric-buffer-menu-mouse-select (event)
229 (select-window (posn-window (event-end event
)))
230 (set-buffer (window-buffer))
231 (goto-char (posn-point (event-end event
)))
232 (throw 'electric-buffer-menu-select
(point)))
234 (defun Electric-buffer-menu-quit ()
235 "Leave Electric Buffer Menu, restoring previous window configuration.
236 Skip execution of select, save, and delete commands."
238 (throw 'electric-buffer-menu-select nil
))
240 (defun Electric-buffer-menu-undefined ()
244 (substitute-command-keys "\
245 Type \\[Electric-buffer-menu-quit] to exit, \
246 \\[Electric-buffer-menu-select] to select, \
247 \\[Helper-help] for help, \\[Helper-describe-bindings] for commands."))
250 (defun Electric-buffer-menu-mode-view-buffer ()
251 "View buffer on current line in Electric Buffer Menu.
252 Return to Electric Buffer Menu when done."
254 (let ((bufnam (Buffer-menu-buffer nil
)))
258 (message "Buffer %s does not exist!" bufnam
)
261 (defvar electric-buffer-overlay nil
)
263 (defun electric-buffer-update-highlight ()
264 (when (derived-mode-p 'electric-buffer-menu-mode
)
265 ;; Make sure we have an overlay to use.
266 (or electric-buffer-overlay
267 (set (make-local-variable 'electric-buffer-overlay
)
268 (make-overlay (point) (point))))
269 (move-overlay electric-buffer-overlay
270 (line-beginning-position)
272 (overlay-put electric-buffer-overlay
'face
'highlight
)))
274 (provide 'ebuff-menu
)
276 ;;; ebuff-menu.el ends here