* net/tramp.el (tramp-read-passwd): Suspend the timers while reading
[bpt/emacs.git] / lisp / savehist.el
index c2674fd..6abc29c 100644 (file)
@@ -1,8 +1,9 @@
-;;; savehist.el --- Save minibuffer history.
+;;; savehist.el --- Save minibuffer history
 
-;; Copyright (C) 1997, 2005, 2006, 2007, 2008  Free Software Foundation, Inc.
+;; Copyright (C) 1997, 2005-2014 Free Software Foundation, Inc.
 
 ;; Author: Hrvoje Niksic <hniksic@xemacs.org>
+;; Maintainer: emacs-devel@gnu.org
 ;; Keywords: minibuffer
 ;; Version: 24
 
@@ -48,7 +49,7 @@
 
 (require 'custom)
 (eval-when-compile
-  (require 'cl))
+  (if (featurep 'xemacs) (require 'cl)))
 
 ;; User variables
 
   :version "22.1"
   :group 'minibuffer)
 
-;;;###autoload
-(defcustom savehist-mode nil
-  "Mode for automatic saving of minibuffer history.
-Set this by calling the `savehist-mode' function or using the customize
-interface."
-  :type 'boolean
-  :set (lambda (symbol value) (savehist-mode (or value 0)))
-  :initialize 'custom-initialize-default
-  :require 'savehist
-  :group 'savehist)
-
 (defcustom savehist-save-minibuffer-history t
-  "*If non-nil, save all recorded minibuffer histories.
-If you want to save only specific histories, use `savehist-save-hook' to
-modify the value of `savehist-minibuffer-history-variables'."
+  "If non-nil, save all recorded minibuffer histories.
+If you want to save only specific histories, use `savehist-save-hook'
+to modify the value of `savehist-minibuffer-history-variables'."
   :type 'boolean
   :group 'savehist)
 
 (defcustom savehist-additional-variables ()
-  "*List of additional variables to save.
+  "List of additional variables to save.
 Each element is a symbol whose value will be persisted across Emacs
-sessions that use savehist.  The contents of variables should be
+sessions that use Savehist.  The contents of variables should be
 printable with the Lisp printer.  You don't need to add minibuffer
 history variables to this list, all minibuffer histories will be
 saved automatically as long as `savehist-save-minibuffer-history' is
 non-nil.
 
-User options should be saved with the customize interface.  This
+User options should be saved with the Customize interface.  This
 list is useful for saving automatically updated variables that are not
 minibuffer histories, such as `compile-command' or `kill-ring'."
   :type '(repeat variable)
   :group 'savehist)
 
 (defcustom savehist-ignored-variables nil ;; '(command-history)
-  "*List of additional variables not to save."
+  "List of additional variables not to save."
   :type '(repeat variable)
   :group 'savehist)
 
 (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 user-emacs-directory))
-    (concat user-emacs-directory "history"))
-   ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
-    "~/.xemacs/history")
-   ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
-   (t "~/.emacs-history"))
-  "*File name where minibuffer history is saved to and loaded from.
+  (locate-user-emacs-file "history" ".emacs-history")
+  "File name where minibuffer history is saved to and loaded from.
 The minibuffer history is a series of Lisp expressions loaded
-automatically when `savehist-mode' is turned on.  See `savehist-mode'
+automatically when Savehist mode is turned on.  See `savehist-mode'
 for more details.
 
 If you want your minibuffer history shared between Emacs and XEmacs,
@@ -117,7 +99,7 @@ set to a coding system that exists in both emacsen."
   :group 'savehist)
 
 (defcustom savehist-file-modes #o600
-  "*Default permissions of the history file.
+  "Default permissions of the history file.
 This is decimal, not octal.  The default is 384 (0600 in octal).
 Set to nil to use the default permissions that Emacs uses, typically
 mandated by umask.  The default is a bit more restrictive to protect
@@ -126,20 +108,21 @@ the user's privacy."
   :group 'savehist)
 
 (defcustom savehist-autosave-interval (* 5 60)
-  "*The interval between autosaves of minibuffer history.
+  "The interval between autosaves of minibuffer history.
 If set to nil, disables timer-based autosaving."
-  :type 'integer
+  :type '(choice (const :tag "Disabled" nil)
+                 (integer :tag "Seconds"))
   :group 'savehist)
 
 (defcustom savehist-mode-hook nil
-  "Hook called when `savehist-mode' is turned on."
+  "Hook called when Savehist mode is turned on."
   :type 'hook
   :group 'savehist)
 
 (defcustom savehist-save-hook nil
   "Hook called by `savehist-save' before saving the variables.
-You can use this hook to influence choice and content of variables to
-save."
+You can use this hook to influence choice and content of variables
+to save."
   :type 'hook
   :group 'savehist)
 
@@ -151,7 +134,7 @@ save."
                                        (<= emacs-major-version 21)
                                        (< emacs-minor-version 5))
                                   'iso-2022-8 'utf-8-unix)
