Function `std11-field-bodies' was renamed to
[elisp/apel.git] / std11.el
1 ;;; std11.el --- STD 11 parser for GNU Emacs
2
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
4
5 ;; Author:   MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, RFC 822, STD 11
7 ;; Version: $Id: std11.el,v 0.10 1996-08-28 15:25:16 morioka Exp $
8
9 ;; This file is part of tl (Tiny Library).
10
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.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with This program; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (autoload 'buffer-substring-no-properties "emu")
29 (autoload 'member "emu")
30
31
32 ;;; @ field
33 ;;;
34
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 ":"))
40
41 (defun std11-find-field-body (name &optional boundary)
42   "Return body of field NAME.
43 If BOUNDARY is not nil, it is used as message header separator.
44 \[std11.el]"
45   (save-excursion
46     (save-restriction
47       (std11-narrow-to-header boundary)
48       (goto-char (point-min))
49       (let ((case-fold-search t))
50         (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
51             (buffer-substring-no-properties (match-end 0) (std11-field-end))
52           )))))
53
54 (defun std11-field-end ()
55   "Move to end of field and return this point. [std11.el]"
56   (if (re-search-forward std11-next-field-head-regexp nil t)
57       (goto-char (match-beginning 0))
58     (if (re-search-forward "^$" nil t)
59         (goto-char (1- (match-beginning 0)))
60       (end-of-line)
61       ))
62   (point)
63   )
64
65 (defun std11-field-names (&optional boundary)
66   "Return list of all field-names of the message header in current buffer.
67 If BOUNDARY is not nil, it is used as message header separator.
68 \[std11.el]"
69   (save-excursion
70     (save-restriction
71       (std11-narrow-to-header boundary)
72       (goto-char (point-min))
73       (let (dest name)
74         (while (re-search-forward std11-field-head-regexp nil t)
75           (setq name (buffer-substring-no-properties
76                       (match-beginning 0)(1- (match-end 0))))
77           (or (member name dest)
78               (setq dest (cons name dest))
79               )
80           )
81         dest))))
82
83 (defun std11-find-field-bodies (field-names &optional default-value boundary)
84   "Return list of each field-bodies of FIELD-NAMES of the message header
85 in current buffer. If BOUNDARY is not nil, it is used as message
86 header separator. [std11.el]"
87   (save-excursion
88     (save-restriction
89       (std11-narrow-to-header boundary)
90       (let* ((case-fold-search t)
91              (dest (make-list (length field-names) default-value))
92              (s-rest field-names)
93              (d-rest dest)
94              field-name)
95         (while (setq field-name (car s-rest))
96           (goto-char (point-min))
97           (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
98               (setcar d-rest
99                       (buffer-substring-no-properties
100                        (match-end 0) (std11-field-end)))
101             )
102           (setq s-rest (cdr s-rest)
103                 d-rest (cdr d-rest))
104           )
105         dest))))
106
107
108 ;;; @ unfolding
109 ;;;
110
111 (defun std11-unfold-string (string)
112   "Unfold STRING as message header field. [std11.el]"
113   (let ((dest ""))
114     (while (string-match "\n\\s +" string)
115       (setq dest (concat dest (substring string 0 (match-beginning 0)) " "))
116       (setq string (substring string (match-end 0)))
117       )
118     (concat dest string)
119     ))
120
121
122 ;;; @ header
123 ;;;
124
125 (defun std11-narrow-to-header (&optional boundary)
126   "Narrow to the message header.
127 If BOUNDARY is not nil, it is used as message header separator.
128 \[std11.el]"
129   (narrow-to-region
130    (goto-char (point-min))
131    (if (re-search-forward
132         (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
133         nil t)
134        (match-beginning 0)
135      (point-max)
136      )))
137
138 (defun std11-header-string (regexp &optional boundary)
139   "Return string of message header fields matched by REGEXP.
140 If BOUNDARY is not nil, it is used as message header separator.
141 \[std11.el]"
142   (let ((case-fold-search t))
143     (save-excursion
144       (save-restriction
145         (std11-narrow-to-header boundary)
146         (goto-char (point-min))
147         (let (field header)
148           (while (re-search-forward std11-field-head-regexp nil t)
149             (setq field
150                   (buffer-substring (match-beginning 0) (std11-field-end)))
151             (if (string-match regexp field)
152                 (setq header (concat header field "\n"))
153               ))
154           header)
155         ))))
156
157 (defun std11-header-string-except (regexp &optional boundary)
158   "Return string of message header fields not matched by REGEXP.
159 If BOUNDARY is not nil, it is used as message header separator.
160 \[std11.el]"
161   (let ((case-fold-search t))
162     (save-excursion
163       (save-restriction
164         (std11-narrow-to-header boundary)
165         (goto-char (point-min))
166         (let (field header)
167           (while (re-search-forward std11-field-head-regexp nil t)
168             (setq field
169                   (buffer-substring (match-beginning 0) (std11-field-end)))
170             (if (not (string-match regexp field))
171                 (setq header (concat header field "\n"))
172               ))
173           header)
174         ))))
175
176
177 ;;; @ end
178 ;;;
179
180 (provide 'std11)
181
182 ;;; std11.el ends here