1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,94,95,96,97,98,99,2000 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.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)
120 (autoload 'pgg-encrypt-region "pgg"
121 "PGP encryption of current region." t)
122 (autoload 'pgg-sign-region "pgg"
123 "PGP signature of current region." t)
124 (autoload 'pgg-insert-key "pgg"
125 "Insert PGP public key at point." t)
126 (autoload 'smime-encrypt-region "smime"
127 "S/MIME encryption of current region.")
128 (autoload 'smime-sign-region "smime"
129 "S/MIME signature of current region.")
130 (defvar smime-output-buffer)
131 (defvar smime-errors-buffer)
138 (defconst mime-edit-version
140 (mime-product-name mime-user-interface-product) " "
141 (mapconcat #'number-to-string
142 (mime-product-version mime-user-interface-product) ".")
143 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
149 (defgroup mime-edit nil
153 (defcustom mime-ignore-preceding-spaces nil
154 "*Ignore preceding white spaces if non-nil."
158 (defcustom mime-ignore-trailing-spaces nil
159 "*Ignore trailing white spaces if non-nil."
163 (defcustom mime-ignore-same-text-tag t
164 "*Ignore preceding text content-type tag that is same with new one.
165 If non-nil, the text tag is not inserted unless something different."
169 (defcustom mime-auto-hide-body t
170 "*Hide non-textual body encoded in base64 after insertion if non-nil."
174 (defcustom mime-edit-voice-recorder
175 (function mime-edit-voice-recorder-for-sun)
176 "*Function to record a voice message and encode it."
180 (defcustom mime-edit-mode-hook nil
181 "*Hook called when enter MIME mode."
185 (defcustom mime-edit-translate-hook nil
186 "*Hook called before translating into a MIME compliant message.
187 To insert a signature file automatically, call the function
188 `mime-edit-insert-signature' from this hook."
192 (defcustom mime-edit-exit-hook nil
193 "*Hook called when exit MIME mode."
197 (defvar mime-content-types
199 ;; Charset parameter need not to be specified, since it is
200 ;; defined automatically while translation.
202 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
215 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
216 ("directory" "/pub/GNU/elisp/mime")
218 ("mode" "image" "ascii" "local8"))
223 ("mode" "image" "ascii" "local8"))
224 ("tftp" ("site") ("name"))
225 ("afs" ("site") ("name"))
226 ("local-file" ("site") ("name"))
228 ("server" "ftpmail@nic.karrn.ad.jp")
236 ("octet-stream" ("type" "" "tar" "shar"))
238 ("vnd.ms-powerpoint")
239 ("x-kiss" ("x-cnf")))
253 "*Alist of content-type, subtype, parameters and its values.")
255 (defcustom mime-file-types
258 ;; Programming languages
261 "application" "octet-stream" (("type" . "C++"))
263 "attachment" (("filename" . file))
267 "application" "octet-stream" (("type" . "emacs-lisp"))
269 "attachment" (("filename" . file))
273 "application" "octet-stream" (("type" . "common-lisp"))
275 "attachment" (("filename" . file))
279 "application" "octet-stream" (("type" . "perl"))
281 "attachment" (("filename" . file))
284 ;; Text or translated text
289 "inline" (("filename" . file))
292 ;; .rc : procmail modules pm-xxxx.rc
293 ;; *rc : other resource files
295 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
298 "attachment" (("filename" . file))
306 ("\\.diff$\\|\\.patch$"
307 "application" "octet-stream" (("type" . "patch"))
309 "attachment" (("filename" . file))
313 "text" "plain" nil nil nil nil)
316 ;; Octect binary text
319 "application" "msword" nil
321 "attachment" (("filename" . file))
323 ("\\.ppt$" ; MS Power Point
324 "application" "vnd.ms-powerpoint" nil
326 "attachment" (("filename" . file))
332 "inline" (("filename" . file))
335 "application" "postscript" nil
337 "attachment" (("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 "inline" (("filename" . file))
385 "attachment" (("filename" . file))
390 "attachment" (("filename" . file))
393 "application" "octet-stream" (("type" . "tar+gzip"))
395 "attachment" (("filename" . file))
398 "application" "octet-stream" (("type" . "tar+gzip"))
400 "attachment" (("filename" . file))
403 "application" "octet-stream" (("type" . "tar+compress"))
405 "attachment" (("filename" . file))
408 "application" "octet-stream" (("type" . "tar+compress"))
410 "attachment" (("filename" . file))
413 "application" "octet-stream" (("type" . "gzip"))
415 "attachment" (("filename" . file))
418 "application" "octet-stream" (("type" . "compress"))
420 "attachment" (("filename" . file))
423 "application" "octet-stream" (("type" . "lha"))
425 "attachment" (("filename" . file))
428 "application" "zip" nil
430 "attachment" (("filename" . file))
436 "application" "octet-stream" nil
438 "attachment" (("filename" . file)))
440 "*Alist of file name, types, parameters, and default encoding.
441 If encoding is nil, it is determined from its contents."
445 (choice :tag "Primary-Type"
446 ,@(nconc (mapcar (lambda (cell)
447 (list 'item (car cell))
452 (choice :tag "Sub-Type"
455 (mapcar (lambda (cell)
456 (mapcar (lambda (cell)
457 (list 'item (car cell))
463 (repeat :tag "Parameters of Content-Type field"
464 (cons string (choice string symbol)))
465 ;; content-transfer-encoding
466 (choice :tag "Encoding"
469 (mapcar (lambda (cell)
472 (mime-encoding-list))))
474 (choice :tag "Disposition-Type"
480 (repeat :tag "Parameters of Content-Disposition field"
481 (cons string (choice string symbol)))
486 ;;; @@ about charset, encoding and transfer-level
489 (defvar mime-charset-type-list
491 (iso-8859-1 8 "quoted-printable")
492 (iso-8859-2 8 "quoted-printable")
493 (iso-8859-3 8 "quoted-printable")
494 (iso-8859-4 8 "quoted-printable")
495 (iso-8859-5 8 "quoted-printable")
496 (koi8-r 8 "quoted-printable")
497 (iso-8859-7 8 "quoted-printable")
498 (iso-8859-8 8 "quoted-printable")
499 (iso-8859-9 8 "quoted-printable")
500 (iso-2022-jp 7 "base64")
501 (iso-2022-jp-3 7 "base64")
502 (iso-2022-kr 7 "base64")
508 (shift_jis 8 "base64")
510 (iso-2022-jp-2 7 "base64")
511 (iso-2022-int-1 7 "base64")
514 (defvar mime-transfer-level 7
515 "*A number of network transfer level. It should be bigger than 7.")
516 (make-variable-buffer-local 'mime-transfer-level)
518 (defsubst mime-encoding-name (transfer-level &optional not-omit)
519 (cond ((> transfer-level 8) "binary")
520 ((= transfer-level 8) "8bit")
524 (defvar mime-transfer-level-string
525 (mime-encoding-name mime-transfer-level 'not-omit)
526 "A string formatted version of mime-transfer-level")
527 (make-variable-buffer-local 'mime-transfer-level-string)
529 ;;; @@ about content transfer encoding
531 (defvar mime-content-transfer-encoding-priority-list
532 '(nil "8bit" "binary"))
534 ;;; @@ about message inserting
537 (defvar mime-edit-yank-ignored-field-list
538 '("Received" "Approved" "Path" "Replied" "Status"
539 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
540 "Delete these fields from original message when it is inserted
541 as message/rfc822 part.
542 Each elements are regexp of field-name.")
544 (defvar mime-edit-yank-ignored-field-regexp
546 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
549 (defvar mime-edit-message-inserter-alist nil)
550 (defvar mime-edit-mail-inserter-alist nil)
553 ;;; @@ about message splitting
556 (defcustom mime-edit-split-message t
557 "*Split large message if it is non-nil."
561 (defcustom mime-edit-message-default-max-lines 1000
562 "*Default maximum lines of a message."
566 (defcustom mime-edit-message-max-lines-alist
567 '((news-reply-mode . 500))
568 "Alist of major-mode vs maximum lines of a message.
569 If it is not specified for a major-mode,
570 `mime-edit-message-default-max-lines' is used."
574 (defconst mime-edit-split-ignored-field-regexp
575 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
577 (defcustom mime-edit-split-blind-field-regexp
578 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
579 "*Regular expression to match field-name to be ignored when split sending."
583 (defvar mime-edit-split-message-sender-alist nil)
585 (defvar mime-edit-news-reply-mode-server-running nil)
591 (defconst mime-edit-single-part-tag-regexp
592 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
593 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
595 (defconst mime-edit-quoted-single-part-tag-regexp
596 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
598 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
600 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
602 (defconst mime-edit-beginning-tag-regexp
603 (regexp-or mime-edit-single-part-tag-regexp
604 mime-edit-multipart-beginning-regexp))
606 (defconst mime-edit-end-tag-regexp
607 (regexp-or mime-edit-single-part-tag-regexp
608 mime-edit-multipart-end-regexp))
610 (defconst mime-edit-tag-regexp
611 (regexp-or mime-edit-single-part-tag-regexp
612 mime-edit-multipart-beginning-regexp
613 mime-edit-multipart-end-regexp))
615 (defvar mime-tag-format "--[[%s]]"
616 "*Control-string making a MIME tag.")
618 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
619 "*Control-string making a MIME tag with encoding.")
622 ;;; @@ multipart boundary
625 (defvar mime-multipart-boundary "Multipart"
626 "*Boundary of a multipart message.")
629 ;;; @@ optional header fields
632 (defvar mime-edit-insert-user-agent-field t
633 "*If non-nil, insert User-Agent header field.")
635 (defvar mime-edit-user-agent-value
636 (concat (mime-product-name mime-user-interface-product)
638 (mapconcat #'number-to-string
639 (mime-product-version mime-user-interface-product) ".")
641 (mime-product-code-name mime-user-interface-product)
643 (mime-product-name mime-library-product)
645 (mapconcat #'number-to-string
646 (mime-product-version mime-library-product) ".")
648 (mime-product-code-name mime-library-product)
650 (if (fboundp 'apel-version)
651 (concat (apel-version) " "))
652 (if (featurep 'xemacs)
653 (concat (cond ((featurep 'utf-2000)
654 (concat "UTF-2000-MULE/" utf-2000-version))
655 ((featurep 'mule) "MULE"))
657 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
660 (substring emacs-version 0 (match-end 0))
661 (cond ((and (boundp 'xemacs-betaname)
663 ;; It does not exist in XEmacs
664 ;; versions prior to 20.3.
665 (concat " " xemacs-betaname))
666 ((and (boundp 'emacs-patch-level)
668 ;; It does not exist in FSF Emacs or in
669 ;; XEmacs versions earlier than 21.1.1.
670 (format " (patch %d)" emacs-patch-level))
672 " (" xemacs-codename ") ("
673 system-configuration ")")
674 " (" emacs-version ")"))
675 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
676 (substring emacs-version 0 (match-beginning 0))
679 (if (boundp 'enable-multibyte-characters)
681 " (" system-configuration ")"
682 (if enable-multibyte-characters
683 (concat " MULE/" mule-version)
684 " (with unibyte mode)")
685 (if (featurep 'meadow)
686 (let ((mver (Meadow-version)))
687 (if (string-match "^Meadow-" mver)
692 (concat "MULE/" mule-version
693 " (based on Emacs " ver ")"))
694 (concat "Emacs/" ver " (" system-configuration ")")))))
695 "Body of User-Agent field.
696 If variable `mime-edit-insert-user-agent-field' is not nil, it is
697 inserted into message header.")
703 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
704 "*Specify MIME tspecials.
705 Tspecials means any character that matches with it in header must be quoted.")
707 (defconst mime-edit-mime-version-value
708 (concat "1.0 (generated by " mime-edit-version ")")
709 "MIME version number.")
711 (defconst mime-edit-mime-version-field-for-message/partial
712 (concat "MIME-Version:"
713 (mime-encode-field-body
714 (concat " 1.0 (split by " mime-edit-version ")\n")
716 "MIME version field for message/partial.")
719 ;;; @ keymap and menu
722 (defvar mime-edit-mode-flag nil)
723 (make-variable-buffer-local 'mime-edit-mode-flag)
725 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
726 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
727 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
728 "Keymap for MIME-Edit mode commands to insert entity or set status.")
730 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
731 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
732 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
733 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
734 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
735 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
736 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
737 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
738 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
739 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
741 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
742 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
743 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
744 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
745 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
746 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
747 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
748 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
749 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
750 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
752 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
753 "Keymap prefix for MIME-Edit mode commands about enclosure.")
754 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
755 "Keymap for MIME-Edit mode commands about enclosure.")
757 (define-key mime-edit-mode-enclosure-map
758 "\C-a" 'mime-edit-enclose-alternative-region)
759 (define-key mime-edit-mode-enclosure-map
760 "\C-p" 'mime-edit-enclose-parallel-region)
761 (define-key mime-edit-mode-enclosure-map
762 "\C-m" 'mime-edit-enclose-mixed-region)
763 (define-key mime-edit-mode-enclosure-map
764 "\C-d" 'mime-edit-enclose-digest-region)
765 (define-key mime-edit-mode-enclosure-map
766 "\C-s" 'mime-edit-enclose-pgp-signed-region)
767 (define-key mime-edit-mode-enclosure-map
768 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
769 (define-key mime-edit-mode-enclosure-map
770 "\C-q" 'mime-edit-enclose-quote-region)
772 (defvar mime-edit-mode-map (make-sparse-keymap)
773 "Keymap for MIME-Edit mode commands.")
774 (define-key mime-edit-mode-map
775 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
776 (define-key mime-edit-mode-map
777 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
779 (defconst mime-edit-menu-title "MIME-Edit")
781 (defconst mime-edit-menu-list
782 '((mime-help "Describe MIME editor mode" mime-edit-help)
783 (file "Insert File" mime-edit-insert-file)
784 (external "Insert External" mime-edit-insert-external)
785 (voice "Insert Voice" mime-edit-insert-voice)
786 (message "Insert Message" mime-edit-insert-message)
787 (mail "Insert Mail" mime-edit-insert-mail)
788 (signature "Insert Signature" mime-edit-insert-signature)
789 (text "Insert Text" mime-edit-insert-text)
790 (tag "Insert Tag" mime-edit-insert-tag)
791 (alternative "Enclose as alternative"
792 mime-edit-enclose-alternative-region)
793 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
794 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
795 (digest "Enclose as digest" mime-edit-enclose-digest-region)
796 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
797 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
798 (quote "Verbatim region" mime-edit-enclose-quote-region)
799 (key "Insert Public Key" mime-edit-insert-key)
800 (split "About split" mime-edit-set-split)
801 (sign "About sign" mime-edit-set-sign)
802 (encrypt "About encryption" mime-edit-set-encrypt)
803 (preview "Preview Message" mime-edit-preview-message)
804 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
806 "MIME-edit menubar entry.")
808 (cond ((featurep 'xemacs)
809 ;; modified by Pekka Marjola <pema@iki.fi>
810 ;; 1995/9/5 (c.f. [tm-en:69])
811 (defun mime-edit-define-menu-for-xemacs ()
812 "Define menu for XEmacs."
813 (cond ((featurep 'menubar)
814 (make-local-variable 'current-menubar)
815 (set-buffer-menubar current-menubar)
818 (cons mime-edit-menu-title
821 (vector (nth 1 item)(nth 2 item)
824 mime-edit-menu-list)))
827 ;; modified by Steven L. Baur <steve@miranova.com>
828 ;; 1995/12/6 (c.f. [tm-en:209])
829 (or (boundp 'mime-edit-popup-menu-for-xemacs)
830 (setq mime-edit-popup-menu-for-xemacs
831 (append '("MIME Commands" "---")
832 (mapcar (function (lambda (item)
836 mime-edit-menu-list)))
839 ((>= emacs-major-version 19)
840 (define-key mime-edit-mode-map [menu-bar mime-edit]
841 (cons mime-edit-menu-title
842 (make-sparse-keymap mime-edit-menu-title)))
845 (define-key mime-edit-mode-map
846 (vector 'menu-bar 'mime-edit (car item))
847 (cons (nth 1 item)(nth 2 item))
850 (reverse mime-edit-menu-list)
858 (defvar mime-edit-touched-flag nil)
861 (defun mime-edit-mode ()
862 "MIME minor mode for editing the tagged MIME message.
864 In this mode, basically, the message is composed in the tagged MIME
865 format. The message tag looks like:
867 --[[text/plain; charset=ISO-2022-JP][7bit]]
869 The tag specifies the MIME content type, subtype, optional parameters
870 and transfer encoding of the message following the tag. Messages
871 without any tag are treated as `text/plain' by default. Charset and
872 transfer encoding are automatically defined unless explicitly
873 specified. Binary messages such as audio and image are usually
874 hidden. The messages in the tagged MIME format are automatically
875 translated into a MIME compliant message when exiting this mode.
877 Available charsets depend on Emacs version being used. The following
878 lists the available charsets of each emacs.
880 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
881 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
882 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
883 CN-BIG5 and ISO-2022-INT-1 are available.
885 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
886 be used to represent multilingual text in intermixed manner. Any
887 languages that has no registered charset are represented as either
888 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
890 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
891 without mule, please set variable `default-mime-charset'. This
892 variable must be symbol of which name is a MIME charset.
894 If you want to add more charsets in mule, please set variable
895 `charsets-mime-charset-alist'. This variable must be alist of which
896 key is list of charset and value is symbol of MIME charset. If name
897 of coding-system is different as MIME charset, please set variable
898 `mime-charset-coding-system-alist'. This variable must be alist of
899 which key is MIME charset and value is coding-system.
901 Following commands are available in addition to major mode commands:
904 \\[mime-edit-insert-text] insert a text message.
905 \\[mime-edit-insert-file] insert a (binary) file.
906 \\[mime-edit-insert-external] insert a reference to external body.
907 \\[mime-edit-insert-voice] insert a voice message.
908 \\[mime-edit-insert-message] insert a mail or news message.
909 \\[mime-edit-insert-mail] insert a mail message.
910 \\[mime-edit-insert-signature] insert a signature file at end.
911 \\[mime-edit-insert-key] insert PGP public key.
912 \\[mime-edit-insert-tag] insert a new MIME tag.
914 \[make enclosure (maybe multipart)\]
915 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
916 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
917 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
918 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
919 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
920 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
921 \\[mime-edit-enclose-quote-region] enclose as verbose mode
922 (to avoid to expand tags)
925 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
926 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
927 \\[mime-edit-set-split] set message splitting mode.
928 \\[mime-edit-set-sign] set PGP-sign mode.
929 \\[mime-edit-set-encrypt] set PGP-encryption mode.
930 \\[mime-edit-preview-message] preview editing MIME message.
931 \\[mime-edit-exit] exit and translate into a MIME
933 \\[mime-edit-help] show this help.
934 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
937 Additional commands are available in some major modes:
938 C-c C-c exit, translate and run the original command.
939 C-c C-s exit, translate and run the original command.
941 The following is a message example written in the tagged MIME format.
942 TABs at the beginning of the line are not a part of the message:
944 This is a conventional plain text. It should be translated
947 This is also a plain text. But, it is explicitly specified as
949 --[[text/plain; charset=ISO-8859-1]]
950 This is also a plain text. But charset is specified as
953 ¡Hola! Buenos días. ¿Cómo está usted?
955 This is a <bold>enriched text</bold>.
956 --[[image/gif][base64]]...image encoded in base64 here...
957 --[[audio/basic][base64]]...audio encoded in base64 here...
959 User customizable variables (not documented all of them):
961 Specifies a key prefix for MIME minor mode commands.
963 mime-ignore-preceding-spaces
964 Preceding white spaces in a message body are ignored if non-nil.
966 mime-ignore-trailing-spaces
967 Trailing white spaces in a message body are ignored if non-nil.
970 Hide a non-textual body message encoded in base64 after insertion
974 A number of network transfer level. It should be bigger than 7.
975 If you are in 8bit-through environment, please set 8.
977 mime-edit-voice-recorder
978 Specifies a function to record a voice message and encode it.
979 The function `mime-edit-voice-recorder-for-sun' is for Sun
983 Turning on MIME mode calls the value of mime-edit-mode-hook, if
986 mime-edit-translate-hook
987 The value of mime-edit-translate-hook is called just before translating
988 the tagged MIME format into a MIME compliant message if it is
989 non-nil. If the hook call the function mime-edit-insert-signature,
990 the signature file will be inserted automatically.
993 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
996 (if mime-edit-mode-flag
998 (if mime-edit-touched-flag
1000 (make-local-variable 'mime-edit-touched-flag)
1001 (setq mime-edit-touched-flag t)
1006 (cond ((featurep 'xemacs)
1007 (add-minor-mode 'mime-edit-mode-flag
1008 '((" MIME-Edit " mime-transfer-level-string))
1014 (set-alist 'minor-mode-alist
1015 'mime-edit-mode-flag
1016 '((" MIME-Edit " mime-transfer-level-string)))
1017 (set-alist 'minor-mode-map-alist
1018 'mime-edit-mode-flag
1024 (defun turn-on-mime-edit ()
1025 "Unconditionally turn on MIME-Edit mode."
1027 (if mime-edit-mode-flag
1028 (error "You are already editing a MIME message.")
1029 (setq mime-edit-mode-flag t)
1031 ;; Set transfer level into mode line
1033 (setq mime-transfer-level-string
1034 (mime-encoding-name mime-transfer-level 'not-omit))
1035 (force-mode-line-update)
1037 ;; Define menu for XEmacs.
1038 (if (featurep 'xemacs)
1039 (mime-edit-define-menu-for-xemacs)
1044 ;; I don't care about saving these.
1045 (setq paragraph-start
1046 (regexp-or mime-edit-single-part-tag-regexp
1048 (setq paragraph-separate
1049 (regexp-or mime-edit-single-part-tag-regexp
1050 paragraph-separate))
1051 (run-hooks 'mime-edit-mode-hook)
1053 (substitute-command-keys
1054 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1058 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1061 (defun mime-edit-exit (&optional nomime no-error)
1062 "Translate the tagged MIME message into a MIME compliant message.
1063 With no argument encode a message in the buffer into MIME, otherwise
1064 just return to previous mode."
1066 (if (not mime-edit-mode-flag)
1068 (error "You aren't editing a MIME message.")
1072 (run-hooks 'mime-edit-translate-hook)
1073 (mime-edit-translate-buffer)))
1074 ;; Restore previous state.
1075 (setq mime-edit-mode-flag nil)
1076 (if (and (featurep 'xemacs)
1077 (featurep 'menubar))
1078 (delete-menu-item (list mime-edit-menu-title))
1081 (set-buffer-modified-p (buffer-modified-p))
1082 (run-hooks 'mime-edit-exit-hook)
1083 (message "Exit MIME editor mode.")
1086 (defun mime-edit-maybe-translate ()
1088 (mime-edit-exit nil t)
1089 (call-interactively 'mime-edit-maybe-split-and-send)
1092 (defun mime-edit-help ()
1093 "Show help message about MIME mode."
1095 (with-output-to-temp-buffer "*Help*"
1096 (princ "MIME editor mode:\n")
1097 (princ (documentation 'mime-edit-mode))
1098 (print-help-return-message)))
1100 (defun mime-edit-insert-text (&optional subtype)
1101 "Insert a text message.
1102 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1103 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1105 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1107 (if (looking-at mime-edit-single-part-tag-regexp)
1109 ;; Make a space between the following message.
1113 (if (and (member (cadr ret) '("enriched"))
1114 (fboundp 'enriched-mode))
1116 (if (boundp 'enriched-mode)
1121 (defun mime-edit-insert-file (file &optional verbose)
1122 "Insert a message from a file."
1123 (interactive "fInsert file as MIME message: \nP")
1124 (let* ((guess (mime-find-file-type file))
1125 (type (nth 0 guess))
1126 (subtype (nth 1 guess))
1127 (parameters (nth 2 guess))
1128 (encoding (nth 3 guess))
1129 (disposition-type (nth 4 guess))
1130 (disposition-params (nth 5 guess))
1133 (setq type (mime-prompt-for-type type)
1134 subtype (mime-prompt-for-subtype type subtype)
1136 (if (or (interactive-p) verbose)
1137 (setq encoding (mime-prompt-for-encoding encoding))
1139 (if (or (consp parameters) (stringp disposition-type))
1140 (let ((rest parameters) cell attribute value)
1141 (setq parameters "")
1143 (setq cell (car rest))
1144 (setq attribute (car cell))
1145 (setq value (cdr cell))
1146 (if (eq value 'file)
1147 (setq value (std11-wrap-as-quoted-string
1148 (file-name-nondirectory file)))
1150 (setq parameters (concat parameters "; " attribute "=" value))
1151 (setq rest (cdr rest))
1153 (if disposition-type
1156 (concat parameters "\n"
1157 "Content-Disposition: " disposition-type))
1158 (setq rest disposition-params)
1160 (setq cell (car rest))
1161 (setq attribute (car cell))
1162 (setq value (cdr cell))
1163 (if (eq value 'file)
1164 (setq value (std11-wrap-as-quoted-string
1165 (file-name-nondirectory file)))
1168 (concat parameters "; " attribute "=" value))
1169 (setq rest (cdr rest))
1173 (mime-edit-insert-tag type subtype parameters)
1174 (mime-edit-insert-binary-file file encoding)
1177 (defun mime-edit-insert-external ()
1178 "Insert a reference to external body."
1180 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1182 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1184 (let* ((pritype (mime-prompt-for-type))
1185 (subtype (mime-prompt-for-subtype pritype))
1186 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1189 (insert "Content-Type: "
1190 pritype "/" subtype (or parameters "") "\n")))
1191 (if (and (not (eobp))
1192 (not (looking-at mime-edit-single-part-tag-regexp)))
1193 (insert (mime-make-text-tag) "\n")))
1195 (defun mime-edit-insert-voice ()
1196 "Insert a voice message."
1200 "What transfer encoding: "
1201 (mime-encoding-alist) nil t nil)))
1202 (mime-edit-insert-tag "audio" "basic" nil)
1203 (mime-edit-define-encoding encoding)
1205 (narrow-to-region (1- (point))(point))
1207 (funcall mime-edit-voice-recorder encoding)
1210 (invisible-region (point-min)(point-max))
1211 (goto-char (point-max))
1214 (defun mime-edit-insert-signature (&optional arg)
1215 "Insert a signature file."
1217 (let ((signature-insert-hook
1220 (let ((items (mime-find-file-type signature-file-name)))
1221 (apply (function mime-edit-insert-tag)
1222 (car items) (cadr items) (list (caddr items))))
1225 (insert-signature arg)
1229 ;; Insert a new tag around a point.
1231 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1232 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1233 If nothing is inserted, return nil."
1236 (mime-edit-goto-tag)
1237 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1238 (< (match-beginning 0) p)
1241 (goto-char (match-beginning 0))
1250 (mime-prompt-for-type)))
1253 (mime-prompt-for-subtype pritype)))
1256 (mime-prompt-for-parameters pritype subtype delimiter)))
1257 ;; Make a new MIME tag.
1258 (setq newtag (mime-make-tag pritype subtype parameters))
1259 ;; Find an current MIME tag.
1262 (if (mime-edit-goto-tag)
1263 (buffer-substring (match-beginning 0) (match-end 0))
1264 ;; Assume content type is 'text/plan'.
1265 (mime-make-tag "text" "plain")
1267 ;; We are only interested in TEXT.
1269 (not (mime-test-content-type
1270 (mime-edit-get-contype oldtag) "text")))
1273 (if (or (not oldtag) ;Not text
1274 (or mime-ignore-same-text-tag
1275 (not (string-equal oldtag newtag))))
1277 ;; Mark the beginning of the tag for convenience.
1278 (push-mark (point) 'nomsg)
1279 (insert newtag "\n")
1280 (list pritype subtype parameters) ;New tag is created.
1282 ;; Restore previous point.
1284 nil ;Nothing is created.
1288 (defun mime-edit-insert-binary-file (file &optional encoding)
1289 "Insert binary FILE at point.
1290 Optional argument ENCODING specifies an encoding method such as base64."
1291 (let* ((tagend (1- (point))) ;End of the tag
1292 (hide-p (and mime-auto-hide-body
1295 (let ((en (downcase encoding)))
1296 (or (string-equal en "7bit")
1297 (string-equal en "8bit")
1298 (string-equal en "binary")
1302 (narrow-to-region tagend (point))
1303 (mime-insert-encoded-file file encoding)
1306 (invisible-region (point-min) (point-max))
1307 (goto-char (point-max))
1309 (goto-char (point-max))
1312 (looking-at mime-edit-tag-regexp)
1313 (= (point)(point-max))
1314 (mime-edit-insert-tag "text" "plain")
1316 ;; Define encoding even if it is 7bit.
1317 (if (stringp encoding)
1319 (goto-char tagend) ; Make sure which line the tag is on.
1320 (mime-edit-define-encoding encoding)
1325 ;; Commands work on a current message flagment.
1327 (defun mime-edit-goto-tag ()
1328 "Search for the beginning of the tagged MIME message."
1329 (let ((current (point)))
1330 (if (looking-at mime-edit-tag-regexp)
1332 ;; At first, go to the end.
1333 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1334 (goto-char (1- (match-beginning 0))) ;For multiline tag
1337 (goto-char (point-max))
1339 ;; Then search for the beginning.
1340 (re-search-backward mime-edit-end-tag-regexp nil t)
1341 (or (looking-at mime-edit-beginning-tag-regexp)
1342 ;; Restore previous point.
1349 (defun mime-edit-content-beginning ()
1350 "Return the point of the beginning of content."
1352 (let ((beg (save-excursion
1353 (beginning-of-line) (point))))
1354 (if (mime-edit-goto-tag)
1355 (let ((top (point)))
1356 (goto-char (match-end 0))
1357 (if (and (= beg top)
1358 (= (following-char) ?\^M))
1362 ;; Default text/plain tag.
1363 (goto-char (point-min))
1365 (concat "\n" (regexp-quote mail-header-separator)
1366 (if mime-ignore-preceding-spaces
1367 "[ \t\n]*\n" "\n")) nil 'move)
1371 (defun mime-edit-content-end ()
1372 "Return the point of the end of content."
1374 (if (mime-edit-goto-tag)
1376 (goto-char (match-end 0))
1377 (if (invisible-p (point))
1378 (next-visible-point (point))
1379 ;; Move to the end of this text.
1380 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1381 ;; Don't forget a multiline tag.
1382 (goto-char (match-beginning 0))
1386 ;; Assume the message begins with text/plain.
1387 (goto-char (mime-edit-content-beginning))
1388 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1389 ;; Don't forget a multiline tag.
1390 (goto-char (match-beginning 0)))
1394 (defun mime-edit-define-charset (charset)
1395 "Set charset of current tag to CHARSET."
1397 (if (mime-edit-goto-tag)
1398 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1399 (delete-region (match-beginning 0) (match-end 0))
1402 (mime-edit-set-parameter
1403 (mime-edit-get-contype tag)
1405 (let ((comment (get charset 'mime-charset-comment)))
1407 (concat (upcase (symbol-name charset)) " (" comment ")")
1408 (upcase (symbol-name charset)))))
1409 (mime-edit-get-encoding tag)))
1412 (defun mime-edit-define-encoding (encoding)
1413 "Set encoding of current tag to ENCODING."
1415 (if (mime-edit-goto-tag)
1416 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1417 (delete-region (match-beginning 0) (match-end 0))
1418 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1421 (defun mime-edit-choose-charset ()
1422 "Choose charset of a text following current point."
1423 (detect-mime-charset-region (point) (mime-edit-content-end))
1426 (defun mime-make-text-tag (&optional subtype)
1427 "Make a tag for a text after current point.
1428 Subtype of text type can be specified by an optional argument SUBTYPE.
1429 Otherwise, it is obtained from mime-content-types."
1430 (let* ((pritype "text")
1431 (subtype (or subtype
1432 (car (car (cdr (assoc pritype mime-content-types)))))))
1433 ;; Charset should be defined later.
1434 (mime-make-tag pritype subtype)))
1437 ;; Tag handling functions
1439 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1440 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1441 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1445 (defun mime-create-tag (contype &optional encoding)
1446 "Make a tag with CONTENT-TYPE and optional ENCODING."
1447 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1450 (defun mime-edit-get-contype (tag)
1451 "Return Content-Type (including parameters) of TAG."
1453 (or (string-match mime-edit-single-part-tag-regexp tag)
1454 (string-match mime-edit-multipart-beginning-regexp tag)
1455 (string-match mime-edit-multipart-end-regexp tag)
1457 (substring tag (match-beginning 1) (match-end 1))
1460 (defun mime-edit-get-encoding (tag)
1461 "Return encoding of TAG."
1463 (string-match mime-edit-single-part-tag-regexp tag)
1465 (not (= (match-beginning 3) (match-end 3)))
1466 (substring tag (match-beginning 3) (match-end 3))))
1468 (defun mime-get-parameter (contype parameter)
1469 "For given CONTYPE return value for PARAMETER.
1470 Nil if no such parameter."
1474 (regexp-quote parameter)
1475 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1477 (substring contype (match-beginning 1) (match-end 1))
1478 nil ;No such parameter
1481 (defun mime-edit-set-parameter (contype parameter value)
1482 "For given CONTYPE set PARAMETER to VALUE."
1483 (let (ctype opt-fields)
1484 (if (string-match "\n[^ \t\n\r]+:" contype)
1485 (setq ctype (substring contype 0 (match-beginning 0))
1486 opt-fields (substring contype (match-beginning 0)))
1487 (setq ctype contype)
1492 (regexp-quote parameter)
1493 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1496 (concat (substring ctype 0 (match-beginning 1))
1498 (substring ctype (match-end 1))
1500 (concat ctype "; " parameter "=" value opt-fields)
1503 (defun mime-strip-parameters (contype)
1504 "Return primary content-type and subtype without parameters for CONTYPE."
1505 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1506 (substring contype (match-beginning 1) (match-end 1)) nil))
1508 (defun mime-test-content-type (contype type &optional subtype)
1509 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1510 (and (stringp contype)
1513 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1514 (downcase contype))))
1519 (defun mime-find-file-type (file)
1520 "Guess Content-Type, subtype, and parameters from FILE."
1522 (guesses mime-file-types))
1523 (while (and (not guess) guesses)
1524 (if (string-match (car (car guesses)) file)
1525 (setq guess (cdr (car guesses))))
1526 (setq guesses (cdr guesses)))
1530 (defun mime-prompt-for-type (&optional default)
1531 "Ask for Content-type."
1533 ;; Repeat until primary content type is specified.
1534 (while (string-equal type "")
1536 (completing-read "What content type: "
1539 'require-match ;Type must be specified.
1542 (if (string-equal type "")
1544 (message "Content type is required.")
1551 (defun mime-prompt-for-subtype (type &optional default)
1552 "Ask for subtype of media-type TYPE."
1553 (let ((subtypes (cdr (assoc type mime-content-types))))
1555 (assoc default subtypes))
1556 (setq default (car (car subtypes)))
1562 "What content subtype: (default " default ") ")
1563 "What content subtype: ")
1564 (cdr (assoc type mime-content-types))
1566 'require-match ;Subtype must be specified.
1569 (if (string-equal answer "") default answer)))
1571 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1572 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1573 Optional DELIMITER specifies parameter delimiter (';' by default)."
1574 (let* ((delimiter (or delimiter "; "))
1579 (mime-prompt-for-parameters-1
1581 (cdr (assoc pritype mime-content-types))))))
1584 (if (and (stringp parameters)
1585 (not (string-equal parameters "")))
1586 (concat delimiter parameters)
1587 "" ;"" if no parameters
1590 (defun mime-prompt-for-parameters-1 (optlist)
1591 (apply (function append)
1592 (mapcar (function mime-prompt-for-parameter) optlist)))
1594 (defun mime-prompt-for-parameter (parameter)
1596 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1597 (let* ((prompt (car parameter))
1598 (choices (mapcar (function
1600 (if (consp e) e (list e))))
1602 (default (car (car choices)))
1608 (concat "What " prompt
1610 (if (string-equal default "") "\"\"" default)
1612 choices nil nil ""))
1613 ;; If nothing is selected, use default.
1614 (if (string-equal answer "")
1615 (setq answer default)))
1617 (read-string (concat "What " prompt ": "))))
1618 (cons (if (and answer
1619 (not (string-equal answer "")))
1621 ;; Note: control characters ignored!
1622 (if (string-match mime-tspecials-regexp answer)
1623 (concat "\"" answer "\"") answer)))
1624 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1627 (defun mime-prompt-for-encoding (default)
1628 "Ask for Content-Transfer-Encoding."
1633 "What transfer encoding: "
1634 (mime-encoding-alist) nil t default)
1640 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1643 (defvar mime-edit-translate-buffer-hook
1644 '(mime-edit-pgp-enclose-buffer
1645 mime-edit-translate-body
1646 mime-edit-translate-header))
1648 (defun mime-edit-translate-header ()
1649 "Encode the message header into network representation."
1650 (mime-encode-header-in-buffer 'code-conversion)
1651 (run-hooks 'mime-edit-translate-header-hook))
1653 (defun mime-edit-translate-buffer ()
1654 "Encode the tagged MIME message in current buffer in MIME compliant message."
1657 (if (catch 'mime-edit-error
1659 (run-hooks 'mime-edit-translate-buffer-hook)
1663 (error "Translation error!")
1666 (defun mime-edit-find-inmost ()
1667 (goto-char (point-min))
1668 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1669 (let ((bb (match-beginning 0))
1671 (type (buffer-substring (match-beginning 1)(match-end 1)))
1673 (setq end-exp (format "--}-<<%s>>\n" type))
1675 (if (re-search-forward end-exp nil t)
1676 (setq eb (match-beginning 0))
1677 (setq eb (point-max))
1679 (narrow-to-region be eb)
1681 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1683 (narrow-to-region (match-beginning 0)(point-max))
1684 (mime-edit-find-inmost)
1687 (list type bb be eb)
1690 (defun mime-edit-process-multipart-1 (boundary)
1691 (let ((ret (mime-edit-find-inmost)))
1693 (let ((type (car ret))
1694 (bb (nth 1 ret))(be (nth 2 ret))
1697 (narrow-to-region bb eb)
1698 (delete-region bb be)
1699 (setq bb (point-min))
1700 (setq eb (point-max))
1703 (if (looking-at mime-edit-multipart-end-regexp)
1704 (let ((beg (match-beginning 0))
1707 (delete-region beg end)
1708 (or (looking-at mime-edit-beginning-tag-regexp)
1710 (insert (concat (mime-make-text-tag) "\n"))
1712 (cond ((string-equal type "quote")
1713 (mime-edit-enquote-region bb eb)
1715 ((string-equal type "pgp-signed")
1716 (mime-edit-sign-pgp-mime bb eb boundary)
1718 ((string-equal type "pgp-encrypted")
1719 (mime-edit-encrypt-pgp-mime bb eb boundary)
1721 ((string-equal type "kazu-signed")
1722 (mime-edit-sign-pgp-kazu bb eb boundary)
1724 ((string-equal type "kazu-encrypted")
1725 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1727 ((string-equal type "smime-signed")
1728 (mime-edit-sign-smime bb eb boundary)
1730 ((string-equal type "smime-encrypted")
1731 (mime-edit-encrypt-smime bb eb boundary)
1735 (nth 2 (mime-edit-translate-region bb eb
1739 (format "--[[multipart/%s;
1740 boundary=\"%s\"][7bit]]\n"
1745 (defun mime-edit-enquote-region (beg end)
1748 (narrow-to-region beg end)
1750 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1751 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1752 (replace-match (concat "- " (substring tag 1)))
1755 (defun mime-edit-dequote-region (beg end)
1758 (narrow-to-region beg end)
1760 (while (re-search-forward
1761 mime-edit-quoted-single-part-tag-regexp nil t)
1762 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1763 (replace-match (concat "-" (substring tag 2)))
1766 (defvar mime-edit-pgp-user-id nil)
1768 (defun mime-edit-sign-pgp-mime (beg end boundary)
1771 (let* ((from (std11-field-body "From" mail-header-separator))
1773 (narrow-to-region beg end)
1774 (mime-edit-translate-region beg end boundary)))
1776 (encoding (nth 1 ret))
1777 (pgp-boundary (concat "pgp-sign-" boundary))
1780 (insert (format "Content-Type: %s\n" ctype))
1782 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1785 (or (let ((pgg-default-user-id
1786 (or mime-edit-pgp-user-id
1788 (nth 1 (std11-extract-address-components from))
1789 pgg-default-user-id))))
1790 (pgg-sign-region (point-min)(point-max)))
1791 (throw 'mime-edit-error 'pgp-error)
1794 (cdr (assq 'hash-algorithm
1795 (cdar (with-current-buffer pgg-output-buffer
1796 (pgg-parse-armor-region
1797 (point-min)(point-max))))))
1800 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1803 (insert (format "--[[multipart/signed;
1805 protocol=\"application/pgp-signature\"][7bit]]
1807 " pgp-boundary micalg pgp-boundary))
1808 (goto-char (point-max))
1809 (insert (format "\n--%s
1810 Content-Type: application/pgp-signature
1811 Content-Transfer-Encoding: 7bit
1814 (insert-buffer-substring pgg-output-buffer)
1815 (goto-char (point-max))
1816 (insert (format "\n--%s--\n" pgp-boundary))
1819 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1821 (defun mime-edit-make-encrypt-recipient-header ()
1822 (let* ((names mime-edit-encrypt-recipient-fields-list)
1824 (std11-field-bodies (cons "From" names)
1825 nil mail-header-separator))
1828 (setq values (cdr values))))
1829 (header (and (stringp from)
1830 (if (string-equal from "")
1832 (format "From: %s\n" from)
1835 (while (and names values)
1836 (let ((name (car names))
1837 (value (car values))
1839 (and (stringp value)
1840 (or (string-equal value "")
1842 (setq header (concat header name ": " value "\n")
1843 recipients (if recipients
1844 (concat recipients " ," value)
1847 (setq names (cdr names)
1848 values (cdr values))
1850 (vector from recipients header)
1853 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1856 (let (from recipients header)
1857 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1858 (setq from (aref ret 0)
1859 recipients (aref ret 1)
1860 header (aref ret 2))
1862 (narrow-to-region beg end)
1864 (mime-edit-translate-region beg end boundary))
1866 (encoding (nth 1 ret))
1867 (pgp-boundary (concat "pgp-" boundary)))
1870 (insert (format "Content-Type: %s\n" ctype))
1872 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1875 (mime-encode-header-in-buffer)
1876 (or (let ((pgg-default-user-id
1877 (or mime-edit-pgp-user-id
1879 (nth 1 (std11-extract-address-components from))
1880 pgg-default-user-id))))
1882 (point-min) (point-max)
1883 (mapcar (lambda (recipient)
1884 (nth 1 (std11-extract-address-components
1886 (split-string recipients
1887 "\\([ \t\n]*,[ \t\n]*\\)+")))
1889 (throw 'mime-edit-error 'pgp-error)
1891 (delete-region (point-min)(point-max))
1893 (insert (format "--[[multipart/encrypted;
1895 protocol=\"application/pgp-encrypted\"][7bit]]
1897 Content-Type: application/pgp-encrypted
1900 Content-Type: application/octet-stream
1901 Content-Transfer-Encoding: 7bit
1903 " pgp-boundary pgp-boundary pgp-boundary))
1904 (insert-buffer-substring pgg-output-buffer)
1905 (goto-char (point-max))
1906 (insert (format "\n--%s--\n" pgp-boundary))
1909 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1912 (narrow-to-region beg end)
1914 (mime-edit-translate-region beg end boundary))
1916 (encoding (nth 1 ret)))
1918 (insert (format "Content-Type: %s\n" ctype))
1920 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1923 (or (pgg-sign-region beg (point-max) 'clearsign)
1924 (throw 'mime-edit-error 'pgp-error)
1928 "--[[application/pgp; format=mime][7bit]]\n")
1932 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1934 (let (recipients header)
1935 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1936 (setq recipients (aref ret 1)
1937 header (aref ret 2))
1940 (narrow-to-region beg end)
1942 (mime-edit-translate-region beg end boundary))
1944 (encoding (nth 1 ret)))
1947 (insert (format "Content-Type: %s\n" ctype))
1949 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1952 (or (pgg-encrypt-region beg (point-max) recipients)
1953 (throw 'mime-edit-error 'pgp-error)
1957 "--[[application/pgp; format=mime][7bit]]\n")
1961 (defun mime-edit-sign-smime (beg end boundary)
1965 (narrow-to-region beg end)
1966 (mime-edit-translate-region beg end boundary)))
1968 (encoding (nth 1 ret))
1969 (smime-boundary (concat "smime-sign-" boundary)))
1971 (insert (format "Content-Type: %s\n" ctype))
1973 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1976 (let (buffer-undo-list)
1977 (goto-char (point-min))
1978 (while (progn (end-of-line) (not (eobp)))
1981 (or (prog1 (smime-sign-region (point-min)(point-max))
1982 (push nil buffer-undo-list)
1983 (ignore-errors (undo)))
1984 (throw 'mime-edit-error 'pgp-error)
1987 (insert (format "--[[multipart/signed;
1988 boundary=\"%s\"; micalg=sha1;
1989 protocol=\"application/pkcs7-signature\"][7bit]]
1991 " smime-boundary smime-boundary))
1992 (goto-char (point-max))
1993 (insert (format "\n--%s
1994 Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
1995 Content-Transfer-Encoding: base64
1996 Content-Disposition: attachment; filename=\"smime.p7s\"
1997 Content-Description: S/MIME Cryptographic Signature
2000 (insert-buffer-substring smime-output-buffer)
2001 (goto-char (point-max))
2002 (insert (format "\n--%s--\n" smime-boundary))
2005 (defun mime-edit-encrypt-smime (beg end boundary)
2009 (narrow-to-region beg end)
2010 (mime-edit-translate-region beg end boundary)))
2012 (encoding (nth 1 ret)))
2014 (insert (format "Content-Type: %s\n" ctype))
2016 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2019 (goto-char (point-min))
2020 (while (progn (end-of-line) (not (eobp)))
2023 (or (smime-encrypt-region (point-min)(point-max))
2024 (throw 'mime-edit-error 'pgp-error)
2026 (delete-region (point-min)(point-max))
2027 (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
2028 Content-Disposition: attachment; filename=\"smime.p7m\"
2029 Content-Description: S/MIME Encrypted Message][base64]]\n")
2030 (insert-buffer-substring smime-output-buffer)
2033 (defsubst replace-space-with-underline (str)
2034 (mapconcat (function
2042 (defun mime-edit-make-boundary ()
2043 (concat mime-multipart-boundary "_"
2044 (replace-space-with-underline (current-time-string))
2047 (defun mime-edit-translate-body ()
2048 "Encode the tagged MIME body in current buffer in MIME compliant message."
2051 (let ((boundary (mime-edit-make-boundary))
2054 (while (mime-edit-process-multipart-1
2055 (format "%s-%d" boundary i))
2059 ;; We are interested in message body.
2062 (goto-char (point-min))
2064 (concat "\n" (regexp-quote mail-header-separator)
2065 (if mime-ignore-preceding-spaces
2066 "[ \t\n]*\n" "\n")) nil 'move)
2070 (goto-char (point-max))
2071 (and mime-ignore-trailing-spaces
2072 (re-search-backward "[^ \t\n]\n" beg t)
2075 (setq ret (mime-edit-translate-region
2077 (format "%s-%d" boundary i)))
2079 (mime-edit-dequote-region (point-min)(point-max))
2080 (let ((contype (car ret)) ;Content-Type
2081 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
2083 ;; Insert User-Agent field
2084 (and mime-edit-insert-user-agent-field
2085 (or (mail-position-on-field "User-Agent")
2086 (insert mime-edit-user-agent-value)
2088 ;; Make primary MIME headers.
2089 (or (mail-position-on-field "MIME-Version")
2090 (insert mime-edit-mime-version-value))
2091 ;; Remove old Content-Type and other fields.
2093 (goto-char (point-min))
2094 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2095 (narrow-to-region (point-min) (point))
2096 (goto-char (point-min))
2097 (mime-delete-field "Content-Type")
2098 (mime-delete-field "Content-Transfer-Encoding"))
2099 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2100 (mail-position-on-field "Content-Type")
2104 (mail-position-on-field "Content-Transfer-Encoding")
2108 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2109 "Translate single-part-tag to MIME header."
2110 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2111 (let* ((beg (match-beginning 0))
2113 (tag (buffer-substring beg end)))
2114 (delete-region beg end)
2115 (let ((contype (mime-edit-get-contype tag))
2116 (encoding (mime-edit-get-encoding tag)))
2117 (insert (concat prefix "--" boundary "\n"))
2119 (narrow-to-region (point)(point))
2120 (insert "Content-Type: " contype "\n")
2122 (insert "Content-Transfer-Encoding: " encoding "\n"))
2123 (mime-encode-header-in-buffer))
2127 (downcase encoding))))
2130 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2132 (setq boundary (mime-edit-make-boundary))
2136 (narrow-to-region beg end)
2137 (let ((tag nil) ;MIME tag
2138 (contype nil) ;Content-Type
2139 (encoding nil) ;Content-Transfer-Encoding
2140 (nparts 0)) ;Number of body parts
2141 ;; Normalize the body part by inserting appropriate message
2142 ;; tags for every message contents.
2143 (mime-edit-normalize-body)
2144 ;; Counting the number of Content-Type.
2145 (goto-char (point-min))
2146 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2147 (setq nparts (1+ nparts)))
2148 ;; Begin translation.
2150 ((and (<= nparts 1)(not multipart))
2151 ;; It's a singular message.
2152 (goto-char (point-min))
2153 (while (re-search-forward
2154 mime-edit-single-part-tag-regexp nil t)
2156 (buffer-substring (match-beginning 0) (match-end 0)))
2157 (delete-region (match-beginning 0) (1+ (match-end 0)))
2158 (setq contype (mime-edit-get-contype tag))
2159 (setq encoding (mime-edit-get-encoding tag))
2162 ;; It's a multipart message.
2163 (goto-char (point-min))
2164 (let ((prio mime-content-transfer-encoding-priority-list)
2166 (when (setq part-info
2167 (mime-edit-translate-single-part-tag boundary))
2168 (and (setq nprio (member (cdr part-info) prio))
2170 (while (setq part-info
2171 (mime-edit-translate-single-part-tag boundary "\n"))
2172 (and (setq nprio (member (cdr part-info) prio))
2173 (setq prio nprio))))
2174 ;; Define Content-Type as "multipart/mixed".
2176 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2177 (setq encoding (car prio))
2178 ;; Insert the trailer.
2179 (goto-char (point-max))
2180 (insert "\n--" boundary "--\n")
2182 (list contype encoding boundary nparts)
2185 (defun mime-edit-normalize-body ()
2186 "Normalize the body part by inserting appropriate message tags."
2187 ;; Insert the first MIME tags if necessary.
2188 (goto-char (point-min))
2189 (if (not (looking-at mime-edit-single-part-tag-regexp))
2190 (insert (mime-make-text-tag) "\n"))
2191 ;; Check each tag, and add new tag or correct it if necessary.
2192 (goto-char (point-min))
2193 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2194 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2195 (contype (mime-edit-get-contype tag))
2196 (charset (mime-get-parameter contype "charset"))
2197 (encoding (mime-edit-get-encoding tag)))
2198 ;; Remove extra whitespaces after the tag.
2199 (if (looking-at "[ \t]+$")
2200 (delete-region (match-beginning 0) (match-end 0)))
2202 (end (mime-edit-content-end))
2204 (if (= end (point-max))
2207 (or (looking-at mime-edit-beginning-tag-regexp)
2209 (insert (mime-make-text-tag) "\n")
2211 (visible-region beg end)
2215 ((mime-test-content-type contype "message")
2216 ;; Content-type "message" should be sent as is.
2219 ((mime-test-content-type contype "text")
2220 ;; Define charset for text if necessary.
2221 (setq charset (if charset
2222 (intern (downcase charset))
2223 (mime-edit-choose-charset)))
2224 (mime-edit-define-charset charset)
2225 (cond ((string-equal contype "text/x-rot13-47-48")
2228 (mule-caesar-region (point) (mime-edit-content-end))
2230 ((string-equal contype "text/enriched")
2235 (end (mime-edit-content-end))
2237 ;; Patch for hard newlines
2240 ;; (while (search-forward "\n" end t)
2241 ;; (put-text-property (match-beginning 0)
2244 ;; End patch for hard newlines
2245 (enriched-encode beg end nil)
2247 (if (search-forward "\n\n")
2248 (delete-region beg (match-end 0))
2251 ;; Point is now on current tag.
2252 ;; Define encoding and encode text if necessary.
2253 (or encoding ;Encoding is not specified.
2256 (let ((ret (cdr (assq charset mime-charset-type-list))))
2258 (setq bits (car ret)
2261 conv "quoted-printable")))
2262 (if (<= bits mime-transfer-level)
2263 (mime-encoding-name bits)
2265 (beg (mime-edit-content-beginning)))
2266 (encode-mime-charset-region beg (mime-edit-content-end)
2268 ;; Protect "From " in beginning of line
2270 (narrow-to-region beg (mime-edit-content-end))
2272 (let (case-fold-search)
2273 (if (re-search-forward "^From " nil t)
2275 (if (memq charset '(iso-2022-jp
2280 (replace-match "\e(BFrom ")
2281 (re-search-forward "^From " nil t)
2283 (setq encoding "quoted-printable")
2285 ;; canonicalize line break code
2286 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2288 (narrow-to-region beg (mime-edit-content-end))
2290 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2291 (replace-match "\\1\r\n"))))
2293 (mime-encode-region beg (mime-edit-content-end)
2294 (or encoding "7bit"))
2295 (mime-edit-define-encoding encoding)
2297 (goto-char (mime-edit-content-end))
2299 ((null encoding) ;Encoding is not specified.
2300 ;; Application, image, audio, video, and any other
2301 ;; unknown content-type without encoding should be
2303 (let* ((encoding "base64") ;Encode in BASE64 by default.
2304 (beg (mime-edit-content-beginning))
2305 (end (mime-edit-content-end)))
2306 (mime-encode-region beg end encoding)
2307 (mime-edit-define-encoding encoding))
2312 (defun mime-delete-field (field)
2313 "Delete header FIELD."
2314 (let ((regexp (format "^%s:[ \t]*" field)))
2315 (goto-char (point-min))
2316 (while (re-search-forward regexp nil t)
2317 (delete-region (match-beginning 0)
2318 (1+ (std11-field-end))))))
2322 ;;; Platform dependent functions
2325 ;; Sun implementations
2327 (defun mime-edit-voice-recorder-for-sun (encoding)
2328 "Record voice in a buffer using Sun audio device,
2329 and insert data encoded as ENCODING."
2330 (message "Start the recording on %s. Type C-g to finish the recording..."
2332 (mime-insert-encoded-file "/dev/audio" encoding)
2336 ;;; @ Other useful commands.
2339 ;; Message forwarding commands as content-type "message/rfc822".
2341 (defun mime-edit-insert-message (&optional message)
2343 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2344 (if (and inserter (fboundp inserter))
2346 (mime-edit-insert-tag "message" "rfc822")
2347 (funcall inserter message)
2349 (message "Sorry, I don't have message inserter for your MUA.")
2352 (defun mime-edit-insert-mail (&optional message)
2354 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2355 (if (and inserter (fboundp inserter))
2357 (mime-edit-insert-tag "message" "rfc822")
2358 (funcall inserter message)
2360 (message "Sorry, I don't have mail inserter for your MUA.")
2363 (defun mime-edit-inserted-message-filter ()
2366 (let ((header-start (point))
2367 (case-fold-search t)
2370 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2371 (if (re-search-forward "^$" (mark t))
2372 (narrow-to-region header-start (match-beginning 0))
2374 (goto-char header-start)
2375 (while (and (re-search-forward
2376 mime-edit-yank-ignored-field-regexp nil t)
2377 (setq beg (match-beginning 0))
2378 (setq end (1+ (std11-field-end)))
2380 (delete-region beg end)
2385 ;;; @ multipart enclosure
2388 (defun mime-edit-enclose-region-internal (type beg end)
2392 (narrow-to-region beg end)
2393 (insert (format "--<<%s>>-{\n" type))
2394 (goto-char (point-max))
2395 (insert (format "--}-<<%s>>\n" type))
2396 (goto-char (point-max))
2398 (or (looking-at mime-edit-beginning-tag-regexp)
2400 (insert (mime-make-text-tag) "\n")
2404 (defun mime-edit-enclose-quote-region (beg end)
2406 (mime-edit-enclose-region-internal 'quote beg end)
2409 (defun mime-edit-enclose-mixed-region (beg end)
2411 (mime-edit-enclose-region-internal 'mixed beg end)
2414 (defun mime-edit-enclose-parallel-region (beg end)
2416 (mime-edit-enclose-region-internal 'parallel beg end)
2419 (defun mime-edit-enclose-digest-region (beg end)
2421 (mime-edit-enclose-region-internal 'digest beg end)
2424 (defun mime-edit-enclose-alternative-region (beg end)
2426 (mime-edit-enclose-region-internal 'alternative beg end)
2429 (defun mime-edit-enclose-pgp-signed-region (beg end)
2431 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2434 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2436 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2439 (defun mime-edit-enclose-kazu-signed-region (beg end)
2441 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2444 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2446 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2449 (defun mime-edit-enclose-smime-signed-region (beg end)
2451 (mime-edit-enclose-region-internal 'smime-signed beg end)
2454 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2456 (mime-edit-enclose-region-internal 'smime-encrypted beg end)
2459 (defun mime-edit-insert-key (&optional arg)
2460 "Insert a pgp public key."
2462 (mime-edit-insert-tag "application" "pgp-keys")
2463 (mime-edit-define-encoding "7bit")
2465 (if (and (not (eobp))
2466 (not (looking-at mime-edit-single-part-tag-regexp)))
2467 (insert (mime-make-text-tag) "\n")))
2473 (defun mime-edit-set-split (arg)
2476 (y-or-n-p "Do you want to enable split? ")
2478 (setq mime-edit-split-message arg)
2480 (message "This message is enabled to split.")
2481 (message "This message is not enabled to split.")
2484 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2485 "Toggle transfer-level is 7bit or 8bit through.
2487 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2489 (if (numberp transfer-level)
2490 (setq mime-transfer-level transfer-level)
2491 (if (< mime-transfer-level 8)
2492 (setq mime-transfer-level 8)
2493 (setq mime-transfer-level 7)
2495 (message (format "Current transfer-level is %d bit"
2496 mime-transfer-level))
2497 (setq mime-transfer-level-string
2498 (mime-encoding-name mime-transfer-level 'not-omit))
2499 (force-mode-line-update)
2502 (defun mime-edit-set-transfer-level-7bit ()
2504 (mime-edit-toggle-transfer-level 7)
2507 (defun mime-edit-set-transfer-level-8bit ()
2509 (mime-edit-toggle-transfer-level 8)
2516 (defvar mime-edit-pgp-processing nil)
2517 (make-variable-buffer-local 'mime-edit-pgp-processing)
2519 (defun mime-edit-set-sign (arg)
2522 (y-or-n-p "Do you want to sign? ")
2526 (or (memq 'sign mime-edit-pgp-processing)
2527 (setq mime-edit-pgp-processing
2528 (nconc mime-edit-pgp-processing
2529 (copy-sequence '(sign)))))
2530 (message "This message will be signed.")
2532 (setq mime-edit-pgp-processing
2533 (delq 'sign mime-edit-pgp-processing))
2534 (message "This message will not be signed.")
2537 (defun mime-edit-set-encrypt (arg)
2540 (y-or-n-p "Do you want to encrypt? ")
2544 (or (memq 'encrypt mime-edit-pgp-processing)
2545 (setq mime-edit-pgp-processing
2546 (nconc mime-edit-pgp-processing
2547 (copy-sequence '(encrypt)))))
2548 (message "This message will be encrypt.")
2550 (setq mime-edit-pgp-processing
2551 (delq 'encrypt mime-edit-pgp-processing))
2552 (message "This message will not be encrypt.")
2555 (defun mime-edit-pgp-enclose-buffer ()
2556 (let ((beg (save-excursion
2557 (goto-char (point-min))
2558 (if (search-forward (concat "\n" mail-header-separator "\n"))
2563 (dolist (pgp-processing mime-edit-pgp-processing)
2564 (case pgp-processing
2566 (mime-edit-enclose-pgp-signed-region
2570 (mime-edit-enclose-pgp-encrypted-region
2579 (defun mime-edit-insert-partial-header (fields subject
2580 id number total separator)
2582 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2583 (insert mime-edit-mime-version-field-for-message/partial)
2585 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2586 id number total separator))
2589 (defun mime-edit-split-and-send
2590 (&optional cmd lines mime-edit-message-max-length)
2594 (count-lines (point-min) (point-max)))
2596 (or mime-edit-message-max-length
2597 (setq mime-edit-message-max-length
2598 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2599 mime-edit-message-default-max-lines))
2601 (let* ((mime-edit-draft-file-name
2602 (or (buffer-file-name)
2604 (expand-file-name "mime-draft" temporary-file-directory))))
2605 (separator mail-header-separator)
2607 (replace-space-with-underline (current-time-string))
2608 "@" (system-name) "\"")))
2609 (run-hooks 'mime-edit-before-split-hook)
2610 (let ((the-buf (current-buffer))
2611 (copy-buf (get-buffer-create " *Original Message*"))
2612 (header (std11-header-string-except
2613 mime-edit-split-ignored-field-regexp separator))
2614 (subject (mail-fetch-field "subject"))
2615 (total (+ (/ lines mime-edit-message-max-length)
2616 (if (> (mod lines mime-edit-message-max-length) 0)
2622 mime-edit-split-message-sender-alist))
2626 (error "Split sender is not specified for `%s'." major-mode)
2629 (mime-edit-partial-number 1)
2632 (set-buffer copy-buf)
2634 (insert-buffer the-buf)
2636 (if (re-search-forward
2637 (concat "^" (regexp-quote separator) "$") nil t)
2638 (let ((he (match-beginning 0)))
2640 (narrow-to-region (point-min) he)
2642 (goto-char (point-min))
2643 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2644 (delete-region (match-beginning 0)
2645 (1+ (std11-field-end)))
2647 (while (< mime-edit-partial-number total)
2650 (set-buffer copy-buf)
2651 (setq data (buffer-substring
2654 (goto-line mime-edit-message-max-length)
2657 (delete-region (point-min)(point))
2659 (mime-edit-insert-partial-header
2660 header subject id mime-edit-partial-number total separator)
2663 (message (format "Sending %d/%d..."
2664 mime-edit-partial-number total))
2665 (call-interactively command)
2666 (message (format "Sending %d/%d... done"
2667 mime-edit-partial-number total))
2669 (setq mime-edit-partial-number
2670 (1+ mime-edit-partial-number))
2674 (set-buffer copy-buf)
2675 (setq data (buffer-string))
2678 (mime-edit-insert-partial-header
2679 header subject id mime-edit-partial-number total separator)
2682 (message (format "Sending %d/%d..."
2683 mime-edit-partial-number total))
2684 (message (format "Sending %d/%d... done"
2685 mime-edit-partial-number total))
2689 (defun mime-edit-maybe-split-and-send (&optional cmd)
2691 (run-hooks 'mime-edit-before-send-hook)
2692 (let ((mime-edit-message-max-length
2693 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2694 mime-edit-message-default-max-lines))
2695 (lines (count-lines (point-min) (point-max)))
2697 (if (and (> lines mime-edit-message-max-length)
2698 mime-edit-split-message)
2699 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2703 ;;; @ preview message
2706 (defvar mime-edit-buffer nil) ; buffer local variable
2708 (defun mime-edit-preview-message ()
2709 "preview editing MIME message."
2711 (let* ((str (buffer-string))
2712 (separator mail-header-separator)
2713 (the-buf (current-buffer))
2714 (buf-name (buffer-name))
2715 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2716 (buf (get-buffer temp-buf-name))
2717 (pgp-processing mime-edit-pgp-processing)
2721 (switch-to-buffer buf)
2724 (setq buf (get-buffer-create temp-buf-name))
2725 (switch-to-buffer buf)
2728 (setq major-mode 'mime-temp-message-mode)
2729 (make-local-variable 'mail-header-separator)
2730 (setq mail-header-separator separator)
2731 (make-local-variable 'mime-edit-buffer)
2732 (setq mime-edit-buffer the-buf)
2733 (setq mime-edit-pgp-processing pgp-processing)
2735 (run-hooks 'mime-edit-translate-hook)
2736 (mime-edit-translate-buffer)
2737 (goto-char (point-min))
2738 (if (re-search-forward
2739 (concat "^" (regexp-quote separator) "$"))
2743 (make-local-variable 'mime-edit-temp-message-buffer)
2744 (setq mime-edit-temp-message-buffer buf)))
2746 (defun mime-edit-quitting-method ()
2747 "Quitting method for mime-view."
2748 (let* ((temp mime-edit-temp-message-buffer)
2750 (mime-preview-kill-buffer)
2752 (setq buf mime-edit-buffer)
2754 (switch-to-buffer buf)))
2756 (set-alist 'mime-preview-quitting-method-alist
2757 'mime-temp-message-mode
2758 #'mime-edit-quitting-method)
2764 (defvar mime-edit-again-ignored-field-regexp
2765 (concat "^\\(" "Content-.*\\|Mime-Version"
2766 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2768 "Regexp for deleted header fields when `mime-edit-again' is called.")
2770 (defsubst eliminate-top-spaces (string)
2771 "Eliminate top sequence of space or tab in STRING."
2772 (if (string-match "^[ \t]+" string)
2773 (substring string (match-end 0))
2776 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2779 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2780 '(("application/pgp-encrypted" . pgp-encrypted)
2781 ("application/pgp-signature" . pgp-signed))))
2782 (mime-content-type-subtype content-type)))
2783 (boundary (mime-content-type-parameter content-type "boundary"))
2784 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2785 (re-search-forward boundary-pat nil t)
2786 (let ((bb (match-beginning 0)) eb tag)
2787 (setq tag (format "\n--<<%s>>-{\n" subtype))
2790 (setq bb (+ bb (length tag)))
2792 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2794 (setq eb (match-beginning 0))
2795 (replace-match (format "--}-<<%s>>\n" subtype))
2797 (narrow-to-region bb eb)
2798 (goto-char (point-min))
2799 (while (re-search-forward boundary-pat nil t)
2800 (let ((beg (match-beginning 0))
2802 (delete-region beg (match-end 0))
2804 (if (re-search-forward boundary-pat nil t)
2805 (setq end (match-beginning 0))
2806 (setq end (point-max))
2809 (narrow-to-region beg end)
2811 ((eq subtype 'pgp-encrypted)
2814 (goto-char (point-min))
2815 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2818 (save-window-excursion
2819 (pgg-decrypt-region (match-beginning 0)
2821 (delete-region (point-min)(point-max))))
2822 (insert-buffer-substring pgg-output-buffer)
2823 (mime-edit-decode-message-in-buffer
2824 nil not-decode-text)
2825 (delete-region (goto-char (point-min))
2826 (if (search-forward "\n\n" nil t)
2829 (goto-char (point-max))
2832 (mime-edit-decode-message-in-buffer
2833 (if (eq subtype 'digest)
2835 (make-mime-content-type 'message 'rfc822))
2838 (goto-char (point-max))
2842 (goto-char (point-min))
2843 (or (= (point-min) 1)
2844 (delete-region (point-min)
2845 (if (search-forward "\n\n" nil t)
2851 (defun mime-edit-decode-single-part-in-buffer
2852 (content-type not-decode-text &optional content-disposition)
2853 (let* ((type (mime-content-type-primary-type content-type))
2854 (subtype (mime-content-type-subtype content-type))
2855 (ctype (format "%s/%s" type subtype))
2857 (pstr (let ((bytes (+ 14 (length ctype))))
2858 (mapconcat (function
2860 (if (string= (car attr) "charset")
2862 (setq charset (cdr attr))
2864 (let* ((str (concat (car attr)
2867 (setq bytes (+ bytes bs 2))
2870 (setq bytes (+ bs 1))
2874 (mime-content-type-parameters content-type) "")))
2877 (limit (save-excursion
2878 (if (search-forward "\n\n" nil t)
2881 (mime-content-disposition-type content-disposition))
2883 (if disposition-type
2884 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2885 (mapconcat (function
2890 (if (string-equal "filename"
2892 (std11-wrap-as-quoted-string
2896 (setq bytes (+ bytes bs 2))
2899 (setq bytes (+ bs 1))
2903 (mime-content-disposition-parameters
2904 content-disposition)
2907 (if disposition-type
2908 (setq pstr (format "%s\nContent-Disposition: %s%s"
2909 pstr disposition-type disposition-str))
2912 (if (re-search-forward
2913 "^Content-Transfer-Encoding:" limit t)
2914 (let ((beg (match-beginning 0))
2915 (hbeg (match-end 0))
2916 (end (std11-field-end limit)))
2919 (eliminate-top-spaces
2920 (std11-unfold-string
2921 (buffer-substring hbeg end)))))
2922 (if (or charset (eq type 'text))
2924 (delete-region beg (1+ end))
2925 (goto-char (point-min))
2926 (if (search-forward "\n\n" nil t)
2929 (match-end 0)(point-max) encoding)
2933 (if (or encoded (not not-decode-text))
2936 (goto-char (point-min))
2937 (while (re-search-forward "\r\n" nil t)
2938 (replace-match "\n")
2940 (decode-mime-charset-region (point-min)(point-max)
2941 (or charset default-mime-charset))
2943 (let ((he (if (re-search-forward "^$" nil t)
2947 (if (and (eq type 'text)
2948 (eq subtype 'x-rot13-47-48))
2949 (mule-caesar-region he (point-max))
2951 (if (= (point-min) 1)
2957 (format "%s/%s%s" type subtype pstr)
2960 (delete-region (point-min) he)
2962 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2968 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2971 (goto-char (point-min))
2972 (let ((ctl (or (mime-read-Content-Type)
2973 default-content-type)))
2975 (let ((type (mime-content-type-primary-type ctl)))
2977 ((and (eq type 'application)
2978 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2979 (delete-region (point-min)(point-max))
2981 ((eq type 'multipart)
2982 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
2985 (mime-edit-decode-single-part-in-buffer
2986 ctl not-decode-text (mime-read-Content-Disposition))
2989 (decode-mime-charset-region (point-min) (point-max)
2990 default-mime-charset))
2992 (if (= (point-min) 1)
2995 (std11-narrow-to-header)
2996 (goto-char (point-min))
2997 (while (re-search-forward
2998 mime-edit-again-ignored-field-regexp nil t)
2999 (delete-region (match-beginning 0) (1+ (std11-field-end)))
3001 (mime-decode-header-in-buffer (not not-decode-text))
3006 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
3007 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
3008 Content-Type and Content-Transfer-Encoding header fields will be
3009 converted to MIME-Edit tags."
3011 (goto-char (point-min))
3013 (concat "\n" (regexp-quote mail-header-separator) "\n")
3015 (replace-match "\n\n")
3017 (mime-edit-decode-message-in-buffer nil not-decode-text)
3018 (goto-char (point-min))
3020 (and (re-search-forward "^$")
3021 (replace-match mail-header-separator)
3031 (provide 'mime-edit)
3033 (run-hooks 'mime-edit-load-hook)
3035 ;;; mime-edit.el ends here