diff options
| author | Stefan Monnier | 2005-10-24 17:21:30 +0000 |
|---|---|---|
| committer | Stefan Monnier | 2005-10-24 17:21:30 +0000 |
| commit | 2a2b5b2998012b700f263d0d42fd6fcac00ec51a (patch) | |
| tree | b554874ea279c9115b92452ada78b6bc2b15b2fe | |
| parent | dc447cc519b9e51d0f5a86c7da84ce7bc7648bf8 (diff) | |
| download | emacs-2a2b5b2998012b700f263d0d42fd6fcac00ec51a.tar.gz emacs-2a2b5b2998012b700f263d0d42fd6fcac00ec51a.zip | |
Require CL while compiling.
(savehist-history-variables): Remove.
(savehist-save-minibuffer-history, savehist-additional-variables)
(savehist-minibuffer-history-variables): New vars.
(savehist-save): Use them.
(savehist-uninstall, savehist-minibuffer-hook): New funs.
(savehist-install): New fun, extracted from savehist-load.
(savehist-load): Use them.
| -rw-r--r-- | lisp/savehist.el | 190 |
1 files changed, 90 insertions, 100 deletions
diff --git a/lisp/savehist.el b/lisp/savehist.el index 3021f2805bb..98012d02114 100644 --- a/lisp/savehist.el +++ b/lisp/savehist.el | |||
| @@ -1,10 +1,10 @@ | |||
| 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 | ||
| @@ -27,24 +27,25 @@ | |||
| 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 | ||
| @@ -52,66 +53,25 @@ | |||
| 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 | grep-find-history | ||
| 78 | ;; Viper stuff | ||
| 79 | vip-ex-history vip-search-history | ||
| 80 | vip-replace1-history vip-replace2-history | ||
| 81 | vip-shell-history vip-search-history | ||
| 82 | |||
| 83 | ;; XEmacs-specific: | ||
| 84 | ;; Buffer-related commands | ||
| 85 | buffer-history | ||
| 86 | ;; Reads of variables and functions | ||
| 87 | variable-history function-history | ||
| 88 | ;; Extended commands | ||
| 89 | read-command-history | ||
| 90 | |||
| 91 | ;; Info, lookup, and bookmark historys | ||
| 92 | Info-minibuffer-history | ||
| 93 | Info-search-history | ||
| 94 | Manual-page-minibuffer-history | ||
| 95 | |||
| 96 | ;; Emacs-specific: | ||
| 97 | ;; Extended commands | ||
| 98 | extended-command-history) | ||
| 99 | "*List of symbols to be saved. | ||
| 100 | Every symbol should refer to a variable. The variable will be saved | ||
| 101 | only if it is bound and has a non-nil value. Thus it is safe to | ||
| 102 | specify a superset of the variables a user is expected to want to | ||
| 103 | save. | ||
| 104 | |||
| 105 | Default value contains minibuffer history variables used by Emacs, XEmacs, | ||
| 106 | and Viper (uh-oh). Note that, if you customize this variable, you | ||
| 107 | can lose the benefit of future versions of Emacs adding new values to | ||
| 108 | the list. Because of that it might be more useful to add values using | ||
| 109 | `add-to-list'." | ||
| 110 | :type '(repeat (symbol :tag "Variable")) | 70 | :type '(repeat (symbol :tag "Variable")) |
| 111 | :group 'savehist) | 71 | :group 'savehist) |
| 112 | 72 | ||
| 113 | (defcustom savehist-file "~/.emacs-history" | 73 | (defcustom savehist-file "~/.emacs-history" |
| 114 | "*File name to save minibuffer history to. | 74 | "File name to save minibuffer history to. |
| 115 | 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 |
| 116 | loaded using `savehist-load' from your .emacs. See `savehist-load' for | 76 | loaded using `savehist-load' from your .emacs. See `savehist-load' for |
| 117 | more details." | 77 | more details." |
| @@ -119,14 +79,15 @@ more details." | |||
| 119 | :group 'savehist) | 79 | :group 'savehist) |
| 120 | 80 | ||
| 121 | (defcustom savehist-length 100 | 81 | (defcustom savehist-length 100 |
| 122 | "*Maximum length of a minibuffer list. | 82 | "Maximum length of a minibuffer list. |
| 123 | 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." | ||
| 124 | :type '(choice integer | 85 | :type '(choice integer |
| 125 | (const :tag "Unlimited" nil)) | 86 | (const :tag "Unlimited" nil)) |
| 126 | :group 'savehist) | 87 | :group 'savehist) |
| 127 | 88 | ||
| 128 | (defcustom savehist-modes #o600 | 89 | (defcustom savehist-modes #o600 |
| 129 | "*Default permissions of the history file. | 90 | "Default permissions of the history file. |
| 130 | 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). |
| 131 | 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 |
| 132 | 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 |
| @@ -135,7 +96,7 @@ the user's privacy." | |||
| 135 | :group 'savehist) | 96 | :group 'savehist) |
| 136 | 97 | ||
| 137 | (defcustom savehist-autosave-interval (* 5 60) | 98 | (defcustom savehist-autosave-interval (* 5 60) |
| 138 | "*The interval during which savehist should autosave the history buffer." | 99 | "The interval during which savehist should autosave the history buffer." |
| 139 | :type 'integer | 100 | :type 'integer |
| 140 | :group 'savehist) | 101 | :group 'savehist) |
| 141 | 102 | ||
| @@ -155,46 +116,65 @@ unwise, unless you know what you are doing.") | |||
| 155 | 116 | ||
| 156 | (defvar savehist-last-checksum nil) | 117 | (defvar savehist-last-checksum nil) |
| 157 | 118 | ||
| 119 | (defvar savehist-minibuffer-history-variables nil) | ||
| 120 | |||
| 158 | (defconst savehist-no-conversion (if (featurep 'xemacs) 'binary 'no-conversion) | 121 | (defconst savehist-no-conversion (if (featurep 'xemacs) 'binary 'no-conversion) |
| 159 | ;; FIXME: Why not use savehist-coding-system? | 122 | "Coding system without conversion, only used for calculating checksums. |
| 160 | "Coding system without conversion, only used for calculating checksums.") | 123 | It should be as discriminating as `savehist-coding-system' but faster.") |
| 161 | 124 | ||
| 162 | ;; 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))) | ||
| 163 | 154 | ||
| 164 | ;;;###autoload | 155 | ;;;###autoload |
| 165 | (defun savehist-load (&optional no-hook) | 156 | (defun savehist-load (&optional no-install) |
| 166 | "Load the minibuffer histories from `savehist-file'. | 157 | "Load the minibuffer histories from `savehist-file'. |
| 167 | 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 |
| 168 | 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 |
| 169 | will be saved before leaving Emacs. | 160 | history is saved before leaving Emacs. |
| 170 | 161 | ||
| 171 | 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 |
| 172 | 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 |
| 173 | other time." | 164 | any other time." |
| 174 | (interactive "P") | 165 | (interactive "P") |
| 175 | (unless no-hook | ||
| 176 | (add-hook 'kill-emacs-hook 'savehist-autosave) | ||
| 177 | ;; Install an invocation of savehist-autosave on a timer. This | ||
| 178 | ;; should not cause a noticeable delay -- savehist-autosave | ||
| 179 | ;; executes in under 5 ms on my system. | ||
| 180 | (unless savehist-timer | ||
| 181 | (setq savehist-timer | ||
| 182 | (if (featurep 'xemacs) | ||
| 183 | (start-itimer | ||
| 184 | "savehist" 'savehist-autosave savehist-autosave-interval | ||
| 185 | savehist-autosave-interval) | ||
| 186 | (run-with-idle-timer savehist-autosave-interval savehist-autosave-interval | ||
| 187 | 'savehist-autosave))))) | ||
| 188 | ;; 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 |
| 189 | ;; and the coding cookie to convey that information. That way, if | 167 | ;; and the coding cookie to convey that information. That way, if |
| 190 | ;; the user changes the value of savehist-coding-system, we can | 168 | ;; the user changes the value of savehist-coding-system, we can |
| 191 | ;; still correctly load the old file. | 169 | ;; still correctly load the old file. |
| 192 | (load savehist-file t (not (interactive-p)))) | 170 | (load savehist-file t (not (interactive-p))) |
| 171 | (unless no-install | ||
| 172 | (savehist-install))) | ||
| 193 | 173 | ||
| 194 | ;;;###autoload | 174 | ;;;###autoload |
| 195 | (defun savehist-save (&optional auto-save) | 175 | (defun savehist-save (&optional auto-save) |
| 196 | "Save the histories from `savehist-history-variables' to `savehist-file'. | 176 | "Save the values of minibuffer history variables. |
| 197 | Unbound symbols referenced in `savehist-history-variables' are ignored. | 177 | Unbound symbols referenced in `savehist-additional-variables' are ignored. |
| 198 | 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, |
| 199 | and don't save the buffer if they are the same." | 179 | and don't save the buffer if they are the same." |
| 200 | (interactive) | 180 | (interactive) |
| @@ -206,8 +186,13 @@ If AUTO-SAVE is non-nil, compare the saved contents to the one last saved, | |||
| 206 | (print-string-length nil) | 186 | (print-string-length nil) |
| 207 | (print-level nil) | 187 | (print-level nil) |
| 208 | (print-readably t) | 188 | (print-readably t) |
| 209 | (print-quoted t)) | 189 | (print-quoted t) |
| 210 | (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) | ||
| 211 | (when (boundp sym) | 196 | (when (boundp sym) |
| 212 | (let ((value (savehist-process-for-saving (symbol-value sym)))) | 197 | (let ((value (savehist-process-for-saving (symbol-value sym)))) |
| 213 | (prin1 `(setq ,sym ',value) (current-buffer)) | 198 | (prin1 `(setq ,sym ',value) (current-buffer)) |
| @@ -218,8 +203,9 @@ If AUTO-SAVE is non-nil, compare the saved contents to the one last saved, | |||
| 218 | (unless (and auto-save (equal checksum savehist-last-checksum)) | 203 | (unless (and auto-save (equal checksum savehist-last-checksum)) |
| 219 | ;; Set file-precious-flag when saving the buffer because we | 204 | ;; Set file-precious-flag when saving the buffer because we |
| 220 | ;; don't want a half-finished write ruining the entire | 205 | ;; don't want a half-finished write ruining the entire |
| 221 | ;; history. (Remember that this is run from a timer and from | 206 | ;; history. Remember that this is run from a timer and from |
| 222 | ;; kill-emacs-hook.) | 207 | ;; kill-emacs-hook, and also that multiple Emacs instances |
| 208 | ;; could write to this file at once. | ||
| 223 | (let ((file-precious-flag t) | 209 | (let ((file-precious-flag t) |
| 224 | (coding-system-for-write savehist-coding-system)) | 210 | (coding-system-for-write savehist-coding-system)) |
| 225 | (write-region (point-min) (point-max) savehist-file nil | 211 | (write-region (point-min) (point-max) savehist-file nil |
| @@ -270,6 +256,10 @@ If AUTO-SAVE is non-nil, compare the saved contents to the one last saved, | |||
| 270 | ;; The attempt failed: the object is not printable. | 256 | ;; The attempt failed: the object is not printable. |
| 271 | (error nil))))) | 257 | (error nil))))) |
| 272 | 258 | ||
| 259 | (defun savehist-minibuffer-hook () | ||
| 260 | (add-to-list 'savehist-minibuffer-history-variables | ||
| 261 | minibuffer-history-variable)) | ||
| 262 | |||
| 273 | (provide 'savehist) | 263 | (provide 'savehist) |
| 274 | 264 | ||
| 275 | ;; arch-tag: b3ce47f4-c5ad-4ebc-ad02-73aba705cf9f | 265 | ;; arch-tag: b3ce47f4-c5ad-4ebc-ad02-73aba705cf9f |