Function: term-previous-matching-input-string-position

term-previous-matching-input-string-position is a byte-compiled function defined in term.el.gz.

Signature

(term-previous-matching-input-string-position REGEXP ARG &optional START)

Documentation

Return the index matching REGEXP ARG places along the input ring.

Moves relative to START, or term-input-ring-index.

Source Code

;; Defined in /usr/src/emacs/lisp/term.el.gz
(defun term-previous-matching-input-string-position
  (regexp arg &optional start)
  "Return the index matching REGEXP ARG places along the input ring.
Moves relative to START, or `term-input-ring-index'."
  (when (or (not (ring-p term-input-ring))
	    (ring-empty-p term-input-ring))
    (error "No history"))
  (let* ((len (ring-length term-input-ring))
	 (motion (if (> arg 0) 1 -1))
	 (n (mod (- (or start (term-search-start arg)) motion) len))
	 (tried-each-ring-item nil)
	 (prev nil))
    ;; Do the whole search as many times as the argument says.
    (while (and (/= arg 0) (not tried-each-ring-item))
      ;; Step once.
      (setq prev n
	    n (mod (+ n motion) len))
      ;; If we haven't reached a match, step some more.
      (while (and (< n len) (not tried-each-ring-item)
		  (not (string-match regexp (ring-ref term-input-ring n))))
	(setq n (mod (+ n motion) len)
	      ;; If we have gone all the way around in this search.
	      tried-each-ring-item (= n prev)))
      (setq arg (if (> arg 0) (1- arg) (1+ arg))))
    ;; Now that we know which ring element to use, if we found it, return that.
    (when (string-match regexp (ring-ref term-input-ring n))
      n)))