+ `(aset ,header 6 ,chars))
+
+(defmacro mail-header-lines (header)
+ "Return lines in HEADER."
+ `(aref ,header 7))
+
+(defmacro mail-header-set-lines (header lines)
+ "Set article lines of HEADER to LINES."
+ `(aset ,header 7 ,lines))
+
+(defmacro mail-header-xref (header)
+ "Return xref string in HEADER."
+ `(aref ,header 8))
+
+(defmacro mail-header-set-xref (header xref)
+ "Set article xref of HEADER to xref."
+ `(aset ,header 8 ,xref))
+
+(defun make-mail-header (&optional init)
+ "Create a new mail header structure initialized with INIT."
+ (make-vector 9 init))
+
+(defun make-full-mail-header (&optional number subject from date id
+ references chars lines xref)
+ "Create a new mail header structure initialized with the parameters given."
+ (vector number subject from date id references chars lines xref))
+
+;; fake message-ids: generation and detection
+
+(defvar nnheader-fake-message-id 1)
+
+(defsubst nnheader-generate-fake-message-id ()
+ (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
+
+(defsubst nnheader-fake-message-id-p (id)
+ (save-match-data ; regular message-id's are <.*>
+ (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
+
+;; Parsing headers and NOV lines.
+
+(defsubst nnheader-header-value ()
+ (buffer-substring (match-end 0) (gnus-point-at-eol)))
+
+(defun nnheader-parse-head (&optional naked)
+ (let ((case-fold-search t)
+ (cur (current-buffer))
+ (buffer-read-only nil)
+ in-reply-to lines p ref)
+ (goto-char (point-min))
+ (when naked
+ (insert "\n"))
+ ;; Search to the beginning of the next header. Error messages
+ ;; do not begin with 2 or 3.
+ (prog1
+ (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
+ ;; This implementation of this function, with nine
+ ;; search-forwards instead of the one re-search-forward and
+ ;; a case (which basically was the old function) is actually
+ ;; about twice as fast, even though it looks messier. You
+ ;; can't have everything, I guess. Speed and elegance
+ ;; don't always go hand in hand.
+ (vector
+ ;; Number.
+ (if naked
+ (progn
+ (setq p (point-min))
+ 0)
+ (prog1
+ (read cur)
+ (end-of-line)
+ (setq p (point))
+ (narrow-to-region (point)
+ (or (and (search-forward "\n.\n" nil t)
+ (- (point) 2))
+ (point)))))
+ ;; Subject.
+ (progn
+ (goto-char p)
+ (if (search-forward "\nsubject: " nil t)
+ (nnheader-header-value) "(none)"))
+ ;; From.
+ (progn
+ (goto-char p)
+ (if (search-forward "\nfrom: " nil t)
+ (nnheader-header-value) "(nobody)"))
+ ;; Date.
+ (progn
+ (goto-char p)
+ (if (search-forward "\ndate: " nil t)
+ (nnheader-header-value) ""))
+ ;; Message-ID.
+ (progn
+ (goto-char p)
+ (if (search-forward "\nmessage-id:" nil t)
+ (buffer-substring
+ (1- (or (search-forward "<" (gnus-point-at-eol) t)
+ (point)))
+ (or (search-forward ">" (gnus-point-at-eol) t) (point)))
+ ;; If there was no message-id, we just fake one to make
+ ;; subsequent routines simpler.
+ (nnheader-generate-fake-message-id)))
+ ;; References.
+ (progn
+ (goto-char p)
+ (if (search-forward "\nreferences: " nil t)
+ (nnheader-header-value)
+ ;; Get the references from the in-reply-to header if there
+ ;; were no references and the in-reply-to header looks
+ ;; promising.
+ (if (and (search-forward "\nin-reply-to: " nil t)
+ (setq in-reply-to (nnheader-header-value))
+ (string-match "<[^>]+>" in-reply-to))
+ (let (ref2)
+ (setq ref (substring in-reply-to (match-beginning 0)
+ (match-end 0)))
+ (while (string-match "<[^>]+>" in-reply-to (match-end 0))
+ (setq ref2 (substring in-reply-to (match-beginning 0)
+ (match-end 0)))
+ (when (> (length ref2) (length ref))
+ (setq ref ref2)))
+ ref)
+ nil)))
+ ;; Chars.
+ 0
+ ;; Lines.
+ (progn
+ (goto-char p)
+ (if (search-forward "\nlines: " nil t)
+ (if (numberp (setq lines (read cur)))
+ lines 0)
+ 0))
+ ;; Xref.
+ (progn
+ (goto-char p)
+ (and (search-forward "\nxref: " nil t)
+ (nnheader-header-value)))))
+ (when naked
+ (goto-char (point-min))
+ (delete-char 1)))))
+
+(defmacro nnheader-nov-skip-field ()
+ '(search-forward "\t" eol 'move))
+
+(defmacro nnheader-nov-field ()
+ '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
+
+(defmacro nnheader-nov-read-integer ()
+ '(prog1
+ (if (= (following-char) ?\t)
+ 0
+ (let ((num (ignore-errors (read (current-buffer)))))
+ (if (numberp num) num 0)))
+ (or (eobp) (forward-char 1))))
+
+;; (defvar nnheader-none-counter 0)
+
+(defun nnheader-parse-nov ()
+ (let ((eol (gnus-point-at-eol)))
+ (vector
+ (nnheader-nov-read-integer) ; number
+ (nnheader-nov-field) ; subject
+ (nnheader-nov-field) ; from
+ (nnheader-nov-field) ; date
+ (or (nnheader-nov-field)
+ (nnheader-generate-fake-message-id)) ; id
+ (nnheader-nov-field) ; refs
+ (nnheader-nov-read-integer) ; chars
+ (nnheader-nov-read-integer) ; lines
+ (if (= (following-char) ?\n)
+ nil
+ (nnheader-nov-field)) ; misc
+ )))
+
+(defun nnheader-insert-nov (header)
+ (princ (mail-header-number header) (current-buffer))
+ (insert
+ "\t"
+ (or (mail-header-subject header) "(none)") "\t"
+ (or (mail-header-from header) "(nobody)") "\t"
+ (or (mail-header-date header) "") "\t"
+ (or (mail-header-id header)
+ (nnmail-message-id))
+ "\t"
+ (or (mail-header-references header) "") "\t")
+ (princ (or (mail-header-chars header) 0) (current-buffer))
+ (insert "\t")
+ (princ (or (mail-header-lines header) 0) (current-buffer))
+ (insert "\t")
+ (when (mail-header-xref header)
+ (insert "Xref: " (mail-header-xref header) "\t"))
+ (insert "\n"))
+
+(defun nnheader-insert-article-line (article)
+ (goto-char (point-min))
+ (insert "220 ")
+ (princ article (current-buffer))
+ (insert " Article retrieved.\n")
+ (search-forward "\n\n" nil 'move)
+ (delete-region (point) (point-max))
+ (forward-char -1)
+ (insert "."))
+
+(defun nnheader-nov-delete-outside-range (beg end)
+ "Delete all NOV lines that lie outside the BEG to END range."
+ ;; First we find the first wanted line.
+ (nnheader-find-nov-line beg)
+ (delete-region (point-min) (point))
+ ;; Then we find the last wanted line.
+ (when (nnheader-find-nov-line end)
+ (forward-line 1))
+ (delete-region (point) (point-max)))
+
+(defun nnheader-find-nov-line (article)
+ "Put point at the NOV line that start with ARTICLE.
+If ARTICLE doesn't exist, put point where that line
+would have been. The function will return non-nil if
+the line could be found."
+ ;; This function basically does a binary search.
+ (let ((max (point-max))
+ (min (goto-char (point-min)))
+ (cur (current-buffer))
+ (prev (point-min))
+ num found)
+ (while (not found)
+ (goto-char (/ (+ max min) 2))
+ (beginning-of-line)
+ (if (or (= (point) prev)
+ (eobp))
+ (setq found t)
+ (setq prev (point))
+ (while (and (not (numberp (setq num (read cur))))
+ (not (eobp)))
+ (gnus-delete-line))
+ (cond ((> num article)
+ (setq max (point)))
+ ((< num article)
+ (setq min (point)))
+ (t
+ (setq found 'yes)))))
+ ;; We may be at the first line.
+ (when (and (not num)
+ (not (eobp)))
+ (setq num (read cur)))
+ ;; Now we may have found the article we're looking for, or we
+ ;; may be somewhere near it.
+ (when (and (not (eq found 'yes))
+ (not (eq num article)))
+ (setq found (point))
+ (while (and (< (point) max)
+ (or (not (numberp num))
+ (< num article)))
+ (forward-line 1)
+ (setq found (point))
+ (or (eobp)
+ (= (setq num (read cur)) article)))
+ (unless (eq num article)
+ (goto-char found)))
+ (beginning-of-line)
+ (eq num article)))