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")
216 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
217 ("directory" "/pub/GNU/elisp/mime")
219 ("mode" "image" "ascii" "local8"))
224 ("mode" "image" "ascii" "local8"))
225 ("tftp" ("site") ("name"))
226 ("afs" ("site") ("name"))
227 ("local-file" ("site") ("name"))
229 ("server" "ftpmail@nic.karrn.ad.jp")
237 ("octet-stream" ("type" "" "tar" "shar"))
242 ("vnd.ms-powerpoint")
243 ("x-kiss" ("x-cnf")))
257 "*Alist of content-type, subtype, parameters and its values.")
259 (defcustom mime-file-types
262 ;; Programming languages
265 "application" "octet-stream" (("type" . "C++"))
267 "attachment" (("filename" . file))
271 "application" "octet-stream" (("type" . "emacs-lisp"))
273 "attachment" (("filename" . file))
277 "application" "octet-stream" (("type" . "common-lisp"))
279 "attachment" (("filename" . file))
283 "application" "octet-stream" (("type" . "perl"))
285 "attachment" (("filename" . file))
288 ;; Text or translated text
293 "inline" (("filename" . file))
296 ;; .rc : procmail modules pm-xxxx.rc
297 ;; *rc : other resource files
299 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
302 "attachment" (("filename" . file))
310 ("\\.diff$\\|\\.patch$"
311 "application" "octet-stream" (("type" . "patch"))
313 "attachment" (("filename" . file))
317 "text" "plain" nil nil nil nil)
320 ;; Octect binary text
323 "application" "msword" nil
325 "attachment" (("filename" . file))
327 ("\\.xls$" ; MS Excel
328 "application" "vnd.ms-excel" nil
330 "attachment" (("filename" . file))
332 ("\\.ppt$" ; MS Power Point
333 "application" "vnd.ms-powerpoint" nil
335 "attachment" (("filename" . file))
341 "inline" (("filename" . file))
344 "application" "postscript" nil
346 "attachment" (("filename" . file))
349 "application" "pdf" nil
351 "attachment" (("filename" . file))
356 ("\\.jpg$\\|\\.jpeg$"
359 "inline" (("filename" . file))
364 "inline" (("filename" . file))
369 "inline" (("filename" . file))
374 "inline" (("filename" . file))
379 "inline" (("filename" . file))
384 "inline" (("filename" . file))
389 "inline" (("filename" . file))
394 "inline" (("filename" . file))
399 "attachment" (("filename" . file))
404 "attachment" (("filename" . file))
407 "application" "octet-stream" (("type" . "tar+gzip"))
409 "attachment" (("filename" . file))
412 "application" "octet-stream" (("type" . "tar+gzip"))
414 "attachment" (("filename" . file))
417 "application" "octet-stream" (("type" . "tar+compress"))
419 "attachment" (("filename" . file))
422 "application" "octet-stream" (("type" . "tar+compress"))
424 "attachment" (("filename" . file))
427 "application" "octet-stream" (("type" . "gzip"))
429 "attachment" (("filename" . file))
432 "application" "octet-stream" (("type" . "compress"))
434 "attachment" (("filename" . file))
437 "application" "octet-stream" (("type" . "lha"))
439 "attachment" (("filename" . file))
442 "application" "zip" nil
444 "attachment" (("filename" . file))
450 "application" "octet-stream" nil
452 "attachment" (("filename" . file)))
454 "*Alist of file name, types, parameters, and default encoding.
455 If encoding is nil, it is determined from its contents."
459 (choice :tag "Primary-Type"
460 ,@(nconc (mapcar (lambda (cell)
461 (list 'item (car cell))
466 (choice :tag "Sub-Type"
469 (mapcar (lambda (cell)
470 (mapcar (lambda (cell)
471 (list 'item (car cell))
477 (repeat :tag "Parameters of Content-Type field"
478 (cons string (choice string symbol)))
479 ;; content-transfer-encoding
480 (choice :tag "Encoding"
483 (mapcar (lambda (cell)
486 (mime-encoding-list))))
488 (choice :tag "Disposition-Type"
494 (repeat :tag "Parameters of Content-Disposition field"
495 (cons string (choice string symbol)))
500 ;;; @@ about charset, encoding and transfer-level
503 (defvar mime-charset-type-list
505 (iso-8859-1 8 "quoted-printable")
506 (iso-8859-2 8 "quoted-printable")
507 (iso-8859-3 8 "quoted-printable")
508 (iso-8859-4 8 "quoted-printable")
509 (iso-8859-5 8 "quoted-printable")
510 (koi8-r 8 "quoted-printable")
511 (iso-8859-7 8 "quoted-printable")
512 (iso-8859-8 8 "quoted-printable")
513 (iso-8859-9 8 "quoted-printable")
514 (iso-8859-14 8 "quoted-printable")
515 (iso-8859-15 8 "quoted-printable")
516 (iso-2022-jp 7 "base64")
517 (iso-2022-jp-3 7 "base64")
518 (iso-2022-kr 7 "base64")
524 (shift_jis 8 "base64")
526 (iso-2022-jp-2 7 "base64")
527 (iso-2022-int-1 7 "base64")
530 (defvar mime-transfer-level 7
531 "*A number of network transfer level. It should be bigger than 7.")
532 (make-variable-buffer-local 'mime-transfer-level)
534 (defsubst mime-encoding-name (transfer-level &optional not-omit)
535 (cond ((> transfer-level 8) "binary")
536 ((= transfer-level 8) "8bit")
540 (defvar mime-transfer-level-string
541 (mime-encoding-name mime-transfer-level 'not-omit)
542 "A string formatted version of mime-transfer-level")
543 (make-variable-buffer-local 'mime-transfer-level-string)
545 ;;; @@ about content transfer encoding
547 (defvar mime-content-transfer-encoding-priority-list
548 '(nil "8bit" "binary"))
550 ;;; @@ about message inserting
553 (defvar mime-edit-yank-ignored-field-list
554 '("Received" "Approved" "Path" "Replied" "Status"
555 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
556 "Delete these fields from original message when it is inserted
557 as message/rfc822 part.
558 Each elements are regexp of field-name.")
560 (defvar mime-edit-yank-ignored-field-regexp
562 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
565 (defvar mime-edit-message-inserter-alist nil)
566 (defvar mime-edit-mail-inserter-alist nil)
569 ;;; @@ about message splitting
572 (defcustom mime-edit-split-message t
573 "*Split large message if it is non-nil."
577 (defcustom mime-edit-message-default-max-lines 1000
578 "*Default maximum lines of a message."
582 (defcustom mime-edit-message-max-lines-alist
583 '((news-reply-mode . 500))
584 "Alist of major-mode vs maximum lines of a message.
585 If it is not specified for a major-mode,
586 `mime-edit-message-default-max-lines' is used."
590 (defconst mime-edit-split-ignored-field-regexp
591 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
593 (defcustom mime-edit-split-blind-field-regexp
594 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
595 "*Regular expression to match field-name to be ignored when split sending."
599 (defvar mime-edit-split-message-sender-alist nil)
601 (defvar mime-edit-news-reply-mode-server-running nil)
607 (defconst mime-edit-single-part-tag-regexp
608 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
609 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
611 (defconst mime-edit-quoted-single-part-tag-regexp
612 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
614 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
616 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
618 (defconst mime-edit-beginning-tag-regexp
619 (regexp-or mime-edit-single-part-tag-regexp
620 mime-edit-multipart-beginning-regexp))
622 (defconst mime-edit-end-tag-regexp
623 (regexp-or mime-edit-single-part-tag-regexp
624 mime-edit-multipart-end-regexp))
626 (defconst mime-edit-tag-regexp
627 (regexp-or mime-edit-single-part-tag-regexp
628 mime-edit-multipart-beginning-regexp
629 mime-edit-multipart-end-regexp))
631 (defvar mime-tag-format "--[[%s]]"
632 "*Control-string making a MIME tag.")
634 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
635 "*Control-string making a MIME tag with encoding.")
638 ;;; @@ multipart boundary
641 (defvar mime-multipart-boundary "Multipart"
642 "*Boundary of a multipart message.")
645 ;;; @@ optional header fields
648 (defvar mime-edit-insert-user-agent-field t
649 "*If non-nil, insert User-Agent header field.")
651 (defvar mime-edit-user-agent-value
652 (concat (mime-product-name mime-user-interface-product)
654 (mapconcat #'number-to-string
655 (mime-product-version mime-user-interface-product) ".")
657 (mime-product-code-name mime-user-interface-product)
659 (mime-product-name mime-library-product)
661 (mapconcat #'number-to-string
662 (mime-product-version mime-library-product) ".")
664 (mime-product-code-name mime-library-product)
666 (if (fboundp 'apel-version)
667 (concat (apel-version) " "))
668 (if (featurep 'xemacs)
669 (concat (cond ((and (featurep 'chise)
670 (boundp 'xemacs-chise-version))
671 (concat "CHISE-MULE/" xemacs-chise-version))
672 ((featurep 'utf-2000)
673 (concat "UTF-2000-MULE/" utf-2000-version))
674 ((featurep 'mule) "MULE"))
676 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
679 (substring emacs-version 0 (match-end 0))
680 (cond ((and (boundp 'xemacs-betaname)
682 ;; It does not exist in XEmacs
683 ;; versions prior to 20.3.
684 (concat " " xemacs-betaname))
685 ((and (boundp 'emacs-patch-level)
687 ;; It does not exist in FSF Emacs or in
688 ;; XEmacs versions earlier than 21.1.1.
689 (format " (patch %d)" emacs-patch-level))
691 " (" xemacs-codename ")"
692 ;; `xemacs-extra-name' has appeared in the
693 ;; development version of XEmacs 21.5-b8.
694 (if (and (boundp 'xemacs-extra-name)
695 (symbol-value 'xemacs-extra-name))
696 (concat " " (symbol-value 'xemacs-extra-name))
699 system-configuration ")")
700 " (" emacs-version ")"))
701 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
702 (substring emacs-version 0 (match-beginning 0))
705 (if (boundp 'enable-multibyte-characters)
707 " (" system-configuration ")"
708 (if enable-multibyte-characters
709 (concat " MULE/" mule-version)
710 " (with unibyte mode)")
711 (if (featurep 'meadow)
712 (let ((mver (Meadow-version)))
713 (if (string-match "^Meadow-" mver)
718 (concat "MULE/" mule-version
719 " (based on Emacs " ver ")"))
720 (concat "Emacs/" ver " (" system-configuration ")")))))
721 "Body of User-Agent field.
722 If variable `mime-edit-insert-user-agent-field' is not nil, it is
723 inserted into message header.")
729 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
730 "*Specify MIME tspecials.
731 Tspecials means any character that matches with it in header must be quoted.")
733 (defconst mime-edit-mime-version-value
734 (concat "1.0 (generated by " mime-edit-version ")")
735 "MIME version number.")
737 (defconst mime-edit-mime-version-field-for-message/partial
738 (concat "MIME-Version:"
739 (mime-encode-field-body
740 (concat " 1.0 (split by " mime-edit-version ")\n")
742 "MIME version field for message/partial.")
745 ;;; @ keymap and menu
748 (defvar mime-edit-mode-flag nil)
749 (make-variable-buffer-local 'mime-edit-mode-flag)
751 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
752 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
753 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
754 "Keymap for MIME-Edit mode commands to insert entity or set status.")
756 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
757 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
758 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
759 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
760 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
761 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
762 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
763 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
764 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
765 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
767 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
768 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
769 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
770 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
771 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
772 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
773 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
774 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
775 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
776 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
778 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
779 "Keymap prefix for MIME-Edit mode commands about enclosure.")
780 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
781 "Keymap for MIME-Edit mode commands about enclosure.")
783 (define-key mime-edit-mode-enclosure-map
784 "\C-a" 'mime-edit-enclose-alternative-region)
785 (define-key mime-edit-mode-enclosure-map
786 "\C-p" 'mime-edit-enclose-parallel-region)
787 (define-key mime-edit-mode-enclosure-map
788 "\C-m" 'mime-edit-enclose-mixed-region)
789 (define-key mime-edit-mode-enclosure-map
790 "\C-d" 'mime-edit-enclose-digest-region)
791 (define-key mime-edit-mode-enclosure-map
792 "\C-s" 'mime-edit-enclose-pgp-signed-region)
793 (define-key mime-edit-mode-enclosure-map
794 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
795 (define-key mime-edit-mode-enclosure-map
796 "\C-q" 'mime-edit-enclose-quote-region)
798 (defvar mime-edit-mode-map (make-sparse-keymap)
799 "Keymap for MIME-Edit mode commands.")
800 (define-key mime-edit-mode-map
801 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
802 (define-key mime-edit-mode-map
803 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
805 (defconst mime-edit-menu-title "MIME-Edit")
807 (defconst mime-edit-menu-list
808 '((mime-help "Describe MIME editor mode" mime-edit-help)
809 (file "Insert File" mime-edit-insert-file)
810 (external "Insert External" mime-edit-insert-external)
811 (voice "Insert Voice" mime-edit-insert-voice)
812 (message "Insert Message" mime-edit-insert-message)
813 (mail "Insert Mail" mime-edit-insert-mail)
814 (signature "Insert Signature" mime-edit-insert-signature)
815 (text "Insert Text" mime-edit-insert-text)
816 (tag "Insert Tag" mime-edit-insert-tag)
817 (alternative "Enclose as alternative"
818 mime-edit-enclose-alternative-region)
819 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
820 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
821 (digest "Enclose as digest" mime-edit-enclose-digest-region)
822 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
823 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
824 (quote "Verbatim region" mime-edit-enclose-quote-region)
825 (key "Insert Public Key" mime-edit-insert-key)
826 (split "Set splitting" mime-edit-set-split)
827 (sign "PGP sign" mime-edit-set-sign)
828 (encrypt "PGP encrypt" mime-edit-set-encrypt)
829 (preview "Preview Message" mime-edit-preview-message)
830 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
832 "MIME-edit menubar entry.")
834 (cond ((featurep 'xemacs)
835 ;; modified by Pekka Marjola <pema@iki.fi>
836 ;; 1995/9/5 (c.f. [tm-en:69])
837 (defun mime-edit-define-menu-for-xemacs ()
838 "Define menu for XEmacs."
839 (cond ((featurep 'menubar)
840 (make-local-variable 'current-menubar)
841 (set-buffer-menubar current-menubar)
844 (cons mime-edit-menu-title
847 (vector (nth 1 item)(nth 2 item)
850 mime-edit-menu-list)))
853 ;; modified by Steven L. Baur <steve@miranova.com>
854 ;; 1995/12/6 (c.f. [tm-en:209])
855 (or (boundp 'mime-edit-popup-menu-for-xemacs)
856 (setq mime-edit-popup-menu-for-xemacs
857 (append '("MIME Commands" "---")
858 (mapcar (function (lambda (item)
862 mime-edit-menu-list)))
865 ((>= emacs-major-version 19)
866 (define-key mime-edit-mode-map [menu-bar mime-edit]
867 (cons mime-edit-menu-title
868 (make-sparse-keymap mime-edit-menu-title)))
871 (define-key mime-edit-mode-map
872 (vector 'menu-bar 'mime-edit (car item))
873 (cons (nth 1 item)(nth 2 item))
876 (reverse mime-edit-menu-list)
884 (defvar mime-edit-touched-flag nil)
887 (defun mime-edit-mode ()
888 "MIME minor mode for editing the tagged MIME message.
890 In this mode, basically, the message is composed in the tagged MIME
891 format. The message tag looks like:
893 --[[text/plain; charset=ISO-2022-JP][7bit]]
895 The tag specifies the MIME content type, subtype, optional parameters
896 and transfer encoding of the message following the tag. Messages
897 without any tag are treated as `text/plain' by default. Charset and
898 transfer encoding are automatically defined unless explicitly
899 specified. Binary messages such as audio and image are usually
900 hidden. The messages in the tagged MIME format are automatically
901 translated into a MIME compliant message when exiting this mode.
903 Available charsets depend on Emacs version being used. The following
904 lists the available charsets of each emacs.
906 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
907 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
908 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
909 CN-BIG5 and ISO-2022-INT-1 are available.
911 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
912 be used to represent multilingual text in intermixed manner. Any
913 languages that has no registered charset are represented as either
914 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
916 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
917 without mule, please set variable `default-mime-charset'. This
918 variable must be symbol of which name is a MIME charset.
920 If you want to add more charsets in mule, please set variable
921 `charsets-mime-charset-alist'. This variable must be alist of which
922 key is list of charset and value is symbol of MIME charset. If name
923 of coding-system is different as MIME charset, please set variable
924 `mime-charset-coding-system-alist'. This variable must be alist of
925 which key is MIME charset and value is coding-system.
927 Following commands are available in addition to major mode commands:
930 \\[mime-edit-insert-text] insert a text message.
931 \\[mime-edit-insert-file] insert a (binary) file.
932 \\[mime-edit-insert-external] insert a reference to external body.
933 \\[mime-edit-insert-voice] insert a voice message.
934 \\[mime-edit-insert-message] insert a mail or news message.
935 \\[mime-edit-insert-mail] insert a mail message.
936 \\[mime-edit-insert-signature] insert a signature file at end.
937 \\[mime-edit-insert-key] insert PGP public key.
938 \\[mime-edit-insert-tag] insert a new MIME tag.
940 \[make enclosure (maybe multipart)\]
941 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
942 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
943 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
944 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
945 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
946 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
947 \\[mime-edit-enclose-quote-region] enclose as verbose mode
948 (to avoid to expand tags)
951 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
952 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
953 \\[mime-edit-set-split] set message splitting mode.
954 \\[mime-edit-set-sign] set PGP-sign mode.
955 \\[mime-edit-set-encrypt] set PGP-encryption mode.
956 \\[mime-edit-preview-message] preview editing MIME message.
957 \\[mime-edit-exit] exit and translate into a MIME
959 \\[mime-edit-help] show this help.
960 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
963 Additional commands are available in some major modes:
964 C-c C-c exit, translate and run the original command.
965 C-c C-s exit, translate and run the original command.
967 The following is a message example written in the tagged MIME format.
968 TABs at the beginning of the line are not a part of the message:
970 This is a conventional plain text. It should be translated
973 This is also a plain text. But, it is explicitly specified as
975 --[[text/plain; charset=ISO-8859-1]]
976 This is also a plain text. But charset is specified as
979 ¡Hola! Buenos días. ¿Cómo está usted?
981 This is a <bold>enriched text</bold>.
982 --[[image/gif][base64]]...image encoded in base64 here...
983 --[[audio/basic][base64]]...audio encoded in base64 here...
985 User customizable variables (not documented all of them):
987 Specifies a key prefix for MIME minor mode commands.
989 mime-ignore-preceding-spaces
990 Preceding white spaces in a message body are ignored if non-nil.
992 mime-ignore-trailing-spaces
993 Trailing white spaces in a message body are ignored if non-nil.
996 Hide a non-textual body message encoded in base64 after insertion
1000 A number of network transfer level. It should be bigger than 7.
1001 If you are in 8bit-through environment, please set 8.
1003 mime-edit-voice-recorder
1004 Specifies a function to record a voice message and encode it.
1005 The function `mime-edit-voice-recorder-for-sun' is for Sun
1009 Turning on MIME mode calls the value of mime-edit-mode-hook, if
1012 mime-edit-translate-hook
1013 The value of mime-edit-translate-hook is called just before translating
1014 the tagged MIME format into a MIME compliant message if it is
1015 non-nil. If the hook call the function mime-edit-insert-signature,
1016 the signature file will be inserted automatically.
1019 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
1022 (if mime-edit-mode-flag
1024 (if mime-edit-touched-flag
1026 (make-local-variable 'mime-edit-touched-flag)
1027 (setq mime-edit-touched-flag t)
1032 (cond ((featurep 'xemacs)
1033 (add-minor-mode 'mime-edit-mode-flag
1034 '((" MIME-Edit " mime-transfer-level-string))
1040 (set-alist 'minor-mode-alist
1041 'mime-edit-mode-flag
1042 '((" MIME-Edit " mime-transfer-level-string)))
1043 (set-alist 'minor-mode-map-alist
1044 'mime-edit-mode-flag
1050 (defun turn-on-mime-edit ()
1051 "Unconditionally turn on MIME-Edit mode."
1053 (if mime-edit-mode-flag
1054 (error "You are already editing a MIME message.")
1055 (setq mime-edit-mode-flag t)
1057 ;; Set transfer level into mode line
1059 (setq mime-transfer-level-string
1060 (mime-encoding-name mime-transfer-level 'not-omit))
1061 (force-mode-line-update)
1063 ;; Define menu for XEmacs.
1064 (if (featurep 'xemacs)
1065 (mime-edit-define-menu-for-xemacs)
1070 ;; I don't care about saving these.
1071 (setq paragraph-start
1072 (regexp-or mime-edit-single-part-tag-regexp
1074 (setq paragraph-separate
1075 (regexp-or mime-edit-single-part-tag-regexp
1076 paragraph-separate))
1077 (run-hooks 'mime-edit-mode-hook)
1080 (substitute-command-keys
1081 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1085 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1088 (defun mime-edit-exit (&optional nomime no-error)
1089 "Translate the tagged MIME message into a MIME compliant message.
1090 With no argument encode a message in the buffer into MIME, otherwise
1091 just return to previous mode."
1093 (if (not mime-edit-mode-flag)
1095 (error "You aren't editing a MIME message.")
1099 (run-hooks 'mime-edit-translate-hook)
1100 (mime-edit-translate-buffer)))
1101 ;; Restore previous state.
1102 (setq mime-edit-mode-flag nil)
1103 (if (and (featurep 'xemacs)
1104 (featurep 'menubar))
1105 (delete-menu-item (list mime-edit-menu-title))
1108 (set-buffer-modified-p (buffer-modified-p))
1109 (run-hooks 'mime-edit-exit-hook)
1110 (message "Exit MIME editor mode.")
1113 (defun mime-edit-maybe-translate ()
1115 (mime-edit-exit nil t)
1116 (call-interactively 'mime-edit-maybe-split-and-send)
1119 (defun mime-edit-help ()
1120 "Show help message about MIME mode."
1122 (with-output-to-temp-buffer "*Help*"
1123 (princ "MIME editor mode:\n")
1124 (princ (documentation 'mime-edit-mode))
1125 (print-help-return-message)))
1127 (defun mime-edit-insert-text (&optional subtype)
1128 "Insert a text message.
1129 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1130 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1132 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1134 (if (looking-at mime-edit-single-part-tag-regexp)
1136 ;; Make a space between the following message.
1140 (if (and (member (cadr ret) '("enriched"))
1141 (fboundp 'enriched-mode))
1143 (if (boundp 'enriched-mode)
1148 (defun mime-edit-insert-file (file &optional verbose)
1149 "Insert a message from a file."
1150 (interactive "fInsert file as MIME message: \nP")
1151 (let* ((guess (mime-find-file-type file))
1152 (type (nth 0 guess))
1153 (subtype (nth 1 guess))
1154 (parameters (nth 2 guess))
1155 (encoding (nth 3 guess))
1156 (disposition-type (nth 4 guess))
1157 (disposition-params (nth 5 guess))
1160 (setq type (mime-prompt-for-type type)
1161 subtype (mime-prompt-for-subtype type subtype)
1163 (if (or (interactive-p) verbose)
1164 (setq encoding (mime-prompt-for-encoding encoding))
1166 (if (or (consp parameters) (stringp disposition-type))
1167 (let ((rest parameters) cell attribute value)
1168 (setq parameters "")
1170 (setq cell (car rest))
1171 (setq attribute (car cell))
1172 (setq value (cdr cell))
1173 (if (eq value 'file)
1174 (setq value (std11-wrap-as-quoted-string
1175 (file-name-nondirectory file)))
1177 (setq parameters (concat parameters "; " attribute "=" value))
1178 (setq rest (cdr rest))
1180 (if disposition-type
1183 (concat parameters "\n"
1184 "Content-Disposition: " disposition-type))
1185 (setq rest disposition-params)
1187 (setq cell (car rest))
1188 (setq attribute (car cell))
1189 (setq value (cdr cell))
1190 (if (eq value 'file)
1191 (setq value (std11-wrap-as-quoted-string
1192 (file-name-nondirectory file)))
1195 (concat parameters "; " attribute "=" value))
1196 (setq rest (cdr rest))
1200 (mime-edit-insert-tag type subtype parameters)
1201 (mime-edit-insert-binary-file file encoding)
1204 (defun mime-edit-insert-external ()
1205 "Insert a reference to external body."
1207 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1209 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1211 (let* ((pritype (mime-prompt-for-type))
1212 (subtype (mime-prompt-for-subtype pritype))
1213 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1216 (insert "Content-Type: "
1217 pritype "/" subtype (or parameters "") "\n")))
1218 (if (and (not (eobp))
1219 (not (looking-at mime-edit-single-part-tag-regexp)))
1220 (insert (mime-make-text-tag) "\n")))
1222 (defun mime-edit-insert-voice ()
1223 "Insert a voice message."
1227 "What transfer encoding: "
1228 (mime-encoding-alist) nil t nil)))
1229 (mime-edit-insert-tag "audio" "basic" nil)
1230 (mime-edit-define-encoding encoding)
1232 (narrow-to-region (1- (point))(point))
1234 (funcall mime-edit-voice-recorder encoding)
1237 (invisible-region (point-min)(point-max))
1238 (goto-char (point-max))
1241 (defun mime-edit-insert-signature (&optional arg)
1242 "Insert a signature file."
1244 (let ((signature-insert-hook
1247 (let ((items (mime-find-file-type signature-file-name)))
1248 (apply (function mime-edit-insert-tag)
1249 (car items) (cadr items) (list (caddr items))))
1252 (insert-signature arg)
1256 ;; Insert a new tag around a point.
1258 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1259 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1260 If nothing is inserted, return nil."
1263 (mime-edit-goto-tag)
1264 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1265 (< (match-beginning 0) p)
1268 (goto-char (match-beginning 0))
1277 (mime-prompt-for-type)))
1280 (mime-prompt-for-subtype pritype)))
1283 (mime-prompt-for-parameters pritype subtype delimiter)))
1284 ;; Make a new MIME tag.
1285 (setq newtag (mime-make-tag pritype subtype parameters))
1286 ;; Find an current MIME tag.
1289 (if (mime-edit-goto-tag)
1290 (buffer-substring (match-beginning 0) (match-end 0))
1291 ;; Assume content type is 'text/plan'.
1292 (mime-make-tag "text" "plain")
1294 ;; We are only interested in TEXT.
1296 (not (mime-test-content-type
1297 (mime-edit-get-contype oldtag) "text")))
1300 (if (or (not oldtag) ;Not text
1301 (or mime-ignore-same-text-tag
1302 (not (string-equal oldtag newtag))))
1304 ;; Mark the beginning of the tag for convenience.
1305 (push-mark (point) 'nomsg)
1306 (insert newtag "\n")
1307 (list pritype subtype parameters) ;New tag is created.
1309 ;; Restore previous point.
1311 nil ;Nothing is created.
1315 (defun mime-edit-insert-binary-file (file &optional encoding)
1316 "Insert binary FILE at point.
1317 Optional argument ENCODING specifies an encoding method such as base64."
1318 (let* ((tagend (1- (point))) ;End of the tag
1319 (hide-p (and mime-auto-hide-body
1322 (let ((en (downcase encoding)))
1323 (or (string-equal en "7bit")
1324 (string-equal en "8bit")
1325 (string-equal en "binary")
1329 (narrow-to-region tagend (point))
1330 (mime-insert-encoded-file file encoding)
1333 (invisible-region (point-min) (point-max))
1334 (goto-char (point-max))
1336 (goto-char (point-max))
1339 (looking-at mime-edit-tag-regexp)
1340 (= (point)(point-max))
1341 (mime-edit-insert-tag "text" "plain")
1343 ;; Define encoding even if it is 7bit.
1344 (if (stringp encoding)
1346 (goto-char tagend) ; Make sure which line the tag is on.
1347 (mime-edit-define-encoding encoding)
1352 ;; Commands work on a current message flagment.
1354 (defun mime-edit-goto-tag ()
1355 "Search for the beginning of the tagged MIME message."
1356 (let ((current (point)))
1357 (if (looking-at mime-edit-tag-regexp)
1359 ;; At first, go to the end.
1360 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1361 (goto-char (1- (match-beginning 0))) ;For multiline tag
1364 (goto-char (point-max))
1366 ;; Then search for the beginning.
1367 (re-search-backward mime-edit-end-tag-regexp nil t)
1368 (or (looking-at mime-edit-beginning-tag-regexp)
1369 ;; Restore previous point.
1376 (defun mime-edit-content-beginning ()
1377 "Return the point of the beginning of content."
1379 (let ((beg (save-excursion
1380 (beginning-of-line) (point))))
1381 (if (mime-edit-goto-tag)
1382 (let ((top (point)))
1383 (goto-char (match-end 0))
1384 (if (and (= beg top)
1385 (= (following-char) ?\^M))
1389 ;; Default text/plain tag.
1390 (goto-char (point-min))
1392 (concat "\n" (regexp-quote mail-header-separator)
1393 (if mime-ignore-preceding-spaces
1394 "[ \t\n]*\n" "\n")) nil 'move)
1398 (defun mime-edit-content-end ()
1399 "Return the point of the end of content."
1401 (if (mime-edit-goto-tag)
1403 (goto-char (match-end 0))
1404 (if (invisible-p (point))
1405 (next-visible-point (point))
1406 ;; Move to the end of this text.
1407 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1408 ;; Don't forget a multiline tag.
1409 (goto-char (match-beginning 0))
1413 ;; Assume the message begins with text/plain.
1414 (goto-char (mime-edit-content-beginning))
1415 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1416 ;; Don't forget a multiline tag.
1417 (goto-char (match-beginning 0)))
1421 (defun mime-edit-define-charset (charset)
1422 "Set charset of current tag to CHARSET."
1424 (if (mime-edit-goto-tag)
1425 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1426 (delete-region (match-beginning 0) (match-end 0))
1429 (mime-edit-set-parameter
1430 (mime-edit-get-contype tag)
1432 (let ((comment (get charset 'mime-charset-comment)))
1434 (concat (upcase (symbol-name charset)) " (" comment ")")
1435 (upcase (symbol-name charset)))))
1436 (mime-edit-get-encoding tag)))
1439 (defun mime-edit-define-encoding (encoding)
1440 "Set encoding of current tag to ENCODING."
1442 (if (mime-edit-goto-tag)
1443 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1444 (delete-region (match-beginning 0) (match-end 0))
1445 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1448 (defun mime-edit-choose-charset ()
1449 "Choose charset of a text following current point."
1450 (detect-mime-charset-region (point) (mime-edit-content-end))
1453 (defun mime-make-text-tag (&optional subtype)
1454 "Make a tag for a text after current point.
1455 Subtype of text type can be specified by an optional argument SUBTYPE.
1456 Otherwise, it is obtained from mime-content-types."
1457 (let* ((pritype "text")
1458 (subtype (or subtype
1459 (car (car (cdr (assoc pritype mime-content-types)))))))
1460 ;; Charset should be defined later.
1461 (mime-make-tag pritype subtype)))
1464 ;; Tag handling functions
1466 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1467 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1468 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1472 (defun mime-create-tag (contype &optional encoding)
1473 "Make a tag with CONTENT-TYPE and optional ENCODING."
1474 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1477 (defun mime-edit-get-contype (tag)
1478 "Return Content-Type (including parameters) of TAG."
1480 (or (string-match mime-edit-single-part-tag-regexp tag)
1481 (string-match mime-edit-multipart-beginning-regexp tag)
1482 (string-match mime-edit-multipart-end-regexp tag)
1484 (substring tag (match-beginning 1) (match-end 1))
1487 (defun mime-edit-get-encoding (tag)
1488 "Return encoding of TAG."
1490 (string-match mime-edit-single-part-tag-regexp tag)
1492 (not (= (match-beginning 3) (match-end 3)))
1493 (substring tag (match-beginning 3) (match-end 3))))
1495 (defun mime-get-parameter (contype parameter)
1496 "For given CONTYPE return value for PARAMETER.
1497 Nil if no such parameter."
1501 (regexp-quote parameter)
1502 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1504 (substring contype (match-beginning 1) (match-end 1))
1505 nil ;No such parameter
1508 (defun mime-edit-set-parameter (contype parameter value)
1509 "For given CONTYPE set PARAMETER to VALUE."
1510 (let (ctype opt-fields)
1511 (if (string-match "\n[^ \t\n\r]+:" contype)
1512 (setq ctype (substring contype 0 (match-beginning 0))
1513 opt-fields (substring contype (match-beginning 0)))
1514 (setq ctype contype)
1519 (regexp-quote parameter)
1520 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1523 (concat (substring ctype 0 (match-beginning 1))
1525 (substring ctype (match-end 1))
1527 (concat ctype "; " parameter "=" value opt-fields)
1530 (defun mime-strip-parameters (contype)
1531 "Return primary content-type and subtype without parameters for CONTYPE."
1532 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1533 (substring contype (match-beginning 1) (match-end 1)) nil))
1535 (defun mime-test-content-type (contype type &optional subtype)
1536 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1537 (and (stringp contype)
1540 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1541 (downcase contype))))
1546 (defun mime-find-file-type (file)
1547 "Guess Content-Type, subtype, and parameters from FILE."
1549 (guesses mime-file-types))
1550 (while (and (not guess) guesses)
1551 (if (string-match (car (car guesses)) file)
1552 (setq guess (cdr (car guesses))))
1553 (setq guesses (cdr guesses)))
1557 (defun mime-prompt-for-type (&optional default)
1558 "Ask for Content-type."
1560 ;; Repeat until primary content type is specified.
1561 (while (string-equal type "")
1563 (completing-read "What content type: "
1566 'require-match ;Type must be specified.
1569 (if (string-equal type "")
1571 (message "Content type is required.")
1578 (defun mime-prompt-for-subtype (type &optional default)
1579 "Ask for subtype of media-type TYPE."
1580 (let ((subtypes (cdr (assoc type mime-content-types))))
1582 (assoc default subtypes))
1583 (setq default (car (car subtypes)))
1589 "What content subtype: (default " default ") ")
1590 "What content subtype: ")
1591 (cdr (assoc type mime-content-types))
1593 'require-match ;Subtype must be specified.
1596 (if (string-equal answer "") default answer)))
1598 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1599 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1600 Optional DELIMITER specifies parameter delimiter (';' by default)."
1601 (let* ((delimiter (or delimiter "; "))
1606 (mime-prompt-for-parameters-1
1608 (cdr (assoc pritype mime-content-types))))))
1611 (if (and (stringp parameters)
1612 (not (string-equal parameters "")))
1613 (concat delimiter parameters)
1614 "" ;"" if no parameters
1617 (defun mime-prompt-for-parameters-1 (optlist)
1618 (apply (function append)
1619 (mapcar (function mime-prompt-for-parameter) optlist)))
1621 (defun mime-prompt-for-parameter (parameter)
1623 Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
1624 (let* ((prompt (car parameter))
1625 (choices (mapcar (function
1627 (if (consp e) e (list e))))
1629 (default (car (car choices)))
1635 (concat "What " prompt
1637 (if (string-equal default "") "\"\"" default)
1639 choices nil nil ""))
1640 ;; If nothing is selected, use default.
1641 (if (string-equal answer "")
1642 (setq answer default)))
1644 (read-string (concat "What " prompt ": "))))
1645 (cons (if (and answer
1646 (not (string-equal answer "")))
1648 ;; Note: control characters ignored!
1649 (if (string-match mime-tspecials-regexp answer)
1650 (concat "\"" answer "\"") answer)))
1651 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1654 (defun mime-prompt-for-encoding (default)
1655 "Ask for Content-Transfer-Encoding."
1660 "What transfer encoding: "
1661 (mime-encoding-alist) nil t default)
1667 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1670 (defvar mime-edit-translate-buffer-hook
1671 '(mime-edit-pgp-enclose-buffer
1672 mime-edit-translate-body
1673 mime-edit-translate-header))
1675 (defun mime-edit-translate-header ()
1676 "Encode the message header into network representation."
1677 (mime-encode-header-in-buffer 'code-conversion)
1678 (run-hooks 'mime-edit-translate-header-hook))
1680 (defun mime-edit-translate-buffer ()
1681 "Encode the tagged MIME message in current buffer in MIME compliant message."
1684 (if (catch 'mime-edit-error
1686 (run-hooks 'mime-edit-translate-buffer-hook)
1690 (error "Translation error!")
1693 (defun mime-edit-find-inmost ()
1694 (goto-char (point-min))
1695 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1696 (let ((bb (match-beginning 0))
1698 (type (buffer-substring (match-beginning 1)(match-end 1)))
1700 (setq end-exp (format "--}-<<%s>>\n" type))
1702 (if (re-search-forward end-exp nil t)
1703 (setq eb (match-beginning 0))
1704 (setq eb (point-max))
1706 (narrow-to-region be eb)
1708 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1710 (narrow-to-region (match-beginning 0)(point-max))
1711 (mime-edit-find-inmost)
1714 (list type bb be eb)
1717 (defun mime-edit-process-multipart-1 (boundary)
1718 (let ((ret (mime-edit-find-inmost)))
1720 (let ((type (car ret))
1721 (bb (nth 1 ret))(be (nth 2 ret))
1724 (narrow-to-region bb eb)
1725 (delete-region bb be)
1726 (setq bb (point-min))
1727 (setq eb (point-max))
1730 (if (looking-at mime-edit-multipart-end-regexp)
1731 (let ((beg (match-beginning 0))
1734 (delete-region beg end)
1735 (or (looking-at mime-edit-beginning-tag-regexp)
1737 (insert (concat (mime-make-text-tag) "\n"))
1739 (cond ((string-equal type "quote")
1740 (mime-edit-enquote-region bb eb)
1742 ((string-equal type "pgp-signed")
1743 (mime-edit-sign-pgp-mime bb eb boundary)
1745 ((string-equal type "pgp-encrypted")
1746 (mime-edit-encrypt-pgp-mime bb eb boundary)
1748 ((string-equal type "kazu-signed")
1749 (mime-edit-sign-pgp-kazu bb eb boundary)
1751 ((string-equal type "kazu-encrypted")
1752 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1754 ((string-equal type "smime-signed")
1755 (mime-edit-sign-smime bb eb boundary)
1757 ((string-equal type "smime-encrypted")
1758 (mime-edit-encrypt-smime bb eb boundary)
1762 (nth 2 (mime-edit-translate-region bb eb
1766 (format "--[[multipart/%s;
1767 boundary=\"%s\"][7bit]]\n"
1772 (defun mime-edit-enquote-region (beg end)
1775 (narrow-to-region beg end)
1777 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1778 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1779 (replace-match (concat "- " (substring tag 1)))
1782 (defun mime-edit-dequote-region (beg end)
1785 (narrow-to-region beg end)
1787 (while (re-search-forward
1788 mime-edit-quoted-single-part-tag-regexp nil t)
1789 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1790 (replace-match (concat "-" (substring tag 2)))
1793 (defvar mime-edit-pgp-user-id nil)
1795 (defun mime-edit-delete-trailing-whitespace ()
1798 (goto-char (point-min))
1799 (while (re-search-forward "[ \t]+$" nil t)
1800 (delete-region (match-beginning 0) (match-end 0))))))
1802 (defun mime-edit-sign-pgp-mime (beg end boundary)
1805 (let* ((from (std11-field-body "From" mail-header-separator))
1807 (narrow-to-region beg end)
1808 (mime-edit-translate-region beg end boundary)))
1810 (encoding (nth 1 ret))
1811 (pgp-boundary (concat "pgp-sign-" boundary))
1813 (mime-edit-delete-trailing-whitespace) ; RFC3156
1815 (insert (format "Content-Type: %s\n" ctype))
1817 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1820 (or (let ((pgg-default-user-id
1821 (or mime-edit-pgp-user-id
1823 (nth 1 (std11-extract-address-components from))
1824 pgg-default-user-id)))
1826 (pgg-sign-region (point-min)(point-max)))
1827 (throw 'mime-edit-error 'pgp-error)
1830 (cdr (assq 'hash-algorithm
1831 (cdar (with-current-buffer pgg-output-buffer
1832 (pgg-parse-armor-region
1833 (point-min)(point-max))))))
1836 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1839 (insert (format "--[[multipart/signed;
1841 protocol=\"application/pgp-signature\"][7bit]]
1843 " pgp-boundary micalg pgp-boundary))
1844 (goto-char (point-max))
1845 (insert (format "\n--%s
1846 Content-Type: application/pgp-signature
1847 Content-Transfer-Encoding: 7bit
1850 (insert-buffer-substring pgg-output-buffer)
1851 (goto-char (point-max))
1852 (insert (format "\n--%s--\n" pgp-boundary))
1855 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1857 (defun mime-edit-make-encrypt-recipient-header ()
1858 (let* ((names mime-edit-encrypt-recipient-fields-list)
1860 (std11-field-bodies (cons "From" names)
1861 nil mail-header-separator))
1864 (setq values (cdr values))))
1865 (header (and (stringp from)
1866 (if (string-equal from "")
1868 (format "From: %s\n" from)
1871 (while (and names values)
1872 (let ((name (car names))
1873 (value (car values))
1875 (and (stringp value)
1876 (or (string-equal value "")
1878 (setq header (concat header name ": " value "\n")
1879 recipients (if recipients
1880 (concat recipients " ," value)
1883 (setq names (cdr names)
1884 values (cdr values))
1886 (vector from recipients header)
1889 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1892 (let (from recipients header)
1893 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1894 (setq from (aref ret 0)
1895 recipients (aref ret 1)
1896 header (aref ret 2))
1898 (narrow-to-region beg end)
1900 (mime-edit-translate-region beg end boundary))
1902 (encoding (nth 1 ret))
1903 (pgp-boundary (concat "pgp-" boundary)))
1906 (insert (format "Content-Type: %s\n" ctype))
1908 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1911 (mime-encode-header-in-buffer)
1912 (or (let ((pgg-default-user-id
1913 (or mime-edit-pgp-user-id
1915 (nth 1 (std11-extract-address-components from))
1916 pgg-default-user-id)))
1919 (point-min) (point-max)
1920 (mapcar (lambda (recipient)
1921 (nth 1 (std11-extract-address-components
1923 (split-string recipients
1924 "\\([ \t\n]*,[ \t\n]*\\)+")))
1926 (throw 'mime-edit-error 'pgp-error)
1928 (delete-region (point-min)(point-max))
1930 (insert (format "--[[multipart/encrypted;
1932 protocol=\"application/pgp-encrypted\"][7bit]]
1934 Content-Type: application/pgp-encrypted
1937 Content-Type: application/octet-stream
1938 Content-Transfer-Encoding: 7bit
1940 " pgp-boundary pgp-boundary pgp-boundary))
1941 (insert-buffer-substring pgg-output-buffer)
1942 (goto-char (point-max))
1943 (insert (format "\n--%s--\n" pgp-boundary))
1946 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1949 (narrow-to-region beg end)
1951 (mime-edit-translate-region beg end boundary))
1953 (encoding (nth 1 ret)))
1955 (insert (format "Content-Type: %s\n" ctype))
1957 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1960 (or (pgg-sign-region beg (point-max) 'clearsign)
1961 (throw 'mime-edit-error 'pgp-error)
1965 "--[[application/pgp; format=mime][7bit]]\n")
1969 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1971 (let (recipients header)
1972 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1973 (setq recipients (aref ret 1)
1974 header (aref ret 2))
1977 (narrow-to-region beg end)
1979 (mime-edit-translate-region beg end boundary))
1981 (encoding (nth 1 ret)))
1984 (insert (format "Content-Type: %s\n" ctype))
1986 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1989 (or (pgg-encrypt-region beg (point-max) recipients)
1990 (throw 'mime-edit-error 'pgp-error)
1994 "--[[application/pgp; format=mime][7bit]]\n")
1998 (defun mime-edit-sign-smime (beg end boundary)
2002 (narrow-to-region beg end)
2003 (mime-edit-translate-region beg end boundary)))
2005 (encoding (nth 1 ret))
2006 (smime-boundary (concat "smime-sign-" boundary)))
2008 (insert (format "Content-Type: %s\n" ctype))
2010 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2013 (let (buffer-undo-list)
2014 (goto-char (point-min))
2015 (while (progn (end-of-line) (not (eobp)))
2018 (or (prog1 (smime-sign-region (point-min)(point-max))
2019 (push nil buffer-undo-list)
2020 (ignore-errors (undo)))
2021 (throw 'mime-edit-error 'pgp-error)
2024 (insert (format "--[[multipart/signed;
2025 boundary=\"%s\"; micalg=sha1;
2026 protocol=\"application/pkcs7-signature\"][7bit]]
2028 " smime-boundary smime-boundary))
2029 (goto-char (point-max))
2030 (insert (format "\n--%s
2031 Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
2032 Content-Transfer-Encoding: base64
2033 Content-Disposition: attachment; filename=\"smime.p7s\"
2034 Content-Description: S/MIME Cryptographic Signature
2037 (insert-buffer-substring smime-output-buffer)
2038 (goto-char (point-max))
2039 (insert (format "\n--%s--\n" smime-boundary))
2042 (defun mime-edit-encrypt-smime (beg end boundary)
2046 (narrow-to-region beg end)
2047 (mime-edit-translate-region beg end boundary)))
2049 (encoding (nth 1 ret)))
2051 (insert (format "Content-Type: %s\n" ctype))
2053 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2056 (goto-char (point-min))
2057 (while (progn (end-of-line) (not (eobp)))
2060 (or (smime-encrypt-region (point-min)(point-max))
2061 (throw 'mime-edit-error 'pgp-error)
2063 (delete-region (point-min)(point-max))
2064 (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
2065 Content-Disposition: attachment; filename=\"smime.p7m\"
2066 Content-Description: S/MIME Encrypted Message][base64]]\n")
2067 (insert-buffer-substring smime-output-buffer)
2070 (defsubst replace-space-with-underline (str)
2071 (mapconcat (function
2079 (defun mime-edit-make-boundary ()
2080 (concat mime-multipart-boundary "_"
2081 (replace-space-with-underline (current-time-string))
2084 (defun mime-edit-translate-body ()
2085 "Encode the tagged MIME body in current buffer in MIME compliant message."
2088 (let ((boundary (mime-edit-make-boundary))
2091 (while (mime-edit-process-multipart-1
2092 (format "%s-%d" boundary i))
2096 ;; We are interested in message body.
2099 (goto-char (point-min))
2101 (concat "\n" (regexp-quote mail-header-separator)
2102 (if mime-ignore-preceding-spaces
2103 "[ \t\n]*\n" "\n")) nil 'move)
2107 (goto-char (point-max))
2108 (and mime-ignore-trailing-spaces
2109 (re-search-backward "[^ \t\n]\n" beg t)
2112 (setq ret (mime-edit-translate-region
2114 (format "%s-%d" boundary i)))
2116 (mime-edit-dequote-region (point-min)(point-max))
2117 (let ((contype (car ret)) ;Content-Type
2118 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
2120 ;; Insert User-Agent field
2121 (and mime-edit-insert-user-agent-field
2122 (or (mail-position-on-field "User-Agent")
2123 (insert mime-edit-user-agent-value)
2125 ;; Make primary MIME headers.
2126 (or (mail-position-on-field "MIME-Version")
2127 (insert mime-edit-mime-version-value))
2128 ;; Remove old Content-Type and other fields.
2130 (goto-char (point-min))
2131 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2132 (narrow-to-region (point-min) (point))
2133 (goto-char (point-min))
2134 (mime-delete-field "Content-Type")
2135 (mime-delete-field "Content-Transfer-Encoding"))
2136 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2137 (mail-position-on-field "Content-Type")
2141 (mail-position-on-field "Content-Transfer-Encoding")
2145 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2146 "Translate single-part-tag to MIME header."
2147 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2148 (let* ((beg (match-beginning 0))
2150 (tag (buffer-substring beg end)))
2151 (delete-region beg end)
2152 (let ((contype (mime-edit-get-contype tag))
2153 (encoding (mime-edit-get-encoding tag)))
2154 (insert (concat prefix "--" boundary "\n"))
2156 (narrow-to-region (point)(point))
2157 (insert "Content-Type: " contype "\n")
2159 (insert "Content-Transfer-Encoding: " encoding "\n"))
2160 (mime-encode-header-in-buffer))
2164 (downcase encoding))))
2167 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2169 (setq boundary (mime-edit-make-boundary))
2173 (narrow-to-region beg end)
2174 (let ((tag nil) ;MIME tag
2175 (contype nil) ;Content-Type
2176 (encoding nil) ;Content-Transfer-Encoding
2177 (nparts 0)) ;Number of body parts
2178 ;; Normalize the body part by inserting appropriate message
2179 ;; tags for every message contents.
2180 (mime-edit-normalize-body)
2181 ;; Counting the number of Content-Type.
2182 (goto-char (point-min))
2183 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2184 (setq nparts (1+ nparts)))
2185 ;; Begin translation.
2187 ((and (<= nparts 1)(not multipart))
2188 ;; It's a singular message.
2189 (goto-char (point-min))
2190 (while (re-search-forward
2191 mime-edit-single-part-tag-regexp nil t)
2193 (buffer-substring (match-beginning 0) (match-end 0)))
2194 (delete-region (match-beginning 0) (1+ (match-end 0)))
2195 (setq contype (mime-edit-get-contype tag))
2196 (setq encoding (mime-edit-get-encoding tag))
2199 ;; It's a multipart message.
2200 (goto-char (point-min))
2201 (let ((prio mime-content-transfer-encoding-priority-list)
2203 (when (setq part-info
2204 (mime-edit-translate-single-part-tag boundary))
2205 (and (setq nprio (member (cdr part-info) prio))
2207 (while (setq part-info
2208 (mime-edit-translate-single-part-tag boundary "\n"))
2209 (and (setq nprio (member (cdr part-info) prio))
2210 (setq prio nprio))))
2211 ;; Define Content-Type as "multipart/mixed".
2213 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2214 (setq encoding (car prio))
2215 ;; Insert the trailer.
2216 (goto-char (point-max))
2217 (insert "\n--" boundary "--\n")
2219 (list contype encoding boundary nparts)
2222 (defun mime-edit-normalize-body ()
2223 "Normalize the body part by inserting appropriate message tags."
2224 ;; Insert the first MIME tags if necessary.
2225 (goto-char (point-min))
2226 (if (not (looking-at mime-edit-single-part-tag-regexp))
2227 (insert (mime-make-text-tag) "\n"))
2228 ;; Check each tag, and add new tag or correct it if necessary.
2229 (goto-char (point-min))
2230 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2231 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2232 (contype (mime-edit-get-contype tag))
2233 (charset (mime-get-parameter contype "charset"))
2234 (encoding (mime-edit-get-encoding tag)))
2235 ;; Remove extra whitespaces after the tag.
2236 (if (looking-at "[ \t]+$")
2237 (delete-region (match-beginning 0) (match-end 0)))
2239 (end (mime-edit-content-end))
2241 (if (= end (point-max))
2244 (or (looking-at mime-edit-beginning-tag-regexp)
2246 (insert (mime-make-text-tag) "\n")
2248 (visible-region beg end)
2252 ((mime-test-content-type contype "message")
2253 ;; Content-type "message" should be sent as is.
2256 ((mime-test-content-type contype "text")
2257 ;; Define charset for text if necessary.
2258 (setq charset (if charset
2259 (intern (downcase charset))
2260 (mime-edit-choose-charset)))
2261 (mime-edit-define-charset charset)
2262 (cond ((string-equal contype "text/x-rot13-47-48")
2265 (mule-caesar-region (point) (mime-edit-content-end))
2267 ((string-equal contype "text/enriched")
2272 (end (mime-edit-content-end))
2274 ;; Patch for hard newlines
2277 ;; (while (search-forward "\n" end t)
2278 ;; (put-text-property (match-beginning 0)
2281 ;; End patch for hard newlines
2282 (enriched-encode beg end nil)
2284 (if (search-forward "\n\n")
2285 (delete-region beg (match-end 0))
2288 ;; Point is now on current tag.
2289 ;; Define encoding and encode text if necessary.
2290 (or encoding ;Encoding is not specified.
2293 (let ((ret (cdr (assq charset mime-charset-type-list))))
2295 (setq bits (car ret)
2298 conv "quoted-printable")))
2299 (if (<= bits mime-transfer-level)
2300 (mime-encoding-name bits)
2302 (beg (mime-edit-content-beginning)))
2303 (encode-mime-charset-region beg (mime-edit-content-end)
2305 ;; Protect "From " in beginning of line
2307 (narrow-to-region beg (mime-edit-content-end))
2309 (let (case-fold-search)
2310 (if (re-search-forward "^From " nil t)
2312 (if (memq charset '(iso-2022-jp
2317 (replace-match "\e(BFrom ")
2318 (re-search-forward "^From " nil t)
2320 (setq encoding "quoted-printable")
2322 ;; canonicalize line break code
2323 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2325 (narrow-to-region beg (mime-edit-content-end))
2327 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2328 ;; In a certain period, `replace-match' with "\\N"
2329 ;; converted 8-bit characters into multibyte string,
2330 ;; but it has been fixed at 2004-01-15.
2331 ;;(replace-match "\\1\r\n"))))
2336 (mime-encode-region beg (mime-edit-content-end)
2337 (or encoding "7bit"))
2338 (mime-edit-define-encoding encoding)
2340 (goto-char (mime-edit-content-end))
2342 ((null encoding) ;Encoding is not specified.
2343 ;; Application, image, audio, video, and any other
2344 ;; unknown content-type without encoding should be
2346 (let* ((encoding "base64") ;Encode in BASE64 by default.
2347 (beg (mime-edit-content-beginning))
2348 (end (mime-edit-content-end)))
2349 (mime-encode-region beg end encoding)
2350 (mime-edit-define-encoding encoding))
2355 (defun mime-delete-field (field)
2356 "Delete header FIELD."
2357 (let ((regexp (format "^%s:[ \t]*" field)))
2358 (goto-char (point-min))
2359 (while (re-search-forward regexp nil t)
2360 (delete-region (match-beginning 0)
2361 (1+ (std11-field-end))))))
2365 ;;; Platform dependent functions
2368 ;; Sun implementations
2370 (defun mime-edit-voice-recorder-for-sun (encoding)
2371 "Record voice in a buffer using Sun audio device,
2372 and insert data encoded as ENCODING."
2373 (message "Start the recording on %s. Type C-g to finish the recording..."
2375 (mime-insert-encoded-file "/dev/audio" encoding)
2379 ;;; @ Other useful commands.
2382 ;; Message forwarding commands as content-type "message/rfc822".
2384 (defun mime-edit-insert-message (&optional message)
2386 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2387 (if (and inserter (fboundp inserter))
2389 (mime-edit-insert-tag "message" "rfc822")
2390 (funcall inserter message)
2392 (message "Sorry, I don't have message inserter for your MUA.")
2395 (defun mime-edit-insert-mail (&optional message)
2397 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2398 (if (and inserter (fboundp inserter))
2400 (mime-edit-insert-tag "message" "rfc822")
2401 (funcall inserter message)
2403 (message "Sorry, I don't have mail inserter for your MUA.")
2406 (defun mime-edit-inserted-message-filter ()
2409 (let ((header-start (point))
2410 (case-fold-search t)
2413 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2414 (if (re-search-forward "^$" (mark t))
2415 (narrow-to-region header-start (match-beginning 0))
2417 (goto-char header-start)
2418 (while (and (re-search-forward
2419 mime-edit-yank-ignored-field-regexp nil t)
2420 (setq beg (match-beginning 0))
2421 (setq end (1+ (std11-field-end)))
2423 (delete-region beg end)
2428 ;;; @ multipart enclosure
2431 (defun mime-edit-enclose-region-internal (type beg end)
2435 (narrow-to-region beg end)
2436 (insert (format "--<<%s>>-{\n" type))
2437 (goto-char (point-max))
2438 (insert (format "--}-<<%s>>\n" type))
2439 (goto-char (point-max))
2441 (or (looking-at mime-edit-beginning-tag-regexp)
2443 (insert (mime-make-text-tag) "\n")
2447 (defun mime-edit-enclose-quote-region (beg end)
2449 (mime-edit-enclose-region-internal 'quote beg end)
2452 (defun mime-edit-enclose-mixed-region (beg end)
2454 (mime-edit-enclose-region-internal 'mixed beg end)
2457 (defun mime-edit-enclose-parallel-region (beg end)
2459 (mime-edit-enclose-region-internal 'parallel beg end)
2462 (defun mime-edit-enclose-digest-region (beg end)
2464 (mime-edit-enclose-region-internal 'digest beg end)
2467 (defun mime-edit-enclose-alternative-region (beg end)
2469 (mime-edit-enclose-region-internal 'alternative beg end)
2472 (defun mime-edit-enclose-pgp-signed-region (beg end)
2474 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2477 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2479 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2482 (defun mime-edit-enclose-kazu-signed-region (beg end)
2484 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2487 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2489 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2492 (defun mime-edit-enclose-smime-signed-region (beg end)
2494 (mime-edit-enclose-region-internal 'smime-signed beg end)
2497 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2499 (mime-edit-enclose-region-internal 'smime-encrypted beg end)
2502 (defun mime-edit-insert-key (&optional arg)
2503 "Insert a pgp public key."
2505 (mime-edit-insert-tag "application" "pgp-keys")
2506 (mime-edit-define-encoding "7bit")
2508 (if (and (not (eobp))
2509 (not (looking-at mime-edit-single-part-tag-regexp)))
2510 (insert (mime-make-text-tag) "\n")))
2516 (defun mime-edit-set-split (arg)
2519 (y-or-n-p "Do you want to enable split? ")
2521 (setq mime-edit-split-message arg)
2523 (message "This message is enabled to split.")
2524 (message "This message is not enabled to split.")
2527 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2528 "Toggle transfer-level is 7bit or 8bit through.
2530 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2532 (if (numberp transfer-level)
2533 (setq mime-transfer-level transfer-level)
2534 (if (< mime-transfer-level 8)
2535 (setq mime-transfer-level 8)
2536 (setq mime-transfer-level 7)
2538 (message (format "Current transfer-level is %d bit"
2539 mime-transfer-level))
2540 (setq mime-transfer-level-string
2541 (mime-encoding-name mime-transfer-level 'not-omit))
2542 (force-mode-line-update)
2545 (defun mime-edit-set-transfer-level-7bit ()
2547 (mime-edit-toggle-transfer-level 7)
2550 (defun mime-edit-set-transfer-level-8bit ()
2552 (mime-edit-toggle-transfer-level 8)
2559 (defvar mime-edit-pgp-processing nil)
2560 (make-variable-buffer-local 'mime-edit-pgp-processing)
2562 (defun mime-edit-set-sign (arg)
2565 (y-or-n-p "Do you want to sign? ")
2569 (or (memq 'sign mime-edit-pgp-processing)
2570 (setq mime-edit-pgp-processing
2571 (nconc mime-edit-pgp-processing
2572 (copy-sequence '(sign)))))
2573 (message "This message will be signed.")
2575 (setq mime-edit-pgp-processing
2576 (delq 'sign mime-edit-pgp-processing))
2577 (message "This message will not be signed.")
2580 (defun mime-edit-set-encrypt (arg)
2583 (y-or-n-p "Do you want to encrypt? ")
2587 (or (memq 'encrypt mime-edit-pgp-processing)
2588 (setq mime-edit-pgp-processing
2589 (nconc mime-edit-pgp-processing
2590 (copy-sequence '(encrypt)))))
2591 (message "This message will be encrypt.")
2593 (setq mime-edit-pgp-processing
2594 (delq 'encrypt mime-edit-pgp-processing))
2595 (message "This message will not be encrypt.")
2598 (defun mime-edit-pgp-enclose-buffer ()
2599 (let ((beg (save-excursion
2600 (goto-char (point-min))
2601 (if (search-forward (concat "\n" mail-header-separator "\n"))
2606 (dolist (pgp-processing mime-edit-pgp-processing)
2607 (case pgp-processing
2609 (mime-edit-enclose-pgp-signed-region
2613 (mime-edit-enclose-pgp-encrypted-region
2622 (defun mime-edit-insert-partial-header (fields subject
2623 id number total separator)
2625 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2626 (insert mime-edit-mime-version-field-for-message/partial)
2628 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2629 id number total separator))
2632 (defun mime-edit-split-and-send
2633 (&optional cmd lines mime-edit-message-max-length)
2637 (count-lines (point-min) (point-max)))
2639 (or mime-edit-message-max-length
2640 (setq mime-edit-message-max-length
2641 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2642 mime-edit-message-default-max-lines))
2644 (let ((separator mail-header-separator)
2646 (replace-space-with-underline (current-time-string))
2647 "@" (system-name) "\"")))
2648 (run-hooks 'mime-edit-before-split-hook)
2649 (let ((the-buf (current-buffer))
2650 (copy-buf (get-buffer-create " *Original Message*"))
2651 (header (std11-header-string-except
2652 mime-edit-split-ignored-field-regexp separator))
2653 (subject (mail-fetch-field "subject"))
2654 (total (+ (/ lines mime-edit-message-max-length)
2655 (if (> (mod lines mime-edit-message-max-length) 0)
2661 mime-edit-split-message-sender-alist))
2665 (error "Split sender is not specified for `%s'." major-mode)
2668 (mime-edit-partial-number 1)
2671 (set-buffer copy-buf)
2673 (insert-buffer the-buf)
2675 (if (re-search-forward
2676 (concat "^" (regexp-quote separator) "$") nil t)
2677 (let ((he (match-beginning 0)))
2679 (narrow-to-region (point-min) he)
2681 (goto-char (point-min))
2682 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2683 (delete-region (match-beginning 0)
2684 (1+ (std11-field-end)))
2686 (while (< mime-edit-partial-number total)
2689 (set-buffer copy-buf)
2690 (setq data (buffer-substring
2693 (goto-line mime-edit-message-max-length)
2696 (delete-region (point-min)(point))
2698 (mime-edit-insert-partial-header
2699 header subject id mime-edit-partial-number total separator)
2702 (message (format "Sending %d/%d..."
2703 mime-edit-partial-number total))
2704 (call-interactively command)
2705 (message (format "Sending %d/%d...done"
2706 mime-edit-partial-number total))
2708 (setq mime-edit-partial-number
2709 (1+ mime-edit-partial-number))
2713 (set-buffer copy-buf)
2714 (setq data (buffer-string))
2717 (mime-edit-insert-partial-header
2718 header subject id mime-edit-partial-number total separator)
2721 (message (format "Sending %d/%d..."
2722 mime-edit-partial-number total))
2723 (message (format "Sending %d/%d...done"
2724 mime-edit-partial-number total))
2728 (defun mime-edit-maybe-split-and-send (&optional cmd)
2730 (run-hooks 'mime-edit-before-send-hook)
2731 (let ((mime-edit-message-max-length
2732 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2733 mime-edit-message-default-max-lines))
2734 (lines (count-lines (point-min) (point-max)))
2736 (if (and (> lines mime-edit-message-max-length)
2737 mime-edit-split-message)
2738 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2742 ;;; @ preview message
2745 (defvar mime-edit-buffer nil) ; buffer local variable
2746 (defvar mime-edit-temp-message-buffer nil) ; buffer local variable
2748 (defun mime-edit-preview-message ()
2749 "preview editing MIME message."
2751 (let* ((str (buffer-string))
2752 (separator mail-header-separator)
2753 (the-buf (current-buffer))
2754 (buf-name (buffer-name))
2755 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2756 (buf (get-buffer temp-buf-name))
2757 (pgp-processing mime-edit-pgp-processing)
2761 (switch-to-buffer buf)
2764 (setq buf (get-buffer-create temp-buf-name))
2765 (switch-to-buffer buf)
2768 (setq major-mode 'mime-temp-message-mode)
2769 (make-local-variable 'mail-header-separator)
2770 (setq mail-header-separator separator)
2771 (make-local-variable 'mime-edit-buffer)
2772 (setq mime-edit-buffer the-buf)
2773 (setq mime-edit-pgp-processing pgp-processing)
2775 (run-hooks 'mime-edit-translate-hook)
2776 (mime-edit-translate-buffer)
2777 (goto-char (point-min))
2778 (if (re-search-forward
2779 (concat "^" (regexp-quote separator) "$"))
2783 (make-local-variable 'mime-edit-temp-message-buffer)
2784 (setq mime-edit-temp-message-buffer buf)))
2786 (defun mime-edit-quitting-method ()
2787 "Quitting method for mime-view."
2788 (let* ((temp mime-edit-temp-message-buffer)
2790 (mime-preview-kill-buffer)
2792 (setq buf mime-edit-buffer)
2794 (switch-to-buffer buf)))
2796 (set-alist 'mime-preview-quitting-method-alist
2797 'mime-temp-message-mode
2798 #'mime-edit-quitting-method)
2804 (defvar mime-edit-again-ignored-field-regexp
2805 (concat "^\\(" "Content-.*\\|Mime-Version"
2806 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2808 "Regexp for deleted header fields when `mime-edit-again' is called.")
2810 (defsubst eliminate-top-spaces (string)
2811 "Eliminate top sequence of space or tab in STRING."
2812 (if (string-match "^[ \t]+" string)
2813 (substring string (match-end 0))
2816 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2819 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2820 '(("application/pgp-encrypted" . pgp-encrypted)
2821 ("application/pgp-signature" . pgp-signed))))
2822 (mime-content-type-subtype content-type)))
2823 (boundary (mime-content-type-parameter content-type "boundary"))
2824 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2825 (re-search-forward boundary-pat nil t)
2826 (let ((bb (match-beginning 0)) eb tag)
2827 (setq tag (format "\n--<<%s>>-{\n" subtype))
2830 (setq bb (+ bb (length tag)))
2832 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2834 (setq eb (match-beginning 0))
2835 (replace-match (format "--}-<<%s>>\n" subtype))
2837 (narrow-to-region bb eb)
2838 (goto-char (point-min))
2839 (while (re-search-forward boundary-pat nil t)
2840 (let ((beg (match-beginning 0))
2842 (delete-region beg (match-end 0))
2844 (if (re-search-forward boundary-pat nil t)
2845 (setq end (match-beginning 0))
2846 (setq end (point-max))
2849 (narrow-to-region beg end)
2851 ((eq subtype 'pgp-encrypted)
2854 (goto-char (point-min))
2855 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2858 (save-window-excursion
2859 (pgg-decrypt-region (match-beginning 0)
2861 (delete-region (point-min)(point-max))))
2862 (insert-buffer-substring pgg-output-buffer)
2863 (mime-edit-decode-message-in-buffer
2864 nil not-decode-text)
2865 (delete-region (goto-char (point-min))
2866 (if (search-forward "\n\n" nil t)
2869 (goto-char (point-max))
2872 (mime-edit-decode-message-in-buffer
2873 (if (eq subtype 'digest)
2875 (make-mime-content-type 'message 'rfc822))
2878 (goto-char (point-max))
2882 (goto-char (point-min))
2883 (or (= (point-min) 1)
2884 (delete-region (point-min)
2885 (if (search-forward "\n\n" nil t)
2891 (defun mime-edit-decode-single-part-in-buffer
2892 (content-type not-decode-text &optional content-disposition)
2893 (let* ((type (mime-content-type-primary-type content-type))
2894 (subtype (mime-content-type-subtype content-type))
2895 (ctype (format "%s/%s" type subtype))
2897 (pstr (let ((bytes (+ 14 (length ctype))))
2898 (mapconcat (function
2900 (if (string= (car attr) "charset")
2902 (setq charset (cdr attr))
2904 (let* ((str (concat (car attr)
2907 (setq bytes (+ bytes bs 2))
2910 (setq bytes (+ bs 1))
2914 (mime-content-type-parameters content-type) "")))
2917 (limit (save-excursion
2918 (if (search-forward "\n\n" nil t)
2921 (mime-content-disposition-type content-disposition))
2923 (if disposition-type
2924 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2925 (mapconcat (function
2930 (if (string-equal "filename"
2932 (std11-wrap-as-quoted-string
2936 (setq bytes (+ bytes bs 2))
2939 (setq bytes (+ bs 1))
2943 (mime-content-disposition-parameters
2944 content-disposition)
2947 (if disposition-type
2948 (setq pstr (format "%s\nContent-Disposition: %s%s"
2949 pstr disposition-type disposition-str))
2952 (if (re-search-forward
2953 "^Content-Transfer-Encoding:" limit t)
2954 (let ((beg (match-beginning 0))
2955 (hbeg (match-end 0))
2956 (end (std11-field-end limit)))
2959 (eliminate-top-spaces
2960 (std11-unfold-string
2961 (buffer-substring hbeg end)))))
2962 (if (or charset (eq type 'text))
2964 (delete-region beg (1+ end))
2965 (goto-char (point-min))
2966 (if (search-forward "\n\n" nil t)
2969 (match-end 0)(point-max) encoding)
2973 (if (and (eq type 'text)
2974 (or encoded (not not-decode-text)))
2977 (goto-char (point-min))
2978 (while (re-search-forward "\r\n" nil t)
2979 (replace-match "\n")
2981 (decode-mime-charset-region (point-min)(point-max)
2982 (or charset default-mime-charset))
2984 (let ((he (if (re-search-forward "^$" nil t)
2988 (if (and (eq type 'text)
2989 (eq subtype 'x-rot13-47-48))
2990 (mule-caesar-region he (point-max))
2992 (if (= (point-min) 1)
2998 (format "%s/%s%s" type subtype pstr)
3001 (delete-region (point-min) he)
3003 (mime-create-tag (format "%s/%s%s" type subtype pstr)
3009 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
3012 (goto-char (point-min))
3013 (let ((ctl (or (mime-read-Content-Type)
3014 default-content-type)))
3016 (let ((type (mime-content-type-primary-type ctl)))
3018 ((and (eq type 'application)
3019 (eq (mime-content-type-subtype ctl) 'pgp-signature))
3020 (delete-region (point-min)(point-max))
3022 ((eq type 'multipart)
3023 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
3026 (mime-edit-decode-single-part-in-buffer
3027 ctl not-decode-text (mime-read-Content-Disposition))
3030 (decode-mime-charset-region (point-min) (point-max)
3031 default-mime-charset))
3033 (if (= (point-min) 1)
3036 (std11-narrow-to-header)
3037 (goto-char (point-min))
3038 (while (re-search-forward
3039 mime-edit-again-ignored-field-regexp nil t)
3040 (delete-region (match-beginning 0) (1+ (std11-field-end)))
3042 (mime-decode-header-in-buffer (not not-decode-text))
3047 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
3048 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
3049 Content-Type and Content-Transfer-Encoding header fields will be
3050 converted to MIME-Edit tags."
3052 (goto-char (point-min))
3054 (concat "\n" (regexp-quote mail-header-separator) "\n")
3056 (replace-match "\n\n")
3058 (mime-edit-decode-message-in-buffer nil not-decode-text)
3059 (goto-char (point-min))
3061 (and (re-search-forward "^$")
3062 (replace-match mail-header-separator)
3072 (provide 'mime-edit)
3074 (run-hooks 'mime-edit-load-hook)
3076 ;;; mime-edit.el ends here