1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,1994,1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; Created: 1994/08/21 renamed from mime.el
9 ;; Renamed: 1997/2/21 from tm-edit.el
10 ;; Keywords: MIME, multimedia, multilingual, mail, news
12 ;; This file is part of SEMI (Sophisticated Emacs MIME Interfaces).
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; This is an Emacs minor mode for editing Internet multimedia
32 ;; messages formatted in MIME (RFC 2045, 2046, 2047, 2048 and 2049).
33 ;; All messages in this mode are composed in the tagged MIME format,
34 ;; that are described in the following examples. The messages
35 ;; composed in the tagged MIME format are automatically translated
36 ;; into a MIME compliant message when exiting the mode.
38 ;; Mule (multilingual feature of Emacs 20 and multilingual extension
39 ;; for XEmacs 20) has a capability of handling multilingual text in
40 ;; limited ISO-2022 manner that is based on early experiences in
41 ;; Japanese Internet community and resulted in RFC 1468 (ISO-2022-JP
42 ;; charset for MIME). In order to enable multilingual capability in
43 ;; single text message in MIME, charset of multilingual text written
44 ;; in Mule is declared as either `ISO-2022-JP-2' [RFC 1554]. Mule is
45 ;; required for reading the such messages.
47 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
48 ;; News mode. First of all, you need the following autoload
49 ;; definition to load mime-edit-mode automatically:
51 ;; (autoload 'turn-on-mime-edit "mime-edit"
52 ;; "Minor mode for editing MIME message." t)
54 ;; In case of Mail mode (includes VM mode), you need the following
57 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
58 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
60 ;; In case of MH-E, you need the following hook definition:
62 ;; (add-hook 'mh-letter-mode-hook
65 ;; (turn-on-mime-edit)
66 ;; (make-local-variable 'mail-header-separator)
67 ;; (setq mail-header-separator "--------")
69 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
71 ;; In case of News mode, you need the following hook definition:
73 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
74 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
76 ;; In case of Emacs 19, it is possible to emphasize the message tags
77 ;; using font-lock mode as follows:
79 ;; (add-hook 'mime-edit-mode-hook
83 ;; (setq font-lock-keywords (list mime-edit-tag-regexp))
86 ;; The message tag looks like:
88 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
90 ;; The tagged MIME message examples:
92 ;; This is a conventional plain text. It should be translated into
96 ;; This is also a plain text. But, it is explicitly specified as is.
97 ;;--[[text/plain; charset=ISO-8859-1]]
98 ;; This is also a plain text. But charset is specified as iso-8859-1.
100 ;; ¡Hola! Buenos días. ¿Cómo está usted?
101 ;;--[[text/enriched]]
102 ;; <center>This is a richtext.</center>
104 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
106 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
112 (require 'mail-utils)
115 (require 'eword-encode)
123 (defconst mime-edit-version-string
125 (mapconcat #'number-to-string (cdr semi-version) ".")
126 " - \"" (car semi-version) "\""))
132 (defgroup mime-edit nil
136 (defcustom mime-ignore-preceding-spaces nil
137 "*Ignore preceding white spaces if non-nil."
141 (defcustom mime-ignore-trailing-spaces nil
142 "*Ignore trailing white spaces if non-nil."
146 (defcustom mime-ignore-same-text-tag t
147 "*Ignore preceding text content-type tag that is same with new one.
148 If non-nil, the text tag is not inserted unless something different."
152 (defcustom mime-auto-hide-body t
153 "*Hide non-textual body encoded in base64 after insertion if non-nil."
157 (defcustom mime-edit-voice-recorder
158 (function mime-edit-voice-recorder-for-sun)
159 "*Function to record a voice message and encode it."
163 (defcustom mime-edit-mode-hook nil
164 "*Hook called when enter MIME mode."
168 (defcustom mime-edit-translate-hook nil
169 "*Hook called before translating into a MIME compliant message.
170 To insert a signature file automatically, call the function
171 `mime-edit-insert-signature' from this hook."
175 (defcustom mime-edit-exit-hook nil
176 "*Hook called when exit MIME mode."
180 (defvar mime-content-types
182 ;; Charset parameter need not to be specified, since it is
183 ;; defined automatically while translation.
185 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
188 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
191 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
194 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
197 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
205 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
206 ("directory" "/pub/GNU/elisp/mime")
208 ("mode" "image" "ascii" "local8"))
213 ("mode" "image" "ascii" "local8"))
214 ("tftp" ("site") ("name"))
215 ("afs" ("site") ("name"))
216 ("local-file" ("site") ("name"))
217 ("mail-server" ("server" "ftpmail@nic.karrn.ad.jp"))
222 ("octet-stream" ("type" "" "tar" "shar"))
224 ("x-kiss" ("x-cnf")))
238 "*Alist of content-type, subtype, parameters and its values.")
240 (defvar mime-file-types
242 "text" "richtext" nil
250 "application" "postscript" nil
252 "attachment" (("filename" . file))
257 "inline" (("filename" . file))
262 "inline" (("filename" . file))
267 "inline" (("filename" . file))
272 "inline" (("filename" . file))
277 "inline" (("filename" . file))
282 "inline" (("filename" . file))
287 "inline" (("filename" . file))
292 "inline" (("filename" . file))
297 "attachment" (("filename" . file))
302 "attachment" (("filename" . file))
305 "application" "octet-stream" (("type" . "emacs-lisp"))
307 "attachment" (("filename" . file))
310 "application" "octet-stream" (("type" . "common-lisp"))
312 "attachment" (("filename" . file))
315 "application" "octet-stream" (("type" . "tar+gzip"))
317 "attachment" (("filename" . file))
320 "application" "octet-stream" (("type" . "tar+gzip"))
322 "attachment" (("filename" . file))
325 "application" "octet-stream" (("type" . "tar+compress"))
327 "attachment" (("filename" . file))
330 "application" "octet-stream" (("type" . "tar+compress"))
332 "attachment" (("filename" . file))
335 "application" "octet-stream" (("type" . "gzip"))
337 "attachment" (("filename" . file))
340 "application" "octet-stream" (("type" . "compress"))
342 "attachment" (("filename" . file))
345 "application" "octet-stream" (("type" . "lha"))
347 "attachment" (("filename" . file))
350 "application" "zip" nil
352 "attachment" (("filename" . file))
355 "application" "octet-stream" (("type" . "patch"))
357 "attachment" (("filename" . file))
360 "application" "octet-stream" (("type" . "patch"))
362 "attachment" (("filename" . file))
365 "text" "plain" nil nil)
367 "application" "octet-stream" nil
369 "attachment" (("filename" . file))
372 "*Alist of file name, types, parameters, and default encoding.
373 If encoding is nil, it is determined from its contents.")
376 ;;; @@ about charset, encoding and transfer-level
379 (defvar mime-charset-type-list
381 (iso-8859-1 8 "quoted-printable")
382 (iso-8859-2 8 "quoted-printable")
383 (iso-8859-3 8 "quoted-printable")
384 (iso-8859-4 8 "quoted-printable")
385 (iso-8859-5 8 "quoted-printable")
386 (koi8-r 8 "quoted-printable")
387 (iso-8859-7 8 "quoted-printable")
388 (iso-8859-8 8 "quoted-printable")
389 (iso-8859-9 8 "quoted-printable")
390 (iso-2022-jp 7 "base64")
391 (iso-2022-kr 7 "base64")
393 (cn-gb2312 8 "quoted-printable")
395 (gb2312 8 "quoted-printable")
397 (iso-2022-jp-2 7 "base64")
398 (iso-2022-int-1 7 "base64")
401 (defvar mime-transfer-level 7
402 "*A number of network transfer level. It should be bigger than 7.")
403 (make-variable-buffer-local 'mime-transfer-level)
405 (defsubst mime-encoding-name (transfer-level &optional not-omit)
406 (cond ((> transfer-level 8) "binary")
407 ((= transfer-level 8) "8bit")
411 (defvar mime-transfer-level-string
412 (mime-encoding-name mime-transfer-level 'not-omit)
413 "A string formatted version of mime-transfer-level")
414 (make-variable-buffer-local 'mime-transfer-level-string)
416 (defun mime-make-charset-default-encoding-alist (transfer-level)
418 (lambda (charset-type)
419 (let ((charset (car charset-type))
420 (type (nth 1 charset-type))
421 (encoding (nth 2 charset-type))
423 (if (<= type transfer-level)
424 (cons charset (mime-encoding-name type))
425 (cons charset encoding)
427 mime-charset-type-list))
429 (defvar mime-edit-charset-default-encoding-alist
430 (mime-make-charset-default-encoding-alist mime-transfer-level))
431 (make-variable-buffer-local 'mime-edit-charset-default-encoding-alist)
434 ;;; @@ about message inserting
437 (defvar mime-edit-yank-ignored-field-list
438 '("Received" "Approved" "Path" "Replied" "Status"
439 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
440 "Delete these fields from original message when it is inserted
441 as message/rfc822 part.
442 Each elements are regexp of field-name.")
444 (defvar mime-edit-yank-ignored-field-regexp
446 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
449 (defvar mime-edit-message-inserter-alist nil)
450 (defvar mime-edit-mail-inserter-alist nil)
453 ;;; @@ about message splitting
456 (defcustom mime-edit-split-message t
457 "*Split large message if it is non-nil."
461 (defcustom mime-edit-message-default-max-lines 1000
462 "*Default maximum lines of a message."
466 (defcustom mime-edit-message-max-lines-alist
467 '((news-reply-mode . 500))
468 "Alist of major-mode vs maximum lines of a message.
469 If it is not specified for a major-mode,
470 `mime-edit-message-default-max-lines' is used."
474 (defconst mime-edit-split-ignored-field-regexp
475 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\)")
477 (defvar mime-edit-split-blind-field-regexp
478 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)")
480 (defvar mime-edit-split-message-sender-alist nil)
482 (defvar mime-edit-news-reply-mode-server-running nil)
488 (defvar mime-edit-signing-type 'pgp-elkins
489 "*PGP signing type (pgp-elkins, pgp-kazu or nil).")
491 (defvar mime-edit-encrypting-type 'pgp-elkins
492 "*PGP encrypting type (pgp-elkins, pgp-kazu or nil).")
498 (defconst mime-edit-single-part-tag-regexp
499 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
500 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
502 (defconst mime-edit-quoted-single-part-tag-regexp
503 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
505 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
507 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
509 (defconst mime-edit-beginning-tag-regexp
510 (regexp-or mime-edit-single-part-tag-regexp
511 mime-edit-multipart-beginning-regexp))
513 (defconst mime-edit-end-tag-regexp
514 (regexp-or mime-edit-single-part-tag-regexp
515 mime-edit-multipart-end-regexp))
517 (defconst mime-edit-tag-regexp
518 (regexp-or mime-edit-single-part-tag-regexp
519 mime-edit-multipart-beginning-regexp
520 mime-edit-multipart-end-regexp))
522 (defvar mime-tag-format "--[[%s]]"
523 "*Control-string making a MIME tag.")
525 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
526 "*Control-string making a MIME tag with encoding.")
529 ;;; @@ multipart boundary
532 (defvar mime-multipart-boundary "Multipart"
533 "*Boundary of a multipart message.")
536 ;;; @@ optional header fields
539 (defvar mime-edit-insert-x-emacs-field t
540 "*If non-nil, insert X-Emacs header field.")
542 (defvar mime-edit-x-emacs-value
543 (if (featurep 'xemacs)
544 (concat emacs-version (if (featurep 'mule)
547 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
548 (substring emacs-version 0 (match-beginning 0))
552 (if enable-multibyte-characters
553 (concat ", MULE " mule-version)
554 " (with raw setting)")
555 (if (featurep 'meadow)
556 (concat ", " (Meadow-version))
559 "Body of X-Emacs field.
560 If variable `mime-edit-insert-x-emacs-field' is not nil, it is
561 inserted into message header.")
567 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
568 "*Specify MIME tspecials.
569 Tspecials means any character that matches with it in header must be quoted.")
571 (defconst mime-edit-mime-version-value
572 (concat "1.0 (generated by " mime-edit-version-string ")")
573 "MIME version number.")
575 (defconst mime-edit-mime-version-field-for-message/partial
576 (concat "MIME-Version: 1.0 (split by " mime-edit-version-string ")\n")
577 "MIME version field for message/partial.")
580 ;;; @ keymap and menu
583 (defvar mime-edit-mode-flag nil)
584 (make-variable-buffer-local 'mime-edit-mode-flag)
586 (defvar mime-edit-mode-map (make-sparse-keymap)
587 "Keymap for MIME-Edit mode commands.")
589 (define-key mime-edit-mode-map
590 "\C-c\C-x\C-t" 'mime-edit-insert-text)
591 (define-key mime-edit-mode-map
592 "\C-c\C-x\C-i" 'mime-edit-insert-file)
593 (define-key mime-edit-mode-map
594 "\C-c\C-x\C-e" 'mime-edit-insert-external)
595 (define-key mime-edit-mode-map
596 "\C-c\C-x\C-v" 'mime-edit-insert-voice)
597 (define-key mime-edit-mode-map
598 "\C-c\C-x\C-y" 'mime-edit-insert-message)
599 (define-key mime-edit-mode-map
600 "\C-c\C-x\C-m" 'mime-edit-insert-mail)
601 (define-key mime-edit-mode-map
602 "\C-c\C-x\C-w" 'mime-edit-insert-signature)
603 (define-key mime-edit-mode-map
604 "\C-c\C-x\C-s" 'mime-edit-insert-signature)
605 (define-key mime-edit-mode-map
606 "\C-c\C-x\C-k" 'mime-edit-insert-key)
607 (define-key mime-edit-mode-map
608 "\C-c\C-xt" 'mime-edit-insert-tag)
610 (define-key mime-edit-mode-map
611 "\C-c\C-m\C-a" 'mime-edit-enclose-alternative-region)
612 (define-key mime-edit-mode-map
613 "\C-c\C-m\C-p" 'mime-edit-enclose-parallel-region)
614 (define-key mime-edit-mode-map
615 "\C-c\C-m\C-m" 'mime-edit-enclose-mixed-region)
616 (define-key mime-edit-mode-map
617 "\C-c\C-m\C-d" 'mime-edit-enclose-digest-region)
618 (define-key mime-edit-mode-map
619 "\C-c\C-m\C-s" 'mime-edit-enclose-signed-region)
620 (define-key mime-edit-mode-map
621 "\C-c\C-m\C-e" 'mime-edit-enclose-encrypted-region)
622 (define-key mime-edit-mode-map
623 "\C-c\C-m\C-q" 'mime-edit-enclose-quote-region)
625 (define-key mime-edit-mode-map
626 "\C-c\C-x7" 'mime-edit-set-transfer-level-7bit)
627 (define-key mime-edit-mode-map
628 "\C-c\C-x8" 'mime-edit-set-transfer-level-8bit)
629 (define-key mime-edit-mode-map
630 "\C-c\C-x/" 'mime-edit-set-split)
631 (define-key mime-edit-mode-map
632 "\C-c\C-xs" 'mime-edit-set-sign)
633 (define-key mime-edit-mode-map
634 "\C-c\C-xv" 'mime-edit-set-sign)
635 (define-key mime-edit-mode-map
636 "\C-c\C-xe" 'mime-edit-set-encrypt)
637 (define-key mime-edit-mode-map
638 "\C-c\C-xh" 'mime-edit-set-encrypt)
639 (define-key mime-edit-mode-map
640 "\C-c\C-x\C-p" 'mime-edit-preview-message)
641 (define-key mime-edit-mode-map
642 "\C-c\C-x\C-z" 'mime-edit-exit)
643 (define-key mime-edit-mode-map
644 "\C-c\C-x?" 'mime-edit-help)
646 (defconst mime-edit-menu-title "MIME-Edit")
648 (defconst mime-edit-menu-list
649 '((mime-help "Describe MIME editor mode" mime-edit-help)
650 (file "Insert File" mime-edit-insert-file)
651 (external "Insert External" mime-edit-insert-external)
652 (voice "Insert Voice" mime-edit-insert-voice)
653 (message "Insert Message" mime-edit-insert-message)
654 (mail "Insert Mail" mime-edit-insert-mail)
655 (signature "Insert Signature" mime-edit-insert-signature)
656 (text "Insert Text" mime-edit-insert-text)
657 (tag "Insert Tag" mime-edit-insert-tag)
658 (alternative "Enclose as alternative"
659 mime-edit-enclose-alternative-region)
660 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
661 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
662 (digest "Enclose as digest" mime-edit-enclose-digest-region)
663 (signed "Enclose as signed" mime-edit-enclose-signed-region)
664 (encrypted "Enclose as encrypted" mime-edit-enclose-encrypted-region)
665 (quote "Verbatim region" mime-edit-enclose-quote-region)
666 (key "Insert Public Key" mime-edit-insert-key)
667 (split "About split" mime-edit-set-split)
668 (sign "About sign" mime-edit-set-sign)
669 (encrypt "About encryption" mime-edit-set-encrypt)
670 (preview "Preview Message" mime-edit-preview-message)
671 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
673 "MIME-edit menubar entry.")
675 (cond (running-xemacs
676 ;; modified by Pekka Marjola <pema@iki.fi>
677 ;; 1995/9/5 (c.f. [tm-en:69])
678 (defun mime-edit-define-menu-for-xemacs ()
679 "Define menu for Emacs 19."
680 (cond ((featurep 'menubar)
681 (make-local-variable 'current-menubar)
682 (set-buffer-menubar current-menubar)
685 (cons mime-edit-menu-title
688 (vector (nth 1 item)(nth 2 item)
691 mime-edit-menu-list)))
694 ;; modified by Steven L. Baur <steve@miranova.com>
695 ;; 1995/12/6 (c.f. [tm-en:209])
696 (or (boundp 'mime-edit-popup-menu-for-xemacs)
697 (setq mime-edit-popup-menu-for-xemacs
698 (append '("MIME Commands" "---")
699 (mapcar (function (lambda (item)
703 mime-edit-menu-list)))
706 ((>= emacs-major-version 19)
707 (define-key mime-edit-mode-map [menu-bar mime-edit]
708 (cons mime-edit-menu-title
709 (make-sparse-keymap mime-edit-menu-title)))
712 (define-key mime-edit-mode-map
713 (vector 'menu-bar 'mime-edit (car item))
714 (cons (nth 1 item)(nth 2 item))
717 (reverse mime-edit-menu-list)
726 (defun mime-edit-mode ()
727 "MIME minor mode for editing the tagged MIME message.
729 In this mode, basically, the message is composed in the tagged MIME
730 format. The message tag looks like:
732 --[[text/plain; charset=ISO-2022-JP][7bit]]
734 The tag specifies the MIME content type, subtype, optional parameters
735 and transfer encoding of the message following the tag. Messages
736 without any tag are treated as `text/plain' by default. Charset and
737 transfer encoding are automatically defined unless explicitly
738 specified. Binary messages such as audio and image are usually
739 hidden. The messages in the tagged MIME format are automatically
740 translated into a MIME compliant message when exiting this mode.
742 Available charsets depend on Emacs version being used. The following
743 lists the available charsets of each emacs.
745 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
746 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
747 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
748 CN-BIG5 and ISO-2022-INT-1 are available.
750 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
751 be used to represent multilingual text in intermixed manner. Any
752 languages that has no registered charset are represented as either
753 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
755 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
756 without mule, please set variable `default-mime-charset'. This
757 variable must be symbol of which name is a MIME charset.
759 If you want to add more charsets in mule, please set variable
760 `charsets-mime-charset-alist'. This variable must be alist of which
761 key is list of charset and value is symbol of MIME charset. If name
762 of coding-system is different as MIME charset, please set variable
763 `mime-charset-coding-system-alist'. This variable must be alist of
764 which key is MIME charset and value is coding-system.
766 Following commands are available in addition to major mode commands:
769 \\[mime-edit-insert-text] insert a text message.
770 \\[mime-edit-insert-file] insert a (binary) file.
771 \\[mime-edit-insert-external] insert a reference to external body.
772 \\[mime-edit-insert-voice] insert a voice message.
773 \\[mime-edit-insert-message] insert a mail or news message.
774 \\[mime-edit-insert-mail] insert a mail message.
775 \\[mime-edit-insert-signature] insert a signature file at end.
776 \\[mime-edit-insert-key] insert PGP public key.
777 \\[mime-edit-insert-tag] insert a new MIME tag.
779 \[make enclosure (maybe multipart)\]
780 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
781 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
782 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
783 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
784 \\[mime-edit-enclose-signed-region] enclose as PGP signed.
785 \\[mime-edit-enclose-encrypted-region] enclose as PGP encrypted.
786 \\[mime-edit-enclose-quote-region] enclose as verbose mode (to avoid to expand tags)
789 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
790 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
791 \\[mime-edit-set-split] set message splitting mode.
792 \\[mime-edit-set-sign] set PGP-sign mode.
793 \\[mime-edit-set-encrypt] set PGP-encryption mode.
794 \\[mime-edit-preview-message] preview editing MIME message.
795 \\[mime-edit-exit] exit and translate into a MIME compliant message.
796 \\[mime-edit-help] show this help.
797 \\[mime-edit-maybe-translate] exit and translate if in MIME mode, then split.
799 Additional commands are available in some major modes:
800 C-c C-c exit, translate and run the original command.
801 C-c C-s exit, translate and run the original command.
803 The following is a message example written in the tagged MIME format.
804 TABs at the beginning of the line are not a part of the message:
806 This is a conventional plain text. It should be translated
809 This is also a plain text. But, it is explicitly specified as
811 --[[text/plain; charset=ISO-8859-1]]
812 This is also a plain text. But charset is specified as
815 ¡Hola! Buenos días. ¿Cómo está usted?
817 This is a <bold>enriched text</bold>.
818 --[[image/gif][base64]]...image encoded in base64 here...
819 --[[audio/basic][base64]]...audio encoded in base64 here...
821 User customizable variables (not documented all of them):
823 Specifies a key prefix for MIME minor mode commands.
825 mime-ignore-preceding-spaces
826 Preceding white spaces in a message body are ignored if non-nil.
828 mime-ignore-trailing-spaces
829 Trailing white spaces in a message body are ignored if non-nil.
832 Hide a non-textual body message encoded in base64 after insertion
836 A number of network transfer level. It should be bigger than 7.
837 If you are in 8bit-through environment, please set 8.
839 mime-edit-voice-recorder
840 Specifies a function to record a voice message and encode it.
841 The function `mime-edit-voice-recorder-for-sun' is for Sun
845 Turning on MIME mode calls the value of mime-edit-mode-hook, if
848 mime-edit-translate-hook
849 The value of mime-edit-translate-hook is called just before translating
850 the tagged MIME format into a MIME compliant message if it is
851 non-nil. If the hook call the function mime-edit-insert-signature,
852 the signature file will be inserted automatically.
855 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
858 (if mime-edit-mode-flag
860 (if (and (boundp 'mime-edit-touched-flag)
861 mime-edit-touched-flag)
863 (make-local-variable 'mime-edit-touched-flag)
864 (setq mime-edit-touched-flag t)
869 (cond (running-xemacs
870 (add-minor-mode 'mime-edit-mode-flag
871 '((" MIME-Edit " mime-transfer-level-string))
877 (set-alist 'minor-mode-alist
879 '((" MIME-Edit " mime-transfer-level-string)))
880 (set-alist 'minor-mode-map-alist
887 (defun turn-on-mime-edit ()
888 "Unconditionally turn on MIME-Edit mode."
890 (if mime-edit-mode-flag
891 (error "You are already editing a MIME message.")
892 (setq mime-edit-mode-flag t)
894 ;; Set transfer level into mode line
896 (setq mime-transfer-level-string
897 (mime-encoding-name mime-transfer-level 'not-omit))
898 (force-mode-line-update)
900 ;; Define menu for XEmacs.
902 (mime-edit-define-menu-for-xemacs)
907 ;; I don't care about saving these.
908 (setq paragraph-start
909 (regexp-or mime-edit-single-part-tag-regexp
911 (setq paragraph-separate
912 (regexp-or mime-edit-single-part-tag-regexp
914 (run-hooks 'mime-edit-mode-hook)
916 (substitute-command-keys
917 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
921 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
924 (defun mime-edit-exit (&optional nomime no-error)
925 "Translate the tagged MIME message into a MIME compliant message.
926 With no argument encode a message in the buffer into MIME, otherwise
927 just return to previous mode."
929 (if (not mime-edit-mode-flag)
931 (error "You aren't editing a MIME message.")
935 (run-hooks 'mime-edit-translate-hook)
936 (mime-edit-translate-buffer)))
937 ;; Restore previous state.
938 (setq mime-edit-mode-flag nil)
939 (if (and running-xemacs
941 (delete-menu-item (list mime-edit-menu-title))
944 (set-buffer-modified-p (buffer-modified-p))
945 (run-hooks 'mime-edit-exit-hook)
946 (message "Exit MIME editor mode.")
949 (defun mime-edit-maybe-translate ()
951 (mime-edit-exit nil t)
952 (call-interactively 'mime-edit-maybe-split-and-send)
955 (defun mime-edit-help ()
956 "Show help message about MIME mode."
958 (with-output-to-temp-buffer "*Help*"
959 (princ "MIME editor mode:\n")
960 (princ (documentation 'mime-edit-mode))
961 (print-help-return-message)))
963 (defun mime-edit-insert-text ()
964 "Insert a text message.
965 Charset is automatically obtained from the `charsets-mime-charset-alist'."
967 (let ((ret (mime-edit-insert-tag "text" nil nil)))
970 (if (looking-at mime-edit-single-part-tag-regexp)
972 ;; Make a space between the following message.
976 (if (and (member (cadr ret) '("enriched" "richtext"))
977 (fboundp 'enriched-mode)
980 (if (boundp 'enriched-mode)
984 (defun mime-edit-insert-file (file &optional verbose)
985 "Insert a message from a file."
986 (interactive "fInsert file as MIME message: \nP")
987 (let* ((guess (mime-find-file-type file))
989 (subtype (nth 1 guess))
990 (parameters (nth 2 guess))
991 (encoding (nth 3 guess))
992 (disposition-type (nth 4 guess))
993 (disposition-params (nth 5 guess))
996 (setq type (mime-prompt-for-type type)
997 subtype (mime-prompt-for-subtype type subtype)
999 (if (or (interactive-p) verbose)
1000 (setq encoding (mime-prompt-for-encoding encoding))
1002 (if (or (consp parameters) (stringp disposition-type))
1003 (let ((rest parameters) cell attribute value)
1004 (setq parameters "")
1006 (setq cell (car rest))
1007 (setq attribute (car cell))
1008 (setq value (cdr cell))
1009 (if (eq value 'file)
1010 (setq value (std11-wrap-as-quoted-string
1011 (file-name-nondirectory file)))
1013 (setq parameters (concat parameters "; " attribute "=" value))
1014 (setq rest (cdr rest))
1016 (if disposition-type
1019 (concat parameters "\n"
1020 "Content-Disposition: " disposition-type))
1021 (setq rest disposition-params)
1023 (setq cell (car rest))
1024 (setq attribute (car cell))
1025 (setq value (cdr cell))
1026 (if (eq value 'file)
1027 (setq value (std11-wrap-as-quoted-string
1028 (file-name-nondirectory file)))
1031 (concat parameters "; " attribute "=" value))
1032 (setq rest (cdr rest))
1036 (mime-edit-insert-tag type subtype parameters)
1037 (mime-edit-insert-binary-file file encoding)
1040 (defun mime-edit-insert-external ()
1041 "Insert a reference to external body."
1043 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1045 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1047 (let* ((pritype (mime-prompt-for-type))
1048 (subtype (mime-prompt-for-subtype pritype))
1049 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1052 (insert "Content-Type: "
1053 pritype "/" subtype (or parameters "") "\n")))
1054 (if (and (not (eobp))
1055 (not (looking-at mime-edit-single-part-tag-regexp)))
1056 (insert (mime-make-text-tag) "\n")))
1058 (defun mime-edit-insert-voice ()
1059 "Insert a voice message."
1063 "What transfer encoding: "
1064 mime-file-encoding-method-alist nil t nil)))
1065 (mime-edit-insert-tag "audio" "basic" nil)
1066 (mime-edit-define-encoding encoding)
1068 (narrow-to-region (1- (point))(point))
1070 (funcall mime-edit-voice-recorder encoding)
1073 (invisible-region (point-min)(point-max))
1074 (goto-char (point-max))
1077 (defun mime-edit-insert-signature (&optional arg)
1078 "Insert a signature file."
1080 (let ((signature-insert-hook
1083 (apply (function mime-edit-insert-tag)
1084 (mime-find-file-type signature-file-name))
1087 (insert-signature arg)
1091 ;; Insert a new tag around a point.
1093 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1094 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1095 If nothing is inserted, return nil."
1098 (mime-edit-goto-tag)
1099 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1100 (< (match-beginning 0) p)
1103 (goto-char (match-beginning 0))
1112 (mime-prompt-for-type)))
1115 (mime-prompt-for-subtype pritype)))
1118 (mime-prompt-for-parameters pritype subtype delimiter)))
1119 ;; Make a new MIME tag.
1120 (setq newtag (mime-make-tag pritype subtype parameters))
1121 ;; Find an current MIME tag.
1124 (if (mime-edit-goto-tag)
1125 (buffer-substring (match-beginning 0) (match-end 0))
1126 ;; Assume content type is 'text/plan'.
1127 (mime-make-tag "text" "plain")
1129 ;; We are only interested in TEXT.
1131 (not (mime-test-content-type
1132 (mime-edit-get-contype oldtag) "text")))
1135 (if (or (not oldtag) ;Not text
1136 (or mime-ignore-same-text-tag
1137 (not (string-equal oldtag newtag))))
1139 ;; Mark the beginning of the tag for convenience.
1140 (push-mark (point) 'nomsg)
1141 (insert newtag "\n")
1142 (list pritype subtype parameters) ;New tag is created.
1144 ;; Restore previous point.
1146 nil ;Nothing is created.
1150 (defun mime-edit-insert-binary-file (file &optional encoding)
1151 "Insert binary FILE at point.
1152 Optional argument ENCODING specifies an encoding method such as base64."
1153 (let* ((tagend (1- (point))) ;End of the tag
1154 (hide-p (and mime-auto-hide-body
1157 (let ((en (downcase encoding)))
1158 (or (string-equal en "7bit")
1159 (string-equal en "8bit")
1160 (string-equal en "binary")
1164 (narrow-to-region tagend (point))
1165 (mime-insert-encoded-file file encoding)
1168 (invisible-region (point-min) (point-max))
1169 (goto-char (point-max))
1171 (goto-char (point-max))
1174 (looking-at mime-edit-tag-regexp)
1175 (= (point)(point-max))
1176 (mime-edit-insert-tag "text" "plain")
1178 ;; Define encoding even if it is 7bit.
1179 (if (stringp encoding)
1181 (goto-char tagend) ; Make sure which line the tag is on.
1182 (mime-edit-define-encoding encoding)
1187 ;; Commands work on a current message flagment.
1189 (defun mime-edit-goto-tag ()
1190 "Search for the beginning of the tagged MIME message."
1191 (let ((current (point)))
1192 (if (looking-at mime-edit-tag-regexp)
1194 ;; At first, go to the end.
1195 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1196 (goto-char (1- (match-beginning 0))) ;For multiline tag
1199 (goto-char (point-max))
1201 ;; Then search for the beginning.
1202 (re-search-backward mime-edit-end-tag-regexp nil t)
1203 (or (looking-at mime-edit-beginning-tag-regexp)
1204 ;; Restore previous point.
1211 (defun mime-edit-content-beginning ()
1212 "Return the point of the beginning of content."
1214 (let ((beg (save-excursion
1215 (beginning-of-line) (point))))
1216 (if (mime-edit-goto-tag)
1217 (let ((top (point)))
1218 (goto-char (match-end 0))
1219 (if (and (= beg top)
1220 (= (following-char) ?\^M))
1224 ;; Default text/plain tag.
1225 (goto-char (point-min))
1227 (concat "\n" (regexp-quote mail-header-separator)
1228 (if mime-ignore-preceding-spaces
1229 "[ \t\n]*\n" "\n")) nil 'move)
1233 (defun mime-edit-content-end ()
1234 "Return the point of the end of content."
1236 (if (mime-edit-goto-tag)
1238 (goto-char (match-end 0))
1239 (if (invisible-p (point))
1240 (next-visible-point (point))
1241 ;; Move to the end of this text.
1242 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1243 ;; Don't forget a multiline tag.
1244 (goto-char (match-beginning 0))
1248 ;; Assume the message begins with text/plain.
1249 (goto-char (mime-edit-content-beginning))
1250 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1251 ;; Don't forget a multiline tag.
1252 (goto-char (match-beginning 0)))
1256 (defun mime-edit-define-charset (charset)
1257 "Set charset of current tag to CHARSET."
1259 (if (mime-edit-goto-tag)
1260 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1261 (delete-region (match-beginning 0) (match-end 0))
1264 (mime-edit-set-parameter
1265 (mime-edit-get-contype tag)
1266 "charset" (upcase (symbol-name charset)))
1267 (mime-edit-get-encoding tag)))
1270 (defun mime-edit-define-encoding (encoding)
1271 "Set encoding of current tag to ENCODING."
1273 (if (mime-edit-goto-tag)
1274 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1275 (delete-region (match-beginning 0) (match-end 0))
1276 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1279 (defun mime-edit-choose-charset ()
1280 "Choose charset of a text following current point."
1281 (detect-mime-charset-region (point) (mime-edit-content-end))
1284 (defun mime-make-text-tag (&optional subtype)
1285 "Make a tag for a text after current point.
1286 Subtype of text type can be specified by an optional argument SUBTYPE.
1287 Otherwise, it is obtained from mime-content-types."
1288 (let* ((pritype "text")
1289 (subtype (or subtype
1290 (car (car (cdr (assoc pritype mime-content-types)))))))
1291 ;; Charset should be defined later.
1292 (mime-make-tag pritype subtype)))
1295 ;; Tag handling functions
1297 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1298 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1299 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1303 (defun mime-create-tag (contype &optional encoding)
1304 "Make a tag with CONTENT-TYPE and optional ENCODING."
1305 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1308 (defun mime-edit-get-contype (tag)
1309 "Return Content-Type (including parameters) of TAG."
1311 (or (string-match mime-edit-single-part-tag-regexp tag)
1312 (string-match mime-edit-multipart-beginning-regexp tag)
1313 (string-match mime-edit-multipart-end-regexp tag)
1315 (substring tag (match-beginning 1) (match-end 1))
1318 (defun mime-edit-get-encoding (tag)
1319 "Return encoding of TAG."
1321 (string-match mime-edit-single-part-tag-regexp tag)
1323 (not (= (match-beginning 3) (match-end 3)))
1324 (substring tag (match-beginning 3) (match-end 3))))
1326 (defun mime-get-parameter (contype parameter)
1327 "For given CONTYPE return value for PARAMETER.
1328 Nil if no such parameter."
1332 (regexp-quote parameter)
1333 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1335 (substring contype (match-beginning 1) (match-end 1))
1336 nil ;No such parameter
1339 (defun mime-edit-set-parameter (contype parameter value)
1340 "For given CONTYPE set PARAMETER to VALUE."
1341 (let (ctype opt-fields)
1342 (if (string-match "\n[^ \t\n\r]+:" contype)
1343 (setq ctype (substring contype 0 (match-beginning 0))
1344 opt-fields (substring contype (match-beginning 0)))
1345 (setq ctype contype)
1350 (regexp-quote parameter)
1351 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1354 (concat (substring ctype 0 (match-beginning 1))
1356 (substring contype (match-end 1))
1358 (concat ctype "; " parameter "=" value opt-fields)
1361 (defun mime-strip-parameters (contype)
1362 "Return primary content-type and subtype without parameters for CONTYPE."
1363 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1364 (substring contype (match-beginning 1) (match-end 1)) nil))
1366 (defun mime-test-content-type (contype type &optional subtype)
1367 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1368 (and (stringp contype)
1371 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1372 (downcase contype))))
1377 (defun mime-find-file-type (file)
1378 "Guess Content-Type, subtype, and parameters from FILE."
1380 (guesses mime-file-types))
1381 (while (and (not guess) guesses)
1382 (if (string-match (car (car guesses)) file)
1383 (setq guess (cdr (car guesses))))
1384 (setq guesses (cdr guesses)))
1388 (defun mime-prompt-for-type (&optional default)
1389 "Ask for Content-type."
1391 ;; Repeat until primary content type is specified.
1392 (while (string-equal type "")
1394 (completing-read "What content type: "
1397 'require-match ;Type must be specified.
1400 (if (string-equal type "")
1402 (message "Content type is required.")
1409 (defun mime-prompt-for-subtype (type &optional default)
1410 "Ask for subtype of media-type TYPE."
1411 (let ((subtypes (cdr (assoc type mime-content-types))))
1413 (assoc default subtypes))
1414 (setq default (car (car subtypes)))
1420 "What content subtype: (default " default ") ")
1421 "What content subtype: ")
1422 (cdr (assoc type mime-content-types))
1424 'require-match ;Subtype must be specified.
1427 (if (string-equal answer "") default answer)))
1429 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1430 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1431 Optional DELIMITER specifies parameter delimiter (';' by default)."
1432 (let* ((delimiter (or delimiter "; "))
1437 (mime-prompt-for-parameters-1
1439 (cdr (assoc pritype mime-content-types))))))
1442 (if (and (stringp parameters)
1443 (not (string-equal parameters "")))
1444 (concat delimiter parameters)
1445 "" ;"" if no parameters
1448 (defun mime-prompt-for-parameters-1 (optlist)
1449 (apply (function append)
1450 (mapcar (function mime-prompt-for-parameter) optlist)))
1452 (defun mime-prompt-for-parameter (parameter)
1454 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1455 (let* ((prompt (car parameter))
1456 (choices (mapcar (function
1458 (if (consp e) e (list e))))
1460 (default (car (car choices)))
1466 (concat "What " prompt
1468 (if (string-equal default "") "\"\"" default)
1470 choices nil nil ""))
1471 ;; If nothing is selected, use default.
1472 (if (string-equal answer "")
1473 (setq answer default)))
1475 (read-string (concat "What " prompt ": "))))
1476 (cons (if (and answer
1477 (not (string-equal answer "")))
1479 ;; Note: control characters ignored!
1480 (if (string-match mime-tspecials-regexp answer)
1481 (concat "\"" answer "\"") answer)))
1482 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1485 (defun mime-prompt-for-encoding (default)
1486 "Ask for Content-Transfer-Encoding."
1491 "What transfer encoding: "
1492 mime-file-encoding-method-alist nil t default)
1498 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1501 (defvar mime-edit-translate-buffer-hook
1502 '(mime-edit-pgp-enclose-buffer
1503 mime-edit-translate-body
1504 mime-edit-translate-header))
1506 (defun mime-edit-translate-header ()
1507 "Encode the message header into network representation."
1508 (eword-encode-header 'code-conversion)
1509 (run-hooks 'mime-edit-translate-header-hook)
1512 (defun mime-edit-translate-buffer ()
1513 "Encode the tagged MIME message in current buffer in MIME compliant message."
1515 (if (catch 'mime-edit-error
1517 (run-hooks 'mime-edit-translate-buffer-hook)
1521 (error "Translation error!")
1524 (defun mime-edit-find-inmost ()
1525 (goto-char (point-min))
1526 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1527 (let ((bb (match-beginning 0))
1529 (type (buffer-substring (match-beginning 1)(match-end 1)))
1531 (setq end-exp (format "--}-<<%s>>\n" type))
1533 (if (re-search-forward end-exp nil t)
1535 (setq eb (match-beginning 0))
1536 (setq ee (match-end 0))
1538 (setq eb (point-max))
1539 (setq ee (point-max))
1541 (narrow-to-region be eb)
1543 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1545 (narrow-to-region (match-beginning 0)(point-max))
1546 (mime-edit-find-inmost)
1549 (list type bb be eb)
1552 (defun mime-edit-process-multipart-1 (boundary)
1553 (let ((ret (mime-edit-find-inmost)))
1555 (let ((type (car ret))
1556 (bb (nth 1 ret))(be (nth 2 ret))
1559 (narrow-to-region bb eb)
1560 (delete-region bb be)
1561 (setq bb (point-min))
1562 (setq eb (point-max))
1565 (if (looking-at mime-edit-multipart-end-regexp)
1566 (let ((beg (match-beginning 0))
1569 (delete-region beg end)
1570 (or (looking-at mime-edit-beginning-tag-regexp)
1572 (insert (concat (mime-make-text-tag) "\n"))
1574 (cond ((string-equal type "quote")
1575 (mime-edit-enquote-region bb eb)
1577 ((string-equal type "signed")
1578 (cond ((eq mime-edit-signing-type 'pgp-elkins)
1579 (mime-edit-sign-pgp-elkins bb eb boundary)
1581 ((eq mime-edit-signing-type 'pgp-kazu)
1582 (mime-edit-sign-pgp-kazu bb eb boundary)
1585 ((string-equal type "encrypted")
1586 (cond ((eq mime-edit-encrypting-type 'pgp-elkins)
1587 (mime-edit-encrypt-pgp-elkins bb eb boundary)
1589 ((eq mime-edit-encrypting-type 'pgp-kazu)
1590 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1594 (nth 2 (mime-edit-translate-region bb eb
1598 (format "--[[multipart/%s;
1599 boundary=\"%s\"][7bit]]\n"
1604 (defun mime-edit-enquote-region (beg end)
1607 (narrow-to-region beg end)
1609 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1610 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1611 (replace-match (concat "- " (substring tag 1)))
1614 (defun mime-edit-dequote-region (beg end)
1617 (narrow-to-region beg end)
1619 (while (re-search-forward
1620 mime-edit-quoted-single-part-tag-regexp nil t)
1621 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1622 (replace-match (concat "-" (substring tag 2)))
1625 (defun mime-edit-sign-pgp-elkins (beg end boundary)
1628 (narrow-to-region beg end)
1630 (mime-edit-translate-region beg end boundary))
1632 (encoding (nth 1 ret))
1634 (pgp-boundary (concat "pgp-sign-" boundary))
1637 (insert (format "Content-Type: %s\n" ctype))
1639 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1642 (or (as-binary-process
1643 (funcall (pgp-function 'mime-sign)
1644 (point-min)(point-max) nil nil pgp-boundary))
1645 (throw 'mime-edit-error 'pgp-error)
1649 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1651 (defun mime-edit-make-encrypt-recipient-header ()
1652 (let* ((names mime-edit-encrypt-recipient-fields-list)
1654 (std11-field-bodies (cons "From" names)
1655 nil mail-header-separator))
1658 (setq values (cdr values))))
1659 (header (and (stringp from)
1660 (if (string-equal from "")
1662 (format "From: %s\n" from)
1665 (while (and names values)
1666 (let ((name (car names))
1667 (value (car values))
1669 (and (stringp value)
1670 (or (string-equal value "")
1672 (setq header (concat header name ": " value "\n")
1673 recipients (if recipients
1674 (concat recipients " ," value)
1677 (setq names (cdr names)
1678 values (cdr values))
1680 (vector from recipients header)
1683 (defun mime-edit-encrypt-pgp-elkins (beg end boundary)
1686 (let (from recipients header)
1687 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1688 (setq from (aref ret 0)
1689 recipients (aref ret 1)
1690 header (aref ret 2))
1692 (narrow-to-region beg end)
1694 (mime-edit-translate-region beg end boundary))
1696 (encoding (nth 1 ret))
1698 (pgp-boundary (concat "pgp-" boundary))
1702 (insert (format "Content-Type: %s\n" ctype))
1704 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1707 (or (funcall (pgp-function 'encrypt)
1708 recipients (point-min) (point-max) from)
1709 (throw 'mime-edit-error 'pgp-error)
1712 (insert (format "--[[multipart/encrypted;
1714 protocol=\"application/pgp-encrypted\"][7bit]]
1716 Content-Type: application/pgp-encrypted
1719 Content-Type: application/octet-stream
1720 Content-Transfer-Encoding: 7bit
1722 " pgp-boundary pgp-boundary pgp-boundary))
1723 (goto-char (point-max))
1724 (insert (format "\n--%s--\n" pgp-boundary))
1727 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1730 (narrow-to-region beg end)
1732 (mime-edit-translate-region beg end boundary))
1734 (encoding (nth 1 ret))
1738 (insert (format "Content-Type: %s\n" ctype))
1740 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1743 (or (as-binary-process
1744 (funcall (pgp-function 'traditional-sign)
1746 (throw 'mime-edit-error 'pgp-error)
1750 "--[[application/pgp; format=mime][7bit]]\n")
1754 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1756 (let (from recipients header)
1757 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1758 (setq from (aref ret 0)
1759 recipients (aref ret 1)
1760 header (aref ret 2))
1763 (narrow-to-region beg end)
1765 (mime-edit-translate-region beg end boundary))
1767 (encoding (nth 1 ret))
1772 (insert (format "Content-Type: %s\n" ctype))
1774 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1777 (or (as-binary-process
1778 (funcall (pgp-function 'encrypt)
1779 recipients beg (point-max) nil 'maybe)
1781 (throw 'mime-edit-error 'pgp-error)
1785 "--[[application/pgp; format=mime][7bit]]\n")
1789 (defsubst replace-space-with-underline (str)
1790 (mapconcat (function
1798 (defun mime-edit-make-boundary ()
1799 (concat mime-multipart-boundary "_"
1800 (replace-space-with-underline (current-time-string))
1803 (defun mime-edit-translate-body ()
1804 "Encode the tagged MIME body in current buffer in MIME compliant message."
1807 (let ((boundary (mime-edit-make-boundary))
1810 (while (mime-edit-process-multipart-1
1811 (format "%s-%d" boundary i))
1815 ;; We are interested in message body.
1818 (goto-char (point-min))
1820 (concat "\n" (regexp-quote mail-header-separator)
1821 (if mime-ignore-preceding-spaces
1822 "[ \t\n]*\n" "\n")) nil 'move)
1826 (goto-char (point-max))
1827 (and mime-ignore-trailing-spaces
1828 (re-search-backward "[^ \t\n]\n" beg t)
1831 (setq ret (mime-edit-translate-region
1833 (format "%s-%d" boundary i)))
1835 (mime-edit-dequote-region (point-min)(point-max))
1836 (let ((contype (car ret)) ;Content-Type
1837 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1839 ;; Insert X-Emacs field
1840 (and mime-edit-insert-x-emacs-field
1841 (or (mail-position-on-field "X-Emacs")
1842 (insert mime-edit-x-emacs-value)
1844 ;; Make primary MIME headers.
1845 (or (mail-position-on-field "MIME-Version")
1846 (insert mime-edit-mime-version-value))
1847 ;; Remove old Content-Type and other fields.
1849 (goto-char (point-min))
1850 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1851 (narrow-to-region (point-min) (point))
1852 (goto-char (point-min))
1853 (mime-delete-field "Content-Type")
1854 (mime-delete-field "Content-Transfer-Encoding"))
1855 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1856 (mail-position-on-field "Content-Type")
1860 (mail-position-on-field "Content-Transfer-Encoding")
1864 (defun mime-edit-translate-single-part-tag (&optional prefix)
1865 "Translate single-part-tag to MIME header."
1866 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1867 (let* ((beg (match-beginning 0))
1869 (tag (buffer-substring beg end))
1871 (delete-region beg end)
1872 (let ((contype (mime-edit-get-contype tag))
1873 (encoding (mime-edit-get-encoding tag))
1875 (insert (concat prefix "--" boundary "\n"))
1877 (narrow-to-region (point)(point))
1878 (insert "Content-Type: " contype "\n")
1880 (insert "Content-Transfer-Encoding: " encoding "\n"))
1881 (eword-encode-header)
1885 (defun mime-edit-translate-region (beg end &optional boundary multipart)
1887 (setq boundary (mime-edit-make-boundary))
1891 (narrow-to-region beg end)
1892 (let ((tag nil) ;MIME tag
1893 (contype nil) ;Content-Type
1894 (encoding nil) ;Content-Transfer-Encoding
1895 (nparts 0)) ;Number of body parts
1896 ;; Normalize the body part by inserting appropriate message
1897 ;; tags for every message contents.
1898 (mime-edit-normalize-body)
1899 ;; Counting the number of Content-Type.
1900 (goto-char (point-min))
1901 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1902 (setq nparts (1+ nparts)))
1903 ;; Begin translation.
1905 ((and (<= nparts 1)(not multipart))
1906 ;; It's a singular message.
1907 (goto-char (point-min))
1908 (while (re-search-forward
1909 mime-edit-single-part-tag-regexp nil t)
1911 (buffer-substring (match-beginning 0) (match-end 0)))
1912 (delete-region (match-beginning 0) (1+ (match-end 0)))
1913 (setq contype (mime-edit-get-contype tag))
1914 (setq encoding (mime-edit-get-encoding tag))
1917 ;; It's a multipart message.
1918 (goto-char (point-min))
1919 (and (mime-edit-translate-single-part-tag)
1920 (while (mime-edit-translate-single-part-tag "\n"))
1922 ;; Define Content-Type as "multipart/mixed".
1924 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
1925 ;; Content-Transfer-Encoding must be "7bit".
1926 ;; The following encoding can be `nil', but is
1927 ;; specified as is since there is no way that a user
1929 (setq encoding "7bit")
1930 ;; Insert the trailer.
1931 (goto-char (point-max))
1932 (insert "\n--" boundary "--\n")
1934 (list contype encoding boundary nparts)
1937 (defun mime-edit-normalize-body ()
1938 "Normalize the body part by inserting appropriate message tags."
1939 ;; Insert the first MIME tags if necessary.
1940 (goto-char (point-min))
1941 (if (not (looking-at mime-edit-single-part-tag-regexp))
1942 (insert (mime-make-text-tag) "\n"))
1943 ;; Check each tag, and add new tag or correct it if necessary.
1944 (goto-char (point-min))
1945 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1946 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
1947 (contype (mime-edit-get-contype tag))
1948 (charset (mime-get-parameter contype "charset"))
1949 (encoding (mime-edit-get-encoding tag)))
1950 ;; Remove extra whitespaces after the tag.
1951 (if (looking-at "[ \t]+$")
1952 (delete-region (match-beginning 0) (match-end 0)))
1954 (end (mime-edit-content-end))
1956 (if (= end (point-max))
1959 (or (looking-at mime-edit-beginning-tag-regexp)
1961 (insert (mime-make-text-tag) "\n")
1963 (visible-region beg end)
1967 ((mime-test-content-type contype "message")
1968 ;; Content-type "message" should be sent as is.
1971 ((mime-test-content-type contype "text")
1972 ;; Define charset for text if necessary.
1973 (setq charset (if charset
1974 (intern (downcase charset))
1975 (mime-edit-choose-charset)))
1976 (mime-edit-define-charset charset)
1977 (cond ((string-equal contype "text/x-rot13-47-48")
1980 (mule-caesar-region (point) (mime-edit-content-end))
1982 ((string-equal contype "text/enriched")
1987 (end (mime-edit-content-end))
1989 ;; Patch for hard newlines
1992 ;; (while (search-forward "\n" end t)
1993 ;; (put-text-property (match-beginning 0)
1996 ;; End patch for hard newlines
1997 (enriched-encode beg end)
1999 (if (search-forward "\n\n")
2000 (delete-region beg (match-end 0))
2003 ;; Point is now on current tag.
2004 ;; Define encoding and encode text if necessary.
2005 (or encoding ;Encoding is not specified.
2009 mime-edit-charset-default-encoding-alist)
2011 (beg (mime-edit-content-beginning))
2013 (encode-mime-charset-region beg (mime-edit-content-end)
2015 ;; Protect "From " in beginning of line
2017 (narrow-to-region beg (mime-edit-content-end))
2019 (let (case-fold-search)
2020 (if (re-search-forward "^From " nil t)
2022 (if (memq charset '(iso-2022-jp
2027 (replace-match "\e(BFrom ")
2028 (re-search-forward "^From " nil t)
2030 (setq encoding "quoted-printable")
2032 ;; canonicalize line break code
2033 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2035 (narrow-to-region beg (mime-edit-content-end))
2037 (while (re-search-forward "\\([^\r]\\)\n" nil t)
2039 (concat (buffer-substring (match-beginning 0)
2040 (match-end 1)) "\r\n"))
2043 (mime-encode-region beg (mime-edit-content-end) encoding)
2044 (mime-edit-define-encoding encoding)
2046 (goto-char (mime-edit-content-end))
2048 ((null encoding) ;Encoding is not specified.
2049 ;; Application, image, audio, video, and any other
2050 ;; unknown content-type without encoding should be
2052 (let* ((encoding "base64") ;Encode in BASE64 by default.
2053 (beg (mime-edit-content-beginning))
2054 (end (mime-edit-content-end))
2055 (body (buffer-substring beg end))
2057 (mime-encode-region beg end encoding)
2058 (mime-edit-define-encoding encoding))
2063 (defun mime-delete-field (field)
2064 "Delete header FIELD."
2065 (let ((regexp (format "^%s:[ \t]*" field)))
2066 (goto-char (point-min))
2067 (while (re-search-forward regexp nil t)
2068 (delete-region (match-beginning 0)
2069 (progn (forward-line 1) (point)))
2074 ;;; Platform dependent functions
2077 ;; Sun implementations
2079 (defun mime-edit-voice-recorder-for-sun (encoding)
2080 "Record voice in a buffer using Sun audio device,
2081 and insert data encoded as ENCODING."
2082 (message "Start the recording on %s. Type C-g to finish the recording..."
2084 (mime-insert-encoded-file "/dev/audio" encoding)
2088 ;;; @ Other useful commands.
2091 ;; Message forwarding commands as content-type "message/rfc822".
2093 (defun mime-edit-insert-message (&optional message)
2095 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2096 (if (and inserter (fboundp inserter))
2098 (mime-edit-insert-tag "message" "rfc822")
2099 (funcall inserter message)
2101 (message "Sorry, I don't have message inserter for your MUA.")
2104 (defun mime-edit-insert-mail (&optional message)
2106 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2107 (if (and inserter (fboundp inserter))
2109 (mime-edit-insert-tag "message" "rfc822")
2110 (funcall inserter message)
2112 (message "Sorry, I don't have mail inserter for your MUA.")
2115 (defun mime-edit-inserted-message-filter ()
2118 (let ((header-start (point))
2119 (case-fold-search t)
2122 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2123 (if (re-search-forward "^$" (mark t))
2124 (narrow-to-region header-start (match-beginning 0))
2126 (goto-char header-start)
2127 (while (and (re-search-forward
2128 mime-edit-yank-ignored-field-regexp nil t)
2129 (setq beg (match-beginning 0))
2130 (setq end (1+ (std11-field-end)))
2132 (delete-region beg end)
2137 ;;; @ multipart enclosure
2140 (defun mime-edit-enclose-region-internal (type beg end)
2143 (let ((current (point)))
2145 (narrow-to-region beg end)
2146 (insert (format "--<<%s>>-{\n" type))
2147 (goto-char (point-max))
2148 (insert (format "--}-<<%s>>\n" type))
2149 (goto-char (point-max))
2151 (or (looking-at mime-edit-beginning-tag-regexp)
2153 (insert (mime-make-text-tag) "\n")
2157 (defun mime-edit-enclose-quote-region (beg end)
2159 (mime-edit-enclose-region-internal 'quote beg end)
2162 (defun mime-edit-enclose-mixed-region (beg end)
2164 (mime-edit-enclose-region-internal 'mixed beg end)
2167 (defun mime-edit-enclose-parallel-region (beg end)
2169 (mime-edit-enclose-region-internal 'parallel beg end)
2172 (defun mime-edit-enclose-digest-region (beg end)
2174 (mime-edit-enclose-region-internal 'digest beg end)
2177 (defun mime-edit-enclose-alternative-region (beg end)
2179 (mime-edit-enclose-region-internal 'alternative beg end)
2182 (defun mime-edit-enclose-signed-region (beg end)
2184 (if mime-edit-signing-type
2185 (mime-edit-enclose-region-internal 'signed beg end)
2186 (message "Please specify signing type.")
2189 (defun mime-edit-enclose-encrypted-region (beg end)
2191 (if mime-edit-signing-type
2192 (mime-edit-enclose-region-internal 'encrypted beg end)
2193 (message "Please specify encrypting type.")
2196 (defun mime-edit-insert-key (&optional arg)
2197 "Insert a pgp public key."
2199 (mime-edit-insert-tag "application" "pgp-keys")
2200 (mime-edit-define-encoding "7bit")
2201 (funcall (pgp-function 'insert-key))
2208 (defun mime-edit-set-split (arg)
2211 (y-or-n-p "Do you want to enable split? ")
2213 (setq mime-edit-split-message arg)
2215 (message "This message is enabled to split.")
2216 (message "This message is not enabled to split.")
2219 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2220 "Toggle transfer-level is 7bit or 8bit through.
2222 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2224 (if (numberp transfer-level)
2225 (setq mime-transfer-level transfer-level)
2226 (if (< mime-transfer-level 8)
2227 (setq mime-transfer-level 8)
2228 (setq mime-transfer-level 7)
2230 (setq mime-edit-charset-default-encoding-alist
2231 (mime-make-charset-default-encoding-alist mime-transfer-level))
2232 (message (format "Current transfer-level is %d bit"
2233 mime-transfer-level))
2234 (setq mime-transfer-level-string
2235 (mime-encoding-name mime-transfer-level 'not-omit))
2236 (force-mode-line-update)
2239 (defun mime-edit-set-transfer-level-7bit ()
2241 (mime-edit-toggle-transfer-level 7)
2244 (defun mime-edit-set-transfer-level-8bit ()
2246 (mime-edit-toggle-transfer-level 8)
2253 (defun mime-edit-set-sign (arg)
2256 (y-or-n-p "Do you want to sign? ")
2259 (if mime-edit-signing-type
2261 (setq mime-edit-pgp-processing 'sign)
2262 (message "This message will be signed.")
2264 (message "Please specify signing type.")
2266 (if (eq mime-edit-pgp-processing 'sign)
2267 (setq mime-edit-pgp-processing nil)
2269 (message "This message will not be signed.")
2272 (defun mime-edit-set-encrypt (arg)
2275 (y-or-n-p "Do you want to encrypt? ")
2278 (if mime-edit-encrypting-type
2280 (setq mime-edit-pgp-processing 'encrypt)
2281 (message "This message will be encrypt.")
2283 (message "Please specify encrypting type.")
2285 (if (eq mime-edit-pgp-processing 'encrypt)
2286 (setq mime-edit-pgp-processing nil)
2288 (message "This message will not be encrypt.")
2291 (defvar mime-edit-pgp-processing nil)
2292 (make-variable-buffer-local 'mime-edit-pgp-processing)
2294 (defun mime-edit-pgp-enclose-buffer ()
2295 (let ((beg (save-excursion
2296 (goto-char (point-min))
2297 (if (search-forward (concat "\n" mail-header-separator "\n"))
2303 (cond ((eq mime-edit-pgp-processing 'sign)
2304 (mime-edit-enclose-signed-region beg end)
2306 ((eq mime-edit-pgp-processing 'encrypt)
2307 (mime-edit-enclose-encrypted-region beg end)
2315 (defun mime-edit-insert-partial-header (fields subject
2316 id number total separator)
2318 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2319 (insert mime-edit-mime-version-field-for-message/partial)
2321 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2322 id number total separator))
2325 (defun mime-edit-split-and-send
2326 (&optional cmd lines mime-edit-message-max-length)
2330 (count-lines (point-min) (point-max)))
2332 (or mime-edit-message-max-length
2333 (setq mime-edit-message-max-length
2334 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2335 mime-edit-message-default-max-lines))
2337 (let* ((mime-edit-draft-file-name
2338 (or (buffer-file-name)
2340 (expand-file-name "mime-draft" mime-temp-directory))))
2341 (separator mail-header-separator)
2343 (replace-space-with-underline (current-time-string))
2344 "@" (system-name) "\"")))
2345 (run-hooks 'mime-edit-before-split-hook)
2346 (let ((the-buf (current-buffer))
2347 (copy-buf (get-buffer-create " *Original Message*"))
2348 (header (std11-header-string-except
2349 mime-edit-split-ignored-field-regexp separator))
2350 (subject (mail-fetch-field "subject"))
2351 (total (+ (/ lines mime-edit-message-max-length)
2352 (if (> (mod lines mime-edit-message-max-length) 0)
2358 mime-edit-split-message-sender-alist))
2362 (error "Split sender is not specified for `%s'." major-mode)
2365 (mime-edit-partial-number 1)
2368 (set-buffer copy-buf)
2370 (insert-buffer the-buf)
2372 (if (re-search-forward
2373 (concat "^" (regexp-quote separator) "$") nil t)
2374 (let ((he (match-beginning 0)))
2376 (narrow-to-region (point-min) he)
2378 (goto-char (point-min))
2379 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2380 (delete-region (match-beginning 0)
2381 (1+ (std11-field-end)))
2383 (while (< mime-edit-partial-number total)
2386 (set-buffer copy-buf)
2387 (setq data (buffer-substring
2390 (goto-line mime-edit-message-max-length)
2393 (delete-region (point-min)(point))
2395 (mime-edit-insert-partial-header
2396 header subject id mime-edit-partial-number total separator)
2399 (message (format "Sending %d/%d..."
2400 mime-edit-partial-number total))
2401 (call-interactively command)
2402 (message (format "Sending %d/%d... done"
2403 mime-edit-partial-number total))
2405 (setq mime-edit-partial-number
2406 (1+ mime-edit-partial-number))
2410 (set-buffer copy-buf)
2411 (setq data (buffer-string))
2414 (mime-edit-insert-partial-header
2415 header subject id mime-edit-partial-number total separator)
2418 (message (format "Sending %d/%d..."
2419 mime-edit-partial-number total))
2420 (message (format "Sending %d/%d... done"
2421 mime-edit-partial-number total))
2425 (defun mime-edit-maybe-split-and-send (&optional cmd)
2427 (run-hooks 'mime-edit-before-send-hook)
2428 (let ((mime-edit-message-max-length
2429 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2430 mime-edit-message-default-max-lines))
2431 (lines (count-lines (point-min) (point-max)))
2433 (if (and (> lines mime-edit-message-max-length)
2434 mime-edit-split-message)
2435 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2439 ;;; @ preview message
2442 (defvar mime-edit-buffer nil) ; buffer local variable
2444 (defun mime-edit-preview-message ()
2445 "preview editing MIME message."
2447 (let* ((str (buffer-string))
2448 (separator mail-header-separator)
2449 (the-buf (current-buffer))
2450 (buf-name (buffer-name))
2451 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2452 (buf (get-buffer temp-buf-name))
2456 (switch-to-buffer buf)
2459 (setq buf (get-buffer-create temp-buf-name))
2460 (switch-to-buffer buf)
2463 (setq major-mode 'mime-temp-message-mode)
2464 (make-local-variable 'mail-header-separator)
2465 (setq mail-header-separator separator)
2466 (make-local-variable 'mime-edit-buffer)
2467 (setq mime-edit-buffer the-buf)
2469 (run-hooks 'mime-edit-translate-hook)
2470 (mime-edit-translate-buffer)
2471 (goto-char (point-min))
2472 (if (re-search-forward
2473 (concat "^" (regexp-quote separator) "$"))
2479 (defun mime-edit-quitting-method ()
2480 "Quitting method for mime-view."
2481 (let ((temp mime-raw-buffer)
2483 (mime-view-kill-buffer)
2485 (setq buf mime-edit-buffer)
2487 (switch-to-buffer buf)
2490 (set-alist 'mime-view-quitting-method-alist
2491 'mime-temp-message-mode
2492 (function mime-edit-quitting-method)
2499 (defvar mime-edit-again-ignored-field-regexp
2500 (concat "^\\(" "Content-.*\\|Mime-Version"
2501 (if mime-edit-insert-x-emacs-field "\\|X-Emacs")
2503 "Regexp for deleted header fields when `mime-edit-again' is called.")
2505 (defun mime-edit-decode-buffer (not-decode-text)
2507 (goto-char (point-min))
2508 (let ((ctl (mime-read-Content-Type)))
2510 (let ((type (car ctl))
2511 (stype (car (cdr ctl)))
2512 (params (cdr (cdr ctl)))
2515 ((and (eq type 'application)(eq stype 'pgp-signature))
2516 (delete-region (point-min)(point-max))
2518 ((eq type 'multipart)
2519 (let* ((boundary (cdr (assoc "boundary" params)))
2521 (concat "\n--" (regexp-quote boundary) "[ \t]*\n"))
2523 (re-search-forward boundary-pat nil t)
2524 (let ((bb (match-beginning 0)) eb tag)
2525 (setq tag (format "\n--<<%s>>-{\n" stype))
2528 (setq bb (+ bb (length tag)))
2530 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2532 (setq eb (match-beginning 0))
2533 (replace-match (format "--}-<<%s>>\n" stype))
2535 (narrow-to-region bb eb)
2536 (goto-char (point-min))
2537 (while (re-search-forward boundary-pat nil t)
2538 (let ((beg (match-beginning 0))
2540 (delete-region beg (match-end 0))
2542 (if (re-search-forward boundary-pat nil t)
2543 (setq end (match-beginning 0))
2544 (setq end (point-max))
2547 (narrow-to-region beg end)
2548 (mime-edit-decode-buffer not-decode-text)
2549 (goto-char (point-max))
2552 (goto-char (point-min))
2553 (or (= (point-min) 1)
2554 (delete-region (point-min)
2555 (if (search-forward "\n\n" nil t)
2561 (let* ((ctype (format "%s/%s" type stype))
2564 (let ((bytes (+ 14 (length ctype))))
2565 (mapconcat (function
2567 (if (string= (car attr) "charset")
2569 (setq charset (cdr attr))
2577 (setq bytes (+ bytes bs 2))
2580 (setq bytes (+ bs 1))
2588 (if (re-search-forward
2589 "Content-Transfer-Encoding:" nil t)
2590 (let ((beg (match-beginning 0))
2591 (hbeg (match-end 0))
2592 (end (std11-field-end)))
2594 (eliminate-top-spaces
2595 (std11-unfold-string
2596 (buffer-substring hbeg end))))
2597 (if (or charset (eq type 'text))
2599 (delete-region beg (1+ end))
2600 (goto-char (point-min))
2601 (if (search-forward "\n\n" nil t)
2604 (match-end 0)(point-max) encoding)
2608 (if (or encoded (not not-decode-text))
2609 (decode-mime-charset-region
2610 (point-min)(point-max)
2611 (or charset default-mime-charset))
2614 (if (re-search-forward "^$" nil t)
2618 (if (= (point-min) 1)
2624 (format "%s/%s%s" type stype pstr)
2627 (delete-region (point-min) he)
2630 (format "%s/%s%s" type stype pstr)
2635 (decode-mime-charset-region (point-min) (point-max)
2636 default-mime-charset)
2640 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2641 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2642 Content-Type and Content-Transfer-Encoding header fields will be
2643 converted to MIME-Edit tags."
2645 (goto-char (point-min))
2647 (concat "\n" (regexp-quote mail-header-separator) "\n")
2649 (replace-match "\n\n")
2651 (mime-edit-decode-buffer not-decode-text)
2652 (goto-char (point-min))
2654 (std11-narrow-to-header)
2655 (goto-char (point-min))
2656 (while (re-search-forward mime-edit-again-ignored-field-regexp nil t)
2657 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2660 (and (re-search-forward "^$")
2661 (replace-match mail-header-separator)
2671 (provide 'mime-edit)
2673 (run-hooks 'mime-edit-load-hook)
2675 ;;; mime-edit.el ends here