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