Fix bug #16099 with failed out-of-tree builds on MS-Windows.
[bpt/emacs.git] / lisp / isearch.el
index ec4f32a..b411396 100644 (file)
@@ -187,21 +187,15 @@ or to the end of the buffer for a backward search.")
   "Function to save a function restoring the mode-specific Isearch state
 to the search status stack.")
 
-(defvar isearch-filter-predicates nil
-  "Predicates that filter the search hits that would normally be available.
-Search hits that dissatisfy the list of predicates are skipped.
-Each function in this list has two arguments: the positions of
-start and end of text matched by the search.
-The search loop uses `run-hook-with-args-until-failure' to call
-each predicate in order, and when one of the predicates returns nil,
-skips this match and continues searching for the next match.
-When the list of predicates is empty, `run-hook-with-args-until-failure'
-returns non-nil that means that the found match is accepted.
-The property `isearch-message-prefix' put on the predicate's symbol
-specifies the prefix string displayed in the search message.")
-(define-obsolete-variable-alias 'isearch-filter-predicate
-                                'isearch-filter-predicates
-                                "24.4")
+(defvar isearch-filter-predicate #'isearch-filter-visible
+  "Predicate that filter the search hits that would normally be available.
+Search hits that dissatisfy the predicate are skipped.  The function
+has two arguments: the positions of start and end of text matched by
+the search.  If this function returns nil, continue searching without
+stopping at this match.
+If you use `add-function' to modify this variable, you can use the
+`isearch-message-prefix' advice property to specify the prefix string
+displayed in the search message.")
 
 ;; Search ring.
 
@@ -355,7 +349,6 @@ A value of nil means highlight all matches."
 
 (defvar isearch-help-map
   (let ((map (make-sparse-keymap)))
-    (define-key map [t] 'isearch-other-control-char)
     (define-key map (char-to-string help-char) 'isearch-help-for-help)
     (define-key map [help] 'isearch-help-for-help)
     (define-key map [f1] 'isearch-help-for-help)
@@ -429,9 +422,6 @@ This is like `describe-bindings', but displays only Isearch keys."
     ;; Make all multibyte characters search for themselves.
     (set-char-table-range (nth 1 map) (cons #x100 (max-char))
                          'isearch-printing-char)
-    ;; Make function keys, etc, which aren't bound to a scrolling-function
-    ;; exit the search.
-    (define-key map [t] 'isearch-other-control-char)
 
     ;; Single-byte printing chars extend the search string by default.
     (setq i ?\s)
@@ -446,8 +436,7 @@ This is like `describe-bindings', but displays only Isearch keys."
     ;; default local key binding for any key not otherwise bound.
     (let ((meta-map (make-sparse-keymap)))
       (define-key map (char-to-string meta-prefix-char) meta-map)
-      (define-key map [escape] meta-map)
-      (define-key meta-map [t] 'isearch-other-meta-char))
+      (define-key map [escape] meta-map))
 
     ;; Several non-printing chars change the searching behavior.
     (define-key map "\C-s" 'isearch-repeat-forward)
@@ -527,9 +516,6 @@ This is like `describe-bindings', but displays only Isearch keys."
 
     ;; The key translations defined in the C-x 8 prefix should add
     ;; characters to the search string.  See iso-transl.el.
-    (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-char-by-name)
 
     map)
@@ -651,6 +637,8 @@ Each set is a vector of the form:
 ;; isearch is invoked.
 (defvar isearch-input-method-local-p nil)
 
+(defvar isearch--saved-overriding-local-map nil)
+
 ;; Minor-mode-alist changes - kind of redundant with the
 ;; echo area, but if isearching in multiple windows, it can be useful.
 
@@ -783,7 +771,7 @@ See the command `isearch-forward' for more information."
   (interactive "P\np")
   (isearch-mode t nil nil (not no-recursive-edit) (null not-word)))
 
-(defun isearch-forward-symbol (&optional not-symbol no-recursive-edit)
+(defun isearch-forward-symbol (&optional _not-symbol no-recursive-edit)
   "Do incremental search forward for a symbol.
 The prefix argument is currently unused.
 Like ordinary incremental search except that your input is treated
@@ -918,6 +906,9 @@ convert the search string to a regexp used by regexp search functions."
 
   (setq overriding-terminal-local-map isearch-mode-map)
   (run-hooks 'isearch-mode-hook)
+  ;; Remember the initial map possibly modified
+  ;; by external packages in isearch-mode-hook.  (Bug#16035)
+  (setq isearch--saved-overriding-local-map overriding-terminal-local-map)
 
   ;; Pushing the initial state used to be before running isearch-mode-hook,
   ;; but a hook might set `isearch-push-state-function' used in
@@ -926,6 +917,8 @@ convert the search string to a regexp used by regexp search functions."
 
   (isearch-update)
 
+  (add-hook 'pre-command-hook 'isearch-pre-command-hook)
+  (add-hook 'post-command-hook 'isearch-post-command-hook)
   (add-hook 'mouse-leave-buffer-hook 'isearch-done)
   (add-hook 'kbd-macro-termination-hook 'isearch-done)
 
@@ -1004,6 +997,8 @@ NOPUSH is t and EDIT is t."
        (unless (equal (car command-history) command)
          (setq command-history (cons command command-history)))))
 
+  (remove-hook 'pre-command-hook 'isearch-pre-command-hook)
+  (remove-hook 'post-command-hook 'isearch-post-command-hook)
   (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
   (remove-hook 'kbd-macro-termination-hook 'isearch-done)
   (setq isearch-lazy-highlight-start nil)
@@ -1015,7 +1010,7 @@ NOPUSH is t and EDIT is t."
   (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
   (isearch-dehighlight)
   (lazy-highlight-cleanup lazy-highlight-cleanup)
-  (let ((found-start (window-start (selected-window)))
+  (let ((found-start (window-start))
        (found-point (point)))
     (when isearch-window-configuration
       (set-window-configuration isearch-window-configuration)
@@ -2106,26 +2101,6 @@ to the barrier."
                         (min last-other-end isearch-barrier)))
            (setq isearch-adjusted t)))))))
 
