;;; savehist.el --- Save minibuffer history
;; Copyright (c) 1997 Free Software Foundation
;; Author: Hrvoje Niksic <firstname.lastname@example.org>
;; Keywords: minibuffer
;; Version: 0.4
;; This file is part of XEmacs.
;; XEmacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; XEmacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with XEmacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Synched up with: not in FSF
;; Many editors (e.g. Vim) have the feature of saving minibuffer
;; history to an external file after exit. This package provides the
;; same feature in Emacs. When Emacs is about the exit,
;; `savehist-save' will dump the contents of various minibuffer
;; histories (as determined by `savehist-history-variables') to a save
;; file (`~/.emacs-history' by default). Although the package was
;; designed for saving the minibuffer histories, any variables can be
;; saved that way.
;; To use savehist, put the following to `~/.emacs':
;; (require 'savehist)
;; Be sure to have `savehist.el' in a directory that is in your
;; load-path, and byte-compile it.
;; This code should work on XEmacs 19.14 and later, as well as GNU
;; Emacs 19.34 and later. It requires the Customize library, however
;; (shipped with XEmacs 19.15 and later, and with GNU Emacs 20.x).
;; User variables
(defgroup savehist nil
"Save minibuffer history."
;; Catch-all minibuffer history
;; File-oriented commands
;; Regexp-related reads
;; Searches in minibuffer (via `M-r' and such)
;; Query replace
;; eval-expression (`M-:')
;; shell-command (`M-!')
;; find-tag (`M-.')
;; Viper stuff
;; Buffer-related commands
;; Reads of variables and functions
;; Extended commands
;; Info, lookup, and bookmark historys
;; GNU Emacs-specific:
;; Extended commands
"*List of symbols to be saved.
Every symbol should refer to a variable. The variable will be saved
only if it is bound and has a non-nil value. Thus it is safe to
specify a superset of the variables a user is expected to want to
Default value contains minibuffer history variables used by XEmacs, GNU
Emacs and Viper (uh-oh)."
:type '(repeat (symbol :tag "Variable"))
(defcustom savehist-file "~/.emacs-history"
"*File name to save minibuffer history to.
The minibuffer history is a series of Lisp expressions, which should be
loaded using `savehist-load' from your .emacs. See `savehist-load' for
(defcustom savehist-length 100
"*Maximum length of a minibuffer list.
If set to nil, the length is unlimited."
:type '(choice integer
(const :tag "Unlimited" nil))
(defcustom savehist-modes 384
"*Default permissions of the history file.
This is decimal, not octal. The default is 384 (0600 in octal)."
(defun savehist-load (&optional no-hook)
"Load the minibuffer histories from `savehist-file'.
Unless NO-HOOK is specified, the function will also add the save function
to `kill-emacs-hook', thus ensuring that the minibuffer contents will be
saved before leaving Emacs.
This function should be normally used from your Emacs init file. Since it
removes your current minibuffer histories, it is unwise to call it at any
(add-hook 'kill-emacs-hook 'savehist-save))
(load savehist-file t))
(defun savehist-save ()
"Save the histories from `savehist-history-variables' to `savehist-file'.
A variable will be saved if it is bound and non-nil."
;; Is it wise to junk `find-file-hooks' just like that? How else
;; should I avoid font-lock et al.?
(let ((find-file-hooks nil)
(buffer-exists-p (get-file-buffer savehist-file)))
(set-buffer (find-file-noselect savehist-file))
";; -*- emacs-lisp -*-\n"
";; Minibuffer history file.\n\n"
";; This file is automatically generated by `savehist-save'"
" or when\n"
";; exiting Emacs.\n"
";; Do not edit. Unless you really want to, that is.\n\n")
(let ((print-length nil)
(dolist (sym savehist-history-variables)
(when (and (boundp sym)
`(setq ,sym (quote ,(savehist-delimit (symbol-value sym)
(set-file-modes savehist-file savehist-modes))
;; If ARG is a list with less than N elements, return it, else return
;; its subsequence of N elements. If N is nil or ARG is not a list,
;; always return ARG.
(defun savehist-delimit (arg n)
(if (and n
(> (length arg) n))
(subseq arg 0 n)
;;; savehist.el ends here