iswitchb.el 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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. (defcustom iswitchb-all-frames 'visible
  317. "Argument to pass to `walk-windows' when iswitchb is finding buffers.
  318. See documentation of `walk-windows' for useful values."
  319. :type '(choice (const :tag "Selected frame only" nil)
  320. (const :tag "All existing frames" t)
  321. (const :tag "All visible frames" visible)
  322. (const :tag "All frames on this terminal" 0))
  323. :group 'iswitchb)
  324. (defcustom iswitchb-minibuffer-setup-hook nil
  325. "Iswitchb-specific customization of minibuffer setup.
  326. This hook is run during minibuffer setup if `iswitchb' is active.
  327. For instance:
  328. \(add-hook 'iswitchb-minibuffer-setup-hook
  329. '\(lambda () (set (make-local-variable 'max-mini-window-height) 3)))
  330. will constrain the minibuffer to a maximum height of 3 lines when
  331. iswitchb is running."
  332. :type 'hook
  333. :group 'iswitchb)
  334. (defface iswitchb-single-match
  335. '((t
  336. (:inherit font-lock-comment-face)))
  337. "Iswitchb face for single matching buffer name."
  338. :version "22.1"
  339. :group 'iswitchb)
  340. (defface iswitchb-current-match
  341. '((t
  342. (:inherit font-lock-function-name-face)))
  343. "Iswitchb face for current matching buffer name."
  344. :version "22.1"
  345. :group 'iswitchb)
  346. (defface iswitchb-virtual-matches
  347. '((t
  348. (:inherit font-lock-builtin-face)))
  349. "Iswitchb face for matching virtual buffer names.
  350. See also `iswitchb-use-virtual-buffers'."
  351. :version "22.1"
  352. :group 'iswitchb)
  353. (defface iswitchb-invalid-regexp
  354. '((t
  355. (:inherit font-lock-warning-face)))
  356. "Iswitchb face for indicating invalid regexp. "
  357. :version "22.1"
  358. :group 'iswitchb)
  359. ;; Do we need the variable iswitchb-use-mycompletion?
  360. ;;; Internal Variables
  361. (defvar iswitchb-method nil
  362. "Stores the method for viewing the selected buffer.
  363. Its value is one of `samewindow', `otherwindow', `display', `otherframe',
  364. `maybe-frame' or `always-frame'. See `iswitchb-default-method' for
  365. details of values.")
  366. (defvar iswitchb-eoinput 1
  367. "Point where minibuffer input ends and completion info begins.
  368. Copied from `icomplete-eoinput'.")
  369. (make-variable-buffer-local 'iswitchb-eoinput)
  370. (defvar iswitchb-buflist nil
  371. "Stores the current list of buffers that will be searched through.
  372. The list is ordered, so that the most recent buffers come first,
  373. although by default, the buffers visible in the current frame are put
  374. at the end of the list. Created by `iswitchb-make-buflist'.")
  375. ;; todo -- is this necessary?
  376. (defvar iswitchb-use-mycompletion nil
  377. "Non-nil means use `iswitchb-buffer' completion feedback.
  378. Should only be set to t by iswitchb functions, so that it doesn't
  379. interfere with other minibuffer usage.")
  380. (defvar iswitchb-change-word-sub nil
  381. "Private variable used by `iswitchb-word-matching-substring'.")
  382. (defvar iswitchb-common-match-string nil
  383. "Stores the string that is common to all matching buffers.")
  384. (defvar iswitchb-rescan nil
  385. "Non-nil means we need to regenerate the list of matching buffers.")
  386. (defvar iswitchb-text nil
  387. "Stores the users string as it is typed in.")
  388. (defvar iswitchb-matches nil
  389. "List of buffers currently matching `iswitchb-text'.")
  390. (defvar iswitchb-mode-map
  391. (let ((map (make-sparse-keymap)))
  392. (set-keymap-parent map minibuffer-local-map)
  393. (define-key map "?" 'iswitchb-completion-help)
  394. (define-key map "\C-s" 'iswitchb-next-match)
  395. (define-key map "\C-r" 'iswitchb-prev-match)
  396. (define-key map "\t" 'iswitchb-complete)
  397. (define-key map "\C-j" 'iswitchb-select-buffer-text)
  398. (define-key map "\C-t" 'iswitchb-toggle-regexp)
  399. (define-key map "\C-x\C-f" 'iswitchb-find-file)
  400. (define-key map "\C-c" 'iswitchb-toggle-case)
  401. (define-key map "\C-k" 'iswitchb-kill-buffer)
  402. (define-key map "\C-m" 'iswitchb-exit-minibuffer)
  403. map)
  404. "Minibuffer keymap for `iswitchb-buffer'.")
  405. (defvar iswitchb-global-map
  406. (let ((map (make-sparse-keymap)))
  407. (dolist (b '((switch-to-buffer . iswitchb-buffer)
  408. (switch-to-buffer-other-window . iswitchb-buffer-other-window)
  409. (switch-to-buffer-other-frame . iswitchb-buffer-other-frame)
  410. (display-buffer . iswitchb-display-buffer)))
  411. (if (fboundp 'command-remapping)
  412. (define-key map (vector 'remap (car b)) (cdr b))
  413. (substitute-key-definition (car b) (cdr b) map global-map)))
  414. map)
  415. "Global keymap for `iswitchb-mode'.")
  416. (defvar iswitchb-history nil
  417. "History of buffers selected using `iswitchb-buffer'.")
  418. (defvar iswitchb-exit nil
  419. "Flag to monitor how `iswitchb-buffer' exits.
  420. If equal to `takeprompt', we use the prompt as the buffer name to be
  421. selected.")
  422. (defvar iswitchb-buffer-ignore-orig nil
  423. "Stores original value of `iswitchb-buffer-ignore'.")
  424. (defvar iswitchb-default nil
  425. "Default buffer for iswitchb.")
  426. ;; The following variables are needed to keep the byte compiler quiet.
  427. (defvar iswitchb-require-match nil
  428. "Non-nil if matching buffer must be selected.")
  429. (defvar iswitchb-temp-buflist nil
  430. "Stores a temporary version of the buffer list being created.")
  431. (defvar iswitchb-bufs-in-frame nil
  432. "List of the buffers visible in the current frame.")
  433. (defvar iswitchb-minibuf-depth nil
  434. "Value we expect to be returned by `minibuffer-depth' in the minibuffer.")
  435. (defvar iswitchb-common-match-inserted nil
  436. "Non-nil if we have just inserted a common match in the minibuffer.")
  437. (defvar iswitchb-invalid-regexp)
  438. ;;; FUNCTIONS
  439. ;;; ISWITCHB KEYMAP
  440. (defun iswitchb-define-mode-map ()
  441. "Set up the keymap for `iswitchb-buffer'."
  442. (interactive)
  443. (let (map)
  444. ;; generated every time so that it can inherit new functions.
  445. ;;(or iswitchb-mode-map
  446. (setq map (copy-keymap minibuffer-local-map))
  447. (define-key map "?" 'iswitchb-completion-help)
  448. (define-key map "\C-s" 'iswitchb-next-match)
  449. (define-key map "\C-r" 'iswitchb-prev-match)
  450. (define-key map "\t" 'iswitchb-complete)
  451. (define-key map "\C-j" 'iswitchb-select-buffer-text)
  452. (define-key map "\C-t" 'iswitchb-toggle-regexp)
  453. (define-key map "\C-x\C-f" 'iswitchb-find-file)
  454. (define-key map "\C-n" 'iswitchb-toggle-ignore)
  455. (define-key map "\C-c" 'iswitchb-toggle-case)
  456. (define-key map "\C-k" 'iswitchb-kill-buffer)
  457. (define-key map "\C-m" 'iswitchb-exit-minibuffer)
  458. (setq iswitchb-mode-map map)
  459. (run-hooks 'iswitchb-define-mode-map-hook)))
  460. (make-obsolete 'iswitchb-define-mode-map
  461. "use M-x iswitchb-mode or customize the variable `iswitchb-mode'."
  462. "21.1")
  463. ;;; MAIN FUNCTION
  464. (defun iswitchb ()
  465. "Switch to buffer matching a substring.
  466. As you type in a string, all of the buffers matching the string are
  467. displayed. When you have found the buffer you want, it can then be
  468. selected. As you type, most keys have their normal keybindings,
  469. except for the following:
  470. \\<iswitchb-mode-map>
  471. RET Select the buffer at the front of the list of matches. If the
  472. list is empty, possibly prompt to create new buffer.
  473. \\[iswitchb-select-buffer-text] Select the current prompt as the buffer.
  474. If no buffer is found, prompt for a new one.
  475. \\[iswitchb-next-match] Put the first element at the end of the list.
  476. \\[iswitchb-prev-match] Put the last element at the start of the list.
  477. \\[iswitchb-complete] Complete a common suffix to the current string that
  478. matches all buffers. If there is only one match, select that buffer.
  479. If there is no common suffix, show a list of all matching buffers
  480. in a separate window.
  481. \\[iswitchb-toggle-regexp] Toggle regexp searching.
  482. \\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names.
  483. \\[iswitchb-completion-help] Show list of matching buffers in separate window.
  484. \\[iswitchb-find-file] Exit iswitchb and drop into `find-file'.
  485. \\[iswitchb-kill-buffer] Kill buffer at head of buffer list."
  486. ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
  487. ;;`iswitchb-buffer-ignore')
  488. (let* ((prompt "iswitch ")
  489. iswitchb-invalid-regexp
  490. (buf (iswitchb-read-buffer prompt)))
  491. ;;(message "chosen text %s" iswitchb-final-text)
  492. ;; Choose the buffer name: either the text typed in, or the head
  493. ;; of the list of matches
  494. (cond ( (eq iswitchb-exit 'findfile)
  495. (call-interactively 'find-file))
  496. (iswitchb-invalid-regexp
  497. (message "Won't make invalid regexp named buffer"))
  498. (t
  499. ;; View the buffer
  500. ;;(message "go to buf %s" buf)
  501. ;; Check buf is non-nil.
  502. (if buf
  503. (if (get-buffer buf)
  504. ;; buffer exists, so view it and then exit
  505. (iswitchb-visit-buffer buf)
  506. ;; else buffer doesn't exist
  507. (iswitchb-possible-new-buffer buf)))
  508. ))))
  509. (defun iswitchb-read-buffer (prompt &optional default require-match
  510. start matches-set)
  511. "Replacement for the built-in `read-buffer'.
  512. Return the name of a buffer selected.
  513. PROMPT is the prompt to give to the user.
  514. DEFAULT if given is the default buffer to be selected, which will
  515. go to the front of the list.
  516. If REQUIRE-MATCH is non-nil, an existing buffer must be selected.
  517. If START is a string, the selection process is started with that
  518. string.
  519. If MATCHES-SET is non-nil, the buflist is not updated before
  520. the selection process begins. Used by isearchb.el."
  521. (let
  522. (
  523. buf-sel
  524. iswitchb-final-text
  525. (icomplete-mode nil) ;; prevent icomplete starting up
  526. )
  527. (iswitchb-define-mode-map)
  528. (setq iswitchb-exit nil)
  529. (setq iswitchb-default
  530. (if (bufferp default)
  531. (buffer-name default)
  532. default))
  533. (setq iswitchb-text (or start ""))
  534. (unless matches-set
  535. (setq iswitchb-rescan t)
  536. (iswitchb-make-buflist iswitchb-default)
  537. (iswitchb-set-matches))
  538. (let
  539. ((minibuffer-local-completion-map iswitchb-mode-map)
  540. ;; Record the minibuffer depth that we expect to find once
  541. ;; the minibuffer is set up and iswitchb-entryfn-p is called.
  542. (iswitchb-minibuf-depth (1+ (minibuffer-depth)))
  543. (iswitchb-require-match require-match))
  544. ;; prompt the user for the buffer name
  545. (setq iswitchb-final-text (completing-read
  546. prompt ;the prompt
  547. '(("dummy" . 1)) ;table
  548. nil ;predicate
  549. nil ;require-match [handled elsewhere]
  550. start ;initial-contents
  551. 'iswitchb-history)))
  552. (if (and (not (eq iswitchb-exit 'usefirst))
  553. (get-buffer iswitchb-final-text))
  554. ;; This happens for example if the buffer was chosen with the mouse.
  555. (setq iswitchb-matches (list iswitchb-final-text)
  556. iswitchb-virtual-buffers nil))
  557. ;; If no buffer matched, but a virtual buffer was selected, visit
  558. ;; that file now and act as though that buffer had been selected.
  559. (if (and iswitchb-virtual-buffers
  560. (not (iswitchb-existing-buffer-p)))
  561. (let ((virt (car iswitchb-virtual-buffers))
  562. (new-buf))
  563. ;; Keep the name of the buffer returned by find-file-noselect, as
  564. ;; the buffer 'virt' could be a symlink to a file of a different name.
  565. (setq new-buf (buffer-name (find-file-noselect (cdr virt))))
  566. (setq iswitchb-matches (list new-buf)
  567. iswitchb-virtual-buffers nil)))
  568. ;; Handling the require-match must be done in a better way.
  569. (if (and require-match
  570. (not (iswitchb-existing-buffer-p)))
  571. (error "Must specify valid buffer"))
  572. (if (or (eq iswitchb-exit 'takeprompt)
  573. (null iswitchb-matches))
  574. (setq buf-sel iswitchb-final-text)
  575. ;; else take head of list
  576. (setq buf-sel (car iswitchb-matches)))
  577. ;; Or possibly choose the default buffer
  578. (if (equal iswitchb-final-text "")
  579. (setq buf-sel (car iswitchb-matches)))
  580. buf-sel))
  581. (defun iswitchb-existing-buffer-p ()
  582. "Return non-nil if there is a matching buffer."
  583. (not (null iswitchb-matches)))
  584. ;;; COMPLETION CODE
  585. (defun iswitchb-set-common-completion ()
  586. "Find common completion of `iswitchb-text' in `iswitchb-matches'.
  587. The result is stored in `iswitchb-common-match-string'."
  588. (let (val)
  589. (setq iswitchb-common-match-string nil)
  590. (if (and iswitchb-matches
  591. (not iswitchb-regexp) ;; testing
  592. (stringp iswitchb-text)
  593. (> (length iswitchb-text) 0))
  594. (if (setq val (iswitchb-find-common-substring
  595. iswitchb-matches iswitchb-text))
  596. (setq iswitchb-common-match-string val)))
  597. val))
  598. (defun iswitchb-complete ()
  599. "Try and complete the current pattern amongst the buffer names."
  600. (interactive)
  601. (let (res)
  602. (cond ((not iswitchb-matches)
  603. (run-hooks 'iswitchb-cannot-complete-hook))
  604. (iswitchb-invalid-regexp
  605. ;; Do nothing
  606. )
  607. ((= 1 (length iswitchb-matches))
  608. ;; only one choice, so select it.
  609. (exit-minibuffer))
  610. (t
  611. ;; else there could be some completions
  612. (setq res iswitchb-common-match-string)
  613. (if (and (not (memq res '(t nil)))
  614. (not (equal res iswitchb-text)))
  615. ;; found something to complete, so put it in the minibuffer.
  616. (progn
  617. (setq iswitchb-rescan nil
  618. iswitchb-common-match-inserted t)
  619. (delete-region (minibuffer-prompt-end) (point))
  620. (insert res))
  621. ;; else nothing to complete
  622. (run-hooks 'iswitchb-cannot-complete-hook)
  623. )))))
  624. ;;; TOGGLE FUNCTIONS
  625. (defun iswitchb-toggle-case ()
  626. "Toggle the value of variable `iswitchb-case'."
  627. (interactive)
  628. (setq iswitchb-case (not iswitchb-case))
  629. ;; ask for list to be regenerated.
  630. (setq iswitchb-rescan t))
  631. (defun iswitchb-toggle-regexp ()
  632. "Toggle the value of `iswitchb-regexp'."
  633. (interactive)
  634. (setq iswitchb-regexp (not iswitchb-regexp))
  635. ;; ask for list to be regenerated.
  636. (setq iswitchb-rescan t))
  637. (defun iswitchb-toggle-ignore ()
  638. "Toggle ignoring buffers specified with `iswitchb-buffer-ignore'."
  639. (interactive)
  640. (if iswitchb-buffer-ignore
  641. (progn
  642. (setq iswitchb-buffer-ignore-orig iswitchb-buffer-ignore)
  643. (setq iswitchb-buffer-ignore nil))
  644. ;; else
  645. (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig))
  646. (iswitchb-make-buflist iswitchb-default)
  647. ;; ask for list to be regenerated.
  648. (setq iswitchb-rescan t))
  649. (defun iswitchb-exit-minibuffer ()
  650. "Exit minibuffer, but make sure we have a match if one is needed."
  651. (interactive)
  652. (if (or (not iswitchb-require-match)
  653. (iswitchb-existing-buffer-p))
  654. (progn
  655. (setq iswitchb-exit 'usefirst)
  656. (throw 'exit nil))))
  657. (defun iswitchb-select-buffer-text ()
  658. "Select the buffer named by the prompt.
  659. If no buffer exactly matching the prompt exists, maybe create a new one."
  660. (interactive)
  661. (setq iswitchb-exit 'takeprompt)
  662. (exit-minibuffer))
  663. (defun iswitchb-find-file ()
  664. "Drop into `find-file' from buffer switching."
  665. (interactive)
  666. (setq iswitchb-exit 'findfile)
  667. (exit-minibuffer))
  668. (defvar recentf-list)
  669. (defun iswitchb-next-match ()
  670. "Put first element of `iswitchb-matches' at the end of the list."
  671. (interactive)
  672. (let ((next (cadr iswitchb-matches)))
  673. (if (and (null next) iswitchb-virtual-buffers)
  674. (setq recentf-list
  675. (iswitchb-chop recentf-list
  676. (cdr (cadr iswitchb-virtual-buffers))))
  677. (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist next)))
  678. (setq iswitchb-rescan t)))
  679. (defun iswitchb-prev-match ()
  680. "Put last element of `iswitchb-matches' at the front of the list."
  681. (interactive)
  682. (let ((prev (car (last iswitchb-matches))))
  683. (if (and (null prev) iswitchb-virtual-buffers)
  684. (setq recentf-list
  685. (iswitchb-chop recentf-list
  686. (cdr (car (last iswitchb-virtual-buffers)))))
  687. (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist prev)))
  688. (setq iswitchb-rescan t)))
  689. (defun iswitchb-chop (list elem)
  690. "Remove all elements before ELEM and put them at the end of LIST."
  691. (let ((ret nil)
  692. (next nil)
  693. (sofar nil))
  694. (while (not ret)
  695. (setq next (car list))
  696. (if (equal next elem)
  697. (setq ret (append list (nreverse sofar)))
  698. ;; else
  699. (progn
  700. (setq list (cdr list))
  701. (setq sofar (cons next sofar)))))
  702. ret))
  703. ;;; CREATE LIST OF ALL CURRENT BUFFERS
  704. (defun iswitchb-make-buflist (default)
  705. "Set `iswitchb-buflist' to the current list of buffers.
  706. Currently visible buffers are put at the end of the list.
  707. The hook `iswitchb-make-buflist-hook' is run after the list has been
  708. created to allow the user to further modify the order of the buffer names
  709. in this list. If DEFAULT is non-nil, and corresponds to an existing buffer,
  710. it is put to the start of the list."
  711. (setq iswitchb-buflist
  712. (let* ((iswitchb-current-buffers (iswitchb-get-buffers-in-frames))
  713. (iswitchb-temp-buflist
  714. (delq nil
  715. (mapcar
  716. (lambda (x)
  717. (let ((b-name (buffer-name x)))
  718. (if (not
  719. (or
  720. (iswitchb-ignore-buffername-p b-name)
  721. (memq b-name iswitchb-current-buffers)))
  722. b-name)))
  723. (buffer-list (and iswitchb-use-frame-buffer-list
  724. (selected-frame)))))))
  725. (setq iswitchb-temp-buflist
  726. (nconc iswitchb-temp-buflist iswitchb-current-buffers))
  727. (run-hooks 'iswitchb-make-buflist-hook)
  728. ;; Should this be after the hooks, or should the hooks be the
  729. ;; final thing to be run?
  730. (if default
  731. (progn
  732. (setq iswitchb-temp-buflist
  733. (delete default iswitchb-temp-buflist))
  734. (setq iswitchb-temp-buflist
  735. (cons default iswitchb-temp-buflist))))
  736. iswitchb-temp-buflist)))
  737. (defun iswitchb-to-end (lst)
  738. "Move the elements from LST to the end of `iswitchb-temp-buflist'."
  739. (dolist (elem lst)
  740. (setq iswitchb-temp-buflist (delq elem iswitchb-temp-buflist)))
  741. (setq iswitchb-temp-buflist (nconc iswitchb-temp-buflist lst)))
  742. (defun iswitchb-get-buffers-in-frames (&optional current)
  743. "Return the list of buffers that are visible in the current frame.
  744. If optional argument CURRENT is given, restrict searching to the
  745. current frame, rather than all frames, regardless of value of
  746. `iswitchb-all-frames'."
  747. (let ((iswitchb-bufs-in-frame nil))
  748. (walk-windows 'iswitchb-get-bufname nil
  749. (if current
  750. nil
  751. iswitchb-all-frames))
  752. iswitchb-bufs-in-frame))
  753. (defun iswitchb-get-bufname (win)
  754. "Used by `iswitchb-get-buffers-in-frames' to walk through all windows."
  755. (let ((buf (buffer-name (window-buffer win))))
  756. (if (not (member buf iswitchb-bufs-in-frame))
  757. ;; Only add buf if it is not already in list.
  758. ;; This prevents same buf in two different windows being
  759. ;; put into the list twice.
  760. (setq iswitchb-bufs-in-frame
  761. (cons buf iswitchb-bufs-in-frame)))))
  762. ;;; FIND MATCHING BUFFERS
  763. (defun iswitchb-set-matches ()
  764. "Set `iswitchb-matches' to the list of buffers matching prompt."
  765. (if iswitchb-rescan
  766. (setq iswitchb-matches
  767. (let ((buflist iswitchb-buflist))
  768. (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp
  769. buflist))
  770. iswitchb-virtual-buffers nil)))
  771. (defun iswitchb-get-matched-buffers (regexp
  772. &optional string-format buffer-list)
  773. "Return buffers matching REGEXP.
  774. If STRING-FORMAT is nil, consider REGEXP as just a string.
  775. BUFFER-LIST can be list of buffers or list of strings."
  776. (let ((case-fold-search (iswitchb-case))
  777. name ret)
  778. (if (null string-format) (setq regexp (regexp-quote regexp)))
  779. (setq iswitchb-invalid-regexp nil)
  780. (condition-case error
  781. (dolist (x buffer-list (nreverse ret))
  782. (setq name (if (stringp x) x (buffer-name x)))
  783. (when (and (string-match regexp name)
  784. (not (iswitchb-ignore-buffername-p name)))
  785. (push name ret)))
  786. (invalid-regexp
  787. (setq iswitchb-invalid-regexp t)
  788. (cdr error)))))
  789. (defun iswitchb-ignore-buffername-p (bufname)
  790. "Return t if the buffer BUFNAME should be ignored."
  791. (let ((data (match-data))
  792. (re-list iswitchb-buffer-ignore)
  793. ignorep
  794. nextstr)
  795. (while re-list
  796. (setq nextstr (car re-list))
  797. (cond
  798. ((stringp nextstr)
  799. (if (string-match nextstr bufname)
  800. (progn
  801. (setq ignorep t)
  802. (setq re-list nil))))
  803. ((functionp nextstr)
  804. (if (funcall nextstr bufname)
  805. (progn
  806. (setq ignorep t)
  807. (setq re-list nil)))))
  808. (setq re-list (cdr re-list)))
  809. (set-match-data data)
  810. ;; return the result
  811. ignorep))
  812. (defun iswitchb-word-matching-substring (word)
  813. "Return part of WORD before 1st match to `iswitchb-change-word-sub'.
  814. If `iswitchb-change-word-sub' cannot be found in WORD, return nil."
  815. (let ((case-fold-search (iswitchb-case)))
  816. (let ((m (string-match iswitchb-change-word-sub word)))
  817. (if m
  818. (substring word m)
  819. ;; else no match
  820. nil))))
  821. (defun iswitchb-find-common-substring (lis subs)
  822. "Return common string following SUBS in each element of LIS."
  823. (let (res
  824. alist
  825. iswitchb-change-word-sub)
  826. (setq iswitchb-change-word-sub
  827. (if iswitchb-regexp
  828. subs
  829. (regexp-quote subs)))
  830. (setq res (mapcar 'iswitchb-word-matching-substring lis))
  831. (setq res (delq nil res)) ;; remove any nil elements (shouldn't happen)
  832. (setq alist (mapcar 'iswitchb-makealist res)) ;; could use an OBARRAY
  833. ;; try-completion returns t if there is an exact match.
  834. (let ((completion-ignore-case (iswitchb-case)))
  835. (try-completion subs alist))))
  836. (defun iswitchb-makealist (res)
  837. "Return dotted pair (RES . 1)."
  838. (cons res 1))
  839. ;; from Wayne Mesard <wmesard@esd.sgi.com>
  840. (defun iswitchb-rotate-list (lis)
  841. "Destructively remove the last element from LIS.
  842. Return the modified list with the last element prepended to it."
  843. (if (<= (length lis) 1)
  844. lis
  845. (let ((las lis)
  846. (prev lis))
  847. (while (consp (cdr las))
  848. (setq prev las
  849. las (cdr las)))
  850. (setcdr prev nil)
  851. (cons (car las) lis))))
  852. (defun iswitchb-completion-help ()
  853. "Show possible completions in a *Completions* buffer."
  854. ;; we could allow this buffer to be used to select match, but I think
  855. ;; choose-completion-string will need redefining, so it just inserts
  856. ;; choice with out any previous input.
  857. (interactive)
  858. (setq iswitchb-rescan nil)
  859. (let ((buf (current-buffer))
  860. (temp-buf "*Completions*")
  861. (win))
  862. (if (and (eq last-command this-command)
  863. (not iswitchb-common-match-inserted))
  864. ;; scroll buffer
  865. (progn
  866. (set-buffer temp-buf)
  867. (setq win (get-buffer-window temp-buf))
  868. (if (pos-visible-in-window-p (point-max) win)
  869. (set-window-start win (point-min))
  870. (scroll-other-window))
  871. (set-buffer buf))
  872. (with-output-to-temp-buffer temp-buf
  873. (if (featurep 'xemacs)
  874. ;; XEmacs extents are put on by default, doesn't seem to be
  875. ;; any way of switching them off.
  876. (display-completion-list (or iswitchb-matches iswitchb-buflist)
  877. :help-string "iswitchb "
  878. :activate-callback
  879. (lambda (_x _y _z)
  880. (message "doesn't work yet, sorry!")))
  881. ;; else running Emacs
  882. (display-completion-list (or iswitchb-matches iswitchb-buflist))))
  883. (setq iswitchb-common-match-inserted nil))))
  884. ;;; KILL CURRENT BUFFER
  885. (defun iswitchb-kill-buffer ()
  886. "Kill the buffer at the head of `iswitchb-matches'."
  887. (interactive)
  888. (let ((enable-recursive-minibuffers t)
  889. buf)
  890. (setq buf (car iswitchb-matches))
  891. ;; check to see if buf is non-nil.
  892. (if buf
  893. (let ((bufobjs (mapcar (lambda (name)
  894. (or (get-buffer name) name))
  895. iswitchb-buflist)))
  896. (kill-buffer buf)
  897. ;; Check if buffer exists. XEmacs gnuserv.el makes alias
  898. ;; for kill-buffer which does not return t if buffer is
  899. ;; killed, so we can't rely on kill-buffer return value.
  900. (if (get-buffer buf)
  901. ;; buffer couldn't be killed.
  902. (setq iswitchb-rescan t)
  903. ;; Else `kill-buffer' succeeds so re-make the buffer list
  904. ;; taking into account packages like uniquify may rename
  905. ;; buffers, and try to preserve the ordering of buffers.
  906. (setq iswitchb-buflist
  907. (delq nil (mapcar (lambda (b)
  908. (if (bufferp b)
  909. (buffer-name b)
  910. b))
  911. bufobjs))))))))
  912. ;;; VISIT CHOSEN BUFFER
  913. (defun iswitchb-visit-buffer (buffer)
  914. "Visit buffer named BUFFER according to `iswitchb-method'."
  915. (let (win newframe)
  916. (cond
  917. ((eq iswitchb-method 'samewindow)
  918. (switch-to-buffer buffer))
  919. ((memq iswitchb-method '(always-frame maybe-frame))
  920. (cond
  921. ((and (setq win (iswitchb-window-buffer-p buffer))
  922. (or (eq iswitchb-method 'always-frame)
  923. (y-or-n-p "Jump to frame? ")))
  924. (setq newframe (window-frame win))
  925. (if (fboundp 'select-frame-set-input-focus)
  926. (select-frame-set-input-focus newframe)
  927. (raise-frame newframe)
  928. (select-frame newframe)
  929. )
  930. (select-window win))
  931. (t
  932. ;; No buffer in other frames...
  933. (switch-to-buffer buffer)
  934. )))
  935. ((eq iswitchb-method 'otherwindow)
  936. (switch-to-buffer-other-window buffer))
  937. ((eq iswitchb-method 'display)
  938. (display-buffer buffer))
  939. ((eq iswitchb-method 'otherframe)
  940. (progn
  941. (switch-to-buffer-other-frame buffer)
  942. (if (fboundp 'select-frame-set-input-focus)
  943. (select-frame-set-input-focus (selected-frame)))
  944. )))))
  945. (defun iswitchb-possible-new-buffer (buf)
  946. "Possibly create and visit a new buffer called BUF."
  947. (let ((newbufcreated))
  948. (if (and iswitchb-newbuffer
  949. (or
  950. (not iswitchb-prompt-newbuffer)
  951. (and iswitchb-prompt-newbuffer
  952. (y-or-n-p
  953. (format
  954. "No buffer matching `%s', create one? "
  955. buf)))))
  956. ;; then create a new buffer
  957. (progn
  958. (setq newbufcreated (get-buffer-create buf))
  959. (if (fboundp 'set-buffer-major-mode)
  960. (set-buffer-major-mode newbufcreated))
  961. (iswitchb-visit-buffer newbufcreated))
  962. ;; else won't create new buffer
  963. (message "no buffer matching `%s'" buf))))
  964. (defun iswitchb-window-buffer-p (buffer)
  965. "Return window pointer if BUFFER is visible in another frame.
  966. If BUFFER is visible in the current frame, return nil."
  967. (interactive)
  968. (let ((blist (iswitchb-get-buffers-in-frames 'current)))
  969. ;; If the buffer is visible in current frame, return nil
  970. (unless (member buffer blist)
  971. ;; maybe in other frame or icon
  972. (get-buffer-window buffer 0) ; better than 'visible
  973. )))
  974. (defun iswitchb-buffer ()
  975. "Switch to another buffer.
  976. The buffer name is selected interactively by typing a substring. The
  977. buffer is displayed according to `iswitchb-default-method' -- the
  978. default is to show it in the same window, unless it is already visible
  979. in another frame.
  980. For details of keybindings, do `\\[describe-function] iswitchb'."
  981. (interactive)
  982. (setq iswitchb-method iswitchb-default-method)
  983. (iswitchb))
  984. (defun iswitchb-buffer-other-window ()
  985. "Switch to another buffer and show it in another window.
  986. The buffer name is selected interactively by typing a substring.
  987. For details of keybindings, do `\\[describe-function] iswitchb'."
  988. (interactive)
  989. (setq iswitchb-method 'otherwindow)
  990. (iswitchb))
  991. (defun iswitchb-display-buffer ()
  992. "Display a buffer in another window but don't select it.
  993. The buffer name is selected interactively by typing a substring.
  994. For details of keybindings, do `\\[describe-function] iswitchb'."
  995. (interactive)
  996. (setq iswitchb-method 'display)
  997. (iswitchb))
  998. (defun iswitchb-buffer-other-frame ()
  999. "Switch to another buffer and show it in another frame.
  1000. The buffer name is selected interactively by typing a substring.
  1001. For details of keybindings, do `\\[describe-function] iswitchb'."
  1002. (interactive)
  1003. (setq iswitchb-method 'otherframe)
  1004. (iswitchb))
  1005. ;;; XEmacs hack for showing default buffer
  1006. ;; The first time we enter the minibuffer, Emacs puts up the default
  1007. ;; buffer to switch to, but XEmacs doesn't -- presumably there is a
  1008. ;; subtle difference in the two versions of post-command-hook. The
  1009. ;; default is shown for both whenever we delete all of our text
  1010. ;; though, indicating its just a problem the first time we enter the
  1011. ;; function. To solve this, we use another entry hook for emacs to
  1012. ;; show the default the first time we enter the minibuffer.
  1013. (defun iswitchb-init-XEmacs-trick ()
  1014. "Display default buffer when first entering minibuffer.
  1015. This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'."
  1016. (if (iswitchb-entryfn-p)
  1017. (progn
  1018. (iswitchb-exhibit)
  1019. (goto-char (point-min)))))
  1020. ;; add this hook for XEmacs only.
  1021. (if (featurep 'xemacs)
  1022. (add-hook 'iswitchb-minibuffer-setup-hook
  1023. 'iswitchb-init-XEmacs-trick))
  1024. ;;; XEmacs / backspace key
  1025. ;; For some reason, if the backspace key is pressed in XEmacs, the
  1026. ;; line gets confused, so I've added a simple key definition to make
  1027. ;; backspace act like the normal delete key.
  1028. (defun iswitchb-xemacs-backspacekey ()
  1029. "Bind backspace to `backward-delete-char'."
  1030. (define-key iswitchb-mode-map '[backspace] 'backward-delete-char)
  1031. (define-key iswitchb-mode-map '[(meta backspace)] 'backward-kill-word))
  1032. (if (featurep 'xemacs)
  1033. (add-hook 'iswitchb-define-mode-map-hook
  1034. 'iswitchb-xemacs-backspacekey))
  1035. ;;; ICOMPLETE TYPE CODE
  1036. (defun iswitchb-exhibit ()
  1037. "Find matching buffers and display a list in the minibuffer.
  1038. Copied from `icomplete-exhibit' with two changes:
  1039. 1. It prints a default buffer name when there is no text yet entered.
  1040. 2. It calls my completion routine rather than the standard completion."
  1041. (if iswitchb-use-mycompletion
  1042. (let ((contents (buffer-substring (minibuffer-prompt-end) (point-max)))
  1043. (buffer-undo-list t))
  1044. (save-excursion
  1045. (goto-char (point-max))
  1046. ; Register the end of input, so we
  1047. ; know where the extra stuff
  1048. ; (match-status info) begins:
  1049. (if (not (boundp 'iswitchb-eoinput))
  1050. ;; In case it got wiped out by major mode business:
  1051. (make-local-variable 'iswitchb-eoinput))
  1052. (setq iswitchb-eoinput (point))
  1053. ;; Update the list of matches
  1054. (setq iswitchb-text contents)
  1055. (iswitchb-set-matches)
  1056. (setq iswitchb-rescan t)
  1057. (iswitchb-set-common-completion)
  1058. ;; Insert the match-status information:
  1059. (insert (iswitchb-completions
  1060. contents))))))
  1061. (defvar most-len)
  1062. (defvar most-is-exact)
  1063. (defun iswitchb-output-completion (com)
  1064. (if (= (length com) most-len)
  1065. ;; Most is one exact match,
  1066. ;; note that and leave out
  1067. ;; for later indication:
  1068. (ignore
  1069. (setq most-is-exact t))
  1070. (substring com most-len)))
  1071. (defun iswitchb-completions (name)
  1072. "Return the string that is displayed after the user's text.
  1073. Modified from `icomplete-completions'."
  1074. (let ((comps iswitchb-matches)
  1075. ; "-determined" - only one candidate
  1076. (open-bracket-determined "[")
  1077. (close-bracket-determined "]")
  1078. ;"-prospects" - more than one candidate
  1079. (open-bracket-prospects "{")
  1080. (close-bracket-prospects "}")
  1081. first)
  1082. (if (and iswitchb-use-faces comps)
  1083. (progn
  1084. (setq first (car comps))
  1085. (setq first (format "%s" first))
  1086. (put-text-property 0 (length first) 'face
  1087. (if (= (length comps) 1)
  1088. (if iswitchb-invalid-regexp
  1089. 'iswitchb-invalid-regexp
  1090. 'iswitchb-single-match)
  1091. 'iswitchb-current-match)
  1092. first)
  1093. (setq comps (cons first (cdr comps)))))
  1094. ;; If no buffers matched, and virtual buffers are being used, then
  1095. ;; consult the list of past visited files, to see if we can find
  1096. ;; the file which the user might thought was still open.
  1097. (when (and iswitchb-use-virtual-buffers (null comps)
  1098. recentf-list)
  1099. (setq iswitchb-virtual-buffers nil)
  1100. (let ((head recentf-list) name)
  1101. (while head
  1102. (if (and (setq name (file-name-nondirectory (car head)))
  1103. (string-match (if iswitchb-regexp
  1104. iswitchb-text
  1105. (regexp-quote iswitchb-text)) name)
  1106. (null (get-file-buffer (car head)))
  1107. (not (assoc name iswitchb-virtual-buffers))
  1108. (not (iswitchb-ignore-buffername-p name))
  1109. (file-exists-p (car head)))
  1110. (setq iswitchb-virtual-buffers
  1111. (cons (cons name (car head))
  1112. iswitchb-virtual-buffers)))
  1113. (setq head (cdr head)))
  1114. (setq iswitchb-virtual-buffers (nreverse iswitchb-virtual-buffers)
  1115. comps (mapcar 'car iswitchb-virtual-buffers))
  1116. (let ((comp comps))
  1117. (while comp
  1118. (put-text-property 0 (length (car comp))
  1119. 'face 'iswitchb-virtual-matches
  1120. (car comp))
  1121. (setq comp (cdr comp))))))
  1122. (cond ((null comps) (format " %sNo match%s"
  1123. open-bracket-determined
  1124. close-bracket-determined))
  1125. (iswitchb-invalid-regexp
  1126. (concat " " (car comps)))
  1127. ((null (cdr comps)) ;one match
  1128. (concat
  1129. (if (if (not iswitchb-regexp)
  1130. (= (length name)
  1131. (length (car comps)))
  1132. (string-match name (car comps))
  1133. (string-equal (match-string 0 (car comps))
  1134. (car comps)))
  1135. ""
  1136. (concat open-bracket-determined
  1137. ;; when there is one match, show the
  1138. ;; matching buffer name in full
  1139. (car comps)
  1140. close-bracket-determined))
  1141. (if (not iswitchb-use-faces) " [Matched]")))
  1142. (t ;multiple matches
  1143. (if (and iswitchb-max-to-show
  1144. (> (length comps) iswitchb-max-to-show))
  1145. (setq comps
  1146. (append
  1147. (let ((res nil)
  1148. (comp comps)
  1149. (end (/ iswitchb-max-to-show 2)))
  1150. (while (>= (setq end (1- end)) 0)
  1151. (setq res (cons (car comp) res)
  1152. comp (cdr comp)))
  1153. (nreverse res))
  1154. (list "...")
  1155. (nthcdr (- (length comps)
  1156. (/ iswitchb-max-to-show 2)) comps))))
  1157. (let* (
  1158. ;;(most (try-completion name candidates predicate))
  1159. (most nil)
  1160. (most-len (length most))
  1161. most-is-exact
  1162. (alternatives
  1163. (mapconcat (if most 'iswitchb-output-completion
  1164. 'identity) comps iswitchb-delim)))
  1165. (concat
  1166. ;; put in common completion item -- what you get by
  1167. ;; pressing tab
  1168. (if (and (stringp iswitchb-common-match-string)
  1169. (> (length iswitchb-common-match-string) (length name)))
  1170. (concat open-bracket-determined
  1171. (substring iswitchb-common-match-string
  1172. (length name))
  1173. close-bracket-determined))
  1174. ;; end of partial matches...
  1175. ;; think this bit can be ignored.
  1176. (and (> most-len (length name))
  1177. (concat open-bracket-determined
  1178. (substring most (length name))
  1179. close-bracket-determined))
  1180. ;; list all alternatives
  1181. open-bracket-prospects
  1182. (if most-is-exact
  1183. (concat iswitchb-delim alternatives)
  1184. alternatives)
  1185. close-bracket-prospects))))))
  1186. (defun iswitchb-minibuffer-setup ()
  1187. "Set up minibuffer for `iswitchb-buffer'.
  1188. Copied from `icomplete-minibuffer-setup-hook'."
  1189. (when (iswitchb-entryfn-p)
  1190. (set (make-local-variable 'iswitchb-use-mycompletion) t)
  1191. (add-hook 'pre-command-hook 'iswitchb-pre-command nil t)
  1192. (add-hook 'post-command-hook 'iswitchb-post-command nil t)
  1193. (run-hooks 'iswitchb-minibuffer-setup-hook)))
  1194. (defun iswitchb-pre-command ()
  1195. "Run before command in `iswitchb-buffer'."
  1196. (iswitchb-tidy))
  1197. (defun iswitchb-post-command ()
  1198. "Run after command in `iswitchb-buffer'."
  1199. (iswitchb-exhibit))
  1200. (defun iswitchb-tidy ()
  1201. "Remove completions display, if any, prior to new user input.
  1202. Copied from `icomplete-tidy'."
  1203. (if (and (boundp 'iswitchb-eoinput)
  1204. iswitchb-eoinput)
  1205. (if (> iswitchb-eoinput (point-max))
  1206. ;; Oops, got rug pulled out from under us - reinit:
  1207. (setq iswitchb-eoinput (point-max))
  1208. (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
  1209. (delete-region iswitchb-eoinput (point-max))))
  1210. ;; Reestablish the local variable 'cause minibuffer-setup is weird:
  1211. (make-local-variable 'iswitchb-eoinput)
  1212. (setq iswitchb-eoinput 1)))
  1213. (defun iswitchb-entryfn-p ()
  1214. "Return non-nil if we are using `iswitchb-buffer'."
  1215. (eq iswitchb-minibuf-depth (minibuffer-depth)))
  1216. (defun iswitchb-summaries-to-end ()
  1217. "Move the summaries to the end of the list.
  1218. This is an example function which can be hooked on to
  1219. `iswitchb-make-buflist-hook'. Any buffer matching the regexps
  1220. `Summary' or `output\*$'are put to the end of the list."
  1221. (let ((summaries (delq nil
  1222. (mapcar
  1223. (lambda (x)
  1224. (if (string-match "Summary\\|output\\*$" x)
  1225. x))
  1226. iswitchb-temp-buflist))))
  1227. (iswitchb-to-end summaries)))
  1228. (defun iswitchb-case ()
  1229. "Return non-nil if we should ignore case when matching.
  1230. See the variable `iswitchb-case' for details."
  1231. (if iswitchb-case
  1232. (if (featurep 'xemacs)
  1233. (isearch-no-upper-case-p iswitchb-text)
  1234. (isearch-no-upper-case-p iswitchb-text t))))
  1235. ;;;###autoload
  1236. (define-minor-mode iswitchb-mode
  1237. "Toggle Iswitchb mode.
  1238. With a prefix argument ARG, enable Iswitchb mode if ARG is
  1239. positive, and disable it otherwise. If called from Lisp, enable
  1240. the mode if ARG is omitted or nil.
  1241. Iswitchb mode is a global minor mode that enables switching
  1242. between buffers using substrings. See `iswitchb' for details."
  1243. nil nil iswitchb-global-map :global t :group 'iswitchb
  1244. (if iswitchb-mode
  1245. (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
  1246. (remove-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)))
  1247. (provide 'iswitchb)
  1248. ;;; iswitchb.el ends here