savehist.el 9.63 KB
Newer Older
1
;;; savehist.el --- Save minibuffer history.
Richard M. Stallman's avatar
Richard M. Stallman committed
2

Stefan Monnier's avatar
Stefan Monnier committed
3
;; Copyright (C) 1997,2005 Free Software Foundation
Richard M. Stallman's avatar
Richard M. Stallman committed
4 5 6

;; Author: Hrvoje Niksic <hniksic@xemacs.org>
;; Keywords: minibuffer
Stefan Monnier's avatar
Stefan Monnier committed
7
;; Version: 9
Richard M. Stallman's avatar
Richard M. Stallman committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

;; This file is part of GNU Emacs.

;; GNU Emacs 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.

;; GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

28 29
;; Many editors (e.g. Vim) have the feature of saving minibuffer
;; history to an external file after exit.  This package provides the
Stefan Monnier's avatar
Stefan Monnier committed
30 31 32 33
;; same feature in Emacs.  When set up, it saves recorded minibuffer
;; histories to a file (`~/.emacs-history' by default).  Additional
;; variables may be specified by customizing
;; `savehist-additional-variables'.
Richard M. Stallman's avatar
Richard M. Stallman committed
34 35 36 37 38 39

;; To use savehist, put the following to `~/.emacs':
;;
;; (require 'savehist)
;; (savehist-load)

Stefan Monnier's avatar
Stefan Monnier committed
40 41 42
;; If you are using a version of Emacs that does not ship with this
;; package, be sure to have `savehist.el' in a directory that is in
;; your load-path, and to byte-compile it.
43

Richard M. Stallman's avatar
Richard M. Stallman committed
44 45
;;; Code:

46
(require 'custom)
Stefan Monnier's avatar
Stefan Monnier committed
47 48
(eval-when-compile
  (require 'cl))
49

Richard M. Stallman's avatar
Richard M. Stallman committed
50 51 52 53 54 55
;; User variables

(defgroup savehist nil
  "Save minibuffer history."
  :group 'minibuffer)

Stefan Monnier's avatar
Stefan Monnier committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
(defcustom savehist-save-minibuffer-history t
  "If non-nil, save all recorded minibuffer histories."
  :type 'boolean
  :group 'savehist)

(defcustom savehist-additional-variables ()
  "List of additional variables to save.
Each element is a symbol whose value will be persisted across Emacs
sessions that use savehist.  The contents of variables should be
printable with the Lisp printer.  If the variable's value is a list,
it will be trimmed to `savehist-length' elements.

You don't need to add minibuffer history variables to this list.  All
minibuffer histories will be saved automatically."
Richard M. Stallman's avatar
Richard M. Stallman committed
70 71 72 73
  :type '(repeat (symbol :tag "Variable"))
  :group 'savehist)

(defcustom savehist-file "~/.emacs-history"
Stefan Monnier's avatar
Stefan Monnier committed
74
  "File name to save minibuffer history to.
Richard M. Stallman's avatar
Richard M. Stallman committed
75 76 77 78 79 80 81
The minibuffer history is a series of Lisp expressions, which should be
loaded using `savehist-load' from your .emacs.  See `savehist-load' for
more details."
  :type 'file
  :group 'savehist)

(defcustom savehist-length 100
Stefan Monnier's avatar
Stefan Monnier committed
82 83 84
  "Maximum length of a minibuffer list.
Minibuffer histories with more entries are trimmed when saved, the older
entries being removed first.  If set to nil, the length is unlimited."
Richard M. Stallman's avatar
Richard M. Stallman committed
85 86 87 88
  :type '(choice integer
		 (const :tag "Unlimited" nil))
  :group 'savehist)

89
(defcustom savehist-modes #o600
Stefan Monnier's avatar
Stefan Monnier committed
90
  "Default permissions of the history file.
91 92 93 94
This is decimal, not octal.  The default is 384 (0600 in octal).
Set to nil to use the default permissions that Emacs uses, typically
mandated by umask.  The default is a bit more restrictive to protect
the user's privacy."
Richard M. Stallman's avatar
Richard M. Stallman committed
95 96 97
  :type 'integer
  :group 'savehist)

98
(defcustom savehist-autosave-interval (* 5 60)
Stefan Monnier's avatar
Stefan Monnier committed
99
  "The interval during which savehist should autosave the history buffer."
100 101 102
  :type 'integer
  :group 'savehist)

103 104 105 106 107 108
(defvar savehist-coding-system
  ;; UTF-8 is usually preferable to ISO-2022-8 when available, but under
  ;; XEmacs, UTF-8 is provided by external packages, and may not always be
  ;; available, so even if it currently is available, we prefer not to
  ;; use is.
  (if (featurep 'xemacs) 'iso-2022-8 'utf-8)
109 110 111 112 113 114 115 116 117 118
  "The coding system savehist uses for saving the minibuffer history.
Changing this value while Emacs is running is supported, but considered
unwise, unless you know what you are doing.")

;; Internal variables.

(defvar savehist-timer nil)

(defvar savehist-last-checksum nil)

Stefan Monnier's avatar
Stefan Monnier committed
119 120
(defvar savehist-minibuffer-history-variables nil)

Stefan Monnier's avatar
Stefan Monnier committed
121
(defconst savehist-no-conversion (if (featurep 'xemacs) 'binary 'no-conversion)
Stefan Monnier's avatar
Stefan Monnier committed
122 123
  "Coding system without conversion, only used for calculating checksums.
It should be as discriminating as `savehist-coding-system' but faster.")
Richard M. Stallman's avatar
Richard M. Stallman committed
124

Stefan Monnier's avatar
Stefan Monnier committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
;; Functions.

(defun savehist-install ()
  "Hook savehist into Emacs.
This will install `savehist-autosave' in `kill-emacs-hook' and on a timer.
To undo this, call `savehist-uninstall'."
  (add-hook 'minibuffer-setup-hook 'savehist-minibuffer-hook)
  (add-hook 'kill-emacs-hook 'savehist-autosave)
  ;; Install an invocation of savehist-autosave on a timer.  This
  ;; should not cause noticeable delays for users -- savehist-autosave
  ;; executes in under 5 ms on my system.
  (unless savehist-timer
    (setq savehist-timer
	  (if (featurep 'xemacs)
	      (start-itimer
	       "savehist" 'savehist-autosave savehist-autosave-interval
	       savehist-autosave-interval)
	    (run-with-timer savehist-autosave-interval savehist-autosave-interval
			    'savehist-autosave)))))

(defun savehist-uninstall ()
  "Undo installing savehist."
  (remove-hook 'minibuffer-setup-hook 'savehist-minibuffer-hook)
  (remove-hook 'kill-emacs-hook 'savehist-autosave)
  (when savehist-timer
    (if (featurep 'xemacs)
	(delete-itimer savehist-timer)
      (cancel-timer savehist-timer))
    (setq savehist-timer nil)))
Richard M. Stallman's avatar
Richard M. Stallman committed
154 155

;;;###autoload
Stefan Monnier's avatar
Stefan Monnier committed
156
(defun savehist-load (&optional no-install)
Richard M. Stallman's avatar
Richard M. Stallman committed
157
  "Load the minibuffer histories from `savehist-file'.
Stefan Monnier's avatar
Stefan Monnier committed
158 159 160
Unless NO-INSTALL is present and non-nil, the function will also install
`savehist-autosave' in `kill-emacs-hook' and on a timer, ensuring that
history is saved before leaving Emacs.
Richard M. Stallman's avatar
Richard M. Stallman committed
161

Stefan Monnier's avatar
Stefan Monnier committed
162 163 164
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 other time."
Richard M. Stallman's avatar
Richard M. Stallman committed
165
  (interactive "P")
166 167 168 169
  ;; Don't set coding-system-for-read here.  We rely on autodetection
  ;; and the coding cookie to convey that information.  That way, if
  ;; the user changes the value of savehist-coding-system, we can
  ;; still correctly load the old file.
Stefan Monnier's avatar
Stefan Monnier committed
170 171 172
  (load savehist-file t (not (interactive-p)))
  (unless no-install
    (savehist-install)))
Richard M. Stallman's avatar
Richard M. Stallman committed
173 174

;;;###autoload
175
(defun savehist-save (&optional auto-save)
Stefan Monnier's avatar
Stefan Monnier committed
176 177
  "Save the values of minibuffer history variables.
Unbound symbols referenced in `savehist-additional-variables' are ignored.
178 179
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."
Richard M. Stallman's avatar
Richard M. Stallman committed
180
  (interactive)
181 182 183 184 185 186 187 188
  (with-temp-buffer
    (insert
     (format ";; -*- mode: emacs-lisp; coding: %s -*-\n" savehist-coding-system)
     ";; Minibuffer history file, automatically generated by `savehist'.\n\n")
    (let ((print-length nil)
	  (print-string-length nil)
	  (print-level nil)
	  (print-readably t)
Stefan Monnier's avatar
Stefan Monnier committed
189 190 191 192 193 194 195
	  (print-quoted t)
	  (symbol-list (append
			(and savehist-save-minibuffer-history
			     (cons 'savehist-minibuffer-history-variables
				   savehist-minibuffer-history-variables))
			savehist-additional-variables)))
      (dolist (sym symbol-list)
196 197 198 199 200 201 202 203 204 205
	(when (boundp sym)
	  (let ((value (savehist-process-for-saving (symbol-value sym))))
	    (prin1 `(setq ,sym ',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-no-conversion)))
      (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
Stefan Monnier's avatar
Stefan Monnier committed
206 207 208
	;; 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.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	(let ((file-precious-flag t)
	      (coding-system-for-write savehist-coding-system))
	  (write-region (point-min) (point-max) savehist-file nil
			(unless (interactive-p) 'quiet)))
	(when savehist-modes
	  (set-file-modes savehist-file savehist-modes))
	(setq savehist-last-checksum checksum)))))

(defun savehist-autosave ()
  "Save the minibuffer history if it has been modified since the last save."
  (savehist-save t))

(defun savehist-process-for-saving (value)
  ;; Process VALUE for saving to file.  If it is a list, retain only
  ;; the first `savehist-length' values and prune non-printable ones.
  ;; If VALUE is not a list, return it as-is if it is printable and
  ;; nil otherwise.
  (cond
   ((listp value)
    (when (and savehist-length (> (length value) savehist-length))
229
      ;; This should be: (setq value (subseq value 0 savehist-length))
Stefan Monnier's avatar
Stefan Monnier committed
230 231
      (setq value (copy-sequence value))
      (setcdr (nthcdr savehist-length value) nil))
232
    ;; And this should be (remove-if-not #'savehist-printable value)
Stefan Monnier's avatar
Stefan Monnier committed
233
    (delq nil (mapcar (lambda (x) (if (savehist-printable x) x)) value)))
234 235 236 237
   ((savehist-printable value) value)
   (t nil)))

(defun savehist-printable (value)
Stefan Monnier's avatar
Stefan Monnier committed
238
  "Return non-nil if VALUE is printable."
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  ;; Quick response for oft-encountered types known to be printable.
  (cond
   ((stringp value))
   ((numberp value))
   ((symbolp value))
   (t
    ;; For others, check explicitly.
    (condition-case nil
	(let ((print-readably t)
	      (print-level nil)
	      (chars ()))
	  ;; Print the value into a string...
	  (prin1 value (lambda (char) (push char chars)))
	  ;; ...and attempt to read it.
	  (read (apply #'string (nreverse chars)))
	  ;; The attempt worked: the object is printable.
	  t)
      ;; The attempt failed: the object is not printable.
      (error nil)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
258

Stefan Monnier's avatar
Stefan Monnier committed
259 260 261 262
(defun savehist-minibuffer-hook ()
  (add-to-list 'savehist-minibuffer-history-variables
               minibuffer-history-variable))

Richard M. Stallman's avatar
Richard M. Stallman committed
263 264
(provide 'savehist)

Miles Bader's avatar
Miles Bader committed
265
;; arch-tag: b3ce47f4-c5ad-4ebc-ad02-73aba705cf9f
Richard M. Stallman's avatar
Richard M. Stallman committed
266
;;; savehist.el ends here