-  "The coding system savehist uses for saving the minibuffer history.
+  "The coding system Savehist uses for saving the minibuffer history.
 Changing this value while Emacs is running is supported, but considered
 unwise, unless you know what you are doing.")
 
@@ -175,7 +158,7 @@ buffer text.")
 
 (defvar savehist-loaded nil
   "Whether the history has already been loaded.
-This prevents toggling `savehist-mode' from destroying existing
+This prevents toggling Savehist mode from destroying existing
 minibuffer history.")
 
 (when (featurep 'xemacs)
@@ -186,21 +169,21 @@ minibuffer history.")
 ;; Functions.
 
 ;;;###autoload
-(defun savehist-mode (arg)
-  "Toggle savehist-mode.
-Positive ARG turns on `savehist-mode'.  When on, savehist-mode causes
-minibuffer history to be saved periodically and when exiting Emacs.
-When turned on for the first time in an Emacs session, it causes the
-previous minibuffer history to be loaded from `savehist-file'.
+(define-minor-mode savehist-mode
+  "Toggle saving of minibuffer history (Savehist mode).
+With a prefix argument ARG, enable Savehist mode if ARG is
+positive, and disable it otherwise.  If called from Lisp, enable
+the mode if ARG is omitted or nil.
+
+When Savehist mode is enabled, minibuffer history is saved
+periodically and when exiting Emacs.  When Savehist mode is
+enabled for the first time in an Emacs session, it loads the
+previous minibuffer history from `savehist-file'.
 
 This mode should normally be turned on from your Emacs init file.
-Calling it at any other time replaces your current minibuffer histories,
-which is probably undesirable."
-  (interactive "P")
-  (setq savehist-mode
-       (if (null arg)
-           (not savehist-mode)
-         (> (prefix-numeric-value arg) 0)))
+Calling it at any other time replaces your current minibuffer
+histories, which is probably undesirable."
+  :global t
   (if (not savehist-mode)
       (savehist-uninstall)
     (when (and (not savehist-loaded)
@@ -211,7 +194,7 @@ which is probably undesirable."
            ;; coding cookie to convey that information.  That way, if
            ;; the user changes the value of savehist-coding-system,
            ;; we can still correctly load the old file.
-           (load savehist-file nil (not (interactive-p)))
+           (load savehist-file nil (not (called-interactively-p 'interactive)))
            (setq savehist-loaded t))
        (error
         ;; Don't install the mode if reading failed.  Doing so would
@@ -219,17 +202,14 @@ which is probably undesirable."
         (setq savehist-mode nil)
         (savehist-uninstall)
         (signal (car errvar) (cdr errvar)))))
-    (savehist-install)
-    (run-hooks 'savehist-mode-hook))
-  ;; Return the new setting.
-  savehist-mode)
-(add-minor-mode 'savehist-mode "")
+    (savehist-install)))
 
 (defun savehist-load ()
-  "Load the variables stored in `savehist-file' and turn on `savehist-mode'.
+  "Load the variables stored in `savehist-file' and turn on Savehist mode.
 If `savehist-file' is in the old format that doesn't record
 the value of `savehist-minibuffer-history-variables', that
 value is deducted from the contents of the file."
+  (declare (obsolete savehist-mode "22.1"))
   (savehist-mode 1)
   ;; Old versions of savehist distributed with XEmacs didn't save
   ;; savehist-minibuffer-history-variables.  If that variable is nil
@@ -246,10 +226,9 @@ value is deducted from the contents of the file."
                ;; Collect VAR, i.e. (nth form 1).
                 (push (nth 1 form) vars))
               vars)))))
