1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,94,95,96,97,98,99 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Created: 1994/08/21 renamed from mime.el
8 ;; Renamed: 1997/2/21 from tm-edit.el
9 ;; Keywords: MIME, multimedia, multilingual, mail, news
11 ;; This file is part of SEMI (Sophisticated Emacs MIME Interfaces).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; This is an Emacs minor mode for editing Internet multimedia
31 ;; messages formatted in MIME (RFC 2045, 2046, 2047, 2048 and 2049).
32 ;; All messages in this mode are composed in the tagged MIME format,
33 ;; that are described in the following examples. The messages
34 ;; composed in the tagged MIME format are automatically translated
35 ;; into a MIME compliant message when exiting the mode.
37 ;; Mule (multilingual feature of Emacs 20 and multilingual extension
38 ;; for XEmacs 20) has a capability of handling multilingual text in
39 ;; limited ISO-2022 manner that is based on early experiences in
40 ;; Japanese Internet community and resulted in RFC 1468 (ISO-2022-JP
41 ;; charset for MIME). In order to enable multilingual capability in
42 ;; single text message in MIME, charset of multilingual text written
43 ;; in Mule is declared as either `ISO-2022-JP-2' [RFC 1554]. Mule is
44 ;; required for reading the such messages.
46 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
47 ;; News mode. First of all, you need the following autoload
48 ;; definition to load mime-edit-mode automatically:
50 ;; (autoload 'turn-on-mime-edit "mime-edit"
51 ;; "Minor mode for editing MIME message." t)
53 ;; In case of Mail mode (includes VM mode), you need the following
56 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
57 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
59 ;; In case of MH-E, you need the following hook definition:
61 ;; (add-hook 'mh-letter-mode-hook
64 ;; (turn-on-mime-edit)
65 ;; (make-local-variable 'mail-header-separator)
66 ;; (setq mail-header-separator "--------")
68 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
70 ;; In case of News mode, you need the following hook definition:
72 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
73 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
75 ;; In case of Emacs 19, it is possible to emphasize the message tags
76 ;; using font-lock mode as follows:
78 ;; (add-hook 'mime-edit-mode-hook
82 ;; (setq font-lock-keywords (list mime-edit-tag-regexp))
85 ;; The message tag looks like:
87 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
89 ;; The tagged MIME message examples:
91 ;; This is a conventional plain text. It should be translated into
95 ;; This is also a plain text. But, it is explicitly specified as is.
96 ;;--[[text/plain; charset=ISO-8859-1]]
97 ;; This is also a plain text. But charset is specified as iso-8859-1.
99 ;; ¡Hola! Buenos días. ¿Cómo está usted?
100 ;;--[[text/enriched]]
101 ;; <center>This is a richtext.</center>
103 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
105 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
110 (require 'mail-utils)
112 (require 'eword-encode) ; eword-encode-field-body
123 (defconst mime-edit-version
125 (mime-product-name mime-user-interface-product) " "
126 (mapconcat #'number-to-string
127 (mime-product-version mime-user-interface-product) ".")
128 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
134 (defgroup mime-edit nil
138 (defcustom mime-ignore-preceding-spaces nil
139 "*Ignore preceding white spaces if non-nil."
143 (defcustom mime-ignore-trailing-spaces nil
144 "*Ignore trailing white spaces if non-nil."
148 (defcustom mime-ignore-same-text-tag t
149 "*Ignore preceding text content-type tag that is same with new one.
150 If non-nil, the text tag is not inserted unless something different."
154 (defcustom mime-auto-hide-body t
155 "*Hide non-textual body encoded in base64 after insertion if non-nil."
159 (defcustom mime-edit-voice-recorder
160 (function mime-edit-voice-recorder-for-sun)
161 "*Function to record a voice message and encode it."
165 (defcustom mime-edit-mode-hook nil
166 "*Hook called when enter MIME mode."
170 (defcustom mime-edit-translate-hook nil
171 "*Hook called before translating into a MIME compliant message.
172 To insert a signature file automatically, call the function
173 `mime-edit-insert-signature' from this hook."
177 (defcustom mime-edit-exit-hook nil
178 "*Hook called when exit MIME mode."
182 (defvar mime-content-types
184 ;; Charset parameter need not to be specified, since it is
185 ;; defined automatically while translation.
187 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
200 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
201 ("directory" "/pub/GNU/elisp/mime")
203 ("mode" "image" "ascii" "local8"))
208 ("mode" "image" "ascii" "local8"))
209 ("tftp" ("site") ("name"))
210 ("afs" ("site") ("name"))
211 ("local-file" ("site") ("name"))
213 ("server" "ftpmail@nic.karrn.ad.jp")
221 ("octet-stream" ("type" "" "tar" "shar"))
223 ("vnd.ms-powerpoint")
224 ("x-kiss" ("x-cnf")))
238 "*Alist of content-type, subtype, parameters and its values.")
240 (defcustom mime-file-types
243 ;; Programming languages
246 "application" "octet-stream" (("type" . "C++"))
248 "attachment" (("filename" . file))
252 "application" "octet-stream" (("type" . "emacs-lisp"))
254 "attachment" (("filename" . file))
258 "application" "octet-stream" (("type" . "common-lisp"))
260 "attachment" (("filename" . file))
264 "application" "octet-stream" (("type" . "perl"))
266 "attachment" (("filename" . file))
269 ;; Text or translated text
274 "inline" (("filename" . file))
277 ;; .rc : procmail modules pm-xxxx.rc
278 ;; *rc : other resource files
280 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
283 "attachment" (("filename" . file))
291 ("\\.diff$\\|\\.patch$"
292 "application" "octet-stream" (("type" . "patch"))
294 "attachment" (("filename" . file))
298 "text" "plain" nil nil nil nil)
301 ;; Octect binary text
304 "application" "winword" nil
306 "attachment" (("filename" . file))
308 ("\\.ppt$" ; MS Power Point
309 "application" "vnd.ms-powerpoint" nil
311 "attachment" (("filename" . file))
317 "inline" (("filename" . file))
320 "application" "postscript" nil
322 "attachment" (("filename" . file))
330 "inline" (("filename" . file))
335 "inline" (("filename" . file))
340 "inline" (("filename" . file))
345 "inline" (("filename" . file))
350 "inline" (("filename" . file))
355 "inline" (("filename" . file))
360 "inline" (("filename" . file))
365 "inline" (("filename" . file))
370 "attachment" (("filename" . file))
375 "attachment" (("filename" . file))
378 "application" "octet-stream" (("type" . "tar+gzip"))
380 "attachment" (("filename" . file))
383 "application" "octet-stream" (("type" . "tar+gzip"))
385 "attachment" (("filename" . file))
388 "application" "octet-stream" (("type" . "tar+compress"))
390 "attachment" (("filename" . file))
393 "application" "octet-stream" (("type" . "tar+compress"))
395 "attachment" (("filename" . file))
398 "application" "octet-stream" (("type" . "gzip"))
400 "attachment" (("filename" . file))
403 "application" "octet-stream" (("type" . "compress"))
405 "attachment" (("filename" . file))
408 "application" "octet-stream" (("type" . "lha"))
410 "attachment" (("filename" . file))
413 "application" "zip" nil
415 "attachment" (("filename" . file))
421 "application" "octet-stream" nil
423 "attachment" (("filename" . file)))
425 "*Alist of file name, types, parameters, and default encoding.
426 If encoding is nil, it is determined from its contents."
430 (choice :tag "Primary-Type"
431 ,@(nconc (mapcar (lambda (cell)
432 (list 'item (car cell))
437 (choice :tag "Sub-Type"
440 (mapcar (lambda (cell)
441 (mapcar (lambda (cell)
442 (list 'item (car cell))
448 (repeat :tag "Parameters of Content-Type field"
449 (cons string (choice string symbol)))
450 ;; content-transfer-encoding
451 (choice :tag "Encoding"
454 (mapcar (lambda (cell)
457 (mime-encoding-list))))
459 (choice :tag "Disposition-Type"
465 (repeat :tag "Parameters of Content-Disposition field"
466 (cons string (choice string symbol)))
471 ;;; @@ about charset, encoding and transfer-level
474 (defvar mime-charset-type-list
476 (iso-8859-1 8 "quoted-printable")
477 (iso-8859-2 8 "quoted-printable")
478 (iso-8859-3 8 "quoted-printable")
479 (iso-8859-4 8 "quoted-printable")
480 (iso-8859-5 8 "quoted-printable")
481 (koi8-r 8 "quoted-printable")
482 (iso-8859-7 8 "quoted-printable")
483 (iso-8859-8 8 "quoted-printable")
484 (iso-8859-9 8 "quoted-printable")
485 (iso-2022-jp 7 "base64")
486 (iso-2022-kr 7 "base64")
492 (shift_jis 8 "base64")
494 (iso-2022-jp-2 7 "base64")
495 (iso-2022-int-1 7 "base64")
498 (defvar mime-transfer-level 7
499 "*A number of network transfer level. It should be bigger than 7.")
500 (make-variable-buffer-local 'mime-transfer-level)
502 (defsubst mime-encoding-name (transfer-level &optional not-omit)
503 (cond ((> transfer-level 8) "binary")
504 ((= transfer-level 8) "8bit")
508 (defvar mime-transfer-level-string
509 (mime-encoding-name mime-transfer-level 'not-omit)
510 "A string formatted version of mime-transfer-level")
511 (make-variable-buffer-local 'mime-transfer-level-string)
514 ;;; @@ about content transfer encoding
516 (defvar mime-content-transfer-encoding-priority-list
517 '(nil "8bit" "binary"))
519 ;;; @@ about message inserting
522 (defvar mime-edit-yank-ignored-field-list
523 '("Received" "Approved" "Path" "Replied" "Status"
524 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
525 "Delete these fields from original message when it is inserted
526 as message/rfc822 part.
527 Each elements are regexp of field-name.")
529 (defvar mime-edit-yank-ignored-field-regexp
531 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
534 (defvar mime-edit-message-inserter-alist nil)
535 (defvar mime-edit-mail-inserter-alist nil)
538 ;;; @@ about message splitting
541 (defcustom mime-edit-split-message t
542 "*Split large message if it is non-nil."
546 (defcustom mime-edit-message-default-max-lines 1000
547 "*Default maximum lines of a message."
551 (defcustom mime-edit-message-max-lines-alist
552 '((news-reply-mode . 500))
553 "Alist of major-mode vs maximum lines of a message.
554 If it is not specified for a major-mode,
555 `mime-edit-message-default-max-lines' is used."
559 (defconst mime-edit-split-ignored-field-regexp
560 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
562 (defcustom mime-edit-split-blind-field-regexp
563 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
564 "*Regular expression to match field-name to be ignored when split sending."
568 (defvar mime-edit-split-message-sender-alist nil)
570 (defvar mime-edit-news-reply-mode-server-running nil)
576 (defconst mime-edit-single-part-tag-regexp
577 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
578 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
580 (defconst mime-edit-quoted-single-part-tag-regexp
581 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
583 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
585 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
587 (defconst mime-edit-beginning-tag-regexp
588 (regexp-or mime-edit-single-part-tag-regexp
589 mime-edit-multipart-beginning-regexp))
591 (defconst mime-edit-end-tag-regexp
592 (regexp-or mime-edit-single-part-tag-regexp
593 mime-edit-multipart-end-regexp))
595 (defconst mime-edit-tag-regexp
596 (regexp-or mime-edit-single-part-tag-regexp
597 mime-edit-multipart-beginning-regexp
598 mime-edit-multipart-end-regexp))
600 (defvar mime-tag-format "--[[%s]]"
601 "*Control-string making a MIME tag.")
603 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
604 "*Control-string making a MIME tag with encoding.")
607 ;;; @@ multipart boundary
610 (defvar mime-multipart-boundary "Multipart"
611 "*Boundary of a multipart message.")
614 ;;; @@ optional header fields
617 (defvar mime-edit-insert-user-agent-field t
618 "*If non-nil, insert User-Agent header field.")
620 (defvar mime-edit-user-agent-value
621 (concat (mime-product-name mime-user-interface-product)
623 (mapconcat #'number-to-string
624 (mime-product-version mime-user-interface-product) ".")
626 (mime-product-code-name mime-user-interface-product)
628 (mime-product-name mime-library-product)
630 (mapconcat #'number-to-string
631 (mime-product-version mime-library-product) ".")
633 (mime-product-code-name mime-library-product)
635 (if (featurep 'xemacs)
636 (concat (cond ((featurep 'utf-2000)
637 (concat "UTF-2000-MULE/" utf-2000-version))
638 ((featurep 'mule) "MULE"))
640 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
643 (substring emacs-version 0 (match-end 0))
644 (cond ((and (boundp 'xemacs-betaname)
646 ;; It does not exist in XEmacs
647 ;; versions prior to 20.3.
648 (concat " " xemacs-betaname))
649 ((and (boundp 'emacs-patch-level)
651 ;; It does not exist in FSF Emacs or in
652 ;; XEmacs versions earlier than 21.1.1.
653 (format " (patch %d)" emacs-patch-level))
655 " (" xemacs-codename ") ("
656 system-configuration ")")
657 " (" emacs-version ")"))
658 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
659 (substring emacs-version 0 (match-beginning 0))
662 (if (boundp 'enable-multibyte-characters)
664 " (" system-configuration ")"
665 (if enable-multibyte-characters
666 (concat " MULE/" mule-version)
667 " (with unibyte mode)")
668 (if (featurep 'meadow)
669 (let ((mver (Meadow-version)))
670 (if (string-match "^Meadow-" mver)
675 (concat "MULE/" mule-version
676 " (based on Emacs " ver ")"))
677 (concat "Emacs/" ver " (" system-configuration ")")))))
678 "Body of User-Agent field.
679 If variable `mime-edit-insert-user-agent-field' is not nil, it is
680 inserted into message header.")
686 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
687 "*Specify MIME tspecials.
688 Tspecials means any character that matches with it in header must be quoted.")
690 (defconst mime-edit-mime-version-value
691 (concat "1.0 (generated by " mime-edit-version ")")
692 "MIME version number.")
694 (defconst mime-edit-mime-version-field-for-message/partial
695 (concat "MIME-Version:"
696 (eword-encode-field-body
697 (concat " 1.0 (split by " mime-edit-version ")\n")
699 "MIME version field for message/partial.")
702 ;;; @ keymap and menu
705 (defvar mime-edit-mode-flag nil)
706 (make-variable-buffer-local 'mime-edit-mode-flag)
708 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
709 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
710 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
711 "Keymap for MIME-Edit mode commands to insert entity or set status.")
713 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
714 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
715 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
716 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
717 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
718 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
719 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
720 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
721 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
722 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
724 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
725 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
726 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
727 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
728 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
729 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
730 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
731 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
732 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
733 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
735 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
736 "Keymap prefix for MIME-Edit mode commands about enclosure.")
737 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
738 "Keymap for MIME-Edit mode commands about enclosure.")
740 (define-key mime-edit-mode-enclosure-map
741 "\C-a" 'mime-edit-enclose-alternative-region)
742 (define-key mime-edit-mode-enclosure-map
743 "\C-p" 'mime-edit-enclose-parallel-region)
744 (define-key mime-edit-mode-enclosure-map
745 "\C-m" 'mime-edit-enclose-mixed-region)
746 (define-key mime-edit-mode-enclosure-map
747 "\C-d" 'mime-edit-enclose-digest-region)
748 (define-key mime-edit-mode-enclosure-map
749 "\C-s" 'mime-edit-enclose-pgp-signed-region)
750 (define-key mime-edit-mode-enclosure-map
751 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
752 (define-key mime-edit-mode-enclosure-map
753 "\C-q" 'mime-edit-enclose-quote-region)
755 (defvar mime-edit-mode-map (make-sparse-keymap)
756 "Keymap for MIME-Edit mode commands.")
757 (define-key mime-edit-mode-map
758 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
759 (define-key mime-edit-mode-map
760 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
762 (defconst mime-edit-menu-title "MIME-Edit")
764 (defconst mime-edit-menu-list
765 '((mime-help "Describe MIME editor mode" mime-edit-help)
766 (file "Insert File" mime-edit-insert-file)
767 (external "Insert External" mime-edit-insert-external)
768 (voice "Insert Voice" mime-edit-insert-voice)
769 (message "Insert Message" mime-edit-insert-message)
770 (mail "Insert Mail" mime-edit-insert-mail)
771 (signature "Insert Signature" mime-edit-insert-signature)
772 (text "Insert Text" mime-edit-insert-text)
773 (tag "Insert Tag" mime-edit-insert-tag)
774 (alternative "Enclose as alternative"
775 mime-edit-enclose-alternative-region)
776 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
777 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
778 (digest "Enclose as digest" mime-edit-enclose-digest-region)
779 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
780 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
781 (quote "Verbatim region" mime-edit-enclose-quote-region)
782 (key "Insert Public Key" mime-edit-insert-key)
783 (split "About split" mime-edit-set-split)
784 (sign "About sign" mime-edit-set-sign)
785 (encrypt "About encryption" mime-edit-set-encrypt)
786 (preview "Preview Message" mime-edit-preview-message)
787 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
789 "MIME-edit menubar entry.")
791 (cond ((featurep 'xemacs)
792 ;; modified by Pekka Marjola <pema@iki.fi>
793 ;; 1995/9/5 (c.f. [tm-en:69])
794 (defun mime-edit-define-menu-for-xemacs ()
795 "Define menu for XEmacs."
796 (cond ((featurep 'menubar)
797 (make-local-variable 'current-menubar)
798 (set-buffer-menubar current-menubar)
801 (cons mime-edit-menu-title
804 (vector (nth 1 item)(nth 2 item)
807 mime-edit-menu-list)))
810 ;; modified by Steven L. Baur <steve@miranova.com>
811 ;; 1995/12/6 (c.f. [tm-en:209])
812 (or (boundp 'mime-edit-popup-menu-for-xemacs)
813 (setq mime-edit-popup-menu-for-xemacs
814 (append '("MIME Commands" "---")
815 (mapcar (function (lambda (item)
819 mime-edit-menu-list)))
822 ((>= emacs-major-version 19)
823 (define-key mime-edit-mode-map [menu-bar mime-edit]
824 (cons mime-edit-menu-title
825 (make-sparse-keymap mime-edit-menu-title)))
828 (define-key mime-edit-mode-map
829 (vector 'menu-bar 'mime-edit (car item))
830 (cons (nth 1 item)(nth 2 item))
833 (reverse mime-edit-menu-list)
841 (defvar mime-edit-touched-flag nil)
844 (defun mime-edit-mode ()
845 "MIME minor mode for editing the tagged MIME message.
847 In this mode, basically, the message is composed in the tagged MIME
848 format. The message tag looks like:
850 --[[text/plain; charset=ISO-2022-JP][7bit]]
852 The tag specifies the MIME content type, subtype, optional parameters
853 and transfer encoding of the message following the tag. Messages
854 without any tag are treated as `text/plain' by default. Charset and
855 transfer encoding are automatically defined unless explicitly
856 specified. Binary messages such as audio and image are usually
857 hidden. The messages in the tagged MIME format are automatically
858 translated into a MIME compliant message when exiting this mode.
860 Available charsets depend on Emacs version being used. The following
861 lists the available charsets of each emacs.
863 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
864 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
865 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
866 CN-BIG5 and ISO-2022-INT-1 are available.
868 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
869 be used to represent multilingual text in intermixed manner. Any
870 languages that has no registered charset are represented as either
871 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
873 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
874 without mule, please set variable `default-mime-charset'. This
875 variable must be symbol of which name is a MIME charset.
877 If you want to add more charsets in mule, please set variable
878 `charsets-mime-charset-alist'. This variable must be alist of which
879 key is list of charset and value is symbol of MIME charset. If name
880 of coding-system is different as MIME charset, please set variable
881 `mime-charset-coding-system-alist'. This variable must be alist of
882 which key is MIME charset and value is coding-system.
884 Following commands are available in addition to major mode commands:
887 \\[mime-edit-insert-text] insert a text message.
888 \\[mime-edit-insert-file] insert a (binary) file.
889 \\[mime-edit-insert-external] insert a reference to external body.
890 \\[mime-edit-insert-voice] insert a voice message.
891 \\[mime-edit-insert-message] insert a mail or news message.
892 \\[mime-edit-insert-mail] insert a mail message.
893 \\[mime-edit-insert-signature] insert a signature file at end.
894 \\[mime-edit-insert-key] insert PGP public key.
895 \\[mime-edit-insert-tag] insert a new MIME tag.
897 \[make enclosure (maybe multipart)\]
898 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
899 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
900 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
901 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
902 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
903 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
904 \\[mime-edit-enclose-quote-region] enclose as verbose mode
905 (to avoid to expand tags)
908 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
909 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
910 \\[mime-edit-set-split] set message splitting mode.
911 \\[mime-edit-set-sign] set PGP-sign mode.
912 \\[mime-edit-set-encrypt] set PGP-encryption mode.
913 \\[mime-edit-preview-message] preview editing MIME message.
914 \\[mime-edit-exit] exit and translate into a MIME
916 \\[mime-edit-help] show this help.
917 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
920 Additional commands are available in some major modes:
921 C-c C-c exit, translate and run the original command.
922 C-c C-s exit, translate and run the original command.
924 The following is a message example written in the tagged MIME format.
925 TABs at the beginning of the line are not a part of the message:
927 This is a conventional plain text. It should be translated
930 This is also a plain text. But, it is explicitly specified as
932 --[[text/plain; charset=ISO-8859-1]]
933 This is also a plain text. But charset is specified as
936 ¡Hola! Buenos días. ¿Cómo está usted?
938 This is a <bold>enriched text</bold>.
939 --[[image/gif][base64]]...image encoded in base64 here...
940 --[[audio/basic][base64]]...audio encoded in base64 here...
942 User customizable variables (not documented all of them):
944 Specifies a key prefix for MIME minor mode commands.
946 mime-ignore-preceding-spaces
947 Preceding white spaces in a message body are ignored if non-nil.
949 mime-ignore-trailing-spaces
950 Trailing white spaces in a message body are ignored if non-nil.
953 Hide a non-textual body message encoded in base64 after insertion
957 A number of network transfer level. It should be bigger than 7.
958 If you are in 8bit-through environment, please set 8.
960 mime-edit-voice-recorder
961 Specifies a function to record a voice message and encode it.
962 The function `mime-edit-voice-recorder-for-sun' is for Sun
966 Turning on MIME mode calls the value of mime-edit-mode-hook, if
969 mime-edit-translate-hook
970 The value of mime-edit-translate-hook is called just before translating
971 the tagged MIME format into a MIME compliant message if it is
972 non-nil. If the hook call the function mime-edit-insert-signature,
973 the signature file will be inserted automatically.
976 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
979 (if mime-edit-mode-flag
981 (if mime-edit-touched-flag
983 (make-local-variable 'mime-edit-touched-flag)
984 (setq mime-edit-touched-flag t)
989 (cond ((featurep 'xemacs)
990 (add-minor-mode 'mime-edit-mode-flag
991 '((" MIME-Edit " mime-transfer-level-string))
997 (set-alist 'minor-mode-alist
999 '((" MIME-Edit " mime-transfer-level-string)))
1000 (set-alist 'minor-mode-map-alist
1001 'mime-edit-mode-flag
1007 (defun turn-on-mime-edit ()
1008 "Unconditionally turn on MIME-Edit mode."
1010 (if mime-edit-mode-flag
1011 (error "You are already editing a MIME message.")
1012 (setq mime-edit-mode-flag t)
1014 ;; Set transfer level into mode line
1016 (setq mime-transfer-level-string
1017 (mime-encoding-name mime-transfer-level 'not-omit))
1018 (force-mode-line-update)
1020 ;; Define menu for XEmacs.
1021 (if (featurep 'xemacs)
1022 (mime-edit-define-menu-for-xemacs)
1027 ;; I don't care about saving these.
1028 (setq paragraph-start
1029 (regexp-or mime-edit-single-part-tag-regexp
1031 (setq paragraph-separate
1032 (regexp-or mime-edit-single-part-tag-regexp
1033 paragraph-separate))
1034 (run-hooks 'mime-edit-mode-hook)
1036 (substitute-command-keys
1037 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1041 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1044 (defun mime-edit-exit (&optional nomime no-error)
1045 "Translate the tagged MIME message into a MIME compliant message.
1046 With no argument encode a message in the buffer into MIME, otherwise
1047 just return to previous mode."
1049 (if (not mime-edit-mode-flag)
1051 (error "You aren't editing a MIME message.")
1055 (run-hooks 'mime-edit-translate-hook)
1056 (mime-edit-translate-buffer)))
1057 ;; Restore previous state.
1058 (setq mime-edit-mode-flag nil)
1059 (if (and (featurep 'xemacs)
1060 (featurep 'menubar))
1061 (delete-menu-item (list mime-edit-menu-title))
1064 (set-buffer-modified-p (buffer-modified-p))
1065 (run-hooks 'mime-edit-exit-hook)
1066 (message "Exit MIME editor mode.")
1069 (defun mime-edit-maybe-translate ()
1071 (mime-edit-exit nil t)
1072 (call-interactively 'mime-edit-maybe-split-and-send)
1075 (defun mime-edit-help ()
1076 "Show help message about MIME mode."
1078 (with-output-to-temp-buffer "*Help*"
1079 (princ "MIME editor mode:\n")
1080 (princ (documentation 'mime-edit-mode))
1081 (print-help-return-message)))
1083 (defun mime-edit-insert-text (&optional subtype)
1084 "Insert a text message.
1085 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1086 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1088 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1090 (if (looking-at mime-edit-single-part-tag-regexp)
1092 ;; Make a space between the following message.
1096 (if (and (member (cadr ret) '("enriched"))
1097 (fboundp 'enriched-mode))
1099 (if (boundp 'enriched-mode)
1104 (defun mime-edit-insert-file (file &optional verbose)
1105 "Insert a message from a file."
1106 (interactive "fInsert file as MIME message: \nP")
1107 (let* ((guess (mime-find-file-type file))
1108 (type (nth 0 guess))
1109 (subtype (nth 1 guess))
1110 (parameters (nth 2 guess))
1111 (encoding (nth 3 guess))
1112 (disposition-type (nth 4 guess))
1113 (disposition-params (nth 5 guess))
1116 (setq type (mime-prompt-for-type type)
1117 subtype (mime-prompt-for-subtype type subtype)
1119 (if (or (interactive-p) verbose)
1120 (setq encoding (mime-prompt-for-encoding encoding))
1122 (if (or (consp parameters) (stringp disposition-type))
1123 (let ((rest parameters) cell attribute value)
1124 (setq parameters "")
1126 (setq cell (car rest))
1127 (setq attribute (car cell))
1128 (setq value (cdr cell))
1129 (if (eq value 'file)
1130 (setq value (std11-wrap-as-quoted-string
1131 (file-name-nondirectory file)))
1133 (setq parameters (concat parameters "; " attribute "=" value))
1134 (setq rest (cdr rest))
1136 (if disposition-type
1139 (concat parameters "\n"
1140 "Content-Disposition: " disposition-type))
1141 (setq rest disposition-params)
1143 (setq cell (car rest))
1144 (setq attribute (car cell))
1145 (setq value (cdr cell))
1146 (if (eq value 'file)
1147 (setq value (std11-wrap-as-quoted-string
1148 (file-name-nondirectory file)))
1151 (concat parameters "; " attribute "=" value))
1152 (setq rest (cdr rest))
1156 (mime-edit-insert-tag type subtype parameters)
1157 (mime-edit-insert-binary-file file encoding)
1160 (defun mime-edit-insert-external ()
1161 "Insert a reference to external body."
1163 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1165 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1167 (let* ((pritype (mime-prompt-for-type))
1168 (subtype (mime-prompt-for-subtype pritype))
1169 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1172 (insert "Content-Type: "
1173 pritype "/" subtype (or parameters "") "\n")))
1174 (if (and (not (eobp))
1175 (not (looking-at mime-edit-single-part-tag-regexp)))
1176 (insert (mime-make-text-tag) "\n")))
1178 (defun mime-edit-insert-voice ()
1179 "Insert a voice message."
1183 "What transfer encoding: "
1184 (mime-encoding-alist) nil t nil)))
1185 (mime-edit-insert-tag "audio" "basic" nil)
1186 (mime-edit-define-encoding encoding)
1188 (narrow-to-region (1- (point))(point))
1190 (funcall mime-edit-voice-recorder encoding)
1193 (invisible-region (point-min)(point-max))
1194 (goto-char (point-max))
1197 (defun mime-edit-insert-signature (&optional arg)
1198 "Insert a signature file."
1200 (let ((signature-insert-hook
1203 (let ((items (mime-find-file-type signature-file-name)))
1204 (apply (function mime-edit-insert-tag)
1205 (car items) (cadr items) (list (caddr items))))
1208 (insert-signature arg)
1212 ;; Insert a new tag around a point.
1214 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1215 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1216 If nothing is inserted, return nil."
1219 (mime-edit-goto-tag)
1220 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1221 (< (match-beginning 0) p)
1224 (goto-char (match-beginning 0))
1233 (mime-prompt-for-type)))
1236 (mime-prompt-for-subtype pritype)))
1239 (mime-prompt-for-parameters pritype subtype delimiter)))
1240 ;; Make a new MIME tag.
1241 (setq newtag (mime-make-tag pritype subtype parameters))
1242 ;; Find an current MIME tag.
1245 (if (mime-edit-goto-tag)
1246 (buffer-substring (match-beginning 0) (match-end 0))
1247 ;; Assume content type is 'text/plan'.
1248 (mime-make-tag "text" "plain")
1250 ;; We are only interested in TEXT.
1252 (not (mime-test-content-type
1253 (mime-edit-get-contype oldtag) "text")))
1256 (if (or (not oldtag) ;Not text
1257 (or mime-ignore-same-text-tag
1258 (not (string-equal oldtag newtag))))
1260 ;; Mark the beginning of the tag for convenience.
1261 (push-mark (point) 'nomsg)
1262 (insert newtag "\n")
1263 (list pritype subtype parameters) ;New tag is created.
1265 ;; Restore previous point.
1267 nil ;Nothing is created.
1271 (defun mime-edit-insert-binary-file (file &optional encoding)
1272 "Insert binary FILE at point.
1273 Optional argument ENCODING specifies an encoding method such as base64."
1274 (let* ((tagend (1- (point))) ;End of the tag
1275 (hide-p (and mime-auto-hide-body
1278 (let ((en (downcase encoding)))
1279 (or (string-equal en "7bit")
1280 (string-equal en "8bit")
1281 (string-equal en "binary")
1285 (narrow-to-region tagend (point))
1286 (mime-insert-encoded-file file encoding)
1289 (invisible-region (point-min) (point-max))
1290 (goto-char (point-max))
1292 (goto-char (point-max))
1295 (looking-at mime-edit-tag-regexp)
1296 (= (point)(point-max))
1297 (mime-edit-insert-tag "text" "plain")
1299 ;; Define encoding even if it is 7bit.
1300 (if (stringp encoding)
1302 (goto-char tagend) ; Make sure which line the tag is on.
1303 (mime-edit-define-encoding encoding)
1308 ;; Commands work on a current message flagment.
1310 (defun mime-edit-goto-tag ()
1311 "Search for the beginning of the tagged MIME message."
1312 (let ((current (point)))
1313 (if (looking-at mime-edit-tag-regexp)
1315 ;; At first, go to the end.
1316 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1317 (goto-char (1- (match-beginning 0))) ;For multiline tag
1320 (goto-char (point-max))
1322 ;; Then search for the beginning.
1323 (re-search-backward mime-edit-end-tag-regexp nil t)
1324 (or (looking-at mime-edit-beginning-tag-regexp)
1325 ;; Restore previous point.
1332 (defun mime-edit-content-beginning ()
1333 "Return the point of the beginning of content."
1335 (let ((beg (save-excursion
1336 (beginning-of-line) (point))))
1337 (if (mime-edit-goto-tag)
1338 (let ((top (point)))
1339 (goto-char (match-end 0))
1340 (if (and (= beg top)
1341 (= (following-char) ?\^M))
1345 ;; Default text/plain tag.
1346 (goto-char (point-min))
1348 (concat "\n" (regexp-quote mail-header-separator)
1349 (if mime-ignore-preceding-spaces
1350 "[ \t\n]*\n" "\n")) nil 'move)
1354 (defun mime-edit-content-end ()
1355 "Return the point of the end of content."
1357 (if (mime-edit-goto-tag)
1359 (goto-char (match-end 0))
1360 (if (invisible-p (point))
1361 (next-visible-point (point))
1362 ;; Move to the end of this text.
1363 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1364 ;; Don't forget a multiline tag.
1365 (goto-char (match-beginning 0))
1369 ;; Assume the message begins with text/plain.
1370 (goto-char (mime-edit-content-beginning))
1371 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1372 ;; Don't forget a multiline tag.
1373 (goto-char (match-beginning 0)))
1377 (defun mime-edit-define-charset (charset)
1378 "Set charset of current tag to CHARSET."
1380 (if (mime-edit-goto-tag)
1381 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1382 (delete-region (match-beginning 0) (match-end 0))
1385 (mime-edit-set-parameter
1386 (mime-edit-get-contype tag)
1387 "charset" (upcase (symbol-name charset)))
1388 (mime-edit-get-encoding tag)))
1391 (defun mime-edit-define-encoding (encoding)
1392 "Set encoding of current tag to ENCODING."
1394 (if (mime-edit-goto-tag)
1395 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1396 (delete-region (match-beginning 0) (match-end 0))
1397 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1400 (defun mime-edit-choose-charset ()
1401 "Choose charset of a text following current point."
1402 (detect-mime-charset-region (point) (mime-edit-content-end))
1405 (defun mime-make-text-tag (&optional subtype)
1406 "Make a tag for a text after current point.
1407 Subtype of text type can be specified by an optional argument SUBTYPE.
1408 Otherwise, it is obtained from mime-content-types."
1409 (let* ((pritype "text")
1410 (subtype (or subtype
1411 (car (car (cdr (assoc pritype mime-content-types)))))))
1412 ;; Charset should be defined later.
1413 (mime-make-tag pritype subtype)))
1416 ;; Tag handling functions
1418 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1419 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1420 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1424 (defun mime-create-tag (contype &optional encoding)
1425 "Make a tag with CONTENT-TYPE and optional ENCODING."
1426 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1429 (defun mime-edit-get-contype (tag)
1430 "Return Content-Type (including parameters) of TAG."
1432 (or (string-match mime-edit-single-part-tag-regexp tag)
1433 (string-match mime-edit-multipart-beginning-regexp tag)
1434 (string-match mime-edit-multipart-end-regexp tag)
1436 (substring tag (match-beginning 1) (match-end 1))
1439 (defun mime-edit-get-encoding (tag)
1440 "Return encoding of TAG."
1442 (string-match mime-edit-single-part-tag-regexp tag)
1444 (not (= (match-beginning 3) (match-end 3)))
1445 (substring tag (match-beginning 3) (match-end 3))))
1447 (defun mime-get-parameter (contype parameter)
1448 "For given CONTYPE return value for PARAMETER.
1449 Nil if no such parameter."
1453 (regexp-quote parameter)
1454 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1456 (substring contype (match-beginning 1) (match-end 1))
1457 nil ;No such parameter
1460 (defun mime-edit-set-parameter (contype parameter value)
1461 "For given CONTYPE set PARAMETER to VALUE."
1462 (let (ctype opt-fields)
1463 (if (string-match "\n[^ \t\n\r]+:" contype)
1464 (setq ctype (substring contype 0 (match-beginning 0))
1465 opt-fields (substring contype (match-beginning 0)))
1466 (setq ctype contype)
1471 (regexp-quote parameter)
1472 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1475 (concat (substring ctype 0 (match-beginning 1))
1477 (substring contype (match-end 1))
1479 (concat ctype "; " parameter "=" value opt-fields)
1482 (defun mime-strip-parameters (contype)
1483 "Return primary content-type and subtype without parameters for CONTYPE."
1484 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1485 (substring contype (match-beginning 1) (match-end 1)) nil))
1487 (defun mime-test-content-type (contype type &optional subtype)
1488 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1489 (and (stringp contype)
1492 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1493 (downcase contype))))
1498 (defun mime-find-file-type (file)
1499 "Guess Content-Type, subtype, and parameters from FILE."
1501 (guesses mime-file-types))
1502 (while (and (not guess) guesses)
1503 (if (string-match (car (car guesses)) file)
1504 (setq guess (cdr (car guesses))))
1505 (setq guesses (cdr guesses)))
1509 (defun mime-prompt-for-type (&optional default)
1510 "Ask for Content-type."
1512 ;; Repeat until primary content type is specified.
1513 (while (string-equal type "")
1515 (completing-read "What content type: "
1518 'require-match ;Type must be specified.
1521 (if (string-equal type "")
1523 (message "Content type is required.")
1530 (defun mime-prompt-for-subtype (type &optional default)
1531 "Ask for subtype of media-type TYPE."
1532 (let ((subtypes (cdr (assoc type mime-content-types))))
1534 (assoc default subtypes))
1535 (setq default (car (car subtypes)))
1541 "What content subtype: (default " default ") ")
1542 "What content subtype: ")
1543 (cdr (assoc type mime-content-types))
1545 'require-match ;Subtype must be specified.
1548 (if (string-equal answer "") default answer)))
1550 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1551 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1552 Optional DELIMITER specifies parameter delimiter (';' by default)."
1553 (let* ((delimiter (or delimiter "; "))
1558 (mime-prompt-for-parameters-1
1560 (cdr (assoc pritype mime-content-types))))))
1563 (if (and (stringp parameters)
1564 (not (string-equal parameters "")))
1565 (concat delimiter parameters)
1566 "" ;"" if no parameters
1569 (defun mime-prompt-for-parameters-1 (optlist)
1570 (apply (function append)
1571 (mapcar (function mime-prompt-for-parameter) optlist)))
1573 (defun mime-prompt-for-parameter (parameter)
1575 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1576 (let* ((prompt (car parameter))
1577 (choices (mapcar (function
1579 (if (consp e) e (list e))))
1581 (default (car (car choices)))
1587 (concat "What " prompt
1589 (if (string-equal default "") "\"\"" default)
1591 choices nil nil ""))
1592 ;; If nothing is selected, use default.
1593 (if (string-equal answer "")
1594 (setq answer default)))
1596 (read-string (concat "What " prompt ": "))))
1597 (cons (if (and answer
1598 (not (string-equal answer "")))
1600 ;; Note: control characters ignored!
1601 (if (string-match mime-tspecials-regexp answer)
1602 (concat "\"" answer "\"") answer)))
1603 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1606 (defun mime-prompt-for-encoding (default)
1607 "Ask for Content-Transfer-Encoding."
1612 "What transfer encoding: "
1613 (mime-encoding-alist) nil t default)
1619 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1622 (defvar mime-edit-translate-buffer-hook
1623 '(mime-edit-pgp-enclose-buffer
1624 mime-edit-translate-body
1625 mime-edit-translate-header))
1627 (defun mime-edit-translate-header ()
1628 "Encode the message header into network representation."
1629 (eword-encode-header 'code-conversion)
1630 (run-hooks 'mime-edit-translate-header-hook)
1633 (defun mime-edit-translate-buffer ()
1634 "Encode the tagged MIME message in current buffer in MIME compliant message."
1636 (if (catch 'mime-edit-error
1638 (run-hooks 'mime-edit-translate-buffer-hook)
1642 (error "Translation error!")
1645 (defun mime-edit-find-inmost ()
1646 (goto-char (point-min))
1647 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1648 (let ((bb (match-beginning 0))
1650 (type (buffer-substring (match-beginning 1)(match-end 1)))
1652 (setq end-exp (format "--}-<<%s>>\n" type))
1654 (if (re-search-forward end-exp nil t)
1655 (setq eb (match-beginning 0))
1656 (setq eb (point-max))
1658 (narrow-to-region be eb)
1660 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1662 (narrow-to-region (match-beginning 0)(point-max))
1663 (mime-edit-find-inmost)
1666 (list type bb be eb)
1669 (defun mime-edit-process-multipart-1 (boundary)
1670 (let ((ret (mime-edit-find-inmost)))
1672 (let ((type (car ret))
1673 (bb (nth 1 ret))(be (nth 2 ret))
1676 (narrow-to-region bb eb)
1677 (delete-region bb be)
1678 (setq bb (point-min))
1679 (setq eb (point-max))
1682 (if (looking-at mime-edit-multipart-end-regexp)
1683 (let ((beg (match-beginning 0))
1686 (delete-region beg end)
1687 (or (looking-at mime-edit-beginning-tag-regexp)
1689 (insert (concat (mime-make-text-tag) "\n"))
1691 (cond ((string-equal type "quote")
1692 (mime-edit-enquote-region bb eb)
1694 ((string-equal type "pgp-signed")
1695 (mime-edit-sign-pgp-mime bb eb boundary)
1697 ((string-equal type "pgp-encrypted")
1698 (mime-edit-encrypt-pgp-mime bb eb boundary)
1700 ((string-equal type "kazu-signed")
1701 (mime-edit-sign-pgp-kazu bb eb boundary)
1703 ((string-equal type "kazu-encrypted")
1704 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1708 (nth 2 (mime-edit-translate-region bb eb
1712 (format "--[[multipart/%s;
1713 boundary=\"%s\"][7bit]]\n"
1718 (defun mime-edit-enquote-region (beg end)
1721 (narrow-to-region beg end)
1723 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1724 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1725 (replace-match (concat "- " (substring tag 1)))
1728 (defun mime-edit-dequote-region (beg end)
1731 (narrow-to-region beg end)
1733 (while (re-search-forward
1734 mime-edit-quoted-single-part-tag-regexp nil t)
1735 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1736 (replace-match (concat "-" (substring tag 2)))
1739 (defun mime-edit-sign-pgp-mime (beg end boundary)
1742 (narrow-to-region beg end)
1744 (mime-edit-translate-region beg end boundary))
1746 (encoding (nth 1 ret))
1747 (pgp-boundary (concat "pgp-sign-" boundary)))
1749 (insert (format "Content-Type: %s\n" ctype))
1751 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1754 (or (as-binary-process
1755 (funcall (pgp-function 'mime-sign)
1756 (point-min)(point-max) nil nil pgp-boundary))
1757 (throw 'mime-edit-error 'pgp-error)
1761 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1763 (defun mime-edit-make-encrypt-recipient-header ()
1764 (let* ((names mime-edit-encrypt-recipient-fields-list)
1766 (std11-field-bodies (cons "From" names)
1767 nil mail-header-separator))
1770 (setq values (cdr values))))
1771 (header (and (stringp from)
1772 (if (string-equal from "")
1774 (format "From: %s\n" from)
1777 (while (and names values)
1778 (let ((name (car names))
1779 (value (car values))
1781 (and (stringp value)
1782 (or (string-equal value "")
1784 (setq header (concat header name ": " value "\n")
1785 recipients (if recipients
1786 (concat recipients " ," value)
1789 (setq names (cdr names)
1790 values (cdr values))
1792 (vector from recipients header)
1795 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1798 (let (from recipients header)
1799 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1800 (setq from (aref ret 0)
1801 recipients (aref ret 1)
1802 header (aref ret 2))
1804 (narrow-to-region beg end)
1806 (mime-edit-translate-region beg end boundary))
1808 (encoding (nth 1 ret))
1809 (pgp-boundary (concat "pgp-" boundary)))
1812 (insert (format "Content-Type: %s\n" ctype))
1814 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1817 (or (funcall (pgp-function 'encrypt)
1818 recipients (point-min) (point-max) from)
1819 (throw 'mime-edit-error 'pgp-error)
1822 (insert (format "--[[multipart/encrypted;
1824 protocol=\"application/pgp-encrypted\"][7bit]]
1826 Content-Type: application/pgp-encrypted
1829 Content-Type: application/octet-stream
1830 Content-Transfer-Encoding: 7bit
1832 " pgp-boundary pgp-boundary pgp-boundary))
1833 (goto-char (point-max))
1834 (insert (format "\n--%s--\n" pgp-boundary))
1837 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1840 (narrow-to-region beg end)
1842 (mime-edit-translate-region beg end boundary))
1844 (encoding (nth 1 ret)))
1846 (insert (format "Content-Type: %s\n" ctype))
1848 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1851 (or (as-binary-process
1852 (funcall (pgp-function 'traditional-sign)
1854 (throw 'mime-edit-error 'pgp-error)
1858 "--[[application/pgp; format=mime][7bit]]\n")
1862 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1864 (let (recipients header)
1865 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1866 (setq recipients (aref ret 1)
1867 header (aref ret 2))
1870 (narrow-to-region beg end)
1872 (mime-edit-translate-region beg end boundary))
1874 (encoding (nth 1 ret)))
1877 (insert (format "Content-Type: %s\n" ctype))
1879 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1882 (or (as-binary-process
1883 (funcall (pgp-function 'encrypt)
1884 recipients beg (point-max) nil 'maybe)
1886 (throw 'mime-edit-error 'pgp-error)
1890 "--[[application/pgp; format=mime][7bit]]\n")
1894 (defsubst replace-space-with-underline (str)
1895 (mapconcat (function
1903 (defun mime-edit-make-boundary ()
1904 (concat mime-multipart-boundary "_"
1905 (replace-space-with-underline (current-time-string))
1908 (defun mime-edit-translate-body ()
1909 "Encode the tagged MIME body in current buffer in MIME compliant message."
1912 (let ((boundary (mime-edit-make-boundary))
1915 (while (mime-edit-process-multipart-1
1916 (format "%s-%d" boundary i))
1920 ;; We are interested in message body.
1923 (goto-char (point-min))
1925 (concat "\n" (regexp-quote mail-header-separator)
1926 (if mime-ignore-preceding-spaces
1927 "[ \t\n]*\n" "\n")) nil 'move)
1931 (goto-char (point-max))
1932 (and mime-ignore-trailing-spaces
1933 (re-search-backward "[^ \t\n]\n" beg t)
1936 (setq ret (mime-edit-translate-region
1938 (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)
1949 ;; Make primary MIME headers.
1950 (or (mail-position-on-field "MIME-Version")
1951 (insert mime-edit-mime-version-value))
1952 ;; Remove old Content-Type and other fields.
1954 (goto-char (point-min))
1955 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1956 (narrow-to-region (point-min) (point))
1957 (goto-char (point-min))
1958 (mime-delete-field "Content-Type")
1959 (mime-delete-field "Content-Transfer-Encoding"))
1960 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1961 (mail-position-on-field "Content-Type")
1965 (mail-position-on-field "Content-Transfer-Encoding")
1969 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
1970 "Translate single-part-tag to MIME header."
1971 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1972 (let* ((beg (match-beginning 0))
1974 (tag (buffer-substring beg end)))
1975 (delete-region beg end)
1976 (let ((contype (mime-edit-get-contype tag))
1977 (encoding (mime-edit-get-encoding tag)))
1978 (insert (concat prefix "--" boundary "\n"))
1980 (narrow-to-region (point)(point))
1981 (insert "Content-Type: " contype "\n")
1983 (insert "Content-Transfer-Encoding: " encoding "\n"))
1984 (eword-encode-header)
1989 (downcase encoding))))
1992 (defun mime-edit-translate-region (beg end &optional boundary multipart)
1994 (setq boundary (mime-edit-make-boundary))
1998 (narrow-to-region beg end)
1999 (let ((tag nil) ;MIME tag
2000 (contype nil) ;Content-Type
2001 (encoding nil) ;Content-Transfer-Encoding
2002 (nparts 0)) ;Number of body parts
2003 ;; Normalize the body part by inserting appropriate message
2004 ;; tags for every message contents.
2005 (mime-edit-normalize-body)
2006 ;; Counting the number of Content-Type.
2007 (goto-char (point-min))
2008 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2009 (setq nparts (1+ nparts)))
2010 ;; Begin translation.
2012 ((and (<= nparts 1)(not multipart))
2013 ;; It's a singular message.
2014 (goto-char (point-min))
2015 (while (re-search-forward
2016 mime-edit-single-part-tag-regexp nil t)
2018 (buffer-substring (match-beginning 0) (match-end 0)))
2019 (delete-region (match-beginning 0) (1+ (match-end 0)))
2020 (setq contype (mime-edit-get-contype tag))
2021 (setq encoding (mime-edit-get-encoding tag))
2024 ;; It's a multipart message.
2025 (goto-char (point-min))
2026 (let ((prio mime-content-transfer-encoding-priority-list)
2028 (when (setq part-info
2029 (mime-edit-translate-single-part-tag boundary))
2030 (and (setq nprio (member (cdr part-info) prio))
2032 (while (setq part-info
2033 (mime-edit-translate-single-part-tag boundary "\n"))
2034 (and (setq nprio (member (cdr part-info) prio))
2035 (setq prio nprio))))
2036 ;; Define Content-Type as "multipart/mixed".
2038 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2039 (setq encoding (car prio))
2040 ;; Insert the trailer.
2041 (goto-char (point-max))
2042 (insert "\n--" boundary "--\n")
2044 (list contype encoding boundary nparts)
2047 (defun mime-edit-normalize-body ()
2048 "Normalize the body part by inserting appropriate message tags."
2049 ;; Insert the first MIME tags if necessary.
2050 (goto-char (point-min))
2051 (if (not (looking-at mime-edit-single-part-tag-regexp))
2052 (insert (mime-make-text-tag) "\n"))
2053 ;; Check each tag, and add new tag or correct it if necessary.
2054 (goto-char (point-min))
2055 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2056 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2057 (contype (mime-edit-get-contype tag))
2058 (charset (mime-get-parameter contype "charset"))
2059 (encoding (mime-edit-get-encoding tag)))
2060 ;; Remove extra whitespaces after the tag.
2061 (if (looking-at "[ \t]+$")
2062 (delete-region (match-beginning 0) (match-end 0)))
2064 (end (mime-edit-content-end))
2066 (if (= end (point-max))
2069 (or (looking-at mime-edit-beginning-tag-regexp)
2071 (insert (mime-make-text-tag) "\n")
2073 (visible-region beg end)
2077 ((mime-test-content-type contype "message")
2078 ;; Content-type "message" should be sent as is.
2081 ((mime-test-content-type contype "text")
2082 ;; Define charset for text if necessary.
2083 (setq charset (if charset
2084 (intern (downcase charset))
2085 (mime-edit-choose-charset)))
2086 (mime-edit-define-charset charset)
2087 (cond ((string-equal contype "text/x-rot13-47-48")
2090 (mule-caesar-region (point) (mime-edit-content-end))
2092 ((string-equal contype "text/enriched")
2097 (end (mime-edit-content-end))
2099 ;; Patch for hard newlines
2102 ;; (while (search-forward "\n" end t)
2103 ;; (put-text-property (match-beginning 0)
2106 ;; End patch for hard newlines
2107 (enriched-encode beg end)
2109 (if (search-forward "\n\n")
2110 (delete-region beg (match-end 0))
2113 ;; Point is now on current tag.
2114 ;; Define encoding and encode text if necessary.
2115 (or encoding ;Encoding is not specified.
2118 (let ((ret (cdr (assq charset mime-charset-type-list))))
2120 (setq bits (car ret)
2123 conv "quoted-printable")))
2124 (if (<= bits mime-transfer-level)
2125 (mime-encoding-name bits)
2127 (beg (mime-edit-content-beginning)))
2128 (encode-mime-charset-region beg (mime-edit-content-end)
2130 ;; Protect "From " in beginning of line
2132 (narrow-to-region beg (mime-edit-content-end))
2134 (let (case-fold-search)
2135 (if (re-search-forward "^From " nil t)
2137 (if (memq charset '(iso-2022-jp
2142 (replace-match "\e(BFrom ")
2143 (re-search-forward "^From " nil t)
2145 (setq encoding "quoted-printable")
2147 ;; canonicalize line break code
2148 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2150 (narrow-to-region beg (mime-edit-content-end))
2152 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2153 (replace-match "\\1\r\n"))))
2155 (mime-encode-region beg (mime-edit-content-end)
2156 (or encoding "7bit"))
2157 (mime-edit-define-encoding encoding)
2159 (goto-char (mime-edit-content-end))
2161 ((null encoding) ;Encoding is not specified.
2162 ;; Application, image, audio, video, and any other
2163 ;; unknown content-type without encoding should be
2165 (let* ((encoding "base64") ;Encode in BASE64 by default.
2166 (beg (mime-edit-content-beginning))
2167 (end (mime-edit-content-end)))
2168 (mime-encode-region beg end encoding)
2169 (mime-edit-define-encoding encoding))
2174 (defun mime-delete-field (field)
2175 "Delete header FIELD."
2176 (let ((regexp (format "^%s:[ \t]*" field)))
2177 (goto-char (point-min))
2178 (while (re-search-forward regexp nil t)
2179 (delete-region (match-beginning 0)
2180 (1+ (std11-field-end))))))
2184 ;;; Platform dependent functions
2187 ;; Sun implementations
2189 (defun mime-edit-voice-recorder-for-sun (encoding)
2190 "Record voice in a buffer using Sun audio device,
2191 and insert data encoded as ENCODING."
2192 (message "Start the recording on %s. Type C-g to finish the recording..."
2194 (mime-insert-encoded-file "/dev/audio" encoding)
2198 ;;; @ Other useful commands.
2201 ;; Message forwarding commands as content-type "message/rfc822".
2203 (defun mime-edit-insert-message (&optional message)
2205 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2206 (if (and inserter (fboundp inserter))
2208 (mime-edit-insert-tag "message" "rfc822")
2209 (funcall inserter message)
2211 (message "Sorry, I don't have message inserter for your MUA.")
2214 (defun mime-edit-insert-mail (&optional message)
2216 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2217 (if (and inserter (fboundp inserter))
2219 (mime-edit-insert-tag "message" "rfc822")
2220 (funcall inserter message)
2222 (message "Sorry, I don't have mail inserter for your MUA.")
2225 (defun mime-edit-inserted-message-filter ()
2228 (let ((header-start (point))
2229 (case-fold-search t)
2232 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2233 (if (re-search-forward "^$" (mark t))
2234 (narrow-to-region header-start (match-beginning 0))
2236 (goto-char header-start)
2237 (while (and (re-search-forward
2238 mime-edit-yank-ignored-field-regexp nil t)
2239 (setq beg (match-beginning 0))
2240 (setq end (1+ (std11-field-end)))
2242 (delete-region beg end)
2247 ;;; @ multipart enclosure
2250 (defun mime-edit-enclose-region-internal (type beg end)
2254 (narrow-to-region beg end)
2255 (insert (format "--<<%s>>-{\n" type))
2256 (goto-char (point-max))
2257 (insert (format "--}-<<%s>>\n" type))
2258 (goto-char (point-max))
2260 (or (looking-at mime-edit-beginning-tag-regexp)
2262 (insert (mime-make-text-tag) "\n")
2266 (defun mime-edit-enclose-quote-region (beg end)
2268 (mime-edit-enclose-region-internal 'quote beg end)
2271 (defun mime-edit-enclose-mixed-region (beg end)
2273 (mime-edit-enclose-region-internal 'mixed beg end)
2276 (defun mime-edit-enclose-parallel-region (beg end)
2278 (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)
2286 (defun mime-edit-enclose-alternative-region (beg end)
2288 (mime-edit-enclose-region-internal 'alternative beg end)
2291 (defun mime-edit-enclose-pgp-signed-region (beg end)
2293 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2296 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2298 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2301 (defun mime-edit-enclose-kazu-signed-region (beg end)
2303 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2306 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2308 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2311 (defun mime-edit-insert-key (&optional arg)
2312 "Insert a pgp public key."
2314 (mime-edit-insert-tag "application" "pgp-keys")
2315 (mime-edit-define-encoding "7bit")
2316 (funcall (pgp-function 'insert-key))
2323 (defun mime-edit-set-split (arg)
2326 (y-or-n-p "Do you want to enable split? ")
2328 (setq mime-edit-split-message arg)
2330 (message "This message is enabled to split.")
2331 (message "This message is not enabled to split.")
2334 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2335 "Toggle transfer-level is 7bit or 8bit through.
2337 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2339 (if (numberp transfer-level)
2340 (setq mime-transfer-level transfer-level)
2341 (if (< mime-transfer-level 8)
2342 (setq mime-transfer-level 8)
2343 (setq mime-transfer-level 7)
2345 (message (format "Current transfer-level is %d bit"
2346 mime-transfer-level))
2347 (setq mime-transfer-level-string
2348 (mime-encoding-name mime-transfer-level 'not-omit))
2349 (force-mode-line-update)
2352 (defun mime-edit-set-transfer-level-7bit ()
2354 (mime-edit-toggle-transfer-level 7)
2357 (defun mime-edit-set-transfer-level-8bit ()
2359 (mime-edit-toggle-transfer-level 8)
2366 (defvar mime-edit-pgp-processing nil)
2367 (make-variable-buffer-local 'mime-edit-pgp-processing)
2369 (defun mime-edit-set-sign (arg)
2372 (y-or-n-p "Do you want to sign? ")
2376 (setq mime-edit-pgp-processing 'sign)
2377 (message "This message will be signed.")
2379 (if (eq mime-edit-pgp-processing 'sign)
2380 (setq mime-edit-pgp-processing nil)
2382 (message "This message will not be signed.")
2385 (defun mime-edit-set-encrypt (arg)
2388 (y-or-n-p "Do you want to encrypt? ")
2392 (setq mime-edit-pgp-processing 'encrypt)
2393 (message "This message will be encrypt.")
2395 (if (eq mime-edit-pgp-processing 'encrypt)
2396 (setq mime-edit-pgp-processing nil)
2398 (message "This message will not be encrypt.")
2401 (defun mime-edit-pgp-enclose-buffer ()
2402 (let ((beg (save-excursion
2403 (goto-char (point-min))
2404 (if (search-forward (concat "\n" mail-header-separator "\n"))
2410 (cond ((eq mime-edit-pgp-processing 'sign)
2411 (mime-edit-enclose-pgp-signed-region beg end)
2413 ((eq mime-edit-pgp-processing 'encrypt)
2414 (mime-edit-enclose-pgp-encrypted-region beg end)
2422 (defun mime-edit-insert-partial-header (fields subject
2423 id number total separator)
2425 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2426 (insert mime-edit-mime-version-field-for-message/partial)
2428 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2429 id number total separator))
2432 (defun mime-edit-split-and-send
2433 (&optional cmd lines mime-edit-message-max-length)
2437 (count-lines (point-min) (point-max)))
2439 (or mime-edit-message-max-length
2440 (setq mime-edit-message-max-length
2441 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2442 mime-edit-message-default-max-lines))
2444 (let* ((mime-edit-draft-file-name
2445 (or (buffer-file-name)
2447 (expand-file-name "mime-draft" temporary-file-directory))))
2448 (separator mail-header-separator)
2450 (replace-space-with-underline (current-time-string))
2451 "@" (system-name) "\"")))
2452 (run-hooks 'mime-edit-before-split-hook)
2453 (let ((the-buf (current-buffer))
2454 (copy-buf (get-buffer-create " *Original Message*"))
2455 (header (std11-header-string-except
2456 mime-edit-split-ignored-field-regexp separator))
2457 (subject (mail-fetch-field "subject"))
2458 (total (+ (/ lines mime-edit-message-max-length)
2459 (if (> (mod lines mime-edit-message-max-length) 0)
2465 mime-edit-split-message-sender-alist))
2469 (error "Split sender is not specified for `%s'." major-mode)
2472 (mime-edit-partial-number 1)
2475 (set-buffer copy-buf)
2477 (insert-buffer the-buf)
2479 (if (re-search-forward
2480 (concat "^" (regexp-quote separator) "$") nil t)
2481 (let ((he (match-beginning 0)))
2483 (narrow-to-region (point-min) he)
2485 (goto-char (point-min))
2486 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2487 (delete-region (match-beginning 0)
2488 (1+ (std11-field-end)))
2490 (while (< mime-edit-partial-number total)
2493 (set-buffer copy-buf)
2494 (setq data (buffer-substring
2497 (goto-line mime-edit-message-max-length)
2500 (delete-region (point-min)(point))
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 (call-interactively command)
2509 (message (format "Sending %d/%d... done"
2510 mime-edit-partial-number total))
2512 (setq mime-edit-partial-number
2513 (1+ mime-edit-partial-number))
2517 (set-buffer copy-buf)
2518 (setq data (buffer-string))
2521 (mime-edit-insert-partial-header
2522 header subject id mime-edit-partial-number total separator)
2525 (message (format "Sending %d/%d..."
2526 mime-edit-partial-number total))
2527 (message (format "Sending %d/%d... done"
2528 mime-edit-partial-number total))
2532 (defun mime-edit-maybe-split-and-send (&optional cmd)
2534 (run-hooks 'mime-edit-before-send-hook)
2535 (let ((mime-edit-message-max-length
2536 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2537 mime-edit-message-default-max-lines))
2538 (lines (count-lines (point-min) (point-max)))
2540 (if (and (> lines mime-edit-message-max-length)
2541 mime-edit-split-message)
2542 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2546 ;;; @ preview message
2549 (defvar mime-edit-buffer nil) ; buffer local variable
2551 (defun mime-edit-preview-message ()
2552 "preview editing MIME message."
2554 (let* ((str (buffer-string))
2555 (separator mail-header-separator)
2556 (the-buf (current-buffer))
2557 (buf-name (buffer-name))
2558 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2559 (buf (get-buffer temp-buf-name))
2563 (switch-to-buffer buf)
2566 (setq buf (get-buffer-create temp-buf-name))
2567 (switch-to-buffer buf)
2570 (setq major-mode 'mime-temp-message-mode)
2571 (make-local-variable 'mail-header-separator)
2572 (setq mail-header-separator separator)
2573 (make-local-variable 'mime-edit-buffer)
2574 (setq mime-edit-buffer the-buf)
2576 (run-hooks 'mime-edit-translate-hook)
2577 (mime-edit-translate-buffer)
2578 (goto-char (point-min))
2579 (if (re-search-forward
2580 (concat "^" (regexp-quote separator) "$"))
2586 (defun mime-edit-quitting-method ()
2587 "Quitting method for mime-view."
2588 (let* ((entity (get-text-property (point-min) 'mime-view-entity))
2589 (temp (mime-entity-buffer entity))
2591 (mime-preview-kill-buffer)
2593 (setq buf mime-edit-buffer)
2595 (switch-to-buffer buf)
2598 (set-alist 'mime-preview-quitting-method-alist
2599 'mime-temp-message-mode
2600 #'mime-edit-quitting-method)
2606 (defvar mime-edit-again-ignored-field-regexp
2607 (concat "^\\(" "Content-.*\\|Mime-Version"
2608 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2610 "Regexp for deleted header fields when `mime-edit-again' is called.")
2612 (defsubst eliminate-top-spaces (string)
2613 "Eliminate top sequence of space or tab in STRING."
2614 (if (string-match "^[ \t]+" string)
2615 (substring string (match-end 0))
2618 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2619 (let* ((subtype (mime-content-type-subtype content-type))
2620 (boundary (mime-content-type-parameter content-type "boundary"))
2621 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2622 (re-search-forward boundary-pat nil t)
2623 (let ((bb (match-beginning 0)) eb tag)
2624 (setq tag (format "\n--<<%s>>-{\n" subtype))
2627 (setq bb (+ bb (length tag)))
2629 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2631 (setq eb (match-beginning 0))
2632 (replace-match (format "--}-<<%s>>\n" subtype))
2634 (narrow-to-region bb eb)
2635 (goto-char (point-min))
2636 (while (re-search-forward boundary-pat nil t)
2637 (let ((beg (match-beginning 0))
2639 (delete-region beg (match-end 0))
2641 (if (re-search-forward boundary-pat nil t)
2642 (setq end (match-beginning 0))
2643 (setq end (point-max))
2646 (narrow-to-region beg end)
2647 (mime-edit-decode-message-in-buffer
2648 (if (eq subtype 'digest)
2650 (make-mime-content-type 'message 'rfc822))
2653 (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)
2665 (defun mime-edit-decode-single-part-in-buffer (content-type not-decode-text)
2666 (let* ((type (mime-content-type-primary-type content-type))
2667 (subtype (mime-content-type-subtype content-type))
2668 (ctype (format "%s/%s" type subtype))
2670 (pstr (let ((bytes (+ 14 (length ctype))))
2671 (mapconcat (function
2673 (if (string= (car attr) "charset")
2675 (setq charset (cdr attr))
2677 (let* ((str (concat (car attr)
2680 (setq bytes (+ bytes bs 2))
2683 (setq bytes (+ bs 1))
2687 (mime-content-type-parameters content-type) "")))
2690 (limit (save-excursion
2691 (if (search-forward "\n\n" nil t)
2694 (if (re-search-forward
2695 "^Content-Transfer-Encoding:" limit t)
2696 (let ((beg (match-beginning 0))
2697 (hbeg (match-end 0))
2698 (end (std11-field-end limit)))
2701 (eliminate-top-spaces
2702 (std11-unfold-string
2703 (buffer-substring hbeg end)))))
2704 (if (or charset (eq type 'text))
2706 (delete-region beg (1+ end))
2707 (goto-char (point-min))
2708 (if (search-forward "\n\n" nil t)
2711 (match-end 0)(point-max) encoding)
2715 (if (or encoded (not not-decode-text))
2718 (goto-char (point-min))
2719 (while (re-search-forward "\r\n" nil t)
2720 (replace-match "\n")
2722 (decode-mime-charset-region (point-min)(point-max)
2723 (or charset default-mime-charset))
2725 (let ((he (if (re-search-forward "^$" nil t)
2729 (if (and (eq type 'text)
2730 (eq subtype 'x-rot13-47-48))
2731 (mule-caesar-region he (point-max))
2733 (if (= (point-min) 1)
2739 (format "%s/%s%s" type subtype pstr)
2742 (delete-region (point-min) he)
2744 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2750 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2753 (goto-char (point-min))
2754 (let ((ctl (or (mime-read-Content-Type)
2755 default-content-type)))
2757 (let ((type (mime-content-type-primary-type ctl)))
2759 ((and (eq type 'application)
2760 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2761 (delete-region (point-min)(point-max))
2763 ((eq type 'multipart)
2764 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
2767 (mime-edit-decode-single-part-in-buffer ctl not-decode-text)
2770 (decode-mime-charset-region (point-min) (point-max)
2771 default-mime-charset))
2774 (std11-narrow-to-header)
2775 (goto-char (point-min))
2776 (while (re-search-forward mime-edit-again-ignored-field-regexp nil t)
2777 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2779 (mime-decode-header-in-buffer (not not-decode-text))
2783 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2784 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2785 Content-Type and Content-Transfer-Encoding header fields will be
2786 converted to MIME-Edit tags."
2788 (goto-char (point-min))
2790 (concat "\n" (regexp-quote mail-header-separator) "\n")
2792 (replace-match "\n\n")
2794 (mime-edit-decode-message-in-buffer nil not-decode-text)
2795 (goto-char (point-min))
2797 (and (re-search-forward "^$")
2798 (replace-match mail-header-separator)
2808 (provide 'mime-edit)
2810 (run-hooks 'mime-edit-load-hook)
2812 ;;; mime-edit.el ends here