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