(nnfolder-read-folder): Check if most-positive-fixnum is
[gnus] / lisp / nnfolder.el
index ea72817..6ba4aff 100644 (file)
@@ -1,17 +1,21 @@
 ;;; nnfolder.el --- mail folder access for Gnus
-;; Copyright (C) 1995,96 Free Software Foundation, Inc.
 
-;; Author: Scott Byer <byer@mv.us.adobe.com>
-;;     Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
-;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
+;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+
+;; 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>
 ;; Keywords: mail
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
-;; For an overview of what the interface functions do, please see the
-;; Gnus sources.  
-
-;; Various enhancements by byer@mv.us.adobe.com (Scott Byer).
-
 ;;; Code:
 
+;; For Emacs < 22.2.
+(eval-and-compile
+  (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
+
 (require 'nnheader)
 (require 'message)
 (require 'nnmail)
 (require 'nnoo)
 (eval-when-compile (require 'cl))
+(require 'gnus)
+(require 'gnus-util)
+(require 'gnus-range)
+
+;; FIXME not explicitly used in this file.
+(autoload 'gnus-article-unpropagatable-p "gnus-sum")
 
 (nnoo-declare nnfolder)
 
 (defvoo nnfolder-directory (expand-file-name message-directory)
   "The name of the nnfolder directory.")
 
-(defvoo nnfolder-active-file 
-  (nnheader-concat nnfolder-directory "active")
+(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.")
 
 ;; I renamed this variable to something more in keeping with the general GNU
 ;; style. -SLB
 
 (defvoo nnfolder-ignore-active-file nil
-  "If non-nil, causes nnfolder to do some extra work in order to determine
-the true active ranges of an mbox file.  Note that the active file is still
-saved, but it's values are not used.  This costs some extra time when 
-scanning an mbox when opening it.")
+  "If non-nil, the active file is ignored.
+This causes nnfolder to do some extra work in order to determine the
+true active ranges of an mbox file.  Note that the active file is
+still saved, but its values are not used.  This costs some extra time
+when scanning an mbox when opening it.")
 
 (defvoo nnfolder-distrust-mbox nil
-  "If non-nil, causes nnfolder to not trust the user with respect to
-inserting unaccounted for mail in the middle of an mbox file.  This can greatly
-slow down scans, which now must scan the entire file for unmarked messages.
-When nil, scans occur forward from the last marked message, a huge
-time saver for large mailboxes.")
-
-(defvoo nnfolder-newsgroups-file 
-  (concat (file-name-as-directory nnfolder-directory) "newsgroups")
+  "If non-nil, the folder will be distrusted.
+This means that nnfolder will not trust the user with respect to
+inserting unaccounted for mail in the middle of an mbox file.  This
+can greatly slow down scans, which now must scan the entire file for
+unmarked messages.  When nil, scans occur forward from the last marked
+message, a huge time saver for large mailboxes.")
+
+(defvoo nnfolder-newsgroups-file
+    (concat (file-name-as-directory nnfolder-directory) "newsgroups")
   "Mail newsgroups description file.")
 
 (defvoo nnfolder-get-new-mail t
@@ -76,12 +93,13 @@ time saver for large mailboxes.")
 (defvoo nnfolder-save-buffer-hook nil
   "Hook run before saving the nnfolder mbox buffer.")
 
+
 (defvoo nnfolder-inhibit-expiry nil
   "If non-nil, inhibit expiry.")
 
 \f
 
-(defconst nnfolder-version "nnfolder 1.0"
+(defconst nnfolder-version "nnfolder 2.0"
   "nnfolder version.")
 
 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
@@ -93,6 +111,44 @@ time saver for large mailboxes.")
 (defvoo nnfolder-group-alist nil)
 (defvoo nnfolder-buffer-alist nil)
 (defvoo nnfolder-scantime-alist nil)
+(defvoo nnfolder-active-timestamp nil)
+(defvoo nnfolder-active-file-coding-system mm-text-coding-system)
+(defvoo nnfolder-active-file-coding-system-for-write
+    nnmail-active-file-coding-system)
+(defvoo nnfolder-file-coding-system mm-text-coding-system)
+(defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
+  "Coding system for save nnfolder file.
+if nil, `nnfolder-file-coding-system' is used.") ; FIXME: fill-in the doc-string of this variable
+
+(defvoo nnfolder-nov-is-evil nil
+  "If non-nil, Gnus will never generate and use nov databases for mail groups.
+Using nov databases will speed up header fetching considerably.
+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.")
+
+(defvoo nnfolder-nov-file-suffix ".nov")
+
+(defvoo nnfolder-nov-buffer-alist nil)
+
+(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.")
+
+(defvoo nnfolder-marks nil)
+
+(defvoo nnfolder-marks-file-suffix ".mrk")
+
+(defvar nnfolder-marks-modtime (gnus-make-hashtable))
 
 \f
 
@@ -104,47 +160,83 @@ time saver for large mailboxes.")
   (save-excursion
     (set-buffer nntp-server-buffer)
     (erase-buffer)
-    (let ((delim-string (concat "^" message-unix-mail-delimiter))
-         article art-string start stop)
+    (let (article start stop num)
       (nnfolder-possibly-change-group group server)
       (when nnfolder-current-buffer
        (set-buffer nnfolder-current-buffer)
        (goto-char (point-min))
        (if (stringp (car articles))
            'headers
-         (while articles
-           (setq article (car articles))
-           (setq art-string (nnfolder-article-string article))
-           (set-buffer nnfolder-current-buffer)
-           (if (or (search-forward art-string nil t)
-                   ;; Don't search the whole file twice!  Also, articles
-                   ;; probably have some locality by number, so searching
-                   ;; backwards will be faster.  Especially if we're at the
-                   ;; beginning of the buffer :-). -SLB
-                   (search-backward art-string nil t))
-               (progn
-                 (setq start (or (re-search-backward delim-string nil t)
-                                 (point)))
-                 (search-forward "\n\n" nil t)
-                 (setq stop (1- (point)))
-                 (set-buffer nntp-server-buffer)
-                 (insert (format "221 %d Article retrieved.\n" article))
-                 (insert-buffer-substring nnfolder-current-buffer start stop)
-                 (goto-char (point-max))
-                 (insert ".\n")))
-           (setq articles (cdr articles)))
-
-         (set-buffer nntp-server-buffer)
-         (nnheader-fold-continuation-lines)
-         'headers)))))
+         (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
+             'nov
+           (setq articles (gnus-sorted-intersection
+                           ;; Is ARTICLES sorted?
+                           (sort articles '<)
+                           (nnfolder-existing-articles)))
+           (while (setq article (pop articles))
+             (set-buffer nnfolder-current-buffer)
+             (cond ((nnfolder-goto-article article)
+                    (setq start (point))
+                    (setq stop (if (search-forward "\n\n" nil t)
+                                   (1- (point))
+                                 (point-max)))
+                    (set-buffer nntp-server-buffer)
+                    (insert (format "221 %d Article retrieved.\n" article))
+                    (insert-buffer-substring nnfolder-current-buffer
+                                             start stop)
+                    (goto-char (point-max))
+                    (insert ".\n"))
+
+                   ;; If we couldn't find this article, skip over ranges
+                   ;; of missing articles so we don't search the whole file
+                   ;; for each of them.
+                   ((numberp article)
+                    (setq start (point))
+                    (and
+                     ;; Check that we are either at BOF or after an
+                     ;; article with a lower number.  We do this so we
+                     ;; won't be confused by out-of-order article numbers,
+                     ;; as caused by active file bogosity.
+                     (cond
+                      ((bobp))
+                      ((search-backward (concat "\n" nnfolder-article-marker)
+                                        nil t)
+                       (goto-char (match-end 0))
+                       (setq num (string-to-number
+                                  (buffer-substring
+                                   (point) (point-at-eol))))
+                       (goto-char start)
+                       (< num article)))
+                     ;; Check that we are before an article with a
+                     ;; higher number.
+                     (search-forward (concat "\n" nnfolder-article-marker)
+                                     nil t)
+                     (progn
+                       (setq num (string-to-number
+                                  (buffer-substring
+                                   (point) (point-at-eol))))
+                       (> num article))
+                     ;; Discard any article numbers before the one we're
+                     ;; now looking at.
+                     (while (and articles
+                                 (< (car articles) num))
+                       (setq articles (cdr articles))))
+                    (goto-char start))))
+           (set-buffer nntp-server-buffer)
+           (nnheader-fold-continuation-lines)
+           'headers))))))
 
 (deffoo nnfolder-open-server (server &optional defs)
   (nnoo-change-server 'nnfolder server defs)
-  (when (not (file-exists-p nnfolder-directory))
-    (condition-case ()
-       (make-directory nnfolder-directory t)
-      (error t)))
-  (cond 
+  (nnmail-activate 'nnfolder t)
+  (gnus-make-directory nnfolder-directory)
+  (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)
     (nnheader-report 'nnfolder "Couldn't create directory: %s"
@@ -153,6 +245,7 @@ time saver for large mailboxes.")
     (nnfolder-close-server)
     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
    (t
+    (nnmail-activate 'nnfolder)
     (nnheader-report 'nnfolder "Opened server %s using directory %s"
                     server nnfolder-directory)
     t)))
@@ -171,75 +264,80 @@ time saver for large mailboxes.")
   (save-excursion
     (set-buffer nnfolder-current-buffer)
     (goto-char (point-min))
-    (if (search-forward (nnfolder-article-string article) nil t)
-       (let (start stop)
-         (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
-         (setq start (point))
-         (forward-line 1)
-         (or (and (re-search-forward 
-                   (concat "^" message-unix-mail-delimiter) nil t)
-                  (forward-line -1))
-             (goto-char (point-max)))
-         (setq stop (point))
-         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
-           (set-buffer nntp-server-buffer)
-           (erase-buffer)
-           (insert-buffer-substring nnfolder-current-buffer start stop)
+    (when (nnfolder-goto-article article)
+      (let (start stop)
+       (setq start (point))
+       (forward-line 1)
+       (unless (and (nnmail-search-unix-mail-delim)
+                    (forward-line -1))
+         (goto-char (point-max)))
+       (setq stop (point))
+       (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
+         (set-buffer nntp-server-buffer)
+         (erase-buffer)
+         (insert-buffer-substring nnfolder-current-buffer start stop)
+         (goto-char (point-min))
+         (while (looking-at "From ")
+           (delete-char 5)
+           (insert "X-From-Line: ")
+           (forward-line 1))
+         (if (numberp article)
+             (cons nnfolder-current-group article)
            (goto-char (point-min))
-           (while (looking-at "From ")
-             (delete-char 5)
-             (insert "X-From-Line: ")
-             (forward-line 1))
-           (if (numberp article) 
-               (cons nnfolder-current-group article)
-             (goto-char (point-min))
-             (search-forward (concat "\n" nnfolder-article-marker))
-             (cons nnfolder-current-group
-                   (string-to-int 
-                    (buffer-substring 
-                     (point) (progn (end-of-line) (point)))))))))))
+           (cons nnfolder-current-group
+                 (if (search-forward (concat "\n" nnfolder-article-marker)
+                                     nil t)
+                     (string-to-number (buffer-substring
+                                     (point) (point-at-eol)))
+                   -1))))))))
 
 (deffoo nnfolder-request-group (group &optional server dont-check)
+  (nnfolder-possibly-change-group group server t)
   (save-excursion
-    (nnmail-activate 'nnfolder)
-    (if (not (assoc group nnfolder-group-alist))
-       (nnheader-report 'nnfolder "No such group: %s" group)
-      (nnfolder-possibly-change-group group server)
-      (if dont-check
-         (progn 
-           (nnheader-report 'nnfolder "Selected group %s" group)
-           t)
-       (let* ((active (assoc group nnfolder-group-alist))
-              (group (car active))
-              (range (cadr active)))
-         (cond 
-          ((null active)
-           (nnheader-report 'nnfolder "No such group: %s" group))
-          ((null nnfolder-current-group)
-           (nnheader-report 'nnfolder "Empty group: %s" group))
-          (t
-           (nnheader-report 'nnfolder "Selected group %s" group)
-           (nnheader-insert "211 %d %d %d %s\n" 
-                            (1+ (- (cdr range) (car range)))
-                            (car range) (cdr range) group))))))))
+    (cond ((not (assoc group nnfolder-group-alist))
+          (nnheader-report 'nnfolder "No such group: %s" group))
+         ((file-directory-p (nnfolder-group-pathname group))
+          (nnheader-report 'nnfolder "%s is a directory"
+                           (file-name-as-directory
+                            (let ((nnmail-pathname-coding-system nil))
+                              (nnfolder-group-pathname group)))))
+         (dont-check
+          (nnheader-report 'nnfolder "Selected group %s" group)
+          t)
+         (t
+          (let* ((active (assoc group nnfolder-group-alist))
+                 (group (car active))
+                 (range (cadr active)))
+            (cond
+             ((null active)
+              (nnheader-report 'nnfolder "No such group: %s" group))
+             ((null nnfolder-current-group)
+              (nnheader-report 'nnfolder "Empty group: %s" group))
+             (t
+              (nnheader-report 'nnfolder "Selected group %s" group)
+              (nnheader-insert "211 %d %d %d %s\n"
+                               (1+ (- (cdr range) (car range)))
+                               (car range) (cdr range) group))))))))
 
 (deffoo nnfolder-request-scan (&optional group server)
-  (nnfolder-possibly-change-group group server t)
-  (nnmail-get-new-mail
-   'nnfolder 
-   (lambda ()
-     (let ((bufs nnfolder-buffer-alist))
-       (save-excursion
-        (while bufs
-          (if (not (buffer-name (nth 1 (car bufs))))
-              (setq nnfolder-buffer-alist 
-                    (delq (car bufs) nnfolder-buffer-alist))
-            (set-buffer (nth 1 (car bufs)))
-            (nnfolder-save-buffer)
-            (kill-buffer (current-buffer)))
-          (setq bufs (cdr bufs))))))
-   nnfolder-directory
-   group))
+  (nnfolder-possibly-change-group nil server)
+  (when nnfolder-get-new-mail
+    (nnfolder-possibly-change-group group server)
+    (nnmail-get-new-mail
+     'nnfolder
+     (lambda ()
+       (let ((bufs nnfolder-buffer-alist))
+        (save-excursion
+          (while bufs
+            (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
+                (setq nnfolder-buffer-alist
+                      (delq (car bufs) nnfolder-buffer-alist))
+              (set-buffer (nth 1 (car bufs)))
+              (nnfolder-save-buffer)
+              (kill-buffer (current-buffer)))
+            (setq bufs (cdr bufs))))))
+     nnfolder-directory
+     group)))
 
 ;; Don't close the buffer if we're not shutting down the server.  This way,
 ;; we can keep the buffer in the group buffer cache, and not have to grovel
@@ -252,7 +350,8 @@ time saver for large mailboxes.")
            (equal group nnfolder-current-group))
     (let ((inf (assoc group nnfolder-buffer-alist)))
       (when inf
-       (when nnfolder-current-group
+       (when (and nnfolder-current-group
+                  nnfolder-current-buffer)
          (push (list nnfolder-current-group nnfolder-current-buffer)
                nnfolder-buffer-alist))
        (setq nnfolder-buffer-alist
@@ -275,20 +374,32 @@ time saver for large mailboxes.")
        nnfolder-current-buffer nil)
   t)
 
-(deffoo nnfolder-request-create-group (group &optional server
+(deffoo nnfolder-request-create-group (group &optional server args)
   (nnfolder-possibly-change-group nil server)
   (nnmail-activate 'nnfolder)
-  (when group 
-    (unless (assoc group nnfolder-group-alist)
-      (push (list group (cons 1 0)) nnfolder-group-alist)
-      (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
-  t)
+  (cond ((zerop (length group))
+        (nnheader-report 'nnfolder "Invalid (empty) group name"))
+       ((file-directory-p (nnfolder-group-pathname group))
+        (nnheader-report 'nnfolder "%s is a directory"
+                         (file-name-as-directory
+                          (let ((nnmail-pathname-coding-system nil))
+                            (nnfolder-group-pathname group)))))
+       ((assoc group nnfolder-group-alist)
+        t)
+       (t
+        (push (list group (cons 1 0)) nnfolder-group-alist)
+        (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
+        (save-current-buffer
+          (nnfolder-read-folder group))
+        t)))
 
 (deffoo nnfolder-request-list (&optional server)
   (nnfolder-possibly-change-group nil server)
   (save-excursion
-    (nnmail-find-file nnfolder-active-file)
-    (setq nnfolder-group-alist (nnmail-get-active))))
+    (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
+      (nnmail-find-file nnfolder-active-file)
+      (setq nnfolder-group-alist (nnmail-get-active)))
+    t))
 
 (deffoo nnfolder-request-newgroups (date &optional server)
   (nnfolder-possibly-change-group nil server)
@@ -297,119 +408,202 @@ time saver for large mailboxes.")
 (deffoo nnfolder-request-list-newsgroups (&optional server)
   (nnfolder-possibly-change-group nil server)
   (save-excursion
-    (nnmail-find-file nnfolder-newsgroups-file)))
+    (let ((nnmail-file-coding-system nnfolder-file-coding-system))
+      (nnmail-find-file nnfolder-newsgroups-file))))
+
+;; Return a list consisting of all article numbers existing in the
+;; current folder.
 
-(deffoo nnfolder-request-expire-articles 
-  (articles newsgroup &optional server force)
+(defun nnfolder-existing-articles ()
+  (save-excursion
+    (when nnfolder-current-buffer
+      (set-buffer nnfolder-current-buffer)
+      (goto-char (point-min))
+      (let ((marker (concat "\n" nnfolder-article-marker))
+           (number "[0-9]+")
+           numbers)
+       (while (and (search-forward marker nil t)
+                   (re-search-forward number nil t))
+         (let ((newnum (string-to-number (match-string 0))))
+           (if (nnmail-within-headers-p)
+               (push newnum numbers))))
+      ;; The article numbers are increasing, so this result is sorted.
+       (nreverse numbers)))))
+
+(autoload 'gnus-request-group "gnus-int")
+(declare-function gnus-request-create-group "gnus-int"
+                  (group &optional gnus-command-method args))
+
+(deffoo nnfolder-request-expire-articles (articles newsgroup
+                                                  &optional server force)
   (nnfolder-possibly-change-group newsgroup server)
-  (let* ((is-old t)
-        rest)
+  (let ((is-old t)
+       ;; The articles we have deleted so far.
+       (deleted-articles nil)
+       ;; The articles that really exist and will
+       ;; be expired if they are old enough.
+       (maybe-expirable
+        (gnus-sorted-intersection articles (nnfolder-existing-articles)))
+       target)
     (nnmail-activate 'nnfolder)
 
-    (save-excursion 
+    (save-excursion
       (set-buffer nnfolder-current-buffer)
-      (while (and articles is-old)
+      ;; Since messages are sorted in arrival order and expired in the
+      ;; same order, we can stop as soon as we find a message that is
+      ;; too old.
+      (while (and maybe-expirable is-old)
        (goto-char (point-min))
-       (if (search-forward (nnfolder-article-string (car articles)) nil t)
-           (if (setq is-old
-                     (nnmail-expired-article-p 
+       (when (and (nnfolder-goto-article (car maybe-expirable))
+                  (search-forward (concat "\n" nnfolder-article-marker)
+                                  nil t))
+         (forward-sexp)
+         (when (setq is-old
+                     (nnmail-expired-article-p
                       newsgroup
-                      (buffer-substring 
-                       (point) (progn (end-of-line) (point))) 
+                      (buffer-substring
+                       (point) (progn (end-of-line) (point)))
                       force nnfolder-inhibit-expiry))
-               (progn
-                 (nnheader-message 5 "Deleting article %d..." 
-                                   (car articles) newsgroup)
-                 (nnfolder-delete-mail))
-             (setq rest (cons (car articles) rest))))
-       (setq articles (cdr articles)))
+           (setq target nnmail-expiry-target)
+           (unless (eq target 'delete)
+             (with-temp-buffer
+               (nnfolder-request-article (car maybe-expirable)
+                                         newsgroup server (current-buffer))
+               (let ((nnfolder-current-directory nil))
+                 (when (functionp target)
+                   (setq target (funcall target newsgroup)))
+                 (when (and target (not (eq target 'delete)))
+                   (if (or (gnus-request-group target)
+                           (gnus-request-create-group target))
+                       (nnmail-expiry-target-group target newsgroup)
+                     (setq target nil)))))
+             (nnfolder-possibly-change-group newsgroup server))
+           (when target
+             (nnheader-message 5 "Deleting article %d in %s..."
+                               (car maybe-expirable) newsgroup)
+             (nnfolder-delete-mail)
+             (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
+               (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
+             ;; Must remember which articles were actually deleted
+             (push (car maybe-expirable) deleted-articles))))
+       (setq maybe-expirable (cdr maybe-expirable)))
+      (unless nnfolder-inhibit-expiry
+       (nnheader-message 5 "Deleting articles...done"))
       (nnfolder-save-buffer)
-      ;; Find the lowest active article in this group.
-      (let* ((active (cadr (assoc newsgroup nnfolder-group-alist)))
-            (marker (concat "\n" nnfolder-article-marker))
-            (number "[0-9]+")
-            (activemin (cdr active)))
-       (goto-char (point-min))
-       (while (and (search-forward marker nil t)
-                   (re-search-forward number nil t))
-         (setq activemin (min activemin
-                              (string-to-number (buffer-substring
-                                                 (match-beginning 0)
-                                                 (match-end 0))))))
-       (setcar active activemin))
-      (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
-      (nconc rest articles))))
-
-(deffoo nnfolder-request-move-article
-  (article group server accept-form &optional last)
-  (nnfolder-possibly-change-group group server)
-  (let ((buf (get-buffer-create " *nnfolder move*"))
-       result)
-    (and 
-     (nnfolder-request-article article group server)
-     (save-excursion
-       (set-buffer buf)
-       (buffer-disable-undo (current-buffer))
-       (erase-buffer)
-       (insert-buffer-substring nntp-server-buffer)
-       (goto-char (point-min))
-       (while (re-search-forward 
-              (concat "^" nnfolder-article-marker)
-              (save-excursion (search-forward "\n\n" nil t) (point)) t)
-        (delete-region (progn (beginning-of-line) (point))
-                       (progn (forward-line 1) (point))))
-       (setq result (eval accept-form))
-       (kill-buffer buf)
-       result)
-     (save-excursion
-       (nnfolder-possibly-change-group group server)
-       (set-buffer nnfolder-current-buffer)
-       (goto-char (point-min))
-       (if (search-forward (nnfolder-article-string article) nil t)
+      (nnfolder-adjust-min-active newsgroup)
+      (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
+      (gnus-sorted-difference articles (nreverse deleted-articles)))))
+
+(deffoo nnfolder-request-move-article (article group server accept-form 
+                                              &optional last move-is-internal)
+  (save-excursion
+    (let ((buf (get-buffer-create " *nnfolder move*"))
+         result)
+      (and
+       (nnfolder-request-article article group server)
+       (save-excursion
+        (set-buffer buf)
+        (erase-buffer)
+        (insert-buffer-substring nntp-server-buffer)
+        (goto-char (point-min))
+        (while (re-search-forward
+                (concat "^" nnfolder-article-marker)
+                (save-excursion (and (search-forward "\n\n" nil t) (point)))
+                t)
+          (gnus-delete-line))
+        (setq result (eval accept-form))
+        (kill-buffer buf)
+        result)
+       (save-excursion
+        (nnfolder-possibly-change-group group server)
+        (set-buffer nnfolder-current-buffer)
+        (goto-char (point-min))
+        (when (nnfolder-goto-article article)
           (nnfolder-delete-mail))
-       (and last (nnfolder-save-buffer))))
-    result))
+        (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
+          (nnfolder-nov-delete-article group article))
+        (when last
+          (nnfolder-save-buffer)
+          (nnfolder-adjust-min-active group)
+          (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
+      result)))
 
 (deffoo nnfolder-request-accept-article (group &optional server last)
-  (nnfolder-possibly-change-group group server)
-  (nnmail-check-syntax)
-  (and (stringp group) (nnfolder-possibly-change-group group))
-  (let ((buf (current-buffer))
-       result)
-    (goto-char (point-min))
-    (when (looking-at "X-From-Line: ")
-      (replace-match "From "))
-    (and 
-     (nnfolder-request-list)
-     (save-excursion
-       (set-buffer buf)
-       (goto-char (point-min))
-       (search-forward "\n\n" nil t)
-       (forward-line -1)
-       (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
-        (delete-region (point) (progn (forward-line 1) (point))))
-       (setq result
-            (car (nnfolder-save-mail
-                  (if (stringp group)
-                      (list (cons group (nnfolder-active-number group)))
-                    (nnmail-article-group 'nnfolder-active-number))))))
-     (save-excursion
-       (set-buffer nnfolder-current-buffer)
-       (and last (nnfolder-save-buffer))))
-    (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
-    (unless result
-      (nnheader-report 'nnfolder "Couldn't store article"))
-    result))
+  (save-excursion
+    (nnfolder-possibly-change-group group server)
+    (nnmail-check-syntax)
+    (let ((buf (current-buffer))
+         result art-group)
+      (goto-char (point-min))
+      (when (looking-at "X-From-Line: ")
+       (replace-match "From ")
+       (while (progn (forward-line) (looking-at "[ \t]"))
+         (delete-char -1)))
+      (with-temp-buffer
+       (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
+             (nntp-server-buffer (current-buffer)))
+         (nnmail-find-file nnfolder-active-file)
+         (setq nnfolder-group-alist (nnmail-parse-active))))
+      (save-excursion
+       (goto-char (point-min))
+       (if (search-forward "\n\n" nil t)
+           (forward-line -1)
+         (goto-char (point-max)))
+       (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
+         (delete-region (point) (progn (forward-line 1) (point))))
+       (when nnmail-cache-accepted-message-ids
+         (nnmail-cache-insert (nnmail-fetch-field "message-id") 
+                              group
+                              (nnmail-fetch-field "subject")
+                              (nnmail-fetch-field "from")))
+       (setq result (if (stringp group)
+                        (list (cons group (nnfolder-active-number group)))
+                      (setq art-group
+                            (nnmail-article-group 'nnfolder-active-number))))
+       (if (and (null result)
+                (yes-or-no-p "Moved to `junk' group; delete article? "))
+           (setq result 'junk)
+         (setq result
+               (car (nnfolder-save-mail result)))))
+      (when last
+       (save-excursion
+         (nnfolder-possibly-change-folder (or (caar art-group) group))
+         (nnfolder-save-buffer)
+         (when nnmail-cache-accepted-message-ids
+           (nnmail-cache-close))))
+      (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
+      (unless result
+       (nnheader-report 'nnfolder "Couldn't store article"))
+      result)))
 
 (deffoo nnfolder-request-replace-article (article group buffer)
   (nnfolder-possibly-change-group group)
   (save-excursion
+    (set-buffer buffer)
+    (goto-char (point-min))
+    (if (not (looking-at "X-From-Line: "))
+       (insert "From nobody " (current-time-string) "\n")
+      (replace-match "From ")
+      (forward-line 1)
+      (while (looking-at "[ \t]")
+       (delete-char -1)
+       (forward-line 1)))
+    (nnfolder-normalize-buffer)
     (set-buffer nnfolder-current-buffer)
     (goto-char (point-min))
-    (if (not (search-forward (nnfolder-article-string article) nil t))
+    (if (not (nnfolder-goto-article article))
        nil
-      (nnfolder-delete-mail t t)
+      (nnfolder-delete-mail)
       (insert-buffer-substring buffer)
+      (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
+       (save-excursion
+         (set-buffer buffer)
+         (let ((headers (nnfolder-parse-head article
+                                             (point-min) (point-max))))
+           (with-current-buffer (nnfolder-open-nov group)
+             (if (nnheader-find-nov-line article)
+                 (delete-region (point) (progn (forward-line 1) (point))))
+             (nnheader-insert-nov headers)))))
       (nnfolder-save-buffer)
       t)))
 
@@ -419,16 +613,19 @@ time saver for large mailboxes.")
   (if (not force)
       ()                               ; Don't delete the articles.
     ;; Delete the file that holds the group.
-    (condition-case nil
-       (delete-file (nnfolder-group-pathname group))
-      (error nil)))
+    (let ((data (nnfolder-group-pathname group))
+         (nov (nnfolder-group-nov-pathname group))
+         (mrk (nnfolder-group-marks-pathname group)))
+      (ignore-errors (delete-file data))
+      (ignore-errors (delete-file nov))
+      (ignore-errors (delete-file mrk))))
   ;; Remove the group from all structures.
-  (setq nnfolder-group-alist 
+  (setq nnfolder-group-alist
        (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
        nnfolder-current-group nil
        nnfolder-current-buffer nil)
   ;; Save the active file.
-  (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
+  (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
   t)
 
 (deffoo nnfolder-request-rename-group (group new-name &optional server)
@@ -436,129 +633,174 @@ time saver for large mailboxes.")
   (save-excursion
     (set-buffer nnfolder-current-buffer)
     (and (file-writable-p buffer-file-name)
-        (condition-case ()
-            (progn
-              (rename-file 
-               buffer-file-name
-               (nnfolder-group-pathname new-name))
-              t)
-          (error nil))
+        (ignore-errors
+          (let ((new-file (nnfolder-group-pathname new-name)))
+            (gnus-make-directory (file-name-directory new-file))
+            (rename-file buffer-file-name 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)))
           (and entry (setcar entry new-name))
           (setq nnfolder-current-buffer nil
                 nnfolder-current-group nil)
           ;; Save the new group alist.
-          (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
+          (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
           ;; We kill the buffer instead of renaming it and stuff.
           (kill-buffer (current-buffer))
           t))))
 
+(deffoo nnfolder-request-regenerate (server)
+  (nnfolder-possibly-change-group nil server)
+  (nnfolder-generate-active-file)
+  t)
+
 \f
 ;;; Internal functions.
 
+(defun nnfolder-adjust-min-active (group)
+  ;; Find the lowest active article in this group.
+  (let* ((active (cadr (assoc group nnfolder-group-alist)))
+        (marker (concat "\n" nnfolder-article-marker))
+        (number "[0-9]+")
+        (activemin (cdr active)))
+    (save-excursion
+      (set-buffer nnfolder-current-buffer)
+      (goto-char (point-min))
+      (while (and (search-forward marker nil t)
+                 (re-search-forward number nil t))
+       (let ((newnum (string-to-number (match-string 0))))
+         (if (nnmail-within-headers-p)
+             (setq activemin (min activemin newnum)))))
+      (setcar active activemin))))
+
 (defun nnfolder-article-string (article)
   (if (numberp article)
       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
     (concat "\nMessage-ID: " article)))
 
-(defun nnfolder-delete-mail (&optional force leave-delim)
-  "Delete the message that point is in."
-  (save-excursion
-    (delete-region
+(defun nnfolder-goto-article (article)
+  "Place point at the start of the headers of ARTICLE.
+ARTICLE can be an article number or a Message-ID.
+Returns t if successful, nil otherwise."
+  (let ((art-string (nnfolder-article-string article))
+       start found)
+    ;; It is likely that we are at or before the delimiter line.
+    ;; We therefore go to the end of the previous line, and start
+    ;; searching from there.
+    (beginning-of-line)
+    (unless (bobp)
+      (forward-char -1))
+    (setq start (point))
+    ;; First search forward.
+    (while (and (setq found (search-forward art-string nil t))
+               (not (nnmail-within-headers-p))))
+    ;; If unsuccessful, search backward from where we started,
+    (unless found
+      (goto-char start)
+      (while (and (setq found (search-backward art-string nil t))
+                 (not (nnmail-within-headers-p)))))
+    (when found