comparison lisp/savehist.el @ 66106:9b8e76617c8c

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