(tibetan-pre-write-conversion): Cancel previous
[bpt/emacs.git] / lisp / isearch.el
index a921799..aef204c 100644 (file)
@@ -140,7 +140,7 @@ apply to chars in regexps that are prefixed with `\\'.
 If this value is `not-yanks', yanked text is always downcased."
   :type '(choice (const :tag "off" nil)
                 (const not-yanks)
-                (sexp :tag "on" :format "%t\n" t))
+                (other :tag "on" t))
   :group 'isearch)
 
 (defcustom search-nonincremental-instead t
@@ -152,7 +152,10 @@ string, and RET terminates editing and does a nonincremental search."
 
 (defcustom search-whitespace-regexp "\\s-+"
   "*If non-nil, regular expression to match a sequence of whitespace chars.
-You might want to use something like \"[ \\t\\r\\n]+\" instead."
+This applies to regular expression incremental search.
+You might want to use something like \"[ \\t\\r\\n]+\" instead.
+In the Customization buffer, that is `[' followed by a space,
+a tab, a carriage return (control-M), a newline, and `]+'."
   :type 'regexp
   :group 'isearch)
 
@@ -174,11 +177,11 @@ an overlay having an `invisible' property and that overlay has a property
   :group 'isearch)
 
 (defcustom isearch-hide-immediately t
-  "If t hide the previous match if needed.  
-This has efect iff `search-invisible' is set to `open' and it means
-that if the current match is out of one of the previously shown
-regions hide is right away, as opposed to hiding it at the end of
-isearch."  
+  "If non-nil, re-hide an invisible match right away.
+This variable makes a difference when `search-invisible' is set to `open'.
+It means that after search makes some invisible text visible
+to show the match, it makes the text invisible again when the match moves.
+Ordinarily the text becomes invisible again at the end of the search."  
   :type 'boolean 
   :group 'isearch)
 
@@ -228,13 +231,12 @@ Default value, nil, means edit the string instead."
       (or (vectorp (nth 1 map))
          (char-table-p (nth 1 map))
          (error "The initialization of isearch-mode-map must be updated"))
-      ;; Make Latin-1, Latin-2, Latin-3 and Latin-4 characters
-      ;; search for themselves.
-      (aset (nth 1 map) (make-char 'latin-iso8859-1) 'isearch-printing-char)
-      (aset (nth 1 map) (make-char 'latin-iso8859-2) 'isearch-printing-char)
-      (aset (nth 1 map) (make-char 'latin-iso8859-3) 'isearch-printing-char)
-      (aset (nth 1 map) (make-char 'latin-iso8859-4) 'isearch-printing-char)
-      (aset (nth 1 map) (make-char 'latin-iso8859-9) 'isearch-printing-char)
+      ;; Make all multibyte characters search for themselves.
+      (let ((l (generic-character-list))
+           (table (nth 1 map)))
+       (while l
+         (set-char-table-default table (car l) 'isearch-printing-char)
+         (setq l (cdr l))))
       ;; Make function keys, etc, exit the search.
       (define-key map [t] 'isearch-other-control-char)
       ;; Control chars, by default, end isearch mode transparently.
@@ -245,9 +247,9 @@ Default value, nil, means edit the string instead."
        (define-key map (make-string 1 i) 'isearch-other-control-char)
        (setq i (1+ i)))
 
-      ;; Printing chars extend the search string by default.
+      ;; Single-byte printing chars extend the search string by default.
       (setq i ?\ )
-      (while (< i (length (nth 1 map)))
+      (while (< i 256)
        (define-key map (vector i) 'isearch-printing-char)
        (setq i (1+ i)))
 
@@ -278,6 +280,7 @@ Default value, nil, means edit the string instead."
       (define-key map "\C-j" 'isearch-printing-char)
       (define-key map "\t" 'isearch-printing-char)
       (define-key map " " 'isearch-whitespace-chars)
+      (define-key map [?\S-\ ] 'isearch-whitespace-chars)
     
       (define-key map "\C-w" 'isearch-yank-word)
       (define-key map "\C-y" 'isearch-yank-line)
@@ -369,9 +372,6 @@ Default value, nil, means edit the string instead."
 ;; Flag to indicate a yank occurred, so don't move the cursor.
 (defvar isearch-yank-flag nil)
 
-;; Flag to indicate that we are searching multibyte characaters.
-(defvar isearch-multibyte-characters-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'.
@@ -389,6 +389,13 @@ Default value, nil, means edit the string instead."
 ;; Accumulate here the overlays opened during searching.
 (defvar isearch-opened-overlays nil)
 
+;; The value of input-method-function when isearch is invoked.
+(defvar isearch-input-method-function nil)
+
+;; A flag to tell if input-method-function is locally bound when
+;; isearch is invoked.
+(defvar isearch-input-method-local-p nil)
+
 ;; Minor-mode-alist changes - kind of redundant with the
 ;; echo area, but if isearching in multiple windows, it can be useful.
 
@@ -426,7 +433,8 @@ Type \\[isearch-yank-word] to yank word from buffer onto end of search\
  string and search for it.
 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-kill] to yank last killed text onto end of search string\
+ and search for 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
@@ -521,13 +529,21 @@ is treated as a regexp.  See \\[isearch-forward] for more info."
        isearch-other-end nil
        isearch-small-window nil
        isearch-just-started t
-       isearch-multibyte-characters-flag nil
-       isearch-input-method nil
 
        isearch-opoint (point)
        search-ring-yank-pointer nil
        isearch-opened-overlays nil
+       isearch-input-method-function input-method-function
+       isearch-input-method-local-p (local-variable-p 'input-method-function)
        regexp-search-ring-yank-pointer nil)
+
+  ;; We must bypass input method while reading key.  When a user type
+  ;; printable character, appropriate input method is turned on in
+  ;; minibuffer to read multibyte charactes.
+  (or isearch-input-method-local-p
+      (make-local-variable 'input-method-function))
+  (setq input-method-function nil)
+
   (looking-at "")
   (setq isearch-window-configuration
        (if isearch-slow-terminal-mode (current-window-configuration) nil))
@@ -625,6 +641,10 @@ is treated as a regexp.  See \\[isearch-forward] for more info."
                  (message "Mark saved where search started"))))))
 
   (setq isearch-mode nil)
+  (if isearch-input-method-local-p
+      (setq input-method-function isearch-input-method-function)
+    (kill-local-variable 'input-method-function))
+
   (force-mode-line-update)
 
   (if (and (> (length isearch-string) 0) (not nopush))
@@ -947,55 +967,53 @@ If no previous match was done, just beep."
   (isearch-update))
 
 
-(defun isearch-yank (chunk)
-  ;; Helper for isearch-yank-word and isearch-yank-line
-  ;; CHUNK should be word, line, kill, or x-sel.
-  (let ((string (cond
-                 ((eq chunk 'kill)
-                  (current-kill 0))
-                 ((eq chunk 'x-sel)
-                  (x-get-selection))
-                 (t
-                 (save-excursion
-                   (and (not isearch-forward) isearch-other-end
-                        (goto-char isearch-other-end))
-                   (buffer-substring
-                    (point)
-                    (save-excursion
-                      (cond
-                       ((eq chunk 'word)
-                        (forward-word 1))
-                       ((eq chunk 'line)
-                        (end-of-line)))
-                      (point))))))))
-    ;; Downcase the string if not supposed to case-fold yanked strings.
-    (if (and isearch-case-fold-search
-            (eq 'not-yanks search-upper-case))
-       (setq string (downcase string)))
-    (if isearch-regexp (setq string (regexp-quote string)))
-    (setq isearch-string (concat isearch-string string)
-         isearch-message
-         (concat isearch-message
-                 (mapconcat 'isearch-text-char-description
-                            string ""))
-         ;; Don't move cursor in reverse search.
-         isearch-yank-flag t))
+(defun isearch-yank-string (string)
+  "Pull STRING into search string."
+  ;; Downcase the string if not supposed to case-fold yanked strings.
+  (if (and isearch-case-fold-search
+          (eq 'not-yanks search-upper-case))
+      (setq string (downcase string)))
+  (if isearch-regexp (setq string (regexp-quote string)))
+  (setq isearch-string (concat isearch-string string)
+       isearch-message
+       (concat isearch-message
+               (mapconcat 'isearch-text-char-description
+                          string ""))
+       ;; Don't move cursor in reverse search.
+       isearch-yank-flag t)
   (isearch-search-and-update))
 
 (defun isearch-yank-kill ()
   "Pull string from kill ring into search string."
   (interactive)
-  (isearch-yank 'kill))
+  (isearch-yank-string (current-kill 0)))
+
+(defun isearch-yank-x-selection ()
+  "Pull current X selection into search string.
+Some users like to put this command on Mouse-2.
+To do that, evaluate these expressions:
+    (define-key isearch-mode-map [down-mouse-2] nil)
+    (define-key isearch-mode-map [mouse-2] 'isearch-yank-x-selection)"
+  (interactive)
+  (isearch-yank-string (x-get-selection)))
 
 (defun isearch-yank-word ()
   "Pull next word from buffer into search string."
   (interactive)
-  (isearch-yank 'word))
+  (isearch-yank-string
+   (save-excursion
+     (and (not isearch-forward) isearch-other-end
+         (goto-char isearch-other-end))
+     (buffer-substring (point) (progn (forward-word 1) (point))))))
 
 (defun isearch-yank-line ()
   "Pull rest of line from buffer into search string."
   (interactive)
-  (isearch-yank 'line))
+  (isearch-yank-string
+   (save-excursion
+     (and (not isearch-forward) isearch-other-end
+         (goto-char isearch-other-end))
+     (buffer-substring (point) (line-end-position)))))
 
 
 (defun isearch-search-and-update ()
@@ -1014,12 +1032,15 @@ If no previous match was done, just beep."
     (if (and (not isearch-forward) (not isearch-adjusted)
             (condition-case ()
                 (let ((case-fold-search isearch-case-fold-search))
+                  (if (and (eq case-fold-search t) search-upper-case)
+                      (setq case-fold-search
+                            (isearch-no-upper-case-p isearch-string isearch-regexp)))
                   (looking-at (if isearch-regexp isearch-string
                                 (regexp-quote isearch-string))))
               (error nil))
-              (or isearch-yank-flag
-                  (<= (match-end 0) 
-                      (min isearch-opoint isearch-barrier))))
+            (or isearch-yank-flag
+                (<= (match-end 0) 
+                    (min isearch-opoint isearch-barrier))))
        (progn
          (setq isearch-success t 
                isearch-invalid-regexp nil
@@ -1051,20 +1072,23 @@ If no previous match was done, just beep."
   "Handle * and ? specially in regexps."
   (interactive)
   (if isearch-regexp 
-
-      (progn
-       (setq isearch-adjusted t)
-       ;; Get the isearch-other-end from before the last search.
-       ;; We want to start from there,
-       ;; so that we don't retreat farther than that.
-       ;; (car isearch-cmds) is after last search;
-       ;; (car (cdr isearch-cmds)) is from before it.
-       (let ((cs (nth 5 (car (cdr isearch-cmds)))))
-         (setq cs (or cs isearch-barrier))
-         (goto-char
-          (if isearch-forward
-              (max cs isearch-barrier)
-            (min cs isearch-barrier))))))
+      (let ((idx (length isearch-string)))
+       (while (and (> idx 0)
+                   (eq (aref isearch-string (1- idx)) ?\\))
+         (setq idx (1- idx)))
+       (when (= (mod (- (length isearch-string) idx) 2) 0)
+         (setq isearch-adjusted t)
+         ;; Get the isearch-other-end from before the last search.
+         ;; We want to start from there,
+         ;; so that we don't retreat farther than that.
+         ;; (car isearch-cmds) is after last search;
+         ;; (car (cdr isearch-cmds)) is from before it.
+         (let ((cs (nth 5 (car (cdr isearch-cmds)))))
+           (setq cs (or cs isearch-barrier))
+           (goto-char
+            (if isearch-forward
+                (max cs isearch-barrier)
+              (min cs isearch-barrier)))))))
   (isearch-process-search-char (isearch-last-command-char)))
   
 
@@ -1149,7 +1173,9 @@ and the meta character is unread so that it applies to editing the string."
             ;; is in isearch mode.  So end the search in that buffer.
             (if (and (listp main-event)
                      (setq window (posn-window (event-start main-event)))
-                     (windowp window))
+                     (windowp window)
+                     (or (> (minibuffer-depth) 0)
+                         (not (window-minibuffer-p window))))
                 (save-excursion
                   (set-buffer (window-buffer window))
                   (isearch-done)
@@ -1162,8 +1188,15 @@ and the meta character is unread so that it applies to editing the string."
 (defun isearch-quote-char ()
   "Quote special characters for incremental search."
   (interactive)
-  (isearch-process-search-char (+ (read-quoted-char (isearch-message t))
-                                 nonascii-insert-offset)))
+  (let ((char (read-quoted-char (isearch-message t))))
+    ;; Assume character codes 0200 - 0377 stand for 
+    ;; European characters in Latin-1, and convert them
+    ;; to Emacs characters.
+    (and enable-multibyte-characters
+        (>= char ?\200)
+        (<= char ?\377)
+        (setq char (+ char nonascii-insert-offset)))
+    (isearch-process-search-char char)))
 
 (defun isearch-return-char ()
   "Convert return into newline for incremental search.
@@ -1174,9 +1207,16 @@ Obsolete."
 (defun isearch-printing-char ()
   "Add this ordinary printing character to the search string and search."
   (interactive)
-  (if isearch-input-method
-      (isearch-process-search-multibyte-characters (isearch-last-command-char))
-    (isearch-process-search-char (isearch-last-command-char))))
+  (let ((char (isearch-last-command-char)))
+    (if (= char ?\S-\ )
+       (setq char ?\ ))
+    (if (and enable-multibyte-characters
+            (>= char ?\200)
+            (<= char ?\377))
+       (isearch-process-search-char (+ char nonascii-insert-offset))
+      (if current-input-method
+         (isearch-process-search-multibyte-characters char)
+       (isearch-process-search-char char)))))
 
 (defun isearch-whitespace-chars ()
   "Match all whitespace chars, if in regexp mode.
@@ -1410,8 +1450,8 @@ If there is no completion possible, say so and continue searching."
                   (if isearch-regexp "regexp " "")
                   (if nonincremental "search" "I-search")
                   (if isearch-forward "" " backward")
-                  (if isearch-input-method
-                      (concat " [" isearch-input-method-title "]: ")
+                  (if current-input-method
+                      (concat " [" current-input-method-title "]: ")
                     ": ")
                   )))
     (aset m 0 (upcase (aref m 0)))
@@ -1605,8 +1645,10 @@ If there is no completion possible, say so and continue searching."
                   (while overlays
                     (setq o (car overlays)
                           invis-prop (overlay-get o 'invisible))
-                    (if (or (memq invis-prop buffer-invisibility-spec)
-                            (assq invis-prop buffer-invisibility-spec))
+                    (if (if (eq buffer-invisibility-spec t)
+                            invis-prop
+                          (or (memq invis-prop buffer-invisibility-spec)
+                              (assq invis-prop buffer-invisibility-spec)))
                         (if (overlay-get o 'isearch-open-invisible)
                             (setq ov-list (cons o ov-list))
                           ;; We found one overlay that cannot be
@@ -1618,8 +1660,8 @@ If there is no completion possible, say so and continue searching."
                       ;; It makes sense to append to the open
                       ;; overlays list only if we know that this is
                       ;; t.
-                      (setq crt-overlays (append ov-list crt-overlays))))
-                (goto-char (next-overlay-change (point))))))
+                      (setq crt-overlays (append ov-list crt-overlays)))))
+              (goto-char (next-overlay-change (point)))))
         ;; See if invisibility reaches up thru END.
         (if (>= (point) end)
             (if (and (not (null can-be-opened)) (consp crt-overlays))