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.)
41 (eval-when-compile (require 'cl))
43 (defvar nnheader-max-head-length 4096
44 "*Max length of the head of articles.")
46 (defalias 'nntp-header-number 'mail-header-number)
47 (defmacro mail-header-number (header)
48 "Return article number in HEADER."
49 (` (aref (, header) 0)))
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))))
56 (defalias 'nntp-header-subject 'mail-header-subject)
57 (defmacro mail-header-subject (header)
58 "Return subject string in HEADER."
59 (` (aref (, header) 1)))
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))))
66 (defalias 'nntp-header-from 'mail-header-from)
67 (defmacro mail-header-from (header)
68 "Return author string in HEADER."
69 (` (aref (, header) 2)))
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))))
76 (defalias 'nntp-header-date 'mail-header-date)
77 (defmacro mail-header-date (header)
78 "Return date in HEADER."
79 (` (aref (, header) 3)))
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))))
86 (defalias 'nntp-header-id 'mail-header-id)
87 (defmacro mail-header-id (header)
88 "Return Id in HEADER."
89 (` (aref (, header) 4)))
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))))
96 (defalias 'nntp-header-references 'mail-header-references)
97 (defmacro mail-header-references (header)
98 "Return references in HEADER."
99 (` (aref (, header) 5)))
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))))
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)))
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))))
116 (defalias 'nntp-header-lines 'mail-header-lines)
117 (defmacro mail-header-lines (header)
118 "Return lines in HEADER."
119 (` (aref (, header) 7)))
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))))
126 (defalias 'nntp-header-xref 'mail-header-xref)
127 (defmacro mail-header-xref (header)
128 "Return xref string in HEADER."
129 (` (aref (, header) 8)))
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))))
137 ;; Various cruft the backends and Gnus need to communicate.
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)
147 ;; All backends use this function, so I moved it to this file.
149 (defun nnheader-init-server-buffer ()
151 (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
152 (set-buffer nntp-server-buffer)
153 (buffer-disable-undo (current-buffer))
155 (kill-all-local-variables)
156 (setq case-fold-search t) ;Should ignore case.
159 (defun nnheader-set-init-variables (server defs)
162 ;; First we set the server variables in the sequence required. We
163 ;; use the definitions from the `defs' list where that is
167 (if (setq val (assq (car (car s)) defs))
171 ;; The we go through the defs list and set any variables that were
172 ;; not set in the first sweep.
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)))))
179 (eval (nth 1 (car defs))))))
180 (setq defs (cdr defs)))))
182 (defun nnheader-save-variables (server)
185 (setq out (cons (list (car (car server))
186 (symbol-value (car (car server))))
188 (setq server (cdr server)))
191 (defun nnheader-restore-variables (state)
193 (set (car (car state)) (nth 1 (car state)))
194 (setq state (cdr state))))
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)
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)))))))
211 (defun nnheader-article-p ()
212 (goto-char (point-min))
213 (if (not (search-forward "\n\n" nil t))
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)))
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)
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)
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)))))
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."
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))))
266 ;; Let user know if there is a buffer with the same truename.
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)
280 (verify-visited-file-modtime buf)
281 (cond ((not (file-exists-p filename))
282 (error "File %s no longer exists!" filename))
284 (if (string= (file-name-nondirectory filename)
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))
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)
299 (revert-buffer t t)))))
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)
314 (nnheader-insert-file-contents-literally filename t)
316 ;; Unconditionally set error
319 (insert-file-contents filename t)
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.
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)
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
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))
347 (make-local-variable 'backup-inhibited)
348 (setq backup-inhibited t)))
351 (after-find-file error (not nowarn)))))
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)))
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))))))
369 (defun nnheader-remove-header (header &optional is-regexp first)
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 ":")))
378 (while (and (re-search-forward regexp nil t)
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)
392 (defun nnheader-set-temp-buffer (name)
393 (set-buffer (get-buffer-create name))
394 (buffer-disable-undo (current-buffer))
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 "\\|")
405 "Regexp that match numerical files.")
407 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
408 "Regexp that matches numerical file names.")
410 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
411 "Regexp that matches numerical full file paths.")
413 (defun nnheader-file-to-number (file)
414 (if (not (boundp 'jka-compr-compression-info-list))
416 (string-match nnheader-numerical-short-files file)
417 (string-to-int (match-string 0 file))))
419 (defun nnheader-directory-articles (dir)
420 (mapcar 'nnheader-file-to-number
421 (directory-files dir nil nnheader-numerical-short-files t)))
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)))
431 ;;; nnheader.el ends here