-(make-obsolete 'savehist-load 'savehist-mode "22.1")
 
 (defun savehist-install ()
-  "Hook savehist into Emacs.
+  "Hook Savehist into Emacs.
 Normally invoked by calling `savehist-mode' to set the minor mode.
 Installs `savehist-autosave' in `kill-emacs-hook' and on a timer.
 To undo this, call `savehist-uninstall'."
@@ -279,6 +258,10 @@ Normally invoked by calling `savehist-mode' to unset the minor mode."
       (cancel-timer savehist-timer))
     (setq savehist-timer nil)))
 
+;; From XEmacs?
+(defvar print-readably)
+(defvar print-string-length)
+
 (defun savehist-save (&optional auto-save)
   "Save the values of minibuffer history variables.
 Unbound symbols referenced in `savehist-additional-variables' are ignored.
@@ -295,6 +278,13 @@ If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
          (print-level nil)
          (print-readably t)
          (print-quoted t))
+      ;; During the 24.3 development, read-passwd had a bug which resulted in
+      ;; the passwords being saved by savehist.  Trim them, retroactively.
+      ;; This code can be removed after the 24.3 release.
+      (dolist (sym savehist-minibuffer-history-variables)
+        (if (and (symbolp sym) (equal (symbol-name sym) "forget-history"))
+            (setq savehist-minibuffer-history-variables
+                  (delq sym savehist-minibuffer-history-variables))))
       ;; Save the minibuffer histories, along with the value of
       ;; savehist-minibuffer-history-variables itself.
       (when savehist-save-minibuffer-history
@@ -318,14 +308,15 @@ If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
                (dolist (elt value)
                  (let ((start (point)))
                    (insert " ")
-                   (prin1 elt (current-buffer))
-                   ;; Try to read the element we just printed.
+                   ;; Try to print and then to read an element.
                    (condition-case nil
-                       (save-excursion
-                         (goto-char start)
-                         (read (current-buffer)))
+                       (progn
+                         (prin1 elt (current-buffer))
+                         (save-excursion
+                           (goto-char start)
+                           (read (current-buffer))))
                      (error
-                      ;; If reading it gets an error, comment it out.
+                      ;; If writing or reading gave an error, comment it out.
                       (goto-char start)
                       (insert "\n")
                       (while (not (eobp))
@@ -358,14 +349,14 @@ If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
        (let ((file-precious-flag t)
              (coding-system-for-write savehist-coding-system))
          (write-region (point-min) (point-max) savehist-file nil
-                       (unless (interactive-p) 'quiet)))
+                       (unless (called-interactively-p 'interactive) 'quiet)))
        (when savehist-file-modes
          (set-file-modes savehist-file savehist-file-modes))
        (setq savehist-last-checksum checksum)))))
 
 (defun savehist-autosave ()
   "Save the minibuffer history if it has been modified since the last save.
-Does nothing if `savehist-mode' is off."
+Does nothing if Savehist mode is off."
   (when savehist-mode
     (savehist-save t)))
 
@@ -385,9 +376,11 @@ trimming of history lists to `history-length' items."
   "Return non-nil if VALUE is printable."
   (cond
    ;; Quick response for oft-encountered types known to be printable.
-   ((stringp value))
    ((numberp value))
    ((symbolp value))
+   ;; String without properties
+   ((and (stringp value)
+        (equal-including-properties value (substring-no-properties value))))
    (t
     ;; For others, check explicitly.
     (with-temp-buffer
@@ -412,6 +405,5 @@ trimming of history lists to `history-length' items."
 
 (provide 'savehist)
 \f
-;; arch-tag: b3ce47f4-c5ad-4ebc-ad02-73aba705cf9f
 
 ;;; savehist.el ends here