Subject: Re: bug#30187: M-e should restore isearch correctly in special modes

From: Juri Linkov <[email-masked]>
Date: Mon, 29 Jan 2018 23:57:17 +0200

> There was an old problem that ‘isearch-edit-string’ incorrectly
> restored a previous isearch mode in such commands as
> ‘comint-history-isearch-backward’ and ‘dired-isearch-filenames’
> because they let-bind variables that affects the search parameters,
> e.g. ‘(let ((dired-isearch-filenames t)))’ only at the starting
> isearch, but when ‘M-e’ (isearch-edit-string) exits isearch,
> and later starts again, these let-binding are not in effect
> and thus restore the wrong search state.

There is another problem with ‘comint-history-isearch-backward’:
when it is called at the end of an “*Async Shell Command*” buffer
it should not activate comint-history isearch, only a normal isearch
should be started.  I tried different approaches, but only workable
solution is to check whether the shell prompt is empty as it is
in all “*Async Shell Command*” buffers:

diff --git a/lisp/comint.el b/lisp/comint.el
index 8dba317..b7179e2 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -1448,10 +1448,17 @@ comint-history-isearch-message-overlay
 (defun comint-history-isearch-setup ()
   "Set up a comint for using Isearch to search the input history.
 Intended to be added to `isearch-mode-hook' in `comint-mode'."
-  (when (or (eq comint-history-isearch t)
-	    (and (eq comint-history-isearch 'dwim)
-		 ;; Point is at command line.
-		 (comint-after-pmark-p)))
+  (when (and (get-buffer-process (current-buffer))
+	     (or (eq comint-history-isearch t)
+		 (and (eq comint-history-isearch 'dwim)
+		      ;; Point is at command line.
+		      (comint-after-pmark-p)
+		      ;; Prompt is not empty like in Async Shell Command buffers
+		      (not (eq (save-excursion
+				 (goto-char (comint-line-beginning-position))
+				 (forward-line 0)
+				 (point))
+			       (comint-line-beginning-position))))))
     (setq isearch-message-prefix-add "history ")
     (setq-local isearch-search-fun-function
                 #'comint-history-isearch-search)