96c53bb45f3990d59eacc0e16a6195810ea62cdf
[elisp/gnus.git-] / lisp / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <simon@josefsson.org> (adding MARKS)
6 ;;      ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
7 ;;      Scott Byer <byer@mv.us.adobe.com>
8 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; Keywords: mail
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;;; Code:
32
33 (eval-when-compile (require 'cl))
34 (eval-when-compile (require 'gnus-clfns))
35
36 (require 'nnheader)
37 (require 'message)
38 (require 'nnmail)
39 (require 'nnoo)
40 (require 'gnus)
41 (require 'gnus-util)
42 (require 'gnus-range)
43
44 (eval-and-compile
45   (autoload 'gnus-article-unpropagatable-p "gnus-sum")
46   (autoload 'gnus-intersection "gnus-range"))
47
48 (nnoo-declare nnfolder)
49
50 (defvoo nnfolder-directory (expand-file-name message-directory)
51   "The name of the nnfolder directory.")
52
53 (defvoo nnfolder-nov-directory nil
54   "The name of the nnfolder NOV directory.
55 If nil, `nnfolder-directory' is used.")
56
57 (defvoo nnfolder-marks-directory nil
58   "The name of the nnfolder MARKS directory.
59 If nil, `nnfolder-directory' is used.")
60
61 (defvoo nnfolder-active-file
62     (nnheader-concat nnfolder-directory "active")
63   "The name of the active file.")
64
65 ;; I renamed this variable to something more in keeping with the general GNU
66 ;; style. -SLB
67
68 (defvoo nnfolder-ignore-active-file nil
69   "If non-nil, the active file is ignored.
70 This causes nnfolder to do some extra work in order to determine the
71 true active ranges of an mbox file.  Note that the active file is
72 still saved, but its values are not used.  This costs some extra time
73 when scanning an mbox when opening it.")
74
75 (defvoo nnfolder-distrust-mbox nil
76   "If non-nil, the folder will be distrusted.
77 This means that nnfolder will not trust the user with respect to
78 inserting unaccounted for mail in the middle of an mbox file.  This
79 can greatly slow down scans, which now must scan the entire file for
80 unmarked messages.  When nil, scans occur forward from the last marked
81 message, a huge time saver for large mailboxes.")
82
83 (defvoo nnfolder-newsgroups-file
84     (concat (file-name-as-directory nnfolder-directory) "newsgroups")
85   "Mail newsgroups description file.")
86
87 (defvoo nnfolder-get-new-mail t
88   "If non-nil, nnfolder will check the incoming mail file and split the mail.")
89
90 (defvoo nnfolder-prepare-save-mail-hook nil
91   "Hook run narrowed to an article before saving.")
92
93 (defvoo nnfolder-save-buffer-hook nil
94   "Hook run before saving the nnfolder mbox buffer.")
95
96
97 (defvoo nnfolder-inhibit-expiry nil
98   "If non-nil, inhibit expiry.")
99
100 \f
101
102 (defconst nnfolder-version "nnfolder 2.0"
103   "nnfolder version.")
104
105 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
106   "String used to demarcate what the article number for a message is.")
107
108 (defvoo nnfolder-current-group nil)
109 (defvoo nnfolder-current-buffer nil)
110 (defvoo nnfolder-status-string "")
111 (defvoo nnfolder-group-alist nil)
112 (defvoo nnfolder-buffer-alist nil)
113 (defvoo nnfolder-scantime-alist nil)
114 (defvoo nnfolder-active-timestamp nil)
115 (defvoo nnfolder-active-file-coding-system nnheader-text-coding-system)
116 (defvoo nnfolder-active-file-coding-system-for-write
117     nnmail-active-file-coding-system)
118 (defvoo nnfolder-file-coding-system nnheader-text-coding-system)
119 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
120   "Coding system for save nnfolder file.
121 if nil, `nnfolder-file-coding-system' is used.") ; FIXME: fill-in the doc-string of this variable
122
123 (defvoo nnfolder-nov-is-evil nil
124   "If non-nil, Gnus will never generate and use nov databases for mail groups.
125 Using nov databases will speed up header fetching considerably.
126 This variable shouldn't be flipped much.  If you have, for some reason,
127 set this to t, and want to set it to nil again, you should always run
128 the `nnfolder-generate-active-file' command.  The function will go
129 through all nnfolder directories and generate nov databases for them
130 all.  This may very well take some time.")
131
132 (defvoo nnfolder-nov-file-suffix ".nov")
133
134 (defvoo nnfolder-nov-buffer-alist nil)
135
136 (defvar nnfolder-nov-buffer-file-name nil)
137
138 (defvoo nnfolder-marks-is-evil nil
139   "If non-nil, Gnus will never generate and use marks file for mail groups.
140 Using marks files makes it possible to backup and restore mail groups
141 separately from `.newsrc.eld'.  If you have, for some reason, set
142 this to t, and want to set it to nil again, you should always remove
143 the corresponding marks file (usually base nnfolder file name
144 concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
145 the group.  Then the marks file will be regenerated properly by Gnus.")
146
147 (defvoo nnfolder-marks nil)
148
149 (defvoo nnfolder-marks-file-suffix ".mrk")
150
151 (defvar nnfolder-marks-modtime (gnus-make-hashtable))
152
153 \f
154
155 ;;; Interface functions
156
157 (nnoo-define-basics nnfolder)
158
159 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
160   (save-excursion
161     (set-buffer nntp-server-buffer)
162     (erase-buffer)
163     (let (article start stop)
164       (nnfolder-possibly-change-group group server)
165       (when nnfolder-current-buffer
166         (set-buffer nnfolder-current-buffer)
167         (goto-char (point-min))
168         (if (stringp (car articles))
169             'headers
170           (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
171               'nov
172             (setq articles (gnus-sorted-intersection
173                             ;; Is ARTICLES sorted?
174                             (sort articles '<)
175                             (nnfolder-existing-articles)))
176             (while (setq article (pop articles))
177               (set-buffer nnfolder-current-buffer)
178               (when (nnfolder-goto-article article)
179                 (setq start (point))
180                 (setq stop (if (search-forward "\n\n" nil t)
181                                (1- (point))
182                              (point-max)))
183                 (set-buffer nntp-server-buffer)
184                 (insert (format "221 %d Article retrieved.\n" article))
185                 (insert-buffer-substring nnfolder-current-buffer start stop)
186                 (goto-char (point-max))
187                 (insert ".\n")))
188             (set-buffer nntp-server-buffer)
189             (nnheader-fold-continuation-lines)
190             'headers))))))
191
192 (deffoo nnfolder-open-server (server &optional defs)
193   (nnoo-change-server 'nnfolder server defs)
194   (nnmail-activate 'nnfolder t)
195   (gnus-make-directory nnfolder-directory)
196   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
197     (and nnfolder-nov-directory
198          (gnus-make-directory nnfolder-nov-directory)))
199   (unless nnfolder-marks-is-evil
200     (and nnfolder-marks-directory
201          (gnus-make-directory nnfolder-marks-directory)))
202   (cond
203    ((not (file-exists-p nnfolder-directory))
204     (nnfolder-close-server)
205     (nnheader-report 'nnfolder "Couldn't create directory: %s"
206                      nnfolder-directory))
207    ((not (file-directory-p (file-truename nnfolder-directory)))
208     (nnfolder-close-server)
209     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
210    (t
211     (nnmail-activate 'nnfolder)
212     (nnheader-report 'nnfolder "Opened server %s using directory %s"
213                      server nnfolder-directory)
214     t)))
215
216 (deffoo nnfolder-request-close ()
217   (let ((alist nnfolder-buffer-alist))
218     (while alist
219       (nnfolder-close-group (caar alist) nil t)
220       (setq alist (cdr alist))))
221   (nnoo-close-server 'nnfolder)
222   (setq nnfolder-buffer-alist nil
223         nnfolder-group-alist nil))
224
225 (deffoo nnfolder-request-article (article &optional group server buffer)
226   (nnfolder-possibly-change-group group server)
227   (save-excursion
228     (set-buffer nnfolder-current-buffer)
229     (goto-char (point-min))
230     (when (nnfolder-goto-article article)
231       (let (start stop)
232         (setq start (point))
233         (forward-line 1)
234         (unless (and (nnmail-search-unix-mail-delim)
235                      (forward-line -1))
236           (goto-char (point-max)))
237         (setq stop (point))
238         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
239           (set-buffer nntp-server-buffer)
240           (erase-buffer)
241           (insert-buffer-substring nnfolder-current-buffer start stop)
242           (goto-char (point-min))
243           (while (looking-at "From ")
244             (delete-char 5)
245             (insert "X-From-Line: ")
246             (forward-line 1))
247           (if (numberp article)
248               (cons nnfolder-current-group article)
249             (goto-char (point-min))
250             (cons nnfolder-current-group
251                   (if (search-forward (concat "\n" nnfolder-article-marker)
252                                       nil t)
253                       (string-to-int
254                        (buffer-substring
255                         (point) (progn (end-of-line) (point))))
256                     -1))))))))
257
258 (deffoo nnfolder-request-group (group &optional server dont-check)
259   (nnfolder-possibly-change-group group server t)
260   (save-excursion
261     (if (not (assoc group nnfolder-group-alist))
262         (nnheader-report 'nnfolder "No such group: %s" group)
263       (if dont-check
264           (progn
265             (nnheader-report 'nnfolder "Selected group %s" group)
266             t)
267         (let* ((active (assoc group nnfolder-group-alist))
268                (group (car active))
269                (range (cadr active)))
270           (cond
271            ((null active)
272             (nnheader-report 'nnfolder "No such group: %s" group))
273            ((null nnfolder-current-group)
274             (nnheader-report 'nnfolder "Empty group: %s" group))
275            (t
276             (nnheader-report 'nnfolder "Selected group %s" group)
277             (nnheader-insert "211 %d %d %d %s\n"
278                              (1+ (- (cdr range) (car range)))
279                              (car range) (cdr range) group))))))))
280
281 (deffoo nnfolder-request-scan (&optional group server)
282   (nnfolder-possibly-change-group nil server)
283   (when nnfolder-get-new-mail
284     (nnfolder-possibly-change-group group server)
285     (nnmail-get-new-mail
286      'nnfolder
287      (lambda ()
288        (let ((bufs nnfolder-buffer-alist))
289          (save-excursion
290            (while bufs
291              (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
292                  (setq nnfolder-buffer-alist
293                        (delq (car bufs) nnfolder-buffer-alist))
294                (set-buffer (nth 1 (car bufs)))
295                (nnfolder-save-buffer)
296                (kill-buffer (current-buffer)))
297              (setq bufs (cdr bufs))))))
298      nnfolder-directory
299      group)))
300
301 ;; Don't close the buffer if we're not shutting down the server.  This way,
302 ;; we can keep the buffer in the group buffer cache, and not have to grovel
303 ;; over the buffer again unless we add new mail to it or modify it in some
304 ;; way.
305
306 (deffoo nnfolder-close-group (group &optional server force)
307   ;; Make sure we _had_ the group open.
308   (when (or (assoc group nnfolder-buffer-alist)
309             (equal group nnfolder-current-group))
310     (let ((inf (assoc group nnfolder-buffer-alist)))
311       (when inf
312         (when (and nnfolder-current-group
313                    nnfolder-current-buffer)
314           (push (list nnfolder-current-group nnfolder-current-buffer)
315                 nnfolder-buffer-alist))
316         (setq nnfolder-buffer-alist
317               (delq inf nnfolder-buffer-alist))
318         (setq nnfolder-current-buffer (cadr inf)
319               nnfolder-current-group (car inf))))
320     (when (and nnfolder-current-buffer
321                (buffer-name nnfolder-current-buffer))
322       (save-excursion
323         (set-buffer nnfolder-current-buffer)
324         ;; If the buffer was modified, write the file out now.
325         (nnfolder-save-buffer)
326         ;; If we're shutting the server down, we need to kill the
327         ;; buffer and remove it from the open buffer list.  Or, of
328         ;; course, if we're trying to minimize our space impact.
329         (kill-buffer (current-buffer))
330         (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
331                                           nnfolder-buffer-alist)))))
332   (setq nnfolder-current-group nil
333         nnfolder-current-buffer nil)
334   t)
335
336 (deffoo nnfolder-request-create-group (group &optional server args)
337   (nnfolder-possibly-change-group nil server)
338   (nnmail-activate 'nnfolder)
339   (when group
340     (unless (assoc group nnfolder-group-alist)
341       (push (list group (cons 1 0)) nnfolder-group-alist)
342       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
343       (nnfolder-read-folder group)))
344   t)
345
346 (deffoo nnfolder-request-list (&optional server)
347   (nnfolder-possibly-change-group nil server)
348   (save-excursion
349     (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
350       (nnmail-find-file nnfolder-active-file)
351       (setq nnfolder-group-alist (nnmail-get-active)))
352     t))
353
354 (deffoo nnfolder-request-newgroups (date &optional server)
355   (nnfolder-possibly-change-group nil server)
356   (nnfolder-request-list server))
357
358 (deffoo nnfolder-request-list-newsgroups (&optional server)
359   (nnfolder-possibly-change-group nil server)
360   (save-excursion
361     (let ((nnmail-file-coding-system nnfolder-file-coding-system))
362       (nnmail-find-file nnfolder-newsgroups-file))))
363
364 ;; Return a list consisting of all article numbers existing in the
365 ;; current folder.
366
367 (defun nnfolder-existing-articles ()
368   (save-excursion
369     (when nnfolder-current-buffer
370       (set-buffer nnfolder-current-buffer)
371       (goto-char (point-min))
372       (let ((marker (concat "\n" nnfolder-article-marker))
373             (number "[0-9]+")
374             numbers)
375         (while (and (search-forward marker nil t)
376                     (re-search-forward number nil t))
377           (let ((newnum (string-to-number (match-string 0))))
378             (if (nnmail-within-headers-p)
379                 (push newnum numbers))))
380         ;; The article numbers are increasing, so this result is sorted.
381         (nreverse numbers)))))
382
383 (deffoo nnfolder-request-expire-articles
384     (articles newsgroup &optional server force)
385   (nnfolder-possibly-change-group newsgroup server)
386   (let* ((is-old t)
387          ;; The articles we have deleted so far.
388          (deleted-articles nil)
389          ;; The articles that really exist and will
390          ;; be expired if they are old enough.
391          (maybe-expirable
392           (gnus-sorted-intersection articles (nnfolder-existing-articles))))
393     (nnmail-activate 'nnfolder)
394
395     (save-excursion
396       (set-buffer nnfolder-current-buffer)
397       ;; Since messages are sorted in arrival order and expired in the
398       ;; same order, we can stop as soon as we find a message that is
399       ;; too old.
400       (while (and maybe-expirable is-old)
401         (goto-char (point-min))
402         (when (and (nnfolder-goto-article (car maybe-expirable))
403                    (search-forward (concat "\n" nnfolder-article-marker)
404                                    nil t))
405           (forward-sexp)
406           (when (setq is-old
407                       (nnmail-expired-article-p
408                        newsgroup
409                        (buffer-substring
410                         (point) (progn (end-of-line) (point)))
411                        force nnfolder-inhibit-expiry))
412             (unless (eq nnmail-expiry-target 'delete)
413               (with-temp-buffer
414                 (nnfolder-request-article (car maybe-expirable)
415                                           newsgroup server (current-buffer))
416                 (let ((nnfolder-current-directory nil))
417                   (nnmail-expiry-target-group
418                    nnmail-expiry-target newsgroup)))
419               (nnfolder-possibly-change-group newsgroup server))
420             (nnheader-message 5 "Deleting article %d in %s..."
421                               (car maybe-expirable) newsgroup)
422             (nnfolder-delete-mail)
423             (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
424               (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
425             ;; Must remember which articles were actually deleted
426             (push (car maybe-expirable) deleted-articles)))
427         (setq maybe-expirable (cdr maybe-expirable)))
428       (unless nnfolder-inhibit-expiry
429         (nnheader-message 5 "Deleting articles...done"))
430       (nnfolder-save-buffer)
431       (nnfolder-adjust-min-active newsgroup)
432       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
433       (gnus-sorted-complement articles (nreverse deleted-articles)))))
434
435 (deffoo nnfolder-request-move-article (article group server
436                                                accept-form &optional last)
437   (save-excursion
438     (let ((buf (get-buffer-create " *nnfolder move*"))
439           result)
440       (and
441        (nnfolder-request-article article group server)
442        (save-excursion
443          (set-buffer buf)
444          (erase-buffer)
445          (insert-buffer-substring nntp-server-buffer)
446          (goto-char (point-min))
447          (while (re-search-forward
448                  (concat "^" nnfolder-article-marker)
449                  (save-excursion (and (search-forward "\n\n" nil t) (point)))
450                  t)
451            (delete-region (progn (beginning-of-line) (point))
452                           (progn (forward-line 1) (point))))
453          (setq result (eval accept-form))
454          (kill-buffer buf)
455          result)
456        (save-excursion
457          (nnfolder-possibly-change-group group server)
458          (set-buffer nnfolder-current-buffer)
459          (goto-char (point-min))
460          (when (nnfolder-goto-article article)
461            (nnfolder-delete-mail))
462          (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
463            (nnfolder-nov-delete-article group article))
464          (when last
465            (nnfolder-save-buffer)
466            (nnfolder-adjust-min-active group)
467            (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
468       result)))
469
470 (deffoo nnfolder-request-accept-article (group &optional server last)
471   (save-excursion
472     (nnfolder-possibly-change-group group server)
473     (nnmail-check-syntax)
474     (let ((buf (current-buffer))
475           result art-group)
476       (goto-char (point-min))
477       (when (looking-at "X-From-Line: ")
478         (save-match-data
479           (mail-header-unfold-field))
480         (replace-match "From "))
481       (with-temp-buffer
482         (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
483               (nntp-server-buffer (current-buffer)))
484           (nnmail-find-file nnfolder-active-file)
485           (setq nnfolder-group-alist (nnmail-parse-active))))
486       (save-excursion
487         (goto-char (point-min))
488         (if (search-forward "\n\n" nil t)
489             (forward-line -1)
490           (goto-char (point-max)))
491         (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
492           (delete-region (point) (progn (forward-line 1) (point))))
493         (when nnmail-cache-accepted-message-ids
494           (nnmail-cache-insert (nnmail-fetch-field "message-id")))
495         (setq result (if (stringp group)
496                          (list (cons group (nnfolder-active-number group)))
497                        (setq art-group
498                              (nnmail-article-group 'nnfolder-active-number))))
499         (if (and (null result)
500                  (yes-or-no-p "Moved to `junk' group; delete article? "))
501             (setq result 'junk)
502           (setq result
503                 (car (nnfolder-save-mail result)))))
504       (when last
505         (save-excursion
506           (nnfolder-possibly-change-folder (or (caar art-group) group))
507           (nnfolder-save-buffer)
508           (when nnmail-cache-accepted-message-ids
509             (nnmail-cache-close))))
510       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
511       (unless result
512         (nnheader-report 'nnfolder "Couldn't store article"))
513       result)))
514
515 (deffoo nnfolder-request-replace-article (article group buffer)
516   (nnfolder-possibly-change-group group)
517   (save-excursion
518     (set-buffer buffer)
519     (goto-char (point-min))
520     (if (not (looking-at "X-From-Line: "))
521         (insert "From nobody " (current-time-string) "\n")
522       (save-match-data
523         (mail-header-unfold-field))
524       (replace-match "From "))
525     (nnfolder-normalize-buffer)
526     (set-buffer nnfolder-current-buffer)
527     (goto-char (point-min))
528     (if (not (nnfolder-goto-article article))
529         nil
530       (nnfolder-delete-mail)
531       (insert-buffer-substring buffer)
532       (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
533         (save-excursion
534           (set-buffer buffer)
535           (let ((headers (nnfolder-parse-head article
536                                               (point-min) (point-max))))
537             (with-current-buffer (nnfolder-open-nov group)
538               (if (nnheader-find-nov-line article)
539                   (delete-region (point) (progn (forward-line 1) (point))))
540               (nnheader-insert-nov headers)))))
541       (nnfolder-save-buffer)
542       t)))
543
544 (deffoo nnfolder-request-delete-group (group &optional force server)
545   (nnfolder-close-group group server t)
546   ;; Delete all articles in GROUP.
547   (if (not force)
548       ()                                ; Don't delete the articles.
549     ;; Delete the file that holds the group.
550     (ignore-errors
551       (delete-file (nnfolder-group-pathname group))
552       (when (file-exists-p (nnfolder-group-nov-pathname group))
553         (delete-file (nnfolder-group-nov-pathname group)))
554       (when (file-exists-p (nnfolder-group-marks-pathname group))
555         (delete-file (nnfolder-group-marks-pathname group)))))
556   ;; Remove the group from all structures.
557   (setq nnfolder-group-alist
558         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
559         nnfolder-current-group nil
560         nnfolder-current-buffer nil)
561   ;; Save the active file.
562   (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
563   t)
564
565 (deffoo nnfolder-request-rename-group (group new-name &optional server)
566   (nnfolder-possibly-change-group group server)
567   (save-excursion
568     (set-buffer nnfolder-current-buffer)
569     (and (file-writable-p buffer-file-name)
570          (ignore-errors
571            (let ((new-file (nnfolder-group-pathname new-name)))
572              (gnus-make-directory (file-name-directory new-file))
573              (rename-file buffer-file-name new-file)
574              (when (file-exists-p (nnfolder-group-nov-pathname group))
575                (setq new-file (nnfolder-group-nov-pathname new-name))
576                (gnus-make-directory (file-name-directory new-file))
577                (rename-file (nnfolder-group-nov-pathname group) new-file))
578              (when (file-exists-p (nnfolder-group-marks-pathname group))
579                (setq new-file (nnfolder-group-marks-pathname new-name))
580                (gnus-make-directory (file-name-directory new-file))
581                (rename-file (nnfolder-group-marks-pathname group) new-file)))
582            t)
583          ;; That went ok, so we change the internal structures.
584          (let ((entry (assoc group nnfolder-group-alist)))
585            (and entry (setcar entry new-name))
586            (setq nnfolder-current-buffer nil
587                  nnfolder-current-group nil)
588            ;; Save the new group alist.
589            (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
590            ;; We kill the buffer instead of renaming it and stuff.
591            (kill-buffer (current-buffer))
592            t))))
593
594 (deffoo nnfolder-request-regenerate (server)
595   (nnfolder-possibly-change-group nil server)
596   (nnfolder-generate-active-file)
597   t)
598
599 \f
600 ;;; Internal functions.
601
602 (defun nnfolder-adjust-min-active (group)
603   ;; Find the lowest active article in this group.
604   (let* ((active (cadr (assoc group nnfolder-group-alist)))
605          (marker (concat "\n" nnfolder-article-marker))
606          (number "[0-9]+")
607          (activemin (cdr active)))
608     (save-excursion
609       (set-buffer nnfolder-current-buffer)
610       (goto-char (point-min))
611       (while (and (search-forward marker nil t)
612                   (re-search-forward number nil t))
613         (let ((newnum (string-to-number (match-string 0))))
614           (if (nnmail-within-headers-p)
615               (setq activemin (min activemin newnum)))))
616       (setcar active activemin))))
617
618 (defun nnfolder-article-string (article)
619   (if (numberp article)
620       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
621     (concat "\nMessage-ID: " article)))
622
623 (defun nnfolder-goto-article (article)
624   "Place point at the start of the headers of ARTICLE.
625 ARTICLE can be an article number or a Message-ID.
626 Returns t if successful, nil otherwise."
627   (let ((art-string (nnfolder-article-string article))
628         start found)
629     ;; It is likely that we are at or before the delimiter line.
630     ;; We therefore go to the end of the previous line, and start
631     ;; searching from there.
632     (beginning-of-line)
633     (unless (bobp)
634       (forward-char -1))
635     (setq start (point))
636     ;; First search forward.
637     (while (and (setq found (search-forward art-string nil t))
638                 (not (nnmail-within-headers-p))))
639     ;; If unsuccessful, search backward from where we started,
640     (unless found
641       (goto-char start)
642       (while (and (setq found (search-backward art-string nil t))
643                   (not (nnmail-within-headers-p)))))
644     (when found
645       (nnmail-search-unix-mail-delim-backward))))
646
647 (defun nnfolder-delete-mail (&optional leave-delim)
648   "Delete the message that point is in.
649 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
650 deleted.  Point is left where the deleted region was."
651   (save-restriction
652     (narrow-to-region
653      (save-excursion
654        ;; In case point is at the beginning of the message already.
655        (forward-line 1)
656        (nnmail-search-unix-mail-delim-backward)
657        (if leave-delim (progn (forward-line 1) (point))
658          (point)))
659      (progn
660        (forward-line 1)
661        (if (nnmail-search-unix-mail-delim)
662            (point)
663          (point-max))))
664     (run-hooks 'nnfolder-delete-mail-hook)
665     (delete-region (point-min) (point-max))))
666
667 (defun nnfolder-possibly-change-group (group &optional server dont-check)
668   ;; Change servers.
669   (when (and server
670              (not (nnfolder-server-opened server)))
671     (nnfolder-open-server server))
672   (unless (gnus-buffer-live-p nnfolder-current-buffer)
673     (setq nnfolder-current-buffer nil
674           nnfolder-current-group nil))
675   ;; Change group.
676   (let ((file-name-coding-system nnmail-pathname-coding-system)
677         (pathname-coding-system nnmail-pathname-coding-system))
678     (when (and group
679                (not (equal group nnfolder-current-group))
680                (progn
681                  (nnmail-activate 'nnfolder)
682                  (and (assoc group nnfolder-group-alist)
683                       (file-exists-p (nnfolder-group-pathname group)))))
684       (if dont-check
685           (setq nnfolder-current-group group
686                 nnfolder-current-buffer nil)
687         (let (inf file)
688           ;; If we have to change groups, see if we don't already have the
689           ;; folder in memory.  If we do, verify the modtime and destroy
690           ;; the folder if needed so we can rescan it.
691           (setq nnfolder-current-buffer
692                 (nth 1 (assoc group nnfolder-buffer-alist)))
693
694           ;; If the buffer is not live, make sure it isn't in the alist.  If it
695           ;; is live, verify that nobody else has touched the file since last
696           ;; time.
697           (when (and nnfolder-current-buffer
698                      (not (gnus-buffer-live-p nnfolder-current-buffer)))
699             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
700                   nnfolder-current-buffer nil))
701
702           (setq nnfolder-current-group group)
703
704           (when (or (not nnfolder-current-buffer)
705                     (not (verify-visited-file-modtime
706                           nnfolder-current-buffer)))
707             (save-excursion
708               (setq file (nnfolder-group-pathname group))
709               ;; See whether we need to create the new file.
710               (unless (file-exists-p file)
711                 (gnus-make-directory (file-name-directory file))
712                 (let ((nnmail-file-coding-system
713                        (or nnfolder-file-coding-system-for-write
714                            nnfolder-file-coding-system-for-write)))
715                   (nnmail-write-region 1 1 file t 'nomesg)))
716               (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
717                 (set-buffer nnfolder-current-buffer)
718                 (push (list group nnfolder-current-buffer)
719                       nnfolder-buffer-alist)))))))))
720
721 (defun nnfolder-save-mail (group-art-list)
722   "Called narrowed to an article."
723   (let* (save-list group-art)
724     (goto-char (point-min))
725     ;; The From line may have been quoted by movemail.
726     (when (looking-at ">From")
727       (delete-char 1))
728     ;; This might come from somewhere else.
729     (unless (looking-at "From ")
730       (insert "From nobody " (current-time-string) "\n")
731       (goto-char (point-min)))
732     ;; Quote all "From " lines in the article.
733     (forward-line 1)
734     (let (case-fold-search)
735       (while (re-search-forward "^From " nil t)
736         (beginning-of-line)
737         (insert "> ")))
738     (setq save-list group-art-list)
739     (nnmail-insert-lines)
740     (nnmail-insert-xref group-art-list)
741     (run-hooks 'nnmail-prepare-save-mail-hook)
742     (run-hooks 'nnfolder-prepare-save-mail-hook)
743
744     ;; Insert the mail into each of the destination groups.
745     (while (setq group-art (pop group-art-list))
746       ;; Kill any previous newsgroup markers.
747       (goto-char (point-min))
748       (if (search-forward "\n\n" nil t)
749           (forward-line -1)
750         (goto-char (point-max)))
751       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
752         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
753
754       ;; Insert the new newsgroup marker.
755       (nnfolder-insert-newsgroup-line group-art)
756
757       (save-excursion
758         (let ((beg (point-min))
759               (end (point-max))
760               (obuf (current-buffer)))
761           (nnfolder-possibly-change-folder (car group-art))
762           (let ((buffer-read-only nil))
763             (nnfolder-normalize-buffer)
764             (insert-buffer-substring obuf beg end))
765           (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
766             (set-buffer obuf)
767             (nnfolder-add-nov (car group-art) (cdr group-art)
768                               (nnfolder-parse-head nil beg end))))))
769
770     ;; Did we save it anywhere?
771     save-list))
772
773 (defun nnfolder-normalize-buffer ()
774   "Make sure there are two newlines at the end of the buffer."
775   (goto-char (point-max))
776   (skip-chars-backward "\n")
777   (delete-region (point) (point-max))
778   (unless (bobp)
779     (insert "\n\n")))
780
781 (defun nnfolder-insert-newsgroup-line (group-art)
782   (save-excursion
783     (goto-char (point-min))
784     (unless (search-forward "\n\n" nil t)
785       (goto-char (point-max))
786       (insert "\n"))
787     (forward-char -1)
788     (insert (format (concat nnfolder-article-marker "%d   %s\n")
789                     (cdr group-art) (current-time-string)))))
790
791 (defun nnfolder-active-number (group)
792   ;; Find the next article number in GROUP.
793   (let ((active (cadr (assoc group nnfolder-group-alist))))
794     (if active
795         (setcdr active (1+ (cdr active)))
796       ;; This group is new, so we create a new entry for it.
797       ;; This might be a bit naughty... creating groups on the drop of
798       ;; a hat, but I don't know...
799       (push (list group (setq active (cons 1 1)))
800             nnfolder-group-alist))
801     (cdr active)))
802
803 (defun nnfolder-possibly-change-folder (group)
804   (let ((inf (assoc group nnfolder-buffer-alist)))
805     (if (and inf
806              (gnus-buffer-live-p (cadr inf)))
807         (set-buffer (cadr inf))
808       (when inf
809         (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
810       (when nnfolder-group-alist
811         (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
812       (push (list group (nnfolder-read-folder group))
813             nnfolder-buffer-alist))))
814
815 ;; This method has a problem if you've accidentally let the active
816 ;; list get out of sync with the files.  This could happen, say, if
817 ;; you've accidentally gotten new mail with something other than Gnus
818 ;; (but why would _that_ ever happen? :-).  In that case, we will be
819 ;; in the middle of processing the file, ready to add new X-Gnus
820 ;; article number markers, and we'll run across a message with no ID
821 ;; yet - the active list _may_not_ be ready for us yet.
822
823 ;; To handle this, I'm modifying this routine to maintain the maximum
824 ;; ID seen so far, and when we hit a message with no ID, we will
825 ;; _manually_ scan the rest of the message looking for any more,
826 ;; possibly higher IDs.  We'll assume the maximum that we find is the
827 ;; highest active.  Note that this shouldn't cost us much extra time
828 ;; at all, but will be a lot less vulnerable to glitches between the
829 ;; mbox and the active file.
830
831 (defun nnfolder-read-folder (group)
832   (let* ((file (nnfolder-group-pathname group))
833          (nov  (nnfolder-group-nov-pathname group))
834          (buffer (set-buffer
835                   (let ((nnheader-file-coding-system
836                          nnfolder-file-coding-system))
837                     (nnheader-find-file-noselect file)))))
838     (if (equal (cadr (assoc group nnfolder-scantime-alist))
839                (nth 5 (file-attributes file)))
840         ;; This looks up-to-date, so we don't do any scanning.
841         (if (file-exists-p file)
842             buffer
843           (push (list group buffer) nnfolder-buffer-alist)
844           (set-buffer-modified-p t)
845           (nnfolder-save-buffer))
846       ;; Parse the damn thing.
847       (save-excursion
848         (goto-char (point-min))
849         ;; Remove any blank lines at the start.
850         (while (eq (following-char) ?\n)
851           (delete-char 1))
852         (nnmail-activate 'nnfolder)
853         ;; Read in the file.
854         (let ((delim "^From ")
855               (marker (concat "\n" nnfolder-article-marker))
856               (number "[0-9]+")
857               (active (or (cadr (assoc group nnfolder-group-alist))
858                           (cons 1 0)))
859               (scantime (assoc group nnfolder-scantime-alist))
860               (minid (lsh -1 -1))
861               maxid start end newscantime
862               novbuf articles newnum
863               buffer-read-only)
864           (buffer-disable-undo)
865           (setq maxid (cdr active))
866
867           (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
868                       (and (file-exists-p nov)
869                            (file-newer-than-file-p nov file)))
870             (unless (file-exists-p nov)
871               (gnus-make-directory (file-name-directory nov)))
872             (with-current-buffer
873                 (setq novbuf (nnfolder-open-nov group))
874               (goto-char (point-min))
875               (while (not (eobp))
876                 (push (read novbuf) articles)
877                 (forward-line 1))
878               (setq articles (nreverse articles))))
879           (goto-char (point-min))
880
881           ;; Anytime the active number is 1 or 0, it is suspect.  In that
882           ;; case, search the file manually to find the active number.  Or,
883           ;; of course, if we're being paranoid.  (This would also be the
884           ;; place to build other lists from the header markers, such as
885           ;; expunge lists, etc., if we ever desired to abandon the active
886           ;; file entirely for mboxes.)
887           (when (or nnfolder-ignore-active-file
888                     novbuf
889                     (< maxid 2))
890             (while (and (search-forward marker nil t)
891                         (looking-at number))
892               (setq newnum (string-to-number (match-string 0)))
893               (when (nnmail-within-headers-p)
894                 (setq maxid (max maxid newnum)
895                       minid (min minid newnum))
896                 (when novbuf
897                   (if (memq newnum articles)
898                       (setq articles (delq newnum articles))
899                     (let ((headers (nnfolder-parse-head newnum)))
900                       (with-current-buffer novbuf
901                         (nnheader-find-nov-line newnum)
902                         (nnheader-insert-nov headers)))))))
903             (when (and novbuf articles)
904               (with-current-buffer novbuf
905                 (dolist (article articles)
906                   (when (nnheader-find-nov-line article)
907                     (delete-region (point)
908                                    (progn (forward-line 1) (point)))))))
909             (setcar active (max 1 (min minid maxid)))
910             (setcdr active (max maxid (cdr active)))
911             (goto-char (point-min)))
912
913           ;; As long as we trust that the user will only insert unmarked mail
914           ;; at the end, go to the end and search backwards for the last
915           ;; marker.  Find the start of that message, and begin to search for
916           ;; unmarked messages from there.
917           (when (not (or nnfolder-distrust-mbox
918                          (< maxid 2)))
919             (goto-char (point-max))
920             (unless (re-search-backward marker nil t)
921               (goto-char (point-min)))
922             ;;(when (nnmail-search-unix-mail-delim)
923             ;;  (goto-char (point-min)))
924             )
925
926           ;; Keep track of the active number on our own, and insert it back
927           ;; into the active list when we're done.  Also, prime the pump to
928           ;; cut down on the number of searches we do.
929           (unless (nnmail-search-unix-mail-delim)
930             (goto-char (point-max)))
931           (setq end (point-marker))
932           (while (not (= end (point-max)))
933             (setq start (marker-position end))
934             (goto-char end)
935             ;; There may be more than one "From " line, so we skip past
936             ;; them.
937             (while (looking-at delim)
938               (forward-line 1))
939             (set-marker end (if (nnmail-search-unix-mail-delim)
940                                 (point)
941                               (point-max)))
942             (goto-char start)
943             (when (not (search-forward marker end t))
944               (narrow-to-region start end)
945               (nnmail-insert-lines)
946               (nnfolder-insert-newsgroup-line
947                (cons nil
948                      (setq newnum
949                            (nnfolder-active-number group))))
950               (when novbuf
951                 (let ((headers (nnfolder-parse-head newnum (point-min)
952                                                     (point-max))))
953                   (with-current-buffer novbuf
954                     (goto-char (point-max))
955                     (nnheader-insert-nov headers))))
956               (widen)))
957
958           (set-marker end nil)
959           ;; Make absolutely sure that the active list reflects reality!
960           (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
961
962           ;; Set the scantime for this group.
963           (setq newscantime (visited-file-modtime))
964           (if scantime
965               (setcdr scantime (list newscantime))
966             (push (list group newscantime)
967                   nnfolder-scantime-alist))
968           ;; Save nov.
969           (when novbuf
970             (nnfolder-save-nov))
971           (current-buffer))))))
972
973 ;;;###autoload
974 (defun nnfolder-generate-active-file ()
975   "Look for mbox folders in the nnfolder directory and make them into groups.
976 This command does not work if you use short group names."
977   (interactive)
978   (nnmail-activate 'nnfolder)
979   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
980     (dolist (file (directory-files (or nnfolder-nov-directory
981                                        nnfolder-directory)
982                                    t
983                                    (concat
984                                     (regexp-quote nnfolder-nov-file-suffix)
985                                     "$")))
986       (when (not (message-mail-file-mbox-p file))
987         (ignore-errors
988           (delete-file file)))))
989   (let ((files (directory-files nnfolder-directory))
990         file)
991     (while (setq file (pop files))
992       (when (and (not (backup-file-name-p file))
993                  (message-mail-file-mbox-p
994                   (nnheader-concat nnfolder-directory file)))
995         (let ((oldgroup (assoc file nnfolder-group-alist)))
996           (if oldgroup
997               (nnheader-message 5 "Refreshing group %s..." file)
998             (nnheader-message 5 "Adding group %s..." file))
999           (if oldgroup
1000               (setq nnfolder-group-alist
1001                     (delq oldgroup (copy-sequence nnfolder-group-alist))))
1002           (push (list file (cons 1 0)) nnfolder-group-alist)
1003           (nnfolder-possibly-change-folder file)
1004           (nnfolder-possibly-change-group file)
1005           (nnfolder-close-group file))))
1006     (nnheader-message 5 "")))
1007
1008 (defun nnfolder-group-pathname (group)
1009   "Make pathname for GROUP."
1010   (setq group
1011         (encode-coding-string group nnmail-pathname-coding-system))
1012   (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
1013     ;; If this file exists, we use it directly.
1014     (if (or nnmail-use-long-file-names
1015             (file-exists-p (concat dir group)))
1016         (concat dir group)
1017       ;; If not, we translate dots into slashes.
1018       (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
1019
1020 (defun nnfolder-group-nov-pathname (group)
1021   "Make pathname for GROUP NOV."
1022   (let ((nnfolder-directory
1023          (or nnfolder-nov-directory nnfolder-directory)))
1024     (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
1025
1026 (defun nnfolder-save-buffer ()
1027   "Save the buffer."
1028   (when (buffer-modified-p)
1029     (run-hooks 'nnfolder-save-buffer-hook)
1030     (gnus-make-directory (file-name-directory (buffer-file-name)))
1031     (let* ((coding-system-for-write
1032             (or nnfolder-file-coding-system-for-write
1033                 nnfolder-file-coding-system))
1034            (output-coding-system coding-system-for-write))
1035       (save-buffer)))
1036   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1037     (nnfolder-save-nov)))
1038
1039 (defun nnfolder-save-active (group-alist active-file)
1040   (let ((nnmail-active-file-coding-system
1041          (or nnfolder-active-file-coding-system-for-write
1042              nnfolder-active-file-coding-system)))
1043     (nnmail-save-active group-alist active-file)))
1044
1045 (defun nnfolder-open-nov (group)
1046   (or (cdr (assoc group nnfolder-nov-buffer-alist))
1047       (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1048         (save-excursion
1049           (set-buffer buffer)
1050           (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1051                (nnfolder-group-nov-pathname group))
1052           (erase-buffer)
1053           (when (file-exists-p nnfolder-nov-buffer-file-name)
1054             (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1055         (push (cons group buffer) nnfolder-nov-buffer-alist)
1056         buffer)))
1057
1058 (defun nnfolder-save-nov ()
1059   (save-excursion
1060     (while nnfolder-nov-buffer-alist
1061       (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1062         (set-buffer (cdar nnfolder-nov-buffer-alist))
1063         (when (buffer-modified-p)
1064           (gnus-make-directory (file-name-directory
1065                                 nnfolder-nov-buffer-file-name))
1066           (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1067                                nil 'nomesg))
1068         (set-buffer-modified-p nil)
1069         (kill-buffer (current-buffer)))
1070       (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1071
1072 (defun nnfolder-nov-delete-article (group article)
1073   (save-excursion
1074     (set-buffer (nnfolder-open-nov group))
1075     (when (nnheader-find-nov-line article)
1076       (delete-region (point) (progn (forward-line 1) (point))))
1077     t))
1078
1079 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1080   (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1081       nil
1082     (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1083       (when (file-exists-p nov)
1084         (save-excursion
1085           (set-buffer nntp-server-buffer)
1086           (erase-buffer)
1087           (nnheader-insert-file-contents nov)
1088           (if (and fetch-old
1089                    (not (numberp fetch-old)))
1090               t                         ; Don't remove anything.
1091             (nnheader-nov-delete-outside-range
1092              (if fetch-old (max 1 (- (car articles) fetch-old))
1093                (car articles))
1094              (car (last articles)))
1095             t))))))
1096
1097 (defun nnfolder-parse-head (&optional number b e)
1098   "Parse the head of the current buffer."
1099   (let ((buf (current-buffer))
1100         chars)
1101     (save-excursion
1102       (unless b
1103         (setq b (if (nnmail-search-unix-mail-delim-backward)
1104                     (point) (point-min)))
1105         (forward-line 1)
1106         (setq e (if (nnmail-search-unix-mail-delim)
1107                     (point) (point-max))))
1108       (setq chars (- e b))
1109       (unless (zerop chars)
1110         (goto-char b)
1111         (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1112       (with-temp-buffer
1113         (insert-buffer-substring buf b e)
1114         ;; Fold continuation lines.
1115         (goto-char (point-min))
1116         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1117           (replace-match " " t t))
1118         ;; Remove any tabs; they are too confusing.
1119         (subst-char-in-region (point-min) (point-max) ?\t ? )
1120         (let ((headers (nnheader-parse-head t)))
1121           (mail-header-set-chars headers chars)
1122           (mail-header-set-number headers number)
1123           headers)))))
1124
1125 (defun nnfolder-add-nov (group article headers)
1126   "Add a nov line for the GROUP base."
1127   (save-excursion
1128     (set-buffer (nnfolder-open-nov group))
1129     (goto-char (point-max))
1130     (mail-header-set-number headers article)
1131     (nnheader-insert-nov headers)))
1132
1133 (deffoo nnfolder-request-set-mark (group actions &optional server)
1134   (when (and server
1135              (not (nnfolder-server-opened server)))
1136     (nnfolder-open-server server))
1137   (unless nnfolder-marks-is-evil
1138     (nnfolder-open-marks group server)
1139     (dolist (action actions)
1140       (let ((range (nth 0 action))
1141             (what  (nth 1 action))
1142             (marks (nth 2 action)))
1143         (assert (or (eq what 'add) (eq what 'del)) t
1144                 "Unknown request-set-mark action: %s" what)
1145         (dolist (mark marks)
1146           (setq nnfolder-marks (gnus-update-alist-soft
1147                             mark
1148                             (funcall (if (eq what 'add) 'gnus-range-add
1149                                        'gnus-remove-from-range)
1150                                      (cdr (assoc mark nnfolder-marks)) range)
1151                             nnfolder-marks)))))
1152     (nnfolder-save-marks group server))
1153   nil)
1154
1155 (deffoo nnfolder-request-update-info (group info &optional server)
1156   ;; Change servers.
1157   (when (and server
1158              (not (nnfolder-server-opened server)))
1159     (nnfolder-open-server server))
1160   (when (and (not nnfolder-marks-is-evil) (nnfolder-marks-changed-p group))
1161     (nnheader-message 8 "Updating marks for %s..." group)
1162     (nnfolder-open-marks group server)
1163     ;; Update info using `nnfolder-marks'.
1164     (mapcar (lambda (pred)
1165               (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1166                 (gnus-info-set-marks
1167                  info
1168                  (gnus-update-alist-soft
1169                   (cdr pred)
1170                   (cdr (assq (cdr pred) nnfolder-marks))
1171                   (gnus-info-marks info))
1172                  t)))
1173             gnus-article-mark-lists)
1174     (let ((seen (cdr (assq 'read nnfolder-marks))))
1175       (gnus-info-set-read info
1176                           (if (and (integerp (car seen))
1177                                    (null (cdr seen)))
1178                               (list (cons (car seen) (car seen)))
1179                             seen)))
1180     (nnheader-message 8 "Updating marks for %s...done" group))
1181   info)
1182
1183 (defun nnfolder-group-marks-pathname (group)
1184   "Make pathname for GROUP NOV."
1185   (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
1186     (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
1187
1188 (defun nnfolder-marks-changed-p (group)
1189   (let ((file (nnfolder-group-marks-pathname group)))
1190     (if (null (gnus-gethash file nnfolder-marks-modtime))
1191         t ;; never looked at marks file, assume it has changed
1192       (not (equal (gnus-gethash file nnfolder-marks-modtime)
1193                   (nth 5 (file-attributes file)))))))
1194
1195 (defun nnfolder-save-marks (group server)
1196   (let ((file-name-coding-system nnmail-pathname-coding-system)
1197         (file (nnfolder-group-marks-pathname group)))
1198     (condition-case err
1199         (progn
1200           (with-temp-file file
1201             (erase-buffer)
1202             (gnus-prin1 nnfolder-marks)
1203             (insert "\n"))
1204           (gnus-sethash file
1205                         (nth 5 (file-attributes file))
1206                         nnfolder-marks-modtime))
1207       (error (or (gnus-yes-or-no-p
1208                   (format "Could not write to %s (%s).  Continue? " file err))
1209                  (error "Cannot write to %s (%s)" err))))))
1210
1211 (defun nnfolder-open-marks (group server)
1212   (let ((file (nnfolder-group-marks-pathname group)))
1213     (if (file-exists-p file)
1214         (condition-case err
1215             (with-temp-buffer
1216               (gnus-sethash file (nth 5 (file-attributes file))
1217                             nnfolder-marks-modtime)
1218               (nnheader-insert-file-contents file)
1219               (setq nnfolder-marks (read (current-buffer)))
1220               (dolist (el gnus-article-unpropagated-mark-lists)
1221                 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks))))
1222           (error (or (gnus-yes-or-no-p
1223                       (format "Error reading nnfolder marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
1224                      (error "Cannot read nnfolder marks file %s (%s)" file err))))
1225       ;; User didn't have a .marks file.  Probably first time
1226       ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
1227       (let ((info (gnus-get-info
1228                    (gnus-group-prefixed-name
1229                     group
1230                     (gnus-server-to-method (format "nnfolder:%s" server))))))
1231         (nnheader-message 7 "Bootstrapping marks for %s..." group)
1232         (setq nnfolder-marks (gnus-info-marks info))
1233         (push (cons 'read (gnus-info-read info)) nnfolder-marks)
1234         (dolist (el gnus-article-unpropagated-mark-lists)
1235           (setq nnfolder-marks (gnus-remassoc el nnfolder-marks)))
1236         (nnfolder-save-marks group server)))))
1237
1238 (provide 'nnfolder)
1239
1240 ;;; nnfolder.el ends here