-(defun isearch-unread-key-sequence (keylist)
-  "Unread the given key-sequence KEYLIST.
-Scroll-bar or mode-line events are processed appropriately."
-  (cancel-kbd-macro-events)
-  (apply 'isearch-unread keylist)
-  ;; If the event was a scroll-bar or mode-line click, the event will have
-  ;; been prefixed by a symbol such as vertical-scroll-bar.  We must remove
-  ;; it here, because this symbol will be attached to the event again next
-  ;; time it gets read by read-key-sequence.
-  ;;
-  ;; (Old comment from isearch-other-meta-char: "Note that we don't have to
-  ;; modify the event anymore in 21 because read_key_sequence no longer
-  ;; modifies events to produce fake prefix keys.")
-  (if (and (> (length keylist) 1)
-           (symbolp (car keylist))
-           (listp (cadr keylist))
-           (not (numberp (posn-point
-                          (event-start (cadr keylist)  )))))
-      (pop unread-command-events)))
-
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; scrolling within Isearch mode.  Alan Mackenzie (acm@muc.de), 2003/2/24
 ;;
@@ -2250,179 +2225,58 @@ the bottom."
         (recenter 0))))
   (goto-char isearch-point))
 
-(defun isearch-reread-key-sequence-naturally (keylist)
-  "Reread key sequence KEYLIST with an inactive Isearch-mode keymap.
-Return the key sequence as a string/vector."
-  (isearch-unread-key-sequence keylist)
-  (let (overriding-terminal-local-map)
-    (read-key-sequence nil)))  ; This will go through function-key-map, if nec.
-
-(defun isearch-lookup-scroll-key (key-seq)
-  "If KEY-SEQ is bound to a scrolling command, return it as a symbol.
-Otherwise return nil."
-  (let* ((overriding-terminal-local-map nil)
-         (binding (key-binding key-seq)))
-    (and binding (symbolp binding) (commandp binding)
-         (or (eq (get binding 'isearch-scroll) t)
-            (eq (get binding 'scroll-command) t))
-         binding)))
-
-(defalias 'isearch-other-control-char 'isearch-other-meta-char)
-
-(defun isearch-other-meta-char (&optional arg)
-  "Process a miscellaneous key sequence in Isearch mode.
-
-Try to convert the current key-sequence to something usable in Isearch
-mode, either by converting it with `function-key-map', downcasing a
-key with C-<upper case>, or finding a \"scrolling command\" bound to
-it.  \(In the last case, we may have to read more events.)  If so,
-either unread the converted sequence or execute the command.
-
-Otherwise, if `search-exit-option' is non-nil (the default) unread the
-key-sequence and exit the search normally.  If it is the symbol
-`edit', the search string is edited in the minibuffer and the meta
-character is unread so that it applies to editing the string.
-
-ARG is the prefix argument.  It will be transmitted through to the
-scrolling command or to the command whose key-sequence exits
-Isearch mode."
-  (interactive "P")
-  (let* ((key (if current-prefix-arg    ; not nec the same as ARG
-                  (substring (this-command-keys) universal-argument-num-events)
-                (this-command-keys)))
-        (main-event (aref key 0))
-        (keylist (listify-key-sequence key))
-         scroll-command isearch-point)
-    (cond ((and (= (length key) 1)
-               (let ((lookup (lookup-key local-function-key-map key)))
-                 (not (or (null lookup) (integerp lookup)
-                          (keymapp lookup)))))
-          ;; Handle a function key that translates into something else.
-          ;; If the key has a global definition too,
-          ;; exit and unread the key itself, so its global definition runs.
-          ;; Otherwise, unread the translation,
-          ;; so that the translated key takes effect within isearch.
-          (cancel-kbd-macro-events)
-          (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)))
-            (while keylist
-              (setq key (car keylist))
-              ;; If KEY is a printing char, we handle it here
-              ;; directly to avoid the input method and keyboard
-              ;; coding system translating it.
-              (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
-                ;; here, we must reread them.
-                (setq prefix-arg arg)
-                (apply 'isearch-unread keylist)
-                (setq keylist nil)))))
-         (
-          ;; Handle an undefined shifted control character
-          ;; by downshifting it if that makes it defined.
-          ;; (As read-key-sequence would normally do,
-          ;; if we didn't have a default definition.)
-          (let ((mods (event-modifiers main-event)))
-            (and (integerp main-event)
-                 (memq 'shift mods)
-                 (memq 'control mods)
-                 (not (memq (lookup-key isearch-mode-map
-                                        (let ((copy (copy-sequence key)))
-                                          (aset copy 0
-                                                (- main-event
-                                                   (- ?\C-\S-a ?\C-a)))
-                                          copy)
-                                        nil)
-                            '(nil
-                              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 or prefix argument.
-          ((progn
-            (setq key (isearch-reread-key-sequence-naturally keylist)
-                  keylist (listify-key-sequence key)
-                  main-event (aref key 0))
-            (or (and isearch-allow-scroll
-                     (setq scroll-command (isearch-lookup-scroll-key key)))
-                (and isearch-allow-prefix
-                     (let (overriding-terminal-local-map)
-                       (setq scroll-command (key-binding key))
-                       (memq scroll-command
-                             '(universal-argument
-                               negative-argument digit-argument))))))
-           ;; From this point onwards, KEY, KEYLIST and MAIN-EVENT hold a
-           ;; complete key sequence, possibly as modified by function-key-map,
-           ;; not merely the one or two event fragment which invoked
-           ;; isearch-other-meta-char in the first place.
-           (setq isearch-point (point))
-           (setq prefix-arg arg)
-           (command-execute scroll-command)
-           (let ((ab-bel (isearch-string-out-of-window isearch-point)))
-             (if ab-bel
-                 (isearch-back-into-window (eq ab-bel 'above) isearch-point)
-               (goto-char isearch-point)))
-           (isearch-update))
-         ;; A mouse click on the isearch message starts editing the search string
-         ((and (eq (car-safe main-event) 'down-mouse-1)
-               (window-minibuffer-p (posn-window (event-start main-event))))
-          ;; Swallow the up-event.
-          (read-event)
-          (isearch-edit-string))
-         (search-exit-option
-          (let (window)
-            (setq prefix-arg arg)
-             (isearch-unread-key-sequence keylist)
-             (setq main-event (car unread-command-events))
-
-            ;; If we got a mouse click event, that event contains the
-            ;; window clicked on. maybe it was read with the buffer
-            ;; it was clicked on.  If so, that buffer, not the current one,
-            ;; is in isearch mode.  So end the search in that buffer.
-
-            ;; ??? I have no idea what this if checks for, but it's
-            ;; obviously wrong for the case that a down-mouse event
-            ;; on another window invokes this function.  The event
-            ;; will contain the window clicked on and that window's
-            ;; buffer is certainly not always in Isearch mode.
-            ;;
-            ;; Leave the code in, but check for current buffer not
-            ;; being in Isearch mode for now, until someone tells
-            ;; what it's really supposed to do.
-            ;;
-            ;; --gerd 2001-08-10.
-
-            (if (and (not isearch-mode)
-                     (listp main-event)
-                     (setq window (posn-window (event-start main-event)))
-                     (windowp window)
-                     (or (> (minibuffer-depth) 0)
-                         (not (window-minibuffer-p window))))
-                (with-current-buffer (window-buffer window)
-                  (isearch-done)
-                  (isearch-clean-overlays))
-              (isearch-done)
-              (isearch-clean-overlays)
-               (setq prefix-arg arg))))
-          (t;; otherwise nil
-          (isearch-process-search-string key key)))))
+(defvar isearch-pre-scroll-point nil)
+
+(defun isearch-pre-command-hook ()
+  "Decide whether to exit Isearch mode before executing the command.
+Don't exit Isearch if the key sequence that invoked this command
+is bound in `isearch-mode-map', or if the invoked command is
+a prefix argument command (when `isearch-allow-prefix' is non-nil),
+or it is a scrolling command (when `isearch-allow-scroll' is non-nil).
+Otherwise, exit Isearch (when `search-exit-option' is non-nil)
+before the command is executed globally with terminated Isearch."
+  (let* ((key (this-single-command-keys))
+        (main-event (aref key 0)))
+    (cond
+     ;; Don't exit Isearch if we're in the middle of some
+     ;; set-temporary-overlay-map thingy like universal-argument--mode.
+     ((not (eq overriding-terminal-local-map isearch--saved-overriding-local-map)))
+     ;; Don't exit Isearch for isearch key bindings.
+     ((commandp (lookup-key isearch-mode-map key nil)))
+     ;; Optionally edit the search string instead of exiting.
+     ((eq search-exit-option 'edit)
+      (setq this-command 'isearch-edit-string))
+     ;; Handle a scrolling function or prefix argument.
+     ((or (and isearch-allow-prefix
+              (memq this-command '(universal-argument
+                                   digit-argument negative-argument)))
+         (and isearch-allow-scroll
+              (or (eq (get this-command 'isearch-scroll) t)
+                  (eq (get this-command 'scroll-command) t))))
+      (when isearch-allow-scroll
+       (setq isearch-pre-scroll-point (point))))
+     ;; A mouse click on the isearch message starts editing the search string.
+     ((and (eq (car-safe main-event) 'down-mouse-1)
+          (window-minibuffer-p (posn-window (event-start main-event))))
+      ;; Swallow the up-event.
+      (read-event)
+      (setq this-command 'isearch-edit-string))
+     ;; Other characters terminate the search and are then executed normally.
+     (search-exit-option
+      (isearch-done)
+      (isearch-clean-overlays))
+     ;; If search-exit-option is nil, run the command without exiting Isearch.
+     (t
+      (isearch-process-search-string key key)))))
+
+(defun isearch-post-command-hook ()
+  (when isearch-pre-scroll-point
+    (let ((ab-bel (isearch-string-out-of-window isearch-pre-scroll-point)))
+      (if ab-bel
+         (isearch-back-into-window (eq ab-bel 'above) isearch-pre-scroll-point)
+       (goto-char isearch-pre-scroll-point)))
+    (setq isearch-pre-scroll-point nil)
+    (isearch-update)))
 
 (defun isearch-quote-char (&optional count)
   "Quote special characters for incremental search.
@@ -2436,10 +2290,14 @@ With argument, add COUNT copies of the character."
        (if (subregexp-context-p isearch-string (length isearch-string))
            (isearch-process-search-string "[ ]" " ")
          (isearch-process-search-char char count))
-      (and enable-multibyte-characters
-          (>= char ?\200)
-          (<= char ?\377)
-          (setq char (unibyte-char-to-multibyte char)))
+      ;; This used to assume character codes 0240 - 0377 stand for
+      ;; characters in some single-byte character set, and converted them
+      ;; to Emacs characters.  But in 23.1 this feature is deprecated
+      ;; in favor of inserting the corresponding Unicode characters.
+      ;; (and enable-multibyte-characters
+      ;;      (>= char ?\200)
+      ;;      (<= char ?\377)
+      ;;      (setq char (unibyte-char-to-multibyte char)))
       (isearch-process-search-char char count))))
 
 (defun isearch-printing-char (&optional char count)
@@ -2614,13 +2472,13 @@ If there is no completion possible, say so and continue searching."
                              (< (point) isearch-opoint)))
                       "over")
                   (if isearch-wrapped "wrapped ")
-                  (mapconcat (lambda (s)
-                               (and (symbolp s)
-                                    (get s 'isearch-message-prefix)))
-                             (if (consp isearch-filter-predicates)
-                                 isearch-filter-predicates
-                               (list isearch-filter-predicates))
-                             "")
+                   (let ((prefix ""))
+                     (advice-function-mapc
+                      (lambda (_ props)
+                        (let ((np (cdr (assq 'isearch-message-prefix props))))
+                          (if np (setq prefix (concat np prefix)))))
+                      isearch-filter-predicate)
+                    prefix)
                   (if isearch-word
                       (or (and (symbolp isearch-word)
                                (get isearch-word 'isearch-message-prefix))
@@ -2766,15 +2624,8 @@ update the match data, and return point."
          (if (or (not isearch-success)
                  (bobp) (eobp)
                  (= (match-beginning 0) (match-end 0))
-                 ;; When one of filter predicates returns nil,
-                 ;; retry the search.  Otherwise, act according
-                 ;; to search-invisible (open overlays, etc.)
-                 (and (run-hook-with-args-until-failure
-                       'isearch-filter-predicates
-                       (match-beginning 0) (match-end 0))
-                      (or (eq search-invisible t)
-                          (not (isearch-range-invisible
-                                (match-beginning 0) (match-end 0))))))
+                 (funcall isearch-filter-predicate
+                          (match-beginning 0) (match-end 0)))
              (setq retry nil)))
        (setq isearch-just-started nil)
        (if isearch-success
@@ -2786,10 +2637,18 @@ update the match data, and return point."
 
     (invalid-regexp
      (setq isearch-error (car (cdr lossage)))
-     (if (string-match
-         "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
-         isearch-error)
-        (setq isearch-error "incomplete input")))
+     (cond
+      ((string-match
+       "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
+       isearch-error)
+       (setq isearch-error "incomplete input"))
+      ((and (not isearch-regexp)
+           (string-match "\\`Regular expression too big" isearch-error))
+       (cond
+       (isearch-word
+        (setq isearch-error "Too many words"))
+       ((and isearch-lax-whitespace search-whitespace-regexp)
+        (setq isearch-error "Too many spaces for whitespace matching"))))))
 
     (search-failed
      (setq isearch-success nil)
@@ -2951,7 +2810,6 @@ determined by `isearch-range-invisible' unless invisible text can be
 searched too when `search-invisible' is t."
   (or (eq search-invisible t)
       (not (isearch-range-invisible beg end))))
-(make-obsolete 'isearch-filter-visible 'isearch-invisible "24.4")
 
 \f
 ;; General utilities
@@ -3177,11 +3035,8 @@ Attempt to do the search exactly the way the pending Isearch would."
          (if (or (not success)
                  (= (point) bound) ; like (bobp) (eobp) in `isearch-search'.
                  (= (match-beginning 0) (match-end 0))
-                 (and (run-hook-with-args-until-failure
-                       'isearch-filter-predicates
-                       (match-beginning 0) (match-end 0))
-                      (not (isearch-range-invisible
-                            (match-beginning 0) (match-end 0)))))
+                 (funcall isearch-filter-predicate
+                          (match-beginning 0) (match-end 0)))
              (setq retry nil)))
        success)
     (error nil)))