1 ;;; mml.el --- A package for parsing and validating MML documents
2 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
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)
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.
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.
30 (eval-when-compile 'cl)
33 (autoload 'message-make-message-id "message")
34 (autoload 'gnus-setup-posting-charset "gnus-msg")
35 (autoload 'message-fetch-field "message")
36 (autoload 'message-posting-charset "message"))
38 (defvar mml-generate-multipart-alist nil
39 "*Alist of multipart generation functions.
40 Each entry has the form (NAME . FUNCTION), where
41 NAME is a string containing the name of the part (without the
42 leading \"/multipart/\"),
43 FUNCTION is a Lisp function which is called to generate the part.
45 The Lisp function has to supply the appropriate MIME headers and the
46 contents of this part.")
48 (defvar mml-syntax-table
49 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
50 (modify-syntax-entry ?\\ "/" table)
51 (modify-syntax-entry ?< "(" table)
52 (modify-syntax-entry ?> ")" table)
53 (modify-syntax-entry ?@ "w" table)
54 (modify-syntax-entry ?/ "w" table)
55 (modify-syntax-entry ?= " " table)
56 (modify-syntax-entry ?* " " table)
57 (modify-syntax-entry ?\; " " table)
58 (modify-syntax-entry ?\' " " table)
61 (defvar mml-boundary-function 'mml-make-boundary
62 "A function called to suggest a boundary.
63 The function may be called several times, and should try to make a new
64 suggestion each time. The function is called with one parameter,
65 which is a number that says how many times the function has been
66 called for this message.")
68 (defvar mml-confirmation-set nil
69 "A list of symbols, each of which disables some warning.
70 `unknown-encoding': always send messages contain characters with
71 unknown encoding; `use-ascii': always use ASCII for those characters
72 with unknown encoding; `multipart': always send messages with more than
75 (defvar mml-generate-mime-preprocess-function nil
76 "A function called before generating a mime part.
77 The function is called with one parameter, which is the part to be
80 (defvar mml-generate-mime-postprocess-function nil
81 "A function called after generating a mime part.
82 The function is called with one parameter, which is the generated part.")
84 (defvar mml-generate-default-type "text/plain")
86 (defvar mml-buffer-list nil)
88 (defun mml-generate-new-buffer (name)
89 (let ((buf (generate-new-buffer name)))
90 (push buf mml-buffer-list)
93 (defun mml-destroy-buffers ()
94 (let (kill-buffer-hook)
95 (mapcar 'kill-buffer mml-buffer-list)
96 (setq mml-buffer-list nil)))
99 "Parse the current buffer as an MML document."
100 (goto-char (point-min))
101 (let ((table (syntax-table)))
104 (set-syntax-table mml-syntax-table)
106 (set-syntax-table table))))
108 (defun mml-parse-1 ()
109 "Parse the current buffer as an MML document."
110 (let (struct tag point contents charsets warn use-ascii no-markup-p)
111 (while (and (not (eobp))
112 (not (looking-at "<#/multipart")))
114 ((looking-at "<#multipart")
115 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
116 ((looking-at "<#external")
117 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
120 (if (or (looking-at "<#part") (looking-at "<#mml"))
121 (setq tag (mml-read-tag)
124 (setq tag (list 'part '(type . "text/plain"))
128 contents (mml-read-part (eq 'mml (car tag)))
129 charsets (mm-find-mime-charset-region point (point)))
130 (when (memq nil charsets)
131 (if (or (memq 'unknown-encoding mml-confirmation-set)
133 "Warning: You message contains characters with unknown encoding. Really send?"))
135 (or (memq 'use-ascii mml-confirmation-set)
136 (y-or-n-p "Use ASCII as charset?")))
137 (setq charsets (delq nil charsets))
139 (error "Edit your message to remove those characters")))
140 (if (< (length charsets) 2)
141 (if (or (not no-markup-p)
142 (string-match "[^ \t\r\n]" contents))
143 ;; Don't create blank parts.
144 (push (nconc tag (list (cons 'contents contents)))
146 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
147 tag point (point) use-ascii)))
149 (not (memq 'multipart mml-confirmation-set))
153 "Warning: Your message contains more than %d parts. Really send? "
155 (error "Edit your message to use only one charset"))
156 (setq struct (nconc nstruct struct)))))))
161 (defun mml-parse-singlepart-with-multiple-charsets
162 (orig-tag beg end &optional use-ascii)
165 (narrow-to-region beg end)
166 (goto-char (point-min))
167 (let ((current (or (mm-mime-charset (mm-charset-after))
168 (and use-ascii 'us-ascii)))
169 charset struct space newline paragraph)
171 (setq charset (mm-mime-charset (mm-charset-after)))
173 ;; The charset remains the same.
174 ((eq charset 'us-ascii))
175 ((or (and use-ascii (not charset))
176 (eq charset current))
180 ;; The initial charset was ascii.
181 ((eq current 'us-ascii)
182 (setq current charset
186 ;; We have a change in charsets.
190 (list (cons 'contents
191 (buffer-substring-no-properties
192 beg (or paragraph newline space (point))))))
194 (setq beg (or paragraph newline space (point))
199 ;; Compute places where it might be nice to break the part.
201 ((memq (following-char) '(? ?\t))
202 (setq space (1+ (point))))
203 ((and (eq (following-char) ?\n)
205 (eq (char-after (1- (point))) ?\n))
206 (setq paragraph (point)))
207 ((eq (following-char) ?\n)
208 (setq newline (1+ (point)))))
210 ;; Do the final part.
211 (unless (= beg (point))
212 (push (append orig-tag
213 (list (cons 'contents
214 (buffer-substring-no-properties
219 (defun mml-read-tag ()
220 "Read a tag and return the contents."
221 (let (contents name elem val)
223 (setq name (buffer-substring-no-properties
224 (point) (progn (forward-sexp 1) (point))))
225 (skip-chars-forward " \t\n")
226 (while (not (looking-at ">"))
227 (setq elem (buffer-substring-no-properties
228 (point) (progn (forward-sexp 1) (point))))
229 (skip-chars-forward "= \t\n")
230 (setq val (buffer-substring-no-properties
231 (point) (progn (forward-sexp 1) (point))))
232 (when (string-match "^\"\\(.*\\)\"$" val)
233 (setq val (match-string 1 val)))
234 (push (cons (intern elem) val) contents)
235 (skip-chars-forward " \t\n"))
237 (skip-chars-forward " \t\n")
238 (cons (intern name) (nreverse contents))))
240 (defun mml-read-part (&optional mml)
241 "Return the buffer up till the next part, multipart or closing part or multipart.
242 If MML is non-nil, return the buffer up till the correspondent mml tag."
243 (let ((beg (point)) (count 1))
244 ;; If the tag ended at the end of the line, we go to the next line.
245 (when (looking-at "[ \t]*\n")
249 (while (and (> count 0) (not (eobp)))
250 (if (re-search-forward "<#\\(/\\)?mml." nil t)
251 (setq count (+ count (if (match-beginning 1) -1 1)))
252 (goto-char (point-max))))
253 (buffer-substring-no-properties beg (if (> count 0)
255 (match-beginning 0))))
256 (if (re-search-forward
257 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
259 (buffer-substring-no-properties beg (match-beginning 0))
260 (if (or (not (match-beginning 1))
261 (equal (match-string 2) "multipart"))
262 (goto-char (match-beginning 0))
263 (when (looking-at "[ \t]*\n")
265 (buffer-substring-no-properties beg (goto-char (point-max)))))))
267 (defvar mml-boundary nil)
268 (defvar mml-base-boundary "-=-=")
269 (defvar mml-multipart-number 0)
271 (defun mml-generate-mime ()
272 "Generate a MIME message based on the current MML document."
273 (let ((cont (mml-parse))
274 (mml-multipart-number mml-multipart-number))
278 (if (and (consp (car cont))
280 (mml-generate-mime-1 (car cont))
281 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
285 (defun mml-generate-mime-1 (cont)
287 (narrow-to-region (point) (point))
288 (if mml-generate-mime-preprocess-function
289 (funcall mml-generate-mime-preprocess-function cont))
291 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
292 (let (coded encoding charset filename type)
293 (setq type (or (cdr (assq 'type cont)) "text/plain"))
294 (if (member (car (split-string type "/")) '("text" "message"))
297 ((cdr (assq 'buffer cont))
298 (insert-buffer-substring (cdr (assq 'buffer cont))))
299 ((and (setq filename (cdr (assq 'filename cont)))
300 (not (equal (cdr (assq 'nofile cont)) "yes")))
301 (mm-insert-file-contents filename))
302 ((eq 'mml (car cont))
303 (insert (cdr (assq 'contents cont))))
306 (narrow-to-region (point) (point))
307 (insert (cdr (assq 'contents cont)))
308 ;; Remove quotes from quoted tags.
309 (goto-char (point-min))
310 (while (re-search-forward
311 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)" nil t)
312 (delete-region (+ (match-beginning 0) 2)
313 (+ (match-beginning 0) 3))))))
315 ((eq (car cont) 'mml)
316 (let ((mml-boundary (funcall mml-boundary-function
317 (incf mml-multipart-number)))
318 (mml-generate-default-type "text/plain"))
320 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
321 ;; ignore 0x1b, it is part of iso-2022-jp
322 (setq encoding (mm-body-7-or-8))))
323 ((string= (car (split-string type "/")) "message")
324 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
325 ;; ignore 0x1b, it is part of iso-2022-jp
326 (setq encoding (mm-body-7-or-8))))
328 (setq charset (mm-encode-body))
329 (setq encoding (mm-body-encoding
330 charset (cdr (assq 'encoding cont))))))
331 (setq coded (buffer-string)))
332 (mm-with-unibyte-buffer
334 ((cdr (assq 'buffer cont))
335 (insert-buffer-substring (cdr (assq 'buffer cont))))
336 ((and (setq filename (cdr (assq 'filename cont)))
337 (not (equal (cdr (assq 'nofile cont)) "yes")))
338 (let ((coding-system-for-read mm-binary-coding-system))
339 (mm-insert-file-contents filename nil nil nil nil t)))
341 (insert (cdr (assq 'contents cont)))))
342 (setq encoding (mm-encode-buffer type)
343 coded (buffer-string))))
344 (mml-insert-mime-headers cont type charset encoding)
347 ((eq (car cont) 'external)
348 (insert "Content-Type: message/external-body")
349 (let ((parameters (mml-parameter-string
350 cont '(expiration size permission)))
351 (name (cdr (assq 'name cont))))
353 (setq name (mml-parse-file-name name))
355 (mml-insert-parameter
356 (mail-header-encode-parameter "name" name)
357 "access-type=local-file")
358 (mml-insert-parameter
359 (mail-header-encode-parameter
360 "name" (file-name-nondirectory (nth 2 name)))
361 (mail-header-encode-parameter "site" (nth 1 name))
362 (mail-header-encode-parameter
363 "directory" (file-name-directory (nth 2 name))))
364 (mml-insert-parameter
365 (concat "access-type="
366 (if (member (nth 0 name) '("ftp@" "anonymous@"))
370 (mml-insert-parameter-string
371 cont '(expiration size permission))))
373 (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
374 (insert "Content-ID: " (message-make-message-id) "\n")
375 (insert "Content-Transfer-Encoding: "
376 (or (cdr (assq 'encoding cont)) "binary"))
378 (insert (or (cdr (assq 'contents cont))))
380 ((eq (car cont) 'multipart)
381 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
382 (mml-generate-default-type (if (equal type "digest")
385 (handler (assoc type mml-generate-multipart-alist)))
387 (funcall (cdr handler) cont)
388 ;; No specific handler. Use default one.
389 (let ((mml-boundary (mml-compute-boundary cont)))
390 (insert (format "Content-Type: multipart/%s; boundary=\"%s\"\n"
392 ;; Skip `multipart' and `type' elements.
393 (setq cont (cddr cont))
395 (insert "\n--" mml-boundary "\n")
396 (mml-generate-mime-1 (pop cont)))
397 (insert "\n--" mml-boundary "--\n")))))
399 (error "Invalid element: %S" cont)))
400 (if mml-generate-mime-postprocess-function
401 (funcall mml-generate-mime-postprocess-function cont))))
403 (defun mml-compute-boundary (cont)
404 "Return a unique boundary that does not exist in CONT."
405 (let ((mml-boundary (funcall mml-boundary-function
406 (incf mml-multipart-number))))
407 ;; This function tries again and again until it has found
408 ;; a unique boundary.
409 (while (not (catch 'not-unique
410 (mml-compute-boundary-1 cont))))
413 (defun mml-compute-boundary-1 (cont)
416 ((eq (car cont) 'part)
419 ((cdr (assq 'buffer cont))
420 (insert-buffer-substring (cdr (assq 'buffer cont))))
421 ((and (setq filename (cdr (assq 'filename cont)))
422 (not (equal (cdr (assq 'nofile cont)) "yes")))
423 (mm-insert-file-contents filename))
425 (insert (cdr (assq 'contents cont)))))
426 (goto-char (point-min))
427 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
429 (setq mml-boundary (funcall mml-boundary-function
430 (incf mml-multipart-number)))
431 (throw 'not-unique nil))))
432 ((eq (car cont) 'multipart)
433 (mapcar 'mml-compute-boundary-1 (cddr cont))))
436 (defun mml-make-boundary (number)
437 (concat (make-string (% number 60) ?=)
443 (defun mml-make-string (num string)
445 (while (not (zerop (decf num)))
446 (setq out (concat out string)))
449 (defun mml-insert-mime-headers (cont type charset encoding)
450 (let (parameters disposition description)
452 (mml-parameter-string
453 cont '(name access-type expiration size permission)))
456 (not (equal type mml-generate-default-type)))
457 (when (consp charset)
459 "Can't encode a part with several charsets."))
460 (insert "Content-Type: " type)
462 (insert "; " (mail-header-encode-parameter
463 "charset" (symbol-name charset))))
465 (mml-insert-parameter-string
466 cont '(name access-type expiration size permission)))
469 (mml-parameter-string
470 cont '(filename creation-date modification-date read-date)))
471 (when (or (setq disposition (cdr (assq 'disposition cont)))
473 (insert "Content-Disposition: " (or disposition "inline"))
475 (mml-insert-parameter-string
476 cont '(filename creation-date modification-date read-date)))
478 (unless (eq encoding '7bit)
479 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
480 (when (setq description (cdr (assq 'description cont)))
481 (insert "Content-Description: "
482 (mail-encode-encoded-word-string description) "\n"))))
484 (defun mml-parameter-string (cont types)
487 (while (setq type (pop types))
488 (when (setq value (cdr (assq type cont)))
489 ;; Strip directory component from the filename parameter.
490 (when (eq type 'filename)
491 (setq value (file-name-nondirectory value)))
492 (setq string (concat string "; "
493 (mail-header-encode-parameter
494 (symbol-name type) value)))))
495 (when (not (zerop (length string)))
498 (defun mml-insert-parameter-string (cont types)
500 (while (setq type (pop types))
501 (when (setq value (cdr (assq type cont)))
502 ;; Strip directory component from the filename parameter.
503 (when (eq type 'filename)
504 (setq value (file-name-nondirectory value)))
505 (mml-insert-parameter
506 (mail-header-encode-parameter
507 (symbol-name type) value))))))
509 (defvar ange-ftp-path-format)
510 (defvar efs-path-regexp)
511 (defun mml-parse-file-name (path)
512 (if (if (boundp 'efs-path-regexp)
513 (string-match efs-path-regexp path)
514 (if (boundp 'ange-ftp-path-format)
515 (string-match (car ange-ftp-path-format))))
516 (list (match-string 1 path) (match-string 2 path)
517 (substring path (1+ (match-end 2))))
520 (defun mml-insert-buffer (buffer)
521 "Insert BUFFER at point and quote any MML markup."
523 (narrow-to-region (point) (point))
524 (insert-buffer-substring buffer)
525 (mml-quote-region (point-min) (point-max))
526 (goto-char (point-max))))
529 ;;; Transforming MIME to MML
532 (defun mime-to-mml ()
533 "Translate the current buffer (which should be a message) into MML."
534 ;; First decode the head.
536 (message-narrow-to-head)
537 (mail-decode-encoded-word-region (point-min) (point-max)))
538 (let ((handles (mm-dissect-buffer t)))
539 (goto-char (point-min))
540 (search-forward "\n\n" nil t)
541 (delete-region (point) (point-max))
542 (if (stringp (car handles))
543 (mml-insert-mime handles)
544 (mml-insert-mime handles t))
545 (mm-destroy-parts handles))
547 (message-narrow-to-head)
548 ;; Remove them, they are confusing.
549 (message-remove-header "Content-Type")
550 (message-remove-header "MIME-Version")
551 (message-remove-header "Content-Transfer-Encoding")))
553 (defun mml-to-mime ()
554 "Translate the current buffer from MML to MIME."
555 (message-encode-message-body)
557 (message-narrow-to-headers-or-head)
558 (let ((mail-parse-charset message-default-charset))
559 (mail-encode-encoded-word-buffer))))
561 (defun mml-insert-mime (handle &optional no-markup)
562 (let (textp buffer mmlp)
563 ;; Determine type and stuff.
564 (unless (stringp (car handle))
565 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
567 (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
568 (mm-insert-part handle)
569 (if (setq mmlp (equal (mm-handle-media-type handle)
573 (mml-insert-mml-markup handle nil t t)
574 (unless (and no-markup
575 (equal (mm-handle-media-type handle) "text/plain"))
576 (mml-insert-mml-markup handle buffer textp)))
579 (insert-buffer buffer)
580 (goto-char (point-max))
581 (insert "<#/mml>\n"))
582 ((stringp (car handle))
583 (mapcar 'mml-insert-mime (cdr handle))
584 (insert "<#/multipart>\n"))
586 (let ((text (mm-get-part handle))
587 (charset (mail-content-type-get
588 (mm-handle-type handle) 'charset)))
589 (insert (mm-decode-string text charset)))
590 (goto-char (point-max)))
592 (insert "<#/part>\n")))))
594 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
595 "Take a MIME handle and insert an MML tag."
596 (if (stringp (car handle))
597 (insert "<#multipart type=" (mm-handle-media-subtype handle)
600 (insert "<#mml type=" (mm-handle-media-type handle))
601 (insert "<#part type=" (mm-handle-media-type handle)))
602 (dolist (elem (append (cdr (mm-handle-type handle))
603 (cdr (mm-handle-disposition handle))))
604 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\""))
605 (when (mm-handle-disposition handle)
606 (insert " disposition=" (car (mm-handle-disposition handle))))
608 (insert " buffer=\"" (buffer-name buffer) "\""))
610 (insert " nofile=yes"))
611 (when (mm-handle-description handle)
612 (insert " description=\"" (mm-handle-description handle) "\""))
615 (defun mml-insert-parameter (&rest parameters)
616 "Insert PARAMETERS in a nice way."
617 (dolist (param parameters)
619 (let ((point (point)))
621 (when (> (current-column) 71)
627 ;;; Mode for inserting and editing MML forms
631 (let ((map (make-sparse-keymap))
632 (main (make-sparse-keymap)))
633 (define-key map "f" 'mml-attach-file)
634 (define-key map "b" 'mml-attach-buffer)
635 (define-key map "e" 'mml-attach-external)
636 (define-key map "q" 'mml-quote-region)
637 (define-key map "m" 'mml-insert-multipart)
638 (define-key map "p" 'mml-insert-part)
639 (define-key map "v" 'mml-validate)
640 (define-key map "P" 'mml-preview)
641 ;;(define-key map "n" 'mml-narrow-to-part)
642 (define-key main "\M-m" map)
646 mml-menu mml-mode-map ""
649 ["File" mml-attach-file t]
650 ["Buffer" mml-attach-buffer t]
651 ["External" mml-attach-external t])
653 ["Multipart" mml-insert-multipart t]
654 ["Part" mml-insert-part t])
655 ;;["Narrow" mml-narrow-to-part t]
656 ["Quote" mml-quote-region t]
657 ["Validate" mml-validate t]
658 ["Preview" mml-preview t]))
661 "Minor mode for editing MML.")
663 (defun mml-mode (&optional arg)
664 "Minor mode for editing MML.
668 (if (not (set (make-local-variable 'mml-mode)
669 (if (null arg) (not mml-mode)
670 (> (prefix-numeric-value arg) 0))))
672 (set (make-local-variable 'mml-mode) t)
673 (unless (assq 'mml-mode minor-mode-alist)
674 (push `(mml-mode " MML") minor-mode-alist))
675 (unless (assq 'mml-mode minor-mode-map-alist)
676 (push (cons 'mml-mode mml-mode-map)
677 minor-mode-map-alist)))
678 (run-hooks 'mml-mode-hook))
681 ;;; Helper functions for reading MIME stuff from the minibuffer and
682 ;;; inserting stuff to the buffer.
685 (defun mml-minibuffer-read-file (prompt)
686 (let ((file (read-file-name prompt nil nil t)))
687 ;; Prevent some common errors. This is inspired by similar code in
689 (when (file-directory-p file)
690 (error "%s is a directory, cannot attach" file))
691 (unless (file-exists-p file)
692 (error "No such file: %s" file))
693 (unless (file-readable-p file)
694 (error "Permission denied: %s" file))
697 (defun mml-minibuffer-read-type (name &optional default)
698 (let* ((default (or default
699 (mm-default-file-encoding name)
700 ;; Perhaps here we should check what the file
701 ;; looks like, and offer text/plain if it looks
703 "application/octet-stream"))
704 (string (completing-read
705 (format "Content type (default %s): " default)
708 (mm-delete-duplicates
710 (mapcar 'cdr mailcap-mime-extensions)
718 (let ((type (cdr (assq 'type (cdr m)))))
719 (if (equal (cadr (split-string type "/"))
724 mailcap-mime-data))))))))
725 (if (not (equal string ""))
729 (defun mml-minibuffer-read-description ()
730 (let ((description (read-string "One line description: ")))
731 (when (string-match "\\`[ \t]*\\'" description)
732 (setq description nil))
735 (defun mml-quote-region (beg end)
736 "Quote the MML tags in the region."
740 ;; Temporarily narrow the region to defend from changes
742 (narrow-to-region beg end)
743 (goto-char (point-min))
745 (while (re-search-forward
746 "<#/?!*\\(multipart\\|part\\|external\\|mml\\)" nil t)
747 ;; Insert ! after the #.
748 (goto-char (+ (match-beginning 0) 2))
751 (defun mml-insert-tag (name &rest plist)
752 "Insert an MML tag described by NAME and PLIST."
754 (setq name (symbol-name name)))
757 (let ((key (pop plist))
760 ;; Quote VALUE if it contains suspicious characters.
761 (when (string-match "[\"'\\~/*;() \t\n]" value)
762 (setq value (prin1-to-string value)))
763 (insert (format " %s=%s" key value)))))
766 (defun mml-insert-empty-tag (name &rest plist)
767 "Insert an empty MML tag described by NAME and PLIST."
769 (setq name (symbol-name name)))
770 (apply #'mml-insert-tag name plist)
771 (insert "<#/" name ">\n"))
773 ;;; Attachment functions.
775 (defun mml-attach-file (file &optional type description)
776 "Attach a file to the outgoing MIME message.
777 The file is not inserted or encoded until you send the message with
778 `\\[message-send-and-exit]' or `\\[message-send]'.
780 FILE is the name of the file to attach. TYPE is its content-type, a
781 string of the form \"type/subtype\". DESCRIPTION is a one-line
782 description of the attachment."
784 (let* ((file (mml-minibuffer-read-file "Attach file: "))
785 (type (mml-minibuffer-read-type file))
786 (description (mml-minibuffer-read-description)))
787 (list file type description)))
788 (mml-insert-empty-tag 'part 'type type 'filename file
789 'disposition "attachment" 'description description))
791 (defun mml-attach-buffer (buffer &optional type description)
792 "Attach a buffer to the outgoing MIME message.
793 See `mml-attach-file' for details of operation."
795 (let* ((buffer (read-buffer "Attach buffer: "))
796 (type (mml-minibuffer-read-type buffer "text/plain"))
797 (description (mml-minibuffer-read-description)))
798 (list buffer type description)))
799 (mml-insert-empty-tag 'part 'type type 'buffer buffer
800 'disposition "attachment" 'description description))
802 (defun mml-attach-external (file &optional type description)
803 "Attach an external file into the buffer.
804 FILE is an ange-ftp/efs specification of the part location.
805 TYPE is the MIME type to use."
807 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
808 (type (mml-minibuffer-read-type file))
809 (description (mml-minibuffer-read-description)))
810 (list file type description)))
811 (mml-insert-empty-tag 'external 'type type 'name file
812 'disposition "attachment" 'description description))
814 (defun mml-insert-multipart (&optional type)
815 (interactive (list (completing-read "Multipart type (default mixed): "
816 '(("mixed") ("alternative") ("digest") ("parallel")
817 ("signed") ("encrypted"))
821 (mml-insert-empty-tag "multipart" 'type type)
824 (defun mml-insert-part (&optional type)
826 (list (mml-minibuffer-read-type "")))
827 (mml-insert-tag 'part 'type type 'disposition "inline")
830 (defun mml-preview (&optional raw)
831 "Display current buffer with Gnus, in a new buffer.
832 If RAW, don't highlight the article."
834 (let ((buf (current-buffer))
835 (message-posting-charset (or (gnus-setup-posting-charset
837 (message-narrow-to-headers-or-head)
838 (message-fetch-field "Newsgroups")))
839 message-posting-charset)))
840 (switch-to-buffer (get-buffer-create
841 (concat (if raw "*Raw MIME preview of "
842 "*MIME preview of ") (buffer-name))))
845 (if (re-search-forward
846 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
847 (replace-match "\n"))
850 (mm-disable-multibyte)
851 (let ((gnus-newsgroup-charset (car message-posting-charset)))
852 (run-hooks 'gnus-article-decode-hook)
853 (let ((gnus-newsgroup-name "dummy"))
854 (gnus-article-prepare-display))))
856 (setq buffer-read-only t)
857 (goto-char (point-min))))
859 (defun mml-validate ()
860 "Validate the current MML document."