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