buff-menu.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. ;;; buff-menu.el --- Interface for viewing and manipulating buffers
  2. ;; Copyright (C) 1985-1987, 1993-1995, 2000-2015 Free Software
  3. ;; Foundation, Inc.
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: convenience
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; The Buffer Menu is used to view, edit, delete, or change attributes
  20. ;; of buffers. The entry points are C-x C-b (`list-buffers') and
  21. ;; M-x buffer-menu.
  22. ;;; Code:
  23. (require 'tabulated-list)
  24. (defgroup Buffer-menu nil
  25. "Show a menu of all buffers in a buffer."
  26. :group 'tools
  27. :group 'convenience)
  28. (defcustom Buffer-menu-use-header-line t
  29. "If non-nil, use the header line to display Buffer Menu column titles."
  30. :type 'boolean
  31. :group 'Buffer-menu)
  32. (defface buffer-menu-buffer
  33. '((t (:weight bold)))
  34. "Face for buffer names in the Buffer Menu."
  35. :group 'Buffer-menu)
  36. (put 'Buffer-menu-buffer 'face-alias 'buffer-menu-buffer)
  37. (defcustom Buffer-menu-buffer+size-width nil
  38. "Combined width of buffer name and size columns in Buffer Menu.
  39. If nil, use `Buffer-menu-name-width' and `Buffer-menu-size-width'.
  40. If non-nil, the value of `Buffer-menu-name-width' is overridden;
  41. the name column is assigned width `Buffer-menu-buffer+size-width'
  42. minus `Buffer-menu-size-width'. This use is deprecated."
  43. :type '(choice (const nil) number)
  44. :group 'Buffer-menu
  45. :version "24.3")
  46. (make-obsolete-variable 'Buffer-menu-buffer+size-width
  47. "use `Buffer-menu-name-width' and `Buffer-menu-size-width' instead."
  48. "24.3")
  49. (defcustom Buffer-menu-name-width 19
  50. "Width of buffer name column in the Buffer Menu."
  51. :type 'number
  52. :group 'Buffer-menu
  53. :version "24.3")
  54. (defcustom Buffer-menu-size-width 7
  55. "Width of buffer size column in the Buffer Menu."
  56. :type 'number
  57. :group 'Buffer-menu
  58. :version "24.3")
  59. (defcustom Buffer-menu-mode-width 16
  60. "Width of mode name column in the Buffer Menu."
  61. :type 'number
  62. :group 'Buffer-menu)
  63. (defcustom Buffer-menu-use-frame-buffer-list t
  64. "If non-nil, the Buffer Menu uses the selected frame's buffer list.
  65. Buffers that were never selected in that frame are listed at the end.
  66. If the value is nil, the Buffer Menu uses the global buffer list.
  67. This variable matters if the Buffer Menu is sorted by visited order,
  68. as it is by default."
  69. :type 'boolean
  70. :group 'Buffer-menu
  71. :version "22.1")
  72. (defvar Buffer-menu-files-only nil
  73. "Non-nil if the current Buffer Menu lists only file buffers.
  74. This is set by the prefix argument to `buffer-menu' and related
  75. commands.")
  76. (make-variable-buffer-local 'Buffer-menu-files-only)
  77. (defvar Info-current-file) ; from info.el
  78. (defvar Info-current-node) ; from info.el
  79. (defvar Buffer-menu-mode-map
  80. (let ((map (make-sparse-keymap))
  81. (menu-map (make-sparse-keymap)))
  82. (set-keymap-parent map tabulated-list-mode-map)
  83. (define-key map "v" 'Buffer-menu-select)
  84. (define-key map "2" 'Buffer-menu-2-window)
  85. (define-key map "1" 'Buffer-menu-1-window)
  86. (define-key map "f" 'Buffer-menu-this-window)
  87. (define-key map "e" 'Buffer-menu-this-window)
  88. (define-key map "\C-m" 'Buffer-menu-this-window)
  89. (define-key map "o" 'Buffer-menu-other-window)
  90. (define-key map "\C-o" 'Buffer-menu-switch-other-window)
  91. (define-key map "s" 'Buffer-menu-save)
  92. (define-key map "d" 'Buffer-menu-delete)
  93. (define-key map "k" 'Buffer-menu-delete)
  94. (define-key map "\C-k" 'Buffer-menu-delete)
  95. (define-key map "\C-d" 'Buffer-menu-delete-backwards)
  96. (define-key map "x" 'Buffer-menu-execute)
  97. (define-key map " " 'next-line)
  98. (define-key map "\177" 'Buffer-menu-backup-unmark)
  99. (define-key map "~" 'Buffer-menu-not-modified)
  100. (define-key map "u" 'Buffer-menu-unmark)
  101. (define-key map "m" 'Buffer-menu-mark)
  102. (define-key map "t" 'Buffer-menu-visit-tags-table)
  103. (define-key map "%" 'Buffer-menu-toggle-read-only)
  104. (define-key map "b" 'Buffer-menu-bury)
  105. (define-key map "V" 'Buffer-menu-view)
  106. (define-key map "T" 'Buffer-menu-toggle-files-only)
  107. (define-key map (kbd "M-s a C-s") 'Buffer-menu-isearch-buffers)
  108. (define-key map (kbd "M-s a M-C-s") 'Buffer-menu-isearch-buffers-regexp)
  109. (define-key map (kbd "M-s a C-o") 'Buffer-menu-multi-occur)
  110. (define-key map [mouse-2] 'Buffer-menu-mouse-select)
  111. (define-key map [follow-link] 'mouse-face)
  112. (define-key map [menu-bar Buffer-menu-mode] (cons (purecopy "Buffer-Menu") menu-map))
  113. (bindings--define-key menu-map [quit]
  114. '(menu-item "Quit" quit-window
  115. :help "Remove the buffer menu from the display"))
  116. (bindings--define-key menu-map [rev]
  117. '(menu-item "Refresh" revert-buffer
  118. :help "Refresh the *Buffer List* buffer contents"))
  119. (bindings--define-key menu-map [s0] menu-bar-separator)
  120. (bindings--define-key menu-map [tf]
  121. '(menu-item "Show Only File Buffers" Buffer-menu-toggle-files-only
  122. :button (:toggle . Buffer-menu-files-only)
  123. :help "Toggle whether the current buffer-menu displays only file buffers"))
  124. (bindings--define-key menu-map [s1] menu-bar-separator)
  125. ;; FIXME: The "Select" entries could use better names...
  126. (bindings--define-key menu-map [sel]
  127. '(menu-item "Select Marked" Buffer-menu-select
  128. :help "Select this line's buffer; also display buffers marked with `>'"))
  129. (bindings--define-key menu-map [bm2]
  130. '(menu-item "Select Two" Buffer-menu-2-window
  131. :help "Select this line's buffer, with previous buffer in second window"))
  132. (bindings--define-key menu-map [bm1]
  133. '(menu-item "Select Current" Buffer-menu-1-window
  134. :help "Select this line's buffer, alone, in full frame"))
  135. (bindings--define-key menu-map [ow]
  136. '(menu-item "Select in Other Window" Buffer-menu-other-window
  137. :help "Select this line's buffer in other window, leaving buffer menu visible"))
  138. (bindings--define-key menu-map [tw]
  139. '(menu-item "Select in Current Window" Buffer-menu-this-window
  140. :help "Select this line's buffer in this window"))
  141. (bindings--define-key menu-map [s2] menu-bar-separator)
  142. (bindings--define-key menu-map [is]
  143. '(menu-item "Regexp Isearch Marked Buffers..." Buffer-menu-isearch-buffers-regexp
  144. :help "Search for a regexp through all marked buffers using Isearch"))
  145. (bindings--define-key menu-map [ir]
  146. '(menu-item "Isearch Marked Buffers..." Buffer-menu-isearch-buffers
  147. :help "Search for a string through all marked buffers using Isearch"))
  148. (bindings--define-key menu-map [mo]
  149. '(menu-item "Multi Occur Marked Buffers..." Buffer-menu-multi-occur
  150. :help "Show lines matching a regexp in marked buffers using Occur"))
  151. (bindings--define-key menu-map [s3] menu-bar-separator)
  152. (bindings--define-key menu-map [by]
  153. '(menu-item "Bury" Buffer-menu-bury
  154. :help "Bury the buffer listed on this line"))
  155. (bindings--define-key menu-map [vt]
  156. '(menu-item "Set Unmodified" Buffer-menu-not-modified
  157. :help "Mark buffer on this line as unmodified (no changes to save)"))
  158. (bindings--define-key menu-map [ex]
  159. '(menu-item "Execute" Buffer-menu-execute
  160. :help "Save and/or delete buffers marked with s or k commands"))
  161. (bindings--define-key menu-map [s4] menu-bar-separator)
  162. (bindings--define-key menu-map [delb]
  163. '(menu-item "Mark for Delete and Move Backwards" Buffer-menu-delete-backwards
  164. :help "Mark buffer on this line to be deleted by x command and move up one line"))
  165. (bindings--define-key menu-map [del]
  166. '(menu-item "Mark for Delete" Buffer-menu-delete
  167. :help "Mark buffer on this line to be deleted by x command"))
  168. (bindings--define-key menu-map [sv]
  169. '(menu-item "Mark for Save" Buffer-menu-save
  170. :help "Mark buffer on this line to be saved by x command"))
  171. (bindings--define-key menu-map [umk]
  172. '(menu-item "Unmark" Buffer-menu-unmark
  173. :help "Cancel all requested operations on buffer on this line and move down"))
  174. (bindings--define-key menu-map [mk]
  175. '(menu-item "Mark" Buffer-menu-mark
  176. :help "Mark buffer on this line for being displayed by v command"))
  177. map)
  178. "Local keymap for `Buffer-menu-mode' buffers.")
  179. (define-obsolete-variable-alias 'buffer-menu-mode-hook
  180. 'Buffer-menu-mode-hook "23.1")
  181. (define-derived-mode Buffer-menu-mode tabulated-list-mode "Buffer Menu"
  182. "Major mode for Buffer Menu buffers.
  183. The Buffer Menu is invoked by the commands \\[list-buffers],
  184. \\[buffer-menu], and \\[buffer-menu-other-window].
  185. See `buffer-menu' for a description of its contents.
  186. In Buffer Menu mode, the following commands are defined:
  187. \\<Buffer-menu-mode-map>
  188. \\[quit-window] Remove the Buffer Menu from the display.
  189. \\[Buffer-menu-this-window] Select current line's buffer in place of the buffer menu.
  190. \\[Buffer-menu-other-window] Select that buffer in another window,
  191. so the Buffer Menu remains visible in its window.
  192. \\[Buffer-menu-view] Select current line's buffer, in View mode.
  193. \\[Buffer-menu-view-other-window] Select that buffer in
  194. another window, in view-mode.
  195. \\[Buffer-menu-switch-other-window] Make another window display that buffer.
  196. \\[Buffer-menu-mark] Mark current line's buffer to be displayed.
  197. \\[Buffer-menu-select] Select current line's buffer.
  198. Also show buffers marked with m, in other windows.
  199. \\[Buffer-menu-1-window] Select that buffer in full-frame window.
  200. \\[Buffer-menu-2-window] Select that buffer in one window, together with the
  201. buffer selected before this one in another window.
  202. \\[Buffer-menu-isearch-buffers] Incremental search in the marked buffers.
  203. \\[Buffer-menu-isearch-buffers-regexp] Isearch for regexp in the marked buffers.
  204. \\[Buffer-menu-multi-occur] Show lines matching regexp in the marked buffers.
  205. \\[Buffer-menu-visit-tags-table] visit-tags-table this buffer.
  206. \\[Buffer-menu-not-modified] Clear modified-flag on that buffer.
  207. \\[Buffer-menu-save] Mark that buffer to be saved, and move down.
  208. \\[Buffer-menu-delete] Mark that buffer to be deleted, and move down.
  209. \\[Buffer-menu-delete-backwards] Mark that buffer to be deleted, and move up.
  210. \\[Buffer-menu-execute] Delete or save marked buffers.
  211. \\[Buffer-menu-unmark] Remove all marks from current line.
  212. With prefix argument, also move up one line.
  213. \\[Buffer-menu-backup-unmark] Back up a line and remove marks.
  214. \\[Buffer-menu-toggle-read-only] Toggle read-only status of buffer on this line.
  215. \\[revert-buffer] Update the list of buffers.
  216. \\[Buffer-menu-toggle-files-only] Toggle whether the menu displays only file buffers.
  217. \\[Buffer-menu-bury] Bury the buffer listed on this line."
  218. (set (make-local-variable 'buffer-stale-function)
  219. (lambda (&optional _noconfirm) 'fast))
  220. (add-hook 'tabulated-list-revert-hook 'list-buffers--refresh nil t))
  221. (defun buffer-menu (&optional arg)
  222. "Switch to the Buffer Menu.
  223. By default, the Buffer Menu lists all buffers except those whose
  224. names start with a space (which are for internal use). With
  225. prefix argument ARG, show only buffers that are visiting files.
  226. In the Buffer Menu, the first column (denoted \"C\") shows \".\"
  227. for the buffer from which you came, \">\" for buffers you mark to
  228. be displayed, and \"D\" for those you mark for deletion.
  229. The \"R\" column has a \"%\" if the buffer is read-only.
  230. The \"M\" column has a \"*\" if it is modified, or \"S\" if you
  231. have marked it for saving.
  232. The remaining columns show the buffer name, the buffer size in
  233. characters, its major mode, and the visited file name (if any).
  234. See `Buffer-menu-mode' for the keybindings available the Buffer
  235. Menu."
  236. (interactive "P")
  237. (switch-to-buffer (list-buffers-noselect arg))
  238. (message
  239. "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
  240. (defun buffer-menu-other-window (&optional arg)
  241. "Display the Buffer Menu in another window.
  242. See `buffer-menu' for a description of the Buffer Menu.
  243. By default, all buffers are listed except those whose names start
  244. with a space (which are for internal use). With prefix argument
  245. ARG, show only buffers that are visiting files."
  246. (interactive "P")
  247. (switch-to-buffer-other-window (list-buffers-noselect arg))
  248. (message
  249. "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
  250. ;;;###autoload
  251. (defun list-buffers (&optional arg)
  252. "Display a list of existing buffers.
  253. The list is displayed in a buffer named \"*Buffer List*\".
  254. See `buffer-menu' for a description of the Buffer Menu.
  255. By default, all buffers are listed except those whose names start
  256. with a space (which are for internal use). With prefix argument
  257. ARG, show only buffers that are visiting files."
  258. (interactive "P")
  259. (display-buffer (list-buffers-noselect arg)))
  260. (defun Buffer-menu-toggle-files-only (arg)
  261. "Toggle whether the current buffer-menu displays only file buffers.
  262. With a positive ARG, display only file buffers. With zero or
  263. negative ARG, display other buffers as well."
  264. (interactive "P")
  265. (setq Buffer-menu-files-only
  266. (cond ((not arg) (not Buffer-menu-files-only))
  267. ((> (prefix-numeric-value arg) 0) t)))
  268. (message (if Buffer-menu-files-only
  269. "Showing only file-visiting buffers."
  270. "Showing all non-internal buffers."))
  271. (revert-buffer))
  272. (defalias 'Buffer-menu-sort 'tabulated-list-sort)
  273. (defun Buffer-menu-buffer (&optional error-if-non-existent-p)
  274. "Return the buffer described by the current Buffer Menu line.
  275. If there is no buffer here, return nil if ERROR-IF-NON-EXISTENT-P
  276. is nil or omitted, and signal an error otherwise."
  277. (let ((buffer (tabulated-list-get-id)))
  278. (cond ((null buffer)
  279. (if error-if-non-existent-p
  280. (error "No buffer on this line")))
  281. ((not (buffer-live-p buffer))
  282. (if error-if-non-existent-p
  283. (error "This buffer has been killed")))
  284. (t buffer))))
  285. (defun Buffer-menu-no-header ()
  286. (beginning-of-line)
  287. (if (or Buffer-menu-use-header-line
  288. (not (eq (char-after) ?C)))
  289. t
  290. (ding)
  291. (forward-line 1)
  292. nil))
  293. (defun Buffer-menu-beginning ()
  294. (goto-char (point-min))
  295. (unless Buffer-menu-use-header-line
  296. (forward-line)))
  297. ;;; Commands for modifying Buffer Menu entries.
  298. (defun Buffer-menu-mark ()
  299. "Mark the Buffer menu entry at point for later display.
  300. It will be displayed by the \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
  301. (interactive)
  302. (tabulated-list-set-col 0 ">" t)
  303. (forward-line))
  304. (defun Buffer-menu-unmark (&optional backup)
  305. "Cancel all requested operations on buffer on this line and move down.
  306. Optional prefix arg means move up."
  307. (interactive "P")
  308. (Buffer-menu--unmark)
  309. (forward-line (if backup -1 1)))
  310. (defun Buffer-menu-backup-unmark ()
  311. "Move up and cancel all requested operations on buffer on line above."
  312. (interactive)
  313. (forward-line -1)
  314. (Buffer-menu--unmark))
  315. (defun Buffer-menu--unmark ()
  316. (tabulated-list-set-col 0 " " t)
  317. (let ((buf (Buffer-menu-buffer)))
  318. (when buf
  319. (if (buffer-modified-p buf)
  320. (tabulated-list-set-col 2 "*" t)
  321. (tabulated-list-set-col 2 " " t)))))
  322. (defun Buffer-menu-delete (&optional arg)
  323. "Mark the buffer on this Buffer Menu buffer line for deletion.
  324. A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command
  325. will delete it.
  326. If prefix argument ARG is non-nil, it specifies the number of
  327. buffers to delete; a negative ARG means to delete backwards."
  328. (interactive "p")
  329. (if (or (null arg) (= arg 0))
  330. (setq arg 1))
  331. (while (> arg 0)
  332. (when (Buffer-menu-buffer)
  333. (tabulated-list-set-col 0 "D" t))
  334. (forward-line 1)
  335. (setq arg (1- arg)))
  336. (while (< arg 0)
  337. (when (Buffer-menu-buffer)
  338. (tabulated-list-set-col 0 "D" t))
  339. (forward-line -1)
  340. (setq arg (1+ arg))))
  341. (defun Buffer-menu-delete-backwards (&optional arg)
  342. "Mark the buffer on this Buffer Menu line for deletion, and move up.
  343. A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]'
  344. command will delete the marked buffer. Prefix ARG means move
  345. that many lines."
  346. (interactive "p")
  347. (Buffer-menu-delete (- (or arg 1))))
  348. (defun Buffer-menu-save ()
  349. "Mark the buffer on this Buffer Menu line for saving.
  350. A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command
  351. will save it."
  352. (interactive)
  353. (when (Buffer-menu-buffer)
  354. (tabulated-list-set-col 2 "S" t)
  355. (forward-line 1)))
  356. (defun Buffer-menu-not-modified (&optional arg)
  357. "Mark the buffer on this line as unmodified (no changes to save).
  358. If ARG is non-nil (interactively, with a prefix argument), mark
  359. it as modified."
  360. (interactive "P")
  361. (with-current-buffer (Buffer-menu-buffer t)
  362. (set-buffer-modified-p arg))
  363. (tabulated-list-set-col 2 (if arg "*" " ") t))
  364. (defun Buffer-menu-execute ()
  365. "Save and/or delete marked buffers in the Buffer Menu.
  366. Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-save]' are saved.
  367. Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-delete]' are deleted."
  368. (interactive)
  369. (save-excursion
  370. (Buffer-menu-beginning)
  371. (while (not (eobp))
  372. (let ((buffer (tabulated-list-get-id))
  373. (entry (tabulated-list-get-entry)))
  374. (cond ((null entry)
  375. (forward-line 1))
  376. ((not (buffer-live-p buffer))
  377. (tabulated-list-delete-entry))
  378. (t
  379. (let ((delete (eq (char-after) ?D)))
  380. (when (equal (aref entry 2) "S")
  381. (condition-case nil
  382. (progn
  383. (with-current-buffer buffer
  384. (save-buffer))
  385. (tabulated-list-set-col 2 " " t))
  386. (error (warn "Error saving %s" buffer))))
  387. (if delete
  388. (unless (eq buffer (current-buffer))
  389. (kill-buffer buffer)
  390. (tabulated-list-delete-entry))
  391. (forward-line 1)))))))))
  392. (defun Buffer-menu-select ()
  393. "Select this line's buffer; also, display buffers marked with `>'.
  394. You can mark buffers with the \\<Buffer-menu-mode-map>`\\[Buffer-menu-mark]' command.
  395. This command deletes and replaces all the previously existing windows
  396. in the selected frame."
  397. (interactive)
  398. (let* ((this-buffer (Buffer-menu-buffer t))
  399. (menu-buffer (current-buffer))
  400. (others (delq this-buffer (Buffer-menu-marked-buffers t)))
  401. (height (/ (1- (frame-height)) (1+ (length others)))))
  402. (delete-other-windows)
  403. (switch-to-buffer this-buffer)
  404. (unless (eq menu-buffer this-buffer)
  405. (bury-buffer menu-buffer))
  406. (dolist (buffer others)
  407. (split-window nil height)
  408. (other-window 1)
  409. (switch-to-buffer buffer))
  410. ;; Back to the beginning!
  411. (other-window 1)))
  412. (defun Buffer-menu-marked-buffers (&optional unmark)
  413. "Return the list of buffers marked with `Buffer-menu-mark'.
  414. If UNMARK is non-nil, unmark them."
  415. (let (buffers)
  416. (Buffer-menu-beginning)
  417. (while (re-search-forward "^>" nil t)
  418. (let ((buffer (Buffer-menu-buffer)))
  419. (if (and buffer unmark)
  420. (tabulated-list-set-col 0 " " t))
  421. (if (buffer-live-p buffer)
  422. (push buffer buffers))))
  423. (nreverse buffers)))
  424. (defun Buffer-menu-isearch-buffers ()
  425. "Search for a string through all marked buffers using Isearch."
  426. (interactive)
  427. (multi-isearch-buffers (Buffer-menu-marked-buffers)))
  428. (defun Buffer-menu-isearch-buffers-regexp ()
  429. "Search for a regexp through all marked buffers using Isearch."
  430. (interactive)
  431. (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
  432. (defun Buffer-menu-multi-occur (regexp &optional nlines)
  433. "Show all lines in marked buffers containing a match for a regexp."
  434. (interactive (occur-read-primary-args))
  435. (multi-occur (Buffer-menu-marked-buffers) regexp nlines))
  436. (defun Buffer-menu-visit-tags-table ()
  437. "Visit the tags table in the buffer on this line. See `visit-tags-table'."
  438. (interactive)
  439. (let ((file (buffer-file-name (Buffer-menu-buffer t))))
  440. (if file
  441. (visit-tags-table file)
  442. (error "Specified buffer has no file"))))
  443. (defun Buffer-menu-1-window ()
  444. "Select this line's buffer, alone, in full frame."
  445. (interactive)
  446. (switch-to-buffer (Buffer-menu-buffer t))
  447. (bury-buffer (other-buffer))
  448. (delete-other-windows))
  449. (defun Buffer-menu-this-window ()
  450. "Select this line's buffer in this window."
  451. (interactive)
  452. (switch-to-buffer (Buffer-menu-buffer t)))
  453. (defun Buffer-menu-other-window ()
  454. "Select this line's buffer in other window, leaving buffer menu visible."
  455. (interactive)
  456. (switch-to-buffer-other-window (Buffer-menu-buffer t)))
  457. (defun Buffer-menu-switch-other-window ()
  458. "Make the other window select this line's buffer.
  459. The current window remains selected."
  460. (interactive)
  461. (display-buffer (Buffer-menu-buffer t) t))
  462. (defun Buffer-menu-2-window ()
  463. "Select this line's buffer, with previous buffer in second window."
  464. (interactive)
  465. (let ((buff (Buffer-menu-buffer t))
  466. (menu (current-buffer)))
  467. (delete-other-windows)
  468. (switch-to-buffer (other-buffer))
  469. (switch-to-buffer-other-window buff)
  470. (bury-buffer menu)))
  471. (defun Buffer-menu-toggle-read-only ()
  472. "Toggle read-only status of buffer on this line.
  473. This behaves like invoking \\[read-only-mode] in that buffer."
  474. (interactive)
  475. (let ((read-only
  476. (with-current-buffer (Buffer-menu-buffer t)
  477. (read-only-mode 'toggle)
  478. buffer-read-only)))
  479. (tabulated-list-set-col 1 (if read-only "%" " ") t)))
  480. (defun Buffer-menu-bury ()
  481. "Bury the buffer listed on this line."
  482. (interactive)
  483. (let ((buffer (tabulated-list-get-id)))
  484. (cond ((null buffer))
  485. ((buffer-live-p buffer)
  486. (bury-buffer buffer)
  487. (save-excursion
  488. (let ((elt (tabulated-list-delete-entry)))
  489. (goto-char (point-max))
  490. (apply 'tabulated-list-print-entry elt)))
  491. (message "Buffer buried."))
  492. (t
  493. (tabulated-list-delete-entry)
  494. (message "Buffer is dead; removing from list.")))))
  495. (defun Buffer-menu-view ()
  496. "View this line's buffer in View mode."
  497. (interactive)
  498. (view-buffer (Buffer-menu-buffer t)))
  499. (defun Buffer-menu-view-other-window ()
  500. "View this line's buffer in View mode in another window."
  501. (interactive)
  502. (view-buffer-other-window (Buffer-menu-buffer t)))
  503. ;;; Functions for populating the Buffer Menu.
  504. ;;;###autoload
  505. (defun list-buffers-noselect (&optional files-only buffer-list)
  506. "Create and return a Buffer Menu buffer.
  507. This is called by `buffer-menu' and others as a subroutine.
  508. If FILES-ONLY is non-nil, show only file-visiting buffers.
  509. If BUFFER-LIST is non-nil, it should be a list of buffers; it
  510. means list those buffers and no others."
  511. (let ((old-buffer (current-buffer))
  512. (buffer (get-buffer-create "*Buffer List*")))
  513. (with-current-buffer buffer
  514. (Buffer-menu-mode)
  515. (setq Buffer-menu-files-only
  516. (and files-only (>= (prefix-numeric-value files-only) 0)))
  517. (list-buffers--refresh buffer-list old-buffer)
  518. (tabulated-list-print))
  519. buffer))
  520. (defun Buffer-menu-mouse-select (event)
  521. "Select the buffer whose line you click on."
  522. (interactive "e")
  523. (select-window (posn-window (event-end event)))
  524. (let ((buffer (tabulated-list-get-id (posn-point (event-end event)))))
  525. (when (buffer-live-p buffer)
  526. (if (and (window-dedicated-p)
  527. (eq (selected-window) (frame-root-window)))
  528. (switch-to-buffer-other-frame buffer)
  529. (switch-to-buffer buffer)))))
  530. (defun list-buffers--refresh (&optional buffer-list old-buffer)
  531. ;; Set up `tabulated-list-format'.
  532. (let ((name-width Buffer-menu-name-width)
  533. (size-width Buffer-menu-size-width))
  534. ;; Handle obsolete variable:
  535. (if Buffer-menu-buffer+size-width
  536. (setq name-width (- Buffer-menu-buffer+size-width size-width)))
  537. (setq tabulated-list-format
  538. (vector '("C" 1 t :pad-right 0)
  539. '("R" 1 t :pad-right 0)
  540. '("M" 1 t)
  541. `("Buffer" ,name-width t)
  542. `("Size" ,size-width tabulated-list-entry-size->
  543. :right-align t)
  544. `("Mode" ,Buffer-menu-mode-width t)
  545. '("File" 1 t))))
  546. (setq tabulated-list-use-header-line Buffer-menu-use-header-line)
  547. ;; Collect info for each buffer we're interested in.
  548. (let ((buffer-menu-buffer (current-buffer))
  549. (show-non-file (not Buffer-menu-files-only))
  550. entries)
  551. (dolist (buffer (or buffer-list
  552. (buffer-list (if Buffer-menu-use-frame-buffer-list
  553. (selected-frame)))))
  554. (with-current-buffer buffer
  555. (let* ((name (buffer-name))
  556. (file buffer-file-name))
  557. (when (and (buffer-live-p buffer)
  558. (or buffer-list
  559. (and (not (string= (substring name 0 1) " "))
  560. (not (eq buffer buffer-menu-buffer))
  561. (or file show-non-file))))
  562. (push (list buffer
  563. (vector (if (eq buffer old-buffer) "." " ")
  564. (if buffer-read-only "%" " ")
  565. (if (buffer-modified-p) "*" " ")
  566. (Buffer-menu--pretty-name name)
  567. (number-to-string (buffer-size))
  568. (concat (format-mode-line mode-name nil nil buffer)
  569. (if mode-line-process
  570. (format-mode-line mode-line-process
  571. nil nil buffer)))
  572. (Buffer-menu--pretty-file-name file)))
  573. entries)))))
  574. (setq tabulated-list-entries (nreverse entries)))
  575. (tabulated-list-init-header))
  576. (defun tabulated-list-entry-size-> (entry1 entry2)
  577. (> (string-to-number (aref (cadr entry1) 4))
  578. (string-to-number (aref (cadr entry2) 4))))
  579. (defun Buffer-menu--pretty-name (name)
  580. (propertize name
  581. 'font-lock-face 'buffer-menu-buffer
  582. 'mouse-face 'highlight))
  583. (defun Buffer-menu--pretty-file-name (file)
  584. (cond (file
  585. (abbreviate-file-name file))
  586. ((and (boundp 'list-buffers-directory)
  587. list-buffers-directory)
  588. list-buffers-directory)
  589. ((eq major-mode 'Info-mode)
  590. (Buffer-menu-info-node-description Info-current-file))
  591. (t "")))
  592. (defun Buffer-menu-info-node-description (file)
  593. (cond
  594. ((equal file "dir") "*Info Directory*")
  595. ((eq file 'apropos) "*Info Apropos*")
  596. ((eq file 'history) "*Info History*")
  597. ((eq file 'toc) "*Info TOC*")
  598. ((not (stringp file)) "") ; Avoid errors
  599. (t
  600. (concat "(" (file-name-nondirectory file) ") " Info-current-node))))
  601. ;;; buff-menu.el ends here