;;; Code:
\f
-;;; Some additional options and constants.
+;; Some additional options and constants.
(defgroup isearch nil
"Incremental search minor mode."
(defvar isearch-mode-end-hook nil
"Function(s) to call after terminating an incremental search.")
-;;; Search ring.
+;; Search ring.
(defvar search-ring nil
"List of search string sequences.")
:type 'boolean
:group 'isearch)
-;;; Define isearch-mode keymap.
+;; Define isearch-mode keymap.
(defvar isearch-mode-map
(let* ((i 0)
(define-key map "?" 'isearch-*-char)
(define-key map "|" 'isearch-|-char)
-;;; Turned off because I find I expect to get the global definition--rms.
-;;; ;; Instead bind C-h to special help command for isearch-mode.
-;;; (define-key map "\C-h" 'isearch-mode-help)
+ ;; Turned off because I find I expect to get the global definition--rms.
+ ;; ;; Instead bind C-h to special help command for isearch-mode.
+ ;; (define-key map "\C-h" 'isearch-mode-help)
(define-key map "\M-n" 'isearch-ring-advance)
(define-key map "\M-p" 'isearch-ring-retreat)
(defvar isearch-regexp nil) ; Searching for a regexp.
(defvar isearch-word nil) ; Searching for words.
-(defvar isearch-cmds nil) ; Stack of search status sets.
+(defvar isearch-cmds nil
+ "Stack of search status sets.
+Each set is a list of the form:
+ (STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
+ INVALID-REGEXP WRAPPED BARRIER WITHIN-BRACKETS CASE-FOLD-SEARCH)")
+
(defvar isearch-string "") ; The current search string.
(defvar isearch-message "") ; text-char-description version of isearch-string
(defvar isearch-adjusted nil)
(defvar isearch-slow-terminal-mode nil)
-;;; If t, using a small window.
+;; If t, using a small window.
(defvar isearch-small-window nil)
(defvar isearch-opoint 0)
-;;; The window configuration active at the beginning of the search.
+;; The window configuration active at the beginning of the search.
(defvar isearch-window-configuration nil)
;; Flag to indicate a yank occurred, so don't move the cursor.
(defvar isearch-yank-flag nil)
-;;; A function to be called after each input character is processed.
-;;; (It is not called after characters that exit the search.)
-;;; It is only set from an optional argument to `isearch-mode'.
+;; A function to be called after each input character is processed.
+;; (It is not called after characters that exit the search.)
+;; It is only set from an optional argument to `isearch-mode'.
(defvar isearch-op-fun nil)
-;;; Is isearch-mode in a recursive edit for modal searching.
+;; Is isearch-mode in a recursive edit for modal searching.
(defvar isearch-recursive-edit nil)
-;;; Should isearch be terminated after doing one search?
+;; Should isearch be terminated after doing one search?
(defvar isearch-nonincremental nil)
;; New value of isearch-forward after isearch-edit-string.
(define-key global-map "\C-r" 'isearch-backward)
(define-key esc-map "\C-r" 'isearch-backward-regexp)
-;;; Entry points to isearch-mode.
+;; Entry points to isearch-mode.
(defun isearch-forward (&optional regexp-p no-recursive-edit)
"\
;; Some high level utilities. Others below.
(defun isearch-update ()
- ;; Called after each command to update the display.
+ ;; Called after each command to update the display.
(if (and (null unread-command-events)
(null executing-kbd-macro))
(progn
(if (> (length search-ring) search-ring-max)
(setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
-;;; Switching buffers should first terminate isearch-mode.
-;;; ;; For Emacs 19, the frame switch event is handled.
-;;; (defun isearch-switch-frame-handler ()
-;;; (interactive) ;; Is this necessary?
-;;; ;; First terminate isearch-mode.
-;;; (isearch-done)
-;;; (isearch-clean-overlays)
-;;; (handle-switch-frame (car (cdr last-command-char))))
+;; Switching buffers should first terminate isearch-mode.
+;; ;; For Emacs 19, the frame switch event is handled.
+;; (defun isearch-switch-frame-handler ()
+;; (interactive) ;; Is this necessary?
+;; ;; First terminate isearch-mode.
+;; (isearch-done)
+;; (isearch-clean-overlays)
+;; (handle-switch-frame (car (cdr last-command-char))))
\f
;; Commands active while inside of the isearch minor mode.
(isearch-yank-string (x-get-selection)))
-(defun isearch-mouse-2 (click arg)
+(defun isearch-mouse-2 (click)
"Handle mouse-2 in Isearch mode.
For a click in the echo area, invoke `isearch-yank-x-selection'.
Otherwise invoke whatever mouse-2 is bound to outside of Isearch."
- (interactive "e\nP")
+ (interactive "e")
(let* ((w (posn-window (event-start click)))
(overriding-terminal-local-map nil)
(key (vector (event-basic-type click)))
- (binding (key-binding key)))
+ ;; FIXME: `key-binding' should accept an event as argument
+ ;; and do all the overlay/text-properties lookup etc...
+ (binding (with-current-buffer
+ (if (window-live-p w) (window-buffer w) (current-buffer))
+ (key-binding key))))
(if (and (window-minibuffer-p w)
(not (minibuffer-window-active-p w))) ; in echo area
(isearch-yank-x-selection)
(when binding
- ;; Kluge to allow passing ARG to functions that support it,
- ;; like mouse-yank-at-click.
- (if (equal (cadr (interactive-form binding)) "e\nP")
- (funcall binding click arg)
- (funcall binding click))))))
+ (call-interactively binding)))))
(defun isearch-yank-internal (jumpform)
(defun isearch-search-and-update ()
;; Do the search and update the display.
- (if (and (not isearch-success)
- ;; unsuccessful regexp search may become
- ;; successful by addition of characters which
- ;; make isearch-string valid
- (not isearch-regexp))
- nil
+ (when (or isearch-success
+ ;; unsuccessful regexp search may become
+ ;; successful by addition of characters which
+ ;; make isearch-string valid
+ isearch-regexp)
;; In reverse search, adding stuff at
;; the end may cause zero or many more chars to be
;; matched, in the string following point.
(regexp-quote isearch-string))))
(error nil))
(or isearch-yank-flag
- (<= (match-end 0)
+ (<= (match-end 0)
(min isearch-opoint isearch-barrier))))
(progn
- (setq isearch-success t
+ (setq isearch-success t
isearch-invalid-regexp nil
isearch-within-brackets nil
isearch-other-end (match-end 0))
;; Not regexp, not reverse, or no match at point.
(if (and isearch-other-end (not isearch-adjusted))
(goto-char (if isearch-forward isearch-other-end
- (min isearch-opoint
- isearch-barrier
+ (min isearch-opoint
+ isearch-barrier
(1+ isearch-other-end)))))
(isearch-search)
))
(defun isearch-*-char ()
"Handle * and ? specially in regexps."
(interactive)
- (if isearch-regexp
+ (if isearch-regexp
(let ((idx (length isearch-string)))
(while (and (> idx 0)
(eq (aref isearch-string (1- idx)) ?\\))
(when (and (> (length key) 1)
(symbolp (aref key 0))
(listp (aref key 1))
- (not (numberp (posn-point
+ (not (numberp (posn-point
(event-start (aref key 1))))))
(pop unread-command-events)
(setq main-event (car unread-command-events)))
(defun isearch-process-search-char (char)
;; Append the char to the search string, update the message and re-search.
- (isearch-process-search-string
- (char-to-string char)
+ (isearch-process-search-string
+ (char-to-string char)
(if (>= char ?\200)
(char-to-string char)
(isearch-text-char-description char))))
"")))
\f
-;;; Searching
+;; Searching
+
+(defvar isearch-search-fun-function nil "Override `isearch-function-fun'.")
+
+(defun isearch-search-fun ()
+ "Return the function to use for the search.
+Can be changed via `isearch-search-fun-function' for special needs."
+ (if isearch-search-fun-function
+ (funcall isearch-search-fun-function)
+ (cond
+ (isearch-word
+ (if isearch-forward 'word-search-forward 'word-search-backward))
+ (isearch-regexp
+ (if isearch-forward 're-search-forward 're-search-backward))
+ (t
+ (if isearch-forward 'search-forward 'search-backward)))))
(defun isearch-search ()
;; Do the search with the current search string.
(while retry
(setq isearch-success
(funcall
- (cond (isearch-word
- (if isearch-forward
- 'word-search-forward 'word-search-backward))
- (isearch-regexp
- (if isearch-forward
- 're-search-forward 're-search-backward))
- (t
- (if isearch-forward 'search-forward 'search-backward)))
+ (isearch-search-fun)
isearch-string nil t))
;; Clear RETRY unless we matched some invisible text
;; and we aren't supposed to do that.
(quit (isearch-unread ?\C-g)
(setq isearch-success nil))
- (invalid-regexp
+ (invalid-regexp
(setq isearch-invalid-regexp (car (cdr lossage)))
(setq isearch-within-brackets (string-match "\\`Unmatched \\["
isearch-invalid-regexp))
(goto-char (nth 2 (car isearch-cmds)))))
-;;; Called when opening an overlay, and we are still in isearch.
+;; Called when opening an overlay, and we are still in isearch.
(defun isearch-open-overlay-temporary (ov)
(if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
;; Some modes would want to open the overlays temporary during
(overlay-put ov 'intangible nil)))
-;;; This is called at the end of isearch. It will open the overlays
-;;; that contain the latest match. Obviously in case of a C-g the
-;;; point returns to the original location which surely is not contain
-;;; in any of these overlays, se we are safe in this case too.
+;; This is called at the end of isearch. It will open the overlays
+;; that contain the latest match. Obviously in case of a C-g the
+;; point returns to the original location which surely is not contain
+;; in any of these overlays, se we are safe in this case too.
(defun isearch-open-necessary-overlays (ov)
- (let ((inside-overlay (and (> (point) (overlay-start ov))
+ (let ((inside-overlay (and (> (point) (overlay-start ov))
(< (point) (overlay-end ov))))
;; If this exists it means that the overlay was opened using
;; this function, not by us tweaking the overlay properties.
(if fct-temp
(funcall fct-temp ov t)))))
-;;; This is called when exiting isearch. It closes the temporary
-;;; opened overlays, except the ones that contain the latest match.
+;; This is called when exiting isearch. It closes the temporary
+;; opened overlays, except the ones that contain the latest match.
(defun isearch-clean-overlays ()
(when isearch-opened-overlays
(mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
(and (> end1 start0) (<= end1 end0))))
-;;; Verify if the current match is outside of each element of
-;;; `isearch-opened-overlays', if so close that overlay.
+;; Verify if the current match is outside of each element of
+;; `isearch-opened-overlays', if so close that overlay.
(defun isearch-close-unnecessary-overlays (begin end)
(let ((overlays isearch-opened-overlays))
t))))))
\f
-;;; Highlighting
+;; Highlighting
(defvar isearch-overlay nil)
(delete-overlay isearch-overlay)))
-;;; General utilities
+;; General utilities
(defun isearch-no-upper-case-p (string regexp-flag)
(append char-or-events unread-command-events)))
\f
-;;; isearch-lazy-highlight feature
-;;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
-
-;;; When active, *every* match for the current search string is
-;;; highlighted: the current one using the normal isearch match color
-;;; and all the others using `isearch-lazy-highlight-face'. The extra
-;;; highlighting makes it easier to anticipate where the cursor will
-;;; land each time you press C-s or C-r to repeat a pending search.
-;;; Highlighting of these additional matches happens in a deferred
-;;; fashion using "idle timers," so the cycles needed do not rob
-;;; isearch of its usual snappy response.
-
-;;; IMPLEMENTATION NOTE: This depends on some isearch internals.
-;;; Specifically:
-;;; - `isearch-update' is expected to be called (at least) every time
-;;; the search string or window-start changes;
-;;; - `isearch-string' is expected to contain the current search
-;;; string as entered by the user;
-;;; - the type of the current search is expected to be given by
-;;; `isearch-word' and `isearch-regexp';
-;;; - the direction of the current search is expected to be given by
-;;; `isearch-forward';
-;;; - the variable `isearch-invalid-regexp' is expected to be true
-;;; iff `isearch-string' is an invalid regexp.
+;; isearch-lazy-highlight feature
+;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
+
+;; When active, *every* match for the current search string is
+;; highlighted: the current one using the normal isearch match color
+;; and all the others using `isearch-lazy-highlight-face'. The extra
+;; highlighting makes it easier to anticipate where the cursor will
+;; land each time you press C-s or C-r to repeat a pending search.
+;; Highlighting of these additional matches happens in a deferred
+;; fashion using "idle timers," so the cycles needed do not rob
+;; isearch of its usual snappy response.
+
+;; IMPLEMENTATION NOTE: This depends on some isearch internals.
+;; Specifically:
+;; - `isearch-update' is expected to be called (at least) every time
+;; the search string or window-start changes;
+;; - `isearch-string' is expected to contain the current search
+;; string as entered by the user;
+;; - the type of the current search is expected to be given by
+;; `isearch-word' and `isearch-regexp';
+;; - the direction of the current search is expected to be given by
+;; `isearch-forward';
+;; - the variable `isearch-invalid-regexp' is expected to be true
+;; iff `isearch-string' is an invalid regexp.
(defgroup isearch-lazy-highlight nil
"Lazy highlighting feature for incremental search."
(defun isearch-lazy-highlight-search ()
"Search ahead for the next or previous match, for lazy highlighting.
Attempt to do the search exactly the way the pending isearch would."
- (let ((case-fold-search isearch-case-fold-search)
- (choices (cond (isearch-word
- '(word-search-forward . word-search-backward))
- (isearch-regexp
- '(re-search-forward . re-search-backward))
- (t
- '(search-forward . search-backward)))))
- (funcall (if isearch-forward
- (car choices)
- (cdr choices))
+ (let ((case-fold-search isearch-case-fold-search))
+ (funcall (isearch-search-fun)
isearch-string
(if isearch-forward
(if isearch-lazy-highlight-wrapped
isearch-message message
isearch-case-fold-search case-fold)
(isearch-search))
-
+
;;; isearch.el ends here