(savehist-mode-hook): Re-add the var.
[bpt/emacs.git] / lisp / savehist.el
CommitLineData
216ee1a4 1;;; savehist.el --- Save minibuffer history.
cb23c7c1 2
2a2b5b29 3;; Copyright (C) 1997,2005 Free Software Foundation
cb23c7c1
RS
4
5;; Author: Hrvoje Niksic <hniksic@xemacs.org>
6;; Keywords: minibuffer
ed5a258a 7;; Version: 24
cb23c7c1
RS
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
25
26;;; Commentary:
27
216ee1a4
SM
28;; Many editors (e.g. Vim) have the feature of saving minibuffer
29;; history to an external file after exit. This package provides the
2a2b5b29
SM
30;; same feature in Emacs. When set up, it saves recorded minibuffer
31;; histories to a file (`~/.emacs-history' by default). Additional
32;; variables may be specified by customizing
33;; `savehist-additional-variables'.
cb23c7c1 34
b91f17dc
SM
35;; To use savehist, turn on savehist-mode by putting the following in
36;; `~/.emacs':
cb23c7c1 37;;
b91f17dc
SM
38;; (savehist-mode 1)
39;;
40;; or with customize: `M-x customize-option RET savehist-mode RET'.
41;;
42;; You can also explicitly save history with `M-x savehist-save' and
b13e8fb3 43;; load it by loading the `savehist-file' with `M-x load-file'.
cb23c7c1 44
2a2b5b29
SM
45;; If you are using a version of Emacs that does not ship with this
46;; package, be sure to have `savehist.el' in a directory that is in
47;; your load-path, and to byte-compile it.
216ee1a4 48
cb23c7c1
RS
49;;; Code:
50
216ee1a4 51(require 'custom)
2a2b5b29
SM
52(eval-when-compile
53 (require 'cl))
216ee1a4 54
cb23c7c1
RS
55;; User variables
56
57(defgroup savehist nil
58 "Save minibuffer history."
59 :group 'minibuffer)
60
b91f17dc
SM
61;;;###autoload
62(defcustom savehist-mode nil
63 "Mode for automatic saving of minibuffer history.
64Set this by calling the `savehist-mode' function or using the customize
65interface."
66 :type 'boolean
2d10b62b 67 :set (lambda (symbol value) (savehist-mode (or value 0)))
b91f17dc
SM
68 :initialize 'custom-initialize-default
69 :require 'savehist
70 :group 'savehist)
71
2a2b5b29 72(defcustom savehist-save-minibuffer-history t
b91f17dc
SM
73 "*If non-nil, save all recorded minibuffer histories.
74If you want to save only specific histories, use `savehist-save-hook' to
75modify the value of `savehist-minibuffer-history-variables'."
2a2b5b29
SM
76 :type 'boolean
77 :group 'savehist)
78
79(defcustom savehist-additional-variables ()
b91f17dc 80 "*List of additional variables to save.
2a2b5b29
SM
81Each element is a symbol whose value will be persisted across Emacs
82sessions that use savehist. The contents of variables should be
b91f17dc
SM
83printable with the Lisp printer. You don't need to add minibuffer
84history variables to this list, all minibuffer histories will be
85saved automatically as long as `savehist-save-minibuffer-history' is
86non-nil.
87
88User options should be saved with the customize interface. This
89list is useful for saving automatically updated variables that are not
90minibuffer histories, such as `compile-command' or `kill-ring'."
91 :type '(repeat variable)
cb23c7c1
RS
92 :group 'savehist)
93
b91f17dc
SM
94(defcustom savehist-file
95 (cond
96 ;; Backward compatibility with previous versions of savehist.
97 ((file-exists-p "~/.emacs-history") "~/.emacs-history")
98 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
99 "~/.emacs.d/history")
100 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
101 "~/.xemacs/history")
102 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
103 (t "~/.emacs-history"))
104 "*File name where minibuffer history is saved to and loaded from.
105The minibuffer history is a series of Lisp expressions loaded
106automatically when `savehist-mode' is turned on. See `savehist-mode'
107for more details.
108
109If you want your minibuffer history shared between Emacs and XEmacs,
110customize this value and make sure that `savehist-coding-system' is
111set to a coding system that exists in both emacsen."
cb23c7c1
RS
112 :type 'file
113 :group 'savehist)
114
b91f17dc
SM
115(defcustom savehist-file-modes #o600
116 "*Default permissions of the history file.
216ee1a4
SM
117This is decimal, not octal. The default is 384 (0600 in octal).
118Set to nil to use the default permissions that Emacs uses, typically
119mandated by umask. The default is a bit more restrictive to protect
120the user's privacy."
cb23c7c1
RS
121 :type 'integer
122 :group 'savehist)
123
216ee1a4 124(defcustom savehist-autosave-interval (* 5 60)
b91f17dc
SM
125 "*The interval between autosaves of minibuffer history.
126If set to nil, disables timer-based autosaving."
216ee1a4
SM
127 :type 'integer
128 :group 'savehist)
129
ed5a258a
SM
130(defcustom savehist-mode-hook nil
131 "Hook called when `savehist-mode' is turned on."
132 :type 'hook)
133
b91f17dc 134(defcustom savehist-save-hook nil
b13e8fb3 135 "Hook called by `savehist-save' before saving the variables.
b91f17dc
SM
136You can use this hook to influence choice and content of variables to
137save."
138 :type 'hook)
139
2d10b62b
SM
140;; This should be capable of representing characters used by Emacs.
141;; We prefer UTF-8 over ISO 2022 because it is well-known outside
142;; Mule. XEmacs prir to 21.5 had UTF-8 provided by an external
143;; package which may not be loaded, which is why we check for version.
144(defvar savehist-coding-system (if (and (featurep 'xemacs)
145 (<= emacs-major-version 21)
146 (< emacs-minor-version 5))
147 'iso-2022-8 'utf-8)
216ee1a4
SM
148 "The coding system savehist uses for saving the minibuffer history.
149Changing this value while Emacs is running is supported, but considered
150unwise, unless you know what you are doing.")
151
152;; Internal variables.
153
154(defvar savehist-timer nil)
155
156(defvar savehist-last-checksum nil)
157
b91f17dc
SM
158(defvar savehist-minibuffer-history-variables nil
159 "List of minibuffer histories.
160The contents of this variable is built while Emacs is running, and saved
161along with minibuffer history. You can change its value off
162`savehist-save-hook' to influence which variables are saved.")
2a2b5b29 163
ed5a258a
SM
164(defconst savehist-no-conversion (if (featurep 'xemacs) 'binary 'no-conversion)
165 "Coding system without any conversion.
166This is used for calculating an internal checksum. Should be as fast
167as possible, ideally simply exposing the internal representation of
168buffer text.")
b91f17dc 169
ed5a258a
SM
170(defvar savehist-loaded nil
171 "Whether the history has already been loaded.
172This prevents toggling savehist-mode from destroying existing
173minibuffer history.")
b91f17dc 174
2d10b62b
SM
175(when (featurep 'xemacs)
176 ;; Must declare this under XEmacs, which doesn't have built-in
177 ;; minibuffer history truncation.
178 (defvar history-length 100))
cb23c7c1 179\f
2a2b5b29
SM
180;; Functions.
181
b91f17dc
SM
182;;;###autoload
183(defun savehist-mode (arg)
184 "Toggle savehist-mode.
185Positive ARG turns on `savehist-mode'. When on, savehist-mode causes
186minibuffer history to be saved periodically and when exiting Emacs.
187When turned on for the first time in an Emacs session, it causes the
188previous minibuffer history to be loaded from `savehist-file'.
189
190This mode should normally be turned on from your Emacs init file.
191Calling it at any other time replaces your current minibuffer histories,
192which is probably undesirable."
193 (interactive "P")
194 (setq savehist-mode
195 (if (null arg)
196 (not savehist-mode)
197 (> (prefix-numeric-value arg) 0)))
198 (if (not savehist-mode)
199 (savehist-uninstall)
200 (when (and (not savehist-loaded)
201 (file-exists-p savehist-file))
202 (condition-case errvar
203 (progn
204 ;; Don't set coding-system-for-read -- we rely on the
205 ;; coding cookie to convey that information. That way, if
206 ;; the user changes the value of savehist-coding-system,
207 ;; we can still correctly load the old file.
208 (load savehist-file nil (not (interactive-p)))
209 (setq savehist-loaded t))
210 (error
211 ;; Don't install the mode if reading failed. Doing so would
212 ;; effectively destroy the user's data at the next save.
213 (setq savehist-mode nil)
214 (savehist-uninstall)
215 (signal (car errvar) (cdr errvar)))))
ed5a258a
SM
216 (savehist-install)
217 (run-hooks 'savehist-mode-hook))
b13e8fb3
SM
218 ;; Return the new setting.
219 savehist-mode)
b91f17dc
SM
220(add-minor-mode 'savehist-mode "")
221
222(defun savehist-load ()
223 "Obsolete function provided for transition from old versions of savehist.
224Don't call this from new code, use (savehist-mode 1) instead.
225
226This function loads the variables stored in `savehist-file' and turns on
227savehist-mode. If savehist-file is in the old format that doesn't record
228the value of `savehist-minibuffer-history-variables', that value is
229deducted from the contents of the file."
230 (savehist-mode 1)
231 ;; Old versions of savehist distributed with XEmacs didn't save
232 ;; savehist-minibuffer-history-variables. If that variable is nil
233 ;; after loading the file, try to intuit the intended value.
234 (when (null savehist-minibuffer-history-variables)
235 (setq savehist-minibuffer-history-variables
236 (with-temp-buffer
237 (ignore-errors
238 (insert-file-contents savehist-file))
239 (let ((vars ()) form)
240 (while (setq form (condition-case nil
241 (read (current-buffer)) (error nil)))
242 ;; Each form read is of the form (setq VAR VALUE).
243 ;; Collect VAR, i.e. (nth form 1).
244 (push (nth 1 form) vars))
245 vars)))))
246(make-obsolete 'savehist-load 'savehist-mode)
247
2a2b5b29
SM
248(defun savehist-install ()
249 "Hook savehist into Emacs.
b91f17dc
SM
250Normally invoked by calling `savehist-mode' to set the minor mode.
251Installs `savehist-autosave' in `kill-emacs-hook' and on a timer. To
252undo this, call `savehist-uninstall'."
2a2b5b29
SM
253 (add-hook 'minibuffer-setup-hook 'savehist-minibuffer-hook)
254 (add-hook 'kill-emacs-hook 'savehist-autosave)
255 ;; Install an invocation of savehist-autosave on a timer. This
256 ;; should not cause noticeable delays for users -- savehist-autosave
257 ;; executes in under 5 ms on my system.
b91f17dc
SM
258 (when (and savehist-autosave-interval
259 (null savehist-timer))
2a2b5b29
SM
260 (setq savehist-timer
261 (if (featurep 'xemacs)
262 (start-itimer
263 "savehist" 'savehist-autosave savehist-autosave-interval
264 savehist-autosave-interval)
b91f17dc
SM
265 (run-with-timer savehist-autosave-interval
266 savehist-autosave-interval 'savehist-autosave)))))
2a2b5b29
SM
267
268(defun savehist-uninstall ()
b91f17dc
SM
269 "Undo installing savehist.
270Normally invoked by calling `savehist-mode' to unset the minor mode."
2a2b5b29
SM
271 (remove-hook 'minibuffer-setup-hook 'savehist-minibuffer-hook)
272 (remove-hook 'kill-emacs-hook 'savehist-autosave)
273 (when savehist-timer
274 (if (featurep 'xemacs)
275 (delete-itimer savehist-timer)
276 (cancel-timer savehist-timer))
277 (setq savehist-timer nil)))
cb23c7c1 278
216ee1a4 279(defun savehist-save (&optional auto-save)
2a2b5b29
SM
280 "Save the values of minibuffer history variables.
281Unbound symbols referenced in `savehist-additional-variables' are ignored.
216ee1a4
SM
282If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
283 and don't save the buffer if they are the same."
cb23c7c1 284 (interactive)
216ee1a4
SM
285 (with-temp-buffer
286 (insert
287 (format ";; -*- mode: emacs-lisp; coding: %s -*-\n" savehist-coding-system)
288 ";; Minibuffer history file, automatically generated by `savehist'.\n\n")
b91f17dc 289 (run-hooks 'savehist-save-hook)
216ee1a4
SM
290 (let ((print-length nil)
291 (print-string-length nil)
292 (print-level nil)
293 (print-readably t)
b91f17dc
SM
294 (print-quoted t))
295 ;; Save the minibuffer histories, along with the value of
296 ;; savehist-minibuffer-history-variables itself.
297 (when savehist-save-minibuffer-history
298 (prin1 `(setq savehist-minibuffer-history-variables
299 ',savehist-minibuffer-history-variables)
300 (current-buffer))
301 (insert ?\n)
302 (dolist (symbol savehist-minibuffer-history-variables)
303 (when (boundp symbol)
304 (let ((value (savehist-trim-history (symbol-value symbol))))
305 (when value ; don't save empty histories
306 (prin1 `(setq ,symbol ',value) (current-buffer))
307 (insert ?\n))))))
308 ;; Save the additional variables.
309 (dolist (symbol savehist-additional-variables)
310 (when (boundp symbol)
311 (let ((value (symbol-value symbol)))
312 (when (savehist-printable value)
313 (prin1 `(setq ,symbol ',value) (current-buffer))
314 (insert ?\n))))))
216ee1a4
SM
315 ;; If autosaving, avoid writing if nothing has changed since the
316 ;; last write.
317 (let ((checksum (md5 (current-buffer) nil nil savehist-no-conversion)))
318 (unless (and auto-save (equal checksum savehist-last-checksum))
319 ;; Set file-precious-flag when saving the buffer because we
320 ;; don't want a half-finished write ruining the entire
2a2b5b29
SM
321 ;; history. Remember that this is run from a timer and from
322 ;; kill-emacs-hook, and also that multiple Emacs instances
323 ;; could write to this file at once.
216ee1a4
SM
324 (let ((file-precious-flag t)
325 (coding-system-for-write savehist-coding-system))
326 (write-region (point-min) (point-max) savehist-file nil
327 (unless (interactive-p) 'quiet)))
b91f17dc
SM
328 (when savehist-file-modes
329 (set-file-modes savehist-file savehist-file-modes))
216ee1a4
SM
330 (setq savehist-last-checksum checksum)))))
331
332(defun savehist-autosave ()
b91f17dc
SM
333 "Save the minibuffer history if it has been modified since the last save.
334Does nothing if savehist-mode is off."
335 (when savehist-mode
336 (savehist-save t)))
337
338(defun savehist-trim-history (value)
ed5a258a
SM
339 "Retain only the first history-length items in VALUE.
340Only used under XEmacs, which doesn't (yet) implement automatic
341trimming of history lists to history-length items."
b91f17dc
SM
342 (if (and (featurep 'xemacs)
343 (natnump history-length)
344 (> (length value) history-length))
345 ;; Equivalent to `(subseq value 0 history-length)', but doesn't
346 ;; need cl-extra at run-time.
347 (loop repeat history-length collect (pop value))
348 value))
216ee1a4
SM
349
350(defun savehist-printable (value)
3930b7e4 351 "Return non-nil if VALUE is printable."
216ee1a4 352 (cond
b91f17dc 353 ;; Quick response for oft-encountered types known to be printable.
216ee1a4
SM
354 ((stringp value))
355 ((numberp value))
356 ((symbolp value))
357 (t
358 ;; For others, check explicitly.
b91f17dc
SM
359 (with-temp-buffer
360 (condition-case nil
361 (let ((print-readably t) (print-level nil))
362 ;; Print the value into a buffer...
363 (prin1 value (current-buffer))
216ee1a4 364 ;; ...and attempt to read it.
b91f17dc 365 (read (point-min-marker))
216ee1a4
SM
366 ;; The attempt worked: the object is printable.
367 t)
b91f17dc
SM
368 ;; The attempt failed: the object is not printable.
369 (error nil))))))
cb23c7c1 370
2a2b5b29 371(defun savehist-minibuffer-hook ()
2d10b62b
SM
372 ;; XEmacs sets minibuffer-history-variable to t to mean "no history
373 ;; is being recorded".
374 (unless (eq minibuffer-history-variable t)
375 (add-to-list 'savehist-minibuffer-history-variables
376 minibuffer-history-variable)))
2a2b5b29 377
cb23c7c1 378(provide 'savehist)
b91f17dc 379\f
16b52b61 380;; arch-tag: b3ce47f4-c5ad-4ebc-ad02-73aba705cf9f
b91f17dc 381
cb23c7c1 382;;; savehist.el ends here