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