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