isearchb.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ;;; isearchb --- a marriage between iswitchb and isearch
  2. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@gnu.org>
  4. ;; Maintainer: FSF
  5. ;; Created: 16 Apr 2004
  6. ;; Version: 1.5
  7. ;; Keywords: lisp
  8. ;; X-URL: http://www.newartisans.com/johnw/emacs.html
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; This module allows you to switch to buffers even faster than with
  22. ;; iswitchb! It is not intended to replace it, however, as it works
  23. ;; well only with buffers whose names don't typically overlap. You'll
  24. ;; have to try it first, and see how your mileage varies.
  25. ;;
  26. ;; The first way to use isearchb is by holding down a modifier key, in
  27. ;; which case every letter you type while holding it searches for any
  28. ;; buffer matching what you're typing (using the same ordering scheme
  29. ;; employed by iswitchb). To use it this way, add to your .emacs:
  30. ;;
  31. ;; (isearchb-set-keybindings 'super) ; s-x s-y s-z now finds "xyz"
  32. ;;
  33. ;; The other way is by using a command that puts you into "search"
  34. ;; mode, just like with isearch. I use C-z for this. The binding in
  35. ;; my .emacs looks like:
  36. ;;
  37. ;; (define-key global-map [(control ?z)] 'isearchb-activate)
  38. ;;
  39. ;; Now, after pressing C-z (for example), each self-inserting
  40. ;; character thereafter will search for a buffer containing those
  41. ;; characters. For instance, typing "C-z xyz" will switch to the
  42. ;; first buffer containing "xyz". Once you press a non-self-inserting
  43. ;; character (such as any control key sequence), the search will end.
  44. ;;
  45. ;; C-z after C-z toggles between the previously selected buffer and
  46. ;; the current one.
  47. ;;
  48. ;; C-g aborts the search and returns you to your original buffer.
  49. ;;
  50. ;; TAB, after typing in a few characters (after C-z), will jump into
  51. ;; iswitchb, using the prefix you've typed so far. This is handy when
  52. ;; you realize that isearchb is not powerful enough to find the buffer
  53. ;; you're looking for.
  54. ;;
  55. ;; C-s and C-r move forward and backward in the buffer list. If
  56. ;; `isearchb-show-completions' is non-nil (the default), the list of
  57. ;; possible completions is shown in the minibuffer.
  58. ;;
  59. ;; If `isearchb-idle-timeout' is set to a number, isearchb will quit
  60. ;; after that many seconds of idle time. I recommend trying it set to
  61. ;; one or two seconds. Then, if you switch to a buffer and wait for
  62. ;; that amount of time, you can start typing without manually exiting
  63. ;; isearchb.
  64. ;; TODO:
  65. ;; C-z C-z is broken
  66. ;; killing iswitchb.el and then trying to switch back is broken
  67. ;; make sure TAB isn't broken
  68. (require 'iswitchb)
  69. (defgroup isearchb nil
  70. "Switch between buffers using a mechanism like isearch."
  71. :group 'iswitchb)
  72. (defcustom isearchb-idle-timeout nil
  73. "Number of idle seconds before isearchb turns itself off.
  74. If nil, don't use a timeout."
  75. :type '(choice (integer :tag "Seconds")
  76. (const :tag "Disable" nil))
  77. :group 'isearchb)
  78. (defcustom isearchb-show-completions t
  79. "If non-nil, show possible completions in the minibuffer."
  80. :type 'boolean
  81. :group 'isearchb)
  82. (defvar isearchb-start-buffer nil)
  83. (defvar isearchb-last-buffer nil)
  84. (defvar isearchb-idle-timer nil)
  85. (defun isearchb-stop (&optional return-to-buffer ignore-command)
  86. "Called by isearchb to terminate a search in progress."
  87. (remove-hook 'pre-command-hook 'isearchb-follow-char)
  88. (if return-to-buffer
  89. (switch-to-buffer isearchb-start-buffer)
  90. (setq isearchb-last-buffer isearchb-start-buffer))
  91. (when isearchb-idle-timer
  92. (cancel-timer isearchb-idle-timer)
  93. (setq isearchb-idle-timer nil))
  94. (if ignore-command
  95. (setq this-command 'ignore
  96. last-command 'ignore))
  97. (message nil))
  98. (defun isearchb-iswitchb ()
  99. "isearchb's custom version of the `iswitchb' command.
  100. Its purpose is to pass different call arguments to
  101. `iswitchb-read-buffer'."
  102. (interactive)
  103. (let* ((prompt "iswitch ")
  104. (iswitchb-method 'samewindow)
  105. (buf (iswitchb-read-buffer prompt nil nil iswitchb-text t)))
  106. (if (eq iswitchb-exit 'findfile)
  107. (call-interactively 'find-file)
  108. (when buf
  109. (if (get-buffer buf)
  110. ;; buffer exists, so view it and then exit
  111. (iswitchb-visit-buffer buf)
  112. ;; else buffer doesn't exist
  113. (iswitchb-possible-new-buffer buf))))))
  114. (defun isearchb ()
  115. "Switch to buffer matching a substring, based on chars typed."
  116. (interactive)
  117. (unless (eq last-command 'isearchb)
  118. (setq iswitchb-text nil))
  119. (unless iswitchb-text
  120. (setq iswitchb-text "")
  121. (iswitchb-make-buflist nil))
  122. (if last-command-event
  123. (setq iswitchb-rescan t
  124. iswitchb-text (concat iswitchb-text
  125. (char-to-string last-command-event))))
  126. (iswitchb-set-matches)
  127. (let* ((match (car iswitchb-matches))
  128. (buf (and match (get-buffer match))))
  129. (if (null buf)
  130. (progn
  131. (isearchb-stop t)
  132. (isearchb-iswitchb))
  133. (switch-to-buffer buf)
  134. (if isearchb-show-completions
  135. (message "isearchb: %s%s" iswitchb-text
  136. (iswitchb-completions iswitchb-text))
  137. (if (= 1 (length iswitchb-matches))
  138. (message "isearchb: %s (only match)" iswitchb-text)
  139. (message "isearchb: %s" iswitchb-text))))))
  140. (defun isearchb-set-keybindings (modifier)
  141. "Setup isearchb on the given MODIFIER."
  142. (dotimes (i 128)
  143. (if (eq 'self-insert-command
  144. (lookup-key global-map (vector i)))
  145. (define-key global-map (vector (list modifier i)) 'isearchb))))
  146. (defun isearchb-follow-char ()
  147. "Function added to `post-command-hook' to handle the isearchb \"mode\"."
  148. (let (keys)
  149. (if (not (and (memq last-command '(isearchb isearchb-activate))
  150. (setq keys (this-command-keys))
  151. (= 1 (length keys))))
  152. (isearchb-stop)
  153. (cond
  154. ((or (equal keys "\C-h") (equal keys "\C-?")
  155. (equal keys [backspace]) (equal keys [delete]))
  156. (setq iswitchb-text
  157. (substring iswitchb-text 0 (1- (length iswitchb-text))))
  158. (if (= 0 (length iswitchb-text))
  159. (isearchb-stop t t)
  160. (setq last-command-event nil)
  161. (setq this-command 'isearchb)))
  162. ((or (equal keys "\C-i") (equal keys [tab]))
  163. (setq this-command 'isearchb-iswitchb))
  164. ((equal keys "\C-s")
  165. (iswitchb-next-match)
  166. (setq last-command-event nil)
  167. (setq this-command 'isearchb))
  168. ((equal keys "\C-r")
  169. (iswitchb-prev-match)
  170. (setq last-command-event nil)
  171. (setq this-command 'isearchb))
  172. ((equal keys "\C-g")
  173. (ding)
  174. (isearchb-stop t t))
  175. ((eq (lookup-key global-map keys) 'self-insert-command)
  176. (setq this-command 'isearchb)))
  177. (if (and isearchb-idle-timeout
  178. (null isearchb-idle-timer))
  179. (setq isearchb-idle-timer
  180. (run-with-idle-timer isearchb-idle-timeout nil
  181. 'isearchb-stop))))))
  182. ;;;###autoload
  183. (defun isearchb-activate ()
  184. "Active isearchb mode for subsequent alphanumeric keystrokes.
  185. Executing this command again will terminate the search; or, if
  186. the search has not yet begun, will toggle to the last buffer
  187. accessed via isearchb."
  188. (interactive)
  189. (cond
  190. ((eq last-command 'isearchb)
  191. (isearchb-stop nil t))
  192. ((eq last-command 'isearchb-activate)
  193. (if isearchb-last-buffer
  194. (switch-to-buffer isearchb-last-buffer)
  195. (error "isearchb: There is no previous buffer to toggle to"))
  196. (isearchb-stop nil t))
  197. (t
  198. (message "isearchb: ")
  199. (setq iswitchb-text nil
  200. isearchb-start-buffer (current-buffer))
  201. (add-hook 'pre-command-hook 'isearchb-follow-char))))
  202. (provide 'isearchb)
  203. ;;; isearchb.el ends here