1 ;;; ebuff-menu.el --- electric-buffer-list mode
3 ;; Copyright (C) 1985-1986, 1994, 2001-2018 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 <https://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
)
58 (define-key map
"\M-\177" 'Buffer-menu-unmark-all-buffers
)
59 (define-key map
"U" 'Buffer-menu-unmark-all
)
62 (define-key map
(char-to-string i
) 'digit-argument
)
63 (define-key map
(concat "\e" (char-to-string i
)) 'digit-argument
)
65 (define-key map
"-" 'negative-argument
)
66 (define-key map
"\e-" 'negative-argument
)
67 (define-key map
"m" 'Buffer-menu-mark
)
68 (define-key map
"\C-u" 'universal-argument
)
69 (define-key map
"\C-p" 'previous-line
)
70 (define-key map
"\C-n" 'next-line
)
71 (define-key map
"p" 'previous-line
)
72 (define-key map
"n" 'next-line
)
73 (define-key map
"\C-v" 'scroll-up-command
)
74 (define-key map
"\ev" 'scroll-down-command
)
75 (define-key map
">" 'scroll-right
)
76 (define-key map
"<" 'scroll-left
)
77 (define-key map
"\e\C-v" 'scroll-other-window
)
78 (define-key map
"\e>" 'end-of-buffer
)
79 (define-key map
"\e<" 'beginning-of-buffer
)
80 (define-key map
"\e\e" nil
)
81 (define-key map
"\e\e\e" 'Electric-buffer-menu-quit
)
82 ;; This binding prevents the "escape => ESC" function-key-map mapping from
84 ;; (define-key map [escape escape escape] 'Electric-buffer-menu-quit)
85 (define-key map
[mouse-2
] 'Electric-buffer-menu-mouse-select
)
88 (put 'Electric-buffer-menu-quit
:advertised-binding
"\C-c\C-c")
89 (put 'Electric-buffer-menu-select
:advertised-binding
" ")
90 (put 'Helper-help
:advertised-binding
(char-to-string help-char
))
91 (put 'Helper-describe-bindings
:advertised-binding
"?")
93 (defvar electric-buffer-menu-mode-hook nil
94 "Normal hook run by `electric-buffer-menu-mode'.")
97 (defun electric-buffer-list (arg)
98 "Pop up the Buffer Menu in an \"electric\" window.
99 If you type SPC or RET (`Electric-buffer-menu-select'), that
100 selects the buffer at point and quits the \"electric\" window.
101 Otherwise, you can move around in the Buffer Menu, marking
102 buffers to be selected, saved or deleted; these other commands
103 are much like those of `Buffer-menu-mode'.
105 Run hooks in `electric-buffer-menu-mode-hook' on entry.
107 \\<electric-buffer-menu-mode-map>
108 \\[keyboard-quit] or \\[Electric-buffer-menu-quit] -- exit buffer menu, returning to previous window and buffer
109 configuration. If the very first character typed is a space, it
110 also has this effect.
111 \\[Electric-buffer-menu-select] -- select buffer of line point is on.
112 Also show buffers marked with m in other windows,
113 deletes buffers marked with \"D\", and saves those marked with \"S\".
114 \\[Buffer-menu-mark] -- mark buffer to be displayed.
115 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
116 \\[Buffer-menu-save] -- mark that buffer to be saved.
117 \\[Buffer-menu-delete] or \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted.
118 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
119 \\[Buffer-menu-unmark-all] -- remove all kinds of marks from all lines.
120 \\[Electric-buffer-menu-mode-view-buffer] -- view buffer, returning when done.
121 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks."
124 (save-window-excursion
125 (setq buffer
(list-buffers-noselect arg
))
126 (Electric-pop-up-window buffer
)
128 (let ((header header-line-format
))
130 (electric-buffer-menu-mode)
131 (setq header-line-format header
)
132 (goto-char (point-min))
133 (if (search-forward "\n." nil t
)
135 (electric-buffer-update-highlight)
137 (catch 'electric-buffer-menu-select
138 (message "<<< Type SPC or RET to bury the buffer list >>>")
139 (push (read-event) unread-command-events
)
140 (let ((start-point (point))
141 (first (progn (goto-char (point-min))
142 (unless Buffer-menu-use-header-line
145 (last (progn (goto-char (point-max))
149 ;; Use start-point if it is meaningful.
150 (goto-char (if (or (< start-point first
)
151 (> start-point last
))
154 (Electric-command-loop 'electric-buffer-menu-select
157 'electric-buffer-menu-looper
158 (cons first last
))))))
161 (bury-buffer) ;Get rid of window, if dedicated.
165 (let ((opoint (point-marker)))
166 (Buffer-menu-execute)
167 (goto-char (point-min))
168 (if (prog1 (search-forward "\n>" nil t
)
170 (set-marker opoint nil
))
172 (switch-to-buffer (Buffer-menu-buffer t
)))))))
174 (defun electric-buffer-menu-looper (state condition
)
175 (cond ((and condition
176 (not (memq (car condition
) '(buffer-read-only
178 beginning-of-buffer
))))
179 (signal (car condition
) (cdr condition
)))
180 ((< (point) (car state
))
181 (goto-char (point-min))
182 (unless Buffer-menu-use-header-line
184 ((> (point) (cdr state
))
185 (goto-char (point-max))
187 (if (pos-visible-in-window-p (point-max))
189 (electric-buffer-update-highlight))
191 (defvar Helper-return-blurb
)
193 (define-derived-mode electric-buffer-menu-mode Buffer-menu-mode
194 "Electric Buffer Menu"
195 "Toggle Electric Buffer Menu mode in this buffer.
196 With a prefix argument ARG, enable Long Lines mode if ARG is
197 positive, and disable it otherwise. If called from Lisp, enable
198 the mode if ARG is omitted or nil.
200 Electric Buffer Menu mode is a minor mode which is automatically
201 enabled and disabled by the \\[electric-buffer-list] command.
202 See the documentation of `electric-buffer-list' for details."
203 (setq mode-line-buffer-identification
"Electric Buffer List")
204 (set (make-local-variable 'Helper-return-blurb
)
205 "return to buffer editing"))
207 (define-obsolete-function-alias 'Electric-buffer-menu-mode
208 'electric-buffer-menu-mode
"24.3")
210 ;; generally the same as Buffer-menu-mode-map
211 ;; (except we don't indirect to global-map)
212 (put 'Electric-buffer-menu-undefined
'suppress-keymap t
)
214 (defun Electric-buffer-menu-exit ()
216 (setq unread-command-events
217 (nconc (listify-key-sequence (this-command-keys))
218 unread-command-events
))
221 (throw 'electric-buffer-menu-select nil
)
222 (error (Buffer-menu-mode)
225 (defun Electric-buffer-menu-select ()
226 "Leave Electric Buffer Menu, selecting buffers and executing changes.
227 Save buffers marked \"S\". Delete buffers marked \"K\".
228 Select buffer at point and display buffers marked \">\" in other windows."
230 (throw 'electric-buffer-menu-select
(point)))
232 (defun Electric-buffer-menu-mouse-select (event)
234 (select-window (posn-window (event-end event
)))
235 (set-buffer (window-buffer))
236 (goto-char (posn-point (event-end event
)))
237 (throw 'electric-buffer-menu-select
(point)))
239 (defun Electric-buffer-menu-quit ()
240 "Leave Electric Buffer Menu, restoring previous window configuration.
241 Skip execution of select, save, and delete commands."
243 (throw 'electric-buffer-menu-select nil
))
245 (defun Electric-buffer-menu-undefined ()
249 (substitute-command-keys "\
250 Type \\[Electric-buffer-menu-quit] to exit, \
251 \\[Electric-buffer-menu-select] to select, \
252 \\[Helper-help] for help, \\[Helper-describe-bindings] for commands."))
255 (defun Electric-buffer-menu-mode-view-buffer ()
256 "View buffer on current line in Electric Buffer Menu.
257 Return to Electric Buffer Menu when done."
259 (let ((bufnam (Buffer-menu-buffer nil
)))
263 (message "Buffer %s does not exist!" bufnam
)
266 (defvar electric-buffer-overlay nil
)
268 (defun electric-buffer-update-highlight ()
269 (when (derived-mode-p 'electric-buffer-menu-mode
)
270 ;; Make sure we have an overlay to use.
271 (or electric-buffer-overlay
272 (set (make-local-variable 'electric-buffer-overlay
)
273 (make-overlay (point) (point))))
274 (move-overlay electric-buffer-overlay
275 (line-beginning-position)
277 (overlay-put electric-buffer-overlay
'face
'highlight
)))
279 (provide 'ebuff-menu
)
281 ;;; ebuff-menu.el ends here