1 ;;; std11.el --- STD 11 functions for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, RFC 822, STD 11
8 ;; This file is part of MU (Message Utilities).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 (autoload 'buffer-substring-no-properties "emu")
28 (autoload 'member "emu")
32 (require 'std11-parse))
38 (defconst std11-field-name-regexp "[!-9;-~]+")
39 (defconst std11-field-head-regexp
40 (concat "^" std11-field-name-regexp ":"))
41 (defconst std11-next-field-head-regexp
42 (concat "\n" std11-field-name-regexp ":"))
44 (defun std11-field-end ()
45 "Move to end of field and return this point. [std11.el]"
46 (if (re-search-forward std11-next-field-head-regexp nil t)
47 (goto-char (match-beginning 0))
48 (if (re-search-forward "^$" nil t)
49 (goto-char (1- (match-beginning 0)))
55 (defun std11-field-body (name &optional boundary)
56 "Return body of field NAME.
57 If BOUNDARY is not nil, it is used as message header separator.
61 (std11-narrow-to-header boundary)
62 (goto-char (point-min))
63 (let ((case-fold-search t))
64 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
65 (buffer-substring-no-properties (match-end 0) (std11-field-end))
68 (defun std11-find-field-body (field-names &optional boundary)
69 "Return the first found field-body specified by FIELD-NAMES
70 of the message header in current buffer. If BOUNDARY is not nil, it is
71 used as message header separator. [std11.el]"
74 (std11-narrow-to-header boundary)
75 (let ((case-fold-search t)
78 (while (setq field-name (car field-names))
79 (goto-char (point-min))
80 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
82 (buffer-substring-no-properties
83 (match-end 0) (std11-field-end)))
85 (setq field-names (cdr field-names))
88 (defun std11-field-bodies (field-names &optional default-value boundary)
89 "Return list of each field-bodies of FIELD-NAMES of the message header
90 in current buffer. If BOUNDARY is not nil, it is used as message
91 header separator. [std11.el]"
94 (std11-narrow-to-header boundary)
95 (let* ((case-fold-search t)
96 (dest (make-list (length field-names) default-value))
100 (while (setq field-name (car s-rest))
101 (goto-char (point-min))
102 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
104 (buffer-substring-no-properties
105 (match-end 0) (std11-field-end)))
107 (setq s-rest (cdr s-rest)
116 (defun std11-unfold-string (string)
117 "Unfold STRING as message header field. [std11.el]"
119 (while (string-match "\n\\([ \t]\\)" string)
120 (setq dest (concat dest
121 (substring string 0 (match-beginning 0))
122 (match-string 1 string)
124 (setq string (substring string (match-end 0)))
133 (defun std11-narrow-to-header (&optional boundary)
134 "Narrow to the message header.
135 If BOUNDARY is not nil, it is used as message header separator.
138 (goto-char (point-min))
139 (if (re-search-forward
140 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
146 (defun std11-header-string (regexp &optional boundary)
147 "Return string of message header fields matched by REGEXP.
148 If BOUNDARY is not nil, it is used as message header separator.
150 (let ((case-fold-search t))
153 (std11-narrow-to-header boundary)
154 (goto-char (point-min))
156 (while (re-search-forward std11-field-head-regexp nil t)
158 (buffer-substring (match-beginning 0) (std11-field-end)))
159 (if (string-match regexp field)
160 (setq header (concat header field "\n"))
165 (defun std11-header-string-except (regexp &optional boundary)
166 "Return string of message header fields not matched by REGEXP.
167 If BOUNDARY is not nil, it is used as message header separator.
169 (let ((case-fold-search t))
172 (std11-narrow-to-header boundary)
173 (goto-char (point-min))
175 (while (re-search-forward std11-field-head-regexp nil t)
177 (buffer-substring (match-beginning 0) (std11-field-end)))
178 (if (not (string-match regexp field))
179 (setq header (concat header field "\n"))
184 (defun std11-collect-field-names (&optional boundary)
185 "Return list of all field-names of the message header in current buffer.
186 If BOUNDARY is not nil, it is used as message header separator.
190 (std11-narrow-to-header boundary)
191 (goto-char (point-min))
193 (while (re-search-forward std11-field-head-regexp nil t)
194 (setq name (buffer-substring-no-properties
195 (match-beginning 0)(1- (match-end 0))))
196 (or (member name dest)
197 (setq dest (cons name dest))
206 (defun std11-wrap-as-quoted-pairs (string specials)
210 (len (length string))
213 (let ((chr (aref string i)))
214 (if (memq chr specials)
215 (setq dest (concat dest (substring string b i) "\\")
220 (concat dest (substring string b))
223 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
225 (defun std11-wrap-as-quoted-string (string)
226 "Wrap STRING as RFC 822 quoted-string. [std11.el]"
228 (std11-wrap-as-quoted-pairs string std11-non-qtext-char-list)
231 (defun std11-strip-quoted-pair (string)
232 "Strip quoted-pairs in STRING. [std11.el]"
236 (len (length string))
239 (let ((chr (aref string i)))
241 (setq dest (concat dest (substring string b i))
246 (concat dest (substring string b))
249 (defun std11-strip-quoted-string (string)
250 "Strip quoted-string STRING. [std11.el]"
251 (let ((len (length string)))
253 (let ((max (1- len)))
254 (and (eq (aref string 0) ?\")
255 (eq (aref string max) ?\")
256 (std11-strip-quoted-pair (substring string 1 max))
264 (defun std11-addr-to-string (seq)
265 "Return string from lexical analyzed list SEQ
266 represents addr-spec of RFC 822. [std11.el]"
269 (let ((name (car token)))
271 ((eq name 'spaces) "")
272 ((eq name 'comment) "")
273 ((eq name 'quoted-string)
274 (concat "\"" (cdr token) "\""))
280 (defun std11-address-string (address)
281 "Return string of address part from parsed ADDRESS of RFC 822.
283 (cond ((eq (car address) 'group)
284 (mapconcat (function std11-address-string)
288 ((eq (car address) 'mailbox)
289 (let ((addr (nth 1 address)))
290 (std11-addr-to-string
291 (if (eq (car addr) 'phrase-route-addr)
297 (defun std11-full-name-string (address)
298 "Return string of full-name part from parsed ADDRESS of RFC 822.
300 (cond ((eq (car address) 'group)
307 ((eq (car address) 'mailbox)
308 (let ((addr (nth 1 address))
309 (comment (nth 2 address))
311 (if (eq (car addr) 'phrase-route-addr)
316 (let ((type (car token)))
317 (cond ((eq type 'quoted-string)
318 (std11-strip-quoted-pair (cdr token))
323 (std11-strip-quoted-pair (cdr token))
331 (cond ((> (length phrase) 0) phrase)
332 (comment (std11-strip-quoted-pair comment))
336 (defun std11-msg-id-string (msg-id)
337 "Return string from parsed MSG-ID of RFC 822."
338 (concat "<" (std11-addr-to-string (cdr msg-id)) ">")
341 (defun std11-fill-msg-id-list-string (string &optional column)
342 "Fill list of msg-id in STRING, and return the result."
345 (let ((lal (std11-lexical-analyze string))
347 (let ((ret (std11-parse-msg-id lal)))
349 (let* ((str (std11-msg-id-string (car ret)))
352 (if (> (+ len column) 76)
353 (setq dest (concat dest "\n " str)
356 column (+ column len))
358 (setq dest (concat dest (cdr (car lal)))
362 (let ((ret (std11-parse-msg-id lal)))
364 (let* ((str (std11-msg-id-string (car ret)))
365 (len (1+ (length str))))
367 (if (> (+ len column) 76)
368 (setq dest (concat dest "\n " str)
370 (setq dest (concat dest " " str)
371 column (+ column len))
373 (setq dest (concat dest (cdr (car lal)))
382 (defun std11-parse-address-string (string)
383 "Parse STRING as mail address. [std11.el]"
384 (std11-parse-address (std11-lexical-analyze string))
387 (defun std11-parse-addresses-string (string)
388 "Parse STRING as mail address list. [std11.el]"
389 (std11-parse-addresses (std11-lexical-analyze string))
392 (defun std11-extract-address-components (string)
393 "Extract full name and canonical address from STRING.
394 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
395 If no name can be extracted, FULL-NAME will be nil. [std11.el]"
396 (let* ((structure (car (std11-parse-address-string
397 (std11-unfold-string string))))
398 (phrase (std11-full-name-string structure))
399 (address (std11-address-string structure))
401 (list phrase address)
408 (autoload func "std11-parse")
410 '(std11-lexical-analyze
411 std11-parse-address std11-parse-addresses
412 std11-parse-address-string))
418 ;;; std11.el ends here