153
|
1 ;;; savehist.el --- Save minibuffer history
|
|
2
|
|
3 ;; Copyright (c) 1997 Free Software Foundation
|
|
4
|
|
5 ;; Author: Hrvoje Niksic <hniksic@srce.hr>
|
|
6 ;; Keywords: minibuffer
|
171
|
7 ;; Version: 0.4
|
153
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs 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.
|
|
15
|
|
16 ;; XEmacs 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
|
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
|
26 ;;; Synched up with: not in FSF
|
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;; Many editors (e.g. Vim) have the feature of saving minibuffer
|
|
31 ;; history to an external file after exit. This package provides the
|
|
32 ;; same feature in Emacs. When Emacs is about the exit,
|
|
33 ;; `savehist-save' will dump the contents of various minibuffer
|
|
34 ;; histories (as determined by `savehist-history-variables') to a save
|
|
35 ;; file (`~/.emacs-history' by default). Although the package was
|
|
36 ;; designed for saving the minibuffer histories, any variables can be
|
|
37 ;; saved that way.
|
|
38
|
|
39 ;; To use savehist, put the following to `~/.emacs':
|
|
40 ;;
|
|
41 ;; (require 'savehist)
|
|
42 ;; (savehist-load)
|
|
43
|
|
44 ;; Be sure to have `savehist.el' in a directory that is in your
|
|
45 ;; load-path, and byte-compile it.
|
|
46
|
|
47 ;; This code should work on XEmacs 19.14 and later, as well as GNU
|
171
|
48 ;; Emacs 19.34 and later. It requires the Customize library, however
|
|
49 ;; (shipped with XEmacs 19.15 and later, and with GNU Emacs 20.x).
|
153
|
50
|
|
51
|
|
52 ;;; Code:
|
|
53
|
|
54 (require 'custom)
|
|
55
|
|
56 ;; User variables
|
|
57
|
|
58 (defgroup savehist nil
|
|
59 "Save minibuffer history."
|
|
60 :group 'minibuffer)
|
|
61
|
|
62
|
|
63 (defcustom savehist-history-variables
|
|
64 '(
|
|
65 ;; Catch-all minibuffer history
|
|
66 minibuffer-history
|
|
67 ;; File-oriented commands
|
|
68 file-name-history
|
|
69 ;; Regexp-related reads
|
|
70 regexp-history
|
|
71 ;; Searches in minibuffer (via `M-r' and such)
|
|
72 minibuffer-history-search-history
|
|
73 ;; Query replace
|
|
74 query-replace-history
|
|
75 ;; eval-expression (`M-:')
|
|
76 read-expression-history
|
|
77 ;; shell-command (`M-!')
|
|
78 shell-command-history
|
157
|
79 ;; compile
|
|
80 compile-history
|
|
81 ;; find-tag (`M-.')
|
|
82 find-tag-history
|
|
83 ;; grep
|
|
84 grep-history
|
153
|
85 ;; Viper stuff
|
|
86 vip-ex-history vip-search-history
|
|
87 vip-replace1-history vip-replace2-history
|
|
88 vip-shell-history vip-search-history
|
|
89
|
|
90 ;; XEmacs-specific:
|
|
91 ;; Buffer-related commands
|
|
92 buffer-history
|
|
93 ;; Reads of variables and functions
|
|
94 variable-history function-history
|
|
95 ;; Extended commands
|
|
96 read-command-history
|
|
97
|
|
98 ;; GNU Emacs-specific:
|
|
99 ;; Extended commands
|
157
|
100 extended-command-history)
|
153
|
101 "*List of symbols to be saved.
|
157
|
102 Every symbol should refer to a variable. The variable will be saved
|
|
103 only if it is bound and has a non-nil value. Thus it is safe to
|
|
104 specify a superset of the variables a user is expected to want to
|
|
105 save.
|
153
|
106
|
|
107 Default value contains minibuffer history variables used by XEmacs, GNU
|
157
|
108 Emacs and Viper (uh-oh)."
|
153
|
109 :type '(repeat (symbol :tag "Variable"))
|
157
|
110 :group 'savehist)
|
153
|
111
|
|
112 (defcustom savehist-file "~/.emacs-history"
|
|
113 "*File name to save minibuffer history to.
|
|
114 The minibuffer history is a series of Lisp expressions, which should be
|
|
115 loaded using `savehist-load' from your .emacs. See `savehist-load' for
|
|
116 more details."
|
|
117 :type 'file
|
|
118 :group 'savehist)
|
|
119
|
|
120 (defcustom savehist-length 100
|
|
121 "*Maximum length of a minibuffer list.
|
|
122 If set to nil, the length is unlimited."
|
|
123 :type '(choice integer
|
|
124 (const :tag "Unlimited" nil))
|
|
125 :group 'savehist)
|
|
126
|
157
|
127 (defcustom savehist-modes 384
|
171
|
128 "*Default permissions of the history file.
|
|
129 This is decimal, not octal. The default is 384 (0600 in octal)."
|
157
|
130 :type 'integer
|
|
131 :group 'savehist)
|
|
132
|
153
|
133
|
|
134 ;; Functions
|
|
135
|
|
136 ;;;###autoload
|
171
|
137 (defun savehist-load (&optional no-hook)
|
|
138 "Load the minibuffer histories from `savehist-file'.
|
|
139 Unless NO-HOOK is specified, the function will also add the save function
|
|
140 to `kill-emacs-hook', thus ensuring that the minibuffer contents will be
|
|
141 saved before leaving Emacs.
|
153
|
142
|
|
143 This function should be normally used from your Emacs init file. Since it
|
171
|
144 removes your current minibuffer histories, it is unwise to call it at any
|
|
145 other time."
|
153
|
146 (interactive "P")
|
171
|
147 (unless no-hook
|
153
|
148 (add-hook 'kill-emacs-hook 'savehist-save))
|
157
|
149 (load savehist-file t))
|
153
|
150
|
|
151 ;;;###autoload
|
|
152 (defun savehist-save ()
|
|
153 "Save the histories from `savehist-history-variables' to `savehist-file'.
|
|
154 A variable will be saved if it is bound and non-nil."
|
|
155 (interactive)
|
|
156 (save-excursion
|
|
157 ;; Is it wise to junk `find-file-hooks' just like that? How else
|
|
158 ;; should I avoid font-lock et al.?
|
|
159 (let ((find-file-hooks nil)
|
|
160 (buffer-exists-p (get-file-buffer savehist-file)))
|
|
161 (set-buffer (find-file-noselect savehist-file))
|
|
162 (unwind-protect
|
|
163 (progn
|
|
164 (erase-buffer)
|
|
165 (insert
|
|
166 ";; -*- emacs-lisp -*-\n"
|
|
167 ";; Minibuffer history file.\n\n"
|
|
168 ";; This file is automatically generated by `savehist-save'"
|
|
169 " or when\n"
|
|
170 ";; exiting Emacs.\n"
|
|
171 ";; Do not edit. Unless you really want to, that is.\n\n")
|
171
|
172 (let ((print-length nil)
|
|
173 (print-string-length nil)
|
|
174 (print-level nil)
|
|
175 (print-readably t))
|
|
176 (dolist (sym savehist-history-variables)
|
|
177 (when (and (boundp sym)
|
|
178 (symbol-value sym))
|
|
179 (prin1
|
|
180 `(setq ,sym (quote ,(savehist-delimit (symbol-value sym)
|
|
181 savehist-length)))
|
|
182 (current-buffer))
|
|
183 (insert ?\n))))
|
157
|
184 (save-buffer)
|
|
185 (set-file-modes savehist-file savehist-modes))
|
153
|
186 (or buffer-exists-p
|
|
187 (kill-buffer (current-buffer)))))))
|
|
188
|
157
|
189 ;; If ARG is a list with less than N elements, return it, else return
|
|
190 ;; its subsequence of N elements. If N is nil or ARG is not a list,
|
|
191 ;; always return ARG.
|
153
|
192 (defun savehist-delimit (arg n)
|
|
193 (if (and n
|
|
194 (listp arg)
|
|
195 (> (length arg) n))
|
|
196 (subseq arg 0 n)
|
|
197 arg))
|
|
198
|
|
199 (provide 'savehist)
|
|
200
|
|
201 ;;; savehist.el ends here
|