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