(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / buff-menu.el
blob7e9115a79dcb503c0e45507c67d4803d6c7f7e73
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 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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; Edit, delete, or change attributes of all currently active Emacs
29 ;; buffers from a list summarizing their state. A good way to browse
30 ;; any special or scratch buffers you have loaded, since you can't find
31 ;; them by filename. The single entry point is `list-buffers',
32 ;; normally bound to C-x C-b.
34 ;;; Change Log:
36 ;; Buffer-menu-view: New function
37 ;; Buffer-menu-view-other-window: New function
39 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
41 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
43 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
44 ;; current entry and then move to previous one.
46 ;; Based on FSF code dating back to 1985.
48 ;;; Code:
50 ;;Trying to preserve the old window configuration works well in
51 ;;simple scenarios, when you enter the buffer menu, use it, and exit it.
52 ;;But it does strange things when you switch back to the buffer list buffer
53 ;;with C-x b, later on, when the window configuration is different.
54 ;;The choice seems to be, either restore the window configuration
55 ;;in all cases, or in no cases.
56 ;;I decided it was better not to restore the window config at all. -- rms.
58 ;;But since then, I changed buffer-menu to use the selected window,
59 ;;so q now once again goes back to the previous window configuration.
61 ;;(defvar Buffer-menu-window-config nil
62 ;; "Window configuration saved from entry to `buffer-menu'.")
64 ;; Put buffer *Buffer List* into proper mode right away
65 ;; so that from now on even list-buffers is enough to get a buffer menu.
67 (defgroup Buffer-menu nil
68 "Show a menu of all buffers in a buffer."
69 :group 'tools
70 :group 'convenience)
72 (defcustom Buffer-menu-use-header-line t
73 "*Non-nil means to use an immovable header-line."
74 :type 'boolean
75 :group 'Buffer-menu)
77 (defface Buffer-menu-buffer-face
78 '((t (:weight bold)))
79 "Face used to highlight buffer name."
80 :group 'Buffer-menu
81 :group 'font-lock-highlighting-faces)
83 (defcustom Buffer-menu-buffer+size-width 26
84 "*How wide to jointly make the buffer name and size columns."
85 :type 'number
86 :group 'Buffer-menu)
88 (defcustom Buffer-menu-mode-width 16
89 "*How wide to make the mode name column."
90 :type 'number
91 :group 'Buffer-menu)
93 ;; This should get updated & resorted when you click on a column heading
94 (defvar Buffer-menu-sort-column nil
95 "*2 for sorting by buffer names. 5 for sorting by file names.
96 nil for default sorting by visited order.")
98 (defconst Buffer-menu-buffer-column 4)
100 (defvar Buffer-menu-mode-map nil
101 "Local keymap for `Buffer-menu-mode' buffers.")
103 (defvar Buffer-menu-files-only nil
104 "Non-nil if the current buffer-menu lists only file buffers.
105 This variable determines whether reverting the buffer lists only
106 file buffers. It affects both manual reverting and reverting by
107 Auto Revert Mode.")
109 (make-variable-buffer-local 'Buffer-menu-files-only)
111 (if Buffer-menu-mode-map
113 (setq Buffer-menu-mode-map (make-keymap))
114 (suppress-keymap Buffer-menu-mode-map t)
115 (define-key Buffer-menu-mode-map "q" 'quit-window)
116 (define-key Buffer-menu-mode-map "v" 'Buffer-menu-select)
117 (define-key Buffer-menu-mode-map "2" 'Buffer-menu-2-window)
118 (define-key Buffer-menu-mode-map "1" 'Buffer-menu-1-window)
119 (define-key Buffer-menu-mode-map "f" 'Buffer-menu-this-window)
120 (define-key Buffer-menu-mode-map "e" 'Buffer-menu-this-window)
121 (define-key Buffer-menu-mode-map "\C-m" 'Buffer-menu-this-window)
122 (define-key Buffer-menu-mode-map "o" 'Buffer-menu-other-window)
123 (define-key Buffer-menu-mode-map "\C-o" 'Buffer-menu-switch-other-window)
124 (define-key Buffer-menu-mode-map "s" 'Buffer-menu-save)
125 (define-key Buffer-menu-mode-map "d" 'Buffer-menu-delete)
126 (define-key Buffer-menu-mode-map "k" 'Buffer-menu-delete)
127 (define-key Buffer-menu-mode-map "\C-d" 'Buffer-menu-delete-backwards)
128 (define-key Buffer-menu-mode-map "\C-k" 'Buffer-menu-delete)
129 (define-key Buffer-menu-mode-map "x" 'Buffer-menu-execute)
130 (define-key Buffer-menu-mode-map " " 'next-line)
131 (define-key Buffer-menu-mode-map "n" 'next-line)
132 (define-key Buffer-menu-mode-map "p" 'previous-line)
133 (define-key Buffer-menu-mode-map "\177" 'Buffer-menu-backup-unmark)
134 (define-key Buffer-menu-mode-map "~" 'Buffer-menu-not-modified)
135 (define-key Buffer-menu-mode-map "?" 'describe-mode)
136 (define-key Buffer-menu-mode-map "u" 'Buffer-menu-unmark)
137 (define-key Buffer-menu-mode-map "m" 'Buffer-menu-mark)
138 (define-key Buffer-menu-mode-map "t" 'Buffer-menu-visit-tags-table)
139 (define-key Buffer-menu-mode-map "%" 'Buffer-menu-toggle-read-only)
140 (define-key Buffer-menu-mode-map "b" 'Buffer-menu-bury)
141 (define-key Buffer-menu-mode-map "g" 'Buffer-menu-revert)
142 (define-key Buffer-menu-mode-map "V" 'Buffer-menu-view)
143 (define-key Buffer-menu-mode-map "T" 'Buffer-menu-toggle-files-only)
144 (define-key Buffer-menu-mode-map [mouse-2] 'Buffer-menu-mouse-select)
145 (define-key Buffer-menu-mode-map [follow-link] 'mouse-face)
148 ;; Buffer Menu mode is suitable only for specially formatted data.
149 (put 'Buffer-menu-mode 'mode-class 'special)
151 (defun Buffer-menu-mode ()
152 "Major mode for editing a list of buffers.
153 Each line describes one of the buffers in Emacs.
154 Letters do not insert themselves; instead, they are commands.
155 \\<Buffer-menu-mode-map>
156 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
157 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
158 \\[Buffer-menu-other-window] -- select that buffer in another window,
159 so the buffer menu buffer remains visible in its window.
160 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
161 \\[Buffer-menu-view-other-window] -- select that buffer in
162 another window, in view-mode.
163 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
164 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
165 \\[Buffer-menu-select] -- select current line's buffer.
166 Also show buffers marked with m, in other windows.
167 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
168 \\[Buffer-menu-2-window] -- select that buffer in one window,
169 together with buffer selected before this one in another window.
170 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
171 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
172 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
173 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
174 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
175 \\[Buffer-menu-execute] -- delete or save marked buffers.
176 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
177 With prefix argument, also move up one line.
178 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
179 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
180 \\[Buffer-menu-revert] -- update the list of buffers.
181 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
182 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
183 (kill-all-local-variables)
184 (use-local-map Buffer-menu-mode-map)
185 (setq major-mode 'Buffer-menu-mode)
186 (setq mode-name "Buffer Menu")
187 (set (make-local-variable 'revert-buffer-function)
188 'Buffer-menu-revert-function)
189 (set (make-local-variable 'buffer-stale-function)
190 #'(lambda (&optional noconfirm) 'fast))
191 (setq truncate-lines t)
192 (setq buffer-read-only t)
193 (run-mode-hooks 'buffer-menu-mode-hook))
195 ;; This function exists so we can make the doc string of Buffer-menu-mode
196 ;; look nice.
197 (defun Buffer-menu-revert ()
198 "Update the list of buffers."
199 (interactive)
200 (revert-buffer))
202 (defun Buffer-menu-revert-function (ignore1 ignore2)
203 (or (eq buffer-undo-list t)
204 (setq buffer-undo-list nil))
205 ;; We can not use save-excursion here. The buffer gets erased.
206 (let ((opoint (point))
207 (eobp (eobp))
208 (ocol (current-column))
209 (oline (progn (move-to-column 4)
210 (get-text-property (point) 'buffer)))
211 (prop (point-min))
212 ;; do not make undo records for the reversion.
213 (buffer-undo-list t))
214 (list-buffers-noselect Buffer-menu-files-only)
215 (if oline
216 (while (setq prop (next-single-property-change prop 'buffer))
217 (when (eq (get-text-property prop 'buffer) oline)
218 (goto-char prop)
219 (move-to-column ocol)))
220 (goto-char (if eobp (point-max) opoint)))))
222 (defun Buffer-menu-toggle-files-only (arg)
223 "Toggle whether the current buffer-menu displays only file buffers.
224 With a positive ARG display only file buffers. With zero or
225 negative ARG, display other buffers as well."
226 (interactive "P")
227 (setq Buffer-menu-files-only
228 (cond ((not arg) (not Buffer-menu-files-only))
229 ((> (prefix-numeric-value arg) 0) t)))
230 (revert-buffer))
233 (defun Buffer-menu-buffer (error-if-non-existent-p)
234 "Return buffer described by this line of buffer menu."
235 (let* ((where (save-excursion
236 (beginning-of-line)
237 (+ (point) Buffer-menu-buffer-column)))
238 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
239 (buf (and (not (eobp)) (get-text-property where 'buffer))))
240 (if name
241 (or (get-buffer name)
242 (and buf (buffer-name buf) buf)
243 (if error-if-non-existent-p
244 (error "No buffer named `%s'" name)
245 nil))
246 (or (and buf (buffer-name buf) buf)
247 (if error-if-non-existent-p
248 (error "No buffer on this line")
249 nil)))))
251 (defun buffer-menu (&optional arg)
252 "Make a menu of buffers so you can save, delete or select them.
253 With argument, show only buffers that are visiting files.
254 Type ? after invocation to get help on commands available.
255 Type q to remove the buffer menu from the display.
257 The first column shows `>' for a buffer you have
258 marked to be displayed, `D' for one you have marked for
259 deletion, and `.' for the current buffer.
261 The C column has a `.' for the buffer from which you came.
262 The R column has a `%' if the buffer is read-only.
263 The M column has a `*' if it is modified,
264 or `S' if you have marked it for saving.
265 After this come the buffer name, its size in characters,
266 its major mode, and the visited file name (if any)."
267 (interactive "P")
268 ;;; (setq Buffer-menu-window-config (current-window-configuration))
269 (switch-to-buffer (list-buffers-noselect arg))
270 (message
271 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
273 (defun buffer-menu-other-window (&optional arg)
274 "Display a list of buffers in another window.
275 With the buffer list buffer, you can save, delete or select the buffers.
276 With argument, show only buffers that are visiting files.
277 Type ? after invocation to get help on commands available.
278 Type q to remove the buffer menu from the display.
279 For more information, see the function `buffer-menu'."
280 (interactive "P")
281 ;;; (setq Buffer-menu-window-config (current-window-configuration))
282 (switch-to-buffer-other-window (list-buffers-noselect arg))
283 (message
284 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
286 (defun Buffer-menu-no-header ()
287 (beginning-of-line)
288 (if (or Buffer-menu-use-header-line
289 (not (eq (char-after) ?C)))
291 (ding)
292 (forward-line 1)
293 nil))
295 (defun Buffer-menu-mark ()
296 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
297 (interactive)
298 (when (Buffer-menu-no-header)
299 (let ((buffer-read-only nil))
300 (delete-char 1)
301 (insert ?>)
302 (forward-line 1))))
304 (defun Buffer-menu-unmark (&optional backup)
305 "Cancel all requested operations on buffer on this line and move down.
306 Optional ARG means move up."
307 (interactive "P")
308 (when (Buffer-menu-no-header)
309 (let* ((buf (Buffer-menu-buffer t))
310 (mod (buffer-modified-p buf))
311 (readonly (save-excursion (set-buffer buf) buffer-read-only))
312 (buffer-read-only nil))
313 (delete-char 3)
314 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
315 (forward-line (if backup -1 1)))
317 (defun Buffer-menu-backup-unmark ()
318 "Move up and cancel all requested operations on buffer on line above."
319 (interactive)
320 (forward-line -1)
321 (Buffer-menu-unmark)
322 (forward-line -1))
324 (defun Buffer-menu-delete (&optional arg)
325 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
326 Prefix arg is how many buffers to delete.
327 Negative arg means delete backwards."
328 (interactive "p")
329 (when (Buffer-menu-no-header)
330 (let ((buffer-read-only nil))
331 (if (or (null arg) (= arg 0))
332 (setq arg 1))
333 (while (> arg 0)
334 (delete-char 1)
335 (insert ?D)
336 (forward-line 1)
337 (setq arg (1- arg)))
338 (while (and (< arg 0)
339 (Buffer-menu-no-header))
340 (delete-char 1)
341 (insert ?D)
342 (forward-line -1)
343 (setq arg (1+ arg))))))
345 (defun Buffer-menu-delete-backwards (&optional arg)
346 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
347 and then move up one line. Prefix arg means move that many lines."
348 (interactive "p")
349 (Buffer-menu-delete (- (or arg 1))))
351 (defun Buffer-menu-save ()
352 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
353 (interactive)
354 (when (Buffer-menu-no-header)
355 (let ((buffer-read-only nil))
356 (forward-char 2)
357 (delete-char 1)
358 (insert ?S)
359 (forward-line 1))))
361 (defun Buffer-menu-not-modified (&optional arg)
362 "Mark buffer on this line as unmodified (no changes to save)."
363 (interactive "P")
364 (save-excursion
365 (set-buffer (Buffer-menu-buffer t))
366 (set-buffer-modified-p arg))
367 (save-excursion
368 (beginning-of-line)
369 (forward-char 2)
370 (if (= (char-after) (if arg ? ?*))
371 (let ((buffer-read-only nil))
372 (delete-char 1)
373 (insert (if arg ?* ? ))))))
375 (defun Buffer-menu-beginning ()
376 (goto-char (point-min))
377 (unless Buffer-menu-use-header-line
378 (forward-line)))
380 (defun Buffer-menu-execute ()
381 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
382 (interactive)
383 (save-excursion
384 (Buffer-menu-beginning)
385 (while (re-search-forward "^..S" nil t)
386 (let ((modp nil))
387 (save-excursion
388 (set-buffer (Buffer-menu-buffer t))
389 (save-buffer)
390 (setq modp (buffer-modified-p)))
391 (let ((buffer-read-only nil))
392 (delete-char -1)
393 (insert (if modp ?* ? ))))))
394 (save-excursion
395 (Buffer-menu-beginning)
396 (let ((buff-menu-buffer (current-buffer))
397 (buffer-read-only nil))
398 (while (re-search-forward "^D" nil t)
399 (forward-char -1)
400 (let ((buf (Buffer-menu-buffer nil)))
401 (or (eq buf nil)
402 (eq buf buff-menu-buffer)
403 (save-excursion (kill-buffer buf)))
404 (if (and buf (buffer-name buf))
405 (progn (delete-char 1)
406 (insert ? ))
407 (delete-region (point) (progn (forward-line 1) (point)))
408 (unless (bobp)
409 (forward-char -1))))))))
411 (defun Buffer-menu-select ()
412 "Select this line's buffer; also display buffers marked with `>'.
413 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
414 This command deletes and replaces all the previously existing windows
415 in the selected frame."
416 (interactive)
417 (let ((buff (Buffer-menu-buffer t))
418 (menu (current-buffer))
419 (others ())
420 tem)
421 (Buffer-menu-beginning)
422 (while (re-search-forward "^>" nil t)
423 (setq tem (Buffer-menu-buffer t))
424 (let ((buffer-read-only nil))
425 (delete-char -1)
426 (insert ?\ ))
427 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
428 (setq others (nreverse others)
429 tem (/ (1- (frame-height)) (1+ (length others))))
430 (delete-other-windows)
431 (switch-to-buffer buff)
432 (or (eq menu buff)
433 (bury-buffer menu))
434 (if (equal (length others) 0)
435 (progn
436 ;;; ;; Restore previous window configuration before displaying
437 ;;; ;; selected buffers.
438 ;;; (if Buffer-menu-window-config
439 ;;; (progn
440 ;;; (set-window-configuration Buffer-menu-window-config)
441 ;;; (setq Buffer-menu-window-config nil)))
442 (switch-to-buffer buff))
443 (while others
444 (split-window nil tem)
445 (other-window 1)
446 (switch-to-buffer (car others))
447 (setq others (cdr others)))
448 (other-window 1) ;back to the beginning!
453 (defun Buffer-menu-visit-tags-table ()
454 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
455 (interactive)
456 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
457 (if file
458 (visit-tags-table file)
459 (error "Specified buffer has no file"))))
461 (defun Buffer-menu-1-window ()
462 "Select this line's buffer, alone, in full frame."
463 (interactive)
464 (switch-to-buffer (Buffer-menu-buffer t))
465 (bury-buffer (other-buffer))
466 (delete-other-windows))
468 (defun Buffer-menu-mouse-select (event)
469 "Select the buffer whose line you click on."
470 (interactive "e")
471 (let (buffer)
472 (save-excursion
473 (set-buffer (window-buffer (posn-window (event-end event))))
474 (save-excursion
475 (goto-char (posn-point (event-end event)))
476 (setq buffer (Buffer-menu-buffer t))))
477 (select-window (posn-window (event-end event)))
478 (if (and (window-dedicated-p (selected-window))
479 (eq (selected-window) (frame-root-window)))
480 (switch-to-buffer-other-frame buffer)
481 (switch-to-buffer buffer))))
483 (defun Buffer-menu-this-window ()
484 "Select this line's buffer in this window."
485 (interactive)
486 (switch-to-buffer (Buffer-menu-buffer t)))
488 (defun Buffer-menu-other-window ()
489 "Select this line's buffer in other window, leaving buffer menu visible."
490 (interactive)
491 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
493 (defun Buffer-menu-switch-other-window ()
494 "Make the other window select this line's buffer.
495 The current window remains selected."
496 (interactive)
497 (let ((pop-up-windows t)
498 same-window-buffer-names
499 same-window-regexps)
500 (display-buffer (Buffer-menu-buffer t))))
502 (defun Buffer-menu-2-window ()
503 "Select this line's buffer, with previous buffer in second window."
504 (interactive)
505 (let ((buff (Buffer-menu-buffer t))
506 (menu (current-buffer))
507 (pop-up-windows t)
508 same-window-buffer-names
509 same-window-regexps)
510 (delete-other-windows)
511 (switch-to-buffer (other-buffer))
512 (pop-to-buffer buff)
513 (bury-buffer menu)))
515 (defun Buffer-menu-toggle-read-only ()
516 "Toggle read-only status of buffer on this line, perhaps via version control."
517 (interactive)
518 (let (char)
519 (save-excursion
520 (set-buffer (Buffer-menu-buffer t))
521 (vc-toggle-read-only)
522 (setq char (if buffer-read-only ?% ? )))
523 (save-excursion
524 (beginning-of-line)
525 (forward-char 1)
526 (if (/= (following-char) char)
527 (let (buffer-read-only)
528 (delete-char 1)
529 (insert char))))))
531 (defun Buffer-menu-bury ()
532 "Bury the buffer listed on this line."
533 (interactive)
534 (when (Buffer-menu-no-header)
535 (save-excursion
536 (beginning-of-line)
537 (bury-buffer (Buffer-menu-buffer t))
538 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
539 (buffer-read-only nil))
540 (delete-region (point) (progn (forward-line -1) (point)))
541 (goto-char (point-max))
542 (insert line))
543 (message "Buried buffer moved to the end"))))
546 (defun Buffer-menu-view ()
547 "View this line's buffer in View mode."
548 (interactive)
549 (view-buffer (Buffer-menu-buffer t)))
552 (defun Buffer-menu-view-other-window ()
553 "View this line's buffer in View mode in another window."
554 (interactive)
555 (view-buffer-other-window (Buffer-menu-buffer t)))
558 (define-key ctl-x-map "\C-b" 'list-buffers)
560 (defun list-buffers (&optional files-only)
561 "Display a list of names of existing buffers.
562 The list is displayed in a buffer named `*Buffer List*'.
563 Note that buffers with names starting with spaces are omitted.
564 Non-null optional arg FILES-ONLY means mention only file buffers.
566 For more information, see the function `buffer-menu'."
567 (interactive "P")
568 (display-buffer (list-buffers-noselect files-only)))
570 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
571 (if (> (+ (length name) (length size) 2) Buffer-menu-buffer+size-width)
572 (setq name
573 (if (string-match "<[0-9]+>$" name)
574 (concat (substring name 0
575 (- Buffer-menu-buffer+size-width
576 (max (length size) 3)
577 (match-end 0)
578 (- (match-beginning 0))
580 ":" ; narrow ellipsis
581 (match-string 0 name))
582 (concat (substring name 0
583 (- Buffer-menu-buffer+size-width
584 (max (length size) 3)
586 ":"))) ; narrow ellipsis
587 ;; Don't put properties on (buffer-name).
588 (setq name (copy-sequence name)))
589 (add-text-properties 0 (length name) name-props name)
590 (add-text-properties 0 (length size) size-props size)
591 (concat name
592 (make-string (- Buffer-menu-buffer+size-width
593 (length name)
594 (length size))
596 size))
598 (defun Buffer-menu-sort (column)
599 "Sort the buffer menu by COLUMN."
600 (interactive "P")
601 (when column
602 (setq column (prefix-numeric-value column))
603 (if (< column 2) (setq column 2))
604 (if (> column 5) (setq column 5)))
605 (setq Buffer-menu-sort-column column)
606 (let (buffer-read-only l buf m1 m2)
607 (save-excursion
608 (Buffer-menu-beginning)
609 (while (not (eobp))
610 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
611 (setq m1 (char-after)
612 m1 (if (memq m1 '(?> ?D)) m1)
613 m2 (char-after (+ (point) 2))
614 m2 (if (eq m2 ?S) m2))
615 (if (or m1 m2)
616 (push (list buf m1 m2) l)))
617 (forward-line)))
618 (Buffer-menu-revert)
619 (setq buffer-read-only)
620 (save-excursion
621 (Buffer-menu-beginning)
622 (while (not (eobp))
623 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
624 (setq m1 (cadr buf)
625 m2 (cadr (cdr buf)))
626 (when m1
627 (delete-char 1)
628 (insert m1)
629 (backward-char 1))
630 (when m2
631 (forward-char 2)
632 (delete-char 1)
633 (insert m2)))
634 (forward-line)))))
636 (defun Buffer-menu-make-sort-button (name column)
637 (if (equal column Buffer-menu-sort-column) (setq column nil))
638 (propertize name
639 'help-echo (if column
640 (if Buffer-menu-use-header-line
641 (concat "mouse-2: sort by " (downcase name))
642 (concat "mouse-2, RET: sort by "
643 (downcase name)))
644 (if Buffer-menu-use-header-line
645 "mouse-2: sort by visited order"
646 "mouse-2, RET: sort by visited order"))
647 'mouse-face 'highlight
648 'keymap (let ((map (make-sparse-keymap)))
649 (if Buffer-menu-use-header-line
650 (define-key map [header-line mouse-2]
651 `(lambda (e)
652 (interactive "e")
653 (save-window-excursion
654 (if e (mouse-select-window e))
655 (Buffer-menu-sort ,column))))
656 (define-key map [mouse-2]
657 `(lambda (e)
658 (interactive "e")
659 (if e (mouse-select-window e))
660 (Buffer-menu-sort ,column)))
661 (define-key map "\C-m"
662 `(lambda () (interactive)
663 (Buffer-menu-sort ,column))))
664 map)))
666 (defun list-buffers-noselect (&optional files-only buffer-list)
667 "Create and return a buffer with a list of names of existing buffers.
668 The buffer is named `*Buffer List*'.
669 Note that buffers with names starting with spaces are omitted.
670 Non-null optional arg FILES-ONLY means mention only file buffers.
672 If BUFFER-LIST is non-nil, it should be a list of buffers;
673 it means list those buffers and no others.
675 For more information, see the function `buffer-menu'."
676 (let* ((old-buffer (current-buffer))
677 (standard-output standard-output)
678 (mode-end (make-string (- Buffer-menu-mode-width 2) ? ))
679 (header (concat "CRM "
680 (Buffer-menu-buffer+size
681 (Buffer-menu-make-sort-button "Buffer" 2)
682 (Buffer-menu-make-sort-button "Size" 3))
684 (Buffer-menu-make-sort-button "Mode" 4) mode-end
685 (Buffer-menu-make-sort-button "File" 5) "\n"))
686 list desired-point)
687 (when Buffer-menu-use-header-line
688 (let ((pos 0))
689 ;; Turn spaces in the header into stretch specs so they work
690 ;; regardless of the header-line face.
691 (while (string-match "[ \t]+" header pos)
692 (setq pos (match-end 0))
693 (put-text-property (match-beginning 0) pos 'display
694 ;; Assume fixed-size chars in the buffer.
695 (list 'space :align-to pos)
696 header)))
697 ;; Try to better align the one-char headers.
698 (put-text-property 0 3 'face 'fixed-pitch header)
699 ;; Add a "dummy" leading space to align the beginning of the header
700 ;; line with the beginning of the text (rather than with the left
701 ;; scrollbar or the left fringe). –-Stef
702 (setq header (concat (propertize " " 'display '(space :align-to 0))
703 header)))
704 (with-current-buffer (get-buffer-create "*Buffer List*")
705 (setq buffer-read-only nil)
706 (erase-buffer)
707 (setq standard-output (current-buffer))
708 (unless Buffer-menu-use-header-line
709 (let ((underline (if (char-displayable-p ?—) ?— ?-)))
710 (insert header
711 (apply 'string
712 (mapcar (lambda (c)
713 (if (memq c '(?\n ?\ )) c underline))
714 header)))))
715 ;; Collect info for every buffer we're interested in.
716 (dolist (buffer (or buffer-list (buffer-list)))
717 (with-current-buffer buffer
718 (let ((name (buffer-name))
719 (file buffer-file-name))
720 (unless (and (not buffer-list)
722 ;; Don't mention internal buffers.
723 (and (string= (substring name 0 1) " ") (null file))
724 ;; Maybe don't mention buffers without files.
725 (and files-only (not file))
726 (string= name "*Buffer List*")))
727 ;; Otherwise output info.
728 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
729 (if mode-line-process
730 (format-mode-line mode-line-process
731 nil nil buffer))))
732 (bits (string
733 (if (eq buffer old-buffer) ?. ?\ )
734 ;; Handle readonly status. The output buffer
735 ;; is special cased to appear readonly; it is
736 ;; actually made so at a later date.
737 (if (or (eq buffer standard-output)
738 buffer-read-only)
739 ?% ?\ )
740 ;; Identify modified buffers.
741 (if (buffer-modified-p) ?* ?\ )
742 ;; Space separator.
743 ?\ )))
744 (unless file
745 ;; No visited file. Check local value of
746 ;; list-buffers-directory.
747 (when (and (boundp 'list-buffers-directory)
748 list-buffers-directory)
749 (setq file list-buffers-directory)))
750 (push (list buffer bits name (buffer-size) mode file)
751 list))))))
752 ;; Preserve the original buffer-list ordering, just in case.
753 (setq list (nreverse list))
754 ;; Place the buffers's info in the output buffer, sorted if necessary.
755 (dolist (buffer
756 (if Buffer-menu-sort-column
757 (sort list
758 (if (eq Buffer-menu-sort-column 3)
759 (lambda (a b)
760 (< (nth Buffer-menu-sort-column a)
761 (nth Buffer-menu-sort-column b)))
762 (lambda (a b)
763 (string< (nth Buffer-menu-sort-column a)
764 (nth Buffer-menu-sort-column b)))))
765 list))
766 (if (eq (car buffer) old-buffer)
767 (setq desired-point (point)))
768 (insert (cadr buffer)
769 ;; Put the buffer name into a text property
770 ;; so we don't have to extract it from the text.
771 ;; This way we avoid problems with unusual buffer names.
772 (Buffer-menu-buffer+size (nth 2 buffer)
773 (int-to-string (nth 3 buffer))
774 `(buffer-name ,(nth 2 buffer)
775 buffer ,(car buffer)
776 font-lock-face Buffer-menu-buffer-face
777 mouse-face highlight
778 help-echo "mouse-2: select this buffer"))
780 (if (> (length (nth 4 buffer)) Buffer-menu-mode-width)
781 (substring (nth 4 buffer) 0 Buffer-menu-mode-width)
782 (nth 4 buffer)))
783 (when (nth 5 buffer)
784 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
785 Buffer-menu-mode-width 4) 1)
786 (princ (abbreviate-file-name (nth 5 buffer))))
787 (princ "\n"))
788 (Buffer-menu-mode)
789 (when Buffer-menu-use-header-line
790 (setq header-line-format header))
791 ;; DESIRED-POINT doesn't have to be set; it is not when the
792 ;; current buffer is not displayed for some reason.
793 (and desired-point
794 (goto-char desired-point))
795 (setq Buffer-menu-files-only files-only)
796 (set-buffer-modified-p nil)
797 (current-buffer))))
799 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
800 ;;; buff-menu.el ends here