buff-menu.el 35 KB

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