diff --git a/shell-maker.el b/shell-maker.el index 759b7e0..0966faa 100644 --- a/shell-maker.el +++ b/shell-maker.el @@ -571,14 +571,21 @@ Error if invoked from non-shell buffer." "Search forward for a real prompt matching PROMPT-REGEXP before BOUND. Skips matches in response content by verifying that the matched -text has `comint-highlight-prompt' face." - (let (found) +text has `comint-highlight-prompt' face. + +Like `re-search-forward', leave point unchanged when no prompt is +found. Without this, skipped lookalikes strand point on the last +one searched." + (let ((start (point)) + found) (while (and (not found) (re-search-forward prompt-regexp bound t)) (when (memq 'comint-highlight-prompt (ensure-list (get-text-property (match-beginning 0) 'font-lock-face))) (setq found t))) + (unless found + (goto-char start)) found)) (defun shell-maker-narrow-to-prompt () diff --git a/tests/shell-maker-history-tests.el b/tests/shell-maker-history-tests.el index f2b402c..89b3945 100644 --- a/tests/shell-maker-history-tests.el +++ b/tests/shell-maker-history-tests.el @@ -136,6 +136,20 @@ (should (equal (cdr (car result)) "Here is the code:\n\nMore text after"))))) +(ert-deftest shell-maker-history-test-search-prompt-leaves-point-when-not-found () + "Searching past prompt lookalikes should not move point when none is real." + (with-temp-buffer + (insert (shell-maker-history-test--prompt "Agent> ") "explain\n" + (shell-maker-history-test--marker "") + "Here is a transcript excerpt:\n" + "Agent> hello\n" + "and the rest of the answer\n") + (goto-char (point-min)) + (should (shell-maker--re-search-forward-prompt "^Agent> ")) + (let ((pos (point))) + (should-not (shell-maker--re-search-forward-prompt "^Agent> ")) + (should (= (point) pos))))) + (provide 'shell-maker-history-tests) ;;; shell-maker-history-tests.el ends here