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