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