iswitchb.el 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. ;;; iswitchb.el --- switch between buffers using substrings
  2. ;; Copyright (C) 1996-1997, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Stephen Eglen <stephen@gnu.org>
  4. ;; Maintainer: Stephen Eglen <stephen@gnu.org>
  5. ;; Keywords: completion 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. ;; Installation:
  19. ;; To get the functions in this package bound to keys, use
  20. ;; M-x iswitchb-mode or customize the option `iswitchb-mode'.
  21. ;; Alternatively, add the following line to your .emacs:
  22. ;; (iswitchb-mode 1)
  23. ;; As you type in a substring, the list of buffers currently matching
  24. ;; the substring is displayed as you type. The list is ordered so
  25. ;; that the most recent buffers visited come at the start of the list.
  26. ;; The buffer at the start of the list will be the one visited when
  27. ;; you press return. By typing more of the substring, the list is
  28. ;; narrowed down so that gradually the buffer you want will be at the
  29. ;; top of the list. Alternatively, you can use C-s and C-r to rotate
  30. ;; buffer names in the list until the one you want is at the top of
  31. ;; the list. Completion is also available so that you can see what is
  32. ;; common to all of the matching buffers as you type.
  33. ;; This code is similar to a couple of other packages. Michael R Cook
  34. ;; <cook@sightpath.com> wrote a similar buffer switching package, but
  35. ;; does exact matching rather than substring matching on buffer names.
  36. ;; I also modified a couple of functions from icomplete.el to provide
  37. ;; the completion feedback in the minibuffer.
  38. ;;; Example
  39. ;; If I have two buffers called "123456" and "123", with "123456" the
  40. ;; most recent, when I use iswitchb, I first of all get presented with
  41. ;; the list of all the buffers
  42. ;;
  43. ;; iswitch {123456,123}
  44. ;;
  45. ;; If I then press 2:
  46. ;; iswitch 2[3]{123456,123}
  47. ;;
  48. ;; The list in {} are the matching buffers, most recent first (buffers
  49. ;; visible in the current frame are put at the end of the list by
  50. ;; default). At any time I can select the item at the head of the
  51. ;; list by pressing RET. I can also put the first element at the end
  52. ;; of the list by pressing C-s, or put the last element at the head of
  53. ;; the list by pressing C-r. The item in [] indicates what can be
  54. ;; added to my input by pressing TAB. In this case, I will get "3"
  55. ;; added to my input. So, press TAB:
  56. ;; iswitch 23{123456,123}
  57. ;;
  58. ;; At this point, I still have two matching buffers.
  59. ;; If I want the first buffer in the list, I simply press RET. If I
  60. ;; wanted the second in the list, I could press C-s to move it to the
  61. ;; top of the list and then RET to select it.
  62. ;;
  63. ;; However, if I type 4, I only have one match left:
  64. ;; iswitch 234[123456] [Matched]
  65. ;;
  66. ;; Since there is only one matching buffer left, it is given in [] and we
  67. ;; see the text [Matched] afterwards. I can now press TAB or RET to go
  68. ;; to that buffer.
  69. ;;
  70. ;; If however, I now type "a":
  71. ;; iswitch 234a [No match]
  72. ;; There are no matching buffers. If I press RET or TAB, I can be
  73. ;; prompted to create a new buffer called "234a".
  74. ;;
  75. ;; Of course, where this function comes in really useful is when you
  76. ;; can specify the buffer using only a few keystrokes. In the above
  77. ;; example, the quickest way to get to the "123456" buffer would be
  78. ;; just to type 4 and then RET (assuming there isn't any newer buffer
  79. ;; with 4 in its name).
  80. ;; To see a full list of all matching buffers in a separate buffer,
  81. ;; hit ? or press TAB when there are no further completions to the
  82. ;; substring. Repeated TAB presses will scroll you through this
  83. ;; separate buffer.
  84. ;; The buffer at the head of the list can be killed by pressing C-k.
  85. ;; If the buffer needs saving, you will be queried before the buffer
  86. ;; is killed.
  87. ;; If you find that the file you are after is not in a buffer, you can
  88. ;; press C-x C-f to immediately drop into find-file.
  89. ;; See the doc string of iswitchb for full keybindings and features.
  90. ;; (describe-function 'iswitchb)
  91. ;; Case matching: The case of strings when matching can be ignored or
  92. ;; used depending on the value of iswitchb-case (default is the same
  93. ;; as case-fold-search, normally t). Imagine you have the following
  94. ;; buffers:
  95. ;;
  96. ;; INBOX *info* *scratch*
  97. ;;
  98. ;; Then these will be the matching buffers, depending on how you type
  99. ;; the two letters `in' and the value of iswitchb-case:
  100. ;;
  101. ;; iswitchb-case user input | matching buffers
  102. ;; ----------------------------------------------
  103. ;; nil in | *info*
  104. ;; t in | INBOX, *info*
  105. ;; t IN | INBOX
  106. ;; t In | [No match]
  107. ;;; Customization
  108. ;; See the User Variables section below for easy ways to change the
  109. ;; functionality of the program. These are accessible using the
  110. ;; custom package.
  111. ;; To modify the keybindings, use something like:
  112. ;;
  113. ;;(add-hook 'iswitchb-mode-hook 'iswitchb-my-keys)
  114. ;;(defun iswitchb-my-keys ()
  115. ;; "Add my keybindings for iswitchb."
  116. ;; (define-key iswitchb-mode-map " " 'iswitchb-next-match))
  117. ;;
  118. ;; Seeing all the matching buffers
  119. ;;
  120. ;; If you have many matching buffers, they may not all fit onto one
  121. ;; line of the minibuffer. In Emacs 21, the variable
  122. ;; `resize-mini-windows' controls how many lines of the minibuffer can
  123. ;; be seen. For older versions of emacs, you can use
  124. ;; `resize-minibuffer-mode'. You can also limit iswitchb so that it
  125. ;; only shows a certain number of lines -- see the documentation for
  126. ;; `iswitchb-minibuffer-setup-hook'.
  127. ;; Changing the list of buffers
  128. ;; By default, the list of current buffers is most recent first,
  129. ;; oldest last, with the exception that the buffers visible in the
  130. ;; current frame are put at the end of the list. A hook exists to
  131. ;; allow other functions to order the list. For example, if you add:
  132. ;;
  133. ;; (add-hook 'iswitchb-make-buflist-hook 'iswitchb-summaries-to-end)
  134. ;;
  135. ;; then all buffers matching "Summary" are moved to the end of the
  136. ;; list. (I find this handy for keeping the INBOX Summary and so on
  137. ;; out of the way.) It also moves buffers matching "output\*$" to the
  138. ;; end of the list (these are created by AUCTeX when compiling.)
  139. ;; Other functions could be made available which alter the list of
  140. ;; matching buffers (either deleting or rearranging elements.)
  141. ;; Font-Lock
  142. ;; font-lock is used to highlight the first matching buffer. To
  143. ;; switch this off, set (setq iswitchb-use-faces nil). Coloring of
  144. ;; the matching buffer name was suggested by Carsten Dominik
  145. ;; (dominik@strw.leidenuniv.nl)
  146. ;; Replacement for read-buffer
  147. ;; iswitchb-read-buffer has been written to be a drop in replacement
  148. ;; for the normal buffer selection routine `read-buffer'. To use
  149. ;; iswitch for all buffer selections in Emacs, add:
  150. ;; (setq read-buffer-function 'iswitchb-read-buffer)
  151. ;; (This variable was introduced in Emacs 20.3.)
  152. ;; XEmacs users can get the same behavior by doing:
  153. ;; (defalias 'read-buffer 'iswitchb-read-buffer)
  154. ;; since `read-buffer' is defined in lisp.
  155. ;; Using iswitchb for other completion tasks.
  156. ;; Kin Cho (kin@neoscale.com) sent the following suggestion to use
  157. ;; iswitchb for other completion tasks.
  158. ;;
  159. ;; (defun my-icompleting-read (prompt choices)
  160. ;; "Use iswitch as a completing-read replacement to choose from
  161. ;; choices. PROMPT is a string to prompt with. CHOICES is a list of
  162. ;; strings to choose from."
  163. ;; (let ((iswitchb-make-buflist-hook
  164. ;; (lambda ()
  165. ;; (setq iswitchb-temp-buflist choices))))
  166. ;; (iswitchb-read-buffer prompt)))
  167. ;;
  168. ;; example:
  169. ;; (my-icompleting-read "Which fruit? " '
  170. ;; ("apple" "pineapple" "pear" "bananas" "oranges") )
  171. ;; Kin Cho also suggested the following defun. Once you have a subset of
  172. ;; matching buffers matching your current prompt, you can then press
  173. ;; e.g. C-o to restrict matching to those buffers and clearing the prompt:
  174. ;; (defun iswitchb-exclude-nonmatching()
  175. ;; "Make iswitchb work on only the currently matching names."
  176. ;; (interactive)
  177. ;; (setq iswitchb-buflist iswitchb-matches)
  178. ;; (setq iswitchb-rescan t)
  179. ;; (delete-minibuffer-contents))
  180. ;;
  181. ;; (add-hook 'iswitchb-define-mode-map-hook
  182. ;; (lambda () (define-key
  183. ;; iswitchb-mode-map "\C-o"
  184. ;; 'iswitchb-exclude-nonmatching)))
  185. ;; Other lisp packages extend iswitchb behavior to other tasks. See
  186. ;; ido.el (by Kim Storm) and mcomplete.el (Yuji Minejima).
  187. ;; Window managers: Switching frames/focus follows mouse; Sawfish.
  188. ;; If you switch to a buffer that is visible in another frame,
  189. ;; iswitchb can switch focus to that frame. If your window manager
  190. ;; uses "click to focus" policy for window selection, you should also
  191. ;; set focus-follows-mouse to nil.
  192. ;; iswitch functionality has also been implemented for switching
  193. ;; between windows in the Sawfish window manager.
  194. ;; Regexp matching
  195. ;; There is provision for regexp matching within iswitchb, enabled
  196. ;; through `iswitchb-regexp'. This allows you to type `c$' for
  197. ;; example and see all buffer names ending in `c'. No completion
  198. ;; mechanism is currently offered when regexp searching.
  199. ;;; TODO
  200. ;;; Acknowledgements
  201. ;; Thanks to Jari Aalto <jari.aalto@poboxes.com> for help with the
  202. ;; first version of this package, iswitch-buffer. Thanks also to many
  203. ;; others for testing earlier versions.
  204. ;;; Code:
  205. (require 'font-lock)
  206. ;;; User Variables
  207. ;;
  208. ;; These are some things you might want to change.
  209. (defgroup iswitchb nil
  210. "Switch between buffers using substrings."
  211. :group 'convenience
  212. :group 'completion
  213. :link '(emacs-commentary-link :tag "Commentary" "iswitchb.el")
  214. :link '(url-link "http://www.anc.ed.ac.uk/~stephen/emacs/")
  215. :link '(emacs-library-link :tag "Lisp File" "iswitchb.el"))
  216. (defcustom iswitchb-case case-fold-search
  217. "Non-nil if searching of buffer names should ignore case.
  218. If this is non-nil but the user input has any upper case letters, matching
  219. is temporarily case sensitive."
  220. :type 'boolean
  221. :group 'iswitchb)
  222. (defcustom iswitchb-buffer-ignore
  223. '("^ ")
  224. "List of regexps or functions matching buffer names to ignore.
  225. For example, traditional behavior is not to list buffers whose names begin
  226. with a space, for which the regexp is `^ '. See the source file for
  227. example functions that filter buffer names."
  228. :type '(repeat (choice regexp function))
  229. :group 'iswitchb)
  230. (put 'iswitchb-buffer-ignore 'risky-local-variable t)
  231. (defcustom iswitchb-max-to-show nil
  232. "If non-nil, limit the number of names shown in the minibuffer.
  233. If this value is N, and N is greater than the number of matching
  234. buffers, the first N/2 and the last N/2 matching buffers are
  235. shown. This can greatly speed up iswitchb if you have a
  236. multitude of buffers open."
  237. :type '(choice (const :tag "Show all" nil) integer)
  238. :group 'iswitchb)
  239. (defcustom iswitchb-use-virtual-buffers nil
  240. "If non-nil, refer to past buffers when none match.
  241. This feature relies upon the `recentf' package, which will be
  242. enabled if this variable is configured to a non-nil value."
  243. :type 'boolean
  244. :require 'recentf
  245. :set (function
  246. (lambda (sym value)
  247. (if value (recentf-mode 1))
  248. (set sym value)))
  249. :group 'iswitchb)
  250. (defvar iswitchb-virtual-buffers nil)
  251. (defcustom iswitchb-cannot-complete-hook 'iswitchb-completion-help
  252. "Hook run when `iswitchb-complete' can't complete any more.
  253. The most useful values are `iswitchb-completion-help', which pops up a
  254. window with completion alternatives, or `iswitchb-next-match' or
  255. `iswitchb-prev-match', which cycle the buffer list."
  256. :type 'hook
  257. :group 'iswitchb)
  258. ;; Examples for setting the value of iswitchb-buffer-ignore
  259. ;;(defun iswitchb-ignore-c-mode (name)
  260. ;; "Ignore all c mode buffers -- example function for iswitchb."
  261. ;; (with-current-buffer name
  262. ;; (derived-mode-p 'c-mode)))
  263. ;;(setq iswitchb-buffer-ignore '("^ " iswitchb-ignore-c-mode))
  264. ;;(setq iswitchb-buffer-ignore '("^ " "\\.c\\'" "\\.h\\'"))
  265. (defcustom iswitchb-default-method 'always-frame
  266. "How to switch to new buffer when using `iswitchb-buffer'.
  267. Possible values:
  268. `samewindow' Show new buffer in same window
  269. `otherwindow' Show new buffer in another window (same frame)
  270. `display' Display buffer in another window without switching to it
  271. `otherframe' Show new buffer in another frame
  272. `maybe-frame' If a buffer is visible in another frame, prompt to ask if you
  273. you want to see the buffer in the same window of the current
  274. frame or in the other frame.
  275. `always-frame' If a buffer is visible in another frame, raise that
  276. frame. Otherwise, visit the buffer in the same window."
  277. :type '(choice (const samewindow)
  278. (const otherwindow)
  279. (const display)
  280. (const otherframe)
  281. (const maybe-frame)
  282. (const always-frame))
  283. :group 'iswitchb)
  284. (defcustom iswitchb-regexp nil
  285. "Non-nil means that `iswitchb' will do regexp matching.
  286. Value can be toggled within `iswitchb' using `iswitchb-toggle-regexp'."
  287. :type 'boolean
  288. :group 'iswitchb)
  289. (defcustom iswitchb-newbuffer t
  290. "Non-nil means create new buffer if no buffer matches substring.
  291. See also `iswitchb-prompt-newbuffer'."
  292. :type 'boolean
  293. :group 'iswitchb)
  294. (defcustom iswitchb-prompt-newbuffer t
  295. "Non-nil means prompt user to confirm before creating new buffer.
  296. See also `iswitchb-newbuffer'."
  297. :type 'boolean
  298. :group 'iswitchb)
  299. (define-obsolete-variable-alias 'iswitchb-use-fonts 'iswitchb-use-faces "22.1")
  300. (defcustom iswitchb-use-faces t
  301. "Non-nil means use font-lock faces for showing first match."
  302. :type 'boolean
  303. :group 'iswitchb)
  304. (defcustom iswitchb-use-frame-buffer-list nil
  305. "Non-nil means use the currently selected frame's buffer list."
  306. :type 'boolean
  307. :group 'iswitchb)
  308. (defcustom iswitchb-make-buflist-hook nil
  309. "Hook to run when list of matching buffers is created."
  310. :type 'hook
  311. :group 'iswitchb)
  312. (defcustom iswitchb-delim ","
  313. "Delimiter to put between buffer names when displaying results."
  314. :type 'string
  315. :group 'iswitchb)
  316. (defvar iswitchb-all-frames 'visible
  317. "*Argument to pass to `walk-windows' when finding visible buffers.
  318. See documentation of `walk-windows' for useful values.")
  319. (defcustom iswitchb-minibuffer-setup-hook nil
  320. "Iswitchb-specific customization of minibuffer setup.
  321. This hook is run during minibuffer setup if `iswitchb' is active.
  322. For instance:
  323. \(add-hook 'iswitchb-minibuffer-setup-hook
  324. '\(lambda () (set (make-local-variable 'max-mini-window-height) 3)))
  325. will constrain the minibuffer to a maximum height of 3 lines when
  326. iswitchb is running."
  327. :type 'hook
  328. :group 'iswitchb)
  329. (defface iswitchb-single-match
  330. '((t
  331. (:inherit font-lock-comment-face)))
  332. "Iswitchb face for single matching buffer name."
  333. :version "22.1"
  334. :group 'iswitchb)
  335. (defface iswitchb-current-match
  336. '((t
  337. (:inherit font-lock-function-name-face)))
  338. "Iswitchb face for current matching buffer name."
  339. :version "22.1"
  340. :group 'iswitchb)
  341. (defface iswitchb-virtual-matches
  342. '((t
  343. (:inherit font-lock-builtin-face)))
  344. "Iswitchb face for matching virtual buffer names.
  345. See also `iswitchb-use-virtual-buffers'."
  346. :version "22.1"
  347. :group 'iswitchb)
  348. (defface iswitchb-invalid-regexp
  349. '((t
  350. (:inherit font-lock-warning-face)))
  351. "Iswitchb face for indicating invalid regexp. "
  352. :version "22.1"
  353. :group 'iswitchb)
  354. ;; Do we need the variable iswitchb-use-mycompletion?
  355. ;;; Internal Variables
  356. (defvar iswitchb-method nil
  357. "Stores the method for viewing the selected buffer.
  358. Its value is one of `samewindow', `otherwindow', `display', `otherframe',
  359. `maybe-frame' or `always-frame'. See `iswitchb-default-method' for
  360. details of values.")
  361. (defvar iswitchb-eoinput 1
  362. "Point where minibuffer input ends and completion info begins.
  363. Copied from `icomplete-eoinput'.")
  364. (make-variable-buffer-local 'iswitchb-eoinput)
  365. (defvar iswitchb-buflist nil
  366. "Stores the current list of buffers that will be searched through.
  367. The list is ordered, so that the most recent buffers come first,
  368. although by default, the buffers visible in the current frame are put
  369. at the end of the list. Created by `iswitchb-make-buflist'.")
  370. ;; todo -- is this necessary?
  371. (defvar iswitchb-use-mycompletion nil
  372. "Non-nil means use `iswitchb-buffer' completion feedback.
  373. Should only be set to t by iswitchb functions, so that it doesn't
  374. interfere with other minibuffer usage.")
  375. (defvar iswitchb-change-word-sub nil
  376. "Private variable used by `iswitchb-word-matching-substring'.")
  377. (defvar iswitchb-common-match-string nil
  378. "Stores the string that is common to all matching buffers.")
  379. (defvar iswitchb-rescan nil
  380. "Non-nil means we need to regenerate the list of matching buffers.")
  381. (defvar iswitchb-text nil
  382. "Stores the users string as it is typed in.")
  383. (defvar iswitchb-matches nil
  384. "List of buffers currently matching `iswitchb-text'.")
  385. (defvar iswitchb-mode-map
  386. (let ((map (make-sparse-keymap)))
  387. (set-keymap-parent map minibuffer-local-map)
  388. (define-key map "?" 'iswitchb-completion-help)
  389. (define-key map "\C-s" 'iswitchb-next-match)
  390. (define-key map "\C-r" 'iswitchb-prev-match)
  391. (define-key map "\t" 'iswitchb-complete)
  392. (define-key map "\C-j" 'iswitchb-select-buffer-text)
  393. (define-key map "\C-t" 'iswitchb-toggle-regexp)
  394. (define-key map "\C-x\C-f" 'iswitchb-find-file)
  395. (define-key map "\C-c" 'iswitchb-toggle-case)
  396. (define-key map "\C-k" 'iswitchb-kill-buffer)
  397. (define-key map "\C-m" 'iswitchb-exit-minibuffer)
  398. map)
  399. "Minibuffer keymap for `iswitchb-buffer'.")
  400. (defvar iswitchb-global-map
  401. (let ((map (make-sparse-keymap)))
  402. (dolist (b '((switch-to-buffer . iswitchb-buffer)
  403. (switch-to-buffer-other-window . iswitchb-buffer-other-window)
  404. (switch-to-buffer-other-frame . iswitchb-buffer-other-frame)
  405. (display-buffer . iswitchb-display-buffer)))
  406. (if (fboundp 'command-remapping)
  407. (define-key map (vector 'remap (car b)) (cdr b))
  408. (substitute-key-definition (car b) (cdr b) map global-map)))
  409. map)
  410. "Global keymap for `iswitchb-mode'.")
  411. (defvar iswitchb-history nil
  412. "History of buffers selected using `iswitchb-buffer'.")
  413. (defvar iswitchb-exit nil
  414. "Flag to monitor how `iswitchb-buffer' exits.
  415. If equal to `takeprompt', we use the prompt as the buffer name to be
  416. selected.")
  417. (defvar iswitchb-buffer-ignore-orig nil
  418. "Stores original value of `iswitchb-buffer-ignore'.")
  419. (defvar iswitchb-default nil
  420. "Default buffer for iswitchb.")
  421. ;; The following variables are needed to keep the byte compiler quiet.
  422. (defvar iswitchb-require-match nil
  423. "Non-nil if matching buffer must be selected.")
  424. (defvar iswitchb-temp-buflist nil
  425. "Stores a temporary version of the buffer list being created.")
  426. (defvar iswitchb-bufs-in-frame nil
  427. "List of the buffers visible in the current frame.")
  428. (defvar iswitchb-minibuf-depth nil
  429. "Value we expect to be returned by `minibuffer-depth' in the minibuffer.")
  430. (defvar iswitchb-common-match-inserted nil
  431. "Non-nil if we have just inserted a common match in the minibuffer.")
  432. (defvar iswitchb-invalid-regexp)
  433. ;;; FUNCTIONS
  434. ;;; ISWITCHB KEYMAP
  435. (defun iswitchb-define-mode-map ()
  436. "Set up the keymap for `iswitchb-buffer'."
  437. (interactive)
  438. (let (map)
  439. ;; generated every time so that it can inherit new functions.
  440. ;;(or iswitchb-mode-map
  441. (setq map (copy-keymap minibuffer-local-map))
  442. (define-key map "?" 'iswitchb-completion-help)
  443. (define-key map "\C-s" 'iswitchb-next-match)
  444. (define-key map "\C-r" 'iswitchb-prev-match)
  445. (define-key map "\t" 'iswitchb-complete)
  446. (define-key map "\C-j" 'iswitchb-select-buffer-text)
  447. (define-key map "\C-t" 'iswitchb-toggle-regexp)
  448. (define-key map "\C-x\C-f" 'iswitchb-find-file)
  449. (define-key map "\C-n" 'iswitchb-toggle-ignore)
  450. (define-key map "\C-c" 'iswitchb-toggle-case)
  451. (define-key map "\C-k" 'iswitchb-kill-buffer)
  452. (define-key map "\C-m" 'iswitchb-exit-minibuffer)
  453. (setq iswitchb-mode-map map)
  454. (run-hooks 'iswitchb-define-mode-map-hook)))
  455. (make-obsolete 'iswitchb-define-mode-map
  456. "use M-x iswitchb-mode or customize the variable `iswitchb-mode'."
  457. "21.1")
  458. ;;; MAIN FUNCTION
  459. (defun iswitchb ()
  460. "Switch to buffer matching a substring.
  461. As you type in a string, all of the buffers matching the string are
  462. displayed. When you have found the buffer you want, it can then be
  463. selected. As you type, most keys have their normal keybindings,
  464. except for the following:
  465. \\<iswitchb-mode-map>
  466. RET Select the buffer at the front of the list of matches. If the
  467. list is empty, possibly prompt to create new buffer.
  468. \\[iswitchb-select-buffer-text] Select the current prompt as the buffer.
  469. If no buffer is found, prompt for a new one.
  470. \\[iswitchb-next-match] Put the first element at the end of the list.
  471. \\[iswitchb-prev-match] Put the last element at the start of the list.
  472. \\[iswitchb-complete] Complete a common suffix to the current string that
  473. matches all buffers. If there is only one match, select that buffer.
  474. If there is no common suffix, show a list of all matching buffers
  475. in a separate window.
  476. \\[iswitchb-toggle-regexp] Toggle regexp searching.
  477. \\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names.
  478. \\[iswitchb-completion-help] Show list of matching buffers in separate window.
  479. \\[iswitchb-find-file] Exit iswitchb and drop into `find-file'.
  480. \\[iswitchb-kill-buffer] Kill buffer at head of buffer list."
  481. ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
  482. ;;`iswitchb-buffer-ignore')
  483. (let* ((prompt "iswitch ")
  484. iswitchb-invalid-regexp
  485. (buf (iswitchb-read-buffer prompt)))
  486. ;;(message "chosen text %s" iswitchb-final-text)
  487. ;; Choose the buffer name: either the text typed in, or the head
  488. ;; of the list of matches
  489. (cond ( (eq iswitchb-exit 'findfile)
  490. (call-interactively 'find-file))
  491. (iswitchb-invalid-regexp
  492. (message "Won't make invalid regexp named buffer"))
  493. (t
  494. ;; View the buffer
  495. ;;(message "go to buf %s" buf)
  496. ;; Check buf is non-nil.
  497. (if buf
  498. (if (get-buffer buf)
  499. ;; buffer exists, so view it and then exit
  500. (iswitchb-visit-buffer buf)
  501. ;; else buffer doesn't exist
  502. (iswitchb-possible-new-buffer buf)))
  503. ))))
  504. (defun iswitchb-read-buffer (prompt &optional default require-match
  505. start matches-set)
  506. "Replacement for the built-in `read-buffer'.
  507. Return the name of a buffer selected.
  508. PROMPT is the prompt to give to the user.
  509. DEFAULT if given is the default buffer to be selected, which will
  510. go to the front of the list.
  511. If REQUIRE-MATCH is non-nil, an existing buffer must be selected.
  512. If START is a string, the selection process is started with that
  513. string.
  514. If MATCHES-SET is non-nil, the buflist is not updated before
  515. the selection process begins. Used by isearchb.el."
  516. (let
  517. (
  518. buf-sel
  519. iswitchb-final-text
  520. (icomplete-mode nil) ;; prevent icomplete starting up
  521. )
  522. (iswitchb-define-mode-map)
  523. (setq iswitchb-exit nil)
  524. (setq iswitchb-default
  525. (if (bufferp default)
  526. (buffer-name default)
  527. default))
  528. (setq iswitchb-text (or start ""))
  529. (unless matches-set
  530. (setq iswitchb-rescan t)
  531. (iswitchb-make-buflist iswitchb-default)
  532. (iswitchb-set-matches))
  533. (let
  534. ((minibuffer-local-completion-map iswitchb-mode-map)
  535. ;; Record the minibuffer depth that we expect to find once
  536. ;; the minibuffer is set up and iswitchb-entryfn-p is called.
  537. (iswitchb-minibuf-depth (1+ (minibuffer-depth)))
  538. (iswitchb-require-match require-match))
  539. ;; prompt the user for the buffer name
  540. (setq iswitchb-final-text (completing-read
  541. prompt ;the prompt
  542. '(("dummy" . 1)) ;table
  543. nil ;predicate
  544. nil ;require-match [handled elsewhere]
  545. start ;initial-contents
  546. 'iswitchb-history)))
  547. (if (and (not (eq iswitchb-exit 'usefirst))
  548. (get-buffer iswitchb-final-text))
  549. ;; This happens for example if the buffer was chosen with the mouse.
  550. (setq iswitchb-matches (list iswitchb-final-text)
  551. iswitchb-virtual-buffers nil))
  552. ;; If no buffer matched, but a virtual buffer was selected, visit
  553. ;; that file now and act as though that buffer had been selected.
  554. (if (and iswitchb-virtual-buffers
  555. (not (iswitchb-existing-buffer-p)))
  556. (let ((virt (car iswitchb-virtual-buffers))
  557. (new-buf))
  558. ;; Keep the name of the buffer returned by find-file-noselect, as
  559. ;; the buffer 'virt' could be a symlink to a file of a different name.
  560. (setq new-buf (buffer-name (find-file-noselect (cdr virt))))
  561. (setq iswitchb-matches (list new-buf)
  562. iswitchb-virtual-buffers nil)))
  563. ;; Handling the require-match must be done in a better way.
  564. (if (and require-match
  565. (not (iswitchb-existing-buffer-p)))
  566. (error "Must specify valid buffer"))
  567. (if (or (eq iswitchb-exit 'takeprompt)
  568. (null iswitchb-matches))
  569. (setq buf-sel iswitchb-final-text)
  570. ;; else take head of list
  571. (setq buf-sel (car iswitchb-matches)))
  572. ;; Or possibly choose the default buffer
  573. (if (equal iswitchb-final-text "")
  574. (setq buf-sel (car iswitchb-matches)))
  575. buf-sel))
  576. (defun iswitchb-existing-buffer-p ()
  577. "Return non-nil if there is a matching buffer."
  578. (not (null iswitchb-matches)))
  579. ;;; COMPLETION CODE
  580. (defun iswitchb-set-common-completion ()
  581. "Find common completion of `iswitchb-text' in `iswitchb-matches'.
  582. The result is stored in `iswitchb-common-match-string'."
  583. (let (val)
  584. (setq iswitchb-common-match-string nil)
  585. (if (and iswitchb-matches
  586. (not iswitchb-regexp) ;; testing
  587. (stringp iswitchb-text)
  588. (> (length iswitchb-text) 0))
  589. (if (setq val (iswitchb-find-common-substring
  590. iswitchb-matches iswitchb-text))
  591. (setq iswitchb-common-match-string val)))
  592. val))
  593. (defun iswitchb-complete ()
  594. "Try and complete the current pattern amongst the buffer names."
  595. (interactive)
  596. (let (res)
  597. (cond ((not iswitchb-matches)
  598. (run-hooks 'iswitchb-cannot-complete-hook))
  599. (iswitchb-invalid-regexp
  600. ;; Do nothing
  601. )
  602. ((= 1 (length iswitchb-matches))
  603. ;; only one choice, so select it.
  604. (exit-minibuffer))
  605. (t
  606. ;; else there could be some completions
  607. (setq res iswitchb-common-match-string)
  608. (if (and (not (memq res '(t nil)))
  609. (not (equal res iswitchb-text)))
  610. ;; found something to complete, so put it in the minibuffer.
  611. (progn
  612. (setq iswitchb-rescan nil
  613. iswitchb-common-match-inserted t)
  614. (delete-region (minibuffer-prompt-end) (point))
  615. (insert res))
  616. ;; else nothing to complete
  617. (run-hooks 'iswitchb-cannot-complete-hook)
  618. )))))
  619. ;;; TOGGLE FUNCTIONS
  620. (defun iswitchb-toggle-case ()
  621. "Toggle the value of variable `iswitchb-case'."
  622. (interactive)
  623. (setq iswitchb-case (not iswitchb-case))
  624. ;; ask for list to be regenerated.
  625. (setq iswitchb-rescan t))
  626. (defun iswitchb-toggle-regexp ()
  627. "Toggle the value of `iswitchb-regexp'."
  628. (interactive)
  629. (setq iswitchb-regexp (not iswitchb-regexp))
  630. ;; ask for list to be regenerated.
  631. (setq iswitchb-rescan t))
  632. (defun iswitchb-toggle-ignore ()
  633. "Toggle ignoring buffers specified with `iswitchb-buffer-ignore'."
  634. (interactive)
  635. (if iswitchb-buffer-ignore
  636. (progn
  637. (setq iswitchb-buffer-ignore-orig iswitchb-buffer-ignore)
  638. (setq iswitchb-buffer-ignore nil))
  639. ;; else
  640. (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig))
  641. (iswitchb-make-buflist iswitchb-default)
  642. ;; ask for list to be regenerated.
  643. (setq iswitchb-rescan t))
  644. (defun iswitchb-exit-minibuffer ()
  645. "Exit minibuffer, but make sure we have a match if one is needed."
  646. (interactive)
  647. (if (or (not iswitchb-require-match)
  648. (iswitchb-existing-buffer-p))
  649. (progn
  650. (setq iswitchb-exit 'usefirst)
  651. (throw 'exit nil))))
  652. (defun iswitchb-select-buffer-text ()
  653. "Select the buffer named by the prompt.
  654. If no buffer exactly matching the prompt exists, maybe create a new one."
  655. (interactive)
  656. (setq iswitchb-exit 'takeprompt)
  657. (exit-minibuffer))
  658. (defun iswitchb-find-file ()
  659. "Drop into `find-file' from buffer switching."
  660. (interactive)
  661. (setq iswitchb-exit 'findfile)
  662. (exit-minibuffer))
  663. (defvar recentf-list)
  664. (defun iswitchb-next-match ()
  665. "Put first element of `iswitchb-matches' at the end of the list."
  666. (interactive)
  667. (let ((next (cadr iswitchb-matches)))
  668. (if (and (null next) iswitchb-virtual-buffers)
  669. (setq recentf-list
  670. (iswitchb-chop recentf-list
  671. (cdr (cadr iswitchb-virtual-buffers))))
  672. (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist next)))
  673. (setq iswitchb-rescan t)))
  674. (defun iswitchb-prev-match ()
  675. "Put last element of `iswitchb-matches' at the front of the list."
  676. (interactive)
  677. (let ((prev (car (last iswitchb-matches))))
  678. (if (and (null prev) iswitchb-virtual-buffers)
  679. (setq recentf-list
  680. (iswitchb-chop recentf-list
  681. (cdr (car (last iswitchb-virtual-buffers)))))
  682. (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist prev)))
  683. (setq iswitchb-rescan t)))
  684. (defun iswitchb-chop (list elem)
  685. "Remove all elements before ELEM and put them at the end of LIST."
  686. (let ((ret nil)
  687. (next nil)
  688. (sofar nil))
  689. (while (not ret)
  690. (setq next (car list))
  691. (if (equal next elem)
  692. (setq ret (append list (nreverse sofar)))
  693. ;; else
  694. (progn
  695. (setq list (cdr list))
  696. (setq sofar (cons next sofar)))))
  697. ret))
  698. ;;; CREATE LIST OF ALL CURRENT BUFFERS
  699. (defun iswitchb-make-buflist (default)
  700. "Set `iswitchb-buflist' to the current list of buffers.
  701. Currently visible buffers are put at the end of the list.
  702. The hook `iswitchb-make-buflist-hook' is run after the list has been
  703. created to allow the user to further modify the order of the buffer names
  704. in this list. If DEFAULT is non-nil, and corresponds to an existing buffer,
  705. it is put to the start of the list."
  706. (setq iswitchb-buflist
  707. (let* ((iswitchb-current-buffers (iswitchb-get-buffers-in-frames))
  708. (iswitchb-temp-buflist
  709. (delq nil
  710. (mapcar
  711. (lambda (x)
  712. (let ((b-name (buffer-name x)))
  713. (if (not
  714. (or
  715. (iswitchb-ignore-buffername-p b-name)
  716. (memq b-name iswitchb-current-buffers)))
  717. b-name)))
  718. (buffer-list (and iswitchb-use-frame-buffer-list
  719. (selected-frame)))))))
  720. (setq iswitchb-temp-buflist
  721. (nconc iswitchb-temp-buflist iswitchb-current-buffers))
  722. (run-hooks 'iswitchb-make-buflist-hook)
  723. ;; Should this be after the hooks, or should the hooks be the
  724. ;; final thing to be run?
  725. (if default
  726. (progn
  727. (setq iswitchb-temp-buflist
  728. (delete default iswitchb-temp-buflist))
  729. (setq iswitchb-temp-buflist
  730. (cons default iswitchb-temp-buflist))))
  731. iswitchb-temp-buflist)))
  732. (defun iswitchb-to-end (lst)
  733. "Move the elements from LST to the end of `iswitchb-temp-buflist'."
  734. (dolist (elem lst)
  735. (setq iswitchb-temp-buflist (delq elem iswitchb-temp-buflist)))
  736. (setq iswitchb-temp-buflist (nconc iswitchb-temp-buflist lst)))
  737. (defun iswitchb-get-buffers-in-frames (&optional current)
  738. "Return the list of buffers that are visible in the current frame.
  739. If optional argument CURRENT is given, restrict searching to the
  740. current frame, rather than all frames, regardless of value of
  741. `iswitchb-all-frames'."
  742. (let ((iswitchb-bufs-in-frame nil))
  743. (walk-windows 'iswitchb-get-bufname nil
  744. (if current
  745. nil
  746. iswitchb-all-frames))
  747. iswitchb-bufs-in-frame))
  748. (defun iswitchb-get-bufname (win)
  749. "Used by `iswitchb-get-buffers-in-frames' to walk through all windows."
  750. (let ((buf (buffer-name (window-buffer win))))
  751. (if (not (member buf iswitchb-bufs-in-frame))
  752. ;; Only add buf if it is not already in list.
  753. ;; This prevents same buf in two different windows being
  754. ;; put into the list twice.
  755. (setq iswitchb-bufs-in-frame
  756. (cons buf iswitchb-bufs-in-frame)))))
  757. ;;; FIND MATCHING BUFFERS
  758. (defun iswitchb-set-matches ()
  759. "Set `iswitchb-matches' to the list of buffers matching prompt."
  760. (if iswitchb-rescan
  761. (setq iswitchb-matches
  762. (let ((buflist iswitchb-buflist))
  763. (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp
  764. buflist))
  765. iswitchb-virtual-buffers nil)))
  766. (defun iswitchb-get-matched-buffers (regexp
  767. &optional string-format buffer-list)
  768. "Return buffers matching REGEXP.
  769. If STRING-FORMAT is nil, consider REGEXP as just a string.
  770. BUFFER-LIST can be list of buffers or list of strings."
  771. (let ((case-fold-search (iswitchb-case))
  772. name ret)
  773. (if (null string-format) (setq regexp (regexp-quote regexp)))
  774. (setq iswitchb-invalid-regexp nil)
  775. (condition-case error
  776. (dolist (x buffer-list (nreverse ret))
  777. (setq name (if (stringp x) x (buffer-name x)))
  778. (when (and (string-match regexp name)
  779. (not (iswitchb-ignore-buffername-p name)))
  780. (push name ret)))
  781. (invalid-regexp
  782. (setq iswitchb-invalid-regexp t)
  783. (cdr error)))))
  784. (defun iswitchb-ignore-buffername-p (bufname)
  785. "Return t if the buffer BUFNAME should be ignored."
  786. (let ((data (match-data))
  787. (re-list iswitchb-buffer-ignore)
  788. ignorep
  789. nextstr)
  790. (while re-list
  791. (setq nextstr (car re-list))
  792. (cond
  793. ((stringp nextstr)
  794. (if (string-match nextstr bufname)
  795. (progn
  796. (setq ignorep t)
  797. (setq re-list nil))))
  798. ((functionp nextstr)
  799. (if (funcall nextstr bufname)
  800. (progn
  801. (setq ignorep t)
  802. (setq re-list nil)))))
  803. (setq re-list (cdr re-list)))
  804. (set-match-data data)
  805. ;; return the result
  806. ignorep))
  807. (defun iswitchb-word-matching-substring (word)
  808. "Return part of WORD before 1st match to `iswitchb-change-word-sub'.
  809. If `iswitchb-change-word-sub' cannot be found in WORD, return nil."
  810. (let ((case-fold-search (iswitchb-case)))
  811. (let ((m (string-match iswitchb-change-word-sub word)))
  812. (if m
  813. (substring word m)
  814. ;; else no match
  815. nil))))
  816. (defun iswitchb-find-common-substring (lis subs)
  817. "Return common string following SUBS in each element of LIS."
  818. (let (res
  819. alist
  820. iswitchb-change-word-sub)
  821. (setq iswitchb-change-word-sub
  822. (if iswitchb-regexp
  823. subs
  824. (regexp-quote subs)))
  825. (setq res (mapcar 'iswitchb-word-matching-substring lis))
  826. (setq res (delq nil res)) ;; remove any nil elements (shouldn't happen)
  827. (setq alist (mapcar 'iswitchb-makealist res)) ;; could use an OBARRAY
  828. ;; try-completion returns t if there is an exact match.
  829. (let ((completion-ignore-case (iswitchb-case)))
  830. (try-completion subs alist))))
  831. (defun iswitchb-makealist (res)
  832. "Return dotted pair (RES . 1)."
  833. (cons res 1))
  834. ;; from Wayne Mesard <wmesard@esd.sgi.com>
  835. (defun iswitchb-rotate-list (lis)
  836. "Destructively remove the last element from LIS.
  837. Return the modified list with the last element prepended to it."
  838. (if (<= (length lis) 1)
  839. lis
  840. (let ((las lis)
  841. (prev lis))
  842. (while (consp (cdr las))
  843. (setq prev las
  844. las (cdr las)))
  845. (setcdr prev nil)
  846. (cons (car las) lis))))
  847. (defun iswitchb-completion-help ()
  848. "Show possible completions in a *Completions* buffer."
  849. ;; we could allow this buffer to be used to select match, but I think
  850. ;; choose-completion-string will need redefining, so it just inserts
  851. ;; choice with out any previous input.
  852. (interactive)
  853. (setq iswitchb-rescan nil)
  854. (let ((buf (current-buffer))
  855. (temp-buf "*Completions*")
  856. (win))
  857. (if (and (eq last-command this-command)
  858. (not iswitchb-common-match-inserted))
  859. ;; scroll buffer
  860. (progn
  861. (set-buffer temp-buf)
  862. (setq win (get-buffer-window temp-buf))
  863. (if (pos-visible-in-window-p (point-max) win)
  864. (set-window-start win (point-min))
  865. (scroll-other-window))
  866. (set-buffer buf))
  867. (with-output-to-temp-buffer temp-buf
  868. (if (featurep 'xemacs)
  869. ;; XEmacs extents are put on by default, doesn't seem to be
  870. ;; any way of switching them off.
  871. (display-completion-list (or iswitchb-matches iswitchb-buflist)
  872. :help-string "iswitchb "
  873. :activate-callback
  874. (lambda (_x _y _z)
  875. (message "doesn't work yet, sorry!")))
  876. ;; else running Emacs
  877. (display-completion-list (or iswitchb-matches iswitchb-buflist))))
  878. (setq iswitchb-common-match-inserted nil))))
  879. ;;; KILL CURRENT BUFFER
  880. (defun iswitchb-kill-buffer ()
  881. "Kill the buffer at the head of `iswitchb-matches'."
  882. (interactive)
  883. (let ((enable-recursive-minibuffers t)
  884. buf)
  885. (setq buf (car iswitchb-matches))
  886. ;; check to see if buf is non-nil.
  887. (if buf
  888. (let ((bufobjs (mapcar (lambda (name)
  889. (or (get-buffer name) name))
  890. iswitchb-buflist)))
  891. (kill-buffer buf)
  892. ;; Check if buffer exists. XEmacs gnuserv.el makes alias
  893. ;; for kill-buffer which does not return t if buffer is
  894. ;; killed, so we can't rely on kill-buffer return value.
  895. (if (get-buffer buf)
  896. ;; buffer couldn't be killed.
  897. (setq iswitchb-rescan t)
  898. ;; Else `kill-buffer' succeeds so re-make the buffer list
  899. ;; taking into account packages like uniquify may rename
  900. ;; buffers, and try to preserve the ordering of buffers.
  901. (setq iswitchb-buflist
  902. (delq nil (mapcar (lambda (b)
  903. (if (bufferp b)
  904. (buffer-name b)
  905. b))
  906. bufobjs))))))))
  907. ;;; VISIT CHOSEN BUFFER
  908. (defun iswitchb-visit-buffer (buffer)
  909. "Visit buffer named BUFFER according to `iswitchb-method'."
  910. (let (win newframe)
  911. (cond
  912. ((eq iswitchb-method 'samewindow)
  913. (switch-to-buffer buffer))
  914. ((memq iswitchb-method '(always-frame maybe-frame))
  915. (cond
  916. ((and (setq win (iswitchb-window-buffer-p buffer))
  917. (or (eq iswitchb-method 'always-frame)
  918. (y-or-n-p "Jump to frame? ")))
  919. (setq newframe (window-frame win))
  920. (if (fboundp 'select-frame-set-input-focus)
  921. (select-frame-set-input-focus newframe)
  922. (raise-frame newframe)
  923. (select-frame newframe)
  924. )
  925. (select-window win))
  926. (t
  927. ;; No buffer in other frames...
  928. (switch-to-buffer buffer)
  929. )))
  930. ((eq iswitchb-method 'otherwindow)
  931. (switch-to-buffer-other-window buffer))
  932. ((eq iswitchb-method 'display)
  933. (display-buffer buffer))
  934. ((eq iswitchb-method 'otherframe)
  935. (progn
  936. (switch-to-buffer-other-frame buffer)
  937. (if (fboundp 'select-frame-set-input-focus)
  938. (select-frame-set-input-focus (selected-frame)))
  939. )))))
  940. (defun iswitchb-possible-new-buffer (buf)
  941. "Possibly create and visit a new buffer called BUF."
  942. (let ((newbufcreated))
  943. (if (and iswitchb-newbuffer
  944. (or
  945. (not iswitchb-prompt-newbuffer)
  946. (and iswitchb-prompt-newbuffer
  947. (y-or-n-p
  948. (format
  949. "No buffer matching `%s', create one? "
  950. buf)))))
  951. ;; then create a new buffer
  952. (progn
  953. (setq newbufcreated (get-buffer-create buf))
  954. (if (fboundp 'set-buffer-major-mode)
  955. (set-buffer-major-mode newbufcreated))
  956. (iswitchb-visit-buffer newbufcreated))
  957. ;; else won't create new buffer
  958. (message "no buffer matching `%s'" buf))))
  959. (defun iswitchb-window-buffer-p (buffer)
  960. "Return window pointer if BUFFER is visible in another frame.
  961. If BUFFER is visible in the current frame, return nil."
  962. (interactive)
  963. (let ((blist (iswitchb-get-buffers-in-frames 'current)))
  964. ;; If the buffer is visible in current frame, return nil
  965. (unless (member buffer blist)
  966. ;; maybe in other frame or icon
  967. (get-buffer-window buffer 0) ; better than 'visible
  968. )))
  969. (defun iswitchb-buffer ()
  970. "Switch to another buffer.
  971. The buffer name is selected interactively by typing a substring. The
  972. buffer is displayed according to `iswitchb-default-method' -- the
  973. default is to show it in the same window, unless it is already visible
  974. in another frame.
  975. For details of keybindings, do `\\[describe-function] iswitchb'."
  976. (interactive)
  977. (setq iswitchb-method iswitchb-default-method)
  978. (iswitchb))
  979. (defun iswitchb-buffer-other-window ()
  980. "Switch to another buffer and show it in another window.
  981. The buffer name is selected interactively by typing a substring.
  982. For details of keybindings, do `\\[describe-function] iswitchb'."
  983. (interactive)
  984. (setq iswitchb-method 'otherwindow)
  985. (iswitchb))
  986. (defun iswitchb-display-buffer ()
  987. "Display a buffer in another window but don't select it.
  988. The buffer name is selected interactively by typing a substring.
  989. For details of keybindings, do `\\[describe-function] iswitchb'."
  990. (interactive)
  991. (setq iswitchb-method 'display)
  992. (iswitchb))
  993. (defun iswitchb-buffer-other-frame ()
  994. "Switch to another buffer and show it in another frame.
  995. The buffer name is selected interactively by typing a substring.
  996. For details of keybindings, do `\\[describe-function] iswitchb'."
  997. (interactive)
  998. (setq iswitchb-method 'otherframe)
  999. (iswitchb))
  1000. ;;; XEmacs hack for showing default buffer
  1001. ;; The first time we enter the minibuffer, Emacs puts up the default
  1002. ;; buffer to switch to, but XEmacs doesn't -- presumably there is a
  1003. ;; subtle difference in the two versions of post-command-hook. The
  1004. ;; default is shown for both whenever we delete all of our text
  1005. ;; though, indicating its just a problem the first time we enter the
  1006. ;; function. To solve this, we use another entry hook for emacs to
  1007. ;; show the default the first time we enter the minibuffer.
  1008. (defun iswitchb-init-XEmacs-trick ()
  1009. "Display default buffer when first entering minibuffer.
  1010. This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'."
  1011. (if (iswitchb-entryfn-p)
  1012. (progn
  1013. (iswitchb-exhibit)
  1014. (goto-char (point-min)))))
  1015. ;; add this hook for XEmacs only.
  1016. (if (featurep 'xemacs)
  1017. (add-hook 'iswitchb-minibuffer-setup-hook
  1018. 'iswitchb-init-XEmacs-trick))
  1019. ;;; XEmacs / backspace key
  1020. ;; For some reason, if the backspace key is pressed in XEmacs, the
  1021. ;; line gets confused, so I've added a simple key definition to make
  1022. ;; backspace act like the normal delete key.
  1023. (defun iswitchb-xemacs-backspacekey ()
  1024. "Bind backspace to `backward-delete-char'."
  1025. (define-key iswitchb-mode-map '[backspace] 'backward-delete-char)
  1026. (define-key iswitchb-mode-map '[(meta backspace)] 'backward-kill-word))
  1027. (if (featurep 'xemacs)
  1028. (add-hook 'iswitchb-define-mode-map-hook
  1029. 'iswitchb-xemacs-backspacekey))
  1030. ;;; ICOMPLETE TYPE CODE
  1031. (defun iswitchb-exhibit ()
  1032. "Find matching buffers and display a list in the minibuffer.
  1033. Copied from `icomplete-exhibit' with two changes:
  1034. 1. It prints a default buffer name when there is no text yet entered.
  1035. 2. It calls my completion routine rather than the standard completion."
  1036. (if iswitchb-use-mycompletion
  1037. (let ((contents (buffer-substring (minibuffer-prompt-end) (point-max)))
  1038. (buffer-undo-list t))
  1039. (save-excursion
  1040. (goto-char (point-max))
  1041. ; Register the end of input, so we
  1042. ; know where the extra stuff
  1043. ; (match-status info) begins:
  1044. (if (not (boundp 'iswitchb-eoinput))
  1045. ;; In case it got wiped out by major mode business:
  1046. (make-local-variable 'iswitchb-eoinput))
  1047. (setq iswitchb-eoinput (point))
  1048. ;; Update the list of matches
  1049. (setq iswitchb-text contents)
  1050. (iswitchb-set-matches)
  1051. (setq iswitchb-rescan t)
  1052. (iswitchb-set-common-completion)
  1053. ;; Insert the match-status information:
  1054. (insert (iswitchb-completions
  1055. contents))))))
  1056. (defvar most-len)
  1057. (defvar most-is-exact)
  1058. (defun iswitchb-output-completion (com)
  1059. (if (= (length com) most-len)
  1060. ;; Most is one exact match,
  1061. ;; note that and leave out
  1062. ;; for later indication:
  1063. (ignore
  1064. (setq most-is-exact t))
  1065. (substring com most-len)))
  1066. (defun iswitchb-completions (name)
  1067. "Return the string that is displayed after the user's text.
  1068. Modified from `icomplete-completions'."
  1069. (let ((comps iswitchb-matches)
  1070. ; "-determined" - only one candidate
  1071. (open-bracket-determined "[")
  1072. (close-bracket-determined "]")
  1073. ;"-prospects" - more than one candidate
  1074. (open-bracket-prospects "{")
  1075. (close-bracket-prospects "}")
  1076. first)
  1077. (if (and iswitchb-use-faces comps)
  1078. (progn
  1079. (setq first (car comps))
  1080. (setq first (format "%s" first))
  1081. (put-text-property 0 (length first) 'face
  1082. (if (= (length comps) 1)
  1083. (if iswitchb-invalid-regexp
  1084. 'iswitchb-invalid-regexp
  1085. 'iswitchb-single-match)
  1086. 'iswitchb-current-match)
  1087. first)
  1088. (setq comps (cons first (cdr comps)))))
  1089. ;; If no buffers matched, and virtual buffers are being used, then
  1090. ;; consult the list of past visited files, to see if we can find
  1091. ;; the file which the user might thought was still open.
  1092. (when (and iswitchb-use-virtual-buffers (null comps)
  1093. recentf-list)
  1094. (setq iswitchb-virtual-buffers nil)
  1095. (let ((head recentf-list) name)
  1096. (while head
  1097. (if (and (setq name (file-name-nondirectory (car head)))
  1098. (string-match (if iswitchb-regexp
  1099. iswitchb-text
  1100. (regexp-quote iswitchb-text)) name)
  1101. (null (get-file-buffer (car head)))
  1102. (not (assoc name iswitchb-virtual-buffers))
  1103. (not (iswitchb-ignore-buffername-p name))
  1104. (file-exists-p (car head)))
  1105. (setq iswitchb-virtual-buffers
  1106. (cons (cons name (car head))
  1107. iswitchb-virtual-buffers)))
  1108. (setq head (cdr head)))
  1109. (setq iswitchb-virtual-buffers (nreverse iswitchb-virtual-buffers)
  1110. comps (mapcar 'car iswitchb-virtual-buffers))
  1111. (let ((comp comps))
  1112. (while comp
  1113. (put-text-property 0 (length (car comp))
  1114. 'face 'iswitchb-virtual-matches
  1115. (car comp))
  1116. (setq comp (cdr comp))))))
  1117. (cond ((null comps) (format " %sNo match%s"
  1118. open-bracket-determined
  1119. close-bracket-determined))
  1120. (iswitchb-invalid-regexp
  1121. (concat " " (car comps)))
  1122. ((null (cdr comps)) ;one match
  1123. (concat
  1124. (if (if (not iswitchb-regexp)
  1125. (= (length name)
  1126. (length (car comps)))
  1127. (string-match name (car comps))
  1128. (string-equal (match-string 0 (car comps))
  1129. (car comps)))
  1130. ""
  1131. (concat open-bracket-determined
  1132. ;; when there is one match, show the
  1133. ;; matching buffer name in full
  1134. (car comps)
  1135. close-bracket-determined))
  1136. (if (not iswitchb-use-faces) " [Matched]")))
  1137. (t ;multiple matches
  1138. (if (and iswitchb-max-to-show
  1139. (> (length comps) iswitchb-max-to-show))
  1140. (setq comps
  1141. (append
  1142. (let ((res nil)
  1143. (comp comps)
  1144. (end (/ iswitchb-max-to-show 2)))
  1145. (while (>= (setq end (1- end)) 0)
  1146. (setq res (cons (car comp) res)
  1147. comp (cdr comp)))
  1148. (nreverse res))
  1149. (list "...")
  1150. (nthcdr (- (length comps)
  1151. (/ iswitchb-max-to-show 2)) comps))))
  1152. (let* (
  1153. ;;(most (try-completion name candidates predicate))
  1154. (most nil)
  1155. (most-len (length most))
  1156. most-is-exact
  1157. (alternatives
  1158. (mapconcat (if most 'iswitchb-output-completion
  1159. 'identity) comps iswitchb-delim)))
  1160. (concat
  1161. ;; put in common completion item -- what you get by
  1162. ;; pressing tab
  1163. (if (and (stringp iswitchb-common-match-string)
  1164. (> (length iswitchb-common-match-string) (length name)))
  1165. (concat open-bracket-determined
  1166. (substring iswitchb-common-match-string
  1167. (length name))
  1168. close-bracket-determined))
  1169. ;; end of partial matches...
  1170. ;; think this bit can be ignored.
  1171. (and (> most-len (length name))
  1172. (concat open-bracket-determined
  1173. (substring most (length name))
  1174. close-bracket-determined))
  1175. ;; list all alternatives
  1176. open-bracket-prospects
  1177. (if most-is-exact
  1178. (concat iswitchb-delim alternatives)
  1179. alternatives)
  1180. close-bracket-prospects))))))
  1181. (defun iswitchb-minibuffer-setup ()
  1182. "Set up minibuffer for `iswitchb-buffer'.
  1183. Copied from `icomplete-minibuffer-setup-hook'."
  1184. (when (iswitchb-entryfn-p)
  1185. (set (make-local-variable 'iswitchb-use-mycompletion) t)
  1186. (add-hook 'pre-command-hook 'iswitchb-pre-command nil t)
  1187. (add-hook 'post-command-hook 'iswitchb-post-command nil t)
  1188. (run-hooks 'iswitchb-minibuffer-setup-hook)))
  1189. (defun iswitchb-pre-command ()
  1190. "Run before command in `iswitchb-buffer'."
  1191. (iswitchb-tidy))
  1192. (defun iswitchb-post-command ()
  1193. "Run after command in `iswitchb-buffer'."
  1194. (iswitchb-exhibit))
  1195. (defun iswitchb-tidy ()
  1196. "Remove completions display, if any, prior to new user input.
  1197. Copied from `icomplete-tidy'."
  1198. (if (and (boundp 'iswitchb-eoinput)
  1199. iswitchb-eoinput)
  1200. (if (> iswitchb-eoinput (point-max))
  1201. ;; Oops, got rug pulled out from under us - reinit:
  1202. (setq iswitchb-eoinput (point-max))
  1203. (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
  1204. (delete-region iswitchb-eoinput (point-max))))
  1205. ;; Reestablish the local variable 'cause minibuffer-setup is weird:
  1206. (make-local-variable 'iswitchb-eoinput)
  1207. (setq iswitchb-eoinput 1)))
  1208. (defun iswitchb-entryfn-p ()
  1209. "Return non-nil if we are using `iswitchb-buffer'."
  1210. (eq iswitchb-minibuf-depth (minibuffer-depth)))
  1211. (defun iswitchb-summaries-to-end ()
  1212. "Move the summaries to the end of the list.
  1213. This is an example function which can be hooked on to
  1214. `iswitchb-make-buflist-hook'. Any buffer matching the regexps
  1215. `Summary' or `output\*$'are put to the end of the list."
  1216. (let ((summaries (delq nil
  1217. (mapcar
  1218. (lambda (x)
  1219. (if (string-match "Summary\\|output\\*$" x)
  1220. x))
  1221. iswitchb-temp-buflist))))
  1222. (iswitchb-to-end summaries)))
  1223. (defun iswitchb-case ()
  1224. "Return non-nil if we should ignore case when matching.
  1225. See the variable `iswitchb-case' for details."
  1226. (if iswitchb-case
  1227. (if (featurep 'xemacs)
  1228. (isearch-no-upper-case-p iswitchb-text)
  1229. (isearch-no-upper-case-p iswitchb-text t))))
  1230. ;;;###autoload
  1231. (define-minor-mode iswitchb-mode
  1232. "Toggle Iswitchb mode.
  1233. With a prefix argument ARG, enable Iswitchb mode if ARG is
  1234. positive, and disable it otherwise. If called from Lisp, enable
  1235. the mode if ARG is omitted or nil.
  1236. Iswitchb mode is a global minor mode that enables switching
  1237. between buffers using substrings. See `iswitchb' for details."
  1238. nil nil iswitchb-global-map :global t :group 'iswitchb
  1239. (if iswitchb-mode
  1240. (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
  1241. (remove-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)))
  1242. (provide 'iswitchb)
  1243. ;;; iswitchb.el ends here