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