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
|
173
|
98 ;; Info, lookup, and bookmark historys
|
|
99 Info-minibuffer-history
|
|
100 Manual-page-minibuffer-history
|
|
101
|
153
|
102 ;; GNU Emacs-specific:
|
|
103 ;; Extended commands
|
157
|
104 extended-command-history)
|
153
|
105 "*List of symbols to be saved.
|
157
|
106 Every symbol should refer to a variable. The variable will be saved
|
|
107 only if it is bound and has a non-nil value. Thus it is safe to
|
|
108 specify a superset of the variables a user is expected to want to
|
|
109 save.
|
153
|
110
|
|
111 Default value contains minibuffer history variables used by XEmacs, GNU
|
157
|
112 Emacs and Viper (uh-oh)."
|
153
|
113 :type '(repeat (symbol :tag "Variable"))
|
157
|
114 :group 'savehist)
|
153
|
115
|
|
116 (defcustom savehist-file "~/.emacs-history"
|
|
117 "*File name to save minibuffer history to.
|
|
118 The minibuffer history is a series of Lisp expressions, which should be
|
|
119 loaded using `savehist-load' from your .emacs. See `savehist-load' for
|
|
120 more details."
|
|
121 :type 'file
|
|
122 :group 'savehist)
|
|
123
|
|
124 (defcustom savehist-length 100
|
|
125 "*Maximum length of a minibuffer list.
|
|
126 If set to nil, the length is unlimited."
|
|
127 :type '(choice integer
|
|
128 (const :tag "Unlimited" nil))
|
|
129 :group 'savehist)
|
|
130
|
157
|
131 (defcustom savehist-modes 384
|
171
|
132 "*Default permissions of the history file.
|
|
133 This is decimal, not octal. The default is 384 (0600 in octal)."
|
157
|
134 :type 'integer
|
|
135 :group 'savehist)
|
|
136
|
153
|
137
|
|
138 ;; Functions
|
|
139
|
|
140 ;;;###autoload
|
171
|
141 (defun savehist-load (&optional no-hook)
|
|
142 "Load the minibuffer histories from `savehist-file'.
|
|
143 Unless NO-HOOK is specified, the function will also add the save function
|
|
144 to `kill-emacs-hook', thus ensuring that the minibuffer contents will be
|
|
145 saved before leaving Emacs.
|
153
|
146
|
|
147 This function should be normally used from your Emacs init file. Since it
|
171
|
148 removes your current minibuffer histories, it is unwise to call it at any
|
|
149 other time."
|
153
|
150 (interactive "P")
|
171
|
151 (unless no-hook
|
153
|
152 (add-hook 'kill-emacs-hook 'savehist-save))
|
157
|
153 (load savehist-file t))
|
153
|
154
|
|
155 ;;;###autoload
|
|
156 (defun savehist-save ()
|
|
157 "Save the histories from `savehist-history-variables' to `savehist-file'.
|
|
158 A variable will be saved if it is bound and non-nil."
|
|
159 (interactive)
|
|
160 (save-excursion
|
|
161 ;; Is it wise to junk `find-file-hooks' just like that? How else
|
|
162 ;; should I avoid font-lock et al.?
|
|
163 (let ((find-file-hooks nil)
|
|
164 (buffer-exists-p (get-file-buffer savehist-file)))
|
|
165 (set-buffer (find-file-noselect savehist-file))
|
|
166 (unwind-protect
|
|
167 (progn
|
|
168 (erase-buffer)
|
|
169 (insert
|
|
170 ";; -*- emacs-lisp -*-\n"
|
|
171 ";; Minibuffer history file.\n\n"
|
|
172 ";; This file is automatically generated by `savehist-save'"
|
|
173 " or when\n"
|
|
174 ";; exiting Emacs.\n"
|
|
175 ";; Do not edit. Unless you really want to, that is.\n\n")
|
171
|
176 (let ((print-length nil)
|
|
177 (print-string-length nil)
|
|
178 (print-level nil)
|
|
179 (print-readably t))
|
|
180 (dolist (sym savehist-history-variables)
|
|
181 (when (and (boundp sym)
|
|
182 (symbol-value sym))
|
|
183 (prin1
|
|
184 `(setq ,sym (quote ,(savehist-delimit (symbol-value sym)
|
|
185 savehist-length)))
|
|
186 (current-buffer))
|
|
187 (insert ?\n))))
|
157
|
188 (save-buffer)
|
|
189 (set-file-modes savehist-file savehist-modes))
|
153
|
190 (or buffer-exists-p
|
|
191 (kill-buffer (current-buffer)))))))
|
|
192
|
157
|
193 ;; If ARG is a list with less than N elements, return it, else return
|
|
194 ;; its subsequence of N elements. If N is nil or ARG is not a list,
|
|
195 ;; always return ARG.
|
153
|
196 (defun savehist-delimit (arg n)
|
|
197 (if (and n
|
|
198 (listp arg)
|
|
199 (> (length arg) n))
|
|
200 (subseq arg 0 n)
|
|
201 arg))
|
|
202
|
|
203 (provide 'savehist)
|
|
204
|
|
205 ;;; savehist.el ends here
|