Backport fix for minor Bazaar leftovers.
[emacs.git] / lisp / mail / mailheader.el
blobdaacc2b853ce481bbc5a05c6daf04eec13341dca
1 ;;; mailheader.el --- mail header parsing, merging, formatting
3 ;; Copyright (C) 1996, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Erik Naggum <erik@naggum.no>
6 ;; Keywords: tools, mail, news
7 ;; Package: mail-utils
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This package provides an abstraction to RFC822-style messages, used in
27 ;; mail, news, and some other systems. The simple syntactic rules for such
28 ;; headers, such as quoting and line folding, are routinely reimplemented
29 ;; in many individual packages. This package removes the need for this
30 ;; redundancy by representing message headers as association lists,
31 ;; offering functions to extract the set of headers from a message, to
32 ;; parse individual headers, to merge sets of headers, and to format a set
33 ;; of headers.
35 ;; The car of each element in the message-header alist is a symbol whose
36 ;; print name is the name of the header, in all lower-case. The cdr of an
37 ;; element depends on the operation. After extracting headers from a
38 ;; message, it is a string, the value of the header. An extracted set of
39 ;; headers may be parsed further, which may turn it into a list, whose car
40 ;; is the original value and whose subsequent elements depend on the
41 ;; header. For formatting, it is evaluated to obtain the strings to be
42 ;; inserted. For merging, one set of headers consists of strings, while
43 ;; the other set will be evaluated with the symbols in the first set of
44 ;; headers bound to their respective values.
46 ;;; Code:
48 (defun mail-header-extract ()
49 "Extract headers from current buffer after point.
50 Returns a header alist, where each element is a cons cell (name . value),
51 where NAME is a symbol, and VALUE is the string value of the header having
52 that name."
53 (let ((message-headers ()) (top (point))
54 start end)
55 (while (and (setq start (point))
56 (> (skip-chars-forward "^\0- :") 0)
57 (= (following-char) ?:)
58 (setq end (point))
59 (progn (forward-char)
60 (> (skip-chars-forward " \t") 0)))
61 (let ((header (intern (downcase (buffer-substring start end))))
62 (value (list (buffer-substring
63 (point) (progn (end-of-line) (point))))))
64 (while (progn (forward-char) (> (skip-chars-forward " \t") 0))
65 (push (buffer-substring (point) (progn (end-of-line) (point)))
66 value))
67 (push (if (cdr value)
68 (cons header (mapconcat #'identity (nreverse value) " "))
69 (cons header (car value)))
70 message-headers)))
71 (goto-char top)
72 (nreverse message-headers)))
74 (defun mail-header-extract-no-properties ()
75 "Extract headers from current buffer after point, without properties.
76 Returns a header alist, where each element is a cons cell (name . value),
77 where NAME is a symbol, and VALUE is the string value of the header having
78 that name."
79 (mapcar
80 (lambda (elt)
81 (set-text-properties 0 (length (cdr elt)) nil (cdr elt))
82 elt)
83 (mail-header-extract)))
85 (defun mail-header-parse (parsing-rules headers)
86 "Apply PARSING-RULES to HEADERS.
87 PARSING-RULES is an alist whose keys are header names (symbols) and whose
88 value is a parsing function. The function takes one argument, a string,
89 and return a list of values, which will destructively replace the value
90 associated with the key in HEADERS, after being prepended with the original
91 value."
92 (dolist (rule parsing-rules)
93 (let ((header (assq (car rule) headers)))
94 (when header
95 (if (consp (cdr header))
96 (setf (cddr header) (funcall (cdr rule) (cadr header)))
97 (setf (cdr header)
98 (cons (cdr header) (funcall (cdr rule) (cdr header))))))))
99 headers)
101 ;; Advertised part of the interface; see mail-header, mail-header-set.
102 (defvar headers)
104 (defsubst mail-header (header &optional header-alist)
105 "Return the value associated with header HEADER in HEADER-ALIST.
106 If the value is a string, it is the original value of the header. If the
107 value is a list, its first element is the original value of the header,
108 with any subsequent elements being the result of parsing the value.
109 If HEADER-ALIST is nil, the dynamically bound variable `headers' is used."
110 (declare (gv-setter (lambda (value)
111 `(mail-header-set ,header ,value ,header-alist))))
112 (cdr (assq header (or header-alist headers))))
114 (defun mail-header-set (header value &optional header-alist)
115 "Set the value associated with header HEADER to VALUE in HEADER-ALIST.
116 HEADER-ALIST defaults to the dynamically bound variable `headers' if nil.
117 See `mail-header' for the semantics of VALUE."
118 (let* ((alist (or header-alist headers))
119 (entry (assq header alist)))
120 (if entry
121 (setf (cdr entry) value)
122 (nconc alist (list (cons header value)))))
123 value)
125 (defun mail-header-merge (merge-rules headers)
126 "Return a new header alist with MERGE-RULES applied to HEADERS.
127 MERGE-RULES is an alist whose keys are header names (symbols) and whose
128 values are forms to evaluate, the results of which are the new headers. It
129 should be a string or a list of string. The first element may be nil to
130 denote that the formatting functions must use the remaining elements, or
131 skip the header altogether if there are no other elements.
132 The macro `mail-header' can be used to access headers in HEADERS."
133 (mapcar
134 (lambda (rule)
135 (cons (car rule) (eval (cdr rule))))
136 merge-rules))
138 (defvar mail-header-format-function
139 (lambda (header value)
140 "Function to format headers without a specified formatting function."
141 (insert (capitalize (symbol-name header))
142 ": "
143 (if (consp value) (car value) value)
144 "\n")))
146 (defun mail-header-format (format-rules headers)
147 "Use FORMAT-RULES to format HEADERS and insert into current buffer.
148 HEADERS should be an alist of the form (HEADER . VALUE),
149 where HEADER is a header field name (a symbol or a string),
150 and VALUE is the contents for that header field.
152 FORMAT-RULES is an alist of elements (HEADER . FUNCTION) Here HEADER
153 is a header field name (a symbol), and FUNCTION is how to format that
154 header field, if it appears in HEADERS. Each FUNCTION should take two
155 arguments: the header symbol, and the value of that header. The value
156 returned by FUNCTION is inserted in the buffer unless it is nil.
158 If the function for a header field is nil, or if no function is
159 specified for a particular header field, the default action is to
160 insert the value of the header, unless it is nil.
162 The headers are inserted in the order of the FORMAT-RULES.
163 A key of t in FORMAT-RULES represents any otherwise unmentioned headers.
164 A key of nil has as its value a list of defaulted headers to ignore."
165 (let ((ignore (append (cdr (assq nil format-rules))
166 (mapcar #'car format-rules))))
167 (dolist (rule format-rules)
168 (let* ((header (car rule))
169 (value (mail-header header)))
170 (if (stringp header)
171 (setq header (intern header)))
172 (cond ((null header) 'ignore)
173 ((eq header t)
174 (dolist (defaulted headers)
175 (unless (memq (car defaulted) ignore)
176 (let* ((header (car defaulted))
177 (value (cdr defaulted)))
178 (if (cdr rule)
179 (funcall (cdr rule) header value)
180 (funcall mail-header-format-function header value))))))
181 (value
182 (if (cdr rule)
183 (funcall (cdr rule) header value)
184 (funcall mail-header-format-function header value))))))
185 (insert "\n")))
187 (provide 'mailheader)
189 ;;; mailheader.el ends here