Function: eshell-write-history

eshell-write-history is a byte-compiled function defined in em-hist.el.gz.

Signature

(eshell-write-history &optional FILENAME APPEND)

Documentation

Writes the buffer's eshell-history-ring to a history file.

If the optional argument FILENAME is nil, the value of eshell-history-file-name is used. This function does nothing if the value resolves to nil.

If the optional argument APPEND is non-nil, then append new history items to the history file. Otherwise, overwrite the contents of the file with eshell-history-ring (so long as it is not empty).

Useful within process sentinels.

See also eshell-read-history.

Source Code

;; Defined in /usr/src/emacs/lisp/eshell/em-hist.el.gz
(defun eshell-write-history (&optional filename append)
  "Writes the buffer's `eshell-history-ring' to a history file.
If the optional argument FILENAME is nil, the value of
`eshell-history-file-name' is used.  This function does nothing
if the value resolves to nil.

If the optional argument APPEND is non-nil, then append new
history items to the history file.  Otherwise, overwrite the
contents of the file with `eshell-history-ring' (so long as it is
not empty).

Useful within process sentinels.

See also `eshell-read-history'."
  (let* ((file (or filename eshell-history-file-name))
	 (resolved-file (if (stringp file) (file-truename file))))
    (cond
     ((or (null file)
	  (equal file "")
	  (null eshell-history-ring)
	  (ring-empty-p eshell-history-ring)
          (and append (= eshell-hist--new-items 0)))
      nil)
     ((not (file-writable-p resolved-file))
      (message "Cannot write history file %s" resolved-file))
     (t
      (let* ((ring eshell-history-ring)
	     (index (if append eshell-hist--new-items (ring-length ring))))
	;; Write it all out into a buffer first.  Much faster, but
	;; messier, than writing it one line at a time.
	(with-temp-buffer
	  (while (> index 0)
	    (setq index (1- index))
	    (let ((start (point)))
              ;; Remove properties before inserting, to avoid trouble
              ;; with read-only strings (Bug#28700).
              (insert (substring-no-properties (ring-ref ring index)) ?\n)
	      (subst-char-in-region start (1- (point)) ?\n ?\177)))
	  (eshell-with-private-file-modes
	   (write-region (point-min) (point-max) resolved-file append
			 'no-message)))
        (setq eshell-hist--new-items 0))))))