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
|
|
7 ;; Version: 0.2
|
|
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
|
|
48 ;; Emacs 19.34 and later.
|
|
49
|
|
50
|
|
51 ;;; Code:
|
|
52
|
|
53 (require 'custom)
|
|
54
|
|
55 ;; User variables
|
|
56
|
|
57 (defgroup savehist nil
|
|
58 "Save minibuffer history."
|
|
59 :group 'minibuffer)
|
|
60
|
|
61
|
|
62 (defcustom savehist-history-variables
|
|
63 '(
|
|
64 ;; Catch-all minibuffer history
|
|
65 minibuffer-history
|
|
66 ;; File-oriented commands
|
|
67 file-name-history
|
|
68 ;; Regexp-related reads
|
|
69 regexp-history
|
|
70 ;; Searches in minibuffer (via `M-r' and such)
|
|
71 minibuffer-history-search-history
|
|
72 ;; Query replace
|
|
73 query-replace-history
|
|
74 ;; eval-expression (`M-:')
|
|
75 read-expression-history
|
|
76 ;; shell-command (`M-!')
|
|
77 shell-command-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 ;; GNU Emacs-specific:
|
|
92 ;; Extended commands
|
|
93 extended-command-history
|
|
94
|
|
95 ;; This is not a list, but it's cool to have it anyway, since it's
|
|
96 ;; minibuffer history too.
|
|
97 compile-command)
|
|
98 "*List of symbols to be saved.
|
|
99 Every symbol should refer to a variable. The variable will be saved only
|
|
100 if it is bound 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 save.
|
|
102
|
|
103 Default value contains minibuffer history variables used by XEmacs, GNU
|
|
104 Emacs and Viper (uh-oh). `compile-command' was added for good measure."
|
|
105 :type '(repeat (symbol :tag "Variable"))
|
|
106 :group 'minibuffer)
|
|
107
|
|
108 (defcustom savehist-file "~/.emacs-history"
|
|
109 "*File name to save minibuffer history to.
|
|
110 The minibuffer history is a series of Lisp expressions, which should be
|
|
111 loaded using `savehist-load' from your .emacs. See `savehist-load' for
|
|
112 more details."
|
|
113 :type 'file
|
|
114 :group 'savehist)
|
|
115
|
|
116 (defcustom savehist-length 100
|
|
117 "*Maximum length of a minibuffer list.
|
|
118 If set to nil, the length is unlimited."
|
|
119 :type '(choice integer
|
|
120 (const :tag "Unlimited" nil))
|
|
121 :group 'savehist)
|
|
122
|
|
123
|
|
124 ;; Functions
|
|
125
|
|
126 ;;;###autoload
|
|
127 (defun savehist-load (&optional prefix)
|
|
128 "Load the histories saved to `savehist-file'.
|
|
129 Unless PREFIX is non-nil, the function will also add the save function to
|
|
130 `kill-emacs-hook'.
|
|
131
|
|
132 This function should be normally used from your Emacs init file. Since it
|
|
133 removes your current minibuffer histories (if any), it is unwise to call it
|
|
134 at any other time."
|
|
135 (interactive "P")
|
|
136 (unless prefix
|
|
137 (add-hook 'kill-emacs-hook 'savehist-save))
|
|
138 (when (file-exists-p savehist-file)
|
|
139 (load savehist-file)))
|
|
140
|
|
141 ;;;###autoload
|
|
142 (defun savehist-save ()
|
|
143 "Save the histories from `savehist-history-variables' to `savehist-file'.
|
|
144 A variable will be saved if it is bound and non-nil."
|
|
145 (interactive)
|
|
146 (save-excursion
|
|
147 ;; Is it wise to junk `find-file-hooks' just like that? How else
|
|
148 ;; should I avoid font-lock et al.?
|
|
149 (let ((find-file-hooks nil)
|
|
150 (buffer-exists-p (get-file-buffer savehist-file)))
|
|
151 (set-buffer (find-file-noselect savehist-file))
|
|
152 (unwind-protect
|
|
153 (progn
|
|
154 (erase-buffer)
|
|
155 (insert
|
|
156 ";; -*- emacs-lisp -*-\n"
|
|
157 ";; Minibuffer history file.\n\n"
|
|
158 ";; This file is automatically generated by `savehist-save'"
|
|
159 " or when\n"
|
|
160 ";; exiting Emacs.\n"
|
|
161 ";; Do not edit. Unless you really want to, that is.\n\n")
|
|
162 (dolist (sym savehist-history-variables)
|
|
163 (when (and (boundp sym)
|
|
164 (symbol-value sym))
|
|
165 (prin1
|
|
166 `(setq ,sym (quote ,(savehist-delimit (symbol-value sym)
|
|
167 savehist-length)))
|
|
168 (current-buffer))
|
|
169 (insert ?\n)))
|
|
170 (save-buffer))
|
|
171 (or buffer-exists-p
|
|
172 (kill-buffer (current-buffer)))))))
|
|
173
|
|
174 ;; If ARG is a arg with less than N elements, return it, else return
|
|
175 ;; its subsequence of N elements. If N is nil, always return ARG. If
|
|
176 ;; ARG is not a list, just return it.
|
|
177 (defun savehist-delimit (arg n)
|
|
178 (if (and n
|
|
179 (listp arg)
|
|
180 (> (length arg) n))
|
|
181 (subseq arg 0 n)
|
|
182 arg))
|
|
183
|
|
184 (provide 'savehist)
|
|
185
|
|
186 ;;; savehist.el ends here
|