1 ;;; std11.el --- STD 11 functions for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, RFC 822, STD 11
7 ;; Version: $Id: std11.el,v 1.1 1998-02-04 07:24:33 morioka Exp $
9 ;; This file is part of MU (Message Utilities).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
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.
28 (autoload 'buffer-substring-no-properties "emu")
29 (autoload 'member "emu")
35 (defconst std11-field-name-regexp "[!-9;-~]+")
36 (defconst std11-field-head-regexp
37 (concat "^" std11-field-name-regexp ":"))
38 (defconst std11-next-field-head-regexp
39 (concat "\n" std11-field-name-regexp ":"))
41 (defun std11-field-end ()
42 "Move to end of field and return this point. [std11.el]"
43 (if (re-search-forward std11-next-field-head-regexp nil t)
44 (goto-char (match-beginning 0))
45 (if (re-search-forward "^$" nil t)
46 (goto-char (1- (match-beginning 0)))
52 (defun std11-field-body (name &optional boundary)
53 "Return body of field NAME.
54 If BOUNDARY is not nil, it is used as message header separator.
58 (std11-narrow-to-header boundary)
59 (goto-char (point-min))
60 (let ((case-fold-search t))
61 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
62 (buffer-substring-no-properties (match-end 0) (std11-field-end))
65 (defun std11-find-field-body (field-names &optional boundary)
66 "Return the first found field-body specified by FIELD-NAMES
67 of the message header in current buffer. If BOUNDARY is not nil, it is
68 used as message header separator. [std11.el]"
71 (std11-narrow-to-header boundary)
72 (let ((case-fold-search t)
75 (while (setq field-name (car field-names))
76 (goto-char (point-min))
77 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
79 (buffer-substring-no-properties
80 (match-end 0) (std11-field-end)))
82 (setq field-names (cdr field-names))
85 (defun std11-field-bodies (field-names &optional default-value boundary)
86 "Return list of each field-bodies of FIELD-NAMES of the message header
87 in current buffer. If BOUNDARY is not nil, it is used as message
88 header separator. [std11.el]"
91 (std11-narrow-to-header boundary)
92 (let* ((case-fold-search t)
93 (dest (make-list (length field-names) default-value))
97 (while (setq field-name (car s-rest))
98 (goto-char (point-min))
99 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
101 (buffer-substring-no-properties
102 (match-end 0) (std11-field-end)))
104 (setq s-rest (cdr s-rest)
113 (defun std11-unfold-string (string)
114 "Unfold STRING as message header field. [std11.el]"
116 (while (string-match "\n\\([ \t]\\)" string)
117 (setq dest (concat dest
118 (substring string 0 (match-beginning 0))
119 (match-string 1 string)
121 (setq string (substring string (match-end 0)))
130 (defun std11-narrow-to-header (&optional boundary)
131 "Narrow to the message header.
132 If BOUNDARY is not nil, it is used as message header separator.
135 (goto-char (point-min))
136 (if (re-search-forward
137 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
143 (defun std11-header-string (regexp &optional boundary)
144 "Return string of message header fields matched by REGEXP.
145 If BOUNDARY is not nil, it is used as message header separator.
147 (let ((case-fold-search t))
150 (std11-narrow-to-header boundary)
151 (goto-char (point-min))
153 (while (re-search-forward std11-field-head-regexp nil t)
155 (buffer-substring (match-beginning 0) (std11-field-end)))
156 (if (string-match regexp field)
157 (setq header (concat header field "\n"))
162 (defun std11-header-string-except (regexp &optional boundary)
163 "Return string of message header fields not matched by REGEXP.
164 If BOUNDARY is not nil, it is used as message header separator.
166 (let ((case-fold-search t))
169 (std11-narrow-to-header boundary)
170 (goto-char (point-min))
172 (while (re-search-forward std11-field-head-regexp nil t)
174 (buffer-substring (match-beginning 0) (std11-field-end)))
175 (if (not (string-match regexp field))
176 (setq header (concat header field "\n"))
181 (defun std11-collect-field-names (&optional boundary)
182 "Return list of all field-names of the message header in current buffer.
183 If BOUNDARY is not nil, it is used as message header separator.
187 (std11-narrow-to-header boundary)
188 (goto-char (point-min))
190 (while (re-search-forward std11-field-head-regexp nil t)
191 (setq name (buffer-substring-no-properties
192 (match-beginning 0)(1- (match-end 0))))
193 (or (member name dest)
194 (setq dest (cons name dest))
203 (defun std11-wrap-as-quoted-pairs (string specials)
207 (len (length string))
210 (let ((chr (aref string i)))
211 (if (memq chr specials)
212 (setq dest (concat dest (substring string b i) "\\")
217 (concat dest (substring string b))
220 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
222 (defun std11-wrap-as-quoted-string (string)
223 "Wrap STRING as RFC 822 quoted-string. [std11.el]"
225 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
228 (defun std11-strip-quoted-pair (string)
229 "Strip quoted-pairs in STRING. [std11.el]"
233 (len (length string))
236 (let ((chr (aref string i)))
238 (setq dest (concat dest (substring string b i))
243 (concat dest (substring string b))
246 (defun std11-strip-quoted-string (string)
247 "Strip quoted-string STRING. [std11.el]"
248 (let ((len (length string)))
250 (let ((max (1- len)))
251 (and (eq (aref string 0) ?\")
252 (eq (aref string max) ?\")
253 (std11-strip-quoted-pair (substring string 1 max))
261 (defun std11-addr-to-string (seq)
262 "Return string from lexical analyzed list SEQ
263 represents addr-spec of RFC 822. [std11.el]"
266 (let ((name (car token)))
268 ((eq name 'spaces) "")
269 ((eq name 'comment) "")
270 ((eq name 'quoted-string)
271 (concat "\"" (cdr token) "\""))
277 (defun std11-address-string (address)
278 "Return string of address part from parsed ADDRESS of RFC 822.
280 (cond ((eq (car address) 'group)
281 (mapconcat (function std11-address-string)
285 ((eq (car address) 'mailbox)
286 (let ((addr (nth 1 address)))
287 (std11-addr-to-string
288 (if (eq (car addr) 'phrase-route-addr)
294 (defun std11-full-name-string (address)
295 "Return string of full-name part from parsed ADDRESS of RFC 822.
297 (cond ((eq (car address) 'group)
304 ((eq (car address) 'mailbox)
305 (let ((addr (nth 1 address))
306 (comment (nth 2 address))
308 (if (eq (car addr) 'phrase-route-addr)
313 (let ((type (car token)))
314 (cond ((eq type 'quoted-string)
315 (std11-strip-quoted-pair (cdr token))
320 (std11-strip-quoted-pair (cdr token))
328 (cond ((> (length phrase) 0) phrase)
329 (comment (std11-strip-quoted-pair comment))
333 (defun std11-msg-id-string (msg-id)
334 "Return string from parsed MSG-ID of RFC 822."
335 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
338 (defun std11-fill-msg-id-list-string (string &optional column)
339 "Fill list of msg-id in STRING, and return the result."
342 (let ((lal (std11-lexical-analyze string))
344 (let ((ret (std11-parse-msg-id lal)))
346 (let* ((str (std11-msg-id-string (car ret)))
349 (if (> (+ len column) 76)
350 (setq dest (concat dest "\n " str)
353 column (+ column len))
355 (setq dest (concat dest (cdr (car lal)))
359 (let ((ret (std11-parse-msg-id lal)))
361 (let* ((str (std11-msg-id-string (car ret)))
362 (len (1+ (length str))))
364 (if (> (+ len column) 76)
365 (setq dest (concat dest "\n " str)
367 (setq dest (concat dest " " str)
368 column (+ column len))
370 (setq dest (concat dest (cdr (car lal)))
379 (defun std11-parse-address-string (string)
380 "Parse STRING as mail address. [std11.el]"
381 (std11-parse-address (std11-lexical-analyze string))
384 (defun std11-parse-addresses-string (string)
385 "Parse STRING as mail address list. [std11.el]"
386 (std11-parse-addresses (std11-lexical-analyze string))
389 (defun std11-extract-address-components (string)
390 "Extract full name and canonical address from STRING.
391 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
392 If no name can be extracted, FULL-NAME will be nil. [std11.el]"
393 (let* ((structure (car (std11-parse-address-string
394 (std11-unfold-string string))))
395 (phrase (std11-full-name-string structure))
396 (address (std11-address-string structure))
398 (list phrase address)
405 (autoload func "std11-parse")
407 '(std11-lexical-analyze
408 std11-parse-address std11-parse-addresses
409 std11-parse-address-string))
415 ;;; std11.el ends here