bs.el 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. ;;; bs.el --- menu for selecting and displaying buffers -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Olaf Sylvester <Olaf.Sylvester@netsurf.de>
  4. ;; Maintainer: Olaf Sylvester <Olaf.Sylvester@netsurf.de>
  5. ;; Keywords: convenience
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Version: 1.17
  19. ;; X-URL: http://www.geekware.de/software/emacs
  20. ;;
  21. ;; The bs-package contains a main function bs-show for popping up a
  22. ;; buffer in a way similar to `list-buffers' and `electric-buffer-list':
  23. ;; The new buffer offers a Buffer Selection Menu for manipulating
  24. ;; the buffer list and buffers.
  25. ;;
  26. ;; -----------------------------------------------------------------------
  27. ;; | MR Buffer Size Mode File |
  28. ;; | -- ------ ---- ---- ---- |
  29. ;; |. bs.el 14690 Emacs-Lisp /home/sun/sylvester/el/bs.e$|
  30. ;; | % executable.el 9429 Emacs-Lisp /usr/share/emacs/19.34/lisp$|
  31. ;; | % vc.el 104893 Emacs-Lisp /usr/share/emacs/19.34/lisp$|
  32. ;; | % test_vc.el 486 Emacs-Lisp /home/sun/sylvester/el/test$|
  33. ;; | % vc-hooks.el 43605 Emacs-Lisp /usr/share/emacs/19.34/lisp$|
  34. ;; -----------------------------------------------------------------------
  35. ;;; Quick Installation and Customization:
  36. ;; To display the bs menu, do
  37. ;; M-x bs-show
  38. ;; To customize its behavior, do
  39. ;; M-x bs-customize
  40. ;;; More Commentary:
  41. ;; bs-show will generate a new buffer named *buffer-selection*, which shows
  42. ;; all buffers or a subset of them, and has possibilities for deleting,
  43. ;; saving and selecting buffers. For more details see docstring of
  44. ;; function `bs-mode'. A current configuration describes which buffers appear
  45. ;; in *buffer-selection*. See docstring of variable `bs-configurations' for
  46. ;; more details.
  47. ;;
  48. ;; The package bs combines the advantages of the Emacs functions
  49. ;; `list-buffers' and `electric-buffer-list'.
  50. ;;
  51. ;; Additional features for Buffer Selection Menu:
  52. ;; - configurable list of buffers (show only files etc.).
  53. ;; - comfortable way to change displayed subset of all buffers.
  54. ;; - show sorted list of buffers.
  55. ;; - cyclic navigation:
  56. ;; - goes to top of buffer list if you are on last line and press down.
  57. ;; - goes to end of buffer list if you are on first line and press up.
  58. ;; - Offer an alternative buffer list by prefix key C-u.
  59. ;;; Cycling through buffers
  60. ;; This package offers two functions for buffer cycling. If you want to cycle
  61. ;; through buffer list you can use `bs-cycle-next' or `bs-cycle-previous'.
  62. ;; Bind these function to a key like
  63. ;; (global-set-key [(f9)] 'bs-cycle-previous)
  64. ;; (global-set-key [(f10)] 'bs-cycle-next)
  65. ;;
  66. ;; Both functions use a special subset of all buffers for cycling to avoid
  67. ;; to go through internal buffers like *Messages*.
  68. ;;
  69. ;; Cycling through buffers ignores sorting because sorting destroys
  70. ;; the logical buffer list. If buffer list is sorted by size you
  71. ;; won't be able to cycle to the smallest buffer.
  72. ;;; Customization:
  73. ;; There is a customization group called `bs' in group `convenience'.
  74. ;; Start customization by M-x bs-customize
  75. ;;
  76. ;; Buffer list
  77. ;; -----------
  78. ;; You can define your own configurations by extending variable
  79. ;; `bs-configurations' (see docstring for details).
  80. ;;
  81. ;; `bs-default-configuration' contains the name of default configuration.
  82. ;; The default value is "files" which means to show only files.
  83. ;;
  84. ;; If you always want to see all buffers, customize variable
  85. ;; `bs-default-configuration' in customization group `bs'.
  86. ;;
  87. ;; Configure sorting
  88. ;; -----------------
  89. ;; You can define functions for sorting the buffer list.
  90. ;; When selecting buffers, you can step through available sorting
  91. ;; methods with key 'S'.
  92. ;; To define a new way of sorting, customize variable `bs-sort-functions'.
  93. ;;
  94. ;; There are four basic functions for sorting:
  95. ;; by buffer name, by mode, by size, or by filename
  96. ;;
  97. ;; Configure buffer cycling
  98. ;; ------------------------
  99. ;; When cycling through buffer list the functions for cycling will use
  100. ;; the current configuration of bs to calculate the buffer list.
  101. ;; If you want to use a different configuration for cycling you have to set
  102. ;; the variable `bs-cycle-configuration-name'. You can customize this variable.
  103. ;;
  104. ;; For example: If you use the configuration called "files-and-scratch" you
  105. ;; can cycle through all file buffers and *scratch* although your current
  106. ;; configuration perhaps is "files" which ignores buffer *scratch*.
  107. ;;; History:
  108. ;;; Code:
  109. (eval-when-compile (require 'cl))
  110. ;; ----------------------------------------------------------------------
  111. ;; Globals for customization
  112. ;; ----------------------------------------------------------------------
  113. (defgroup bs nil
  114. "Buffer Selection: Maintaining buffers by buffer menu."
  115. :version "21.1"
  116. :link '(emacs-commentary-link "bs")
  117. :link '(url-link "http://www.geekware.de/software/emacs")
  118. :group 'convenience)
  119. (defgroup bs-appearance nil
  120. "Buffer Selection appearance: Appearance of bs buffer menu."
  121. :group 'bs)
  122. (defcustom bs-attributes-list
  123. '(("" 1 1 left bs--get-marked-string)
  124. ("M" 1 1 left bs--get-modified-string)
  125. ("R" 2 2 left bs--get-readonly-string)
  126. ("Buffer" bs--get-name-length 10 left bs--get-name)
  127. ("" 1 1 left " ")
  128. ("Size" 8 8 right bs--get-size-string)
  129. ("" 1 1 left " ")
  130. ("Mode" 12 12 right bs--get-mode-name)
  131. ("" 2 2 left " ")
  132. ("File" 12 12 left bs--get-file-name)
  133. ("" 2 2 left " "))
  134. "List specifying the layout of a Buffer Selection Menu buffer.
  135. Each entry specifies a column and is a list of the form of:
  136. \(HEADER MINIMUM-LENGTH MAXIMUM-LENGTH ALIGNMENT FUN-OR-STRING)
  137. HEADER : String for header for first line or a function
  138. which calculates column title.
  139. MINIMUM-LENGTH : Minimum width of column (number or name of function).
  140. The function must return a positive integer.
  141. MAXIMUM-LENGTH : Maximum width of column (number or name of function)
  142. (currently ignored).
  143. ALIGNMENT : Alignment of column (`left', `right', `middle').
  144. FUN-OR-STRING : Name of a function for calculating the value or a
  145. string for a constant value.
  146. The function gets as parameter the buffer where we have started
  147. buffer selection and the list of all buffers to show. The function must
  148. return a string representing the column's value."
  149. :group 'bs-appearance
  150. :type '(repeat sexp))
  151. (defun bs--make-header-match-string ()
  152. "Return a regexp matching the first line of a Buffer Selection Menu buffer."
  153. (concat "^\\(" (mapconcat #'car bs-attributes-list " *") " *$\\)"))
  154. ;; Font-Lock-Settings
  155. (defvar bs-mode-font-lock-keywords
  156. (list ;; header in font-lock-type-face
  157. (list (bs--make-header-match-string)
  158. '(1 font-lock-type-face append) '(1 'bold append))
  159. ;; Buffername embedded by *
  160. (list "^\\(.*\\*.*\\*.*\\)$"
  161. 1
  162. ;; problem in XEmacs with font-lock-constant-face
  163. (if (facep 'font-lock-constant-face)
  164. 'font-lock-constant-face
  165. 'font-lock-comment-face))
  166. ;; Dired-Buffers
  167. '("^..\\(.*Dired .*\\)$" 1 font-lock-function-name-face)
  168. ;; the star for modified buffers
  169. '("^.\\(\\*\\) +[^\\*]" 1 font-lock-comment-face))
  170. "Default font lock expressions for Buffer Selection Menu.")
  171. (defcustom bs-max-window-height 20
  172. "Maximal window height of Buffer Selection Menu."
  173. :group 'bs-appearance
  174. :type 'integer)
  175. (defvar bs-dont-show-regexp nil
  176. "Regular expression specifying which buffers not to show.
  177. A buffer whose name matches this regular expression will not be
  178. included in the buffer list.")
  179. (defvar bs-must-show-regexp nil
  180. "Regular expression for specifying buffers which must be shown.
  181. A buffer whose name matches this regular expression will be
  182. included in the buffer list.
  183. Note that this variable is temporary: if the configuration is changed
  184. it is reset to nil. Use `bs-must-always-show-regexp' to specify buffers
  185. that must always be shown regardless of the configuration.")
  186. (defcustom bs-must-always-show-regexp nil
  187. "Regular expression for specifying buffers to show always.
  188. A buffer whose name matches this regular expression will
  189. be shown regardless of current configuration of Buffer Selection Menu."
  190. :group 'bs
  191. :type '(choice (const :tag "Nothing at all" nil) regexp))
  192. (defvar bs-dont-show-function nil
  193. "Function for specifying buffers not to show.
  194. The function gets one argument - the buffer to test. The function must
  195. return a value different from nil to ignore the buffer in
  196. Buffer Selection Menu.")
  197. (defvar bs-must-show-function nil
  198. "Function for specifying buffers which must be shown.
  199. The function gets one argument - the buffer to test.")
  200. (defvar bs-buffer-sort-function nil
  201. "Sort function to sort the buffers that appear in Buffer Selection Menu.
  202. The function gets two arguments - the buffers to compare.
  203. It must return non-nil if the first buffer should sort before the second.")
  204. (defcustom bs-maximal-buffer-name-column 45
  205. "Maximum column width for buffer names.
  206. The column for buffer names has dynamic width. The width depends on
  207. maximal and minimal length of names of buffers to show. The maximal
  208. width is bounded by `bs-maximal-buffer-name-column'.
  209. See also `bs-minimal-buffer-name-column'."
  210. :group 'bs-appearance
  211. :type 'integer)
  212. (defcustom bs-minimal-buffer-name-column 15
  213. "Minimum column width for buffer names.
  214. The column for buffer names has dynamic width. The width depends on
  215. maximal and minimal length of names of buffers to show. The minimal
  216. width is bounded by `bs-minimal-buffer-name-column'.
  217. See also `bs-maximal-buffer-name-column'."
  218. :group 'bs-appearance
  219. :type 'integer)
  220. (defconst bs-header-lines-length 2
  221. "Number of lines for headers in Buffer Selection Menu.")
  222. (defcustom bs-configurations
  223. '(("all" nil nil nil nil nil)
  224. ("files" nil nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)
  225. ("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file
  226. bs-sort-buffer-interns-are-last)
  227. ("all-intern-last" nil nil nil nil bs-sort-buffer-interns-are-last))
  228. "List of all configurations you can use in the Buffer Selection Menu.
  229. A configuration describes which buffers appear in Buffer Selection Menu
  230. and also the order of buffers. A configuration is a list with
  231. six elements. The first element is a string and describes the configuration.
  232. The following five elements represent the values for Buffer Selection Menu
  233. configuration variables `bs-must-show-regexp', `bs-must-show-function',
  234. `bs-dont-show-regexp', `bs-dont-show-function' and `bs-buffer-sort-function'.
  235. By setting these variables you define a configuration."
  236. :group 'bs-appearance
  237. :type '(repeat sexp))
  238. (defcustom bs-default-configuration "files"
  239. "Name of default configuration used by the Buffer Selection Menu.
  240. \\<bs-mode-map>
  241. Will be changed using key \\[bs-select-next-configuration].
  242. Must be a string used in `bs-configurations' for naming a configuration."
  243. :group 'bs
  244. :type 'string)
  245. (defcustom bs-alternative-configuration "all"
  246. "Name of configuration used when calling `bs-show' with \
  247. \\[universal-argument] as prefix key.
  248. Must be a string used in `bs-configurations' for naming a configuration."
  249. :group 'bs
  250. :type 'string)
  251. (defvar bs-current-configuration bs-default-configuration
  252. "Name of current configuration.
  253. Must be a string used in `bs-configurations' for naming a configuration.")
  254. (defcustom bs-cycle-configuration-name nil
  255. "Name of configuration used when cycling through the buffer list.
  256. A value of nil means to use current configuration `bs-default-configuration'.
  257. Must be a string used in `bs-configurations' for naming a configuration."
  258. :group 'bs
  259. :type '(choice (const :tag "like current configuration" nil)
  260. string))
  261. (defcustom bs-string-show-always "+"
  262. "String added in column 1 indicating a buffer will always be shown."
  263. :group 'bs-appearance
  264. :type 'string)
  265. (defcustom bs-string-show-never "-"
  266. "String added in column 1 indicating a buffer will never be shown."
  267. :group 'bs-appearance
  268. :type 'string)
  269. (defcustom bs-string-current "."
  270. "String added in column 1 indicating the current buffer."
  271. :group 'bs-appearance
  272. :type 'string)
  273. (defcustom bs-string-current-marked "#"
  274. "String added in column 1 indicating the current buffer when it is marked."
  275. :group 'bs-appearance
  276. :type 'string)
  277. (defcustom bs-string-marked ">"
  278. "String added in column 1 indicating a marked buffer."
  279. :group 'bs-appearance
  280. :type 'string)
  281. (defcustom bs-string-show-normally " "
  282. "String added in column 1 indicating an unmarked buffer."
  283. :group 'bs-appearance
  284. :type 'string)
  285. (defvar bs--name-entry-length 20
  286. "Maximum length of all displayed buffer names.
  287. Used internally, only.")
  288. ;; ----------------------------------------------------------------------
  289. ;; Internal globals
  290. ;; ----------------------------------------------------------------------
  291. (defvar bs-buffer-show-mark nil
  292. "Flag for the current mode for showing this buffer.
  293. A value of nil means buffer will be shown depending on the current
  294. configuration.
  295. A value of `never' means to never show the buffer.
  296. A value of `always' means to show buffer regardless of the configuration.")
  297. (make-variable-buffer-local 'bs-buffer-show-mark)
  298. ;; Make face named region (for XEmacs)
  299. (unless (facep 'region)
  300. (make-face 'region)
  301. (set-face-background 'region "gray75"))
  302. (defun bs--sort-by-name (b1 b2)
  303. "Compare buffers B1 and B2 by buffer name."
  304. (string< (buffer-name b1)
  305. (buffer-name b2)))
  306. (defun bs--sort-by-filename (b1 b2)
  307. "Compare buffers B1 and B2 by file name."
  308. (string< (or (buffer-file-name b1) "")
  309. (or (buffer-file-name b2) "")))
  310. (defun bs--sort-by-mode (b1 b2)
  311. "Compare buffers B1 and B2 by mode name."
  312. (save-current-buffer
  313. (string< (progn (set-buffer b1) (format-mode-line mode-name nil nil b1))
  314. (progn (set-buffer b2) (format-mode-line mode-name nil nil b2)))))
  315. (defun bs--sort-by-size (b1 b2)
  316. "Compare buffers B1 and B2 by buffer size."
  317. (< (buffer-size b1) (buffer-size b2)))
  318. (defcustom bs-sort-functions
  319. '(("by name" bs--sort-by-name "Buffer" region)
  320. ("by size" bs--sort-by-size "Size" region)
  321. ("by mode" bs--sort-by-mode "Mode" region)
  322. ("by filename" bs--sort-by-filename "File" region)
  323. ("by nothing" nil nil nil))
  324. "List of all possible sorting aspects for Buffer Selection Menu.
  325. You can add a new entry with a call to `bs-define-sort-function'.
  326. Each element is a list of four elements (NAME FUNCTION REGEXP-FOR-SORTING FACE).
  327. NAME specifies the sort order defined by function FUNCTION.
  328. FUNCTION nil means don't sort the buffer list. Otherwise the function
  329. must have two parameters - the buffers to compare.
  330. REGEXP-FOR-SORTING is a regular expression which describes the
  331. column title to highlight.
  332. FACE is a face used to fontify the sorted column title. A value of nil means
  333. don't highlight."
  334. :group 'bs
  335. :type '(repeat sexp))
  336. (defun bs-define-sort-function (name fun &optional regexp-for-sorting face)
  337. "Define a new function for buffer sorting in Buffer Selection Menu.
  338. NAME specifies the sort order defined by function FUN.
  339. A value of nil for FUN means don't sort the buffer list. Otherwise the
  340. functions must have two parameters - the buffers to compare.
  341. REGEXP-FOR-SORTING is a regular expression which describes the
  342. column title to highlight.
  343. FACE is a face used to fontify the sorted column title. A value of nil means
  344. don't highlight.
  345. The new sort aspect will be inserted into list `bs-sort-functions'."
  346. (let ((tupel (assoc name bs-sort-functions)))
  347. (if tupel
  348. (setcdr tupel (list fun regexp-for-sorting face))
  349. (setq bs-sort-functions
  350. (cons (list name fun regexp-for-sorting face)
  351. bs-sort-functions)))))
  352. (defvar bs--current-sort-function nil
  353. "Description of the current function for sorting the buffer list.
  354. This is an element of `bs-sort-functions'.")
  355. (defcustom bs-default-sort-name "by nothing"
  356. "Name of default sort behavior.
  357. Must be \"by nothing\" or a string used in `bs-sort-functions' for
  358. naming a sort behavior. Default is \"by nothing\" which means no sorting."
  359. :group 'bs
  360. :type 'string
  361. :set (lambda (var-name value)
  362. (set var-name value)
  363. (setq bs--current-sort-function
  364. (assoc value bs-sort-functions))))
  365. (defvar bs--buffer-coming-from nil
  366. "The buffer in which the user started the current Buffer Selection Menu.")
  367. (defvar bs--show-all nil
  368. "Flag whether showing all buffers regardless of current configuration.
  369. Non-nil means to show all buffers. Otherwise show buffers
  370. defined by current configuration `bs-current-configuration'.")
  371. (defvar bs--window-config-coming-from nil
  372. "Window configuration before starting Buffer Selection Menu.")
  373. (defvar bs--intern-show-never "^ \\|\\*buffer-selection\\*"
  374. "Regular expression specifying which buffers never to show.
  375. A buffer whose name matches this regular expression will never be
  376. included in the buffer list.")
  377. (defvar bs-current-list nil
  378. "List of buffers shown in Buffer Selection Menu.
  379. Used internally, only.")
  380. (defvar bs--marked-buffers nil
  381. "Currently marked buffers in Buffer Selection Menu.")
  382. (defvar bs-mode-map
  383. (let ((map (make-sparse-keymap)))
  384. (define-key map " " 'bs-select)
  385. (define-key map "f" 'bs-select)
  386. (define-key map "v" 'bs-view)
  387. (define-key map "!" 'bs-select-in-one-window)
  388. (define-key map [mouse-2] 'bs-mouse-select) ;; for GNU EMACS
  389. (define-key map [button2] 'bs-mouse-select) ;; for XEmacs
  390. (define-key map "F" 'bs-select-other-frame)
  391. (let ((key ?1))
  392. (while (<= key ?9)
  393. (define-key map (char-to-string key) 'digit-argument)
  394. (setq key (1+ key))))
  395. (define-key map "-" 'negative-argument)
  396. (define-key map "\e-" 'negative-argument)
  397. (define-key map "o" 'bs-select-other-window)
  398. (define-key map "\C-o" 'bs-tmp-select-other-window)
  399. ;; for GNU EMACS
  400. (define-key map [mouse-3] 'bs-mouse-select-other-frame)
  401. ;; for XEmacs
  402. (define-key map [button3] 'bs-mouse-select-other-frame)
  403. (define-key map [up] 'bs-up)
  404. (define-key map "n" 'bs-down)
  405. (define-key map "p" 'bs-up)
  406. (define-key map [down] 'bs-down)
  407. (define-key map "\C-m" 'bs-select)
  408. (define-key map "b" 'bs-bury-buffer)
  409. (define-key map "s" 'bs-save)
  410. (define-key map "S" 'bs-show-sorted)
  411. (define-key map "a" 'bs-toggle-show-all)
  412. (define-key map "d" 'bs-delete)
  413. (define-key map "\C-d" 'bs-delete-backward)
  414. (define-key map "k" 'bs-delete)
  415. (define-key map "g" 'bs-refresh)
  416. (define-key map "C" 'bs-set-configuration-and-refresh)
  417. (define-key map "c" 'bs-select-next-configuration)
  418. (define-key map "q" 'bs-kill)
  419. ;; (define-key map "z" 'bs-kill)
  420. (define-key map "\C-c\C-c" 'bs-kill)
  421. (define-key map "\C-g" 'bs-abort)
  422. (define-key map "\C-]" 'bs-abort)
  423. (define-key map "%" 'bs-toggle-readonly)
  424. (define-key map "~" 'bs-clear-modified)
  425. (define-key map "M" 'bs-toggle-current-to-show)
  426. (define-key map "+" 'bs-set-current-buffer-to-show-always)
  427. ;;(define-key map "-" 'bs-set-current-buffer-to-show-never)
  428. (define-key map "t" 'bs-visit-tags-table)
  429. (define-key map "m" 'bs-mark-current)
  430. (define-key map "u" 'bs-unmark-current)
  431. (define-key map ">" 'scroll-right)
  432. (define-key map "<" 'scroll-left)
  433. (define-key map "?" 'bs-help)
  434. map)
  435. "Keymap of `bs-mode'.")
  436. ;; ----------------------------------------------------------------------
  437. ;; Functions
  438. ;; ----------------------------------------------------------------------
  439. (defun bs-buffer-list (&optional list sort-description)
  440. "Return a list of buffers to be shown.
  441. LIST is a list of buffers to test for appearance in Buffer Selection Menu.
  442. The result list depends on the global variables `bs-dont-show-regexp',
  443. `bs-must-show-regexp', `bs-dont-show-function', `bs-must-show-function'
  444. and `bs-buffer-sort-function'.
  445. If SORT-DESCRIPTION isn't nil the list will be sorted by
  446. a special function. SORT-DESCRIPTION is an element of `bs-sort-functions'."
  447. (setq sort-description (or sort-description bs--current-sort-function)
  448. list (or list (buffer-list)))
  449. (let ((result nil))
  450. (dolist (buf list)
  451. (let* ((buffername (buffer-name buf))
  452. (int-show-never (string-match-p bs--intern-show-never buffername))
  453. (ext-show-never (and bs-dont-show-regexp
  454. (string-match-p bs-dont-show-regexp
  455. buffername)))
  456. (extern-must-show (or (and bs-must-always-show-regexp
  457. (string-match-p
  458. bs-must-always-show-regexp
  459. buffername))
  460. (and bs-must-show-regexp
  461. (string-match-p bs-must-show-regexp
  462. buffername))))
  463. (extern-show-never-from-fun (and bs-dont-show-function
  464. (funcall bs-dont-show-function
  465. buf)))
  466. (extern-must-show-from-fun (and bs-must-show-function
  467. (funcall bs-must-show-function
  468. buf)))
  469. (show-flag (buffer-local-value 'bs-buffer-show-mark buf)))
  470. (when (or (eq show-flag 'always)
  471. (and (or bs--show-all (not (eq show-flag 'never)))
  472. (not int-show-never)
  473. (or bs--show-all
  474. extern-must-show
  475. extern-must-show-from-fun
  476. (and (not ext-show-never)
  477. (not extern-show-never-from-fun)))))
  478. (setq result (cons buf result)))))
  479. (setq result (reverse result))
  480. ;; The current buffer which was the start point of bs should be an element
  481. ;; of result list, so that we can leave with space and be back in the
  482. ;; buffer we started bs-show.
  483. (when (and bs--buffer-coming-from
  484. (buffer-live-p bs--buffer-coming-from)
  485. (not (memq bs--buffer-coming-from result)))
  486. (setq result (cons bs--buffer-coming-from result)))
  487. ;; sorting
  488. (if (and sort-description
  489. (nth 1 sort-description))
  490. (setq result (sort result (nth 1 sort-description)))
  491. ;; else standard sorting
  492. (bs-buffer-sort result))))
  493. (defun bs-buffer-sort (buffer-list)
  494. "Sort buffers in BUFFER-LIST according to `bs-buffer-sort-function'."
  495. (if bs-buffer-sort-function
  496. (sort buffer-list bs-buffer-sort-function)
  497. buffer-list))
  498. (defun bs--redisplay (&optional keep-line-p sort-description)
  499. "Redisplay whole Buffer Selection Menu.
  500. If KEEP-LINE-P is non-nil the point will stay on current line.
  501. SORT-DESCRIPTION is an element of `bs-sort-functions'."
  502. (let ((line (count-lines 1 (point))))
  503. (bs-show-in-buffer (bs-buffer-list nil sort-description))
  504. (when keep-line-p
  505. (goto-char (point-min))
  506. (forward-line line))
  507. (beginning-of-line)))
  508. (defun bs--goto-current-buffer ()
  509. "Goto line which represents the current buffer;
  510. actually the line which begins with character in `bs-string-current' or
  511. `bs-string-current-marked'."
  512. (let ((regexp (concat "^"
  513. (regexp-quote bs-string-current)
  514. "\\|^"
  515. (regexp-quote bs-string-current-marked)))
  516. point)
  517. (save-excursion
  518. (goto-char (point-min))
  519. (when (search-forward-regexp regexp nil t)
  520. (setq point (1- (point)))))
  521. (when point
  522. (goto-char point))))
  523. (defun bs--current-config-message ()
  524. "Return a string describing the current `bs-mode' configuration."
  525. (if bs--show-all
  526. "Show all buffers."
  527. (format "Show buffer by configuration %S"
  528. bs-current-configuration)))
  529. (defun bs--track-window-changes (frame)
  530. "Track window changes to refresh the buffer list.
  531. Used from `window-size-change-functions'."
  532. (let ((win (get-buffer-window "*buffer-selection*" frame)))
  533. (when win
  534. (with-selected-window win
  535. (bs--set-window-height)))))
  536. (defun bs--remove-hooks ()
  537. "Remove `bs--track-window-changes' and auxiliary hooks."
  538. (remove-hook 'window-size-change-functions 'bs--track-window-changes)
  539. ;; Remove itself
  540. (remove-hook 'kill-buffer-hook 'bs--remove-hooks t)
  541. (remove-hook 'change-major-mode-hook 'bs--remove-hooks t))
  542. (put 'bs-mode 'mode-class 'special)
  543. (define-derived-mode bs-mode nil "Buffer-Selection-Menu"
  544. "Major mode for editing a subset of Emacs's buffers.
  545. \\<bs-mode-map>
  546. Aside from two header lines each line describes one buffer.
  547. Move to a line representing the buffer you want to edit and select
  548. buffer by \\[bs-select] or SPC. Abort buffer list with \\[bs-kill].
  549. There are many key commands similar to `Buffer-menu-mode' for
  550. manipulating the buffer list and buffers.
  551. For faster navigation each digit key is a digit argument.
  552. \\[bs-select] or SPACE -- select current line's buffer and other marked buffers.
  553. \\[bs-toggle-show-all] -- toggle between all buffers and a special subset.
  554. \\[bs-select-other-window] -- select current line's buffer in other window.
  555. \\[bs-tmp-select-other-window] -- make another window display that buffer and
  556. remain in Buffer Selection Menu.
  557. \\[bs-mouse-select] -- select current line's buffer and other marked buffers.
  558. \\[bs-save] -- save current line's buffer immediately.
  559. \\[bs-delete] -- kill current line's buffer immediately.
  560. \\[bs-toggle-readonly] -- toggle read-only status of current line's buffer.
  561. \\[bs-clear-modified] -- clear modified-flag on that buffer.
  562. \\[bs-mark-current] -- mark current line's buffer to be displayed.
  563. \\[bs-unmark-current] -- unmark current line's buffer to be displayed.
  564. \\[bs-show-sorted] -- display buffer list sorted by next sort aspect.
  565. \\[bs-set-configuration-and-refresh] -- ask user for a configuration and \
  566. apply selected configuration.
  567. \\[bs-select-next-configuration] -- select and apply next \
  568. available Buffer Selection Menu configuration.
  569. \\[bs-kill] -- leave Buffer Selection Menu without a selection.
  570. \\[bs-toggle-current-to-show] -- toggle status of appearance.
  571. \\[bs-set-current-buffer-to-show-always] -- mark current line's buffer \
  572. to show always.
  573. \\[bs-visit-tags-table] -- call `visit-tags-table' on current line's buffer.
  574. \\[bs-help] -- display this help text."
  575. (make-local-variable 'font-lock-defaults)
  576. (make-local-variable 'font-lock-verbose)
  577. (make-local-variable 'font-lock-global-modes)
  578. (buffer-disable-undo)
  579. (setq buffer-read-only t
  580. truncate-lines t
  581. show-trailing-whitespace nil
  582. font-lock-global-modes '(not bs-mode)
  583. font-lock-defaults '(bs-mode-font-lock-keywords t)
  584. font-lock-verbose nil)
  585. (set (make-local-variable 'revert-buffer-function) 'bs-refresh)
  586. (add-hook 'window-size-change-functions 'bs--track-window-changes)
  587. (add-hook 'kill-buffer-hook 'bs--remove-hooks nil t)
  588. (add-hook 'change-major-mode-hook 'bs--remove-hooks nil t))
  589. (defun bs--restore-window-config ()
  590. "Restore window configuration on the current frame."
  591. (when bs--window-config-coming-from
  592. (let ((frame (selected-frame)))
  593. (unwind-protect
  594. (set-window-configuration bs--window-config-coming-from)
  595. (select-frame frame)))
  596. (setq bs--window-config-coming-from nil)))
  597. (defun bs-kill ()
  598. "Let buffer disappear and reset window configuration."
  599. (interactive)
  600. (bury-buffer (current-buffer))
  601. (bs--restore-window-config))
  602. (defun bs-abort ()
  603. "Ding and leave Buffer Selection Menu without a selection."
  604. (interactive)
  605. (ding)
  606. (bs-kill))
  607. (defun bs-set-configuration-and-refresh ()
  608. "Ask user for a configuration and apply selected configuration.
  609. Refresh whole Buffer Selection Menu."
  610. (interactive)
  611. (call-interactively 'bs-set-configuration)
  612. (bs--redisplay t))
  613. (defun bs-refresh (&rest _ignored)
  614. "Refresh whole Buffer Selection Menu.
  615. Arguments are IGNORED (for `revert-buffer')."
  616. (interactive)
  617. (bs--redisplay t))
  618. (defun bs--set-window-height ()
  619. "Change the height of the selected window to suit the current buffer list."
  620. (unless (one-window-p t)
  621. (fit-window-to-buffer (selected-window) bs-max-window-height)))
  622. (defun bs--current-buffer ()
  623. "Return buffer on current line.
  624. Raise an error if not on a buffer line."
  625. (beginning-of-line)
  626. (let ((line (+ (- bs-header-lines-length)
  627. (count-lines 1 (point)))))
  628. (when (< line 0)
  629. (error "You are on a header row"))
  630. (nth line bs-current-list)))
  631. (defun bs--update-current-line ()
  632. "Update the entry on current line for Buffer Selection Menu."
  633. (let ((buffer (bs--current-buffer))
  634. (inhibit-read-only t))
  635. (beginning-of-line)
  636. (delete-region (point) (line-end-position))
  637. (bs--insert-one-entry buffer)
  638. (beginning-of-line)))
  639. (defun bs-view ()
  640. "View current line's buffer in View mode.
  641. Leave Buffer Selection Menu."
  642. (interactive)
  643. (view-buffer (bs--current-buffer)))
  644. (defun bs-select ()
  645. "Select current line's buffer and other marked buffers.
  646. If there are no marked buffers the window configuration before starting
  647. Buffer Selection Menu will be restored.
  648. If there are marked buffers each marked buffer and the current line's buffer
  649. will be selected in a window.
  650. Leave Buffer Selection Menu."
  651. (interactive)
  652. (let ((buffer (bs--current-buffer)))
  653. (bury-buffer (current-buffer))
  654. (bs--restore-window-config)
  655. (switch-to-buffer buffer)
  656. (when bs--marked-buffers
  657. ;; Some marked buffers for selection
  658. (let* ((all (delq buffer bs--marked-buffers))
  659. (height (/ (1- (frame-height)) (1+ (length all)))))
  660. (delete-other-windows)
  661. (switch-to-buffer buffer)
  662. (dolist (buf all)
  663. (split-window nil height)
  664. (other-window 1)
  665. (switch-to-buffer buf))
  666. ;; goto window we have started bs.
  667. (other-window 1)))))
  668. (defun bs-select-other-window ()
  669. "Select current line's buffer by `switch-to-buffer-other-window'.
  670. The window configuration before starting Buffer Selection Menu will be restored
  671. unless there is no other window. In this case a new window will be created.
  672. Leave Buffer Selection Menu."
  673. (interactive)
  674. (let ((buffer (bs--current-buffer)))
  675. (bury-buffer (current-buffer))
  676. (bs--restore-window-config)
  677. (switch-to-buffer-other-window buffer)))
  678. (defun bs-tmp-select-other-window ()
  679. "Make the other window select this line's buffer.
  680. The current window remains selected."
  681. (interactive)
  682. (let ((buffer (bs--current-buffer)))
  683. (display-buffer buffer t)))
  684. (defun bs-select-other-frame ()
  685. "Select current line's buffer in new created frame.
  686. Leave Buffer Selection Menu."
  687. (interactive)
  688. (let ((buffer (bs--current-buffer)))
  689. (bury-buffer (current-buffer))
  690. (bs--restore-window-config)
  691. (switch-to-buffer-other-frame buffer)))
  692. (defun bs-mouse-select-other-frame (event)
  693. "Select selected line's buffer in new created frame.
  694. Leave Buffer Selection Menu.
  695. EVENT: a mouse click event."
  696. (interactive "e")
  697. (mouse-set-point event)
  698. (bs-select-other-frame))
  699. (defun bs-mouse-select (event)
  700. "Select buffer on mouse click EVENT.
  701. Select buffer by `bs-select'."
  702. (interactive "e")
  703. (mouse-set-point event)
  704. (bs-select))
  705. (defun bs-select-in-one-window ()
  706. "Select current line's buffer in one window and delete other windows.
  707. Leave Buffer Selection Menu."
  708. (interactive)
  709. (bs-select)
  710. (delete-other-windows))
  711. (defun bs-bury-buffer ()
  712. "Bury buffer on current line."
  713. (interactive)
  714. (bury-buffer (bs--current-buffer))
  715. (bs--redisplay t))
  716. (defun bs-save ()
  717. "Save buffer on current line."
  718. (interactive)
  719. (with-current-buffer (bs--current-buffer)
  720. (save-buffer))
  721. (bs--update-current-line))
  722. (defun bs-visit-tags-table ()
  723. "Visit the tags table in the buffer on this line.
  724. See `visit-tags-table'."
  725. (interactive)
  726. (let ((file (buffer-file-name (bs--current-buffer))))
  727. (if file
  728. (visit-tags-table file)
  729. (error "Specified buffer has no file"))))
  730. (defun bs-toggle-current-to-show ()
  731. "Toggle status of showing flag for buffer in current line."
  732. (interactive)
  733. (let ((res
  734. (with-current-buffer (bs--current-buffer)
  735. (setq bs-buffer-show-mark (case bs-buffer-show-mark
  736. ((nil) 'never)
  737. ((never) 'always)
  738. (t nil))))))
  739. (bs--update-current-line)
  740. (bs--set-window-height)
  741. (bs--show-config-message res)))
  742. (defun bs-set-current-buffer-to-show-always (&optional not-to-show-p)
  743. "Toggle status of buffer on line to `always shown'.
  744. NOT-TO-SHOW-P: prefix argument.
  745. With no prefix argument the buffer on current line is marked to show
  746. always. Otherwise it is marked to show never."
  747. (interactive "P")
  748. (if not-to-show-p
  749. (bs-set-current-buffer-to-show-never)
  750. (bs--set-toggle-to-show (bs--current-buffer) 'always)))
  751. (defun bs-set-current-buffer-to-show-never ()
  752. "Toggle status of buffer on line to `never shown'."
  753. (interactive)
  754. (bs--set-toggle-to-show (bs--current-buffer) 'never))
  755. (defun bs--set-toggle-to-show (buffer what)
  756. "Set value `bs-buffer-show-mark' of buffer BUFFER to WHAT.
  757. Redisplay current line and display a message describing
  758. the status of buffer on current line."
  759. (with-current-buffer buffer (setq bs-buffer-show-mark what))
  760. (bs--update-current-line)
  761. (bs--set-window-height)
  762. (bs--show-config-message what))
  763. (defun bs--mark-unmark (count fun)
  764. "Call FUN on COUNT consecutive buffers of *buffer-selection*."
  765. (let ((dir (if (> count 0) 1 -1)))
  766. (dotimes (_i (abs count))
  767. (let ((buffer (bs--current-buffer)))
  768. (when buffer (funcall fun buffer))
  769. (bs--update-current-line)
  770. (bs-down dir)))))
  771. (defun bs-mark-current (count)
  772. "Mark buffers.
  773. COUNT is the number of buffers to mark.
  774. Move cursor vertically down COUNT lines."
  775. (interactive "p")
  776. (bs--mark-unmark count
  777. (lambda (buf)
  778. (add-to-list 'bs--marked-buffers buf))))
  779. (defun bs-unmark-current (count)
  780. "Unmark buffers.
  781. COUNT is the number of buffers to unmark.
  782. Move cursor vertically down COUNT lines."
  783. (interactive "p")
  784. (bs--mark-unmark count
  785. (lambda (buf)
  786. (setq bs--marked-buffers (delq buf bs--marked-buffers)))))
  787. (defun bs--show-config-message (what)
  788. "Show message indicating the new showing status WHAT.
  789. WHAT is a value of nil, `never', or `always'."
  790. (bs-message-without-log (cond ((null what)
  791. "Buffer will be shown normally.")
  792. ((eq what 'never)
  793. "Mark buffer to never be shown.")
  794. (t "Mark buffer to show always."))))
  795. (defun bs-delete ()
  796. "Kill buffer on current line."
  797. (interactive)
  798. (let ((current (bs--current-buffer))
  799. (inhibit-read-only t))
  800. (unless (kill-buffer current)
  801. (error "Buffer was not deleted"))
  802. (setq bs-current-list (delq current bs-current-list))
  803. (beginning-of-line)
  804. (delete-region (point) (save-excursion
  805. (end-of-line)
  806. (if (eobp) (point) (1+ (point)))))
  807. (when (eobp)
  808. (backward-delete-char 1)
  809. (beginning-of-line)
  810. (recenter -1))
  811. (bs--set-window-height)))
  812. (defun bs-delete-backward ()
  813. "Like `bs-delete' but go to buffer in front of current."
  814. (interactive)
  815. (let ((on-last-line-p (save-excursion (end-of-line) (eobp))))
  816. (bs-delete)
  817. (unless on-last-line-p
  818. (bs-up 1))))
  819. (defun bs-show-sorted ()
  820. "Show buffer list sorted by next sort aspect."
  821. (interactive)
  822. (setq bs--current-sort-function
  823. (bs-next-config-aux (car bs--current-sort-function)
  824. bs-sort-functions))
  825. (bs--redisplay)
  826. (bs--goto-current-buffer)
  827. (bs-message-without-log "Sorted %s" (car bs--current-sort-function)))
  828. (defun bs-apply-sort-faces (&optional sort-description)
  829. "Set text properties for the sort described by SORT-DESCRIPTION.
  830. SORT-DESCRIPTION is an element of `bs-sort-functions'.
  831. Default is `bs--current-sort-function'."
  832. (let ((sort-description (or sort-description
  833. bs--current-sort-function)))
  834. (save-excursion
  835. (goto-char (point-min))
  836. (when (and (nth 2 sort-description)
  837. (search-forward-regexp (nth 2 sort-description) nil t))
  838. (let ((inhibit-read-only t))
  839. (put-text-property (match-beginning 0)
  840. (match-end 0)
  841. 'face
  842. (or (nth 3 sort-description)
  843. 'region)))))))
  844. (defun bs-toggle-show-all ()
  845. "Toggle show all buffers / show buffers with current configuration."
  846. (interactive)
  847. (setq bs--show-all (not bs--show-all))
  848. (bs--redisplay)
  849. (bs--goto-current-buffer)
  850. (bs-message-without-log "%s" (bs--current-config-message)))
  851. (defun bs-toggle-readonly ()
  852. "Toggle read-only status for buffer on current line.
  853. Uses function `toggle-read-only'."
  854. (interactive)
  855. (with-current-buffer (bs--current-buffer)
  856. (toggle-read-only))
  857. (bs--update-current-line))
  858. (defun bs-clear-modified ()
  859. "Set modified flag for buffer on current line to nil."
  860. (interactive)
  861. (with-current-buffer (bs--current-buffer)
  862. (set-buffer-modified-p nil))
  863. (bs--update-current-line))
  864. (defun bs--nth-wrapper (count fun &rest args)
  865. "Call COUNT times function FUN with arguments ARGS."
  866. (dotimes (_i (or count 1))
  867. (apply fun args)))
  868. (defun bs-up (arg)
  869. "Move cursor vertically up ARG lines in Buffer Selection Menu."
  870. (interactive "p")
  871. (if (and arg (numberp arg) (< arg 0))
  872. (bs--nth-wrapper (- arg) 'bs--down)
  873. (bs--nth-wrapper arg 'bs--up)))
  874. (defun bs--up ()
  875. "Move cursor vertically up one line.
  876. If on top of buffer list go to last line."
  877. (if (> (count-lines 1 (point)) bs-header-lines-length)
  878. (forward-line -1)
  879. (goto-char (point-max))
  880. (beginning-of-line)
  881. (recenter -1)))
  882. (defun bs-down (arg)
  883. "Move cursor vertically down ARG lines in Buffer Selection Menu."
  884. (interactive "p")
  885. (if (and arg (numberp arg) (< arg 0))
  886. (bs--nth-wrapper (- arg) 'bs--up)
  887. (bs--nth-wrapper arg 'bs--down)))
  888. (defun bs--down ()
  889. "Move cursor vertically down one line.
  890. If at end of buffer list go to first line."
  891. (if (eq (line-end-position) (point-max))
  892. (progn
  893. (goto-char (point-min))
  894. (forward-line bs-header-lines-length))
  895. (forward-line 1)))
  896. (defun bs-visits-non-file (buffer)
  897. "Return whether BUFFER visits no file.
  898. A value of t means BUFFER belongs to no file.
  899. A value of nil means BUFFER belongs to a file."
  900. (not (buffer-file-name buffer)))
  901. (defun bs-sort-buffer-interns-are-last (_b1 b2)
  902. "Function for sorting internal buffers at the end of all buffers."
  903. (string-match-p "^\\*" (buffer-name b2)))
  904. ;; ----------------------------------------------------------------------
  905. ;; Configurations:
  906. ;; ----------------------------------------------------------------------
  907. (defun bs-config-clear ()
  908. "Reset all variables which specify a configuration.
  909. These variables are `bs-dont-show-regexp', `bs-must-show-regexp',
  910. `bs-dont-show-function', `bs-must-show-function' and
  911. `bs-buffer-sort-function'."
  912. (setq bs-dont-show-regexp nil
  913. bs-must-show-regexp nil
  914. bs-dont-show-function nil
  915. bs-must-show-function nil
  916. bs-buffer-sort-function nil))
  917. (defun bs-config--only-files ()
  918. "Define a configuration for showing only buffers visiting a file."
  919. (bs-config-clear)
  920. (setq ;; I want to see *-buffers at the end
  921. bs-buffer-sort-function 'bs-sort-buffer-interns-are-last
  922. ;; Don't show files who don't belong to a file
  923. bs-dont-show-function 'bs-visits-non-file))
  924. (defun bs-config--files-and-scratch ()
  925. "Define a configuration for showing buffer *scratch* and file buffers."
  926. (bs-config-clear)
  927. (setq ;; I want to see *-buffers at the end
  928. bs-buffer-sort-function 'bs-sort-buffer-interns-are-last
  929. ;; Don't show files who don't belong to a file
  930. bs-dont-show-function 'bs-visits-non-file
  931. ;; Show *scratch* buffer.
  932. bs-must-show-regexp "^\\*scratch\\*$"))
  933. (defun bs-config--all ()
  934. "Define a configuration for showing all buffers.
  935. Reset all according variables by `bs-config-clear'."
  936. (bs-config-clear))
  937. (defun bs-config--all-intern-last ()
  938. "Define a configuration for showing all buffers.
  939. Internal buffers appear at end of all buffers."
  940. (bs-config-clear)
  941. ;; I want to see *-buffers at the end
  942. (setq bs-buffer-sort-function 'bs-sort-buffer-interns-are-last))
  943. (defun bs-set-configuration (name)
  944. "Set configuration to the one saved under string NAME in `bs-configurations'.
  945. When called interactively ask user for a configuration and apply selected
  946. configuration."
  947. (interactive (list (completing-read "Use configuration: "
  948. bs-configurations
  949. nil
  950. t)))
  951. (let ((list (assoc name bs-configurations)))
  952. (if list
  953. (if (listp list)
  954. (setq bs-current-configuration name
  955. bs-must-show-regexp (nth 1 list)
  956. bs-must-show-function (nth 2 list)
  957. bs-dont-show-regexp (nth 3 list)
  958. bs-dont-show-function (nth 4 list)
  959. bs-buffer-sort-function (nth 5 list))
  960. ;; for backward compatibility
  961. (funcall (cdr list)))
  962. ;; else
  963. (ding)
  964. (bs-message-without-log "No bs-configuration named %S." name))))
  965. (defun bs-help ()
  966. "Help for `bs-show'."
  967. (interactive)
  968. (describe-function 'bs-mode))
  969. (defun bs-next-config-aux (start-name list)
  970. "Get the next assoc after START-NAME in list LIST.
  971. Will return the first if START-NAME is at end."
  972. (let ((assocs list)
  973. (length (length list))
  974. pos)
  975. (while (and assocs (not pos))
  976. (when (string= (car (car assocs)) start-name)
  977. (setq pos (- length (length assocs))))
  978. (setq assocs (cdr assocs)))
  979. (setq pos (1+ pos))
  980. (if (eq pos length)
  981. (car list)
  982. (nth pos list))))
  983. (defun bs-next-config (name)
  984. "Return next configuration with respect to configuration with name NAME."
  985. (bs-next-config-aux name bs-configurations))
  986. (defun bs-select-next-configuration (&optional start-name)
  987. "Apply next configuration START-NAME and refresh buffer list.
  988. If START-NAME is nil the current configuration `bs-current-configuration'
  989. will be used."
  990. (interactive)
  991. (let ((config (bs-next-config (or start-name bs-current-configuration))))
  992. (bs-set-configuration (car config))
  993. (setq bs-default-configuration bs-current-configuration)
  994. (bs--redisplay t)
  995. (bs--set-window-height)
  996. (bs-message-without-log "Selected configuration: %s" (car config))))
  997. (defun bs-show-in-buffer (list)
  998. "Display buffer list LIST in buffer *buffer-selection*.
  999. Select buffer *buffer-selection* and display buffers according to current
  1000. configuration `bs-current-configuration'. Set window height, fontify buffer
  1001. and move point to current buffer."
  1002. (setq bs-current-list list)
  1003. (switch-to-buffer (get-buffer-create "*buffer-selection*"))
  1004. (bs-mode)
  1005. (let* ((inhibit-read-only t)
  1006. (map-fun (lambda (entry)
  1007. (length (buffer-name entry))))
  1008. (max-length-of-names (apply 'max
  1009. (cons 0 (mapcar map-fun list))))
  1010. (name-entry-length (min bs-maximal-buffer-name-column
  1011. (max bs-minimal-buffer-name-column
  1012. max-length-of-names))))
  1013. (erase-buffer)
  1014. (setq bs--name-entry-length name-entry-length)
  1015. (bs--show-header)
  1016. (dolist (buffer list)
  1017. (bs--insert-one-entry buffer)
  1018. (insert "\n"))
  1019. (delete-char -1)
  1020. (bs--set-window-height)
  1021. (bs--goto-current-buffer)
  1022. (font-lock-fontify-buffer)
  1023. (bs-apply-sort-faces)
  1024. (set-buffer-modified-p nil)))
  1025. (defun bs-next-buffer (&optional buffer-list sorting-p)
  1026. "Return next buffer and buffer list for buffer cycling in BUFFER-LIST.
  1027. Ignore sorting when SORTING-P is nil.
  1028. If BUFFER-LIST is nil the result of `bs-buffer-list' will be used as
  1029. buffer list. The result is a cons of normally the second element of
  1030. BUFFER-LIST and the buffer list used for buffer cycling."
  1031. (let* ((bs--current-sort-function (if sorting-p
  1032. bs--current-sort-function))
  1033. (bs-buffer-list (or buffer-list (bs-buffer-list))))
  1034. (cons (or (car (cdr bs-buffer-list))
  1035. (car bs-buffer-list)
  1036. (current-buffer))
  1037. bs-buffer-list)))
  1038. (defun bs-previous-buffer (&optional buffer-list sorting-p)
  1039. "Return previous buffer and buffer list for buffer cycling in BUFFER-LIST.
  1040. Ignore sorting when SORTING-P is nil.
  1041. If BUFFER-LIST is nil the result of `bs-buffer-list' will be used as
  1042. buffer list. The result is a cons of last element of BUFFER-LIST and the
  1043. buffer list used for buffer cycling."
  1044. (let* ((bs--current-sort-function (if sorting-p
  1045. bs--current-sort-function))
  1046. (bs-buffer-list (or buffer-list (bs-buffer-list))))
  1047. (cons (or (car (last bs-buffer-list))
  1048. (current-buffer))
  1049. bs-buffer-list)))
  1050. (defun bs-message-without-log (&rest args)
  1051. "Like `message' but don't log it on the message log.
  1052. All arguments ARGS are transferred to function `message'."
  1053. (let ((message-log-max nil))
  1054. (apply 'message args)))
  1055. (defvar bs--cycle-list nil
  1056. "Current buffer list used for cycling.")
  1057. ;;;###autoload
  1058. (defun bs-cycle-next ()
  1059. "Select next buffer defined by buffer cycling.
  1060. The buffers taking part in buffer cycling are defined
  1061. by buffer configuration `bs-cycle-configuration-name'."
  1062. (interactive)
  1063. (let ((bs--buffer-coming-from (current-buffer))
  1064. (bs-dont-show-regexp bs-dont-show-regexp)
  1065. (bs-must-show-regexp bs-must-show-regexp)
  1066. (bs-dont-show-function bs-dont-show-function)
  1067. (bs-must-show-function bs-must-show-function)
  1068. (bs--show-all nil))
  1069. (bs-set-configuration (or bs-cycle-configuration-name bs-default-configuration))
  1070. (let ((bs-buffer-sort-function nil)
  1071. (bs--current-sort-function nil))
  1072. (let* ((tupel (bs-next-buffer (if (or (eq last-command
  1073. 'bs-cycle-next)
  1074. (eq last-command
  1075. 'bs-cycle-previous))
  1076. bs--cycle-list)))
  1077. (next (car tupel))
  1078. (cycle-list (cdr tupel)))
  1079. ;; We don't want the frame iconified if the only window in the frame
  1080. ;; happens to be dedicated.
  1081. (bury-buffer (current-buffer))
  1082. (switch-to-buffer next nil t)
  1083. (setq bs--cycle-list (append (cdr cycle-list)
  1084. (list (car cycle-list))))
  1085. (bs-message-without-log "Next buffers: %s"
  1086. (or (cdr bs--cycle-list)
  1087. "this buffer"))))))
  1088. ;;;###autoload
  1089. (defun bs-cycle-previous ()
  1090. "Select previous buffer defined by buffer cycling.
  1091. The buffers taking part in buffer cycling are defined
  1092. by buffer configuration `bs-cycle-configuration-name'."
  1093. (interactive)
  1094. (let ((bs--buffer-coming-from (current-buffer))
  1095. (bs-dont-show-regexp bs-dont-show-regexp)
  1096. (bs-must-show-regexp bs-must-show-regexp)
  1097. (bs-dont-show-function bs-dont-show-function)
  1098. (bs-must-show-function bs-must-show-function)
  1099. (bs--show-all nil))
  1100. (bs-set-configuration (or bs-cycle-configuration-name bs-default-configuration))
  1101. (let ((bs-buffer-sort-function nil)
  1102. (bs--current-sort-function nil))
  1103. (let* ((tupel (bs-previous-buffer (if (or (eq last-command
  1104. 'bs-cycle-next)
  1105. (eq last-command
  1106. 'bs-cycle-previous))
  1107. bs--cycle-list)))
  1108. (prev-buffer (car tupel))
  1109. (cycle-list (cdr tupel)))
  1110. (switch-to-buffer prev-buffer nil t)
  1111. (setq bs--cycle-list (append (last cycle-list)
  1112. (reverse (cdr (reverse cycle-list)))))
  1113. (bs-message-without-log "Previous buffers: %s"
  1114. (or (reverse (cdr bs--cycle-list))
  1115. "this buffer"))))))
  1116. (defun bs--get-value (fun &optional args)
  1117. "Apply function FUN with arguments ARGS.
  1118. Return result of evaluation. Will return FUN if FUN is a number
  1119. or a string."
  1120. (cond ((numberp fun)
  1121. fun)
  1122. ((stringp fun)
  1123. fun)
  1124. (t (apply fun args))))
  1125. (defun bs--get-marked-string (start-buffer _all-buffers)
  1126. "Return a string which describes whether current buffer is marked.
  1127. START-BUFFER is the buffer where we started buffer selection.
  1128. ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu.
  1129. The result string is one of `bs-string-current', `bs-string-current-marked',
  1130. `bs-string-marked', `bs-string-show-normally', `bs-string-show-never', or
  1131. `bs-string-show-always'."
  1132. (cond ;; current buffer is the buffer we started buffer selection.
  1133. ((eq (current-buffer) start-buffer)
  1134. (if (memq (current-buffer) bs--marked-buffers)
  1135. bs-string-current-marked ; buffer is marked
  1136. bs-string-current))
  1137. ;; current buffer is marked
  1138. ((memq (current-buffer) bs--marked-buffers)
  1139. bs-string-marked)
  1140. ;; current buffer hasn't a special mark.
  1141. ((null bs-buffer-show-mark)
  1142. bs-string-show-normally)
  1143. ;; current buffer has a mark not to show itself.
  1144. ((eq bs-buffer-show-mark 'never)
  1145. bs-string-show-never)
  1146. ;; otherwise current buffer is marked to show always.
  1147. (t
  1148. bs-string-show-always)))
  1149. (defun bs--get-modified-string (_start-buffer _all-buffers)
  1150. "Return a string which describes whether current buffer is modified.
  1151. START-BUFFER is the buffer where we started buffer selection.
  1152. ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
  1153. (if (buffer-modified-p) "*" " "))
  1154. (defun bs--get-readonly-string (_start-buffer _all-buffers)
  1155. "Return a string which describes whether current buffer is read only.
  1156. START-BUFFER is the buffer where we started buffer selection.
  1157. ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
  1158. (if buffer-read-only "%" " "))
  1159. (defun bs--get-size-string (_start-buffer _all-buffers)
  1160. "Return a string which describes the size of current buffer.
  1161. START-BUFFER is the buffer where we started buffer selection.
  1162. ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
  1163. (int-to-string (buffer-size)))
  1164. (defun bs--get-name (_start-buffer _all-buffers)
  1165. "Return name of current buffer for Buffer Selection Menu.
  1166. The name of current buffer gets additional text properties
  1167. for mouse highlighting.
  1168. START-BUFFER is the buffer where we started buffer selection.
  1169. ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
  1170. (propertize (buffer-name)
  1171. 'help-echo "mouse-2: select this buffer, mouse-3: select in other frame"
  1172. 'mouse-face 'highlight))
  1173. (defun bs--get-mode-name (start-buffer _all-buffers)
  1174. "Return the name of mode of current buffer for Buffer Selection Menu.
  1175. START-BUFFER is the buffer where we started buffer selection.
  1176. ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
  1177. (format-mode-line mode-name nil nil start-buffer))
  1178. (defun bs--get-file-name (_start-buffer _all-buffers)
  1179. "Return string for column 'File' in Buffer Selection Menu.
  1180. This is the variable `buffer-file-name' of current buffer.
  1181. If not visiting a file, `list-buffers-directory' is returned instead.
  1182. START-BUFFER is the buffer where we started buffer selection.
  1183. ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
  1184. (propertize (or buffer-file-name
  1185. (bound-and-true-p list-buffers-directory)
  1186. "")
  1187. 'mouse-face 'highlight
  1188. 'help-echo "mouse-2: select this buffer, mouse-3: select in other frame"))
  1189. (defun bs--insert-one-entry (buffer)
  1190. "Generate one entry for buffer BUFFER in Buffer Selection Menu.
  1191. It goes over all columns described in `bs-attributes-list'
  1192. and evaluates corresponding string. Inserts string in current buffer;
  1193. normally *buffer-selection*."
  1194. (let ((string "")
  1195. (to-much 0)
  1196. (apply-args (append (list bs--buffer-coming-from bs-current-list))))
  1197. (with-current-buffer buffer
  1198. (dolist (column bs-attributes-list)
  1199. (let* ((min (bs--get-value (nth 1 column)))
  1200. (new-string (bs--format-aux (bs--get-value (nth 4 column) ; fun
  1201. apply-args)
  1202. (nth 3 column) ; align
  1203. (- min to-much)))
  1204. (len (length new-string)))
  1205. (setq string (concat string new-string))
  1206. (when (> len min)
  1207. (setq to-much (- len min))))))
  1208. (insert string)))
  1209. (defun bs--format-aux (string align len)
  1210. "Pad STRING to length LEN with alignment ALIGN.
  1211. ALIGN is one of the symbols `left', `middle', or `right'."
  1212. (let* ((width (length string))
  1213. (len (max len width)))
  1214. (format (format "%%%s%ds" (if (eq align 'right) "" "-") len)
  1215. (if (eq align 'middle)
  1216. (concat (make-string (/ (- len width) 2) ?\s) string)
  1217. string))))
  1218. (defun bs--show-header ()
  1219. "Insert header for Buffer Selection Menu in current buffer."
  1220. (insert (bs--create-header-line #'identity)
  1221. "\n"
  1222. (bs--create-header-line (lambda (title)
  1223. (make-string (length title) ?-)))
  1224. "\n"))
  1225. (defun bs--get-name-length ()
  1226. "Return value of `bs--name-entry-length'."
  1227. bs--name-entry-length)
  1228. (defun bs--create-header-line (col)
  1229. "Generate a line for the header.
  1230. COL is called for each column in `bs-attributes-list' as a
  1231. function of one argument, the string heading for the column."
  1232. (mapconcat (lambda (column)
  1233. (bs--format-aux (funcall col (bs--get-value (car column)))
  1234. (nth 3 column) ; align
  1235. (bs--get-value (nth 1 column))))
  1236. bs-attributes-list
  1237. ""))
  1238. (defun bs--show-with-configuration (name &optional arg)
  1239. "Display buffer list of configuration with name NAME.
  1240. Set configuration NAME and determine window for Buffer Selection Menu.
  1241. Unless current buffer is buffer *buffer-selection* we have to save
  1242. the buffer we started Buffer Selection Menu and the current window
  1243. configuration to restore buffer and window configuration after a
  1244. selection. If there is already a window displaying *buffer-selection*
  1245. select this window for Buffer Selection Menu. Otherwise open a new
  1246. window.
  1247. The optional argument ARG is the prefix argument when calling a function
  1248. for buffer selection."
  1249. (bs-set-configuration name)
  1250. (let ((bs--show-all (or bs--show-all arg)))
  1251. (unless (string= "*buffer-selection*" (buffer-name))
  1252. ;; Only when not in buffer *buffer-selection*
  1253. ;; we have to set the buffer we started the command
  1254. (setq bs--buffer-coming-from (current-buffer)))
  1255. (let ((liste (bs-buffer-list))
  1256. (active-window (get-window-with-predicate
  1257. (lambda (w)
  1258. (string= (buffer-name (window-buffer w))
  1259. "*buffer-selection*"))
  1260. nil (selected-frame))))
  1261. (if active-window
  1262. (select-window active-window)
  1263. (bs--restore-window-config)
  1264. (setq bs--window-config-coming-from (current-window-configuration))
  1265. (when (> (window-height (selected-window)) 7)
  1266. ;; Errors would mess with the window configuration (bug#10882).
  1267. (ignore-errors (select-window (split-window-below)))))
  1268. (bs-show-in-buffer liste)
  1269. (bs-message-without-log "%s" (bs--current-config-message)))))
  1270. (defun bs--configuration-name-for-prefix-arg (prefix)
  1271. "Convert prefix argument PREFIX to a name of a buffer configuration.
  1272. If PREFIX is nil return `bs-default-configuration'.
  1273. If PREFIX is an integer return PREFIX element of `bs-configurations'.
  1274. Otherwise return `bs-alternative-configuration'."
  1275. (cond ;; usually activation
  1276. ((null prefix)
  1277. bs-default-configuration)
  1278. ;; call with integer as prefix argument
  1279. ((integerp prefix)
  1280. (if (and (< 0 prefix) (<= prefix (length bs-configurations)))
  1281. (car (nth (1- prefix) bs-configurations))
  1282. bs-default-configuration))
  1283. ;; call by prefix argument C-u
  1284. (t bs-alternative-configuration)))
  1285. ;; ----------------------------------------------------------------------
  1286. ;; Main function bs-customize and bs-show
  1287. ;; ----------------------------------------------------------------------
  1288. ;;;###autoload
  1289. (defun bs-customize ()
  1290. "Customization of group bs for Buffer Selection Menu."
  1291. (interactive)
  1292. (customize-group "bs"))
  1293. ;;;###autoload
  1294. (defun bs-show (arg)
  1295. "Make a menu of buffers so you can manipulate buffers or the buffer list.
  1296. \\<bs-mode-map>
  1297. There are many key commands similar to `Buffer-menu-mode' for
  1298. manipulating the buffer list and the buffers themselves.
  1299. User can move with [up] or [down], select a buffer
  1300. by \\[bs-select] or [SPC]\n
  1301. Type \\[bs-kill] to leave Buffer Selection Menu without a selection.
  1302. Type \\[bs-help] after invocation to get help on commands available.
  1303. With prefix argument ARG show a different buffer list. Function
  1304. `bs--configuration-name-for-prefix-arg' determine accordingly
  1305. name of buffer configuration."
  1306. (interactive "P")
  1307. (setq bs--marked-buffers nil)
  1308. (bs--show-with-configuration (bs--configuration-name-for-prefix-arg arg)))
  1309. ;; ----------------------------------------------------------------------
  1310. ;; Cleanup
  1311. ;; ----------------------------------------------------------------------
  1312. (defun bs-unload-function ()
  1313. "Unload the Buffer Selection library."
  1314. (let ((bs-buf (get-buffer "*buffer-selection*")))
  1315. (when bs-buf
  1316. (with-current-buffer bs-buf
  1317. (when (eq major-mode 'bs-mode)
  1318. (bs-kill)
  1319. (kill-buffer bs-buf)))))
  1320. ;; continue standard unloading
  1321. nil)
  1322. ;; Now provide feature bs
  1323. (provide 'bs)
  1324. ;;; bs.el ends here