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)
122 (defconst mime-edit-version
124 (mime-product-name mime-user-interface-product) " "
125 (mapconcat #'number-to-string
126 (mime-product-version mime-user-interface-product) ".")
127 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
133 (defgroup mime-edit nil
137 (defcustom mime-ignore-preceding-spaces nil
138 "*Ignore preceding white spaces if non-nil."
142 (defcustom mime-ignore-trailing-spaces nil
143 "*Ignore trailing white spaces if non-nil."
147 (defcustom mime-ignore-same-text-tag t
148 "*Ignore preceding text content-type tag that is same with new one.
149 If non-nil, the text tag is not inserted unless something different."
153 (defcustom mime-auto-hide-body t
154 "*Hide non-textual body encoded in base64 after insertion if non-nil."
158 (defcustom mime-edit-voice-recorder
159 (function mime-edit-voice-recorder-for-sun)
160 "*Function to record a voice message and encode it."
164 (defcustom mime-edit-mode-hook nil
165 "*Hook called when enter MIME mode."
169 (defcustom mime-edit-translate-hook nil
170 "*Hook called before translating into a MIME compliant message.
171 To insert a signature file automatically, call the function
172 `mime-edit-insert-signature' from this hook."
176 (defcustom mime-edit-exit-hook nil
177 "*Hook called when exit MIME mode."
181 (defvar mime-content-types
183 ;; Charset parameter need not to be specified, since it is
184 ;; defined automatically while translation.
186 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
199 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
200 ("directory" "/pub/GNU/elisp/mime")
202 ("mode" "image" "ascii" "local8"))
207 ("mode" "image" "ascii" "local8"))
208 ("tftp" ("site") ("name"))
209 ("afs" ("site") ("name"))
210 ("local-file" ("site") ("name"))
212 ("server" "ftpmail@nic.karrn.ad.jp")
220 ("octet-stream" ("type" "" "tar" "shar"))
222 ("vnd.ms-powerpoint")
223 ("x-kiss" ("x-cnf")))
237 "*Alist of content-type, subtype, parameters and its values.")
239 (defcustom mime-file-types
242 ;; Programming languages
245 "application" "octet-stream" (("type" . "C++"))
247 "attachment" (("filename" . file))
251 "application" "octet-stream" (("type" . "emacs-lisp"))
253 "attachment" (("filename" . file))
257 "application" "octet-stream" (("type" . "common-lisp"))
259 "attachment" (("filename" . file))
263 "application" "octet-stream" (("type" . "perl"))
265 "attachment" (("filename" . file))
268 ;; Text or translated text
273 "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))
290 ("\\.diff$\\|\\.patch$"
291 "application" "octet-stream" (("type" . "patch"))
293 "attachment" (("filename" . file))
297 "text" "plain" nil nil nil nil)
300 ;; Octect binary text
303 "application" "winword" nil
305 "attachment" (("filename" . file))
307 ("\\.ppt$" ; MS Power Point
308 "application" "vnd.ms-powerpoint" nil
310 "attachment" (("filename" . file))
316 "inline" (("filename" . file))
319 "application" "postscript" nil
321 "attachment" (("filename" . file))
329 "inline" (("filename" . file))
334 "inline" (("filename" . file))
339 "inline" (("filename" . file))
344 "inline" (("filename" . file))
349 "inline" (("filename" . file))
354 "inline" (("filename" . file))
359 "inline" (("filename" . file))
364 "inline" (("filename" . file))
369 "attachment" (("filename" . file))
374 "attachment" (("filename" . file))
377 "application" "octet-stream" (("type" . "tar+gzip"))
379 "attachment" (("filename" . file))
382 "application" "octet-stream" (("type" . "tar+gzip"))
384 "attachment" (("filename" . file))
387 "application" "octet-stream" (("type" . "tar+compress"))
389 "attachment" (("filename" . file))
392 "application" "octet-stream" (("type" . "tar+compress"))
394 "attachment" (("filename" . file))
397 "application" "octet-stream" (("type" . "gzip"))
399 "attachment" (("filename" . file))
402 "application" "octet-stream" (("type" . "compress"))
404 "attachment" (("filename" . file))
407 "application" "octet-stream" (("type" . "lha"))
409 "attachment" (("filename" . file))
412 "application" "zip" nil
414 "attachment" (("filename" . file))
420 "application" "octet-stream" nil
422 "attachment" (("filename" . file)))
424 "*Alist of file name, types, parameters, and default encoding.
425 If encoding is nil, it is determined from its contents."
429 (choice :tag "Primary-Type"
430 ,@(nconc (mapcar (lambda (cell)
431 (list 'item (car cell))
436 (choice :tag "Sub-Type"
439 (mapcar (lambda (cell)
440 (mapcar (lambda (cell)
441 (list 'item (car cell))
447 (repeat :tag "Parameters of Content-Type field"
448 (cons string (choice string symbol)))
449 ;; content-transfer-encoding
450 (choice :tag "Encoding"
453 (mapcar (lambda (cell)
456 (mime-encoding-list))))
458 (choice :tag "Disposition-Type"
464 (repeat :tag "Parameters of Content-Disposition field"
465 (cons string (choice string symbol)))
470 ;;; @@ about charset, encoding and transfer-level
473 (defvar mime-charset-type-list
475 (iso-8859-1 8 "quoted-printable")
476 (iso-8859-2 8 "quoted-printable")
477 (iso-8859-3 8 "quoted-printable")
478 (iso-8859-4 8 "quoted-printable")
479 (iso-8859-5 8 "quoted-printable")
480 (koi8-r 8 "quoted-printable")
481 (iso-8859-7 8 "quoted-printable")
482 (iso-8859-8 8 "quoted-printable")
483 (iso-8859-9 8 "quoted-printable")
484 (iso-2022-jp 7 "base64")
485 (iso-2022-kr 7 "base64")
491 (shift_jis 8 "base64")
493 (iso-2022-jp-2 7 "base64")
494 (iso-2022-int-1 7 "base64")
497 (defvar mime-transfer-level 7
498 "*A number of network transfer level. It should be bigger than 7.")
499 (make-variable-buffer-local 'mime-transfer-level)
501 (defsubst mime-encoding-name (transfer-level &optional not-omit)
502 (cond ((> transfer-level 8) "binary")
503 ((= transfer-level 8) "8bit")
507 (defvar mime-transfer-level-string
508 (mime-encoding-name mime-transfer-level 'not-omit)
509 "A string formatted version of mime-transfer-level")
510 (make-variable-buffer-local 'mime-transfer-level-string)
512 (defvar mime-edit-use-long-mime-charset-comment t
513 "*Use charset comment")
515 (put 'iso-2022-jp-2 'mime-charset-comment "RFC1554")
516 (put 'iso-2022-jp 'mime-charset-comment "RFC1468 with trivial bugfix")
518 ;;; @@ about content transfer encoding
520 (defvar mime-content-transfer-encoding-priority-list
521 '(nil "8bit" "binary"))
523 ;;; @@ about message inserting
526 (defvar mime-edit-yank-ignored-field-list
527 '("Received" "Approved" "Path" "Replied" "Status"
528 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
529 "Delete these fields from original message when it is inserted
530 as message/rfc822 part.
531 Each elements are regexp of field-name.")
533 (defvar mime-edit-yank-ignored-field-regexp
535 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
538 (defvar mime-edit-message-inserter-alist nil)
539 (defvar mime-edit-mail-inserter-alist nil)
542 ;;; @@ about message splitting
545 (defcustom mime-edit-split-message t
546 "*Split large message if it is non-nil."
550 (defcustom mime-edit-message-default-max-lines 1000
551 "*Default maximum lines of a message."
555 (defcustom mime-edit-message-max-lines-alist
556 '((news-reply-mode . 500))
557 "Alist of major-mode vs maximum lines of a message.
558 If it is not specified for a major-mode,
559 `mime-edit-message-default-max-lines' is used."
563 (defconst mime-edit-split-ignored-field-regexp
564 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
566 (defcustom mime-edit-split-blind-field-regexp
567 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
568 "*Regular expression to match field-name to be ignored when split sending."
572 (defvar mime-edit-split-message-sender-alist nil)
574 (defvar mime-edit-news-reply-mode-server-running nil)
580 (defconst mime-edit-single-part-tag-regexp
581 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
582 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
584 (defconst mime-edit-quoted-single-part-tag-regexp
585 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
587 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
589 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
591 (defconst mime-edit-beginning-tag-regexp
592 (regexp-or mime-edit-single-part-tag-regexp
593 mime-edit-multipart-beginning-regexp))
595 (defconst mime-edit-end-tag-regexp
596 (regexp-or mime-edit-single-part-tag-regexp
597 mime-edit-multipart-end-regexp))
599 (defconst mime-edit-tag-regexp
600 (regexp-or mime-edit-single-part-tag-regexp
601 mime-edit-multipart-beginning-regexp
602 mime-edit-multipart-end-regexp))
604 (defvar mime-tag-format "--[[%s]]"
605 "*Control-string making a MIME tag.")
607 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
608 "*Control-string making a MIME tag with encoding.")
611 ;;; @@ multipart boundary
614 (defvar mime-multipart-boundary "Multipart"
615 "*Boundary of a multipart message.")
618 ;;; @@ optional header fields
621 (defvar mime-edit-insert-user-agent-field t
622 "*If non-nil, insert User-Agent header field.")
624 (defvar mime-edit-user-agent-value
625 (concat (mime-product-name mime-user-interface-product)
627 (mapconcat #'number-to-string
628 (mime-product-version mime-user-interface-product) ".")
630 (mime-product-code-name mime-user-interface-product)
632 (mime-product-name mime-library-product)
634 (mapconcat #'number-to-string
635 (mime-product-version mime-library-product) ".")
637 (mime-product-code-name mime-library-product)
639 (if (featurep 'xemacs)
640 (concat (if (featurep 'mule) "MULE")
642 (if (string-match "\\s +\\\"" emacs-version)
644 (substring emacs-version 0
646 (if (and (boundp 'xemacs-betaname)
647 ;; It does not exist in XEmacs
648 ;; versions prior to 20.3.
650 (concat " " xemacs-betaname)
652 " (" xemacs-codename ") ("
653 system-configuration ")")
654 " (" emacs-version ")"))
655 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
656 (substring emacs-version 0 (match-beginning 0))
659 (if (boundp 'enable-multibyte-characters)
661 " (" system-configuration ")"
662 (if enable-multibyte-characters
663 (concat " MULE/" mule-version)
664 " (with unibyte mode)")
665 (if (featurep 'meadow)
666 (let ((mver (Meadow-version)))
667 (if (string-match "^Meadow-" mver)
672 (concat "MULE/" mule-version
673 " (based on Emacs " ver ")"))
674 (concat "Emacs/" ver " (" system-configuration ")")))))
675 "Body of User-Agent field.
676 If variable `mime-edit-insert-user-agent-field' is not nil, it is
677 inserted into message header.")
683 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
684 "*Specify MIME tspecials.
685 Tspecials means any character that matches with it in header must be quoted.")
687 (defconst mime-edit-mime-version-value
689 (concat "1.0 (generated by " mime-edit-version ")"))
690 "MIME version number.")
692 (defconst mime-edit-mime-version-field-for-message/partial
694 (concat "MIME-Version: 1.0 (split by " mime-edit-version ")\n"))
695 "MIME version field for message/partial.")
698 ;;; @ keymap and menu
701 (defvar mime-edit-mode-flag nil)
702 (make-variable-buffer-local 'mime-edit-mode-flag)
704 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
705 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
706 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
707 "Keymap for MIME-Edit mode commands to insert entity or set status.")
709 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
710 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
711 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
712 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
713 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
714 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
715 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
716 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
717 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
718 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
720 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
721 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
722 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
723 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
724 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
725 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
726 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
727 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
728 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
729 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
731 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
732 "Keymap prefix for MIME-Edit mode commands about enclosure.")
733 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
734 "Keymap for MIME-Edit mode commands about enclosure.")
736 (define-key mime-edit-mode-enclosure-map
737 "\C-a" 'mime-edit-enclose-alternative-region)
738 (define-key mime-edit-mode-enclosure-map
739 "\C-p" 'mime-edit-enclose-parallel-region)
740 (define-key mime-edit-mode-enclosure-map
741 "\C-m" 'mime-edit-enclose-mixed-region)
742 (define-key mime-edit-mode-enclosure-map
743 "\C-d" 'mime-edit-enclose-digest-region)
744 (define-key mime-edit-mode-enclosure-map
745 "\C-s" 'mime-edit-enclose-pgp-signed-region)
746 (define-key mime-edit-mode-enclosure-map
747 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
748 (define-key mime-edit-mode-enclosure-map
749 "\C-q" 'mime-edit-enclose-quote-region)
751 (defvar mime-edit-mode-map (make-sparse-keymap)
752 "Keymap for MIME-Edit mode commands.")
753 (define-key mime-edit-mode-map
754 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
755 (define-key mime-edit-mode-map
756 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
758 (defconst mime-edit-menu-title "MIME-Edit")
760 (defconst mime-edit-menu-list
761 '((mime-help "Describe MIME editor mode" mime-edit-help)
762 (file "Insert File" mime-edit-insert-file)
763 (external "Insert External" mime-edit-insert-external)
764 (voice "Insert Voice" mime-edit-insert-voice)
765 (message "Insert Message" mime-edit-insert-message)
766 (mail "Insert Mail" mime-edit-insert-mail)
767 (signature "Insert Signature" mime-edit-insert-signature)
768 (text "Insert Text" mime-edit-insert-text)
769 (tag "Insert Tag" mime-edit-insert-tag)
770 (alternative "Enclose as alternative"
771 mime-edit-enclose-alternative-region)
772 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
773 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
774 (digest "Enclose as digest" mime-edit-enclose-digest-region)
775 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
776 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
777 (quote "Verbatim region" mime-edit-enclose-quote-region)
778 (key "Insert Public Key" mime-edit-insert-key)
779 (split "About split" mime-edit-set-split)
780 (sign "About sign" mime-edit-set-sign)
781 (encrypt "About encryption" mime-edit-set-encrypt)
782 (preview "Preview Message" mime-edit-preview-message)
783 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
785 "MIME-edit menubar entry.")
787 (cond ((featurep 'xemacs)
788 ;; modified by Pekka Marjola <pema@iki.fi>
789 ;; 1995/9/5 (c.f. [tm-en:69])
790 (defun mime-edit-define-menu-for-xemacs ()
791 "Define menu for XEmacs."
792 (cond ((featurep 'menubar)
793 (make-local-variable 'current-menubar)
794 (set-buffer-menubar current-menubar)
797 (cons mime-edit-menu-title
800 (vector (nth 1 item)(nth 2 item)
803 mime-edit-menu-list)))
806 ;; modified by Steven L. Baur <steve@miranova.com>
807 ;; 1995/12/6 (c.f. [tm-en:209])
808 (or (boundp 'mime-edit-popup-menu-for-xemacs)
809 (setq mime-edit-popup-menu-for-xemacs
810 (append '("MIME Commands" "---")
811 (mapcar (function (lambda (item)
815 mime-edit-menu-list)))
818 ((>= emacs-major-version 19)
819 (define-key mime-edit-mode-map [menu-bar mime-edit]
820 (cons mime-edit-menu-title
821 (make-sparse-keymap mime-edit-menu-title)))
824 (define-key mime-edit-mode-map
825 (vector 'menu-bar 'mime-edit (car item))
826 (cons (nth 1 item)(nth 2 item))
829 (reverse mime-edit-menu-list)
837 (defvar mime-edit-touched-flag nil)
840 (defun mime-edit-mode ()
841 "MIME minor mode for editing the tagged MIME message.
843 In this mode, basically, the message is composed in the tagged MIME
844 format. The message tag looks like:
846 --[[text/plain; charset=ISO-2022-JP][7bit]]
848 The tag specifies the MIME content type, subtype, optional parameters
849 and transfer encoding of the message following the tag. Messages
850 without any tag are treated as `text/plain' by default. Charset and
851 transfer encoding are automatically defined unless explicitly
852 specified. Binary messages such as audio and image are usually
853 hidden. The messages in the tagged MIME format are automatically
854 translated into a MIME compliant message when exiting this mode.
856 Available charsets depend on Emacs version being used. The following
857 lists the available charsets of each emacs.
859 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
860 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
861 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
862 CN-BIG5 and ISO-2022-INT-1 are available.
864 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
865 be used to represent multilingual text in intermixed manner. Any
866 languages that has no registered charset are represented as either
867 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
869 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
870 without mule, please set variable `default-mime-charset'. This
871 variable must be symbol of which name is a MIME charset.
873 If you want to add more charsets in mule, please set variable
874 `charsets-mime-charset-alist'. This variable must be alist of which
875 key is list of charset and value is symbol of MIME charset. If name
876 of coding-system is different as MIME charset, please set variable
877 `mime-charset-coding-system-alist'. This variable must be alist of
878 which key is MIME charset and value is coding-system.
880 Following commands are available in addition to major mode commands:
883 \\[mime-edit-insert-text] insert a text message.
884 \\[mime-edit-insert-file] insert a (binary) file.
885 \\[mime-edit-insert-external] insert a reference to external body.
886 \\[mime-edit-insert-voice] insert a voice message.
887 \\[mime-edit-insert-message] insert a mail or news message.
888 \\[mime-edit-insert-mail] insert a mail message.
889 \\[mime-edit-insert-signature] insert a signature file at end.
890 \\[mime-edit-insert-key] insert PGP public key.
891 \\[mime-edit-insert-tag] insert a new MIME tag.
893 \[make enclosure (maybe multipart)\]
894 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
895 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
896 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
897 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
898 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
899 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
900 \\[mime-edit-enclose-quote-region] enclose as verbose mode
901 (to avoid to expand tags)
904 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
905 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
906 \\[mime-edit-set-split] set message splitting mode.
907 \\[mime-edit-set-sign] set PGP-sign mode.
908 \\[mime-edit-set-encrypt] set PGP-encryption mode.
909 \\[mime-edit-preview-message] preview editing MIME message.
910 \\[mime-edit-exit] exit and translate into a MIME
912 \\[mime-edit-help] show this help.
913 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
916 Additional commands are available in some major modes:
917 C-c C-c exit, translate and run the original command.
918 C-c C-s exit, translate and run the original command.
920 The following is a message example written in the tagged MIME format.
921 TABs at the beginning of the line are not a part of the message:
923 This is a conventional plain text. It should be translated
926 This is also a plain text. But, it is explicitly specified as
928 --[[text/plain; charset=ISO-8859-1]]
929 This is also a plain text. But charset is specified as
932 ¡Hola! Buenos días. ¿Cómo está usted?
934 This is a <bold>enriched text</bold>.
935 --[[image/gif][base64]]...image encoded in base64 here...
936 --[[audio/basic][base64]]...audio encoded in base64 here...
938 User customizable variables (not documented all of them):
940 Specifies a key prefix for MIME minor mode commands.
942 mime-ignore-preceding-spaces
943 Preceding white spaces in a message body are ignored if non-nil.
945 mime-ignore-trailing-spaces
946 Trailing white spaces in a message body are ignored if non-nil.
949 Hide a non-textual body message encoded in base64 after insertion
953 A number of network transfer level. It should be bigger than 7.
954 If you are in 8bit-through environment, please set 8.
956 mime-edit-voice-recorder
957 Specifies a function to record a voice message and encode it.
958 The function `mime-edit-voice-recorder-for-sun' is for Sun
962 Turning on MIME mode calls the value of mime-edit-mode-hook, if
965 mime-edit-translate-hook
966 The value of mime-edit-translate-hook is called just before translating
967 the tagged MIME format into a MIME compliant message if it is
968 non-nil. If the hook call the function mime-edit-insert-signature,
969 the signature file will be inserted automatically.
972 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
975 (if mime-edit-mode-flag
977 (if mime-edit-touched-flag
979 (make-local-variable 'mime-edit-touched-flag)
980 (setq mime-edit-touched-flag t)
985 (cond ((featurep 'xemacs)
986 (add-minor-mode 'mime-edit-mode-flag
987 '((" MIME-Edit " mime-transfer-level-string))
993 (set-alist 'minor-mode-alist
995 '((" MIME-Edit " mime-transfer-level-string)))
996 (set-alist 'minor-mode-map-alist
1003 (defun turn-on-mime-edit ()
1004 "Unconditionally turn on MIME-Edit mode."
1006 (if mime-edit-mode-flag
1007 (error "You are already editing a MIME message.")
1008 (setq mime-edit-mode-flag t)
1010 ;; Set transfer level into mode line
1012 (setq mime-transfer-level-string
1013 (mime-encoding-name mime-transfer-level 'not-omit))
1014 (force-mode-line-update)
1016 ;; Define menu for XEmacs.
1017 (if (featurep 'xemacs)
1018 (mime-edit-define-menu-for-xemacs)
1023 ;; I don't care about saving these.
1024 (setq paragraph-start
1025 (regexp-or mime-edit-single-part-tag-regexp
1027 (setq paragraph-separate
1028 (regexp-or mime-edit-single-part-tag-regexp
1029 paragraph-separate))
1030 (run-hooks 'mime-edit-mode-hook)
1032 (substitute-command-keys
1033 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1037 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1040 (defun mime-edit-exit (&optional nomime no-error)
1041 "Translate the tagged MIME message into a MIME compliant message.
1042 With no argument encode a message in the buffer into MIME, otherwise
1043 just return to previous mode."
1045 (if (not mime-edit-mode-flag)
1047 (error "You aren't editing a MIME message.")
1051 (run-hooks 'mime-edit-translate-hook)
1052 (mime-edit-translate-buffer)))
1053 ;; Restore previous state.
1054 (setq mime-edit-mode-flag nil)
1055 (if (and (featurep 'xemacs)
1056 (featurep 'menubar))
1057 (delete-menu-item (list mime-edit-menu-title))
1060 (set-buffer-modified-p (buffer-modified-p))
1061 (run-hooks 'mime-edit-exit-hook)
1062 (message "Exit MIME editor mode.")
1065 (defun mime-edit-maybe-translate ()
1067 (mime-edit-exit nil t)
1068 (call-interactively 'mime-edit-maybe-split-and-send)
1071 (defun mime-edit-help ()
1072 "Show help message about MIME mode."
1074 (with-output-to-temp-buffer "*Help*"
1075 (princ "MIME editor mode:\n")
1076 (princ (documentation 'mime-edit-mode))
1077 (print-help-return-message)))
1079 (defun mime-edit-insert-text (&optional subtype)
1080 "Insert a text message.
1081 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1082 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1084 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1086 (if (looking-at mime-edit-single-part-tag-regexp)
1088 ;; Make a space between the following message.
1092 (if (and (member (cadr ret) '("enriched"))
1093 (fboundp 'enriched-mode))
1095 (if (boundp 'enriched-mode)
1100 (defun mime-edit-insert-file (file &optional verbose)
1101 "Insert a message from a file."
1102 (interactive "fInsert file as MIME message: \nP")
1103 (let* ((guess (mime-find-file-type file))
1104 (type (nth 0 guess))
1105 (subtype (nth 1 guess))
1106 (parameters (nth 2 guess))
1107 (encoding (nth 3 guess))
1108 (disposition-type (nth 4 guess))
1109 (disposition-params (nth 5 guess))
1112 (setq type (mime-prompt-for-type type)
1113 subtype (mime-prompt-for-subtype type subtype)
1115 (if (or (interactive-p) verbose)
1116 (setq encoding (mime-prompt-for-encoding encoding))
1118 (if (or (consp parameters) (stringp disposition-type))
1119 (let ((rest parameters) cell attribute value)
1120 (setq parameters "")
1122 (setq cell (car rest))
1123 (setq attribute (car cell))
1124 (setq value (cdr cell))
1125 (if (eq value 'file)
1126 (setq value (std11-wrap-as-quoted-string
1127 (file-name-nondirectory file)))
1129 (setq parameters (concat parameters "; " attribute "=" value))
1130 (setq rest (cdr rest))
1132 (if disposition-type
1135 (concat parameters "\n"
1136 "Content-Disposition: " disposition-type))
1137 (setq rest disposition-params)
1139 (setq cell (car rest))
1140 (setq attribute (car cell))
1141 (setq value (cdr cell))
1142 (if (eq value 'file)
1143 (setq value (std11-wrap-as-quoted-string
1144 (file-name-nondirectory file)))
1147 (concat parameters "; " attribute "=" value))
1148 (setq rest (cdr rest))
1152 (mime-edit-insert-tag type subtype parameters)
1153 (mime-edit-insert-binary-file file encoding)
1156 (defun mime-edit-insert-external ()
1157 "Insert a reference to external body."
1159 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1161 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1163 (let* ((pritype (mime-prompt-for-type))
1164 (subtype (mime-prompt-for-subtype pritype))
1165 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1168 (insert "Content-Type: "
1169 pritype "/" subtype (or parameters "") "\n")))
1170 (if (and (not (eobp))
1171 (not (looking-at mime-edit-single-part-tag-regexp)))
1172 (insert (mime-make-text-tag) "\n")))
1174 (defun mime-edit-insert-voice ()
1175 "Insert a voice message."
1179 "What transfer encoding: "
1180 (mime-encoding-alist) nil t nil)))
1181 (mime-edit-insert-tag "audio" "basic" nil)
1182 (mime-edit-define-encoding encoding)
1184 (narrow-to-region (1- (point))(point))
1186 (funcall mime-edit-voice-recorder encoding)
1189 (invisible-region (point-min)(point-max))
1190 (goto-char (point-max))
1193 (defun mime-edit-insert-signature (&optional arg)
1194 "Insert a signature file."
1196 (let ((signature-insert-hook
1199 (let ((items (mime-find-file-type signature-file-name)))
1200 (apply (function mime-edit-insert-tag)
1201 (car items) (cadr items) (list (caddr items))))
1204 (insert-signature arg)
1208 ;; Insert a new tag around a point.
1210 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1211 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1212 If nothing is inserted, return nil."
1215 (mime-edit-goto-tag)
1216 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1217 (< (match-beginning 0) p)
1220 (goto-char (match-beginning 0))
1229 (mime-prompt-for-type)))
1232 (mime-prompt-for-subtype pritype)))
1235 (mime-prompt-for-parameters pritype subtype delimiter)))
1236 ;; Make a new MIME tag.
1237 (setq newtag (mime-make-tag pritype subtype parameters))
1238 ;; Find an current MIME tag.
1241 (if (mime-edit-goto-tag)
1242 (buffer-substring (match-beginning 0) (match-end 0))
1243 ;; Assume content type is 'text/plan'.
1244 (mime-make-tag "text" "plain")
1246 ;; We are only interested in TEXT.
1248 (not (mime-test-content-type
1249 (mime-edit-get-contype oldtag) "text")))
1252 (if (or (not oldtag) ;Not text
1253 (or mime-ignore-same-text-tag
1254 (not (string-equal oldtag newtag))))
1256 ;; Mark the beginning of the tag for convenience.
1257 (push-mark (point) 'nomsg)
1258 (insert newtag "\n")
1259 (list pritype subtype parameters) ;New tag is created.
1261 ;; Restore previous point.
1263 nil ;Nothing is created.
1267 (defun mime-edit-insert-binary-file (file &optional encoding)
1268 "Insert binary FILE at point.
1269 Optional argument ENCODING specifies an encoding method such as base64."
1270 (let* ((tagend (1- (point))) ;End of the tag
1271 (hide-p (and mime-auto-hide-body
1274 (let ((en (downcase encoding)))
1275 (or (string-equal en "7bit")
1276 (string-equal en "8bit")
1277 (string-equal en "binary")
1281 (narrow-to-region tagend (point))
1282 (mime-insert-encoded-file file encoding)
1285 (invisible-region (point-min) (point-max))
1286 (goto-char (point-max))
1288 (goto-char (point-max))
1291 (looking-at mime-edit-tag-regexp)
1292 (= (point)(point-max))
1293 (mime-edit-insert-tag "text" "plain")
1295 ;; Define encoding even if it is 7bit.
1296 (if (stringp encoding)
1298 (goto-char tagend) ; Make sure which line the tag is on.
1299 (mime-edit-define-encoding encoding)
1304 ;; Commands work on a current message flagment.
1306 (defun mime-edit-goto-tag ()
1307 "Search for the beginning of the tagged MIME message."
1308 (let ((current (point)))
1309 (if (looking-at mime-edit-tag-regexp)
1311 ;; At first, go to the end.
1312 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1313 (goto-char (1- (match-beginning 0))) ;For multiline tag
1316 (goto-char (point-max))
1318 ;; Then search for the beginning.
1319 (re-search-backward mime-edit-end-tag-regexp nil t)
1320 (or (looking-at mime-edit-beginning-tag-regexp)
1321 ;; Restore previous point.
1328 (defun mime-edit-content-beginning ()
1329 "Return the point of the beginning of content."
1331 (let ((beg (save-excursion
1332 (beginning-of-line) (point))))
1333 (if (mime-edit-goto-tag)
1334 (let ((top (point)))
1335 (goto-char (match-end 0))
1336 (if (and (= beg top)
1337 (= (following-char) ?\^M))
1341 ;; Default text/plain tag.
1342 (goto-char (point-min))
1344 (concat "\n" (regexp-quote mail-header-separator)
1345 (if mime-ignore-preceding-spaces
1346 "[ \t\n]*\n" "\n")) nil 'move)
1350 (defun mime-edit-content-end ()
1351 "Return the point of the end of content."
1353 (if (mime-edit-goto-tag)
1355 (goto-char (match-end 0))
1356 (if (invisible-p (point))
1357 (next-visible-point (point))
1358 ;; Move to the end of this text.
1359 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1360 ;; Don't forget a multiline tag.
1361 (goto-char (match-beginning 0))
1365 ;; Assume the message begins with text/plain.
1366 (goto-char (mime-edit-content-beginning))
1367 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1368 ;; Don't forget a multiline tag.
1369 (goto-char (match-beginning 0)))
1373 (defun mime-edit-define-charset (charset)
1374 "Set charset of current tag to CHARSET."
1376 (if (mime-edit-goto-tag)
1377 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1378 (delete-region (match-beginning 0) (match-end 0))
1381 (mime-edit-set-parameter
1382 (mime-edit-get-contype tag)
1384 (let ((comment (get charset 'mime-charset-comment)))
1385 (if (and mime-edit-use-long-mime-charset-comment comment)
1386 (concat (upcase (symbol-name charset)) " (" comment ")")
1387 (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 nil)
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 ((temp mime-raw-buffer)
2590 (mime-preview-kill-buffer)
2592 (setq buf mime-edit-buffer)
2594 (switch-to-buffer buf)
2597 (set-alist 'mime-preview-quitting-method-alist
2598 'mime-temp-message-mode
2599 #'mime-edit-quitting-method)
2605 (defvar mime-edit-again-ignored-field-regexp
2606 (concat "^\\(" "Content-.*\\|Mime-Version"
2607 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2609 "Regexp for deleted header fields when `mime-edit-again' is called.")
2611 (defsubst eliminate-top-spaces (string)
2612 "Eliminate top sequence of space or tab in STRING."
2613 (if (string-match "^[ \t]+" string)
2614 (substring string (match-end 0))
2617 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2618 (let* ((subtype (mime-content-type-subtype content-type))
2619 (boundary (mime-content-type-parameter content-type "boundary"))
2620 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2621 (re-search-forward boundary-pat nil t)
2622 (let ((bb (match-beginning 0)) eb tag)
2623 (setq tag (format "\n--<<%s>>-{\n" subtype))
2626 (setq bb (+ bb (length tag)))
2628 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2630 (setq eb (match-beginning 0))
2631 (replace-match (format "--}-<<%s>>\n" subtype))
2633 (narrow-to-region bb eb)
2634 (goto-char (point-min))
2635 (while (re-search-forward boundary-pat nil t)
2636 (let ((beg (match-beginning 0))
2638 (delete-region beg (match-end 0))
2640 (if (re-search-forward boundary-pat nil t)
2641 (setq end (match-beginning 0))
2642 (setq end (point-max))
2645 (narrow-to-region beg end)
2646 (mime-edit-decode-message-in-buffer
2647 (if (eq subtype 'digest)
2649 (make-mime-content-type 'message 'rfc822))
2652 (goto-char (point-max))
2655 (goto-char (point-min))
2656 (or (= (point-min) 1)
2657 (delete-region (point-min)
2658 (if (search-forward "\n\n" nil t)
2664 (defun mime-edit-decode-single-part-in-buffer (content-type not-decode-text)
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))
2686 (mime-content-type-parameters content-type) "")))
2689 (limit (save-excursion
2690 (if (search-forward "\n\n" nil t)
2693 (if (re-search-forward
2694 "^Content-Transfer-Encoding:" limit t)
2695 (let ((beg (match-beginning 0))
2696 (hbeg (match-end 0))
2697 (end (std11-field-end limit)))
2700 (eliminate-top-spaces
2701 (std11-unfold-string
2702 (buffer-substring hbeg end)))))
2703 (if (or charset (eq type 'text))
2705 (delete-region beg (1+ end))
2706 (goto-char (point-min))
2707 (if (search-forward "\n\n" nil t)
2710 (match-end 0)(point-max) encoding)
2714 (if (or encoded (not not-decode-text))
2717 (goto-char (point-min))
2718 (while (re-search-forward "\r\n" nil t)
2719 (replace-match "\n")
2721 (decode-mime-charset-region (point-min)(point-max)
2722 (or charset default-mime-charset))
2724 (let ((he (if (re-search-forward "^$" nil t)
2728 (if (and (eq type 'text)
2729 (eq subtype 'x-rot13-47-48))
2730 (mule-caesar-region he (point-max))
2732 (if (= (point-min) 1)
2738 (format "%s/%s%s" type subtype pstr)
2741 (delete-region (point-min) he)
2743 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2749 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2752 (goto-char (point-min))
2753 (let ((ctl (or (mime-read-Content-Type)
2754 default-content-type)))
2756 (let ((type (mime-content-type-primary-type ctl)))
2758 ((and (eq type 'application)
2759 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2760 (delete-region (point-min)(point-max))
2762 ((eq type 'multipart)
2763 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
2766 (mime-edit-decode-single-part-in-buffer ctl not-decode-text)
2769 (decode-mime-charset-region (point-min) (point-max)
2770 default-mime-charset))
2773 (std11-narrow-to-header)
2774 (goto-char (point-min))
2775 (while (re-search-forward mime-edit-again-ignored-field-regexp nil t)
2776 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2778 (mime-decode-header-in-buffer (not not-decode-text))
2782 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2783 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2784 Content-Type and Content-Transfer-Encoding header fields will be
2785 converted to MIME-Edit tags."
2787 (goto-char (point-min))
2789 (concat "\n" (regexp-quote mail-header-separator) "\n")
2791 (replace-match "\n\n")
2793 (mime-edit-decode-message-in-buffer nil not-decode-text)
2794 (goto-char (point-min))
2796 (and (re-search-forward "^$")
2797 (replace-match mail-header-separator)
2807 (provide 'mime-edit)
2809 (run-hooks 'mime-edit-load-hook)
2811 ;;; mime-edit.el ends here