Mercurial > emacs
comparison lisp/savehist.el @ 83395:b31326248cf6
Merged from miles@gnu.org--gnu-2005 (patch 142-148, 615-628)
Patches applied:
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-615
Merge from gnus--rel--5.10
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-616
Add lisp/mh-e/.arch-inventory
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-617
Update from CVS
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-618
Update from CVS
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-619
Update from CVS
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-620
Update from CVS
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-621
Merge from gnus--rel--5.10
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-622
Update from CVS
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-623
Update from CVS
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-624
Update from CVS: lisp/smerge-mode.el: Add 'tools' to file keywords.
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-625
Update from CVS
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-626
Update from CVS
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-627
Update from CVS
* miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-628
Update from CVS
* miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-142
Update from CVS
* miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-143
Update from CVS
* miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-144
Update from CVS
* miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-145
Update from CVS
* miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-146
Update from CVS
* miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-147
Update from CVS
* miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-148
Update from CVS
git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-435
author | Karoly Lorentey <lorentey@elte.hu> |
---|---|
date | Tue, 01 Nov 2005 06:23:08 +0000 |
parents | 9ee3fe90596c |
children | 00e4363eadb1 |
comparison
equal
deleted
inserted
replaced
83394:7d093d9d4479 | 83395:b31326248cf6 |
---|---|
1 ;;; savehist.el --- Save minibuffer history. | 1 ;;; savehist.el --- Save minibuffer history. |
2 | 2 |
3 ;; Copyright (C) 1997, 2005 Free Software Foundation | 3 ;; Copyright (C) 1997,2005 Free Software Foundation |
4 | 4 |
5 ;; Author: Hrvoje Niksic <hniksic@xemacs.org> | 5 ;; Author: Hrvoje Niksic <hniksic@xemacs.org> |
6 ;; Keywords: minibuffer | 6 ;; Keywords: minibuffer |
7 ;; Version: 7 | 7 ;; Version: 9 |
8 | 8 |
9 ;; This file is part of GNU Emacs. | 9 ;; This file is part of GNU Emacs. |
10 | 10 |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | 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 | 12 ;; it under the terms of the GNU General Public License as published by |
25 | 25 |
26 ;;; Commentary: | 26 ;;; Commentary: |
27 | 27 |
28 ;; Many editors (e.g. Vim) have the feature of saving minibuffer | 28 ;; Many editors (e.g. Vim) have the feature of saving minibuffer |
29 ;; history to an external file after exit. This package provides the | 29 ;; history to an external file after exit. This package provides the |
30 ;; same feature in Emacs. When Emacs is about the exit, | 30 ;; same feature in Emacs. When set up, it saves recorded minibuffer |
31 ;; `savehist-save' will dump the contents of various minibuffer | 31 ;; histories to a file (`~/.emacs-history' by default). Additional |
32 ;; histories (as determined by `savehist-history-variables') to a save | 32 ;; variables may be specified by customizing |
33 ;; file (`~/.emacs-history' by default). Although the package was | 33 ;; `savehist-additional-variables'. |
34 ;; designed for saving the minibuffer histories, any variables can be | |
35 ;; saved that way. | |
36 | 34 |
37 ;; To use savehist, put the following to `~/.emacs': | 35 ;; To use savehist, put the following to `~/.emacs': |
38 ;; | 36 ;; |
39 ;; (require 'savehist) | 37 ;; (require 'savehist) |
40 ;; (savehist-load) | 38 ;; (savehist-load) |
41 | 39 |
42 ;; Be sure to have `savehist.el' in a directory that is in your | 40 ;; If you are using a version of Emacs that does not ship with this |
43 ;; load-path, and byte-compile it. | 41 ;; package, be sure to have `savehist.el' in a directory that is in |
42 ;; your load-path, and to byte-compile it. | |
44 | 43 |
45 ;;; Code: | 44 ;;; Code: |
46 | 45 |
47 (require 'custom) | 46 (require 'custom) |
47 (eval-when-compile | |
48 (require 'cl)) | |
48 | 49 |
49 ;; User variables | 50 ;; User variables |
50 | 51 |
51 (defgroup savehist nil | 52 (defgroup savehist nil |
52 "Save minibuffer history." | 53 "Save minibuffer history." |
53 :group 'minibuffer) | 54 :group 'minibuffer) |
54 | 55 |
55 (defcustom savehist-history-variables | 56 (defcustom savehist-save-minibuffer-history t |
56 '( | 57 "If non-nil, save all recorded minibuffer histories." |
57 ;; Catch-all minibuffer history | 58 :type 'boolean |
58 minibuffer-history | 59 :group 'savehist) |
59 ;; File-oriented commands | 60 |
60 file-name-history | 61 (defcustom savehist-additional-variables () |
61 ;; Regexp-related reads | 62 "List of additional variables to save. |
62 regexp-history | 63 Each element is a symbol whose value will be persisted across Emacs |
63 ;; Searches in minibuffer (via `M-r' and such) | 64 sessions that use savehist. The contents of variables should be |
64 minibuffer-history-search-history | 65 printable with the Lisp printer. If the variable's value is a list, |
65 ;; Query replace | 66 it will be trimmed to `savehist-length' elements. |
66 query-replace-history | 67 |
67 ;; eval-expression (`M-:') | 68 You don't need to add minibuffer history variables to this list. All |
68 read-expression-history | 69 minibuffer histories will be saved automatically." |
69 ;; shell-command (`M-!') | |
70 shell-command-history | |
71 ;; compile | |
72 compile-history | |
73 ;; find-tag (`M-.') | |
74 find-tag-history | |
75 ;; grep | |
76 grep-history | |
77 ;; Viper stuff | |
78 vip-ex-history vip-search-history | |
79 vip-replace1-history vip-replace2-history | |
80 vip-shell-history vip-search-history | |
81 | |
82 ;; XEmacs-specific: | |
83 ;; Buffer-related commands | |
84 buffer-history | |
85 ;; Reads of variables and functions | |
86 variable-history function-history | |
87 ;; Extended commands | |
88 read-command-history | |
89 | |
90 ;; Info, lookup, and bookmark historys | |
91 Info-minibuffer-history | |
92 Info-search-history | |
93 Manual-page-minibuffer-history | |
94 | |
95 ;; Emacs-specific: | |
96 ;; Extended commands | |
97 extended-command-history) | |
98 "*List of symbols to be saved. | |
99 Every symbol should refer to a variable. The variable will be saved | |
100 only if it is bound and has a non-nil value. Thus it is safe to | |
101 specify a superset of the variables a user is expected to want to | |
102 save. | |
103 | |
104 Default value contains minibuffer history variables used by Emacs, XEmacs, | |
105 and Viper (uh-oh). Note that, if you customize this variable, you | |
106 can lose the benefit of future versions of Emacs adding new values to | |
107 the list. Because of that it might be more useful to add values using | |
108 `add-to-list'." | |
109 :type '(repeat (symbol :tag "Variable")) | 70 :type '(repeat (symbol :tag "Variable")) |
110 :group 'savehist) | 71 :group 'savehist) |
111 | 72 |
112 (defcustom savehist-file "~/.emacs-history" | 73 (defcustom savehist-file "~/.emacs-history" |
113 "*File name to save minibuffer history to. | 74 "File name to save minibuffer history to. |
114 The minibuffer history is a series of Lisp expressions, which should be | 75 The minibuffer history is a series of Lisp expressions, which should be |
115 loaded using `savehist-load' from your .emacs. See `savehist-load' for | 76 loaded using `savehist-load' from your .emacs. See `savehist-load' for |
116 more details." | 77 more details." |
117 :type 'file | 78 :type 'file |
118 :group 'savehist) | 79 :group 'savehist) |
119 | 80 |
120 (defcustom savehist-length 100 | 81 (defcustom savehist-length 100 |
121 "*Maximum length of a minibuffer list. | 82 "Maximum length of a minibuffer list. |
122 If set to nil, the length is unlimited." | 83 Minibuffer histories with more entries are trimmed when saved, the older |
84 entries being removed first. If set to nil, the length is unlimited." | |
123 :type '(choice integer | 85 :type '(choice integer |
124 (const :tag "Unlimited" nil)) | 86 (const :tag "Unlimited" nil)) |
125 :group 'savehist) | 87 :group 'savehist) |
126 | 88 |
127 (defcustom savehist-modes #o600 | 89 (defcustom savehist-modes #o600 |
128 "*Default permissions of the history file. | 90 "Default permissions of the history file. |
129 This is decimal, not octal. The default is 384 (0600 in octal). | 91 This is decimal, not octal. The default is 384 (0600 in octal). |
130 Set to nil to use the default permissions that Emacs uses, typically | 92 Set to nil to use the default permissions that Emacs uses, typically |
131 mandated by umask. The default is a bit more restrictive to protect | 93 mandated by umask. The default is a bit more restrictive to protect |
132 the user's privacy." | 94 the user's privacy." |
133 :type 'integer | 95 :type 'integer |
134 :group 'savehist) | 96 :group 'savehist) |
135 | 97 |
136 (defcustom savehist-autosave-interval (* 5 60) | 98 (defcustom savehist-autosave-interval (* 5 60) |
137 "*The interval during which savehist should autosave the history buffer." | 99 "The interval during which savehist should autosave the history buffer." |
138 :type 'integer | 100 :type 'integer |
139 :group 'savehist) | 101 :group 'savehist) |
140 | 102 |
141 (defvar savehist-coding-system | 103 (defvar savehist-coding-system |
142 ;; UTF-8 is usually preferable to ISO-2022-8 when available, but under | 104 ;; UTF-8 is usually preferable to ISO-2022-8 when available, but under |
152 | 114 |
153 (defvar savehist-timer nil) | 115 (defvar savehist-timer nil) |
154 | 116 |
155 (defvar savehist-last-checksum nil) | 117 (defvar savehist-last-checksum nil) |
156 | 118 |
119 (defvar savehist-minibuffer-history-variables nil) | |
120 | |
157 (defconst savehist-no-conversion (if (featurep 'xemacs) 'binary 'no-conversion) | 121 (defconst savehist-no-conversion (if (featurep 'xemacs) 'binary 'no-conversion) |
158 ;; FIXME: Why not use savehist-coding-system? | 122 "Coding system without conversion, only used for calculating checksums. |
159 "Coding system without conversion, only used for calculating checksums.") | 123 It should be as discriminating as `savehist-coding-system' but faster.") |
160 | 124 |
161 ;; Functions | 125 ;; Functions. |
126 | |
127 (defun savehist-install () | |
128 "Hook savehist into Emacs. | |
129 This will install `savehist-autosave' in `kill-emacs-hook' and on a timer. | |
130 To undo this, call `savehist-uninstall'." | |
131 (add-hook 'minibuffer-setup-hook 'savehist-minibuffer-hook) | |
132 (add-hook 'kill-emacs-hook 'savehist-autosave) | |
133 ;; Install an invocation of savehist-autosave on a timer. This | |
134 ;; should not cause noticeable delays for users -- savehist-autosave | |
135 ;; executes in under 5 ms on my system. | |
136 (unless savehist-timer | |
137 (setq savehist-timer | |
138 (if (featurep 'xemacs) | |
139 (start-itimer | |
140 "savehist" 'savehist-autosave savehist-autosave-interval | |
141 savehist-autosave-interval) | |
142 (run-with-timer savehist-autosave-interval savehist-autosave-interval | |
143 'savehist-autosave))))) | |
144 | |
145 (defun savehist-uninstall () | |
146 "Undo installing savehist." | |
147 (remove-hook 'minibuffer-setup-hook 'savehist-minibuffer-hook) | |
148 (remove-hook 'kill-emacs-hook 'savehist-autosave) | |
149 (when savehist-timer | |
150 (if (featurep 'xemacs) | |
151 (delete-itimer savehist-timer) | |
152 (cancel-timer savehist-timer)) | |
153 (setq savehist-timer nil))) | |
162 | 154 |
163 ;;;###autoload | 155 ;;;###autoload |
164 (defun savehist-load (&optional no-hook) | 156 (defun savehist-load (&optional no-install) |
165 "Load the minibuffer histories from `savehist-file'. | 157 "Load the minibuffer histories from `savehist-file'. |
166 Unless NO-HOOK is specified, the function will also add the save function | 158 Unless NO-INSTALL is present and non-nil, the function will also install |
167 to `kill-emacs-hook' and on a timer, ensuring that the minibuffer contents | 159 `savehist-autosave' in `kill-emacs-hook' and on a timer, ensuring that |
168 will be saved before leaving Emacs. | 160 history is saved before leaving Emacs. |
169 | 161 |
170 This function should be normally used from your Emacs init file. Since it | 162 This function should be normally used from your Emacs init file. Since |
171 removes your current minibuffer histories, it is unwise to call it at any | 163 it removes your current minibuffer histories, it is unwise to call it at |
172 other time." | 164 any other time." |
173 (interactive "P") | 165 (interactive "P") |
174 (unless no-hook | |
175 (add-hook 'kill-emacs-hook 'savehist-autosave) | |
176 ;; Install an invocation of savehist-autosave on a timer. This | |
177 ;; should not cause a noticeable delay -- savehist-autosave | |
178 ;; executes in under 5 ms on my system. | |
179 (unless savehist-timer | |
180 (setq savehist-timer | |
181 (if (featurep 'xemacs) | |
182 (start-itimer | |
183 "savehist" 'savehist-autosave savehist-autosave-interval | |
184 savehist-autosave-interval) | |
185 (run-with-idle-timer savehist-autosave-interval savehist-autosave-interval | |
186 'savehist-autosave))))) | |
187 ;; Don't set coding-system-for-read here. We rely on autodetection | 166 ;; Don't set coding-system-for-read here. We rely on autodetection |
188 ;; and the coding cookie to convey that information. That way, if | 167 ;; and the coding cookie to convey that information. That way, if |
189 ;; the user changes the value of savehist-coding-system, we can | 168 ;; the user changes the value of savehist-coding-system, we can |
190 ;; still correctly load the old file. | 169 ;; still correctly load the old file. |
191 (load savehist-file t (not (interactive-p)))) | 170 (load savehist-file t (not (interactive-p))) |
171 (unless no-install | |
172 (savehist-install))) | |
192 | 173 |
193 ;;;###autoload | 174 ;;;###autoload |
194 (defun savehist-save (&optional auto-save) | 175 (defun savehist-save (&optional auto-save) |
195 "Save the histories from `savehist-history-variables' to `savehist-file'. | 176 "Save the values of minibuffer history variables. |
196 Unbound symbols referenced in `savehist-history-variables' are ignored. | 177 Unbound symbols referenced in `savehist-additional-variables' are ignored. |
197 If AUTO-SAVE is non-nil, compare the saved contents to the one last saved, | 178 If AUTO-SAVE is non-nil, compare the saved contents to the one last saved, |
198 and don't save the buffer if they are the same." | 179 and don't save the buffer if they are the same." |
199 (interactive) | 180 (interactive) |
200 (with-temp-buffer | 181 (with-temp-buffer |
201 (insert | 182 (insert |
203 ";; Minibuffer history file, automatically generated by `savehist'.\n\n") | 184 ";; Minibuffer history file, automatically generated by `savehist'.\n\n") |
204 (let ((print-length nil) | 185 (let ((print-length nil) |
205 (print-string-length nil) | 186 (print-string-length nil) |
206 (print-level nil) | 187 (print-level nil) |
207 (print-readably t) | 188 (print-readably t) |
208 (print-quoted t)) | 189 (print-quoted t) |
209 (dolist (sym savehist-history-variables) | 190 (symbol-list (append |
191 (and savehist-save-minibuffer-history | |
192 (cons 'savehist-minibuffer-history-variables | |
193 savehist-minibuffer-history-variables)) | |
194 savehist-additional-variables))) | |
195 (dolist (sym symbol-list) | |
210 (when (boundp sym) | 196 (when (boundp sym) |
211 (let ((value (savehist-process-for-saving (symbol-value sym)))) | 197 (let ((value (savehist-process-for-saving (symbol-value sym)))) |
212 (prin1 `(setq ,sym ',value) (current-buffer)) | 198 (prin1 `(setq ,sym ',value) (current-buffer)) |
213 (insert ?\n))))) | 199 (insert ?\n))))) |
214 ;; If autosaving, avoid writing if nothing has changed since the | 200 ;; If autosaving, avoid writing if nothing has changed since the |
215 ;; last write. | 201 ;; last write. |
216 (let ((checksum (md5 (current-buffer) nil nil savehist-no-conversion))) | 202 (let ((checksum (md5 (current-buffer) nil nil savehist-no-conversion))) |
217 (unless (and auto-save (equal checksum savehist-last-checksum)) | 203 (unless (and auto-save (equal checksum savehist-last-checksum)) |
218 ;; Set file-precious-flag when saving the buffer because we | 204 ;; Set file-precious-flag when saving the buffer because we |
219 ;; don't want a half-finished write ruining the entire | 205 ;; don't want a half-finished write ruining the entire |
220 ;; history. (Remember that this is run from a timer and from | 206 ;; history. Remember that this is run from a timer and from |
221 ;; kill-emacs-hook.) | 207 ;; kill-emacs-hook, and also that multiple Emacs instances |
208 ;; could write to this file at once. | |
222 (let ((file-precious-flag t) | 209 (let ((file-precious-flag t) |
223 (coding-system-for-write savehist-coding-system)) | 210 (coding-system-for-write savehist-coding-system)) |
224 (write-region (point-min) (point-max) savehist-file nil | 211 (write-region (point-min) (point-max) savehist-file nil |
225 (unless (interactive-p) 'quiet))) | 212 (unless (interactive-p) 'quiet))) |
226 (when savehist-modes | 213 (when savehist-modes |
267 ;; The attempt worked: the object is printable. | 254 ;; The attempt worked: the object is printable. |
268 t) | 255 t) |
269 ;; The attempt failed: the object is not printable. | 256 ;; The attempt failed: the object is not printable. |
270 (error nil))))) | 257 (error nil))))) |
271 | 258 |
259 (defun savehist-minibuffer-hook () | |
260 (add-to-list 'savehist-minibuffer-history-variables | |
261 minibuffer-history-variable)) | |
262 | |
272 (provide 'savehist) | 263 (provide 'savehist) |
273 | 264 |
274 ;; arch-tag: b3ce47f4-c5ad-4ebc-ad02-73aba705cf9f | 265 ;; arch-tag: b3ce47f4-c5ad-4ebc-ad02-73aba705cf9f |
275 ;;; savehist.el ends here | 266 ;;; savehist.el ends here |