Ensure undo-boundary after insert-file-contents.
[emacs.git] / lisp / ebuff-menu.el
blob5536f946dc362bdabdefd13d900e351da639483f
1 ;;; ebuff-menu.el --- electric-buffer-list mode
3 ;; Copyright (C) 1985-1986, 1994, 2001-2016 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 (let ((i ?0))
59 (while (<= i ?9)
60 (define-key map (char-to-string i) 'digit-argument)
61 (define-key map (concat "\e" (char-to-string i)) 'digit-argument)
62 (setq i (1+ i))))
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
81 ;; kicking in!
82 ;; (define-key map [escape escape escape] 'Electric-buffer-menu-quit)
83 (define-key map [mouse-2] 'Electric-buffer-menu-mouse-select)
84 map))
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'.")
94 ;;;###autoload
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."
119 (interactive "P")
120 (let (select buffer)
121 (save-window-excursion
122 (setq buffer (list-buffers-noselect arg))
123 (Electric-pop-up-window buffer)
124 (unwind-protect
125 (let ((header header-line-format))
126 (set-buffer buffer)
127 (electric-buffer-menu-mode)
128 (setq header-line-format header)
129 (goto-char (point-min))
130 (if (search-forward "\n." nil t)
131 (forward-char -1))
132 (electric-buffer-update-highlight)
133 (setq select
134 (catch 'electric-buffer-menu-select
135 (message "<<< Type SPC or RET to bury the buffer list >>>")
136 (push (read-event) unread-command-events)
137 (let ((start-point (point))
138 (first (progn (goto-char (point-min))
139 (unless Buffer-menu-use-header-line
140 (forward-line 2))
141 (point)))
142 (last (progn (goto-char (point-max))
143 (forward-line -1)
144 (point)))
145 (goal-column 0))
146 ;; Use start-point if it is meaningful.
147 (goto-char (if (or (< start-point first)
148 (> start-point last))
149 first
150 start-point))
151 (Electric-command-loop 'electric-buffer-menu-select
154 'electric-buffer-menu-looper
155 (cons first last))))))
156 (set-buffer buffer)
157 (Buffer-menu-mode)
158 (bury-buffer) ;Get rid of window, if dedicated.
159 (message "")))
160 (when select
161 (set-buffer buffer)
162 (let ((opoint (point-marker)))
163 (Buffer-menu-execute)
164 (goto-char (point-min))
165 (if (prog1 (search-forward "\n>" nil t)
166 (goto-char opoint)
167 (set-marker opoint nil))
168 (Buffer-menu-select)
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
174 end-of-buffer
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
180 (forward-line 2)))
181 ((> (point) (cdr state))
182 (goto-char (point-max))
183 (forward-line -1)
184 (if (pos-visible-in-window-p (point-max))
185 (recenter -1))))
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 ()
212 (interactive)
213 (setq unread-command-events
214 (nconc (listify-key-sequence (this-command-keys))
215 unread-command-events))
216 ;; for robustness
217 (condition-case ()
218 (throw 'electric-buffer-menu-select nil)
219 (error (Buffer-menu-mode)
220 (other-buffer))))
222 (defun Electric-buffer-menu-select ()
223 "Leave Electric Buffer Menu, selecting buffers and executing changes.
224 Save buffers marked \"S\". Delete buffers marked \"K\".
225 Select buffer at point and display buffers marked \">\" in other windows."
226 (interactive)
227 (throw 'electric-buffer-menu-select (point)))
229 (defun Electric-buffer-menu-mouse-select (event)
230 (interactive "e")
231 (select-window (posn-window (event-end event)))
232 (set-buffer (window-buffer))
233 (goto-char (posn-point (event-end event)))
234 (throw 'electric-buffer-menu-select (point)))
236 (defun Electric-buffer-menu-quit ()
237 "Leave Electric Buffer Menu, restoring previous window configuration.
238 Skip execution of select, save, and delete commands."
239 (interactive)
240 (throw 'electric-buffer-menu-select nil))
242 (defun Electric-buffer-menu-undefined ()
243 (interactive)
244 (ding)
245 (message "%s"
246 (substitute-command-keys "\
247 Type \\[Electric-buffer-menu-quit] to exit, \
248 \\[Electric-buffer-menu-select] to select, \
249 \\[Helper-help] for help, \\[Helper-describe-bindings] for commands."))
250 (sit-for 4))
252 (defun Electric-buffer-menu-mode-view-buffer ()
253 "View buffer on current line in Electric Buffer Menu.
254 Return to Electric Buffer Menu when done."
255 (interactive)
256 (let ((bufnam (Buffer-menu-buffer nil)))
257 (if bufnam
258 (view-buffer bufnam)
259 (ding)
260 (message "Buffer %s does not exist!" bufnam)
261 (sit-for 4))))
263 (defvar electric-buffer-overlay nil)
265 (defun electric-buffer-update-highlight ()
266 (when (derived-mode-p 'electric-buffer-menu-mode)
267 ;; Make sure we have an overlay to use.
268 (or electric-buffer-overlay
269 (set (make-local-variable 'electric-buffer-overlay)
270 (make-overlay (point) (point))))
271 (move-overlay electric-buffer-overlay
272 (line-beginning-position)
273 (line-end-position))
274 (overlay-put electric-buffer-overlay 'face 'highlight)))
276 (provide 'ebuff-menu)
278 ;;; ebuff-menu.el ends here