1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,94,95,96,97,98,99 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
8 ;; Created: 1994/08/21 renamed from mime.el
9 ;; Renamed: 1997/2/21 from tm-edit.el
10 ;; Keywords: MIME, multimedia, multilingual, mail, news
12 ;; This file is part of SEMI (Sophisticated Emacs MIME Interfaces).
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; This is an Emacs minor mode for editing Internet multimedia
32 ;; messages formatted in MIME (RFC 2045, 2046, 2047, 2048 and 2049).
33 ;; All messages in this mode are composed in the tagged MIME format,
34 ;; that are described in the following examples. The messages
35 ;; composed in the tagged MIME format are automatically translated
36 ;; into a MIME compliant message when exiting the mode.
38 ;; Mule (multilingual feature of Emacs 20 and multilingual extension
39 ;; for XEmacs 20) has a capability of handling multilingual text in
40 ;; limited ISO-2022 manner that is based on early experiences in
41 ;; Japanese Internet community and resulted in RFC 1468 (ISO-2022-JP
42 ;; charset for MIME). In order to enable multilingual capability in
43 ;; single text message in MIME, charset of multilingual text written
44 ;; in Mule is declared as either `ISO-2022-JP-2' [RFC 1554]. Mule is
45 ;; required for reading the such messages.
47 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
48 ;; News mode. First of all, you need the following autoload
49 ;; definition to load mime-edit-mode automatically:
51 ;; (autoload 'turn-on-mime-edit "mime-edit"
52 ;; "Minor mode for editing MIME message." t)
54 ;; In case of Mail mode (includes VM mode), you need the following
57 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
58 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
60 ;; In case of MH-E, you need the following hook definition:
62 ;; (add-hook 'mh-letter-mode-hook
65 ;; (turn-on-mime-edit)
66 ;; (make-local-variable 'mail-header-separator)
67 ;; (setq mail-header-separator "--------")
69 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
71 ;; In case of News mode, you need the following hook definition:
73 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
74 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
76 ;; In case of Emacs 19, it is possible to emphasize the message tags
77 ;; using font-lock mode as follows:
79 ;; (add-hook 'mime-edit-mode-hook
83 ;; (setq font-lock-keywords (list mime-edit-tag-regexp))
86 ;; The message tag looks like:
88 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
90 ;; The tagged MIME message examples:
92 ;; This is a conventional plain text. It should be translated into
96 ;; This is also a plain text. But, it is explicitly specified as is.
97 ;;--[[text/plain; charset=ISO-8859-1]]
98 ;; This is also a plain text. But charset is specified as iso-8859-1.
100 ;; ¡Hola! Buenos días. ¿Cómo está usted?
101 ;;--[[text/enriched]]
102 ;; <center>This is a richtext.</center>
104 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
106 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
111 (require 'mail-utils)
113 (require 'eword-encode) ; eword-encode-field-body
121 (autoload 'pgg-encrypt-region "pgg"
122 "PGP encryption of current region." t)
123 (autoload 'pgg-sign-region "pgg"
124 "PGP signature of current region." t)
125 (autoload 'pgg-insert-key "pgg"
126 "Insert PGP public key at point." t)
133 (defconst mime-edit-version
135 (mime-product-name mime-user-interface-product) " "
136 (mapconcat #'number-to-string
137 (mime-product-version mime-user-interface-product) ".")
138 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
144 (defgroup mime-edit nil
148 (defcustom mime-ignore-preceding-spaces nil
149 "*Ignore preceding white spaces if non-nil."
153 (defcustom mime-ignore-trailing-spaces nil
154 "*Ignore trailing white spaces if non-nil."
158 (defcustom mime-ignore-same-text-tag t
159 "*Ignore preceding text content-type tag that is same with new one.
160 If non-nil, the text tag is not inserted unless something different."
164 (defcustom mime-auto-hide-body t
165 "*Hide non-textual body encoded in base64 after insertion if non-nil."
169 (defcustom mime-edit-voice-recorder
170 (function mime-edit-voice-recorder-for-sun)
171 "*Function to record a voice message and encode it."
175 (defcustom mime-edit-mode-hook nil
176 "*Hook called when enter MIME mode."
180 (defcustom mime-edit-translate-hook nil
181 "*Hook called before translating into a MIME compliant message.
182 To insert a signature file automatically, call the function
183 `mime-edit-insert-signature' from this hook."
187 (defcustom mime-edit-exit-hook nil
188 "*Hook called when exit MIME mode."
192 (defvar mime-content-types
194 ;; Charset parameter need not to be specified, since it is
195 ;; defined automatically while translation.
197 ;;("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"))
223 ("server" "ftpmail@nic.karrn.ad.jp")
231 ("octet-stream" ("type" "" "tar" "shar"))
233 ("vnd.ms-powerpoint")
234 ("x-kiss" ("x-cnf")))
248 "*Alist of content-type, subtype, parameters and its values.")
250 (defcustom mime-file-types
253 ;; Programming languages
256 "application" "octet-stream" (("type" . "C++"))
258 "attachment" (("filename" . file))
262 "application" "octet-stream" (("type" . "emacs-lisp"))
264 "attachment" (("filename" . file))
268 "application" "octet-stream" (("type" . "common-lisp"))
270 "attachment" (("filename" . file))
274 "application" "octet-stream" (("type" . "perl"))
276 "attachment" (("filename" . file))
279 ;; Text or translated text
284 "inline" (("filename" . file))
287 ;; .rc : procmail modules pm-xxxx.rc
288 ;; *rc : other resource files
290 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
293 "attachment" (("filename" . file))
301 ("\\.diff$\\|\\.patch$"
302 "application" "octet-stream" (("type" . "patch"))
304 "attachment" (("filename" . file))
308 "text" "plain" nil nil nil nil)
311 ;; Octect binary text
314 "application" "winword" nil
316 "attachment" (("filename" . file))
318 ("\\.ppt$" ; MS Power Point
319 "application" "vnd.ms-powerpoint" nil
321 "attachment" (("filename" . file))
327 "inline" (("filename" . file))
330 "application" "postscript" nil
332 "attachment" (("filename" . file))
340 "inline" (("filename" . file))
345 "inline" (("filename" . file))
350 "inline" (("filename" . file))
355 "inline" (("filename" . file))
360 "inline" (("filename" . file))
365 "inline" (("filename" . file))
370 "inline" (("filename" . file))
375 "inline" (("filename" . file))
380 "attachment" (("filename" . file))
385 "attachment" (("filename" . file))
388 "application" "octet-stream" (("type" . "tar+gzip"))
390 "attachment" (("filename" . file))
393 "application" "octet-stream" (("type" . "tar+gzip"))
395 "attachment" (("filename" . file))
398 "application" "octet-stream" (("type" . "tar+compress"))
400 "attachment" (("filename" . file))
403 "application" "octet-stream" (("type" . "tar+compress"))
405 "attachment" (("filename" . file))
408 "application" "octet-stream" (("type" . "gzip"))
410 "attachment" (("filename" . file))
413 "application" "octet-stream" (("type" . "compress"))
415 "attachment" (("filename" . file))
418 "application" "octet-stream" (("type" . "lha"))
420 "attachment" (("filename" . file))
423 "application" "zip" nil
425 "attachment" (("filename" . file))
431 "application" "octet-stream" nil
433 "attachment" (("filename" . file)))
435 "*Alist of file name, types, parameters, and default encoding.
436 If encoding is nil, it is determined from its contents."
440 (choice :tag "Primary-Type"
441 ,@(nconc (mapcar (lambda (cell)
442 (list 'item (car cell))
447 (choice :tag "Sub-Type"
450 (mapcar (lambda (cell)
451 (mapcar (lambda (cell)
452 (list 'item (car cell))
458 (repeat :tag "Parameters of Content-Type field"
459 (cons string (choice string symbol)))
460 ;; content-transfer-encoding
461 (choice :tag "Encoding"
464 (mapcar (lambda (cell)
467 (mime-encoding-list))))
469 (choice :tag "Disposition-Type"
475 (repeat :tag "Parameters of Content-Disposition field"
476 (cons string (choice string symbol)))
481 ;;; @@ about charset, encoding and transfer-level
484 (defvar mime-charset-type-list
486 (iso-8859-1 8 "quoted-printable")
487 (iso-8859-2 8 "quoted-printable")
488 (iso-8859-3 8 "quoted-printable")
489 (iso-8859-4 8 "quoted-printable")
490 (iso-8859-5 8 "quoted-printable")
491 (koi8-r 8 "quoted-printable")
492 (iso-8859-7 8 "quoted-printable")
493 (iso-8859-8 8 "quoted-printable")
494 (iso-8859-9 8 "quoted-printable")
495 (iso-2022-jp 7 "base64")
496 (iso-2022-kr 7 "base64")
502 (shift_jis 8 "base64")
504 (iso-2022-jp-2 7 "base64")
505 (iso-2022-int-1 7 "base64")
508 (defvar mime-transfer-level 7
509 "*A number of network transfer level. It should be bigger than 7.")
510 (make-variable-buffer-local 'mime-transfer-level)
512 (defsubst mime-encoding-name (transfer-level &optional not-omit)
513 (cond ((> transfer-level 8) "binary")
514 ((= transfer-level 8) "8bit")
518 (defvar mime-transfer-level-string
519 (mime-encoding-name mime-transfer-level 'not-omit)
520 "A string formatted version of mime-transfer-level")
521 (make-variable-buffer-local 'mime-transfer-level-string)
524 ;;; @@ about content transfer encoding
526 (defvar mime-content-transfer-encoding-priority-list
527 '(nil "8bit" "binary"))
529 ;;; @@ about message inserting
532 (defvar mime-edit-yank-ignored-field-list
533 '("Received" "Approved" "Path" "Replied" "Status"
534 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
535 "Delete these fields from original message when it is inserted
536 as message/rfc822 part.
537 Each elements are regexp of field-name.")
539 (defvar mime-edit-yank-ignored-field-regexp
541 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
544 (defvar mime-edit-message-inserter-alist nil)
545 (defvar mime-edit-mail-inserter-alist nil)
548 ;;; @@ about message splitting
551 (defcustom mime-edit-split-message t
552 "*Split large message if it is non-nil."
556 (defcustom mime-edit-message-default-max-lines 1000
557 "*Default maximum lines of a message."
561 (defcustom mime-edit-message-max-lines-alist
562 '((news-reply-mode . 500))
563 "Alist of major-mode vs maximum lines of a message.
564 If it is not specified for a major-mode,
565 `mime-edit-message-default-max-lines' is used."
569 (defconst mime-edit-split-ignored-field-regexp
570 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
572 (defcustom mime-edit-split-blind-field-regexp
573 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
574 "*Regular expression to match field-name to be ignored when split sending."
578 (defvar mime-edit-split-message-sender-alist nil)
580 (defvar mime-edit-news-reply-mode-server-running nil)
586 (defconst mime-edit-single-part-tag-regexp
587 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
588 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
590 (defconst mime-edit-quoted-single-part-tag-regexp
591 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
593 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
595 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
597 (defconst mime-edit-beginning-tag-regexp
598 (regexp-or mime-edit-single-part-tag-regexp
599 mime-edit-multipart-beginning-regexp))
601 (defconst mime-edit-end-tag-regexp
602 (regexp-or mime-edit-single-part-tag-regexp
603 mime-edit-multipart-end-regexp))
605 (defconst mime-edit-tag-regexp
606 (regexp-or mime-edit-single-part-tag-regexp
607 mime-edit-multipart-beginning-regexp
608 mime-edit-multipart-end-regexp))
610 (defvar mime-tag-format "--[[%s]]"
611 "*Control-string making a MIME tag.")
613 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
614 "*Control-string making a MIME tag with encoding.")
617 ;;; @@ multipart boundary
620 (defvar mime-multipart-boundary "Multipart"
621 "*Boundary of a multipart message.")
624 ;;; @@ optional header fields
627 (defvar mime-edit-insert-user-agent-field t
628 "*If non-nil, insert User-Agent header field.")
630 (defvar mime-edit-user-agent-value
631 (concat (mime-product-name mime-user-interface-product)
633 (mapconcat #'number-to-string
634 (mime-product-version mime-user-interface-product) ".")
636 (mime-product-code-name mime-user-interface-product)
638 (mime-product-name mime-library-product)
640 (mapconcat #'number-to-string
641 (mime-product-version mime-library-product) ".")
643 (mime-product-code-name mime-library-product)
648 (concat (apel-version) " "))
650 (if (featurep 'xemacs)
651 (concat (cond ((featurep 'utf-2000)
652 (concat "UTF-2000-MULE/" utf-2000-version))
653 ((featurep 'mule) "MULE"))
655 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
658 (substring emacs-version 0 (match-end 0))
659 (cond ((and (boundp 'xemacs-betaname)
661 ;; It does not exist in XEmacs
662 ;; versions prior to 20.3.
663 (concat " " xemacs-betaname))
664 ((and (boundp 'emacs-patch-level)
666 ;; It does not exist in FSF Emacs or in
667 ;; XEmacs versions earlier than 21.1.1.
668 (format " (patch %d)" emacs-patch-level))
670 " (" xemacs-codename ") ("
671 system-configuration ")")
672 " (" emacs-version ")"))
673 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
674 (substring emacs-version 0 (match-beginning 0))
677 (if (boundp 'enable-multibyte-characters)
679 " (" system-configuration ")"
680 (if enable-multibyte-characters
681 (concat " MULE/" mule-version)
682 " (with unibyte mode)")
683 (if (featurep 'meadow)
684 (let ((mver (Meadow-version)))
685 (if (string-match "^Meadow-" mver)
690 (concat "MULE/" mule-version
691 " (based on Emacs " ver ")"))
692 (concat "Emacs/" ver " (" system-configuration ")")))))
693 "Body of User-Agent field.
694 If variable `mime-edit-insert-user-agent-field' is not nil, it is
695 inserted into message header.")
701 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
702 "*Specify MIME tspecials.
703 Tspecials means any character that matches with it in header must be quoted.")
705 (defconst mime-edit-mime-version-value
706 (concat "1.0 (generated by " mime-edit-version ")")
707 "MIME version number.")
709 (defconst mime-edit-mime-version-field-for-message/partial
710 (concat "MIME-Version:"
711 (eword-encode-field-body
712 (concat " 1.0 (split by " mime-edit-version ")\n")
714 "MIME version field for message/partial.")
717 ;;; @ keymap and menu
720 (defvar mime-edit-mode-flag nil)
721 (make-variable-buffer-local 'mime-edit-mode-flag)
723 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
724 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
725 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
726 "Keymap for MIME-Edit mode commands to insert entity or set status.")
728 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
729 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
730 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
731 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
732 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
733 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
734 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
735 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
736 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
737 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
739 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
740 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
741 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
742 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
743 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
744 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
745 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
746 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
747 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
748 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
750 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
751 "Keymap prefix for MIME-Edit mode commands about enclosure.")
752 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
753 "Keymap for MIME-Edit mode commands about enclosure.")
755 (define-key mime-edit-mode-enclosure-map
756 "\C-a" 'mime-edit-enclose-alternative-region)
757 (define-key mime-edit-mode-enclosure-map
758 "\C-p" 'mime-edit-enclose-parallel-region)
759 (define-key mime-edit-mode-enclosure-map
760 "\C-m" 'mime-edit-enclose-mixed-region)
761 (define-key mime-edit-mode-enclosure-map
762 "\C-d" 'mime-edit-enclose-digest-region)
763 (define-key mime-edit-mode-enclosure-map
764 "\C-s" 'mime-edit-enclose-pgp-signed-region)
765 (define-key mime-edit-mode-enclosure-map
766 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
767 (define-key mime-edit-mode-enclosure-map
768 "\C-q" 'mime-edit-enclose-quote-region)
770 (defvar mime-edit-mode-map (make-sparse-keymap)
771 "Keymap for MIME-Edit mode commands.")
772 (define-key mime-edit-mode-map
773 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
774 (define-key mime-edit-mode-map
775 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
777 (defconst mime-edit-menu-title "MIME-Edit")
779 (defconst mime-edit-menu-list
780 '((mime-help "Describe MIME editor mode" mime-edit-help)
781 (file "Insert File" mime-edit-insert-file)
782 (external "Insert External" mime-edit-insert-external)
783 (voice "Insert Voice" mime-edit-insert-voice)
784 (message "Insert Message" mime-edit-insert-message)
785 (mail "Insert Mail" mime-edit-insert-mail)
786 (signature "Insert Signature" mime-edit-insert-signature)
787 (text "Insert Text" mime-edit-insert-text)
788 (tag "Insert Tag" mime-edit-insert-tag)
789 (alternative "Enclose as alternative"
790 mime-edit-enclose-alternative-region)
791 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
792 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
793 (digest "Enclose as digest" mime-edit-enclose-digest-region)
794 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
795 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
796 (quote "Verbatim region" mime-edit-enclose-quote-region)
797 (key "Insert Public Key" mime-edit-insert-key)
798 (split "About split" mime-edit-set-split)
799 (sign "About sign" mime-edit-set-sign)
800 (encrypt "About encryption" mime-edit-set-encrypt)
801 (preview "Preview Message" mime-edit-preview-message)
802 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
804 "MIME-edit menubar entry.")
806 (cond ((featurep 'xemacs)
807 ;; modified by Pekka Marjola <pema@iki.fi>
808 ;; 1995/9/5 (c.f. [tm-en:69])
809 (defun mime-edit-define-menu-for-xemacs ()
810 "Define menu for XEmacs."
811 (cond ((featurep 'menubar)
812 (make-local-variable 'current-menubar)
813 (set-buffer-menubar current-menubar)
816 (cons mime-edit-menu-title
819 (vector (nth 1 item)(nth 2 item)
822 mime-edit-menu-list)))
825 ;; modified by Steven L. Baur <steve@miranova.com>
826 ;; 1995/12/6 (c.f. [tm-en:209])
827 (or (boundp 'mime-edit-popup-menu-for-xemacs)
828 (setq mime-edit-popup-menu-for-xemacs
829 (append '("MIME Commands" "---")
830 (mapcar (function (lambda (item)
834 mime-edit-menu-list)))
837 ((>= emacs-major-version 19)
838 (define-key mime-edit-mode-map [menu-bar mime-edit]
839 (cons mime-edit-menu-title
840 (make-sparse-keymap mime-edit-menu-title)))
843 (define-key mime-edit-mode-map
844 (vector 'menu-bar 'mime-edit (car item))
845 (cons (nth 1 item)(nth 2 item))
848 (reverse mime-edit-menu-list)
856 (defvar mime-edit-touched-flag nil)
859 (defun mime-edit-mode ()
860 "MIME minor mode for editing the tagged MIME message.
862 In this mode, basically, the message is composed in the tagged MIME
863 format. The message tag looks like:
865 --[[text/plain; charset=ISO-2022-JP][7bit]]
867 The tag specifies the MIME content type, subtype, optional parameters
868 and transfer encoding of the message following the tag. Messages
869 without any tag are treated as `text/plain' by default. Charset and
870 transfer encoding are automatically defined unless explicitly
871 specified. Binary messages such as audio and image are usually
872 hidden. The messages in the tagged MIME format are automatically
873 translated into a MIME compliant message when exiting this mode.
875 Available charsets depend on Emacs version being used. The following
876 lists the available charsets of each emacs.
878 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
879 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
880 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
881 CN-BIG5 and ISO-2022-INT-1 are available.
883 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
884 be used to represent multilingual text in intermixed manner. Any
885 languages that has no registered charset are represented as either
886 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
888 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
889 without mule, please set variable `default-mime-charset'. This
890 variable must be symbol of which name is a MIME charset.
892 If you want to add more charsets in mule, please set variable
893 `charsets-mime-charset-alist'. This variable must be alist of which
894 key is list of charset and value is symbol of MIME charset. If name
895 of coding-system is different as MIME charset, please set variable
896 `mime-charset-coding-system-alist'. This variable must be alist of
897 which key is MIME charset and value is coding-system.
899 Following commands are available in addition to major mode commands:
902 \\[mime-edit-insert-text] insert a text message.
903 \\[mime-edit-insert-file] insert a (binary) file.
904 \\[mime-edit-insert-external] insert a reference to external body.
905 \\[mime-edit-insert-voice] insert a voice message.
906 \\[mime-edit-insert-message] insert a mail or news message.
907 \\[mime-edit-insert-mail] insert a mail message.
908 \\[mime-edit-insert-signature] insert a signature file at end.
909 \\[mime-edit-insert-key] insert PGP public key.
910 \\[mime-edit-insert-tag] insert a new MIME tag.
912 \[make enclosure (maybe multipart)\]
913 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
914 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
915 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
916 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
917 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
918 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
919 \\[mime-edit-enclose-quote-region] enclose as verbose mode
920 (to avoid to expand tags)
923 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
924 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
925 \\[mime-edit-set-split] set message splitting mode.
926 \\[mime-edit-set-sign] set PGP-sign mode.
927 \\[mime-edit-set-encrypt] set PGP-encryption mode.
928 \\[mime-edit-preview-message] preview editing MIME message.
929 \\[mime-edit-exit] exit and translate into a MIME
931 \\[mime-edit-help] show this help.
932 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
935 Additional commands are available in some major modes:
936 C-c C-c exit, translate and run the original command.
937 C-c C-s exit, translate and run the original command.
939 The following is a message example written in the tagged MIME format.
940 TABs at the beginning of the line are not a part of the message:
942 This is a conventional plain text. It should be translated
945 This is also a plain text. But, it is explicitly specified as
947 --[[text/plain; charset=ISO-8859-1]]
948 This is also a plain text. But charset is specified as
951 ¡Hola! Buenos días. ¿Cómo está usted?
953 This is a <bold>enriched text</bold>.
954 --[[image/gif][base64]]...image encoded in base64 here...
955 --[[audio/basic][base64]]...audio encoded in base64 here...
957 User customizable variables (not documented all of them):
959 Specifies a key prefix for MIME minor mode commands.
961 mime-ignore-preceding-spaces
962 Preceding white spaces in a message body are ignored if non-nil.
964 mime-ignore-trailing-spaces
965 Trailing white spaces in a message body are ignored if non-nil.
968 Hide a non-textual body message encoded in base64 after insertion
972 A number of network transfer level. It should be bigger than 7.
973 If you are in 8bit-through environment, please set 8.
975 mime-edit-voice-recorder
976 Specifies a function to record a voice message and encode it.
977 The function `mime-edit-voice-recorder-for-sun' is for Sun
981 Turning on MIME mode calls the value of mime-edit-mode-hook, if
984 mime-edit-translate-hook
985 The value of mime-edit-translate-hook is called just before translating
986 the tagged MIME format into a MIME compliant message if it is
987 non-nil. If the hook call the function mime-edit-insert-signature,
988 the signature file will be inserted automatically.
991 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
994 (if mime-edit-mode-flag
996 (if mime-edit-touched-flag
998 (make-local-variable 'mime-edit-touched-flag)
999 (setq mime-edit-touched-flag t)
1004 (cond ((featurep 'xemacs)
1005 (add-minor-mode 'mime-edit-mode-flag
1006 '((" MIME-Edit " mime-transfer-level-string))
1012 (set-alist 'minor-mode-alist
1013 'mime-edit-mode-flag
1014 '((" MIME-Edit " mime-transfer-level-string)))
1015 (set-alist 'minor-mode-map-alist
1016 'mime-edit-mode-flag
1022 (defun turn-on-mime-edit ()
1023 "Unconditionally turn on MIME-Edit mode."
1025 (if mime-edit-mode-flag
1026 (error "You are already editing a MIME message.")
1027 (setq mime-edit-mode-flag t)
1029 ;; Set transfer level into mode line
1031 (setq mime-transfer-level-string
1032 (mime-encoding-name mime-transfer-level 'not-omit))
1033 (force-mode-line-update)
1035 ;; Define menu for XEmacs.
1036 (if (featurep 'xemacs)
1037 (mime-edit-define-menu-for-xemacs)
1042 ;; I don't care about saving these.
1043 (setq paragraph-start
1044 (regexp-or mime-edit-single-part-tag-regexp
1046 (setq paragraph-separate
1047 (regexp-or mime-edit-single-part-tag-regexp
1048 paragraph-separate))
1049 (run-hooks 'mime-edit-mode-hook)
1051 (substitute-command-keys
1052 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1056 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1059 (defun mime-edit-exit (&optional nomime no-error)
1060 "Translate the tagged MIME message into a MIME compliant message.
1061 With no argument encode a message in the buffer into MIME, otherwise
1062 just return to previous mode."
1064 (if (not mime-edit-mode-flag)
1066 (error "You aren't editing a MIME message.")
1070 (run-hooks 'mime-edit-translate-hook)
1071 (mime-edit-translate-buffer)))
1072 ;; Restore previous state.
1073 (setq mime-edit-mode-flag nil)
1074 (if (and (featurep 'xemacs)
1075 (featurep 'menubar))
1076 (delete-menu-item (list mime-edit-menu-title))
1079 (set-buffer-modified-p (buffer-modified-p))
1080 (run-hooks 'mime-edit-exit-hook)
1081 (message "Exit MIME editor mode.")
1084 (defun mime-edit-maybe-translate ()
1086 (mime-edit-exit nil t)
1087 (call-interactively 'mime-edit-maybe-split-and-send)
1090 (defun mime-edit-help ()
1091 "Show help message about MIME mode."
1093 (with-output-to-temp-buffer "*Help*"
1094 (princ "MIME editor mode:\n")
1095 (princ (documentation 'mime-edit-mode))
1096 (print-help-return-message)))
1098 (defun mime-edit-insert-text (&optional subtype)
1099 "Insert a text message.
1100 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1101 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1103 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1105 (if (looking-at mime-edit-single-part-tag-regexp)
1107 ;; Make a space between the following message.
1111 (if (and (member (cadr ret) '("enriched"))
1112 (fboundp 'enriched-mode))
1114 (if (boundp 'enriched-mode)
1119 (defun mime-edit-insert-file (file &optional verbose)
1120 "Insert a message from a file."
1121 (interactive "fInsert file as MIME message: \nP")
1122 (let* ((guess (mime-find-file-type file))
1123 (type (nth 0 guess))
1124 (subtype (nth 1 guess))
1125 (parameters (nth 2 guess))
1126 (encoding (nth 3 guess))
1127 (disposition-type (nth 4 guess))
1128 (disposition-params (nth 5 guess))
1131 (setq type (mime-prompt-for-type type)
1132 subtype (mime-prompt-for-subtype type subtype)
1134 (if (or (interactive-p) verbose)
1135 (setq encoding (mime-prompt-for-encoding encoding))
1137 (if (or (consp parameters) (stringp disposition-type))
1138 (let ((rest parameters) cell attribute value)
1139 (setq parameters "")
1141 (setq cell (car rest))
1142 (setq attribute (car cell))
1143 (setq value (cdr cell))
1144 (if (eq value 'file)
1145 (setq value (std11-wrap-as-quoted-string
1146 (file-name-nondirectory file)))
1148 (setq parameters (concat parameters "; " attribute "=" value))
1149 (setq rest (cdr rest))
1151 (if disposition-type
1154 (concat parameters "\n"
1155 "Content-Disposition: " disposition-type))
1156 (setq rest disposition-params)
1158 (setq cell (car rest))
1159 (setq attribute (car cell))
1160 (setq value (cdr cell))
1161 (if (eq value 'file)
1162 (setq value (std11-wrap-as-quoted-string
1163 (file-name-nondirectory file)))
1166 (concat parameters "; " attribute "=" value))
1167 (setq rest (cdr rest))
1171 (mime-edit-insert-tag type subtype parameters)
1172 (mime-edit-insert-binary-file file encoding)
1175 (defun mime-edit-insert-external ()
1176 "Insert a reference to external body."
1178 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1180 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1182 (let* ((pritype (mime-prompt-for-type))
1183 (subtype (mime-prompt-for-subtype pritype))
1184 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1187 (insert "Content-Type: "
1188 pritype "/" subtype (or parameters "") "\n")))
1189 (if (and (not (eobp))
1190 (not (looking-at mime-edit-single-part-tag-regexp)))
1191 (insert (mime-make-text-tag) "\n")))
1193 (defun mime-edit-insert-voice ()
1194 "Insert a voice message."
1198 "What transfer encoding: "
1199 (mime-encoding-alist) nil t nil)))
1200 (mime-edit-insert-tag "audio" "basic" nil)
1201 (mime-edit-define-encoding encoding)
1203 (narrow-to-region (1- (point))(point))
1205 (funcall mime-edit-voice-recorder encoding)
1208 (invisible-region (point-min)(point-max))
1209 (goto-char (point-max))
1212 (defun mime-edit-insert-signature (&optional arg)
1213 "Insert a signature file."
1215 (let ((signature-insert-hook
1218 (let ((items (mime-find-file-type signature-file-name)))
1219 (apply (function mime-edit-insert-tag)
1220 (car items) (cadr items) (list (caddr items))))
1223 (insert-signature arg)
1227 ;; Insert a new tag around a point.
1229 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1230 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1231 If nothing is inserted, return nil."
1234 (mime-edit-goto-tag)
1235 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1236 (< (match-beginning 0) p)
1239 (goto-char (match-beginning 0))
1248 (mime-prompt-for-type)))
1251 (mime-prompt-for-subtype pritype)))
1254 (mime-prompt-for-parameters pritype subtype delimiter)))
1255 ;; Make a new MIME tag.
1256 (setq newtag (mime-make-tag pritype subtype parameters))
1257 ;; Find an current MIME tag.
1260 (if (mime-edit-goto-tag)
1261 (buffer-substring (match-beginning 0) (match-end 0))
1262 ;; Assume content type is 'text/plan'.
1263 (mime-make-tag "text" "plain")
1265 ;; We are only interested in TEXT.
1267 (not (mime-test-content-type
1268 (mime-edit-get-contype oldtag) "text")))
1271 (if (or (not oldtag) ;Not text
1272 (or mime-ignore-same-text-tag
1273 (not (string-equal oldtag newtag))))
1275 ;; Mark the beginning of the tag for convenience.
1276 (push-mark (point) 'nomsg)
1277 (insert newtag "\n")
1278 (list pritype subtype parameters) ;New tag is created.
1280 ;; Restore previous point.
1282 nil ;Nothing is created.
1286 (defun mime-edit-insert-binary-file (file &optional encoding)
1287 "Insert binary FILE at point.
1288 Optional argument ENCODING specifies an encoding method such as base64."
1289 (let* ((tagend (1- (point))) ;End of the tag
1290 (hide-p (and mime-auto-hide-body
1293 (let ((en (downcase encoding)))
1294 (or (string-equal en "7bit")
1295 (string-equal en "8bit")
1296 (string-equal en "binary")
1300 (narrow-to-region tagend (point))
1301 (mime-insert-encoded-file file encoding)
1304 (invisible-region (point-min) (point-max))
1305 (goto-char (point-max))
1307 (goto-char (point-max))
1310 (looking-at mime-edit-tag-regexp)
1311 (= (point)(point-max))
1312 (mime-edit-insert-tag "text" "plain")
1314 ;; Define encoding even if it is 7bit.
1315 (if (stringp encoding)
1317 (goto-char tagend) ; Make sure which line the tag is on.
1318 (mime-edit-define-encoding encoding)
1323 ;; Commands work on a current message flagment.
1325 (defun mime-edit-goto-tag ()
1326 "Search for the beginning of the tagged MIME message."
1327 (let ((current (point)))
1328 (if (looking-at mime-edit-tag-regexp)
1330 ;; At first, go to the end.
1331 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1332 (goto-char (1- (match-beginning 0))) ;For multiline tag
1335 (goto-char (point-max))
1337 ;; Then search for the beginning.
1338 (re-search-backward mime-edit-end-tag-regexp nil t)
1339 (or (looking-at mime-edit-beginning-tag-regexp)
1340 ;; Restore previous point.
1347 (defun mime-edit-content-beginning ()
1348 "Return the point of the beginning of content."
1350 (let ((beg (save-excursion
1351 (beginning-of-line) (point))))
1352 (if (mime-edit-goto-tag)
1353 (let ((top (point)))
1354 (goto-char (match-end 0))
1355 (if (and (= beg top)
1356 (= (following-char) ?\^M))
1360 ;; Default text/plain tag.
1361 (goto-char (point-min))
1363 (concat "\n" (regexp-quote mail-header-separator)
1364 (if mime-ignore-preceding-spaces
1365 "[ \t\n]*\n" "\n")) nil 'move)
1369 (defun mime-edit-content-end ()
1370 "Return the point of the end of content."
1372 (if (mime-edit-goto-tag)
1374 (goto-char (match-end 0))
1375 (if (invisible-p (point))
1376 (next-visible-point (point))
1377 ;; Move to the end of this text.
1378 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1379 ;; Don't forget a multiline tag.
1380 (goto-char (match-beginning 0))
1384 ;; Assume the message begins with text/plain.
1385 (goto-char (mime-edit-content-beginning))
1386 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1387 ;; Don't forget a multiline tag.
1388 (goto-char (match-beginning 0)))
1392 (defun mime-edit-define-charset (charset)
1393 "Set charset of current tag to CHARSET."
1395 (if (mime-edit-goto-tag)
1396 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1397 (delete-region (match-beginning 0) (match-end 0))
1400 (mime-edit-set-parameter
1401 (mime-edit-get-contype tag)
1402 "charset" (upcase (symbol-name charset)))
1403 (mime-edit-get-encoding tag)))
1406 (defun mime-edit-define-encoding (encoding)
1407 "Set encoding of current tag to ENCODING."
1409 (if (mime-edit-goto-tag)
1410 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1411 (delete-region (match-beginning 0) (match-end 0))
1412 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1415 (defun mime-edit-choose-charset ()
1416 "Choose charset of a text following current point."
1417 (detect-mime-charset-region (point) (mime-edit-content-end))
1420 (defun mime-make-text-tag (&optional subtype)
1421 "Make a tag for a text after current point.
1422 Subtype of text type can be specified by an optional argument SUBTYPE.
1423 Otherwise, it is obtained from mime-content-types."
1424 (let* ((pritype "text")
1425 (subtype (or subtype
1426 (car (car (cdr (assoc pritype mime-content-types)))))))
1427 ;; Charset should be defined later.
1428 (mime-make-tag pritype subtype)))
1431 ;; Tag handling functions
1433 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1434 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1435 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1439 (defun mime-create-tag (contype &optional encoding)
1440 "Make a tag with CONTENT-TYPE and optional ENCODING."
1441 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1444 (defun mime-edit-get-contype (tag)
1445 "Return Content-Type (including parameters) of TAG."
1447 (or (string-match mime-edit-single-part-tag-regexp tag)
1448 (string-match mime-edit-multipart-beginning-regexp tag)
1449 (string-match mime-edit-multipart-end-regexp tag)
1451 (substring tag (match-beginning 1) (match-end 1))
1454 (defun mime-edit-get-encoding (tag)
1455 "Return encoding of TAG."
1457 (string-match mime-edit-single-part-tag-regexp tag)
1459 (not (= (match-beginning 3) (match-end 3)))
1460 (substring tag (match-beginning 3) (match-end 3))))
1462 (defun mime-get-parameter (contype parameter)
1463 "For given CONTYPE return value for PARAMETER.
1464 Nil if no such parameter."
1468 (regexp-quote parameter)
1469 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1471 (substring contype (match-beginning 1) (match-end 1))
1472 nil ;No such parameter
1475 (defun mime-edit-set-parameter (contype parameter value)
1476 "For given CONTYPE set PARAMETER to VALUE."
1477 (let (ctype opt-fields)
1478 (if (string-match "\n[^ \t\n\r]+:" contype)
1479 (setq ctype (substring contype 0 (match-beginning 0))
1480 opt-fields (substring contype (match-beginning 0)))
1481 (setq ctype contype)
1486 (regexp-quote parameter)
1487 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1490 (concat (substring ctype 0 (match-beginning 1))
1492 (substring contype (match-end 1))
1494 (concat ctype "; " parameter "=" value opt-fields)
1497 (defun mime-strip-parameters (contype)
1498 "Return primary content-type and subtype without parameters for CONTYPE."
1499 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1500 (substring contype (match-beginning 1) (match-end 1)) nil))
1502 (defun mime-test-content-type (contype type &optional subtype)
1503 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1504 (and (stringp contype)
1507 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1508 (downcase contype))))
1513 (defun mime-find-file-type (file)
1514 "Guess Content-Type, subtype, and parameters from FILE."
1516 (guesses mime-file-types))
1517 (while (and (not guess) guesses)
1518 (if (string-match (car (car guesses)) file)
1519 (setq guess (cdr (car guesses))))
1520 (setq guesses (cdr guesses)))
1524 (defun mime-prompt-for-type (&optional default)
1525 "Ask for Content-type."
1527 ;; Repeat until primary content type is specified.
1528 (while (string-equal type "")
1530 (completing-read "What content type: "
1533 'require-match ;Type must be specified.
1536 (if (string-equal type "")
1538 (message "Content type is required.")
1545 (defun mime-prompt-for-subtype (type &optional default)
1546 "Ask for subtype of media-type TYPE."
1547 (let ((subtypes (cdr (assoc type mime-content-types))))
1549 (assoc default subtypes))
1550 (setq default (car (car subtypes)))
1556 "What content subtype: (default " default ") ")
1557 "What content subtype: ")
1558 (cdr (assoc type mime-content-types))
1560 'require-match ;Subtype must be specified.
1563 (if (string-equal answer "") default answer)))
1565 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1566 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1567 Optional DELIMITER specifies parameter delimiter (';' by default)."
1568 (let* ((delimiter (or delimiter "; "))
1573 (mime-prompt-for-parameters-1
1575 (cdr (assoc pritype mime-content-types))))))
1578 (if (and (stringp parameters)
1579 (not (string-equal parameters "")))
1580 (concat delimiter parameters)
1581 "" ;"" if no parameters
1584 (defun mime-prompt-for-parameters-1 (optlist)
1585 (apply (function append)
1586 (mapcar (function mime-prompt-for-parameter) optlist)))
1588 (defun mime-prompt-for-parameter (parameter)
1590 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1591 (let* ((prompt (car parameter))
1592 (choices (mapcar (function
1594 (if (consp e) e (list e))))
1596 (default (car (car choices)))
1602 (concat "What " prompt
1604 (if (string-equal default "") "\"\"" default)
1606 choices nil nil ""))
1607 ;; If nothing is selected, use default.
1608 (if (string-equal answer "")
1609 (setq answer default)))
1611 (read-string (concat "What " prompt ": "))))
1612 (cons (if (and answer
1613 (not (string-equal answer "")))
1615 ;; Note: control characters ignored!
1616 (if (string-match mime-tspecials-regexp answer)
1617 (concat "\"" answer "\"") answer)))
1618 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1621 (defun mime-prompt-for-encoding (default)
1622 "Ask for Content-Transfer-Encoding."
1627 "What transfer encoding: "
1628 (mime-encoding-alist) nil t default)
1634 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1637 (defvar mime-edit-translate-buffer-hook
1638 '(mime-edit-pgp-enclose-buffer
1639 mime-edit-translate-body
1640 mime-edit-translate-header))
1642 (defun mime-edit-translate-header ()
1643 "Encode the message header into network representation."
1644 (eword-encode-header 'code-conversion)
1645 (run-hooks 'mime-edit-translate-header-hook)
1648 (defun mime-edit-translate-buffer ()
1649 "Encode the tagged MIME message in current buffer in MIME compliant message."
1652 (if (catch 'mime-edit-error
1654 (run-hooks 'mime-edit-translate-buffer-hook)
1658 (error "Translation error!")
1661 (defun mime-edit-find-inmost ()
1662 (goto-char (point-min))
1663 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1664 (let ((bb (match-beginning 0))
1666 (type (buffer-substring (match-beginning 1)(match-end 1)))
1668 (setq end-exp (format "--}-<<%s>>\n" type))
1670 (if (re-search-forward end-exp nil t)
1671 (setq eb (match-beginning 0))
1672 (setq eb (point-max))
1674 (narrow-to-region be eb)
1676 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1678 (narrow-to-region (match-beginning 0)(point-max))
1679 (mime-edit-find-inmost)
1682 (list type bb be eb)
1685 (defun mime-edit-process-multipart-1 (boundary)
1686 (let ((ret (mime-edit-find-inmost)))
1688 (let ((type (car ret))
1689 (bb (nth 1 ret))(be (nth 2 ret))
1692 (narrow-to-region bb eb)
1693 (delete-region bb be)
1694 (setq bb (point-min))
1695 (setq eb (point-max))
1698 (if (looking-at mime-edit-multipart-end-regexp)
1699 (let ((beg (match-beginning 0))
1702 (delete-region beg end)
1703 (or (looking-at mime-edit-beginning-tag-regexp)
1705 (insert (concat (mime-make-text-tag) "\n"))
1707 (cond ((string-equal type "quote")
1708 (mime-edit-enquote-region bb eb)
1710 ((string-equal type "pgp-signed")
1711 (mime-edit-sign-pgp-mime bb eb boundary)
1713 ((string-equal type "pgp-encrypted")
1714 (mime-edit-encrypt-pgp-mime bb eb boundary)
1716 ((string-equal type "kazu-signed")
1717 (mime-edit-sign-pgp-kazu bb eb boundary)
1719 ((string-equal type "kazu-encrypted")
1720 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1724 (nth 2 (mime-edit-translate-region bb eb
1728 (format "--[[multipart/%s;
1729 boundary=\"%s\"][7bit]]\n"
1734 (defun mime-edit-enquote-region (beg end)
1737 (narrow-to-region beg end)
1739 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1740 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1741 (replace-match (concat "- " (substring tag 1)))
1744 (defun mime-edit-dequote-region (beg end)
1747 (narrow-to-region beg end)
1749 (while (re-search-forward
1750 mime-edit-quoted-single-part-tag-regexp nil t)
1751 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1752 (replace-match (concat "-" (substring tag 2)))
1755 (defun mime-edit-sign-pgp-mime (beg end boundary)
1758 (let* ((from (std11-field-body "From" mail-header-separator))
1760 (narrow-to-region beg end)
1761 (mime-edit-translate-region beg end boundary)))
1763 (encoding (nth 1 ret))
1764 (pgp-boundary (concat "pgp-sign-" boundary))
1767 (insert (format "Content-Type: %s\n" ctype))
1769 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1772 (or (let ((pgg-default-user-id
1773 (if (and from (not pgg-default-user-id))
1774 (nth 1 (std11-extract-address-components from))
1775 pgg-default-user-id)))
1776 (pgg-sign-region (point-min)(point-max)))
1777 (throw 'mime-edit-error 'pgp-error)
1780 (cdr (assq 'hash-algorithm
1781 (cdar (with-current-buffer pgg-output-buffer
1782 (pgg-parse-armor-region
1783 (point-min)(point-max))))))
1786 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1789 (insert (format "--[[multipart/signed;
1791 protocol=\"application/pgp-signature\"][7bit]]
1793 " pgp-boundary micalg pgp-boundary))
1794 (goto-char (point-max))
1795 (insert (format "\n--%s
1796 Content-Type: application/pgp-signature
1797 Content-Transfer-Encoding: 7bit
1800 (insert-buffer-substring pgg-output-buffer)
1801 (goto-char (point-max))
1802 (insert (format "\n--%s--\n" pgp-boundary))
1805 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1807 (defun mime-edit-make-encrypt-recipient-header ()
1808 (let* ((names mime-edit-encrypt-recipient-fields-list)
1810 (std11-field-bodies (cons "From" names)
1811 nil mail-header-separator))
1814 (setq values (cdr values))))
1815 (header (and (stringp from)
1816 (if (string-equal from "")
1818 (format "From: %s\n" from)
1821 (while (and names values)
1822 (let ((name (car names))
1823 (value (car values))
1825 (and (stringp value)
1826 (or (string-equal value "")
1828 (setq header (concat header name ": " value "\n")
1829 recipients (if recipients
1830 (concat recipients " ," value)
1833 (setq names (cdr names)
1834 values (cdr values))
1836 (vector from recipients header)
1839 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1842 (let (from recipients header)
1843 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1844 (setq from (aref ret 0)
1845 recipients (aref ret 1)
1846 header (aref ret 2))
1848 (narrow-to-region beg end)
1850 (mime-edit-translate-region beg end boundary))
1852 (encoding (nth 1 ret))
1853 (pgp-boundary (concat "pgp-" boundary)))
1856 (insert (format "Content-Type: %s\n" ctype))
1858 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1861 (eword-encode-header)
1862 (or (let ((pgg-default-user-id
1863 (if (and from (not pgg-default-user-id))
1864 (nth 1 (std11-extract-address-components from))
1865 pgg-default-user-id)))
1867 (point-min) (point-max)
1868 (mapcar (lambda (recipient)
1869 (nth 1 (std11-extract-address-components
1871 (split-string recipients
1872 "\\([ \t\n]*,[ \t\n]*\\)+")))
1874 (throw 'mime-edit-error 'pgp-error)
1876 (delete-region (point-min)(point-max))
1878 (insert (format "--[[multipart/encrypted;
1880 protocol=\"application/pgp-encrypted\"][7bit]]
1882 Content-Type: application/pgp-encrypted
1885 Content-Type: application/octet-stream
1886 Content-Transfer-Encoding: 7bit
1888 " pgp-boundary pgp-boundary pgp-boundary))
1889 (insert-buffer-substring pgg-output-buffer)
1890 (goto-char (point-max))
1891 (insert (format "\n--%s--\n" pgp-boundary))
1894 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1897 (narrow-to-region beg end)
1899 (mime-edit-translate-region beg end boundary))
1901 (encoding (nth 1 ret)))
1903 (insert (format "Content-Type: %s\n" ctype))
1905 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1908 (or (pgg-sign-region beg (point-max) 'clearsign)
1909 (throw 'mime-edit-error 'pgp-error)
1913 "--[[application/pgp; format=mime][7bit]]\n")
1917 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1919 (let (recipients header)
1920 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1921 (setq recipients (aref ret 1)
1922 header (aref ret 2))
1925 (narrow-to-region beg end)
1927 (mime-edit-translate-region beg end boundary))
1929 (encoding (nth 1 ret)))
1932 (insert (format "Content-Type: %s\n" ctype))
1934 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1937 (or (pgg-sign-region beg (point-max) recipients)
1938 (throw 'mime-edit-error 'pgp-error)
1942 "--[[application/pgp; format=mime][7bit]]\n")
1946 (defsubst replace-space-with-underline (str)
1947 (mapconcat (function
1955 (defun mime-edit-make-boundary ()
1956 (concat mime-multipart-boundary "_"
1957 (replace-space-with-underline (current-time-string))
1960 (defun mime-edit-translate-body ()
1961 "Encode the tagged MIME body in current buffer in MIME compliant message."
1964 (let ((boundary (mime-edit-make-boundary))
1967 (while (mime-edit-process-multipart-1
1968 (format "%s-%d" boundary i))
1972 ;; We are interested in message body.
1975 (goto-char (point-min))
1977 (concat "\n" (regexp-quote mail-header-separator)
1978 (if mime-ignore-preceding-spaces
1979 "[ \t\n]*\n" "\n")) nil 'move)
1983 (goto-char (point-max))
1984 (and mime-ignore-trailing-spaces
1985 (re-search-backward "[^ \t\n]\n" beg t)
1988 (setq ret (mime-edit-translate-region
1990 (format "%s-%d" boundary i)))
1992 (mime-edit-dequote-region (point-min)(point-max))
1993 (let ((contype (car ret)) ;Content-Type
1994 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1996 ;; Insert User-Agent field
1997 (and mime-edit-insert-user-agent-field
1998 (or (mail-position-on-field "User-Agent")
1999 (insert mime-edit-user-agent-value)
2001 ;; Make primary MIME headers.
2002 (or (mail-position-on-field "MIME-Version")
2003 (insert mime-edit-mime-version-value))
2004 ;; Remove old Content-Type and other fields.
2006 (goto-char (point-min))
2007 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2008 (narrow-to-region (point-min) (point))
2009 (goto-char (point-min))
2010 (mime-delete-field "Content-Type")
2011 (mime-delete-field "Content-Transfer-Encoding"))
2012 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2013 (mail-position-on-field "Content-Type")
2017 (mail-position-on-field "Content-Transfer-Encoding")
2021 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2022 "Translate single-part-tag to MIME header."
2023 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2024 (let* ((beg (match-beginning 0))
2026 (tag (buffer-substring beg end)))
2027 (delete-region beg end)
2028 (let ((contype (mime-edit-get-contype tag))
2029 (encoding (mime-edit-get-encoding tag)))
2030 (insert (concat prefix "--" boundary "\n"))
2032 (narrow-to-region (point)(point))
2033 (insert "Content-Type: " contype "\n")
2035 (insert "Content-Transfer-Encoding: " encoding "\n"))
2036 (eword-encode-header)
2041 (downcase encoding))))
2044 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2046 (setq boundary (mime-edit-make-boundary))
2050 (narrow-to-region beg end)
2051 (let ((tag nil) ;MIME tag
2052 (contype nil) ;Content-Type
2053 (encoding nil) ;Content-Transfer-Encoding
2054 (nparts 0)) ;Number of body parts
2055 ;; Normalize the body part by inserting appropriate message
2056 ;; tags for every message contents.
2057 (mime-edit-normalize-body)
2058 ;; Counting the number of Content-Type.
2059 (goto-char (point-min))
2060 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2061 (setq nparts (1+ nparts)))
2062 ;; Begin translation.
2064 ((and (<= nparts 1)(not multipart))
2065 ;; It's a singular message.
2066 (goto-char (point-min))
2067 (while (re-search-forward
2068 mime-edit-single-part-tag-regexp nil t)
2070 (buffer-substring (match-beginning 0) (match-end 0)))
2071 (delete-region (match-beginning 0) (1+ (match-end 0)))
2072 (setq contype (mime-edit-get-contype tag))
2073 (setq encoding (mime-edit-get-encoding tag))
2076 ;; It's a multipart message.
2077 (goto-char (point-min))
2078 (let ((prio mime-content-transfer-encoding-priority-list)
2080 (when (setq part-info
2081 (mime-edit-translate-single-part-tag boundary))
2082 (and (setq nprio (member (cdr part-info) prio))
2084 (while (setq part-info
2085 (mime-edit-translate-single-part-tag boundary "\n"))
2086 (and (setq nprio (member (cdr part-info) prio))
2087 (setq prio nprio))))
2088 ;; Define Content-Type as "multipart/mixed".
2090 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2091 (setq encoding (car prio))
2092 ;; Insert the trailer.
2093 (goto-char (point-max))
2094 (insert "\n--" boundary "--\n")
2096 (list contype encoding boundary nparts)
2099 (defun mime-edit-normalize-body ()
2100 "Normalize the body part by inserting appropriate message tags."
2101 ;; Insert the first MIME tags if necessary.
2102 (goto-char (point-min))
2103 (if (not (looking-at mime-edit-single-part-tag-regexp))
2104 (insert (mime-make-text-tag) "\n"))
2105 ;; Check each tag, and add new tag or correct it if necessary.
2106 (goto-char (point-min))
2107 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2108 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2109 (contype (mime-edit-get-contype tag))
2110 (charset (mime-get-parameter contype "charset"))
2111 (encoding (mime-edit-get-encoding tag)))
2112 ;; Remove extra whitespaces after the tag.
2113 (if (looking-at "[ \t]+$")
2114 (delete-region (match-beginning 0) (match-end 0)))
2116 (end (mime-edit-content-end))
2118 (if (= end (point-max))
2121 (or (looking-at mime-edit-beginning-tag-regexp)
2123 (insert (mime-make-text-tag) "\n")
2125 (visible-region beg end)
2129 ((mime-test-content-type contype "message")
2130 ;; Content-type "message" should be sent as is.
2133 ((mime-test-content-type contype "text")
2134 ;; Define charset for text if necessary.
2135 (setq charset (if charset
2136 (intern (downcase charset))
2137 (mime-edit-choose-charset)))
2138 (mime-edit-define-charset charset)
2139 (cond ((string-equal contype "text/x-rot13-47-48")
2142 (mule-caesar-region (point) (mime-edit-content-end))
2144 ((string-equal contype "text/enriched")
2149 (end (mime-edit-content-end))
2151 ;; Patch for hard newlines
2154 ;; (while (search-forward "\n" end t)
2155 ;; (put-text-property (match-beginning 0)
2158 ;; End patch for hard newlines
2159 (enriched-encode beg end)
2161 (if (search-forward "\n\n")
2162 (delete-region beg (match-end 0))
2165 ;; Point is now on current tag.
2166 ;; Define encoding and encode text if necessary.
2167 (or encoding ;Encoding is not specified.
2170 (let ((ret (cdr (assq charset mime-charset-type-list))))
2172 (setq bits (car ret)
2175 conv "quoted-printable")))
2176 (if (<= bits mime-transfer-level)
2177 (mime-encoding-name bits)
2179 (beg (mime-edit-content-beginning)))
2180 (encode-mime-charset-region beg (mime-edit-content-end)
2182 ;; Protect "From " in beginning of line
2184 (narrow-to-region beg (mime-edit-content-end))
2186 (let (case-fold-search)
2187 (if (re-search-forward "^From " nil t)
2189 (if (memq charset '(iso-2022-jp
2194 (replace-match "\e(BFrom ")
2195 (re-search-forward "^From " nil t)
2197 (setq encoding "quoted-printable")
2199 ;; canonicalize line break code
2200 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2202 (narrow-to-region beg (mime-edit-content-end))
2204 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2205 (replace-match "\\1\r\n"))))
2207 (mime-encode-region beg (mime-edit-content-end)
2208 (or encoding "7bit"))
2209 (mime-edit-define-encoding encoding)
2211 (goto-char (mime-edit-content-end))
2213 ((null encoding) ;Encoding is not specified.
2214 ;; Application, image, audio, video, and any other
2215 ;; unknown content-type without encoding should be
2217 (let* ((encoding "base64") ;Encode in BASE64 by default.
2218 (beg (mime-edit-content-beginning))
2219 (end (mime-edit-content-end)))
2220 (mime-encode-region beg end encoding)
2221 (mime-edit-define-encoding encoding))
2226 (defun mime-delete-field (field)
2227 "Delete header FIELD."
2228 (let ((regexp (format "^%s:[ \t]*" field)))
2229 (goto-char (point-min))
2230 (while (re-search-forward regexp nil t)
2231 (delete-region (match-beginning 0)
2232 (1+ (std11-field-end))))))
2236 ;;; Platform dependent functions
2239 ;; Sun implementations
2241 (defun mime-edit-voice-recorder-for-sun (encoding)
2242 "Record voice in a buffer using Sun audio device,
2243 and insert data encoded as ENCODING."
2244 (message "Start the recording on %s. Type C-g to finish the recording..."
2246 (mime-insert-encoded-file "/dev/audio" encoding)
2250 ;;; @ Other useful commands.
2253 ;; Message forwarding commands as content-type "message/rfc822".
2255 (defun mime-edit-insert-message (&optional message)
2257 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2258 (if (and inserter (fboundp inserter))
2260 (mime-edit-insert-tag "message" "rfc822")
2261 (funcall inserter message)
2263 (message "Sorry, I don't have message inserter for your MUA.")
2266 (defun mime-edit-insert-mail (&optional message)
2268 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2269 (if (and inserter (fboundp inserter))
2271 (mime-edit-insert-tag "message" "rfc822")
2272 (funcall inserter message)
2274 (message "Sorry, I don't have mail inserter for your MUA.")
2277 (defun mime-edit-inserted-message-filter ()
2280 (let ((header-start (point))
2281 (case-fold-search t)
2284 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2285 (if (re-search-forward "^$" (mark t))
2286 (narrow-to-region header-start (match-beginning 0))
2288 (goto-char header-start)
2289 (while (and (re-search-forward
2290 mime-edit-yank-ignored-field-regexp nil t)
2291 (setq beg (match-beginning 0))
2292 (setq end (1+ (std11-field-end)))
2294 (delete-region beg end)
2299 ;;; @ multipart enclosure
2302 (defun mime-edit-enclose-region-internal (type beg end)
2306 (narrow-to-region beg end)
2307 (insert (format "--<<%s>>-{\n" type))
2308 (goto-char (point-max))
2309 (insert (format "--}-<<%s>>\n" type))
2310 (goto-char (point-max))
2312 (or (looking-at mime-edit-beginning-tag-regexp)
2314 (insert (mime-make-text-tag) "\n")
2318 (defun mime-edit-enclose-quote-region (beg end)
2320 (mime-edit-enclose-region-internal 'quote beg end)
2323 (defun mime-edit-enclose-mixed-region (beg end)
2325 (mime-edit-enclose-region-internal 'mixed beg end)
2328 (defun mime-edit-enclose-parallel-region (beg end)
2330 (mime-edit-enclose-region-internal 'parallel beg end)
2333 (defun mime-edit-enclose-digest-region (beg end)
2335 (mime-edit-enclose-region-internal 'digest beg end)
2338 (defun mime-edit-enclose-alternative-region (beg end)
2340 (mime-edit-enclose-region-internal 'alternative beg end)
2343 (defun mime-edit-enclose-pgp-signed-region (beg end)
2345 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2348 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2350 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2353 (defun mime-edit-enclose-kazu-signed-region (beg end)
2355 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2358 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2360 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2363 (defun mime-edit-insert-key (&optional arg)
2364 "Insert a pgp public key."
2366 (mime-edit-insert-tag "application" "pgp-keys")
2367 (mime-edit-define-encoding "7bit")
2375 (defun mime-edit-set-split (arg)
2378 (y-or-n-p "Do you want to enable split? ")
2380 (setq mime-edit-split-message arg)
2382 (message "This message is enabled to split.")
2383 (message "This message is not enabled to split.")
2386 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2387 "Toggle transfer-level is 7bit or 8bit through.
2389 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2391 (if (numberp transfer-level)
2392 (setq mime-transfer-level transfer-level)
2393 (if (< mime-transfer-level 8)
2394 (setq mime-transfer-level 8)
2395 (setq mime-transfer-level 7)
2397 (message (format "Current transfer-level is %d bit"
2398 mime-transfer-level))
2399 (setq mime-transfer-level-string
2400 (mime-encoding-name mime-transfer-level 'not-omit))
2401 (force-mode-line-update)
2404 (defun mime-edit-set-transfer-level-7bit ()
2406 (mime-edit-toggle-transfer-level 7)
2409 (defun mime-edit-set-transfer-level-8bit ()
2411 (mime-edit-toggle-transfer-level 8)
2418 (defvar mime-edit-pgp-processing nil)
2419 (make-variable-buffer-local 'mime-edit-pgp-processing)
2421 (defun mime-edit-set-sign (arg)
2424 (y-or-n-p "Do you want to sign? ")
2428 (or (memq 'sign mime-edit-pgp-processing)
2429 (setq mime-edit-pgp-processing
2430 (nconc mime-edit-pgp-processing
2431 (copy-sequence '(sign)))))
2432 (message "This message will be signed.")
2434 (setq mime-edit-pgp-processing
2435 (delq 'sign mime-edit-pgp-processing))
2436 (message "This message will not be signed.")
2439 (defun mime-edit-set-encrypt (arg)
2442 (y-or-n-p "Do you want to encrypt? ")
2446 (or (memq 'encrypt mime-edit-pgp-processing)
2447 (setq mime-edit-pgp-processing
2448 (nconc mime-edit-pgp-processing
2449 (copy-sequence '(encrypt)))))
2450 (message "This message will be encrypt.")
2452 (setq mime-edit-pgp-processing
2453 (delq 'encrypt mime-edit-pgp-processing))
2454 (message "This message will not be encrypt.")
2457 (defun mime-edit-pgp-enclose-buffer ()
2458 (let ((beg (save-excursion
2459 (goto-char (point-min))
2460 (if (search-forward (concat "\n" mail-header-separator "\n"))
2465 (dolist (pgp-processing mime-edit-pgp-processing)
2466 (case pgp-processing
2468 (mime-edit-enclose-pgp-signed-region
2472 (mime-edit-enclose-pgp-encrypted-region
2481 (defun mime-edit-insert-partial-header (fields subject
2482 id number total separator)
2484 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2485 (insert mime-edit-mime-version-field-for-message/partial)
2487 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2488 id number total separator))
2491 (defun mime-edit-split-and-send
2492 (&optional cmd lines mime-edit-message-max-length)
2496 (count-lines (point-min) (point-max)))
2498 (or mime-edit-message-max-length
2499 (setq mime-edit-message-max-length
2500 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2501 mime-edit-message-default-max-lines))
2503 (let* ((mime-edit-draft-file-name
2504 (or (buffer-file-name)
2506 (expand-file-name "mime-draft" temporary-file-directory))))
2507 (separator mail-header-separator)
2509 (replace-space-with-underline (current-time-string))
2510 "@" (system-name) "\"")))
2511 (run-hooks 'mime-edit-before-split-hook)
2512 (let ((the-buf (current-buffer))
2513 (copy-buf (get-buffer-create " *Original Message*"))
2514 (header (std11-header-string-except
2515 mime-edit-split-ignored-field-regexp separator))
2516 (subject (mail-fetch-field "subject"))
2517 (total (+ (/ lines mime-edit-message-max-length)
2518 (if (> (mod lines mime-edit-message-max-length) 0)
2524 mime-edit-split-message-sender-alist))
2528 (error "Split sender is not specified for `%s'." major-mode)
2531 (mime-edit-partial-number 1)
2534 (set-buffer copy-buf)
2536 (insert-buffer the-buf)
2538 (if (re-search-forward
2539 (concat "^" (regexp-quote separator) "$") nil t)
2540 (let ((he (match-beginning 0)))
2542 (narrow-to-region (point-min) he)
2544 (goto-char (point-min))
2545 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2546 (delete-region (match-beginning 0)
2547 (1+ (std11-field-end)))
2549 (while (< mime-edit-partial-number total)
2552 (set-buffer copy-buf)
2553 (setq data (buffer-substring
2556 (goto-line mime-edit-message-max-length)
2559 (delete-region (point-min)(point))
2561 (mime-edit-insert-partial-header
2562 header subject id mime-edit-partial-number total separator)
2565 (message (format "Sending %d/%d..."
2566 mime-edit-partial-number total))
2567 (call-interactively command)
2568 (message (format "Sending %d/%d... done"
2569 mime-edit-partial-number total))
2571 (setq mime-edit-partial-number
2572 (1+ mime-edit-partial-number))
2576 (set-buffer copy-buf)
2577 (setq data (buffer-string))
2580 (mime-edit-insert-partial-header
2581 header subject id mime-edit-partial-number total separator)
2584 (message (format "Sending %d/%d..."
2585 mime-edit-partial-number total))
2586 (message (format "Sending %d/%d... done"
2587 mime-edit-partial-number total))
2591 (defun mime-edit-maybe-split-and-send (&optional cmd)
2593 (run-hooks 'mime-edit-before-send-hook)
2594 (let ((mime-edit-message-max-length
2595 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2596 mime-edit-message-default-max-lines))
2597 (lines (count-lines (point-min) (point-max)))
2599 (if (and (> lines mime-edit-message-max-length)
2600 mime-edit-split-message)
2601 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2605 ;;; @ preview message
2608 (defvar mime-edit-buffer nil) ; buffer local variable
2610 (defun mime-edit-preview-message ()
2611 "preview editing MIME message."
2613 (let* ((str (buffer-string))
2614 (separator mail-header-separator)
2615 (the-buf (current-buffer))
2616 (buf-name (buffer-name))
2617 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2618 (buf (get-buffer temp-buf-name))
2619 (pgp-processing mime-edit-pgp-processing)
2623 (switch-to-buffer buf)
2626 (setq buf (get-buffer-create temp-buf-name))
2627 (switch-to-buffer buf)
2630 (setq major-mode 'mime-temp-message-mode)
2631 (make-local-variable 'mail-header-separator)
2632 (setq mail-header-separator separator)
2633 (make-local-variable 'mime-edit-buffer)
2634 (setq mime-edit-buffer the-buf)
2635 (setq mime-edit-pgp-processing pgp-processing)
2637 (run-hooks 'mime-edit-translate-hook)
2638 (mime-edit-translate-buffer)
2639 (goto-char (point-min))
2640 (if (re-search-forward
2641 (concat "^" (regexp-quote separator) "$"))
2647 (defun mime-edit-quitting-method ()
2648 "Quitting method for mime-view."
2649 (let* ((entity (get-text-property (point-min) 'mime-view-entity))
2650 (temp (mime-entity-buffer entity))
2652 (mime-preview-kill-buffer)
2654 (setq buf mime-edit-buffer)
2656 (switch-to-buffer buf)
2659 (set-alist 'mime-preview-quitting-method-alist
2660 'mime-temp-message-mode
2661 #'mime-edit-quitting-method)
2667 (defvar mime-edit-again-ignored-field-regexp
2668 (concat "^\\(" "Content-.*\\|Mime-Version"
2669 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2671 "Regexp for deleted header fields when `mime-edit-again' is called.")
2673 (defsubst eliminate-top-spaces (string)
2674 "Eliminate top sequence of space or tab in STRING."
2675 (if (string-match "^[ \t]+" string)
2676 (substring string (match-end 0))
2679 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2682 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2683 '(("application/pgp-encrypted" . pgp-encrypted)
2684 ("application/pgp-signature" . pgp-signed))))
2685 (mime-content-type-subtype content-type)))
2686 (boundary (mime-content-type-parameter content-type "boundary"))
2687 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2688 (re-search-forward boundary-pat nil t)
2689 (let ((bb (match-beginning 0)) eb tag)
2690 (setq tag (format "\n--<<%s>>-{\n" subtype))
2693 (setq bb (+ bb (length tag)))
2695 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2697 (setq eb (match-beginning 0))
2698 (replace-match (format "--}-<<%s>>\n" subtype))
2700 (narrow-to-region bb eb)
2701 (goto-char (point-min))
2702 (while (re-search-forward boundary-pat nil t)
2703 (let ((beg (match-beginning 0))
2705 (delete-region beg (match-end 0))
2707 (if (re-search-forward boundary-pat nil t)
2708 (setq end (match-beginning 0))
2709 (setq end (point-max))
2712 (narrow-to-region beg end)
2714 ((eq subtype 'pgp-encrypted)
2717 (goto-char (point-min))
2718 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2721 (save-window-excursion
2722 (pgg-decrypt-region (match-beginning 0)
2724 (delete-region (point-min)(point-max))))
2725 (insert-buffer-substring pgg-output-buffer)
2726 (mime-edit-decode-message-in-buffer
2727 nil not-decode-text)
2728 (delete-region (goto-char (point-min))
2729 (if (search-forward "\n\n" nil t)
2732 (goto-char (point-max))
2735 (mime-edit-decode-message-in-buffer
2736 (if (eq subtype 'digest)
2738 (make-mime-content-type 'message 'rfc822))
2741 (goto-char (point-max))
2745 (goto-char (point-min))
2746 (or (= (point-min) 1)
2747 (delete-region (point-min)
2748 (if (search-forward "\n\n" nil t)
2754 (defun mime-edit-decode-single-part-in-buffer
2755 (content-type not-decode-text &optional content-disposition)
2756 (let* ((type (mime-content-type-primary-type content-type))
2757 (subtype (mime-content-type-subtype content-type))
2758 (ctype (format "%s/%s" type subtype))
2760 (pstr (let ((bytes (+ 14 (length ctype))))
2761 (mapconcat (function
2763 (if (string= (car attr) "charset")
2765 (setq charset (cdr attr))
2767 (let* ((str (concat (car attr)
2770 (setq bytes (+ bytes bs 2))
2773 (setq bytes (+ bs 1))
2777 (mime-content-type-parameters content-type) "")))
2780 (limit (save-excursion
2781 (if (search-forward "\n\n" nil t)
2784 (mime-content-disposition-type content-disposition))
2786 (if disposition-type
2787 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2788 (mapconcat (function
2793 (if (string-equal "filename"
2795 (std11-wrap-as-quoted-string
2799 (setq bytes (+ bytes bs 2))
2802 (setq bytes (+ bs 1))
2806 (mime-content-disposition-parameters
2807 content-disposition)
2810 (if disposition-type
2811 (setq pstr (format "%s\nContent-Disposition: %s%s"
2812 pstr disposition-type disposition-str))
2815 (if (re-search-forward
2816 "^Content-Transfer-Encoding:" limit t)
2817 (let ((beg (match-beginning 0))
2818 (hbeg (match-end 0))
2819 (end (std11-field-end limit)))
2822 (eliminate-top-spaces
2823 (std11-unfold-string
2824 (buffer-substring hbeg end)))))
2825 (if (or charset (eq type 'text))
2827 (delete-region beg (1+ end))
2828 (goto-char (point-min))
2829 (if (search-forward "\n\n" nil t)
2832 (match-end 0)(point-max) encoding)
2836 (if (or encoded (not not-decode-text))
2839 (goto-char (point-min))
2840 (while (re-search-forward "\r\n" nil t)
2841 (replace-match "\n")
2843 (decode-mime-charset-region (point-min)(point-max)
2844 (or charset default-mime-charset))
2846 (let ((he (if (re-search-forward "^$" nil t)
2850 (if (and (eq type 'text)
2851 (eq subtype 'x-rot13-47-48))
2852 (mule-caesar-region he (point-max))
2854 (if (= (point-min) 1)
2860 (format "%s/%s%s" type subtype pstr)
2863 (delete-region (point-min) he)
2865 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2871 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2874 (goto-char (point-min))
2875 (let ((ctl (or (mime-read-Content-Type)
2876 default-content-type)))
2878 (let ((type (mime-content-type-primary-type ctl)))
2880 ((and (eq type 'application)
2881 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2882 (delete-region (point-min)(point-max))
2884 ((eq type 'multipart)
2885 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
2888 (mime-edit-decode-single-part-in-buffer
2889 ctl not-decode-text (mime-read-Content-Disposition))
2892 (decode-mime-charset-region (point-min) (point-max)
2893 default-mime-charset))
2895 (if (= (point-min) 1)
2898 (std11-narrow-to-header)
2899 (goto-char (point-min))
2900 (while (re-search-forward
2901 mime-edit-again-ignored-field-regexp nil t)
2902 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2904 (mime-decode-header-in-buffer (not not-decode-text))
2906 (mime-decode-header-in-buffer (not not-decode-text))
2910 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2911 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2912 Content-Type and Content-Transfer-Encoding header fields will be
2913 converted to MIME-Edit tags."
2915 (goto-char (point-min))
2917 (concat "\n" (regexp-quote mail-header-separator) "\n")
2919 (replace-match "\n\n")
2921 (mime-edit-decode-message-in-buffer nil not-decode-text)
2922 (goto-char (point-min))
2924 (and (re-search-forward "^$")
2925 (replace-match mail-header-separator)
2935 (provide 'mime-edit)
2937 (run-hooks 'mime-edit-load-hook)
2939 ;;; mime-edit.el ends here