changelog shortlog tags changeset files revisions annotate raw

savehist-20+.el

changeset 66: 5b737eefe5ea
author: kim.vanwyk
date: Wed Nov 10 15:19:03 2010 +0200 (18 months ago)
permissions: -rw-r--r--
description: Adding CSharp Mode and Google Weather
1;;; savehist-20+.el --- Save minibuffer history.
2
3;; Copyright (C) 1997, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5;; Author: Hrvoje Niksic <hniksic@xemacs.org>
6;; Maintainer: Drew Adams
7;; Keywords: minibuffer history
8;; Version: 24
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
26
27;;; Commentary:
28
29;; This is Hrvoje Niksic's `savehist.el', very slightly modified to
30;; work also with versions of GNU Emacs prior to Emacs 22. (You can
31;; of course use it with Emacs 22 too.) All changes made are marked
32;; with "DADAMS".
33;;
34;; - Drew Adams
35
36;; Many editors (e.g. Vim) have the feature of saving minibuffer
37;; history to an external file after exit. This package provides the
38;; same feature in Emacs. When set up, it saves recorded minibuffer
39;; histories to a file (`~/.emacs-history' by default). Additional
40;; variables may be specified by customizing
41;; `savehist-additional-variables'.
42
43;; To use savehist, turn on savehist-mode by putting the following in
44;; `~/.emacs':
45;;
46;; (savehist-mode 1)
47;;
48;; or with customize: `M-x customize-option RET savehist-mode RET'.
49;;
50;; You can also explicitly save history with `M-x savehist-save' and
51;; load it by loading the `savehist-file' with `M-x load-file'.
52
53;; If you are using a version of Emacs that does not ship with this
54;; package, be sure to have `savehist.el' in a directory that is in
55;; your load-path, and to byte-compile it.
56;;
57;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
58;;; Change log:
59;;
60;; 2007/07/11 dadams
61;; Updated wrt latest version, in Emacs 22.1.
62;; 2005/12/01 dadams
63;; savehist-autosave: Wrapped in condition-case.
64;; 2005/10/18 dadams
65;; savehist-save: no checksum support if `md5' is not defined (e.g. Emacs 20).
66;; 2005/10/15 dadams
67;; savehist-modes Changed value from octal #o600 to decimal 384.
68;; savehist-coding-system: Change to emacs-mule (for Emacs 20).
69;;
70;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
71;;
72;;; Code:
73
74(require 'custom)
75(eval-when-compile
76 (require 'cl))
77
78;; User variables
79
80(defgroup savehist nil
81 "Save minibuffer history."
82 :version "22.1"
83 :group 'minibuffer)
84
85;;;###autoload
86(defcustom savehist-mode nil
87 "Mode for automatic saving of minibuffer history.
88Set this by calling the `savehist-mode' function or using the customize
89interface."
90 :type 'boolean
91 :set (lambda (symbol value) (savehist-mode (or value 0)))
92 :initialize 'custom-initialize-default
93 :require 'savehist
94 :group 'savehist)
95
96(defcustom savehist-save-minibuffer-history t
97 "*If non-nil, save all recorded minibuffer histories.
98If you want to save only specific histories, use `savehist-save-hook' to
99modify the value of `savehist-minibuffer-history-variables'."
100 :type 'boolean
101 :group 'savehist)
102
103(defcustom savehist-additional-variables ()
104 "*List of additional variables to save.
105Each element is a symbol whose value will be persisted across Emacs
106sessions that use savehist. The contents of variables should be
107printable with the Lisp printer. You don't need to add minibuffer
108history variables to this list, all minibuffer histories will be
109saved automatically as long as `savehist-save-minibuffer-history' is
110non-nil.
111
112User options should be saved with the customize interface. This
113list is useful for saving automatically updated variables that are not
114minibuffer histories, such as `compile-command' or `kill-ring'."
115 :type '(repeat variable)
116 :group 'savehist)
117
118(defcustom savehist-ignored-variables nil ;; '(command-history)
119 "*List of additional variables not to save."
120 :type '(repeat variable)
121 :group 'savehist)
122
123(defcustom savehist-file
124 (cond
125 ;; Backward compatibility with previous versions of savehist.
126 ((file-exists-p "~/.emacs-history") "~/.emacs-history")
127 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
128 "~/.emacs.d/history")
129 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
130 "~/.xemacs/history")
131 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
132 (t "~/.emacs-history"))
133 "*File name where minibuffer history is saved to and loaded from.
134The minibuffer history is a series of Lisp expressions loaded
135automatically when `savehist-mode' is turned on. See `savehist-mode'
136for more details.
137
138If you want your minibuffer history shared between Emacs and XEmacs,
139customize this value and make sure that `savehist-coding-system' is
140set to a coding system that exists in both emacsen."
141 :type 'file
142 :group 'savehist)
143
144;; DADAMS, 2005-10-15: changed to decimal 384.
145(defcustom savehist-file-modes 384 ; Octal: #o600
146 "*Default permissions of the history file.
147This is decimal, not octal. The default is 384 (0600 in octal).
148Set to nil to use the default permissions that Emacs uses, typically
149mandated by umask. The default is a bit more restrictive to protect
150the user's privacy."
151 :type 'integer
152 :group 'savehist)
153
154(defcustom savehist-autosave-interval (* 5 60)
155 "*The interval between autosaves of minibuffer history.
156If set to nil, disables timer-based autosaving."
157 :type 'integer
158 :group 'savehist)
159
160(defcustom savehist-mode-hook nil
161 "Hook called when `savehist-mode' is turned on."
162 :type 'hook
163 :group 'savehist)
164
165(defcustom savehist-save-hook nil
166 "Hook called by `savehist-save' before saving the variables.
167You can use this hook to influence choice and content of variables to
168save."
169 :type 'hook
170 :group 'savehist)
171
172;;; DADAMS, 2005-10-15: changed to `emacs-mule' (for Emacs 20).
173;; This should be capable of representing characters used by Emacs.
174;; We prefer UTF-8 over ISO 2022 because it is well-known outside
175;; Mule. XEmacs prir to 21.5 had UTF-8 provided by an external
176;; package which may not be loaded, which is why we check for version.
177(defvar savehist-coding-system
178 (if (and (featurep 'xemacs)
179 (<= emacs-major-version 21)
180 (< emacs-minor-version 5))
181 'iso-2022-8 ; XEmacs
182 (if (coding-system-p 'utf-8)
183 'utf-8 ; Emacs 22
184 'emacs-mule)) ; Emacs 20
185 "The coding system savehist uses for saving the minibuffer history.
186Changing this value while Emacs is running is supported, but considered
187unwise, unless you know what you are doing.")
188
189;; Internal variables.
190
191(defvar savehist-timer nil)
192
193(defvar savehist-last-checksum nil)
194
195(defvar savehist-minibuffer-history-variables nil
196 "List of minibuffer histories.
197The contents of this variable is built while Emacs is running, and saved
198along with minibuffer history. You can change its value off
199`savehist-save-hook' to influence which variables are saved.")
200
201(defconst savehist-no-conversion (if (featurep 'xemacs) 'binary 'no-conversion)
202 "Coding system without any conversion.
203This is used for calculating an internal checksum. Should be as fast
204as possible, ideally simply exposing the internal representation of
205buffer text.")
206
207(defvar savehist-loaded nil
208 "Whether the history has already been loaded.
209This prevents toggling `savehist-mode' from destroying existing
210minibuffer history.")
211
212(when (featurep 'xemacs)
213 ;; Must declare this under XEmacs, which doesn't have built-in
214 ;; minibuffer history truncation.
215 (defvar history-length 100))
216
217;; Functions.
218
219;;;###autoload
220(defun savehist-mode (arg)
221 "Toggle savehist-mode.
222Positive ARG turns on `savehist-mode'. When on, savehist-mode causes
223minibuffer history to be saved periodically and when exiting Emacs.
224When turned on for the first time in an Emacs session, it causes the
225previous minibuffer history to be loaded from `savehist-file'.
226
227This mode should normally be turned on from your Emacs init file.
228Calling it at any other time replaces your current minibuffer histories,
229which is probably undesirable."
230 (interactive "P")
231 (setq savehist-mode
232 (if (null arg)
233 (not savehist-mode)
234 (> (prefix-numeric-value arg) 0)))
235 (if (not savehist-mode)
236 (savehist-uninstall)
237 (when (and (not savehist-loaded)
238 (file-exists-p savehist-file))
239 (condition-case errvar
240 (progn
241 ;; Don't set coding-system-for-read -- we rely on the
242 ;; coding cookie to convey that information. That way, if
243 ;; the user changes the value of savehist-coding-system,
244 ;; we can still correctly load the old file.
245 (load savehist-file nil (not (interactive-p)))
246 (setq savehist-loaded t))
247 (error
248 ;; Don't install the mode if reading failed. Doing so would
249 ;; effectively destroy the user's data at the next save.
250 (setq savehist-mode nil)
251 (savehist-uninstall)
252 (signal (car errvar) (cdr errvar)))))
253 (savehist-install)
254 (run-hooks 'savehist-mode-hook))
255 ;; Return the new setting.
256 savehist-mode)
257
258;;; DADAMS, 2007-07-11: Protect by fboundp - not defined for Emacs 20.
259(when (fboundp 'add-minor-mode) (add-minor-mode 'savehist-mode ""))
260
261(defun savehist-load ()
262 "Obsolete function provided for transition from old versions of savehist.
263Don't call this from new code, use (savehist-mode 1) instead.
264
265This function loads the variables stored in `savehist-file' and turns on
266`savehist-mode'. If `savehist-file' is in the old format that doesn't
267record the value of `savehist-minibuffer-history-variables', that value
268is deducted from the contents of the file."
269 (savehist-mode 1)
270 ;; Old versions of savehist distributed with XEmacs didn't save
271 ;; savehist-minibuffer-history-variables. If that variable is nil
272 ;; after loading the file, try to intuit the intended value.
273 (when (null savehist-minibuffer-history-variables)
274 (setq savehist-minibuffer-history-variables
275 (with-temp-buffer
276 (ignore-errors
277 (insert-file-contents savehist-file))
278 (let ((vars ()) form)
279 (while (setq form (condition-case nil
280 (read (current-buffer)) (error nil)))
281 ;; Each form read is of the form (setq VAR VALUE).
282 ;; Collect VAR, i.e. (nth form 1).
283 (push (nth 1 form) vars))
284 vars)))))
285(make-obsolete 'savehist-load 'savehist-mode)
286
287(defun savehist-install ()
288 "Hook savehist into Emacs.
289Normally invoked by calling `savehist-mode' to set the minor mode.
290Installs `savehist-autosave' in `kill-emacs-hook' and on a timer.
291To undo this, call `savehist-uninstall'."
292 (add-hook 'minibuffer-setup-hook 'savehist-minibuffer-hook)
293 (add-hook 'kill-emacs-hook 'savehist-autosave)
294 ;; Install an invocation of savehist-autosave on a timer. This
295 ;; should not cause noticeable delays for users -- savehist-autosave
296 ;; executes in under 5 ms on my system.
297 (when (and savehist-autosave-interval
298 (null savehist-timer))
299 (setq savehist-timer
300 (if (featurep 'xemacs)
301 (start-itimer
302 "savehist" 'savehist-autosave savehist-autosave-interval
303 savehist-autosave-interval)
304 (run-with-timer savehist-autosave-interval savehist-autosave-interval
305 'savehist-autosave)))))
306
307(defun savehist-uninstall ()
308 "Undo installing savehist.
309Normally invoked by calling `savehist-mode' to unset the minor mode."
310 (remove-hook 'minibuffer-setup-hook 'savehist-minibuffer-hook)
311 (remove-hook 'kill-emacs-hook 'savehist-autosave)
312 (when savehist-timer
313 (if (featurep 'xemacs)
314 (delete-itimer savehist-timer)
315 (cancel-timer savehist-timer))
316 (setq savehist-timer nil)))
317
318(defun savehist-save (&optional auto-save)
319 "Save the values of minibuffer history variables.
320Unbound symbols referenced in `savehist-additional-variables' are ignored.
321If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
322 and don't save the buffer if they are the same."
323 (interactive)
324 (with-temp-buffer
325 (insert
326 (format ";; -*- mode: emacs-lisp; coding: %s -*-\n" savehist-coding-system)
327 ";; Minibuffer history file, automatically generated by `savehist'.\n\n")
328 (run-hooks 'savehist-save-hook)
329 (let ((print-length nil)
330 (print-string-length nil)
331 (print-level nil)
332 (print-readably t)
333 (print-quoted t))
334 ;; Save the minibuffer histories, along with the value of
335 ;; savehist-minibuffer-history-variables itself.
336 (when savehist-save-minibuffer-history
337 (prin1 `(setq savehist-minibuffer-history-variables
338 ',savehist-minibuffer-history-variables)
339 (current-buffer))
340 (insert ?\n)
341 (dolist (symbol savehist-minibuffer-history-variables)
342 (when (boundp symbol)
343 (let ((value (savehist-trim-history (symbol-value symbol))))
344 (when value ; don't save empty histories
345 (prin1 `(setq ,symbol ',value) (current-buffer))
346 (insert ?\n))))))
347 ;; Save the additional variables.
348 (dolist (symbol savehist-additional-variables)
349 (when (boundp symbol)
350 (let ((value (symbol-value symbol)))
351 (when (savehist-printable value)
352 (prin1 `(setq ,symbol ',value) (current-buffer))
353 (insert ?\n))))))
354 ;; DADAMS, 2005-10-18: no checksum support if `md5' is not defined (e.g. Emacs 20).
355 ;;
356 ;; If autosaving, avoid writing if nothing has changed since the
357 ;; last write.
358 (let ((checksum (and (fboundp 'md5) (md5 (current-buffer) nil nil savehist-no-conversion))))
359 (unless (and auto-save checksum (equal checksum savehist-last-checksum))
360 ;; Set file-precious-flag when saving the buffer because we
361 ;; don't want a half-finished write ruining the entire
362 ;; history. Remember that this is run from a timer and from
363 ;; kill-emacs-hook, and also that multiple Emacs instances
364 ;; could write to this file at once.
365 (let ((file-precious-flag t)
366 (coding-system-for-write savehist-coding-system))
367 (write-region (point-min) (point-max) savehist-file nil
368 (unless (interactive-p) 'quiet)))
369 (when savehist-file-modes
370 (set-file-modes savehist-file savehist-file-modes))
371 (setq savehist-last-checksum checksum)))))
372
373;; DADAMS, 2005-12-01: Wrapped in `condition-case'.
374(defun savehist-autosave ()
375 "Save the minibuffer history if it has been modified since the last
376save.
377Does nothing if `savehist-mode' is off."
378 (condition-case nil (when savehist-mode (savehist-save t)) (error nil)))
379
380(defun savehist-trim-history (value)
381 "Retain only the first `history-length' items in VALUE.
382Only used under XEmacs, which doesn't (yet) implement automatic
383trimming of history lists to `history-length' items."
384 (if (and (featurep 'xemacs)
385 (natnump history-length)
386 (> (length value) history-length))
387 ;; Equivalent to `(subseq value 0 history-length)', but doesn't
388 ;; need cl-extra at run-time.
389 (loop repeat history-length collect (pop value))
390 value))
391
392(defun savehist-printable (value)
393 "Return non-nil if VALUE is printable."
394 (cond
395 ;; Quick response for oft-encountered types known to be printable.
396 ((stringp value))
397 ((numberp value))
398 ((symbolp value))
399 (t
400 ;; For others, check explicitly.
401 (with-temp-buffer
402 (condition-case nil
403 (let ((print-readably t) (print-level nil))
404 ;; Print the value into a buffer...
405 (prin1 value (current-buffer))
406 ;; ...and attempt to read it.
407 (read (point-min-marker))
408 ;; The attempt worked: the object is printable.
409 t)
410 ;; The attempt failed: the object is not printable.
411 (error nil))))))
412
413(defun savehist-minibuffer-hook ()
414 (unless (or (eq minibuffer-history-variable t)
415 ;; XEmacs sets minibuffer-history-variable to t to mean "no
416 ;; history is being recorded".
417 (memq minibuffer-history-variable savehist-ignored-variables))
418 (add-to-list 'savehist-minibuffer-history-variables
419 minibuffer-history-variable)))
420
421(provide 'savehist-20+)
422
423;; arch-tag: b3ce47f4-c5ad-4ebc-ad02-73aba705cf9f
424;;; savehist-20+.el ends here
425