(rmail-convert-babyl-to-mbox): Bind coding-system-for-read to `raw-text', and
[emacs.git] / lisp / buff-menu.el
bloba644b2ffccbac5abff3b9cc9830999c41a2d46ad
1 ;;; buff-menu.el --- buffer menu main function and support functions -*- coding:utf-8 -*-
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
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 ;; Edit, delete, or change attributes of all currently active Emacs
27 ;; buffers from a list summarizing their state. A good way to browse
28 ;; any special or scratch buffers you have loaded, since you can't find
29 ;; them by filename. The single entry point is `list-buffers',
30 ;; normally bound to C-x C-b.
32 ;;; Change Log:
34 ;; Buffer-menu-view: New function
35 ;; Buffer-menu-view-other-window: New function
37 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
39 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
41 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
42 ;; current entry and then move to previous one.
44 ;; Based on FSF code dating back to 1985.
46 ;;; Code:
48 ;;Trying to preserve the old window configuration works well in
49 ;;simple scenarios, when you enter the buffer menu, use it, and exit it.
50 ;;But it does strange things when you switch back to the buffer list buffer
51 ;;with C-x b, later on, when the window configuration is different.
52 ;;The choice seems to be, either restore the window configuration
53 ;;in all cases, or in no cases.
54 ;;I decided it was better not to restore the window config at all. -- rms.
56 ;;But since then, I changed buffer-menu to use the selected window,
57 ;;so q now once again goes back to the previous window configuration.
59 ;;(defvar Buffer-menu-window-config nil
60 ;; "Window configuration saved from entry to `buffer-menu'.")
62 ;; Put buffer *Buffer List* into proper mode right away
63 ;; so that from now on even list-buffers is enough to get a buffer menu.
65 (defgroup Buffer-menu nil
66 "Show a menu of all buffers in a buffer."
67 :group 'tools
68 :group 'convenience)
70 (defcustom Buffer-menu-use-header-line t
71 "Non-nil means to use an immovable header-line."
72 :type 'boolean
73 :group 'Buffer-menu)
75 (defface buffer-menu-buffer
76 '((t (:weight bold)))
77 "Face used to highlight buffer names in the buffer menu."
78 :group 'Buffer-menu)
79 (put 'Buffer-menu-buffer 'face-alias 'buffer-menu-buffer)
81 (defcustom Buffer-menu-buffer+size-width 26
82 "How wide to jointly make the buffer name and size columns."
83 :type 'number
84 :group 'Buffer-menu)
86 (defcustom Buffer-menu-mode-width 16
87 "How wide to make the mode name column."
88 :type 'number
89 :group 'Buffer-menu)
91 (defcustom Buffer-menu-use-frame-buffer-list t
92 "If non-nil, the Buffer Menu uses the selected frame's buffer list.
93 Buffers that were never selected in that frame are listed at the end.
94 If the value is nil, the Buffer Menu uses the global buffer list.
95 This variable matters if the Buffer Menu is sorted by visited order,
96 as it is by default."
97 :type 'boolean
98 :group 'Buffer-menu
99 :version "22.1")
101 ;; This should get updated & resorted when you click on a column heading
102 (defvar Buffer-menu-sort-column nil
103 "Which column to sort the menu on.
104 Use 2 to sort by buffer names, or 5 to sort by file names.
105 A nil value means sort by visited order (the default).")
107 (defconst Buffer-menu-buffer-column 4)
109 (defvar Buffer-menu-files-only nil
110 "Non-nil if the current buffer-menu lists only file buffers.
111 This variable determines whether reverting the buffer lists only
112 file buffers. It affects both manual reverting and reverting by
113 Auto Revert Mode.")
115 (make-variable-buffer-local 'Buffer-menu-files-only)
117 (defvar Info-current-file) ;; from info.el
118 (defvar Info-current-node) ;; from info.el
120 (defvar Buffer-menu-mode-map
121 (let ((map (make-keymap))
122 (menu-map (make-sparse-keymap)))
123 (suppress-keymap map t)
124 (define-key map "v" 'Buffer-menu-select)
125 (define-key map "2" 'Buffer-menu-2-window)
126 (define-key map "1" 'Buffer-menu-1-window)
127 (define-key map "f" 'Buffer-menu-this-window)
128 (define-key map "e" 'Buffer-menu-this-window)
129 (define-key map "\C-m" 'Buffer-menu-this-window)
130 (define-key map "o" 'Buffer-menu-other-window)
131 (define-key map "\C-o" 'Buffer-menu-switch-other-window)
132 (define-key map "s" 'Buffer-menu-save)
133 (define-key map "d" 'Buffer-menu-delete)
134 (define-key map "k" 'Buffer-menu-delete)
135 (define-key map "\C-d" 'Buffer-menu-delete-backwards)
136 (define-key map "\C-k" 'Buffer-menu-delete)
137 (define-key map "x" 'Buffer-menu-execute)
138 (define-key map " " 'next-line)
139 (define-key map "n" 'next-line)
140 (define-key map "p" 'previous-line)
141 (define-key map "\177" 'Buffer-menu-backup-unmark)
142 (define-key map "~" 'Buffer-menu-not-modified)
143 (define-key map "u" 'Buffer-menu-unmark)
144 (define-key map "m" 'Buffer-menu-mark)
145 (define-key map "t" 'Buffer-menu-visit-tags-table)
146 (define-key map "%" 'Buffer-menu-toggle-read-only)
147 (define-key map "b" 'Buffer-menu-bury)
148 (define-key map "V" 'Buffer-menu-view)
149 (define-key map "T" 'Buffer-menu-toggle-files-only)
150 (define-key map [mouse-2] 'Buffer-menu-mouse-select)
151 (define-key map [follow-link] 'mouse-face)
152 (define-key map (kbd "M-s a C-s") 'Buffer-menu-isearch-buffers)
153 (define-key map (kbd "M-s a M-C-s") 'Buffer-menu-isearch-buffers-regexp)
154 (define-key map [menu-bar Buffer-menu-mode] (cons "Buffer Menu" menu-map))
155 (define-key menu-map [quit]
156 '(menu-item "Quit" quit-window
157 :help "Mark buffer on this line to be deleted by x command"))
158 (define-key menu-map [rev]
159 '(menu-item "Refresh" revert-buffer
160 :help "Refresh the *Buffer List* buffer contents"))
161 (define-key menu-map [s0] '("--"))
162 (define-key menu-map [tf]
163 '(menu-item "Show only file buffers" Buffer-menu-toggle-files-only
164 :button (:toggle . Buffer-menu-files-only)
165 :help "Toggle whether the current buffer-menu displays only file buffers"))
166 (define-key menu-map [s1] '("--"))
167 ;; FIXME: The "Select" entries could use better names...
168 (define-key menu-map [sel]
169 '(menu-item "Select marked" Buffer-menu-select
170 :help "Select this line's buffer; also display buffers marked with `>'"))
171 (define-key menu-map [bm2]
172 '(menu-item "Select two" Buffer-menu-2-window
173 :help "Select this line's buffer, with previous buffer in second window"))
174 (define-key menu-map [bm1]
175 '(menu-item "Select current" Buffer-menu-1-window
176 :help "Select this line's buffer, alone, in full frame"))
177 (define-key menu-map [ow]
178 '(menu-item "Select in other window" Buffer-menu-other-window
179 :help "Select this line's buffer in other window, leaving buffer menu visible"))
180 (define-key menu-map [tw]
181 '(menu-item "Select in current window" Buffer-menu-this-window
182 :help "Select this line's buffer in this window"))
183 (define-key menu-map [s2] '("--"))
184 (define-key menu-map [is]
185 '(menu-item "Regexp Isearch marked buffers" Buffer-menu-isearch-buffers-regexp
186 :help "Search for a regexp through all marked buffers using Isearch"))
187 (define-key menu-map [ir]
188 '(menu-item "Isearch marked buffers" Buffer-menu-isearch-buffers
189 :help "Search for a string through all marked buffers using Isearch"))
190 (define-key menu-map [s3] '("--"))
191 (define-key menu-map [by]
192 '(menu-item "Bury" Buffer-menu-bury
193 :help "Bury the buffer listed on this line"))
194 (define-key menu-map [vt]
195 '(menu-item "Set unmodified" Buffer-menu-not-modified
196 :help "Mark buffer on this line as unmodified (no changes to save)"))
197 (define-key menu-map [ex]
198 '(menu-item "Execute" Buffer-menu-execute
199 :help "Save and/or delete buffers marked with s or k commands"))
200 (define-key menu-map [s4] '("--"))
201 (define-key menu-map [delb]
202 '(menu-item "Mark for delete and move backwards" Buffer-menu-delete-backwards
203 :help "Mark buffer on this line to be deleted by x command and move up one line"))
204 (define-key menu-map [del]
205 '(menu-item "Mark for delete" Buffer-menu-delete
206 :help "Mark buffer on this line to be deleted by x command"))
208 (define-key menu-map [sv]
209 '(menu-item "Mark for save" Buffer-menu-save
210 :help "Mark buffer on this line to be saved by x command"))
211 (define-key menu-map [umk]
212 '(menu-item "Unmark" Buffer-menu-unmark
213 :help "Cancel all requested operations on buffer on this line and move down"))
214 (define-key menu-map [mk]
215 '(menu-item "Mark" Buffer-menu-mark
216 :help "Mark buffer on this line for being displayed by v command"))
217 map)
218 "Local keymap for `Buffer-menu-mode' buffers.")
220 ;; Buffer Menu mode is suitable only for specially formatted data.
221 (put 'Buffer-menu-mode 'mode-class 'special)
223 (define-derived-mode Buffer-menu-mode special-mode "Buffer Menu"
224 "Major mode for editing a list of buffers.
225 Each line describes one of the buffers in Emacs.
226 Letters do not insert themselves; instead, they are commands.
227 \\<Buffer-menu-mode-map>
228 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
229 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
230 \\[Buffer-menu-other-window] -- select that buffer in another window,
231 so the buffer menu buffer remains visible in its window.
232 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
233 \\[Buffer-menu-view-other-window] -- select that buffer in
234 another window, in view-mode.
235 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
236 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
237 \\[Buffer-menu-select] -- select current line's buffer.
238 Also show buffers marked with m, in other windows.
239 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
240 \\[Buffer-menu-2-window] -- select that buffer in one window,
241 together with buffer selected before this one in another window.
242 \\[Buffer-menu-isearch-buffers] -- Do incremental search in the marked buffers.
243 \\[Buffer-menu-isearch-buffers-regexp] -- Isearch for regexp in the marked buffers.
244 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
245 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
246 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
247 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
248 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
249 \\[Buffer-menu-execute] -- delete or save marked buffers.
250 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
251 With prefix argument, also move up one line.
252 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
253 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
254 \\[revert-buffer] -- update the list of buffers.
255 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
256 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
257 (set (make-local-variable 'revert-buffer-function)
258 'Buffer-menu-revert-function)
259 (set (make-local-variable 'buffer-stale-function)
260 #'(lambda (&optional noconfirm) 'fast))
261 (setq truncate-lines t)
262 (setq buffer-read-only t))
264 (define-obsolete-variable-alias 'buffer-menu-mode-hook
265 'Buffer-menu-mode-hook "23.1")
267 (defun Buffer-menu-revert-function (ignore1 ignore2)
268 (or (eq buffer-undo-list t)
269 (setq buffer-undo-list nil))
270 ;; We can not use save-excursion here. The buffer gets erased.
271 (let ((opoint (point))
272 (eobp (eobp))
273 (ocol (current-column))
274 (oline (progn (move-to-column 4)
275 (get-text-property (point) 'buffer)))
276 (prop (point-min))
277 ;; do not make undo records for the reversion.
278 (buffer-undo-list t))
279 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
280 ;; temporarily the current buffer. Make sure that the
281 ;; interactively current buffer is correctly identified with a `.'
282 ;; by `list-buffers-noselect'.
283 (with-current-buffer (window-buffer)
284 (list-buffers-noselect Buffer-menu-files-only))
285 (if oline
286 (while (setq prop (next-single-property-change prop 'buffer))
287 (when (eq (get-text-property prop 'buffer) oline)
288 (goto-char prop)
289 (move-to-column ocol)))
290 (goto-char (if eobp (point-max) opoint)))))
292 (defun Buffer-menu-toggle-files-only (arg)
293 "Toggle whether the current buffer-menu displays only file buffers.
294 With a positive ARG display only file buffers. With zero or
295 negative ARG, display other buffers as well."
296 (interactive "P")
297 (setq Buffer-menu-files-only
298 (cond ((not arg) (not Buffer-menu-files-only))
299 ((> (prefix-numeric-value arg) 0) t)))
300 (revert-buffer))
303 (defun Buffer-menu-buffer (error-if-non-existent-p)
304 "Return buffer described by this line of buffer menu."
305 (let* ((where (save-excursion
306 (beginning-of-line)
307 (+ (point) Buffer-menu-buffer-column)))
308 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
309 (buf (and (not (eobp)) (get-text-property where 'buffer))))
310 (if name
311 (or (get-buffer name)
312 (and buf (buffer-name buf) buf)
313 (if error-if-non-existent-p
314 (error "No buffer named `%s'" name)
315 nil))
316 (or (and buf (buffer-name buf) buf)
317 (if error-if-non-existent-p
318 (error "No buffer on this line")
319 nil)))))
321 (defun buffer-menu (&optional arg)
322 "Make a menu of buffers so you can save, delete or select them.
323 With argument, show only buffers that are visiting files.
324 Type ? after invocation to get help on commands available.
325 Type q to remove the buffer menu from the display.
327 The first column shows `>' for a buffer you have
328 marked to be displayed, `D' for one you have marked for
329 deletion, and `.' for the current buffer.
331 The C column has a `.' for the buffer from which you came.
332 The R column has a `%' if the buffer is read-only.
333 The M column has a `*' if it is modified,
334 or `S' if you have marked it for saving.
335 After this come the buffer name, its size in characters,
336 its major mode, and the visited file name (if any)."
337 (interactive "P")
338 ;;; (setq Buffer-menu-window-config (current-window-configuration))
339 (switch-to-buffer (list-buffers-noselect arg))
340 (message
341 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
343 (defun buffer-menu-other-window (&optional arg)
344 "Display a list of buffers in another window.
345 With the buffer list buffer, you can save, delete or select the buffers.
346 With argument, show only buffers that are visiting files.
347 Type ? after invocation to get help on commands available.
348 Type q to remove the buffer menu from the display.
349 For more information, see the function `buffer-menu'."
350 (interactive "P")
351 ;;; (setq Buffer-menu-window-config (current-window-configuration))
352 (switch-to-buffer-other-window (list-buffers-noselect arg))
353 (message
354 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
356 (defun Buffer-menu-no-header ()
357 (beginning-of-line)
358 (if (or Buffer-menu-use-header-line
359 (not (eq (char-after) ?C)))
361 (ding)
362 (forward-line 1)
363 nil))
365 (defun Buffer-menu-mark ()
366 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
367 (interactive)
368 (when (Buffer-menu-no-header)
369 (let ((inhibit-read-only t))
370 (delete-char 1)
371 (insert ?>)
372 (forward-line 1))))
374 (defun Buffer-menu-unmark (&optional backup)
375 "Cancel all requested operations on buffer on this line and move down.
376 Optional prefix arg means move up."
377 (interactive "P")
378 (when (Buffer-menu-no-header)
379 (let* ((buf (Buffer-menu-buffer t))
380 (mod (buffer-modified-p buf))
381 (readonly (with-current-buffer buf buffer-read-only))
382 (inhibit-read-only t))
383 (delete-char 3)
384 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
385 (forward-line (if backup -1 1)))
387 (defun Buffer-menu-backup-unmark ()
388 "Move up and cancel all requested operations on buffer on line above."
389 (interactive)
390 (forward-line -1)
391 (Buffer-menu-unmark)
392 (forward-line -1))
394 (defun Buffer-menu-delete (&optional arg)
395 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
396 Prefix arg is how many buffers to delete.
397 Negative arg means delete backwards."
398 (interactive "p")
399 (when (Buffer-menu-no-header)
400 (let ((inhibit-read-only t))
401 (if (or (null arg) (= arg 0))
402 (setq arg 1))
403 (while (> arg 0)
404 (delete-char 1)
405 (insert ?D)
406 (forward-line 1)
407 (setq arg (1- arg)))
408 (while (and (< arg 0)
409 (Buffer-menu-no-header))
410 (delete-char 1)
411 (insert ?D)
412 (forward-line -1)
413 (setq arg (1+ arg))))))
415 (defun Buffer-menu-delete-backwards (&optional arg)
416 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
417 and then move up one line. Prefix arg means move that many lines."
418 (interactive "p")
419 (Buffer-menu-delete (- (or arg 1))))
421 (defun Buffer-menu-save ()
422 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
423 (interactive)
424 (when (Buffer-menu-no-header)
425 (let ((inhibit-read-only t))
426 (forward-char 2)
427 (delete-char 1)
428 (insert ?S)
429 (forward-line 1))))
431 (defun Buffer-menu-not-modified (&optional arg)
432 "Mark buffer on this line as unmodified (no changes to save)."
433 (interactive "P")
434 (with-current-buffer (Buffer-menu-buffer t)
435 (set-buffer-modified-p arg))
436 (save-excursion
437 (beginning-of-line)
438 (forward-char 2)
439 (if (= (char-after) (if arg ?\s ?*))
440 (let ((inhibit-read-only t))
441 (delete-char 1)
442 (insert (if arg ?* ?\s))))))
444 (defun Buffer-menu-beginning ()
445 (goto-char (point-min))
446 (unless Buffer-menu-use-header-line
447 (forward-line)))
449 (defun Buffer-menu-execute ()
450 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
451 (interactive)
452 (save-excursion
453 (Buffer-menu-beginning)
454 (while (re-search-forward "^..S" nil t)
455 (let ((modp nil))
456 (with-current-buffer (Buffer-menu-buffer t)
457 (save-buffer)
458 (setq modp (buffer-modified-p)))
459 (let ((inhibit-read-only t))
460 (delete-char -1)
461 (insert (if modp ?* ?\s))))))
462 (save-excursion
463 (Buffer-menu-beginning)
464 (let ((buff-menu-buffer (current-buffer))
465 (inhibit-read-only t))
466 (while (re-search-forward "^D" nil t)
467 (forward-char -1)
468 (let ((buf (Buffer-menu-buffer nil)))
469 (or (eq buf nil)
470 (eq buf buff-menu-buffer)
471 (save-excursion (kill-buffer buf)))
472 (if (and buf (buffer-name buf))
473 (progn (delete-char 1)
474 (insert ?\s))
475 (delete-region (point) (progn (forward-line 1) (point)))
476 (unless (bobp)
477 (forward-char -1))))))))
479 (defun Buffer-menu-select ()
480 "Select this line's buffer; also display buffers marked with `>'.
481 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
482 This command deletes and replaces all the previously existing windows
483 in the selected frame."
484 (interactive)
485 (let ((buff (Buffer-menu-buffer t))
486 (menu (current-buffer))
487 (others ())
488 tem)
489 (Buffer-menu-beginning)
490 (while (re-search-forward "^>" nil t)
491 (setq tem (Buffer-menu-buffer t))
492 (let ((inhibit-read-only t))
493 (delete-char -1)
494 (insert ?\s))
495 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
496 (setq others (nreverse others)
497 tem (/ (1- (frame-height)) (1+ (length others))))
498 (delete-other-windows)
499 (switch-to-buffer buff)
500 (or (eq menu buff)
501 (bury-buffer menu))
502 (if (equal (length others) 0)
503 (progn
504 ;;; ;; Restore previous window configuration before displaying
505 ;;; ;; selected buffers.
506 ;;; (if Buffer-menu-window-config
507 ;;; (progn
508 ;;; (set-window-configuration Buffer-menu-window-config)
509 ;;; (setq Buffer-menu-window-config nil)))
510 (switch-to-buffer buff))
511 (while others
512 (split-window nil tem)
513 (other-window 1)
514 (switch-to-buffer (car others))
515 (setq others (cdr others)))
516 (other-window 1) ;back to the beginning!
519 (defun Buffer-menu-marked-buffers ()
520 "Return a list of buffers marked with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
521 (let (buffers)
522 (Buffer-menu-beginning)
523 (while (re-search-forward "^>" nil t)
524 (setq buffers (cons (Buffer-menu-buffer t) buffers)))
525 (nreverse buffers)))
527 (defun Buffer-menu-isearch-buffers ()
528 "Search for a string through all marked buffers using Isearch."
529 (interactive)
530 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
532 (defun Buffer-menu-isearch-buffers-regexp ()
533 "Search for a regexp through all marked buffers using Isearch."
534 (interactive)
535 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
538 (defun Buffer-menu-visit-tags-table ()
539 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
540 (interactive)
541 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
542 (if file
543 (visit-tags-table file)
544 (error "Specified buffer has no file"))))
546 (defun Buffer-menu-1-window ()
547 "Select this line's buffer, alone, in full frame."
548 (interactive)
549 (switch-to-buffer (Buffer-menu-buffer t))
550 (bury-buffer (other-buffer))
551 (delete-other-windows))
553 (defun Buffer-menu-mouse-select (event)
554 "Select the buffer whose line you click on."
555 (interactive "e")
556 (let (buffer)
557 (with-current-buffer (window-buffer (posn-window (event-end event)))
558 (save-excursion
559 (goto-char (posn-point (event-end event)))
560 (setq buffer (Buffer-menu-buffer t))))
561 (select-window (posn-window (event-end event)))
562 (if (and (window-dedicated-p (selected-window))
563 (eq (selected-window) (frame-root-window)))
564 (switch-to-buffer-other-frame buffer)
565 (switch-to-buffer buffer))))
567 (defun Buffer-menu-this-window ()
568 "Select this line's buffer in this window."
569 (interactive)
570 (switch-to-buffer (Buffer-menu-buffer t)))
572 (defun Buffer-menu-other-window ()
573 "Select this line's buffer in other window, leaving buffer menu visible."
574 (interactive)
575 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
577 (defun Buffer-menu-switch-other-window ()
578 "Make the other window select this line's buffer.
579 The current window remains selected."
580 (interactive)
581 (let ((pop-up-windows t)
582 same-window-buffer-names
583 same-window-regexps)
584 (display-buffer (Buffer-menu-buffer t))))
586 (defun Buffer-menu-2-window ()
587 "Select this line's buffer, with previous buffer in second window."
588 (interactive)
589 (let ((buff (Buffer-menu-buffer t))
590 (menu (current-buffer))
591 (pop-up-windows t)
592 same-window-buffer-names
593 same-window-regexps)
594 (delete-other-windows)
595 (switch-to-buffer (other-buffer))
596 (pop-to-buffer buff)
597 (bury-buffer menu)))
599 (defun Buffer-menu-toggle-read-only ()
600 "Toggle read-only status of buffer on this line, perhaps via version control."
601 (interactive)
602 (let (char)
603 (with-current-buffer (Buffer-menu-buffer t)
604 (toggle-read-only)
605 (setq char (if buffer-read-only ?% ?\s)))
606 (save-excursion
607 (beginning-of-line)
608 (forward-char 1)
609 (if (/= (following-char) char)
610 (let ((inhibit-read-only t))
611 (delete-char 1)
612 (insert char))))))
614 (defun Buffer-menu-bury ()
615 "Bury the buffer listed on this line."
616 (interactive)
617 (when (Buffer-menu-no-header)
618 (save-excursion
619 (beginning-of-line)
620 (bury-buffer (Buffer-menu-buffer t))
621 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
622 (inhibit-read-only t))
623 (delete-region (point) (progn (forward-line -1) (point)))
624 (goto-char (point-max))
625 (insert line))
626 (message "Buried buffer moved to the end"))))
629 (defun Buffer-menu-view ()
630 "View this line's buffer in View mode."
631 (interactive)
632 (view-buffer (Buffer-menu-buffer t)))
635 (defun Buffer-menu-view-other-window ()
636 "View this line's buffer in View mode in another window."
637 (interactive)
638 (view-buffer-other-window (Buffer-menu-buffer t)))
641 (define-key ctl-x-map "\C-b" 'list-buffers)
643 (defun list-buffers (&optional files-only)
644 "Display a list of names of existing buffers.
645 The list is displayed in a buffer named `*Buffer List*'.
646 Note that buffers with names starting with spaces are omitted.
647 Non-null optional arg FILES-ONLY means mention only file buffers.
649 For more information, see the function `buffer-menu'."
650 (interactive "P")
651 (display-buffer (list-buffers-noselect files-only)))
653 (defconst Buffer-menu-short-ellipsis
654 ;; This file is preloaded, so we can't use char-displayable-p here
655 ;; because we don't know yet what display we're going to connect to.
656 ":" ;; (if (char-displayable-p ?…) "…" ":")
659 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
660 (if (> (+ (string-width name) (string-width size) 2)
661 Buffer-menu-buffer+size-width)
662 (setq name
663 (let ((tail
664 (if (string-match "<[0-9]+>$" name)
665 (match-string 0 name)
666 "")))
667 (concat (truncate-string-to-width
668 name
669 (- Buffer-menu-buffer+size-width
670 (max (string-width size) 3)
671 (string-width tail)
673 Buffer-menu-short-ellipsis
674 tail)))
675 ;; Don't put properties on (buffer-name).
676 (setq name (copy-sequence name)))
677 (add-text-properties 0 (length name) name-props name)
678 (add-text-properties 0 (length size) size-props size)
679 (concat name
680 (make-string (- Buffer-menu-buffer+size-width
681 (string-width name)
682 (string-width size))
683 ?\s)
684 size))
686 (defun Buffer-menu-sort (column)
687 "Sort the buffer menu by COLUMN."
688 (interactive "P")
689 (when column
690 (setq column (prefix-numeric-value column))
691 (if (< column 2) (setq column 2))
692 (if (> column 5) (setq column 5)))
693 (setq Buffer-menu-sort-column column)
694 (let ((inhibit-read-only t) l buf m1 m2)
695 (save-excursion
696 (Buffer-menu-beginning)
697 (while (not (eobp))
698 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
699 (setq m1 (char-after)
700 m1 (if (memq m1 '(?> ?D)) m1)
701 m2 (char-after (+ (point) 2))
702 m2 (if (eq m2 ?S) m2))
703 (if (or m1 m2)
704 (push (list buf m1 m2) l)))
705 (forward-line)))
706 (revert-buffer)
707 (save-excursion
708 (Buffer-menu-beginning)
709 (while (not (eobp))
710 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
711 (setq m1 (cadr buf)
712 m2 (cadr (cdr buf)))
713 (when m1
714 (delete-char 1)
715 (insert m1)
716 (backward-char 1))
717 (when m2
718 (forward-char 2)
719 (delete-char 1)
720 (insert m2)))
721 (forward-line)))))
723 (defun Buffer-menu-sort-by-column (&optional e)
724 "Sort the buffer menu by the column clicked on."
725 (interactive (list last-input-event))
726 (if e (mouse-select-window e))
727 (let* ((pos (event-start e))
728 (obj (posn-object pos))
729 (col (if obj
730 (get-text-property (cdr obj) 'column (car obj))
731 (get-text-property (posn-point pos) 'column))))
732 (Buffer-menu-sort col)))
734 (defvar Buffer-menu-sort-button-map
735 (let ((map (make-sparse-keymap)))
736 ;; This keymap handles both nil and non-nil values for
737 ;; Buffer-menu-use-header-line.
738 (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column)
739 (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column)
740 (define-key map [mouse-2] 'Buffer-menu-sort-by-column)
741 (define-key map [follow-link] 'mouse-face)
742 (define-key map "\C-m" 'Buffer-menu-sort-by-column)
743 map)
744 "Local keymap for Buffer menu sort buttons.")
746 (defun Buffer-menu-make-sort-button (name column)
747 (if (equal column Buffer-menu-sort-column) (setq column nil))
748 (propertize name
749 'column column
750 'help-echo (concat
751 (if Buffer-menu-use-header-line
752 "mouse-1, mouse-2: sort by "
753 "mouse-2, RET: sort by ")
754 (if column (downcase name) "visited order"))
755 'mouse-face 'highlight
756 'keymap Buffer-menu-sort-button-map))
758 (defun list-buffers-noselect (&optional files-only buffer-list)
759 "Create and return a buffer with a list of names of existing buffers.
760 The buffer is named `*Buffer List*'.
761 Note that buffers with names starting with spaces are omitted.
762 Non-null optional arg FILES-ONLY means mention only file buffers.
764 If BUFFER-LIST is non-nil, it should be a list of buffers;
765 it means list those buffers and no others.
767 For more information, see the function `buffer-menu'."
768 (let* ((old-buffer (current-buffer))
769 (standard-output standard-output)
770 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
771 (header (concat "CRM "
772 (Buffer-menu-buffer+size
773 (Buffer-menu-make-sort-button "Buffer" 2)
774 (Buffer-menu-make-sort-button "Size" 3))
776 (Buffer-menu-make-sort-button "Mode" 4) mode-end
777 (Buffer-menu-make-sort-button "File" 5) "\n"))
778 list desired-point)
779 (when Buffer-menu-use-header-line
780 (let ((pos 0))
781 ;; Turn whitespace chars in the header into stretch specs so
782 ;; they work regardless of the header-line face.
783 (while (string-match "[ \t\n]+" header pos)
784 (setq pos (match-end 0))
785 (put-text-property (match-beginning 0) pos 'display
786 ;; Assume fixed-size chars in the buffer.
787 (list 'space :align-to pos)
788 header)))
789 ;; Try to better align the one-char headers.
790 (put-text-property 0 3 'face 'fixed-pitch header)
791 ;; Add a "dummy" leading space to align the beginning of the header
792 ;; line with the beginning of the text (rather than with the left
793 ;; scrollbar or the left fringe). --Stef
794 (setq header (concat (propertize " " 'display '(space :align-to 0))
795 header)))
796 (with-current-buffer (get-buffer-create "*Buffer List*")
797 (setq buffer-read-only nil)
798 (erase-buffer)
799 (setq standard-output (current-buffer))
800 (unless Buffer-menu-use-header-line
801 ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII
802 ;; (i.e. U+002D, HYPHEN-MINUS).
803 (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-)))
804 (insert header
805 (apply 'string
806 (mapcar (lambda (c)
807 (if (memq c '(?\n ?\s)) c underline))
808 header)))))
809 ;; Collect info for every buffer we're interested in.
810 (dolist (buffer (or buffer-list
811 (buffer-list
812 (when Buffer-menu-use-frame-buffer-list
813 (selected-frame)))))
814 (with-current-buffer buffer
815 (let ((name (buffer-name))
816 (file buffer-file-name))
817 (unless (and (not buffer-list)
819 ;; Don't mention internal buffers.
820 (and (string= (substring name 0 1) " ") (null file))
821 ;; Maybe don't mention buffers without files.
822 (and files-only (not file))
823 (string= name "*Buffer List*")))
824 ;; Otherwise output info.
825 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
826 (if mode-line-process
827 (format-mode-line mode-line-process
828 nil nil buffer))))
829 (bits (string
830 (if (eq buffer old-buffer) ?. ?\s)
831 ;; Handle readonly status. The output buffer
832 ;; is special cased to appear readonly; it is
833 ;; actually made so at a later date.
834 (if (or (eq buffer standard-output)
835 buffer-read-only)
836 ?% ?\s)
837 ;; Identify modified buffers.
838 (if (buffer-modified-p) ?* ?\s)
839 ;; Space separator.
840 ?\s)))
841 (unless file
842 ;; No visited file. Check local value of
843 ;; list-buffers-directory and, for Info buffers,
844 ;; Info-current-file.
845 (cond ((and (boundp 'list-buffers-directory)
846 list-buffers-directory)
847 (setq file list-buffers-directory))
848 ((eq major-mode 'Info-mode)
849 (setq file Info-current-file)
850 (cond
851 ((equal file "dir")
852 (setq file "*Info Directory*"))
853 ((eq file 'apropos)
854 (setq file "*Info Apropos*"))
855 ((eq file 'history)
856 (setq file "*Info History*"))
857 ((eq file 'toc)
858 (setq file "*Info TOC*"))
859 ((not (stringp file)) ;; avoid errors
860 (setq file nil))
862 (setq file (concat "("
863 (file-name-nondirectory file)
864 ") "
865 Info-current-node)))))))
866 (push (list buffer bits name (buffer-size) mode file)
867 list))))))
868 ;; Preserve the original buffer-list ordering, just in case.
869 (setq list (nreverse list))
870 ;; Place the buffers's info in the output buffer, sorted if necessary.
871 (dolist (buffer
872 (if Buffer-menu-sort-column
873 (sort list
874 (if (eq Buffer-menu-sort-column 3)
875 (lambda (a b)
876 (< (nth Buffer-menu-sort-column a)
877 (nth Buffer-menu-sort-column b)))
878 (lambda (a b)
879 (string< (nth Buffer-menu-sort-column a)
880 (nth Buffer-menu-sort-column b)))))
881 list))
882 (if (eq (car buffer) old-buffer)
883 (setq desired-point (point)))
884 (insert (cadr buffer)
885 ;; Put the buffer name into a text property
886 ;; so we don't have to extract it from the text.
887 ;; This way we avoid problems with unusual buffer names.
888 (let ((name (nth 2 buffer))
889 (size (int-to-string (nth 3 buffer))))
890 (Buffer-menu-buffer+size name size
891 `(buffer-name ,name
892 buffer ,(car buffer)
893 font-lock-face buffer-menu-buffer
894 mouse-face highlight
895 help-echo
896 ,(if (>= (length name)
897 (- Buffer-menu-buffer+size-width
898 (max (length size) 3)
900 name
901 "mouse-2: select this buffer"))))
903 (if (> (string-width (nth 4 buffer)) Buffer-menu-mode-width)
904 (truncate-string-to-width (nth 4 buffer)
905 Buffer-menu-mode-width)
906 (nth 4 buffer)))
907 (when (nth 5 buffer)
908 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
909 Buffer-menu-mode-width 4) 1)
910 (princ (abbreviate-file-name (nth 5 buffer))))
911 (princ "\n"))
912 (Buffer-menu-mode)
913 (when Buffer-menu-use-header-line
914 (setq header-line-format header))
915 ;; DESIRED-POINT doesn't have to be set; it is not when the
916 ;; current buffer is not displayed for some reason.
917 (and desired-point
918 (goto-char desired-point))
919 (setq Buffer-menu-files-only files-only)
920 (set-buffer-modified-p nil)
921 (current-buffer))))
923 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
924 ;;; buff-menu.el ends here