1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,94,95,96,97,98,99 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Created: 1994/08/21 renamed from mime.el
8 ;; Renamed: 1997/2/21 from tm-edit.el
9 ;; Keywords: MIME, multimedia, multilingual, mail, news
11 ;; This file is part of SEMI (Sophisticated Emacs MIME Interfaces).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; This is an Emacs minor mode for editing Internet multimedia
31 ;; messages formatted in MIME (RFC 2045, 2046, 2047, 2048 and 2049).
32 ;; All messages in this mode are composed in the tagged MIME format,
33 ;; that are described in the following examples. The messages
34 ;; composed in the tagged MIME format are automatically translated
35 ;; into a MIME compliant message when exiting the mode.
37 ;; Mule (multilingual feature of Emacs 20 and multilingual extension
38 ;; for XEmacs 20) has a capability of handling multilingual text in
39 ;; limited ISO-2022 manner that is based on early experiences in
40 ;; Japanese Internet community and resulted in RFC 1468 (ISO-2022-JP
41 ;; charset for MIME). In order to enable multilingual capability in
42 ;; single text message in MIME, charset of multilingual text written
43 ;; in Mule is declared as either `ISO-2022-JP-2' [RFC 1554]. Mule is
44 ;; required for reading the such messages.
46 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
47 ;; News mode. First of all, you need the following autoload
48 ;; definition to load mime-edit-mode automatically:
50 ;; (autoload 'turn-on-mime-edit "mime-edit"
51 ;; "Minor mode for editing MIME message." t)
53 ;; In case of Mail mode (includes VM mode), you need the following
56 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
57 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
59 ;; In case of MH-E, you need the following hook definition:
61 ;; (add-hook 'mh-letter-mode-hook
64 ;; (turn-on-mime-edit)
65 ;; (make-local-variable 'mail-header-separator)
66 ;; (setq mail-header-separator "--------")
68 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
70 ;; In case of News mode, you need the following hook definition:
72 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
73 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
75 ;; In case of Emacs 19, it is possible to emphasize the message tags
76 ;; using font-lock mode as follows:
78 ;; (add-hook 'mime-edit-mode-hook
82 ;; (setq font-lock-keywords (list mime-edit-tag-regexp))
85 ;; The message tag looks like:
87 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
89 ;; The tagged MIME message examples:
91 ;; This is a conventional plain text. It should be translated into
95 ;; This is also a plain text. But, it is explicitly specified as is.
96 ;;--[[text/plain; charset=ISO-8859-1]]
97 ;; This is also a plain text. But charset is specified as iso-8859-1.
99 ;; ¡Hola! Buenos días. ¿Cómo está usted?
100 ;;--[[text/enriched]]
101 ;; <center>This is a richtext.</center>
103 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
105 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
110 (require 'mail-utils)
112 (require 'eword-encode) ; eword-encode-field-body
120 (autoload 'pgg-encrypt-region "pgg"
121 "PGP encryption of current region." t)
122 (autoload 'pgg-sign-region "pgg"
123 "PGP signature of current region." t)
124 (autoload 'pgg-insert-key "pgg"
125 "Insert PGP public key at point." t)
132 (defconst mime-edit-version
134 (mime-product-name mime-user-interface-product) " "
135 (mapconcat #'number-to-string
136 (mime-product-version mime-user-interface-product) ".")
137 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
143 (defgroup mime-edit nil
147 (defcustom mime-ignore-preceding-spaces nil
148 "*Ignore preceding white spaces if non-nil."
152 (defcustom mime-ignore-trailing-spaces nil
153 "*Ignore trailing white spaces if non-nil."
157 (defcustom mime-ignore-same-text-tag t
158 "*Ignore preceding text content-type tag that is same with new one.
159 If non-nil, the text tag is not inserted unless something different."
163 (defcustom mime-auto-hide-body t
164 "*Hide non-textual body encoded in base64 after insertion if non-nil."
168 (defcustom mime-edit-voice-recorder
169 (function mime-edit-voice-recorder-for-sun)
170 "*Function to record a voice message and encode it."
174 (defcustom mime-edit-mode-hook nil
175 "*Hook called when enter MIME mode."
179 (defcustom mime-edit-translate-hook nil
180 "*Hook called before translating into a MIME compliant message.
181 To insert a signature file automatically, call the function
182 `mime-edit-insert-signature' from this hook."
186 (defcustom mime-edit-exit-hook nil
187 "*Hook called when exit MIME mode."
191 (defvar mime-content-types
193 ;; Charset parameter need not to be specified, since it is
194 ;; defined automatically while translation.
196 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
209 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
210 ("directory" "/pub/GNU/elisp/mime")
212 ("mode" "image" "ascii" "local8"))
217 ("mode" "image" "ascii" "local8"))
218 ("tftp" ("site") ("name"))
219 ("afs" ("site") ("name"))
220 ("local-file" ("site") ("name"))
222 ("server" "ftpmail@nic.karrn.ad.jp")
230 ("octet-stream" ("type" "" "tar" "shar"))
232 ("vnd.ms-powerpoint")
233 ("x-kiss" ("x-cnf")))
247 "*Alist of content-type, subtype, parameters and its values.")
249 (defcustom mime-file-types
252 ;; Programming languages
255 "application" "octet-stream" (("type" . "C++"))
257 "attachment" (("filename" . file))
261 "application" "octet-stream" (("type" . "emacs-lisp"))
263 "attachment" (("filename" . file))
267 "application" "octet-stream" (("type" . "common-lisp"))
269 "attachment" (("filename" . file))
273 "application" "octet-stream" (("type" . "perl"))
275 "attachment" (("filename" . file))
278 ;; Text or translated text
283 "inline" (("filename" . file))
286 ;; .rc : procmail modules pm-xxxx.rc
287 ;; *rc : other resource files
289 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
292 "attachment" (("filename" . file))
300 ("\\.diff$\\|\\.patch$"
301 "application" "octet-stream" (("type" . "patch"))
303 "attachment" (("filename" . file))
307 "text" "plain" nil nil nil nil)
310 ;; Octect binary text
313 "application" "winword" nil
315 "attachment" (("filename" . file))
317 ("\\.ppt$" ; MS Power Point
318 "application" "vnd.ms-powerpoint" nil
320 "attachment" (("filename" . file))
326 "inline" (("filename" . file))
329 "application" "postscript" nil
331 "attachment" (("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 "inline" (("filename" . file))
374 "inline" (("filename" . file))
379 "attachment" (("filename" . file))
384 "attachment" (("filename" . file))
387 "application" "octet-stream" (("type" . "tar+gzip"))
389 "attachment" (("filename" . file))
392 "application" "octet-stream" (("type" . "tar+gzip"))
394 "attachment" (("filename" . file))
397 "application" "octet-stream" (("type" . "tar+compress"))
399 "attachment" (("filename" . file))
402 "application" "octet-stream" (("type" . "tar+compress"))
404 "attachment" (("filename" . file))
407 "application" "octet-stream" (("type" . "gzip"))
409 "attachment" (("filename" . file))
412 "application" "octet-stream" (("type" . "compress"))
414 "attachment" (("filename" . file))
417 "application" "octet-stream" (("type" . "lha"))
419 "attachment" (("filename" . file))
422 "application" "zip" nil
424 "attachment" (("filename" . file))
430 "application" "octet-stream" nil
432 "attachment" (("filename" . file)))
434 "*Alist of file name, types, parameters, and default encoding.
435 If encoding is nil, it is determined from its contents."
439 (choice :tag "Primary-Type"
440 ,@(nconc (mapcar (lambda (cell)
441 (list 'item (car cell))
446 (choice :tag "Sub-Type"
449 (mapcar (lambda (cell)
450 (mapcar (lambda (cell)
451 (list 'item (car cell))
457 (repeat :tag "Parameters of Content-Type field"
458 (cons string (choice string symbol)))
459 ;; content-transfer-encoding
460 (choice :tag "Encoding"
463 (mapcar (lambda (cell)
466 (mime-encoding-list))))
468 (choice :tag "Disposition-Type"
474 (repeat :tag "Parameters of Content-Disposition field"
475 (cons string (choice string symbol)))
480 ;;; @@ about charset, encoding and transfer-level
483 (defvar mime-charset-type-list
485 (iso-8859-1 8 "quoted-printable")
486 (iso-8859-2 8 "quoted-printable")
487 (iso-8859-3 8 "quoted-printable")
488 (iso-8859-4 8 "quoted-printable")
489 (iso-8859-5 8 "quoted-printable")
490 (koi8-r 8 "quoted-printable")
491 (iso-8859-7 8 "quoted-printable")
492 (iso-8859-8 8 "quoted-printable")
493 (iso-8859-9 8 "quoted-printable")
494 (iso-2022-jp 7 "base64")
495 (iso-2022-kr 7 "base64")
501 (shift_jis 8 "base64")
503 (iso-2022-jp-2 7 "base64")
504 (iso-2022-int-1 7 "base64")
507 (defvar mime-transfer-level 7
508 "*A number of network transfer level. It should be bigger than 7.")
509 (make-variable-buffer-local 'mime-transfer-level)
511 (defsubst mime-encoding-name (transfer-level &optional not-omit)
512 (cond ((> transfer-level 8) "binary")
513 ((= transfer-level 8) "8bit")
517 (defvar mime-transfer-level-string
518 (mime-encoding-name mime-transfer-level 'not-omit)
519 "A string formatted version of mime-transfer-level")
520 (make-variable-buffer-local 'mime-transfer-level-string)
523 ;;; @@ about content transfer encoding
525 (defvar mime-content-transfer-encoding-priority-list
526 '(nil "8bit" "binary"))
528 ;;; @@ about message inserting
531 (defvar mime-edit-yank-ignored-field-list
532 '("Received" "Approved" "Path" "Replied" "Status"
533 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
534 "Delete these fields from original message when it is inserted
535 as message/rfc822 part.
536 Each elements are regexp of field-name.")
538 (defvar mime-edit-yank-ignored-field-regexp
540 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
543 (defvar mime-edit-message-inserter-alist nil)
544 (defvar mime-edit-mail-inserter-alist nil)
547 ;;; @@ about message splitting
550 (defcustom mime-edit-split-message t
551 "*Split large message if it is non-nil."
555 (defcustom mime-edit-message-default-max-lines 1000
556 "*Default maximum lines of a message."
560 (defcustom mime-edit-message-max-lines-alist
561 '((news-reply-mode . 500))
562 "Alist of major-mode vs maximum lines of a message.
563 If it is not specified for a major-mode,
564 `mime-edit-message-default-max-lines' is used."
568 (defconst mime-edit-split-ignored-field-regexp
569 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
571 (defcustom mime-edit-split-blind-field-regexp
572 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
573 "*Regular expression to match field-name to be ignored when split sending."
577 (defvar mime-edit-split-message-sender-alist nil)
579 (defvar mime-edit-news-reply-mode-server-running nil)
585 (defconst mime-edit-single-part-tag-regexp
586 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
587 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
589 (defconst mime-edit-quoted-single-part-tag-regexp
590 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
592 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
594 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
596 (defconst mime-edit-beginning-tag-regexp
597 (regexp-or mime-edit-single-part-tag-regexp
598 mime-edit-multipart-beginning-regexp))
600 (defconst mime-edit-end-tag-regexp
601 (regexp-or mime-edit-single-part-tag-regexp
602 mime-edit-multipart-end-regexp))
604 (defconst mime-edit-tag-regexp
605 (regexp-or mime-edit-single-part-tag-regexp
606 mime-edit-multipart-beginning-regexp
607 mime-edit-multipart-end-regexp))
609 (defvar mime-tag-format "--[[%s]]"
610 "*Control-string making a MIME tag.")
612 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
613 "*Control-string making a MIME tag with encoding.")
616 ;;; @@ multipart boundary
619 (defvar mime-multipart-boundary "Multipart"
620 "*Boundary of a multipart message.")
623 ;;; @@ optional header fields
626 (defvar mime-edit-insert-user-agent-field t
627 "*If non-nil, insert User-Agent header field.")
629 (defvar mime-edit-user-agent-value
630 (concat (mime-product-name mime-user-interface-product)
632 (mapconcat #'number-to-string
633 (mime-product-version mime-user-interface-product) ".")
635 (mime-product-code-name mime-user-interface-product)
637 (mime-product-name mime-library-product)
639 (mapconcat #'number-to-string
640 (mime-product-version mime-library-product) ".")
642 (mime-product-code-name mime-library-product)
644 (if (featurep 'xemacs)
645 (concat (cond ((featurep 'utf-2000)
646 (concat "UTF-2000-MULE/" utf-2000-version))
647 ((featurep 'mule) "MULE"))
649 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
652 (substring emacs-version 0 (match-end 0))
653 (cond ((and (boundp 'xemacs-betaname)
655 ;; It does not exist in XEmacs
656 ;; versions prior to 20.3.
657 (concat " " xemacs-betaname))
658 ((and (boundp 'emacs-patch-level)
660 ;; It does not exist in FSF Emacs or in
661 ;; XEmacs versions earlier than 21.1.1.
662 (format " (patch %d)" emacs-patch-level))
664 " (" xemacs-codename ") ("
665 system-configuration ")")
666 " (" emacs-version ")"))
667 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
668 (substring emacs-version 0 (match-beginning 0))
671 (if (boundp 'enable-multibyte-characters)
673 " (" system-configuration ")"
674 (if enable-multibyte-characters
675 (concat " MULE/" mule-version)
676 " (with unibyte mode)")
677 (if (featurep 'meadow)
678 (let ((mver (Meadow-version)))
679 (if (string-match "^Meadow-" mver)
684 (concat "MULE/" mule-version
685 " (based on Emacs " ver ")"))
686 (concat "Emacs/" ver " (" system-configuration ")")))))
687 "Body of User-Agent field.
688 If variable `mime-edit-insert-user-agent-field' is not nil, it is
689 inserted into message header.")
695 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
696 "*Specify MIME tspecials.
697 Tspecials means any character that matches with it in header must be quoted.")
699 (defconst mime-edit-mime-version-value
700 (concat "1.0 (generated by " mime-edit-version ")")
701 "MIME version number.")
703 (defconst mime-edit-mime-version-field-for-message/partial
704 (concat "MIME-Version:"
705 (eword-encode-field-body
706 (concat " 1.0 (split by " mime-edit-version ")\n")
708 "MIME version field for message/partial.")
711 ;;; @ keymap and menu
714 (defvar mime-edit-mode-flag nil)
715 (make-variable-buffer-local 'mime-edit-mode-flag)
717 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
718 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
719 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
720 "Keymap for MIME-Edit mode commands to insert entity or set status.")
722 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
723 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
724 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
725 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
726 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
727 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
728 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
729 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
730 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
731 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
733 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
734 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
735 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
736 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
737 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
738 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
739 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
740 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
741 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
742 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
744 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
745 "Keymap prefix for MIME-Edit mode commands about enclosure.")
746 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
747 "Keymap for MIME-Edit mode commands about enclosure.")
749 (define-key mime-edit-mode-enclosure-map
750 "\C-a" 'mime-edit-enclose-alternative-region)
751 (define-key mime-edit-mode-enclosure-map
752 "\C-p" 'mime-edit-enclose-parallel-region)
753 (define-key mime-edit-mode-enclosure-map
754 "\C-m" 'mime-edit-enclose-mixed-region)
755 (define-key mime-edit-mode-enclosure-map
756 "\C-d" 'mime-edit-enclose-digest-region)
757 (define-key mime-edit-mode-enclosure-map
758 "\C-s" 'mime-edit-enclose-pgp-signed-region)
759 (define-key mime-edit-mode-enclosure-map
760 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
761 (define-key mime-edit-mode-enclosure-map
762 "\C-q" 'mime-edit-enclose-quote-region)
764 (defvar mime-edit-mode-map (make-sparse-keymap)
765 "Keymap for MIME-Edit mode commands.")
766 (define-key mime-edit-mode-map
767 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
768 (define-key mime-edit-mode-map
769 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
771 (defconst mime-edit-menu-title "MIME-Edit")
773 (defconst mime-edit-menu-list
774 '((mime-help "Describe MIME editor mode" mime-edit-help)
775 (file "Insert File" mime-edit-insert-file)
776 (external "Insert External" mime-edit-insert-external)
777 (voice "Insert Voice" mime-edit-insert-voice)
778 (message "Insert Message" mime-edit-insert-message)
779 (mail "Insert Mail" mime-edit-insert-mail)
780 (signature "Insert Signature" mime-edit-insert-signature)
781 (text "Insert Text" mime-edit-insert-text)
782 (tag "Insert Tag" mime-edit-insert-tag)
783 (alternative "Enclose as alternative"
784 mime-edit-enclose-alternative-region)
785 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
786 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
787 (digest "Enclose as digest" mime-edit-enclose-digest-region)
788 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
789 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
790 (quote "Verbatim region" mime-edit-enclose-quote-region)
791 (key "Insert Public Key" mime-edit-insert-key)
792 (split "About split" mime-edit-set-split)
793 (sign "About sign" mime-edit-set-sign)
794 (encrypt "About encryption" mime-edit-set-encrypt)
795 (preview "Preview Message" mime-edit-preview-message)
796 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
798 "MIME-edit menubar entry.")
800 (cond ((featurep 'xemacs)
801 ;; modified by Pekka Marjola <pema@iki.fi>
802 ;; 1995/9/5 (c.f. [tm-en:69])
803 (defun mime-edit-define-menu-for-xemacs ()
804 "Define menu for XEmacs."
805 (cond ((featurep 'menubar)
806 (make-local-variable 'current-menubar)
807 (set-buffer-menubar current-menubar)
810 (cons mime-edit-menu-title
813 (vector (nth 1 item)(nth 2 item)
816 mime-edit-menu-list)))
819 ;; modified by Steven L. Baur <steve@miranova.com>
820 ;; 1995/12/6 (c.f. [tm-en:209])
821 (or (boundp 'mime-edit-popup-menu-for-xemacs)
822 (setq mime-edit-popup-menu-for-xemacs
823 (append '("MIME Commands" "---")
824 (mapcar (function (lambda (item)
828 mime-edit-menu-list)))
831 ((>= emacs-major-version 19)
832 (define-key mime-edit-mode-map [menu-bar mime-edit]
833 (cons mime-edit-menu-title
834 (make-sparse-keymap mime-edit-menu-title)))
837 (define-key mime-edit-mode-map
838 (vector 'menu-bar 'mime-edit (car item))
839 (cons (nth 1 item)(nth 2 item))
842 (reverse mime-edit-menu-list)
850 (defvar mime-edit-touched-flag nil)
853 (defun mime-edit-mode ()
854 "MIME minor mode for editing the tagged MIME message.
856 In this mode, basically, the message is composed in the tagged MIME
857 format. The message tag looks like:
859 --[[text/plain; charset=ISO-2022-JP][7bit]]
861 The tag specifies the MIME content type, subtype, optional parameters
862 and transfer encoding of the message following the tag. Messages
863 without any tag are treated as `text/plain' by default. Charset and
864 transfer encoding are automatically defined unless explicitly
865 specified. Binary messages such as audio and image are usually
866 hidden. The messages in the tagged MIME format are automatically
867 translated into a MIME compliant message when exiting this mode.
869 Available charsets depend on Emacs version being used. The following
870 lists the available charsets of each emacs.
872 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
873 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
874 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
875 CN-BIG5 and ISO-2022-INT-1 are available.
877 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
878 be used to represent multilingual text in intermixed manner. Any
879 languages that has no registered charset are represented as either
880 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
882 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
883 without mule, please set variable `default-mime-charset'. This
884 variable must be symbol of which name is a MIME charset.
886 If you want to add more charsets in mule, please set variable
887 `charsets-mime-charset-alist'. This variable must be alist of which
888 key is list of charset and value is symbol of MIME charset. If name
889 of coding-system is different as MIME charset, please set variable
890 `mime-charset-coding-system-alist'. This variable must be alist of
891 which key is MIME charset and value is coding-system.
893 Following commands are available in addition to major mode commands:
896 \\[mime-edit-insert-text] insert a text message.
897 \\[mime-edit-insert-file] insert a (binary) file.
898 \\[mime-edit-insert-external] insert a reference to external body.
899 \\[mime-edit-insert-voice] insert a voice message.
900 \\[mime-edit-insert-message] insert a mail or news message.
901 \\[mime-edit-insert-mail] insert a mail message.
902 \\[mime-edit-insert-signature] insert a signature file at end.
903 \\[mime-edit-insert-key] insert PGP public key.
904 \\[mime-edit-insert-tag] insert a new MIME tag.
906 \[make enclosure (maybe multipart)\]
907 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
908 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
909 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
910 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
911 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
912 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
913 \\[mime-edit-enclose-quote-region] enclose as verbose mode
914 (to avoid to expand tags)
917 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
918 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
919 \\[mime-edit-set-split] set message splitting mode.
920 \\[mime-edit-set-sign] set PGP-sign mode.
921 \\[mime-edit-set-encrypt] set PGP-encryption mode.
922 \\[mime-edit-preview-message] preview editing MIME message.
923 \\[mime-edit-exit] exit and translate into a MIME
925 \\[mime-edit-help] show this help.
926 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
929 Additional commands are available in some major modes:
930 C-c C-c exit, translate and run the original command.
931 C-c C-s exit, translate and run the original command.
933 The following is a message example written in the tagged MIME format.
934 TABs at the beginning of the line are not a part of the message:
936 This is a conventional plain text. It should be translated
939 This is also a plain text. But, it is explicitly specified as
941 --[[text/plain; charset=ISO-8859-1]]
942 This is also a plain text. But charset is specified as
945 ¡Hola! Buenos días. ¿Cómo está usted?
947 This is a <bold>enriched text</bold>.
948 --[[image/gif][base64]]...image encoded in base64 here...
949 --[[audio/basic][base64]]...audio encoded in base64 here...
951 User customizable variables (not documented all of them):
953 Specifies a key prefix for MIME minor mode commands.
955 mime-ignore-preceding-spaces
956 Preceding white spaces in a message body are ignored if non-nil.
958 mime-ignore-trailing-spaces
959 Trailing white spaces in a message body are ignored if non-nil.
962 Hide a non-textual body message encoded in base64 after insertion
966 A number of network transfer level. It should be bigger than 7.
967 If you are in 8bit-through environment, please set 8.
969 mime-edit-voice-recorder
970 Specifies a function to record a voice message and encode it.
971 The function `mime-edit-voice-recorder-for-sun' is for Sun
975 Turning on MIME mode calls the value of mime-edit-mode-hook, if
978 mime-edit-translate-hook
979 The value of mime-edit-translate-hook is called just before translating
980 the tagged MIME format into a MIME compliant message if it is
981 non-nil. If the hook call the function mime-edit-insert-signature,
982 the signature file will be inserted automatically.
985 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
988 (if mime-edit-mode-flag
990 (if mime-edit-touched-flag
992 (make-local-variable 'mime-edit-touched-flag)
993 (setq mime-edit-touched-flag t)
998 (cond ((featurep 'xemacs)
999 (add-minor-mode 'mime-edit-mode-flag
1000 '((" MIME-Edit " mime-transfer-level-string))
1006 (set-alist 'minor-mode-alist
1007 'mime-edit-mode-flag
1008 '((" MIME-Edit " mime-transfer-level-string)))
1009 (set-alist 'minor-mode-map-alist
1010 'mime-edit-mode-flag
1016 (defun turn-on-mime-edit ()
1017 "Unconditionally turn on MIME-Edit mode."
1019 (if mime-edit-mode-flag
1020 (error "You are already editing a MIME message.")
1021 (setq mime-edit-mode-flag t)
1023 ;; Set transfer level into mode line
1025 (setq mime-transfer-level-string
1026 (mime-encoding-name mime-transfer-level 'not-omit))
1027 (force-mode-line-update)
1029 ;; Define menu for XEmacs.
1030 (if (featurep 'xemacs)
1031 (mime-edit-define-menu-for-xemacs)
1036 ;; I don't care about saving these.
1037 (setq paragraph-start
1038 (regexp-or mime-edit-single-part-tag-regexp
1040 (setq paragraph-separate
1041 (regexp-or mime-edit-single-part-tag-regexp
1042 paragraph-separate))
1043 (run-hooks 'mime-edit-mode-hook)
1045 (substitute-command-keys
1046 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1050 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1053 (defun mime-edit-exit (&optional nomime no-error)
1054 "Translate the tagged MIME message into a MIME compliant message.
1055 With no argument encode a message in the buffer into MIME, otherwise
1056 just return to previous mode."
1058 (if (not mime-edit-mode-flag)
1060 (error "You aren't editing a MIME message.")
1064 (run-hooks 'mime-edit-translate-hook)
1065 (mime-edit-translate-buffer)))
1066 ;; Restore previous state.
1067 (setq mime-edit-mode-flag nil)
1068 (if (and (featurep 'xemacs)
1069 (featurep 'menubar))
1070 (delete-menu-item (list mime-edit-menu-title))
1073 (set-buffer-modified-p (buffer-modified-p))
1074 (run-hooks 'mime-edit-exit-hook)
1075 (message "Exit MIME editor mode.")
1078 (defun mime-edit-maybe-translate ()
1080 (mime-edit-exit nil t)
1081 (call-interactively 'mime-edit-maybe-split-and-send)
1084 (defun mime-edit-help ()
1085 "Show help message about MIME mode."
1087 (with-output-to-temp-buffer "*Help*"
1088 (princ "MIME editor mode:\n")
1089 (princ (documentation 'mime-edit-mode))
1090 (print-help-return-message)))
1092 (defun mime-edit-insert-text (&optional subtype)
1093 "Insert a text message.
1094 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1095 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1097 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1099 (if (looking-at mime-edit-single-part-tag-regexp)
1101 ;; Make a space between the following message.
1105 (if (and (member (cadr ret) '("enriched"))
1106 (fboundp 'enriched-mode))
1108 (if (boundp 'enriched-mode)
1113 (defun mime-edit-insert-file (file &optional verbose)
1114 "Insert a message from a file."
1115 (interactive "fInsert file as MIME message: \nP")
1116 (let* ((guess (mime-find-file-type file))
1117 (type (nth 0 guess))
1118 (subtype (nth 1 guess))
1119 (parameters (nth 2 guess))
1120 (encoding (nth 3 guess))
1121 (disposition-type (nth 4 guess))
1122 (disposition-params (nth 5 guess))
1125 (setq type (mime-prompt-for-type type)
1126 subtype (mime-prompt-for-subtype type subtype)
1128 (if (or (interactive-p) verbose)
1129 (setq encoding (mime-prompt-for-encoding encoding))
1131 (if (or (consp parameters) (stringp disposition-type))
1132 (let ((rest parameters) cell attribute value)
1133 (setq parameters "")
1135 (setq cell (car rest))
1136 (setq attribute (car cell))
1137 (setq value (cdr cell))
1138 (if (eq value 'file)
1139 (setq value (std11-wrap-as-quoted-string
1140 (file-name-nondirectory file)))
1142 (setq parameters (concat parameters "; " attribute "=" value))
1143 (setq rest (cdr rest))
1145 (if disposition-type
1148 (concat parameters "\n"
1149 "Content-Disposition: " disposition-type))
1150 (setq rest disposition-params)
1152 (setq cell (car rest))
1153 (setq attribute (car cell))
1154 (setq value (cdr cell))
1155 (if (eq value 'file)
1156 (setq value (std11-wrap-as-quoted-string
1157 (file-name-nondirectory file)))
1160 (concat parameters "; " attribute "=" value))
1161 (setq rest (cdr rest))
1165 (mime-edit-insert-tag type subtype parameters)
1166 (mime-edit-insert-binary-file file encoding)
1169 (defun mime-edit-insert-external ()
1170 "Insert a reference to external body."
1172 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1174 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1176 (let* ((pritype (mime-prompt-for-type))
1177 (subtype (mime-prompt-for-subtype pritype))
1178 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1181 (insert "Content-Type: "
1182 pritype "/" subtype (or parameters "") "\n")))
1183 (if (and (not (eobp))
1184 (not (looking-at mime-edit-single-part-tag-regexp)))
1185 (insert (mime-make-text-tag) "\n")))
1187 (defun mime-edit-insert-voice ()
1188 "Insert a voice message."
1192 "What transfer encoding: "
1193 (mime-encoding-alist) nil t nil)))
1194 (mime-edit-insert-tag "audio" "basic" nil)
1195 (mime-edit-define-encoding encoding)
1197 (narrow-to-region (1- (point))(point))
1199 (funcall mime-edit-voice-recorder encoding)
1202 (invisible-region (point-min)(point-max))
1203 (goto-char (point-max))
1206 (defun mime-edit-insert-signature (&optional arg)
1207 "Insert a signature file."
1209 (let ((signature-insert-hook
1212 (let ((items (mime-find-file-type signature-file-name)))
1213 (apply (function mime-edit-insert-tag)
1214 (car items) (cadr items) (list (caddr items))))
1217 (insert-signature arg)
1221 ;; Insert a new tag around a point.
1223 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1224 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1225 If nothing is inserted, return nil."
1228 (mime-edit-goto-tag)
1229 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1230 (< (match-beginning 0) p)
1233 (goto-char (match-beginning 0))
1242 (mime-prompt-for-type)))
1245 (mime-prompt-for-subtype pritype)))
1248 (mime-prompt-for-parameters pritype subtype delimiter)))
1249 ;; Make a new MIME tag.
1250 (setq newtag (mime-make-tag pritype subtype parameters))
1251 ;; Find an current MIME tag.
1254 (if (mime-edit-goto-tag)
1255 (buffer-substring (match-beginning 0) (match-end 0))
1256 ;; Assume content type is 'text/plan'.
1257 (mime-make-tag "text" "plain")
1259 ;; We are only interested in TEXT.
1261 (not (mime-test-content-type
1262 (mime-edit-get-contype oldtag) "text")))
1265 (if (or (not oldtag) ;Not text
1266 (or mime-ignore-same-text-tag
1267 (not (string-equal oldtag newtag))))
1269 ;; Mark the beginning of the tag for convenience.
1270 (push-mark (point) 'nomsg)
1271 (insert newtag "\n")
1272 (list pritype subtype parameters) ;New tag is created.
1274 ;; Restore previous point.
1276 nil ;Nothing is created.
1280 (defun mime-edit-insert-binary-file (file &optional encoding)
1281 "Insert binary FILE at point.
1282 Optional argument ENCODING specifies an encoding method such as base64."
1283 (let* ((tagend (1- (point))) ;End of the tag
1284 (hide-p (and mime-auto-hide-body
1287 (let ((en (downcase encoding)))
1288 (or (string-equal en "7bit")
1289 (string-equal en "8bit")
1290 (string-equal en "binary")
1294 (narrow-to-region tagend (point))
1295 (mime-insert-encoded-file file encoding)
1298 (invisible-region (point-min) (point-max))
1299 (goto-char (point-max))
1301 (goto-char (point-max))
1304 (looking-at mime-edit-tag-regexp)
1305 (= (point)(point-max))
1306 (mime-edit-insert-tag "text" "plain")
1308 ;; Define encoding even if it is 7bit.
1309 (if (stringp encoding)
1311 (goto-char tagend) ; Make sure which line the tag is on.
1312 (mime-edit-define-encoding encoding)
1317 ;; Commands work on a current message flagment.
1319 (defun mime-edit-goto-tag ()
1320 "Search for the beginning of the tagged MIME message."
1321 (let ((current (point)))
1322 (if (looking-at mime-edit-tag-regexp)
1324 ;; At first, go to the end.
1325 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1326 (goto-char (1- (match-beginning 0))) ;For multiline tag
1329 (goto-char (point-max))
1331 ;; Then search for the beginning.
1332 (re-search-backward mime-edit-end-tag-regexp nil t)
1333 (or (looking-at mime-edit-beginning-tag-regexp)
1334 ;; Restore previous point.
1341 (defun mime-edit-content-beginning ()
1342 "Return the point of the beginning of content."
1344 (let ((beg (save-excursion
1345 (beginning-of-line) (point))))
1346 (if (mime-edit-goto-tag)
1347 (let ((top (point)))
1348 (goto-char (match-end 0))
1349 (if (and (= beg top)
1350 (= (following-char) ?\^M))
1354 ;; Default text/plain tag.
1355 (goto-char (point-min))
1357 (concat "\n" (regexp-quote mail-header-separator)
1358 (if mime-ignore-preceding-spaces
1359 "[ \t\n]*\n" "\n")) nil 'move)
1363 (defun mime-edit-content-end ()
1364 "Return the point of the end of content."
1366 (if (mime-edit-goto-tag)
1368 (goto-char (match-end 0))
1369 (if (invisible-p (point))
1370 (next-visible-point (point))
1371 ;; Move to the end of this text.
1372 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1373 ;; Don't forget a multiline tag.
1374 (goto-char (match-beginning 0))
1378 ;; Assume the message begins with text/plain.
1379 (goto-char (mime-edit-content-beginning))
1380 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1381 ;; Don't forget a multiline tag.
1382 (goto-char (match-beginning 0)))
1386 (defun mime-edit-define-charset (charset)
1387 "Set charset of current tag to CHARSET."
1389 (if (mime-edit-goto-tag)
1390 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1391 (delete-region (match-beginning 0) (match-end 0))
1394 (mime-edit-set-parameter
1395 (mime-edit-get-contype tag)
1396 "charset" (upcase (symbol-name charset)))
1397 (mime-edit-get-encoding tag)))
1400 (defun mime-edit-define-encoding (encoding)
1401 "Set encoding of current tag to ENCODING."
1403 (if (mime-edit-goto-tag)
1404 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1405 (delete-region (match-beginning 0) (match-end 0))
1406 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1409 (defun mime-edit-choose-charset ()
1410 "Choose charset of a text following current point."
1411 (detect-mime-charset-region (point) (mime-edit-content-end))
1414 (defun mime-make-text-tag (&optional subtype)
1415 "Make a tag for a text after current point.
1416 Subtype of text type can be specified by an optional argument SUBTYPE.
1417 Otherwise, it is obtained from mime-content-types."
1418 (let* ((pritype "text")
1419 (subtype (or subtype
1420 (car (car (cdr (assoc pritype mime-content-types)))))))
1421 ;; Charset should be defined later.
1422 (mime-make-tag pritype subtype)))
1425 ;; Tag handling functions
1427 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1428 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1429 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1433 (defun mime-create-tag (contype &optional encoding)
1434 "Make a tag with CONTENT-TYPE and optional ENCODING."
1435 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1438 (defun mime-edit-get-contype (tag)
1439 "Return Content-Type (including parameters) of TAG."
1441 (or (string-match mime-edit-single-part-tag-regexp tag)
1442 (string-match mime-edit-multipart-beginning-regexp tag)
1443 (string-match mime-edit-multipart-end-regexp tag)
1445 (substring tag (match-beginning 1) (match-end 1))
1448 (defun mime-edit-get-encoding (tag)
1449 "Return encoding of TAG."
1451 (string-match mime-edit-single-part-tag-regexp tag)
1453 (not (= (match-beginning 3) (match-end 3)))
1454 (substring tag (match-beginning 3) (match-end 3))))
1456 (defun mime-get-parameter (contype parameter)
1457 "For given CONTYPE return value for PARAMETER.
1458 Nil if no such parameter."
1462 (regexp-quote parameter)
1463 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1465 (substring contype (match-beginning 1) (match-end 1))
1466 nil ;No such parameter
1469 (defun mime-edit-set-parameter (contype parameter value)
1470 "For given CONTYPE set PARAMETER to VALUE."
1471 (let (ctype opt-fields)
1472 (if (string-match "\n[^ \t\n\r]+:" contype)
1473 (setq ctype (substring contype 0 (match-beginning 0))
1474 opt-fields (substring contype (match-beginning 0)))
1475 (setq ctype contype)
1480 (regexp-quote parameter)
1481 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1484 (concat (substring ctype 0 (match-beginning 1))
1486 (substring contype (match-end 1))
1488 (concat ctype "; " parameter "=" value opt-fields)
1491 (defun mime-strip-parameters (contype)
1492 "Return primary content-type and subtype without parameters for CONTYPE."
1493 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1494 (substring contype (match-beginning 1) (match-end 1)) nil))
1496 (defun mime-test-content-type (contype type &optional subtype)
1497 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1498 (and (stringp contype)
1501 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1502 (downcase contype))))
1507 (defun mime-find-file-type (file)
1508 "Guess Content-Type, subtype, and parameters from FILE."
1510 (guesses mime-file-types))
1511 (while (and (not guess) guesses)
1512 (if (string-match (car (car guesses)) file)
1513 (setq guess (cdr (car guesses))))
1514 (setq guesses (cdr guesses)))
1518 (defun mime-prompt-for-type (&optional default)
1519 "Ask for Content-type."
1521 ;; Repeat until primary content type is specified.
1522 (while (string-equal type "")
1524 (completing-read "What content type: "
1527 'require-match ;Type must be specified.
1530 (if (string-equal type "")
1532 (message "Content type is required.")
1539 (defun mime-prompt-for-subtype (type &optional default)
1540 "Ask for subtype of media-type TYPE."
1541 (let ((subtypes (cdr (assoc type mime-content-types))))
1543 (assoc default subtypes))
1544 (setq default (car (car subtypes)))
1550 "What content subtype: (default " default ") ")
1551 "What content subtype: ")
1552 (cdr (assoc type mime-content-types))
1554 'require-match ;Subtype must be specified.
1557 (if (string-equal answer "") default answer)))
1559 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1560 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1561 Optional DELIMITER specifies parameter delimiter (';' by default)."
1562 (let* ((delimiter (or delimiter "; "))
1567 (mime-prompt-for-parameters-1
1569 (cdr (assoc pritype mime-content-types))))))
1572 (if (and (stringp parameters)
1573 (not (string-equal parameters "")))
1574 (concat delimiter parameters)
1575 "" ;"" if no parameters
1578 (defun mime-prompt-for-parameters-1 (optlist)
1579 (apply (function append)
1580 (mapcar (function mime-prompt-for-parameter) optlist)))
1582 (defun mime-prompt-for-parameter (parameter)
1584 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1585 (let* ((prompt (car parameter))
1586 (choices (mapcar (function
1588 (if (consp e) e (list e))))
1590 (default (car (car choices)))
1596 (concat "What " prompt
1598 (if (string-equal default "") "\"\"" default)
1600 choices nil nil ""))
1601 ;; If nothing is selected, use default.
1602 (if (string-equal answer "")
1603 (setq answer default)))
1605 (read-string (concat "What " prompt ": "))))
1606 (cons (if (and answer
1607 (not (string-equal answer "")))
1609 ;; Note: control characters ignored!
1610 (if (string-match mime-tspecials-regexp answer)
1611 (concat "\"" answer "\"") answer)))
1612 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1615 (defun mime-prompt-for-encoding (default)
1616 "Ask for Content-Transfer-Encoding."
1621 "What transfer encoding: "
1622 (mime-encoding-alist) nil t default)
1628 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1631 (defvar mime-edit-translate-buffer-hook
1632 '(mime-edit-pgp-enclose-buffer
1633 mime-edit-translate-body
1634 mime-edit-translate-header))
1636 (defun mime-edit-translate-header ()
1637 "Encode the message header into network representation."
1638 (eword-encode-header 'code-conversion)
1639 (run-hooks 'mime-edit-translate-header-hook)
1642 (defun mime-edit-translate-buffer ()
1643 "Encode the tagged MIME message in current buffer in MIME compliant message."
1646 (if (catch 'mime-edit-error
1648 (run-hooks 'mime-edit-translate-buffer-hook)
1652 (error "Translation error!")
1655 (defun mime-edit-find-inmost ()
1656 (goto-char (point-min))
1657 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1658 (let ((bb (match-beginning 0))
1660 (type (buffer-substring (match-beginning 1)(match-end 1)))
1662 (setq end-exp (format "--}-<<%s>>\n" type))
1664 (if (re-search-forward end-exp nil t)
1665 (setq eb (match-beginning 0))
1666 (setq eb (point-max))
1668 (narrow-to-region be eb)
1670 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1672 (narrow-to-region (match-beginning 0)(point-max))
1673 (mime-edit-find-inmost)
1676 (list type bb be eb)
1679 (defun mime-edit-process-multipart-1 (boundary)
1680 (let ((ret (mime-edit-find-inmost)))
1682 (let ((type (car ret))
1683 (bb (nth 1 ret))(be (nth 2 ret))
1686 (narrow-to-region bb eb)
1687 (delete-region bb be)
1688 (setq bb (point-min))
1689 (setq eb (point-max))
1692 (if (looking-at mime-edit-multipart-end-regexp)
1693 (let ((beg (match-beginning 0))
1696 (delete-region beg end)
1697 (or (looking-at mime-edit-beginning-tag-regexp)
1699 (insert (concat (mime-make-text-tag) "\n"))
1701 (cond ((string-equal type "quote")
1702 (mime-edit-enquote-region bb eb)
1704 ((string-equal type "pgp-signed")
1705 (mime-edit-sign-pgp-mime bb eb boundary)
1707 ((string-equal type "pgp-encrypted")
1708 (mime-edit-encrypt-pgp-mime bb eb boundary)
1710 ((string-equal type "kazu-signed")
1711 (mime-edit-sign-pgp-kazu bb eb boundary)
1713 ((string-equal type "kazu-encrypted")
1714 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1718 (nth 2 (mime-edit-translate-region bb eb
1722 (format "--[[multipart/%s;
1723 boundary=\"%s\"][7bit]]\n"
1728 (defun mime-edit-enquote-region (beg end)
1731 (narrow-to-region beg end)
1733 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1734 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1735 (replace-match (concat "- " (substring tag 1)))
1738 (defun mime-edit-dequote-region (beg end)
1741 (narrow-to-region beg end)
1743 (while (re-search-forward
1744 mime-edit-quoted-single-part-tag-regexp nil t)
1745 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1746 (replace-match (concat "-" (substring tag 2)))
1749 (defun mime-edit-sign-pgp-mime (beg end boundary)
1752 (narrow-to-region beg end)
1754 (mime-edit-translate-region beg end boundary))
1756 (encoding (nth 1 ret))
1757 (pgp-boundary (concat "pgp-sign-" boundary))
1760 (insert (format "Content-Type: %s\n" ctype))
1762 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1765 (or (pgg-sign-region (point-min)(point-max))
1766 (throw 'mime-edit-error 'pgp-error)
1769 (cdr (assq 'hash-algorithm
1770 (cdar (with-current-buffer pgg-output-buffer
1771 (pgg-parse-armor-region
1772 (point-min)(point-max))))))
1775 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1778 (insert (format "--[[multipart/signed;
1780 protocol=\"application/pgp-signature\"][7bit]]
1782 " pgp-boundary micalg pgp-boundary))
1783 (goto-char (point-max))
1784 (insert (format "\n--%s
1785 Content-Type: application/pgp-signature
1786 Content-Transfer-Encoding: 7bit
1789 (insert-buffer-substring pgg-output-buffer)
1790 (goto-char (point-max))
1791 (insert (format "\n--%s--\n" pgp-boundary))
1794 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1796 (defun mime-edit-make-encrypt-recipient-header ()
1797 (let* ((names mime-edit-encrypt-recipient-fields-list)
1799 (std11-field-bodies (cons "From" names)
1800 nil mail-header-separator))
1803 (setq values (cdr values))))
1804 (header (and (stringp from)
1805 (if (string-equal from "")
1807 (format "From: %s\n" from)
1810 (while (and names values)
1811 (let ((name (car names))
1812 (value (car values))
1814 (and (stringp value)
1815 (or (string-equal value "")
1817 (setq header (concat header name ": " value "\n")
1818 recipients (if recipients
1819 (concat recipients " ," value)
1822 (setq names (cdr names)
1823 values (cdr values))
1825 (vector from recipients header)
1828 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1831 (let (from recipients header)
1832 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1833 (setq from (aref ret 0)
1834 recipients (aref ret 1)
1835 header (aref ret 2))
1837 (narrow-to-region beg end)
1839 (mime-edit-translate-region beg end boundary))
1841 (encoding (nth 1 ret))
1842 (pgp-boundary (concat "pgp-" boundary)))
1845 (insert (format "Content-Type: %s\n" ctype))
1847 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1850 (or (let ((pgg-default-user-id
1852 (nth 1 (std11-extract-address-components from))
1853 pgg-default-user-id)))
1855 (point-min) (point-max)
1856 (mapcar (lambda (recipient)
1857 (nth 1 (std11-extract-address-components
1859 (split-string recipients
1860 "\\([ \t\n]*,[ \t\n]*\\)+")))
1862 (throw 'mime-edit-error 'pgp-error)
1864 (delete-region (point-min)(point-max))
1866 (insert (format "--[[multipart/encrypted;
1868 protocol=\"application/pgp-encrypted\"][7bit]]
1870 Content-Type: application/pgp-encrypted
1873 Content-Type: application/octet-stream
1874 Content-Transfer-Encoding: 7bit
1876 " pgp-boundary pgp-boundary pgp-boundary))
1877 (insert-buffer-substring pgg-output-buffer)
1878 (goto-char (point-max))
1879 (insert (format "\n--%s--\n" pgp-boundary))
1882 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1885 (narrow-to-region beg end)
1887 (mime-edit-translate-region beg end boundary))
1889 (encoding (nth 1 ret)))
1891 (insert (format "Content-Type: %s\n" ctype))
1893 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1896 (or (pgg-sign-region beg (point-max) 'clearsign)
1897 (throw 'mime-edit-error 'pgp-error)
1901 "--[[application/pgp; format=mime][7bit]]\n")
1905 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1907 (let (recipients header)
1908 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1909 (setq recipients (aref ret 1)
1910 header (aref ret 2))
1913 (narrow-to-region beg end)
1915 (mime-edit-translate-region beg end boundary))
1917 (encoding (nth 1 ret)))
1920 (insert (format "Content-Type: %s\n" ctype))
1922 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1925 (or (pgg-sign-region beg (point-max) recipients)
1926 (throw 'mime-edit-error 'pgp-error)
1930 "--[[application/pgp; format=mime][7bit]]\n")
1934 (defsubst replace-space-with-underline (str)
1935 (mapconcat (function
1943 (defun mime-edit-make-boundary ()
1944 (concat mime-multipart-boundary "_"
1945 (replace-space-with-underline (current-time-string))
1948 (defun mime-edit-translate-body ()
1949 "Encode the tagged MIME body in current buffer in MIME compliant message."
1952 (let ((boundary (mime-edit-make-boundary))
1955 (while (mime-edit-process-multipart-1
1956 (format "%s-%d" boundary i))
1960 ;; We are interested in message body.
1963 (goto-char (point-min))
1965 (concat "\n" (regexp-quote mail-header-separator)
1966 (if mime-ignore-preceding-spaces
1967 "[ \t\n]*\n" "\n")) nil 'move)
1971 (goto-char (point-max))
1972 (and mime-ignore-trailing-spaces
1973 (re-search-backward "[^ \t\n]\n" beg t)
1976 (setq ret (mime-edit-translate-region
1978 (format "%s-%d" boundary i)))
1980 (mime-edit-dequote-region (point-min)(point-max))
1981 (let ((contype (car ret)) ;Content-Type
1982 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1984 ;; Insert User-Agent field
1985 (and mime-edit-insert-user-agent-field
1986 (or (mail-position-on-field "User-Agent")
1987 (insert mime-edit-user-agent-value)
1989 ;; Make primary MIME headers.
1990 (or (mail-position-on-field "MIME-Version")
1991 (insert mime-edit-mime-version-value))
1992 ;; Remove old Content-Type and other fields.
1994 (goto-char (point-min))
1995 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1996 (narrow-to-region (point-min) (point))
1997 (goto-char (point-min))
1998 (mime-delete-field "Content-Type")
1999 (mime-delete-field "Content-Transfer-Encoding"))
2000 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2001 (mail-position-on-field "Content-Type")
2005 (mail-position-on-field "Content-Transfer-Encoding")
2009 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2010 "Translate single-part-tag to MIME header."
2011 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2012 (let* ((beg (match-beginning 0))
2014 (tag (buffer-substring beg end)))
2015 (delete-region beg end)
2016 (let ((contype (mime-edit-get-contype tag))
2017 (encoding (mime-edit-get-encoding tag)))
2018 (insert (concat prefix "--" boundary "\n"))
2020 (narrow-to-region (point)(point))
2021 (insert "Content-Type: " contype "\n")
2023 (insert "Content-Transfer-Encoding: " encoding "\n"))
2024 (eword-encode-header)
2029 (downcase encoding))))
2032 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2034 (setq boundary (mime-edit-make-boundary))
2038 (narrow-to-region beg end)
2039 (let ((tag nil) ;MIME tag
2040 (contype nil) ;Content-Type
2041 (encoding nil) ;Content-Transfer-Encoding
2042 (nparts 0)) ;Number of body parts
2043 ;; Normalize the body part by inserting appropriate message
2044 ;; tags for every message contents.
2045 (mime-edit-normalize-body)
2046 ;; Counting the number of Content-Type.
2047 (goto-char (point-min))
2048 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2049 (setq nparts (1+ nparts)))
2050 ;; Begin translation.
2052 ((and (<= nparts 1)(not multipart))
2053 ;; It's a singular message.
2054 (goto-char (point-min))
2055 (while (re-search-forward
2056 mime-edit-single-part-tag-regexp nil t)
2058 (buffer-substring (match-beginning 0) (match-end 0)))
2059 (delete-region (match-beginning 0) (1+ (match-end 0)))
2060 (setq contype (mime-edit-get-contype tag))
2061 (setq encoding (mime-edit-get-encoding tag))
2064 ;; It's a multipart message.
2065 (goto-char (point-min))
2066 (let ((prio mime-content-transfer-encoding-priority-list)
2068 (when (setq part-info
2069 (mime-edit-translate-single-part-tag boundary))
2070 (and (setq nprio (member (cdr part-info) prio))
2072 (while (setq part-info
2073 (mime-edit-translate-single-part-tag boundary "\n"))
2074 (and (setq nprio (member (cdr part-info) prio))
2075 (setq prio nprio))))
2076 ;; Define Content-Type as "multipart/mixed".
2078 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2079 (setq encoding (car prio))
2080 ;; Insert the trailer.
2081 (goto-char (point-max))
2082 (insert "\n--" boundary "--\n")
2084 (list contype encoding boundary nparts)
2087 (defun mime-edit-normalize-body ()
2088 "Normalize the body part by inserting appropriate message tags."
2089 ;; Insert the first MIME tags if necessary.
2090 (goto-char (point-min))
2091 (if (not (looking-at mime-edit-single-part-tag-regexp))
2092 (insert (mime-make-text-tag) "\n"))
2093 ;; Check each tag, and add new tag or correct it if necessary.
2094 (goto-char (point-min))
2095 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2096 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2097 (contype (mime-edit-get-contype tag))
2098 (charset (mime-get-parameter contype "charset"))
2099 (encoding (mime-edit-get-encoding tag)))
2100 ;; Remove extra whitespaces after the tag.
2101 (if (looking-at "[ \t]+$")
2102 (delete-region (match-beginning 0) (match-end 0)))
2104 (end (mime-edit-content-end))
2106 (if (= end (point-max))
2109 (or (looking-at mime-edit-beginning-tag-regexp)
2111 (insert (mime-make-text-tag) "\n")
2113 (visible-region beg end)
2117 ((mime-test-content-type contype "message")
2118 ;; Content-type "message" should be sent as is.
2121 ((mime-test-content-type contype "text")
2122 ;; Define charset for text if necessary.
2123 (setq charset (if charset
2124 (intern (downcase charset))
2125 (mime-edit-choose-charset)))
2126 (mime-edit-define-charset charset)
2127 (cond ((string-equal contype "text/x-rot13-47-48")
2130 (mule-caesar-region (point) (mime-edit-content-end))
2132 ((string-equal contype "text/enriched")
2137 (end (mime-edit-content-end))
2139 ;; Patch for hard newlines
2142 ;; (while (search-forward "\n" end t)
2143 ;; (put-text-property (match-beginning 0)
2146 ;; End patch for hard newlines
2147 (enriched-encode beg end)
2149 (if (search-forward "\n\n")
2150 (delete-region beg (match-end 0))
2153 ;; Point is now on current tag.
2154 ;; Define encoding and encode text if necessary.
2155 (or encoding ;Encoding is not specified.
2158 (let ((ret (cdr (assq charset mime-charset-type-list))))
2160 (setq bits (car ret)
2163 conv "quoted-printable")))
2164 (if (<= bits mime-transfer-level)
2165 (mime-encoding-name bits)
2167 (beg (mime-edit-content-beginning)))
2168 (encode-mime-charset-region beg (mime-edit-content-end)
2170 ;; Protect "From " in beginning of line
2172 (narrow-to-region beg (mime-edit-content-end))
2174 (let (case-fold-search)
2175 (if (re-search-forward "^From " nil t)
2177 (if (memq charset '(iso-2022-jp
2182 (replace-match "\e(BFrom ")
2183 (re-search-forward "^From " nil t)
2185 (setq encoding "quoted-printable")
2187 ;; canonicalize line break code
2188 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2190 (narrow-to-region beg (mime-edit-content-end))
2192 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2193 (replace-match "\\1\r\n"))))
2195 (mime-encode-region beg (mime-edit-content-end)
2196 (or encoding "7bit"))
2197 (mime-edit-define-encoding encoding)
2199 (goto-char (mime-edit-content-end))
2201 ((null encoding) ;Encoding is not specified.
2202 ;; Application, image, audio, video, and any other
2203 ;; unknown content-type without encoding should be
2205 (let* ((encoding "base64") ;Encode in BASE64 by default.
2206 (beg (mime-edit-content-beginning))
2207 (end (mime-edit-content-end)))
2208 (mime-encode-region beg end encoding)
2209 (mime-edit-define-encoding encoding))
2214 (defun mime-delete-field (field)
2215 "Delete header FIELD."
2216 (let ((regexp (format "^%s:[ \t]*" field)))
2217 (goto-char (point-min))
2218 (while (re-search-forward regexp nil t)
2219 (delete-region (match-beginning 0)
2220 (1+ (std11-field-end))))))
2224 ;;; Platform dependent functions
2227 ;; Sun implementations
2229 (defun mime-edit-voice-recorder-for-sun (encoding)
2230 "Record voice in a buffer using Sun audio device,
2231 and insert data encoded as ENCODING."
2232 (message "Start the recording on %s. Type C-g to finish the recording..."
2234 (mime-insert-encoded-file "/dev/audio" encoding)
2238 ;;; @ Other useful commands.
2241 ;; Message forwarding commands as content-type "message/rfc822".
2243 (defun mime-edit-insert-message (&optional message)
2245 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2246 (if (and inserter (fboundp inserter))
2248 (mime-edit-insert-tag "message" "rfc822")
2249 (funcall inserter message)
2251 (message "Sorry, I don't have message inserter for your MUA.")
2254 (defun mime-edit-insert-mail (&optional message)
2256 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2257 (if (and inserter (fboundp inserter))
2259 (mime-edit-insert-tag "message" "rfc822")
2260 (funcall inserter message)
2262 (message "Sorry, I don't have mail inserter for your MUA.")
2265 (defun mime-edit-inserted-message-filter ()
2268 (let ((header-start (point))
2269 (case-fold-search t)
2272 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2273 (if (re-search-forward "^$" (mark t))
2274 (narrow-to-region header-start (match-beginning 0))
2276 (goto-char header-start)
2277 (while (and (re-search-forward
2278 mime-edit-yank-ignored-field-regexp nil t)
2279 (setq beg (match-beginning 0))
2280 (setq end (1+ (std11-field-end)))
2282 (delete-region beg end)
2287 ;;; @ multipart enclosure
2290 (defun mime-edit-enclose-region-internal (type beg end)
2294 (narrow-to-region beg end)
2295 (insert (format "--<<%s>>-{\n" type))
2296 (goto-char (point-max))
2297 (insert (format "--}-<<%s>>\n" type))
2298 (goto-char (point-max))
2300 (or (looking-at mime-edit-beginning-tag-regexp)
2302 (insert (mime-make-text-tag) "\n")
2306 (defun mime-edit-enclose-quote-region (beg end)
2308 (mime-edit-enclose-region-internal 'quote beg end)
2311 (defun mime-edit-enclose-mixed-region (beg end)
2313 (mime-edit-enclose-region-internal 'mixed beg end)
2316 (defun mime-edit-enclose-parallel-region (beg end)
2318 (mime-edit-enclose-region-internal 'parallel beg end)
2321 (defun mime-edit-enclose-digest-region (beg end)
2323 (mime-edit-enclose-region-internal 'digest beg end)
2326 (defun mime-edit-enclose-alternative-region (beg end)
2328 (mime-edit-enclose-region-internal 'alternative beg end)
2331 (defun mime-edit-enclose-pgp-signed-region (beg end)
2333 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2336 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2338 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2341 (defun mime-edit-enclose-kazu-signed-region (beg end)
2343 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2346 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2348 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2351 (defun mime-edit-insert-key (&optional arg)
2352 "Insert a pgp public key."
2354 (mime-edit-insert-tag "application" "pgp-keys")
2355 (mime-edit-define-encoding "7bit")
2363 (defun mime-edit-set-split (arg)
2366 (y-or-n-p "Do you want to enable split? ")
2368 (setq mime-edit-split-message arg)
2370 (message "This message is enabled to split.")
2371 (message "This message is not enabled to split.")
2374 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2375 "Toggle transfer-level is 7bit or 8bit through.
2377 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2379 (if (numberp transfer-level)
2380 (setq mime-transfer-level transfer-level)
2381 (if (< mime-transfer-level 8)
2382 (setq mime-transfer-level 8)
2383 (setq mime-transfer-level 7)
2385 (message (format "Current transfer-level is %d bit"
2386 mime-transfer-level))
2387 (setq mime-transfer-level-string
2388 (mime-encoding-name mime-transfer-level 'not-omit))
2389 (force-mode-line-update)
2392 (defun mime-edit-set-transfer-level-7bit ()
2394 (mime-edit-toggle-transfer-level 7)
2397 (defun mime-edit-set-transfer-level-8bit ()
2399 (mime-edit-toggle-transfer-level 8)
2406 (defvar mime-edit-pgp-processing nil)
2407 (make-variable-buffer-local 'mime-edit-pgp-processing)
2409 (defun mime-edit-set-sign (arg)
2412 (y-or-n-p "Do you want to sign? ")
2416 (or (memq 'sign mime-edit-pgp-processing)
2417 (setq mime-edit-pgp-processing
2418 (nconc mime-edit-pgp-processing
2419 (copy-sequence '(sign)))))
2420 (message "This message will be signed.")
2422 (setq mime-edit-pgp-processing
2423 (delq 'sign mime-edit-pgp-processing))
2424 (message "This message will not be signed.")
2427 (defun mime-edit-set-encrypt (arg)
2430 (y-or-n-p "Do you want to encrypt? ")
2434 (or (memq 'encrypt mime-edit-set-encrypt)
2435 (setq mime-edit-pgp-processing
2436 (nconc mime-edit-pgp-processing
2437 (copy-sequence '(encrypt)))))
2438 (message "This message will be encrypt.")
2440 (setq mime-edit-pgp-processing
2441 (delq 'encrypt mime-edit-pgp-processing))
2442 (message "This message will not be encrypt.")
2445 (defun mime-edit-pgp-enclose-buffer ()
2446 (let ((beg (save-excursion
2447 (goto-char (point-min))
2448 (if (search-forward (concat "\n" mail-header-separator "\n"))
2453 (dolist (pgp-processing mime-edit-pgp-processing)
2454 (case pgp-processing
2456 (mime-edit-enclose-pgp-signed-region
2460 (mime-edit-enclose-pgp-encrypted-region
2469 (defun mime-edit-insert-partial-header (fields subject
2470 id number total separator)
2472 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2473 (insert mime-edit-mime-version-field-for-message/partial)
2475 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2476 id number total separator))
2479 (defun mime-edit-split-and-send
2480 (&optional cmd lines mime-edit-message-max-length)
2484 (count-lines (point-min) (point-max)))
2486 (or mime-edit-message-max-length
2487 (setq mime-edit-message-max-length
2488 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2489 mime-edit-message-default-max-lines))
2491 (let* ((mime-edit-draft-file-name
2492 (or (buffer-file-name)
2494 (expand-file-name "mime-draft" temporary-file-directory))))
2495 (separator mail-header-separator)
2497 (replace-space-with-underline (current-time-string))
2498 "@" (system-name) "\"")))
2499 (run-hooks 'mime-edit-before-split-hook)
2500 (let ((the-buf (current-buffer))
2501 (copy-buf (get-buffer-create " *Original Message*"))
2502 (header (std11-header-string-except
2503 mime-edit-split-ignored-field-regexp separator))
2504 (subject (mail-fetch-field "subject"))
2505 (total (+ (/ lines mime-edit-message-max-length)
2506 (if (> (mod lines mime-edit-message-max-length) 0)
2512 mime-edit-split-message-sender-alist))
2516 (error "Split sender is not specified for `%s'." major-mode)
2519 (mime-edit-partial-number 1)
2522 (set-buffer copy-buf)
2524 (insert-buffer the-buf)
2526 (if (re-search-forward
2527 (concat "^" (regexp-quote separator) "$") nil t)
2528 (let ((he (match-beginning 0)))
2530 (narrow-to-region (point-min) he)
2532 (goto-char (point-min))
2533 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2534 (delete-region (match-beginning 0)
2535 (1+ (std11-field-end)))
2537 (while (< mime-edit-partial-number total)
2540 (set-buffer copy-buf)
2541 (setq data (buffer-substring
2544 (goto-line mime-edit-message-max-length)
2547 (delete-region (point-min)(point))
2549 (mime-edit-insert-partial-header
2550 header subject id mime-edit-partial-number total separator)
2553 (message (format "Sending %d/%d..."
2554 mime-edit-partial-number total))
2555 (call-interactively command)
2556 (message (format "Sending %d/%d... done"
2557 mime-edit-partial-number total))
2559 (setq mime-edit-partial-number
2560 (1+ mime-edit-partial-number))
2564 (set-buffer copy-buf)
2565 (setq data (buffer-string))
2568 (mime-edit-insert-partial-header
2569 header subject id mime-edit-partial-number total separator)
2572 (message (format "Sending %d/%d..."
2573 mime-edit-partial-number total))
2574 (message (format "Sending %d/%d... done"
2575 mime-edit-partial-number total))
2579 (defun mime-edit-maybe-split-and-send (&optional cmd)
2581 (run-hooks 'mime-edit-before-send-hook)
2582 (let ((mime-edit-message-max-length
2583 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2584 mime-edit-message-default-max-lines))
2585 (lines (count-lines (point-min) (point-max)))
2587 (if (and (> lines mime-edit-message-max-length)
2588 mime-edit-split-message)
2589 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2593 ;;; @ preview message
2596 (defvar mime-edit-buffer nil) ; buffer local variable
2598 (defun mime-edit-preview-message ()
2599 "preview editing MIME message."
2601 (let* ((str (buffer-string))
2602 (separator mail-header-separator)
2603 (the-buf (current-buffer))
2604 (buf-name (buffer-name))
2605 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2606 (buf (get-buffer temp-buf-name))
2607 (pgp-processing mime-edit-pgp-processing)
2611 (switch-to-buffer buf)
2614 (setq buf (get-buffer-create temp-buf-name))
2615 (switch-to-buffer buf)
2618 (setq major-mode 'mime-temp-message-mode)
2619 (make-local-variable 'mail-header-separator)
2620 (setq mail-header-separator separator)
2621 (make-local-variable 'mime-edit-buffer)
2622 (setq mime-edit-buffer the-buf)
2623 (setq mime-edit-pgp-processing pgp-processing)
2625 (run-hooks 'mime-edit-translate-hook)
2626 (mime-edit-translate-buffer)
2627 (goto-char (point-min))
2628 (if (re-search-forward
2629 (concat "^" (regexp-quote separator) "$"))
2635 (defun mime-edit-quitting-method ()
2636 "Quitting method for mime-view."
2637 (let* ((entity (get-text-property (point-min) 'mime-view-entity))
2638 (temp (mime-entity-buffer entity))
2640 (mime-preview-kill-buffer)
2642 (setq buf mime-edit-buffer)
2644 (switch-to-buffer buf)
2647 (set-alist 'mime-preview-quitting-method-alist
2648 'mime-temp-message-mode
2649 #'mime-edit-quitting-method)
2655 (defvar mime-edit-again-ignored-field-regexp
2656 (concat "^\\(" "Content-.*\\|Mime-Version"
2657 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2659 "Regexp for deleted header fields when `mime-edit-again' is called.")
2661 (defsubst eliminate-top-spaces (string)
2662 "Eliminate top sequence of space or tab in STRING."
2663 (if (string-match "^[ \t]+" string)
2664 (substring string (match-end 0))
2667 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2670 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2671 '(("application/pgp-encrypted" . pgp-encrypted)
2672 ("application/pgp-signature" . pgp-signed))))
2673 (mime-content-type-subtype content-type)))
2674 (boundary (mime-content-type-parameter content-type "boundary"))
2675 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2676 (re-search-forward boundary-pat nil t)
2677 (let ((bb (match-beginning 0)) eb tag)
2678 (setq tag (format "\n--<<%s>>-{\n" subtype))
2681 (setq bb (+ bb (length tag)))
2683 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2685 (setq eb (match-beginning 0))
2686 (replace-match (format "--}-<<%s>>\n" subtype))
2688 (narrow-to-region bb eb)
2689 (goto-char (point-min))
2690 (while (re-search-forward boundary-pat nil t)
2691 (let ((beg (match-beginning 0))
2693 (delete-region beg (match-end 0))
2695 (if (re-search-forward boundary-pat nil t)
2696 (setq end (match-beginning 0))
2697 (setq end (point-max))
2700 (narrow-to-region beg end)
2702 ((eq subtype 'pgp-encrypted)
2705 (goto-char (point-min))
2706 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2709 (save-window-excursion
2710 (pgg-decrypt-region (match-beginning 0)
2712 (delete-region (point-min)(point-max))))
2713 (insert-buffer-substring pgg-output-buffer)
2714 (mime-edit-decode-message-in-buffer
2715 nil not-decode-text)
2716 (delete-region (goto-char (point-min))
2717 (if (search-forward "\n\n" nil t)
2720 (goto-char (point-max))
2723 (mime-edit-decode-message-in-buffer
2724 (if (eq subtype 'digest)
2726 (make-mime-content-type 'message 'rfc822))
2729 (goto-char (point-max))
2733 (goto-char (point-min))
2734 (or (= (point-min) 1)
2735 (delete-region (point-min)
2736 (if (search-forward "\n\n" nil t)
2742 (defun mime-edit-decode-single-part-in-buffer
2743 (content-type not-decode-text &optional content-disposition)
2744 (let* ((type (mime-content-type-primary-type content-type))
2745 (subtype (mime-content-type-subtype content-type))
2746 (ctype (format "%s/%s" type subtype))
2748 (pstr (let ((bytes (+ 14 (length ctype))))
2749 (mapconcat (function
2751 (if (string= (car attr) "charset")
2753 (setq charset (cdr attr))
2755 (let* ((str (concat (car attr)
2758 (setq bytes (+ bytes bs 2))
2761 (setq bytes (+ bs 1))
2765 (mime-content-type-parameters content-type) "")))
2768 (limit (save-excursion
2769 (if (search-forward "\n\n" nil t)
2772 (mime-content-disposition-type content-disposition))
2774 (if disposition-type
2775 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2776 (mapconcat (function
2781 (if (string-equal "filename"
2783 (std11-wrap-as-quoted-string
2787 (setq bytes (+ bytes bs 2))
2790 (setq bytes (+ bs 1))
2794 (mime-content-disposition-parameters
2795 content-disposition)
2798 (if disposition-type
2799 (setq pstr (format "%s\nContent-Disposition: %s%s"
2800 pstr disposition-type disposition-str))
2803 (if (re-search-forward
2804 "^Content-Transfer-Encoding:" limit t)
2805 (let ((beg (match-beginning 0))
2806 (hbeg (match-end 0))
2807 (end (std11-field-end limit)))
2810 (eliminate-top-spaces
2811 (std11-unfold-string
2812 (buffer-substring hbeg end)))))
2813 (if (or charset (eq type 'text))
2815 (delete-region beg (1+ end))
2816 (goto-char (point-min))
2817 (if (search-forward "\n\n" nil t)
2820 (match-end 0)(point-max) encoding)
2824 (if (or encoded (not not-decode-text))
2827 (goto-char (point-min))
2828 (while (re-search-forward "\r\n" nil t)
2829 (replace-match "\n")
2831 (decode-mime-charset-region (point-min)(point-max)
2832 (or charset default-mime-charset))
2834 (let ((he (if (re-search-forward "^$" nil t)
2838 (if (and (eq type 'text)
2839 (eq subtype 'x-rot13-47-48))
2840 (mule-caesar-region he (point-max))
2842 (if (= (point-min) 1)
2848 (format "%s/%s%s" type subtype pstr)
2851 (delete-region (point-min) he)
2853 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2859 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2862 (goto-char (point-min))
2863 (let ((ctl (or (mime-read-Content-Type)
2864 default-content-type)))
2866 (let ((type (mime-content-type-primary-type ctl)))
2868 ((and (eq type 'application)
2869 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2870 (delete-region (point-min)(point-max))
2872 ((eq type 'multipart)
2873 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
2876 (mime-edit-decode-single-part-in-buffer
2877 ctl not-decode-text (mime-read-Content-Disposition))
2880 (decode-mime-charset-region (point-min) (point-max)
2881 default-mime-charset))
2883 (if (= (point-min) 1)
2886 (std11-narrow-to-header)
2887 (goto-char (point-min))
2888 (while (re-search-forward
2889 mime-edit-again-ignored-field-regexp nil t)
2890 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2892 (mime-decode-header-in-buffer (not not-decode-text))
2894 (mime-decode-header-in-buffer (not not-decode-text))
2898 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2899 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2900 Content-Type and Content-Transfer-Encoding header fields will be
2901 converted to MIME-Edit tags."
2903 (goto-char (point-min))
2905 (concat "\n" (regexp-quote mail-header-separator) "\n")
2907 (replace-match "\n\n")
2909 (mime-edit-decode-message-in-buffer nil not-decode-text)
2910 (goto-char (point-min))
2912 (and (re-search-forward "^$")
2913 (replace-match mail-header-separator)
2923 (provide 'mime-edit)
2925 (run-hooks 'mime-edit-load-hook)
2927 ;;; mime-edit.el ends here