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)
  (with-temp-buffer
    (insert
     (format-message
      (concat
       ";; -*- mode: emacs-lisp; 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))
      ;; 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 (symbol savehist-additional-variables)
	(when (boundp symbol)
	  (let ((value (symbol-value symbol)))
	    (when (savehist-printable value)
	      (prin1 `(setq ,symbol ',value) (current-buffer))
	      (insert ?\n))))))
    ;; If autosaving, avoid writing if nothing has changed since the
    ;; last write.
    (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-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)))))))