1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,1994,1995,1996,1997 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 ;; Version: $Revision: 0.100 $
11 ;; Keywords: MIME, multimedia, multilingual, mail, news
13 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
15 ;; This program is free software; you can redistribute it and/or
16 ;; modify it under the terms of the GNU General Public License as
17 ;; published by the Free Software Foundation; either version 2, or (at
18 ;; your option) any later version.
20 ;; This program is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
32 ;; This is an Emacs minor mode for editing Internet multimedia
33 ;; messages formatted in MIME (RFC 2045, 2046, 2047, 2048 and 2049).
34 ;; All messages in this mode are composed in the tagged MIME format,
35 ;; that are described in the following examples. The messages
36 ;; composed in the tagged MIME format are automatically translated
37 ;; into a MIME compliant message when exiting the mode.
39 ;; Mule (multilingual feature of Emacs 20 and multilingual extension
40 ;; for XEmacs 20) has a capability of handling multilingual text in
41 ;; limited ISO-2022 manner that is based on early experiences in
42 ;; Japanese Internet community and resulted in RFC 1468 (ISO-2022-JP
43 ;; charset for MIME). In order to enable multilingual capability in
44 ;; single text message in MIME, charset of multilingual text written
45 ;; in Mule is declared as either `ISO-2022-JP-2' [RFC 1554]. Mule is
46 ;; required for reading the such messages.
48 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
49 ;; News mode. First of all, you need the following autoload
50 ;; definition to load mime-edit-mode automatically:
52 ;; (autoload 'turn-on-mime-edit "mime-edit"
53 ;; "Minor mode for editing MIME message." t)
55 ;; In case of Mail mode (includes VM mode), you need the following
58 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
59 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
61 ;; In case of MH-E, you need the following hook definition:
63 ;; (add-hook 'mh-letter-mode-hook
66 ;; (turn-on-mime-edit)
67 ;; (make-local-variable 'mail-header-separator)
68 ;; (setq mail-header-separator "--------")
70 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
72 ;; In case of News mode, you need the following hook definition:
74 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
75 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
77 ;; In case of Emacs 19, it is possible to emphasize the message tags
78 ;; using font-lock mode as follows:
80 ;; (add-hook 'mime-edit-mode-hook
84 ;; (setq font-lock-keywords (list mime-edit-tag-regexp))
87 ;; The message tag looks like:
89 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
91 ;; The tagged MIME message examples:
93 ;; This is a conventional plain text. It should be translated into
97 ;; This is also a plain text. But, it is explicitly specified as is.
98 ;;--[[text/plain; charset=ISO-8859-1]]
99 ;; This is also a plain text. But charset is specified as iso-8859-1.
101 ;; ¡Hola! Buenos días. ¿Cómo está usted?
102 ;;--[[text/enriched]]
103 ;; <center>This is a richtext.</center>
105 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
107 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
113 (require 'mail-utils)
116 (require 'eword-encode)
124 (defconst mime-edit-RCS-ID
125 "$Id: mime-edit.el,v 0.100 1998-02-06 00:39:23 morioka Exp $")
127 (defconst mime-edit-version `,(get-version-string mime-edit-RCS-ID))
129 (defconst mime-edit-version-name
130 `,(concat "SEMI MIME-Edit " mime-edit-version
131 " - \"" semi-version-name "\""))
137 (defgroup mime-edit nil
141 (defcustom mime-ignore-preceding-spaces nil
142 "*Ignore preceding white spaces if non-nil."
146 (defcustom mime-ignore-trailing-spaces nil
147 "*Ignore trailing white spaces if non-nil."
151 (defcustom mime-ignore-same-text-tag t
152 "*Ignore preceding text content-type tag that is same with new one.
153 If non-nil, the text tag is not inserted unless something different."
157 (defcustom mime-auto-hide-body t
158 "*Hide non-textual body encoded in base64 after insertion if non-nil."
162 (defcustom mime-edit-voice-recorder
163 (function mime-edit-voice-recorder-for-sun)
164 "*Function to record a voice message and encode it."
168 (defcustom mime-edit-mode-hook nil
169 "*Hook called when enter MIME mode."
173 (defcustom mime-edit-translate-hook nil
174 "*Hook called before translating into a MIME compliant message.
175 To insert a signature file automatically, call the function
176 `mime-edit-insert-signature' from this hook."
180 (defcustom mime-edit-exit-hook nil
181 "*Hook called when exit MIME mode."
185 (defvar mime-content-types
187 ;; Charset parameter need not to be specified, since it is
188 ;; defined automatically while translation.
190 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
193 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
196 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
199 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
202 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
210 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
211 ("directory" "/pub/GNU/elisp/mime")
213 ("mode" "image" "ascii" "local8"))
218 ("mode" "image" "ascii" "local8"))
219 ("tftp" ("site") ("name"))
220 ("afs" ("site") ("name"))
221 ("local-file" ("site") ("name"))
222 ("mail-server" ("server" "ftpmail@nic.karrn.ad.jp"))
227 ("octet-stream" ("type" "" "tar" "shar"))
229 ("x-kiss" ("x-cnf")))
243 "*Alist of content-type, subtype, parameters and its values.")
245 (defvar mime-file-types
247 "text" "richtext" nil
255 "application" "postscript" nil
257 "attachment" (("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 "inline" (("filename" . file))
302 "attachment" (("filename" . file))
307 "attachment" (("filename" . file))
310 "application" "octet-stream" (("type" . "emacs-lisp"))
312 "attachment" (("filename" . file))
315 "application" "octet-stream" (("type" . "common-lisp"))
317 "attachment" (("filename" . file))
320 "application" "octet-stream" (("type" . "tar+gzip"))
322 "attachment" (("filename" . file))
325 "application" "octet-stream" (("type" . "tar+gzip"))
327 "attachment" (("filename" . file))
330 "application" "octet-stream" (("type" . "tar+compress"))
332 "attachment" (("filename" . file))
335 "application" "octet-stream" (("type" . "tar+compress"))
337 "attachment" (("filename" . file))
340 "application" "octet-stream" (("type" . "gzip"))
342 "attachment" (("filename" . file))
345 "application" "octet-stream" (("type" . "compress"))
347 "attachment" (("filename" . file))
350 "application" "octet-stream" (("type" . "lha"))
352 "attachment" (("filename" . file))
355 "application" "zip" nil
357 "attachment" (("filename" . file))
360 "application" "octet-stream" (("type" . "patch"))
362 "attachment" (("filename" . file))
365 "application" "octet-stream" (("type" . "patch"))
367 "attachment" (("filename" . file))
370 "text" "plain" nil nil)
372 "application" "octet-stream" nil
374 "attachment" (("filename" . file))
377 "*Alist of file name, types, parameters, and default encoding.
378 If encoding is nil, it is determined from its contents.")
381 ;;; @@ about charset, encoding and transfer-level
384 (defvar mime-charset-type-list
386 (iso-8859-1 8 "quoted-printable")
387 (iso-8859-2 8 "quoted-printable")
388 (iso-8859-3 8 "quoted-printable")
389 (iso-8859-4 8 "quoted-printable")
390 (iso-8859-5 8 "quoted-printable")
391 (koi8-r 8 "quoted-printable")
392 (iso-8859-7 8 "quoted-printable")
393 (iso-8859-8 8 "quoted-printable")
394 (iso-8859-9 8 "quoted-printable")
395 (iso-2022-jp 7 "base64")
396 (iso-2022-kr 7 "base64")
398 (cn-gb2312 8 "quoted-printable")
400 (gb2312 8 "quoted-printable")
402 (iso-2022-jp-2 7 "base64")
403 (iso-2022-int-1 7 "base64")
406 (defvar mime-transfer-level 7
407 "*A number of network transfer level. It should be bigger than 7.")
408 (make-variable-buffer-local 'mime-transfer-level)
410 (defsubst mime-encoding-name (transfer-level &optional not-omit)
411 (cond ((> transfer-level 8) "binary")
412 ((= transfer-level 8) "8bit")
416 (defvar mime-transfer-level-string
417 (mime-encoding-name mime-transfer-level 'not-omit)
418 "A string formatted version of mime-transfer-level")
419 (make-variable-buffer-local 'mime-transfer-level-string)
421 (defun mime-make-charset-default-encoding-alist (transfer-level)
423 (lambda (charset-type)
424 (let ((charset (car charset-type))
425 (type (nth 1 charset-type))
426 (encoding (nth 2 charset-type))
428 (if (<= type transfer-level)
429 (cons charset (mime-encoding-name type))
430 (cons charset encoding)
432 mime-charset-type-list))
434 (defvar mime-edit-charset-default-encoding-alist
435 (mime-make-charset-default-encoding-alist mime-transfer-level))
436 (make-variable-buffer-local 'mime-edit-charset-default-encoding-alist)
439 ;;; @@ about message inserting
442 (defvar mime-edit-yank-ignored-field-list
443 '("Received" "Approved" "Path" "Replied" "Status"
444 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
445 "Delete these fields from original message when it is inserted
446 as message/rfc822 part.
447 Each elements are regexp of field-name.")
449 (defvar mime-edit-yank-ignored-field-regexp
451 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
454 (defvar mime-edit-message-inserter-alist nil)
455 (defvar mime-edit-mail-inserter-alist nil)
458 ;;; @@ about message splitting
461 (defcustom mime-edit-split-message t
462 "*Split large message if it is non-nil."
466 (defcustom mime-edit-message-default-max-lines 1000
467 "*Default maximum lines of a message."
471 (defcustom mime-edit-message-max-lines-alist
472 '((news-reply-mode . 500))
473 "Alist of major-mode vs maximum lines of a message.
474 If it is not specified for a major-mode,
475 `mime-edit-message-default-max-lines' is used."
479 (defconst mime-edit-split-ignored-field-regexp
480 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\)")
482 (defvar mime-edit-split-blind-field-regexp
483 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)")
485 (defvar mime-edit-split-message-sender-alist nil)
487 (defvar mime-edit-news-reply-mode-server-running nil)
493 (defvar mime-edit-signing-type 'pgp-elkins
494 "*PGP signing type (pgp-elkins, pgp-kazu or nil).")
496 (defvar mime-edit-encrypting-type 'pgp-elkins
497 "*PGP encrypting type (pgp-elkins, pgp-kazu or nil).")
503 (defconst mime-edit-single-part-tag-regexp
504 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
505 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
507 (defconst mime-edit-quoted-single-part-tag-regexp
508 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
510 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
512 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
514 (defconst mime-edit-beginning-tag-regexp
515 (regexp-or mime-edit-single-part-tag-regexp
516 mime-edit-multipart-beginning-regexp))
518 (defconst mime-edit-end-tag-regexp
519 (regexp-or mime-edit-single-part-tag-regexp
520 mime-edit-multipart-end-regexp))
522 (defconst mime-edit-tag-regexp
523 (regexp-or mime-edit-single-part-tag-regexp
524 mime-edit-multipart-beginning-regexp
525 mime-edit-multipart-end-regexp))
527 (defvar mime-tag-format "--[[%s]]"
528 "*Control-string making a MIME tag.")
530 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
531 "*Control-string making a MIME tag with encoding.")
534 ;;; @@ multipart boundary
537 (defvar mime-multipart-boundary "Multipart"
538 "*Boundary of a multipart message.")
541 ;;; @@ optional header fields
544 (defvar mime-edit-insert-x-emacs-field t
545 "*If non-nil, insert X-Emacs header field.")
547 (defvar mime-edit-x-emacs-value
548 (if (featurep 'xemacs)
549 (concat emacs-version (if (featurep 'mule)
552 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
553 (substring emacs-version 0 (match-beginning 0))
557 (if enable-multibyte-characters
558 (concat ", MULE " mule-version)
559 " (with raw setting)")
560 (if (featurep 'meadow)
561 (concat ", " (Meadow-version))
564 "Body of X-Emacs field.
565 If variable `mime-edit-insert-x-emacs-field' is not nil, it is
566 inserted into message header.")
572 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
573 "*Specify MIME tspecials.
574 Tspecials means any character that matches with it in header must be quoted.")
576 (defconst mime-edit-mime-version-value
577 (concat "1.0 (generated by " mime-edit-version-name ")")
578 "MIME version number.")
580 (defconst mime-edit-mime-version-field-for-message/partial
581 (concat "MIME-Version: 1.0 (split by " mime-edit-version-name ")\n")
582 "MIME version field for message/partial.")
585 ;;; @ keymap and menu
588 (defvar mime-edit-mode-flag nil)
589 (make-variable-buffer-local 'mime-edit-mode-flag)
591 (defvar mime-edit-mode-map (make-sparse-keymap)
592 "Keymap for MIME-Edit mode commands.")
594 (define-key mime-edit-mode-map
595 "\C-c\C-x\C-t" 'mime-edit-insert-text)
596 (define-key mime-edit-mode-map
597 "\C-c\C-x\C-i" 'mime-edit-insert-file)
598 (define-key mime-edit-mode-map
599 "\C-c\C-x\C-e" 'mime-edit-insert-external)
600 (define-key mime-edit-mode-map
601 "\C-c\C-x\C-v" 'mime-edit-insert-voice)
602 (define-key mime-edit-mode-map
603 "\C-c\C-x\C-y" 'mime-edit-insert-message)
604 (define-key mime-edit-mode-map
605 "\C-c\C-x\C-m" 'mime-edit-insert-mail)
606 (define-key mime-edit-mode-map
607 "\C-c\C-x\C-w" 'mime-edit-insert-signature)
608 (define-key mime-edit-mode-map
609 "\C-c\C-x\C-s" 'mime-edit-insert-signature)
610 (define-key mime-edit-mode-map
611 "\C-c\C-x\C-k" 'mime-edit-insert-key)
612 (define-key mime-edit-mode-map
613 "\C-c\C-xt" 'mime-edit-insert-tag)
615 (define-key mime-edit-mode-map
616 "\C-c\C-m\C-a" 'mime-edit-enclose-alternative-region)
617 (define-key mime-edit-mode-map
618 "\C-c\C-m\C-p" 'mime-edit-enclose-parallel-region)
619 (define-key mime-edit-mode-map
620 "\C-c\C-m\C-m" 'mime-edit-enclose-mixed-region)
621 (define-key mime-edit-mode-map
622 "\C-c\C-m\C-d" 'mime-edit-enclose-digest-region)
623 (define-key mime-edit-mode-map
624 "\C-c\C-m\C-s" 'mime-edit-enclose-signed-region)
625 (define-key mime-edit-mode-map
626 "\C-c\C-m\C-e" 'mime-edit-enclose-encrypted-region)
627 (define-key mime-edit-mode-map
628 "\C-c\C-m\C-q" 'mime-edit-enclose-quote-region)
630 (define-key mime-edit-mode-map
631 "\C-c\C-x7" 'mime-edit-set-transfer-level-7bit)
632 (define-key mime-edit-mode-map
633 "\C-c\C-x8" 'mime-edit-set-transfer-level-8bit)
634 (define-key mime-edit-mode-map
635 "\C-c\C-x/" 'mime-edit-set-split)
636 (define-key mime-edit-mode-map
637 "\C-c\C-xs" 'mime-edit-set-sign)
638 (define-key mime-edit-mode-map
639 "\C-c\C-xv" 'mime-edit-set-sign)
640 (define-key mime-edit-mode-map
641 "\C-c\C-xe" 'mime-edit-set-encrypt)
642 (define-key mime-edit-mode-map
643 "\C-c\C-xh" 'mime-edit-set-encrypt)
644 (define-key mime-edit-mode-map
645 "\C-c\C-x\C-p" 'mime-edit-preview-message)
646 (define-key mime-edit-mode-map
647 "\C-c\C-x\C-z" 'mime-edit-exit)
648 (define-key mime-edit-mode-map
649 "\C-c\C-x?" 'mime-edit-help)
651 (defconst mime-edit-menu-title "MIME-Edit")
653 (defconst mime-edit-menu-list
654 '((mime-help "Describe MIME editor mode" mime-edit-help)
655 (file "Insert File" mime-edit-insert-file)
656 (external "Insert External" mime-edit-insert-external)
657 (voice "Insert Voice" mime-edit-insert-voice)
658 (message "Insert Message" mime-edit-insert-message)
659 (mail "Insert Mail" mime-edit-insert-mail)
660 (signature "Insert Signature" mime-edit-insert-signature)
661 (text "Insert Text" mime-edit-insert-text)
662 (tag "Insert Tag" mime-edit-insert-tag)
663 (alternative "Enclose as alternative"
664 mime-edit-enclose-alternative-region)
665 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
666 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
667 (digest "Enclose as digest" mime-edit-enclose-digest-region)
668 (signed "Enclose as signed" mime-edit-enclose-signed-region)
669 (encrypted "Enclose as encrypted" mime-edit-enclose-encrypted-region)
670 (quote "Verbatim region" mime-edit-enclose-quote-region)
671 (key "Insert Public Key" mime-edit-insert-key)
672 (split "About split" mime-edit-set-split)
673 (sign "About sign" mime-edit-set-sign)
674 (encrypt "About encryption" mime-edit-set-encrypt)
675 (preview "Preview Message" mime-edit-preview-message)
676 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
678 "MIME-edit menubar entry.")
680 (cond (running-xemacs
681 ;; modified by Pekka Marjola <pema@iki.fi>
682 ;; 1995/9/5 (c.f. [tm-en:69])
683 (defun mime-edit-define-menu-for-xemacs ()
684 "Define menu for Emacs 19."
685 (cond ((featurep 'menubar)
686 (make-local-variable 'current-menubar)
687 (set-buffer-menubar current-menubar)
690 (cons mime-edit-menu-title
693 (vector (nth 1 item)(nth 2 item)
696 mime-edit-menu-list)))
699 ;; modified by Steven L. Baur <steve@miranova.com>
700 ;; 1995/12/6 (c.f. [tm-en:209])
701 (or (boundp 'mime-edit-popup-menu-for-xemacs)
702 (setq mime-edit-popup-menu-for-xemacs
703 (append '("MIME Commands" "---")
704 (mapcar (function (lambda (item)
708 mime-edit-menu-list)))
711 ((>= emacs-major-version 19)
712 (define-key mime-edit-mode-map [menu-bar mime-edit]
713 (cons mime-edit-menu-title
714 (make-sparse-keymap mime-edit-menu-title)))
717 (define-key mime-edit-mode-map
718 (vector 'menu-bar 'mime-edit (car item))
719 (cons (nth 1 item)(nth 2 item))
722 (reverse mime-edit-menu-list)
731 (defun mime-edit-mode ()
732 "MIME minor mode for editing the tagged MIME message.
734 In this mode, basically, the message is composed in the tagged MIME
735 format. The message tag looks like:
737 --[[text/plain; charset=ISO-2022-JP][7bit]]
739 The tag specifies the MIME content type, subtype, optional parameters
740 and transfer encoding of the message following the tag. Messages
741 without any tag are treated as `text/plain' by default. Charset and
742 transfer encoding are automatically defined unless explicitly
743 specified. Binary messages such as audio and image are usually
744 hidden. The messages in the tagged MIME format are automatically
745 translated into a MIME compliant message when exiting this mode.
747 Available charsets depend on Emacs version being used. The following
748 lists the available charsets of each emacs.
750 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
751 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
752 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
753 CN-BIG5 and ISO-2022-INT-1 are available.
755 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
756 be used to represent multilingual text in intermixed manner. Any
757 languages that has no registered charset are represented as either
758 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
760 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
761 without mule, please set variable `default-mime-charset'. This
762 variable must be symbol of which name is a MIME charset.
764 If you want to add more charsets in mule, please set variable
765 `charsets-mime-charset-alist'. This variable must be alist of which
766 key is list of charset and value is symbol of MIME charset. If name
767 of coding-system is different as MIME charset, please set variable
768 `mime-charset-coding-system-alist'. This variable must be alist of
769 which key is MIME charset and value is coding-system.
771 Following commands are available in addition to major mode commands:
774 \\[mime-edit-insert-text] insert a text message.
775 \\[mime-edit-insert-file] insert a (binary) file.
776 \\[mime-edit-insert-external] insert a reference to external body.
777 \\[mime-edit-insert-voice] insert a voice message.
778 \\[mime-edit-insert-message] insert a mail or news message.
779 \\[mime-edit-insert-mail] insert a mail message.
780 \\[mime-edit-insert-signature] insert a signature file at end.
781 \\[mime-edit-insert-key] insert PGP public key.
782 \\[mime-edit-insert-tag] insert a new MIME tag.
784 \[make enclosure (maybe multipart)\]
785 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
786 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
787 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
788 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
789 \\[mime-edit-enclose-signed-region] enclose as PGP signed.
790 \\[mime-edit-enclose-encrypted-region] enclose as PGP encrypted.
791 \\[mime-edit-enclose-quote-region] enclose as verbose mode (to avoid to expand tags)
794 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
795 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
796 \\[mime-edit-set-split] set message splitting mode.
797 \\[mime-edit-set-sign] set PGP-sign mode.
798 \\[mime-edit-set-encrypt] set PGP-encryption mode.
799 \\[mime-edit-preview-message] preview editing MIME message.
800 \\[mime-edit-exit] exit and translate into a MIME compliant message.
801 \\[mime-edit-help] show this help.
802 \\[mime-edit-maybe-translate] exit and translate if in MIME mode, then split.
804 Additional commands are available in some major modes:
805 C-c C-c exit, translate and run the original command.
806 C-c C-s exit, translate and run the original command.
808 The following is a message example written in the tagged MIME format.
809 TABs at the beginning of the line are not a part of the message:
811 This is a conventional plain text. It should be translated
814 This is also a plain text. But, it is explicitly specified as
816 --[[text/plain; charset=ISO-8859-1]]
817 This is also a plain text. But charset is specified as
820 ¡Hola! Buenos días. ¿Cómo está usted?
822 This is a <bold>enriched text</bold>.
823 --[[image/gif][base64]]...image encoded in base64 here...
824 --[[audio/basic][base64]]...audio encoded in base64 here...
826 User customizable variables (not documented all of them):
828 Specifies a key prefix for MIME minor mode commands.
830 mime-ignore-preceding-spaces
831 Preceding white spaces in a message body are ignored if non-nil.
833 mime-ignore-trailing-spaces
834 Trailing white spaces in a message body are ignored if non-nil.
837 Hide a non-textual body message encoded in base64 after insertion
841 A number of network transfer level. It should be bigger than 7.
842 If you are in 8bit-through environment, please set 8.
844 mime-edit-voice-recorder
845 Specifies a function to record a voice message and encode it.
846 The function `mime-edit-voice-recorder-for-sun' is for Sun
850 Turning on MIME mode calls the value of mime-edit-mode-hook, if
853 mime-edit-translate-hook
854 The value of mime-edit-translate-hook is called just before translating
855 the tagged MIME format into a MIME compliant message if it is
856 non-nil. If the hook call the function mime-edit-insert-signature,
857 the signature file will be inserted automatically.
860 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
863 (if mime-edit-mode-flag
865 (if (and (boundp 'mime-edit-touched-flag)
866 mime-edit-touched-flag)
868 (make-local-variable 'mime-edit-touched-flag)
869 (setq mime-edit-touched-flag t)
874 (cond (running-xemacs
875 (add-minor-mode 'mime-edit-mode-flag
876 '((" MIME-Edit " mime-transfer-level-string))
882 (set-alist 'minor-mode-alist
884 '((" MIME-Edit " mime-transfer-level-string)))
885 (set-alist 'minor-mode-map-alist
892 (defun turn-on-mime-edit ()
893 "Unconditionally turn on MIME-Edit mode."
895 (if mime-edit-mode-flag
896 (error "You are already editing a MIME message.")
897 (setq mime-edit-mode-flag t)
899 ;; Set transfer level into mode line
901 (setq mime-transfer-level-string
902 (mime-encoding-name mime-transfer-level 'not-omit))
903 (force-mode-line-update)
905 ;; Define menu for XEmacs.
907 (mime-edit-define-menu-for-xemacs)
912 ;; I don't care about saving these.
913 (setq paragraph-start
914 (regexp-or mime-edit-single-part-tag-regexp
916 (setq paragraph-separate
917 (regexp-or mime-edit-single-part-tag-regexp
919 (run-hooks 'mime-edit-mode-hook)
921 (substitute-command-keys
922 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
926 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
929 (defun mime-edit-exit (&optional nomime no-error)
930 "Translate the tagged MIME message into a MIME compliant message.
931 With no argument encode a message in the buffer into MIME, otherwise
932 just return to previous mode."
934 (if (not mime-edit-mode-flag)
936 (error "You aren't editing a MIME message.")
940 (run-hooks 'mime-edit-translate-hook)
941 (mime-edit-translate-buffer)))
942 ;; Restore previous state.
943 (setq mime-edit-mode-flag nil)
944 (if (and running-xemacs
946 (delete-menu-item (list mime-edit-menu-title))
949 (set-buffer-modified-p (buffer-modified-p))
950 (run-hooks 'mime-edit-exit-hook)
951 (message "Exit MIME editor mode.")
954 (defun mime-edit-maybe-translate ()
956 (mime-edit-exit nil t)
957 (call-interactively 'mime-edit-maybe-split-and-send)
960 (defun mime-edit-help ()
961 "Show help message about MIME mode."
963 (with-output-to-temp-buffer "*Help*"
964 (princ "MIME editor mode:\n")
965 (princ (documentation 'mime-edit-mode))
966 (print-help-return-message)))
968 (defun mime-edit-insert-text ()
969 "Insert a text message.
970 Charset is automatically obtained from the `charsets-mime-charset-alist'."
972 (let ((ret (mime-edit-insert-tag "text" nil nil)))
975 (if (looking-at mime-edit-single-part-tag-regexp)
977 ;; Make a space between the following message.
981 (if (and (member (cadr ret) '("enriched" "richtext"))
982 (fboundp 'enriched-mode)
985 (if (boundp 'enriched-mode)
989 (defun mime-edit-insert-file (file &optional verbose)
990 "Insert a message from a file."
991 (interactive "fInsert file as MIME message: \nP")
992 (let* ((guess (mime-find-file-type file))
994 (subtype (nth 1 guess))
995 (parameters (nth 2 guess))
996 (encoding (nth 3 guess))
997 (disposition-type (nth 4 guess))
998 (disposition-params (nth 5 guess))
1001 (setq type (mime-prompt-for-type type)
1002 subtype (mime-prompt-for-subtype type subtype)
1004 (if (or (interactive-p) verbose)
1005 (setq encoding (mime-prompt-for-encoding encoding))
1007 (if (or (consp parameters) (stringp disposition-type))
1008 (let ((rest parameters) cell attribute value)
1009 (setq parameters "")
1011 (setq cell (car rest))
1012 (setq attribute (car cell))
1013 (setq value (cdr cell))
1014 (if (eq value 'file)
1015 (setq value (std11-wrap-as-quoted-string
1016 (file-name-nondirectory file)))
1018 (setq parameters (concat parameters "; " attribute "=" value))
1019 (setq rest (cdr rest))
1021 (if disposition-type
1024 (concat parameters "\n"
1025 "Content-Disposition: " disposition-type))
1026 (setq rest disposition-params)
1028 (setq cell (car rest))
1029 (setq attribute (car cell))
1030 (setq value (cdr cell))
1031 (if (eq value 'file)
1032 (setq value (std11-wrap-as-quoted-string
1033 (file-name-nondirectory file)))
1036 (concat parameters "; " attribute "=" value))
1037 (setq rest (cdr rest))
1041 (mime-edit-insert-tag type subtype parameters)
1042 (mime-edit-insert-binary-file file encoding)
1045 (defun mime-edit-insert-external ()
1046 "Insert a reference to external body."
1048 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1050 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1052 (let* ((pritype (mime-prompt-for-type))
1053 (subtype (mime-prompt-for-subtype pritype))
1054 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1057 (insert "Content-Type: "
1058 pritype "/" subtype (or parameters "") "\n")))
1059 (if (and (not (eobp))
1060 (not (looking-at mime-edit-single-part-tag-regexp)))
1061 (insert (mime-make-text-tag) "\n")))
1063 (defun mime-edit-insert-voice ()
1064 "Insert a voice message."
1068 "What transfer encoding: "
1069 mime-file-encoding-method-alist nil t nil)))
1070 (mime-edit-insert-tag "audio" "basic" nil)
1071 (mime-edit-define-encoding encoding)
1073 (narrow-to-region (1- (point))(point))
1075 (funcall mime-edit-voice-recorder encoding)
1078 (invisible-region (point-min)(point-max))
1079 (goto-char (point-max))
1082 (defun mime-edit-insert-signature (&optional arg)
1083 "Insert a signature file."
1085 (let ((signature-insert-hook
1088 (apply (function mime-edit-insert-tag)
1089 (mime-find-file-type signature-file-name))
1092 (insert-signature arg)
1096 ;; Insert a new tag around a point.
1098 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1099 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1100 If nothing is inserted, return nil."
1103 (mime-edit-goto-tag)
1104 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1105 (< (match-beginning 0) p)
1108 (goto-char (match-beginning 0))
1117 (mime-prompt-for-type)))
1120 (mime-prompt-for-subtype pritype)))
1123 (mime-prompt-for-parameters pritype subtype delimiter)))
1124 ;; Make a new MIME tag.
1125 (setq newtag (mime-make-tag pritype subtype parameters))
1126 ;; Find an current MIME tag.
1129 (if (mime-edit-goto-tag)
1130 (buffer-substring (match-beginning 0) (match-end 0))
1131 ;; Assume content type is 'text/plan'.
1132 (mime-make-tag "text" "plain")
1134 ;; We are only interested in TEXT.
1136 (not (mime-test-content-type
1137 (mime-edit-get-contype oldtag) "text")))
1140 (if (or (not oldtag) ;Not text
1141 (or mime-ignore-same-text-tag
1142 (not (string-equal oldtag newtag))))
1144 ;; Mark the beginning of the tag for convenience.
1145 (push-mark (point) 'nomsg)
1146 (insert newtag "\n")
1147 (list pritype subtype parameters) ;New tag is created.
1149 ;; Restore previous point.
1151 nil ;Nothing is created.
1155 (defun mime-edit-insert-binary-file (file &optional encoding)
1156 "Insert binary FILE at point.
1157 Optional argument ENCODING specifies an encoding method such as base64."
1158 (let* ((tagend (1- (point))) ;End of the tag
1159 (hide-p (and mime-auto-hide-body
1162 (let ((en (downcase encoding)))
1163 (or (string-equal en "7bit")
1164 (string-equal en "8bit")
1165 (string-equal en "binary")
1169 (narrow-to-region tagend (point))
1170 (mime-insert-encoded-file file encoding)
1173 (invisible-region (point-min) (point-max))
1174 (goto-char (point-max))
1176 (goto-char (point-max))
1179 (looking-at mime-edit-tag-regexp)
1180 (= (point)(point-max))
1181 (mime-edit-insert-tag "text" "plain")
1183 ;; Define encoding even if it is 7bit.
1184 (if (stringp encoding)
1186 (goto-char tagend) ; Make sure which line the tag is on.
1187 (mime-edit-define-encoding encoding)
1192 ;; Commands work on a current message flagment.
1194 (defun mime-edit-goto-tag ()
1195 "Search for the beginning of the tagged MIME message."
1196 (let ((current (point)))
1197 (if (looking-at mime-edit-tag-regexp)
1199 ;; At first, go to the end.
1200 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1201 (goto-char (1- (match-beginning 0))) ;For multiline tag
1204 (goto-char (point-max))
1206 ;; Then search for the beginning.
1207 (re-search-backward mime-edit-end-tag-regexp nil t)
1208 (or (looking-at mime-edit-beginning-tag-regexp)
1209 ;; Restore previous point.
1216 (defun mime-edit-content-beginning ()
1217 "Return the point of the beginning of content."
1219 (let ((beg (save-excursion
1220 (beginning-of-line) (point))))
1221 (if (mime-edit-goto-tag)
1222 (let ((top (point)))
1223 (goto-char (match-end 0))
1224 (if (and (= beg top)
1225 (= (following-char) ?\^M))
1229 ;; Default text/plain tag.
1230 (goto-char (point-min))
1232 (concat "\n" (regexp-quote mail-header-separator)
1233 (if mime-ignore-preceding-spaces
1234 "[ \t\n]*\n" "\n")) nil 'move)
1238 (defun mime-edit-content-end ()
1239 "Return the point of the end of content."
1241 (if (mime-edit-goto-tag)
1243 (goto-char (match-end 0))
1244 (if (invisible-p (point))
1245 (next-visible-point (point))
1246 ;; Move to the end of this text.
1247 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1248 ;; Don't forget a multiline tag.
1249 (goto-char (match-beginning 0))
1253 ;; Assume the message begins with text/plain.
1254 (goto-char (mime-edit-content-beginning))
1255 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1256 ;; Don't forget a multiline tag.
1257 (goto-char (match-beginning 0)))
1261 (defun mime-edit-define-charset (charset)
1262 "Set charset of current tag to CHARSET."
1264 (if (mime-edit-goto-tag)
1265 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1266 (delete-region (match-beginning 0) (match-end 0))
1269 (mime-edit-set-parameter
1270 (mime-edit-get-contype tag)
1271 "charset" (upcase (symbol-name charset)))
1272 (mime-edit-get-encoding tag)))
1275 (defun mime-edit-define-encoding (encoding)
1276 "Set encoding of current tag to ENCODING."
1278 (if (mime-edit-goto-tag)
1279 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1280 (delete-region (match-beginning 0) (match-end 0))
1281 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1284 (defun mime-edit-choose-charset ()
1285 "Choose charset of a text following current point."
1286 (detect-mime-charset-region (point) (mime-edit-content-end))
1289 (defun mime-make-text-tag (&optional subtype)
1290 "Make a tag for a text after current point.
1291 Subtype of text type can be specified by an optional argument SUBTYPE.
1292 Otherwise, it is obtained from mime-content-types."
1293 (let* ((pritype "text")
1294 (subtype (or subtype
1295 (car (car (cdr (assoc pritype mime-content-types)))))))
1296 ;; Charset should be defined later.
1297 (mime-make-tag pritype subtype)))
1300 ;; Tag handling functions
1302 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1303 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1304 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1308 (defun mime-create-tag (contype &optional encoding)
1309 "Make a tag with CONTENT-TYPE and optional ENCODING."
1310 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1313 (defun mime-edit-get-contype (tag)
1314 "Return Content-Type (including parameters) of TAG."
1316 (or (string-match mime-edit-single-part-tag-regexp tag)
1317 (string-match mime-edit-multipart-beginning-regexp tag)
1318 (string-match mime-edit-multipart-end-regexp tag)
1320 (substring tag (match-beginning 1) (match-end 1))
1323 (defun mime-edit-get-encoding (tag)
1324 "Return encoding of TAG."
1326 (string-match mime-edit-single-part-tag-regexp tag)
1328 (not (= (match-beginning 3) (match-end 3)))
1329 (substring tag (match-beginning 3) (match-end 3))))
1331 (defun mime-get-parameter (contype parameter)
1332 "For given CONTYPE return value for PARAMETER.
1333 Nil if no such parameter."
1337 (regexp-quote parameter)
1338 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1340 (substring contype (match-beginning 1) (match-end 1))
1341 nil ;No such parameter
1344 (defun mime-edit-set-parameter (contype parameter value)
1345 "For given CONTYPE set PARAMETER to VALUE."
1346 (let (ctype opt-fields)
1347 (if (string-match "\n[^ \t\n\r]+:" contype)
1348 (setq ctype (substring contype 0 (match-beginning 0))
1349 opt-fields (substring contype (match-beginning 0)))
1350 (setq ctype contype)
1355 (regexp-quote parameter)
1356 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1359 (concat (substring ctype 0 (match-beginning 1))
1361 (substring contype (match-end 1))
1363 (concat ctype "; " parameter "=" value opt-fields)
1366 (defun mime-strip-parameters (contype)
1367 "Return primary content-type and subtype without parameters for CONTYPE."
1368 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1369 (substring contype (match-beginning 1) (match-end 1)) nil))
1371 (defun mime-test-content-type (contype type &optional subtype)
1372 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1373 (and (stringp contype)
1376 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1377 (downcase contype))))
1382 (defun mime-find-file-type (file)
1383 "Guess Content-Type, subtype, and parameters from FILE."
1385 (guesses mime-file-types))
1386 (while (and (not guess) guesses)
1387 (if (string-match (car (car guesses)) file)
1388 (setq guess (cdr (car guesses))))
1389 (setq guesses (cdr guesses)))
1393 (defun mime-prompt-for-type (&optional default)
1394 "Ask for Content-type."
1396 ;; Repeat until primary content type is specified.
1397 (while (string-equal type "")
1399 (completing-read "What content type: "
1402 'require-match ;Type must be specified.
1405 (if (string-equal type "")
1407 (message "Content type is required.")
1414 (defun mime-prompt-for-subtype (type &optional default)
1415 "Ask for subtype of media-type TYPE."
1416 (let ((subtypes (cdr (assoc type mime-content-types))))
1418 (assoc default subtypes))
1419 (setq default (car (car subtypes)))
1425 "What content subtype: (default " default ") ")
1426 "What content subtype: ")
1427 (cdr (assoc type mime-content-types))
1429 'require-match ;Subtype must be specified.
1432 (if (string-equal answer "") default answer)))
1434 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1435 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1436 Optional DELIMITER specifies parameter delimiter (';' by default)."
1437 (let* ((delimiter (or delimiter "; "))
1442 (mime-prompt-for-parameters-1
1444 (cdr (assoc pritype mime-content-types))))))
1447 (if (and (stringp parameters)
1448 (not (string-equal parameters "")))
1449 (concat delimiter parameters)
1450 "" ;"" if no parameters
1453 (defun mime-prompt-for-parameters-1 (optlist)
1454 (apply (function append)
1455 (mapcar (function mime-prompt-for-parameter) optlist)))
1457 (defun mime-prompt-for-parameter (parameter)
1459 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1460 (let* ((prompt (car parameter))
1461 (choices (mapcar (function
1463 (if (consp e) e (list e))))
1465 (default (car (car choices)))
1471 (concat "What " prompt
1473 (if (string-equal default "") "\"\"" default)
1475 choices nil nil ""))
1476 ;; If nothing is selected, use default.
1477 (if (string-equal answer "")
1478 (setq answer default)))
1480 (read-string (concat "What " prompt ": "))))
1481 (cons (if (and answer
1482 (not (string-equal answer "")))
1484 ;; Note: control characters ignored!
1485 (if (string-match mime-tspecials-regexp answer)
1486 (concat "\"" answer "\"") answer)))
1487 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1490 (defun mime-prompt-for-encoding (default)
1491 "Ask for Content-Transfer-Encoding."
1496 "What transfer encoding: "
1497 mime-file-encoding-method-alist nil t default)
1503 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1506 (defvar mime-edit-translate-buffer-hook
1507 '(mime-edit-pgp-enclose-buffer
1508 mime-edit-translate-body
1509 mime-edit-translate-header))
1511 (defun mime-edit-translate-header ()
1512 "Encode the message header into network representation."
1513 (eword-encode-header 'code-conversion)
1514 (run-hooks 'mime-edit-translate-header-hook)
1517 (defun mime-edit-translate-buffer ()
1518 "Encode the tagged MIME message in current buffer in MIME compliant message."
1520 (if (catch 'mime-edit-error
1522 (run-hooks 'mime-edit-translate-buffer-hook)
1526 (error "Translation error!")
1529 (defun mime-edit-find-inmost ()
1530 (goto-char (point-min))
1531 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1532 (let ((bb (match-beginning 0))
1534 (type (buffer-substring (match-beginning 1)(match-end 1)))
1536 (setq end-exp (format "--}-<<%s>>\n" type))
1538 (if (re-search-forward end-exp nil t)
1540 (setq eb (match-beginning 0))
1541 (setq ee (match-end 0))
1543 (setq eb (point-max))
1544 (setq ee (point-max))
1546 (narrow-to-region be eb)
1548 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1550 (narrow-to-region (match-beginning 0)(point-max))
1551 (mime-edit-find-inmost)
1554 (list type bb be eb)
1557 (defun mime-edit-process-multipart-1 (boundary)
1558 (let ((ret (mime-edit-find-inmost)))
1560 (let ((type (car ret))
1561 (bb (nth 1 ret))(be (nth 2 ret))
1564 (narrow-to-region bb eb)
1565 (delete-region bb be)
1566 (setq bb (point-min))
1567 (setq eb (point-max))
1570 (if (looking-at mime-edit-multipart-end-regexp)
1571 (let ((beg (match-beginning 0))
1574 (delete-region beg end)
1575 (or (looking-at mime-edit-beginning-tag-regexp)
1577 (insert (concat (mime-make-text-tag) "\n"))
1579 (cond ((string-equal type "quote")
1580 (mime-edit-enquote-region bb eb)
1582 ((string-equal type "signed")
1583 (cond ((eq mime-edit-signing-type 'pgp-elkins)
1584 (mime-edit-sign-pgp-elkins bb eb boundary)
1586 ((eq mime-edit-signing-type 'pgp-kazu)
1587 (mime-edit-sign-pgp-kazu bb eb boundary)
1590 ((string-equal type "encrypted")
1591 (cond ((eq mime-edit-encrypting-type 'pgp-elkins)
1592 (mime-edit-encrypt-pgp-elkins bb eb boundary)
1594 ((eq mime-edit-encrypting-type 'pgp-kazu)
1595 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1599 (nth 2 (mime-edit-translate-region bb eb
1603 (format "--[[multipart/%s;
1604 boundary=\"%s\"][7bit]]\n"
1609 (defun mime-edit-enquote-region (beg end)
1612 (narrow-to-region beg end)
1614 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1615 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1616 (replace-match (concat "- " (substring tag 1)))
1619 (defun mime-edit-dequote-region (beg end)
1622 (narrow-to-region beg end)
1624 (while (re-search-forward
1625 mime-edit-quoted-single-part-tag-regexp nil t)
1626 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1627 (replace-match (concat "-" (substring tag 2)))
1630 (defun mime-edit-sign-pgp-elkins (beg end boundary)
1633 (narrow-to-region beg end)
1635 (mime-edit-translate-region beg end boundary))
1637 (encoding (nth 1 ret))
1639 (pgp-boundary (concat "pgp-sign-" boundary))
1642 (insert (format "Content-Type: %s\n" ctype))
1644 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1647 (or (as-binary-process
1648 (funcall (pgp-function 'mime-sign)
1649 (point-min)(point-max) nil nil pgp-boundary))
1650 (throw 'mime-edit-error 'pgp-error)
1654 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1656 (defun mime-edit-make-encrypt-recipient-header ()
1657 (let* ((names mime-edit-encrypt-recipient-fields-list)
1659 (std11-field-bodies (cons "From" names)
1660 nil mail-header-separator))
1663 (setq values (cdr values))))
1664 (header (and (stringp from)
1665 (if (string-equal from "")
1667 (format "From: %s\n" from)
1670 (while (and names values)
1671 (let ((name (car names))
1672 (value (car values))
1674 (and (stringp value)
1675 (or (string-equal value "")
1677 (setq header (concat header name ": " value "\n")
1678 recipients (if recipients
1679 (concat recipients " ," value)
1682 (setq names (cdr names)
1683 values (cdr values))
1685 (vector from recipients header)
1688 (defun mime-edit-encrypt-pgp-elkins (beg end boundary)
1691 (let (from recipients header)
1692 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1693 (setq from (aref ret 0)
1694 recipients (aref ret 1)
1695 header (aref ret 2))
1697 (narrow-to-region beg end)
1699 (mime-edit-translate-region beg end boundary))
1701 (encoding (nth 1 ret))
1703 (pgp-boundary (concat "pgp-" boundary))
1707 (insert (format "Content-Type: %s\n" ctype))
1709 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1712 (or (funcall (pgp-function 'encrypt)
1713 recipients (point-min) (point-max) from)
1714 (throw 'mime-edit-error 'pgp-error)
1717 (insert (format "--[[multipart/encrypted;
1719 protocol=\"application/pgp-encrypted\"][7bit]]
1721 Content-Type: application/pgp-encrypted
1724 Content-Type: application/octet-stream
1725 Content-Transfer-Encoding: 7bit
1727 " pgp-boundary pgp-boundary pgp-boundary))
1728 (goto-char (point-max))
1729 (insert (format "\n--%s--\n" pgp-boundary))
1732 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1735 (narrow-to-region beg end)
1737 (mime-edit-translate-region beg end boundary))
1739 (encoding (nth 1 ret))
1743 (insert (format "Content-Type: %s\n" ctype))
1745 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1748 (or (as-binary-process
1749 (funcall (pgp-function 'traditional-sign)
1751 (throw 'mime-edit-error 'pgp-error)
1755 "--[[application/pgp; format=mime][7bit]]\n")
1759 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1761 (let (from recipients header)
1762 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1763 (setq from (aref ret 0)
1764 recipients (aref ret 1)
1765 header (aref ret 2))
1768 (narrow-to-region beg end)
1770 (mime-edit-translate-region beg end boundary))
1772 (encoding (nth 1 ret))
1777 (insert (format "Content-Type: %s\n" ctype))
1779 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1782 (or (as-binary-process
1783 (funcall (pgp-function 'encrypt)
1784 recipients beg (point-max) nil 'maybe)
1786 (throw 'mime-edit-error 'pgp-error)
1790 "--[[application/pgp; format=mime][7bit]]\n")
1794 (defsubst replace-space-with-underline (str)
1795 (mapconcat (function
1803 (defun mime-edit-make-boundary ()
1804 (concat mime-multipart-boundary "_"
1805 (replace-space-with-underline (current-time-string))
1808 (defun mime-edit-translate-body ()
1809 "Encode the tagged MIME body in current buffer in MIME compliant message."
1812 (let ((boundary (mime-edit-make-boundary))
1815 (while (mime-edit-process-multipart-1
1816 (format "%s-%d" boundary i))
1820 ;; We are interested in message body.
1823 (goto-char (point-min))
1825 (concat "\n" (regexp-quote mail-header-separator)
1826 (if mime-ignore-preceding-spaces
1827 "[ \t\n]*\n" "\n")) nil 'move)
1831 (goto-char (point-max))
1832 (and mime-ignore-trailing-spaces
1833 (re-search-backward "[^ \t\n]\n" beg t)
1836 (setq ret (mime-edit-translate-region
1838 (format "%s-%d" boundary i)))
1840 (mime-edit-dequote-region (point-min)(point-max))
1841 (let ((contype (car ret)) ;Content-Type
1842 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1844 ;; Insert X-Emacs field
1845 (and mime-edit-insert-x-emacs-field
1846 (or (mail-position-on-field "X-Emacs")
1847 (insert mime-edit-x-emacs-value)
1849 ;; Make primary MIME headers.
1850 (or (mail-position-on-field "MIME-Version")
1851 (insert mime-edit-mime-version-value))
1852 ;; Remove old Content-Type and other fields.
1854 (goto-char (point-min))
1855 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1856 (narrow-to-region (point-min) (point))
1857 (goto-char (point-min))
1858 (mime-delete-field "Content-Type")
1859 (mime-delete-field "Content-Transfer-Encoding"))
1860 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1861 (mail-position-on-field "Content-Type")
1865 (mail-position-on-field "Content-Transfer-Encoding")
1869 (defun mime-edit-translate-single-part-tag (&optional prefix)
1870 "Translate single-part-tag to MIME header."
1871 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1872 (let* ((beg (match-beginning 0))
1874 (tag (buffer-substring beg end))
1876 (delete-region beg end)
1877 (let ((contype (mime-edit-get-contype tag))
1878 (encoding (mime-edit-get-encoding tag))
1880 (insert (concat prefix "--" boundary "\n"))
1882 (narrow-to-region (point)(point))
1883 (insert "Content-Type: " contype "\n")
1885 (insert "Content-Transfer-Encoding: " encoding "\n"))
1886 (eword-encode-header)
1890 (defun mime-edit-translate-region (beg end &optional boundary multipart)
1892 (setq boundary (mime-edit-make-boundary))
1896 (narrow-to-region beg end)
1897 (let ((tag nil) ;MIME tag
1898 (contype nil) ;Content-Type
1899 (encoding nil) ;Content-Transfer-Encoding
1900 (nparts 0)) ;Number of body parts
1901 ;; Normalize the body part by inserting appropriate message
1902 ;; tags for every message contents.
1903 (mime-edit-normalize-body)
1904 ;; Counting the number of Content-Type.
1905 (goto-char (point-min))
1906 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1907 (setq nparts (1+ nparts)))
1908 ;; Begin translation.
1910 ((and (<= nparts 1)(not multipart))
1911 ;; It's a singular message.
1912 (goto-char (point-min))
1913 (while (re-search-forward
1914 mime-edit-single-part-tag-regexp nil t)
1916 (buffer-substring (match-beginning 0) (match-end 0)))
1917 (delete-region (match-beginning 0) (1+ (match-end 0)))
1918 (setq contype (mime-edit-get-contype tag))
1919 (setq encoding (mime-edit-get-encoding tag))
1922 ;; It's a multipart message.
1923 (goto-char (point-min))
1924 (and (mime-edit-translate-single-part-tag)
1925 (while (mime-edit-translate-single-part-tag "\n"))
1927 ;; Define Content-Type as "multipart/mixed".
1929 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
1930 ;; Content-Transfer-Encoding must be "7bit".
1931 ;; The following encoding can be `nil', but is
1932 ;; specified as is since there is no way that a user
1934 (setq encoding "7bit")
1935 ;; Insert the trailer.
1936 (goto-char (point-max))
1937 (insert "\n--" boundary "--\n")
1939 (list contype encoding boundary nparts)
1942 (defun mime-edit-normalize-body ()
1943 "Normalize the body part by inserting appropriate message tags."
1944 ;; Insert the first MIME tags if necessary.
1945 (goto-char (point-min))
1946 (if (not (looking-at mime-edit-single-part-tag-regexp))
1947 (insert (mime-make-text-tag) "\n"))
1948 ;; Check each tag, and add new tag or correct it if necessary.
1949 (goto-char (point-min))
1950 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1951 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
1952 (contype (mime-edit-get-contype tag))
1953 (charset (mime-get-parameter contype "charset"))
1954 (encoding (mime-edit-get-encoding tag)))
1955 ;; Remove extra whitespaces after the tag.
1956 (if (looking-at "[ \t]+$")
1957 (delete-region (match-beginning 0) (match-end 0)))
1959 (end (mime-edit-content-end))
1961 (if (= end (point-max))
1964 (or (looking-at mime-edit-beginning-tag-regexp)
1966 (insert (mime-make-text-tag) "\n")
1968 (visible-region beg end)
1972 ((mime-test-content-type contype "message")
1973 ;; Content-type "message" should be sent as is.
1976 ((mime-test-content-type contype "text")
1977 ;; Define charset for text if necessary.
1978 (setq charset (if charset
1979 (intern (downcase charset))
1980 (mime-edit-choose-charset)))
1981 (mime-edit-define-charset charset)
1982 (cond ((string-equal contype "text/x-rot13-47-48")
1985 (mule-caesar-region (point) (mime-edit-content-end))
1987 ((string-equal contype "text/enriched")
1992 (end (mime-edit-content-end))
1994 ;; Patch for hard newlines
1997 ;; (while (search-forward "\n" end t)
1998 ;; (put-text-property (match-beginning 0)
2001 ;; End patch for hard newlines
2002 (enriched-encode beg end)
2004 (if (search-forward "\n\n")
2005 (delete-region beg (match-end 0))
2008 ;; Point is now on current tag.
2009 ;; Define encoding and encode text if necessary.
2010 (or encoding ;Encoding is not specified.
2014 mime-edit-charset-default-encoding-alist)
2016 (beg (mime-edit-content-beginning))
2018 (encode-mime-charset-region beg (mime-edit-content-end)
2020 ;; Protect "From " in beginning of line
2022 (narrow-to-region beg (mime-edit-content-end))
2024 (let (case-fold-search)
2025 (if (re-search-forward "^From " nil t)
2027 (if (memq charset '(iso-2022-jp
2032 (replace-match "\e(BFrom ")
2033 (re-search-forward "^From " nil t)
2035 (setq encoding "quoted-printable")
2037 ;; canonicalize line break code
2038 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2040 (narrow-to-region beg (mime-edit-content-end))
2042 (while (re-search-forward "\\([^\r]\\)\n" nil t)
2044 (concat (buffer-substring (match-beginning 0)
2045 (match-end 1)) "\r\n"))
2048 (mime-encode-region beg (mime-edit-content-end) encoding)
2049 (mime-edit-define-encoding encoding)
2051 (goto-char (mime-edit-content-end))
2053 ((null encoding) ;Encoding is not specified.
2054 ;; Application, image, audio, video, and any other
2055 ;; unknown content-type without encoding should be
2057 (let* ((encoding "base64") ;Encode in BASE64 by default.
2058 (beg (mime-edit-content-beginning))
2059 (end (mime-edit-content-end))
2060 (body (buffer-substring beg end))
2062 (mime-encode-region beg end encoding)
2063 (mime-edit-define-encoding encoding))
2068 (defun mime-delete-field (field)
2069 "Delete header FIELD."
2070 (let ((regexp (format "^%s:[ \t]*" field)))
2071 (goto-char (point-min))
2072 (while (re-search-forward regexp nil t)
2073 (delete-region (match-beginning 0)
2074 (progn (forward-line 1) (point)))
2079 ;;; Platform dependent functions
2082 ;; Sun implementations
2084 (defun mime-edit-voice-recorder-for-sun (encoding)
2085 "Record voice in a buffer using Sun audio device,
2086 and insert data encoded as ENCODING."
2087 (message "Start the recording on %s. Type C-g to finish the recording..."
2089 (mime-insert-encoded-file "/dev/audio" encoding)
2093 ;;; @ Other useful commands.
2096 ;; Message forwarding commands as content-type "message/rfc822".
2098 (defun mime-edit-insert-message (&optional message)
2100 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2101 (if (and inserter (fboundp inserter))
2103 (mime-edit-insert-tag "message" "rfc822")
2104 (funcall inserter message)
2106 (message "Sorry, I don't have message inserter for your MUA.")
2109 (defun mime-edit-insert-mail (&optional message)
2111 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2112 (if (and inserter (fboundp inserter))
2114 (mime-edit-insert-tag "message" "rfc822")
2115 (funcall inserter message)
2117 (message "Sorry, I don't have mail inserter for your MUA.")
2120 (defun mime-edit-inserted-message-filter ()
2123 (let ((header-start (point))
2124 (case-fold-search t)
2127 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2128 (if (re-search-forward "^$" (mark t))
2129 (narrow-to-region header-start (match-beginning 0))
2131 (goto-char header-start)
2132 (while (and (re-search-forward
2133 mime-edit-yank-ignored-field-regexp nil t)
2134 (setq beg (match-beginning 0))
2135 (setq end (1+ (std11-field-end)))
2137 (delete-region beg end)
2142 ;;; @ multipart enclosure
2145 (defun mime-edit-enclose-region-internal (type beg end)
2148 (let ((current (point)))
2150 (narrow-to-region beg end)
2151 (insert (format "--<<%s>>-{\n" type))
2152 (goto-char (point-max))
2153 (insert (format "--}-<<%s>>\n" type))
2154 (goto-char (point-max))
2156 (or (looking-at mime-edit-beginning-tag-regexp)
2158 (insert (mime-make-text-tag) "\n")
2162 (defun mime-edit-enclose-quote-region (beg end)
2164 (mime-edit-enclose-region-internal 'quote beg end)
2167 (defun mime-edit-enclose-mixed-region (beg end)
2169 (mime-edit-enclose-region-internal 'mixed beg end)
2172 (defun mime-edit-enclose-parallel-region (beg end)
2174 (mime-edit-enclose-region-internal 'parallel beg end)
2177 (defun mime-edit-enclose-digest-region (beg end)
2179 (mime-edit-enclose-region-internal 'digest beg end)
2182 (defun mime-edit-enclose-alternative-region (beg end)
2184 (mime-edit-enclose-region-internal 'alternative beg end)
2187 (defun mime-edit-enclose-signed-region (beg end)
2189 (if mime-edit-signing-type
2190 (mime-edit-enclose-region-internal 'signed beg end)
2191 (message "Please specify signing type.")
2194 (defun mime-edit-enclose-encrypted-region (beg end)
2196 (if mime-edit-signing-type
2197 (mime-edit-enclose-region-internal 'encrypted beg end)
2198 (message "Please specify encrypting type.")
2201 (defun mime-edit-insert-key (&optional arg)
2202 "Insert a pgp public key."
2204 (mime-edit-insert-tag "application" "pgp-keys")
2205 (mime-edit-define-encoding "7bit")
2206 (funcall (pgp-function 'insert-key))
2213 (defun mime-edit-set-split (arg)
2216 (y-or-n-p "Do you want to enable split? ")
2218 (setq mime-edit-split-message arg)
2220 (message "This message is enabled to split.")
2221 (message "This message is not enabled to split.")
2224 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2225 "Toggle transfer-level is 7bit or 8bit through.
2227 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2229 (if (numberp transfer-level)
2230 (setq mime-transfer-level transfer-level)
2231 (if (< mime-transfer-level 8)
2232 (setq mime-transfer-level 8)
2233 (setq mime-transfer-level 7)
2235 (setq mime-edit-charset-default-encoding-alist
2236 (mime-make-charset-default-encoding-alist mime-transfer-level))
2237 (message (format "Current transfer-level is %d bit"
2238 mime-transfer-level))
2239 (setq mime-transfer-level-string
2240 (mime-encoding-name mime-transfer-level 'not-omit))
2241 (force-mode-line-update)
2244 (defun mime-edit-set-transfer-level-7bit ()
2246 (mime-edit-toggle-transfer-level 7)
2249 (defun mime-edit-set-transfer-level-8bit ()
2251 (mime-edit-toggle-transfer-level 8)
2258 (defun mime-edit-set-sign (arg)
2261 (y-or-n-p "Do you want to sign? ")
2264 (if mime-edit-signing-type
2266 (setq mime-edit-pgp-processing 'sign)
2267 (message "This message will be signed.")
2269 (message "Please specify signing type.")
2271 (if (eq mime-edit-pgp-processing 'sign)
2272 (setq mime-edit-pgp-processing nil)
2274 (message "This message will not be signed.")
2277 (defun mime-edit-set-encrypt (arg)
2280 (y-or-n-p "Do you want to encrypt? ")
2283 (if mime-edit-encrypting-type
2285 (setq mime-edit-pgp-processing 'encrypt)
2286 (message "This message will be encrypt.")
2288 (message "Please specify encrypting type.")
2290 (if (eq mime-edit-pgp-processing 'encrypt)
2291 (setq mime-edit-pgp-processing nil)
2293 (message "This message will not be encrypt.")
2296 (defvar mime-edit-pgp-processing nil)
2297 (make-variable-buffer-local 'mime-edit-pgp-processing)
2299 (defun mime-edit-pgp-enclose-buffer ()
2300 (let ((beg (save-excursion
2301 (goto-char (point-min))
2302 (if (search-forward (concat "\n" mail-header-separator "\n"))
2308 (cond ((eq mime-edit-pgp-processing 'sign)
2309 (mime-edit-enclose-signed-region beg end)
2311 ((eq mime-edit-pgp-processing 'encrypt)
2312 (mime-edit-enclose-encrypted-region beg end)
2320 (defun mime-edit-insert-partial-header (fields subject
2321 id number total separator)
2323 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2324 (insert mime-edit-mime-version-field-for-message/partial)
2326 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2327 id number total separator))
2330 (defun mime-edit-split-and-send
2331 (&optional cmd lines mime-edit-message-max-length)
2335 (count-lines (point-min) (point-max)))
2337 (or mime-edit-message-max-length
2338 (setq mime-edit-message-max-length
2339 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2340 mime-edit-message-default-max-lines))
2342 (let* ((mime-edit-draft-file-name
2343 (or (buffer-file-name)
2345 (expand-file-name "mime-draft" mime-temp-directory))))
2346 (separator mail-header-separator)
2348 (replace-space-with-underline (current-time-string))
2349 "@" (system-name) "\"")))
2350 (run-hooks 'mime-edit-before-split-hook)
2351 (let ((the-buf (current-buffer))
2352 (copy-buf (get-buffer-create " *Original Message*"))
2353 (header (std11-header-string-except
2354 mime-edit-split-ignored-field-regexp separator))
2355 (subject (mail-fetch-field "subject"))
2356 (total (+ (/ lines mime-edit-message-max-length)
2357 (if (> (mod lines mime-edit-message-max-length) 0)
2363 mime-edit-split-message-sender-alist))
2367 (error "Split sender is not specified for `%s'." major-mode)
2370 (mime-edit-partial-number 1)
2373 (set-buffer copy-buf)
2375 (insert-buffer the-buf)
2377 (if (re-search-forward
2378 (concat "^" (regexp-quote separator) "$") nil t)
2379 (let ((he (match-beginning 0)))
2381 (narrow-to-region (point-min) he)
2383 (goto-char (point-min))
2384 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2385 (delete-region (match-beginning 0)
2386 (1+ (std11-field-end)))
2388 (while (< mime-edit-partial-number total)
2391 (set-buffer copy-buf)
2392 (setq data (buffer-substring
2395 (goto-line mime-edit-message-max-length)
2398 (delete-region (point-min)(point))
2400 (mime-edit-insert-partial-header
2401 header subject id mime-edit-partial-number total separator)
2404 (message (format "Sending %d/%d..."
2405 mime-edit-partial-number total))
2406 (call-interactively command)
2407 (message (format "Sending %d/%d... done"
2408 mime-edit-partial-number total))
2410 (setq mime-edit-partial-number
2411 (1+ mime-edit-partial-number))
2415 (set-buffer copy-buf)
2416 (setq data (buffer-string))
2419 (mime-edit-insert-partial-header
2420 header subject id mime-edit-partial-number total separator)
2423 (message (format "Sending %d/%d..."
2424 mime-edit-partial-number total))
2425 (message (format "Sending %d/%d... done"
2426 mime-edit-partial-number total))
2430 (defun mime-edit-maybe-split-and-send (&optional cmd)
2432 (run-hooks 'mime-edit-before-send-hook)
2433 (let ((mime-edit-message-max-length
2434 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2435 mime-edit-message-default-max-lines))
2436 (lines (count-lines (point-min) (point-max)))
2438 (if (and (> lines mime-edit-message-max-length)
2439 mime-edit-split-message)
2440 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2444 ;;; @ preview message
2447 (defvar mime-edit-buffer nil) ; buffer local variable
2449 (defun mime-edit-preview-message ()
2450 "preview editing MIME message."
2452 (let* ((str (buffer-string))
2453 (separator mail-header-separator)
2454 (the-buf (current-buffer))
2455 (buf-name (buffer-name))
2456 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2457 (buf (get-buffer temp-buf-name))
2461 (switch-to-buffer buf)
2464 (setq buf (get-buffer-create temp-buf-name))
2465 (switch-to-buffer buf)
2468 (setq major-mode 'mime-temp-message-mode)
2469 (make-local-variable 'mail-header-separator)
2470 (setq mail-header-separator separator)
2471 (make-local-variable 'mime-edit-buffer)
2472 (setq mime-edit-buffer the-buf)
2474 (run-hooks 'mime-edit-translate-hook)
2475 (mime-edit-translate-buffer)
2476 (goto-char (point-min))
2477 (if (re-search-forward
2478 (concat "^" (regexp-quote separator) "$"))
2484 (defun mime-edit-quitting-method ()
2485 "Quitting method for mime-view."
2486 (let ((temp mime-raw-buffer)
2488 (mime-view-kill-buffer)
2490 (setq buf mime-edit-buffer)
2492 (switch-to-buffer buf)
2495 (set-alist 'mime-view-quitting-method-alist
2496 'mime-temp-message-mode
2497 (function mime-edit-quitting-method)
2504 (defvar mime-edit-again-ignored-field-regexp
2505 (concat "^\\(" "Content-.*\\|Mime-Version"
2506 (if mime-edit-insert-x-emacs-field "\\|X-Emacs")
2508 "Regexp for deleted header fields when `mime-edit-again' is called.")
2510 (defun mime-edit-decode-buffer (not-decode-text)
2512 (goto-char (point-min))
2513 (let ((ctl (mime-read-Content-Type)))
2515 (let ((type (car ctl))
2516 (stype (car (cdr ctl)))
2517 (params (cdr (cdr ctl)))
2520 ((and (eq type 'application)(eq stype 'pgp-signature))
2521 (delete-region (point-min)(point-max))
2523 ((eq type 'multipart)
2524 (let* ((boundary (cdr (assoc "boundary" params)))
2526 (concat "\n--" (regexp-quote boundary) "[ \t]*\n"))
2528 (re-search-forward boundary-pat nil t)
2529 (let ((bb (match-beginning 0)) eb tag)
2530 (setq tag (format "\n--<<%s>>-{\n" stype))
2533 (setq bb (+ bb (length tag)))
2535 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2537 (setq eb (match-beginning 0))
2538 (replace-match (format "--}-<<%s>>\n" stype))
2540 (narrow-to-region bb eb)
2541 (goto-char (point-min))
2542 (while (re-search-forward boundary-pat nil t)
2543 (let ((beg (match-beginning 0))
2545 (delete-region beg (match-end 0))
2547 (if (re-search-forward boundary-pat nil t)
2548 (setq end (match-beginning 0))
2549 (setq end (point-max))
2552 (narrow-to-region beg end)
2553 (mime-edit-decode-buffer not-decode-text)
2554 (goto-char (point-max))
2557 (goto-char (point-min))
2558 (or (= (point-min) 1)
2559 (delete-region (point-min)
2560 (if (search-forward "\n\n" nil t)
2566 (let* ((ctype (format "%s/%s" type stype))
2569 (let ((bytes (+ 14 (length ctype))))
2570 (mapconcat (function
2572 (if (string= (car attr) "charset")
2574 (setq charset (cdr attr))
2582 (setq bytes (+ bytes bs 2))
2585 (setq bytes (+ bs 1))
2593 (if (re-search-forward
2594 "Content-Transfer-Encoding:" nil t)
2595 (let ((beg (match-beginning 0))
2596 (hbeg (match-end 0))
2597 (end (std11-field-end)))
2599 (eliminate-top-spaces
2600 (std11-unfold-string
2601 (buffer-substring hbeg end))))
2602 (if (or charset (eq type 'text))
2604 (delete-region beg (1+ end))
2605 (goto-char (point-min))
2606 (if (search-forward "\n\n" nil t)
2609 (match-end 0)(point-max) encoding)
2613 (if (or encoded (not not-decode-text))
2614 (decode-mime-charset-region
2615 (point-min)(point-max)
2616 (or charset default-mime-charset))
2619 (if (re-search-forward "^$" nil t)
2623 (if (= (point-min) 1)
2629 (format "%s/%s%s" type stype pstr)
2632 (delete-region (point-min) he)
2635 (format "%s/%s%s" type stype pstr)
2640 (decode-mime-charset-region (point-min) (point-max)
2641 default-mime-charset)
2645 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2646 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2647 Content-Type and Content-Transfer-Encoding header fields will be
2648 converted to MIME-Edit tags."
2650 (goto-char (point-min))
2652 (concat "\n" (regexp-quote mail-header-separator) "\n")
2654 (replace-match "\n\n")
2656 (mime-edit-decode-buffer not-decode-text)
2657 (goto-char (point-min))
2659 (std11-narrow-to-header)
2660 (goto-char (point-min))
2661 (while (re-search-forward mime-edit-again-ignored-field-regexp nil t)
2662 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2665 (and (re-search-forward "^$")
2666 (replace-match mail-header-separator)
2676 (provide 'mime-edit)
2678 (run-hooks 'mime-edit-load-hook)
2680 ;;; mime-edit.el ends here