[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Saving minibuffer history

From: Stefan Monnier
Subject: Re: Saving minibuffer history
Date: Tue, 25 Oct 2005 00:37:55 -0400
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

>> I've now applied your changes to my version.
>> http://fly.srk.fer.hr/~hniksic/emacs/savehist.el

Here's yet another suggested patch.
It includes the change of default of savehist-file and introduces
savehist-mode, which would become the main advertized way to turn the
feature ON (can also be done via Customize) in place of savehist-load.

There are also minor changes: don't save empty histories, and try to avoid
computing the checksum when no minibuffer input occurred since last check.


--- savehist.el 24 oct 2005 13:15:48 -0400      1.8
+++ savehist.el 25 oct 2005 00:35:03 -0400      
@@ -70,8 +70,14 @@
   :type '(repeat (symbol :tag "Variable"))
   :group 'savehist)
-(defcustom savehist-file "~/.emacs-history"
-  "File name to save minibuffer history to.
+(defcustom savehist-file
+  (cond
+   ;; Backward compatibility with previous versions of savehist.
+   ((file-exists-p "~/.emacs-history") "~/.emacs-history")
+   ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
+    "~/.emacs.d/history")
+   (t "~/.emacs-history"))
+  "*File name to save minibuffer history to.
 The minibuffer history is a series of Lisp expressions, which should be
 loaded using `savehist-load' from your .emacs.  See `savehist-load' for
 more details."
@@ -139,7 +145,8 @@
               "savehist" 'savehist-autosave savehist-autosave-interval
-           (run-with-timer savehist-autosave-interval 
+           (run-with-timer savehist-autosave-interval
+                            savehist-autosave-interval
 (defun savehist-uninstall ()
@@ -152,6 +159,17 @@
       (cancel-timer savehist-timer))
     (setq savehist-timer nil)))
+(defvar savehist-loaded nil)
+(define-minor-mode savehist-mode
+  "Minor mode that preserves minibuffer history across Emacs sessions."
+  :lighter nil
+  (if (not savehist-mode)
+      (savehist-uninstall)
+    (unless savehist-loaded (savehist-load 'no-install))
+    (savehist-install)))
 (defun savehist-load (&optional no-install)
   "Load the minibuffer histories from `savehist-file'.
@@ -168,9 +186,17 @@
   ;; the user changes the value of savehist-coding-system, we can
   ;; still correctly load the old file.
   (load savehist-file t (not (interactive-p)))
+  ;; Remember we've already loaded it, so we can try and avoid loading it
+  ;; several times, which would risk throwing away history info.
+  (setq savehist-loaded t)
   (unless no-install
+(defvar savehist-changed nil
+  "If nil, the history hasn't changed.
+This variable is only used as an optimization, to avoid generating a new
+savehist state and checksum, only to discover that it hasn't changed.")
 (defun savehist-save (&optional auto-save)
   "Save the values of minibuffer history variables.
@@ -178,9 +204,11 @@
 If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
  and don't save the buffer if they are the same."
+  (unless (and auto-save (not savehist-changed))
-     (format ";; -*- mode: emacs-lisp; coding: %s -*-\n" 
+       (format ";; -*- mode: emacs-lisp; coding: %s -*-\n"
+               savehist-coding-system)
      ";; Minibuffer history file, automatically generated by `savehist'.\n\n")
     (let ((print-length nil)
          (print-string-length nil)
@@ -195,8 +223,10 @@
       (dolist (sym symbol-list)
        (when (boundp sym)
          (let ((value (savehist-process-for-saving (symbol-value sym))))
+              ;; Don't save empty histories.
+              (when value
            (prin1 `(setq ,sym ',value) (current-buffer))
-           (insert ?\n)))))
+                (insert ?\n))))))
     ;; If autosaving, avoid writing if nothing has changed since the
     ;; last write.
     (let ((checksum (md5 (current-buffer) nil nil savehist-no-conversion)))
@@ -212,7 +242,8 @@
                        (unless (interactive-p) 'quiet)))
        (when savehist-modes
          (set-file-modes savehist-file savehist-modes))
-       (setq savehist-last-checksum checksum)))))
+          (setq savehist-changed nil)
+          (setq savehist-last-checksum checksum))))))
 (defun savehist-autosave ()
   "Save the minibuffer history if it has been modified since the last save."
@@ -257,6 +288,7 @@
       (error nil)))))
 (defun savehist-minibuffer-hook ()
+  (setq savehist-changed t)
   (add-to-list 'savehist-minibuffer-history-variables

reply via email to

[Prev in Thread] Current Thread [Next in Thread]