2001-10-17 11:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / nnfolder.el
index ceedda3..f225f32 100644 (file)
@@ -1,8 +1,9 @@
 ;;; nnfolder.el --- mail folder access for Gnus
-;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
+;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
 ;;        Free Software Foundation, Inc.
 
-;; Author: ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
+;; Author: Simon Josefsson <simon@josefsson.org> (adding MARKS)
+;;      ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
 ;;      Scott Byer <byer@mv.us.adobe.com>
 ;;     Lars Magne Ingebrigtsen <larsi@gnus.org>
 ;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
 (require 'nnmail)
 (require 'nnoo)
 (eval-when-compile (require 'cl))
+(require 'gnus)
 (require 'gnus-util)
 (require 'gnus-range)
 
 (eval-and-compile
+  (autoload 'gnus-article-unpropagatable-p "gnus-sum")
   (autoload 'gnus-intersection "gnus-range"))
 
 (nnoo-declare nnfolder)
 
 (defvoo nnfolder-directory (expand-file-name message-directory)
-  "The name of the nnfolder directory.")
+  "The name of the nnfolder directory.
+
+This variable is a virtual server slot.  See the Gnus manual for details.")
 
 (defvoo nnfolder-nov-directory nil
   "The name of the nnfolder NOV directory.
 If nil, `nnfolder-directory' is used.")
 
+(defvoo nnfolder-marks-directory nil
+  "The name of the nnfolder MARKS directory.
+If nil, `nnfolder-directory' is used.")
+
 (defvoo nnfolder-active-file
     (nnheader-concat nnfolder-directory "active")
-  "The name of the active file.")
+  "The name of the active file.
+
+This variable is a virtual server slot.  See the Gnus manual for details.")
 
 ;; I renamed this variable to something more in keeping with the general GNU
 ;; style. -SLB
@@ -73,19 +84,28 @@ message, a huge time saver for large mailboxes.")
 
 (defvoo nnfolder-newsgroups-file
     (concat (file-name-as-directory nnfolder-directory) "newsgroups")
-  "Mail newsgroups description file.")
+  "Mail newsgroups description file.
+
+This variable is a virtual server slot.  See the Gnus manual for details.")
 
 (defvoo nnfolder-get-new-mail t
-  "If non-nil, nnfolder will check the incoming mail file and split the mail.")
+  "If non-nil, nnfolder will check the incoming mail file and split the mail.
+
+This variable is a virtual server slot.  See the Gnus manual for details.")
 
 (defvoo nnfolder-prepare-save-mail-hook nil
   "Hook run narrowed to an article before saving.")
 
 (defvoo nnfolder-save-buffer-hook nil
-  "Hook run before saving the nnfolder mbox buffer.")
+  "Hook run before saving the nnfolder mbox buffer.
+
+This variable is a virtual server slot.  See the Gnus manual for details.")
+
 
 (defvoo nnfolder-inhibit-expiry nil
-  "If non-nil, inhibit expiry.")
+  "If non-nil, inhibit expiry.
+
+This variable is a virtual server slot.  See the Gnus manual for details.")
 
 \f
 
@@ -117,7 +137,9 @@ This variable shouldn't be flipped much.  If you have, for some reason,
 set this to t, and want to set it to nil again, you should always run
 the `nnfolder-generate-active-file' command.  The function will go
 through all nnfolder directories and generate nov databases for them
-all.  This may very well take some time.")
+all.  This may very well take some time.
+
+This variable is a virtual server slot.  See the Gnus manual for details.")
 
 (defvoo nnfolder-nov-file-suffix ".nov")
 
@@ -125,6 +147,23 @@ all.  This may very well take some time.")
 
 (defvar nnfolder-nov-buffer-file-name nil)
 
+(defvoo nnfolder-marks-is-evil nil
+  "If non-nil, Gnus will never generate and use marks file for mail groups.
+Using marks files makes it possible to backup and restore mail groups
+separately from `.newsrc.eld'.  If you have, for some reason, set
+this to t, and want to set it to nil again, you should always remove
+the corresponding marks file (usually base nnfolder file name
+concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
+the group.  Then the marks file will be regenerated properly by Gnus.
+
+This variable is a virtual server slot.  See the Gnus manual for details.")
+
+(defvoo nnfolder-marks nil)
+
+(defvoo nnfolder-marks-file-suffix ".mrk")
+
+(defvar nnfolder-marks-modtime (gnus-make-hashtable))
+
 \f
 
 ;;; Interface functions
@@ -171,6 +210,9 @@ all.  This may very well take some time.")
   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
     (and nnfolder-nov-directory
         (gnus-make-directory nnfolder-nov-directory)))
+  (unless nnfolder-marks-is-evil
+    (and nnfolder-marks-directory
+        (gnus-make-directory nnfolder-marks-directory)))
   (cond
    ((not (file-exists-p nnfolder-directory))
     (nnfolder-close-server)
@@ -385,9 +427,10 @@ all.  This may very well take some time.")
              (with-temp-buffer
                (nnfolder-request-article (car maybe-expirable)
                                          newsgroup server (current-buffer))
-               (let ((nnml-current-directory nil))
+               (let ((nnfolder-current-directory nil))
                  (nnmail-expiry-target-group
-                  nnmail-expiry-target newsgroup))))
+                  nnmail-expiry-target newsgroup)))
+             (nnfolder-possibly-change-group newsgroup server))
            (nnheader-message 5 "Deleting article %d in %s..."
                              (car maybe-expirable) newsgroup)
            (nnfolder-delete-mail)
@@ -522,7 +565,10 @@ all.  This may very well take some time.")
     ;; Delete the file that holds the group.
     (ignore-errors
       (delete-file (nnfolder-group-pathname group))
-      (delete-file (nnfolder-group-nov-pathname group))))
+      (when (file-exists-p (nnfolder-group-nov-pathname group))
+       (delete-file (nnfolder-group-nov-pathname group)))
+      (when (file-exists-p (nnfolder-group-marks-pathname group))
+       (delete-file (nnfolder-group-marks-pathname group)))))
   ;; Remove the group from all structures.
   (setq nnfolder-group-alist
        (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
@@ -541,9 +587,14 @@ all.  This may very well take some time.")
           (let ((new-file (nnfolder-group-pathname new-name)))
             (gnus-make-directory (file-name-directory new-file))
             (rename-file buffer-file-name new-file)
-            (setq new-file (nnfolder-group-nov-pathname new-name))
-            (rename-file (nnfolder-group-nov-pathname group)
-                         new-file))
+            (when (file-exists-p (nnfolder-group-nov-pathname group))
+              (setq new-file (nnfolder-group-nov-pathname new-name))
+              (gnus-make-directory (file-name-directory new-file))
+              (rename-file (nnfolder-group-nov-pathname group) new-file))
+            (when (file-exists-p (nnfolder-group-marks-pathname group))
+              (setq new-file (nnfolder-group-marks-pathname new-name))
+              (gnus-make-directory (file-name-directory new-file))
+              (rename-file (nnfolder-group-marks-pathname group) new-file)))
           t)
         ;; That went ok, so we change the internal structures.
         (let ((entry (assoc group nnfolder-group-alist)))
@@ -739,7 +790,8 @@ deleted.  Point is left where the deleted region was."
   (goto-char (point-max))
   (skip-chars-backward "\n")
   (delete-region (point) (point-max))
-  (insert "\n\n"))
+  (unless (bobp)
+    (insert "\n\n")))
 
 (defun nnfolder-insert-newsgroup-line (group-art)
   (save-excursion
@@ -1095,6 +1147,110 @@ This command does not work if you use short group names."
     (mail-header-set-number headers article)
     (nnheader-insert-nov headers)))
 
+(deffoo nnfolder-request-set-mark (group actions &optional server)
+  (when (and server
+            (not (nnfolder-server-opened server)))
+    (nnfolder-open-server server))
+  (unless nnfolder-marks-is-evil
+    (nnfolder-open-marks group server)
+    (dolist (action actions)
+      (let ((range (nth 0 action))
+           (what  (nth 1 action))
+           (marks (nth 2 action)))
+       (assert (or (eq what 'add) (eq what 'del)) t
+               "Unknown request-set-mark action: %s" what)
+       (dolist (mark marks)
+         (setq nnfolder-marks (gnus-update-alist-soft
+                           mark
+                           (funcall (if (eq what 'add) 'gnus-range-add
+                                      'gnus-remove-from-range)
+                                    (cdr (assoc mark nnfolder-marks)) range)
+                           nnfolder-marks)))))
+    (nnfolder-save-marks group server))
+  nil)
+
+(deffoo nnfolder-request-update-info (group info &optional server)
+  ;; Change servers.
+  (when (and server
+            (not (nnfolder-server-opened server)))
+    (nnfolder-open-server server))
+  (when (and (not nnfolder-marks-is-evil) (nnfolder-marks-changed-p group))
+    (nnheader-message 8 "Updating marks for %s..." group)
+    (nnfolder-open-marks group server)
+    ;; Update info using `nnfolder-marks'.
+    (mapcar (lambda (pred)
+             (gnus-info-set-marks
+              info
+              (gnus-update-alist-soft
+               (cdr pred)
+               (cdr (assq (cdr pred) nnfolder-marks))
+               (gnus-info-marks info))
+              t))
+           gnus-article-mark-lists)
+    (let ((seen (cdr (assq 'read nnfolder-marks))))
+      (gnus-info-set-read info
+                         (if (and (integerp (car seen))
+                                  (null (cdr seen)))
+                             (list (cons (car seen) (car seen)))
+                           seen)))
+    (nnheader-message 8 "Updating marks for %s...done" group))
+  info)
+
+(defun nnfolder-group-marks-pathname (group)
+  "Make pathname for GROUP NOV."
+  (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
+    (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
+
+(defun nnfolder-marks-changed-p (group)
+  (let ((file (nnfolder-group-marks-pathname group)))
+    (if (null (gnus-gethash file nnfolder-marks-modtime))
+       t ;; never looked at marks file, assume it has changed
+      (not (eq (gnus-gethash file nnfolder-marks-modtime)
+              (nth 5 (file-attributes file)))))))
+
+(defun nnfolder-save-marks (group server)
+  (let ((file-name-coding-system nnmail-pathname-coding-system)
+       (file (nnfolder-group-marks-pathname group)))
+    (condition-case err
+       (progn
+         (with-temp-file file
+           (erase-buffer)
+           (princ nnfolder-marks (current-buffer))
+           (insert "\n"))
+         (gnus-sethash file
+                       (nth 5 (file-attributes file))
+                       nnfolder-marks-modtime))
+      (error (or (gnus-yes-or-no-p
+                 (format "Could not write to %s (%s).  Continue? " file err))
+                (error "Cannot write to %s (%s)" err))))))
+
+(defun nnfolder-open-marks (group server)
+  (let ((file (nnfolder-group-marks-pathname group)))
+    (if (file-exists-p file)
+       (condition-case err
+           (with-temp-buffer
+             (gnus-sethash file (nth 5 (file-attributes file)) 
+                           nnfolder-marks-modtime)
+             (nnheader-insert-file-contents file)
+             (setq nnfolder-marks (read (current-buffer)))
+             (dolist (el gnus-article-unpropagated-mark-lists)
+               (setq nnfolder-marks (gnus-remassoc el nnfolder-marks))))
+         (error (or (gnus-yes-or-no-p
+                     (format "Error reading nnfolder marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
+                    (error "Cannot read nnfolder marks file %s (%s)" file err))))
+      ;; User didn't have a .marks file.  Probably first time
+      ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
+      (let ((info (gnus-get-info
+                  (gnus-group-prefixed-name
+                   group
+                   (gnus-server-to-method (format "nnfolder:%s" server))))))
+       (nnheader-message 7 "Bootstrapping marks for %s..." group)
+       (setq nnfolder-marks (gnus-info-marks info))
+       (push (cons 'read (gnus-info-read info)) nnfolder-marks)
+       (dolist (el gnus-article-unpropagated-mark-lists)
+         (setq nnfolder-marks (gnus-remassoc el nnfolder-marks)))
+       (nnfolder-save-marks group server)))))
+
 (provide 'nnfolder)
 
 ;;; nnfolder.el ends here