Function: comint-previous-matching-input

comint-previous-matching-input is an interactive and byte-compiled function defined in comint.el.gz.

Signature

(comint-previous-matching-input REGEXP N &optional RESTORE)

Documentation

Search backwards through input history for match for REGEXP.

(Previous history elements are earlier commands.)
With prefix argument N, search for Nth previous match. If N is negative, find the next or Nth next match.

Probably introduced at or before Emacs version 19.20.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/comint.el.gz
(defun comint-previous-matching-input (regexp n &optional restore)
  "Search backwards through input history for match for REGEXP.
\(Previous history elements are earlier commands.)
With prefix argument N, search for Nth previous match.
If N is negative, find the next or Nth next match."
  (interactive (comint-regexp-arg "Previous input matching (regexp): ") comint-mode)
  (setq n (comint-search-arg n))
  (let ((pos (comint-previous-matching-input-string-position regexp n)))
    ;; Has a match been found?
    (if (null pos)
	(user-error "Not found")
      (if (and comint-input-ring-index
               restore
               (or (and (< n 0)
                        (< comint-input-ring-index pos))
                   (and (> n 0)
                        (> comint-input-ring-index pos))))
          ;; We have a wrap; restore contents.
          (comint-restore-input)
        ;; If leaving the edit line, save partial input
        (if (null comint-input-ring-index) ;not yet on ring
	    (setq comint-stored-incomplete-input
		  (funcall comint-get-old-input)))
        (setq comint-input-ring-index pos)
        (unless isearch-mode
	  (let ((message-log-max nil))	; Do not write to *Messages*.
	    (message "History item: %d" (1+ pos))))
        (comint-delete-input)
        (insert (ring-ref comint-input-ring pos))))))