Function: comint-read-input-ring

comint-read-input-ring is a byte-compiled function defined in comint.el.gz.

Signature

(comint-read-input-ring &optional SILENT)

Documentation

Set the buffer's comint-input-ring from a history file.

The name of the file is given by the variable comint-input-ring-file-name. The history ring is of size comint-input-ring-size, regardless of file size. If comint-input-ring-file-name is nil this function does nothing.

If the optional argument SILENT is non-nil, we say nothing about a failure to read the history file.

This function is useful for major mode commands and mode hooks.

The commands stored in the history file are separated by the comint-input-ring-separator, and entries that match comint-input-history-ignore are ignored. The most recent command comes last.

See also comint-input-ignoredups and comint-write-input-ring.

Source Code

;; Defined in /usr/src/emacs/lisp/comint.el.gz
;;; Input history processing in a buffer
;;============================================================================
;; Useful input history functions, courtesy of the Ergo group.

;; Eleven commands:
;; comint-dynamic-list-input-ring	List history in help buffer.
;; comint-previous-input		Previous input...
;; comint-previous-matching-input	...matching a string.
;; comint-previous-matching-input-from-input ... matching the current input.
;; comint-next-input			Next input...
;; comint-next-matching-input		...matching a string.
;; comint-next-matching-input-from-input     ... matching the current input.
;; comint-backward-matching-input      Backwards input...
;; comint-forward-matching-input       ...matching a string.
;; comint-replace-by-expanded-history	Expand history at point;
;;					replace with expanded history.
;; comint-magic-space			Expand history and insert space.
;;
;; Three functions:
;; comint-read-input-ring              Read into comint-input-ring...
;; comint-write-input-ring             Write to comint-input-ring-file-name.
;; comint-replace-by-expanded-history-before-point Workhorse function.

(defun comint-read-input-ring (&optional silent)
  "Set the buffer's `comint-input-ring' from a history file.
The name of the file is given by the variable `comint-input-ring-file-name'.
The history ring is of size `comint-input-ring-size', regardless of file size.
If `comint-input-ring-file-name' is nil this function does nothing.

If the optional argument SILENT is non-nil, we say nothing about a
failure to read the history file.

This function is useful for major mode commands and mode hooks.

The commands stored in the history file are separated by the
`comint-input-ring-separator', and entries that match
`comint-input-history-ignore' are ignored.  The most recent command
comes last.

See also `comint-input-ignoredups' and `comint-write-input-ring'."
  (cond ((or (null comint-input-ring-file-name)
	     (equal comint-input-ring-file-name ""))
	 nil)
	((not (file-readable-p comint-input-ring-file-name))
	 (or silent
	     (message "Cannot read history file %s"
		      comint-input-ring-file-name)))
	(t
	 (let* ((file comint-input-ring-file-name)
		(count 0)
		;; Some users set HISTSIZE or `comint-input-ring-size'
		;; to huge numbers.  Don't allocate a huge ring right
		;; away; there might not be that much history.
		(ring-size (min 1500 comint-input-ring-size))
		(ring (make-ring ring-size))
                ;; Use possibly buffer-local values of these variables.
                (ring-max-size comint-input-ring-size)
                (ring-separator comint-input-ring-separator)
                (ring-file-prefix comint-input-ring-file-prefix)
                (history-ignore comint-input-history-ignore)
                (ignoredups comint-input-ignoredups))
	   (with-temp-buffer
             (insert-file-contents file)
             ;; Save restriction in case file is already visited...
             ;; Watch for those date stamps in history files!
             (goto-char (point-max))
             (let (start end history)
               (while (and (< count ring-max-size)
                           (re-search-backward ring-separator nil t)
                           (setq end (match-beginning 0)))
                 (goto-char (if (re-search-backward ring-separator nil t)
                                (match-end 0)
                              (point-min)))
                 (when (and ring-file-prefix
                            (looking-at ring-file-prefix))
                   ;; Skip zsh extended_history stamps
                   (goto-char (match-end 0)))
                 (setq start (point))
                 (setq history (buffer-substring start end))
                 (when (and (not (string-match history-ignore history))
			    (or (null ignoredups)
				(ring-empty-p ring)
				(not (string-equal
                                      (ring-ref ring (1- (ring-length ring)))
				      history))))
		   (when (= count ring-size)
		     (ring-extend ring (min (- ring-max-size ring-size)
					    ring-size))
		     (setq ring-size (ring-size ring)))
		   (ring-insert-at-beginning ring history)
		   (setq count (1+ count))))))
	   (setq comint-input-ring ring
		 comint-input-ring-index nil)))))