* gnus.el: Fixed all the doc strings to match the FSF convetions.
[gnus] / lisp / mml.el
1 ;;; mml.el --- A package for parsing and validating MML documents
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'mm-util)
27 (require 'mm-bodies)
28 (require 'mm-encode)
29 (require 'mm-decode)
30
31 (eval-and-compile
32   (autoload 'message-make-message-id "message"))
33
34 (defvar mml-generate-multipart-alist
35   '(("signed" . rfc2015-generate-signed-multipart)
36     ("encrypted" . rfc2015-generate-encrypted-multipart))
37   "*Alist of multipart generation functions.
38
39 Each entry has the form (NAME . FUNCTION), where
40 NAME: is a string containing the name of the part (without the 
41 leading \"/multipart/\"),
42 FUNCTION: is a Lisp function which is called to generate the part.
43
44 The Lisp function has to supply the appropriate MIME headers and the
45 contents of this part.")
46
47 (defvar mml-syntax-table
48   (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
49     (modify-syntax-entry ?\\ "/" table)
50     (modify-syntax-entry ?< "(" table)
51     (modify-syntax-entry ?> ")" table)
52     (modify-syntax-entry ?@ "w" table)
53     (modify-syntax-entry ?/ "w" table)
54     (modify-syntax-entry ?= " " table)
55     (modify-syntax-entry ?* " " table)
56     (modify-syntax-entry ?\; " " table)
57     (modify-syntax-entry ?\' " " table)
58     table))
59
60 (defvar mml-boundary-function 'mml-make-boundary
61   "A function called to suggest a boundary.
62 The function may be called several times, and should try to make a new
63 suggestion each time.  The function is called with one parameter,
64 which is a number that says how many times the function has been
65 called for this message.")
66
67 (defun mml-parse ()
68   "Parse the current buffer as an MML document."
69   (goto-char (point-min))
70   (let ((table (syntax-table)))
71     (unwind-protect
72         (progn
73           (set-syntax-table mml-syntax-table)
74           (mml-parse-1))
75       (set-syntax-table table))))
76
77 (defun mml-parse-1 ()
78   "Parse the current buffer as an MML document."
79   (let (struct tag point contents charsets warn)
80     (while (and (not (eobp))
81                 (not (looking-at "<#/multipart")))
82       (cond
83        ((looking-at "<#multipart")
84         (push (nconc (mml-read-tag) (mml-parse-1)) struct))
85        ((looking-at "<#external")
86         (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
87               struct))
88        (t
89         (if (looking-at "<#part")
90             (setq tag (mml-read-tag))
91           (setq tag (list 'part '(type . "text/plain"))
92                 warn t))
93         (setq point (point)
94               contents (mml-read-part)
95               charsets (mm-find-mime-charset-region point (point)))
96         (if (< (length charsets) 2)
97             (push (nconc tag (list (cons 'contents contents)))
98                   struct)
99           (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
100                           tag point (point))))
101             (when (and warn
102                        (not
103                         (y-or-n-p
104                          (format
105                           "Warning: Your message contains %d parts.  Really send? "
106                           (length nstruct)))))
107               (error "Edit your message to use only one charset"))
108             (setq struct (nconc nstruct struct)))))))
109     (unless (eobp)
110       (forward-line 1))
111     (nreverse struct)))
112
113 (defun mml-parse-singlepart-with-multiple-charsets (orig-tag beg end)
114   (save-excursion
115     (narrow-to-region beg end)
116     (goto-char (point-min))
117     (let ((current (mm-mime-charset (char-charset (following-char))))
118           charset struct space newline paragraph)
119       (while (not (eobp))
120         (cond
121          ;; The charset remains the same.
122          ((or (eq (setq charset (mm-mime-charset
123                                  (char-charset (following-char)))) 'us-ascii)
124               (eq charset current)))
125          ;; The initial charset was ascii.
126          ((eq current 'us-ascii)
127           (setq current charset
128                 space nil
129                 newline nil
130                 paragraph nil))
131          ;; We have a change in charsets.
132          (t
133           (push (append
134                  orig-tag
135                  (list (cons 'contents
136                              (buffer-substring-no-properties
137                               beg (or paragraph newline space (point))))))
138                 struct)
139           (setq beg (or paragraph newline space (point))
140                 current charset
141                 space nil
142                 newline nil
143                 paragraph nil)))
144         ;; Compute places where it might be nice to break the part.
145         (cond
146          ((memq (following-char) '(?  ?\t))
147           (setq space (1+ (point))))
148          ((eq (following-char) ?\n)
149           (setq newline (1+ (point))))
150          ((and (eq (following-char) ?\n)
151                (not (bobp))
152                (eq (char-after (1- (point))) ?\n))
153           (setq paragraph (point))))
154         (forward-char 1))
155       ;; Do the final part.
156       (unless (= beg (point))
157         (push (append orig-tag
158                       (list (cons 'contents
159                                   (buffer-substring-no-properties
160                                    beg (point)))))
161               struct))
162       struct)))
163
164 (defun mml-read-tag ()
165   "Read a tag and return the contents."
166   (let (contents name elem val)
167     (forward-char 2)
168     (setq name (buffer-substring-no-properties
169                 (point) (progn (forward-sexp 1) (point))))
170     (skip-chars-forward " \t\n")
171     (while (not (looking-at ">"))
172       (setq elem (buffer-substring-no-properties
173                   (point) (progn (forward-sexp 1) (point))))
174       (skip-chars-forward "= \t\n")
175       (setq val (buffer-substring-no-properties
176                  (point) (progn (forward-sexp 1) (point))))
177       (when (string-match "^\"\\(.*\\)\"$" val)
178         (setq val (match-string 1 val)))
179       (push (cons (intern elem) val) contents)
180       (skip-chars-forward " \t\n"))
181     (forward-char 1)
182     (skip-chars-forward " \t\n")
183     (cons (intern name) (nreverse contents))))
184
185 (defun mml-read-part ()
186   "Return the buffer up till the next part, multipart or closing part or multipart."
187   (let ((beg (point)))
188     ;; If the tag ended at the end of the line, we go to the next line.
189     (when (looking-at "[ \t]*\n")
190       (forward-line 1))
191     (if (re-search-forward
192          "<#\\(/\\)?\\(multipart\\|part\\|external\\)." nil t)
193         (prog1
194             (buffer-substring-no-properties beg (match-beginning 0))
195           (if (or (not (match-beginning 1))
196                   (equal (match-string 2) "multipart"))
197               (goto-char (match-beginning 0))
198             (when (looking-at "[ \t]*\n")
199               (forward-line 1))))
200       (buffer-substring-no-properties beg (goto-char (point-max))))))
201
202 (defvar mml-boundary nil)
203 (defvar mml-base-boundary "-=-=")
204 (defvar mml-multipart-number 0)
205
206 (defun mml-generate-mime ()
207   "Generate a MIME message based on the current MML document."
208   (let ((cont (mml-parse))
209         (mml-multipart-number 0))
210     (if (not cont)
211         nil
212       (with-temp-buffer
213         (if (and (consp (car cont))
214                  (= (length cont) 1))
215             (mml-generate-mime-1 (car cont))
216           (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
217                                       cont)))
218         (buffer-string)))))
219
220 (defun mml-generate-mime-1 (cont)
221   (cond
222    ((eq (car cont) 'part)
223     (let (coded encoding charset filename type)
224       (setq type (or (cdr (assq 'type cont)) "text/plain"))
225       (if (member (car (split-string type "/")) '("text" "message"))
226           (with-temp-buffer
227             (cond
228              ((cdr (assq 'buffer cont))
229               (insert-buffer-substring (cdr (assq 'buffer cont))))
230              ((and (setq filename (cdr (assq 'filename cont)))
231                    (not (equal (cdr (assq 'nofile cont)) "yes")))
232               (mm-insert-file-contents filename))
233              (t
234               (save-restriction
235                 (narrow-to-region (point) (point))
236                 (insert (cdr (assq 'contents cont)))
237                 ;; Remove quotes from quoted tags.
238                 (goto-char (point-min))
239                 (while (re-search-forward
240                         "<#!+/?\\(part\\|multipart\\|external\\)" nil t)
241                   (delete-region (+ (match-beginning 0) 2)
242                                  (+ (match-beginning 0) 3))))))
243             (setq charset (mm-encode-body))
244             (setq encoding (mm-body-encoding charset 
245                                              (cdr (assq 'encoding cont))))
246             (setq coded (buffer-string)))
247         (mm-with-unibyte-buffer
248           (cond
249            ((cdr (assq 'buffer cont))
250             (insert-buffer-substring (cdr (assq 'buffer cont))))
251            ((and (setq filename (cdr (assq 'filename cont)))
252                  (not (equal (cdr (assq 'nofile cont)) "yes")))
253             (let ((coding-system-for-read mm-binary-coding-system))
254               (mm-insert-file-contents filename nil nil nil nil t)))
255            (t
256             (insert (cdr (assq 'contents cont)))))
257           (setq encoding (mm-encode-buffer type)
258                 coded (buffer-string))))
259       (mml-insert-mime-headers cont type charset encoding)
260       (insert "\n")
261       (insert coded)))
262    ((eq (car cont) 'external)
263     (insert "Content-Type: message/external-body")
264     (let ((parameters (mml-parameter-string
265                        cont '(expiration size permission)))
266           (name (cdr (assq 'name cont))))
267       (when name
268         (setq name (mml-parse-file-name name))
269         (if (stringp name)
270             (mml-insert-parameter
271              (mail-header-encode-parameter "name" name)
272              "access-type=local-file")
273           (mml-insert-parameter
274            (mail-header-encode-parameter
275             "name" (file-name-nondirectory (nth 2 name)))
276            (mail-header-encode-parameter "site" (nth 1 name))
277            (mail-header-encode-parameter
278             "directory" (file-name-directory (nth 2 name))))
279           (mml-insert-parameter
280            (concat "access-type="
281                    (if (member (nth 0 name) '("ftp@" "anonymous@"))
282                        "anon-ftp"
283                      "ftp")))))      
284       (when parameters
285         (mml-insert-parameter-string
286          cont '(expiration size permission))))
287     (insert "\n\n")
288     (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
289     (insert "Content-ID: " (message-make-message-id) "\n")
290     (insert "Content-Transfer-Encoding: "
291             (or (cdr (assq 'encoding cont)) "binary"))
292     (insert "\n\n")
293     (insert (or (cdr (assq 'contents cont))))
294     (insert "\n"))
295    ((eq (car cont) 'multipart)
296     (let* ((type (or (cdr (assq 'type cont)) "mixed"))
297            (handler (assoc type mml-generate-multipart-alist)))
298       (if handler
299           (funcall (cdr handler) cont)
300         ;; No specific handler.  Use default one.
301         (let ((mml-boundary (mml-compute-boundary cont)))
302           (insert (format "Content-Type: multipart/%s; boundary=\"%s\"\n"
303                           type mml-boundary))
304           (setq cont (cddr cont))
305           (while cont
306             (insert "\n--" mml-boundary "\n")
307             (mml-generate-mime-1 (pop cont)))
308           (insert "\n--" mml-boundary "--\n")))))
309    (t
310     (error "Invalid element: %S" cont))))
311
312 (defun mml-compute-boundary (cont)
313   "Return a unique boundary that does not exist in CONT."
314   (let ((mml-boundary (funcall mml-boundary-function
315                                (incf mml-multipart-number))))
316     ;; This function tries again and again until it has found
317     ;; a unique boundary.
318     (while (not (catch 'not-unique
319                   (mml-compute-boundary-1 cont))))
320     mml-boundary))
321
322 (defun mml-compute-boundary-1 (cont)
323   (let (filename)
324     (cond
325      ((eq (car cont) 'part)
326       (with-temp-buffer
327         (cond
328          ((cdr (assq 'buffer cont))
329           (insert-buffer-substring (cdr (assq 'buffer cont))))
330          ((and (setq filename (cdr (assq 'filename cont)))
331                (not (equal (cdr (assq 'nofile cont)) "yes")))
332           (mm-insert-file-contents filename))
333          (t
334           (insert (cdr (assq 'contents cont)))))
335         (goto-char (point-min))
336         (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
337                                  nil t)
338           (setq mml-boundary (funcall mml-boundary-function
339                                       (incf mml-multipart-number)))
340           (throw 'not-unique nil))))
341      ((eq (car cont) 'multipart)
342       (mapcar 'mml-compute-boundary-1 (cddr cont))))
343     t))
344
345 (defun mml-make-boundary (number)
346   (concat (make-string (% number 60) ?=)
347           (if (> number 17)
348               (format "%x" number)
349             "")
350           mml-base-boundary))
351
352 (defun mml-make-string (num string)
353   (let ((out ""))
354     (while (not (zerop (decf num)))
355       (setq out (concat out string)))
356     out))
357
358 (defun mml-insert-mime-headers (cont type charset encoding)
359   (let (parameters disposition description)
360     (setq parameters
361           (mml-parameter-string
362            cont '(name access-type expiration size permission)))
363     (when (or charset
364               parameters
365               (not (equal type "text/plain")))
366       (when (consp charset)
367         (error
368          "Can't encode a part with several charsets."))
369       (insert "Content-Type: " type)
370       (when charset
371         (insert "; " (mail-header-encode-parameter
372                       "charset" (symbol-name charset))))
373       (when parameters
374         (mml-insert-parameter-string
375          cont '(name access-type expiration size permission)))
376       (insert "\n"))
377     (setq parameters
378           (mml-parameter-string
379            cont '(filename creation-date modification-date read-date)))
380     (when (or (setq disposition (cdr (assq 'disposition cont)))
381               parameters)
382       (insert "Content-Disposition: " (or disposition "inline"))
383       (when parameters
384         (mml-insert-parameter-string
385          cont '(filename creation-date modification-date read-date)))
386       (insert "\n"))
387     (unless (eq encoding '7bit)
388       (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
389     (when (setq description (cdr (assq 'description cont)))
390       (insert "Content-Description: "
391               (mail-encode-encoded-word-string description) "\n"))))
392
393 (defun mml-parameter-string (cont types)
394   (let ((string "")
395         value type)
396     (while (setq type (pop types))
397       (when (setq value (cdr (assq type cont)))
398         ;; Strip directory component from the filename parameter.
399         (when (eq type 'filename)
400           (setq value (file-name-nondirectory value)))
401         (setq string (concat string "; "
402                              (mail-header-encode-parameter
403                               (symbol-name type) value)))))
404     (when (not (zerop (length string)))
405       string)))
406
407 (defun mml-insert-parameter-string (cont types)
408   (let (value type)
409     (while (setq type (pop types))
410       (when (setq value (cdr (assq type cont)))
411         ;; Strip directory component from the filename parameter.
412         (when (eq type 'filename)
413           (setq value (file-name-nondirectory value)))
414         (mml-insert-parameter
415          (mail-header-encode-parameter
416           (symbol-name type) value))))))
417
418 (defvar ange-ftp-path-format)
419 (defvar efs-path-regexp)
420 (defun mml-parse-file-name (path)
421   (if (if (boundp 'efs-path-regexp)
422           (string-match efs-path-regexp path)
423         (if (boundp 'ange-ftp-path-format)
424             (string-match (car ange-ftp-path-format))))
425       (list (match-string 1 path) (match-string 2 path)
426             (substring path (1+ (match-end 2))))
427     path))
428
429 (defun mml-insert-buffer (buffer)
430   "Insert BUFFER at point and quote any MML markup."
431   (save-restriction
432     (narrow-to-region (point) (point))
433     (insert-buffer-substring buffer)
434     (mml-quote-region (point-min) (point-max))
435     (goto-char (point-max))))
436
437 ;;;
438 ;;; Transforming MIME to MML
439 ;;;
440
441 (defun mime-to-mml ()
442   "Translate the current buffer (which should be a message) into MML."
443   ;; First decode the head.
444   (save-restriction
445     (message-narrow-to-head)
446     (mail-decode-encoded-word-region (point-min) (point-max)))
447   (let ((handles (mm-dissect-buffer t)))
448     (goto-char (point-min))
449     (search-forward "\n\n" nil t)
450     (delete-region (point) (point-max))
451     (if (stringp (car handles))
452         (mml-insert-mime handles)
453       (mml-insert-mime handles t))
454     (mm-destroy-parts handles)))
455
456 (defun mml-to-mime ()
457   "Translate the current buffer from MML to MIME."
458   (message-encode-message-body)
459   (save-restriction
460     (message-narrow-to-headers-or-head)
461     (mail-encode-encoded-word-buffer)))
462
463 (defun mml-insert-mime (handle &optional no-markup)
464   (let (textp buffer)
465     ;; Determine type and stuff.
466     (unless (stringp (car handle))
467       (unless (setq textp (equal (mm-handle-media-supertype handle)
468                                  "text"))
469         (save-excursion
470           (set-buffer (setq buffer (generate-new-buffer " *mml*")))
471           (mm-insert-part handle))))
472     (unless no-markup
473       (mml-insert-mml-markup handle buffer textp))
474     (cond
475      ((stringp (car handle))
476       (mapcar 'mml-insert-mime (cdr handle))
477       (insert "<#/multipart>\n"))
478      (textp
479       (let ((text (mm-get-part handle))
480             (charset (mail-content-type-get
481                       (mm-handle-type handle) 'charset)))
482         (insert (mm-decode-string text charset)))
483       (goto-char (point-max)))
484      (t
485       (insert "<#/part>\n")))))
486
487 (defun mml-insert-mml-markup (handle &optional buffer nofile)
488   "Take a MIME handle and insert an MML tag."
489   (if (stringp (car handle))
490       (insert "<#multipart type=" (mm-handle-media-subtype handle)
491               ">\n")
492     (insert "<#part type=" (mm-handle-media-type handle))
493     (dolist (elem (append (cdr (mm-handle-type handle))
494                           (cdr (mm-handle-disposition handle))))
495       (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\""))
496     (when (mm-handle-disposition handle)
497       (insert " disposition=" (car (mm-handle-disposition handle))))
498     (when buffer
499       (insert " buffer=\"" (buffer-name buffer) "\""))
500     (when nofile
501       (insert " nofile=yes"))
502     (when (mm-handle-description handle)
503       (insert " description=\"" (mm-handle-description handle) "\""))
504     (insert ">\n")))
505
506 (defun mml-insert-parameter (&rest parameters)
507   "Insert PARAMETERS in a nice way."
508   (dolist (param parameters)
509     (insert ";")
510     (let ((point (point)))
511       (insert " " param)
512       (when (> (current-column) 71)
513         (goto-char point)
514         (insert "\n ")
515         (end-of-line)))))
516
517 ;;;
518 ;;; Mode for inserting and editing MML forms
519 ;;;
520
521 (defvar mml-mode-map
522   (let ((map (make-sparse-keymap))
523         (main (make-sparse-keymap)))
524     (define-key map "f" 'mml-attach-file)
525     (define-key map "b" 'mml-attach-buffer)
526     (define-key map "e" 'mml-attach-external)
527     (define-key map "q" 'mml-quote-region)
528     (define-key map "m" 'mml-insert-multipart)
529     (define-key map "p" 'mml-insert-part)
530     (define-key map "v" 'mml-validate)
531     (define-key map "P" 'mml-preview)
532     (define-key map "n" 'mml-narrow-to-part)
533     (define-key main "\M-m" map)
534     main))
535
536 (easy-menu-define
537  mml-menu mml-mode-map ""
538  '("MML"
539    ("Attach"
540     ["File" mml-attach-file t]
541     ["Buffer" mml-attach-buffer t]
542     ["External" mml-attach-external t])
543    ("Insert"
544     ["Multipart" mml-insert-multipart t]
545     ["Part" mml-insert-part t])
546    ["Narrow" mml-narrow-to-part t]
547    ["Quote" mml-quote-region t]
548    ["Validate" mml-validate t]
549    ["Preview" mml-preview t]))
550
551 (defvar mml-mode nil
552   "Minor mode for editing MML.")
553
554 (defun mml-mode (&optional arg)
555   "Minor mode for editing MML.
556
557 \\{mml-mode-map}"
558   (interactive "P")
559   (if (not (set (make-local-variable 'mml-mode)
560                 (if (null arg) (not mml-mode)
561                   (> (prefix-numeric-value arg) 0))))
562       nil
563     (set (make-local-variable 'mml-mode) t)
564     (unless (assq 'mml-mode minor-mode-alist)
565       (push `(mml-mode " MML") minor-mode-alist))
566     (unless (assq 'mml-mode minor-mode-map-alist)
567       (push (cons 'mml-mode mml-mode-map)
568             minor-mode-map-alist)))
569   (run-hooks 'mml-mode-hook))
570
571 ;;;
572 ;;; Helper functions for reading MIME stuff from the minibuffer and
573 ;;; inserting stuff to the buffer.
574 ;;;
575
576 (defun mml-minibuffer-read-file (prompt)
577   (let ((file (read-file-name prompt nil nil t)))
578     ;; Prevent some common errors.  This is inspired by similar code in
579     ;; VM.
580     (when (file-directory-p file)
581       (error "%s is a directory, cannot attach" file))
582     (unless (file-exists-p file)
583       (error "No such file: %s" file))
584     (unless (file-readable-p file)
585       (error "Permission denied: %s" file))
586     file))
587
588 (defun mml-minibuffer-read-type (name &optional default)
589   (let* ((default (or default
590                       (mm-default-file-encoding name)
591                       ;; Perhaps here we should check what the file
592                       ;; looks like, and offer text/plain if it looks
593                       ;; like text/plain.
594                       "application/octet-stream"))
595          (string (completing-read
596                   (format "Content type (default %s): " default)
597                   (mapcar
598                    'list
599                    (delete-duplicates
600                     (nconc
601                      (mapcar (lambda (m) (cdr m))
602                              mailcap-mime-extensions)
603                      (apply
604                       'nconc
605                       (mapcar
606                        (lambda (l)
607                          (delq nil
608                                (mapcar
609                                 (lambda (m)
610                                   (let ((type (cdr (assq 'type (cdr m)))))
611                                     (if (equal (cadr (split-string type "/"))
612                                                "*")
613                                         nil
614                                       type)))
615                                 (cdr l))))
616                        mailcap-mime-data)))
617                     :test 'equal)))))
618     (if (not (equal string ""))
619         string
620       default)))
621
622 (defun mml-minibuffer-read-description ()
623   (let ((description (read-string "One line description: ")))
624     (when (string-match "\\`[ \t]*\\'" description)
625       (setq description nil))
626     description))
627
628 (defun mml-quote-region (beg end)
629   "Quote the MML tags in the region."
630   (interactive "r")
631   (save-excursion
632     (save-restriction
633       ;; Temporarily narrow the region to defend from changes
634       ;; invalidating END.
635       (narrow-to-region beg end)
636       (goto-char (point-min))
637       ;; Quote parts.
638       (while (re-search-forward
639               "<#/?!*\\(multipart\\|part\\|external\\)" nil t)
640         ;; Insert ! after the #.
641         (goto-char (+ (match-beginning 0) 2))
642         (insert "!")))))
643
644 (defun mml-insert-tag (name &rest plist)
645   "Insert an MML tag described by NAME and PLIST."
646   (when (symbolp name)
647     (setq name (symbol-name name)))
648   (insert "<#" name)
649   (while plist
650     (let ((key (pop plist))
651           (value (pop plist)))
652       (when value
653         ;; Quote VALUE if it contains suspicious characters.
654         (when (string-match "[\"\\~/* \t\n]" value)
655           (setq value (prin1-to-string value)))
656         (insert (format " %s=%s" key value)))))
657   (insert ">\n"))
658
659 (defun mml-insert-empty-tag (name &rest plist)
660   "Insert an empty MML tag described by NAME and PLIST."
661   (when (symbolp name)
662     (setq name (symbol-name name)))
663   (apply #'mml-insert-tag name plist)
664   (insert "<#/" name ">\n"))
665
666 ;;; Attachment functions.
667
668 (defun mml-attach-file (file &optional type description)
669   "Attach a file to the outgoing MIME message.
670 The file is not inserted or encoded until you send the message with
671 `\\[message-send-and-exit]' or `\\[message-send]'.
672
673 FILE is the name of the file to attach.  TYPE is its content-type, a
674 string of the form \"type/subtype\".  DESCRIPTION is a one-line
675 description of the attachment."
676   (interactive
677    (let* ((file (mml-minibuffer-read-file "Attach file: "))
678           (type (mml-minibuffer-read-type file))
679           (description (mml-minibuffer-read-description)))
680      (list file type description)))
681   (mml-insert-empty-tag 'part 'type type 'filename file
682                         'disposition "attachment" 'description description))
683
684 (defun mml-attach-buffer (buffer &optional type description)
685   "Attach a buffer to the outgoing MIME message.
686 See `mml-attach-file' for details of operation."
687   (interactive
688    (let* ((buffer (read-buffer "Attach buffer: "))
689           (type (mml-minibuffer-read-type buffer "text/plain"))
690           (description (mml-minibuffer-read-description)))
691      (list buffer type description)))
692   (mml-insert-empty-tag 'part 'type type 'buffer buffer
693                         'disposition "attachment" 'description description))
694
695 (defun mml-attach-external (file &optional type description)
696   "Attach an external file into the buffer.
697 FILE is an ange-ftp/efs specification of the part location.
698 TYPE is the MIME type to use."
699   (interactive
700    (let* ((file (mml-minibuffer-read-file "Attach external file: "))
701           (type (mml-minibuffer-read-type file))
702           (description (mml-minibuffer-read-description)))
703      (list file type description)))
704   (mml-insert-empty-tag 'external 'type type 'name file
705                         'disposition "attachment" 'description description))
706
707 (defun mml-insert-multipart (&optional type)
708   (interactive (list (completing-read "Multipart type (default mixed): "
709                                       '(("mixed") ("alternative") ("digest") ("parallel")
710                                         ("signed") ("encrypted"))
711                                       nil nil "mixed")))
712   (or type
713       (setq type "mixed"))
714   (mml-insert-empty-tag "multipart" 'type type)
715   (forward-line -1))
716
717 (defun mml-insert-part (&optional type)
718   (interactive
719    (list (mml-minibuffer-read-type "")))
720   (mml-insert-tag 'part 'type type 'disposition "inline")
721   (forward-line -1))
722
723 (defun mml-preview (&optional raw)
724   "Display current buffer with Gnus, in a new buffer.
725 If RAW, don't highlight the article."
726   (interactive "P")
727   (let ((buf (current-buffer)))
728     (switch-to-buffer (get-buffer-create 
729                        (concat (if raw "*Raw MIME preview of "
730                                  "*MIME preview of ") (buffer-name))))
731     (erase-buffer)
732     (insert-buffer buf)
733     (if (re-search-forward
734          (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
735         (replace-match "\n"))
736     (mml-to-mime)
737     (unless raw
738       (run-hooks 'gnus-article-decode-hook)
739       (let ((gnus-newsgroup-name "dummy"))
740         (gnus-article-prepare-display)))
741     (fundamental-mode)
742     (setq buffer-read-only t)
743     (goto-char (point-min))))
744
745 (defun mml-validate ()
746   "Validate the current MML document."
747   (interactive)
748   (mml-parse))
749
750 (provide 'mml)
751
752 ;;; mml.el ends here