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)
118 (autoload 'smime-encrypt-buffer "smime"
119 "S/MIME encryption of current buffer.")
120 (autoload 'smime-sign-buffer "smime"
121 "S/MIME signature of current buffer.")
128 (defconst mime-edit-version
130 (mime-product-name mime-user-interface-product) " "
131 (mapconcat #'number-to-string
132 (mime-product-version mime-user-interface-product) ".")
133 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
139 (defgroup mime-edit nil
143 (defcustom mime-ignore-preceding-spaces nil
144 "*Ignore preceding white spaces if non-nil."
148 (defcustom mime-ignore-trailing-spaces nil
149 "*Ignore trailing white spaces if non-nil."
153 (defcustom mime-ignore-same-text-tag t
154 "*Ignore preceding text content-type tag that is same with new one.
155 If non-nil, the text tag is not inserted unless something different."
159 (defcustom mime-auto-hide-body t
160 "*Hide non-textual body encoded in base64 after insertion if non-nil."
164 (defcustom mime-edit-voice-recorder
165 (function mime-edit-voice-recorder-for-sun)
166 "*Function to record a voice message and encode it."
170 (defcustom mime-edit-mode-hook nil
171 "*Hook called when enter MIME mode."
175 (defcustom mime-edit-translate-hook nil
176 "*Hook called before translating into a MIME compliant message.
177 To insert a signature file automatically, call the function
178 `mime-edit-insert-signature' from this hook."
182 (defcustom mime-edit-exit-hook nil
183 "*Hook called when exit MIME mode."
187 (defvar mime-content-types
189 ;; Charset parameter need not to be specified, since it is
190 ;; defined automatically while translation.
192 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
205 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
206 ("directory" "/pub/GNU/elisp/mime")
208 ("mode" "image" "ascii" "local8"))
213 ("mode" "image" "ascii" "local8"))
214 ("tftp" ("site") ("name"))
215 ("afs" ("site") ("name"))
216 ("local-file" ("site") ("name"))
218 ("server" "ftpmail@nic.karrn.ad.jp")
224 ("octet-stream" ("type" "" "tar" "shar"))
229 ("vnd.ms-powerpoint")
230 ("x-kiss" ("x-cnf")))
242 "*Alist of content-type, subtype, parameters and its values.")
244 (defcustom mime-file-types
247 ;; Programming languages
250 "application" "octet-stream" (("type" . "C++"))
252 "attachment" (("filename" . file)))
255 "application" "octet-stream" (("type" . "emacs-lisp"))
257 "attachment" (("filename" . file)))
260 "application" "octet-stream" (("type" . "common-lisp"))
262 "attachment" (("filename" . file)))
265 "application" "octet-stream" (("type" . "perl"))
267 "attachment" (("filename" . file)))
269 ;; Text or translated text
274 "inline" (("filename" . file)))
276 ;; .rc : procmail modules pm-xxxx.rc
277 ;; *rc : other resource files
279 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
282 "attachment" (("filename" . file)))
289 ("\\.diff$\\|\\.patch$"
290 "application" "octet-stream" (("type" . "patch"))
292 "attachment" (("filename" . file)))
295 "text" "plain" nil nil nil nil)
298 ;; Octect binary text
301 "application" "msword" nil
303 "attachment" (("filename" . file)))
304 ("\\.xls$" ; MS Excel
305 "application" "vnd.ms-excel" nil
307 "attachment" (("filename" . file)))
308 ("\\.ppt$" ; MS Power Point
309 "application" "vnd.ms-powerpoint" nil
311 "attachment" (("filename" . file)))
316 "inline" (("filename" . file)))
318 "application" "postscript" nil
320 "attachment" (("filename" . file)))
322 "application" "pdf" nil
324 "attachment" (("filename" . file)))
328 ("\\.jpg$\\|\\.jpeg$"
331 "inline" (("filename" . file)))
335 "inline" (("filename" . file)))
339 "inline" (("filename" . file)))
343 "inline" (("filename" . file)))
347 "inline" (("filename" . file)))
351 "inline" (("filename" . file)))
355 "inline" (("filename" . file)))
359 "inline" (("filename" . file)))
363 "attachment" (("filename" . file)))
367 "attachment" (("filename" . file)))
369 "application" "octet-stream" (("type" . "tar+gzip"))
371 "attachment" (("filename" . file)))
373 "application" "octet-stream" (("type" . "tar+gzip"))
375 "attachment" (("filename" . file)))
377 "application" "octet-stream" (("type" . "tar+compress"))
379 "attachment" (("filename" . file)))
381 "application" "octet-stream" (("type" . "tar+compress"))
383 "attachment" (("filename" . file)))
385 "application" "octet-stream" (("type" . "gzip"))
387 "attachment" (("filename" . file)))
389 "application" "octet-stream" (("type" . "compress"))
391 "attachment" (("filename" . file)))
393 "application" "octet-stream" (("type" . "lha"))
395 "attachment" (("filename" . file)))
397 "application" "zip" nil
399 "attachment" (("filename" . file)))
404 "application" "octet-stream" nil
406 "attachment" (("filename" . file))))
407 "*Alist of file name, types, parameters, and default encoding.
408 If encoding is nil, it is determined from its contents."
412 (choice :tag "Primary-Type"
413 ,@(nconc (mapcar (lambda (cell)
414 (list 'item (car cell)))
418 (choice :tag "Sub-Type"
421 (mapcar (lambda (cell)
422 (mapcar (lambda (cell)
423 (list 'item (car cell)))
428 (repeat :tag "Parameters of Content-Type field"
429 (cons string (choice string symbol)))
430 ;; content-transfer-encoding
431 (choice :tag "Encoding"
434 (mapcar (lambda (cell)
436 (mime-encoding-list))))
438 (choice :tag "Disposition-Type"
444 (repeat :tag "Parameters of Content-Disposition field"
445 (cons string (choice string symbol)))))
449 ;;; @@ about charset, encoding and transfer-level
452 (defvar mime-charset-type-list
454 (iso-8859-1 8 "quoted-printable")
455 (iso-8859-2 8 "quoted-printable")
456 (iso-8859-3 8 "quoted-printable")
457 (iso-8859-4 8 "quoted-printable")
458 (iso-8859-5 8 "quoted-printable")
459 (koi8-r 8 "quoted-printable")
460 (iso-8859-7 8 "quoted-printable")
461 (iso-8859-8 8 "quoted-printable")
462 (iso-8859-9 8 "quoted-printable")
463 (iso-8859-14 8 "quoted-printable")
464 (iso-8859-15 8 "quoted-printable")
465 (iso-2022-jp 7 "base64")
466 (iso-2022-jp-3 7 "base64")
467 (iso-2022-kr 7 "base64")
473 (shift_jis 8 "base64")
475 (iso-2022-jp-2 7 "base64")
476 (iso-2022-int-1 7 "base64")))
478 (defvar mime-transfer-level 7
479 "*A number of network transfer level. It should be bigger than 7.")
480 (make-variable-buffer-local 'mime-transfer-level)
482 (defsubst mime-encoding-name (transfer-level &optional not-omit)
483 (cond ((> transfer-level 8) "binary")
484 ((= transfer-level 8) "8bit")
487 (defvar mime-transfer-level-string
488 (mime-encoding-name mime-transfer-level 'not-omit)
489 "A string formatted version of mime-transfer-level")
490 (make-variable-buffer-local 'mime-transfer-level-string)
492 ;;; @@ about content transfer encoding
494 (defvar mime-content-transfer-encoding-priority-list
495 '(nil "8bit" "binary"))
497 ;;; @@ about message inserting
500 (defvar mime-edit-yank-ignored-field-list
501 '("Received" "Approved" "Path" "Replied" "Status"
502 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
503 "Delete these fields from original message when it is inserted
504 as message/rfc822 part.
505 Each elements are regexp of field-name.")
507 (defvar mime-edit-yank-ignored-field-regexp
509 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
512 (defvar mime-edit-message-inserter-alist nil)
513 (defvar mime-edit-mail-inserter-alist nil)
516 ;;; @@ about message splitting
519 (defcustom mime-edit-split-message t
520 "*Split large message if it is non-nil."
524 (defcustom mime-edit-message-default-max-lines 1000
525 "*Default maximum lines of a message."
529 (defcustom mime-edit-message-max-lines-alist
530 '((news-reply-mode . 500))
531 "Alist of major-mode vs maximum lines of a message.
532 If it is not specified for a major-mode,
533 `mime-edit-message-default-max-lines' is used."
537 (defconst mime-edit-split-ignored-field-regexp
538 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
540 (defcustom mime-edit-split-blind-field-regexp
541 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
542 "*Regular expression to match field-name to be ignored when split sending."
546 (defvar mime-edit-split-message-sender-alist nil)
548 (defvar mime-edit-news-reply-mode-server-running nil)
554 (defconst mime-edit-single-part-tag-regexp
555 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
556 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
558 (defconst mime-edit-quoted-single-part-tag-regexp
559 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
561 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
563 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
565 (defconst mime-edit-beginning-tag-regexp
566 (regexp-or mime-edit-single-part-tag-regexp
567 mime-edit-multipart-beginning-regexp))
569 (defconst mime-edit-end-tag-regexp
570 (regexp-or mime-edit-single-part-tag-regexp
571 mime-edit-multipart-end-regexp))
573 (defconst mime-edit-tag-regexp
574 (regexp-or mime-edit-single-part-tag-regexp
575 mime-edit-multipart-beginning-regexp
576 mime-edit-multipart-end-regexp))
578 (defvar mime-tag-format "--[[%s]]"
579 "*Control-string making a MIME tag.")
581 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
582 "*Control-string making a MIME tag with encoding.")
585 ;;; @@ multipart boundary
588 (defvar mime-multipart-boundary "Multipart"
589 "*Boundary of a multipart message.")
592 ;;; @@ optional header fields
595 (defvar mime-edit-insert-user-agent-field t
596 "*If non-nil, insert User-Agent header field.")
598 (defvar mime-edit-user-agent-value
599 (concat (mime-product-name mime-user-interface-product)
601 (mapconcat #'number-to-string
602 (mime-product-version mime-user-interface-product) ".")
604 (mime-product-code-name mime-user-interface-product)
606 (mime-product-name mime-library-product)
608 (mapconcat #'number-to-string
609 (mime-product-version mime-library-product) ".")
611 (mime-product-code-name mime-library-product)
613 (if (fboundp 'apel-version)
614 (concat (apel-version) " "))
615 "EasyPG/ " epg-version-number " "
616 (if (featurep 'xemacs)
617 (concat (cond ((and (featurep 'chise)
618 (boundp 'xemacs-chise-version))
619 (concat "CHISE-MULE/" xemacs-chise-version))
620 ((featurep 'utf-2000)
621 (concat "UTF-2000-MULE/" utf-2000-version))
622 ((featurep 'mule) "MULE"))
624 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
627 (substring emacs-version 0 (match-end 0))
628 (cond ((and (boundp 'xemacs-betaname)
630 ;; It does not exist in XEmacs
631 ;; versions prior to 20.3.
632 (concat " " xemacs-betaname))
633 ((and (boundp 'emacs-patch-level)
635 ;; It does not exist in FSF Emacs or in
636 ;; XEmacs versions earlier than 21.1.1.
637 (format " (patch %d)" emacs-patch-level))
639 " (" xemacs-codename ")"
640 ;; `xemacs-extra-name' has appeared in the
641 ;; development version of XEmacs 21.5-b8.
642 (if (and (boundp 'xemacs-extra-name)
643 (symbol-value 'xemacs-extra-name))
644 (concat " " (symbol-value 'xemacs-extra-name))
647 system-configuration ")")
648 " (" emacs-version ")"))
649 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
650 (substring emacs-version 0 (match-beginning 0))
653 (if (boundp 'enable-multibyte-characters)
655 " (" system-configuration ")"
656 (if enable-multibyte-characters
657 (concat " MULE/" mule-version)
658 " (with unibyte mode)")
659 (if (featurep 'meadow)
660 (let ((mver (Meadow-version)))
661 (if (string-match "^Meadow-" mver)
665 (concat "MULE/" mule-version
666 " (based on Emacs " ver ")"))
667 (concat "Emacs/" ver " (" system-configuration ")")))))
668 "Body of User-Agent field.
669 If variable `mime-edit-insert-user-agent-field' is not nil, it is
670 inserted into message header.")
676 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
677 "*Specify MIME tspecials.
678 Tspecials means any character that matches with it in header must be quoted.")
680 (defconst mime-edit-mime-version-value
681 (concat "1.0 (generated by " mime-edit-version ")")
682 "MIME version number.")
684 (defconst mime-edit-mime-version-field-for-message/partial
685 (concat "MIME-Version:"
686 (mime-encode-field-body
687 (concat " 1.0 (split by " mime-edit-version ")\n")
689 "MIME version field for message/partial.")
692 ;;; @ keymap and menu
695 (defvar mime-edit-mode-flag nil)
696 (make-variable-buffer-local 'mime-edit-mode-flag)
698 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
699 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
700 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
701 "Keymap for MIME-Edit mode commands to insert entity or set status.")
703 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
704 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
705 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
706 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
707 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
708 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
709 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
710 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
711 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
712 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
714 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
715 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
716 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
717 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
718 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
719 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
720 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
721 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
722 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
723 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
725 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
726 "Keymap prefix for MIME-Edit mode commands about enclosure.")
727 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
728 "Keymap for MIME-Edit mode commands about enclosure.")
730 (define-key mime-edit-mode-enclosure-map
731 "\C-a" 'mime-edit-enclose-alternative-region)
732 (define-key mime-edit-mode-enclosure-map
733 "\C-p" 'mime-edit-enclose-parallel-region)
734 (define-key mime-edit-mode-enclosure-map
735 "\C-m" 'mime-edit-enclose-mixed-region)
736 (define-key mime-edit-mode-enclosure-map
737 "\C-d" 'mime-edit-enclose-digest-region)
738 (define-key mime-edit-mode-enclosure-map
739 "\C-s" 'mime-edit-enclose-pgp-signed-region)
740 (define-key mime-edit-mode-enclosure-map
741 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
742 (define-key mime-edit-mode-enclosure-map
743 "\C-q" 'mime-edit-enclose-quote-region)
745 (defvar mime-edit-mode-map (make-sparse-keymap)
746 "Keymap for MIME-Edit mode commands.")
747 (define-key mime-edit-mode-map
748 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
749 (define-key mime-edit-mode-map
750 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
752 (defconst mime-edit-menu-title "MIME-Edit")
754 (defconst mime-edit-menu-list
755 '((mime-help "Describe MIME editor mode" mime-edit-help)
756 (file "Insert File" mime-edit-insert-file)
757 (external "Insert External" mime-edit-insert-external)
758 (voice "Insert Voice" mime-edit-insert-voice)
759 (message "Insert Message" mime-edit-insert-message)
760 (mail "Insert Mail" mime-edit-insert-mail)
761 (signature "Insert Signature" mime-edit-insert-signature)
762 (text "Insert Text" mime-edit-insert-text)
763 (tag "Insert Tag" mime-edit-insert-tag)
764 (alternative "Enclose as alternative"
765 mime-edit-enclose-alternative-region)
766 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
767 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
768 (digest "Enclose as digest" mime-edit-enclose-digest-region)
769 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
770 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
771 (quote "Verbatim region" mime-edit-enclose-quote-region)
772 (key "Insert Public Key" mime-edit-insert-key)
773 (split "Set splitting" mime-edit-set-split)
774 (sign "PGP sign" mime-edit-set-sign)
775 (encrypt "PGP encrypt" mime-edit-set-encrypt)
776 (preview "Preview Message" mime-edit-preview-message)
777 (level "Toggle transfer-level" mime-edit-toggle-transfer-level))
778 "MIME-edit menubar entry.")
780 (cond ((featurep 'xemacs)
781 ;; modified by Pekka Marjola <pema@iki.fi>
782 ;; 1995/9/5 (c.f. [tm-en:69])
783 (defun mime-edit-define-menu-for-xemacs ()
784 "Define menu for XEmacs."
785 (cond ((featurep 'menubar)
786 (make-local-variable 'current-menubar)
787 (set-buffer-menubar current-menubar)
790 (cons mime-edit-menu-title
793 (vector (nth 1 item)(nth 2 item)
794 mime-edit-mode-flag)))
795 mime-edit-menu-list))))))
797 ;; modified by Steven L. Baur <steve@miranova.com>
798 ;; 1995/12/6 (c.f. [tm-en:209])
799 (or (boundp 'mime-edit-popup-menu-for-xemacs)
800 (setq mime-edit-popup-menu-for-xemacs
801 (append '("MIME Commands" "---")
802 (mapcar (function (lambda (item)
806 mime-edit-menu-list)))))
807 ((>= emacs-major-version 19)
808 (define-key mime-edit-mode-map [menu-bar mime-edit]
809 (cons mime-edit-menu-title
810 (make-sparse-keymap mime-edit-menu-title)))
813 (define-key mime-edit-mode-map
814 (vector 'menu-bar 'mime-edit (car item))
815 (cons (nth 1 item)(nth 2 item)))))
816 (reverse mime-edit-menu-list))))
822 (defvar mime-edit-touched-flag nil)
825 (defun mime-edit-mode ()
826 "MIME minor mode for editing the tagged MIME message.
828 In this mode, basically, the message is composed in the tagged MIME
829 format. The message tag looks like:
831 --[[text/plain; charset=ISO-2022-JP][7bit]]
833 The tag specifies the MIME content type, subtype, optional parameters
834 and transfer encoding of the message following the tag. Messages
835 without any tag are treated as `text/plain' by default. Charset and
836 transfer encoding are automatically defined unless explicitly
837 specified. Binary messages such as audio and image are usually
838 hidden. The messages in the tagged MIME format are automatically
839 translated into a MIME compliant message when exiting this mode.
841 Available charsets depend on Emacs version being used. The following
842 lists the available charsets of each emacs.
844 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
845 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
846 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
847 CN-BIG5 and ISO-2022-INT-1 are available.
849 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
850 be used to represent multilingual text in intermixed manner. Any
851 languages that has no registered charset are represented as either
852 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
854 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
855 without mule, please set variable `default-mime-charset'. This
856 variable must be symbol of which name is a MIME charset.
858 If you want to add more charsets in mule, please set variable
859 `charsets-mime-charset-alist'. This variable must be alist of which
860 key is list of charset and value is symbol of MIME charset. If name
861 of coding-system is different as MIME charset, please set variable
862 `mime-charset-coding-system-alist'. This variable must be alist of
863 which key is MIME charset and value is coding-system.
865 Following commands are available in addition to major mode commands:
868 \\[mime-edit-insert-text] insert a text message.
869 \\[mime-edit-insert-file] insert a (binary) file.
870 \\[mime-edit-insert-external] insert a reference to external body.
871 \\[mime-edit-insert-voice] insert a voice message.
872 \\[mime-edit-insert-message] insert a mail or news message.
873 \\[mime-edit-insert-mail] insert a mail message.
874 \\[mime-edit-insert-signature] insert a signature file at end.
875 \\[mime-edit-insert-key] insert PGP public key.
876 \\[mime-edit-insert-tag] insert a new MIME tag.
878 \[make enclosure (maybe multipart)\]
879 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
880 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
881 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
882 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
883 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
884 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
885 \\[mime-edit-enclose-quote-region] enclose as verbose mode
886 (to avoid to expand tags)
889 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
890 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
891 \\[mime-edit-set-split] set message splitting mode.
892 \\[mime-edit-set-sign] set PGP-sign mode.
893 \\[mime-edit-set-encrypt] set PGP-encryption mode.
894 \\[mime-edit-preview-message] preview editing MIME message.
895 \\[mime-edit-exit] exit and translate into a MIME
897 \\[mime-edit-help] show this help.
898 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
901 Additional commands are available in some major modes:
902 C-c C-c exit, translate and run the original command.
903 C-c C-s exit, translate and run the original command.
905 The following is a message example written in the tagged MIME format.
906 TABs at the beginning of the line are not a part of the message:
908 This is a conventional plain text. It should be translated
911 This is also a plain text. But, it is explicitly specified as
913 --[[text/plain; charset=ISO-8859-1]]
914 This is also a plain text. But charset is specified as
917 ¡Hola! Buenos días. ¿Cómo está usted?
919 This is a <bold>enriched text</bold>.
920 --[[image/gif][base64]]...image encoded in base64 here...
921 --[[audio/basic][base64]]...audio encoded in base64 here...
923 User customizable variables (not documented all of them):
925 Specifies a key prefix for MIME minor mode commands.
927 mime-ignore-preceding-spaces
928 Preceding white spaces in a message body are ignored if non-nil.
930 mime-ignore-trailing-spaces
931 Trailing white spaces in a message body are ignored if non-nil.
934 Hide a non-textual body message encoded in base64 after insertion
938 A number of network transfer level. It should be bigger than 7.
939 If you are in 8bit-through environment, please set 8.
941 mime-edit-voice-recorder
942 Specifies a function to record a voice message and encode it.
943 The function `mime-edit-voice-recorder-for-sun' is for Sun
947 Turning on MIME mode calls the value of mime-edit-mode-hook, if
950 mime-edit-translate-hook
951 The value of mime-edit-translate-hook is called just before translating
952 the tagged MIME format into a MIME compliant message if it is
953 non-nil. If the hook call the function mime-edit-insert-signature,
954 the signature file will be inserted automatically.
957 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
960 (if mime-edit-mode-flag
962 (if mime-edit-touched-flag
964 (make-local-variable 'mime-edit-touched-flag)
965 (setq mime-edit-touched-flag t)
966 (turn-on-mime-edit))))
969 (cond ((featurep 'xemacs)
970 (add-minor-mode 'mime-edit-mode-flag
971 '((" MIME-Edit " mime-transfer-level-string))
976 (set-alist 'minor-mode-alist
978 '((" MIME-Edit " mime-transfer-level-string)))
979 (set-alist 'minor-mode-map-alist
981 mime-edit-mode-map)))
985 (defun turn-on-mime-edit ()
986 "Unconditionally turn on MIME-Edit mode."
988 (if mime-edit-mode-flag
989 (error "You are already editing a MIME message.")
990 (setq mime-edit-mode-flag t)
992 ;; Set transfer level into mode line
994 (setq mime-transfer-level-string
995 (mime-encoding-name mime-transfer-level 'not-omit))
996 (force-mode-line-update)
998 ;; Define menu for XEmacs.
999 (if (featurep 'xemacs)
1000 (mime-edit-define-menu-for-xemacs))
1002 ;; I don't care about saving these.
1003 (setq paragraph-start
1004 (regexp-or mime-edit-single-part-tag-regexp
1006 (setq paragraph-separate
1007 (regexp-or mime-edit-single-part-tag-regexp
1008 paragraph-separate))
1009 (run-hooks 'mime-edit-mode-hook)
1012 (substitute-command-keys
1013 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
1016 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1019 (defun mime-edit-exit (&optional nomime no-error)
1020 "Translate the tagged MIME message into a MIME compliant message.
1021 With no argument encode a message in the buffer into MIME, otherwise
1022 just return to previous mode."
1024 (if (not mime-edit-mode-flag)
1026 (error "You aren't editing a MIME message."))
1029 (run-hooks 'mime-edit-translate-hook)
1030 (mime-edit-translate-buffer)))
1031 ;; Restore previous state.
1032 (setq mime-edit-mode-flag nil)
1033 (if (and (featurep 'xemacs)
1034 (featurep 'menubar))
1035 (delete-menu-item (list mime-edit-menu-title)))
1036 (set-buffer-modified-p (buffer-modified-p))
1037 (run-hooks 'mime-edit-exit-hook)
1038 (message "Exit MIME editor mode.")))
1040 (defun mime-edit-maybe-translate ()
1042 (mime-edit-exit nil t)
1043 (call-interactively 'mime-edit-maybe-split-and-send))
1045 (defun mime-edit-help ()
1046 "Show help message about MIME mode."
1048 (with-output-to-temp-buffer "*Help*"
1049 (princ "MIME editor mode:\n")
1050 (princ (documentation 'mime-edit-mode))
1051 (print-help-return-message)))
1053 (defun mime-edit-insert-text (&optional subtype)
1054 "Insert a text message.
1055 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1056 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1058 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1060 (if (looking-at mime-edit-single-part-tag-regexp)
1062 ;; Make a space between the following message.
1065 (if (and (member (cadr ret) '("enriched"))
1066 (fboundp 'enriched-mode))
1068 (if (boundp 'enriched-mode)
1069 (enriched-mode -1))))))
1071 (defun mime-edit-insert-file (file &optional verbose)
1072 "Insert a message from a file."
1073 (interactive "fInsert file as MIME message: \nP")
1074 (let* ((guess (mime-find-file-type file))
1075 (type (nth 0 guess))
1076 (subtype (nth 1 guess))
1077 (parameters (nth 2 guess))
1078 (encoding (nth 3 guess))
1079 (disposition-type (nth 4 guess))
1080 (disposition-params (nth 5 guess)))
1082 (setq type (mime-prompt-for-type type)
1083 subtype (mime-prompt-for-subtype type subtype)))
1084 (if (or (interactive-p) verbose)
1085 (setq encoding (mime-prompt-for-encoding encoding)))
1086 (if (or (consp parameters) (stringp disposition-type))
1087 (let ((rest parameters) cell attribute value)
1088 (setq parameters "")
1090 (setq cell (car rest))
1091 (setq attribute (car cell))
1092 (setq value (cdr cell))
1093 (if (eq value 'file)
1094 (setq value (std11-wrap-as-quoted-string
1095 (file-name-nondirectory file))))
1096 (setq parameters (concat parameters "; " attribute "=" value))
1097 (setq rest (cdr rest)))
1098 (if disposition-type
1101 (concat parameters "\n"
1102 "Content-Disposition: " disposition-type))
1103 (setq rest disposition-params)
1105 (setq cell (car rest))
1106 (setq attribute (car cell))
1107 (setq value (cdr cell))
1108 (if (eq value 'file)
1109 (setq value (std11-wrap-as-quoted-string
1110 (file-name-nondirectory file))))
1112 (concat parameters "; " attribute "=" value))
1113 (setq rest (cdr rest)))))))
1114 (mime-edit-insert-tag type subtype parameters)
1115 (mime-edit-insert-binary-file file encoding)))
1117 (defun mime-edit-insert-external ()
1118 "Insert a reference to external body."
1120 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1122 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1124 (let* ((pritype (mime-prompt-for-type))
1125 (subtype (mime-prompt-for-subtype pritype))
1126 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1129 (insert "Content-Type: "
1130 pritype "/" subtype (or parameters "") "\n")))
1131 (if (and (not (eobp))
1132 (not (looking-at mime-edit-single-part-tag-regexp)))
1133 (insert (mime-make-text-tag) "\n")))
1135 (defun mime-edit-insert-voice ()
1136 "Insert a voice message."
1140 "What transfer encoding: "
1141 (mime-encoding-alist) nil t nil)))
1142 (mime-edit-insert-tag "audio" "basic" nil)
1143 (mime-edit-define-encoding encoding)
1145 (narrow-to-region (point)(point))
1147 (funcall mime-edit-voice-recorder encoding)
1150 (add-text-properties
1151 (point-min)(point-max) '(invisible t mime-edit-invisible t))
1152 (goto-char (point-max)))))))
1154 (defun mime-edit-insert-signature (&optional arg)
1155 "Insert a signature file."
1157 (let ((signature-insert-hook
1160 (let ((items (mime-find-file-type signature-file-name)))
1161 (apply (function mime-edit-insert-tag)
1162 (car items) (cadr items) (list (caddr items))))))))
1163 (insert-signature arg)))
1166 ;; Insert a new tag around a point.
1168 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1169 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1170 If nothing is inserted, return nil."
1173 (mime-edit-goto-tag)
1174 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1175 (< (match-beginning 0) p)
1176 (< p (match-end 0)))
1177 (goto-char (match-beginning 0))
1184 (mime-prompt-for-type)))
1187 (mime-prompt-for-subtype pritype)))
1190 (mime-prompt-for-parameters pritype subtype delimiter)))
1191 ;; Make a new MIME tag.
1192 (setq newtag (mime-make-tag pritype subtype parameters))
1193 ;; Find an current MIME tag.
1196 (if (mime-edit-goto-tag)
1197 (buffer-substring (match-beginning 0) (match-end 0))
1198 ;; Assume content type is 'text/plan'.
1199 (mime-make-tag "text" "plain"))))
1200 ;; We are only interested in TEXT.
1202 (not (mime-test-content-type
1203 (mime-edit-get-contype oldtag) "text")))
1206 (if (or (not oldtag) ;Not text
1207 (or mime-ignore-same-text-tag
1208 (not (string-equal oldtag newtag))))
1210 ;; Mark the beginning of the tag for convenience.
1211 (push-mark (point) 'nomsg)
1212 (insert newtag "\n")
1213 (list pritype subtype parameters) ;New tag is created.
1215 ;; Restore previous point.
1217 nil ;Nothing is created.
1220 (defun mime-edit-insert-binary-file (file &optional encoding)
1221 "Insert binary FILE at point.
1222 Optional argument ENCODING specifies an encoding method such as base64."
1223 (let* ((tagend (1- (point))) ;End of the tag
1224 (hide-p (and mime-auto-hide-body
1227 (let ((en (downcase encoding)))
1228 (or (string-equal en "7bit")
1229 (string-equal en "8bit")
1230 (string-equal en "binary")))))))
1232 (narrow-to-region (point)(point))
1233 (mime-insert-encoded-file file encoding)
1235 (add-text-properties
1236 (point-min)(point-max) '(invisible t mime-edit-invisible t)))
1237 (goto-char (point-max)))
1239 (looking-at mime-edit-tag-regexp)
1240 (= (point)(point-max))
1241 (mime-edit-insert-tag "text" "plain"))
1242 ;; Define encoding even if it is 7bit.
1243 (if (stringp encoding)
1245 (goto-char tagend) ; Make sure which line the tag is on.
1246 (mime-edit-define-encoding encoding)))))
1249 ;; Commands work on a current message flagment.
1251 (defun mime-edit-goto-tag ()
1252 "Search for the beginning of the tagged MIME message."
1253 (let ((current (point)))
1254 (if (looking-at mime-edit-tag-regexp)
1256 ;; At first, go to the end.
1257 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1258 (goto-char (1- (match-beginning 0))) ;For multiline tag
1261 (goto-char (point-max))))
1262 ;; Then search for the beginning.
1263 (re-search-backward mime-edit-end-tag-regexp nil t)
1264 (or (looking-at mime-edit-beginning-tag-regexp)
1265 ;; Restore previous point.
1270 (defun mime-edit-content-beginning ()
1271 "Return the point of the beginning of content."
1273 (let ((beg (save-excursion
1274 (beginning-of-line) (point))))
1275 (if (mime-edit-goto-tag)
1276 (let ((top (point)))
1277 (goto-char (match-end 0))
1278 (if (and (= beg top)
1279 (= (following-char) ?\^M))
1283 ;; Default text/plain tag.
1284 (goto-char (point-min))
1286 (concat "\n" (regexp-quote mail-header-separator)
1287 (if mime-ignore-preceding-spaces
1288 "[ \t\n]*\n" "\n")) nil 'move)
1291 (defun mime-edit-content-end ()
1292 "Return the point of the end of content."
1294 (if (mime-edit-goto-tag)
1296 (goto-char (1+ (match-end 0)))
1297 (if (get-text-property (point) 'mime-edit-invisible)
1298 (or (next-single-property-change (point) 'mime-edit-invisible)
1300 ;; Move to the end of this text.
1301 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1302 ;; Don't forget a multiline tag.
1303 (goto-char (match-beginning 0)))
1305 ;; Assume the message begins with text/plain.
1306 (goto-char (mime-edit-content-beginning))
1307 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1308 ;; Don't forget a multiline tag.
1309 (goto-char (match-beginning 0)))
1312 (defun mime-edit-define-charset (charset)
1313 "Set charset of current tag to CHARSET."
1315 (if (mime-edit-goto-tag)
1316 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1317 (delete-region (match-beginning 0) (match-end 0))
1320 (mime-edit-set-parameter
1321 (mime-edit-get-contype tag)
1323 (let ((comment (get charset 'mime-charset-comment)))
1325 (concat (upcase (symbol-name charset)) " (" comment ")")
1326 (upcase (symbol-name charset)))))
1327 (mime-edit-get-encoding tag)))))))
1329 (defun mime-edit-define-encoding (encoding)
1330 "Set encoding of current tag to ENCODING."
1332 (if (mime-edit-goto-tag)
1333 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1334 (delete-region (match-beginning 0) (match-end 0))
1335 (insert (mime-create-tag (mime-edit-get-contype tag) encoding))))))
1337 (defun mime-edit-choose-charset ()
1338 "Choose charset of a text following current point."
1339 (detect-mime-charset-region (point) (mime-edit-content-end)))
1341 (defun mime-make-text-tag (&optional subtype)
1342 "Make a tag for a text after current point.
1343 Subtype of text type can be specified by an optional argument SUBTYPE.
1344 Otherwise, it is obtained from mime-content-types."
1345 (let* ((pritype "text")
1346 (subtype (or subtype
1347 (car (car (cdr (assoc pritype mime-content-types)))))))
1348 ;; Charset should be defined later.
1349 (mime-make-tag pritype subtype)))
1352 ;; Tag handling functions
1354 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1355 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1356 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1360 (defun mime-create-tag (contype &optional encoding)
1361 "Make a tag with CONTENT-TYPE and optional ENCODING."
1362 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1365 (defun mime-edit-get-contype (tag)
1366 "Return Content-Type (including parameters) of TAG."
1368 (or (string-match mime-edit-single-part-tag-regexp tag)
1369 (string-match mime-edit-multipart-beginning-regexp tag)
1370 (string-match mime-edit-multipart-end-regexp tag))
1371 (substring tag (match-beginning 1) (match-end 1))))
1373 (defun mime-edit-get-encoding (tag)
1374 "Return encoding of TAG."
1376 (string-match mime-edit-single-part-tag-regexp tag)
1378 (not (= (match-beginning 3) (match-end 3)))
1379 (substring tag (match-beginning 3) (match-end 3))))
1381 (defun mime-get-parameter (contype parameter)
1382 "For given CONTYPE return value for PARAMETER.
1383 Nil if no such parameter."
1387 (regexp-quote parameter)
1388 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1390 (substring contype (match-beginning 1) (match-end 1))
1391 nil ;No such parameter
1394 (defun mime-edit-set-parameter (contype parameter value)
1395 "For given CONTYPE set PARAMETER to VALUE."
1396 (let (ctype opt-fields)
1397 (if (string-match "\n[^ \t\n\r]+:" contype)
1398 (setq ctype (substring contype 0 (match-beginning 0))
1399 opt-fields (substring contype (match-beginning 0)))
1400 (setq ctype contype))
1404 (regexp-quote parameter)
1405 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1408 (concat (substring ctype 0 (match-beginning 1))
1410 (substring ctype (match-end 1))
1412 (concat ctype "; " parameter "=" value opt-fields)
1415 (defun mime-strip-parameters (contype)
1416 "Return primary content-type and subtype without parameters for CONTYPE."
1417 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1418 (substring contype (match-beginning 1) (match-end 1)) nil))
1420 (defun mime-test-content-type (contype type &optional subtype)
1421 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1422 (and (stringp contype)
1425 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1426 (downcase contype))))
1431 (defun mime-find-file-type (file)
1432 "Guess Content-Type, subtype, and parameters from FILE."
1434 (guesses mime-file-types))
1435 (while (and (not guess) guesses)
1436 (if (string-match (car (car guesses)) file)
1437 (setq guess (cdr (car guesses))))
1438 (setq guesses (cdr guesses)))
1441 (defun mime-prompt-for-type (&optional default)
1442 "Ask for Content-type."
1444 ;; Repeat until primary content type is specified.
1445 (while (string-equal type "")
1447 (completing-read "What content type: "
1450 'require-match ;Type must be specified.
1452 (if (string-equal type "")
1454 (message "Content type is required.")
1459 (defun mime-prompt-for-subtype (type &optional default)
1460 "Ask for subtype of media-type TYPE."
1461 (let ((subtypes (cdr (assoc type mime-content-types))))
1463 (assoc default subtypes))
1464 (setq default (car (car subtypes)))))
1469 "What content subtype: (default " default ") ")
1470 "What content subtype: ")
1471 (cdr (assoc type mime-content-types))
1473 'require-match ;Subtype must be specified.
1475 (if (string-equal answer "") default answer)))
1477 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1478 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1479 Optional DELIMITER specifies parameter delimiter (';' by default)."
1480 (let* ((delimiter (or delimiter "; "))
1485 (mime-prompt-for-parameters-1
1487 (cdr (assoc pritype mime-content-types))))))
1489 (if (and (stringp parameters)
1490 (not (string-equal parameters "")))
1491 (concat delimiter parameters)
1492 "" ;"" if no parameters
1495 (defun mime-prompt-for-parameters-1 (optlist)
1496 (apply (function append)
1497 (mapcar (function mime-prompt-for-parameter) optlist)))
1499 (defun mime-prompt-for-parameter (parameter)
1501 Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
1502 (let* ((prompt (car parameter))
1503 (choices (mapcar (function
1505 (if (consp e) e (list e))))
1507 (default (car (car choices)))
1513 (concat "What " prompt
1515 (if (string-equal default "") "\"\"" default)
1517 choices nil nil ""))
1518 ;; If nothing is selected, use default.
1519 (if (string-equal answer "")
1520 (setq answer default)))
1522 (read-string (concat "What " prompt ": "))))
1523 (cons (if (and answer
1524 (not (string-equal answer "")))
1526 ;; Note: control characters ignored!
1527 (if (string-match mime-tspecials-regexp answer)
1528 (concat "\"" answer "\"") answer)))
1529 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))))
1531 (defun mime-prompt-for-encoding (default)
1532 "Ask for Content-Transfer-Encoding."
1537 "What transfer encoding: "
1538 (mime-encoding-alist) nil t default))
1543 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1546 (defvar mime-edit-translate-buffer-hook
1547 '(mime-edit-pgp-enclose-buffer
1548 mime-edit-translate-body
1549 mime-edit-translate-header))
1551 (defun mime-edit-translate-header ()
1552 "Encode the message header into network representation."
1553 (mime-encode-header-in-buffer 'code-conversion)
1554 (run-hooks 'mime-edit-translate-header-hook))
1556 (defun mime-edit-translate-buffer ()
1557 "Encode the tagged MIME message in current buffer in MIME compliant message."
1560 (if (catch 'mime-edit-error
1562 (run-hooks 'mime-edit-translate-buffer-hook)))
1565 (error "Translation error!"))))
1567 (defun mime-edit-find-inmost ()
1568 (goto-char (point-min))
1569 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1570 (let ((bb (match-beginning 0))
1572 (type (buffer-substring (match-beginning 1)(match-end 1)))
1574 (setq end-exp (format "--}-<<%s>>\n" type))
1576 (if (re-search-forward end-exp nil t)
1577 (setq eb (match-beginning 0))
1578 (setq eb (point-max)))
1579 (narrow-to-region be eb)
1581 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1583 (narrow-to-region (match-beginning 0)(point-max))
1584 (mime-edit-find-inmost))
1586 (list type bb be eb)))))
1588 (defun mime-edit-process-multipart-1 (boundary)
1589 (let ((ret (mime-edit-find-inmost)))
1591 (let ((type (car ret))
1592 (bb (nth 1 ret))(be (nth 2 ret))
1594 (narrow-to-region bb eb)
1595 (delete-region bb be)
1596 (setq bb (point-min))
1597 (setq eb (point-max))
1600 (if (looking-at mime-edit-multipart-end-regexp)
1601 (let ((beg (match-beginning 0))
1602 (end (match-end 0)))
1603 (delete-region beg end)
1604 (or (looking-at mime-edit-beginning-tag-regexp)
1606 (insert (concat (mime-make-text-tag) "\n")))))
1607 (cond ((string-equal type "quote")
1608 (mime-edit-enquote-region bb eb))
1609 ((string-equal type "pgp-signed")
1610 (mime-edit-sign-pgp-mime bb eb boundary))
1611 ((string-equal type "pgp-encrypted")
1612 (mime-edit-encrypt-pgp-mime bb eb boundary))
1613 ((string-equal type "kazu-signed")
1614 (mime-edit-sign-pgp-kazu bb eb boundary))
1615 ((string-equal type "kazu-encrypted")
1616 (mime-edit-encrypt-pgp-kazu bb eb boundary))
1617 ((string-equal type "smime-signed")
1618 (mime-edit-sign-smime bb eb boundary))
1619 ((string-equal type "smime-encrypted")
1620 (mime-edit-encrypt-smime bb eb boundary))
1623 (nth 2 (mime-edit-translate-region bb eb
1627 (format "--[[multipart/%s;
1628 boundary=\"%s\"][7bit]]\n"
1632 (defun mime-edit-enquote-region (beg end)
1635 (narrow-to-region beg end)
1637 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1638 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1639 (replace-match (concat "- " (substring tag 1))))))))
1641 (defun mime-edit-dequote-region (beg end)
1644 (narrow-to-region beg end)
1646 (while (re-search-forward
1647 mime-edit-quoted-single-part-tag-regexp nil t)
1648 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1649 (replace-match (concat "-" (substring tag 2))))))))
1651 (defvar mime-edit-pgp-user-id nil)
1653 (defun mime-edit-delete-trailing-whitespace ()
1656 (goto-char (point-min))
1657 (while (re-search-forward "[ \t]+$" nil t)
1658 (delete-region (match-beginning 0) (match-end 0))))))
1660 (defun mime-edit-sign-pgp-mime (beg end boundary)
1663 (let* ((from (std11-field-body "From" mail-header-separator))
1665 (narrow-to-region beg end)
1666 (mime-edit-translate-region beg end boundary)))
1668 (encoding (nth 1 ret))
1669 (pgp-boundary (concat "pgp-sign-" boundary))
1670 (context (epg-make-context))
1673 (mime-edit-delete-trailing-whitespace) ; RFC3156
1675 (insert (format "Content-Type: %s\n" ctype))
1677 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1679 (epg-context-set-armor context t)
1680 (epg-context-set-textmode context t)
1681 (epg-context-set-signers
1685 Select keys for signing.
1686 If no one is selected, default secret key is used. "
1688 (list (nth 1 (std11-extract-address-components from))))
1690 (condition-case error
1692 (epg-sign-string context
1693 (buffer-substring (point-min) (point-max))
1695 (error (signal 'mime-edit-error (cdr error))))
1696 (setq micalg (cdr (assq 'digest-algorithm
1697 (car (epg-context-result-for context 'sign)))))
1699 (insert (format "--[[multipart/signed;
1701 protocol=\"application/pgp-signature\"][7bit]]
1706 (concat "; micalg=pgp-"
1709 epg-digest-algorithm-alist))))
1712 (goto-char (point-max))
1713 (insert (format "\n--%s
1714 Content-Type: application/pgp-signature
1715 Content-Transfer-Encoding: 7bit
1719 (goto-char (point-max))
1720 (insert (format "\n--%s--\n" pgp-boundary))))))
1722 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1724 (defun mime-edit-make-encrypt-recipient-header ()
1725 (let* ((names mime-edit-encrypt-recipient-fields-list)
1727 (std11-field-bodies (cons "From" names)
1728 nil mail-header-separator))
1731 (setq values (cdr values))))
1732 (header (and (stringp from)
1733 (if (string-equal from "")
1735 (format "From: %s\n" from))))
1737 (while (and names values)
1738 (let ((name (car names))
1739 (value (car values)))
1740 (and (stringp value)
1741 (or (string-equal value "")
1743 (setq header (concat header name ": " value "\n")
1744 recipients (if recipients
1745 (concat recipients " ," value)
1747 (setq names (cdr names)
1748 values (cdr values)))
1749 (vector from recipients header)))
1751 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1754 (let (recipients header)
1755 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1756 (setq recipients (aref ret 1)
1757 header (aref ret 2)))
1758 (narrow-to-region beg end)
1760 (mime-edit-translate-region beg end boundary))
1762 (encoding (nth 1 ret))
1763 (pgp-boundary (concat "pgp-" boundary))
1764 (context (epg-make-context))
1768 (insert (format "Content-Type: %s\n" ctype))
1770 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1772 (mime-encode-header-in-buffer)
1773 (epg-context-set-armor context t)
1774 (condition-case error
1778 (buffer-substring (point-min) (point-max))
1781 Select recipents for encryption.
1782 If no one is selected, symmetric encryption will be performed. "
1783 (mapcar (lambda (recipient)
1784 (nth 1 (std11-extract-address-components
1786 (split-string recipients
1787 "\\([ \t\n]*,[ \t\n]*\\)+")))))
1788 (error (signal 'mime-edit-error (cdr error))))
1789 (delete-region (point-min)(point-max))
1791 (insert (format "--[[multipart/encrypted;
1793 protocol=\"application/pgp-encrypted\"][7bit]]
1795 Content-Type: application/pgp-encrypted
1798 Content-Type: application/octet-stream
1799 Content-Transfer-Encoding: 7bit
1801 " pgp-boundary pgp-boundary pgp-boundary))
1803 (goto-char (point-max))
1804 (insert (format "\n--%s--\n" pgp-boundary)))))))
1806 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1809 (narrow-to-region beg end)
1811 (mime-edit-translate-region beg end boundary))
1813 (encoding (nth 1 ret))
1814 (context (epg-make-context))
1817 (insert (format "Content-Type: %s\n" ctype))
1819 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1821 (condition-case error
1823 (epg-sign-string context
1824 (buffer-substring beg (point-max))
1826 (error (signal 'mime-edit-error (cdr error))))
1829 "--[[application/pgp; format=mime][7bit]]\n")
1832 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1834 (let (recipients header)
1835 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1836 (setq recipients (aref ret 1)
1837 header (aref ret 2)))
1839 (narrow-to-region beg end)
1841 (mime-edit-translate-region beg end boundary))
1843 (encoding (nth 1 ret))
1844 (context (epg-make-context))
1848 (insert (format "Content-Type: %s\n" ctype))
1850 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1852 (epg-context-set-armor context t)
1853 (condition-case error
1857 (buffer-substring beg (point-max))
1860 Select recipents for encryption.
1861 If no one is selected, symmetric encryption will be performed. "
1862 (mapcar (lambda (recipient)
1863 (nth 1 (std11-extract-address-components
1865 (split-string recipients
1866 "\\([ \t\n]*,[ \t\n]*\\)+")))))
1867 (error (signal 'mime-edit-error (cdr error))))
1870 "--[[application/pgp; format=mime][7bit]]\n" cipher)
1873 (defun mime-edit-sign-smime (beg end boundary)
1877 (narrow-to-region beg end)
1878 (mime-edit-translate-region beg end boundary)))
1880 (encoding (nth 1 ret))
1881 (smime-boundary (concat "smime-sign-" boundary)))
1883 (insert (format "Content-Type: %s\n" ctype))
1885 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1887 (let (buffer-undo-list)
1888 (goto-char (point-min))
1889 (while (progn (end-of-line) (not (eobp)))
1892 (or (smime-sign-buffer)
1893 (throw 'mime-edit-error 'pgp-error)))
1895 (if (re-search-forward "^Content-Type:\\s-*" nil t)
1896 (let* ((start (match-beginning 0))
1897 (body (buffer-substring (match-end 0) (std11-field-end))))
1898 (delete-region start (line-beginning-position 2))
1900 (insert "--[[" body "][7bit]]\n")))))))
1902 (defun mime-edit-encrypt-smime (beg end boundary)
1906 (narrow-to-region beg end)
1907 (mime-edit-translate-region beg end boundary)))
1909 (encoding (nth 1 ret)))
1911 (insert (format "Content-Type: %s\n" ctype))
1913 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1915 (goto-char (point-min))
1916 (while (progn (end-of-line) (not (eobp)))
1919 (or (smime-encrypt-buffer)
1920 (throw 'mime-edit-error 'pgp-error))
1922 (if (re-search-forward "^Content-Type:\\s-*" nil t)
1923 (let* ((start (match-beginning 0))
1924 (body (buffer-substring (match-end 0) (std11-field-end))))
1925 (delete-region start (line-beginning-position 2))
1927 (insert "--[[" body "]]\n")))))))
1929 (defsubst replace-space-with-underline (str)
1930 (mapconcat (function
1937 (defun mime-edit-make-boundary ()
1938 (concat mime-multipart-boundary "_"
1939 (replace-space-with-underline (current-time-string))))
1941 (defun mime-edit-translate-body ()
1942 "Encode the tagged MIME body in current buffer in MIME compliant message."
1945 (let ((boundary (mime-edit-make-boundary))
1948 (while (mime-edit-process-multipart-1
1949 (format "%s-%d" boundary i))
1952 ;; We are interested in message body.
1955 (goto-char (point-min))
1957 (concat "\n" (regexp-quote mail-header-separator)
1958 (if mime-ignore-preceding-spaces
1959 "[ \t\n]*\n" "\n")) nil 'move)
1963 (goto-char (point-max))
1964 (and mime-ignore-trailing-spaces
1965 (re-search-backward "[^ \t\n]\n" beg t)
1968 (setq ret (mime-edit-translate-region
1970 (format "%s-%d" boundary i)))))
1971 (mime-edit-dequote-region (point-min)(point-max))
1972 (let ((contype (car ret)) ;Content-Type
1973 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1975 ;; Insert User-Agent field
1976 (and mime-edit-insert-user-agent-field
1977 (or (mail-position-on-field "User-Agent")
1978 (insert mime-edit-user-agent-value)))
1979 ;; Make primary MIME headers.
1980 (or (mail-position-on-field "MIME-Version")
1981 (insert mime-edit-mime-version-value))
1982 ;; Remove old Content-Type and other fields.
1984 (goto-char (point-min))
1985 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1986 (narrow-to-region (point-min) (point))
1987 (goto-char (point-min))
1988 (mime-delete-field "Content-Type")
1989 (mime-delete-field "Content-Transfer-Encoding"))
1990 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1991 (mail-position-on-field "Content-Type")
1995 (mail-position-on-field "Content-Transfer-Encoding")
1996 (insert encoding)))))))
1998 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
1999 "Translate single-part-tag to MIME header."
2000 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2001 (let* ((beg (match-beginning 0))
2003 (tag (buffer-substring beg end)))
2004 (delete-region beg end)
2005 (let ((contype (mime-edit-get-contype tag))
2006 (encoding (mime-edit-get-encoding tag)))
2007 (insert (concat prefix "--" boundary "\n"))
2009 (narrow-to-region (point)(point))
2010 (insert "Content-Type: " contype "\n")
2012 (insert "Content-Transfer-Encoding: " encoding "\n"))
2013 (mime-encode-header-in-buffer))
2017 (downcase encoding)))))))
2019 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2021 (setq boundary (mime-edit-make-boundary)))
2024 (narrow-to-region beg end)
2025 (let ((tag nil) ;MIME tag
2026 (contype nil) ;Content-Type
2027 (encoding nil) ;Content-Transfer-Encoding
2028 (nparts 0)) ;Number of body parts
2029 ;; Normalize the body part by inserting appropriate message
2030 ;; tags for every message contents.
2031 (mime-edit-normalize-body)
2032 ;; Counting the number of Content-Type.
2033 (goto-char (point-min))
2034 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2035 (setq nparts (1+ nparts)))
2036 ;; Begin translation.
2038 ((and (<= nparts 1)(not multipart))
2039 ;; It's a singular message.
2040 (goto-char (point-min))
2041 (while (re-search-forward
2042 mime-edit-single-part-tag-regexp nil t)
2044 (buffer-substring (match-beginning 0) (match-end 0)))
2045 (delete-region (match-beginning 0) (1+ (match-end 0)))
2046 (setq contype (mime-edit-get-contype tag))
2047 (setq encoding (mime-edit-get-encoding tag))))
2049 ;; It's a multipart message.
2050 (goto-char (point-min))
2051 (let ((prio mime-content-transfer-encoding-priority-list)
2053 (when (setq part-info
2054 (mime-edit-translate-single-part-tag boundary))
2055 (and (setq nprio (member (cdr part-info) prio))
2057 (while (setq part-info
2058 (mime-edit-translate-single-part-tag boundary "\n"))
2059 (and (setq nprio (member (cdr part-info) prio))
2060 (setq prio nprio))))
2061 ;; Define Content-Type as "multipart/mixed".
2063 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2064 (setq encoding (car prio))
2065 ;; Insert the trailer.
2066 (goto-char (point-max))
2067 (insert "\n--" boundary "--\n"))))
2068 (list contype encoding boundary nparts)))))
2070 (defun mime-edit-normalize-body ()
2071 "Normalize the body part by inserting appropriate message tags."
2072 ;; Insert the first MIME tags if necessary.
2073 (goto-char (point-min))
2074 (if (not (looking-at mime-edit-single-part-tag-regexp))
2075 (insert (mime-make-text-tag) "\n"))
2076 ;; Check each tag, and add new tag or correct it if necessary.
2077 (goto-char (point-min))
2078 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2079 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2080 (contype (mime-edit-get-contype tag))
2081 (charset (mime-get-parameter contype "charset"))
2082 (encoding (mime-edit-get-encoding tag)))
2083 ;; Remove extra whitespaces after the tag.
2084 (if (looking-at "[ \t]+$")
2085 (delete-region (match-beginning 0) (match-end 0)))
2087 (end (mime-edit-content-end)))
2088 (if (= end (point-max))
2091 (or (looking-at mime-edit-beginning-tag-regexp)
2093 (insert (mime-make-text-tag) "\n")))
2094 (remove-text-properties beg end '(invisible mime-edit-invisible))
2097 ((mime-test-content-type contype "message")
2098 ;; Content-type "message" should be sent as is.
2100 ((mime-test-content-type contype "text")
2101 ;; Define charset for text if necessary.
2102 (setq charset (if charset
2103 (intern (downcase charset))
2104 (mime-edit-choose-charset)))
2105 (mime-edit-define-charset charset)
2106 (cond ((string-equal contype "text/x-rot13-47-48")
2109 (mule-caesar-region (point) (mime-edit-content-end))))
2110 ((string-equal contype "text/enriched")
2115 (end (mime-edit-content-end)))
2116 ;; Patch for hard newlines
2119 ;; (while (search-forward "\n" end t)
2120 ;; (put-text-property (match-beginning 0)
2123 ;; End patch for hard newlines
2124 (enriched-encode beg end nil)
2126 (if (search-forward "\n\n")
2127 (delete-region beg (match-end 0)))))))
2128 ;; Point is now on current tag.
2129 ;; Define encoding and encode text if necessary.
2130 (or encoding ;Encoding is not specified.
2133 (let ((ret (cdr (assq charset mime-charset-type-list))))
2135 (setq bits (car ret)
2138 conv "quoted-printable")))
2139 (if (<= bits mime-transfer-level)
2140 (mime-encoding-name bits)
2142 (beg (mime-edit-content-beginning)))
2143 (encode-mime-charset-region beg (mime-edit-content-end)
2145 ;; Protect "From " in beginning of line
2147 (narrow-to-region beg (mime-edit-content-end))
2149 (let (case-fold-search)
2150 (if (re-search-forward "^From " nil t)
2152 (if (memq charset '(iso-2022-jp
2157 (replace-match "\e(BFrom ")
2158 (re-search-forward "^From " nil t)))
2159 (setq encoding "quoted-printable"))))))
2160 ;; canonicalize line break code
2161 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2163 (narrow-to-region beg (mime-edit-content-end))
2165 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2166 ;; In a certain period, `replace-match' with "\\N"
2167 ;; converted 8-bit characters into multibyte string,
2168 ;; but it has been fixed at 2004-01-15.
2169 ;;(replace-match "\\1\r\n"))))
2174 (mime-encode-region beg (mime-edit-content-end)
2175 (or encoding "7bit"))
2176 (mime-edit-define-encoding encoding)))
2177 (goto-char (mime-edit-content-end)))
2178 ((null encoding) ;Encoding is not specified.
2179 ;; Application, image, audio, video, and any other
2180 ;; unknown content-type without encoding should be
2182 (let* ((encoding "base64") ;Encode in BASE64 by default.
2183 (beg (mime-edit-content-beginning))
2184 (end (mime-edit-content-end)))
2185 (mime-encode-region beg end encoding)
2186 (mime-edit-define-encoding encoding))
2187 (forward-line 1))))))
2189 (defun mime-delete-field (field)
2190 "Delete header FIELD."
2191 (let ((regexp (format "^%s:[ \t]*" field)))
2192 (goto-char (point-min))
2193 (while (re-search-forward regexp nil t)
2194 (delete-region (match-beginning 0)
2195 (1+ (std11-field-end))))))
2199 ;;; Platform dependent functions
2202 ;; Sun implementations
2204 (defun mime-edit-voice-recorder-for-sun (encoding)
2205 "Record voice in a buffer using Sun audio device,
2206 and insert data encoded as ENCODING."
2207 (message "Start the recording on %s. Type C-g to finish the recording..."
2209 (mime-insert-encoded-file "/dev/audio" encoding))
2212 ;;; @ Other useful commands.
2215 ;; Message forwarding commands as content-type "message/rfc822".
2217 (defun mime-edit-insert-message (&optional message)
2219 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2220 (if (and inserter (fboundp inserter))
2222 (mime-edit-insert-tag "message" "rfc822")
2223 (funcall inserter message))
2224 (message "Sorry, I don't have message inserter for your MUA."))))
2226 (defun mime-edit-insert-mail (&optional message)
2228 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2229 (if (and inserter (fboundp inserter))
2231 (mime-edit-insert-tag "message" "rfc822")
2232 (funcall inserter message))
2233 (message "Sorry, I don't have mail inserter for your MUA."))))
2235 (defun mime-edit-inserted-message-filter ()
2238 (let ((header-start (point))
2239 (case-fold-search t)
2242 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2243 (if (re-search-forward "^$" (mark t))
2244 (narrow-to-region header-start (match-beginning 0)))
2245 (goto-char header-start)
2246 (while (and (re-search-forward
2247 mime-edit-yank-ignored-field-regexp nil t)
2248 (setq beg (match-beginning 0))
2249 (setq end (1+ (std11-field-end))))
2250 (delete-region beg end))))))
2253 ;;; @ multipart enclosure
2256 (defun mime-edit-enclose-region-internal (type beg end)
2260 (narrow-to-region beg end)
2261 (insert (format "--<<%s>>-{\n" type))
2262 (goto-char (point-max))
2263 (insert (format "--}-<<%s>>\n" type))
2264 (goto-char (point-max)))
2265 (or (looking-at mime-edit-beginning-tag-regexp)
2267 (insert (mime-make-text-tag) "\n"))))
2269 (defun mime-edit-enclose-quote-region (beg end)
2271 (mime-edit-enclose-region-internal 'quote beg end))
2273 (defun mime-edit-enclose-mixed-region (beg end)
2275 (mime-edit-enclose-region-internal 'mixed beg end))
2277 (defun mime-edit-enclose-parallel-region (beg end)
2279 (mime-edit-enclose-region-internal 'parallel beg end))
2281 (defun mime-edit-enclose-digest-region (beg end)
2283 (mime-edit-enclose-region-internal 'digest beg end))
2285 (defun mime-edit-enclose-alternative-region (beg end)
2287 (mime-edit-enclose-region-internal 'alternative beg end))
2289 (defun mime-edit-enclose-pgp-signed-region (beg end)
2291 (mime-edit-enclose-region-internal 'pgp-signed beg end))
2293 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2295 (mime-edit-enclose-region-internal 'pgp-encrypted beg end))
2297 (defun mime-edit-enclose-kazu-signed-region (beg end)
2299 (mime-edit-enclose-region-internal 'kazu-signed beg end))
2301 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2303 (mime-edit-enclose-region-internal 'kazu-encrypted beg end))
2305 (defun mime-edit-enclose-smime-signed-region (beg end)
2307 (mime-edit-enclose-region-internal 'smime-signed beg end))
2309 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2311 (mime-edit-enclose-region-internal 'smime-encrypted beg end))
2313 (defun mime-edit-insert-key (&optional arg)
2314 "Insert a pgp public key."
2316 (mime-edit-insert-tag "application" "pgp-keys")
2317 (mime-edit-define-encoding "7bit")
2318 (let ((context (epg-make-context)))
2319 (epg-context-set-armor t)
2320 (epg-export-keys-to-string context
2321 (epa-select-keys "Select keys for export. ")))
2322 (if (and (not (eobp))
2323 (not (looking-at mime-edit-single-part-tag-regexp)))
2324 (insert (mime-make-text-tag) "\n")))
2330 (defun mime-edit-set-split (arg)
2333 (y-or-n-p "Do you want to enable split? ")))
2334 (setq mime-edit-split-message arg)
2336 (message "This message is enabled to split.")
2337 (message "This message is not enabled to split.")))
2339 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2340 "Toggle transfer-level is 7bit or 8bit through.
2342 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2344 (if (numberp transfer-level)
2345 (setq mime-transfer-level transfer-level)
2346 (if (< mime-transfer-level 8)
2347 (setq mime-transfer-level 8)
2348 (setq mime-transfer-level 7)))
2349 (message (format "Current transfer-level is %d bit"
2350 mime-transfer-level))
2351 (setq mime-transfer-level-string
2352 (mime-encoding-name mime-transfer-level 'not-omit))
2353 (force-mode-line-update))
2355 (defun mime-edit-set-transfer-level-7bit ()
2357 (mime-edit-toggle-transfer-level 7))
2359 (defun mime-edit-set-transfer-level-8bit ()
2361 (mime-edit-toggle-transfer-level 8))
2367 (defvar mime-edit-pgp-processing nil)
2368 (make-variable-buffer-local 'mime-edit-pgp-processing)
2370 (defun mime-edit-set-sign (arg)
2373 (y-or-n-p "Do you want to sign? ")))
2376 (or (memq 'sign mime-edit-pgp-processing)
2377 (setq mime-edit-pgp-processing
2378 (nconc mime-edit-pgp-processing
2379 (copy-sequence '(sign)))))
2380 (message "This message will be signed."))
2381 (setq mime-edit-pgp-processing
2382 (delq 'sign mime-edit-pgp-processing))
2383 (message "This message will not be signed.")))
2385 (defun mime-edit-set-encrypt (arg)
2388 (y-or-n-p "Do you want to encrypt? ")))
2391 (or (memq 'encrypt mime-edit-pgp-processing)
2392 (setq mime-edit-pgp-processing
2393 (nconc mime-edit-pgp-processing
2394 (copy-sequence '(encrypt)))))
2395 (message "This message will be encrypt."))
2396 (setq mime-edit-pgp-processing
2397 (delq 'encrypt mime-edit-pgp-processing))
2398 (message "This message will not be encrypt.")))
2400 (defun mime-edit-pgp-enclose-buffer ()
2401 (let ((beg (save-excursion
2402 (goto-char (point-min))
2403 (if (search-forward (concat "\n" mail-header-separator "\n"))
2406 (dolist (pgp-processing mime-edit-pgp-processing)
2407 (case pgp-processing
2409 (mime-edit-enclose-pgp-signed-region
2412 (mime-edit-enclose-pgp-encrypted-region
2413 beg (point-max))))))))
2419 (defun mime-edit-insert-partial-header (fields subject
2420 id number total separator)
2422 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2423 (insert mime-edit-mime-version-field-for-message/partial)
2425 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2426 id number total separator)))
2428 (defun mime-edit-split-and-send
2429 (&optional cmd lines mime-edit-message-max-length)
2433 (count-lines (point-min) (point-max))))
2434 (or mime-edit-message-max-length
2435 (setq mime-edit-message-max-length
2436 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2437 mime-edit-message-default-max-lines)))
2438 (let ((separator mail-header-separator)
2440 (replace-space-with-underline (current-time-string))
2441 "@" (system-name) "\"")))
2442 (run-hooks 'mime-edit-before-split-hook)
2443 (let ((the-buf (current-buffer))
2444 (copy-buf (get-buffer-create " *Original Message*"))
2445 (header (std11-header-string-except
2446 mime-edit-split-ignored-field-regexp separator))
2447 (subject (mail-fetch-field "subject"))
2448 (total (+ (/ lines mime-edit-message-max-length)
2449 (if (> (mod lines mime-edit-message-max-length) 0)
2455 mime-edit-split-message-sender-alist))
2459 (error "Split sender is not specified for `%s'." major-mode)))))
2460 (mime-edit-partial-number 1)
2463 (set-buffer copy-buf)
2465 (insert-buffer the-buf)
2467 (if (re-search-forward
2468 (concat "^" (regexp-quote separator) "$") nil t)
2469 (let ((he (match-beginning 0)))
2471 (narrow-to-region (point-min) he)))
2472 (goto-char (point-min))
2473 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2474 (delete-region (match-beginning 0)
2475 (1+ (std11-field-end))))))
2476 (while (< mime-edit-partial-number total)
2479 (set-buffer copy-buf)
2480 (setq data (buffer-substring
2483 (goto-line mime-edit-message-max-length)
2485 (delete-region (point-min)(point)))
2486 (mime-edit-insert-partial-header
2487 header subject id mime-edit-partial-number total separator)
2490 (message (format "Sending %d/%d..."
2491 mime-edit-partial-number total))
2492 (call-interactively command)
2493 (message (format "Sending %d/%d...done"
2494 mime-edit-partial-number total)))
2495 (setq mime-edit-partial-number
2496 (1+ mime-edit-partial-number)))
2499 (set-buffer copy-buf)
2500 (setq data (buffer-string))
2502 (mime-edit-insert-partial-header
2503 header subject id mime-edit-partial-number total separator)
2506 (message (format "Sending %d/%d..."
2507 mime-edit-partial-number total))
2508 (message (format "Sending %d/%d...done"
2509 mime-edit-partial-number total))))))
2511 (defun mime-edit-maybe-split-and-send (&optional cmd)
2513 (run-hooks 'mime-edit-before-send-hook)
2514 (let ((mime-edit-message-max-length
2515 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2516 mime-edit-message-default-max-lines))
2517 (lines (count-lines (point-min) (point-max))))
2518 (if (and (> lines mime-edit-message-max-length)
2519 mime-edit-split-message)
2520 (mime-edit-split-and-send cmd lines mime-edit-message-max-length))))
2523 ;;; @ preview message
2526 (defvar mime-edit-buffer nil) ; buffer local variable
2527 (defvar mime-edit-temp-message-buffer nil) ; buffer local variable
2529 (defun mime-edit-preview-message ()
2530 "preview editing MIME message."
2532 (let* ((str (buffer-string))
2533 (separator mail-header-separator)
2534 (the-buf (current-buffer))
2535 (buf-name (buffer-name))
2536 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2537 (buf (get-buffer temp-buf-name))
2538 (pgp-processing mime-edit-pgp-processing))
2541 (switch-to-buffer buf)
2543 (setq buf (get-buffer-create temp-buf-name))
2544 (switch-to-buffer buf))
2546 (setq major-mode 'mime-temp-message-mode)
2547 (make-local-variable 'mail-header-separator)
2548 (setq mail-header-separator separator)
2549 (make-local-variable 'mime-edit-buffer)
2550 (setq mime-edit-buffer the-buf)
2551 (setq mime-edit-pgp-processing pgp-processing)
2553 (run-hooks 'mime-edit-translate-hook)
2554 (mime-edit-translate-buffer)
2555 (goto-char (point-min))
2556 (if (re-search-forward
2557 (concat "^" (regexp-quote separator) "$"))
2560 (make-local-variable 'mime-edit-temp-message-buffer)
2561 (setq mime-edit-temp-message-buffer buf)))
2563 (defun mime-edit-quitting-method ()
2564 "Quitting method for mime-view."
2565 (let* ((temp mime-edit-temp-message-buffer)
2567 (mime-preview-kill-buffer)
2569 (setq buf mime-edit-buffer)
2571 (switch-to-buffer buf)))
2573 (set-alist 'mime-preview-quitting-method-alist
2574 'mime-temp-message-mode
2575 #'mime-edit-quitting-method)
2581 (defvar mime-edit-again-ignored-field-regexp
2582 (concat "^\\(" "Content-.*\\|Mime-Version"
2583 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2585 "Regexp for deleted header fields when `mime-edit-again' is called.")
2587 (defsubst eliminate-top-spaces (string)
2588 "Eliminate top sequence of space or tab in STRING."
2589 (if (string-match "^[ \t]+" string)
2590 (substring string (match-end 0))
2593 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2596 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2597 '(("application/pgp-encrypted" . pgp-encrypted)
2598 ("application/pgp-signature" . pgp-signed))))
2599 (mime-content-type-subtype content-type)))
2600 (boundary (mime-content-type-parameter content-type "boundary"))
2601 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2602 (re-search-forward boundary-pat nil t)
2603 (let ((bb (match-beginning 0)) eb tag)
2604 (setq tag (format "\n--<<%s>>-{\n" subtype))
2607 (setq bb (+ bb (length tag)))
2609 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2611 (setq eb (match-beginning 0))
2612 (replace-match (format "--}-<<%s>>\n" subtype))
2614 (narrow-to-region bb eb)
2615 (goto-char (point-min))
2616 (while (re-search-forward boundary-pat nil t)
2617 (let ((beg (match-beginning 0))
2619 (delete-region beg (match-end 0))
2621 (if (re-search-forward boundary-pat nil t)
2622 (setq end (match-beginning 0))
2623 (setq end (point-max)))
2625 (narrow-to-region beg end)
2627 ((eq subtype 'pgp-encrypted)
2630 (goto-char (point-min))
2631 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2634 (save-window-excursion
2640 (delete-region (point-min)(point-max))))
2641 (insert-buffer-substring pgg-output-buffer)
2642 (mime-edit-decode-message-in-buffer
2643 nil not-decode-text)
2644 (delete-region (goto-char (point-min))
2645 (if (search-forward "\n\n" nil t)
2648 (goto-char (point-max))))
2650 (mime-edit-decode-message-in-buffer
2651 (if (eq subtype 'digest)
2653 (make-mime-content-type 'message 'rfc822)))
2655 (goto-char (point-max))))))))))
2656 (goto-char (point-min))
2657 (or (= (point-min) 1)
2658 (delete-region (point-min)
2659 (if (search-forward "\n\n" nil t)
2663 (defun mime-edit-decode-single-part-in-buffer
2664 (content-type not-decode-text &optional content-disposition)
2665 (let* ((type (mime-content-type-primary-type content-type))
2666 (subtype (mime-content-type-subtype content-type))
2667 (ctype (format "%s/%s" type subtype))
2669 (pstr (let ((bytes (+ 14 (length ctype))))
2670 (mapconcat (function
2672 (if (string= (car attr) "charset")
2674 (setq charset (cdr attr))
2676 (let* ((str (concat (car attr)
2679 (setq bytes (+ bytes bs 2))
2682 (setq bytes (+ bs 1))
2685 (mime-content-type-parameters content-type) "")))
2688 (limit (save-excursion
2689 (if (search-forward "\n\n" nil t)
2692 (mime-content-disposition-type content-disposition))
2694 (if disposition-type
2695 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2696 (mapconcat (function
2701 (if (string-equal "filename"
2703 (std11-wrap-as-quoted-string
2707 (setq bytes (+ bytes bs 2))
2710 (setq bytes (+ bs 1))
2713 (mime-content-disposition-parameters
2714 content-disposition)
2716 (if disposition-type
2717 (setq pstr (format "%s\nContent-Disposition: %s%s"
2718 pstr disposition-type disposition-str)))
2720 (if (re-search-forward
2721 "^Content-Transfer-Encoding:" limit t)
2722 (let ((beg (match-beginning 0))
2723 (hbeg (match-end 0))
2724 (end (std11-field-end limit)))
2727 (eliminate-top-spaces
2728 (std11-unfold-string
2729 (buffer-substring hbeg end)))))
2730 (if (or charset (eq type 'text))
2732 (delete-region beg (1+ end))
2733 (goto-char (point-min))
2734 (if (search-forward "\n\n" nil t)
2737 (match-end 0)(point-max) encoding)
2739 encoding nil))))))))
2740 (if (and (eq type 'text)
2741 (or encoded (not not-decode-text)))
2744 (goto-char (point-min))
2745 (while (re-search-forward "\r\n" nil t)
2746 (replace-match "\n")))
2747 (decode-mime-charset-region (point-min)(point-max)
2748 (or charset default-mime-charset))))
2749 (let ((he (if (re-search-forward "^$" nil t)
2752 (if (and (eq type 'text)
2753 (eq subtype 'x-rot13-47-48))
2754 (mule-caesar-region he (point-max)))
2755 (if (= (point-min) 1)
2761 (format "%s/%s%s" type subtype pstr)
2763 (delete-region (point-min) he)
2765 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2769 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2772 (goto-char (point-min))
2773 (let ((ctl (or (mime-read-Content-Type)
2774 default-content-type)))
2776 (let ((type (mime-content-type-primary-type ctl)))
2778 ((and (eq type 'application)
2779 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2780 (delete-region (point-min)(point-max)))
2781 ((eq type 'multipart)
2782 (mime-edit-decode-multipart-in-buffer ctl not-decode-text))
2784 (mime-edit-decode-single-part-in-buffer
2785 ctl not-decode-text (mime-read-Content-Disposition)))))
2787 (decode-mime-charset-region (point-min) (point-max)
2788 default-mime-charset)))
2789 (if (= (point-min) 1)
2792 (std11-narrow-to-header)
2793 (goto-char (point-min))
2794 (while (re-search-forward
2795 mime-edit-again-ignored-field-regexp nil t)
2796 (delete-region (match-beginning 0) (1+ (std11-field-end)))))
2797 (mime-decode-header-in-buffer (not not-decode-text)))))))
2800 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2801 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2802 Content-Type and Content-Transfer-Encoding header fields will be
2803 converted to MIME-Edit tags."
2805 (goto-char (point-min))
2807 (concat "\n" (regexp-quote mail-header-separator) "\n")
2809 (replace-match "\n\n"))
2810 (mime-edit-decode-message-in-buffer nil not-decode-text)
2811 (goto-char (point-min))
2813 (and (re-search-forward "^$")
2814 (replace-match mail-header-separator)))
2816 (turn-on-mime-edit)))
2822 (provide 'mime-edit)
2824 (run-hooks 'mime-edit-load-hook)
2826 ;;; mime-edit.el ends here