1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,1994,1995,1996,1997,1998 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
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) "\"")))
135 (defgroup mime-edit nil
139 (defcustom mime-ignore-preceding-spaces nil
140 "*Ignore preceding white spaces if non-nil."
144 (defcustom mime-ignore-trailing-spaces nil
145 "*Ignore trailing white spaces if non-nil."
149 (defcustom mime-ignore-same-text-tag t
150 "*Ignore preceding text content-type tag that is same with new one.
151 If non-nil, the text tag is not inserted unless something different."
155 (defcustom mime-auto-hide-body t
156 "*Hide non-textual body encoded in base64 after insertion if non-nil."
160 (defcustom mime-edit-voice-recorder
161 (function mime-edit-voice-recorder-for-sun)
162 "*Function to record a voice message and encode it."
166 (defcustom mime-edit-mode-hook nil
167 "*Hook called when enter MIME mode."
171 (defcustom mime-edit-translate-hook nil
172 "*Hook called before translating into a MIME compliant message.
173 To insert a signature file automatically, call the function
174 `mime-edit-insert-signature' from this hook."
178 (defcustom mime-edit-exit-hook nil
179 "*Hook called when exit MIME mode."
183 (defvar mime-content-types
185 ;; Charset parameter need not to be specified, since it is
186 ;; defined automatically while translation.
188 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
201 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
202 ("directory" "/pub/GNU/elisp/mime")
204 ("mode" "image" "ascii" "local8"))
209 ("mode" "image" "ascii" "local8"))
210 ("tftp" ("site") ("name"))
211 ("afs" ("site") ("name"))
212 ("local-file" ("site") ("name"))
214 ("server" "ftpmail@nic.karrn.ad.jp")
222 ("octet-stream" ("type" "" "tar" "shar"))
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))
312 "inline" (("filename" . file))
315 "application" "postscript" nil
317 "attachment" (("filename" . file))
325 "inline" (("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 "attachment" (("filename" . file))
370 "attachment" (("filename" . file))
373 "application" "octet-stream" (("type" . "tar+gzip"))
375 "attachment" (("filename" . file))
378 "application" "octet-stream" (("type" . "tar+gzip"))
380 "attachment" (("filename" . file))
383 "application" "octet-stream" (("type" . "tar+compress"))
385 "attachment" (("filename" . file))
388 "application" "octet-stream" (("type" . "tar+compress"))
390 "attachment" (("filename" . file))
393 "application" "octet-stream" (("type" . "gzip"))
395 "attachment" (("filename" . file))
398 "application" "octet-stream" (("type" . "compress"))
400 "attachment" (("filename" . file))
403 "application" "octet-stream" (("type" . "lha"))
405 "attachment" (("filename" . file))
408 "application" "zip" nil
410 "attachment" (("filename" . file))
416 "application" "octet-stream" nil
418 "attachment" (("filename" . file)))
420 "*Alist of file name, types, parameters, and default encoding.
421 If encoding is nil, it is determined from its contents."
425 (choice :tag "Primary-Type"
426 ,@(nconc (mapcar (lambda (cell)
427 (list 'item (car cell))
432 (choice :tag "Sub-Type"
435 (mapcar (lambda (cell)
436 (mapcar (lambda (cell)
437 (list 'item (car cell))
443 (repeat :tag "Parameters of Content-Type field"
444 (cons string (choice string symbol)))
445 ;; content-transfer-encoding
446 (choice :tag "Encoding"
449 (mapcar (lambda (cell)
452 (mime-encoding-list))))
454 (choice :tag "Disposition-Type"
460 (repeat :tag "Parameters of Content-Disposition field"
461 (cons string (choice string symbol)))
466 ;;; @@ about charset, encoding and transfer-level
469 (defvar mime-charset-type-list
471 (iso-8859-1 8 "quoted-printable")
472 (iso-8859-2 8 "quoted-printable")
473 (iso-8859-3 8 "quoted-printable")
474 (iso-8859-4 8 "quoted-printable")
475 (iso-8859-5 8 "quoted-printable")
476 (koi8-r 8 "quoted-printable")
477 (iso-8859-7 8 "quoted-printable")
478 (iso-8859-8 8 "quoted-printable")
479 (iso-8859-9 8 "quoted-printable")
480 (iso-2022-jp 7 "base64")
481 (iso-2022-kr 7 "base64")
487 (shift_jis 8 "base64")
488 (iso-2022-jp-2 7 "base64")
489 (iso-2022-int-1 7 "base64")
492 (defvar mime-transfer-level 7
493 "*A number of network transfer level. It should be bigger than 7.")
494 (make-variable-buffer-local 'mime-transfer-level)
496 (defsubst mime-encoding-name (transfer-level &optional not-omit)
497 (cond ((> transfer-level 8) "binary")
498 ((= transfer-level 8) "8bit")
502 (defvar mime-transfer-level-string
503 (mime-encoding-name mime-transfer-level 'not-omit)
504 "A string formatted version of mime-transfer-level")
505 (make-variable-buffer-local 'mime-transfer-level-string)
508 ;;; @@ about content transfer encoding
510 (defvar mime-content-transfer-encoding-priority-list
511 '(nil "8bit" "binary"))
513 ;;; @@ about message inserting
516 (defvar mime-edit-yank-ignored-field-list
517 '("Received" "Approved" "Path" "Replied" "Status"
518 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
519 "Delete these fields from original message when it is inserted
520 as message/rfc822 part.
521 Each elements are regexp of field-name.")
523 (defvar mime-edit-yank-ignored-field-regexp
525 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
528 (defvar mime-edit-message-inserter-alist nil)
529 (defvar mime-edit-mail-inserter-alist nil)
532 ;;; @@ about message splitting
535 (defcustom mime-edit-split-message t
536 "*Split large message if it is non-nil."
540 (defcustom mime-edit-message-default-max-lines 1000
541 "*Default maximum lines of a message."
545 (defcustom mime-edit-message-max-lines-alist
546 '((news-reply-mode . 500))
547 "Alist of major-mode vs maximum lines of a message.
548 If it is not specified for a major-mode,
549 `mime-edit-message-default-max-lines' is used."
553 (defconst mime-edit-split-ignored-field-regexp
554 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
556 (defcustom mime-edit-split-blind-field-regexp
557 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
558 "*Regular expression to match field-name to be ignored when split sending."
562 (defvar mime-edit-split-message-sender-alist nil)
564 (defvar mime-edit-news-reply-mode-server-running nil)
570 (defconst mime-edit-single-part-tag-regexp
571 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
572 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
574 (defconst mime-edit-quoted-single-part-tag-regexp
575 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
577 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
579 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
581 (defconst mime-edit-beginning-tag-regexp
582 (regexp-or mime-edit-single-part-tag-regexp
583 mime-edit-multipart-beginning-regexp))
585 (defconst mime-edit-end-tag-regexp
586 (regexp-or mime-edit-single-part-tag-regexp
587 mime-edit-multipart-end-regexp))
589 (defconst mime-edit-tag-regexp
590 (regexp-or mime-edit-single-part-tag-regexp
591 mime-edit-multipart-beginning-regexp
592 mime-edit-multipart-end-regexp))
594 (defvar mime-tag-format "--[[%s]]"
595 "*Control-string making a MIME tag.")
597 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
598 "*Control-string making a MIME tag with encoding.")
601 ;;; @@ multipart boundary
604 (defvar mime-multipart-boundary "Multipart"
605 "*Boundary of a multipart message.")
608 ;;; @@ optional header fields
611 (defvar mime-edit-insert-user-agent-field t
612 "*If non-nil, insert User-Agent header field.")
614 (defvar mime-edit-user-agent-value
615 (concat (mime-product-name mime-user-interface-product)
617 (mapconcat #'number-to-string
618 (mime-product-version mime-user-interface-product) ".")
620 (mime-product-code-name mime-user-interface-product)
622 (mime-product-name mime-library-product)
624 (mapconcat #'number-to-string
625 (mime-product-version mime-library-product) ".")
627 (mime-product-code-name mime-library-product)
629 (if (featurep 'xemacs)
630 (concat (if (featurep 'mule) "MULE")
632 (if (string-match "\\s +\\\"" emacs-version)
634 (substring emacs-version 0
636 (if (and (boundp 'xemacs-betaname)
637 ;; It does not exist in XEmacs
638 ;; versions prior to 20.3.
640 (concat " " xemacs-betaname)
642 " (" xemacs-codename ") ("
643 system-configuration ")")
644 " (" emacs-version ")"))
645 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
646 (substring emacs-version 0 (match-beginning 0))
649 (if (boundp 'enable-multibyte-characters)
651 " (" system-configuration ")"
652 (if enable-multibyte-characters
653 (concat " MULE/" mule-version)
654 " (with unibyte mode)")
655 (if (featurep 'meadow)
656 (let ((mver (Meadow-version)))
657 (if (string-match "^Meadow-" mver)
662 (concat "MULE/" mule-version
663 " (based on Emacs " ver ")"))
664 (concat "Emacs/" ver " (" system-configuration ")")))))
665 "Body of User-Agent field.
666 If variable `mime-edit-insert-user-agent-field' is not nil, it is
667 inserted into message header.")
673 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
674 "*Specify MIME tspecials.
675 Tspecials means any character that matches with it in header must be quoted.")
677 (defconst mime-edit-mime-version-value
679 (concat "1.0 (generated by " mime-edit-version ")"))
680 "MIME version number.")
682 (defconst mime-edit-mime-version-field-for-message/partial
684 (concat "MIME-Version: 1.0 (split by " mime-edit-version ")\n"))
685 "MIME version field for message/partial.")
688 ;;; @ keymap and menu
691 (defvar mime-edit-mode-flag nil)
692 (make-variable-buffer-local 'mime-edit-mode-flag)
694 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
695 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
696 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
697 "Keymap for MIME-Edit mode commands to insert entity or set status.")
699 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
700 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
701 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
702 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
703 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
704 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
705 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
706 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
707 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
708 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
710 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
711 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
712 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
713 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
714 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
715 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
716 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
717 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
718 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
719 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
721 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
722 "Keymap prefix for MIME-Edit mode commands about enclosure.")
723 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
724 "Keymap for MIME-Edit mode commands about enclosure.")
726 (define-key mime-edit-mode-enclosure-map
727 "\C-a" 'mime-edit-enclose-alternative-region)
728 (define-key mime-edit-mode-enclosure-map
729 "\C-p" 'mime-edit-enclose-parallel-region)
730 (define-key mime-edit-mode-enclosure-map
731 "\C-m" 'mime-edit-enclose-mixed-region)
732 (define-key mime-edit-mode-enclosure-map
733 "\C-d" 'mime-edit-enclose-digest-region)
734 (define-key mime-edit-mode-enclosure-map
735 "\C-s" 'mime-edit-enclose-pgp-signed-region)
736 (define-key mime-edit-mode-enclosure-map
737 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
738 (define-key mime-edit-mode-enclosure-map
739 "\C-q" 'mime-edit-enclose-quote-region)
741 (defvar mime-edit-mode-map (make-sparse-keymap)
742 "Keymap for MIME-Edit mode commands.")
743 (define-key mime-edit-mode-map
744 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
745 (define-key mime-edit-mode-map
746 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
748 (defconst mime-edit-menu-title "MIME-Edit")
750 (defconst mime-edit-menu-list
751 '((mime-help "Describe MIME editor mode" mime-edit-help)
752 (file "Insert File" mime-edit-insert-file)
753 (external "Insert External" mime-edit-insert-external)
754 (voice "Insert Voice" mime-edit-insert-voice)
755 (message "Insert Message" mime-edit-insert-message)
756 (mail "Insert Mail" mime-edit-insert-mail)
757 (signature "Insert Signature" mime-edit-insert-signature)
758 (text "Insert Text" mime-edit-insert-text)
759 (tag "Insert Tag" mime-edit-insert-tag)
760 (alternative "Enclose as alternative"
761 mime-edit-enclose-alternative-region)
762 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
763 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
764 (digest "Enclose as digest" mime-edit-enclose-digest-region)
765 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
766 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
767 (quote "Verbatim region" mime-edit-enclose-quote-region)
768 (key "Insert Public Key" mime-edit-insert-key)
769 (split "About split" mime-edit-set-split)
770 (sign "About sign" mime-edit-set-sign)
771 (encrypt "About encryption" mime-edit-set-encrypt)
772 (preview "Preview Message" mime-edit-preview-message)
773 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
775 "MIME-edit menubar entry.")
777 (cond ((featurep 'xemacs)
778 ;; modified by Pekka Marjola <pema@iki.fi>
779 ;; 1995/9/5 (c.f. [tm-en:69])
780 (defun mime-edit-define-menu-for-xemacs ()
781 "Define menu for XEmacs."
782 (cond ((featurep 'menubar)
783 (make-local-variable 'current-menubar)
784 (set-buffer-menubar current-menubar)
787 (cons mime-edit-menu-title
790 (vector (nth 1 item)(nth 2 item)
793 mime-edit-menu-list)))
796 ;; modified by Steven L. Baur <steve@miranova.com>
797 ;; 1995/12/6 (c.f. [tm-en:209])
798 (or (boundp 'mime-edit-popup-menu-for-xemacs)
799 (setq mime-edit-popup-menu-for-xemacs
800 (append '("MIME Commands" "---")
801 (mapcar (function (lambda (item)
805 mime-edit-menu-list)))
808 ((>= emacs-major-version 19)
809 (define-key mime-edit-mode-map [menu-bar mime-edit]
810 (cons mime-edit-menu-title
811 (make-sparse-keymap mime-edit-menu-title)))
814 (define-key mime-edit-mode-map
815 (vector 'menu-bar 'mime-edit (car item))
816 (cons (nth 1 item)(nth 2 item))
819 (reverse mime-edit-menu-list)
827 (defvar mime-edit-touched-flag nil)
830 (defun mime-edit-mode ()
831 "MIME minor mode for editing the tagged MIME message.
833 In this mode, basically, the message is composed in the tagged MIME
834 format. The message tag looks like:
836 --[[text/plain; charset=ISO-2022-JP][7bit]]
838 The tag specifies the MIME content type, subtype, optional parameters
839 and transfer encoding of the message following the tag. Messages
840 without any tag are treated as `text/plain' by default. Charset and
841 transfer encoding are automatically defined unless explicitly
842 specified. Binary messages such as audio and image are usually
843 hidden. The messages in the tagged MIME format are automatically
844 translated into a MIME compliant message when exiting this mode.
846 Available charsets depend on Emacs version being used. The following
847 lists the available charsets of each emacs.
849 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
850 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
851 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
852 CN-BIG5 and ISO-2022-INT-1 are available.
854 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
855 be used to represent multilingual text in intermixed manner. Any
856 languages that has no registered charset are represented as either
857 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
859 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
860 without mule, please set variable `default-mime-charset'. This
861 variable must be symbol of which name is a MIME charset.
863 If you want to add more charsets in mule, please set variable
864 `charsets-mime-charset-alist'. This variable must be alist of which
865 key is list of charset and value is symbol of MIME charset. If name
866 of coding-system is different as MIME charset, please set variable
867 `mime-charset-coding-system-alist'. This variable must be alist of
868 which key is MIME charset and value is coding-system.
870 Following commands are available in addition to major mode commands:
873 \\[mime-edit-insert-text] insert a text message.
874 \\[mime-edit-insert-file] insert a (binary) file.
875 \\[mime-edit-insert-external] insert a reference to external body.
876 \\[mime-edit-insert-voice] insert a voice message.
877 \\[mime-edit-insert-message] insert a mail or news message.
878 \\[mime-edit-insert-mail] insert a mail message.
879 \\[mime-edit-insert-signature] insert a signature file at end.
880 \\[mime-edit-insert-key] insert PGP public key.
881 \\[mime-edit-insert-tag] insert a new MIME tag.
883 \[make enclosure (maybe multipart)\]
884 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
885 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
886 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
887 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
888 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
889 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
890 \\[mime-edit-enclose-quote-region] enclose as verbose mode
891 (to avoid to expand tags)
894 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
895 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
896 \\[mime-edit-set-split] set message splitting mode.
897 \\[mime-edit-set-sign] set PGP-sign mode.
898 \\[mime-edit-set-encrypt] set PGP-encryption mode.
899 \\[mime-edit-preview-message] preview editing MIME message.
900 \\[mime-edit-exit] exit and translate into a MIME
902 \\[mime-edit-help] show this help.
903 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
906 Additional commands are available in some major modes:
907 C-c C-c exit, translate and run the original command.
908 C-c C-s exit, translate and run the original command.
910 The following is a message example written in the tagged MIME format.
911 TABs at the beginning of the line are not a part of the message:
913 This is a conventional plain text. It should be translated
916 This is also a plain text. But, it is explicitly specified as
918 --[[text/plain; charset=ISO-8859-1]]
919 This is also a plain text. But charset is specified as
922 ¡Hola! Buenos días. ¿Cómo está usted?
924 This is a <bold>enriched text</bold>.
925 --[[image/gif][base64]]...image encoded in base64 here...
926 --[[audio/basic][base64]]...audio encoded in base64 here...
928 User customizable variables (not documented all of them):
930 Specifies a key prefix for MIME minor mode commands.
932 mime-ignore-preceding-spaces
933 Preceding white spaces in a message body are ignored if non-nil.
935 mime-ignore-trailing-spaces
936 Trailing white spaces in a message body are ignored if non-nil.
939 Hide a non-textual body message encoded in base64 after insertion
943 A number of network transfer level. It should be bigger than 7.
944 If you are in 8bit-through environment, please set 8.
946 mime-edit-voice-recorder
947 Specifies a function to record a voice message and encode it.
948 The function `mime-edit-voice-recorder-for-sun' is for Sun
952 Turning on MIME mode calls the value of mime-edit-mode-hook, if
955 mime-edit-translate-hook
956 The value of mime-edit-translate-hook is called just before translating
957 the tagged MIME format into a MIME compliant message if it is
958 non-nil. If the hook call the function mime-edit-insert-signature,
959 the signature file will be inserted automatically.
962 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
965 (if mime-edit-mode-flag
967 (if mime-edit-touched-flag
969 (make-local-variable 'mime-edit-touched-flag)
970 (setq mime-edit-touched-flag t)
975 (cond ((featurep 'xemacs)
976 (add-minor-mode 'mime-edit-mode-flag
977 '((" MIME-Edit " mime-transfer-level-string))
983 (set-alist 'minor-mode-alist
985 '((" MIME-Edit " mime-transfer-level-string)))
986 (set-alist 'minor-mode-map-alist
993 (defun turn-on-mime-edit ()
994 "Unconditionally turn on MIME-Edit mode."
996 (if mime-edit-mode-flag
997 (error "You are already editing a MIME message.")
998 (setq mime-edit-mode-flag t)
1000 ;; Set transfer level into mode line
1002 (setq mime-transfer-level-string
1003 (mime-encoding-name mime-transfer-level 'not-omit))
1004 (force-mode-line-update)
1006 ;; Define menu for XEmacs.
1007 (if (featurep 'xemacs)
1008 (mime-edit-define-menu-for-xemacs)
1013 ;; I don't care about saving these.
1014 (setq paragraph-start
1015 (regexp-or mime-edit-single-part-tag-regexp
1017 (setq paragraph-separate
1018 (regexp-or mime-edit-single-part-tag-regexp
1019 paragraph-separate))
1020 (run-hooks 'mime-edit-mode-hook)
1022 (substitute-command-keys
1023 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1027 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1030 (defun mime-edit-exit (&optional nomime no-error)
1031 "Translate the tagged MIME message into a MIME compliant message.
1032 With no argument encode a message in the buffer into MIME, otherwise
1033 just return to previous mode."
1035 (if (not mime-edit-mode-flag)
1037 (error "You aren't editing a MIME message.")
1041 (run-hooks 'mime-edit-translate-hook)
1042 (mime-edit-translate-buffer)))
1043 ;; Restore previous state.
1044 (setq mime-edit-mode-flag nil)
1045 (if (and (featurep 'xemacs)
1046 (featurep 'menubar))
1047 (delete-menu-item (list mime-edit-menu-title))
1050 (set-buffer-modified-p (buffer-modified-p))
1051 (run-hooks 'mime-edit-exit-hook)
1052 (message "Exit MIME editor mode.")
1055 (defun mime-edit-maybe-translate ()
1057 (mime-edit-exit nil t)
1058 (call-interactively 'mime-edit-maybe-split-and-send)
1061 (defun mime-edit-help ()
1062 "Show help message about MIME mode."
1064 (with-output-to-temp-buffer "*Help*"
1065 (princ "MIME editor mode:\n")
1066 (princ (documentation 'mime-edit-mode))
1067 (print-help-return-message)))
1069 (defun mime-edit-insert-text (&optional subtype)
1070 "Insert a text message.
1071 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1072 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1074 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1076 (if (looking-at mime-edit-single-part-tag-regexp)
1078 ;; Make a space between the following message.
1082 (if (and (member (cadr ret) '("enriched"))
1083 (fboundp 'enriched-mode))
1085 (if (boundp 'enriched-mode)
1090 (defun mime-edit-insert-file (file &optional verbose)
1091 "Insert a message from a file."
1092 (interactive "fInsert file as MIME message: \nP")
1093 (let* ((guess (mime-find-file-type file))
1094 (type (nth 0 guess))
1095 (subtype (nth 1 guess))
1096 (parameters (nth 2 guess))
1097 (encoding (nth 3 guess))
1098 (disposition-type (nth 4 guess))
1099 (disposition-params (nth 5 guess))
1102 (setq type (mime-prompt-for-type type)
1103 subtype (mime-prompt-for-subtype type subtype)
1105 (if (or (interactive-p) verbose)
1106 (setq encoding (mime-prompt-for-encoding encoding))
1108 (if (or (consp parameters) (stringp disposition-type))
1109 (let ((rest parameters) cell attribute value)
1110 (setq parameters "")
1112 (setq cell (car rest))
1113 (setq attribute (car cell))
1114 (setq value (cdr cell))
1115 (if (eq value 'file)
1116 (setq value (std11-wrap-as-quoted-string
1117 (file-name-nondirectory file)))
1119 (setq parameters (concat parameters "; " attribute "=" value))
1120 (setq rest (cdr rest))
1122 (if disposition-type
1125 (concat parameters "\n"
1126 "Content-Disposition: " disposition-type))
1127 (setq rest disposition-params)
1129 (setq cell (car rest))
1130 (setq attribute (car cell))
1131 (setq value (cdr cell))
1132 (if (eq value 'file)
1133 (setq value (std11-wrap-as-quoted-string
1134 (file-name-nondirectory file)))
1137 (concat parameters "; " attribute "=" value))
1138 (setq rest (cdr rest))
1142 (mime-edit-insert-tag type subtype parameters)
1143 (mime-edit-insert-binary-file file encoding)
1146 (defun mime-edit-insert-external ()
1147 "Insert a reference to external body."
1149 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1151 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1153 (let* ((pritype (mime-prompt-for-type))
1154 (subtype (mime-prompt-for-subtype pritype))
1155 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1158 (insert "Content-Type: "
1159 pritype "/" subtype (or parameters "") "\n")))
1160 (if (and (not (eobp))
1161 (not (looking-at mime-edit-single-part-tag-regexp)))
1162 (insert (mime-make-text-tag) "\n")))
1164 (defun mime-edit-insert-voice ()
1165 "Insert a voice message."
1169 "What transfer encoding: "
1170 (mime-encoding-alist) nil t nil)))
1171 (mime-edit-insert-tag "audio" "basic" nil)
1172 (mime-edit-define-encoding encoding)
1174 (narrow-to-region (1- (point))(point))
1176 (funcall mime-edit-voice-recorder encoding)
1179 (invisible-region (point-min)(point-max))
1180 (goto-char (point-max))
1183 (defun mime-edit-insert-signature (&optional arg)
1184 "Insert a signature file."
1186 (let ((signature-insert-hook
1189 (let ((items (mime-find-file-type signature-file-name)))
1190 (apply (function mime-edit-insert-tag)
1191 (car items) (cadr items) (list (caddr items))))
1194 (insert-signature arg)
1198 ;; Insert a new tag around a point.
1200 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1201 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1202 If nothing is inserted, return nil."
1205 (mime-edit-goto-tag)
1206 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1207 (< (match-beginning 0) p)
1210 (goto-char (match-beginning 0))
1219 (mime-prompt-for-type)))
1222 (mime-prompt-for-subtype pritype)))
1225 (mime-prompt-for-parameters pritype subtype delimiter)))
1226 ;; Make a new MIME tag.
1227 (setq newtag (mime-make-tag pritype subtype parameters))
1228 ;; Find an current MIME tag.
1231 (if (mime-edit-goto-tag)
1232 (buffer-substring (match-beginning 0) (match-end 0))
1233 ;; Assume content type is 'text/plan'.
1234 (mime-make-tag "text" "plain")
1236 ;; We are only interested in TEXT.
1238 (not (mime-test-content-type
1239 (mime-edit-get-contype oldtag) "text")))
1242 (if (or (not oldtag) ;Not text
1243 (or mime-ignore-same-text-tag
1244 (not (string-equal oldtag newtag))))
1246 ;; Mark the beginning of the tag for convenience.
1247 (push-mark (point) 'nomsg)
1248 (insert newtag "\n")
1249 (list pritype subtype parameters) ;New tag is created.
1251 ;; Restore previous point.
1253 nil ;Nothing is created.
1257 (defun mime-edit-insert-binary-file (file &optional encoding)
1258 "Insert binary FILE at point.
1259 Optional argument ENCODING specifies an encoding method such as base64."
1260 (let* ((tagend (1- (point))) ;End of the tag
1261 (hide-p (and mime-auto-hide-body
1264 (let ((en (downcase encoding)))
1265 (or (string-equal en "7bit")
1266 (string-equal en "8bit")
1267 (string-equal en "binary")
1271 (narrow-to-region tagend (point))
1272 (mime-insert-encoded-file file encoding)
1275 (invisible-region (point-min) (point-max))
1276 (goto-char (point-max))
1278 (goto-char (point-max))
1281 (looking-at mime-edit-tag-regexp)
1282 (= (point)(point-max))
1283 (mime-edit-insert-tag "text" "plain")
1285 ;; Define encoding even if it is 7bit.
1286 (if (stringp encoding)
1288 (goto-char tagend) ; Make sure which line the tag is on.
1289 (mime-edit-define-encoding encoding)
1294 ;; Commands work on a current message flagment.
1296 (defun mime-edit-goto-tag ()
1297 "Search for the beginning of the tagged MIME message."
1298 (let ((current (point)))
1299 (if (looking-at mime-edit-tag-regexp)
1301 ;; At first, go to the end.
1302 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1303 (goto-char (1- (match-beginning 0))) ;For multiline tag
1306 (goto-char (point-max))
1308 ;; Then search for the beginning.
1309 (re-search-backward mime-edit-end-tag-regexp nil t)
1310 (or (looking-at mime-edit-beginning-tag-regexp)
1311 ;; Restore previous point.
1318 (defun mime-edit-content-beginning ()
1319 "Return the point of the beginning of content."
1321 (let ((beg (save-excursion
1322 (beginning-of-line) (point))))
1323 (if (mime-edit-goto-tag)
1324 (let ((top (point)))
1325 (goto-char (match-end 0))
1326 (if (and (= beg top)
1327 (= (following-char) ?\^M))
1331 ;; Default text/plain tag.
1332 (goto-char (point-min))
1334 (concat "\n" (regexp-quote mail-header-separator)
1335 (if mime-ignore-preceding-spaces
1336 "[ \t\n]*\n" "\n")) nil 'move)
1340 (defun mime-edit-content-end ()
1341 "Return the point of the end of content."
1343 (if (mime-edit-goto-tag)
1345 (goto-char (match-end 0))
1346 (if (invisible-p (point))
1347 (next-visible-point (point))
1348 ;; Move to the end of this text.
1349 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1350 ;; Don't forget a multiline tag.
1351 (goto-char (match-beginning 0))
1355 ;; Assume the message begins with text/plain.
1356 (goto-char (mime-edit-content-beginning))
1357 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1358 ;; Don't forget a multiline tag.
1359 (goto-char (match-beginning 0)))
1363 (defun mime-edit-define-charset (charset)
1364 "Set charset of current tag to CHARSET."
1366 (if (mime-edit-goto-tag)
1367 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1368 (delete-region (match-beginning 0) (match-end 0))
1371 (mime-edit-set-parameter
1372 (mime-edit-get-contype tag)
1373 "charset" (upcase (symbol-name charset)))
1374 (mime-edit-get-encoding tag)))
1377 (defun mime-edit-define-encoding (encoding)
1378 "Set encoding of current tag to ENCODING."
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))
1383 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1386 (defun mime-edit-choose-charset ()
1387 "Choose charset of a text following current point."
1388 (detect-mime-charset-region (point) (mime-edit-content-end))
1391 (defun mime-make-text-tag (&optional subtype)
1392 "Make a tag for a text after current point.
1393 Subtype of text type can be specified by an optional argument SUBTYPE.
1394 Otherwise, it is obtained from mime-content-types."
1395 (let* ((pritype "text")
1396 (subtype (or subtype
1397 (car (car (cdr (assoc pritype mime-content-types)))))))
1398 ;; Charset should be defined later.
1399 (mime-make-tag pritype subtype)))
1402 ;; Tag handling functions
1404 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1405 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1406 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1410 (defun mime-create-tag (contype &optional encoding)
1411 "Make a tag with CONTENT-TYPE and optional ENCODING."
1412 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1415 (defun mime-edit-get-contype (tag)
1416 "Return Content-Type (including parameters) of TAG."
1418 (or (string-match mime-edit-single-part-tag-regexp tag)
1419 (string-match mime-edit-multipart-beginning-regexp tag)
1420 (string-match mime-edit-multipart-end-regexp tag)
1422 (substring tag (match-beginning 1) (match-end 1))
1425 (defun mime-edit-get-encoding (tag)
1426 "Return encoding of TAG."
1428 (string-match mime-edit-single-part-tag-regexp tag)
1430 (not (= (match-beginning 3) (match-end 3)))
1431 (substring tag (match-beginning 3) (match-end 3))))
1433 (defun mime-get-parameter (contype parameter)
1434 "For given CONTYPE return value for PARAMETER.
1435 Nil if no such parameter."
1439 (regexp-quote parameter)
1440 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1442 (substring contype (match-beginning 1) (match-end 1))
1443 nil ;No such parameter
1446 (defun mime-edit-set-parameter (contype parameter value)
1447 "For given CONTYPE set PARAMETER to VALUE."
1448 (let (ctype opt-fields)
1449 (if (string-match "\n[^ \t\n\r]+:" contype)
1450 (setq ctype (substring contype 0 (match-beginning 0))
1451 opt-fields (substring contype (match-beginning 0)))
1452 (setq ctype contype)
1457 (regexp-quote parameter)
1458 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1461 (concat (substring ctype 0 (match-beginning 1))
1463 (substring contype (match-end 1))
1465 (concat ctype "; " parameter "=" value opt-fields)
1468 (defun mime-strip-parameters (contype)
1469 "Return primary content-type and subtype without parameters for CONTYPE."
1470 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1471 (substring contype (match-beginning 1) (match-end 1)) nil))
1473 (defun mime-test-content-type (contype type &optional subtype)
1474 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1475 (and (stringp contype)
1478 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1479 (downcase contype))))
1484 (defun mime-find-file-type (file)
1485 "Guess Content-Type, subtype, and parameters from FILE."
1487 (guesses mime-file-types))
1488 (while (and (not guess) guesses)
1489 (if (string-match (car (car guesses)) file)
1490 (setq guess (cdr (car guesses))))
1491 (setq guesses (cdr guesses)))
1495 (defun mime-prompt-for-type (&optional default)
1496 "Ask for Content-type."
1498 ;; Repeat until primary content type is specified.
1499 (while (string-equal type "")
1501 (completing-read "What content type: "
1504 'require-match ;Type must be specified.
1507 (if (string-equal type "")
1509 (message "Content type is required.")
1516 (defun mime-prompt-for-subtype (type &optional default)
1517 "Ask for subtype of media-type TYPE."
1518 (let ((subtypes (cdr (assoc type mime-content-types))))
1520 (assoc default subtypes))
1521 (setq default (car (car subtypes)))
1527 "What content subtype: (default " default ") ")
1528 "What content subtype: ")
1529 (cdr (assoc type mime-content-types))
1531 'require-match ;Subtype must be specified.
1534 (if (string-equal answer "") default answer)))
1536 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1537 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1538 Optional DELIMITER specifies parameter delimiter (';' by default)."
1539 (let* ((delimiter (or delimiter "; "))
1544 (mime-prompt-for-parameters-1
1546 (cdr (assoc pritype mime-content-types))))))
1549 (if (and (stringp parameters)
1550 (not (string-equal parameters "")))
1551 (concat delimiter parameters)
1552 "" ;"" if no parameters
1555 (defun mime-prompt-for-parameters-1 (optlist)
1556 (apply (function append)
1557 (mapcar (function mime-prompt-for-parameter) optlist)))
1559 (defun mime-prompt-for-parameter (parameter)
1561 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1562 (let* ((prompt (car parameter))
1563 (choices (mapcar (function
1565 (if (consp e) e (list e))))
1567 (default (car (car choices)))
1573 (concat "What " prompt
1575 (if (string-equal default "") "\"\"" default)
1577 choices nil nil ""))
1578 ;; If nothing is selected, use default.
1579 (if (string-equal answer "")
1580 (setq answer default)))
1582 (read-string (concat "What " prompt ": "))))
1583 (cons (if (and answer
1584 (not (string-equal answer "")))
1586 ;; Note: control characters ignored!
1587 (if (string-match mime-tspecials-regexp answer)
1588 (concat "\"" answer "\"") answer)))
1589 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1592 (defun mime-prompt-for-encoding (default)
1593 "Ask for Content-Transfer-Encoding."
1598 "What transfer encoding: "
1599 (mime-encoding-alist) nil t default)
1605 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1608 (defvar mime-edit-translate-buffer-hook
1609 '(mime-edit-pgp-enclose-buffer
1610 mime-edit-translate-body
1611 mime-edit-translate-header))
1613 (defun mime-edit-translate-header ()
1614 "Encode the message header into network representation."
1615 (eword-encode-header 'code-conversion)
1616 (run-hooks 'mime-edit-translate-header-hook)
1619 (defun mime-edit-translate-buffer ()
1620 "Encode the tagged MIME message in current buffer in MIME compliant message."
1622 (if (catch 'mime-edit-error
1624 (run-hooks 'mime-edit-translate-buffer-hook)
1628 (error "Translation error!")
1631 (defun mime-edit-find-inmost ()
1632 (goto-char (point-min))
1633 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1634 (let ((bb (match-beginning 0))
1636 (type (buffer-substring (match-beginning 1)(match-end 1)))
1638 (setq end-exp (format "--}-<<%s>>\n" type))
1640 (if (re-search-forward end-exp nil t)
1641 (setq eb (match-beginning 0))
1642 (setq eb (point-max))
1644 (narrow-to-region be eb)
1646 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1648 (narrow-to-region (match-beginning 0)(point-max))
1649 (mime-edit-find-inmost)
1652 (list type bb be eb)
1655 (defun mime-edit-process-multipart-1 (boundary)
1656 (let ((ret (mime-edit-find-inmost)))
1658 (let ((type (car ret))
1659 (bb (nth 1 ret))(be (nth 2 ret))
1662 (narrow-to-region bb eb)
1663 (delete-region bb be)
1664 (setq bb (point-min))
1665 (setq eb (point-max))
1668 (if (looking-at mime-edit-multipart-end-regexp)
1669 (let ((beg (match-beginning 0))
1672 (delete-region beg end)
1673 (or (looking-at mime-edit-beginning-tag-regexp)
1675 (insert (concat (mime-make-text-tag) "\n"))
1677 (cond ((string-equal type "quote")
1678 (mime-edit-enquote-region bb eb)
1680 ((string-equal type "pgp-signed")
1681 (mime-edit-sign-pgp-mime bb eb boundary)
1683 ((string-equal type "pgp-encrypted")
1684 (mime-edit-encrypt-pgp-mime bb eb boundary)
1686 ((string-equal type "kazu-signed")
1687 (mime-edit-sign-pgp-kazu bb eb boundary)
1689 ((string-equal type "kazu-encrypted")
1690 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1694 (nth 2 (mime-edit-translate-region bb eb
1698 (format "--[[multipart/%s;
1699 boundary=\"%s\"][7bit]]\n"
1704 (defun mime-edit-enquote-region (beg end)
1707 (narrow-to-region beg end)
1709 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1710 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1711 (replace-match (concat "- " (substring tag 1)))
1714 (defun mime-edit-dequote-region (beg end)
1717 (narrow-to-region beg end)
1719 (while (re-search-forward
1720 mime-edit-quoted-single-part-tag-regexp nil t)
1721 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1722 (replace-match (concat "-" (substring tag 2)))
1725 (defun mime-edit-sign-pgp-mime (beg end boundary)
1728 (narrow-to-region beg end)
1730 (mime-edit-translate-region beg end boundary))
1732 (encoding (nth 1 ret))
1733 (pgp-boundary (concat "pgp-sign-" boundary)))
1735 (insert (format "Content-Type: %s\n" ctype))
1737 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1740 (or (as-binary-process
1741 (funcall (pgp-function 'mime-sign)
1742 (point-min)(point-max) nil nil pgp-boundary))
1743 (throw 'mime-edit-error 'pgp-error)
1747 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1749 (defun mime-edit-make-encrypt-recipient-header ()
1750 (let* ((names mime-edit-encrypt-recipient-fields-list)
1752 (std11-field-bodies (cons "From" names)
1753 nil mail-header-separator))
1756 (setq values (cdr values))))
1757 (header (and (stringp from)
1758 (if (string-equal from "")
1760 (format "From: %s\n" from)
1763 (while (and names values)
1764 (let ((name (car names))
1765 (value (car values))
1767 (and (stringp value)
1768 (or (string-equal value "")
1770 (setq header (concat header name ": " value "\n")
1771 recipients (if recipients
1772 (concat recipients " ," value)
1775 (setq names (cdr names)
1776 values (cdr values))
1778 (vector from recipients header)
1781 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1784 (let (from recipients header)
1785 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1786 (setq from (aref ret 0)
1787 recipients (aref ret 1)
1788 header (aref ret 2))
1790 (narrow-to-region beg end)
1792 (mime-edit-translate-region beg end boundary))
1794 (encoding (nth 1 ret))
1795 (pgp-boundary (concat "pgp-" boundary)))
1798 (insert (format "Content-Type: %s\n" ctype))
1800 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1803 (or (funcall (pgp-function 'encrypt)
1804 recipients (point-min) (point-max) from)
1805 (throw 'mime-edit-error 'pgp-error)
1808 (insert (format "--[[multipart/encrypted;
1810 protocol=\"application/pgp-encrypted\"][7bit]]
1812 Content-Type: application/pgp-encrypted
1815 Content-Type: application/octet-stream
1816 Content-Transfer-Encoding: 7bit
1818 " pgp-boundary pgp-boundary pgp-boundary))
1819 (goto-char (point-max))
1820 (insert (format "\n--%s--\n" pgp-boundary))
1823 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1826 (narrow-to-region beg end)
1828 (mime-edit-translate-region beg end boundary))
1830 (encoding (nth 1 ret)))
1832 (insert (format "Content-Type: %s\n" ctype))
1834 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1837 (or (as-binary-process
1838 (funcall (pgp-function 'traditional-sign)
1840 (throw 'mime-edit-error 'pgp-error)
1844 "--[[application/pgp; format=mime][7bit]]\n")
1848 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1850 (let (recipients header)
1851 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1852 (setq recipients (aref ret 1)
1853 header (aref ret 2))
1856 (narrow-to-region beg end)
1858 (mime-edit-translate-region beg end boundary))
1860 (encoding (nth 1 ret)))
1863 (insert (format "Content-Type: %s\n" ctype))
1865 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1868 (or (as-binary-process
1869 (funcall (pgp-function 'encrypt)
1870 recipients beg (point-max) nil 'maybe)
1872 (throw 'mime-edit-error 'pgp-error)
1876 "--[[application/pgp; format=mime][7bit]]\n")
1880 (defsubst replace-space-with-underline (str)
1881 (mapconcat (function
1889 (defun mime-edit-make-boundary ()
1890 (concat mime-multipart-boundary "_"
1891 (replace-space-with-underline (current-time-string))
1894 (defun mime-edit-translate-body ()
1895 "Encode the tagged MIME body in current buffer in MIME compliant message."
1898 (let ((boundary (mime-edit-make-boundary))
1901 (while (mime-edit-process-multipart-1
1902 (format "%s-%d" boundary i))
1906 ;; We are interested in message body.
1909 (goto-char (point-min))
1911 (concat "\n" (regexp-quote mail-header-separator)
1912 (if mime-ignore-preceding-spaces
1913 "[ \t\n]*\n" "\n")) nil 'move)
1917 (goto-char (point-max))
1918 (and mime-ignore-trailing-spaces
1919 (re-search-backward "[^ \t\n]\n" beg t)
1922 (setq ret (mime-edit-translate-region
1924 (format "%s-%d" boundary i)))
1926 (mime-edit-dequote-region (point-min)(point-max))
1927 (let ((contype (car ret)) ;Content-Type
1928 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1930 ;; Insert User-Agent field
1931 (and mime-edit-insert-user-agent-field
1932 (or (mail-position-on-field "User-Agent")
1933 (insert mime-edit-user-agent-value)
1935 ;; Make primary MIME headers.
1936 (or (mail-position-on-field "MIME-Version")
1937 (insert mime-edit-mime-version-value))
1938 ;; Remove old Content-Type and other fields.
1940 (goto-char (point-min))
1941 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1942 (narrow-to-region (point-min) (point))
1943 (goto-char (point-min))
1944 (mime-delete-field "Content-Type")
1945 (mime-delete-field "Content-Transfer-Encoding"))
1946 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1947 (mail-position-on-field "Content-Type")
1951 (mail-position-on-field "Content-Transfer-Encoding")
1955 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
1956 "Translate single-part-tag to MIME header."
1957 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1958 (let* ((beg (match-beginning 0))
1960 (tag (buffer-substring beg end)))
1961 (delete-region beg end)
1962 (let ((contype (mime-edit-get-contype tag))
1963 (encoding (mime-edit-get-encoding tag)))
1964 (insert (concat prefix "--" boundary "\n"))
1966 (narrow-to-region (point)(point))
1967 (insert "Content-Type: " contype "\n")
1969 (insert "Content-Transfer-Encoding: " encoding "\n"))
1970 (eword-encode-header)
1975 (downcase encoding))))
1978 (defun mime-edit-translate-region (beg end &optional boundary multipart)
1980 (setq boundary (mime-edit-make-boundary))
1984 (narrow-to-region beg end)
1985 (let ((tag nil) ;MIME tag
1986 (contype nil) ;Content-Type
1987 (encoding nil) ;Content-Transfer-Encoding
1988 (nparts 0)) ;Number of body parts
1989 ;; Normalize the body part by inserting appropriate message
1990 ;; tags for every message contents.
1991 (mime-edit-normalize-body)
1992 ;; Counting the number of Content-Type.
1993 (goto-char (point-min))
1994 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1995 (setq nparts (1+ nparts)))
1996 ;; Begin translation.
1998 ((and (<= nparts 1)(not multipart))
1999 ;; It's a singular message.
2000 (goto-char (point-min))
2001 (while (re-search-forward
2002 mime-edit-single-part-tag-regexp nil t)
2004 (buffer-substring (match-beginning 0) (match-end 0)))
2005 (delete-region (match-beginning 0) (1+ (match-end 0)))
2006 (setq contype (mime-edit-get-contype tag))
2007 (setq encoding (mime-edit-get-encoding tag))
2010 ;; It's a multipart message.
2011 (goto-char (point-min))
2012 (let ((prio mime-content-transfer-encoding-priority-list)
2014 (when (setq part-info
2015 (mime-edit-translate-single-part-tag boundary))
2016 (and (setq nprio (member (cdr part-info) prio))
2018 (while (setq part-info
2019 (mime-edit-translate-single-part-tag boundary "\n"))
2020 (and (setq nprio (member (cdr part-info) prio))
2021 (setq prio nprio))))
2022 ;; Define Content-Type as "multipart/mixed".
2024 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2025 (setq encoding (car prio))
2026 ;; Insert the trailer.
2027 (goto-char (point-max))
2028 (insert "\n--" boundary "--\n")
2030 (list contype encoding boundary nparts)
2033 (defun mime-edit-normalize-body ()
2034 "Normalize the body part by inserting appropriate message tags."
2035 ;; Insert the first MIME tags if necessary.
2036 (goto-char (point-min))
2037 (if (not (looking-at mime-edit-single-part-tag-regexp))
2038 (insert (mime-make-text-tag) "\n"))
2039 ;; Check each tag, and add new tag or correct it if necessary.
2040 (goto-char (point-min))
2041 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2042 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2043 (contype (mime-edit-get-contype tag))
2044 (charset (mime-get-parameter contype "charset"))
2045 (encoding (mime-edit-get-encoding tag)))
2046 ;; Remove extra whitespaces after the tag.
2047 (if (looking-at "[ \t]+$")
2048 (delete-region (match-beginning 0) (match-end 0)))
2050 (end (mime-edit-content-end))
2052 (if (= end (point-max))
2055 (or (looking-at mime-edit-beginning-tag-regexp)
2057 (insert (mime-make-text-tag) "\n")
2059 (visible-region beg end)
2063 ((mime-test-content-type contype "message")
2064 ;; Content-type "message" should be sent as is.
2067 ((mime-test-content-type contype "text")
2068 ;; Define charset for text if necessary.
2069 (setq charset (if charset
2070 (intern (downcase charset))
2071 (mime-edit-choose-charset)))
2072 (mime-edit-define-charset charset)
2073 (cond ((string-equal contype "text/x-rot13-47-48")
2076 (mule-caesar-region (point) (mime-edit-content-end))
2078 ((string-equal contype "text/enriched")
2083 (end (mime-edit-content-end))
2085 ;; Patch for hard newlines
2088 ;; (while (search-forward "\n" end t)
2089 ;; (put-text-property (match-beginning 0)
2092 ;; End patch for hard newlines
2093 (enriched-encode beg end)
2095 (if (search-forward "\n\n")
2096 (delete-region beg (match-end 0))
2099 ;; Point is now on current tag.
2100 ;; Define encoding and encode text if necessary.
2101 (or encoding ;Encoding is not specified.
2104 (let ((ret (cdr (assq charset mime-charset-type-list))))
2106 (setq bits (car ret)
2109 conv "quoted-printable")))
2110 (if (<= bits mime-transfer-level)
2111 (mime-encoding-name bits)
2113 (beg (mime-edit-content-beginning)))
2114 (encode-mime-charset-region beg (mime-edit-content-end)
2116 ;; Protect "From " in beginning of line
2118 (narrow-to-region beg (mime-edit-content-end))
2120 (let (case-fold-search)
2121 (if (re-search-forward "^From " nil t)
2123 (if (memq charset '(iso-2022-jp
2128 (replace-match "\e(BFrom ")
2129 (re-search-forward "^From " nil t)
2131 (setq encoding "quoted-printable")
2133 ;; canonicalize line break code
2134 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2136 (narrow-to-region beg (mime-edit-content-end))
2138 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2139 (replace-match "\\1\r\n"))))
2141 (mime-encode-region beg (mime-edit-content-end)
2142 (or encoding "7bit"))
2143 (mime-edit-define-encoding encoding)
2145 (goto-char (mime-edit-content-end))
2147 ((null encoding) ;Encoding is not specified.
2148 ;; Application, image, audio, video, and any other
2149 ;; unknown content-type without encoding should be
2151 (let* ((encoding "base64") ;Encode in BASE64 by default.
2152 (beg (mime-edit-content-beginning))
2153 (end (mime-edit-content-end)))
2154 (mime-encode-region beg end encoding)
2155 (mime-edit-define-encoding encoding))
2160 (defun mime-delete-field (field)
2161 "Delete header FIELD."
2162 (let ((regexp (format "^%s:[ \t]*" field)))
2163 (goto-char (point-min))
2164 (while (re-search-forward regexp nil t)
2165 (delete-region (match-beginning 0)
2166 (progn (forward-line 1) (point)))
2171 ;;; Platform dependent functions
2174 ;; Sun implementations
2176 (defun mime-edit-voice-recorder-for-sun (encoding)
2177 "Record voice in a buffer using Sun audio device,
2178 and insert data encoded as ENCODING."
2179 (message "Start the recording on %s. Type C-g to finish the recording..."
2181 (mime-insert-encoded-file "/dev/audio" encoding)
2185 ;;; @ Other useful commands.
2188 ;; Message forwarding commands as content-type "message/rfc822".
2190 (defun mime-edit-insert-message (&optional message)
2192 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2193 (if (and inserter (fboundp inserter))
2195 (mime-edit-insert-tag "message" "rfc822")
2196 (funcall inserter message)
2198 (message "Sorry, I don't have message inserter for your MUA.")
2201 (defun mime-edit-insert-mail (&optional message)
2203 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2204 (if (and inserter (fboundp inserter))
2206 (mime-edit-insert-tag "message" "rfc822")
2207 (funcall inserter message)
2209 (message "Sorry, I don't have mail inserter for your MUA.")
2212 (defun mime-edit-inserted-message-filter ()
2215 (let ((header-start (point))
2216 (case-fold-search t)
2219 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2220 (if (re-search-forward "^$" (mark t))
2221 (narrow-to-region header-start (match-beginning 0))
2223 (goto-char header-start)
2224 (while (and (re-search-forward
2225 mime-edit-yank-ignored-field-regexp nil t)
2226 (setq beg (match-beginning 0))
2227 (setq end (1+ (std11-field-end)))
2229 (delete-region beg end)
2234 ;;; @ multipart enclosure
2237 (defun mime-edit-enclose-region-internal (type beg end)
2241 (narrow-to-region beg end)
2242 (insert (format "--<<%s>>-{\n" type))
2243 (goto-char (point-max))
2244 (insert (format "--}-<<%s>>\n" type))
2245 (goto-char (point-max))
2247 (or (looking-at mime-edit-beginning-tag-regexp)
2249 (insert (mime-make-text-tag) "\n")
2253 (defun mime-edit-enclose-quote-region (beg end)
2255 (mime-edit-enclose-region-internal 'quote beg end)
2258 (defun mime-edit-enclose-mixed-region (beg end)
2260 (mime-edit-enclose-region-internal 'mixed beg end)
2263 (defun mime-edit-enclose-parallel-region (beg end)
2265 (mime-edit-enclose-region-internal 'parallel beg end)
2268 (defun mime-edit-enclose-digest-region (beg end)
2270 (mime-edit-enclose-region-internal 'digest beg end)
2273 (defun mime-edit-enclose-alternative-region (beg end)
2275 (mime-edit-enclose-region-internal 'alternative beg end)
2278 (defun mime-edit-enclose-pgp-signed-region (beg end)
2280 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2283 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2285 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2288 (defun mime-edit-enclose-kazu-signed-region (beg end)
2290 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2293 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2295 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2298 (defun mime-edit-insert-key (&optional arg)
2299 "Insert a pgp public key."
2301 (mime-edit-insert-tag "application" "pgp-keys")
2302 (mime-edit-define-encoding "7bit")
2303 (funcall (pgp-function 'insert-key))
2310 (defun mime-edit-set-split (arg)
2313 (y-or-n-p "Do you want to enable split? ")
2315 (setq mime-edit-split-message arg)
2317 (message "This message is enabled to split.")
2318 (message "This message is not enabled to split.")
2321 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2322 "Toggle transfer-level is 7bit or 8bit through.
2324 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2326 (if (numberp transfer-level)
2327 (setq mime-transfer-level transfer-level)
2328 (if (< mime-transfer-level 8)
2329 (setq mime-transfer-level 8)
2330 (setq mime-transfer-level 7)
2332 (message (format "Current transfer-level is %d bit"
2333 mime-transfer-level))
2334 (setq mime-transfer-level-string
2335 (mime-encoding-name mime-transfer-level 'not-omit))
2336 (force-mode-line-update)
2339 (defun mime-edit-set-transfer-level-7bit ()
2341 (mime-edit-toggle-transfer-level 7)
2344 (defun mime-edit-set-transfer-level-8bit ()
2346 (mime-edit-toggle-transfer-level 8)
2353 (defvar mime-edit-pgp-processing nil)
2354 (make-variable-buffer-local 'mime-edit-pgp-processing)
2356 (defun mime-edit-set-sign (arg)
2359 (y-or-n-p "Do you want to sign? ")
2363 (setq mime-edit-pgp-processing 'sign)
2364 (message "This message will be signed.")
2366 (if (eq mime-edit-pgp-processing 'sign)
2367 (setq mime-edit-pgp-processing nil)
2369 (message "This message will not be signed.")
2372 (defun mime-edit-set-encrypt (arg)
2375 (y-or-n-p "Do you want to encrypt? ")
2379 (setq mime-edit-pgp-processing 'encrypt)
2380 (message "This message will be encrypt.")
2382 (if (eq mime-edit-pgp-processing 'encrypt)
2383 (setq mime-edit-pgp-processing nil)
2385 (message "This message will not be encrypt.")
2388 (defun mime-edit-pgp-enclose-buffer ()
2389 (let ((beg (save-excursion
2390 (goto-char (point-min))
2391 (if (search-forward (concat "\n" mail-header-separator "\n"))
2397 (cond ((eq mime-edit-pgp-processing 'sign)
2398 (mime-edit-enclose-pgp-signed-region beg end)
2400 ((eq mime-edit-pgp-processing 'encrypt)
2401 (mime-edit-enclose-pgp-encrypted-region beg end)
2409 (defun mime-edit-insert-partial-header (fields subject
2410 id number total separator)
2412 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2413 (insert mime-edit-mime-version-field-for-message/partial)
2415 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2416 id number total separator))
2419 (defun mime-edit-split-and-send
2420 (&optional cmd lines mime-edit-message-max-length)
2424 (count-lines (point-min) (point-max)))
2426 (or mime-edit-message-max-length
2427 (setq mime-edit-message-max-length
2428 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2429 mime-edit-message-default-max-lines))
2431 (let* ((mime-edit-draft-file-name
2432 (or (buffer-file-name)
2434 (expand-file-name "mime-draft" temporary-file-directory))))
2435 (separator mail-header-separator)
2437 (replace-space-with-underline (current-time-string))
2438 "@" (system-name) "\"")))
2439 (run-hooks 'mime-edit-before-split-hook)
2440 (let ((the-buf (current-buffer))
2441 (copy-buf (get-buffer-create " *Original Message*"))
2442 (header (std11-header-string-except
2443 mime-edit-split-ignored-field-regexp separator))
2444 (subject (mail-fetch-field "subject"))
2445 (total (+ (/ lines mime-edit-message-max-length)
2446 (if (> (mod lines mime-edit-message-max-length) 0)
2452 mime-edit-split-message-sender-alist))
2456 (error "Split sender is not specified for `%s'." major-mode)
2459 (mime-edit-partial-number 1)
2462 (set-buffer copy-buf)
2464 (insert-buffer the-buf)
2466 (if (re-search-forward
2467 (concat "^" (regexp-quote separator) "$") nil t)
2468 (let ((he (match-beginning 0)))
2470 (narrow-to-region (point-min) he)
2472 (goto-char (point-min))
2473 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2474 (delete-region (match-beginning 0)
2475 (1+ (std11-field-end)))
2477 (while (< mime-edit-partial-number total)
2480 (set-buffer copy-buf)
2481 (setq data (buffer-substring
2484 (goto-line mime-edit-message-max-length)
2487 (delete-region (point-min)(point))
2489 (mime-edit-insert-partial-header
2490 header subject id mime-edit-partial-number total separator)
2493 (message (format "Sending %d/%d..."
2494 mime-edit-partial-number total))
2495 (call-interactively command)
2496 (message (format "Sending %d/%d... done"
2497 mime-edit-partial-number total))
2499 (setq mime-edit-partial-number
2500 (1+ mime-edit-partial-number))
2504 (set-buffer copy-buf)
2505 (setq data (buffer-string))
2508 (mime-edit-insert-partial-header
2509 header subject id mime-edit-partial-number total separator)
2512 (message (format "Sending %d/%d..."
2513 mime-edit-partial-number total))
2514 (message (format "Sending %d/%d... done"
2515 mime-edit-partial-number total))
2519 (defun mime-edit-maybe-split-and-send (&optional cmd)
2521 (run-hooks 'mime-edit-before-send-hook)
2522 (let ((mime-edit-message-max-length
2523 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2524 mime-edit-message-default-max-lines))
2525 (lines (count-lines (point-min) (point-max)))
2527 (if (and (> lines mime-edit-message-max-length)
2528 mime-edit-split-message)
2529 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2533 ;;; @ preview message
2536 (defvar mime-edit-buffer nil) ; buffer local variable
2538 (defun mime-edit-preview-message ()
2539 "preview editing MIME message."
2541 (let* ((str (buffer-string))
2542 (separator mail-header-separator)
2543 (the-buf (current-buffer))
2544 (buf-name (buffer-name))
2545 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2546 (buf (get-buffer temp-buf-name))
2550 (switch-to-buffer buf)
2553 (setq buf (get-buffer-create temp-buf-name))
2554 (switch-to-buffer buf)
2557 (setq major-mode 'mime-temp-message-mode)
2558 (make-local-variable 'mail-header-separator)
2559 (setq mail-header-separator separator)
2560 (make-local-variable 'mime-edit-buffer)
2561 (setq mime-edit-buffer the-buf)
2563 (run-hooks 'mime-edit-translate-hook)
2564 (mime-edit-translate-buffer)
2565 (goto-char (point-min))
2566 (if (re-search-forward
2567 (concat "^" (regexp-quote separator) "$"))
2573 (defun mime-edit-quitting-method ()
2574 "Quitting method for mime-view."
2575 (let ((temp mime-raw-buffer)
2577 (mime-preview-kill-buffer)
2579 (setq buf mime-edit-buffer)
2581 (switch-to-buffer buf)
2584 (set-alist 'mime-preview-quitting-method-alist
2585 'mime-temp-message-mode
2586 #'mime-edit-quitting-method)
2592 (defvar mime-edit-again-ignored-field-regexp
2593 (concat "^\\(" "Content-.*\\|Mime-Version"
2594 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2596 "Regexp for deleted header fields when `mime-edit-again' is called.")
2598 (defsubst eliminate-top-spaces (string)
2599 "Eliminate top sequence of space or tab in STRING."
2600 (if (string-match "^[ \t]+" string)
2601 (substring string (match-end 0))
2604 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2605 (let* ((subtype (mime-content-type-subtype content-type))
2606 (boundary (mime-content-type-parameter content-type "boundary"))
2607 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2608 (re-search-forward boundary-pat nil t)
2609 (let ((bb (match-beginning 0)) eb tag)
2610 (setq tag (format "\n--<<%s>>-{\n" subtype))
2613 (setq bb (+ bb (length tag)))
2615 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2617 (setq eb (match-beginning 0))
2618 (replace-match (format "--}-<<%s>>\n" subtype))
2620 (narrow-to-region bb eb)
2621 (goto-char (point-min))
2622 (while (re-search-forward boundary-pat nil t)
2623 (let ((beg (match-beginning 0))
2625 (delete-region beg (match-end 0))
2627 (if (re-search-forward boundary-pat nil t)
2628 (setq end (match-beginning 0))
2629 (setq end (point-max))
2632 (narrow-to-region beg end)
2633 (mime-edit-decode-message-in-buffer
2634 (if (eq subtype 'digest)
2636 (make-mime-content-type 'message 'rfc822))
2639 (goto-char (point-max))
2642 (goto-char (point-min))
2643 (or (= (point-min) 1)
2644 (delete-region (point-min)
2645 (if (search-forward "\n\n" nil t)
2651 (defun mime-edit-decode-single-part-in-buffer (content-type not-decode-text)
2652 (let* ((type (mime-content-type-primary-type content-type))
2653 (subtype (mime-content-type-subtype content-type))
2654 (ctype (format "%s/%s" type subtype))
2656 (pstr (let ((bytes (+ 14 (length ctype))))
2657 (mapconcat (function
2659 (if (string= (car attr) "charset")
2661 (setq charset (cdr attr))
2663 (let* ((str (concat (car attr)
2666 (setq bytes (+ bytes bs 2))
2669 (setq bytes (+ bs 1))
2673 (mime-content-type-parameters content-type) "")))
2676 (limit (save-excursion
2677 (if (search-forward "\n\n" nil t)
2680 (if (re-search-forward
2681 "^Content-Transfer-Encoding:" limit t)
2682 (let ((beg (match-beginning 0))
2683 (hbeg (match-end 0))
2684 (end (std11-field-end)))
2687 (eliminate-top-spaces
2688 (std11-unfold-string
2689 (buffer-substring hbeg end)))))
2690 (if (or charset (eq type 'text))
2692 (delete-region beg (1+ end))
2693 (goto-char (point-min))
2694 (if (search-forward "\n\n" nil t)
2697 (match-end 0)(point-max) encoding)
2701 (if (or encoded (not not-decode-text))
2702 (decode-mime-charset-region (point-min)(point-max)
2703 (or charset default-mime-charset))
2705 (let ((he (if (re-search-forward "^$" nil t)
2709 (if (and (eq type 'text)
2710 (eq subtype 'x-rot13-47-48))
2711 (mule-caesar-region he (point-max))
2713 (if (= (point-min) 1)
2719 (format "%s/%s%s" type subtype pstr)
2722 (delete-region (point-min) he)
2724 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2730 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2733 (goto-char (point-min))
2734 (let ((ctl (or (mime-read-Content-Type)
2735 default-content-type)))
2737 (let ((type (mime-content-type-primary-type ctl)))
2739 ((and (eq type 'application)
2740 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2741 (delete-region (point-min)(point-max))
2743 ((eq type 'multipart)
2744 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
2747 (mime-edit-decode-single-part-in-buffer ctl not-decode-text)
2750 (decode-mime-charset-region (point-min) (point-max)
2751 default-mime-charset))
2754 (std11-narrow-to-header)
2755 (goto-char (point-min))
2756 (while (re-search-forward mime-edit-again-ignored-field-regexp nil t)
2757 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2759 (mime-decode-header-in-buffer (not not-decode-text))
2763 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2764 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2765 Content-Type and Content-Transfer-Encoding header fields will be
2766 converted to MIME-Edit tags."
2768 (goto-char (point-min))
2770 (concat "\n" (regexp-quote mail-header-separator) "\n")
2772 (replace-match "\n\n")
2774 (mime-edit-decode-message-in-buffer nil not-decode-text)
2775 (goto-char (point-min))
2777 (and (re-search-forward "^$")
2778 (replace-match mail-header-separator)
2788 (provide 'mime-edit)
2790 (run-hooks 'mime-edit-load-hook)
2792 ;;; mime-edit.el ends here