1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,94,95,96,97,98,99,2000,01,02,03
4 ;; Free Software Foundation, Inc.
6 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
7 ;; MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
8 ;; Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
9 ;; Created: 1994/08/21 renamed from mime.el
10 ;; Renamed: 1997/2/21 from tm-edit.el
11 ;; Keywords: MIME, multimedia, multilingual, mail, news
13 ;; This file is part of SEMI (Sophisticated 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., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, 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...
112 (require 'mail-utils)
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)
127 (autoload 'smime-encrypt-region "smime"
128 "S/MIME encryption of current region.")
129 (autoload 'smime-sign-region "smime"
130 "S/MIME signature of current region.")
131 (defvar smime-output-buffer)
132 (defvar smime-errors-buffer)
139 (defconst mime-edit-version
141 (mime-product-name mime-user-interface-product) " "
142 (mapconcat #'number-to-string
143 (mime-product-version mime-user-interface-product) ".")
144 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
150 (defgroup mime-edit nil
154 (defcustom mime-ignore-preceding-spaces nil
155 "*Ignore preceding white spaces if non-nil."
159 (defcustom mime-ignore-trailing-spaces nil
160 "*Ignore trailing white spaces if non-nil."
164 (defcustom mime-ignore-same-text-tag t
165 "*Ignore preceding text content-type tag that is same with new one.
166 If non-nil, the text tag is not inserted unless something different."
170 (defcustom mime-auto-hide-body t
171 "*Hide non-textual body encoded in base64 after insertion if non-nil."
175 (defcustom mime-edit-voice-recorder
176 (function mime-edit-voice-recorder-for-sun)
177 "*Function to record a voice message and encode it."
181 (defcustom mime-edit-mode-hook nil
182 "*Hook called when enter MIME mode."
186 (defcustom mime-edit-translate-hook nil
187 "*Hook called before translating into a MIME compliant message.
188 To insert a signature file automatically, call the function
189 `mime-edit-insert-signature' from this hook."
193 (defcustom mime-edit-exit-hook nil
194 "*Hook called when exit MIME mode."
198 (defvar mime-content-types
200 ;; Charset parameter need not to be specified, since it is
201 ;; defined automatically while translation.
203 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
217 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
218 ("directory" "/pub/GNU/elisp/mime")
220 ("mode" "image" "ascii" "local8"))
225 ("mode" "image" "ascii" "local8"))
226 ("tftp" ("site") ("name"))
227 ("afs" ("site") ("name"))
228 ("local-file" ("site") ("name"))
230 ("server" "ftpmail@nic.karrn.ad.jp")
240 ("octet-stream" ("type" "" "tar" "shar"))
245 ("x-shockwave-flash")
249 ("vnd.oasis.opendocument.text")
250 ("vnd.oasis.opendocument.spreadsheet")
251 ("vnd.oasis.opendocument.graphics")
252 ("vnd.oasis.opendocument.chart")
253 ("vnd.oasis.opendocument.formula")
254 ("vnd.oasis.opendocument.text-master")
255 ("vnd.oasis.opendocument.presentation")
256 ("vnd.oasis.opendocument.text-template")
257 ("vnd.oasis.opendocument.spreadsheet-template")
258 ("vnd.oasis.opendocument.presentation-template")
259 ("vnd.oasis.opendocument.graphics-template")
263 ("vnd.ms-powerpoint")
264 ; Microsoft Office (OpenXML)
265 ("vnd.ms-excel.addin.macroEnabled.12")
266 ("vnd.ms-excel.sheet.binary.macroEnabled.12")
267 ("vnd.ms-excel.sheet.macroEnabled.12")
268 ("vnd.ms-excel.template.macroEnabled.12")
269 ("vnd.ms-powerpoint.addin.macroEnabled.12")
270 ("vnd.ms-powerpoint.presentation.macroEnabled.12")
271 ("vnd.ms-powerpoint.slideshow.macroEnabled.12")
272 ("vnd.ms-powerpoint.template.macroEnabled.12")
273 ("vnd.ms-word.document.macroEnabled.12")
274 ("vnd.ms-word.template.macroEnabled.12")
275 ("vnd.openxmlformats-officedocument.presentationml.presentation")
276 ("vnd.openxmlformats-officedocument.presentationml.slideshow")
277 ("vnd.openxmlformats-officedocument.presentationml.template")
278 ("vnd.openxmlformats-officedocument.spreadsheetml.sheet")
279 ("vnd.openxmlformats-officedocument.spreadsheetml.template")
280 ("vnd.openxmlformats-officedocument.wordprocessingml.document")
281 ("vnd.openxmlformats-officedocument.wordprocessingml.template")
282 ("vnd.ms-xpsdocument")
285 ("x-kiss" ("x-cnf")))
308 "*Alist of content-type, subtype, parameters and its values.")
310 (defcustom mime-file-types
313 ;; Programming languages
316 "application" "octet-stream" (("type" . "C++"))
318 "attachment" (("filename" . file))
322 "application" "octet-stream" (("type" . "emacs-lisp"))
324 "attachment" (("filename" . file))
328 "application" "octet-stream" (("type" . "common-lisp"))
330 "attachment" (("filename" . file))
334 "application" "octet-stream" (("type" . "perl"))
336 "attachment" (("filename" . file))
339 ;; Text or translated text
344 "inline" (("filename" . file))
350 "inline" (("filename" . file))
356 "inline" (("filename" . file))
359 ("\\.tex$\\|\\.latex$"
362 "inline" (("filename" . file))
365 ;; .rc : procmail modules pm-xxxx.rc
366 ;; *rc : other resource files
368 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
371 "attachment" (("filename" . file))
379 ("\\.diff$\\|\\.patch$"
380 "application" "octet-stream" (("type" . "patch"))
382 "attachment" (("filename" . file))
386 "text" "plain" nil nil nil nil)
390 "application" "javascript" nil
392 "inline" (("filename" . file))
398 "application" "vnd.ms-project" nil
400 "attachment" (("filename" . file))
404 ;; Microsoft Office (none-OpenXML)
406 ("\\.rtf$" ; Rich text format
407 "application" "rtf" nil
409 "attachment" (("filename" . file))
412 "application" "msword" nil
414 "attachment" (("filename" . file))
416 ("\\.xls$" ; MS Excel
417 "application" "vnd.ms-excel" nil
419 "attachment" (("filename" . file))
421 ("\\.ppt$" ; MS Power Point
422 "application" "vnd.ms-powerpoint" nil
424 "attachment" (("filename" . file))
428 ;; Microsoft Office (OpenXML)
432 "application" "vnd.ms-word.document.macroEnabled.12" nil
434 "attachment" (("filename" . file))
437 "application" "vnd.openxmlformats-officedocument.wordprocessingml.document" nil
439 "attachment" (("filename" . file))
442 "application" "vnd.ms-word.template.macroEnabled.12" nil
444 "attachment" (("filename" . file))
447 "application" "vnd.openxmlformats-officedocument.wordprocessingml.template" nil
449 "attachment" (("filename" . file))
454 "application" "vnd.ms-powerpoint.template.macroEnabled.12" nil
456 "attachment" (("filename" . file))
459 "application" "vnd.openxmlformats-officedocument.presentationml.template" nil
461 "attachment" (("filename" . file))
464 "application" "vnd.ms-powerpoint.addin.macroEnabled.12" nil
466 "attachment" (("filename" . file))
469 "application" "vnd.ms-powerpoint.slideshow.macroEnabled.12" nil
471 "attachment" (("filename" . file))
474 "application" "vnd.openxmlformats-officedocument.presentationml.slideshow" nil
476 "attachment" (("filename" . file))
479 "application" "vnd.ms-powerpoint.presentation.macroEnabled.12" nil
481 "attachment" (("filename" . file))
484 "application" "vnd.openxmlformats-officedocument.presentationml.presentation" nil
486 "attachment" (("filename" . file))
491 "application" "vnd.ms-excel.addin.macroEnabled.12" nil
493 "attachment" (("filename" . file))
496 "application" "vnd.ms-excel.sheet.binary.macroEnabled.12" nil
498 "attachment" (("filename" . file))
501 "application" "vnd.ms-excel.sheet.macroEnabled.12" nil
503 "attachment" (("filename" . file))
506 "application" "vnd.openxmlformats-officedocument.spreadsheetml.sheet" nil
508 "attachment" (("filename" . file))
511 "application" "vnd.ms-excel.template.macroEnabled.12" nil
513 "attachment" (("filename" . file))
516 "application" "vnd.openxmlformats-officedocument.spreadsheetml.template" nil
518 "attachment" (("filename" . file))
524 "application" "vnd.oasis.opendocument.text" nil
526 "attachment" (("filename" . file))
529 "application" "vnd.oasis.opendocument.spreadsheet" nil
531 "attachment" (("filename" . file))
534 "application" "vnd.oasis.opendocument.graphics" nil
536 "attachment" (("filename" . file))
539 "application" "vnd.oasis.opendocument.formula" nil
541 "attachment" (("filename" . file))
544 "application" "vnd.oasis.opendocument.text-master" nil
546 "attachment" (("filename" . file))
549 "application" "vnd.oasis.opendocument.presentation" nil
551 "attachment" (("filename" . file))
554 "application" "vnd.oasis.opendocument.text-template" nil
556 "attachment" (("filename" . file))
559 "application" "vnd.oasis.opendocument.spreadsheet-template" nil
561 "attachment" (("filename" . file))
564 "application" "vnd.oasis.opendocument.presentation-template" nil
566 "attachment" (("filename" . file))
569 "application" "vnd.oasis.opendocument.graphics-template" nil
571 "attachment" (("filename" . file))
574 ;; Postscript and PDF
576 "application" "postscript" nil
578 "attachment" (("filename" . file))
581 "application" "pdf" nil
583 "attachment" (("filename" . file))
588 ("\\.jpg$\\|\\.jpeg$"
591 "inline" (("filename" . file))
596 "inline" (("filename" . file))
601 "inline" (("filename" . file))
606 "inline" (("filename" . file))
609 "image" "svg+xml" nil
611 "inline" (("filename" . file))
616 "inline" (("filename" . file))
621 "inline" (("filename" . file))
626 "inline" (("filename" . file))
631 "inline" (("filename" . file))
636 "inline" (("filename" . file))
644 "attachment" (("filename" . file))
646 ("\\.mp[234]\\|\\.m4[abp]$"
649 "attachment" (("filename" . file))
654 "attachment" (("filename" . file))
659 "attachment" (("filename" . file))
664 "attachment" (("filename" . file))
669 "attachment" (("filename" . file))
672 "video" "quicktime" nil
674 "attachment" (("filename" . file))
679 "attachment" (("filename" . file))
682 "application" "x-shockwave-flash" nil
684 "attachment" (("filename" . file))
691 "application" "octet-stream" (("type" . "tar+gzip"))
693 "attachment" (("filename" . file))
696 "application" "octet-stream" (("type" . "tar+gzip"))
698 "attachment" (("filename" . file))
701 "application" "octet-stream" (("type" . "tar+compress"))
703 "attachment" (("filename" . file))
706 "application" "octet-stream" (("type" . "tar+compress"))
708 "attachment" (("filename" . file))
711 "application" "octet-stream" (("type" . "gzip"))
713 "attachment" (("filename" . file))
716 "application" "octet-stream" (("type" . "compress"))
718 "attachment" (("filename" . file))
721 "application" "octet-stream" (("type" . "lha"))
723 "attachment" (("filename" . file))
726 "application" "zip" nil
728 "attachment" (("filename" . file))
731 "application" "x-7z-compressed" nil
733 "attachment" (("filename" . file))
739 "application" "octet-stream" nil
741 "attachment" (("filename" . file)))
743 "*Alist of file name, types, parameters, and default encoding.
744 If encoding is nil, it is determined from its contents."
748 (choice :tag "Primary-Type"
749 ,@(nconc (mapcar (lambda (cell)
750 (list 'item (car cell))
755 (choice :tag "Sub-Type"
758 (mapcar (lambda (cell)
759 (mapcar (lambda (cell)
760 (list 'item (car cell))
766 (repeat :tag "Parameters of Content-Type field"
767 (cons string (choice string symbol)))
768 ;; content-transfer-encoding
769 (choice :tag "Encoding"
772 (mapcar (lambda (cell)
775 (mime-encoding-list))))
777 (choice :tag "Disposition-Type"
783 (repeat :tag "Parameters of Content-Disposition field"
784 (cons string (choice string symbol)))
789 ;;; @@ about charset, encoding and transfer-level
792 (defvar mime-charset-type-list
794 (iso-8859-1 8 "quoted-printable")
795 (iso-8859-2 8 "quoted-printable")
796 (iso-8859-3 8 "quoted-printable")
797 (iso-8859-4 8 "quoted-printable")
798 (iso-8859-5 8 "quoted-printable")
799 (koi8-r 8 "quoted-printable")
800 (iso-8859-7 8 "quoted-printable")
801 (iso-8859-8 8 "quoted-printable")
802 (iso-8859-9 8 "quoted-printable")
803 (iso-8859-14 8 "quoted-printable")
804 (iso-8859-15 8 "quoted-printable")
805 (iso-2022-jp 7 "base64")
806 (iso-2022-jp-3 7 "base64")
807 (iso-2022-kr 7 "base64")
813 (shift_jis 8 "base64")
815 (iso-2022-jp-2 7 "base64")
816 (iso-2022-int-1 7 "base64")
819 (defvar mime-transfer-level 7
820 "*A number of network transfer level. It should be bigger than 7.")
821 (make-variable-buffer-local 'mime-transfer-level)
823 (defsubst mime-encoding-name (transfer-level &optional not-omit)
824 (cond ((> transfer-level 8) "binary")
825 ((= transfer-level 8) "8bit")
829 (defvar mime-transfer-level-string
830 (mime-encoding-name mime-transfer-level 'not-omit)
831 "A string formatted version of mime-transfer-level")
832 (make-variable-buffer-local 'mime-transfer-level-string)
834 ;;; @@ about content transfer encoding
836 (defvar mime-content-transfer-encoding-priority-list
837 '(nil "8bit" "binary"))
839 ;;; @@ about message inserting
842 (defvar mime-edit-yank-ignored-field-list
843 '("Received" "Approved" "Path" "Replied" "Status"
844 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
845 "Delete these fields from original message when it is inserted
846 as message/rfc822 part.
847 Each elements are regexp of field-name.")
849 (defvar mime-edit-yank-ignored-field-regexp
851 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
854 (defvar mime-edit-message-inserter-alist nil)
855 (defvar mime-edit-mail-inserter-alist nil)
858 ;;; @@ about message splitting
861 (defcustom mime-edit-split-message t
862 "*Split large message if it is non-nil."
866 (defcustom mime-edit-message-default-max-lines 1000
867 "*Default maximum lines of a message."
871 (defcustom mime-edit-message-max-lines-alist
872 '((news-reply-mode . 500))
873 "Alist of major-mode vs maximum lines of a message.
874 If it is not specified for a major-mode,
875 `mime-edit-message-default-max-lines' is used."
879 (defconst mime-edit-split-ignored-field-regexp
880 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
882 (defcustom mime-edit-split-blind-field-regexp
883 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
884 "*Regular expression to match field-name to be ignored when split sending."
888 (defvar mime-edit-split-message-sender-alist nil)
890 (defvar mime-edit-news-reply-mode-server-running nil)
896 (defconst mime-edit-single-part-tag-regexp
897 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
898 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
900 (defconst mime-edit-quoted-single-part-tag-regexp
901 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
903 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
905 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
907 (defconst mime-edit-beginning-tag-regexp
908 (regexp-or mime-edit-single-part-tag-regexp
909 mime-edit-multipart-beginning-regexp))
911 (defconst mime-edit-end-tag-regexp
912 (regexp-or mime-edit-single-part-tag-regexp
913 mime-edit-multipart-end-regexp))
915 (defconst mime-edit-tag-regexp
916 (regexp-or mime-edit-single-part-tag-regexp
917 mime-edit-multipart-beginning-regexp
918 mime-edit-multipart-end-regexp))
920 (defvar mime-tag-format "--[[%s]]"
921 "*Control-string making a MIME tag.")
923 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
924 "*Control-string making a MIME tag with encoding.")
927 ;;; @@ multipart boundary
930 (defvar mime-multipart-boundary "Multipart"
931 "*Boundary of a multipart message.")
934 ;;; @@ optional header fields
937 (defvar mime-edit-insert-user-agent-field t
938 "*If non-nil, insert User-Agent header field.")
940 (defvar mime-edit-user-agent-value
941 (concat (mime-product-name mime-user-interface-product)
943 (mapconcat #'number-to-string
944 (mime-product-version mime-user-interface-product) ".")
946 (mime-product-code-name mime-user-interface-product)
948 (mime-product-name mime-library-product)
950 (mapconcat #'number-to-string
951 (mime-product-version mime-library-product) ".")
953 (mime-product-code-name mime-library-product)
955 (if (fboundp 'apel-version)
956 (concat (apel-version) " "))
957 (if (featurep 'xemacs)
958 (concat (cond ((and (featurep 'chise)
959 (boundp 'xemacs-chise-version))
960 (concat "CHISE-MULE/" xemacs-chise-version))
961 ((featurep 'utf-2000)
962 (concat "UTF-2000-MULE/" utf-2000-version))
963 ((featurep 'mule) "MULE"))
965 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
968 (substring emacs-version 0 (match-end 0))
969 (cond ((and (boundp 'xemacs-betaname)
971 ;; It does not exist in XEmacs
972 ;; versions prior to 20.3.
973 (concat " " xemacs-betaname))
974 ((and (boundp 'emacs-patch-level)
976 ;; It does not exist in FSF Emacs or in
977 ;; XEmacs versions earlier than 21.1.1.
978 (format " (patch %d)" emacs-patch-level))
980 " (" xemacs-codename ")"
981 ;; `xemacs-extra-name' has appeared in the
982 ;; development version of XEmacs 21.5-b8.
983 (if (and (boundp 'xemacs-extra-name)
984 (symbol-value 'xemacs-extra-name))
985 (concat " " (symbol-value 'xemacs-extra-name))
988 system-configuration ")")
989 " (" emacs-version ")"))
990 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
991 (substring emacs-version 0 (match-beginning 0))
994 (if (boundp 'enable-multibyte-characters)
996 " (" system-configuration ")"
997 (if enable-multibyte-characters
998 (concat " MULE/" mule-version)
999 " (with unibyte mode)")
1000 (if (featurep 'meadow)
1001 (let ((mver (Meadow-version)))
1002 (if (string-match "^Meadow-" mver)
1007 (concat "MULE/" mule-version
1008 " (based on Emacs " ver ")"))
1009 (concat "Emacs/" ver " (" system-configuration ")")))))
1010 "Body of User-Agent field.
1011 If variable `mime-edit-insert-user-agent-field' is not nil, it is
1012 inserted into message header.")
1018 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
1019 "*Specify MIME tspecials.
1020 Tspecials means any character that matches with it in header must be quoted.")
1022 (defconst mime-edit-mime-version-value
1023 (concat "1.0 (generated by " mime-edit-version ")")
1024 "MIME version number.")
1026 (defconst mime-edit-mime-version-field-for-message/partial
1027 (concat "MIME-Version:"
1028 (mime-encode-field-body
1029 (concat " 1.0 (split by " mime-edit-version ")\n")
1031 "MIME version field for message/partial.")
1034 ;;; @ keymap and menu
1037 (defvar mime-edit-mode-flag nil)
1038 (make-variable-buffer-local 'mime-edit-mode-flag)
1040 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
1041 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
1042 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
1043 "Keymap for MIME-Edit mode commands to insert entity or set status.")
1045 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
1046 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
1047 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
1048 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
1049 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
1050 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
1051 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
1052 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
1053 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
1054 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
1056 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
1057 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
1058 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
1059 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
1060 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
1061 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
1062 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
1063 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
1064 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
1065 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
1067 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
1068 "Keymap prefix for MIME-Edit mode commands about enclosure.")
1069 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
1070 "Keymap for MIME-Edit mode commands about enclosure.")
1072 (define-key mime-edit-mode-enclosure-map
1073 "\C-a" 'mime-edit-enclose-alternative-region)
1074 (define-key mime-edit-mode-enclosure-map
1075 "\C-p" 'mime-edit-enclose-parallel-region)
1076 (define-key mime-edit-mode-enclosure-map
1077 "\C-m" 'mime-edit-enclose-mixed-region)
1078 (define-key mime-edit-mode-enclosure-map
1079 "\C-d" 'mime-edit-enclose-digest-region)
1080 (define-key mime-edit-mode-enclosure-map
1081 "\C-s" 'mime-edit-enclose-pgp-signed-region)
1082 (define-key mime-edit-mode-enclosure-map
1083 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
1084 (define-key mime-edit-mode-enclosure-map
1085 "\C-q" 'mime-edit-enclose-quote-region)
1087 (defvar mime-edit-mode-map (make-sparse-keymap)
1088 "Keymap for MIME-Edit mode commands.")
1089 (define-key mime-edit-mode-map
1090 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
1091 (define-key mime-edit-mode-map
1092 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
1094 (defconst mime-edit-menu-title "MIME-Edit")
1096 (defconst mime-edit-menu-list
1097 '((mime-help "Describe MIME editor mode" mime-edit-help)
1098 (file "Insert File" mime-edit-insert-file)
1099 (external "Insert External" mime-edit-insert-external)
1100 (voice "Insert Voice" mime-edit-insert-voice)
1101 (message "Insert Message" mime-edit-insert-message)
1102 (mail "Insert Mail" mime-edit-insert-mail)
1103 (signature "Insert Signature" mime-edit-insert-signature)
1104 (text "Insert Text" mime-edit-insert-text)
1105 (tag "Insert Tag" mime-edit-insert-tag)
1106 (alternative "Enclose as alternative"
1107 mime-edit-enclose-alternative-region)
1108 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
1109 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
1110 (digest "Enclose as digest" mime-edit-enclose-digest-region)
1111 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
1112 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
1113 (quote "Verbatim region" mime-edit-enclose-quote-region)
1114 (key "Insert Public Key" mime-edit-insert-key)
1115 (split "Set splitting" mime-edit-set-split)
1116 (sign "PGP sign" mime-edit-set-sign)
1117 (encrypt "PGP encrypt" mime-edit-set-encrypt)
1118 (preview "Preview Message" mime-edit-preview-message)
1119 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
1121 "MIME-edit menubar entry.")
1123 (cond ((featurep 'xemacs)
1124 ;; modified by Pekka Marjola <pema@iki.fi>
1125 ;; 1995/9/5 (c.f. [tm-en:69])
1126 (defun mime-edit-define-menu-for-xemacs ()
1127 "Define menu for XEmacs."
1128 (cond ((featurep 'menubar)
1129 (make-local-variable 'current-menubar)
1130 (set-buffer-menubar current-menubar)
1133 (cons mime-edit-menu-title
1136 (vector (nth 1 item)(nth 2 item)
1137 mime-edit-mode-flag)
1139 mime-edit-menu-list)))
1142 ;; modified by Steven L. Baur <steve@miranova.com>
1143 ;; 1995/12/6 (c.f. [tm-en:209])
1144 (or (boundp 'mime-edit-popup-menu-for-xemacs)
1145 (setq mime-edit-popup-menu-for-xemacs
1146 (append '("MIME Commands" "---")
1147 (mapcar (function (lambda (item)
1148 (vector (nth 1 item)
1151 mime-edit-menu-list)))
1154 ((>= emacs-major-version 19)
1155 (define-key mime-edit-mode-map [menu-bar mime-edit]
1156 (cons mime-edit-menu-title
1157 (make-sparse-keymap mime-edit-menu-title)))
1160 (define-key mime-edit-mode-map
1161 (vector 'menu-bar 'mime-edit (car item))
1162 (cons (nth 1 item)(nth 2 item))
1165 (reverse mime-edit-menu-list)
1173 (defvar mime-edit-touched-flag nil)
1176 (defun mime-edit-mode ()
1177 "MIME minor mode for editing the tagged MIME message.
1179 In this mode, basically, the message is composed in the tagged MIME
1180 format. The message tag looks like:
1182 --[[text/plain; charset=ISO-2022-JP][7bit]]
1184 The tag specifies the MIME content type, subtype, optional parameters
1185 and transfer encoding of the message following the tag. Messages
1186 without any tag are treated as `text/plain' by default. Charset and
1187 transfer encoding are automatically defined unless explicitly
1188 specified. Binary messages such as audio and image are usually
1189 hidden. The messages in the tagged MIME format are automatically
1190 translated into a MIME compliant message when exiting this mode.
1192 Available charsets depend on Emacs version being used. The following
1193 lists the available charsets of each emacs.
1195 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
1196 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
1197 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
1198 CN-BIG5 and ISO-2022-INT-1 are available.
1200 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
1201 be used to represent multilingual text in intermixed manner. Any
1202 languages that has no registered charset are represented as either
1203 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
1205 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
1206 without mule, please set variable `default-mime-charset'. This
1207 variable must be symbol of which name is a MIME charset.
1209 If you want to add more charsets in mule, please set variable
1210 `charsets-mime-charset-alist'. This variable must be alist of which
1211 key is list of charset and value is symbol of MIME charset. If name
1212 of coding-system is different as MIME charset, please set variable
1213 `mime-charset-coding-system-alist'. This variable must be alist of
1214 which key is MIME charset and value is coding-system.
1216 Following commands are available in addition to major mode commands:
1218 \[make single part\]
1219 \\[mime-edit-insert-text] insert a text message.
1220 \\[mime-edit-insert-file] insert a (binary) file.
1221 \\[mime-edit-insert-external] insert a reference to external body.
1222 \\[mime-edit-insert-voice] insert a voice message.
1223 \\[mime-edit-insert-message] insert a mail or news message.
1224 \\[mime-edit-insert-mail] insert a mail message.
1225 \\[mime-edit-insert-signature] insert a signature file at end.
1226 \\[mime-edit-insert-key] insert PGP public key.
1227 \\[mime-edit-insert-tag] insert a new MIME tag.
1229 \[make enclosure (maybe multipart)\]
1230 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
1231 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
1232 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
1233 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
1234 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
1235 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
1236 \\[mime-edit-enclose-quote-region] enclose as verbose mode
1237 (to avoid to expand tags)
1240 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
1241 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
1242 \\[mime-edit-set-split] set message splitting mode.
1243 \\[mime-edit-set-sign] set PGP-sign mode.
1244 \\[mime-edit-set-encrypt] set PGP-encryption mode.
1245 \\[mime-edit-preview-message] preview editing MIME message.
1246 \\[mime-edit-exit] exit and translate into a MIME
1248 \\[mime-edit-help] show this help.
1249 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
1252 Additional commands are available in some major modes:
1253 C-c C-c exit, translate and run the original command.
1254 C-c C-s exit, translate and run the original command.
1256 The following is a message example written in the tagged MIME format.
1257 TABs at the beginning of the line are not a part of the message:
1259 This is a conventional plain text. It should be translated
1262 This is also a plain text. But, it is explicitly specified as
1264 --[[text/plain; charset=ISO-8859-1]]
1265 This is also a plain text. But charset is specified as
1268 ¡Hola! Buenos días. ¿Cómo está usted?
1270 This is a <bold>enriched text</bold>.
1271 --[[image/gif][base64]]...image encoded in base64 here...
1272 --[[audio/basic][base64]]...audio encoded in base64 here...
1274 User customizable variables (not documented all of them):
1276 Specifies a key prefix for MIME minor mode commands.
1278 mime-ignore-preceding-spaces
1279 Preceding white spaces in a message body are ignored if non-nil.
1281 mime-ignore-trailing-spaces
1282 Trailing white spaces in a message body are ignored if non-nil.
1285 Hide a non-textual body message encoded in base64 after insertion
1289 A number of network transfer level. It should be bigger than 7.
1290 If you are in 8bit-through environment, please set 8.
1292 mime-edit-voice-recorder
1293 Specifies a function to record a voice message and encode it.
1294 The function `mime-edit-voice-recorder-for-sun' is for Sun
1298 Turning on MIME mode calls the value of mime-edit-mode-hook, if
1301 mime-edit-translate-hook
1302 The value of mime-edit-translate-hook is called just before translating
1303 the tagged MIME format into a MIME compliant message if it is
1304 non-nil. If the hook call the function mime-edit-insert-signature,
1305 the signature file will be inserted automatically.
1308 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
1311 (if mime-edit-mode-flag
1313 (if mime-edit-touched-flag
1315 (make-local-variable 'mime-edit-touched-flag)
1316 (setq mime-edit-touched-flag t)
1321 (cond ((featurep 'xemacs)
1322 (add-minor-mode 'mime-edit-mode-flag
1323 '((" MIME-Edit " mime-transfer-level-string))
1329 (set-alist 'minor-mode-alist
1330 'mime-edit-mode-flag
1331 '((" MIME-Edit " mime-transfer-level-string)))
1332 (set-alist 'minor-mode-map-alist
1333 'mime-edit-mode-flag
1339 (defun turn-on-mime-edit ()
1340 "Unconditionally turn on MIME-Edit mode."
1342 (if mime-edit-mode-flag
1343 (error "You are already editing a MIME message.")
1344 (setq mime-edit-mode-flag t)
1346 ;; Set transfer level into mode line
1348 (setq mime-transfer-level-string
1349 (mime-encoding-name mime-transfer-level 'not-omit))
1350 (force-mode-line-update)
1352 ;; Define menu for XEmacs.
1353 (if (featurep 'xemacs)
1354 (mime-edit-define-menu-for-xemacs)
1359 (make-local-variable 'paragraph-start)
1360 (setq paragraph-start
1361 (regexp-or mime-edit-single-part-tag-regexp
1363 (make-local-variable 'paragraph-separate)
1364 (setq paragraph-separate
1365 (regexp-or mime-edit-single-part-tag-regexp
1366 paragraph-separate))
1367 (run-hooks 'mime-edit-mode-hook)
1370 (substitute-command-keys
1371 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1375 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1378 (defun mime-edit-exit (&optional nomime no-error)
1379 "Translate the tagged MIME message into a MIME compliant message.
1380 With no argument encode a message in the buffer into MIME, otherwise
1381 just return to previous mode."
1383 (if (not mime-edit-mode-flag)
1385 (error "You aren't editing a MIME message.")
1389 (run-hooks 'mime-edit-translate-hook)
1390 (mime-edit-translate-buffer)))
1391 ;; Restore previous state.
1392 (setq mime-edit-mode-flag nil)
1393 (if (and (featurep 'xemacs)
1394 (featurep 'menubar))
1395 (delete-menu-item (list mime-edit-menu-title))
1398 (set-buffer-modified-p (buffer-modified-p))
1399 (run-hooks 'mime-edit-exit-hook)
1400 (message "Exit MIME editor mode.")
1403 (defun mime-edit-maybe-translate ()
1405 (mime-edit-exit nil t)
1406 (call-interactively 'mime-edit-maybe-split-and-send)
1409 (defun mime-edit-help ()
1410 "Show help message about MIME mode."
1412 (with-output-to-temp-buffer "*Help*"
1413 (princ "MIME editor mode:\n")
1414 (princ (documentation 'mime-edit-mode))
1415 (print-help-return-message)))
1417 (defun mime-edit-insert-text (&optional subtype)
1418 "Insert a text message.
1419 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1420 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1422 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1424 (if (looking-at mime-edit-single-part-tag-regexp)
1426 ;; Make a space between the following message.
1430 (if (and (member (cadr ret) '("enriched"))
1431 (fboundp 'enriched-mode))
1433 (if (boundp 'enriched-mode)
1438 (defun mime-edit-insert-file (file &optional verbose)
1439 "Insert a message from a file."
1440 (interactive "fInsert file as MIME message: \nP")
1441 (let* ((guess (mime-find-file-type file))
1442 (type (nth 0 guess))
1443 (subtype (nth 1 guess))
1444 (parameters (nth 2 guess))
1445 (encoding (nth 3 guess))
1446 (disposition-type (nth 4 guess))
1447 (disposition-params (nth 5 guess))
1449 (if (or (interactive-p) verbose)
1450 (setq type (mime-prompt-for-type type)
1451 subtype (mime-prompt-for-subtype type subtype)
1452 encoding (mime-prompt-for-encoding encoding)))
1453 (if (or (consp parameters) (stringp disposition-type))
1454 (let ((rest parameters) cell attribute value)
1455 (setq parameters "")
1457 (setq cell (car rest))
1458 (setq attribute (car cell))
1459 (setq value (cdr cell))
1460 (if (eq value 'file)
1461 (setq value (std11-wrap-as-quoted-string
1462 (file-name-nondirectory file)))
1464 (setq parameters (concat parameters "; " attribute "=" value))
1465 (setq rest (cdr rest))
1467 (if disposition-type
1470 (concat parameters "\n"
1471 "Content-Disposition: " disposition-type))
1472 (setq rest disposition-params)
1474 (setq cell (car rest))
1475 (setq attribute (car cell))
1476 (setq value (cdr cell))
1477 (if (eq value 'file)
1478 (setq value (std11-wrap-as-quoted-string
1479 (file-name-nondirectory file)))
1482 (concat parameters "; " attribute "=" value))
1483 (setq rest (cdr rest))
1487 (mime-edit-insert-tag type subtype parameters)
1488 (mime-edit-insert-binary-file file encoding)
1491 (defun mime-edit-insert-external ()
1492 "Insert a reference to external body."
1494 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1496 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1498 (let* ((pritype (mime-prompt-for-type))
1499 (subtype (mime-prompt-for-subtype pritype))
1500 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1503 (insert "Content-Type: "
1504 pritype "/" subtype (or parameters "") "\n")))
1505 (if (and (not (eobp))
1506 (not (looking-at mime-edit-single-part-tag-regexp)))
1507 (insert (mime-make-text-tag) "\n")))
1509 (defun mime-edit-insert-voice ()
1510 "Insert a voice message."
1514 "What transfer encoding: "
1515 (mime-encoding-alist) nil t nil)))
1516 (mime-edit-insert-tag "audio" "basic" nil)
1517 (mime-edit-define-encoding encoding)
1519 (narrow-to-region (1- (point))(point))
1521 (funcall mime-edit-voice-recorder encoding)
1524 (invisible-region (point-min)(point-max))
1525 (goto-char (point-max))
1528 (defun mime-edit-insert-signature (&optional arg)
1529 "Insert a signature file."
1531 (let ((signature-insert-hook
1534 (let ((items (mime-find-file-type signature-file-name)))
1535 (apply (function mime-edit-insert-tag)
1536 (car items) (cadr items) (list (caddr items))))
1539 (insert-signature arg)
1543 ;; Insert a new tag around a point.
1545 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1546 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1547 If nothing is inserted, return nil."
1550 (mime-edit-goto-tag)
1551 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1552 (< (match-beginning 0) p)
1555 (goto-char (match-beginning 0))
1564 (mime-prompt-for-type)))
1567 (mime-prompt-for-subtype pritype)))
1570 (mime-prompt-for-parameters pritype subtype delimiter)))
1571 ;; Make a new MIME tag.
1572 (setq newtag (mime-make-tag pritype subtype parameters))
1573 ;; Find an current MIME tag.
1576 (if (mime-edit-goto-tag)
1577 (buffer-substring (match-beginning 0) (match-end 0))
1578 ;; Assume content type is 'text/plan'.
1579 (mime-make-tag "text" "plain")
1581 ;; We are only interested in TEXT.
1583 (not (mime-test-content-type
1584 (mime-edit-get-contype oldtag) "text")))
1587 (if (or (not oldtag) ;Not text
1588 (or mime-ignore-same-text-tag
1589 (not (string-equal oldtag newtag))))
1591 ;; Mark the beginning of the tag for convenience.
1592 (push-mark (point) 'nomsg)
1593 (insert newtag "\n")
1594 (list pritype subtype parameters) ;New tag is created.
1596 ;; Restore previous point.
1598 nil ;Nothing is created.
1602 (defun mime-edit-insert-binary-file (file &optional encoding)
1603 "Insert binary FILE at point.
1604 Optional argument ENCODING specifies an encoding method such as base64."
1605 (let* ((tagend (1- (point))) ;End of the tag
1606 (hide-p (and mime-auto-hide-body
1609 (let ((en (downcase encoding)))
1610 (or (string-equal en "7bit")
1611 (string-equal en "8bit")
1612 (string-equal en "binary")
1616 (narrow-to-region tagend (point))
1617 (mime-insert-encoded-file file encoding)
1620 (invisible-region (point-min) (point-max))
1621 (goto-char (point-max))
1623 (goto-char (point-max))
1626 (looking-at mime-edit-tag-regexp)
1627 (= (point)(point-max))
1628 (mime-edit-insert-tag "text" "plain")
1630 ;; Define encoding even if it is 7bit.
1631 (if (stringp encoding)
1633 (goto-char tagend) ; Make sure which line the tag is on.
1634 (mime-edit-define-encoding encoding)
1639 ;; Commands work on a current message flagment.
1641 (defun mime-edit-goto-tag ()
1642 "Search for the beginning of the tagged MIME message."
1643 (let ((current (point)))
1644 (if (looking-at mime-edit-tag-regexp)
1646 ;; At first, go to the end.
1647 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1648 (goto-char (1- (match-beginning 0))) ;For multiline tag
1651 (goto-char (point-max))
1653 ;; Then search for the beginning.
1654 (re-search-backward mime-edit-end-tag-regexp nil t)
1655 (or (looking-at mime-edit-beginning-tag-regexp)
1656 ;; Restore previous point.
1663 (defun mime-edit-content-beginning ()
1664 "Return the point of the beginning of content."
1666 (let ((beg (save-excursion
1667 (beginning-of-line) (point))))
1668 (if (mime-edit-goto-tag)
1669 (let ((top (point)))
1670 (goto-char (match-end 0))
1671 (if (and (= beg top)
1672 (= (following-char) ?\^M))
1676 ;; Default text/plain tag.
1677 (goto-char (point-min))
1679 (concat "\n" (regexp-quote mail-header-separator)
1680 (if mime-ignore-preceding-spaces
1681 "[ \t\n]*\n" "\n")) nil 'move)
1685 (defun mime-edit-content-end ()
1686 "Return the point of the end of content."
1688 (if (mime-edit-goto-tag)
1690 (goto-char (match-end 0))
1691 (if (invisible-p (point))
1692 (next-visible-point (point))
1693 ;; Move to the end of this text.
1694 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1695 ;; Don't forget a multiline tag.
1696 (goto-char (match-beginning 0))
1700 ;; Assume the message begins with text/plain.
1701 (goto-char (mime-edit-content-beginning))
1702 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1703 ;; Don't forget a multiline tag.
1704 (goto-char (match-beginning 0)))
1708 (defun mime-edit-define-charset (charset)
1709 "Set charset of current tag to CHARSET."
1711 (if (mime-edit-goto-tag)
1712 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1713 (delete-region (match-beginning 0) (match-end 0))
1716 (mime-edit-set-parameter
1717 (mime-edit-get-contype tag)
1719 (let ((comment (get charset 'mime-charset-comment)))
1721 (concat (upcase (symbol-name charset)) " (" comment ")")
1722 (upcase (symbol-name charset)))))
1723 (mime-edit-get-encoding tag)))
1726 (defun mime-edit-define-encoding (encoding)
1727 "Set encoding of current tag to ENCODING."
1729 (if (mime-edit-goto-tag)
1730 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1731 (delete-region (match-beginning 0) (match-end 0))
1732 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1735 (defun mime-edit-choose-charset ()
1736 "Choose charset of a text following current point."
1737 (detect-mime-charset-region (point) (mime-edit-content-end))
1740 (defun mime-make-text-tag (&optional subtype)
1741 "Make a tag for a text after current point.
1742 Subtype of text type can be specified by an optional argument SUBTYPE.
1743 Otherwise, it is obtained from mime-content-types."
1744 (let* ((pritype "text")
1745 (subtype (or subtype
1746 (car (car (cdr (assoc pritype mime-content-types)))))))
1747 ;; Charset should be defined later.
1748 (mime-make-tag pritype subtype)))
1751 ;; Tag handling functions
1753 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1754 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1755 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1759 (defun mime-create-tag (contype &optional encoding)
1760 "Make a tag with CONTENT-TYPE and optional ENCODING."
1761 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1764 (defun mime-edit-get-contype (tag)
1765 "Return Content-Type (including parameters) of TAG."
1767 (or (string-match mime-edit-single-part-tag-regexp tag)
1768 (string-match mime-edit-multipart-beginning-regexp tag)
1769 (string-match mime-edit-multipart-end-regexp tag)
1771 (substring tag (match-beginning 1) (match-end 1))
1774 (defun mime-edit-get-encoding (tag)
1775 "Return encoding of TAG."
1777 (string-match mime-edit-single-part-tag-regexp tag)
1779 (not (= (match-beginning 3) (match-end 3)))
1780 (substring tag (match-beginning 3) (match-end 3))))
1782 (defun mime-get-parameter (contype parameter)
1783 "For given CONTYPE return value for PARAMETER.
1784 Nil if no such parameter."
1788 (regexp-quote parameter)
1789 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1791 (substring contype (match-beginning 1) (match-end 1))
1792 nil ;No such parameter
1795 (defun mime-edit-set-parameter (contype parameter value)
1796 "For given CONTYPE set PARAMETER to VALUE."
1797 (let (ctype opt-fields)
1798 (if (string-match "\n[^ \t\n\r]+:" contype)
1799 (setq ctype (substring contype 0 (match-beginning 0))
1800 opt-fields (substring contype (match-beginning 0)))
1801 (setq ctype contype)
1806 (regexp-quote parameter)
1807 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1810 (concat (substring ctype 0 (match-beginning 1))
1812 (substring ctype (match-end 1))
1814 (concat ctype "; " parameter "=" value opt-fields)
1817 (defun mime-strip-parameters (contype)
1818 "Return primary content-type and subtype without parameters for CONTYPE."
1819 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1820 (substring contype (match-beginning 1) (match-end 1)) nil))
1822 (defun mime-test-content-type (contype type &optional subtype)
1823 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1824 (and (stringp contype)
1827 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1828 (downcase contype))))
1833 (defun mime-find-file-type (file)
1834 "Guess Content-Type, subtype, and parameters from FILE."
1836 (guesses mime-file-types))
1837 (while (and (not guess) guesses)
1838 (if (string-match (car (car guesses)) file)
1839 (setq guess (cdr (car guesses))))
1840 (setq guesses (cdr guesses)))
1844 (defun mime-prompt-for-type (&optional default)
1845 "Ask for Content-type."
1847 ;; Repeat until primary content type is specified.
1848 (while (string-equal type "")
1850 (completing-read "What content type: "
1853 'require-match ;Type must be specified.
1856 (if (string-equal type "")
1858 (message "Content type is required.")
1865 (defun mime-prompt-for-subtype (type &optional default)
1866 "Ask for subtype of media-type TYPE."
1867 (let ((subtypes (cdr (assoc type mime-content-types))))
1869 (assoc default subtypes))
1870 (setq default (car (car subtypes)))
1876 "What content subtype: (default " default ") ")
1877 "What content subtype: ")
1878 (cdr (assoc type mime-content-types))
1880 'require-match ;Subtype must be specified.
1883 (if (string-equal answer "") default answer)))
1885 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1886 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1887 Optional DELIMITER specifies parameter delimiter (';' by default)."
1888 (let* ((delimiter (or delimiter "; "))
1893 (mime-prompt-for-parameters-1
1895 (cdr (assoc pritype mime-content-types))))))
1898 (if (and (stringp parameters)
1899 (not (string-equal parameters "")))
1900 (concat delimiter parameters)
1901 "" ;"" if no parameters
1904 (defun mime-prompt-for-parameters-1 (optlist)
1905 (apply (function append)
1906 (mapcar (function mime-prompt-for-parameter) optlist)))
1908 (defun mime-prompt-for-parameter (parameter)
1910 Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
1911 (let* ((prompt (car parameter))
1912 (choices (mapcar (function
1914 (if (consp e) e (list e))))
1916 (default (car (car choices)))
1922 (concat "What " prompt
1924 (if (string-equal default "") "\"\"" default)
1926 choices nil nil ""))
1927 ;; If nothing is selected, use default.
1928 (if (string-equal answer "")
1929 (setq answer default)))
1931 (read-string (concat "What " prompt ": "))))
1932 (cons (if (and answer
1933 (not (string-equal answer "")))
1935 ;; Note: control characters ignored!
1936 (if (string-match mime-tspecials-regexp answer)
1937 (concat "\"" answer "\"") answer)))
1938 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1941 (defun mime-prompt-for-encoding (default)
1942 "Ask for Content-Transfer-Encoding."
1947 "What transfer encoding: "
1948 (mime-encoding-alist) nil t default)
1954 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1957 (defvar mime-edit-translate-buffer-hook
1958 '(mime-edit-pgp-enclose-buffer
1959 mime-edit-translate-body
1960 mime-edit-translate-header))
1962 (defun mime-edit-translate-header ()
1963 "Encode the message header into network representation."
1964 (mime-encode-header-in-buffer 'code-conversion)
1965 (run-hooks 'mime-edit-translate-header-hook))
1967 (defun mime-edit-translate-buffer ()
1968 "Encode the tagged MIME message in current buffer in MIME compliant message."
1971 (if (catch 'mime-edit-error
1973 (run-hooks 'mime-edit-translate-buffer-hook)
1977 (error "Translation error!")
1980 (defun mime-edit-find-inmost ()
1981 (goto-char (point-min))
1982 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1983 (let ((bb (match-beginning 0))
1985 (type (buffer-substring (match-beginning 1)(match-end 1)))
1987 (setq end-exp (format "--}-<<%s>>\n" type))
1989 (if (re-search-forward end-exp nil t)
1990 (setq eb (match-beginning 0))
1991 (setq eb (point-max))
1993 (narrow-to-region be eb)
1995 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1997 (narrow-to-region (match-beginning 0)(point-max))
1998 (mime-edit-find-inmost)
2001 (list type bb be eb)
2004 (defun mime-edit-process-multipart-1 (boundary)
2005 (let ((ret (mime-edit-find-inmost)))
2007 (let ((type (car ret))
2008 (bb (nth 1 ret))(be (nth 2 ret))
2011 (narrow-to-region bb eb)
2012 (delete-region bb be)
2013 (setq bb (point-min))
2014 (setq eb (point-max))
2017 (if (looking-at mime-edit-multipart-end-regexp)
2018 (let ((beg (match-beginning 0))
2021 (delete-region beg end)
2022 (or (looking-at mime-edit-beginning-tag-regexp)
2023 (looking-at mime-edit-multipart-end-regexp)
2025 (insert (concat (mime-make-text-tag) "\n"))
2027 (cond ((string-equal type "quote")
2028 (mime-edit-enquote-region bb eb)
2030 ((string-equal type "pgp-signed")
2031 (mime-edit-sign-pgp-mime bb eb boundary)
2033 ((string-equal type "pgp-encrypted")
2034 (mime-edit-encrypt-pgp-mime bb eb boundary)
2036 ((string-equal type "kazu-signed")
2037 (mime-edit-sign-pgp-kazu bb eb boundary)
2039 ((string-equal type "kazu-encrypted")
2040 (mime-edit-encrypt-pgp-kazu bb eb boundary)
2042 ((string-equal type "smime-signed")
2043 (mime-edit-sign-smime bb eb boundary)
2045 ((string-equal type "smime-encrypted")
2046 (mime-edit-encrypt-smime bb eb boundary)
2050 (nth 2 (mime-edit-translate-region bb eb
2054 (format "--[[multipart/%s;
2055 boundary=\"%s\"][7bit]]\n"
2060 (defun mime-edit-enquote-region (beg end)
2063 (narrow-to-region beg end)
2065 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2066 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
2067 (replace-match (concat "- " (substring tag 1)))
2070 (defun mime-edit-dequote-region (beg end)
2073 (narrow-to-region beg end)
2075 (while (re-search-forward
2076 mime-edit-quoted-single-part-tag-regexp nil t)
2077 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
2078 (replace-match (concat "-" (substring tag 2)))
2081 (defvar mime-edit-pgp-user-id nil)
2083 (defun mime-edit-delete-trailing-whitespace ()
2086 (goto-char (point-min))
2087 (while (re-search-forward "[ \t]+$" nil t)
2088 (delete-region (match-beginning 0) (match-end 0))))))
2090 (defun mime-edit-sign-pgp-mime (beg end boundary)
2093 (let* ((from (std11-field-body "From" mail-header-separator))
2095 (narrow-to-region beg end)
2096 (mime-edit-translate-region beg end boundary)))
2098 (encoding (nth 1 ret))
2099 (pgp-boundary (concat "pgp-sign-" boundary))
2101 (mime-edit-delete-trailing-whitespace) ; RFC3156
2103 (insert (format "Content-Type: %s\n" ctype))
2105 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2108 (or (let ((pgg-default-user-id
2109 (or mime-edit-pgp-user-id
2111 (nth 1 (std11-extract-address-components from))
2112 pgg-default-user-id)))
2114 (pgg-sign-region (point-min)(point-max)))
2115 (throw 'mime-edit-error 'pgp-error)
2118 (cdr (assq 'hash-algorithm
2119 (cdar (with-current-buffer pgg-output-buffer
2120 (pgg-parse-armor-region
2121 (point-min)(point-max))))))
2124 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
2127 (insert (format "--[[multipart/signed;
2129 protocol=\"application/pgp-signature\"][7bit]]
2131 " pgp-boundary micalg pgp-boundary))
2132 (goto-char (point-max))
2133 (insert (format "\n--%s
2134 Content-Type: application/pgp-signature
2135 Content-Transfer-Encoding: 7bit
2138 (insert-buffer-substring pgg-output-buffer)
2139 (goto-char (point-max))
2140 (insert (format "\n--%s--\n" pgp-boundary))
2143 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
2145 (defun mime-edit-make-encrypt-recipient-header ()
2146 (let* ((names mime-edit-encrypt-recipient-fields-list)
2148 (std11-field-bodies (cons "From" names)
2149 nil mail-header-separator))
2152 (setq values (cdr values))))
2153 (header (and (stringp from)
2154 (if (string-equal from "")
2156 (format "From: %s\n" from)
2159 (while (and names values)
2160 (let ((name (car names))
2161 (value (car values))
2163 (and (stringp value)
2164 (or (string-equal value "")
2166 (setq header (concat header name ": " value "\n")
2167 recipients (if recipients
2168 (concat recipients " ," value)
2171 (setq names (cdr names)
2172 values (cdr values))
2174 (vector from recipients header)
2177 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
2180 (let (from recipients header)
2181 (let ((ret (mime-edit-make-encrypt-recipient-header)))
2182 (setq from (aref ret 0)
2183 recipients (aref ret 1)
2184 header (aref ret 2))
2186 (narrow-to-region beg end)
2188 (mime-edit-translate-region beg end boundary))
2190 (encoding (nth 1 ret))
2191 (pgp-boundary (concat "pgp-" boundary)))
2194 (insert (format "Content-Type: %s\n" ctype))
2196 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2199 (mime-encode-header-in-buffer)
2200 (or (let ((pgg-default-user-id
2201 (or mime-edit-pgp-user-id
2203 (nth 1 (std11-extract-address-components from))
2204 pgg-default-user-id)))
2207 (point-min) (point-max)
2208 (mapcar (lambda (recipient)
2209 (nth 1 (std11-extract-address-components
2211 (split-string recipients
2212 "\\([ \t\n]*,[ \t\n]*\\)+")))
2214 (throw 'mime-edit-error 'pgp-error)
2216 (delete-region (point-min)(point-max))
2218 (insert (format "--[[multipart/encrypted;
2220 protocol=\"application/pgp-encrypted\"][7bit]]
2222 Content-Type: application/pgp-encrypted
2226 Content-Type: application/octet-stream
2227 Content-Transfer-Encoding: 7bit
2229 " pgp-boundary pgp-boundary pgp-boundary))
2230 (insert-buffer-substring pgg-output-buffer)
2231 (goto-char (point-max))
2232 (insert (format "\n--%s--\n" pgp-boundary))
2235 (defun mime-edit-sign-pgp-kazu (beg end boundary)
2238 (narrow-to-region beg end)
2240 (mime-edit-translate-region beg end boundary))
2242 (encoding (nth 1 ret)))
2244 (insert (format "Content-Type: %s\n" ctype))
2246 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2249 (or (pgg-sign-region beg (point-max) 'clearsign)
2250 (throw 'mime-edit-error 'pgp-error)
2254 "--[[application/pgp; format=mime][7bit]]\n")
2258 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
2260 (let (recipients header)
2261 (let ((ret (mime-edit-make-encrypt-recipient-header)))
2262 (setq recipients (aref ret 1)
2263 header (aref ret 2))
2266 (narrow-to-region beg end)
2268 (mime-edit-translate-region beg end boundary))
2270 (encoding (nth 1 ret)))
2273 (insert (format "Content-Type: %s\n" ctype))
2275 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2278 (or (pgg-encrypt-region beg (point-max) recipients)
2279 (throw 'mime-edit-error 'pgp-error)
2283 "--[[application/pgp; format=mime][7bit]]\n")
2287 (defun mime-edit-sign-smime (beg end boundary)
2291 (narrow-to-region beg end)
2292 (mime-edit-translate-region beg end boundary)))
2294 (encoding (nth 1 ret))
2295 (smime-boundary (concat "smime-sign-" boundary)))
2297 (insert (format "Content-Type: %s\n" ctype))
2299 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2302 (let (buffer-undo-list)
2303 (goto-char (point-min))
2304 (while (progn (end-of-line) (not (eobp)))
2307 (or (prog1 (smime-sign-region (point-min)(point-max))
2308 (push nil buffer-undo-list)
2309 (ignore-errors (undo)))
2310 (throw 'mime-edit-error 'pgp-error)
2313 (insert (format "--[[multipart/signed;
2314 boundary=\"%s\"; micalg=sha1;
2315 protocol=\"application/pkcs7-signature\"][7bit]]
2317 " smime-boundary smime-boundary))
2318 (goto-char (point-max))
2319 (insert (format "\n--%s
2320 Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
2321 Content-Transfer-Encoding: base64
2322 Content-Disposition: attachment; filename=\"smime.p7s\"
2323 Content-Description: S/MIME Cryptographic Signature
2326 (insert-buffer-substring smime-output-buffer)
2327 (goto-char (point-max))
2328 (insert (format "\n--%s--\n" smime-boundary))
2331 (defun mime-edit-encrypt-smime (beg end boundary)
2335 (narrow-to-region beg end)
2336 (mime-edit-translate-region beg end boundary)))
2338 (encoding (nth 1 ret)))
2340 (insert (format "Content-Type: %s\n" ctype))
2342 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2345 (goto-char (point-min))
2346 (while (progn (end-of-line) (not (eobp)))
2349 (or (smime-encrypt-region (point-min)(point-max))
2350 (throw 'mime-edit-error 'pgp-error)
2352 (delete-region (point-min)(point-max))
2353 (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
2354 Content-Disposition: attachment; filename=\"smime.p7m\"
2355 Content-Description: S/MIME Encrypted Message][base64]]\n")
2356 (insert-buffer-substring smime-output-buffer)
2359 (defsubst replace-space-with-underline (str)
2360 (mapconcat (function
2368 (defun mime-edit-make-boundary ()
2369 (concat mime-multipart-boundary "_"
2370 (replace-space-with-underline (current-time-string))
2373 (defun mime-edit-translate-body ()
2374 "Encode the tagged MIME body in current buffer in MIME compliant message."
2377 (let ((boundary (mime-edit-make-boundary))
2380 (while (mime-edit-process-multipart-1
2381 (format "%s-%d" boundary i))
2385 ;; We are interested in message body.
2388 (goto-char (point-min))
2390 (concat "\n" (regexp-quote mail-header-separator)
2391 (if mime-ignore-preceding-spaces
2392 "[ \t\n]*\n" "\n")) nil 'move)
2396 (goto-char (point-max))
2397 (and mime-ignore-trailing-spaces
2398 (re-search-backward "[^ \t\n]\n" beg t)
2401 (setq ret (mime-edit-translate-region
2403 (format "%s-%d" boundary i)))
2405 (mime-edit-dequote-region (point-min)(point-max))
2406 (let ((contype (car ret)) ;Content-Type
2407 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
2409 ;; Insert User-Agent field
2410 (and mime-edit-insert-user-agent-field
2411 (or (mail-position-on-field "User-Agent")
2412 (insert mime-edit-user-agent-value)
2414 ;; Make primary MIME headers.
2415 (or (mail-position-on-field "MIME-Version")
2416 (insert mime-edit-mime-version-value))
2417 ;; Remove old Content-Type and other fields.
2419 (goto-char (point-min))
2420 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2421 (narrow-to-region (point-min) (point))
2422 (goto-char (point-min))
2423 (mime-delete-field "Content-Type")
2424 (mime-delete-field "Content-Transfer-Encoding"))
2425 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2426 (mail-position-on-field "Content-Type")
2430 (mail-position-on-field "Content-Transfer-Encoding")
2434 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2435 "Translate single-part-tag to MIME header."
2436 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2437 (let* ((beg (match-beginning 0))
2439 (tag (buffer-substring beg end)))
2440 (delete-region beg end)
2441 (let ((contype (mime-edit-get-contype tag))
2442 (encoding (mime-edit-get-encoding tag)))
2443 (insert (concat prefix "--" boundary "\n"))
2445 (narrow-to-region (point)(point))
2446 (insert "Content-Type: " contype "\n")
2448 (insert "Content-Transfer-Encoding: " encoding "\n"))
2449 (mime-encode-header-in-buffer))
2453 (downcase encoding))))
2456 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2458 (setq boundary (mime-edit-make-boundary))
2462 (narrow-to-region beg end)
2463 (let ((tag nil) ;MIME tag
2464 (contype nil) ;Content-Type
2465 (encoding nil) ;Content-Transfer-Encoding
2466 (nparts 0)) ;Number of body parts
2467 ;; Normalize the body part by inserting appropriate message
2468 ;; tags for every message contents.
2469 (mime-edit-normalize-body)
2470 ;; Counting the number of Content-Type.
2471 (goto-char (point-min))
2472 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2473 (setq nparts (1+ nparts)))
2474 ;; Begin translation.
2476 ((and (<= nparts 1)(not multipart))
2477 ;; It's a singular message.
2478 (goto-char (point-min))
2479 (while (re-search-forward
2480 mime-edit-single-part-tag-regexp nil t)
2482 (buffer-substring (match-beginning 0) (match-end 0)))
2483 (delete-region (match-beginning 0) (1+ (match-end 0)))
2484 (setq contype (mime-edit-get-contype tag))
2485 (setq encoding (mime-edit-get-encoding tag))
2488 ;; It's a multipart message.
2489 (goto-char (point-min))
2490 (let ((prio mime-content-transfer-encoding-priority-list)
2492 (when (setq part-info
2493 (mime-edit-translate-single-part-tag boundary))
2494 (and (setq nprio (member (cdr part-info) prio))
2496 (while (setq part-info
2497 (mime-edit-translate-single-part-tag boundary "\n"))
2498 (and (setq nprio (member (cdr part-info) prio))
2499 (setq prio nprio))))
2500 ;; Define Content-Type as "multipart/mixed".
2502 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2503 (setq encoding (car prio))
2504 ;; Insert the trailer.
2505 (goto-char (point-max))
2506 (insert "\n--" boundary "--\n")
2508 (list contype encoding boundary nparts)
2511 (defun mime-edit-normalize-body ()
2512 "Normalize the body part by inserting appropriate message tags."
2513 ;; Insert the first MIME tags if necessary.
2514 (goto-char (point-min))
2515 (if (not (looking-at mime-edit-single-part-tag-regexp))
2516 (insert (mime-make-text-tag) "\n"))
2517 ;; Check each tag, and add new tag or correct it if necessary.
2518 (goto-char (point-min))
2519 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2520 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2521 (contype (mime-edit-get-contype tag))
2522 (charset (mime-get-parameter contype "charset"))
2523 (encoding (mime-edit-get-encoding tag)))
2524 ;; Remove extra whitespaces after the tag.
2525 (if (looking-at "[ \t]+$")
2526 (delete-region (match-beginning 0) (match-end 0)))
2528 (end (mime-edit-content-end))
2530 (if (= end (point-max))
2533 (or (looking-at mime-edit-beginning-tag-regexp)
2535 (insert (mime-make-text-tag) "\n")
2537 (visible-region beg end)
2541 ((mime-test-content-type contype "message")
2542 ;; Content-type "message" should be sent as is.
2545 ((mime-test-content-type contype "text")
2546 ;; Define charset for text if necessary.
2547 (setq charset (if charset
2548 (intern (downcase charset))
2549 (mime-edit-choose-charset)))
2550 (mime-edit-define-charset charset)
2551 (cond ((string-equal contype "text/x-rot13-47-48")
2554 (mule-caesar-region (point) (mime-edit-content-end))
2556 ((string-equal contype "text/enriched")
2561 (end (mime-edit-content-end))
2563 ;; Patch for hard newlines
2566 ;; (while (search-forward "\n" end t)
2567 ;; (put-text-property (match-beginning 0)
2570 ;; End patch for hard newlines
2571 (enriched-encode beg end nil)
2573 (if (search-forward "\n\n")
2574 (delete-region beg (match-end 0))
2577 ;; Point is now on current tag.
2578 ;; Define encoding and encode text if necessary.
2579 (or encoding ;Encoding is not specified.
2582 (let ((ret (cdr (assq charset mime-charset-type-list))))
2584 (setq bits (car ret)
2587 conv "quoted-printable")))
2588 (if (<= bits mime-transfer-level)
2589 (mime-encoding-name bits)
2591 (beg (mime-edit-content-beginning)))
2592 (encode-mime-charset-region beg (mime-edit-content-end)
2594 ;; Protect "From " in beginning of line
2596 (narrow-to-region beg (mime-edit-content-end))
2598 (let (case-fold-search)
2599 (if (re-search-forward "^From " nil t)
2601 (if (memq charset '(iso-2022-jp
2606 (replace-match "\e(BFrom ")
2607 (re-search-forward "^From " nil t)
2609 (setq encoding "quoted-printable")
2611 ;; canonicalize line break code
2612 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2614 (narrow-to-region beg (mime-edit-content-end))
2616 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2617 ;; In a certain period, `replace-match' with "\\N"
2618 ;; converted 8-bit characters into multibyte string,
2619 ;; but it has been fixed at 2004-01-15.
2620 ;;(replace-match "\\1\r\n"))))
2625 (mime-encode-region beg (mime-edit-content-end)
2626 (or encoding "7bit"))
2627 (mime-edit-define-encoding encoding)
2629 (goto-char (mime-edit-content-end))
2631 ((null encoding) ;Encoding is not specified.
2632 ;; Application, image, audio, video, and any other
2633 ;; unknown content-type without encoding should be
2635 (let* ((encoding "base64") ;Encode in BASE64 by default.
2636 (beg (mime-edit-content-beginning))
2637 (end (mime-edit-content-end)))
2638 (mime-encode-region beg end encoding)
2639 (mime-edit-define-encoding encoding))
2644 (defun mime-delete-field (field)
2645 "Delete header FIELD."
2646 (let ((regexp (format "^%s:[ \t]*" field)))
2647 (goto-char (point-min))
2648 (while (re-search-forward regexp nil t)
2649 (delete-region (match-beginning 0)
2650 (1+ (std11-field-end))))))
2654 ;;; Platform dependent functions
2657 ;; Sun implementations
2659 (defun mime-edit-voice-recorder-for-sun (encoding)
2660 "Record voice in a buffer using Sun audio device,
2661 and insert data encoded as ENCODING."
2662 (message "Start the recording on %s. Type C-g to finish the recording..."
2664 (mime-insert-encoded-file "/dev/audio" encoding)
2668 ;;; @ Other useful commands.
2671 ;; Message forwarding commands as content-type "message/rfc822".
2673 (defun mime-edit-insert-message (&optional message)
2675 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2676 (if (and inserter (fboundp inserter))
2678 (mime-edit-insert-tag "message" "rfc822")
2679 (funcall inserter message)
2681 (message "Sorry, I don't have message inserter for your MUA.")
2684 (defun mime-edit-insert-mail (&optional message)
2686 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2687 (if (and inserter (fboundp inserter))
2689 (mime-edit-insert-tag "message" "rfc822")
2690 (funcall inserter message)
2692 (message "Sorry, I don't have mail inserter for your MUA.")
2695 (defun mime-edit-inserted-message-filter ()
2698 (let ((header-start (point))
2699 (case-fold-search t)
2702 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2703 (if (re-search-forward "^$" (mark t))
2704 (narrow-to-region header-start (match-beginning 0))
2706 (goto-char header-start)
2707 (while (and (re-search-forward
2708 mime-edit-yank-ignored-field-regexp nil t)
2709 (setq beg (match-beginning 0))
2710 (setq end (1+ (std11-field-end)))
2712 (delete-region beg end)
2717 ;;; @ multipart enclosure
2720 (defun mime-edit-enclose-region-internal (type beg end)
2724 (narrow-to-region beg end)
2725 (insert (format "--<<%s>>-{\n" type))
2726 (goto-char (point-max))
2727 (insert (format "--}-<<%s>>\n" type))
2728 (goto-char (point-max))
2730 (or (looking-at mime-edit-beginning-tag-regexp)
2732 (insert (mime-make-text-tag) "\n")
2736 (defun mime-edit-enclose-quote-region (beg end)
2738 (mime-edit-enclose-region-internal 'quote beg end)
2741 (defun mime-edit-enclose-mixed-region (beg end)
2743 (mime-edit-enclose-region-internal 'mixed beg end)
2746 (defun mime-edit-enclose-parallel-region (beg end)
2748 (mime-edit-enclose-region-internal 'parallel beg end)
2751 (defun mime-edit-enclose-digest-region (beg end)
2753 (mime-edit-enclose-region-internal 'digest beg end)
2756 (defun mime-edit-enclose-alternative-region (beg end)
2758 (mime-edit-enclose-region-internal 'alternative beg end)
2761 (defun mime-edit-enclose-pgp-signed-region (beg end)
2763 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2766 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2768 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2771 (defun mime-edit-enclose-kazu-signed-region (beg end)
2773 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2776 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2778 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2781 (defun mime-edit-enclose-smime-signed-region (beg end)
2783 (mime-edit-enclose-region-internal 'smime-signed beg end)
2786 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2788 (mime-edit-enclose-region-internal 'smime-encrypted beg end)
2791 (defun mime-edit-insert-key (&optional arg)
2792 "Insert a pgp public key."
2794 (mime-edit-insert-tag "application" "pgp-keys")
2795 (mime-edit-define-encoding "7bit")
2797 (if (and (not (eobp))
2798 (not (looking-at mime-edit-single-part-tag-regexp)))
2799 (insert (mime-make-text-tag) "\n")))
2805 (defun mime-edit-set-split (arg)
2808 (y-or-n-p "Do you want to enable split? ")
2810 (setq mime-edit-split-message arg)
2812 (message "This message is enabled to split.")
2813 (message "This message is not enabled to split.")
2816 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2817 "Toggle transfer-level is 7bit or 8bit through.
2819 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2821 (if (numberp transfer-level)
2822 (setq mime-transfer-level transfer-level)
2823 (if (< mime-transfer-level 8)
2824 (setq mime-transfer-level 8)
2825 (setq mime-transfer-level 7)
2827 (message (format "Current transfer-level is %d bit"
2828 mime-transfer-level))
2829 (setq mime-transfer-level-string
2830 (mime-encoding-name mime-transfer-level 'not-omit))
2831 (force-mode-line-update)
2834 (defun mime-edit-set-transfer-level-7bit ()
2836 (mime-edit-toggle-transfer-level 7)
2839 (defun mime-edit-set-transfer-level-8bit ()
2841 (mime-edit-toggle-transfer-level 8)
2848 (defvar mime-edit-pgp-processing nil)
2849 (make-variable-buffer-local 'mime-edit-pgp-processing)
2851 (defun mime-edit-set-sign (arg)
2854 (y-or-n-p "Do you want to sign? ")
2858 (or (memq 'sign mime-edit-pgp-processing)
2859 (setq mime-edit-pgp-processing
2860 (nconc mime-edit-pgp-processing
2861 (copy-sequence '(sign)))))
2862 (message "This message will be signed.")
2864 (setq mime-edit-pgp-processing
2865 (delq 'sign mime-edit-pgp-processing))
2866 (message "This message will not be signed.")
2869 (defun mime-edit-set-encrypt (arg)
2872 (y-or-n-p "Do you want to encrypt? ")
2876 (or (memq 'encrypt mime-edit-pgp-processing)
2877 (setq mime-edit-pgp-processing
2878 (nconc mime-edit-pgp-processing
2879 (copy-sequence '(encrypt)))))
2880 (message "This message will be encrypted.")
2882 (setq mime-edit-pgp-processing
2883 (delq 'encrypt mime-edit-pgp-processing))
2884 (message "This message will not be encrypted.")
2887 (defun mime-edit-pgp-enclose-buffer ()
2888 (let ((beg (save-excursion
2889 (goto-char (point-min))
2890 (if (search-forward (concat "\n" mail-header-separator "\n"))
2895 (if (memq 'sign mime-edit-pgp-processing)
2896 (mime-edit-enclose-pgp-signed-region beg (point-max)))
2897 (if (memq 'encrypt mime-edit-pgp-processing)
2898 (mime-edit-enclose-pgp-encrypted-region beg (point-max)))
2904 (defun mime-edit-insert-partial-header (fields subject
2905 id number total separator)
2907 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2908 (insert mime-edit-mime-version-field-for-message/partial)
2910 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2911 id number total separator))
2914 (defun mime-edit-split-and-send
2915 (&optional cmd lines mime-edit-message-max-length)
2919 (count-lines (point-min) (point-max)))
2921 (or mime-edit-message-max-length
2922 (setq mime-edit-message-max-length
2923 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2924 mime-edit-message-default-max-lines))
2926 (let ((separator mail-header-separator)
2928 (replace-space-with-underline (current-time-string))
2929 "@" (system-name) "\"")))
2930 (run-hooks 'mime-edit-before-split-hook)
2931 (let ((the-buf (current-buffer))
2932 (copy-buf (get-buffer-create " *Original Message*"))
2933 (header (std11-header-string-except
2934 mime-edit-split-ignored-field-regexp separator))
2935 (subject (mail-fetch-field "subject"))
2936 (total (+ (/ lines mime-edit-message-max-length)
2937 (if (> (mod lines mime-edit-message-max-length) 0)
2943 mime-edit-split-message-sender-alist))
2947 (error "Split sender is not specified for `%s'." major-mode)
2950 (mime-edit-partial-number 1)
2953 (set-buffer copy-buf)
2955 (insert-buffer the-buf)
2957 (if (re-search-forward
2958 (concat "^" (regexp-quote separator) "$") nil t)
2959 (let ((he (match-beginning 0)))
2961 (narrow-to-region (point-min) he)
2963 (goto-char (point-min))
2964 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2965 (delete-region (match-beginning 0)
2966 (1+ (std11-field-end)))
2968 (while (< mime-edit-partial-number total)
2971 (set-buffer copy-buf)
2972 (setq data (buffer-substring
2975 (goto-line mime-edit-message-max-length)
2978 (delete-region (point-min)(point))
2980 (mime-edit-insert-partial-header
2981 header subject id mime-edit-partial-number total separator)
2984 (message (format "Sending %d/%d..."
2985 mime-edit-partial-number total))
2986 (call-interactively command)
2987 (message (format "Sending %d/%d...done"
2988 mime-edit-partial-number total))
2990 (setq mime-edit-partial-number
2991 (1+ mime-edit-partial-number))
2995 (set-buffer copy-buf)
2996 (setq data (buffer-string))
2999 (mime-edit-insert-partial-header
3000 header subject id mime-edit-partial-number total separator)
3003 (message (format "Sending %d/%d..."
3004 mime-edit-partial-number total))
3005 (message (format "Sending %d/%d...done"
3006 mime-edit-partial-number total))
3010 (defun mime-edit-maybe-split-and-send (&optional cmd)
3012 (run-hooks 'mime-edit-before-send-hook)
3013 (let ((mime-edit-message-max-length
3014 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
3015 mime-edit-message-default-max-lines))
3016 (lines (count-lines (point-min) (point-max)))
3018 (if (and (> lines mime-edit-message-max-length)
3019 mime-edit-split-message)
3020 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
3024 ;;; @ preview message
3027 (defvar mime-edit-buffer nil) ; buffer local variable
3028 (defvar mime-edit-temp-message-buffer nil) ; buffer local variable
3030 (defun mime-edit-preview-message ()
3031 "preview editing MIME message."
3033 (let* ((str (buffer-string))
3034 (separator mail-header-separator)
3035 (the-buf (current-buffer))
3036 (buf-name (buffer-name))
3037 (temp-buf-name (concat "*temp-article:" buf-name "*"))
3038 (buf (get-buffer temp-buf-name))
3039 (pgp-processing mime-edit-pgp-processing)
3043 (switch-to-buffer buf)
3046 (setq buf (get-buffer-create temp-buf-name))
3047 (switch-to-buffer buf)
3050 (setq major-mode 'mime-temp-message-mode)
3051 (make-local-variable 'mail-header-separator)
3052 (setq mail-header-separator separator)
3053 (make-local-variable 'mime-edit-buffer)
3054 (setq mime-edit-buffer the-buf)
3055 (setq mime-edit-pgp-processing pgp-processing)
3057 (run-hooks 'mime-edit-translate-hook)
3058 (mime-edit-translate-buffer)
3059 (goto-char (point-min))
3060 (if (re-search-forward
3061 (concat "^" (regexp-quote separator) "$"))
3065 (make-local-variable 'mime-edit-temp-message-buffer)
3066 (setq mime-edit-temp-message-buffer buf)))
3068 (defun mime-edit-quitting-method ()
3069 "Quitting method for mime-view."
3070 (let* ((temp mime-edit-temp-message-buffer)
3072 (mime-preview-kill-buffer)
3074 (setq buf mime-edit-buffer)
3076 (switch-to-buffer buf)))
3078 (set-alist 'mime-preview-quitting-method-alist
3079 'mime-temp-message-mode
3080 #'mime-edit-quitting-method)
3086 (defvar mime-edit-again-ignored-field-regexp
3087 (concat "^\\(" "Content-.*\\|Mime-Version"
3088 (if mime-edit-insert-user-agent-field "\\|User-Agent")
3090 "Regexp for deleted header fields when `mime-edit-again' is called.")
3092 (defsubst eliminate-top-spaces (string)
3093 "Eliminate top sequence of space or tab in STRING."
3094 (if (string-match "^[ \t]+" string)
3095 (substring string (match-end 0))
3098 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
3101 (cdr (assoc (mime-content-type-parameter content-type "protocol")
3102 '(("application/pgp-encrypted" . pgp-encrypted)
3103 ("application/pgp-signature" . pgp-signed))))
3104 (mime-content-type-subtype content-type)))
3105 (boundary (mime-content-type-parameter content-type "boundary"))
3106 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
3107 (re-search-forward boundary-pat nil t)
3108 (let ((bb (match-beginning 0)) eb tag)
3109 (setq tag (format "\n--<<%s>>-{\n" subtype))
3112 (setq bb (+ bb (length tag)))
3114 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
3116 (setq eb (match-beginning 0))
3117 (replace-match (format "--}-<<%s>>\n" subtype))
3119 (narrow-to-region bb eb)
3120 (goto-char (point-min))
3121 (while (re-search-forward boundary-pat nil t)
3122 (let ((beg (match-beginning 0))
3124 (delete-region beg (match-end 0))
3126 (if (re-search-forward boundary-pat nil t)
3127 (setq end (match-beginning 0))
3128 (setq end (point-max))
3131 (narrow-to-region beg end)
3133 ((eq subtype 'pgp-encrypted)
3136 (goto-char (point-min))
3137 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
3140 (save-window-excursion
3141 (pgg-decrypt-region (match-beginning 0)
3143 (delete-region (point-min)(point-max))))
3144 (insert-buffer-substring pgg-output-buffer)
3145 (mime-edit-decode-message-in-buffer
3146 nil not-decode-text)
3147 (delete-region (goto-char (point-min))
3148 (if (search-forward "\n\n" nil t)
3151 (goto-char (point-max))
3154 (mime-edit-decode-message-in-buffer
3155 (if (eq subtype 'digest)
3157 (make-mime-content-type 'message 'rfc822))
3160 (goto-char (point-max))
3164 (goto-char (point-min))
3165 (or (= (point-min) 1)
3166 (delete-region (point-min)
3167 (if (search-forward "\n\n" nil t)
3173 (defun mime-edit-decode-single-part-in-buffer
3174 (content-type not-decode-text &optional content-disposition)
3175 (let* ((type (mime-content-type-primary-type content-type))
3176 (subtype (mime-content-type-subtype content-type))
3177 (ctype (format "%s/%s" type subtype))
3179 (pstr (let ((bytes (+ 14 (length ctype))))
3180 (mapconcat (function
3182 (if (string= (car attr) "charset")
3184 (setq charset (cdr attr))
3186 (let* ((str (concat (car attr)
3189 (setq bytes (+ bytes bs 2))
3192 (setq bytes (+ bs 1))
3196 (mime-content-type-parameters content-type) "")))
3199 (limit (save-excursion
3200 (if (search-forward "\n\n" nil t)
3203 (mime-content-disposition-type content-disposition))
3205 (if disposition-type
3206 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
3207 (mapconcat (function
3212 (if (string-equal "filename"
3214 (std11-wrap-as-quoted-string
3218 (setq bytes (+ bytes bs 2))
3221 (setq bytes (+ bs 1))
3225 (mime-content-disposition-parameters
3226 content-disposition)
3229 (if disposition-type
3230 (setq pstr (format "%s\nContent-Disposition: %s%s"
3231 pstr disposition-type disposition-str))
3234 (if (re-search-forward
3235 "^Content-Transfer-Encoding:" limit t)
3236 (let ((beg (match-beginning 0))
3237 (hbeg (match-end 0))
3238 (end (std11-field-end limit)))
3241 (eliminate-top-spaces
3242 (std11-unfold-string
3243 (buffer-substring hbeg end)))))
3244 (if (or charset (eq type 'text))
3246 (delete-region beg (1+ end))
3247 (goto-char (point-min))
3248 (if (search-forward "\n\n" nil t)
3251 (match-end 0)(point-max) encoding)
3255 (if (and (eq type 'text)
3256 (or encoded (not not-decode-text)))
3259 (goto-char (point-min))
3260 (while (re-search-forward "\r\n" nil t)
3261 (replace-match "\n")
3263 (decode-mime-charset-region (point-min)(point-max)
3264 (or charset default-mime-charset))
3266 (let ((he (if (re-search-forward "^$" nil t)
3270 (if (and (eq type 'text)
3271 (eq subtype 'x-rot13-47-48))
3272 (mule-caesar-region he (point-max))
3274 (if (= (point-min) 1)
3280 (format "%s/%s%s" type subtype pstr)
3283 (delete-region (point-min) he)
3285 (mime-create-tag (format "%s/%s%s" type subtype pstr)
3291 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
3294 (goto-char (point-min))
3295 (let ((ctl (or (mime-read-Content-Type)
3296 default-content-type)))
3298 (let ((type (mime-content-type-primary-type ctl)))
3300 ((and (eq type 'application)
3301 (eq (mime-content-type-subtype ctl) 'pgp-signature))
3302 (delete-region (point-min)(point-max))
3304 ((eq type 'multipart)
3305 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
3308 (mime-edit-decode-single-part-in-buffer
3309 ctl not-decode-text (mime-read-Content-Disposition))
3312 (decode-mime-charset-region (point-min) (point-max)
3313 default-mime-charset))
3315 (if (= (point-min) 1)
3318 (std11-narrow-to-header)
3319 (goto-char (point-min))
3320 (while (re-search-forward
3321 mime-edit-again-ignored-field-regexp nil t)
3322 (delete-region (match-beginning 0) (1+ (std11-field-end)))
3324 (mime-decode-header-in-buffer (not not-decode-text))
3329 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
3330 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
3331 Content-Type and Content-Transfer-Encoding header fields will be
3332 converted to MIME-Edit tags."
3334 (goto-char (point-min))
3336 (concat "\n" (regexp-quote mail-header-separator) "\n")
3338 (replace-match "\n\n")
3340 (mime-edit-decode-message-in-buffer nil not-decode-text)
3341 (goto-char (point-min))
3343 (and (re-search-forward "^$")
3344 (replace-match mail-header-separator)
3354 (provide 'mime-edit)
3356 (run-hooks 'mime-edit-load-hook)
3358 ;;; mime-edit.el ends here