(std11-field-names): New function.
[elisp/mu-cite.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.4 1996-08-28 13:03:23 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)
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
62 ;;; @ header
63 ;;;
64
65 (defun std11-narrow-to-header (&optional boundary)
66   (narrow-to-region
67    (goto-char (point-min))
68    (if (re-search-forward
69         (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
70         nil t)
71        (match-beginning 0)
72      (point-max)
73      )))
74
75 (defun std11-header-string (pat &optional boundary)
76   (let ((case-fold-search t))
77     (save-excursion
78       (save-restriction
79         (std11-narrow-to-header boundary)
80         (goto-char (point-min))
81         (let (field header)
82           (while (re-search-forward std11-field-head-regexp nil t)
83             (setq field
84                   (buffer-substring (match-beginning 0) (std11-field-end)))
85             (if (string-match pat field)
86                 (setq header (concat header field "\n"))
87               ))
88           header)
89         ))))
90
91 (defun std11-header-string-except (pat &optional boundary)
92   (let ((case-fold-search t))
93     (save-excursion
94       (save-restriction
95         (std11-narrow-to-header boundary)
96         (goto-char (point-min))
97         (let (field header)
98           (while (re-search-forward std11-field-head-regexp nil t)
99             (setq field
100                   (buffer-substring (match-beginning 0) (std11-field-end)))
101             (if (not (string-match pat field))
102                 (setq header (concat header field "\n"))
103               ))
104           header)
105         ))))
106
107 (defun std11-field-names (&optional boundary)
108   (save-excursion
109     (save-restriction
110       (std11-narrow-to-header boundary)
111       (goto-char (point-min))
112       (let (dest name)
113         (while (re-search-forward std11-field-head-regexp nil t)
114           (setq name (buffer-substring-no-properties
115                       (match-beginning 0)(1- (match-end 0))))
116           (or (member name dest)
117               (setq dest (cons name dest))
118               )
119           )
120         dest))))
121
122
123 ;;; @ end
124 ;;;
125
126 (provide 'std11)
127
128 ;;; std11.el ends here