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