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., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
32 ;; This is an Emacs minor mode for editing Internet multimedia
33 ;; messages formatted in MIME (RFC 2045, 2046, 2047, 2048 and 2049).
34 ;; All messages in this mode are composed in the tagged MIME format,
35 ;; that are described in the following examples. The messages
36 ;; composed in the tagged MIME format are automatically translated
37 ;; into a MIME compliant message when exiting the mode.
39 ;; Mule (multilingual feature of Emacs 20 and multilingual extension
40 ;; for XEmacs 20) has a capability of handling multilingual text in
41 ;; limited ISO-2022 manner that is based on early experiences in
42 ;; Japanese Internet community and resulted in RFC 1468 (ISO-2022-JP
43 ;; charset for MIME). In order to enable multilingual capability in
44 ;; single text message in MIME, charset of multilingual text written
45 ;; in Mule is declared as either `ISO-2022-JP-2' [RFC 1554]. Mule is
46 ;; required for reading the such messages.
48 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
49 ;; News mode. First of all, you need the following autoload
50 ;; definition to load mime-edit-mode automatically:
52 ;; (autoload 'turn-on-mime-edit "mime-edit"
53 ;; "Minor mode for editing MIME message." t)
55 ;; In case of Mail mode (includes VM mode), you need the following
58 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
59 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
61 ;; In case of MH-E, you need the following hook definition:
63 ;; (add-hook 'mh-letter-mode-hook
66 ;; (turn-on-mime-edit)
67 ;; (make-local-variable 'mail-header-separator)
68 ;; (setq mail-header-separator "--------")
70 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
72 ;; In case of News mode, you need the following hook definition:
74 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
75 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
77 ;; In case of Emacs 19, it is possible to emphasize the message tags
78 ;; using font-lock mode as follows:
80 ;; (add-hook 'mime-edit-mode-hook
84 ;; (setq font-lock-keywords (list mime-edit-tag-regexp))
87 ;; The message tag looks like:
89 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
91 ;; The tagged MIME message examples:
93 ;; This is a conventional plain text. It should be translated into
97 ;; This is also a plain text. But, it is explicitly specified as is.
98 ;;--[[text/plain; charset=ISO-8859-1]]
99 ;; This is also a plain text. But charset is specified as iso-8859-1.
101 ;; ¡Hola! Buenos días. ¿Cómo está usted?
102 ;;--[[text/enriched]]
103 ;; <center>This is a richtext.</center>
105 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
107 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
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"))
239 ("vnd.ms-powerpoint")
240 ("x-kiss" ("x-cnf")))
254 "*Alist of content-type, subtype, parameters and its values.")
256 (defcustom mime-file-types
259 ;; Programming languages
262 "application" "octet-stream" (("type" . "C++"))
264 "attachment" (("filename" . file))
268 "application" "octet-stream" (("type" . "emacs-lisp"))
270 "attachment" (("filename" . file))
274 "application" "octet-stream" (("type" . "common-lisp"))
276 "attachment" (("filename" . file))
280 "application" "octet-stream" (("type" . "perl"))
282 "attachment" (("filename" . file))
285 ;; Text or translated text
290 "inline" (("filename" . file))
293 ;; .rc : procmail modules pm-xxxx.rc
294 ;; *rc : other resource files
296 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
299 "attachment" (("filename" . file))
307 ("\\.diff$\\|\\.patch$"
308 "application" "octet-stream" (("type" . "patch"))
310 "attachment" (("filename" . file))
314 "text" "plain" nil nil nil nil)
317 ;; Octect binary text
320 "application" "msword" nil
322 "attachment" (("filename" . file))
324 ("\\.ppt$" ; MS Power Point
325 "application" "vnd.ms-powerpoint" nil
327 "attachment" (("filename" . file))
333 "inline" (("filename" . file))
336 "application" "postscript" nil
338 "attachment" (("filename" . file))
343 ("\\.jpg$\\|\\.jpeg$"
346 "inline" (("filename" . file))
351 "inline" (("filename" . file))
356 "inline" (("filename" . file))
361 "inline" (("filename" . file))
366 "inline" (("filename" . file))
371 "inline" (("filename" . file))
376 "inline" (("filename" . file))
381 "inline" (("filename" . file))
386 "attachment" (("filename" . file))
391 "attachment" (("filename" . file))
394 "application" "octet-stream" (("type" . "tar+gzip"))
396 "attachment" (("filename" . file))
399 "application" "octet-stream" (("type" . "tar+gzip"))
401 "attachment" (("filename" . file))
404 "application" "octet-stream" (("type" . "tar+compress"))
406 "attachment" (("filename" . file))
409 "application" "octet-stream" (("type" . "tar+compress"))
411 "attachment" (("filename" . file))
414 "application" "octet-stream" (("type" . "gzip"))
416 "attachment" (("filename" . file))
419 "application" "octet-stream" (("type" . "compress"))
421 "attachment" (("filename" . file))
424 "application" "octet-stream" (("type" . "lha"))
426 "attachment" (("filename" . file))
429 "application" "zip" nil
431 "attachment" (("filename" . file))
437 "application" "octet-stream" nil
439 "attachment" (("filename" . file)))
441 "*Alist of file name, types, parameters, and default encoding.
442 If encoding is nil, it is determined from its contents."
446 (choice :tag "Primary-Type"
447 ,@(nconc (mapcar (lambda (cell)
448 (list 'item (car cell))
453 (choice :tag "Sub-Type"
456 (mapcar (lambda (cell)
457 (mapcar (lambda (cell)
458 (list 'item (car cell))
464 (repeat :tag "Parameters of Content-Type field"
465 (cons string (choice string symbol)))
466 ;; content-transfer-encoding
467 (choice :tag "Encoding"
470 (mapcar (lambda (cell)
473 (mime-encoding-list))))
475 (choice :tag "Disposition-Type"
481 (repeat :tag "Parameters of Content-Disposition field"
482 (cons string (choice string symbol)))
487 ;;; @@ about charset, encoding and transfer-level
490 (defvar mime-charset-type-list
492 (iso-8859-1 8 "quoted-printable")
493 (iso-8859-2 8 "quoted-printable")
494 (iso-8859-3 8 "quoted-printable")
495 (iso-8859-4 8 "quoted-printable")
496 (iso-8859-5 8 "quoted-printable")
497 (koi8-r 8 "quoted-printable")
498 (iso-8859-7 8 "quoted-printable")
499 (iso-8859-8 8 "quoted-printable")
500 (iso-8859-9 8 "quoted-printable")
501 (iso-8859-14 8 "quoted-printable")
502 (iso-8859-15 8 "quoted-printable")
503 (iso-2022-jp 7 "base64")
504 (iso-2022-jp-3 7 "base64")
505 (iso-2022-kr 7 "base64")
511 (shift_jis 8 "base64")
513 (iso-2022-jp-2 7 "base64")
514 (iso-2022-int-1 7 "base64")
517 (defvar mime-transfer-level 7
518 "*A number of network transfer level. It should be bigger than 7.")
519 (make-variable-buffer-local 'mime-transfer-level)
521 (defsubst mime-encoding-name (transfer-level &optional not-omit)
522 (cond ((> transfer-level 8) "binary")
523 ((= transfer-level 8) "8bit")
527 (defvar mime-transfer-level-string
528 (mime-encoding-name mime-transfer-level 'not-omit)
529 "A string formatted version of mime-transfer-level")
530 (make-variable-buffer-local 'mime-transfer-level-string)
532 ;;; @@ about content transfer encoding
534 (defvar mime-content-transfer-encoding-priority-list
535 '(nil "8bit" "binary"))
537 ;;; @@ about message inserting
540 (defvar mime-edit-yank-ignored-field-list
541 '("Received" "Approved" "Path" "Replied" "Status"
542 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
543 "Delete these fields from original message when it is inserted
544 as message/rfc822 part.
545 Each elements are regexp of field-name.")
547 (defvar mime-edit-yank-ignored-field-regexp
549 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
552 (defvar mime-edit-message-inserter-alist nil)
553 (defvar mime-edit-mail-inserter-alist nil)
556 ;;; @@ about message splitting
559 (defcustom mime-edit-split-message t
560 "*Split large message if it is non-nil."
564 (defcustom mime-edit-message-default-max-lines 1000
565 "*Default maximum lines of a message."
569 (defcustom mime-edit-message-max-lines-alist
570 '((news-reply-mode . 500))
571 "Alist of major-mode vs maximum lines of a message.
572 If it is not specified for a major-mode,
573 `mime-edit-message-default-max-lines' is used."
577 (defconst mime-edit-split-ignored-field-regexp
578 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
580 (defcustom mime-edit-split-blind-field-regexp
581 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
582 "*Regular expression to match field-name to be ignored when split sending."
586 (defvar mime-edit-split-message-sender-alist nil)
588 (defvar mime-edit-news-reply-mode-server-running nil)
594 (defconst mime-edit-single-part-tag-regexp
595 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
596 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
598 (defconst mime-edit-quoted-single-part-tag-regexp
599 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
601 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
603 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
605 (defconst mime-edit-beginning-tag-regexp
606 (regexp-or mime-edit-single-part-tag-regexp
607 mime-edit-multipart-beginning-regexp))
609 (defconst mime-edit-end-tag-regexp
610 (regexp-or mime-edit-single-part-tag-regexp
611 mime-edit-multipart-end-regexp))
613 (defconst mime-edit-tag-regexp
614 (regexp-or mime-edit-single-part-tag-regexp
615 mime-edit-multipart-beginning-regexp
616 mime-edit-multipart-end-regexp))
618 (defvar mime-tag-format "--[[%s]]"
619 "*Control-string making a MIME tag.")
621 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
622 "*Control-string making a MIME tag with encoding.")
625 ;;; @@ multipart boundary
628 (defvar mime-multipart-boundary "Multipart"
629 "*Boundary of a multipart message.")
632 ;;; @@ optional header fields
635 (defvar mime-edit-insert-user-agent-field t
636 "*If non-nil, insert User-Agent header field.")
638 (defvar mime-edit-user-agent-value
639 (concat (mime-product-name mime-user-interface-product)
641 (mapconcat #'number-to-string
642 (mime-product-version mime-user-interface-product) ".")
644 (mime-product-code-name mime-user-interface-product)
646 (mime-product-name mime-library-product)
648 (mapconcat #'number-to-string
649 (mime-product-version mime-library-product) ".")
651 (mime-product-code-name mime-library-product)
653 (if (fboundp 'apel-version)
654 (concat (apel-version) " "))
655 (if (featurep 'xemacs)
656 (concat (cond ((and (featurep 'chise)
657 (boundp 'xemacs-chise-version))
658 (concat "CHISE-MULE/" xemacs-chise-version))
659 ((featurep 'utf-2000)
660 (concat "UTF-2000-MULE/" utf-2000-version))
661 ((featurep 'mule) "MULE"))
663 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
666 (substring emacs-version 0 (match-end 0))
667 (cond ((and (boundp 'xemacs-betaname)
669 ;; It does not exist in XEmacs
670 ;; versions prior to 20.3.
671 (concat " " xemacs-betaname))
672 ((and (boundp 'emacs-patch-level)
674 ;; It does not exist in FSF Emacs or in
675 ;; XEmacs versions earlier than 21.1.1.
676 (format " (patch %d)" emacs-patch-level))
678 " (" xemacs-codename ")"
679 ;; `xemacs-extra-name' has appeared in the
680 ;; development version of XEmacs 21.5-b8.
681 (if (and (boundp 'xemacs-extra-name)
682 (symbol-value 'xemacs-extra-name))
683 (concat " " (symbol-value 'xemacs-extra-name))
686 system-configuration ")")
687 " (" emacs-version ")"))
688 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
689 (substring emacs-version 0 (match-beginning 0))
692 (if (boundp 'enable-multibyte-characters)
694 " (" system-configuration ")"
695 (if enable-multibyte-characters
696 (concat " MULE/" mule-version)
697 " (with unibyte mode)")
698 (if (featurep 'meadow)
699 (let ((mver (Meadow-version)))
700 (if (string-match "^Meadow-" mver)
705 (concat "MULE/" mule-version
706 " (based on Emacs " ver ")"))
707 (concat "Emacs/" ver " (" system-configuration ")")))))
708 "Body of User-Agent field.
709 If variable `mime-edit-insert-user-agent-field' is not nil, it is
710 inserted into message header.")
716 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
717 "*Specify MIME tspecials.
718 Tspecials means any character that matches with it in header must be quoted.")
720 (defconst mime-edit-mime-version-value
721 (concat "1.0 (generated by " mime-edit-version ")")
722 "MIME version number.")
724 (defconst mime-edit-mime-version-field-for-message/partial
725 (concat "MIME-Version:"
726 (mime-encode-field-body
727 (concat " 1.0 (split by " mime-edit-version ")\n")
729 "MIME version field for message/partial.")
732 ;;; @ keymap and menu
735 (defvar mime-edit-mode-flag nil)
736 (make-variable-buffer-local 'mime-edit-mode-flag)
738 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
739 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
740 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
741 "Keymap for MIME-Edit mode commands to insert entity or set status.")
743 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
744 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
745 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
746 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
747 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
748 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
749 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
750 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
751 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
752 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
754 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
755 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
756 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
757 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
758 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
759 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
760 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
761 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
762 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
763 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
765 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
766 "Keymap prefix for MIME-Edit mode commands about enclosure.")
767 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
768 "Keymap for MIME-Edit mode commands about enclosure.")
770 (define-key mime-edit-mode-enclosure-map
771 "\C-a" 'mime-edit-enclose-alternative-region)
772 (define-key mime-edit-mode-enclosure-map
773 "\C-p" 'mime-edit-enclose-parallel-region)
774 (define-key mime-edit-mode-enclosure-map
775 "\C-m" 'mime-edit-enclose-mixed-region)
776 (define-key mime-edit-mode-enclosure-map
777 "\C-d" 'mime-edit-enclose-digest-region)
778 (define-key mime-edit-mode-enclosure-map
779 "\C-s" 'mime-edit-enclose-pgp-signed-region)
780 (define-key mime-edit-mode-enclosure-map
781 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
782 (define-key mime-edit-mode-enclosure-map
783 "\C-q" 'mime-edit-enclose-quote-region)
785 (defvar mime-edit-mode-map (make-sparse-keymap)
786 "Keymap for MIME-Edit mode commands.")
787 (define-key mime-edit-mode-map
788 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
789 (define-key mime-edit-mode-map
790 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
792 (defconst mime-edit-menu-title "MIME-Edit")
794 (defconst mime-edit-menu-list
795 '((mime-help "Describe MIME editor mode" mime-edit-help)
796 (file "Insert File" mime-edit-insert-file)
797 (external "Insert External" mime-edit-insert-external)
798 (voice "Insert Voice" mime-edit-insert-voice)
799 (message "Insert Message" mime-edit-insert-message)
800 (mail "Insert Mail" mime-edit-insert-mail)
801 (signature "Insert Signature" mime-edit-insert-signature)
802 (text "Insert Text" mime-edit-insert-text)
803 (tag "Insert Tag" mime-edit-insert-tag)
804 (alternative "Enclose as alternative"
805 mime-edit-enclose-alternative-region)
806 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
807 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
808 (digest "Enclose as digest" mime-edit-enclose-digest-region)
809 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
810 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
811 (quote "Verbatim region" mime-edit-enclose-quote-region)
812 (key "Insert Public Key" mime-edit-insert-key)
813 (split "Set splitting" mime-edit-set-split)
814 (sign "PGP sign" mime-edit-set-sign)
815 (encrypt "PGP encrypt" mime-edit-set-encrypt)
816 (preview "Preview Message" mime-edit-preview-message)
817 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
819 "MIME-edit menubar entry.")
821 (cond ((featurep 'xemacs)
822 ;; modified by Pekka Marjola <pema@iki.fi>
823 ;; 1995/9/5 (c.f. [tm-en:69])
824 (defun mime-edit-define-menu-for-xemacs ()
825 "Define menu for XEmacs."
826 (cond ((featurep 'menubar)
827 (make-local-variable 'current-menubar)
828 (set-buffer-menubar current-menubar)
831 (cons mime-edit-menu-title
834 (vector (nth 1 item)(nth 2 item)
837 mime-edit-menu-list)))
840 ;; modified by Steven L. Baur <steve@miranova.com>
841 ;; 1995/12/6 (c.f. [tm-en:209])
842 (or (boundp 'mime-edit-popup-menu-for-xemacs)
843 (setq mime-edit-popup-menu-for-xemacs
844 (append '("MIME Commands" "---")
845 (mapcar (function (lambda (item)
849 mime-edit-menu-list)))
852 ((>= emacs-major-version 19)
853 (define-key mime-edit-mode-map [menu-bar mime-edit]
854 (cons mime-edit-menu-title
855 (make-sparse-keymap mime-edit-menu-title)))
858 (define-key mime-edit-mode-map
859 (vector 'menu-bar 'mime-edit (car item))
860 (cons (nth 1 item)(nth 2 item))
863 (reverse mime-edit-menu-list)
871 (defvar mime-edit-touched-flag nil)
874 (defun mime-edit-mode ()
875 "MIME minor mode for editing the tagged MIME message.
877 In this mode, basically, the message is composed in the tagged MIME
878 format. The message tag looks like:
880 --[[text/plain; charset=ISO-2022-JP][7bit]]
882 The tag specifies the MIME content type, subtype, optional parameters
883 and transfer encoding of the message following the tag. Messages
884 without any tag are treated as `text/plain' by default. Charset and
885 transfer encoding are automatically defined unless explicitly
886 specified. Binary messages such as audio and image are usually
887 hidden. The messages in the tagged MIME format are automatically
888 translated into a MIME compliant message when exiting this mode.
890 Available charsets depend on Emacs version being used. The following
891 lists the available charsets of each emacs.
893 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
894 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
895 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
896 CN-BIG5 and ISO-2022-INT-1 are available.
898 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
899 be used to represent multilingual text in intermixed manner. Any
900 languages that has no registered charset are represented as either
901 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
903 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
904 without mule, please set variable `default-mime-charset'. This
905 variable must be symbol of which name is a MIME charset.
907 If you want to add more charsets in mule, please set variable
908 `charsets-mime-charset-alist'. This variable must be alist of which
909 key is list of charset and value is symbol of MIME charset. If name
910 of coding-system is different as MIME charset, please set variable
911 `mime-charset-coding-system-alist'. This variable must be alist of
912 which key is MIME charset and value is coding-system.
914 Following commands are available in addition to major mode commands:
917 \\[mime-edit-insert-text] insert a text message.
918 \\[mime-edit-insert-file] insert a (binary) file.
919 \\[mime-edit-insert-external] insert a reference to external body.
920 \\[mime-edit-insert-voice] insert a voice message.
921 \\[mime-edit-insert-message] insert a mail or news message.
922 \\[mime-edit-insert-mail] insert a mail message.
923 \\[mime-edit-insert-signature] insert a signature file at end.
924 \\[mime-edit-insert-key] insert PGP public key.
925 \\[mime-edit-insert-tag] insert a new MIME tag.
927 \[make enclosure (maybe multipart)\]
928 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
929 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
930 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
931 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
932 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
933 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
934 \\[mime-edit-enclose-quote-region] enclose as verbose mode
935 (to avoid to expand tags)
938 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
939 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
940 \\[mime-edit-set-split] set message splitting mode.
941 \\[mime-edit-set-sign] set PGP-sign mode.
942 \\[mime-edit-set-encrypt] set PGP-encryption mode.
943 \\[mime-edit-preview-message] preview editing MIME message.
944 \\[mime-edit-exit] exit and translate into a MIME
946 \\[mime-edit-help] show this help.
947 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
950 Additional commands are available in some major modes:
951 C-c C-c exit, translate and run the original command.
952 C-c C-s exit, translate and run the original command.
954 The following is a message example written in the tagged MIME format.
955 TABs at the beginning of the line are not a part of the message:
957 This is a conventional plain text. It should be translated
960 This is also a plain text. But, it is explicitly specified as
962 --[[text/plain; charset=ISO-8859-1]]
963 This is also a plain text. But charset is specified as
966 ¡Hola! Buenos días. ¿Cómo está usted?
968 This is a <bold>enriched text</bold>.
969 --[[image/gif][base64]]...image encoded in base64 here...
970 --[[audio/basic][base64]]...audio encoded in base64 here...
972 User customizable variables (not documented all of them):
974 Specifies a key prefix for MIME minor mode commands.
976 mime-ignore-preceding-spaces
977 Preceding white spaces in a message body are ignored if non-nil.
979 mime-ignore-trailing-spaces
980 Trailing white spaces in a message body are ignored if non-nil.
983 Hide a non-textual body message encoded in base64 after insertion
987 A number of network transfer level. It should be bigger than 7.
988 If you are in 8bit-through environment, please set 8.
990 mime-edit-voice-recorder
991 Specifies a function to record a voice message and encode it.
992 The function `mime-edit-voice-recorder-for-sun' is for Sun
996 Turning on MIME mode calls the value of mime-edit-mode-hook, if
999 mime-edit-translate-hook
1000 The value of mime-edit-translate-hook is called just before translating
1001 the tagged MIME format into a MIME compliant message if it is
1002 non-nil. If the hook call the function mime-edit-insert-signature,
1003 the signature file will be inserted automatically.
1006 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
1009 (if mime-edit-mode-flag
1011 (if mime-edit-touched-flag
1013 (make-local-variable 'mime-edit-touched-flag)
1014 (setq mime-edit-touched-flag t)
1019 (cond ((featurep 'xemacs)
1020 (add-minor-mode 'mime-edit-mode-flag
1021 '((" MIME-Edit " mime-transfer-level-string))
1027 (set-alist 'minor-mode-alist
1028 'mime-edit-mode-flag
1029 '((" MIME-Edit " mime-transfer-level-string)))
1030 (set-alist 'minor-mode-map-alist
1031 'mime-edit-mode-flag
1037 (defun turn-on-mime-edit ()
1038 "Unconditionally turn on MIME-Edit mode."
1040 (if mime-edit-mode-flag
1041 (error "You are already editing a MIME message.")
1042 (setq mime-edit-mode-flag t)
1044 ;; Set transfer level into mode line
1046 (setq mime-transfer-level-string
1047 (mime-encoding-name mime-transfer-level 'not-omit))
1048 (force-mode-line-update)
1050 ;; Define menu for XEmacs.
1051 (if (featurep 'xemacs)
1052 (mime-edit-define-menu-for-xemacs)
1057 ;; I don't care about saving these.
1058 (setq paragraph-start
1059 (regexp-or mime-edit-single-part-tag-regexp
1061 (setq paragraph-separate
1062 (regexp-or mime-edit-single-part-tag-regexp
1063 paragraph-separate))
1064 (run-hooks 'mime-edit-mode-hook)
1067 (substitute-command-keys
1068 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1072 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1075 (defun mime-edit-exit (&optional nomime no-error)
1076 "Translate the tagged MIME message into a MIME compliant message.
1077 With no argument encode a message in the buffer into MIME, otherwise
1078 just return to previous mode."
1080 (if (not mime-edit-mode-flag)
1082 (error "You aren't editing a MIME message.")
1086 (run-hooks 'mime-edit-translate-hook)
1087 (mime-edit-translate-buffer)))
1088 ;; Restore previous state.
1089 (setq mime-edit-mode-flag nil)
1090 (if (and (featurep 'xemacs)
1091 (featurep 'menubar))
1092 (delete-menu-item (list mime-edit-menu-title))
1095 (set-buffer-modified-p (buffer-modified-p))
1096 (run-hooks 'mime-edit-exit-hook)
1097 (message "Exit MIME editor mode.")
1100 (defun mime-edit-maybe-translate ()
1102 (mime-edit-exit nil t)
1103 (call-interactively 'mime-edit-maybe-split-and-send)
1106 (defun mime-edit-help ()
1107 "Show help message about MIME mode."
1109 (with-output-to-temp-buffer "*Help*"
1110 (princ "MIME editor mode:\n")
1111 (princ (documentation 'mime-edit-mode))
1112 (print-help-return-message)))
1114 (defun mime-edit-insert-text (&optional subtype)
1115 "Insert a text message.
1116 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1117 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1119 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1121 (if (looking-at mime-edit-single-part-tag-regexp)
1123 ;; Make a space between the following message.
1127 (if (and (member (cadr ret) '("enriched"))
1128 (fboundp 'enriched-mode))
1130 (if (boundp 'enriched-mode)
1135 (defun mime-edit-insert-file (file &optional verbose)
1136 "Insert a message from a file."
1137 (interactive "fInsert file as MIME message: \nP")
1138 (let* ((guess (mime-find-file-type file))
1139 (type (nth 0 guess))
1140 (subtype (nth 1 guess))
1141 (parameters (nth 2 guess))
1142 (encoding (nth 3 guess))
1143 (disposition-type (nth 4 guess))
1144 (disposition-params (nth 5 guess))
1147 (setq type (mime-prompt-for-type type)
1148 subtype (mime-prompt-for-subtype type subtype)
1150 (if (or (interactive-p) verbose)
1151 (setq encoding (mime-prompt-for-encoding encoding))
1153 (if (or (consp parameters) (stringp disposition-type))
1154 (let ((rest parameters) cell attribute value)
1155 (setq parameters "")
1157 (setq cell (car rest))
1158 (setq attribute (car cell))
1159 (setq value (cdr cell))
1160 (if (eq value 'file)
1161 (setq value (std11-wrap-as-quoted-string
1162 (file-name-nondirectory file)))
1164 (setq parameters (concat parameters "; " attribute "=" value))
1165 (setq rest (cdr rest))
1167 (if disposition-type
1170 (concat parameters "\n"
1171 "Content-Disposition: " disposition-type))
1172 (setq rest disposition-params)
1174 (setq cell (car rest))
1175 (setq attribute (car cell))
1176 (setq value (cdr cell))
1177 (if (eq value 'file)
1178 (setq value (std11-wrap-as-quoted-string
1179 (file-name-nondirectory file)))
1182 (concat parameters "; " attribute "=" value))
1183 (setq rest (cdr rest))
1187 (mime-edit-insert-tag type subtype parameters)
1188 (mime-edit-insert-binary-file file encoding)
1191 (defun mime-edit-insert-external ()
1192 "Insert a reference to external body."
1194 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1196 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1198 (let* ((pritype (mime-prompt-for-type))
1199 (subtype (mime-prompt-for-subtype pritype))
1200 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1203 (insert "Content-Type: "
1204 pritype "/" subtype (or parameters "") "\n")))
1205 (if (and (not (eobp))
1206 (not (looking-at mime-edit-single-part-tag-regexp)))
1207 (insert (mime-make-text-tag) "\n")))
1209 (defun mime-edit-insert-voice ()
1210 "Insert a voice message."
1214 "What transfer encoding: "
1215 (mime-encoding-alist) nil t nil)))
1216 (mime-edit-insert-tag "audio" "basic" nil)
1217 (mime-edit-define-encoding encoding)
1219 (narrow-to-region (1- (point))(point))
1221 (funcall mime-edit-voice-recorder encoding)
1224 (invisible-region (point-min)(point-max))
1225 (goto-char (point-max))
1228 (defun mime-edit-insert-signature (&optional arg)
1229 "Insert a signature file."
1231 (let ((signature-insert-hook
1234 (let ((items (mime-find-file-type signature-file-name)))
1235 (apply (function mime-edit-insert-tag)
1236 (car items) (cadr items) (list (caddr items))))
1239 (insert-signature arg)
1243 ;; Insert a new tag around a point.
1245 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1246 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1247 If nothing is inserted, return nil."
1250 (mime-edit-goto-tag)
1251 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1252 (< (match-beginning 0) p)
1255 (goto-char (match-beginning 0))
1264 (mime-prompt-for-type)))
1267 (mime-prompt-for-subtype pritype)))
1270 (mime-prompt-for-parameters pritype subtype delimiter)))
1271 ;; Make a new MIME tag.
1272 (setq newtag (mime-make-tag pritype subtype parameters))
1273 ;; Find an current MIME tag.
1276 (if (mime-edit-goto-tag)
1277 (buffer-substring (match-beginning 0) (match-end 0))
1278 ;; Assume content type is 'text/plan'.
1279 (mime-make-tag "text" "plain")
1281 ;; We are only interested in TEXT.
1283 (not (mime-test-content-type
1284 (mime-edit-get-contype oldtag) "text")))
1287 (if (or (not oldtag) ;Not text
1288 (or mime-ignore-same-text-tag
1289 (not (string-equal oldtag newtag))))
1291 ;; Mark the beginning of the tag for convenience.
1292 (push-mark (point) 'nomsg)
1293 (insert newtag "\n")
1294 (list pritype subtype parameters) ;New tag is created.
1296 ;; Restore previous point.
1298 nil ;Nothing is created.
1302 (defun mime-edit-insert-binary-file (file &optional encoding)
1303 "Insert binary FILE at point.
1304 Optional argument ENCODING specifies an encoding method such as base64."
1305 (let* ((tagend (1- (point))) ;End of the tag
1306 (hide-p (and mime-auto-hide-body
1309 (let ((en (downcase encoding)))
1310 (or (string-equal en "7bit")
1311 (string-equal en "8bit")
1312 (string-equal en "binary")
1316 (narrow-to-region tagend (point))
1317 (mime-insert-encoded-file file encoding)
1320 (invisible-region (point-min) (point-max))
1321 (goto-char (point-max))
1323 (goto-char (point-max))
1326 (looking-at mime-edit-tag-regexp)
1327 (= (point)(point-max))
1328 (mime-edit-insert-tag "text" "plain")
1330 ;; Define encoding even if it is 7bit.
1331 (if (stringp encoding)
1333 (goto-char tagend) ; Make sure which line the tag is on.
1334 (mime-edit-define-encoding encoding)
1339 ;; Commands work on a current message flagment.
1341 (defun mime-edit-goto-tag ()
1342 "Search for the beginning of the tagged MIME message."
1343 (let ((current (point)))
1344 (if (looking-at mime-edit-tag-regexp)
1346 ;; At first, go to the end.
1347 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1348 (goto-char (1- (match-beginning 0))) ;For multiline tag
1351 (goto-char (point-max))
1353 ;; Then search for the beginning.
1354 (re-search-backward mime-edit-end-tag-regexp nil t)
1355 (or (looking-at mime-edit-beginning-tag-regexp)
1356 ;; Restore previous point.
1363 (defun mime-edit-content-beginning ()
1364 "Return the point of the beginning of content."
1366 (let ((beg (save-excursion
1367 (beginning-of-line) (point))))
1368 (if (mime-edit-goto-tag)
1369 (let ((top (point)))
1370 (goto-char (match-end 0))
1371 (if (and (= beg top)
1372 (= (following-char) ?\^M))
1376 ;; Default text/plain tag.
1377 (goto-char (point-min))
1379 (concat "\n" (regexp-quote mail-header-separator)
1380 (if mime-ignore-preceding-spaces
1381 "[ \t\n]*\n" "\n")) nil 'move)
1385 (defun mime-edit-content-end ()
1386 "Return the point of the end of content."
1388 (if (mime-edit-goto-tag)
1390 (goto-char (match-end 0))
1391 (if (invisible-p (point))
1392 (next-visible-point (point))
1393 ;; Move to the end of this text.
1394 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1395 ;; Don't forget a multiline tag.
1396 (goto-char (match-beginning 0))
1400 ;; Assume the message begins with text/plain.
1401 (goto-char (mime-edit-content-beginning))
1402 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1403 ;; Don't forget a multiline tag.
1404 (goto-char (match-beginning 0)))
1408 (defun mime-edit-define-charset (charset)
1409 "Set charset of current tag to CHARSET."
1411 (if (mime-edit-goto-tag)
1412 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1413 (delete-region (match-beginning 0) (match-end 0))
1416 (mime-edit-set-parameter
1417 (mime-edit-get-contype tag)
1419 (let ((comment (get charset 'mime-charset-comment)))
1421 (concat (upcase (symbol-name charset)) " (" comment ")")
1422 (upcase (symbol-name charset)))))
1423 (mime-edit-get-encoding tag)))
1426 (defun mime-edit-define-encoding (encoding)
1427 "Set encoding of current tag to ENCODING."
1429 (if (mime-edit-goto-tag)
1430 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1431 (delete-region (match-beginning 0) (match-end 0))
1432 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1435 (defun mime-edit-choose-charset ()
1436 "Choose charset of a text following current point."
1437 (detect-mime-charset-region (point) (mime-edit-content-end))
1440 (defun mime-make-text-tag (&optional subtype)
1441 "Make a tag for a text after current point.
1442 Subtype of text type can be specified by an optional argument SUBTYPE.
1443 Otherwise, it is obtained from mime-content-types."
1444 (let* ((pritype "text")
1445 (subtype (or subtype
1446 (car (car (cdr (assoc pritype mime-content-types)))))))
1447 ;; Charset should be defined later.
1448 (mime-make-tag pritype subtype)))
1451 ;; Tag handling functions
1453 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1454 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1455 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1459 (defun mime-create-tag (contype &optional encoding)
1460 "Make a tag with CONTENT-TYPE and optional ENCODING."
1461 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1464 (defun mime-edit-get-contype (tag)
1465 "Return Content-Type (including parameters) of TAG."
1467 (or (string-match mime-edit-single-part-tag-regexp tag)
1468 (string-match mime-edit-multipart-beginning-regexp tag)
1469 (string-match mime-edit-multipart-end-regexp tag)
1471 (substring tag (match-beginning 1) (match-end 1))
1474 (defun mime-edit-get-encoding (tag)
1475 "Return encoding of TAG."
1477 (string-match mime-edit-single-part-tag-regexp tag)
1479 (not (= (match-beginning 3) (match-end 3)))
1480 (substring tag (match-beginning 3) (match-end 3))))
1482 (defun mime-get-parameter (contype parameter)
1483 "For given CONTYPE return value for PARAMETER.
1484 Nil if no such parameter."
1488 (regexp-quote parameter)
1489 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1491 (substring contype (match-beginning 1) (match-end 1))
1492 nil ;No such parameter
1495 (defun mime-edit-set-parameter (contype parameter value)
1496 "For given CONTYPE set PARAMETER to VALUE."
1497 (let (ctype opt-fields)
1498 (if (string-match "\n[^ \t\n\r]+:" contype)
1499 (setq ctype (substring contype 0 (match-beginning 0))
1500 opt-fields (substring contype (match-beginning 0)))
1501 (setq ctype contype)
1506 (regexp-quote parameter)
1507 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1510 (concat (substring ctype 0 (match-beginning 1))
1512 (substring ctype (match-end 1))
1514 (concat ctype "; " parameter "=" value opt-fields)
1517 (defun mime-strip-parameters (contype)
1518 "Return primary content-type and subtype without parameters for CONTYPE."
1519 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1520 (substring contype (match-beginning 1) (match-end 1)) nil))
1522 (defun mime-test-content-type (contype type &optional subtype)
1523 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1524 (and (stringp contype)
1527 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1528 (downcase contype))))
1533 (defun mime-find-file-type (file)
1534 "Guess Content-Type, subtype, and parameters from FILE."
1536 (guesses mime-file-types))
1537 (while (and (not guess) guesses)
1538 (if (string-match (car (car guesses)) file)
1539 (setq guess (cdr (car guesses))))
1540 (setq guesses (cdr guesses)))
1544 (defun mime-prompt-for-type (&optional default)
1545 "Ask for Content-type."
1547 ;; Repeat until primary content type is specified.
1548 (while (string-equal type "")
1550 (completing-read "What content type: "
1553 'require-match ;Type must be specified.
1556 (if (string-equal type "")
1558 (message "Content type is required.")
1565 (defun mime-prompt-for-subtype (type &optional default)
1566 "Ask for subtype of media-type TYPE."
1567 (let ((subtypes (cdr (assoc type mime-content-types))))
1569 (assoc default subtypes))
1570 (setq default (car (car subtypes)))
1576 "What content subtype: (default " default ") ")
1577 "What content subtype: ")
1578 (cdr (assoc type mime-content-types))
1580 'require-match ;Subtype must be specified.
1583 (if (string-equal answer "") default answer)))
1585 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1586 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1587 Optional DELIMITER specifies parameter delimiter (';' by default)."
1588 (let* ((delimiter (or delimiter "; "))
1593 (mime-prompt-for-parameters-1
1595 (cdr (assoc pritype mime-content-types))))))
1598 (if (and (stringp parameters)
1599 (not (string-equal parameters "")))
1600 (concat delimiter parameters)
1601 "" ;"" if no parameters
1604 (defun mime-prompt-for-parameters-1 (optlist)
1605 (apply (function append)
1606 (mapcar (function mime-prompt-for-parameter) optlist)))
1608 (defun mime-prompt-for-parameter (parameter)
1610 Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
1611 (let* ((prompt (car parameter))
1612 (choices (mapcar (function
1614 (if (consp e) e (list e))))
1616 (default (car (car choices)))
1622 (concat "What " prompt
1624 (if (string-equal default "") "\"\"" default)
1626 choices nil nil ""))
1627 ;; If nothing is selected, use default.
1628 (if (string-equal answer "")
1629 (setq answer default)))
1631 (read-string (concat "What " prompt ": "))))
1632 (cons (if (and answer
1633 (not (string-equal answer "")))
1635 ;; Note: control characters ignored!
1636 (if (string-match mime-tspecials-regexp answer)
1637 (concat "\"" answer "\"") answer)))
1638 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1641 (defun mime-prompt-for-encoding (default)
1642 "Ask for Content-Transfer-Encoding."
1647 "What transfer encoding: "
1648 (mime-encoding-alist) nil t default)
1654 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1657 (defvar mime-edit-translate-buffer-hook
1658 '(mime-edit-pgp-enclose-buffer
1659 mime-edit-translate-body
1660 mime-edit-translate-header))
1662 (defun mime-edit-translate-header ()
1663 "Encode the message header into network representation."
1664 (mime-encode-header-in-buffer 'code-conversion)
1665 (run-hooks 'mime-edit-translate-header-hook))
1667 (defun mime-edit-translate-buffer ()
1668 "Encode the tagged MIME message in current buffer in MIME compliant message."
1671 (if (catch 'mime-edit-error
1673 (run-hooks 'mime-edit-translate-buffer-hook)
1677 (error "Translation error!")
1680 (defun mime-edit-find-inmost ()
1681 (goto-char (point-min))
1682 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1683 (let ((bb (match-beginning 0))
1685 (type (buffer-substring (match-beginning 1)(match-end 1)))
1687 (setq end-exp (format "--}-<<%s>>\n" type))
1689 (if (re-search-forward end-exp nil t)
1690 (setq eb (match-beginning 0))
1691 (setq eb (point-max))
1693 (narrow-to-region be eb)
1695 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1697 (narrow-to-region (match-beginning 0)(point-max))
1698 (mime-edit-find-inmost)
1701 (list type bb be eb)
1704 (defun mime-edit-process-multipart-1 (boundary)
1705 (let ((ret (mime-edit-find-inmost)))
1707 (let ((type (car ret))
1708 (bb (nth 1 ret))(be (nth 2 ret))
1711 (narrow-to-region bb eb)
1712 (delete-region bb be)
1713 (setq bb (point-min))
1714 (setq eb (point-max))
1717 (if (looking-at mime-edit-multipart-end-regexp)
1718 (let ((beg (match-beginning 0))
1721 (delete-region beg end)
1722 (or (looking-at mime-edit-beginning-tag-regexp)
1724 (insert (concat (mime-make-text-tag) "\n"))
1726 (cond ((string-equal type "quote")
1727 (mime-edit-enquote-region bb eb)
1729 ((string-equal type "pgp-signed")
1730 (mime-edit-sign-pgp-mime bb eb boundary)
1732 ((string-equal type "pgp-encrypted")
1733 (mime-edit-encrypt-pgp-mime bb eb boundary)
1735 ((string-equal type "kazu-signed")
1736 (mime-edit-sign-pgp-kazu bb eb boundary)
1738 ((string-equal type "kazu-encrypted")
1739 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1741 ((string-equal type "smime-signed")
1742 (mime-edit-sign-smime bb eb boundary)
1744 ((string-equal type "smime-encrypted")
1745 (mime-edit-encrypt-smime bb eb boundary)
1749 (nth 2 (mime-edit-translate-region bb eb
1753 (format "--[[multipart/%s;
1754 boundary=\"%s\"][7bit]]\n"
1759 (defun mime-edit-enquote-region (beg end)
1762 (narrow-to-region beg end)
1764 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1765 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1766 (replace-match (concat "- " (substring tag 1)))
1769 (defun mime-edit-dequote-region (beg end)
1772 (narrow-to-region beg end)
1774 (while (re-search-forward
1775 mime-edit-quoted-single-part-tag-regexp nil t)
1776 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1777 (replace-match (concat "-" (substring tag 2)))
1780 (defvar mime-edit-pgp-user-id nil)
1782 (defun mime-edit-sign-pgp-mime (beg end boundary)
1785 (let* ((from (std11-field-body "From" mail-header-separator))
1787 (narrow-to-region beg end)
1788 (mime-edit-translate-region beg end boundary)))
1790 (encoding (nth 1 ret))
1791 (pgp-boundary (concat "pgp-sign-" boundary))
1794 (insert (format "Content-Type: %s\n" ctype))
1796 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1799 (or (let ((pgg-default-user-id
1800 (or mime-edit-pgp-user-id
1802 (nth 1 (std11-extract-address-components from))
1803 pgg-default-user-id))))
1804 (pgg-sign-region (point-min)(point-max)))
1805 (throw 'mime-edit-error 'pgp-error)
1808 (cdr (assq 'hash-algorithm
1809 (cdar (with-current-buffer pgg-output-buffer
1810 (pgg-parse-armor-region
1811 (point-min)(point-max))))))
1814 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1817 (insert (format "--[[multipart/signed;
1819 protocol=\"application/pgp-signature\"][7bit]]
1821 " pgp-boundary micalg pgp-boundary))
1822 (goto-char (point-max))
1823 (insert (format "\n--%s
1824 Content-Type: application/pgp-signature
1825 Content-Transfer-Encoding: 7bit
1828 (insert-buffer-substring pgg-output-buffer)
1829 (goto-char (point-max))
1830 (insert (format "\n--%s--\n" pgp-boundary))
1833 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1835 (defun mime-edit-make-encrypt-recipient-header ()
1836 (let* ((names mime-edit-encrypt-recipient-fields-list)
1838 (std11-field-bodies (cons "From" names)
1839 nil mail-header-separator))
1842 (setq values (cdr values))))
1843 (header (and (stringp from)
1844 (if (string-equal from "")
1846 (format "From: %s\n" from)
1849 (while (and names values)
1850 (let ((name (car names))
1851 (value (car values))
1853 (and (stringp value)
1854 (or (string-equal value "")
1856 (setq header (concat header name ": " value "\n")
1857 recipients (if recipients
1858 (concat recipients " ," value)
1861 (setq names (cdr names)
1862 values (cdr values))
1864 (vector from recipients header)
1867 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1870 (let (from recipients header)
1871 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1872 (setq from (aref ret 0)
1873 recipients (aref ret 1)
1874 header (aref ret 2))
1876 (narrow-to-region beg end)
1878 (mime-edit-translate-region beg end boundary))
1880 (encoding (nth 1 ret))
1881 (pgp-boundary (concat "pgp-" boundary)))
1884 (insert (format "Content-Type: %s\n" ctype))
1886 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1889 (mime-encode-header-in-buffer)
1890 (or (let ((pgg-default-user-id
1891 (or mime-edit-pgp-user-id
1893 (nth 1 (std11-extract-address-components from))
1894 pgg-default-user-id))))
1896 (point-min) (point-max)
1897 (mapcar (lambda (recipient)
1898 (nth 1 (std11-extract-address-components
1900 (split-string recipients
1901 "\\([ \t\n]*,[ \t\n]*\\)+")))
1903 (throw 'mime-edit-error 'pgp-error)
1905 (delete-region (point-min)(point-max))
1907 (insert (format "--[[multipart/encrypted;
1909 protocol=\"application/pgp-encrypted\"][7bit]]
1911 Content-Type: application/pgp-encrypted
1914 Content-Type: application/octet-stream
1915 Content-Transfer-Encoding: 7bit
1917 " pgp-boundary pgp-boundary pgp-boundary))
1918 (insert-buffer-substring pgg-output-buffer)
1919 (goto-char (point-max))
1920 (insert (format "\n--%s--\n" pgp-boundary))
1923 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1926 (narrow-to-region beg end)
1928 (mime-edit-translate-region beg end boundary))
1930 (encoding (nth 1 ret)))
1932 (insert (format "Content-Type: %s\n" ctype))
1934 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1937 (or (pgg-sign-region beg (point-max) 'clearsign)
1938 (throw 'mime-edit-error 'pgp-error)
1942 "--[[application/pgp; format=mime][7bit]]\n")
1946 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1948 (let (recipients header)
1949 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1950 (setq recipients (aref ret 1)
1951 header (aref ret 2))
1954 (narrow-to-region beg end)
1956 (mime-edit-translate-region beg end boundary))
1958 (encoding (nth 1 ret)))
1961 (insert (format "Content-Type: %s\n" ctype))
1963 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1966 (or (pgg-encrypt-region beg (point-max) recipients)
1967 (throw 'mime-edit-error 'pgp-error)
1971 "--[[application/pgp; format=mime][7bit]]\n")
1975 (defun mime-edit-sign-smime (beg end boundary)
1979 (narrow-to-region beg end)
1980 (mime-edit-translate-region beg end boundary)))
1982 (encoding (nth 1 ret))
1983 (smime-boundary (concat "smime-sign-" boundary)))
1985 (insert (format "Content-Type: %s\n" ctype))
1987 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1990 (let (buffer-undo-list)
1991 (goto-char (point-min))
1992 (while (progn (end-of-line) (not (eobp)))
1995 (or (prog1 (smime-sign-region (point-min)(point-max))
1996 (push nil buffer-undo-list)
1997 (ignore-errors (undo)))
1998 (throw 'mime-edit-error 'pgp-error)
2001 (insert (format "--[[multipart/signed;
2002 boundary=\"%s\"; micalg=sha1;
2003 protocol=\"application/pkcs7-signature\"][7bit]]
2005 " smime-boundary smime-boundary))
2006 (goto-char (point-max))
2007 (insert (format "\n--%s
2008 Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
2009 Content-Transfer-Encoding: base64
2010 Content-Disposition: attachment; filename=\"smime.p7s\"
2011 Content-Description: S/MIME Cryptographic Signature
2014 (insert-buffer-substring smime-output-buffer)
2015 (goto-char (point-max))
2016 (insert (format "\n--%s--\n" smime-boundary))
2019 (defun mime-edit-encrypt-smime (beg end boundary)
2023 (narrow-to-region beg end)
2024 (mime-edit-translate-region beg end boundary)))
2026 (encoding (nth 1 ret)))
2028 (insert (format "Content-Type: %s\n" ctype))
2030 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2033 (goto-char (point-min))
2034 (while (progn (end-of-line) (not (eobp)))
2037 (or (smime-encrypt-region (point-min)(point-max))
2038 (throw 'mime-edit-error 'pgp-error)
2040 (delete-region (point-min)(point-max))
2041 (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
2042 Content-Disposition: attachment; filename=\"smime.p7m\"
2043 Content-Description: S/MIME Encrypted Message][base64]]\n")
2044 (insert-buffer-substring smime-output-buffer)
2047 (defsubst replace-space-with-underline (str)
2048 (mapconcat (function
2056 (defun mime-edit-make-boundary ()
2057 (concat mime-multipart-boundary "_"
2058 (replace-space-with-underline (current-time-string))
2061 (defun mime-edit-translate-body ()
2062 "Encode the tagged MIME body in current buffer in MIME compliant message."
2065 (let ((boundary (mime-edit-make-boundary))
2068 (while (mime-edit-process-multipart-1
2069 (format "%s-%d" boundary i))
2073 ;; We are interested in message body.
2076 (goto-char (point-min))
2078 (concat "\n" (regexp-quote mail-header-separator)
2079 (if mime-ignore-preceding-spaces
2080 "[ \t\n]*\n" "\n")) nil 'move)
2084 (goto-char (point-max))
2085 (and mime-ignore-trailing-spaces
2086 (re-search-backward "[^ \t\n]\n" beg t)
2089 (setq ret (mime-edit-translate-region
2091 (format "%s-%d" boundary i)))
2093 (mime-edit-dequote-region (point-min)(point-max))
2094 (let ((contype (car ret)) ;Content-Type
2095 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
2097 ;; Insert User-Agent field
2098 (and mime-edit-insert-user-agent-field
2099 (or (mail-position-on-field "User-Agent")
2100 (insert mime-edit-user-agent-value)
2102 ;; Make primary MIME headers.
2103 (or (mail-position-on-field "MIME-Version")
2104 (insert mime-edit-mime-version-value))
2105 ;; Remove old Content-Type and other fields.
2107 (goto-char (point-min))
2108 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2109 (narrow-to-region (point-min) (point))
2110 (goto-char (point-min))
2111 (mime-delete-field "Content-Type")
2112 (mime-delete-field "Content-Transfer-Encoding"))
2113 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2114 (mail-position-on-field "Content-Type")
2118 (mail-position-on-field "Content-Transfer-Encoding")
2122 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2123 "Translate single-part-tag to MIME header."
2124 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2125 (let* ((beg (match-beginning 0))
2127 (tag (buffer-substring beg end)))
2128 (delete-region beg end)
2129 (let ((contype (mime-edit-get-contype tag))
2130 (encoding (mime-edit-get-encoding tag)))
2131 (insert (concat prefix "--" boundary "\n"))
2133 (narrow-to-region (point)(point))
2134 (insert "Content-Type: " contype "\n")
2136 (insert "Content-Transfer-Encoding: " encoding "\n"))
2137 (mime-encode-header-in-buffer))
2141 (downcase encoding))))
2144 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2146 (setq boundary (mime-edit-make-boundary))
2150 (narrow-to-region beg end)
2151 (let ((tag nil) ;MIME tag
2152 (contype nil) ;Content-Type
2153 (encoding nil) ;Content-Transfer-Encoding
2154 (nparts 0)) ;Number of body parts
2155 ;; Normalize the body part by inserting appropriate message
2156 ;; tags for every message contents.
2157 (mime-edit-normalize-body)
2158 ;; Counting the number of Content-Type.
2159 (goto-char (point-min))
2160 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2161 (setq nparts (1+ nparts)))
2162 ;; Begin translation.
2164 ((and (<= nparts 1)(not multipart))
2165 ;; It's a singular message.
2166 (goto-char (point-min))
2167 (while (re-search-forward
2168 mime-edit-single-part-tag-regexp nil t)
2170 (buffer-substring (match-beginning 0) (match-end 0)))
2171 (delete-region (match-beginning 0) (1+ (match-end 0)))
2172 (setq contype (mime-edit-get-contype tag))
2173 (setq encoding (mime-edit-get-encoding tag))
2176 ;; It's a multipart message.
2177 (goto-char (point-min))
2178 (let ((prio mime-content-transfer-encoding-priority-list)
2180 (when (setq part-info
2181 (mime-edit-translate-single-part-tag boundary))
2182 (and (setq nprio (member (cdr part-info) prio))
2184 (while (setq part-info
2185 (mime-edit-translate-single-part-tag boundary "\n"))
2186 (and (setq nprio (member (cdr part-info) prio))
2187 (setq prio nprio))))
2188 ;; Define Content-Type as "multipart/mixed".
2190 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2191 (setq encoding (car prio))
2192 ;; Insert the trailer.
2193 (goto-char (point-max))
2194 (insert "\n--" boundary "--\n")
2196 (list contype encoding boundary nparts)
2199 (defun mime-edit-normalize-body ()
2200 "Normalize the body part by inserting appropriate message tags."
2201 ;; Insert the first MIME tags if necessary.
2202 (goto-char (point-min))
2203 (if (not (looking-at mime-edit-single-part-tag-regexp))
2204 (insert (mime-make-text-tag) "\n"))
2205 ;; Check each tag, and add new tag or correct it if necessary.
2206 (goto-char (point-min))
2207 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2208 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2209 (contype (mime-edit-get-contype tag))
2210 (charset (mime-get-parameter contype "charset"))
2211 (encoding (mime-edit-get-encoding tag)))
2212 ;; Remove extra whitespaces after the tag.
2213 (if (looking-at "[ \t]+$")
2214 (delete-region (match-beginning 0) (match-end 0)))
2216 (end (mime-edit-content-end))
2218 (if (= end (point-max))
2221 (or (looking-at mime-edit-beginning-tag-regexp)
2223 (insert (mime-make-text-tag) "\n")
2225 (visible-region beg end)
2229 ((mime-test-content-type contype "message")
2230 ;; Content-type "message" should be sent as is.
2233 ((mime-test-content-type contype "text")
2234 ;; Define charset for text if necessary.
2235 (setq charset (if charset
2236 (intern (downcase charset))
2237 (mime-edit-choose-charset)))
2238 (mime-edit-define-charset charset)
2239 (cond ((string-equal contype "text/x-rot13-47-48")
2242 (mule-caesar-region (point) (mime-edit-content-end))
2244 ((string-equal contype "text/enriched")
2249 (end (mime-edit-content-end))
2251 ;; Patch for hard newlines
2254 ;; (while (search-forward "\n" end t)
2255 ;; (put-text-property (match-beginning 0)
2258 ;; End patch for hard newlines
2259 (enriched-encode beg end nil)
2261 (if (search-forward "\n\n")
2262 (delete-region beg (match-end 0))
2265 ;; Point is now on current tag.
2266 ;; Define encoding and encode text if necessary.
2267 (or encoding ;Encoding is not specified.
2270 (let ((ret (cdr (assq charset mime-charset-type-list))))
2272 (setq bits (car ret)
2275 conv "quoted-printable")))
2276 (if (<= bits mime-transfer-level)
2277 (mime-encoding-name bits)
2279 (beg (mime-edit-content-beginning)))
2280 (encode-mime-charset-region beg (mime-edit-content-end)
2282 ;; Protect "From " in beginning of line
2284 (narrow-to-region beg (mime-edit-content-end))
2286 (let (case-fold-search)
2287 (if (re-search-forward "^From " nil t)
2289 (if (memq charset '(iso-2022-jp
2294 (replace-match "\e(BFrom ")
2295 (re-search-forward "^From " nil t)
2297 (setq encoding "quoted-printable")
2299 ;; canonicalize line break code
2300 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2302 (narrow-to-region beg (mime-edit-content-end))
2304 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2305 ;; In a certain period, `replace-match' with "\\N"
2306 ;; converted 8-bit characters into multibyte string,
2307 ;; but it has been fixed at 2004-01-15.
2308 ;;(replace-match "\\1\r\n"))))
2313 (mime-encode-region beg (mime-edit-content-end)
2314 (or encoding "7bit"))
2315 (mime-edit-define-encoding encoding)
2317 (goto-char (mime-edit-content-end))
2319 ((null encoding) ;Encoding is not specified.
2320 ;; Application, image, audio, video, and any other
2321 ;; unknown content-type without encoding should be
2323 (let* ((encoding "base64") ;Encode in BASE64 by default.
2324 (beg (mime-edit-content-beginning))
2325 (end (mime-edit-content-end)))
2326 (mime-encode-region beg end encoding)
2327 (mime-edit-define-encoding encoding))
2332 (defun mime-delete-field (field)
2333 "Delete header FIELD."
2334 (let ((regexp (format "^%s:[ \t]*" field)))
2335 (goto-char (point-min))
2336 (while (re-search-forward regexp nil t)
2337 (delete-region (match-beginning 0)
2338 (1+ (std11-field-end))))))
2342 ;;; Platform dependent functions
2345 ;; Sun implementations
2347 (defun mime-edit-voice-recorder-for-sun (encoding)
2348 "Record voice in a buffer using Sun audio device,
2349 and insert data encoded as ENCODING."
2350 (message "Start the recording on %s. Type C-g to finish the recording..."
2352 (mime-insert-encoded-file "/dev/audio" encoding)
2356 ;;; @ Other useful commands.
2359 ;; Message forwarding commands as content-type "message/rfc822".
2361 (defun mime-edit-insert-message (&optional message)
2363 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2364 (if (and inserter (fboundp inserter))
2366 (mime-edit-insert-tag "message" "rfc822")
2367 (funcall inserter message)
2369 (message "Sorry, I don't have message inserter for your MUA.")
2372 (defun mime-edit-insert-mail (&optional message)
2374 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2375 (if (and inserter (fboundp inserter))
2377 (mime-edit-insert-tag "message" "rfc822")
2378 (funcall inserter message)
2380 (message "Sorry, I don't have mail inserter for your MUA.")
2383 (defun mime-edit-inserted-message-filter ()
2386 (let ((header-start (point))
2387 (case-fold-search t)
2390 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2391 (if (re-search-forward "^$" (mark t))
2392 (narrow-to-region header-start (match-beginning 0))
2394 (goto-char header-start)
2395 (while (and (re-search-forward
2396 mime-edit-yank-ignored-field-regexp nil t)
2397 (setq beg (match-beginning 0))
2398 (setq end (1+ (std11-field-end)))
2400 (delete-region beg end)
2405 ;;; @ multipart enclosure
2408 (defun mime-edit-enclose-region-internal (type beg end)
2412 (narrow-to-region beg end)
2413 (insert (format "--<<%s>>-{\n" type))
2414 (goto-char (point-max))
2415 (insert (format "--}-<<%s>>\n" type))
2416 (goto-char (point-max))
2418 (or (looking-at mime-edit-beginning-tag-regexp)
2420 (insert (mime-make-text-tag) "\n")
2424 (defun mime-edit-enclose-quote-region (beg end)
2426 (mime-edit-enclose-region-internal 'quote beg end)
2429 (defun mime-edit-enclose-mixed-region (beg end)
2431 (mime-edit-enclose-region-internal 'mixed beg end)
2434 (defun mime-edit-enclose-parallel-region (beg end)
2436 (mime-edit-enclose-region-internal 'parallel beg end)
2439 (defun mime-edit-enclose-digest-region (beg end)
2441 (mime-edit-enclose-region-internal 'digest beg end)
2444 (defun mime-edit-enclose-alternative-region (beg end)
2446 (mime-edit-enclose-region-internal 'alternative beg end)
2449 (defun mime-edit-enclose-pgp-signed-region (beg end)
2451 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2454 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2456 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2459 (defun mime-edit-enclose-kazu-signed-region (beg end)
2461 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2464 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2466 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2469 (defun mime-edit-enclose-smime-signed-region (beg end)
2471 (mime-edit-enclose-region-internal 'smime-signed beg end)
2474 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2476 (mime-edit-enclose-region-internal 'smime-encrypted beg end)
2479 (defun mime-edit-insert-key (&optional arg)
2480 "Insert a pgp public key."
2482 (mime-edit-insert-tag "application" "pgp-keys")
2483 (mime-edit-define-encoding "7bit")
2485 (if (and (not (eobp))
2486 (not (looking-at mime-edit-single-part-tag-regexp)))
2487 (insert (mime-make-text-tag) "\n")))
2493 (defun mime-edit-set-split (arg)
2496 (y-or-n-p "Do you want to enable split? ")
2498 (setq mime-edit-split-message arg)
2500 (message "This message is enabled to split.")
2501 (message "This message is not enabled to split.")
2504 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2505 "Toggle transfer-level is 7bit or 8bit through.
2507 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2509 (if (numberp transfer-level)
2510 (setq mime-transfer-level transfer-level)
2511 (if (< mime-transfer-level 8)
2512 (setq mime-transfer-level 8)
2513 (setq mime-transfer-level 7)
2515 (message (format "Current transfer-level is %d bit"
2516 mime-transfer-level))
2517 (setq mime-transfer-level-string
2518 (mime-encoding-name mime-transfer-level 'not-omit))
2519 (force-mode-line-update)
2522 (defun mime-edit-set-transfer-level-7bit ()
2524 (mime-edit-toggle-transfer-level 7)
2527 (defun mime-edit-set-transfer-level-8bit ()
2529 (mime-edit-toggle-transfer-level 8)
2536 (defvar mime-edit-pgp-processing nil)
2537 (make-variable-buffer-local 'mime-edit-pgp-processing)
2539 (defun mime-edit-set-sign (arg)
2542 (y-or-n-p "Do you want to sign? ")
2546 (or (memq 'sign mime-edit-pgp-processing)
2547 (setq mime-edit-pgp-processing
2548 (nconc mime-edit-pgp-processing
2549 (copy-sequence '(sign)))))
2550 (message "This message will be signed.")
2552 (setq mime-edit-pgp-processing
2553 (delq 'sign mime-edit-pgp-processing))
2554 (message "This message will not be signed.")
2557 (defun mime-edit-set-encrypt (arg)
2560 (y-or-n-p "Do you want to encrypt? ")
2564 (or (memq 'encrypt mime-edit-pgp-processing)
2565 (setq mime-edit-pgp-processing
2566 (nconc mime-edit-pgp-processing
2567 (copy-sequence '(encrypt)))))
2568 (message "This message will be encrypt.")
2570 (setq mime-edit-pgp-processing
2571 (delq 'encrypt mime-edit-pgp-processing))
2572 (message "This message will not be encrypt.")
2575 (defun mime-edit-pgp-enclose-buffer ()
2576 (let ((beg (save-excursion
2577 (goto-char (point-min))
2578 (if (search-forward (concat "\n" mail-header-separator "\n"))
2583 (dolist (pgp-processing mime-edit-pgp-processing)
2584 (case pgp-processing
2586 (mime-edit-enclose-pgp-signed-region
2590 (mime-edit-enclose-pgp-encrypted-region
2599 (defun mime-edit-insert-partial-header (fields subject
2600 id number total separator)
2602 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2603 (insert mime-edit-mime-version-field-for-message/partial)
2605 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2606 id number total separator))
2609 (defun mime-edit-split-and-send
2610 (&optional cmd lines mime-edit-message-max-length)
2614 (count-lines (point-min) (point-max)))
2616 (or mime-edit-message-max-length
2617 (setq mime-edit-message-max-length
2618 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2619 mime-edit-message-default-max-lines))
2621 (let* ((separator mail-header-separator)
2623 (replace-space-with-underline (current-time-string))
2624 "@" (system-name) "\"")))
2625 (run-hooks 'mime-edit-before-split-hook)
2626 (let ((the-buf (current-buffer))
2627 (copy-buf (get-buffer-create " *Original Message*"))
2628 (header (std11-header-string-except
2629 mime-edit-split-ignored-field-regexp separator))
2630 (subject (mail-fetch-field "subject"))
2631 (total (+ (/ lines mime-edit-message-max-length)
2632 (if (> (mod lines mime-edit-message-max-length) 0)
2638 mime-edit-split-message-sender-alist))
2642 (error "Split sender is not specified for `%s'." major-mode)
2645 (mime-edit-partial-number 1)
2648 (set-buffer copy-buf)
2650 (insert-buffer the-buf)
2652 (if (re-search-forward
2653 (concat "^" (regexp-quote separator) "$") nil t)
2654 (let ((he (match-beginning 0)))
2656 (narrow-to-region (point-min) he)
2658 (goto-char (point-min))
2659 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2660 (delete-region (match-beginning 0)
2661 (1+ (std11-field-end)))
2663 (while (< mime-edit-partial-number total)
2666 (set-buffer copy-buf)
2667 (setq data (buffer-substring
2670 (goto-line mime-edit-message-max-length)
2673 (delete-region (point-min)(point))
2675 (mime-edit-insert-partial-header
2676 header subject id mime-edit-partial-number total separator)
2679 (message (format "Sending %d/%d..."
2680 mime-edit-partial-number total))
2681 (call-interactively command)
2682 (message (format "Sending %d/%d...done"
2683 mime-edit-partial-number total))
2685 (setq mime-edit-partial-number
2686 (1+ mime-edit-partial-number))
2690 (set-buffer copy-buf)
2691 (setq data (buffer-string))
2694 (mime-edit-insert-partial-header
2695 header subject id mime-edit-partial-number total separator)
2698 (message (format "Sending %d/%d..."
2699 mime-edit-partial-number total))
2700 (message (format "Sending %d/%d...done"
2701 mime-edit-partial-number total))
2705 (defun mime-edit-maybe-split-and-send (&optional cmd)
2707 (run-hooks 'mime-edit-before-send-hook)
2708 (let ((mime-edit-message-max-length
2709 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2710 mime-edit-message-default-max-lines))
2711 (lines (count-lines (point-min) (point-max)))
2713 (if (and (> lines mime-edit-message-max-length)
2714 mime-edit-split-message)
2715 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2719 ;;; @ preview message
2722 (defvar mime-edit-buffer nil) ; buffer local variable
2724 (defun mime-edit-preview-message ()
2725 "preview editing MIME message."
2727 (let* ((str (buffer-string))
2728 (separator mail-header-separator)
2729 (the-buf (current-buffer))
2730 (buf-name (buffer-name))
2731 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2732 (buf (get-buffer temp-buf-name))
2733 (pgp-processing mime-edit-pgp-processing)
2737 (switch-to-buffer buf)
2740 (setq buf (get-buffer-create temp-buf-name))
2741 (switch-to-buffer buf)
2744 (setq major-mode 'mime-temp-message-mode)
2745 (make-local-variable 'mail-header-separator)
2746 (setq mail-header-separator separator)
2747 (make-local-variable 'mime-edit-buffer)
2748 (setq mime-edit-buffer the-buf)
2749 (setq mime-edit-pgp-processing pgp-processing)
2751 (run-hooks 'mime-edit-translate-hook)
2752 (mime-edit-translate-buffer)
2753 (goto-char (point-min))
2754 (if (re-search-forward
2755 (concat "^" (regexp-quote separator) "$"))
2759 (make-local-variable 'mime-edit-temp-message-buffer)
2760 (setq mime-edit-temp-message-buffer buf)))
2762 (defun mime-edit-quitting-method ()
2763 "Quitting method for mime-view."
2764 (let* ((temp mime-edit-temp-message-buffer)
2766 (mime-preview-kill-buffer)
2768 (setq buf mime-edit-buffer)
2770 (switch-to-buffer buf)))
2772 (set-alist 'mime-preview-quitting-method-alist
2773 'mime-temp-message-mode
2774 #'mime-edit-quitting-method)
2780 (defvar mime-edit-again-ignored-field-regexp
2781 (concat "^\\(" "Content-.*\\|Mime-Version"
2782 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2784 "Regexp for deleted header fields when `mime-edit-again' is called.")
2786 (defsubst eliminate-top-spaces (string)
2787 "Eliminate top sequence of space or tab in STRING."
2788 (if (string-match "^[ \t]+" string)
2789 (substring string (match-end 0))
2792 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2795 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2796 '(("application/pgp-encrypted" . pgp-encrypted)
2797 ("application/pgp-signature" . pgp-signed))))
2798 (mime-content-type-subtype content-type)))
2799 (boundary (mime-content-type-parameter content-type "boundary"))
2800 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2801 (re-search-forward boundary-pat nil t)
2802 (let ((bb (match-beginning 0)) eb tag)
2803 (setq tag (format "\n--<<%s>>-{\n" subtype))
2806 (setq bb (+ bb (length tag)))
2808 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2810 (setq eb (match-beginning 0))
2811 (replace-match (format "--}-<<%s>>\n" subtype))
2813 (narrow-to-region bb eb)
2814 (goto-char (point-min))
2815 (while (re-search-forward boundary-pat nil t)
2816 (let ((beg (match-beginning 0))
2818 (delete-region beg (match-end 0))
2820 (if (re-search-forward boundary-pat nil t)
2821 (setq end (match-beginning 0))
2822 (setq end (point-max))
2825 (narrow-to-region beg end)
2827 ((eq subtype 'pgp-encrypted)
2830 (goto-char (point-min))
2831 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2834 (save-window-excursion
2835 (pgg-decrypt-region (match-beginning 0)
2837 (delete-region (point-min)(point-max))))
2838 (insert-buffer-substring pgg-output-buffer)
2839 (mime-edit-decode-message-in-buffer
2840 nil not-decode-text)
2841 (delete-region (goto-char (point-min))
2842 (if (search-forward "\n\n" nil t)
2845 (goto-char (point-max))
2848 (mime-edit-decode-message-in-buffer
2849 (if (eq subtype 'digest)
2851 (make-mime-content-type 'message 'rfc822))
2854 (goto-char (point-max))
2858 (goto-char (point-min))
2859 (or (= (point-min) 1)
2860 (delete-region (point-min)
2861 (if (search-forward "\n\n" nil t)
2867 (defun mime-edit-decode-single-part-in-buffer
2868 (content-type not-decode-text &optional content-disposition)
2869 (let* ((type (mime-content-type-primary-type content-type))
2870 (subtype (mime-content-type-subtype content-type))
2871 (ctype (format "%s/%s" type subtype))
2873 (pstr (let ((bytes (+ 14 (length ctype))))
2874 (mapconcat (function
2876 (if (string= (car attr) "charset")
2878 (setq charset (cdr attr))
2880 (let* ((str (concat (car attr)
2883 (setq bytes (+ bytes bs 2))
2886 (setq bytes (+ bs 1))
2890 (mime-content-type-parameters content-type) "")))
2893 (limit (save-excursion
2894 (if (search-forward "\n\n" nil t)
2897 (mime-content-disposition-type content-disposition))
2899 (if disposition-type
2900 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2901 (mapconcat (function
2906 (if (string-equal "filename"
2908 (std11-wrap-as-quoted-string
2912 (setq bytes (+ bytes bs 2))
2915 (setq bytes (+ bs 1))
2919 (mime-content-disposition-parameters
2920 content-disposition)
2923 (if disposition-type
2924 (setq pstr (format "%s\nContent-Disposition: %s%s"
2925 pstr disposition-type disposition-str))
2928 (if (re-search-forward
2929 "^Content-Transfer-Encoding:" limit t)
2930 (let ((beg (match-beginning 0))
2931 (hbeg (match-end 0))
2932 (end (std11-field-end limit)))
2935 (eliminate-top-spaces
2936 (std11-unfold-string
2937 (buffer-substring hbeg end)))))
2938 (if (or charset (eq type 'text))
2940 (delete-region beg (1+ end))
2941 (goto-char (point-min))
2942 (if (search-forward "\n\n" nil t)
2945 (match-end 0)(point-max) encoding)
2949 (if (and (eq type 'text)
2950 (or encoded (not not-decode-text)))
2953 (goto-char (point-min))
2954 (while (re-search-forward "\r\n" nil t)
2955 (replace-match "\n")
2957 (decode-mime-charset-region (point-min)(point-max)
2958 (or charset default-mime-charset))
2960 (let ((he (if (re-search-forward "^$" nil t)
2964 (if (and (eq type 'text)
2965 (eq subtype 'x-rot13-47-48))
2966 (mule-caesar-region he (point-max))
2968 (if (= (point-min) 1)
2974 (format "%s/%s%s" type subtype pstr)
2977 (delete-region (point-min) he)
2979 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2985 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2988 (goto-char (point-min))
2989 (let ((ctl (or (mime-read-Content-Type)
2990 default-content-type)))
2992 (let ((type (mime-content-type-primary-type ctl)))
2994 ((and (eq type 'application)
2995 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2996 (delete-region (point-min)(point-max))
2998 ((eq type 'multipart)
2999 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
3002 (mime-edit-decode-single-part-in-buffer
3003 ctl not-decode-text (mime-read-Content-Disposition))
3006 (decode-mime-charset-region (point-min) (point-max)
3007 default-mime-charset))
3009 (if (= (point-min) 1)
3012 (std11-narrow-to-header)
3013 (goto-char (point-min))
3014 (while (re-search-forward
3015 mime-edit-again-ignored-field-regexp nil t)
3016 (delete-region (match-beginning 0) (1+ (std11-field-end)))
3018 (mime-decode-header-in-buffer (not not-decode-text))
3023 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
3024 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
3025 Content-Type and Content-Transfer-Encoding header fields will be
3026 converted to MIME-Edit tags."
3028 (goto-char (point-min))
3030 (concat "\n" (regexp-quote mail-header-separator) "\n")
3032 (replace-match "\n\n")
3034 (mime-edit-decode-message-in-buffer nil not-decode-text)
3035 (goto-char (point-min))
3037 (and (re-search-forward "^$")
3038 (replace-match mail-header-separator)
3048 (provide 'mime-edit)
3050 (run-hooks 'mime-edit-load-hook)
3052 ;;; mime-edit.el ends here