Function: savehist-save
savehist-save is an interactive and byte-compiled function defined in
savehist.el.gz.
Signature
(savehist-save &optional AUTO-SAVE)
Documentation
Save the values of minibuffer history variables.
Unbound symbols referenced in savehist-additional-variables are ignored.
If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
and don't save the buffer if they are the same.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/savehist.el.gz
(defun savehist-save (&optional auto-save)
"Save the values of minibuffer history variables.
Unbound symbols referenced in `savehist-additional-variables' are ignored.
If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
and don't save the buffer if they are the same."
(interactive)
;; FIXME: We don't know how to merge old and new values of those vars
;; in `savehist-additional-variables', so only do the auto-sync
;; if there aren't such variables.
(unless (or savehist-additional-variables
(equal savehist--file-sync-modtime (savehist--file-modtime)))
;; The file has been changed since last time we saw it.
;; Probably some other Emacs session. Load the corresponding info so we
;; don't end up throwing it away by blindly overwriting it. There's
;; still a race-condition, but hopefully less problematic.
(savehist--reload nil))
(with-temp-buffer
(insert
(format-message
(concat
";; -*- mode: emacs-lisp; lexical-binding: t; coding: %s -*-\n"
";; Minibuffer history file, automatically generated by `savehist'.\n"
"\n")
savehist-coding-system))
(run-hooks 'savehist-save-hook)
(let ((print-length nil)
(print-level nil)
(print-quoted t)
(print-circle t))
;; Save the minibuffer histories, along with the value of
;; savehist-minibuffer-history-variables itself.
(when savehist-save-minibuffer-history
(prin1 `(setq savehist-minibuffer-history-variables
',savehist-minibuffer-history-variables)
(current-buffer))
(insert ?\n)
(dolist (symbol savehist-minibuffer-history-variables)
(when (and (boundp symbol)
(not (memq symbol savehist-ignored-variables)))
(let ((value (symbol-value symbol))
excess-space)
(when value ; Don't save empty histories.
(insert "(setq ")
(prin1 symbol (current-buffer))
(insert " '(")
;; We will print an extra space before the first element.
;; Record where that is.
(setq excess-space (point))
;; Print elements of VALUE one by one, carefully.
(dolist (elt value)
(let ((start (point)))
(insert " ")
;; Try to print and then to read an element.
(condition-case nil
(progn
(prin1 elt (current-buffer))
(save-excursion
(goto-char start)
(read (current-buffer))))
(error
;; If writing or reading gave an error, comment it out.
(goto-char start)
(insert "\n")
(while (not (eobp))
(insert ";;; ")
(forward-line 1))
(insert "\n")))
(goto-char (point-max))))
;; Delete the extra space before the first element.
(save-excursion
(goto-char excess-space)
(if (eq (following-char) ?\s)
(delete-region (point) (1+ (point)))))
(insert "))\n"))))))
;; Save the additional variables.
(dolist (elem savehist-additional-variables)
(when (not (memq elem savehist-minibuffer-history-variables))
(let ((symbol (if (consp elem)
(car elem)
elem)))
(when (boundp symbol)
(let ((value (symbol-value symbol)))
(when (savehist-printable value)
;; When we have a max-size, chop off the last elements.
(when (and (consp elem)
(listp value)
(length> value (cdr elem)))
(setq value (copy-sequence value))
(setcdr (nthcdr (cdr elem) value) nil))
(prin1 `(setq ,symbol ',value) (current-buffer))
(insert ?\n))))))))
;; If autosaving, avoid writing if nothing has changed since the
;; last write.
;; Note: it may be the case that nothing changed but that the
;; underlying file is not what we would write (because the file has been
;; overwritten by something else, but reloading the file had no effect);
;; the fact that we still do not autosave in this case is not a bug:
;; without this, when several Emacs processes share the same history
;; file, they'd keep fighting over it without ever reaching a fixpoint.
(let ((checksum (md5 (current-buffer) nil nil savehist-coding-system)))
(condition-case err
(unless (and auto-save (equal checksum savehist-last-checksum))
;; Set file-precious-flag when saving the buffer because we
;; don't want a half-finished write ruining the entire
;; history. Remember that this is run from a timer and from
;; kill-emacs-hook, and also that multiple Emacs instances
;; could write to this file at once.
(let ((file-precious-flag t)
(coding-system-for-write savehist-coding-system)
(dir (file-name-directory savehist-file)))
;; Ensure that the directory exists before saving.
(unless (file-exists-p dir)
(make-directory dir t))
(write-region (point-min) (point-max) savehist-file nil
(unless (called-interactively-p 'interactive) 'quiet)))
(when savehist-file-modes
(set-file-modes savehist-file savehist-file-modes))
(setq savehist--file-sync-modtime (savehist--file-modtime))
(setq savehist-last-checksum checksum))
(file-error
(unless savehist--has-given-file-warning
(lwarn '(savehist-file) :warning "Error writing `%s': %s"
savehist-file (caddr err))
(setq savehist--has-given-file-warning t)))))))