]> code.delx.au - gnu-emacs/blobdiff - lisp/isearch.el
Fix race conditions with MS-Windows lock files by using _sopen.
[gnu-emacs] / lisp / isearch.el
index 3799376701372a32de9bc2cecbfabe8005d81b9d..b36b250738afd072be3e18aa0a63756a9535e21d 100644 (file)
@@ -1,6 +1,6 @@
 ;;; isearch.el --- incremental search minor mode
 
-;; Copyright (C) 1992-1997, 1999-201 Free Software Foundation, Inc.
+;; Copyright (C) 1992-1997, 1999-2013 Free Software Foundation, Inc.
 
 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
 ;; Maintainer: FSF
@@ -176,7 +176,7 @@ is non-nil if the user quits the search.")
 
 (defvar isearch-message-function nil
   "Function to call to display the search prompt.
-If nil, use `isearch-message'.")
+If nil, use function `isearch-message'.")
 
 (defvar isearch-wrap-function nil
   "Function to call to wrap the search when search is failed.
@@ -374,10 +374,12 @@ but outside of this help window when you type them in Isearch mode,
 they exit Isearch mode before displaying global help."
   isearch-help-map)
 
+(defvar isearch--display-help-action '(nil (inhibit-same-window . t)))
+
 (defun isearch-help-for-help ()
   "Display Isearch help menu."
   (interactive)
-  (let (same-window-buffer-names same-window-regexps)
+  (let ((display-buffer-overriding-action isearch--display-help-action))
     (isearch-help-for-help-internal))
   (isearch-update))
 
@@ -385,7 +387,7 @@ they exit Isearch mode before displaying global help."
   "Show a list of all keys defined in Isearch mode, and their definitions.
 This is like `describe-bindings', but displays only Isearch keys."
   (interactive)
-  (let (same-window-buffer-names same-window-regexps)
+  (let ((display-buffer-overriding-action isearch--display-help-action))
     (with-help-window "*Help*"
       (with-current-buffer standard-output
        (princ "Isearch Mode Bindings:\n")
@@ -394,14 +396,14 @@ This is like `describe-bindings', but displays only Isearch keys."
 (defun isearch-describe-key ()
   "Display documentation of the function invoked by isearch key."
   (interactive)
-  (let (same-window-buffer-names same-window-regexps)
+  (let ((display-buffer-overriding-action isearch--display-help-action))
     (call-interactively 'describe-key))
   (isearch-update))
 
 (defun isearch-describe-mode ()
   "Display documentation of Isearch mode."
   (interactive)
-  (let (same-window-buffer-names same-window-regexps)
+  (let ((display-buffer-overriding-action isearch--display-help-action))
     (describe-function 'isearch-forward))
   (isearch-update))
 
@@ -518,7 +520,7 @@ This is like `describe-bindings', but displays only Isearch keys."
     (define-key map "\C-x" nil)
     (define-key map [?\C-x t] 'isearch-other-control-char)
     (define-key map "\C-x8" nil)
-    (define-key map "\C-x8\r" 'isearch-other-control-char)
+    (define-key map "\C-x8\r" 'isearch-insert-char-by-name)
 
     map)
   "Keymap for `isearch-mode'.")
@@ -1099,7 +1101,7 @@ nonincremental search instead via `isearch-edit-string'."
 
 (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'."
+If MSG is non-nil, use variable `isearch-message', otherwise `isearch-string'."
   (let ((cmds isearch-cmds)
        (curr-msg (if msg isearch-message isearch-string))
        succ-msg)
@@ -1116,23 +1118,17 @@ If MSG is non-nil, use `isearch-message', otherwise `isearch-string'."
          (length succ-msg)
        0))))
 
-(defun isearch-edit-string ()
-  "Edit the search string in the minibuffer.
-The following additional command keys are active while editing.
-\\<minibuffer-local-isearch-map>
-\\[exit-minibuffer] to resume incremental searching with the edited string.
-\\[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."
-
+(defmacro with-isearch-suspended (&rest body)
+  "Exit Isearch mode, run BODY, and reinvoke the pending search.
+You can update the global isearch variables by setting new values to
+`isearch-new-string', `isearch-new-message', `isearch-new-forward',
+`isearch-new-word', `isearch-new-case-fold'."
   ;; This code is very hairy for several reasons, explained in the code.
   ;; Mainly, isearch-mode must be terminated while editing and then restarted.
   ;; If there were a way to catch any change of buffer from the minibuffer,
   ;; this could be simplified greatly.
   ;; Editing doesn't back up the search point.  Should it?
-  (interactive)
-  (condition-case nil
+  `(condition-case nil
       (progn
        (let ((isearch-nonincremental isearch-nonincremental)
 
@@ -1195,29 +1191,7 @@ The following additional command keys are active while editing.
          (setq old-point (point) old-other-end isearch-other-end)
 
          (unwind-protect
-             (let* ((message-log-max nil)
-                    ;; Don't add a new search string to the search ring here
-                    ;; in `read-from-minibuffer'. It should be added only
-                    ;; by `isearch-update-ring' called from `isearch-done'.
-                    (history-add-new-input nil)
-                    ;; 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 isearch-nonincremental)
-                      (cons isearch-string (1+ (or (isearch-fail-pos)
-                                                   (length isearch-string))))
-                       minibuffer-local-isearch-map nil
-                       (if isearch-regexp
-                          (cons 'regexp-search-ring
-                                (1+ (or regexp-search-ring-yank-pointer -1)))
-                        (cons 'search-ring
-                              (1+ (or search-ring-yank-pointer -1))))
-                       nil t)
-                     isearch-new-message
-                     (mapconcat 'isearch-text-char-description
-                                isearch-new-string "")))
+             (progn ,@body)
 
            ;; Set point at the start (end) of old match if forward (backward),
            ;; so after exiting minibuffer isearch resumes at the start (end)
@@ -1276,6 +1250,41 @@ The following additional command keys are active while editing.
      (isearch-abort)  ;; outside of let to restore outside global values
      )))
 
+(defun isearch-edit-string ()
+  "Edit the search string in the minibuffer.
+The following additional command keys are active while editing.
+\\<minibuffer-local-isearch-map>
+\\[exit-minibuffer] to resume incremental searching with the edited string.
+\\[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."
+  (interactive)
+  (with-isearch-suspended
+   (let* ((message-log-max nil)
+         ;; Don't add a new search string to the search ring here
+         ;; in `read-from-minibuffer'. It should be added only
+         ;; by `isearch-update-ring' called from `isearch-done'.
+         (history-add-new-input nil)
+         ;; 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 isearch-nonincremental)
+           (cons isearch-string (1+ (or (isearch-fail-pos)
+                                        (length isearch-string))))
+           minibuffer-local-isearch-map nil
+           (if isearch-regexp
+               (cons 'regexp-search-ring
+                     (1+ (or regexp-search-ring-yank-pointer -1)))
+             (cons 'search-ring
+                   (1+ (or search-ring-yank-pointer -1))))
+           nil t)
+          isearch-new-message
+          (mapconcat 'isearch-text-char-description
+                     isearch-new-string "")))))
+
 (defun isearch-nonincremental-exit-minibuffer ()
   (interactive)
   (setq isearch-nonincremental t)
@@ -1705,6 +1714,9 @@ and reads its face argument using `hi-lock-read-face-name'."
 \f
 (defun isearch-delete-char ()
   "Discard last input item and move point back.
+Last input means the last character or the last isearch command
+that added or deleted characters from the search string,
+moved point, toggled regexp mode or case-sensitivity, etc.
 If no previous match was done, just beep."
   (interactive)
   (if (null (cdr isearch-cmds))
@@ -1714,6 +1726,8 @@ If no previous match was done, just beep."
 
 (defun isearch-del-char (&optional arg)
   "Delete character from end of search string and search again.
+Unlike `isearch-delete-char', it only deletes the last character,
+but doesn't cancel the effect of other isearch command.
 If search string is empty, just beep."
   (interactive "p")
   (if (= 0 (length isearch-string))
@@ -1834,6 +1848,17 @@ Subword is used when `subword-mode' is activated. "
    (lambda () (let ((inhibit-field-text-motion t))
                (line-end-position (if (eolp) 2 1))))))
 
+(defun isearch-insert-char-by-name ()
+  "Read a character by its Unicode name and insert it into search string."
+  (interactive)
+  (with-isearch-suspended
+   (let ((char (read-char-by-name "Insert character (Unicode name or hex): ")))
+     (when char
+       (setq isearch-new-string (concat isearch-string (string char))
+            isearch-new-message (concat isearch-message
+                                        (mapconcat 'isearch-text-char-description
+                                                   (string char) "")))))))
+
 (defun isearch-search-and-update ()
   ;; Do the search and update the display.
   (when (or isearch-success
@@ -2149,7 +2174,8 @@ Isearch mode."
                 (setq prefix-arg arg)
                 (apply 'isearch-unread keylist))
             (setq keylist
-                  (listify-key-sequence (lookup-key local-function-key-map key)))
+                  (listify-key-sequence
+                   (lookup-key local-function-key-map key)))
             (while keylist
               (setq key (car keylist))
               ;; If KEY is a printing char, we handle it here
@@ -2158,6 +2184,9 @@ Isearch mode."
               (if (and (integerp key)
                        (>= key ?\s) (/= key 127) (< key 256))
                   (progn
+                    ;; Ensure that the processed char is recorded in
+                    ;; the keyboard macro, if any (Bug#4894)
+                    (store-kbd-macro-event key)
                     (isearch-process-search-char key)
                     (setq keylist (cdr keylist)))
                 ;; As the remaining keys in KEYLIST can't be handled
@@ -2491,6 +2520,7 @@ Can be changed via `isearch-search-fun-function' for special needs."
       ;; the user adds and removes characters in the search string
       ;; (or when using nonincremental word isearch)
       (let ((lax (not (or isearch-nonincremental
+                         (null (car isearch-cmds))
                          (eq (length isearch-string)
                              (length (isearch--state-string
                                        (car isearch-cmds))))))))
@@ -2931,8 +2961,15 @@ by other Emacs features."
     (setq isearch-lazy-highlight-window       (selected-window)
          isearch-lazy-highlight-window-start (window-start)
          isearch-lazy-highlight-window-end   (window-end)
-         isearch-lazy-highlight-start        (point)
-         isearch-lazy-highlight-end          (point)
+         ;; Start lazy-highlighting at the beginning of the found
+         ;; match (`isearch-other-end').  If no match, use point.
+         ;; One of the next two variables (depending on search direction)
+         ;; is used to define the starting position of lazy-highlighting
+         ;; and also to remember the current position of point between
+         ;; calls of `isearch-lazy-highlight-update', and another variable
+         ;; is used to define where the wrapped search must stop.
+         isearch-lazy-highlight-start        (or isearch-other-end (point))
+         isearch-lazy-highlight-end          (or isearch-other-end (point))
          isearch-lazy-highlight-wrapped      nil
          isearch-lazy-highlight-last-string  isearch-string
          isearch-lazy-highlight-case-fold-search isearch-case-fold-search
@@ -3030,6 +3067,9 @@ 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))))
+                     ;; Remember the current position of point for
+                     ;; the next call of `isearch-lazy-highlight-update'
+                     ;; when `lazy-highlight-max-at-a-time' is too small.
                      (if isearch-lazy-highlight-forward
                          (setq isearch-lazy-highlight-end (point))
                        (setq isearch-lazy-highlight-start (point)))))