Unbreak macOS build
[emacs.git] / lisp / ebuff-menu.el
blobb399be5d303377293c99db8ad230223c7695e648
1 ;;; ebuff-menu.el --- electric-buffer-list mode
3 ;; Copyright (C) 1985-1986, 1994, 2001-2017 Free Software Foundation,
4 ;; Inc.
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/>.
25 ;;; Commentary:
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.
31 ;;; Code:
33 (require 'electric)
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)
60 (let ((i ?0))
61 (while (<= i ?9)
62 (define-key map (char-to-string i) 'digit-argument)
63 (define-key map (concat "\e" (char-to-string i)) 'digit-argument)
64 (setq i (1+ i))))
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
83 ;; kicking in!
84 ;; (define-key map [escape escape escape] 'Electric-buffer-menu-quit)
85 (define-key map [mouse-2] 'Electric-buffer-menu-mouse-select)
86 map))
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'.")
96 ;;;###autoload
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."
122 (interactive "P")
123 (let (select buffer)
124 (save-window-excursion
125 (setq buffer (list-buffers-noselect arg))
126 (Electric-pop-up-window buffer)
127 (unwind-protect
128 (let ((header header-line-format))
129 (set-buffer buffer)
130 (electric-buffer-menu-mode)
131 (setq header-line-format header)
132 (goto-char (point-min))
133 (if (search-forward "\n." nil t)
134 (forward-char -1))
135 (electric-buffer-update-highlight)
136 (setq select
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
143 (forward-line 2))
144 (point)))
145 (last (progn (goto-char (point-max))
146 (forward-line -1)
147 (point)))
148 (goal-column 0))
149 ;; Use start-point if it is meaningful.
150 (goto-char (if (or (< start-point first)
151 (> start-point last))
152 first
153 start-point))
154 (Electric-command-loop 'electric-buffer-menu-select
157 'electric-buffer-menu-looper
158 (cons first last))))))
159 (set-buffer buffer)
160 (Buffer-menu-mode)
161 (bury-buffer) ;Get rid of window, if dedicated.
162 (message "")))
163 (when select
164 (set-buffer buffer)
165 (let ((opoint (point-marker)))
166 (Buffer-menu-execute)
167 (goto-char (point-min))
168 (if (prog1 (search-forward "\n>" nil t)
169 (goto-char opoint)
170 (set-marker opoint nil))
171 (Buffer-menu-select)
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
177 end-of-buffer
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
183 (forward-line 2)))
184 ((> (point) (cdr state))
185 (goto-char (point-max))
186 (forward-line -1)
187 (if (pos-visible-in-window-p (point-max))
188 (recenter -1))))
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 ()
215 (interactive)
216 (setq unread-command-events
217 (nconc (listify-key-sequence (this-command-keys))
218 unread-command-events))
219 ;; for robustness
220 (condition-case ()
221 (throw 'electric-buffer-menu-select nil)
222 (error (Buffer-menu-mode)
223 (other-buffer))))
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."
229 (interactive)
230 (throw 'electric-buffer-menu-select (point)))
232 (defun Electric-buffer-menu-mouse-select (event)
233 (interactive "e")
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."
242 (interactive)
243 (throw 'electric-buffer-menu-select nil))
245 (defun Electric-buffer-menu-undefined ()
246 (interactive)
247 (ding)
248 (message "%s"
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."))
253 (sit-for 4))
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."
258 (interactive)
259 (let ((bufnam (Buffer-menu-buffer nil)))
260 (if bufnam
261 (view-buffer bufnam)
262 (ding)
263 (message "Buffer %s does not exist!" bufnam)
264 (sit-for 4))))
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)
276 (line-end-position))
277 (overlay-put electric-buffer-overlay 'face 'highlight)))
279 (provide 'ebuff-menu)
281 ;;; ebuff-menu.el ends here