]> code.delx.au - gnu-emacs/blobdiff - lisp/isearch.el
Fix typos.
[gnu-emacs] / lisp / isearch.el
index 5aadac4a3b1e2695f8a033adad20f63ccc28ca45..977de6ac4b013213598a91566aa5bd0aefba75a3 100644 (file)
@@ -464,7 +464,8 @@ This is like `describe-bindings', but displays only Isearch keys."
     (define-key map    "\C-w" 'isearch-yank-word-or-char)
     (define-key map "\M-\C-w" 'isearch-del-char)
     (define-key map "\M-\C-y" 'isearch-yank-char)
-    (define-key map    "\C-y" 'isearch-yank-line)
+    (define-key map    "\C-y" 'isearch-yank-kill)
+    (define-key map "\M-s\C-e" 'isearch-yank-line)
 
     (define-key map (char-to-string help-char) isearch-help-map)
     (define-key map [help] isearch-help-map)
@@ -472,7 +473,7 @@ This is like `describe-bindings', but displays only Isearch keys."
 
     (define-key map "\M-n" 'isearch-ring-advance)
     (define-key map "\M-p" 'isearch-ring-retreat)
-    (define-key map "\M-y" 'isearch-yank-kill)
+    (define-key map "\M-y" 'isearch-yank-pop)
 
     (define-key map "\M-\t" 'isearch-complete)
 
@@ -541,14 +542,15 @@ Each set is a vector of the form:
 (defvar isearch-string "")  ; The current search string.
 (defvar isearch-message "") ; text-char-description version of isearch-string
 
-(defvar isearch-message-prefix-add nil) ; Additonal text for the message prefix
-(defvar isearch-message-suffix-add nil) ; Additonal text for the message suffix
+(defvar isearch-message-prefix-add nil) ; Additional text for the message prefix
+(defvar isearch-message-suffix-add nil) ; Additional text for the message suffix
 
 (defvar isearch-success t)     ; Searching is currently successful.
 (defvar isearch-error nil)     ; Error message for failed search.
 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
 (defvar isearch-wrapped nil)   ; Searching restarted from the top (bottom).
-(defvar isearch-barrier 0)
+(defvar isearch-barrier 0
+  "Recorded minimum/maximal point for the current search.")
 (defvar isearch-just-started nil)
 (defvar isearch-start-hscroll 0)       ; hscroll when starting the search.
 
@@ -636,6 +638,8 @@ Type \\[isearch-yank-char] to yank char from buffer onto end of search\
 Type \\[isearch-yank-line] to yank rest of line onto end of search string\
  and search for it.
 Type \\[isearch-yank-kill] to yank the last string of killed text.
+Type \\[isearch-yank-pop] to replace string just yanked into search prompt
+ with string killed before it.
 Type \\[isearch-quote-char] to quote control character to search for it.
 \\[isearch-abort] while searching or when search has failed cancels input\
  back to what has
@@ -948,7 +952,7 @@ NOPUSH is t and EDIT is t."
       (or (and transient-mark-mode mark-active)
          (progn
            (push-mark isearch-opoint t)
-           (or executing-kbd-macro (> (minibuffer-depth) 0)
+           (or executing-kbd-macro (> (minibuffer-depth) 0) edit
                (message "Mark saved where search started")))))
 
   (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
@@ -1057,6 +1061,26 @@ nonincremental search instead via `isearch-edit-string'."
   (isearch-done)
   (isearch-clean-overlays))
 
+(defvar minibuffer-history-symbol) ;; from external package gmhist.el
+
+(defun isearch-fail-pos (&optional msg)
+  "Return position of first mismatch in search string, or nil if none.
+If MSG is non-nil, use `isearch-message', otherwise `isearch-string'."
+  (let ((cmds isearch-cmds)
+       (curr-msg (if msg isearch-message isearch-string))
+       succ-msg)
+    (when (or (not isearch-success) isearch-error)
+      (while (or (not (isearch-success-state (car cmds)))
+                 (isearch-error-state (car cmds)))
+        (pop cmds))
+      (setq succ-msg (and cmds (if msg (isearch-message-state (car cmds))
+                                (isearch-string-state (car cmds)))))
+      (if (and (stringp succ-msg)
+              (< (length succ-msg) (length curr-msg))
+              (equal succ-msg
+                     (substring curr-msg 0 (length succ-msg))))
+         (length succ-msg)
+       0))))
 
 (defun isearch-edit-string ()
   "Edit the search string in the minibuffer.
@@ -1066,9 +1090,7 @@ The following additional command keys are active while editing.
 \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
 \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
-\\[isearch-complete-edit] to complete the search string using the search ring.
-\\<isearch-mode-map>
-If first char entered is \\[isearch-yank-word-or-char], then do word search instead."
+\\[isearch-complete-edit] to complete the search string using the search ring."
 
   ;; This code is very hairy for several reasons, explained in the code.
   ;; Mainly, isearch-mode must be terminated while editing and then restarted.
@@ -1076,7 +1098,7 @@ If first char entered is \\[isearch-yank-word-or-char], then do word search inst
   ;; this could be simplified greatly.
   ;; Editing doesn't back up the search point.  Should it?
   (interactive)
-  (condition-case err
+  (condition-case nil
       (progn
        (let ((isearch-nonincremental isearch-nonincremental)
 
@@ -1088,6 +1110,7 @@ If first char entered is \\[isearch-yank-word-or-char], then do word search inst
              (isearch-new-message isearch-message)
              (isearch-new-forward isearch-forward)
              (isearch-new-word isearch-word)
+             (isearch-new-case-fold isearch-case-fold-search)
 
              (isearch-regexp isearch-regexp)
              (isearch-op-fun isearch-op-fun)
@@ -1111,6 +1134,14 @@ If first char entered is \\[isearch-yank-word-or-char], then do word search inst
              ;; Save current configuration so we can restore it here.
              (isearch-window-configuration (current-window-configuration))
 
+             ;; This could protect the index of the search rings,
+             ;; but we can't reliably count the number of typed M-p
+             ;; in `read-from-minibuffer' to adjust the index accordingly.
+             ;; So when the following is commented out, `isearch-mode'
+             ;; below resets the index to the predictable value nil.
+             ;; (search-ring-yank-pointer search-ring-yank-pointer)
+             ;; (regexp-search-ring-yank-pointer regexp-search-ring-yank-pointer)
+
              ;; Temporarily restore `minibuffer-message-timeout'.
              (minibuffer-message-timeout
               isearch-original-minibuffer-message-timeout)
@@ -1121,7 +1152,7 @@ If first char entered is \\[isearch-yank-word-or-char], then do word search inst
          ;; Actually terminate isearching until editing is done.
          ;; This is so that the user can do anything without failure,
          ;; like switch buffers and start another isearch, and return.
-         (condition-case err
+         (condition-case nil
              (isearch-done t t)
            (exit nil))                 ; was recursive editing
 
@@ -1131,13 +1162,19 @@ If first char entered is \\[isearch-yank-word-or-char], then do word search inst
 
          (unwind-protect
              (let* ((message-log-max nil)
+                    ;; Protect global value of search rings from updating
+                    ;; by `read-from-minibuffer'.  It should be updated only
+                    ;; by `isearch-update-ring' in `isearch-done', not here.
+                    (search-ring search-ring)
+                    (regexp-search-ring regexp-search-ring)
                     ;; Binding minibuffer-history-symbol to nil is a work-around
                     ;; for some incompatibility with gmhist.
                     (minibuffer-history-symbol))
                (setq isearch-new-string
                       (read-from-minibuffer
                        (isearch-message-prefix nil nil isearch-nonincremental)
-                       isearch-string
+                      (cons isearch-string (1+ (or (isearch-fail-pos)
+                                                   (length isearch-string))))
                        minibuffer-local-isearch-map nil
                        (if isearch-regexp
                           (cons 'regexp-search-ring
@@ -1167,22 +1204,21 @@ If first char entered is \\[isearch-yank-word-or-char], then do word search inst
            (setq isearch-string isearch-new-string
                  isearch-message isearch-new-message
                  isearch-forward isearch-new-forward
-                 isearch-word isearch-new-word))
+                 isearch-word isearch-new-word
+                 isearch-case-fold-search isearch-new-case-fold))
 
          ;; Empty isearch-string means use default.
-         (if (= 0 (length isearch-string))
-             (setq isearch-string (or (car (if isearch-regexp
-                                               regexp-search-ring
-                                             search-ring))
-                                      "")
-
-                   isearch-message
-                   (mapconcat 'isearch-text-char-description
-                              isearch-string ""))
-           ;; This used to set the last search string,
-           ;; but I think it is not right to do that here.
-           ;; Only the string actually used should be saved.
-           ))
+         (when (= 0 (length isearch-string))
+           (setq isearch-string (or (car (if isearch-regexp
+                                             regexp-search-ring
+                                           search-ring))
+                                    "")
+
+                 isearch-message
+                 (mapconcat 'isearch-text-char-description
+                            isearch-string ""))
+           ;; After taking the last element, adjust ring to previous one.
+           (isearch-ring-adjust1 nil)))
 
        ;; This used to push the state as of before this C-s, but it adds
        ;; an inconsistent state where part of variables are from the
@@ -1269,7 +1305,9 @@ Use `isearch-exit' to quit without signaling."
                  isearch-message
                  (mapconcat 'isearch-text-char-description
                             isearch-string "")
-                 isearch-case-fold-search isearch-last-case-fold-search))
+                 isearch-case-fold-search isearch-last-case-fold-search)
+           ;; After taking the last element, adjust ring to previous one.
+           (isearch-ring-adjust1 nil))
        ;; If already have what to search for, repeat it.
        (or isearch-success
            (progn
@@ -1347,8 +1385,8 @@ Use `isearch-exit' to quit without signaling."
   "Start `query-replace' with string to replace from last search string.
 The arg DELIMITED (prefix arg if interactive), if non-nil, means replace
 only matches surrounded by word boundaries.  Note that using the prefix arg
-is possible only when `isearch-allow-scroll' is non-nil, and it don't
-always provides the correct matches for `query-replace', so the preferred
+is possible only when `isearch-allow-scroll' is non-nil, and it doesn't
+always provide the correct matches for `query-replace', so the preferred
 way to run word replacements from Isearch is `M-s w ... M-%'."
   (interactive
    (list current-prefix-arg))
@@ -1411,9 +1449,10 @@ string.  NLINES has the same meaning as in `occur'."
      (t (regexp-quote isearch-string)))
     (if current-prefix-arg (prefix-numeric-value current-prefix-arg))))
   (let ((case-fold-search isearch-case-fold-search)
-       ;; set `search-upper-case' to nil to not call
-       ;; `isearch-no-upper-case-p' in `occur-1'
-       (search-upper-case nil))
+       ;; Set `search-upper-case' to nil to not call
+       ;; `isearch-no-upper-case-p' in `occur-1'.
+       (search-upper-case nil)
+       (search-spaces-regexp (if isearch-regexp search-whitespace-regexp)))
     (occur regexp nlines)))
 
 (declare-function hi-lock-read-face-name "hi-lock" ())
@@ -1495,6 +1534,18 @@ If search string is empty, just beep."
   (interactive)
   (isearch-yank-string (current-kill 0)))
 
+(defun isearch-yank-pop ()
+  "Replace just-yanked search string with previously killed string."
+  (interactive)
+  (if (not (memq last-command '(isearch-yank-kill isearch-yank-pop)))
+      ;; Fall back on `isearch-yank-kill' for the benefits of people
+      ;; who are used to the old behavior of `M-y' in isearch mode. In
+      ;; future, this fallback may be changed if we ever change
+      ;; `yank-pop' to do something like the kill-ring-browser.
+      (isearch-yank-kill)
+    (isearch-pop-state)
+    (isearch-yank-string (current-kill 1))))
+
 (defun isearch-yank-x-selection ()
   "Pull current X selection into search string."
   (interactive)
@@ -1753,9 +1804,13 @@ Scroll-bar or mode-line events are processed appropriately."
 ;; Commands which change the window layout
 (put 'delete-other-windows 'isearch-scroll t)
 (put 'balance-windows 'isearch-scroll t)
+(put 'split-window-right 'isearch-scroll t)
+(put 'split-window-below 'isearch-scroll t)
+(put 'enlarge-window 'isearch-scroll t)
+
+;; Aliases for split-window-*
 (put 'split-window-vertically 'isearch-scroll t)
 (put 'split-window-horizontally 'isearch-scroll t)
-(put 'enlarge-window 'isearch-scroll t)
 
 ;; Universal argument commands
 (put 'universal-argument 'isearch-scroll t)
@@ -1873,6 +1928,7 @@ Isearch mode."
           (if (lookup-key global-map key)
               (progn
                 (isearch-done)
+                (setq prefix-arg arg)
                 (apply 'isearch-unread keylist))
             (setq keylist
                   (listify-key-sequence (lookup-key local-function-key-map key)))
@@ -1888,6 +1944,7 @@ Isearch mode."
                     (setq keylist (cdr keylist)))
                 ;; As the remaining keys in KEYLIST can't be handled
                 ;; here, we must reread them.
+                (setq prefix-arg arg)
                 (apply 'isearch-unread keylist)
                 (setq keylist nil)))))
          (
@@ -1910,8 +1967,10 @@ Isearch mode."
                               isearch-other-control-char)))))
           (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
           (cancel-kbd-macro-events)
+          (setq prefix-arg arg)
           (apply 'isearch-unread keylist))
          ((eq search-exit-option 'edit)
+          (setq prefix-arg arg)
           (apply 'isearch-unread keylist)
           (isearch-edit-string))
           ;; Handle a scrolling function.
@@ -1940,6 +1999,7 @@ Isearch mode."
           (isearch-edit-string))
          (search-exit-option
           (let (window)
+            (setq prefix-arg arg)
              (isearch-unread-key-sequence keylist)
              (setq main-event (car unread-command-events))
 
@@ -2038,7 +2098,7 @@ Isearch mode."
        ()
       (set yank-pointer-name
           (setq yank-pointer
-                (mod (+ (or yank-pointer 0)
+                (mod (+ (or yank-pointer (if advance 0 -1))
                         (if advance -1 1))
                      length)))
       (setq isearch-string (nth yank-pointer ring)
@@ -2124,22 +2184,11 @@ If there is no completion possible, say so and continue searching."
   ;; Generate and print the message string.
   (let ((cursor-in-echo-area ellipsis)
        (m isearch-message)
-       (cmds isearch-cmds)
-       succ-msg)
-    (when (or (not isearch-success) isearch-error)
-      ;; Highlight failed part
-      (while (or (not (isearch-success-state (car cmds)))
-                (isearch-error-state (car cmds)))
-       (pop cmds))
-      (setq succ-msg (and cmds (isearch-message-state (car cmds)))
-           m (copy-sequence m))
-      (add-text-properties
-       (if (and (stringp succ-msg)
-               (< (length succ-msg) (length m))
-               (equal succ-msg (substring m 0 (length succ-msg))))
-          (length succ-msg)
-        0)
-       (length m) '(face isearch-fail) m)
+       (fail-pos (isearch-fail-pos t)))
+    ;; Highlight failed part
+    (when fail-pos
+      (setq m (copy-sequence m))
+      (add-text-properties fail-pos (length m) '(face isearch-fail) m)
       ;; Highlight failed trailing whitespace
       (when (string-match " +$" m)
        (add-text-properties (match-beginning 0) (match-end 0)
@@ -2150,7 +2199,7 @@ If there is no completion possible, say so and continue searching."
             (isearch-message-suffix c-q-hack ellipsis)))
     (if c-q-hack m (let ((message-log-max nil)) (message "%s" m)))))
 
-(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
+(defun isearch-message-prefix (&optional _c-q-hack ellipsis nonincremental)
   ;; If about to search, and previous search regexp was invalid,
   ;; check that it still is.  If it is valid now,
   ;; let the message we display while searching say that it is valid.
@@ -2183,7 +2232,7 @@ If there is no completion possible, say so and continue searching."
     (propertize (concat (upcase (substring m 0 1)) (substring m 1))
                'face 'minibuffer-prompt)))
 
-(defun isearch-message-suffix (&optional c-q-hack ellipsis)
+(defun isearch-message-suffix (&optional c-q-hack _ellipsis)
   (concat (if c-q-hack "^Q" "")
          (if isearch-error
              (concat " [" isearch-error "]")
@@ -2194,10 +2243,13 @@ If there is no completion possible, say so and continue searching."
 ;; Searching
 
 (defvar isearch-search-fun-function nil
-  "Override `isearch-search-fun'.
-This function should return the search function for Isearch to use.
-It will call this function with three arguments
-as if it were `search-forward'.")
+  "Overrides the default `isearch-search-fun' behavior.
+This variable's value should be a function, which will be called
+with no arguments, and should return a function that takes three
+arguments: STRING, BOUND, and NOERROR.
+
+This returned function will be used by `isearch-search-string' to
+search for the first occurrence of STRING or its translation.")
 
 (defun isearch-search-fun ()
   "Return the function to use for the search.
@@ -2418,14 +2470,8 @@ update the match data, and return point."
        ;; If the following character is currently invisible,
        ;; skip all characters with that same `invisible' property value.
        ;; Do that over and over.
-       (while (and (< (point) end)
-                   (let ((prop
-                          (get-char-property (point) 'invisible)))
-                     (if (eq buffer-invisibility-spec t)
-                         prop
-                       (or (memq prop buffer-invisibility-spec)
-                           (assq prop buffer-invisibility-spec)))))
-         (if (get-text-property (point) 'invisible)
+       (while (and (< (point) end) (invisible-p (point)))
+         (if (invisible-p (get-text-property (point) 'invisible))
              (progn
                (goto-char (next-single-property-change (point) 'invisible
                                                        nil end))
@@ -2440,10 +2486,7 @@ update the match data, and return point."
                (while overlays
                  (setq o (car overlays)
                        invis-prop (overlay-get o 'invisible))
-                 (if (if (eq buffer-invisibility-spec t)
-                         invis-prop
-                       (or (memq invis-prop buffer-invisibility-spec)
-                           (assq invis-prop buffer-invisibility-spec)))
+                 (if (invisible-p invis-prop)
                      (if (overlay-get o 'isearch-open-invisible)
                          (setq ov-list (cons o ov-list))
                        ;; We found one overlay that cannot be
@@ -2578,6 +2621,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-word nil)
 (defvar isearch-lazy-highlight-forward nil)
 (defvar isearch-lazy-highlight-error nil)
 
@@ -2616,6 +2660,8 @@ by other Emacs features."
                          isearch-case-fold-search))
                 (not (eq isearch-lazy-highlight-regexp
                          isearch-regexp))
+                (not (eq isearch-lazy-highlight-word
+                         isearch-word))
                  (not (= (window-start)
                          isearch-lazy-highlight-window-start))
                  (not (= (window-end)   ; Window may have been split/joined.
@@ -2636,11 +2682,12 @@ by other Emacs features."
             isearch-lazy-highlight-window-end   (window-end)
             isearch-lazy-highlight-start        (point)
             isearch-lazy-highlight-end          (point)
+            isearch-lazy-highlight-wrapped      nil
             isearch-lazy-highlight-last-string  isearch-string
            isearch-lazy-highlight-case-fold-search isearch-case-fold-search
-           isearch-lazy-highlight-regexp       isearch-regexp
-            isearch-lazy-highlight-wrapped      nil
+           isearch-lazy-highlight-regexp       isearch-regexp
            isearch-lazy-highlight-space-regexp search-whitespace-regexp
+           isearch-lazy-highlight-word         isearch-word
            isearch-lazy-highlight-forward      isearch-forward)
       (unless (equal isearch-string "")
        (setq isearch-lazy-highlight-timer
@@ -2654,6 +2701,7 @@ Attempt to do the search exactly the way the pending Isearch would."
       (let ((case-fold-search isearch-lazy-highlight-case-fold-search)
            (isearch-regexp isearch-lazy-highlight-regexp)
            (search-spaces-regexp isearch-lazy-highlight-space-regexp)
+           (isearch-word isearch-lazy-highlight-word)
            (search-invisible nil)      ; don't match invisible text
            (retry t)
            (success nil)