iswitchb.el 50 KB

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