(std11-field-body): fixed.
[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.6 1996-08-28 14:17:21 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-field-body (name &optional boundary)
42   (save-excursion
43     (save-restriction
44       (std11-narrow-to-header boundary)
45       (goto-char (point-min))
46       (let ((case-fold-search t))
47         (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
48             (buffer-substring-no-properties (match-end 0) (std11-field-end))
49           )))))
50
51 (defun std11-field-end ()
52   (if (re-search-forward std11-next-field-head-regexp nil t)
53       (goto-char (match-beginning 0))
54     (if (re-search-forward "^$" nil t)
55         (goto-char (1- (match-beginning 0)))
56       (end-of-line)
57       ))
58   (point)
59   )
60
61 (defun std11-field-names (&optional boundary)
62   (save-excursion
63     (save-restriction
64       (std11-narrow-to-header boundary)
65       (goto-char (point-min))
66       (let (dest name)
67         (while (re-search-forward std11-field-head-regexp nil t)
68           (setq name (buffer-substring-no-properties
69                       (match-beginning 0)(1- (match-end 0))))
70           (or (member name dest)
71               (setq dest (cons name dest))
72               )
73           )
74         dest))))
75
76 (defun std11-field-bodies (field-names &optional default-value boundary)
77   (save-excursion
78     (save-restriction
79       (std11-narrow-to-header boundary)
80       (let* ((case-fold-search t)
81              (dest (make-list (length field-names) default-value))
82              (s-rest field-names)
83              (d-rest dest)
84              field-name)
85         (while (setq field-name (car s-rest))
86           (goto-char (point-min))
87           (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
88               (setcar d-rest
89                       (buffer-substring-no-properties
90                        (match-end 0) (std11-field-end)))
91             )
92           (setq s-rest (cdr s-rest)
93                 d-rest (cdr d-rest))
94           )
95         dest))))
96
97
98 ;;; @ header
99 ;;;
100
101 (defun std11-narrow-to-header (&optional boundary)
102   (narrow-to-region
103    (goto-char (point-min))
104    (if (re-search-forward
105         (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
106         nil t)
107        (match-beginning 0)
108      (point-max)
109      )))
110
111 (defun std11-header-string (pat &optional boundary)
112   (let ((case-fold-search t))
113     (save-excursion
114       (save-restriction
115         (std11-narrow-to-header boundary)
116         (goto-char (point-min))
117         (let (field header)
118           (while (re-search-forward std11-field-head-regexp nil t)
119             (setq field
120                   (buffer-substring (match-beginning 0) (std11-field-end)))
121             (if (string-match pat field)
122                 (setq header (concat header field "\n"))
123               ))
124           header)
125         ))))
126
127 (defun std11-header-string-except (pat &optional boundary)
128   (let ((case-fold-search t))
129     (save-excursion
130       (save-restriction
131         (std11-narrow-to-header boundary)
132         (goto-char (point-min))
133         (let (field header)
134           (while (re-search-forward std11-field-head-regexp nil t)
135             (setq field
136                   (buffer-substring (match-beginning 0) (std11-field-end)))
137             (if (not (string-match pat field))
138                 (setq header (concat header field "\n"))
139               ))
140           header)
141         ))))
142
143
144 ;;; @ end
145 ;;;
146
147 (provide 'std11)
148
149 ;;; std11.el ends here