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.94 $
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.94 1997-11-04 14:24:59 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 (defvar mime-edit-split-message t
462 "*Split large message if it is non-nil.")
464 (defvar mime-edit-message-default-max-lines 1000
465 "*Default maximum lines of a message.")
467 (defvar mime-edit-message-max-lines-alist
468 '((news-reply-mode . 500))
469 "Alist of major-mode vs maximum lines of a message.
470 If it is not specified for a major-mode,
471 `mime-edit-message-default-max-lines' is used.")
473 (defconst mime-edit-split-ignored-field-regexp
474 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\)")
476 (defvar mime-edit-split-blind-field-regexp
477 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)")
479 (defvar mime-edit-split-message-sender-alist nil)
481 (defvar mime-edit-news-reply-mode-server-running nil)
487 (defvar mime-edit-signing-type 'pgp-elkins
488 "*PGP signing type (pgp-elkins, pgp-kazu or nil).")
490 (defvar mime-edit-encrypting-type 'pgp-elkins
491 "*PGP encrypting type (pgp-elkins, pgp-kazu or nil).")
497 (defconst mime-edit-single-part-tag-regexp
498 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
499 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
501 (defconst mime-edit-quoted-single-part-tag-regexp
502 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
504 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
506 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
508 (defconst mime-edit-beginning-tag-regexp
509 (regexp-or mime-edit-single-part-tag-regexp
510 mime-edit-multipart-beginning-regexp))
512 (defconst mime-edit-end-tag-regexp
513 (regexp-or mime-edit-single-part-tag-regexp
514 mime-edit-multipart-end-regexp))
516 (defconst mime-edit-tag-regexp
517 (regexp-or mime-edit-single-part-tag-regexp
518 mime-edit-multipart-beginning-regexp
519 mime-edit-multipart-end-regexp))
521 (defvar mime-tag-format "--[[%s]]"
522 "*Control-string making a MIME tag.")
524 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
525 "*Control-string making a MIME tag with encoding.")
528 ;;; @@ multipart boundary
531 (defvar mime-multipart-boundary "Multipart"
532 "*Boundary of a multipart message.")
535 ;;; @@ optional header fields
538 (defvar mime-edit-insert-x-emacs-field t
539 "*If non-nil, insert X-Emacs header field.")
541 (defvar mime-edit-x-emacs-value
542 (if (featurep 'xemacs)
543 (concat emacs-version (if (featurep 'mule)
546 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
547 (substring emacs-version 0 (match-beginning 0))
551 (if enable-multibyte-characters
552 (concat ", MULE " mule-version)
553 " (with raw setting)")
554 (if (featurep 'meadow)
555 (concat ", " (Meadow-version))
558 "Body of X-Emacs field.
559 If variable `mime-edit-insert-x-emacs-field' is not nil, it is
560 inserted into message header.")
566 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
567 "*Specify MIME tspecials.
568 Tspecials means any character that matches with it in header must be quoted.")
570 (defconst mime-edit-mime-version-value
571 (concat "1.0 (generated by " mime-edit-version-name ")")
572 "MIME version number.")
574 (defconst mime-edit-mime-version-field-for-message/partial
575 (concat "MIME-Version: 1.0 (split by " mime-edit-version-name ")\n")
576 "MIME version field for message/partial.")
579 ;;; @ keymap and menu
582 (defvar mime-edit-mode-flag nil)
583 (make-variable-buffer-local 'mime-edit-mode-flag)
585 (defvar mime-edit-mode-map (make-sparse-keymap)
586 "Keymap for MIME-Edit mode commands.")
588 (define-key mime-edit-mode-map
589 "\C-c\C-x\C-t" 'mime-edit-insert-text)
590 (define-key mime-edit-mode-map
591 "\C-c\C-x\C-i" 'mime-edit-insert-file)
592 (define-key mime-edit-mode-map
593 "\C-c\C-x\C-e" 'mime-edit-insert-external)
594 (define-key mime-edit-mode-map
595 "\C-c\C-x\C-v" 'mime-edit-insert-voice)
596 (define-key mime-edit-mode-map
597 "\C-c\C-x\C-y" 'mime-edit-insert-message)
598 (define-key mime-edit-mode-map
599 "\C-c\C-x\C-m" 'mime-edit-insert-mail)
600 (define-key mime-edit-mode-map
601 "\C-c\C-x\C-w" 'mime-edit-insert-signature)
602 (define-key mime-edit-mode-map
603 "\C-c\C-x\C-s" 'mime-edit-insert-signature)
604 (define-key mime-edit-mode-map
605 "\C-c\C-x\C-k" 'mime-edit-insert-key)
606 (define-key mime-edit-mode-map
607 "\C-c\C-xt" 'mime-edit-insert-tag)
609 (define-key mime-edit-mode-map
610 "\C-c\C-m\C-a" 'mime-edit-enclose-alternative-region)
611 (define-key mime-edit-mode-map
612 "\C-c\C-m\C-p" 'mime-edit-enclose-parallel-region)
613 (define-key mime-edit-mode-map
614 "\C-c\C-m\C-m" 'mime-edit-enclose-mixed-region)
615 (define-key mime-edit-mode-map
616 "\C-c\C-m\C-d" 'mime-edit-enclose-digest-region)
617 (define-key mime-edit-mode-map
618 "\C-c\C-m\C-s" 'mime-edit-enclose-signed-region)
619 (define-key mime-edit-mode-map
620 "\C-c\C-m\C-e" 'mime-edit-enclose-encrypted-region)
621 (define-key mime-edit-mode-map
622 "\C-c\C-m\C-q" 'mime-edit-enclose-quote-region)
624 (define-key mime-edit-mode-map
625 "\C-c\C-x7" 'mime-edit-set-transfer-level-7bit)
626 (define-key mime-edit-mode-map
627 "\C-c\C-x8" 'mime-edit-set-transfer-level-8bit)
628 (define-key mime-edit-mode-map
629 "\C-c\C-x/" 'mime-edit-set-split)
630 (define-key mime-edit-mode-map
631 "\C-c\C-xs" 'mime-edit-set-sign)
632 (define-key mime-edit-mode-map
633 "\C-c\C-xv" 'mime-edit-set-sign)
634 (define-key mime-edit-mode-map
635 "\C-c\C-xe" 'mime-edit-set-encrypt)
636 (define-key mime-edit-mode-map
637 "\C-c\C-xh" 'mime-edit-set-encrypt)
638 (define-key mime-edit-mode-map
639 "\C-c\C-x\C-p" 'mime-edit-preview-message)
640 (define-key mime-edit-mode-map
641 "\C-c\C-x\C-z" 'mime-edit-exit)
642 (define-key mime-edit-mode-map
643 "\C-c\C-x?" 'mime-edit-help)
645 (defconst mime-edit-menu-title "MIME-Edit")
647 (defconst mime-edit-menu-list
648 '((mime-help "Describe MIME editor mode" mime-edit-help)
649 (file "Insert File" mime-edit-insert-file)
650 (external "Insert External" mime-edit-insert-external)
651 (voice "Insert Voice" mime-edit-insert-voice)
652 (message "Insert Message" mime-edit-insert-message)
653 (mail "Insert Mail" mime-edit-insert-mail)
654 (signature "Insert Signature" mime-edit-insert-signature)
655 (text "Insert Text" mime-edit-insert-text)
656 (tag "Insert Tag" mime-edit-insert-tag)
657 (alternative "Enclose as alternative"
658 mime-edit-enclose-alternative-region)
659 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
660 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
661 (digest "Enclose as digest" mime-edit-enclose-digest-region)
662 (signed "Enclose as signed" mime-edit-enclose-signed-region)
663 (encrypted "Enclose as encrypted" mime-edit-enclose-encrypted-region)
664 (quote "Verbatim region" mime-edit-enclose-quote-region)
665 (key "Insert Public Key" mime-edit-insert-key)
666 (split "About split" mime-edit-set-split)
667 (sign "About sign" mime-edit-set-sign)
668 (encrypt "About encryption" mime-edit-set-encrypt)
669 (preview "Preview Message" mime-edit-preview-message)
670 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
672 "MIME-edit menubar entry.")
674 (cond (running-xemacs
675 ;; modified by Pekka Marjola <pema@iki.fi>
676 ;; 1995/9/5 (c.f. [tm-en:69])
677 (defun mime-edit-define-menu-for-xemacs ()
678 "Define menu for Emacs 19."
679 (cond ((featurep 'menubar)
680 (make-local-variable 'current-menubar)
681 (set-buffer-menubar current-menubar)
684 (cons mime-edit-menu-title
687 (vector (nth 1 item)(nth 2 item)
690 mime-edit-menu-list)))
693 ;; modified by Steven L. Baur <steve@miranova.com>
694 ;; 1995/12/6 (c.f. [tm-en:209])
695 (or (boundp 'mime-edit-popup-menu-for-xemacs)
696 (setq mime-edit-popup-menu-for-xemacs
697 (append '("MIME Commands" "---")
698 (mapcar (function (lambda (item)
702 mime-edit-menu-list)))
705 ((>= emacs-major-version 19)
706 (define-key mime-edit-mode-map [menu-bar mime-edit]
707 (cons mime-edit-menu-title
708 (make-sparse-keymap mime-edit-menu-title)))
711 (define-key mime-edit-mode-map
712 (vector 'menu-bar 'mime-edit (car item))
713 (cons (nth 1 item)(nth 2 item))
716 (reverse mime-edit-menu-list)
725 (defun mime-edit-mode ()
726 "MIME minor mode for editing the tagged MIME message.
728 In this mode, basically, the message is composed in the tagged MIME
729 format. The message tag looks like:
731 --[[text/plain; charset=ISO-2022-JP][7bit]]
733 The tag specifies the MIME content type, subtype, optional parameters
734 and transfer encoding of the message following the tag. Messages
735 without any tag are treated as `text/plain' by default. Charset and
736 transfer encoding are automatically defined unless explicitly
737 specified. Binary messages such as audio and image are usually
738 hidden. The messages in the tagged MIME format are automatically
739 translated into a MIME compliant message when exiting this mode.
741 Available charsets depend on Emacs version being used. The following
742 lists the available charsets of each emacs.
744 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
745 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
746 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
747 CN-BIG5 and ISO-2022-INT-1 are available.
749 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
750 be used to represent multilingual text in intermixed manner. Any
751 languages that has no registered charset are represented as either
752 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
754 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
755 without mule, please set variable `default-mime-charset'. This
756 variable must be symbol of which name is a MIME charset.
758 If you want to add more charsets in mule, please set variable
759 `charsets-mime-charset-alist'. This variable must be alist of which
760 key is list of charset and value is symbol of MIME charset. If name
761 of coding-system is different as MIME charset, please set variable
762 `mime-charset-coding-system-alist'. This variable must be alist of
763 which key is MIME charset and value is coding-system.
765 Following commands are available in addition to major mode commands:
768 \\[mime-edit-insert-text] insert a text message.
769 \\[mime-edit-insert-file] insert a (binary) file.
770 \\[mime-edit-insert-external] insert a reference to external body.
771 \\[mime-edit-insert-voice] insert a voice message.
772 \\[mime-edit-insert-message] insert a mail or news message.
773 \\[mime-edit-insert-mail] insert a mail message.
774 \\[mime-edit-insert-signature] insert a signature file at end.
775 \\[mime-edit-insert-key] insert PGP public key.
776 \\[mime-edit-insert-tag] insert a new MIME tag.
778 \[make enclosure (maybe multipart)\]
779 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
780 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
781 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
782 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
783 \\[mime-edit-enclose-signed-region] enclose as PGP signed.
784 \\[mime-edit-enclose-encrypted-region] enclose as PGP encrypted.
785 \\[mime-edit-enclose-quote-region] enclose as verbose mode (to avoid to expand tags)
788 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
789 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
790 \\[mime-edit-set-split] set message splitting mode.
791 \\[mime-edit-set-sign] set PGP-sign mode.
792 \\[mime-edit-set-encrypt] set PGP-encryption mode.
793 \\[mime-edit-preview-message] preview editing MIME message.
794 \\[mime-edit-exit] exit and translate into a MIME compliant message.
795 \\[mime-edit-help] show this help.
796 \\[mime-edit-maybe-translate] exit and translate if in MIME mode, then split.
798 Additional commands are available in some major modes:
799 C-c C-c exit, translate and run the original command.
800 C-c C-s exit, translate and run the original command.
802 The following is a message example written in the tagged MIME format.
803 TABs at the beginning of the line are not a part of the message:
805 This is a conventional plain text. It should be translated
808 This is also a plain text. But, it is explicitly specified as
810 --[[text/plain; charset=ISO-8859-1]]
811 This is also a plain text. But charset is specified as
814 ¡Hola! Buenos días. ¿Cómo está usted?
816 This is a <bold>enriched text</bold>.
817 --[[image/gif][base64]]...image encoded in base64 here...
818 --[[audio/basic][base64]]...audio encoded in base64 here...
820 User customizable variables (not documented all of them):
822 Specifies a key prefix for MIME minor mode commands.
824 mime-ignore-preceding-spaces
825 Preceding white spaces in a message body are ignored if non-nil.
827 mime-ignore-trailing-spaces
828 Trailing white spaces in a message body are ignored if non-nil.
831 Hide a non-textual body message encoded in base64 after insertion
835 A number of network transfer level. It should be bigger than 7.
836 If you are in 8bit-through environment, please set 8.
838 mime-edit-voice-recorder
839 Specifies a function to record a voice message and encode it.
840 The function `mime-edit-voice-recorder-for-sun' is for Sun
844 Turning on MIME mode calls the value of mime-edit-mode-hook, if
847 mime-edit-translate-hook
848 The value of mime-edit-translate-hook is called just before translating
849 the tagged MIME format into a MIME compliant message if it is
850 non-nil. If the hook call the function mime-edit-insert-signature,
851 the signature file will be inserted automatically.
854 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
857 (if mime-edit-mode-flag
859 (if (and (boundp 'mime-edit-touched-flag)
860 mime-edit-touched-flag)
862 (make-local-variable 'mime-edit-touched-flag)
863 (setq mime-edit-touched-flag t)
868 (cond (running-xemacs
869 (add-minor-mode 'mime-edit-mode-flag
870 '((" MIME-Edit " mime-transfer-level-string))
876 (set-alist 'minor-mode-alist
878 '((" MIME-Edit " mime-transfer-level-string)))
879 (set-alist 'minor-mode-map-alist
886 (defun turn-on-mime-edit ()
887 "Unconditionally turn on MIME-Edit mode."
889 (if mime-edit-mode-flag
890 (error "You are already editing a MIME message.")
891 (setq mime-edit-mode-flag t)
893 ;; Set transfer level into mode line
895 (setq mime-transfer-level-string
896 (mime-encoding-name mime-transfer-level 'not-omit))
897 (force-mode-line-update)
899 ;; Define menu for XEmacs.
901 (mime-edit-define-menu-for-xemacs)
906 ;; I don't care about saving these.
907 (setq paragraph-start
908 (regexp-or mime-edit-single-part-tag-regexp
910 (setq paragraph-separate
911 (regexp-or mime-edit-single-part-tag-regexp
913 (run-hooks 'mime-edit-mode-hook)
915 (substitute-command-keys
916 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
920 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
923 (defun mime-edit-exit (&optional nomime no-error)
924 "Translate the tagged MIME message into a MIME compliant message.
925 With no argument encode a message in the buffer into MIME, otherwise
926 just return to previous mode."
928 (if (not mime-edit-mode-flag)
930 (error "You aren't editing a MIME message.")
934 (run-hooks 'mime-edit-translate-hook)
935 (mime-edit-translate-buffer)))
936 ;; Restore previous state.
937 (setq mime-edit-mode-flag nil)
938 (if (and running-xemacs
940 (delete-menu-item (list mime-edit-menu-title))
943 (set-buffer-modified-p (buffer-modified-p))
944 (run-hooks 'mime-edit-exit-hook)
945 (message "Exit MIME editor mode.")
948 (defun mime-edit-maybe-translate ()
950 (mime-edit-exit nil t)
951 (call-interactively 'mime-edit-maybe-split-and-send)
954 (defun mime-edit-help ()
955 "Show help message about MIME mode."
957 (with-output-to-temp-buffer "*Help*"
958 (princ "MIME editor mode:\n")
959 (princ (documentation 'mime-edit-mode))
960 (print-help-return-message)))
962 (defun mime-edit-insert-text ()
963 "Insert a text message.
964 Charset is automatically obtained from the `charsets-mime-charset-alist'."
966 (let ((ret (mime-edit-insert-tag "text" nil nil)))
969 (if (looking-at mime-edit-single-part-tag-regexp)
971 ;; Make a space between the following message.
975 (if (and (member (cadr ret) '("enriched" "richtext"))
976 (fboundp 'enriched-mode)
979 (if (boundp 'enriched-mode)
983 (defun mime-edit-insert-file (file &optional verbose)
984 "Insert a message from a file."
985 (interactive "fInsert file as MIME message: \nP")
986 (let* ((guess (mime-find-file-type file))
988 (subtype (nth 1 guess))
989 (parameters (nth 2 guess))
990 (encoding (nth 3 guess))
991 (disposition-type (nth 4 guess))
992 (disposition-params (nth 5 guess))
995 (setq type (mime-prompt-for-type type)
996 subtype (mime-prompt-for-subtype type subtype)
998 (if (or (interactive-p) verbose)
999 (setq encoding (mime-prompt-for-encoding encoding))
1001 (if (or (consp parameters) (stringp disposition-type))
1002 (let ((rest parameters) cell attribute value)
1003 (setq parameters "")
1005 (setq cell (car rest))
1006 (setq attribute (car cell))
1007 (setq value (cdr cell))
1008 (if (eq value 'file)
1009 (setq value (std11-wrap-as-quoted-string
1010 (file-name-nondirectory file)))
1012 (setq parameters (concat parameters "; " attribute "=" value))
1013 (setq rest (cdr rest))
1015 (if disposition-type
1018 (concat parameters "\n"
1019 "Content-Disposition: " disposition-type))
1020 (setq rest disposition-params)
1022 (setq cell (car rest))
1023 (setq attribute (car cell))
1024 (setq value (cdr cell))
1025 (if (eq value 'file)
1026 (setq value (std11-wrap-as-quoted-string
1027 (file-name-nondirectory file)))
1030 (concat parameters "; " attribute "=" value))
1031 (setq rest (cdr rest))
1035 (mime-edit-insert-tag type subtype parameters)
1036 (mime-edit-insert-binary-file file encoding)
1039 (defun mime-edit-insert-external ()
1040 "Insert a reference to external body."
1042 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1044 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1046 (let* ((pritype (mime-prompt-for-type))
1047 (subtype (mime-prompt-for-subtype pritype))
1048 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1051 (insert "Content-Type: "
1052 pritype "/" subtype (or parameters "") "\n")))
1053 (if (and (not (eobp))
1054 (not (looking-at mime-edit-single-part-tag-regexp)))
1055 (insert (mime-make-text-tag) "\n")))
1057 (defun mime-edit-insert-voice ()
1058 "Insert a voice message."
1062 "What transfer encoding: "
1063 mime-file-encoding-method-alist nil t nil)))
1064 (mime-edit-insert-tag "audio" "basic" nil)
1065 (mime-edit-define-encoding encoding)
1067 (narrow-to-region (1- (point))(point))
1069 (funcall mime-edit-voice-recorder encoding)
1072 (invisible-region (point-min)(point-max))
1073 (goto-char (point-max))
1076 (defun mime-edit-insert-signature (&optional arg)
1077 "Insert a signature file."
1079 (let ((signature-insert-hook
1082 (apply (function mime-edit-insert-tag)
1083 (mime-find-file-type signature-file-name))
1086 (insert-signature arg)
1090 ;; Insert a new tag around a point.
1092 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1093 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1094 If nothing is inserted, return nil."
1097 (mime-edit-goto-tag)
1098 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1099 (< (match-beginning 0) p)
1102 (goto-char (match-beginning 0))
1111 (mime-prompt-for-type)))
1114 (mime-prompt-for-subtype pritype)))
1117 (mime-prompt-for-parameters pritype subtype delimiter)))
1118 ;; Make a new MIME tag.
1119 (setq newtag (mime-make-tag pritype subtype parameters))
1120 ;; Find an current MIME tag.
1123 (if (mime-edit-goto-tag)
1124 (buffer-substring (match-beginning 0) (match-end 0))
1125 ;; Assume content type is 'text/plan'.
1126 (mime-make-tag "text" "plain")
1128 ;; We are only interested in TEXT.
1130 (not (mime-test-content-type
1131 (mime-edit-get-contype oldtag) "text")))
1134 (if (or (not oldtag) ;Not text
1135 (or mime-ignore-same-text-tag
1136 (not (string-equal oldtag newtag))))
1138 ;; Mark the beginning of the tag for convenience.
1139 (push-mark (point) 'nomsg)
1140 (insert newtag "\n")
1141 (list pritype subtype parameters) ;New tag is created.
1143 ;; Restore previous point.
1145 nil ;Nothing is created.
1149 (defun mime-edit-insert-binary-file (file &optional encoding)
1150 "Insert binary FILE at point.
1151 Optional argument ENCODING specifies an encoding method such as base64."
1152 (let* ((tagend (1- (point))) ;End of the tag
1153 (hide-p (and mime-auto-hide-body
1156 (let ((en (downcase encoding)))
1157 (or (string-equal en "7bit")
1158 (string-equal en "8bit")
1159 (string-equal en "binary")
1163 (narrow-to-region tagend (point))
1164 (mime-insert-encoded-file file encoding)
1167 (invisible-region (point-min) (point-max))
1168 (goto-char (point-max))
1170 (goto-char (point-max))
1173 (looking-at mime-edit-tag-regexp)
1174 (= (point)(point-max))
1175 (mime-edit-insert-tag "text" "plain")
1177 ;; Define encoding even if it is 7bit.
1178 (if (stringp encoding)
1180 (goto-char tagend) ; Make sure which line the tag is on.
1181 (mime-edit-define-encoding encoding)
1186 ;; Commands work on a current message flagment.
1188 (defun mime-edit-goto-tag ()
1189 "Search for the beginning of the tagged MIME message."
1190 (let ((current (point)))
1191 (if (looking-at mime-edit-tag-regexp)
1193 ;; At first, go to the end.
1194 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1195 (goto-char (1- (match-beginning 0))) ;For multiline tag
1198 (goto-char (point-max))
1200 ;; Then search for the beginning.
1201 (re-search-backward mime-edit-end-tag-regexp nil t)
1202 (or (looking-at mime-edit-beginning-tag-regexp)
1203 ;; Restore previous point.
1210 (defun mime-edit-content-beginning ()
1211 "Return the point of the beginning of content."
1213 (let ((beg (save-excursion
1214 (beginning-of-line) (point))))
1215 (if (mime-edit-goto-tag)
1216 (let ((top (point)))
1217 (goto-char (match-end 0))
1218 (if (and (= beg top)
1219 (= (following-char) ?\^M))
1223 ;; Default text/plain tag.
1224 (goto-char (point-min))
1226 (concat "\n" (regexp-quote mail-header-separator)
1227 (if mime-ignore-preceding-spaces
1228 "[ \t\n]*\n" "\n")) nil 'move)
1232 (defun mime-edit-content-end ()
1233 "Return the point of the end of content."
1235 (if (mime-edit-goto-tag)
1237 (goto-char (match-end 0))
1238 (if (invisible-p (point))
1239 (next-visible-point (point))
1240 ;; Move to the end of this text.
1241 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1242 ;; Don't forget a multiline tag.
1243 (goto-char (match-beginning 0))
1247 ;; Assume the message begins with text/plain.
1248 (goto-char (mime-edit-content-beginning))
1249 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1250 ;; Don't forget a multiline tag.
1251 (goto-char (match-beginning 0)))
1255 (defun mime-edit-define-charset (charset)
1256 "Set charset of current tag to CHARSET."
1258 (if (mime-edit-goto-tag)
1259 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1260 (delete-region (match-beginning 0) (match-end 0))
1263 (mime-edit-set-parameter
1264 (mime-edit-get-contype tag)
1265 "charset" (upcase (symbol-name charset)))
1266 (mime-edit-get-encoding tag)))
1269 (defun mime-edit-define-encoding (encoding)
1270 "Set encoding of current tag to ENCODING."
1272 (if (mime-edit-goto-tag)
1273 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1274 (delete-region (match-beginning 0) (match-end 0))
1275 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1278 (defun mime-edit-choose-charset ()
1279 "Choose charset of a text following current point."
1280 (detect-mime-charset-region (point) (mime-edit-content-end))
1283 (defun mime-make-text-tag (&optional subtype)
1284 "Make a tag for a text after current point.
1285 Subtype of text type can be specified by an optional argument SUBTYPE.
1286 Otherwise, it is obtained from mime-content-types."
1287 (let* ((pritype "text")
1288 (subtype (or subtype
1289 (car (car (cdr (assoc pritype mime-content-types)))))))
1290 ;; Charset should be defined later.
1291 (mime-make-tag pritype subtype)))
1294 ;; Tag handling functions
1296 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1297 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1298 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1302 (defun mime-create-tag (contype &optional encoding)
1303 "Make a tag with CONTENT-TYPE and optional ENCODING."
1304 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1307 (defun mime-edit-get-contype (tag)
1308 "Return Content-Type (including parameters) of TAG."
1310 (or (string-match mime-edit-single-part-tag-regexp tag)
1311 (string-match mime-edit-multipart-beginning-regexp tag)
1312 (string-match mime-edit-multipart-end-regexp tag)
1314 (substring tag (match-beginning 1) (match-end 1))
1317 (defun mime-edit-get-encoding (tag)
1318 "Return encoding of TAG."
1320 (string-match mime-edit-single-part-tag-regexp tag)
1322 (not (= (match-beginning 3) (match-end 3)))
1323 (substring tag (match-beginning 3) (match-end 3))))
1325 (defun mime-get-parameter (contype parameter)
1326 "For given CONTYPE return value for PARAMETER.
1327 Nil if no such parameter."
1331 (regexp-quote parameter)
1332 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1334 (substring contype (match-beginning 1) (match-end 1))
1335 nil ;No such parameter
1338 (defun mime-edit-set-parameter (contype parameter value)
1339 "For given CONTYPE set PARAMETER to VALUE."
1340 (let (ctype opt-fields)
1341 (if (string-match "\n[^ \t\n\r]+:" contype)
1342 (setq ctype (substring contype 0 (match-beginning 0))
1343 opt-fields (substring contype (match-beginning 0)))
1344 (setq ctype contype)
1349 (regexp-quote parameter)
1350 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1353 (concat (substring ctype 0 (match-beginning 1))
1355 (substring contype (match-end 1))
1357 (concat ctype "; " parameter "=" value opt-fields)
1360 (defun mime-strip-parameters (contype)
1361 "Return primary content-type and subtype without parameters for CONTYPE."
1362 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1363 (substring contype (match-beginning 1) (match-end 1)) nil))
1365 (defun mime-test-content-type (contype type &optional subtype)
1366 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1367 (and (stringp contype)
1370 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1371 (downcase contype))))
1376 (defun mime-find-file-type (file)
1377 "Guess Content-Type, subtype, and parameters from FILE."
1379 (guesses mime-file-types))
1380 (while (and (not guess) guesses)
1381 (if (string-match (car (car guesses)) file)
1382 (setq guess (cdr (car guesses))))
1383 (setq guesses (cdr guesses)))
1387 (defun mime-prompt-for-type (&optional default)
1388 "Ask for Content-type."
1390 ;; Repeat until primary content type is specified.
1391 (while (string-equal type "")
1393 (completing-read "What content type: "
1396 'require-match ;Type must be specified.
1399 (if (string-equal type "")
1401 (message "Content type is required.")
1408 (defun mime-prompt-for-subtype (type &optional default)
1409 "Ask for subtype of media-type TYPE."
1410 (let ((subtypes (cdr (assoc type mime-content-types))))
1412 (assoc default subtypes))
1413 (setq default (car (car subtypes)))
1419 "What content subtype: (default " default ") ")
1420 "What content subtype: ")
1421 (cdr (assoc type mime-content-types))
1423 'require-match ;Subtype must be specified.
1426 (if (string-equal answer "") default answer)))
1428 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1429 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1430 Optional DELIMITER specifies parameter delimiter (';' by default)."
1431 (let* ((delimiter (or delimiter "; "))
1436 (mime-prompt-for-parameters-1
1438 (cdr (assoc pritype mime-content-types))))))
1441 (if (and (stringp parameters)
1442 (not (string-equal parameters "")))
1443 (concat delimiter parameters)
1444 "" ;"" if no parameters
1447 (defun mime-prompt-for-parameters-1 (optlist)
1448 (apply (function append)
1449 (mapcar (function mime-prompt-for-parameter) optlist)))
1451 (defun mime-prompt-for-parameter (parameter)
1453 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1454 (let* ((prompt (car parameter))
1455 (choices (mapcar (function
1457 (if (consp e) e (list e))))
1459 (default (car (car choices)))
1465 (concat "What " prompt
1467 (if (string-equal default "") "\"\"" default)
1469 choices nil nil ""))
1470 ;; If nothing is selected, use default.
1471 (if (string-equal answer "")
1472 (setq answer default)))
1474 (read-string (concat "What " prompt ": "))))
1475 (cons (if (and answer
1476 (not (string-equal answer "")))
1478 ;; Note: control characters ignored!
1479 (if (string-match mime-tspecials-regexp answer)
1480 (concat "\"" answer "\"") answer)))
1481 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1484 (defun mime-prompt-for-encoding (default)
1485 "Ask for Content-Transfer-Encoding."
1490 "What transfer encoding: "
1491 mime-file-encoding-method-alist nil t default)
1497 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1500 (defvar mime-edit-translate-buffer-hook
1501 '(mime-edit-pgp-enclose-buffer
1502 mime-edit-translate-body
1503 mime-edit-translate-header))
1505 (defun mime-edit-translate-header ()
1506 "Encode the message header into network representation."
1507 (eword-encode-header 'code-conversion)
1508 (run-hooks 'mime-edit-translate-header-hook)
1511 (defun mime-edit-translate-buffer ()
1512 "Encode the tagged MIME message in current buffer in MIME compliant message."
1514 (if (catch 'mime-edit-error
1516 (run-hooks 'mime-edit-translate-buffer-hook)
1520 (error "Translation error!")
1523 (defun mime-edit-find-inmost ()
1524 (goto-char (point-min))
1525 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1526 (let ((bb (match-beginning 0))
1528 (type (buffer-substring (match-beginning 1)(match-end 1)))
1530 (setq end-exp (format "--}-<<%s>>\n" type))
1532 (if (re-search-forward end-exp nil t)
1534 (setq eb (match-beginning 0))
1535 (setq ee (match-end 0))
1537 (setq eb (point-max))
1538 (setq ee (point-max))
1540 (narrow-to-region be eb)
1542 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1544 (narrow-to-region (match-beginning 0)(point-max))
1545 (mime-edit-find-inmost)
1548 (list type bb be eb)
1551 (defun mime-edit-process-multipart-1 (boundary)
1552 (let ((ret (mime-edit-find-inmost)))
1554 (let ((type (car ret))
1555 (bb (nth 1 ret))(be (nth 2 ret))
1558 (narrow-to-region bb eb)
1559 (delete-region bb be)
1560 (setq bb (point-min))
1561 (setq eb (point-max))
1564 (if (looking-at mime-edit-multipart-end-regexp)
1565 (let ((beg (match-beginning 0))
1568 (delete-region beg end)
1569 (or (looking-at mime-edit-beginning-tag-regexp)
1571 (insert (concat (mime-make-text-tag) "\n"))
1573 (cond ((string-equal type "quote")
1574 (mime-edit-enquote-region bb eb)
1576 ((string-equal type "signed")
1577 (cond ((eq mime-edit-signing-type 'pgp-elkins)
1578 (mime-edit-sign-pgp-elkins bb eb boundary)
1580 ((eq mime-edit-signing-type 'pgp-kazu)
1581 (mime-edit-sign-pgp-kazu bb eb boundary)
1584 ((string-equal type "encrypted")
1585 (cond ((eq mime-edit-encrypting-type 'pgp-elkins)
1586 (mime-edit-encrypt-pgp-elkins bb eb boundary)
1588 ((eq mime-edit-encrypting-type 'pgp-kazu)
1589 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1593 (nth 2 (mime-edit-translate-region bb eb
1597 (format "--[[multipart/%s;
1598 boundary=\"%s\"][7bit]]\n"
1603 (defun mime-edit-enquote-region (beg end)
1606 (narrow-to-region beg end)
1608 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1609 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1610 (replace-match (concat "- " (substring tag 1)))
1613 (defun mime-edit-dequote-region (beg end)
1616 (narrow-to-region beg end)
1618 (while (re-search-forward
1619 mime-edit-quoted-single-part-tag-regexp nil t)
1620 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1621 (replace-match (concat "-" (substring tag 2)))
1624 (defun mime-edit-sign-pgp-elkins (beg end boundary)
1627 (narrow-to-region beg end)
1629 (mime-edit-translate-region beg end boundary))
1631 (encoding (nth 1 ret))
1633 (pgp-boundary (concat "pgp-sign-" boundary))
1636 (insert (format "Content-Type: %s\n" ctype))
1638 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1641 (or (as-binary-process
1642 (funcall (pgp-function 'mime-sign)
1643 (point-min)(point-max) nil nil pgp-boundary))
1644 (throw 'mime-edit-error 'pgp-error)
1648 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1650 (defun mime-edit-make-encrypt-recipient-header ()
1651 (let* ((names mime-edit-encrypt-recipient-fields-list)
1653 (std11-field-bodies (cons "From" names)
1654 nil mail-header-separator))
1657 (setq values (cdr values))))
1658 (header (and (stringp from)
1659 (if (string-equal from "")
1661 (format "From: %s\n" from)
1664 (while (and names values)
1665 (let ((name (car names))
1666 (value (car values))
1668 (and (stringp value)
1669 (or (string-equal value "")
1671 (setq header (concat header name ": " value "\n")
1672 recipients (if recipients
1673 (concat recipients " ," value)
1676 (setq names (cdr names)
1677 values (cdr values))
1679 (vector from recipients header)
1682 (defun mime-edit-encrypt-pgp-elkins (beg end boundary)
1685 (let (from recipients header)
1686 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1687 (setq from (aref ret 0)
1688 recipients (aref ret 1)
1689 header (aref ret 2))
1691 (narrow-to-region beg end)
1693 (mime-edit-translate-region beg end boundary))
1695 (encoding (nth 1 ret))
1697 (pgp-boundary (concat "pgp-" boundary))
1701 (insert (format "Content-Type: %s\n" ctype))
1703 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1706 (or (funcall (pgp-function 'encrypt)
1707 recipients (point-min) (point-max) from)
1708 (throw 'mime-edit-error 'pgp-error)
1711 (insert (format "--[[multipart/encrypted;
1713 protocol=\"application/pgp-encrypted\"][7bit]]
1715 Content-Type: application/pgp-encrypted
1718 Content-Type: application/octet-stream
1719 Content-Transfer-Encoding: 7bit
1721 " pgp-boundary pgp-boundary pgp-boundary))
1722 (goto-char (point-max))
1723 (insert (format "\n--%s--\n" pgp-boundary))
1726 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1729 (narrow-to-region beg end)
1731 (mime-edit-translate-region beg end boundary))
1733 (encoding (nth 1 ret))
1737 (insert (format "Content-Type: %s\n" ctype))
1739 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1742 (or (as-binary-process
1743 (funcall (pgp-function 'traditional-sign)
1745 (throw 'mime-edit-error 'pgp-error)
1749 "--[[application/pgp; format=mime][7bit]]\n")
1753 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1755 (let (from recipients header)
1756 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1757 (setq from (aref ret 0)
1758 recipients (aref ret 1)
1759 header (aref ret 2))
1762 (narrow-to-region beg end)
1764 (mime-edit-translate-region beg end boundary))
1766 (encoding (nth 1 ret))
1771 (insert (format "Content-Type: %s\n" ctype))
1773 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1776 (or (as-binary-process
1777 (funcall (pgp-function 'encrypt)
1778 recipients beg (point-max) nil 'maybe)
1780 (throw 'mime-edit-error 'pgp-error)
1784 "--[[application/pgp; format=mime][7bit]]\n")
1788 (defsubst replace-space-with-underline (str)
1789 (mapconcat (function
1797 (defun mime-edit-make-boundary ()
1798 (concat mime-multipart-boundary "_"
1799 (replace-space-with-underline (current-time-string))
1802 (defun mime-edit-translate-body ()
1803 "Encode the tagged MIME body in current buffer in MIME compliant message."
1806 (let ((boundary (mime-edit-make-boundary))
1809 (while (mime-edit-process-multipart-1
1810 (format "%s-%d" boundary i))
1814 ;; We are interested in message body.
1817 (goto-char (point-min))
1819 (concat "\n" (regexp-quote mail-header-separator)
1820 (if mime-ignore-preceding-spaces
1821 "[ \t\n]*\n" "\n")) nil 'move)
1825 (goto-char (point-max))
1826 (and mime-ignore-trailing-spaces
1827 (re-search-backward "[^ \t\n]\n" beg t)
1830 (setq ret (mime-edit-translate-region
1832 (format "%s-%d" boundary i)))
1834 (mime-edit-dequote-region (point-min)(point-max))
1835 (let ((contype (car ret)) ;Content-Type
1836 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1838 ;; Insert X-Emacs field
1839 (and mime-edit-insert-x-emacs-field
1840 (or (mail-position-on-field "X-Emacs")
1841 (insert mime-edit-x-emacs-value)
1843 ;; Make primary MIME headers.
1844 (or (mail-position-on-field "MIME-Version")
1845 (insert mime-edit-mime-version-value))
1846 ;; Remove old Content-Type and other fields.
1848 (goto-char (point-min))
1849 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1850 (narrow-to-region (point-min) (point))
1851 (goto-char (point-min))
1852 (mime-delete-field "Content-Type")
1853 (mime-delete-field "Content-Transfer-Encoding"))
1854 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1855 (mail-position-on-field "Content-Type")
1859 (mail-position-on-field "Content-Transfer-Encoding")
1863 (defun mime-edit-translate-single-part-tag (&optional prefix)
1864 "Translate single-part-tag to MIME header."
1865 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1866 (let* ((beg (match-beginning 0))
1868 (tag (buffer-substring beg end))
1870 (delete-region beg end)
1871 (let ((contype (mime-edit-get-contype tag))
1872 (encoding (mime-edit-get-encoding tag))
1874 (insert (concat prefix "--" boundary "\n"))
1876 (narrow-to-region (point)(point))
1877 (insert "Content-Type: " contype "\n")
1879 (insert "Content-Transfer-Encoding: " encoding "\n"))
1880 (eword-encode-header)
1884 (defun mime-edit-translate-region (beg end &optional boundary multipart)
1886 (setq boundary (mime-edit-make-boundary))
1890 (narrow-to-region beg end)
1891 (let ((tag nil) ;MIME tag
1892 (contype nil) ;Content-Type
1893 (encoding nil) ;Content-Transfer-Encoding
1894 (nparts 0)) ;Number of body parts
1895 ;; Normalize the body part by inserting appropriate message
1896 ;; tags for every message contents.
1897 (mime-edit-normalize-body)
1898 ;; Counting the number of Content-Type.
1899 (goto-char (point-min))
1900 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1901 (setq nparts (1+ nparts)))
1902 ;; Begin translation.
1904 ((and (<= nparts 1)(not multipart))
1905 ;; It's a singular message.
1906 (goto-char (point-min))
1907 (while (re-search-forward
1908 mime-edit-single-part-tag-regexp nil t)
1910 (buffer-substring (match-beginning 0) (match-end 0)))
1911 (delete-region (match-beginning 0) (1+ (match-end 0)))
1912 (setq contype (mime-edit-get-contype tag))
1913 (setq encoding (mime-edit-get-encoding tag))
1916 ;; It's a multipart message.
1917 (goto-char (point-min))
1918 (and (mime-edit-translate-single-part-tag)
1919 (while (mime-edit-translate-single-part-tag "\n"))
1921 ;; Define Content-Type as "multipart/mixed".
1923 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
1924 ;; Content-Transfer-Encoding must be "7bit".
1925 ;; The following encoding can be `nil', but is
1926 ;; specified as is since there is no way that a user
1928 (setq encoding "7bit")
1929 ;; Insert the trailer.
1930 (goto-char (point-max))
1931 (insert "\n--" boundary "--\n")
1933 (list contype encoding boundary nparts)
1936 (defun mime-edit-normalize-body ()
1937 "Normalize the body part by inserting appropriate message tags."
1938 ;; Insert the first MIME tags if necessary.
1939 (goto-char (point-min))
1940 (if (not (looking-at mime-edit-single-part-tag-regexp))
1941 (insert (mime-make-text-tag) "\n"))
1942 ;; Check each tag, and add new tag or correct it if necessary.
1943 (goto-char (point-min))
1944 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1945 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
1946 (contype (mime-edit-get-contype tag))
1947 (charset (mime-get-parameter contype "charset"))
1948 (encoding (mime-edit-get-encoding tag)))
1949 ;; Remove extra whitespaces after the tag.
1950 (if (looking-at "[ \t]+$")
1951 (delete-region (match-beginning 0) (match-end 0)))
1953 (end (mime-edit-content-end))
1955 (if (= end (point-max))
1958 (or (looking-at mime-edit-beginning-tag-regexp)
1960 (insert (mime-make-text-tag) "\n")
1962 (visible-region beg end)
1966 ((mime-test-content-type contype "message")
1967 ;; Content-type "message" should be sent as is.
1970 ((mime-test-content-type contype "text")
1971 ;; Define charset for text if necessary.
1972 (setq charset (if charset
1973 (intern (downcase charset))
1974 (mime-edit-choose-charset)))
1975 (mime-edit-define-charset charset)
1976 (cond ((string-equal contype "text/x-rot13-47-48")
1979 (mule-caesar-region (point) (mime-edit-content-end))
1981 ((string-equal contype "text/enriched")
1986 (end (mime-edit-content-end))
1988 ;; Patch for hard newlines
1991 ;; (while (search-forward "\n" end t)
1992 ;; (put-text-property (match-beginning 0)
1995 ;; End patch for hard newlines
1996 (enriched-encode beg end)
1998 (if (search-forward "\n\n")
1999 (delete-region beg (match-end 0))
2002 ;; Point is now on current tag.
2003 ;; Define encoding and encode text if necessary.
2004 (or encoding ;Encoding is not specified.
2008 mime-edit-charset-default-encoding-alist)
2010 (beg (mime-edit-content-beginning))
2012 (encode-mime-charset-region beg (mime-edit-content-end)
2014 ;; Protect "From " in beginning of line
2016 (narrow-to-region beg (mime-edit-content-end))
2018 (if (re-search-forward "^From " nil t)
2020 (if (memq charset '(iso-2022-jp
2025 (replace-match "\e(BFrom ")
2026 (re-search-forward "^From " nil t)
2028 (setq encoding "quoted-printable")
2030 ;; canonicalize line break code
2031 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2033 (narrow-to-region beg (mime-edit-content-end))
2035 (while (re-search-forward "\\([^\r]\\)\n" nil t)
2037 (concat (buffer-substring (match-beginning 0)
2038 (match-end 1)) "\r\n"))
2041 (mime-encode-region beg (mime-edit-content-end) encoding)
2042 (mime-edit-define-encoding encoding)
2044 (goto-char (mime-edit-content-end))
2046 ((null encoding) ;Encoding is not specified.
2047 ;; Application, image, audio, video, and any other
2048 ;; unknown content-type without encoding should be
2050 (let* ((encoding "base64") ;Encode in BASE64 by default.
2051 (beg (mime-edit-content-beginning))
2052 (end (mime-edit-content-end))
2053 (body (buffer-substring beg end))
2055 (mime-encode-region beg end encoding)
2056 (mime-edit-define-encoding encoding))
2061 (defun mime-delete-field (field)
2062 "Delete header FIELD."
2063 (let ((regexp (format "^%s:[ \t]*" field)))
2064 (goto-char (point-min))
2065 (while (re-search-forward regexp nil t)
2066 (delete-region (match-beginning 0)
2067 (progn (forward-line 1) (point)))
2072 ;;; Platform dependent functions
2075 ;; Sun implementations
2077 (defun mime-edit-voice-recorder-for-sun (encoding)
2078 "Record voice in a buffer using Sun audio device,
2079 and insert data encoded as ENCODING."
2080 (message "Start the recording on %s. Type C-g to finish the recording..."
2082 (mime-insert-encoded-file "/dev/audio" encoding)
2086 ;;; @ Other useful commands.
2089 ;; Message forwarding commands as content-type "message/rfc822".
2091 (defun mime-edit-insert-message (&optional message)
2093 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2094 (if (and inserter (fboundp inserter))
2096 (mime-edit-insert-tag "message" "rfc822")
2097 (funcall inserter message)
2099 (message "Sorry, I don't have message inserter for your MUA.")
2102 (defun mime-edit-insert-mail (&optional message)
2104 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2105 (if (and inserter (fboundp inserter))
2107 (mime-edit-insert-tag "message" "rfc822")
2108 (funcall inserter message)
2110 (message "Sorry, I don't have mail inserter for your MUA.")
2113 (defun mime-edit-inserted-message-filter ()
2116 (let ((header-start (point))
2117 (case-fold-search t)
2120 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2121 (if (re-search-forward "^$" (mark t))
2122 (narrow-to-region header-start (match-beginning 0))
2124 (goto-char header-start)
2125 (while (and (re-search-forward
2126 mime-edit-yank-ignored-field-regexp nil t)
2127 (setq beg (match-beginning 0))
2128 (setq end (1+ (std11-field-end)))
2130 (delete-region beg end)
2135 ;;; @ multipart enclosure
2138 (defun mime-edit-enclose-region (type beg end)
2141 (let ((current (point)))
2143 (narrow-to-region beg end)
2144 (insert (format "--<<%s>>-{\n" type))
2145 (goto-char (point-max))
2146 (insert (format "--}-<<%s>>\n" type))
2147 (goto-char (point-max))
2149 (or (looking-at mime-edit-beginning-tag-regexp)
2151 (insert (mime-make-text-tag) "\n")
2155 (defun mime-edit-enclose-quote-region (beg end)
2157 (mime-edit-enclose-region "quote" beg end)
2160 (defun mime-edit-enclose-mixed-region (beg end)
2162 (mime-edit-enclose-region "mixed" beg end)
2165 (defun mime-edit-enclose-parallel-region (beg end)
2167 (mime-edit-enclose-region "parallel" beg end)
2170 (defun mime-edit-enclose-digest-region (beg end)
2172 (mime-edit-enclose-region "digest" beg end)
2175 (defun mime-edit-enclose-alternative-region (beg end)
2177 (mime-edit-enclose-region "alternative" beg end)
2180 (defun mime-edit-enclose-signed-region (beg end)
2182 (if mime-edit-signing-type
2183 (mime-edit-enclose-region "signed" beg end)
2184 (message "Please specify signing type.")
2187 (defun mime-edit-enclose-encrypted-region (beg end)
2189 (if mime-edit-signing-type
2190 (mime-edit-enclose-region "encrypted" beg end)
2191 (message "Please specify encrypting type.")
2194 (defun mime-edit-insert-key (&optional arg)
2195 "Insert a pgp public key."
2197 (mime-edit-insert-tag "application" "pgp-keys")
2198 (mime-edit-define-encoding "7bit")
2199 (funcall (pgp-function 'insert-key))
2206 (defun mime-edit-set-split (arg)
2209 (y-or-n-p "Do you want to enable split?")
2211 (setq mime-edit-split-message arg)
2213 (message "This message is enabled to split.")
2214 (message "This message is not enabled to split.")
2217 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2218 "Toggle transfer-level is 7bit or 8bit through.
2220 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2222 (if (numberp transfer-level)
2223 (setq mime-transfer-level transfer-level)
2224 (if (< mime-transfer-level 8)
2225 (setq mime-transfer-level 8)
2226 (setq mime-transfer-level 7)
2228 (setq mime-edit-charset-default-encoding-alist
2229 (mime-make-charset-default-encoding-alist mime-transfer-level))
2230 (message (format "Current transfer-level is %d bit"
2231 mime-transfer-level))
2232 (setq mime-transfer-level-string
2233 (mime-encoding-name mime-transfer-level 'not-omit))
2234 (force-mode-line-update)
2237 (defun mime-edit-set-transfer-level-7bit ()
2239 (mime-edit-toggle-transfer-level 7)
2242 (defun mime-edit-set-transfer-level-8bit ()
2244 (mime-edit-toggle-transfer-level 8)
2251 (defun mime-edit-set-sign (arg)
2254 (y-or-n-p "Do you want to sign?")
2257 (if mime-edit-signing-type
2259 (setq mime-edit-pgp-processing 'sign)
2260 (message "This message will be signed.")
2262 (message "Please specify signing type.")
2264 (if (eq mime-edit-pgp-processing 'sign)
2265 (setq mime-edit-pgp-processing nil)
2267 (message "This message will not be signed.")
2270 (defun mime-edit-set-encrypt (arg)
2273 (y-or-n-p "Do you want to encrypt?")
2276 (if mime-edit-encrypting-type
2278 (setq mime-edit-pgp-processing 'encrypt)
2279 (message "This message will be encrypt.")
2281 (message "Please specify encrypting type.")
2283 (if (eq mime-edit-pgp-processing 'encrypt)
2284 (setq mime-edit-pgp-processing nil)
2286 (message "This message will not be encrypt.")
2289 (defvar mime-edit-pgp-processing nil)
2290 (make-variable-buffer-local 'mime-edit-pgp-processing)
2292 (defun mime-edit-pgp-enclose-buffer ()
2293 (let ((beg (save-excursion
2294 (goto-char (point-min))
2295 (if (search-forward (concat "\n" mail-header-separator "\n"))
2301 (cond ((eq mime-edit-pgp-processing 'sign)
2302 (mime-edit-enclose-signed-region beg end)
2304 ((eq mime-edit-pgp-processing 'encrypt)
2305 (mime-edit-enclose-encrypted-region beg end)
2313 (defun mime-edit-insert-partial-header (fields subject
2314 id number total separator)
2316 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2317 (insert mime-edit-mime-version-field-for-message/partial)
2319 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2320 id number total separator))
2323 (defun mime-edit-split-and-send
2324 (&optional cmd lines mime-edit-message-max-length)
2328 (count-lines (point-min) (point-max)))
2330 (or mime-edit-message-max-length
2331 (setq mime-edit-message-max-length
2332 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2333 mime-edit-message-default-max-lines))
2335 (let* ((mime-edit-draft-file-name
2336 (or (buffer-file-name)
2338 (expand-file-name "mime-draft" mime-temp-directory))))
2339 (separator mail-header-separator)
2341 (replace-space-with-underline (current-time-string))
2342 "@" (system-name) "\"")))
2343 (run-hooks 'mime-edit-before-split-hook)
2344 (let ((the-buf (current-buffer))
2345 (copy-buf (get-buffer-create " *Original Message*"))
2346 (header (std11-header-string-except
2347 mime-edit-split-ignored-field-regexp separator))
2348 (subject (mail-fetch-field "subject"))
2349 (total (+ (/ lines mime-edit-message-max-length)
2350 (if (> (mod lines mime-edit-message-max-length) 0)
2356 mime-edit-split-message-sender-alist))
2360 (error "Split sender is not specified for `%s'." major-mode)
2363 (mime-edit-partial-number 1)
2366 (set-buffer copy-buf)
2368 (insert-buffer the-buf)
2370 (if (re-search-forward
2371 (concat "^" (regexp-quote separator) "$") nil t)
2372 (let ((he (match-beginning 0)))
2374 (narrow-to-region (point-min) he)
2376 (goto-char (point-min))
2377 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2378 (delete-region (match-beginning 0)
2379 (1+ (std11-field-end)))
2381 (while (< mime-edit-partial-number total)
2384 (set-buffer copy-buf)
2385 (setq data (buffer-substring
2388 (goto-line mime-edit-message-max-length)
2391 (delete-region (point-min)(point))
2393 (mime-edit-insert-partial-header
2394 header subject id mime-edit-partial-number total separator)
2397 (message (format "Sending %d/%d..."
2398 mime-edit-partial-number total))
2399 (call-interactively command)
2400 (message (format "Sending %d/%d... done"
2401 mime-edit-partial-number total))
2403 (setq mime-edit-partial-number
2404 (1+ mime-edit-partial-number))
2408 (set-buffer copy-buf)
2409 (setq data (buffer-string))
2412 (mime-edit-insert-partial-header
2413 header subject id mime-edit-partial-number total separator)
2416 (message (format "Sending %d/%d..."
2417 mime-edit-partial-number total))
2418 (message (format "Sending %d/%d... done"
2419 mime-edit-partial-number total))
2423 (defun mime-edit-maybe-split-and-send (&optional cmd)
2425 (run-hooks 'mime-edit-before-send-hook)
2426 (let ((mime-edit-message-max-length
2427 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2428 mime-edit-message-default-max-lines))
2429 (lines (count-lines (point-min) (point-max)))
2431 (if (and (> lines mime-edit-message-max-length)
2432 mime-edit-split-message)
2433 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2437 ;;; @ preview message
2440 (defvar mime-edit-buffer nil) ; buffer local variable
2442 (defun mime-edit-preview-message ()
2443 "preview editing MIME message."
2445 (let* ((str (buffer-string))
2446 (separator mail-header-separator)
2447 (the-buf (current-buffer))
2448 (buf-name (buffer-name))
2449 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2450 (buf (get-buffer temp-buf-name))
2454 (switch-to-buffer buf)
2457 (setq buf (get-buffer-create temp-buf-name))
2458 (switch-to-buffer buf)
2461 (setq major-mode 'mime-temp-message-mode)
2462 (make-local-variable 'mail-header-separator)
2463 (setq mail-header-separator separator)
2464 (make-local-variable 'mime-edit-buffer)
2465 (setq mime-edit-buffer the-buf)
2467 (run-hooks 'mime-edit-translate-hook)
2468 (mime-edit-translate-buffer)
2469 (goto-char (point-min))
2470 (if (re-search-forward
2471 (concat "^" (regexp-quote separator) "$"))
2477 (defun mime-edit-quitting-method ()
2478 "Quitting method for mime-view."
2479 (let ((temp mime-raw-buffer)
2481 (mime-view-kill-buffer)
2483 (setq buf mime-edit-buffer)
2485 (switch-to-buffer buf)
2488 (set-alist 'mime-view-quitting-method-alist
2489 'mime-temp-message-mode
2490 (function mime-edit-quitting-method)
2497 (defvar mime-edit-again-ignored-field-regexp
2498 (concat "^\\(" "Content-.*\\|Mime-Version"
2499 (if mime-edit-insert-x-emacs-field "\\|X-Emacs")
2501 "Regexp for deleted header fields when `mime-edit-again' is called.")
2503 (defun mime-edit-decode-buffer (not-decode-text)
2505 (goto-char (point-min))
2506 (let ((ctl (mime-read-Content-Type)))
2508 (let ((type (car ctl))
2509 (stype (car (cdr ctl)))
2510 (params (cdr (cdr ctl)))
2513 ((and (eq type 'application)(eq stype 'pgp-signature))
2514 (delete-region (point-min)(point-max))
2516 ((eq type 'multipart)
2517 (let* ((boundary (cdr (assoc "boundary" params)))
2519 (concat "\n--" (regexp-quote boundary) "[ \t]*\n"))
2521 (re-search-forward boundary-pat nil t)
2522 (let ((bb (match-beginning 0)) eb tag)
2523 (setq tag (format "\n--<<%s>>-{\n" stype))
2526 (setq bb (+ bb (length tag)))
2528 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2530 (setq eb (match-beginning 0))
2531 (replace-match (format "--}-<<%s>>\n" stype))
2533 (narrow-to-region bb eb)
2534 (goto-char (point-min))
2535 (while (re-search-forward boundary-pat nil t)
2536 (let ((beg (match-beginning 0))
2538 (delete-region beg (match-end 0))
2540 (if (re-search-forward boundary-pat nil t)
2541 (setq end (match-beginning 0))
2542 (setq end (point-max))
2545 (narrow-to-region beg end)
2546 (mime-edit-decode-buffer not-decode-text)
2547 (goto-char (point-max))
2550 (goto-char (point-min))
2551 (or (= (point-min) 1)
2552 (delete-region (point-min)
2553 (if (search-forward "\n\n" nil t)
2559 (let* ((ctype (format "%s/%s" type stype))
2562 (let ((bytes (+ 14 (length ctype))))
2563 (mapconcat (function
2565 (if (string= (car attr) "charset")
2567 (setq charset (cdr attr))
2575 (setq bytes (+ bytes bs 2))
2578 (setq bytes (+ bs 1))
2586 (if (re-search-forward
2587 "Content-Transfer-Encoding:" nil t)
2588 (let ((beg (match-beginning 0))
2589 (hbeg (match-end 0))
2590 (end (std11-field-end)))
2592 (eliminate-top-spaces
2593 (std11-unfold-string
2594 (buffer-substring hbeg end))))
2595 (if (or charset (eq type 'text))
2597 (delete-region beg (1+ end))
2598 (goto-char (point-min))
2599 (if (search-forward "\n\n" nil t)
2602 (match-end 0)(point-max) encoding)
2606 (if (or encoded (not not-decode-text))
2607 (decode-mime-charset-region
2608 (point-min)(point-max)
2609 (or charset default-mime-charset))
2612 (if (re-search-forward "^$" nil t)
2616 (if (= (point-min) 1)
2622 (format "%s/%s%s" type stype pstr)
2625 (delete-region (point-min) he)
2628 (format "%s/%s%s" type stype pstr)
2633 (decode-mime-charset-region (point-min) (point-max)
2634 default-mime-charset)
2638 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2639 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2640 Content-Type and Content-Transfer-Encoding header fields will be
2641 converted to MIME-Edit tags."
2643 (goto-char (point-min))
2645 (concat "\n" (regexp-quote mail-header-separator) "\n")
2647 (replace-match "\n\n")
2649 (mime-edit-decode-buffer not-decode-text)
2650 (goto-char (point-min))
2652 (std11-narrow-to-header)
2653 (goto-char (point-min))
2654 (while (re-search-forward mime-edit-again-ignored-field-regexp nil t)
2655 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2658 (and (re-search-forward "^$")
2659 (replace-match mail-header-separator)
2669 (provide 'mime-edit)
2671 (run-hooks 'mime-edit-load-hook)
2673 ;;; mime-edit.el ends here