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