*** empty log message ***
[gnus] / lisp / nnheader.el
1 ;;; nnheader.el --- header access macros for Gnus and its backends
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; These macros may look very much like the ones in GNUS 4.1. They
27 ;; are, in a way, but you should note that the indices they use have
28 ;; been changed from the internal GNUS format to the NOV format. Makes
29 ;; it possible to read headers from XOVER much faster.
30 ;;
31 ;; The format of a header is now:
32 ;; [number subject from date id references chars lines xref]
33 ;;
34 ;; (That last entry is defined as "misc" in the NOV format, but Gnus
35 ;; uses it for xrefs.)
36
37 ;;; Code:
38
39 (require 'mail-utils)
40 (require 'sendmail)
41 (eval-when-compile (require 'cl))
42
43 (defvar nnheader-max-head-length 4096
44   "*Max length of the head of articles.")
45
46 (defalias 'nntp-header-number 'mail-header-number)
47 (defmacro mail-header-number (header)
48   "Return article number in HEADER."
49   (` (aref (, header) 0)))
50
51 (defalias 'nntp-set-header-number 'mail-header-set-number)
52 (defmacro mail-header-set-number (header number)
53   "Set article number of HEADER to NUMBER."
54   (` (aset (, header) 0 (, number))))
55
56 (defalias 'nntp-header-subject 'mail-header-subject)
57 (defmacro mail-header-subject (header)
58   "Return subject string in HEADER."
59   (` (aref (, header) 1)))
60
61 (defalias 'nntp-set-header-subject 'mail-header-set-subject)
62 (defmacro mail-header-set-subject (header subject)
63   "Set article subject of HEADER to SUBJECT."
64   (` (aset (, header) 1 (, subject))))
65
66 (defalias 'nntp-header-from 'mail-header-from)
67 (defmacro mail-header-from (header)
68   "Return author string in HEADER."
69   (` (aref (, header) 2)))
70
71 (defalias 'nntp-set-header-from 'mail-header-set-from)
72 (defmacro mail-header-set-from (header from)
73   "Set article author of HEADER to FROM."
74   (` (aset (, header) 2 (, from))))
75
76 (defalias 'nntp-header-date 'mail-header-date)
77 (defmacro mail-header-date (header)
78   "Return date in HEADER."
79   (` (aref (, header) 3)))
80
81 (defalias 'nntp-set-header-date 'mail-header-set-date)
82 (defmacro mail-header-set-date (header date)
83   "Set article date of HEADER to DATE."
84   (` (aset (, header) 3 (, date))))
85
86 (defalias 'nntp-header-id 'mail-header-id)
87 (defmacro mail-header-id (header)
88   "Return Id in HEADER."
89   (` (aref (, header) 4)))
90
91 (defalias 'nntp-set-header-id 'mail-header-set-id)
92 (defmacro mail-header-set-id (header id)
93   "Set article Id of HEADER to ID."
94   (` (aset (, header) 4 (, id))))
95
96 (defalias 'nntp-header-references 'mail-header-references)
97 (defmacro mail-header-references (header)
98   "Return references in HEADER."
99   (` (aref (, header) 5)))
100
101 (defalias 'nntp-set-header-references 'mail-header-set-references)
102 (defmacro mail-header-set-references (header ref)
103   "Set article references of HEADER to REF."
104   (` (aset (, header) 5 (, ref))))
105
106 (defalias 'nntp-header-chars 'mail-header-chars)
107 (defmacro mail-header-chars (header)
108   "Return number of chars of article in HEADER."
109   (` (aref (, header) 6)))
110
111 (defalias 'nntp-set-header-chars 'mail-header-set-chars)
112 (defmacro mail-header-set-chars (header chars)
113   "Set number of chars in article of HEADER to CHARS."
114   (` (aset (, header) 6 (, chars))))
115
116 (defalias 'nntp-header-lines 'mail-header-lines)
117 (defmacro mail-header-lines (header)
118   "Return lines in HEADER."
119   (` (aref (, header) 7)))
120
121 (defalias 'nntp-set-header-lines 'mail-header-set-lines)
122 (defmacro mail-header-set-lines (header lines)
123   "Set article lines of HEADER to LINES."
124   (` (aset (, header) 7 (, lines))))
125
126 (defalias 'nntp-header-xref 'mail-header-xref)
127 (defmacro mail-header-xref (header)
128   "Return xref string in HEADER."
129   (` (aref (, header) 8)))
130
131 (defalias 'nntp-set-header-xref 'mail-header-set-xref)
132 (defmacro mail-header-set-xref (header xref)
133   "Set article xref of HEADER to xref."
134   (` (aset (, header) 8 (, xref))))
135
136
137 ;; Various cruft the backends and Gnus need to communicate.
138
139 (defvar nntp-server-buffer nil)
140 (defvar gnus-verbose-backends t
141   "*If non-nil, Gnus backends will generate lots of comments.")
142 (defvar gnus-nov-is-evil nil
143   "If non-nil, Gnus backends will never output headers in the NOV format.")
144 (defvar news-reply-yank-from nil)
145 (defvar news-reply-yank-message-id nil)
146
147 ;; All backends use this function, so I moved it to this file.
148
149 (defun nnheader-init-server-buffer ()
150   (save-excursion
151     (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
152     (set-buffer nntp-server-buffer)
153     (buffer-disable-undo (current-buffer))
154     (erase-buffer)
155     (kill-all-local-variables)
156     (setq case-fold-search t)           ;Should ignore case.
157     t))
158
159 (defun nnheader-set-init-variables (server defs)
160   (let ((s server)
161         val)
162     ;; First we set the server variables in the sequence required.  We
163     ;; use the definitions from the `defs' list where that is
164     ;; possible. 
165     (while s
166       (set (car (car s)) 
167            (if (setq val (assq (car (car s)) defs))
168                (nth 1 val)
169              (nth 1 (car s))))
170       (setq s (cdr s)))
171     ;; The we go through the defs list and set any variables that were
172     ;; not set in the first sweep.
173     (while defs
174       (if (not (assq (car (car defs)) server))
175           (set (car (car defs)) 
176                (if (and (symbolp (nth 1 (car defs)))
177                         (not (boundp (nth 1 (car defs)))))
178                    (nth 1 (car defs))
179                  (eval (nth 1 (car defs))))))
180       (setq defs (cdr defs)))))
181
182 (defun nnheader-save-variables (server)
183   (let (out)
184     (while server
185       (setq out (cons (list (car (car server)) 
186                             (symbol-value (car (car server))))
187                       out))
188       (setq server (cdr server)))
189     (nreverse out)))
190
191 (defun nnheader-restore-variables (state)
192   (while state
193     (set (car (car state)) (nth 1 (car state)))
194     (setq state (cdr state))))
195
196 ;; Read the head of an article.
197 (defun nnheader-insert-head (file)
198   (if (eq nnheader-max-head-length t)
199       ;; Just read the entire file.
200       (nnheader-insert-file-contents-literally file)
201     (let ((beg 0)
202           (chop 1024))
203       ;; Read 1K blocks until we find a separator.
204       (while (and (eq chop (nth 1 (nnheader-insert-file-contents-literally
205                                    file nil beg (setq beg (+ chop beg)))))
206                   (prog1 (not (search-backward "\n\n" nil t)) 
207                     (goto-char (point-max)))
208                   (or (null nnheader-max-head-length)
209                       (< beg nnheader-max-head-length)))))))
210
211 (defun nnheader-article-p ()
212   (goto-char (point-min))
213   (if (not (search-forward "\n\n" nil t))
214       nil
215     (narrow-to-region (point-min) (1- (point)))
216     (goto-char (point-min))
217     (while (looking-at "[A-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
218       (goto-char (match-end 0)))
219     (prog1
220         (eobp)
221       (widen))))    
222
223 ;; Written by Erik Naggum <erik@naggum.no>.
224 (defun nnheader-insert-file-contents-literally (filename &optional visit beg end replace)
225   "Like `insert-file-contents', q.v., but only reads in the file.
226 A buffer may be modified in several ways after reading into the buffer due
227 to advanced Emacs features, such as file-name-handlers, format decoding,
228 find-file-hooks, etc.
229   This function ensures that none of these modifications will take place."
230   (let (                                ; (file-name-handler-alist nil)
231         (format-alist nil)
232         (after-insert-file-functions nil)
233         (find-buffer-file-type-function 
234          (if (fboundp 'find-buffer-file-type)
235              (symbol-function 'find-buffer-file-type)
236            nil)))
237     (unwind-protect
238         (progn
239           (fset 'find-buffer-file-type (lambda (filename) t))
240           (insert-file-contents filename visit beg end replace))
241       (if find-buffer-file-type-function
242           (fset 'find-buffer-file-type find-buffer-file-type-function)
243         (fmakunbound 'find-buffer-file-type)))))
244
245 (defun nnheader-find-file-noselect (filename &optional nowarn rawfile)
246   "Read file FILENAME into a buffer and return the buffer.
247 If a buffer exists visiting FILENAME, return that one, but
248 verify that the file has not changed since visited or saved.
249 The buffer is not selected, just returned to the caller."
250   (setq filename
251         (abbreviate-file-name
252          (expand-file-name filename)))
253   (if (file-directory-p filename)
254       (if find-file-run-dired
255           (dired-noselect filename)
256         (error "%s is a directory." filename))
257     (let* ((buf (get-file-buffer filename))
258            (truename (abbreviate-file-name (file-truename filename)))
259            (number (nthcdr 10 (file-attributes truename)))
260            ;; Find any buffer for a file which has same truename.
261            (other (and (not buf) 
262                        (if (fboundp 'find-buffer-visiting)
263                            (find-buffer-visiting filename)
264                          (get-file-buffer filename))))
265            error)
266       ;; Let user know if there is a buffer with the same truename.
267       (if other
268           (progn
269             (or nowarn
270                 (string-equal filename (buffer-file-name other))
271                 (message "%s and %s are the same file"
272                          filename (buffer-file-name other)))
273             ;; Optionally also find that buffer.
274             (if (or (and (boundp 'find-file-existing-other-name)
275                          find-file-existing-other-name)
276                     find-file-visit-truename)
277                 (setq buf other))))
278       (if buf
279           (or nowarn
280               (verify-visited-file-modtime buf)
281               (cond ((not (file-exists-p filename))
282                      (error "File %s no longer exists!" filename))
283                     ((yes-or-no-p
284                       (if (string= (file-name-nondirectory filename)
285                                    (buffer-name buf))
286                           (format
287                            (if (buffer-modified-p buf)
288                                "File %s changed on disk.  Discard your edits? "
289                              "File %s changed on disk.  Reread from disk? ")
290                            (file-name-nondirectory filename))
291                         (format
292                          (if (buffer-modified-p buf)
293                              "File %s changed on disk.  Discard your edits in %s? "
294                            "File %s changed on disk.  Reread from disk into %s? ")
295                          (file-name-nondirectory filename)
296                          (buffer-name buf))))
297                      (save-excursion
298                        (set-buffer buf)
299                        (revert-buffer t t)))))
300         (save-excursion
301 ;;; The truename stuff makes this obsolete.
302 ;;;       (let* ((link-name (car (file-attributes filename)))
303 ;;;              (linked-buf (and (stringp link-name)
304 ;;;                               (get-file-buffer link-name))))
305 ;;;         (if (bufferp linked-buf)
306 ;;;             (message "Symbolic link to file in buffer %s"
307 ;;;                      (buffer-name linked-buf))))
308           (setq buf (create-file-buffer filename))
309           ;;      (set-buffer-major-mode buf)
310           (set-buffer buf)
311           (erase-buffer)
312           (if rawfile
313               (condition-case ()
314                   (nnheader-insert-file-contents-literally filename t)
315                 (file-error
316                  ;; Unconditionally set error
317                  (setq error t)))
318             (condition-case ()
319                 (insert-file-contents filename t)
320               (file-error
321                ;; Run find-file-not-found-hooks until one returns non-nil.
322                (or t                    ; (run-hook-with-args-until-success 'find-file-not-found-hooks)
323                    ;; If they fail too, set error.
324                    (setq error t)))))
325           ;; Find the file's truename, and maybe use that as visited name.
326           (setq buffer-file-truename truename)
327           (setq buffer-file-number number)
328           ;; On VMS, we may want to remember which directory in a search list
329           ;; the file was found in.
330           (and (eq system-type 'vax-vms)
331                (let (logical)
332                  (if (string-match ":" (file-name-directory filename))
333                      (setq logical (substring (file-name-directory filename)
334                                               0 (match-beginning 0))))
335                  (not (member logical find-file-not-true-dirname-list)))
336                (setq buffer-file-name buffer-file-truename))
337           (if find-file-visit-truename
338               (setq buffer-file-name
339                     (setq filename
340                           (expand-file-name buffer-file-truename))))
341           ;; Set buffer's default directory to that of the file.
342           (setq default-directory (file-name-directory filename))
343           ;; Turn off backup files for certain file names.  Since
344           ;; this is a permanent local, the major mode won't eliminate it.
345           (and (not (funcall backup-enable-predicate buffer-file-name))
346                (progn
347                  (make-local-variable 'backup-inhibited)
348                  (setq backup-inhibited t)))
349           (if rawfile
350               nil
351             (after-find-file error (not nowarn)))))
352       buf)))
353
354 (defun nnheader-insert-references (references message-id)
355    (if (and (not references) (not message-id)) 
356        () ; This is illegal, but not all articles have Message-IDs.
357      ;; Fold long references line to follow RFC1036.
358      (mail-position-on-field "References")
359      (let ((begin (save-excursion (beginning-of-line) (point)))
360            (fill-column 78)
361            (fill-prefix "\t"))
362        (if references (insert references))
363        (if (and references message-id) (insert " "))
364        (if message-id (insert message-id))
365        ;; The region must end with a newline to fill the region
366        ;; without inserting extra newline.
367        (fill-region-as-paragraph begin (1+ (point))))))
368
369 (defun nnheader-remove-header (header &optional is-regexp first)
370   "Remove HEADER.
371 If FIRST, only remove the first instance if the header.
372 Return the number of headers removed."
373   (goto-char (point-min))
374   (let ((regexp (if is-regexp header (concat "^" header ":")))
375         (number 0)
376         (case-fold-search t)
377         last)
378     (while (and (re-search-forward regexp nil t)
379                 (not last))
380       (incf number)
381       (when first
382         (setq last t))
383       (delete-region
384        (match-beginning 0) 
385        ;; There might be a continuation header, so we have to search
386        ;; until we find a new non-continuation line.
387        (if (re-search-forward "^[^ \t]" nil t)
388            (match-beginning 0)
389          (point-max))))
390     number))
391
392 (defun nnheader-set-temp-buffer (name)
393   (set-buffer (get-buffer-create name))
394   (buffer-disable-undo (current-buffer))
395   (erase-buffer))
396
397 (defvar jka-compr-compression-info-list)
398 (defvar nnheader-numerical-files
399   (if (boundp 'jka-compr-compression-info-list)
400       (concat "\\([0-9]+\\)\\(" 
401               (mapconcat (lambda (i) (aref i 0))
402                          jka-compr-compression-info-list "\\|")
403               "\\)?")
404     "[0-9]+$")
405   "Regexp that match numerical files.")
406
407 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
408   "Regexp that matches numerical file names.")
409
410 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
411   "Regexp that matches numerical full file paths.")
412
413 (defun nnheader-file-to-number (file)
414   (if (not (boundp 'jka-compr-compression-info-list))
415       (string-to-int file)
416     (string-match nnheader-numerical-short-files file)
417     (string-to-int (match-string 0 file))))
418
419 (defun nnheader-directory-articles (dir)
420   (mapcar 'nnheader-file-to-number
421           (directory-files dir nil nnheader-numerical-short-files t)))
422
423 (defun nnheader-fold-continuation-lines ()
424   "Fold continuation lines in the current buffer."
425   (goto-char (point-min))
426   (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
427     (replace-match " " t t)))
428
429 (provide 'nnheader)
430
431 ;;; nnheader.el ends here