123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- (require 'iswitchb)
- (defgroup isearchb nil
- "Switch between buffers using a mechanism like isearch."
- :group 'iswitchb)
- (defcustom isearchb-idle-timeout nil
- "Number of idle seconds before isearchb turns itself off.
- If nil, don't use a timeout."
- :type '(choice (integer :tag "Seconds")
- (const :tag "Disable" nil))
- :group 'isearchb)
- (defcustom isearchb-show-completions t
- "If non-nil, show possible completions in the minibuffer."
- :type 'boolean
- :group 'isearchb)
- (defvar isearchb-start-buffer nil)
- (defvar isearchb-last-buffer nil)
- (defvar isearchb-idle-timer nil)
- (defun isearchb-stop (&optional return-to-buffer ignore-command)
- "Called by isearchb to terminate a search in progress."
- (remove-hook 'pre-command-hook 'isearchb-follow-char)
- (if return-to-buffer
- (switch-to-buffer isearchb-start-buffer)
- (setq isearchb-last-buffer isearchb-start-buffer))
- (when isearchb-idle-timer
- (cancel-timer isearchb-idle-timer)
- (setq isearchb-idle-timer nil))
- (if ignore-command
- (setq this-command 'ignore
- last-command 'ignore))
- (message nil))
- (defun isearchb-iswitchb ()
- "isearchb's custom version of the `iswitchb' command.
- Its purpose is to pass different call arguments to
- `iswitchb-read-buffer'."
- (interactive)
- (let* ((prompt "iswitch ")
- (iswitchb-method 'samewindow)
- (buf (iswitchb-read-buffer prompt nil nil iswitchb-text t)))
- (if (eq iswitchb-exit 'findfile)
- (call-interactively 'find-file)
- (when buf
- (if (get-buffer buf)
-
- (iswitchb-visit-buffer buf)
-
- (iswitchb-possible-new-buffer buf))))))
- (defun isearchb ()
- "Switch to buffer matching a substring, based on chars typed."
- (interactive)
- (unless (eq last-command 'isearchb)
- (setq iswitchb-text nil))
- (unless iswitchb-text
- (setq iswitchb-text "")
- (iswitchb-make-buflist nil))
- (if last-command-event
- (setq iswitchb-rescan t
- iswitchb-text (concat iswitchb-text
- (char-to-string last-command-event))))
- (iswitchb-set-matches)
- (let* ((match (car iswitchb-matches))
- (buf (and match (get-buffer match))))
- (if (null buf)
- (progn
- (isearchb-stop t)
- (isearchb-iswitchb))
- (switch-to-buffer buf)
- (if isearchb-show-completions
- (message "isearchb: %s%s" iswitchb-text
- (iswitchb-completions iswitchb-text))
- (if (= 1 (length iswitchb-matches))
- (message "isearchb: %s (only match)" iswitchb-text)
- (message "isearchb: %s" iswitchb-text))))))
- (defun isearchb-set-keybindings (modifier)
- "Setup isearchb on the given MODIFIER."
- (dotimes (i 128)
- (if (eq 'self-insert-command
- (lookup-key global-map (vector i)))
- (define-key global-map (vector (list modifier i)) 'isearchb))))
- (defun isearchb-follow-char ()
- "Function added to `post-command-hook' to handle the isearchb \"mode\"."
- (let (keys)
- (if (not (and (memq last-command '(isearchb isearchb-activate))
- (setq keys (this-command-keys))
- (= 1 (length keys))))
- (isearchb-stop)
- (cond
- ((or (equal keys "\C-h") (equal keys "\C-?")
- (equal keys [backspace]) (equal keys [delete]))
- (setq iswitchb-text
- (substring iswitchb-text 0 (1- (length iswitchb-text))))
- (if (= 0 (length iswitchb-text))
- (isearchb-stop t t)
- (setq last-command-event nil)
- (setq this-command 'isearchb)))
- ((or (equal keys "\C-i") (equal keys [tab]))
- (setq this-command 'isearchb-iswitchb))
- ((equal keys "\C-s")
- (iswitchb-next-match)
- (setq last-command-event nil)
- (setq this-command 'isearchb))
- ((equal keys "\C-r")
- (iswitchb-prev-match)
- (setq last-command-event nil)
- (setq this-command 'isearchb))
- ((equal keys "\C-g")
- (ding)
- (isearchb-stop t t))
- ((eq (lookup-key global-map keys) 'self-insert-command)
- (setq this-command 'isearchb)))
- (if (and isearchb-idle-timeout
- (null isearchb-idle-timer))
- (setq isearchb-idle-timer
- (run-with-idle-timer isearchb-idle-timeout nil
- 'isearchb-stop))))))
- (defun isearchb-activate ()
- "Active isearchb mode for subsequent alphanumeric keystrokes.
- Executing this command again will terminate the search; or, if
- the search has not yet begun, will toggle to the last buffer
- accessed via isearchb."
- (interactive)
- (cond
- ((eq last-command 'isearchb)
- (isearchb-stop nil t))
- ((eq last-command 'isearchb-activate)
- (if isearchb-last-buffer
- (switch-to-buffer isearchb-last-buffer)
- (error "isearchb: There is no previous buffer to toggle to"))
- (isearchb-stop nil t))
- (t
- (message "isearchb: ")
- (setq iswitchb-text nil
- isearchb-start-buffer (current-buffer))
- (add-hook 'pre-command-hook 'isearchb-follow-char))))
- (provide 'isearchb)
|