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