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