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