-(defun nnml-generate-nov-databases (dir)
- "Generate nov databases in all nnml mail newsgroups."
- (interactive
- (progn
- (setq nnml-group-alist nil)
- (list nnml-directory)))
- (nnml-open-server (or nnml-current-server ""))
- (let ((dirs (directory-files dir t nil t)))
- (while dirs
- (if (and (not (string-match "/\\.\\.$" (car dirs)))
- (not (string-match "/\\.$" (car dirs)))
- (file-directory-p (car dirs)))
- (nnml-generate-nov-databases (car dirs)))
- (setq dirs (cdr dirs))))
- (let ((files (sort
- (mapcar
- (function
- (lambda (name)
- (string-to-int name)))
- (directory-files dir nil "^[0-9]+$" t))
- (function <)))
- (nov (concat dir "/" nnml-nov-file-name))
- (nov-buffer (get-buffer-create "*nov*"))
- nov-line chars)
- (if files
- (setq nnml-group-alist
- (cons (list (nnmail-replace-chars-in-string
- (substring (expand-file-name dir)
- (length (expand-file-name
- nnml-directory)))
- ?/ ?.)
- (cons (car files)
- (let ((f files))
- (while (cdr f) (setq f (cdr f)))
- (car f))))
- nnml-group-alist)))
- (if files
- (save-excursion
- (set-buffer nntp-server-buffer)
- (if (file-exists-p nov)
- (delete-file nov))
- (save-excursion
- (set-buffer nov-buffer)
- (buffer-disable-undo (current-buffer))
- (erase-buffer))
- (while files
- (erase-buffer)
- (insert-file-contents (concat dir "/" (int-to-string (car files))))
+(defun nnml-generate-nov-databases ()
+ "Generate NOV databases in all nnml directories."
+ (interactive)
+ ;; Read the active file to make sure we don't re-use articles
+ ;; numbers in empty groups.
+ (nnmail-activate 'nnml)
+ (nnml-open-server (or (nnoo-current-server 'nnml) ""))
+ (setq nnml-directory (expand-file-name nnml-directory))
+ ;; Recurse down the directories.
+ (nnml-generate-nov-databases-1 nnml-directory nil t)
+ ;; Save the active file.
+ (nnmail-save-active nnml-group-alist nnml-active-file))
+
+(defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
+ "Regenerate the NOV database in DIR."
+ (interactive "DRegenerate NOV in: ")
+ (setq dir (file-name-as-directory dir))
+ ;; Only scan this sub-tree if we haven't been here yet.
+ (unless (member (file-truename dir) seen)
+ (push (file-truename dir) seen)
+ ;; We descend recursively
+ (let ((dirs (directory-files dir t nil t))
+ dir)
+ (while (setq dir (pop dirs))
+ (when (and (not (member (file-name-nondirectory dir) '("." "..")))
+ (file-directory-p dir))
+ (nnml-generate-nov-databases-1 dir seen))))
+ ;; Do this directory.
+ (let ((files (sort (nnheader-article-to-file-alist dir)
+ 'car-less-than-car)))
+ (if (not files)
+ (let* ((group (nnheader-file-to-group
+ (directory-file-name dir) nnml-directory))
+ (info (cadr (assoc group nnml-group-alist))))
+ (when info
+ (setcar info (1+ (cdr info)))))
+ (funcall nnml-generate-active-function dir)
+ ;; Generate the nov file.
+ (nnml-generate-nov-file dir files)
+ (unless no-active
+ (nnmail-save-active nnml-group-alist nnml-active-file))))))
+
+(defvar files)
+(defun nnml-generate-active-info (dir)
+ ;; Update the active info for this group.
+ (let ((group (nnheader-file-to-group
+ (directory-file-name dir) nnml-directory)))
+ (setq nnml-group-alist
+ (delq (assoc group nnml-group-alist) nnml-group-alist))
+ (push (list group
+ (cons (caar files)
+ (let ((f files))
+ (while (cdr f) (setq f (cdr f)))
+ (caar f))))
+ nnml-group-alist)))
+
+(defun nnml-generate-nov-file (dir files)
+ (let* ((dir (file-name-as-directory dir))
+ (nov (concat dir nnml-nov-file-name))
+ (nov-buffer (get-buffer-create " *nov*"))
+ chars file headers)
+ (save-excursion
+ ;; Init the nov buffer.
+ (set-buffer nov-buffer)
+ (buffer-disable-undo (current-buffer))
+ (erase-buffer)
+ (set-buffer nntp-server-buffer)
+ ;; Delete the old NOV file.
+ (when (file-exists-p nov)
+ (funcall nnmail-delete-file-function nov))
+ (while files
+ (unless (file-directory-p (setq file (concat dir (cdar files))))
+ (erase-buffer)
+ (nnheader-insert-file-contents file)
+ (narrow-to-region
+ (goto-char (point-min))
+ (progn
+ (search-forward "\n\n" nil t)
+ (setq chars (- (point-max) (point)))
+ (max 1 (1- (point)))))
+ (unless (zerop (buffer-size))