Fix arg usage of format-decode-run-method (Bug#7488).
[bpt/emacs.git] / lisp / isearch.el
index 9199039..3b04fa2 100644 (file)
@@ -1,7 +1,7 @@
 ;;; isearch.el --- incremental search minor mode
 
-;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
-;;   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
+;;   2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
 ;;   Free Software Foundation, Inc.
 
 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
@@ -109,7 +109,7 @@ string, and RET terminates editing and does a nonincremental search."
   :type 'boolean
   :group 'isearch)
 
-(defcustom search-whitespace-regexp "\\s-+"
+(defcustom search-whitespace-regexp (purecopy "\\s-+")
   "If non-nil, regular expression to match a sequence of whitespace chars.
 This applies to regular expression incremental search.
 When you put a space or spaces in the incremental regexp, it stands for
@@ -271,30 +271,37 @@ and `lazy-highlight-interval')."
   :group 'isearch
   :group 'matching)
 
+(define-obsolete-variable-alias 'isearch-lazy-highlight-cleanup
+                                'lazy-highlight-cleanup
+                                "22.1")
+
 (defcustom lazy-highlight-cleanup t
   "Controls whether to remove extra highlighting after a search.
 If this is nil, extra highlighting can be \"manually\" removed with
 \\[lazy-highlight-cleanup]."
   :type 'boolean
   :group 'lazy-highlight)
-(define-obsolete-variable-alias 'isearch-lazy-highlight-cleanup
-                                'lazy-highlight-cleanup
+
+(define-obsolete-variable-alias 'isearch-lazy-highlight-initial-delay
+                                'lazy-highlight-initial-delay
                                 "22.1")
 
 (defcustom lazy-highlight-initial-delay 0.25
   "Seconds to wait before beginning to lazily highlight all matches."
   :type 'number
   :group 'lazy-highlight)
-(define-obsolete-variable-alias 'isearch-lazy-highlight-initial-delay
-                                'lazy-highlight-initial-delay
+
+(define-obsolete-variable-alias 'isearch-lazy-highlight-interval
+                                'lazy-highlight-interval
                                 "22.1")
 
 (defcustom lazy-highlight-interval 0 ; 0.0625
   "Seconds between lazily highlighting successive matches."
   :type 'number
   :group 'lazy-highlight)
-(define-obsolete-variable-alias 'isearch-lazy-highlight-interval
-                                'lazy-highlight-interval
+
+(define-obsolete-variable-alias 'isearch-lazy-highlight-max-at-a-time
+                                'lazy-highlight-max-at-a-time
                                 "22.1")
 
 (defcustom lazy-highlight-max-at-a-time 20
@@ -305,9 +312,6 @@ A value of nil means highlight all matches."
   :type '(choice (const :tag "All" nil)
                 (integer :tag "Some"))
   :group 'lazy-highlight)
-(define-obsolete-variable-alias 'isearch-lazy-highlight-max-at-a-time
-                                'lazy-highlight-max-at-a-time
-                                "22.1")
 
 (defface lazy-highlight
   '((((class color) (min-colors 88) (background light))
@@ -322,11 +326,11 @@ A value of nil means highlight all matches."
   "Face for lazy highlighting of matches other than the current one."
   :group 'lazy-highlight
   :group 'basic-faces)
-(put 'isearch-lazy-highlight-face 'face-alias 'lazy-highlight)
-(defvar lazy-highlight-face 'lazy-highlight)
+(define-obsolete-face-alias 'isearch-lazy-highlight-face 'lazy-highlight "22.1")
 (define-obsolete-variable-alias 'isearch-lazy-highlight-face
                                 'lazy-highlight-face
                                 "22.1")
+(defvar lazy-highlight-face 'lazy-highlight)
 \f
 ;; Define isearch help map.
 
@@ -347,7 +351,7 @@ A value of nil means highlight all matches."
 (eval-when-compile (require 'help-macro))
 
 (make-help-screen isearch-help-for-help-internal
-  "Type a help option: [bkm] or ?"
+  (purecopy "Type a help option: [bkm] or ?")
   "You have typed %THIS-KEY%, the help character.  Type a Help option:
 \(Type \\<help-map>\\[help-quit] to exit the Help command.)
 
@@ -544,9 +548,9 @@ Each set is a vector of the form:
 (defvar isearch-just-started nil)
 (defvar isearch-start-hscroll 0)       ; hscroll when starting the search.
 
-; case-fold-search while searching.
-;   either nil, t, or 'yes.  'yes means the same as t except that mixed
-;   case in the search string is ignored.
+;; case-fold-search while searching.
+;;   either nil, t, or 'yes.  'yes means the same as t except that mixed
+;;   case in the search string is ignored.
 (defvar isearch-case-fold-search nil)
 
 (defvar isearch-last-case-fold-search nil)
@@ -798,10 +802,14 @@ It is called by the function `isearch-forward' and other related functions."
   (setq        isearch-mode " Isearch")  ;; forward? regexp?
   (force-mode-line-update)
 
-  (isearch-push-state)
-
   (setq overriding-terminal-local-map isearch-mode-map)
   (run-hooks 'isearch-mode-hook)
+
+  ;; Pushing the initial state used to be before running isearch-mode-hook,
+  ;; but a hook might set `isearch-push-state-function' used in
+  ;; `isearch-push-state' to save mode-specific initial state.  (Bug#4994)
+  (isearch-push-state)
+
   (isearch-update)
 
   (add-hook 'mouse-leave-buffer-hook 'isearch-done)
@@ -1212,10 +1220,12 @@ If first char entered is \\[isearch-yank-word-or-char], then do word search inst
 (defun isearch-cancel ()
   "Terminate the search and go back to the starting point."
   (interactive)
-  (if (functionp (isearch-pop-fun-state (car (last isearch-cmds))))
-      (funcall (isearch-pop-fun-state (car (last isearch-cmds)))
-               (car (last isearch-cmds))))
-  (goto-char isearch-opoint)
+  (if (and isearch-push-state-function isearch-cmds)
+      ;; For defined push-state function, restore the first state.
+      ;; This calls pop-state function and restores original point.
+      (let ((isearch-cmds (last isearch-cmds)))
+       (isearch-top-state))
+    (goto-char isearch-opoint))
   (isearch-done t)                      ; exit isearch
   (isearch-clean-overlays)
   (signal 'quit nil))                   ; and pass on quit signal
@@ -1401,7 +1411,6 @@ string.  NLINES has the same meaning as in `occur'."
        (search-upper-case nil))
     (occur regexp nlines)))
 
-(declare-function hi-lock-regexp-okay "hi-lock" (regexp))
 (declare-function hi-lock-read-face-name "hi-lock" ())
 
 (defun isearch-highlight-regexp ()
@@ -1523,8 +1532,7 @@ or it might return the position of the end of the line."
   (interactive "p")
   (if (eobp)
       (insert
-       (save-excursion
-         (set-buffer (cadr (buffer-list)))
+       (with-current-buffer (cadr (buffer-list))
          (buffer-substring-no-properties
           (point) (progn (forward-char arg) (point)))))
     (forward-char arg)))
@@ -1750,7 +1758,10 @@ Scroll-bar or mode-line events are processed appropriately."
 (put 'digit-argument 'isearch-scroll t)
 
 (defcustom isearch-allow-scroll nil
-  "If non-nil, scrolling commands are allowed during incremental search."
+  "Whether scrolling is allowed during incremental search.
+If non-nil, scrolling commands can be used in Isearch mode.
+However, the current match will never scroll offscreen.
+If nil, scolling commands will first cancel Isearch mode."
   :type 'boolean
   :group 'isearch)
 
@@ -1949,8 +1960,7 @@ Isearch mode."
                      (windowp window)
                      (or (> (minibuffer-depth) 0)
                          (not (window-minibuffer-p window))))
-                (save-excursion
-                  (set-buffer (window-buffer window))
+                (with-current-buffer (window-buffer window)
                   (isearch-done)
                   (isearch-clean-overlays))
               (isearch-done)
@@ -2216,14 +2226,22 @@ update the match data, and return point."
   (let* ((func (isearch-search-fun))
          (pos1 (save-excursion (funcall func string bound noerror)))
          pos2)
-    (when (and (char-table-p translation-table-for-input)
-               (multibyte-string-p string)
-               ;; Minor optimization.
-               (string-match-p "[^[:ascii:]]" string))
+    (when (and
+          ;; Avoid "obsolete" warnings for translation-table-for-input.
+          (with-no-warnings
+            (char-table-p translation-table-for-input))
+          (multibyte-string-p string)
+          ;; Minor optimization.
+          (string-match-p "[^[:ascii:]]" string))
       (let ((translated
              (apply 'string
                     (mapcar (lambda (c)
-                              (or (aref translation-table-for-input c) c))
+                              (or
+                              ;; Avoid "obsolete" warnings for
+                              ;; translation-table-for-input.
+                              (with-no-warnings
+                                (aref translation-table-for-input c))
+                              c))
                             string)))
             match-data)
         (when translated
@@ -2490,8 +2508,8 @@ since they have special meaning in a regexp."
 
 (defun isearch-text-char-description (c)
   (cond
-   ((< c ?\s) (format "^%c" (+ c 64)))
-   ((= c ?\^?) "^?")
+   ((< c ?\s) (propertize (format "^%c" (+ c 64)) 'face 'escape-glyph))
+   ((= c ?\^?) (propertize "^?" 'face 'escape-glyph))
    (t (char-to-string c))))
 
 ;; General function to unread characters or events.
@@ -2560,6 +2578,7 @@ since they have special meaning in a regexp."
 (defvar isearch-lazy-highlight-case-fold-search nil)
 (defvar isearch-lazy-highlight-regexp nil)
 (defvar isearch-lazy-highlight-space-regexp nil)
+(defvar isearch-lazy-highlight-forward nil)
 
 (defun lazy-highlight-cleanup (&optional force)
   "Stop lazy highlighting and remove extra highlighting from current buffer.
@@ -2599,7 +2618,9 @@ by other Emacs features."
                  (not (= (window-start)
                          isearch-lazy-highlight-window-start))
                  (not (= (window-end)   ; Window may have been split/joined.
-                         isearch-lazy-highlight-window-end))))
+                        isearch-lazy-highlight-window-end))
+                (not (eq isearch-forward
+                         isearch-lazy-highlight-forward))))
     ;; something important did indeed change
     (lazy-highlight-cleanup t) ;kill old loop & remove overlays
     (when (not isearch-error)
@@ -2614,7 +2635,8 @@ by other Emacs features."
            isearch-lazy-highlight-case-fold-search isearch-case-fold-search
            isearch-lazy-highlight-regexp       isearch-regexp
             isearch-lazy-highlight-wrapped      nil
-           isearch-lazy-highlight-space-regexp search-whitespace-regexp)
+           isearch-lazy-highlight-space-regexp search-whitespace-regexp
+           isearch-lazy-highlight-forward      isearch-forward)
       (unless (equal isearch-string "")
        (setq isearch-lazy-highlight-timer
              (run-with-idle-timer lazy-highlight-initial-delay nil
@@ -2630,7 +2652,8 @@ Attempt to do the search exactly the way the pending Isearch would."
            (search-invisible nil)      ; don't match invisible text
            (retry t)
            (success nil)
-           (bound (if isearch-forward
+           (isearch-forward isearch-lazy-highlight-forward)
+           (bound (if isearch-lazy-highlight-forward
                       (min (or isearch-lazy-highlight-end-limit (point-max))
                            (if isearch-lazy-highlight-wrapped
                                isearch-lazy-highlight-start
@@ -2664,7 +2687,7 @@ Attempt to do the search exactly the way the pending Isearch would."
            (select-window isearch-lazy-highlight-window))
        (save-excursion
          (save-match-data
-           (goto-char (if isearch-forward
+           (goto-char (if isearch-lazy-highlight-forward
                           isearch-lazy-highlight-end
                         isearch-lazy-highlight-start))
            (while looping
@@ -2677,7 +2700,7 @@ Attempt to do the search exactly the way the pending Isearch would."
                    (let ((mb (match-beginning 0))
                          (me (match-end 0)))
                      (if (= mb me)     ;zero-length match
-                         (if isearch-forward
+                         (if isearch-lazy-highlight-forward
                              (if (= mb (if isearch-lazy-highlight-wrapped
                                            isearch-lazy-highlight-start
                                          (window-end)))
@@ -2697,7 +2720,7 @@ Attempt to do the search exactly the way the pending Isearch would."
                          (overlay-put ov 'priority 1000)
                          (overlay-put ov 'face lazy-highlight-face)
                          (overlay-put ov 'window (selected-window))))
-                     (if isearch-forward
+                     (if isearch-lazy-highlight-forward
                          (setq isearch-lazy-highlight-end (point))
                        (setq isearch-lazy-highlight-start (point)))))
 
@@ -2707,7 +2730,7 @@ Attempt to do the search exactly the way the pending Isearch would."
                        (setq looping nil
                              nomore  t)
                      (setq isearch-lazy-highlight-wrapped t)
-                     (if isearch-forward
+                     (if isearch-lazy-highlight-forward
                          (progn
                            (setq isearch-lazy-highlight-end (window-start))
                            (goto-char (max (or isearch-lazy-highlight-start-limit (point-min))