Import Gnus v5.10.3.
[elisp/gnus.git-] / lisp / spam-stat.el
index 96df016..e85e057 100644 (file)
@@ -183,6 +183,9 @@ effect when spam-stat is invoked through spam.el."
   "Syntax table used when processing mails for statistical analysis.
 The important part is which characters are word constituents.")
 
+(defvar spam-stat-dirty nil
+  "Whether the spam-stat database needs saving.")
+
 (defvar spam-stat-buffer nil
   "Buffer to use for scoring while splitting.
 This is set by hooking into Gnus.")
@@ -238,12 +241,6 @@ This uses `gnus-article-buffer'."
     (set-buffer gnus-original-article-buffer)
     (spam-stat-store-current-buffer)))
 
-(when spam-stat-install-hooks
-  (add-hook 'nnmail-prepare-incoming-message-hook
-           'spam-stat-store-current-buffer)
-  (add-hook 'gnus-select-article-hook
-           'spam-stat-store-gnus-article-buffer))
-
 ;; Data -- not using defstruct in order to save space and time
 
 (defvar spam-stat (make-hash-table :test 'equal)
@@ -347,7 +344,8 @@ Use `spam-stat-ngood', `spam-stat-nbad', `spam-stat-good',
         (setq entry (spam-stat-make-entry 0 count)))
        (spam-stat-set-score entry (spam-stat-compute-score entry))
        (puthash word entry spam-stat)))
-   (spam-stat-buffer-words)))
+   (spam-stat-buffer-words))
+  (setq spam-stat-dirty t))
 
 (defun spam-stat-buffer-is-non-spam ()
   "Consider current buffer to be a new non-spam mail."
@@ -360,7 +358,8 @@ Use `spam-stat-ngood', `spam-stat-nbad', `spam-stat-good',
         (setq entry (spam-stat-make-entry count 0)))
        (spam-stat-set-score entry (spam-stat-compute-score entry))
        (puthash word entry spam-stat)))
-   (spam-stat-buffer-words)))
+   (spam-stat-buffer-words))
+  (setq spam-stat-dirty t))
 
 (defun spam-stat-buffer-change-to-spam ()
   "Consider current buffer no longer normal mail but spam."
@@ -375,7 +374,8 @@ Use `spam-stat-ngood', `spam-stat-nbad', `spam-stat-good',
         (spam-stat-set-bad entry (+ (spam-stat-bad entry) count))
         (spam-stat-set-score entry (spam-stat-compute-score entry))
         (puthash word entry spam-stat))))
-   (spam-stat-buffer-words)))
+   (spam-stat-buffer-words))
+  (setq spam-stat-dirty t))
 
 (defun spam-stat-buffer-change-to-non-spam ()
   "Consider current buffer no longer spam but normal mail."
@@ -390,32 +390,37 @@ Use `spam-stat-ngood', `spam-stat-nbad', `spam-stat-good',
         (spam-stat-set-bad entry (- (spam-stat-bad entry) count))
         (spam-stat-set-score entry (spam-stat-compute-score entry))
         (puthash word entry spam-stat))))
-   (spam-stat-buffer-words)))
+   (spam-stat-buffer-words))
+  (setq spam-stat-dirty t))
 
 ;; Saving and Loading
 
-(defun spam-stat-save ()
+(defun spam-stat-save (&optional force)
   "Save the `spam-stat' hash table as lisp file."
   (interactive)
-  (with-temp-buffer
-    (let ((standard-output (current-buffer))
-         (font-lock-maximum-size 0))
-      (insert "(setq spam-stat-ngood "
-              (number-to-string spam-stat-ngood)
-              " spam-stat-nbad "
-              (number-to-string spam-stat-nbad)
-              " spam-stat (spam-stat-to-hash-table '(")
-      (maphash (lambda (word entry)
-                (prin1 (list word
-                             (spam-stat-good entry)
-                             (spam-stat-bad entry))))
-              spam-stat)
-      (insert ")))")
-    (write-file spam-stat-file))))
+  (when (or force spam-stat-dirty)
+    (with-temp-buffer
+      (let ((standard-output (current-buffer))
+           (font-lock-maximum-size 0))
+       (insert "(setq spam-stat-ngood "
+               (number-to-string spam-stat-ngood)
+               " spam-stat-nbad "
+               (number-to-string spam-stat-nbad)
+               " spam-stat (spam-stat-to-hash-table '(")
+       (maphash (lambda (word entry)
+                  (prin1 (list word
+                               (spam-stat-good entry)
+                               (spam-stat-bad entry))))
+                spam-stat)
+       (insert ")))")
+       (write-file spam-stat-file)))
+    (setq spam-stat-dirty nil)))
 
 (defun spam-stat-load ()
   "Read the `spam-stat' hash table from disk."
-  (load-file spam-stat-file))
+  ;; TODO: maybe we should warn the user if spam-stat-dirty is t?
+  (load-file spam-stat-file)
+  (setq spam-stat-dirty nil))
 
 (defun spam-stat-to-hash-table (entries)
   "Turn list ENTRIES into a hash table and store as `spam-stat'.
@@ -438,7 +443,8 @@ This deletes all the statistics."
   (interactive)
   (setq spam-stat (make-hash-table :test 'equal)
        spam-stat-ngood 0
-       spam-stat-nbad 0))
+       spam-stat-nbad 0)
+  (setq spam-stat-dirty t))
 
 ;; Scoring buffers
 
@@ -567,6 +573,25 @@ COUNT defaults to 5"
               (remhash key spam-stat)))
           spam-stat))
 
+(defun spam-stat-install-hooks-function ()
+  "Install the spam-stat function hooks"
+  (interactive)
+  (add-hook 'nnmail-prepare-incoming-message-hook
+           'spam-stat-store-current-buffer)
+  (add-hook 'gnus-select-article-hook
+           'spam-stat-store-gnus-article-buffer))
+
+(when spam-stat-install-hooks
+  (spam-stat-install-hooks-function))
+
+(defun spam-stat-unload-hook ()
+  "Uninstall the spam-stat function hooks"
+  (interactive)
+  (remove-hook 'nnmail-prepare-incoming-message-hook
+              'spam-stat-store-current-buffer)
+  (remove-hook 'gnus-select-article-hook
+              'spam-stat-store-gnus-article-buffer))
+
 (provide 'spam-stat)
 
 ;;; spam-stat.el ends here