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.
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
31 ;; The format of a header is now:
32 ;; [number subject from date id references chars lines xref]
34 ;; (That last entry is defined as "misc" in the NOV format, but Gnus
35 ;; uses it for xrefs.)
40 (eval-when-compile (require 'cl))
42 (defvar nnheader-max-head-length 4096
43 "*Max length of the head of articles.")
45 (defalias 'nntp-header-number 'mail-header-number)
46 (defmacro mail-header-number (header)
47 "Return article number in HEADER."
48 (` (aref (, header) 0)))
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))))
55 (defalias 'nntp-header-subject 'mail-header-subject)
56 (defmacro mail-header-subject (header)
57 "Return subject string in HEADER."
58 (` (aref (, header) 1)))
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))))
65 (defalias 'nntp-header-from 'mail-header-from)
66 (defmacro mail-header-from (header)
67 "Return author string in HEADER."
68 (` (aref (, header) 2)))
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))))
75 (defalias 'nntp-header-date 'mail-header-date)
76 (defmacro mail-header-date (header)
77 "Return date in HEADER."
78 (` (aref (, header) 3)))
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))))
85 (defalias 'nntp-header-id 'mail-header-id)
86 (defmacro mail-header-id (header)
87 "Return Id in HEADER."
88 (` (aref (, header) 4)))
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))))
95 (defalias 'nntp-header-references 'mail-header-references)
96 (defmacro mail-header-references (header)
97 "Return references in HEADER."
98 (` (aref (, header) 5)))
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))))
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)))
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))))
115 (defalias 'nntp-header-lines 'mail-header-lines)
116 (defmacro mail-header-lines (header)
117 "Return lines in HEADER."
118 (` (aref (, header) 7)))
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))))
125 (defalias 'nntp-header-xref 'mail-header-xref)
126 (defmacro mail-header-xref (header)
127 "Return xref string in HEADER."
128 (` (aref (, header) 8)))
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))))
136 ;; Various cruft the backends and Gnus need to communicate.
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)
146 ;; All backends use this function, so I moved it to this file.
148 (defun nnheader-init-server-buffer ()
150 (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
151 (set-buffer nntp-server-buffer)
152 (buffer-disable-undo (current-buffer))
154 (kill-all-local-variables)
155 (setq case-fold-search t) ;Should ignore case.
158 (defun nnheader-set-init-variables (server defs)
161 ;; First we set the server variables in the sequence required. We
162 ;; use the definitions from the `defs' list where that is
166 (if (setq val (assq (car (car s)) defs))
170 ;; The we go through the defs list and set any variables that were
171 ;; not set in the first sweep.
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)))))
178 (eval (nth 1 (car defs))))))
179 (setq defs (cdr defs)))))
181 (defun nnheader-save-variables (server)
184 (setq out (cons (list (car (car server))
185 (symbol-value (car (car server))))
187 (setq server (cdr server)))
190 (defun nnheader-restore-variables (state)
192 (set (car (car state)) (nth 1 (car state)))
193 (setq state (cdr state))))
195 ;; Read the head of an article by brute force
196 (defvar nnheader-gnus-headers-program "/usr/local/bin/headers")
198 ;; Read the head of an article.
199 (defun nnheader-insert-head (file)
200 (if (eq nnheader-max-head-length t)
201 ;; Just read the entire file.
202 (nnheader-insert-file-contents-literally file)
203 (call-process nnheader-gnus-headers-program file t)
204 (goto-char (point-max))))
208 ;; Read 1K blocks until we find a separator.
209 ; (while (and (eq chop (nth 1 (nnheader-insert-file-contents-literally
210 ; file nil beg (setq beg (+ chop beg)))))
211 ; (prog1 (not (search-backward "\n\n" nil t))
212 ; (goto-char (point-max)))
213 ; (or (null nnheader-max-head-length)
214 ; (< beg nnheader-max-head-length)))))))
216 (defun nnheader-article-p ()
217 (goto-char (point-min))
218 (if (not (search-forward "\n\n" nil t))
220 (narrow-to-region (point-min) (1- (point)))
221 (goto-char (point-min))
222 (while (looking-at "[A-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
223 (goto-char (match-end 0)))
228 ;; Written by Erik Naggum <erik@naggum.no>.
229 (defun nnheader-insert-file-contents-literally (filename &optional visit beg end replace)
230 "Like `insert-file-contents', q.v., but only reads in the file.
231 A buffer may be modified in several ways after reading into the buffer due
232 to advanced Emacs features, such as file-name-handlers, format decoding,
233 find-file-hooks, etc.
234 This function ensures that none of these modifications will take place."
235 (let ( ; (file-name-handler-alist nil)
237 (after-insert-file-functions nil)
238 (find-buffer-file-type-function
239 (if (fboundp 'find-buffer-file-type)
240 (symbol-function 'find-buffer-file-type)
244 (fset 'find-buffer-file-type (lambda (filename) t))
245 (insert-file-contents filename visit beg end replace))
246 (if find-buffer-file-type-function
247 (fset 'find-buffer-file-type find-buffer-file-type-function)
248 (fmakunbound 'find-buffer-file-type)))))
250 (defun nnheader-find-file-noselect (filename &optional nowarn rawfile)
251 "Read file FILENAME into a buffer and return the buffer.
252 If a buffer exists visiting FILENAME, return that one, but
253 verify that the file has not changed since visited or saved.
254 The buffer is not selected, just returned to the caller."
256 (abbreviate-file-name
257 (expand-file-name filename)))
258 (if (file-directory-p filename)
259 (if find-file-run-dired
260 (dired-noselect filename)
261 (error "%s is a directory." filename))
262 (let* ((buf (get-file-buffer filename))
263 (truename (abbreviate-file-name (file-truename filename)))
264 (number (nthcdr 10 (file-attributes truename)))
265 ;; Find any buffer for a file which has same truename.
266 (other (and (not buf)
267 (if (fboundp 'find-buffer-visiting)
268 (find-buffer-visiting filename)
269 (get-file-buffer filename))))
271 ;; Let user know if there is a buffer with the same truename.
275 (string-equal filename (buffer-file-name other))
276 (message "%s and %s are the same file"
277 filename (buffer-file-name other)))
278 ;; Optionally also find that buffer.
279 (if (or (and (boundp 'find-file-existing-other-name)
280 find-file-existing-other-name)
281 find-file-visit-truename)
285 (verify-visited-file-modtime buf)
286 (cond ((not (file-exists-p filename))
287 (error "File %s no longer exists!" filename))
289 (if (string= (file-name-nondirectory filename)
292 (if (buffer-modified-p buf)
293 "File %s changed on disk. Discard your edits? "
294 "File %s changed on disk. Reread from disk? ")
295 (file-name-nondirectory filename))
297 (if (buffer-modified-p buf)
298 "File %s changed on disk. Discard your edits in %s? "
299 "File %s changed on disk. Reread from disk into %s? ")
300 (file-name-nondirectory filename)
304 (revert-buffer t t)))))
306 ;;; The truename stuff makes this obsolete.
307 ;;; (let* ((link-name (car (file-attributes filename)))
308 ;;; (linked-buf (and (stringp link-name)
309 ;;; (get-file-buffer link-name))))
310 ;;; (if (bufferp linked-buf)
311 ;;; (message "Symbolic link to file in buffer %s"
312 ;;; (buffer-name linked-buf))))
313 (setq buf (create-file-buffer filename))
314 ;; (set-buffer-major-mode buf)
319 (nnheader-insert-file-contents-literally filename t)
321 ;; Unconditionally set error
324 (insert-file-contents filename t)
326 ;; Run find-file-not-found-hooks until one returns non-nil.
327 (or t ; (run-hook-with-args-until-success 'find-file-not-found-hooks)
328 ;; If they fail too, set error.
330 ;; Find the file's truename, and maybe use that as visited name.
331 (setq buffer-file-truename truename)
332 (setq buffer-file-number number)
333 ;; On VMS, we may want to remember which directory in a search list
334 ;; the file was found in.
335 (and (eq system-type 'vax-vms)
337 (if (string-match ":" (file-name-directory filename))
338 (setq logical (substring (file-name-directory filename)
339 0 (match-beginning 0))))
340 (not (member logical find-file-not-true-dirname-list)))
341 (setq buffer-file-name buffer-file-truename))
342 (if find-file-visit-truename
343 (setq buffer-file-name
345 (expand-file-name buffer-file-truename))))
346 ;; Set buffer's default directory to that of the file.
347 (setq default-directory (file-name-directory filename))
348 ;; Turn off backup files for certain file names. Since
349 ;; this is a permanent local, the major mode won't eliminate it.
350 (and (not (funcall backup-enable-predicate buffer-file-name))
352 (make-local-variable 'backup-inhibited)
353 (setq backup-inhibited t)))
356 (after-find-file error (not nowarn)))))
359 (defun nnheader-insert-references (references message-id)
360 (if (and (not references) (not message-id))
361 () ; This is illegal, but not all articles have Message-IDs.
362 ;; Fold long references line to follow RFC1036.
363 (mail-position-on-field "References")
364 (let ((begin (save-excursion (beginning-of-line) (point)))
367 (if references (insert references))
368 (if (and references message-id) (insert " "))
369 (if message-id (insert message-id))
370 ;; The region must end with a newline to fill the region
371 ;; without inserting extra newline.
372 (fill-region-as-paragraph begin (1+ (point))))))
374 (defun nnheader-remove-header (header &optional is-regexp first)
376 If FIRST, only remove the first instance if the header.
377 Return the number of headers removed."
378 (goto-char (point-min))
379 (let ((regexp (if is-regexp header (concat "^" header ":")))
383 (while (and (re-search-forward regexp nil t)
390 ;; There might be a continuation header, so we have to search
391 ;; until we find a new non-continuation line.
392 (if (re-search-forward "^[^ \t]" nil t)
397 (defun nnheader-set-temp-buffer (name)
398 (set-buffer (get-buffer-create name))
399 (buffer-disable-undo (current-buffer))
404 ;;; nnheader.el ends here