1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,94,95,96,97,98,99,2000 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Daiki Ueno <ueno@unixuser.org>
8 ;; Created: 1994/08/21 renamed from mime.el
9 ;; Renamed: 1997/2/21 from tm-edit.el
10 ;; Keywords: MIME, multimedia, multilingual, mail, news
12 ;; This file is part of SEMI (Sophisticated Emacs MIME Interfaces).
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; This is an Emacs minor mode for editing Internet multimedia
32 ;; messages formatted in MIME (RFC 2045, 2046, 2047, 2048 and 2049).
33 ;; All messages in this mode are composed in the tagged MIME format,
34 ;; that are described in the following examples. The messages
35 ;; composed in the tagged MIME format are automatically translated
36 ;; into a MIME compliant message when exiting the mode.
38 ;; Mule (multilingual feature of Emacs 20 and multilingual extension
39 ;; for XEmacs 20) has a capability of handling multilingual text in
40 ;; limited ISO-2022 manner that is based on early experiences in
41 ;; Japanese Internet community and resulted in RFC 1468 (ISO-2022-JP
42 ;; charset for MIME). In order to enable multilingual capability in
43 ;; single text message in MIME, charset of multilingual text written
44 ;; in Mule is declared as either `ISO-2022-JP-2' [RFC 1554]. Mule is
45 ;; required for reading the such messages.
47 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
48 ;; News mode. First of all, you need the following autoload
49 ;; definition to load mime-edit-mode automatically:
51 ;; (autoload 'turn-on-mime-edit "mime-edit"
52 ;; "Minor mode for editing MIME message." t)
54 ;; In case of Mail mode (includes VM mode), you need the following
57 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
58 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
60 ;; In case of MH-E, you need the following hook definition:
62 ;; (add-hook 'mh-letter-mode-hook
65 ;; (turn-on-mime-edit)
66 ;; (make-local-variable 'mail-header-separator)
67 ;; (setq mail-header-separator "--------")
69 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
71 ;; In case of News mode, you need the following hook definition:
73 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
74 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
76 ;; In case of Emacs 19, it is possible to emphasize the message tags
77 ;; using font-lock mode as follows:
79 ;; (add-hook 'mime-edit-mode-hook
83 ;; (setq font-lock-keywords (list mime-edit-tag-regexp))
86 ;; The message tag looks like:
88 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
90 ;; The tagged MIME message examples:
92 ;; This is a conventional plain text. It should be translated into
96 ;; This is also a plain text. But, it is explicitly specified as is.
97 ;;--[[text/plain; charset=ISO-8859-1]]
98 ;; This is also a plain text. But charset is specified as iso-8859-1.
100 ;; ¡Hola! Buenos días. ¿Cómo está usted?
101 ;;--[[text/enriched]]
102 ;; <center>This is a richtext.</center>
104 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
106 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
111 (require 'mail-utils)
119 (autoload 'pgg-encrypt-region "pgg"
120 "PGP encryption of current region." t)
121 (autoload 'pgg-sign-region "pgg"
122 "PGP signature of current region." t)
123 (autoload 'pgg-insert-key "pgg"
124 "Insert PGP public key at point." t)
125 (autoload 'smime-encrypt-buffer "smime"
126 "S/MIME encryption of current buffer.")
127 (autoload 'smime-sign-buffer "smime"
128 "S/MIME signature of current buffer.")
135 (defconst mime-edit-version
137 (mime-product-name mime-user-interface-product) " "
138 (mapconcat #'number-to-string
139 (mime-product-version mime-user-interface-product) ".")
140 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
146 (defgroup mime-edit nil
150 (defcustom mime-ignore-preceding-spaces nil
151 "*Ignore preceding white spaces if non-nil."
155 (defcustom mime-ignore-trailing-spaces nil
156 "*Ignore trailing white spaces if non-nil."
160 (defcustom mime-ignore-same-text-tag t
161 "*Ignore preceding text content-type tag that is same with new one.
162 If non-nil, the text tag is not inserted unless something different."
166 (defcustom mime-auto-hide-body t
167 "*Hide non-textual body encoded in base64 after insertion if non-nil."
171 (defcustom mime-edit-voice-recorder
172 (function mime-edit-voice-recorder-for-sun)
173 "*Function to record a voice message and encode it."
177 (defcustom mime-edit-mode-hook nil
178 "*Hook called when enter MIME mode."
182 (defcustom mime-edit-translate-hook nil
183 "*Hook called before translating into a MIME compliant message.
184 To insert a signature file automatically, call the function
185 `mime-edit-insert-signature' from this hook."
189 (defcustom mime-edit-exit-hook nil
190 "*Hook called when exit MIME mode."
194 (defvar mime-content-types
196 ;; Charset parameter need not to be specified, since it is
197 ;; defined automatically while translation.
199 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
212 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
213 ("directory" "/pub/GNU/elisp/mime")
215 ("mode" "image" "ascii" "local8"))
220 ("mode" "image" "ascii" "local8"))
221 ("tftp" ("site") ("name"))
222 ("afs" ("site") ("name"))
223 ("local-file" ("site") ("name"))
225 ("server" "ftpmail@nic.karrn.ad.jp")
231 ("octet-stream" ("type" "" "tar" "shar"))
236 ("vnd.ms-powerpoint")
237 ("x-kiss" ("x-cnf")))
249 "*Alist of content-type, subtype, parameters and its values.")
251 (defcustom mime-file-types
254 ;; Programming languages
257 "application" "octet-stream" (("type" . "C++"))
259 "attachment" (("filename" . file)))
262 "application" "octet-stream" (("type" . "emacs-lisp"))
264 "attachment" (("filename" . file)))
267 "application" "octet-stream" (("type" . "common-lisp"))
269 "attachment" (("filename" . file)))
272 "application" "octet-stream" (("type" . "perl"))
274 "attachment" (("filename" . file)))
276 ;; Text or translated text
281 "inline" (("filename" . file)))
283 ;; .rc : procmail modules pm-xxxx.rc
284 ;; *rc : other resource files
286 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
289 "attachment" (("filename" . file)))
296 ("\\.diff$\\|\\.patch$"
297 "application" "octet-stream" (("type" . "patch"))
299 "attachment" (("filename" . file)))
302 "text" "plain" nil nil nil nil)
305 ;; Octect binary text
308 "application" "msword" nil
310 "attachment" (("filename" . file)))
311 ("\\.xls$" ; MS Excel
312 "application" "vnd.ms-excel" nil
314 "attachment" (("filename" . file)))
315 ("\\.ppt$" ; MS Power Point
316 "application" "vnd.ms-powerpoint" nil
318 "attachment" (("filename" . file)))
323 "inline" (("filename" . file)))
325 "application" "postscript" nil
327 "attachment" (("filename" . file)))
329 "application" "pdf" nil
331 "attachment" (("filename" . file)))
335 ("\\.jpg$\\|\\.jpeg$"
338 "inline" (("filename" . file)))
342 "inline" (("filename" . file)))
346 "inline" (("filename" . file)))
350 "inline" (("filename" . file)))
354 "inline" (("filename" . file)))
358 "inline" (("filename" . file)))
362 "inline" (("filename" . file)))
366 "inline" (("filename" . file)))
370 "attachment" (("filename" . file)))
374 "attachment" (("filename" . file)))
376 "application" "octet-stream" (("type" . "tar+gzip"))
378 "attachment" (("filename" . file)))
380 "application" "octet-stream" (("type" . "tar+gzip"))
382 "attachment" (("filename" . file)))
384 "application" "octet-stream" (("type" . "tar+compress"))
386 "attachment" (("filename" . file)))
388 "application" "octet-stream" (("type" . "tar+compress"))
390 "attachment" (("filename" . file)))
392 "application" "octet-stream" (("type" . "gzip"))
394 "attachment" (("filename" . file)))
396 "application" "octet-stream" (("type" . "compress"))
398 "attachment" (("filename" . file)))
400 "application" "octet-stream" (("type" . "lha"))
402 "attachment" (("filename" . file)))
404 "application" "zip" nil
406 "attachment" (("filename" . file)))
411 "application" "octet-stream" nil
413 "attachment" (("filename" . file))))
414 "*Alist of file name, types, parameters, and default encoding.
415 If encoding is nil, it is determined from its contents."
419 (choice :tag "Primary-Type"
420 ,@(nconc (mapcar (lambda (cell)
421 (list 'item (car cell)))
425 (choice :tag "Sub-Type"
428 (mapcar (lambda (cell)
429 (mapcar (lambda (cell)
430 (list 'item (car cell)))
435 (repeat :tag "Parameters of Content-Type field"
436 (cons string (choice string symbol)))
437 ;; content-transfer-encoding
438 (choice :tag "Encoding"
441 (mapcar (lambda (cell)
443 (mime-encoding-list))))
445 (choice :tag "Disposition-Type"
451 (repeat :tag "Parameters of Content-Disposition field"
452 (cons string (choice string symbol)))))
456 ;;; @@ about charset, encoding and transfer-level
459 (defvar mime-charset-type-list
461 (iso-8859-1 8 "quoted-printable")
462 (iso-8859-2 8 "quoted-printable")
463 (iso-8859-3 8 "quoted-printable")
464 (iso-8859-4 8 "quoted-printable")
465 (iso-8859-5 8 "quoted-printable")
466 (koi8-r 8 "quoted-printable")
467 (iso-8859-7 8 "quoted-printable")
468 (iso-8859-8 8 "quoted-printable")
469 (iso-8859-9 8 "quoted-printable")
470 (iso-8859-14 8 "quoted-printable")
471 (iso-8859-15 8 "quoted-printable")
472 (iso-2022-jp 7 "base64")
473 (iso-2022-jp-3 7 "base64")
474 (iso-2022-kr 7 "base64")
480 (shift_jis 8 "base64")
482 (iso-2022-jp-2 7 "base64")
483 (iso-2022-int-1 7 "base64")))
485 (defvar mime-transfer-level 7
486 "*A number of network transfer level. It should be bigger than 7.")
487 (make-variable-buffer-local 'mime-transfer-level)
489 (defsubst mime-encoding-name (transfer-level &optional not-omit)
490 (cond ((> transfer-level 8) "binary")
491 ((= transfer-level 8) "8bit")
494 (defvar mime-transfer-level-string
495 (mime-encoding-name mime-transfer-level 'not-omit)
496 "A string formatted version of mime-transfer-level")
497 (make-variable-buffer-local 'mime-transfer-level-string)
499 ;;; @@ about content transfer encoding
501 (defvar mime-content-transfer-encoding-priority-list
502 '(nil "8bit" "binary"))
504 ;;; @@ about message inserting
507 (defvar mime-edit-yank-ignored-field-list
508 '("Received" "Approved" "Path" "Replied" "Status"
509 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
510 "Delete these fields from original message when it is inserted
511 as message/rfc822 part.
512 Each elements are regexp of field-name.")
514 (defvar mime-edit-yank-ignored-field-regexp
516 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
519 (defvar mime-edit-message-inserter-alist nil)
520 (defvar mime-edit-mail-inserter-alist nil)
523 ;;; @@ about message splitting
526 (defcustom mime-edit-split-message t
527 "*Split large message if it is non-nil."
531 (defcustom mime-edit-message-default-max-lines 1000
532 "*Default maximum lines of a message."
536 (defcustom mime-edit-message-max-lines-alist
537 '((news-reply-mode . 500))
538 "Alist of major-mode vs maximum lines of a message.
539 If it is not specified for a major-mode,
540 `mime-edit-message-default-max-lines' is used."
544 (defconst mime-edit-split-ignored-field-regexp
545 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
547 (defcustom mime-edit-split-blind-field-regexp
548 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
549 "*Regular expression to match field-name to be ignored when split sending."
553 (defvar mime-edit-split-message-sender-alist nil)
555 (defvar mime-edit-news-reply-mode-server-running nil)
561 (defconst mime-edit-single-part-tag-regexp
562 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
563 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
565 (defconst mime-edit-quoted-single-part-tag-regexp
566 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
568 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
570 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
572 (defconst mime-edit-beginning-tag-regexp
573 (regexp-or mime-edit-single-part-tag-regexp
574 mime-edit-multipart-beginning-regexp))
576 (defconst mime-edit-end-tag-regexp
577 (regexp-or mime-edit-single-part-tag-regexp
578 mime-edit-multipart-end-regexp))
580 (defconst mime-edit-tag-regexp
581 (regexp-or mime-edit-single-part-tag-regexp
582 mime-edit-multipart-beginning-regexp
583 mime-edit-multipart-end-regexp))
585 (defvar mime-tag-format "--[[%s]]"
586 "*Control-string making a MIME tag.")
588 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
589 "*Control-string making a MIME tag with encoding.")
592 ;;; @@ multipart boundary
595 (defvar mime-multipart-boundary "Multipart"
596 "*Boundary of a multipart message.")
599 ;;; @@ optional header fields
602 (defvar mime-edit-insert-user-agent-field t
603 "*If non-nil, insert User-Agent header field.")
605 (defvar mime-edit-user-agent-value
606 (concat (mime-product-name mime-user-interface-product)
608 (mapconcat #'number-to-string
609 (mime-product-version mime-user-interface-product) ".")
611 (mime-product-code-name mime-user-interface-product)
613 (mime-product-name mime-library-product)
615 (mapconcat #'number-to-string
616 (mime-product-version mime-library-product) ".")
618 (mime-product-code-name mime-library-product)
620 (if (fboundp 'apel-version)
621 (concat (apel-version) " "))
622 (if (featurep 'xemacs)
623 (concat (cond ((and (featurep 'chise)
624 (boundp 'xemacs-chise-version))
625 (concat "CHISE-MULE/" xemacs-chise-version))
626 ((featurep 'utf-2000)
627 (concat "UTF-2000-MULE/" utf-2000-version))
628 ((featurep 'mule) "MULE"))
630 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
633 (substring emacs-version 0 (match-end 0))
634 (cond ((and (boundp 'xemacs-betaname)
636 ;; It does not exist in XEmacs
637 ;; versions prior to 20.3.
638 (concat " " xemacs-betaname))
639 ((and (boundp 'emacs-patch-level)
641 ;; It does not exist in FSF Emacs or in
642 ;; XEmacs versions earlier than 21.1.1.
643 (format " (patch %d)" emacs-patch-level))
645 " (" xemacs-codename ")"
646 ;; `xemacs-extra-name' has appeared in the
647 ;; development version of XEmacs 21.5-b8.
648 (if (and (boundp 'xemacs-extra-name)
649 (symbol-value 'xemacs-extra-name))
650 (concat " " (symbol-value 'xemacs-extra-name))
653 system-configuration ")")
654 " (" emacs-version ")"))
655 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
656 (substring emacs-version 0 (match-beginning 0))
659 (if (boundp 'enable-multibyte-characters)
661 " (" system-configuration ")"
662 (if enable-multibyte-characters
663 (concat " MULE/" mule-version)
664 " (with unibyte mode)")
665 (if (featurep 'meadow)
666 (let ((mver (Meadow-version)))
667 (if (string-match "^Meadow-" mver)
671 (concat "MULE/" mule-version
672 " (based on Emacs " ver ")"))
673 (concat "Emacs/" ver " (" system-configuration ")")))))
674 "Body of User-Agent field.
675 If variable `mime-edit-insert-user-agent-field' is not nil, it is
676 inserted into message header.")
682 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
683 "*Specify MIME tspecials.
684 Tspecials means any character that matches with it in header must be quoted.")
686 (defconst mime-edit-mime-version-value
687 (concat "1.0 (generated by " mime-edit-version ")")
688 "MIME version number.")
690 (defconst mime-edit-mime-version-field-for-message/partial
691 (concat "MIME-Version:"
692 (mime-encode-field-body
693 (concat " 1.0 (split by " mime-edit-version ")\n")
695 "MIME version field for message/partial.")
698 ;;; @ keymap and menu
701 (defvar mime-edit-mode-flag nil)
702 (make-variable-buffer-local 'mime-edit-mode-flag)
704 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
705 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
706 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
707 "Keymap for MIME-Edit mode commands to insert entity or set status.")
709 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
710 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
711 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
712 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
713 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
714 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
715 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
716 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
717 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
718 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
720 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
721 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
722 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
723 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
724 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
725 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
726 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
727 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
728 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
729 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
731 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
732 "Keymap prefix for MIME-Edit mode commands about enclosure.")
733 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
734 "Keymap for MIME-Edit mode commands about enclosure.")
736 (define-key mime-edit-mode-enclosure-map
737 "\C-a" 'mime-edit-enclose-alternative-region)
738 (define-key mime-edit-mode-enclosure-map
739 "\C-p" 'mime-edit-enclose-parallel-region)
740 (define-key mime-edit-mode-enclosure-map
741 "\C-m" 'mime-edit-enclose-mixed-region)
742 (define-key mime-edit-mode-enclosure-map
743 "\C-d" 'mime-edit-enclose-digest-region)
744 (define-key mime-edit-mode-enclosure-map
745 "\C-s" 'mime-edit-enclose-pgp-signed-region)
746 (define-key mime-edit-mode-enclosure-map
747 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
748 (define-key mime-edit-mode-enclosure-map
749 "\C-q" 'mime-edit-enclose-quote-region)
751 (defvar mime-edit-mode-map (make-sparse-keymap)
752 "Keymap for MIME-Edit mode commands.")
753 (define-key mime-edit-mode-map
754 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
755 (define-key mime-edit-mode-map
756 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
758 (defconst mime-edit-menu-title "MIME-Edit")
760 (defconst mime-edit-menu-list
761 '((mime-help "Describe MIME editor mode" mime-edit-help)
762 (file "Insert File" mime-edit-insert-file)
763 (external "Insert External" mime-edit-insert-external)
764 (voice "Insert Voice" mime-edit-insert-voice)
765 (message "Insert Message" mime-edit-insert-message)
766 (mail "Insert Mail" mime-edit-insert-mail)
767 (signature "Insert Signature" mime-edit-insert-signature)
768 (text "Insert Text" mime-edit-insert-text)
769 (tag "Insert Tag" mime-edit-insert-tag)
770 (alternative "Enclose as alternative"
771 mime-edit-enclose-alternative-region)
772 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
773 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
774 (digest "Enclose as digest" mime-edit-enclose-digest-region)
775 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
776 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
777 (quote "Verbatim region" mime-edit-enclose-quote-region)
778 (key "Insert Public Key" mime-edit-insert-key)
779 (split "Set splitting" mime-edit-set-split)
780 (sign "PGP sign" mime-edit-set-sign)
781 (encrypt "PGP encrypt" mime-edit-set-encrypt)
782 (preview "Preview Message" mime-edit-preview-message)
783 (level "Toggle transfer-level" mime-edit-toggle-transfer-level))
784 "MIME-edit menubar entry.")
786 (cond ((featurep 'xemacs)
787 ;; modified by Pekka Marjola <pema@iki.fi>
788 ;; 1995/9/5 (c.f. [tm-en:69])
789 (defun mime-edit-define-menu-for-xemacs ()
790 "Define menu for XEmacs."
791 (cond ((featurep 'menubar)
792 (make-local-variable 'current-menubar)
793 (set-buffer-menubar current-menubar)
796 (cons mime-edit-menu-title
799 (vector (nth 1 item)(nth 2 item)
800 mime-edit-mode-flag)))
801 mime-edit-menu-list))))))
803 ;; modified by Steven L. Baur <steve@miranova.com>
804 ;; 1995/12/6 (c.f. [tm-en:209])
805 (or (boundp 'mime-edit-popup-menu-for-xemacs)
806 (setq mime-edit-popup-menu-for-xemacs
807 (append '("MIME Commands" "---")
808 (mapcar (function (lambda (item)
812 mime-edit-menu-list)))))
813 ((>= emacs-major-version 19)
814 (define-key mime-edit-mode-map [menu-bar mime-edit]
815 (cons mime-edit-menu-title
816 (make-sparse-keymap mime-edit-menu-title)))
819 (define-key mime-edit-mode-map
820 (vector 'menu-bar 'mime-edit (car item))
821 (cons (nth 1 item)(nth 2 item)))))
822 (reverse mime-edit-menu-list))))
828 (defvar mime-edit-touched-flag nil)
831 (defun mime-edit-mode ()
832 "MIME minor mode for editing the tagged MIME message.
834 In this mode, basically, the message is composed in the tagged MIME
835 format. The message tag looks like:
837 --[[text/plain; charset=ISO-2022-JP][7bit]]
839 The tag specifies the MIME content type, subtype, optional parameters
840 and transfer encoding of the message following the tag. Messages
841 without any tag are treated as `text/plain' by default. Charset and
842 transfer encoding are automatically defined unless explicitly
843 specified. Binary messages such as audio and image are usually
844 hidden. The messages in the tagged MIME format are automatically
845 translated into a MIME compliant message when exiting this mode.
847 Available charsets depend on Emacs version being used. The following
848 lists the available charsets of each emacs.
850 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
851 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
852 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
853 CN-BIG5 and ISO-2022-INT-1 are available.
855 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
856 be used to represent multilingual text in intermixed manner. Any
857 languages that has no registered charset are represented as either
858 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
860 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
861 without mule, please set variable `default-mime-charset'. This
862 variable must be symbol of which name is a MIME charset.
864 If you want to add more charsets in mule, please set variable
865 `charsets-mime-charset-alist'. This variable must be alist of which
866 key is list of charset and value is symbol of MIME charset. If name
867 of coding-system is different as MIME charset, please set variable
868 `mime-charset-coding-system-alist'. This variable must be alist of
869 which key is MIME charset and value is coding-system.
871 Following commands are available in addition to major mode commands:
874 \\[mime-edit-insert-text] insert a text message.
875 \\[mime-edit-insert-file] insert a (binary) file.
876 \\[mime-edit-insert-external] insert a reference to external body.
877 \\[mime-edit-insert-voice] insert a voice message.
878 \\[mime-edit-insert-message] insert a mail or news message.
879 \\[mime-edit-insert-mail] insert a mail message.
880 \\[mime-edit-insert-signature] insert a signature file at end.
881 \\[mime-edit-insert-key] insert PGP public key.
882 \\[mime-edit-insert-tag] insert a new MIME tag.
884 \[make enclosure (maybe multipart)\]
885 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
886 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
887 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
888 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
889 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
890 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
891 \\[mime-edit-enclose-quote-region] enclose as verbose mode
892 (to avoid to expand tags)
895 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
896 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
897 \\[mime-edit-set-split] set message splitting mode.
898 \\[mime-edit-set-sign] set PGP-sign mode.
899 \\[mime-edit-set-encrypt] set PGP-encryption mode.
900 \\[mime-edit-preview-message] preview editing MIME message.
901 \\[mime-edit-exit] exit and translate into a MIME
903 \\[mime-edit-help] show this help.
904 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
907 Additional commands are available in some major modes:
908 C-c C-c exit, translate and run the original command.
909 C-c C-s exit, translate and run the original command.
911 The following is a message example written in the tagged MIME format.
912 TABs at the beginning of the line are not a part of the message:
914 This is a conventional plain text. It should be translated
917 This is also a plain text. But, it is explicitly specified as
919 --[[text/plain; charset=ISO-8859-1]]
920 This is also a plain text. But charset is specified as
923 ¡Hola! Buenos días. ¿Cómo está usted?
925 This is a <bold>enriched text</bold>.
926 --[[image/gif][base64]]...image encoded in base64 here...
927 --[[audio/basic][base64]]...audio encoded in base64 here...
929 User customizable variables (not documented all of them):
931 Specifies a key prefix for MIME minor mode commands.
933 mime-ignore-preceding-spaces
934 Preceding white spaces in a message body are ignored if non-nil.
936 mime-ignore-trailing-spaces
937 Trailing white spaces in a message body are ignored if non-nil.
940 Hide a non-textual body message encoded in base64 after insertion
944 A number of network transfer level. It should be bigger than 7.
945 If you are in 8bit-through environment, please set 8.
947 mime-edit-voice-recorder
948 Specifies a function to record a voice message and encode it.
949 The function `mime-edit-voice-recorder-for-sun' is for Sun
953 Turning on MIME mode calls the value of mime-edit-mode-hook, if
956 mime-edit-translate-hook
957 The value of mime-edit-translate-hook is called just before translating
958 the tagged MIME format into a MIME compliant message if it is
959 non-nil. If the hook call the function mime-edit-insert-signature,
960 the signature file will be inserted automatically.
963 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
966 (if mime-edit-mode-flag
968 (if mime-edit-touched-flag
970 (make-local-variable 'mime-edit-touched-flag)
971 (setq mime-edit-touched-flag t)
972 (turn-on-mime-edit))))
975 (cond ((featurep 'xemacs)
976 (add-minor-mode 'mime-edit-mode-flag
977 '((" MIME-Edit " mime-transfer-level-string))
982 (set-alist 'minor-mode-alist
984 '((" MIME-Edit " mime-transfer-level-string)))
985 (set-alist 'minor-mode-map-alist
987 mime-edit-mode-map)))
991 (defun turn-on-mime-edit ()
992 "Unconditionally turn on MIME-Edit mode."
994 (if mime-edit-mode-flag
995 (error "You are already editing a MIME message.")
996 (setq mime-edit-mode-flag t)
998 ;; Set transfer level into mode line
1000 (setq mime-transfer-level-string
1001 (mime-encoding-name mime-transfer-level 'not-omit))
1002 (force-mode-line-update)
1004 ;; Define menu for XEmacs.
1005 (if (featurep 'xemacs)
1006 (mime-edit-define-menu-for-xemacs))
1008 (make-local-variable 'paragraph-start)
1009 (setq paragraph-start
1010 (regexp-or mime-edit-single-part-tag-regexp
1012 (make-local-variable 'paragraph-separate)
1013 (setq paragraph-separate
1014 (regexp-or mime-edit-single-part-tag-regexp
1015 paragraph-separate))
1016 (run-hooks 'mime-edit-mode-hook)
1019 (substitute-command-keys
1020 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
1023 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1026 (defun mime-edit-exit (&optional nomime no-error)
1027 "Translate the tagged MIME message into a MIME compliant message.
1028 With no argument encode a message in the buffer into MIME, otherwise
1029 just return to previous mode."
1031 (if (not mime-edit-mode-flag)
1033 (error "You aren't editing a MIME message."))
1036 (run-hooks 'mime-edit-translate-hook)
1037 (mime-edit-translate-buffer)))
1038 ;; Restore previous state.
1039 (setq mime-edit-mode-flag nil)
1040 (if (and (featurep 'xemacs)
1041 (featurep 'menubar))
1042 (delete-menu-item (list mime-edit-menu-title)))
1043 (set-buffer-modified-p (buffer-modified-p))
1044 (run-hooks 'mime-edit-exit-hook)
1045 (message "Exit MIME editor mode.")))
1047 (defun mime-edit-maybe-translate ()
1049 (mime-edit-exit nil t)
1050 (call-interactively 'mime-edit-maybe-split-and-send))
1052 (defun mime-edit-help ()
1053 "Show help message about MIME mode."
1055 (with-output-to-temp-buffer "*Help*"
1056 (princ "MIME editor mode:\n")
1057 (princ (documentation 'mime-edit-mode))
1058 (print-help-return-message)))
1060 (defun mime-edit-insert-text (&optional subtype)
1061 "Insert a text message.
1062 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1063 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1065 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1067 (if (looking-at mime-edit-single-part-tag-regexp)
1069 ;; Make a space between the following message.
1072 (if (and (member (cadr ret) '("enriched"))
1073 (fboundp 'enriched-mode))
1075 (if (boundp 'enriched-mode)
1076 (enriched-mode -1))))))
1078 (defun mime-edit-insert-file (file &optional verbose)
1079 "Insert a message from a file."
1080 (interactive "fInsert file as MIME message: \nP")
1081 (let* ((guess (mime-find-file-type file))
1082 (type (nth 0 guess))
1083 (subtype (nth 1 guess))
1084 (parameters (nth 2 guess))
1085 (encoding (nth 3 guess))
1086 (disposition-type (nth 4 guess))
1087 (disposition-params (nth 5 guess)))
1089 (setq type (mime-prompt-for-type type)
1090 subtype (mime-prompt-for-subtype type subtype)))
1091 (if (or (interactive-p) verbose)
1092 (setq encoding (mime-prompt-for-encoding encoding)))
1093 (if (or (consp parameters) (stringp disposition-type))
1094 (let ((rest parameters) cell attribute value)
1095 (setq parameters "")
1097 (setq cell (car rest))
1098 (setq attribute (car cell))
1099 (setq value (cdr cell))
1100 (if (eq value 'file)
1101 (setq value (std11-wrap-as-quoted-string
1102 (file-name-nondirectory file))))
1103 (setq parameters (concat parameters "; " attribute "=" value))
1104 (setq rest (cdr rest)))
1105 (if disposition-type
1108 (concat parameters "\n"
1109 "Content-Disposition: " disposition-type))
1110 (setq rest disposition-params)
1112 (setq cell (car rest))
1113 (setq attribute (car cell))
1114 (setq value (cdr cell))
1115 (if (eq value 'file)
1116 (setq value (std11-wrap-as-quoted-string
1117 (file-name-nondirectory file))))
1119 (concat parameters "; " attribute "=" value))
1120 (setq rest (cdr rest)))))))
1121 (mime-edit-insert-tag type subtype parameters)
1122 (mime-edit-insert-binary-file file encoding)))
1124 (defun mime-edit-insert-external ()
1125 "Insert a reference to external body."
1127 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1129 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1131 (let* ((pritype (mime-prompt-for-type))
1132 (subtype (mime-prompt-for-subtype pritype))
1133 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1136 (insert "Content-Type: "
1137 pritype "/" subtype (or parameters "") "\n")))
1138 (if (and (not (eobp))
1139 (not (looking-at mime-edit-single-part-tag-regexp)))
1140 (insert (mime-make-text-tag) "\n")))
1142 (defun mime-edit-insert-voice ()
1143 "Insert a voice message."
1147 "What transfer encoding: "
1148 (mime-encoding-alist) nil t nil)))
1149 (mime-edit-insert-tag "audio" "basic" nil)
1150 (mime-edit-define-encoding encoding)
1152 (narrow-to-region (point)(point))
1154 (funcall mime-edit-voice-recorder encoding)
1157 (add-text-properties
1158 (point-min)(point-max) '(invisible t mime-edit-invisible t))
1159 (goto-char (point-max)))))))
1161 (defun mime-edit-insert-signature (&optional arg)
1162 "Insert a signature file."
1164 (let ((signature-insert-hook
1167 (let ((items (mime-find-file-type signature-file-name)))
1168 (apply (function mime-edit-insert-tag)
1169 (car items) (cadr items) (list (caddr items))))))))
1170 (insert-signature arg)))
1173 ;; Insert a new tag around a point.
1175 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1176 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1177 If nothing is inserted, return nil."
1180 (mime-edit-goto-tag)
1181 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1182 (< (match-beginning 0) p)
1183 (< p (match-end 0)))
1184 (goto-char (match-beginning 0))
1191 (mime-prompt-for-type)))
1194 (mime-prompt-for-subtype pritype)))
1197 (mime-prompt-for-parameters pritype subtype delimiter)))
1198 ;; Make a new MIME tag.
1199 (setq newtag (mime-make-tag pritype subtype parameters))
1200 ;; Find an current MIME tag.
1203 (if (mime-edit-goto-tag)
1204 (buffer-substring (match-beginning 0) (match-end 0))
1205 ;; Assume content type is 'text/plan'.
1206 (mime-make-tag "text" "plain"))))
1207 ;; We are only interested in TEXT.
1209 (not (mime-test-content-type
1210 (mime-edit-get-contype oldtag) "text")))
1213 (if (or (not oldtag) ;Not text
1214 (or mime-ignore-same-text-tag
1215 (not (string-equal oldtag newtag))))
1217 ;; Mark the beginning of the tag for convenience.
1218 (push-mark (point) 'nomsg)
1219 (insert newtag "\n")
1220 (list pritype subtype parameters) ;New tag is created.
1222 ;; Restore previous point.
1224 nil ;Nothing is created.
1227 (defun mime-edit-insert-binary-file (file &optional encoding)
1228 "Insert binary FILE at point.
1229 Optional argument ENCODING specifies an encoding method such as base64."
1230 (let* ((tagend (1- (point))) ;End of the tag
1231 (hide-p (and mime-auto-hide-body
1234 (let ((en (downcase encoding)))
1235 (or (string-equal en "7bit")
1236 (string-equal en "8bit")
1237 (string-equal en "binary")))))))
1239 (narrow-to-region (point)(point))
1240 (mime-insert-encoded-file file encoding)
1242 (add-text-properties
1243 (point-min)(point-max) '(invisible t mime-edit-invisible t)))
1244 (goto-char (point-max)))
1246 (looking-at mime-edit-tag-regexp)
1247 (= (point)(point-max))
1248 (mime-edit-insert-tag "text" "plain"))
1249 ;; Define encoding even if it is 7bit.
1250 (if (stringp encoding)
1252 (goto-char tagend) ; Make sure which line the tag is on.
1253 (mime-edit-define-encoding encoding)))))
1256 ;; Commands work on a current message flagment.
1258 (defun mime-edit-goto-tag ()
1259 "Search for the beginning of the tagged MIME message."
1260 (let ((current (point)))
1261 (if (looking-at mime-edit-tag-regexp)
1263 ;; At first, go to the end.
1264 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1265 (goto-char (1- (match-beginning 0))) ;For multiline tag
1268 (goto-char (point-max))))
1269 ;; Then search for the beginning.
1270 (re-search-backward mime-edit-end-tag-regexp nil t)
1271 (or (looking-at mime-edit-beginning-tag-regexp)
1272 ;; Restore previous point.
1277 (defun mime-edit-content-beginning ()
1278 "Return the point of the beginning of content."
1280 (let ((beg (save-excursion
1281 (beginning-of-line) (point))))
1282 (if (mime-edit-goto-tag)
1283 (let ((top (point)))
1284 (goto-char (match-end 0))
1285 (if (and (= beg top)
1286 (= (following-char) ?\^M))
1290 ;; Default text/plain tag.
1291 (goto-char (point-min))
1293 (concat "\n" (regexp-quote mail-header-separator)
1294 (if mime-ignore-preceding-spaces
1295 "[ \t\n]*\n" "\n")) nil 'move)
1298 (defun mime-edit-content-end ()
1299 "Return the point of the end of content."
1301 (if (mime-edit-goto-tag)
1303 (goto-char (1+ (match-end 0)))
1304 (if (get-text-property (point) 'mime-edit-invisible)
1305 (or (next-single-property-change (point) 'mime-edit-invisible)
1307 ;; Move to the end of this text.
1308 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1309 ;; Don't forget a multiline tag.
1310 (goto-char (match-beginning 0)))
1312 ;; Assume the message begins with text/plain.
1313 (goto-char (mime-edit-content-beginning))
1314 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1315 ;; Don't forget a multiline tag.
1316 (goto-char (match-beginning 0)))
1319 (defun mime-edit-define-charset (charset)
1320 "Set charset of current tag to CHARSET."
1322 (if (mime-edit-goto-tag)
1323 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1324 (delete-region (match-beginning 0) (match-end 0))
1327 (mime-edit-set-parameter
1328 (mime-edit-get-contype tag)
1330 (let ((comment (get charset 'mime-charset-comment)))
1332 (concat (upcase (symbol-name charset)) " (" comment ")")
1333 (upcase (symbol-name charset)))))
1334 (mime-edit-get-encoding tag)))))))
1336 (defun mime-edit-define-encoding (encoding)
1337 "Set encoding of current tag to ENCODING."
1339 (if (mime-edit-goto-tag)
1340 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1341 (delete-region (match-beginning 0) (match-end 0))
1342 (insert (mime-create-tag (mime-edit-get-contype tag) encoding))))))
1344 (defun mime-edit-choose-charset ()
1345 "Choose charset of a text following current point."
1346 (detect-mime-charset-region (point) (mime-edit-content-end)))
1348 (defun mime-make-text-tag (&optional subtype)
1349 "Make a tag for a text after current point.
1350 Subtype of text type can be specified by an optional argument SUBTYPE.
1351 Otherwise, it is obtained from mime-content-types."
1352 (let* ((pritype "text")
1353 (subtype (or subtype
1354 (car (car (cdr (assoc pritype mime-content-types)))))))
1355 ;; Charset should be defined later.
1356 (mime-make-tag pritype subtype)))
1359 ;; Tag handling functions
1361 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1362 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1363 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1367 (defun mime-create-tag (contype &optional encoding)
1368 "Make a tag with CONTENT-TYPE and optional ENCODING."
1369 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1372 (defun mime-edit-get-contype (tag)
1373 "Return Content-Type (including parameters) of TAG."
1375 (or (string-match mime-edit-single-part-tag-regexp tag)
1376 (string-match mime-edit-multipart-beginning-regexp tag)
1377 (string-match mime-edit-multipart-end-regexp tag))
1378 (substring tag (match-beginning 1) (match-end 1))))
1380 (defun mime-edit-get-encoding (tag)
1381 "Return encoding of TAG."
1383 (string-match mime-edit-single-part-tag-regexp tag)
1385 (not (= (match-beginning 3) (match-end 3)))
1386 (substring tag (match-beginning 3) (match-end 3))))
1388 (defun mime-get-parameter (contype parameter)
1389 "For given CONTYPE return value for PARAMETER.
1390 Nil if no such parameter."
1394 (regexp-quote parameter)
1395 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1397 (substring contype (match-beginning 1) (match-end 1))
1398 nil ;No such parameter
1401 (defun mime-edit-set-parameter (contype parameter value)
1402 "For given CONTYPE set PARAMETER to VALUE."
1403 (let (ctype opt-fields)
1404 (if (string-match "\n[^ \t\n\r]+:" contype)
1405 (setq ctype (substring contype 0 (match-beginning 0))
1406 opt-fields (substring contype (match-beginning 0)))
1407 (setq ctype contype))
1411 (regexp-quote parameter)
1412 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1415 (concat (substring ctype 0 (match-beginning 1))
1417 (substring ctype (match-end 1))
1419 (concat ctype "; " parameter "=" value opt-fields)
1422 (defun mime-strip-parameters (contype)
1423 "Return primary content-type and subtype without parameters for CONTYPE."
1424 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1425 (substring contype (match-beginning 1) (match-end 1)) nil))
1427 (defun mime-test-content-type (contype type &optional subtype)
1428 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1429 (and (stringp contype)
1432 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1433 (downcase contype))))
1438 (defun mime-find-file-type (file)
1439 "Guess Content-Type, subtype, and parameters from FILE."
1441 (guesses mime-file-types))
1442 (while (and (not guess) guesses)
1443 (if (string-match (car (car guesses)) file)
1444 (setq guess (cdr (car guesses))))
1445 (setq guesses (cdr guesses)))
1448 (defun mime-prompt-for-type (&optional default)
1449 "Ask for Content-type."
1451 ;; Repeat until primary content type is specified.
1452 (while (string-equal type "")
1454 (completing-read "What content type: "
1457 'require-match ;Type must be specified.
1459 (if (string-equal type "")
1461 (message "Content type is required.")
1466 (defun mime-prompt-for-subtype (type &optional default)
1467 "Ask for subtype of media-type TYPE."
1468 (let ((subtypes (cdr (assoc type mime-content-types))))
1470 (assoc default subtypes))
1471 (setq default (car (car subtypes)))))
1476 "What content subtype: (default " default ") ")
1477 "What content subtype: ")
1478 (cdr (assoc type mime-content-types))
1480 'require-match ;Subtype must be specified.
1482 (if (string-equal answer "") default answer)))
1484 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1485 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1486 Optional DELIMITER specifies parameter delimiter (';' by default)."
1487 (let* ((delimiter (or delimiter "; "))
1492 (mime-prompt-for-parameters-1
1494 (cdr (assoc pritype mime-content-types))))))
1496 (if (and (stringp parameters)
1497 (not (string-equal parameters "")))
1498 (concat delimiter parameters)
1499 "" ;"" if no parameters
1502 (defun mime-prompt-for-parameters-1 (optlist)
1503 (apply (function append)
1504 (mapcar (function mime-prompt-for-parameter) optlist)))
1506 (defun mime-prompt-for-parameter (parameter)
1508 Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
1509 (let* ((prompt (car parameter))
1510 (choices (mapcar (function
1512 (if (consp e) e (list e))))
1514 (default (car (car choices)))
1520 (concat "What " prompt
1522 (if (string-equal default "") "\"\"" default)
1524 choices nil nil ""))
1525 ;; If nothing is selected, use default.
1526 (if (string-equal answer "")
1527 (setq answer default)))
1529 (read-string (concat "What " prompt ": "))))
1530 (cons (if (and answer
1531 (not (string-equal answer "")))
1533 ;; Note: control characters ignored!
1534 (if (string-match mime-tspecials-regexp answer)
1535 (concat "\"" answer "\"") answer)))
1536 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))))
1538 (defun mime-prompt-for-encoding (default)
1539 "Ask for Content-Transfer-Encoding."
1544 "What transfer encoding: "
1545 (mime-encoding-alist) nil t default))
1550 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1553 (defvar mime-edit-translate-buffer-hook
1554 '(mime-edit-pgp-enclose-buffer
1555 mime-edit-translate-body
1556 mime-edit-translate-header))
1558 (defun mime-edit-translate-header ()
1559 "Encode the message header into network representation."
1560 (mime-encode-header-in-buffer 'code-conversion)
1561 (run-hooks 'mime-edit-translate-header-hook))
1563 (defun mime-edit-translate-buffer ()
1564 "Encode the tagged MIME message in current buffer in MIME compliant message."
1567 (if (catch 'mime-edit-error
1569 (run-hooks 'mime-edit-translate-buffer-hook)))
1572 (error "Translation error!"))))
1574 (defun mime-edit-find-inmost ()
1575 (goto-char (point-min))
1576 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1577 (let ((bb (match-beginning 0))
1579 (type (buffer-substring (match-beginning 1)(match-end 1)))
1581 (setq end-exp (format "--}-<<%s>>\n" type))
1583 (if (re-search-forward end-exp nil t)
1584 (setq eb (match-beginning 0))
1585 (setq eb (point-max)))
1586 (narrow-to-region be eb)
1588 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1590 (narrow-to-region (match-beginning 0)(point-max))
1591 (mime-edit-find-inmost))
1593 (list type bb be eb)))))
1595 (defun mime-edit-process-multipart-1 (boundary)
1596 (let ((ret (mime-edit-find-inmost)))
1598 (let ((type (car ret))
1599 (bb (nth 1 ret))(be (nth 2 ret))
1601 (narrow-to-region bb eb)
1602 (delete-region bb be)
1603 (setq bb (point-min))
1604 (setq eb (point-max))
1607 (if (looking-at mime-edit-multipart-end-regexp)
1608 (let ((beg (match-beginning 0))
1609 (end (match-end 0)))
1610 (delete-region beg end)
1611 (or (looking-at mime-edit-beginning-tag-regexp)
1612 (looking-at mime-edit-multipart-end-regexp)
1614 (insert (concat (mime-make-text-tag) "\n")))))
1615 (cond ((string-equal type "quote")
1616 (mime-edit-enquote-region bb eb))
1617 ((string-equal type "pgp-signed")
1618 (mime-edit-sign-pgp-mime bb eb boundary))
1619 ((string-equal type "pgp-encrypted")
1620 (mime-edit-encrypt-pgp-mime bb eb boundary))
1621 ((string-equal type "kazu-signed")
1622 (mime-edit-sign-pgp-kazu bb eb boundary))
1623 ((string-equal type "kazu-encrypted")
1624 (mime-edit-encrypt-pgp-kazu bb eb boundary))
1625 ((string-equal type "smime-signed")
1626 (mime-edit-sign-smime bb eb boundary))
1627 ((string-equal type "smime-encrypted")
1628 (mime-edit-encrypt-smime bb eb boundary))
1631 (nth 2 (mime-edit-translate-region bb eb
1635 (format "--[[multipart/%s;
1636 boundary=\"%s\"][7bit]]\n"
1640 (defun mime-edit-enquote-region (beg end)
1643 (narrow-to-region beg end)
1645 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1646 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1647 (replace-match (concat "- " (substring tag 1))))))))
1649 (defun mime-edit-dequote-region (beg end)
1652 (narrow-to-region beg end)
1654 (while (re-search-forward
1655 mime-edit-quoted-single-part-tag-regexp nil t)
1656 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1657 (replace-match (concat "-" (substring tag 2))))))))
1659 (defvar mime-edit-pgp-user-id nil)
1661 (defun mime-edit-delete-trailing-whitespace ()
1664 (goto-char (point-min))
1665 (while (re-search-forward "[ \t]+$" nil t)
1666 (delete-region (match-beginning 0) (match-end 0))))))
1668 (defun mime-edit-sign-pgp-mime (beg end boundary)
1671 (let* ((from (std11-field-body "From" mail-header-separator))
1673 (narrow-to-region beg end)
1674 (mime-edit-translate-region beg end boundary)))
1676 (encoding (nth 1 ret))
1677 (pgp-boundary (concat "pgp-sign-" boundary))
1679 (mime-edit-delete-trailing-whitespace) ; RFC3156
1681 (insert (format "Content-Type: %s\n" ctype))
1683 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1685 (or (let ((pgg-default-user-id
1686 (or mime-edit-pgp-user-id
1688 (nth 1 (std11-extract-address-components from))
1689 pgg-default-user-id))))
1690 (pgg-sign-region (point-min)(point-max)))
1691 (throw 'mime-edit-error 'pgp-error))
1693 (cdr (assq 'hash-algorithm
1694 (cdar (with-current-buffer pgg-output-buffer
1695 (pgg-parse-armor-region
1696 (point-min)(point-max))))))
1699 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1702 (insert (format "--[[multipart/signed;
1704 protocol=\"application/pgp-signature\"][7bit]]
1706 " pgp-boundary micalg pgp-boundary))
1707 (goto-char (point-max))
1708 (insert (format "\n--%s
1709 Content-Type: application/pgp-signature
1710 Content-Transfer-Encoding: 7bit
1713 (insert-buffer-substring pgg-output-buffer)
1714 (goto-char (point-max))
1715 (insert (format "\n--%s--\n" pgp-boundary))))))
1717 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1719 (defun mime-edit-make-encrypt-recipient-header ()
1720 (let* ((names mime-edit-encrypt-recipient-fields-list)
1722 (std11-field-bodies (cons "From" names)
1723 nil mail-header-separator))
1726 (setq values (cdr values))))
1727 (header (and (stringp from)
1728 (if (string-equal from "")
1730 (format "From: %s\n" from))))
1732 (while (and names values)
1733 (let ((name (car names))
1734 (value (car values)))
1735 (and (stringp value)
1736 (or (string-equal value "")
1738 (setq header (concat header name ": " value "\n")
1739 recipients (if recipients
1740 (concat recipients " ," value)
1742 (setq names (cdr names)
1743 values (cdr values)))
1744 (vector from recipients header)))
1746 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1749 (let (from recipients header)
1750 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1751 (setq from (aref ret 0)
1752 recipients (aref ret 1)
1753 header (aref ret 2)))
1754 (narrow-to-region beg end)
1756 (mime-edit-translate-region beg end boundary))
1758 (encoding (nth 1 ret))
1759 (pgp-boundary (concat "pgp-" boundary)))
1762 (insert (format "Content-Type: %s\n" ctype))
1764 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1766 (mime-encode-header-in-buffer)
1767 (or (let ((pgg-default-user-id
1768 (or mime-edit-pgp-user-id
1770 (nth 1 (std11-extract-address-components from))
1771 pgg-default-user-id))))
1773 (point-min) (point-max)
1774 (mapcar (lambda (recipient)
1775 (nth 1 (std11-extract-address-components
1777 (split-string recipients
1778 "\\([ \t\n]*,[ \t\n]*\\)+"))))
1779 (throw 'mime-edit-error 'pgp-error))
1780 (delete-region (point-min)(point-max))
1782 (insert (format "--[[multipart/encrypted;
1784 protocol=\"application/pgp-encrypted\"][7bit]]
1786 Content-Type: application/pgp-encrypted
1789 Content-Type: application/octet-stream
1790 Content-Transfer-Encoding: 7bit
1792 " pgp-boundary pgp-boundary pgp-boundary))
1793 (insert-buffer-substring pgg-output-buffer)
1794 (goto-char (point-max))
1795 (insert (format "\n--%s--\n" pgp-boundary)))))))
1797 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1800 (narrow-to-region beg end)
1802 (mime-edit-translate-region beg end boundary))
1804 (encoding (nth 1 ret)))
1806 (insert (format "Content-Type: %s\n" ctype))
1808 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1810 (or (pgg-sign-region beg (point-max) 'clearsign)
1811 (throw 'mime-edit-error 'pgp-error))
1814 "--[[application/pgp; format=mime][7bit]]\n")
1817 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1819 (let (recipients header)
1820 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1821 (setq recipients (aref ret 1)
1822 header (aref ret 2)))
1824 (narrow-to-region beg end)
1826 (mime-edit-translate-region beg end boundary))
1828 (encoding (nth 1 ret)))
1831 (insert (format "Content-Type: %s\n" ctype))
1833 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1835 (or (pgg-encrypt-region beg (point-max) recipients)
1836 (throw 'mime-edit-error 'pgp-error))
1839 "--[[application/pgp; format=mime][7bit]]\n")
1842 (defun mime-edit-sign-smime (beg end boundary)
1846 (narrow-to-region beg end)
1847 (mime-edit-translate-region beg end boundary)))
1849 (encoding (nth 1 ret))
1850 (smime-boundary (concat "smime-sign-" boundary)))
1852 (insert (format "Content-Type: %s\n" ctype))
1854 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1856 (let (buffer-undo-list)
1857 (goto-char (point-min))
1858 (while (progn (end-of-line) (not (eobp)))
1861 (or (smime-sign-buffer)
1862 (throw 'mime-edit-error 'pgp-error)))
1864 (if (re-search-forward "^Content-Type:\\s-*" nil t)
1865 (let* ((start (match-beginning 0))
1866 (body (buffer-substring (match-end 0) (std11-field-end))))
1867 (delete-region start (line-beginning-position 2))
1869 (insert "--[[" body "][7bit]]\n")))))))
1871 (defun mime-edit-encrypt-smime (beg end boundary)
1875 (narrow-to-region beg end)
1876 (mime-edit-translate-region beg end boundary)))
1878 (encoding (nth 1 ret)))
1880 (insert (format "Content-Type: %s\n" ctype))
1882 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1884 (goto-char (point-min))
1885 (while (progn (end-of-line) (not (eobp)))
1888 (or (smime-encrypt-buffer)
1889 (throw 'mime-edit-error 'pgp-error))
1891 (if (re-search-forward "^Content-Type:\\s-*" nil t)
1892 (let* ((start (match-beginning 0))
1893 (body (buffer-substring (match-end 0) (std11-field-end))))
1894 (delete-region start (line-beginning-position 2))
1896 (insert "--[[" body "]]\n")))))))
1898 (defsubst replace-space-with-underline (str)
1899 (mapconcat (function
1906 (defun mime-edit-make-boundary ()
1907 (concat mime-multipart-boundary "_"
1908 (replace-space-with-underline (current-time-string))))
1910 (defun mime-edit-translate-body ()
1911 "Encode the tagged MIME body in current buffer in MIME compliant message."
1914 (let ((boundary (mime-edit-make-boundary))
1917 (while (mime-edit-process-multipart-1
1918 (format "%s-%d" boundary i))
1921 ;; We are interested in message body.
1924 (goto-char (point-min))
1926 (concat "\n" (regexp-quote mail-header-separator)
1927 (if mime-ignore-preceding-spaces
1928 "[ \t\n]*\n" "\n")) nil 'move)
1932 (goto-char (point-max))
1933 (and mime-ignore-trailing-spaces
1934 (re-search-backward "[^ \t\n]\n" beg t)
1937 (setq ret (mime-edit-translate-region
1939 (format "%s-%d" boundary i)))))
1940 (mime-edit-dequote-region (point-min)(point-max))
1941 (let ((contype (car ret)) ;Content-Type
1942 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1944 ;; Insert User-Agent field
1945 (and mime-edit-insert-user-agent-field
1946 (or (mail-position-on-field "User-Agent")
1947 (insert mime-edit-user-agent-value)))
1948 ;; Make primary MIME headers.
1949 (or (mail-position-on-field "MIME-Version")
1950 (insert mime-edit-mime-version-value))
1951 ;; Remove old Content-Type and other fields.
1953 (goto-char (point-min))
1954 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1955 (narrow-to-region (point-min) (point))
1956 (goto-char (point-min))
1957 (mime-delete-field "Content-Type")
1958 (mime-delete-field "Content-Transfer-Encoding"))
1959 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1960 (mail-position-on-field "Content-Type")
1964 (mail-position-on-field "Content-Transfer-Encoding")
1965 (insert encoding)))))))
1967 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
1968 "Translate single-part-tag to MIME header."
1969 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1970 (let* ((beg (match-beginning 0))
1972 (tag (buffer-substring beg end)))
1973 (delete-region beg end)
1974 (let ((contype (mime-edit-get-contype tag))
1975 (encoding (mime-edit-get-encoding tag)))
1976 (insert (concat prefix "--" boundary "\n"))
1978 (narrow-to-region (point)(point))
1979 (insert "Content-Type: " contype "\n")
1981 (insert "Content-Transfer-Encoding: " encoding "\n"))
1982 (mime-encode-header-in-buffer))
1986 (downcase encoding)))))))
1988 (defun mime-edit-translate-region (beg end &optional boundary multipart)
1990 (setq boundary (mime-edit-make-boundary)))
1993 (narrow-to-region beg end)
1994 (let ((tag nil) ;MIME tag
1995 (contype nil) ;Content-Type
1996 (encoding nil) ;Content-Transfer-Encoding
1997 (nparts 0)) ;Number of body parts
1998 ;; Normalize the body part by inserting appropriate message
1999 ;; tags for every message contents.
2000 (mime-edit-normalize-body)
2001 ;; Counting the number of Content-Type.
2002 (goto-char (point-min))
2003 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2004 (setq nparts (1+ nparts)))
2005 ;; Begin translation.
2007 ((and (<= nparts 1)(not multipart))
2008 ;; It's a singular message.
2009 (goto-char (point-min))
2010 (while (re-search-forward
2011 mime-edit-single-part-tag-regexp nil t)
2013 (buffer-substring (match-beginning 0) (match-end 0)))
2014 (delete-region (match-beginning 0) (1+ (match-end 0)))
2015 (setq contype (mime-edit-get-contype tag))
2016 (setq encoding (mime-edit-get-encoding tag))))
2018 ;; It's a multipart message.
2019 (goto-char (point-min))
2020 (let ((prio mime-content-transfer-encoding-priority-list)
2022 (when (setq part-info
2023 (mime-edit-translate-single-part-tag boundary))
2024 (and (setq nprio (member (cdr part-info) prio))
2026 (while (setq part-info
2027 (mime-edit-translate-single-part-tag boundary "\n"))
2028 (and (setq nprio (member (cdr part-info) prio))
2029 (setq prio nprio))))
2030 ;; Define Content-Type as "multipart/mixed".
2032 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2033 (setq encoding (car prio))
2034 ;; Insert the trailer.
2035 (goto-char (point-max))
2036 (insert "\n--" boundary "--\n"))))
2037 (list contype encoding boundary nparts)))))
2039 (defun mime-edit-normalize-body ()
2040 "Normalize the body part by inserting appropriate message tags."
2041 ;; Insert the first MIME tags if necessary.
2042 (goto-char (point-min))
2043 (if (not (looking-at mime-edit-single-part-tag-regexp))
2044 (insert (mime-make-text-tag) "\n"))
2045 ;; Check each tag, and add new tag or correct it if necessary.
2046 (goto-char (point-min))
2047 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2048 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2049 (contype (mime-edit-get-contype tag))
2050 (charset (mime-get-parameter contype "charset"))
2051 (encoding (mime-edit-get-encoding tag)))
2052 ;; Remove extra whitespaces after the tag.
2053 (if (looking-at "[ \t]+$")
2054 (delete-region (match-beginning 0) (match-end 0)))
2056 (end (mime-edit-content-end)))
2057 (if (= end (point-max))
2060 (or (looking-at mime-edit-beginning-tag-regexp)
2062 (insert (mime-make-text-tag) "\n")))
2063 (remove-text-properties beg end '(invisible mime-edit-invisible))
2066 ((mime-test-content-type contype "message")
2067 ;; Content-type "message" should be sent as is.
2069 ((mime-test-content-type contype "text")
2070 ;; Define charset for text if necessary.
2071 (setq charset (if charset
2072 (intern (downcase charset))
2073 (mime-edit-choose-charset)))
2074 (mime-edit-define-charset charset)
2075 (cond ((string-equal contype "text/x-rot13-47-48")
2078 (mule-caesar-region (point) (mime-edit-content-end))))
2079 ((string-equal contype "text/enriched")
2084 (end (mime-edit-content-end)))
2085 ;; Patch for hard newlines
2088 ;; (while (search-forward "\n" end t)
2089 ;; (put-text-property (match-beginning 0)
2092 ;; End patch for hard newlines
2093 (enriched-encode beg end nil)
2095 (if (search-forward "\n\n")
2096 (delete-region beg (match-end 0)))))))
2097 ;; Point is now on current tag.
2098 ;; Define encoding and encode text if necessary.
2099 (or encoding ;Encoding is not specified.
2102 (let ((ret (cdr (assq charset mime-charset-type-list))))
2104 (setq bits (car ret)
2107 conv "quoted-printable")))
2108 (if (<= bits mime-transfer-level)
2109 (mime-encoding-name bits)
2111 (beg (mime-edit-content-beginning)))
2112 (encode-mime-charset-region beg (mime-edit-content-end)
2114 ;; Protect "From " in beginning of line
2116 (narrow-to-region beg (mime-edit-content-end))
2118 (let (case-fold-search)
2119 (if (re-search-forward "^From " nil t)
2121 (if (memq charset '(iso-2022-jp
2126 (replace-match "\e(BFrom ")
2127 (re-search-forward "^From " nil t)))
2128 (setq encoding "quoted-printable"))))))
2129 ;; canonicalize line break code
2130 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2132 (narrow-to-region beg (mime-edit-content-end))
2134 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2135 ;; In a certain period, `replace-match' with "\\N"
2136 ;; converted 8-bit characters into multibyte string,
2137 ;; but it has been fixed at 2004-01-15.
2138 ;;(replace-match "\\1\r\n"))))
2143 (mime-encode-region beg (mime-edit-content-end)
2144 (or encoding "7bit"))
2145 (mime-edit-define-encoding encoding)))
2146 (goto-char (mime-edit-content-end)))
2147 ((null encoding) ;Encoding is not specified.
2148 ;; Application, image, audio, video, and any other
2149 ;; unknown content-type without encoding should be
2151 (let* ((encoding "base64") ;Encode in BASE64 by default.
2152 (beg (mime-edit-content-beginning))
2153 (end (mime-edit-content-end)))
2154 (mime-encode-region beg end encoding)
2155 (mime-edit-define-encoding encoding))
2156 (forward-line 1))))))
2158 (defun mime-delete-field (field)
2159 "Delete header FIELD."
2160 (let ((regexp (format "^%s:[ \t]*" field)))
2161 (goto-char (point-min))
2162 (while (re-search-forward regexp nil t)
2163 (delete-region (match-beginning 0)
2164 (1+ (std11-field-end))))))
2168 ;;; Platform dependent functions
2171 ;; Sun implementations
2173 (defun mime-edit-voice-recorder-for-sun (encoding)
2174 "Record voice in a buffer using Sun audio device,
2175 and insert data encoded as ENCODING."
2176 (message "Start the recording on %s. Type C-g to finish the recording..."
2178 (mime-insert-encoded-file "/dev/audio" encoding))
2181 ;;; @ Other useful commands.
2184 ;; Message forwarding commands as content-type "message/rfc822".
2186 (defun mime-edit-insert-message (&optional message)
2188 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2189 (if (and inserter (fboundp inserter))
2191 (mime-edit-insert-tag "message" "rfc822")
2192 (funcall inserter message))
2193 (message "Sorry, I don't have message inserter for your MUA."))))
2195 (defun mime-edit-insert-mail (&optional message)
2197 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2198 (if (and inserter (fboundp inserter))
2200 (mime-edit-insert-tag "message" "rfc822")
2201 (funcall inserter message))
2202 (message "Sorry, I don't have mail inserter for your MUA."))))
2204 (defun mime-edit-inserted-message-filter ()
2207 (let ((header-start (point))
2208 (case-fold-search t)
2211 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2212 (if (re-search-forward "^$" (mark t))
2213 (narrow-to-region header-start (match-beginning 0)))
2214 (goto-char header-start)
2215 (while (and (re-search-forward
2216 mime-edit-yank-ignored-field-regexp nil t)
2217 (setq beg (match-beginning 0))
2218 (setq end (1+ (std11-field-end))))
2219 (delete-region beg end))))))
2222 ;;; @ multipart enclosure
2225 (defun mime-edit-enclose-region-internal (type beg end)
2229 (narrow-to-region beg end)
2230 (insert (format "--<<%s>>-{\n" type))
2231 (goto-char (point-max))
2232 (insert (format "--}-<<%s>>\n" type))
2233 (goto-char (point-max)))
2234 (or (looking-at mime-edit-beginning-tag-regexp)
2236 (insert (mime-make-text-tag) "\n"))))
2238 (defun mime-edit-enclose-quote-region (beg end)
2240 (mime-edit-enclose-region-internal 'quote beg end))
2242 (defun mime-edit-enclose-mixed-region (beg end)
2244 (mime-edit-enclose-region-internal 'mixed beg end))
2246 (defun mime-edit-enclose-parallel-region (beg end)
2248 (mime-edit-enclose-region-internal 'parallel beg end))
2250 (defun mime-edit-enclose-digest-region (beg end)
2252 (mime-edit-enclose-region-internal 'digest beg end))
2254 (defun mime-edit-enclose-alternative-region (beg end)
2256 (mime-edit-enclose-region-internal 'alternative beg end))
2258 (defun mime-edit-enclose-pgp-signed-region (beg end)
2260 (mime-edit-enclose-region-internal 'pgp-signed beg end))
2262 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2264 (mime-edit-enclose-region-internal 'pgp-encrypted beg end))
2266 (defun mime-edit-enclose-kazu-signed-region (beg end)
2268 (mime-edit-enclose-region-internal 'kazu-signed beg end))
2270 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2272 (mime-edit-enclose-region-internal 'kazu-encrypted beg end))
2274 (defun mime-edit-enclose-smime-signed-region (beg end)
2276 (mime-edit-enclose-region-internal 'smime-signed beg end))
2278 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2280 (mime-edit-enclose-region-internal 'smime-encrypted beg end))
2282 (defun mime-edit-insert-key (&optional arg)
2283 "Insert a pgp public key."
2285 (mime-edit-insert-tag "application" "pgp-keys")
2286 (mime-edit-define-encoding "7bit")
2288 (if (and (not (eobp))
2289 (not (looking-at mime-edit-single-part-tag-regexp)))
2290 (insert (mime-make-text-tag) "\n")))
2296 (defun mime-edit-set-split (arg)
2299 (y-or-n-p "Do you want to enable split? ")))
2300 (setq mime-edit-split-message arg)
2302 (message "This message is enabled to split.")
2303 (message "This message is not enabled to split.")))
2305 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2306 "Toggle transfer-level is 7bit or 8bit through.
2308 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2310 (if (numberp transfer-level)
2311 (setq mime-transfer-level transfer-level)
2312 (if (< mime-transfer-level 8)
2313 (setq mime-transfer-level 8)
2314 (setq mime-transfer-level 7)))
2315 (message (format "Current transfer-level is %d bit"
2316 mime-transfer-level))
2317 (setq mime-transfer-level-string
2318 (mime-encoding-name mime-transfer-level 'not-omit))
2319 (force-mode-line-update))
2321 (defun mime-edit-set-transfer-level-7bit ()
2323 (mime-edit-toggle-transfer-level 7))
2325 (defun mime-edit-set-transfer-level-8bit ()
2327 (mime-edit-toggle-transfer-level 8))
2333 (defvar mime-edit-pgp-processing nil)
2334 (make-variable-buffer-local 'mime-edit-pgp-processing)
2336 (defun mime-edit-set-sign (arg)
2339 (y-or-n-p "Do you want to sign? ")))
2342 (or (memq 'sign mime-edit-pgp-processing)
2343 (setq mime-edit-pgp-processing
2344 (nconc mime-edit-pgp-processing
2345 (copy-sequence '(sign)))))
2346 (message "This message will be signed."))
2347 (setq mime-edit-pgp-processing
2348 (delq 'sign mime-edit-pgp-processing))
2349 (message "This message will not be signed.")))
2351 (defun mime-edit-set-encrypt (arg)
2354 (y-or-n-p "Do you want to encrypt? ")))
2357 (or (memq 'encrypt mime-edit-pgp-processing)
2358 (setq mime-edit-pgp-processing
2359 (nconc mime-edit-pgp-processing
2360 (copy-sequence '(encrypt)))))
2361 (message "This message will be encrypt."))
2362 (setq mime-edit-pgp-processing
2363 (delq 'encrypt mime-edit-pgp-processing))
2364 (message "This message will not be encrypt.")))
2366 (defun mime-edit-pgp-enclose-buffer ()
2367 (let ((beg (save-excursion
2368 (goto-char (point-min))
2369 (if (search-forward (concat "\n" mail-header-separator "\n"))
2372 (dolist (pgp-processing mime-edit-pgp-processing)
2373 (case pgp-processing
2375 (mime-edit-enclose-pgp-signed-region
2378 (mime-edit-enclose-pgp-encrypted-region
2379 beg (point-max))))))))
2385 (defun mime-edit-insert-partial-header (fields subject
2386 id number total separator)
2388 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2389 (insert mime-edit-mime-version-field-for-message/partial)
2391 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2392 id number total separator)))
2394 (defun mime-edit-split-and-send
2395 (&optional cmd lines mime-edit-message-max-length)
2399 (count-lines (point-min) (point-max))))
2400 (or mime-edit-message-max-length
2401 (setq mime-edit-message-max-length
2402 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2403 mime-edit-message-default-max-lines)))
2404 (let ((separator mail-header-separator)
2406 (replace-space-with-underline (current-time-string))
2407 "@" (system-name) "\"")))
2408 (run-hooks 'mime-edit-before-split-hook)
2409 (let ((the-buf (current-buffer))
2410 (copy-buf (get-buffer-create " *Original Message*"))
2411 (header (std11-header-string-except
2412 mime-edit-split-ignored-field-regexp separator))
2413 (subject (mail-fetch-field "subject"))
2414 (total (+ (/ lines mime-edit-message-max-length)
2415 (if (> (mod lines mime-edit-message-max-length) 0)
2421 mime-edit-split-message-sender-alist))
2425 (error "Split sender is not specified for `%s'." major-mode)))))
2426 (mime-edit-partial-number 1)
2429 (set-buffer copy-buf)
2431 (insert-buffer the-buf)
2433 (if (re-search-forward
2434 (concat "^" (regexp-quote separator) "$") nil t)
2435 (let ((he (match-beginning 0)))
2437 (narrow-to-region (point-min) he)))
2438 (goto-char (point-min))
2439 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2440 (delete-region (match-beginning 0)
2441 (1+ (std11-field-end))))))
2442 (while (< mime-edit-partial-number total)
2445 (set-buffer copy-buf)
2446 (setq data (buffer-substring
2449 (goto-line mime-edit-message-max-length)
2451 (delete-region (point-min)(point)))
2452 (mime-edit-insert-partial-header
2453 header subject id mime-edit-partial-number total separator)
2456 (message (format "Sending %d/%d..."
2457 mime-edit-partial-number total))
2458 (call-interactively command)
2459 (message (format "Sending %d/%d...done"
2460 mime-edit-partial-number total)))
2461 (setq mime-edit-partial-number
2462 (1+ mime-edit-partial-number)))
2465 (set-buffer copy-buf)
2466 (setq data (buffer-string))
2468 (mime-edit-insert-partial-header
2469 header subject id mime-edit-partial-number total separator)
2472 (message (format "Sending %d/%d..."
2473 mime-edit-partial-number total))
2474 (message (format "Sending %d/%d...done"
2475 mime-edit-partial-number total))))))
2477 (defun mime-edit-maybe-split-and-send (&optional cmd)
2479 (run-hooks 'mime-edit-before-send-hook)
2480 (let ((mime-edit-message-max-length
2481 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2482 mime-edit-message-default-max-lines))
2483 (lines (count-lines (point-min) (point-max))))
2484 (if (and (> lines mime-edit-message-max-length)
2485 mime-edit-split-message)
2486 (mime-edit-split-and-send cmd lines mime-edit-message-max-length))))
2489 ;;; @ preview message
2492 (defvar mime-edit-buffer nil) ; buffer local variable
2493 (defvar mime-edit-temp-message-buffer nil) ; buffer local variable
2495 (defun mime-edit-preview-message ()
2496 "preview editing MIME message."
2498 (let* ((str (buffer-string))
2499 (separator mail-header-separator)
2500 (the-buf (current-buffer))
2501 (buf-name (buffer-name))
2502 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2503 (buf (get-buffer temp-buf-name))
2504 (pgp-processing mime-edit-pgp-processing))
2507 (switch-to-buffer buf)
2509 (setq buf (get-buffer-create temp-buf-name))
2510 (switch-to-buffer buf))
2512 (setq major-mode 'mime-temp-message-mode)
2513 (make-local-variable 'mail-header-separator)
2514 (setq mail-header-separator separator)
2515 (make-local-variable 'mime-edit-buffer)
2516 (setq mime-edit-buffer the-buf)
2517 (setq mime-edit-pgp-processing pgp-processing)
2519 (run-hooks 'mime-edit-translate-hook)
2520 (mime-edit-translate-buffer)
2521 (goto-char (point-min))
2522 (if (re-search-forward
2523 (concat "^" (regexp-quote separator) "$"))
2526 (make-local-variable 'mime-edit-temp-message-buffer)
2527 (setq mime-edit-temp-message-buffer buf)))
2529 (defun mime-edit-quitting-method ()
2530 "Quitting method for mime-view."
2531 (let* ((temp mime-edit-temp-message-buffer)
2533 (mime-preview-kill-buffer)
2535 (setq buf mime-edit-buffer)
2537 (switch-to-buffer buf)))
2539 (set-alist 'mime-preview-quitting-method-alist
2540 'mime-temp-message-mode
2541 #'mime-edit-quitting-method)
2547 (defvar mime-edit-again-ignored-field-regexp
2548 (concat "^\\(" "Content-.*\\|Mime-Version"
2549 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2551 "Regexp for deleted header fields when `mime-edit-again' is called.")
2553 (defsubst eliminate-top-spaces (string)
2554 "Eliminate top sequence of space or tab in STRING."
2555 (if (string-match "^[ \t]+" string)
2556 (substring string (match-end 0))
2559 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2562 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2563 '(("application/pgp-encrypted" . pgp-encrypted)
2564 ("application/pgp-signature" . pgp-signed))))
2565 (mime-content-type-subtype content-type)))
2566 (boundary (mime-content-type-parameter content-type "boundary"))
2567 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2568 (re-search-forward boundary-pat nil t)
2569 (let ((bb (match-beginning 0)) eb tag)
2570 (setq tag (format "\n--<<%s>>-{\n" subtype))
2573 (setq bb (+ bb (length tag)))
2575 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2577 (setq eb (match-beginning 0))
2578 (replace-match (format "--}-<<%s>>\n" subtype))
2580 (narrow-to-region bb eb)
2581 (goto-char (point-min))
2582 (while (re-search-forward boundary-pat nil t)
2583 (let ((beg (match-beginning 0))
2585 (delete-region beg (match-end 0))
2587 (if (re-search-forward boundary-pat nil t)
2588 (setq end (match-beginning 0))
2589 (setq end (point-max)))
2591 (narrow-to-region beg end)
2593 ((eq subtype 'pgp-encrypted)
2596 (goto-char (point-min))
2597 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2600 (save-window-excursion
2601 (pgg-decrypt-region (match-beginning 0)
2603 (delete-region (point-min)(point-max))))
2604 (insert-buffer-substring pgg-output-buffer)
2605 (mime-edit-decode-message-in-buffer
2606 nil not-decode-text)
2607 (delete-region (goto-char (point-min))
2608 (if (search-forward "\n\n" nil t)
2611 (goto-char (point-max))))
2613 (mime-edit-decode-message-in-buffer
2614 (if (eq subtype 'digest)
2616 (make-mime-content-type 'message 'rfc822)))
2618 (goto-char (point-max))))))))))
2619 (goto-char (point-min))
2620 (or (= (point-min) 1)
2621 (delete-region (point-min)
2622 (if (search-forward "\n\n" nil t)
2626 (defun mime-edit-decode-single-part-in-buffer
2627 (content-type not-decode-text &optional content-disposition)
2628 (let* ((type (mime-content-type-primary-type content-type))
2629 (subtype (mime-content-type-subtype content-type))
2630 (ctype (format "%s/%s" type subtype))
2632 (pstr (let ((bytes (+ 14 (length ctype))))
2633 (mapconcat (function
2635 (if (string= (car attr) "charset")
2637 (setq charset (cdr attr))
2639 (let* ((str (concat (car attr)
2642 (setq bytes (+ bytes bs 2))
2645 (setq bytes (+ bs 1))
2648 (mime-content-type-parameters content-type) "")))
2651 (limit (save-excursion
2652 (if (search-forward "\n\n" nil t)
2655 (mime-content-disposition-type content-disposition))
2657 (if disposition-type
2658 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2659 (mapconcat (function
2664 (if (string-equal "filename"
2666 (std11-wrap-as-quoted-string
2670 (setq bytes (+ bytes bs 2))
2673 (setq bytes (+ bs 1))
2676 (mime-content-disposition-parameters
2677 content-disposition)
2679 (if disposition-type
2680 (setq pstr (format "%s\nContent-Disposition: %s%s"
2681 pstr disposition-type disposition-str)))
2683 (if (re-search-forward
2684 "^Content-Transfer-Encoding:" limit t)
2685 (let ((beg (match-beginning 0))
2686 (hbeg (match-end 0))
2687 (end (std11-field-end limit)))
2690 (eliminate-top-spaces
2691 (std11-unfold-string
2692 (buffer-substring hbeg end)))))
2693 (if (or charset (eq type 'text))
2695 (delete-region beg (1+ end))
2696 (goto-char (point-min))
2697 (if (search-forward "\n\n" nil t)
2700 (match-end 0)(point-max) encoding)
2702 encoding nil))))))))
2703 (if (and (eq type 'text)
2704 (or encoded (not not-decode-text)))
2707 (goto-char (point-min))
2708 (while (re-search-forward "\r\n" nil t)
2709 (replace-match "\n")))
2710 (decode-mime-charset-region (point-min)(point-max)
2711 (or charset default-mime-charset))))
2712 (let ((he (if (re-search-forward "^$" nil t)
2715 (if (and (eq type 'text)
2716 (eq subtype 'x-rot13-47-48))
2717 (mule-caesar-region he (point-max)))
2718 (if (= (point-min) 1)
2724 (format "%s/%s%s" type subtype pstr)
2726 (delete-region (point-min) he)
2728 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2732 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2735 (goto-char (point-min))
2736 (let ((ctl (or (mime-read-Content-Type)
2737 default-content-type)))
2739 (let ((type (mime-content-type-primary-type ctl)))
2741 ((and (eq type 'application)
2742 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2743 (delete-region (point-min)(point-max)))
2744 ((eq type 'multipart)
2745 (mime-edit-decode-multipart-in-buffer ctl not-decode-text))
2747 (mime-edit-decode-single-part-in-buffer
2748 ctl not-decode-text (mime-read-Content-Disposition)))))
2750 (decode-mime-charset-region (point-min) (point-max)
2751 default-mime-charset)))
2752 (if (= (point-min) 1)
2755 (std11-narrow-to-header)
2756 (goto-char (point-min))
2757 (while (re-search-forward
2758 mime-edit-again-ignored-field-regexp nil t)
2759 (delete-region (match-beginning 0) (1+ (std11-field-end)))))
2760 (mime-decode-header-in-buffer (not not-decode-text)))))))
2763 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2764 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2765 Content-Type and Content-Transfer-Encoding header fields will be
2766 converted to MIME-Edit tags."
2768 (goto-char (point-min))
2770 (concat "\n" (regexp-quote mail-header-separator) "\n")
2772 (replace-match "\n\n"))
2773 (mime-edit-decode-message-in-buffer nil not-decode-text)
2774 (goto-char (point-min))
2776 (and (re-search-forward "^$")
2777 (replace-match mail-header-separator)))
2779 (turn-on-mime-edit)))
2785 (provide 'mime-edit)
2787 (run-hooks 'mime-edit-load-hook)
2789 ;;; mime-edit.el ends here