(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-char
(setq iswitchb-rescan t
iswitchb-text (concat iswitchb-text
(char-to-string last-command-char))))
(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-char 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-char nil)
(setq this-command 'isearchb))
((equal keys "\C-r")
(iswitchb-prev-match)
(setq last-command-char 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)