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 ;; Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
8 ;; Created: 1994/08/21 renamed from mime.el
9 ;; Renamed: 1997/2/21 from tm-edit.el
10 ;; Keywords: MIME, multimedia, multilingual, mail, news
12 ;; This file is part of SEMI (Sophisticated Emacs MIME Interfaces).
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; This is an Emacs minor mode for editing Internet multimedia
32 ;; messages formatted in MIME (RFC 2045, 2046, 2047, 2048 and 2049).
33 ;; All messages in this mode are composed in the tagged MIME format,
34 ;; that are described in the following examples. The messages
35 ;; composed in the tagged MIME format are automatically translated
36 ;; into a MIME compliant message when exiting the mode.
38 ;; Mule (multilingual feature of Emacs 20 and multilingual extension
39 ;; for XEmacs 20) has a capability of handling multilingual text in
40 ;; limited ISO-2022 manner that is based on early experiences in
41 ;; Japanese Internet community and resulted in RFC 1468 (ISO-2022-JP
42 ;; charset for MIME). In order to enable multilingual capability in
43 ;; single text message in MIME, charset of multilingual text written
44 ;; in Mule is declared as either `ISO-2022-JP-2' [RFC 1554]. Mule is
45 ;; required for reading the such messages.
47 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
48 ;; News mode. First of all, you need the following autoload
49 ;; definition to load mime-edit-mode automatically:
51 ;; (autoload 'turn-on-mime-edit "mime-edit"
52 ;; "Minor mode for editing MIME message." t)
54 ;; In case of Mail mode (includes VM mode), you need the following
57 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
58 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
60 ;; In case of MH-E, you need the following hook definition:
62 ;; (add-hook 'mh-letter-mode-hook
65 ;; (turn-on-mime-edit)
66 ;; (make-local-variable 'mail-header-separator)
67 ;; (setq mail-header-separator "--------")
69 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
71 ;; In case of News mode, you need the following hook definition:
73 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
74 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
76 ;; In case of Emacs 19, it is possible to emphasize the message tags
77 ;; using font-lock mode as follows:
79 ;; (add-hook 'mime-edit-mode-hook
83 ;; (setq font-lock-keywords (list mime-edit-tag-regexp))
86 ;; The message tag looks like:
88 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
90 ;; The tagged MIME message examples:
92 ;; This is a conventional plain text. It should be translated into
96 ;; This is also a plain text. But, it is explicitly specified as is.
97 ;;--[[text/plain; charset=ISO-8859-1]]
98 ;; This is also a plain text. But charset is specified as iso-8859-1.
100 ;; ¡Hola! Buenos días. ¿Cómo está usted?
101 ;;--[[text/enriched]]
102 ;; <center>This is a richtext.</center>
104 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
106 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
111 (require 'mail-utils)
113 (require 'eword-encode) ; eword-encode-field-body
121 (autoload 'pgg-encrypt-region "pgg"
122 "PGP encryption of current region." t)
123 (autoload 'pgg-sign-region "pgg"
124 "PGP signature of current region." t)
125 (autoload 'pgg-insert-key "pgg"
126 "Insert PGP public key at point." t)
127 (autoload 'smime-encrypt-region "smime"
128 "S/MIME encryption of current region.")
129 (autoload 'smime-sign-region "smime"
130 "S/MIME signature of current region.")
137 (defconst mime-edit-version
139 (mime-product-name mime-user-interface-product) " "
140 (mapconcat #'number-to-string
141 (mime-product-version mime-user-interface-product) ".")
142 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
148 (defgroup mime-edit nil
152 (defcustom mime-ignore-preceding-spaces nil
153 "*Ignore preceding white spaces if non-nil."
157 (defcustom mime-ignore-trailing-spaces nil
158 "*Ignore trailing white spaces if non-nil."
162 (defcustom mime-ignore-same-text-tag t
163 "*Ignore preceding text content-type tag that is same with new one.
164 If non-nil, the text tag is not inserted unless something different."
168 (defcustom mime-auto-hide-body t
169 "*Hide non-textual body encoded in base64 after insertion if non-nil."
173 (defcustom mime-edit-voice-recorder
174 (function mime-edit-voice-recorder-for-sun)
175 "*Function to record a voice message and encode it."
179 (defcustom mime-edit-mode-hook nil
180 "*Hook called when enter MIME mode."
184 (defcustom mime-edit-translate-hook nil
185 "*Hook called before translating into a MIME compliant message.
186 To insert a signature file automatically, call the function
187 `mime-edit-insert-signature' from this hook."
191 (defcustom mime-edit-exit-hook nil
192 "*Hook called when exit MIME mode."
196 (defvar mime-content-types
198 ;; Charset parameter need not to be specified, since it is
199 ;; defined automatically while translation.
201 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
214 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
215 ("directory" "/pub/GNU/elisp/mime")
217 ("mode" "image" "ascii" "local8"))
222 ("mode" "image" "ascii" "local8"))
223 ("tftp" ("site") ("name"))
224 ("afs" ("site") ("name"))
225 ("local-file" ("site") ("name"))
227 ("server" "ftpmail@nic.karrn.ad.jp")
235 ("octet-stream" ("type" "" "tar" "shar"))
237 ("vnd.ms-powerpoint")
238 ("x-kiss" ("x-cnf")))
252 "*Alist of content-type, subtype, parameters and its values.")
254 (defcustom mime-file-types
257 ;; Programming languages
260 "application" "octet-stream" (("type" . "C++"))
262 "attachment" (("filename" . file))
266 "application" "octet-stream" (("type" . "emacs-lisp"))
268 "attachment" (("filename" . file))
272 "application" "octet-stream" (("type" . "common-lisp"))
274 "attachment" (("filename" . file))
278 "application" "octet-stream" (("type" . "perl"))
280 "attachment" (("filename" . file))
283 ;; Text or translated text
288 "inline" (("filename" . file))
291 ;; .rc : procmail modules pm-xxxx.rc
292 ;; *rc : other resource files
294 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
297 "attachment" (("filename" . file))
305 ("\\.diff$\\|\\.patch$"
306 "application" "octet-stream" (("type" . "patch"))
308 "attachment" (("filename" . file))
312 "text" "plain" nil nil nil nil)
315 ;; Octect binary text
318 "application" "winword" nil
320 "attachment" (("filename" . file))
322 ("\\.ppt$" ; MS Power Point
323 "application" "vnd.ms-powerpoint" nil
325 "attachment" (("filename" . file))
331 "inline" (("filename" . file))
334 "application" "postscript" nil
336 "attachment" (("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 "inline" (("filename" . file))
384 "attachment" (("filename" . file))
389 "attachment" (("filename" . file))
392 "application" "octet-stream" (("type" . "tar+gzip"))
394 "attachment" (("filename" . file))
397 "application" "octet-stream" (("type" . "tar+gzip"))
399 "attachment" (("filename" . file))
402 "application" "octet-stream" (("type" . "tar+compress"))
404 "attachment" (("filename" . file))
407 "application" "octet-stream" (("type" . "tar+compress"))
409 "attachment" (("filename" . file))
412 "application" "octet-stream" (("type" . "gzip"))
414 "attachment" (("filename" . file))
417 "application" "octet-stream" (("type" . "compress"))
419 "attachment" (("filename" . file))
422 "application" "octet-stream" (("type" . "lha"))
424 "attachment" (("filename" . file))
427 "application" "zip" nil
429 "attachment" (("filename" . file))
435 "application" "octet-stream" nil
437 "attachment" (("filename" . file)))
439 "*Alist of file name, types, parameters, and default encoding.
440 If encoding is nil, it is determined from its contents."
444 (choice :tag "Primary-Type"
445 ,@(nconc (mapcar (lambda (cell)
446 (list 'item (car cell))
451 (choice :tag "Sub-Type"
454 (mapcar (lambda (cell)
455 (mapcar (lambda (cell)
456 (list 'item (car cell))
462 (repeat :tag "Parameters of Content-Type field"
463 (cons string (choice string symbol)))
464 ;; content-transfer-encoding
465 (choice :tag "Encoding"
468 (mapcar (lambda (cell)
471 (mime-encoding-list))))
473 (choice :tag "Disposition-Type"
479 (repeat :tag "Parameters of Content-Disposition field"
480 (cons string (choice string symbol)))
485 ;;; @@ about charset, encoding and transfer-level
488 (defvar mime-charset-type-list
490 (iso-8859-1 8 "quoted-printable")
491 (iso-8859-2 8 "quoted-printable")
492 (iso-8859-3 8 "quoted-printable")
493 (iso-8859-4 8 "quoted-printable")
494 (iso-8859-5 8 "quoted-printable")
495 (koi8-r 8 "quoted-printable")
496 (iso-8859-7 8 "quoted-printable")
497 (iso-8859-8 8 "quoted-printable")
498 (iso-8859-9 8 "quoted-printable")
499 (iso-2022-jp 7 "base64")
500 (iso-2022-kr 7 "base64")
506 (shift_jis 8 "base64")
508 (iso-2022-jp-2 7 "base64")
509 (iso-2022-int-1 7 "base64")
512 (defvar mime-transfer-level 7
513 "*A number of network transfer level. It should be bigger than 7.")
514 (make-variable-buffer-local 'mime-transfer-level)
516 (defsubst mime-encoding-name (transfer-level &optional not-omit)
517 (cond ((> transfer-level 8) "binary")
518 ((= transfer-level 8) "8bit")
522 (defvar mime-transfer-level-string
523 (mime-encoding-name mime-transfer-level 'not-omit)
524 "A string formatted version of mime-transfer-level")
525 (make-variable-buffer-local 'mime-transfer-level-string)
528 ;;; @@ about content transfer encoding
530 (defvar mime-content-transfer-encoding-priority-list
531 '(nil "8bit" "binary"))
533 ;;; @@ about message inserting
536 (defvar mime-edit-yank-ignored-field-list
537 '("Received" "Approved" "Path" "Replied" "Status"
538 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
539 "Delete these fields from original message when it is inserted
540 as message/rfc822 part.
541 Each elements are regexp of field-name.")
543 (defvar mime-edit-yank-ignored-field-regexp
545 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
548 (defvar mime-edit-message-inserter-alist nil)
549 (defvar mime-edit-mail-inserter-alist nil)
552 ;;; @@ about message splitting
555 (defcustom mime-edit-split-message t
556 "*Split large message if it is non-nil."
560 (defcustom mime-edit-message-default-max-lines 1000
561 "*Default maximum lines of a message."
565 (defcustom mime-edit-message-max-lines-alist
566 '((news-reply-mode . 500))
567 "Alist of major-mode vs maximum lines of a message.
568 If it is not specified for a major-mode,
569 `mime-edit-message-default-max-lines' is used."
573 (defconst mime-edit-split-ignored-field-regexp
574 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
576 (defcustom mime-edit-split-blind-field-regexp
577 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
578 "*Regular expression to match field-name to be ignored when split sending."
582 (defvar mime-edit-split-message-sender-alist nil)
584 (defvar mime-edit-news-reply-mode-server-running nil)
590 (defconst mime-edit-single-part-tag-regexp
591 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
592 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
594 (defconst mime-edit-quoted-single-part-tag-regexp
595 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
597 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
599 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
601 (defconst mime-edit-beginning-tag-regexp
602 (regexp-or mime-edit-single-part-tag-regexp
603 mime-edit-multipart-beginning-regexp))
605 (defconst mime-edit-end-tag-regexp
606 (regexp-or mime-edit-single-part-tag-regexp
607 mime-edit-multipart-end-regexp))
609 (defconst mime-edit-tag-regexp
610 (regexp-or mime-edit-single-part-tag-regexp
611 mime-edit-multipart-beginning-regexp
612 mime-edit-multipart-end-regexp))
614 (defvar mime-tag-format "--[[%s]]"
615 "*Control-string making a MIME tag.")
617 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
618 "*Control-string making a MIME tag with encoding.")
621 ;;; @@ multipart boundary
624 (defvar mime-multipart-boundary "Multipart"
625 "*Boundary of a multipart message.")
628 ;;; @@ optional header fields
631 (defvar mime-edit-insert-user-agent-field t
632 "*If non-nil, insert User-Agent header field.")
634 (defvar mime-edit-user-agent-value
635 (concat (mime-product-name mime-user-interface-product)
637 (mapconcat #'number-to-string
638 (mime-product-version mime-user-interface-product) ".")
640 (mime-product-code-name mime-user-interface-product)
642 (mime-product-name mime-library-product)
644 (mapconcat #'number-to-string
645 (mime-product-version mime-library-product) ".")
647 (mime-product-code-name mime-library-product)
649 (if (fboundp 'apel-version)
650 (concat (apel-version) " "))
651 (if (featurep 'xemacs)
652 (concat (cond ((featurep 'utf-2000)
653 (concat "UTF-2000-MULE/" utf-2000-version))
654 ((featurep 'mule) "MULE"))
656 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
659 (substring emacs-version 0 (match-end 0))
660 (cond ((and (boundp 'xemacs-betaname)
662 ;; It does not exist in XEmacs
663 ;; versions prior to 20.3.
664 (concat " " xemacs-betaname))
665 ((and (boundp 'emacs-patch-level)
667 ;; It does not exist in FSF Emacs or in
668 ;; XEmacs versions earlier than 21.1.1.
669 (format " (patch %d)" emacs-patch-level))
671 " (" xemacs-codename ") ("
672 system-configuration ")")
673 " (" emacs-version ")"))
674 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
675 (substring emacs-version 0 (match-beginning 0))
678 (if (boundp 'enable-multibyte-characters)
680 " (" system-configuration ")"
681 (if enable-multibyte-characters
682 (concat " MULE/" mule-version)
683 " (with unibyte mode)")
684 (if (featurep 'meadow)
685 (let ((mver (Meadow-version)))
686 (if (string-match "^Meadow-" mver)
691 (concat "MULE/" mule-version
692 " (based on Emacs " ver ")"))
693 (concat "Emacs/" ver " (" system-configuration ")")))))
694 "Body of User-Agent field.
695 If variable `mime-edit-insert-user-agent-field' is not nil, it is
696 inserted into message header.")
702 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
703 "*Specify MIME tspecials.
704 Tspecials means any character that matches with it in header must be quoted.")
706 (defconst mime-edit-mime-version-value
707 (concat "1.0 (generated by " mime-edit-version ")")
708 "MIME version number.")
710 (defconst mime-edit-mime-version-field-for-message/partial
711 (concat "MIME-Version:"
712 (eword-encode-field-body
713 (concat " 1.0 (split by " mime-edit-version ")\n")
715 "MIME version field for message/partial.")
718 ;;; @ keymap and menu
721 (defvar mime-edit-mode-flag nil)
722 (make-variable-buffer-local 'mime-edit-mode-flag)
724 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
725 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
726 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
727 "Keymap for MIME-Edit mode commands to insert entity or set status.")
729 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
730 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
731 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
732 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
733 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
734 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
735 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
736 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
737 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
738 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
740 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
741 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
742 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
743 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
744 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
745 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
746 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
747 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
748 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
749 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
751 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
752 "Keymap prefix for MIME-Edit mode commands about enclosure.")
753 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
754 "Keymap for MIME-Edit mode commands about enclosure.")
756 (define-key mime-edit-mode-enclosure-map
757 "\C-a" 'mime-edit-enclose-alternative-region)
758 (define-key mime-edit-mode-enclosure-map
759 "\C-p" 'mime-edit-enclose-parallel-region)
760 (define-key mime-edit-mode-enclosure-map
761 "\C-m" 'mime-edit-enclose-mixed-region)
762 (define-key mime-edit-mode-enclosure-map
763 "\C-d" 'mime-edit-enclose-digest-region)
764 (define-key mime-edit-mode-enclosure-map
765 "\C-s" 'mime-edit-enclose-pgp-signed-region)
766 (define-key mime-edit-mode-enclosure-map
767 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
768 (define-key mime-edit-mode-enclosure-map
769 "\C-q" 'mime-edit-enclose-quote-region)
771 (defvar mime-edit-mode-map (make-sparse-keymap)
772 "Keymap for MIME-Edit mode commands.")
773 (define-key mime-edit-mode-map
774 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
775 (define-key mime-edit-mode-map
776 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
778 (defconst mime-edit-menu-title "MIME-Edit")
780 (defconst mime-edit-menu-list
781 '((mime-help "Describe MIME editor mode" mime-edit-help)
782 (file "Insert File" mime-edit-insert-file)
783 (external "Insert External" mime-edit-insert-external)
784 (voice "Insert Voice" mime-edit-insert-voice)
785 (message "Insert Message" mime-edit-insert-message)
786 (mail "Insert Mail" mime-edit-insert-mail)
787 (signature "Insert Signature" mime-edit-insert-signature)
788 (text "Insert Text" mime-edit-insert-text)
789 (tag "Insert Tag" mime-edit-insert-tag)
790 (alternative "Enclose as alternative"
791 mime-edit-enclose-alternative-region)
792 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
793 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
794 (digest "Enclose as digest" mime-edit-enclose-digest-region)
795 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
796 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
797 (quote "Verbatim region" mime-edit-enclose-quote-region)
798 (key "Insert Public Key" mime-edit-insert-key)
799 (split "About split" mime-edit-set-split)
800 (sign "About sign" mime-edit-set-sign)
801 (encrypt "About encryption" mime-edit-set-encrypt)
802 (preview "Preview Message" mime-edit-preview-message)
803 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
805 "MIME-edit menubar entry.")
807 (cond ((featurep 'xemacs)
808 ;; modified by Pekka Marjola <pema@iki.fi>
809 ;; 1995/9/5 (c.f. [tm-en:69])
810 (defun mime-edit-define-menu-for-xemacs ()
811 "Define menu for XEmacs."
812 (cond ((featurep 'menubar)
813 (make-local-variable 'current-menubar)
814 (set-buffer-menubar current-menubar)
817 (cons mime-edit-menu-title
820 (vector (nth 1 item)(nth 2 item)
823 mime-edit-menu-list)))
826 ;; modified by Steven L. Baur <steve@miranova.com>
827 ;; 1995/12/6 (c.f. [tm-en:209])
828 (or (boundp 'mime-edit-popup-menu-for-xemacs)
829 (setq mime-edit-popup-menu-for-xemacs
830 (append '("MIME Commands" "---")
831 (mapcar (function (lambda (item)
835 mime-edit-menu-list)))
838 ((>= emacs-major-version 19)
839 (define-key mime-edit-mode-map [menu-bar mime-edit]
840 (cons mime-edit-menu-title
841 (make-sparse-keymap mime-edit-menu-title)))
844 (define-key mime-edit-mode-map
845 (vector 'menu-bar 'mime-edit (car item))
846 (cons (nth 1 item)(nth 2 item))
849 (reverse mime-edit-menu-list)
857 (defvar mime-edit-touched-flag nil)
860 (defun mime-edit-mode ()
861 "MIME minor mode for editing the tagged MIME message.
863 In this mode, basically, the message is composed in the tagged MIME
864 format. The message tag looks like:
866 --[[text/plain; charset=ISO-2022-JP][7bit]]
868 The tag specifies the MIME content type, subtype, optional parameters
869 and transfer encoding of the message following the tag. Messages
870 without any tag are treated as `text/plain' by default. Charset and
871 transfer encoding are automatically defined unless explicitly
872 specified. Binary messages such as audio and image are usually
873 hidden. The messages in the tagged MIME format are automatically
874 translated into a MIME compliant message when exiting this mode.
876 Available charsets depend on Emacs version being used. The following
877 lists the available charsets of each emacs.
879 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
880 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
881 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
882 CN-BIG5 and ISO-2022-INT-1 are available.
884 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
885 be used to represent multilingual text in intermixed manner. Any
886 languages that has no registered charset are represented as either
887 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
889 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
890 without mule, please set variable `default-mime-charset'. This
891 variable must be symbol of which name is a MIME charset.
893 If you want to add more charsets in mule, please set variable
894 `charsets-mime-charset-alist'. This variable must be alist of which
895 key is list of charset and value is symbol of MIME charset. If name
896 of coding-system is different as MIME charset, please set variable
897 `mime-charset-coding-system-alist'. This variable must be alist of
898 which key is MIME charset and value is coding-system.
900 Following commands are available in addition to major mode commands:
903 \\[mime-edit-insert-text] insert a text message.
904 \\[mime-edit-insert-file] insert a (binary) file.
905 \\[mime-edit-insert-external] insert a reference to external body.
906 \\[mime-edit-insert-voice] insert a voice message.
907 \\[mime-edit-insert-message] insert a mail or news message.
908 \\[mime-edit-insert-mail] insert a mail message.
909 \\[mime-edit-insert-signature] insert a signature file at end.
910 \\[mime-edit-insert-key] insert PGP public key.
911 \\[mime-edit-insert-tag] insert a new MIME tag.
913 \[make enclosure (maybe multipart)\]
914 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
915 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
916 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
917 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
918 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
919 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
920 \\[mime-edit-enclose-quote-region] enclose as verbose mode
921 (to avoid to expand tags)
924 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
925 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
926 \\[mime-edit-set-split] set message splitting mode.
927 \\[mime-edit-set-sign] set PGP-sign mode.
928 \\[mime-edit-set-encrypt] set PGP-encryption mode.
929 \\[mime-edit-preview-message] preview editing MIME message.
930 \\[mime-edit-exit] exit and translate into a MIME
932 \\[mime-edit-help] show this help.
933 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
936 Additional commands are available in some major modes:
937 C-c C-c exit, translate and run the original command.
938 C-c C-s exit, translate and run the original command.
940 The following is a message example written in the tagged MIME format.
941 TABs at the beginning of the line are not a part of the message:
943 This is a conventional plain text. It should be translated
946 This is also a plain text. But, it is explicitly specified as
948 --[[text/plain; charset=ISO-8859-1]]
949 This is also a plain text. But charset is specified as
952 ¡Hola! Buenos días. ¿Cómo está usted?
954 This is a <bold>enriched text</bold>.
955 --[[image/gif][base64]]...image encoded in base64 here...
956 --[[audio/basic][base64]]...audio encoded in base64 here...
958 User customizable variables (not documented all of them):
960 Specifies a key prefix for MIME minor mode commands.
962 mime-ignore-preceding-spaces
963 Preceding white spaces in a message body are ignored if non-nil.
965 mime-ignore-trailing-spaces
966 Trailing white spaces in a message body are ignored if non-nil.
969 Hide a non-textual body message encoded in base64 after insertion
973 A number of network transfer level. It should be bigger than 7.
974 If you are in 8bit-through environment, please set 8.
976 mime-edit-voice-recorder
977 Specifies a function to record a voice message and encode it.
978 The function `mime-edit-voice-recorder-for-sun' is for Sun
982 Turning on MIME mode calls the value of mime-edit-mode-hook, if
985 mime-edit-translate-hook
986 The value of mime-edit-translate-hook is called just before translating
987 the tagged MIME format into a MIME compliant message if it is
988 non-nil. If the hook call the function mime-edit-insert-signature,
989 the signature file will be inserted automatically.
992 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
995 (if mime-edit-mode-flag
997 (if mime-edit-touched-flag
999 (make-local-variable 'mime-edit-touched-flag)
1000 (setq mime-edit-touched-flag t)
1005 (cond ((featurep 'xemacs)
1006 (add-minor-mode 'mime-edit-mode-flag
1007 '((" MIME-Edit " mime-transfer-level-string))
1013 (set-alist 'minor-mode-alist
1014 'mime-edit-mode-flag
1015 '((" MIME-Edit " mime-transfer-level-string)))
1016 (set-alist 'minor-mode-map-alist
1017 'mime-edit-mode-flag
1023 (defun turn-on-mime-edit ()
1024 "Unconditionally turn on MIME-Edit mode."
1026 (if mime-edit-mode-flag
1027 (error "You are already editing a MIME message.")
1028 (setq mime-edit-mode-flag t)
1030 ;; Set transfer level into mode line
1032 (setq mime-transfer-level-string
1033 (mime-encoding-name mime-transfer-level 'not-omit))
1034 (force-mode-line-update)
1036 ;; Define menu for XEmacs.
1037 (if (featurep 'xemacs)
1038 (mime-edit-define-menu-for-xemacs)
1043 ;; I don't care about saving these.
1044 (setq paragraph-start
1045 (regexp-or mime-edit-single-part-tag-regexp
1047 (setq paragraph-separate
1048 (regexp-or mime-edit-single-part-tag-regexp
1049 paragraph-separate))
1050 (run-hooks 'mime-edit-mode-hook)
1052 (substitute-command-keys
1053 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1057 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1060 (defun mime-edit-exit (&optional nomime no-error)
1061 "Translate the tagged MIME message into a MIME compliant message.
1062 With no argument encode a message in the buffer into MIME, otherwise
1063 just return to previous mode."
1065 (if (not mime-edit-mode-flag)
1067 (error "You aren't editing a MIME message.")
1071 (run-hooks 'mime-edit-translate-hook)
1072 (mime-edit-translate-buffer)))
1073 ;; Restore previous state.
1074 (setq mime-edit-mode-flag nil)
1075 (if (and (featurep 'xemacs)
1076 (featurep 'menubar))
1077 (delete-menu-item (list mime-edit-menu-title))
1080 (set-buffer-modified-p (buffer-modified-p))
1081 (run-hooks 'mime-edit-exit-hook)
1082 (message "Exit MIME editor mode.")
1085 (defun mime-edit-maybe-translate ()
1087 (mime-edit-exit nil t)
1088 (call-interactively 'mime-edit-maybe-split-and-send)
1091 (defun mime-edit-help ()
1092 "Show help message about MIME mode."
1094 (with-output-to-temp-buffer "*Help*"
1095 (princ "MIME editor mode:\n")
1096 (princ (documentation 'mime-edit-mode))
1097 (print-help-return-message)))
1099 (defun mime-edit-insert-text (&optional subtype)
1100 "Insert a text message.
1101 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1102 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1104 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1106 (if (looking-at mime-edit-single-part-tag-regexp)
1108 ;; Make a space between the following message.
1112 (if (and (member (cadr ret) '("enriched"))
1113 (fboundp 'enriched-mode))
1115 (if (boundp 'enriched-mode)
1120 (defun mime-edit-insert-file (file &optional verbose)
1121 "Insert a message from a file."
1122 (interactive "fInsert file as MIME message: \nP")
1123 (let* ((guess (mime-find-file-type file))
1124 (type (nth 0 guess))
1125 (subtype (nth 1 guess))
1126 (parameters (nth 2 guess))
1127 (encoding (nth 3 guess))
1128 (disposition-type (nth 4 guess))
1129 (disposition-params (nth 5 guess))
1132 (setq type (mime-prompt-for-type type)
1133 subtype (mime-prompt-for-subtype type subtype)
1135 (if (or (interactive-p) verbose)
1136 (setq encoding (mime-prompt-for-encoding encoding))
1138 (if (or (consp parameters) (stringp disposition-type))
1139 (let ((rest parameters) cell attribute value)
1140 (setq parameters "")
1142 (setq cell (car rest))
1143 (setq attribute (car cell))
1144 (setq value (cdr cell))
1145 (if (eq value 'file)
1146 (setq value (std11-wrap-as-quoted-string
1147 (file-name-nondirectory file)))
1149 (setq parameters (concat parameters "; " attribute "=" value))
1150 (setq rest (cdr rest))
1152 (if disposition-type
1155 (concat parameters "\n"
1156 "Content-Disposition: " disposition-type))
1157 (setq rest disposition-params)
1159 (setq cell (car rest))
1160 (setq attribute (car cell))
1161 (setq value (cdr cell))
1162 (if (eq value 'file)
1163 (setq value (std11-wrap-as-quoted-string
1164 (file-name-nondirectory file)))
1167 (concat parameters "; " attribute "=" value))
1168 (setq rest (cdr rest))
1172 (mime-edit-insert-tag type subtype parameters)
1173 (mime-edit-insert-binary-file file encoding)
1176 (defun mime-edit-insert-external ()
1177 "Insert a reference to external body."
1179 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1181 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1183 (let* ((pritype (mime-prompt-for-type))
1184 (subtype (mime-prompt-for-subtype pritype))
1185 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1188 (insert "Content-Type: "
1189 pritype "/" subtype (or parameters "") "\n")))
1190 (if (and (not (eobp))
1191 (not (looking-at mime-edit-single-part-tag-regexp)))
1192 (insert (mime-make-text-tag) "\n")))
1194 (defun mime-edit-insert-voice ()
1195 "Insert a voice message."
1199 "What transfer encoding: "
1200 (mime-encoding-alist) nil t nil)))
1201 (mime-edit-insert-tag "audio" "basic" nil)
1202 (mime-edit-define-encoding encoding)
1204 (narrow-to-region (1- (point))(point))
1206 (funcall mime-edit-voice-recorder encoding)
1209 (invisible-region (point-min)(point-max))
1210 (goto-char (point-max))
1213 (defun mime-edit-insert-signature (&optional arg)
1214 "Insert a signature file."
1216 (let ((signature-insert-hook
1219 (let ((items (mime-find-file-type signature-file-name)))
1220 (apply (function mime-edit-insert-tag)
1221 (car items) (cadr items) (list (caddr items))))
1224 (insert-signature arg)
1228 ;; Insert a new tag around a point.
1230 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1231 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1232 If nothing is inserted, return nil."
1235 (mime-edit-goto-tag)
1236 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1237 (< (match-beginning 0) p)
1240 (goto-char (match-beginning 0))
1249 (mime-prompt-for-type)))
1252 (mime-prompt-for-subtype pritype)))
1255 (mime-prompt-for-parameters pritype subtype delimiter)))
1256 ;; Make a new MIME tag.
1257 (setq newtag (mime-make-tag pritype subtype parameters))
1258 ;; Find an current MIME tag.
1261 (if (mime-edit-goto-tag)
1262 (buffer-substring (match-beginning 0) (match-end 0))
1263 ;; Assume content type is 'text/plan'.
1264 (mime-make-tag "text" "plain")
1266 ;; We are only interested in TEXT.
1268 (not (mime-test-content-type
1269 (mime-edit-get-contype oldtag) "text")))
1272 (if (or (not oldtag) ;Not text
1273 (or mime-ignore-same-text-tag
1274 (not (string-equal oldtag newtag))))
1276 ;; Mark the beginning of the tag for convenience.
1277 (push-mark (point) 'nomsg)
1278 (insert newtag "\n")
1279 (list pritype subtype parameters) ;New tag is created.
1281 ;; Restore previous point.
1283 nil ;Nothing is created.
1287 (defun mime-edit-insert-binary-file (file &optional encoding)
1288 "Insert binary FILE at point.
1289 Optional argument ENCODING specifies an encoding method such as base64."
1290 (let* ((tagend (1- (point))) ;End of the tag
1291 (hide-p (and mime-auto-hide-body
1294 (let ((en (downcase encoding)))
1295 (or (string-equal en "7bit")
1296 (string-equal en "8bit")
1297 (string-equal en "binary")
1301 (narrow-to-region tagend (point))
1302 (mime-insert-encoded-file file encoding)
1305 (invisible-region (point-min) (point-max))
1306 (goto-char (point-max))
1308 (goto-char (point-max))
1311 (looking-at mime-edit-tag-regexp)
1312 (= (point)(point-max))
1313 (mime-edit-insert-tag "text" "plain")
1315 ;; Define encoding even if it is 7bit.
1316 (if (stringp encoding)
1318 (goto-char tagend) ; Make sure which line the tag is on.
1319 (mime-edit-define-encoding encoding)
1324 ;; Commands work on a current message flagment.
1326 (defun mime-edit-goto-tag ()
1327 "Search for the beginning of the tagged MIME message."
1328 (let ((current (point)))
1329 (if (looking-at mime-edit-tag-regexp)
1331 ;; At first, go to the end.
1332 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1333 (goto-char (1- (match-beginning 0))) ;For multiline tag
1336 (goto-char (point-max))
1338 ;; Then search for the beginning.
1339 (re-search-backward mime-edit-end-tag-regexp nil t)
1340 (or (looking-at mime-edit-beginning-tag-regexp)
1341 ;; Restore previous point.
1348 (defun mime-edit-content-beginning ()
1349 "Return the point of the beginning of content."
1351 (let ((beg (save-excursion
1352 (beginning-of-line) (point))))
1353 (if (mime-edit-goto-tag)
1354 (let ((top (point)))
1355 (goto-char (match-end 0))
1356 (if (and (= beg top)
1357 (= (following-char) ?\^M))
1361 ;; Default text/plain tag.
1362 (goto-char (point-min))
1364 (concat "\n" (regexp-quote mail-header-separator)
1365 (if mime-ignore-preceding-spaces
1366 "[ \t\n]*\n" "\n")) nil 'move)
1370 (defun mime-edit-content-end ()
1371 "Return the point of the end of content."
1373 (if (mime-edit-goto-tag)
1375 (goto-char (match-end 0))
1376 (if (invisible-p (point))
1377 (next-visible-point (point))
1378 ;; Move to the end of this text.
1379 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1380 ;; Don't forget a multiline tag.
1381 (goto-char (match-beginning 0))
1385 ;; Assume the message begins with text/plain.
1386 (goto-char (mime-edit-content-beginning))
1387 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1388 ;; Don't forget a multiline tag.
1389 (goto-char (match-beginning 0)))
1393 (defun mime-edit-define-charset (charset)
1394 "Set charset of current tag to CHARSET."
1396 (if (mime-edit-goto-tag)
1397 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1398 (delete-region (match-beginning 0) (match-end 0))
1401 (mime-edit-set-parameter
1402 (mime-edit-get-contype tag)
1403 "charset" (upcase (symbol-name charset)))
1404 (mime-edit-get-encoding tag)))
1407 (defun mime-edit-define-encoding (encoding)
1408 "Set encoding of current tag to ENCODING."
1410 (if (mime-edit-goto-tag)
1411 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1412 (delete-region (match-beginning 0) (match-end 0))
1413 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1416 (defun mime-edit-choose-charset ()
1417 "Choose charset of a text following current point."
1418 (detect-mime-charset-region (point) (mime-edit-content-end))
1421 (defun mime-make-text-tag (&optional subtype)
1422 "Make a tag for a text after current point.
1423 Subtype of text type can be specified by an optional argument SUBTYPE.
1424 Otherwise, it is obtained from mime-content-types."
1425 (let* ((pritype "text")
1426 (subtype (or subtype
1427 (car (car (cdr (assoc pritype mime-content-types)))))))
1428 ;; Charset should be defined later.
1429 (mime-make-tag pritype subtype)))
1432 ;; Tag handling functions
1434 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1435 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1436 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1440 (defun mime-create-tag (contype &optional encoding)
1441 "Make a tag with CONTENT-TYPE and optional ENCODING."
1442 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1445 (defun mime-edit-get-contype (tag)
1446 "Return Content-Type (including parameters) of TAG."
1448 (or (string-match mime-edit-single-part-tag-regexp tag)
1449 (string-match mime-edit-multipart-beginning-regexp tag)
1450 (string-match mime-edit-multipart-end-regexp tag)
1452 (substring tag (match-beginning 1) (match-end 1))
1455 (defun mime-edit-get-encoding (tag)
1456 "Return encoding of TAG."
1458 (string-match mime-edit-single-part-tag-regexp tag)
1460 (not (= (match-beginning 3) (match-end 3)))
1461 (substring tag (match-beginning 3) (match-end 3))))
1463 (defun mime-get-parameter (contype parameter)
1464 "For given CONTYPE return value for PARAMETER.
1465 Nil if no such parameter."
1469 (regexp-quote parameter)
1470 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1472 (substring contype (match-beginning 1) (match-end 1))
1473 nil ;No such parameter
1476 (defun mime-edit-set-parameter (contype parameter value)
1477 "For given CONTYPE set PARAMETER to VALUE."
1478 (let (ctype opt-fields)
1479 (if (string-match "\n[^ \t\n\r]+:" contype)
1480 (setq ctype (substring contype 0 (match-beginning 0))
1481 opt-fields (substring contype (match-beginning 0)))
1482 (setq ctype contype)
1487 (regexp-quote parameter)
1488 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1491 (concat (substring ctype 0 (match-beginning 1))
1493 (substring contype (match-end 1))
1495 (concat ctype "; " parameter "=" value opt-fields)
1498 (defun mime-strip-parameters (contype)
1499 "Return primary content-type and subtype without parameters for CONTYPE."
1500 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1501 (substring contype (match-beginning 1) (match-end 1)) nil))
1503 (defun mime-test-content-type (contype type &optional subtype)
1504 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1505 (and (stringp contype)
1508 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1509 (downcase contype))))
1514 (defun mime-find-file-type (file)
1515 "Guess Content-Type, subtype, and parameters from FILE."
1517 (guesses mime-file-types))
1518 (while (and (not guess) guesses)
1519 (if (string-match (car (car guesses)) file)
1520 (setq guess (cdr (car guesses))))
1521 (setq guesses (cdr guesses)))
1525 (defun mime-prompt-for-type (&optional default)
1526 "Ask for Content-type."
1528 ;; Repeat until primary content type is specified.
1529 (while (string-equal type "")
1531 (completing-read "What content type: "
1534 'require-match ;Type must be specified.
1537 (if (string-equal type "")
1539 (message "Content type is required.")
1546 (defun mime-prompt-for-subtype (type &optional default)
1547 "Ask for subtype of media-type TYPE."
1548 (let ((subtypes (cdr (assoc type mime-content-types))))
1550 (assoc default subtypes))
1551 (setq default (car (car subtypes)))
1557 "What content subtype: (default " default ") ")
1558 "What content subtype: ")
1559 (cdr (assoc type mime-content-types))
1561 'require-match ;Subtype must be specified.
1564 (if (string-equal answer "") default answer)))
1566 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1567 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1568 Optional DELIMITER specifies parameter delimiter (';' by default)."
1569 (let* ((delimiter (or delimiter "; "))
1574 (mime-prompt-for-parameters-1
1576 (cdr (assoc pritype mime-content-types))))))
1579 (if (and (stringp parameters)
1580 (not (string-equal parameters "")))
1581 (concat delimiter parameters)
1582 "" ;"" if no parameters
1585 (defun mime-prompt-for-parameters-1 (optlist)
1586 (apply (function append)
1587 (mapcar (function mime-prompt-for-parameter) optlist)))
1589 (defun mime-prompt-for-parameter (parameter)
1591 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1592 (let* ((prompt (car parameter))
1593 (choices (mapcar (function
1595 (if (consp e) e (list e))))
1597 (default (car (car choices)))
1603 (concat "What " prompt
1605 (if (string-equal default "") "\"\"" default)
1607 choices nil nil ""))
1608 ;; If nothing is selected, use default.
1609 (if (string-equal answer "")
1610 (setq answer default)))
1612 (read-string (concat "What " prompt ": "))))
1613 (cons (if (and answer
1614 (not (string-equal answer "")))
1616 ;; Note: control characters ignored!
1617 (if (string-match mime-tspecials-regexp answer)
1618 (concat "\"" answer "\"") answer)))
1619 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1622 (defun mime-prompt-for-encoding (default)
1623 "Ask for Content-Transfer-Encoding."
1628 "What transfer encoding: "
1629 (mime-encoding-alist) nil t default)
1635 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1638 (defvar mime-edit-translate-buffer-hook
1639 '(mime-edit-pgp-enclose-buffer
1640 mime-edit-translate-body
1641 mime-edit-translate-header))
1643 (defun mime-edit-translate-header ()
1644 "Encode the message header into network representation."
1645 (eword-encode-header 'code-conversion)
1646 (run-hooks 'mime-edit-translate-header-hook)
1649 (defun mime-edit-translate-buffer ()
1650 "Encode the tagged MIME message in current buffer in MIME compliant message."
1653 (if (catch 'mime-edit-error
1655 (run-hooks 'mime-edit-translate-buffer-hook)
1659 (error "Translation error!")
1662 (defun mime-edit-find-inmost ()
1663 (goto-char (point-min))
1664 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1665 (let ((bb (match-beginning 0))
1667 (type (buffer-substring (match-beginning 1)(match-end 1)))
1669 (setq end-exp (format "--}-<<%s>>\n" type))
1671 (if (re-search-forward end-exp nil t)
1672 (setq eb (match-beginning 0))
1673 (setq eb (point-max))
1675 (narrow-to-region be eb)
1677 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1679 (narrow-to-region (match-beginning 0)(point-max))
1680 (mime-edit-find-inmost)
1683 (list type bb be eb)
1686 (defun mime-edit-process-multipart-1 (boundary)
1687 (let ((ret (mime-edit-find-inmost)))
1689 (let ((type (car ret))
1690 (bb (nth 1 ret))(be (nth 2 ret))
1693 (narrow-to-region bb eb)
1694 (delete-region bb be)
1695 (setq bb (point-min))
1696 (setq eb (point-max))
1699 (if (looking-at mime-edit-multipart-end-regexp)
1700 (let ((beg (match-beginning 0))
1703 (delete-region beg end)
1704 (or (looking-at mime-edit-beginning-tag-regexp)
1706 (insert (concat (mime-make-text-tag) "\n"))
1708 (cond ((string-equal type "quote")
1709 (mime-edit-enquote-region bb eb)
1711 ((string-equal type "pgp-signed")
1712 (mime-edit-sign-pgp-mime bb eb boundary)
1714 ((string-equal type "pgp-encrypted")
1715 (mime-edit-encrypt-pgp-mime bb eb boundary)
1717 ((string-equal type "kazu-signed")
1718 (mime-edit-sign-pgp-kazu bb eb boundary)
1720 ((string-equal type "kazu-encrypted")
1721 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1723 ((string-equal type "smime-signed")
1724 (mime-edit-sign-smime bb eb boundary)
1726 ((string-equal type "smime-encrypted")
1727 (mime-edit-encrypt-smime bb eb boundary)
1731 (nth 2 (mime-edit-translate-region bb eb
1735 (format "--[[multipart/%s;
1736 boundary=\"%s\"][7bit]]\n"
1741 (defun mime-edit-enquote-region (beg end)
1744 (narrow-to-region beg end)
1746 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1747 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1748 (replace-match (concat "- " (substring tag 1)))
1751 (defun mime-edit-dequote-region (beg end)
1754 (narrow-to-region beg end)
1756 (while (re-search-forward
1757 mime-edit-quoted-single-part-tag-regexp nil t)
1758 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1759 (replace-match (concat "-" (substring tag 2)))
1762 (defun mime-edit-sign-pgp-mime (beg end boundary)
1765 (let* ((from (std11-field-body "From" mail-header-separator))
1767 (narrow-to-region beg end)
1768 (mime-edit-translate-region beg end boundary)))
1770 (encoding (nth 1 ret))
1771 (pgp-boundary (concat "pgp-sign-" boundary))
1774 (insert (format "Content-Type: %s\n" ctype))
1776 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1779 (or (let ((pgg-default-user-id
1780 (or mime-edit-pgp-user-id
1782 (nth 1 (std11-extract-address-components from))
1783 pgg-default-user-id))))
1784 (pgg-sign-region (point-min)(point-max)))
1785 (throw 'mime-edit-error 'pgp-error)
1788 (cdr (assq 'hash-algorithm
1789 (cdar (with-current-buffer pgg-output-buffer
1790 (pgg-parse-armor-region
1791 (point-min)(point-max))))))
1794 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1797 (insert (format "--[[multipart/signed;
1799 protocol=\"application/pgp-signature\"][7bit]]
1801 " pgp-boundary micalg pgp-boundary))
1802 (goto-char (point-max))
1803 (insert (format "\n--%s
1804 Content-Type: application/pgp-signature
1805 Content-Transfer-Encoding: 7bit
1808 (insert-buffer-substring pgg-output-buffer)
1809 (goto-char (point-max))
1810 (insert (format "\n--%s--\n" pgp-boundary))
1813 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1815 (defun mime-edit-make-encrypt-recipient-header ()
1816 (let* ((names mime-edit-encrypt-recipient-fields-list)
1818 (std11-field-bodies (cons "From" names)
1819 nil mail-header-separator))
1822 (setq values (cdr values))))
1823 (header (and (stringp from)
1824 (if (string-equal from "")
1826 (format "From: %s\n" from)
1829 (while (and names values)
1830 (let ((name (car names))
1831 (value (car values))
1833 (and (stringp value)
1834 (or (string-equal value "")
1836 (setq header (concat header name ": " value "\n")
1837 recipients (if recipients
1838 (concat recipients " ," value)
1841 (setq names (cdr names)
1842 values (cdr values))
1844 (vector from recipients header)
1847 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1850 (let (from recipients header)
1851 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1852 (setq from (aref ret 0)
1853 recipients (aref ret 1)
1854 header (aref ret 2))
1856 (narrow-to-region beg end)
1858 (mime-edit-translate-region beg end boundary))
1860 (encoding (nth 1 ret))
1861 (pgp-boundary (concat "pgp-" boundary)))
1864 (insert (format "Content-Type: %s\n" ctype))
1866 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1869 (eword-encode-header)
1870 (or (let ((pgg-default-user-id
1871 (or mime-edit-pgp-user-id
1873 (nth 1 (std11-extract-address-components from))
1874 pgg-default-user-id))))
1876 (point-min) (point-max)
1877 (mapcar (lambda (recipient)
1878 (nth 1 (std11-extract-address-components
1880 (split-string recipients
1881 "\\([ \t\n]*,[ \t\n]*\\)+")))
1883 (throw 'mime-edit-error 'pgp-error)
1885 (delete-region (point-min)(point-max))
1887 (insert (format "--[[multipart/encrypted;
1889 protocol=\"application/pgp-encrypted\"][7bit]]
1891 Content-Type: application/pgp-encrypted
1894 Content-Type: application/octet-stream
1895 Content-Transfer-Encoding: 7bit
1897 " pgp-boundary pgp-boundary pgp-boundary))
1898 (insert-buffer-substring pgg-output-buffer)
1899 (goto-char (point-max))
1900 (insert (format "\n--%s--\n" pgp-boundary))
1903 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1906 (narrow-to-region beg end)
1908 (mime-edit-translate-region beg end boundary))
1910 (encoding (nth 1 ret)))
1912 (insert (format "Content-Type: %s\n" ctype))
1914 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1917 (or (pgg-sign-region beg (point-max) 'clearsign)
1918 (throw 'mime-edit-error 'pgp-error)
1922 "--[[application/pgp; format=mime][7bit]]\n")
1926 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1928 (let (recipients header)
1929 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1930 (setq recipients (aref ret 1)
1931 header (aref ret 2))
1934 (narrow-to-region beg end)
1936 (mime-edit-translate-region beg end boundary))
1938 (encoding (nth 1 ret)))
1941 (insert (format "Content-Type: %s\n" ctype))
1943 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1946 (or (pgg-encrypt-region beg (point-max) recipients)
1947 (throw 'mime-edit-error 'pgp-error)
1951 "--[[application/pgp; format=mime][7bit]]\n")
1955 (defun mime-edit-sign-smime (beg end boundary)
1959 (narrow-to-region beg end)
1960 (mime-edit-translate-region beg end boundary)))
1962 (encoding (nth 1 ret))
1963 (smime-boundary (concat "smime-sign-" boundary)))
1965 (insert (format "Content-Type: %s\n" ctype))
1967 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1970 (let (buffer-undo-list)
1971 (goto-char (point-min))
1972 (while (progn (end-of-line) (not (eobp)))
1975 (or (prog1 (smime-sign-region (point-min)(point-max))
1976 (push nil buffer-undo-list)
1977 (ignore-errors (undo)))
1978 (throw 'mime-edit-error 'pgp-error)
1981 (insert (format "--[[multipart/signed;
1982 boundary=\"%s\"; micalg=sha1;
1983 protocol=\"application/pkcs7-signature\"][7bit]]
1985 " smime-boundary smime-boundary))
1986 (goto-char (point-max))
1987 (insert (format "\n--%s
1988 Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
1989 Content-Transfer-Encoding: base64
1990 Content-Disposition: attachment; filename=\"smime.p7s\"
1991 Content-Description: S/MIME Cryptographic Signature
1994 (insert-buffer-substring smime-output-buffer)
1995 (goto-char (point-max))
1996 (insert (format "\n--%s--\n" smime-boundary))
1999 (defun mime-edit-encrypt-smime (beg end boundary)
2003 (narrow-to-region beg end)
2004 (mime-edit-translate-region beg end boundary)))
2006 (encoding (nth 1 ret)))
2008 (insert (format "Content-Type: %s\n" ctype))
2010 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2013 (goto-char (point-min))
2014 (while (progn (end-of-line) (not (eobp)))
2017 (or (smime-encrypt-region (point-min)(point-max))
2018 (throw 'mime-edit-error 'pgp-error)
2020 (delete-region (point-min)(point-max))
2021 (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
2022 Content-Disposition: attachment; filename=\"smime.p7m\"
2023 Content-Description: S/MIME Encrypted Message][base64]]\n")
2024 (insert-buffer-substring smime-output-buffer)
2027 (defsubst replace-space-with-underline (str)
2028 (mapconcat (function
2036 (defun mime-edit-make-boundary ()
2037 (concat mime-multipart-boundary "_"
2038 (replace-space-with-underline (current-time-string))
2041 (defun mime-edit-translate-body ()
2042 "Encode the tagged MIME body in current buffer in MIME compliant message."
2045 (let ((boundary (mime-edit-make-boundary))
2048 (while (mime-edit-process-multipart-1
2049 (format "%s-%d" boundary i))
2053 ;; We are interested in message body.
2056 (goto-char (point-min))
2058 (concat "\n" (regexp-quote mail-header-separator)
2059 (if mime-ignore-preceding-spaces
2060 "[ \t\n]*\n" "\n")) nil 'move)
2064 (goto-char (point-max))
2065 (and mime-ignore-trailing-spaces
2066 (re-search-backward "[^ \t\n]\n" beg t)
2069 (setq ret (mime-edit-translate-region
2071 (format "%s-%d" boundary i)))
2073 (mime-edit-dequote-region (point-min)(point-max))
2074 (let ((contype (car ret)) ;Content-Type
2075 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
2077 ;; Insert User-Agent field
2078 (and mime-edit-insert-user-agent-field
2079 (or (mail-position-on-field "User-Agent")
2080 (insert mime-edit-user-agent-value)
2082 ;; Make primary MIME headers.
2083 (or (mail-position-on-field "MIME-Version")
2084 (insert mime-edit-mime-version-value))
2085 ;; Remove old Content-Type and other fields.
2087 (goto-char (point-min))
2088 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2089 (narrow-to-region (point-min) (point))
2090 (goto-char (point-min))
2091 (mime-delete-field "Content-Type")
2092 (mime-delete-field "Content-Transfer-Encoding"))
2093 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2094 (mail-position-on-field "Content-Type")
2098 (mail-position-on-field "Content-Transfer-Encoding")
2102 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2103 "Translate single-part-tag to MIME header."
2104 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2105 (let* ((beg (match-beginning 0))
2107 (tag (buffer-substring beg end)))
2108 (delete-region beg end)
2109 (let ((contype (mime-edit-get-contype tag))
2110 (encoding (mime-edit-get-encoding tag)))
2111 (insert (concat prefix "--" boundary "\n"))
2113 (narrow-to-region (point)(point))
2114 (insert "Content-Type: " contype "\n")
2116 (insert "Content-Transfer-Encoding: " encoding "\n"))
2117 (eword-encode-header)
2122 (downcase encoding))))
2125 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2127 (setq boundary (mime-edit-make-boundary))
2131 (narrow-to-region beg end)
2132 (let ((tag nil) ;MIME tag
2133 (contype nil) ;Content-Type
2134 (encoding nil) ;Content-Transfer-Encoding
2135 (nparts 0)) ;Number of body parts
2136 ;; Normalize the body part by inserting appropriate message
2137 ;; tags for every message contents.
2138 (mime-edit-normalize-body)
2139 ;; Counting the number of Content-Type.
2140 (goto-char (point-min))
2141 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2142 (setq nparts (1+ nparts)))
2143 ;; Begin translation.
2145 ((and (<= nparts 1)(not multipart))
2146 ;; It's a singular message.
2147 (goto-char (point-min))
2148 (while (re-search-forward
2149 mime-edit-single-part-tag-regexp nil t)
2151 (buffer-substring (match-beginning 0) (match-end 0)))
2152 (delete-region (match-beginning 0) (1+ (match-end 0)))
2153 (setq contype (mime-edit-get-contype tag))
2154 (setq encoding (mime-edit-get-encoding tag))
2157 ;; It's a multipart message.
2158 (goto-char (point-min))
2159 (let ((prio mime-content-transfer-encoding-priority-list)
2161 (when (setq part-info
2162 (mime-edit-translate-single-part-tag boundary))
2163 (and (setq nprio (member (cdr part-info) prio))
2165 (while (setq part-info
2166 (mime-edit-translate-single-part-tag boundary "\n"))
2167 (and (setq nprio (member (cdr part-info) prio))
2168 (setq prio nprio))))
2169 ;; Define Content-Type as "multipart/mixed".
2171 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2172 (setq encoding (car prio))
2173 ;; Insert the trailer.
2174 (goto-char (point-max))
2175 (insert "\n--" boundary "--\n")
2177 (list contype encoding boundary nparts)
2180 (defun mime-edit-normalize-body ()
2181 "Normalize the body part by inserting appropriate message tags."
2182 ;; Insert the first MIME tags if necessary.
2183 (goto-char (point-min))
2184 (if (not (looking-at mime-edit-single-part-tag-regexp))
2185 (insert (mime-make-text-tag) "\n"))
2186 ;; Check each tag, and add new tag or correct it if necessary.
2187 (goto-char (point-min))
2188 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2189 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2190 (contype (mime-edit-get-contype tag))
2191 (charset (mime-get-parameter contype "charset"))
2192 (encoding (mime-edit-get-encoding tag)))
2193 ;; Remove extra whitespaces after the tag.
2194 (if (looking-at "[ \t]+$")
2195 (delete-region (match-beginning 0) (match-end 0)))
2197 (end (mime-edit-content-end))
2199 (if (= end (point-max))
2202 (or (looking-at mime-edit-beginning-tag-regexp)
2204 (insert (mime-make-text-tag) "\n")
2206 (visible-region beg end)
2210 ((mime-test-content-type contype "message")
2211 ;; Content-type "message" should be sent as is.
2214 ((mime-test-content-type contype "text")
2215 ;; Define charset for text if necessary.
2216 (setq charset (if charset
2217 (intern (downcase charset))
2218 (mime-edit-choose-charset)))
2219 (mime-edit-define-charset charset)
2220 (cond ((string-equal contype "text/x-rot13-47-48")
2223 (mule-caesar-region (point) (mime-edit-content-end))
2225 ((string-equal contype "text/enriched")
2230 (end (mime-edit-content-end))
2232 ;; Patch for hard newlines
2235 ;; (while (search-forward "\n" end t)
2236 ;; (put-text-property (match-beginning 0)
2239 ;; End patch for hard newlines
2240 (enriched-encode beg end)
2242 (if (search-forward "\n\n")
2243 (delete-region beg (match-end 0))
2246 ;; Point is now on current tag.
2247 ;; Define encoding and encode text if necessary.
2248 (or encoding ;Encoding is not specified.
2251 (let ((ret (cdr (assq charset mime-charset-type-list))))
2253 (setq bits (car ret)
2256 conv "quoted-printable")))
2257 (if (<= bits mime-transfer-level)
2258 (mime-encoding-name bits)
2260 (beg (mime-edit-content-beginning)))
2261 (encode-mime-charset-region beg (mime-edit-content-end)
2263 ;; Protect "From " in beginning of line
2265 (narrow-to-region beg (mime-edit-content-end))
2267 (let (case-fold-search)
2268 (if (re-search-forward "^From " nil t)
2270 (if (memq charset '(iso-2022-jp
2275 (replace-match "\e(BFrom ")
2276 (re-search-forward "^From " nil t)
2278 (setq encoding "quoted-printable")
2280 ;; canonicalize line break code
2281 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2283 (narrow-to-region beg (mime-edit-content-end))
2285 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2286 (replace-match "\\1\r\n"))))
2288 (mime-encode-region beg (mime-edit-content-end)
2289 (or encoding "7bit"))
2290 (mime-edit-define-encoding encoding)
2292 (goto-char (mime-edit-content-end))
2294 ((null encoding) ;Encoding is not specified.
2295 ;; Application, image, audio, video, and any other
2296 ;; unknown content-type without encoding should be
2298 (let* ((encoding "base64") ;Encode in BASE64 by default.
2299 (beg (mime-edit-content-beginning))
2300 (end (mime-edit-content-end)))
2301 (mime-encode-region beg end encoding)
2302 (mime-edit-define-encoding encoding))
2307 (defun mime-delete-field (field)
2308 "Delete header FIELD."
2309 (let ((regexp (format "^%s:[ \t]*" field)))
2310 (goto-char (point-min))
2311 (while (re-search-forward regexp nil t)
2312 (delete-region (match-beginning 0)
2313 (1+ (std11-field-end))))))
2317 ;;; Platform dependent functions
2320 ;; Sun implementations
2322 (defun mime-edit-voice-recorder-for-sun (encoding)
2323 "Record voice in a buffer using Sun audio device,
2324 and insert data encoded as ENCODING."
2325 (message "Start the recording on %s. Type C-g to finish the recording..."
2327 (mime-insert-encoded-file "/dev/audio" encoding)
2331 ;;; @ Other useful commands.
2334 ;; Message forwarding commands as content-type "message/rfc822".
2336 (defun mime-edit-insert-message (&optional message)
2338 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2339 (if (and inserter (fboundp inserter))
2341 (mime-edit-insert-tag "message" "rfc822")
2342 (funcall inserter message)
2344 (message "Sorry, I don't have message inserter for your MUA.")
2347 (defun mime-edit-insert-mail (&optional message)
2349 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2350 (if (and inserter (fboundp inserter))
2352 (mime-edit-insert-tag "message" "rfc822")
2353 (funcall inserter message)
2355 (message "Sorry, I don't have mail inserter for your MUA.")
2358 (defun mime-edit-inserted-message-filter ()
2361 (let ((header-start (point))
2362 (case-fold-search t)
2365 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2366 (if (re-search-forward "^$" (mark t))
2367 (narrow-to-region header-start (match-beginning 0))
2369 (goto-char header-start)
2370 (while (and (re-search-forward
2371 mime-edit-yank-ignored-field-regexp nil t)
2372 (setq beg (match-beginning 0))
2373 (setq end (1+ (std11-field-end)))
2375 (delete-region beg end)
2380 ;;; @ multipart enclosure
2383 (defun mime-edit-enclose-region-internal (type beg end)
2387 (narrow-to-region beg end)
2388 (insert (format "--<<%s>>-{\n" type))
2389 (goto-char (point-max))
2390 (insert (format "--}-<<%s>>\n" type))
2391 (goto-char (point-max))
2393 (or (looking-at mime-edit-beginning-tag-regexp)
2395 (insert (mime-make-text-tag) "\n")
2399 (defun mime-edit-enclose-quote-region (beg end)
2401 (mime-edit-enclose-region-internal 'quote beg end)
2404 (defun mime-edit-enclose-mixed-region (beg end)
2406 (mime-edit-enclose-region-internal 'mixed beg end)
2409 (defun mime-edit-enclose-parallel-region (beg end)
2411 (mime-edit-enclose-region-internal 'parallel beg end)
2414 (defun mime-edit-enclose-digest-region (beg end)
2416 (mime-edit-enclose-region-internal 'digest beg end)
2419 (defun mime-edit-enclose-alternative-region (beg end)
2421 (mime-edit-enclose-region-internal 'alternative beg end)
2424 (defun mime-edit-enclose-pgp-signed-region (beg end)
2426 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2429 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2431 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2434 (defun mime-edit-enclose-kazu-signed-region (beg end)
2436 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2439 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2441 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2444 (defun mime-edit-enclose-smime-signed-region (beg end)
2446 (mime-edit-enclose-region-internal 'smime-signed beg end)
2449 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2451 (mime-edit-enclose-region-internal 'smime-encrypted beg end)
2454 (defun mime-edit-insert-key (&optional arg)
2455 "Insert a pgp public key."
2457 (mime-edit-insert-tag "application" "pgp-keys")
2458 (mime-edit-define-encoding "7bit")
2466 (defun mime-edit-set-split (arg)
2469 (y-or-n-p "Do you want to enable split? ")
2471 (setq mime-edit-split-message arg)
2473 (message "This message is enabled to split.")
2474 (message "This message is not enabled to split.")
2477 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2478 "Toggle transfer-level is 7bit or 8bit through.
2480 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2482 (if (numberp transfer-level)
2483 (setq mime-transfer-level transfer-level)
2484 (if (< mime-transfer-level 8)
2485 (setq mime-transfer-level 8)
2486 (setq mime-transfer-level 7)
2488 (message (format "Current transfer-level is %d bit"
2489 mime-transfer-level))
2490 (setq mime-transfer-level-string
2491 (mime-encoding-name mime-transfer-level 'not-omit))
2492 (force-mode-line-update)
2495 (defun mime-edit-set-transfer-level-7bit ()
2497 (mime-edit-toggle-transfer-level 7)
2500 (defun mime-edit-set-transfer-level-8bit ()
2502 (mime-edit-toggle-transfer-level 8)
2509 (defvar mime-edit-pgp-processing nil)
2510 (make-variable-buffer-local 'mime-edit-pgp-processing)
2512 (defvar mime-edit-pgp-user-id nil)
2514 (defun mime-edit-set-sign (arg)
2517 (y-or-n-p "Do you want to sign? ")
2521 (or (memq 'sign mime-edit-pgp-processing)
2522 (setq mime-edit-pgp-processing
2523 (nconc mime-edit-pgp-processing
2524 (copy-sequence '(sign)))))
2525 (message "This message will be signed.")
2527 (setq mime-edit-pgp-processing
2528 (delq 'sign mime-edit-pgp-processing))
2529 (message "This message will not be signed.")
2532 (defun mime-edit-set-encrypt (arg)
2535 (y-or-n-p "Do you want to encrypt? ")
2539 (or (memq 'encrypt mime-edit-pgp-processing)
2540 (setq mime-edit-pgp-processing
2541 (nconc mime-edit-pgp-processing
2542 (copy-sequence '(encrypt)))))
2543 (message "This message will be encrypt.")
2545 (setq mime-edit-pgp-processing
2546 (delq 'encrypt mime-edit-pgp-processing))
2547 (message "This message will not be encrypt.")
2550 (defun mime-edit-pgp-enclose-buffer ()
2551 (let ((beg (save-excursion
2552 (goto-char (point-min))
2553 (if (search-forward (concat "\n" mail-header-separator "\n"))
2558 (dolist (pgp-processing mime-edit-pgp-processing)
2559 (case pgp-processing
2561 (mime-edit-enclose-pgp-signed-region
2565 (mime-edit-enclose-pgp-encrypted-region
2574 (defun mime-edit-insert-partial-header (fields subject
2575 id number total separator)
2577 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2578 (insert mime-edit-mime-version-field-for-message/partial)
2580 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2581 id number total separator))
2584 (defun mime-edit-split-and-send
2585 (&optional cmd lines mime-edit-message-max-length)
2589 (count-lines (point-min) (point-max)))
2591 (or mime-edit-message-max-length
2592 (setq mime-edit-message-max-length
2593 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2594 mime-edit-message-default-max-lines))
2596 (let* ((mime-edit-draft-file-name
2597 (or (buffer-file-name)
2599 (expand-file-name "mime-draft" temporary-file-directory))))
2600 (separator mail-header-separator)
2602 (replace-space-with-underline (current-time-string))
2603 "@" (system-name) "\"")))
2604 (run-hooks 'mime-edit-before-split-hook)
2605 (let ((the-buf (current-buffer))
2606 (copy-buf (get-buffer-create " *Original Message*"))
2607 (header (std11-header-string-except
2608 mime-edit-split-ignored-field-regexp separator))
2609 (subject (mail-fetch-field "subject"))
2610 (total (+ (/ lines mime-edit-message-max-length)
2611 (if (> (mod lines mime-edit-message-max-length) 0)
2617 mime-edit-split-message-sender-alist))
2621 (error "Split sender is not specified for `%s'." major-mode)
2624 (mime-edit-partial-number 1)
2627 (set-buffer copy-buf)
2629 (insert-buffer the-buf)
2631 (if (re-search-forward
2632 (concat "^" (regexp-quote separator) "$") nil t)
2633 (let ((he (match-beginning 0)))
2635 (narrow-to-region (point-min) he)
2637 (goto-char (point-min))
2638 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2639 (delete-region (match-beginning 0)
2640 (1+ (std11-field-end)))
2642 (while (< mime-edit-partial-number total)
2645 (set-buffer copy-buf)
2646 (setq data (buffer-substring
2649 (goto-line mime-edit-message-max-length)
2652 (delete-region (point-min)(point))
2654 (mime-edit-insert-partial-header
2655 header subject id mime-edit-partial-number total separator)
2658 (message (format "Sending %d/%d..."
2659 mime-edit-partial-number total))
2660 (call-interactively command)
2661 (message (format "Sending %d/%d... done"
2662 mime-edit-partial-number total))
2664 (setq mime-edit-partial-number
2665 (1+ mime-edit-partial-number))
2669 (set-buffer copy-buf)
2670 (setq data (buffer-string))
2673 (mime-edit-insert-partial-header
2674 header subject id mime-edit-partial-number total separator)
2677 (message (format "Sending %d/%d..."
2678 mime-edit-partial-number total))
2679 (message (format "Sending %d/%d... done"
2680 mime-edit-partial-number total))
2684 (defun mime-edit-maybe-split-and-send (&optional cmd)
2686 (run-hooks 'mime-edit-before-send-hook)
2687 (let ((mime-edit-message-max-length
2688 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2689 mime-edit-message-default-max-lines))
2690 (lines (count-lines (point-min) (point-max)))
2692 (if (and (> lines mime-edit-message-max-length)
2693 mime-edit-split-message)
2694 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2698 ;;; @ preview message
2701 (defvar mime-edit-buffer nil) ; buffer local variable
2703 (defun mime-edit-preview-message ()
2704 "preview editing MIME message."
2706 (let* ((str (buffer-string))
2707 (separator mail-header-separator)
2708 (the-buf (current-buffer))
2709 (buf-name (buffer-name))
2710 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2711 (buf (get-buffer temp-buf-name))
2712 (pgp-processing mime-edit-pgp-processing)
2716 (switch-to-buffer buf)
2719 (setq buf (get-buffer-create temp-buf-name))
2720 (switch-to-buffer buf)
2723 (setq major-mode 'mime-temp-message-mode)
2724 (make-local-variable 'mail-header-separator)
2725 (setq mail-header-separator separator)
2726 (make-local-variable 'mime-edit-buffer)
2727 (setq mime-edit-buffer the-buf)
2728 (setq mime-edit-pgp-processing pgp-processing)
2730 (run-hooks 'mime-edit-translate-hook)
2731 (mime-edit-translate-buffer)
2732 (goto-char (point-min))
2733 (if (re-search-forward
2734 (concat "^" (regexp-quote separator) "$"))
2740 (defun mime-edit-quitting-method ()
2741 "Quitting method for mime-view."
2742 (let* ((entity (get-text-property (point-min) 'mime-view-entity))
2743 (temp (mime-entity-buffer entity))
2745 (mime-preview-kill-buffer)
2747 (setq buf mime-edit-buffer)
2749 (switch-to-buffer buf)
2752 (set-alist 'mime-preview-quitting-method-alist
2753 'mime-temp-message-mode
2754 #'mime-edit-quitting-method)
2760 (defvar mime-edit-again-ignored-field-regexp
2761 (concat "^\\(" "Content-.*\\|Mime-Version"
2762 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2764 "Regexp for deleted header fields when `mime-edit-again' is called.")
2766 (defsubst eliminate-top-spaces (string)
2767 "Eliminate top sequence of space or tab in STRING."
2768 (if (string-match "^[ \t]+" string)
2769 (substring string (match-end 0))
2772 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2775 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2776 '(("application/pgp-encrypted" . pgp-encrypted)
2777 ("application/pgp-signature" . pgp-signed))))
2778 (mime-content-type-subtype content-type)))
2779 (boundary (mime-content-type-parameter content-type "boundary"))
2780 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2781 (re-search-forward boundary-pat nil t)
2782 (let ((bb (match-beginning 0)) eb tag)
2783 (setq tag (format "\n--<<%s>>-{\n" subtype))
2786 (setq bb (+ bb (length tag)))
2788 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2790 (setq eb (match-beginning 0))
2791 (replace-match (format "--}-<<%s>>\n" subtype))
2793 (narrow-to-region bb eb)
2794 (goto-char (point-min))
2795 (while (re-search-forward boundary-pat nil t)
2796 (let ((beg (match-beginning 0))
2798 (delete-region beg (match-end 0))
2800 (if (re-search-forward boundary-pat nil t)
2801 (setq end (match-beginning 0))
2802 (setq end (point-max))
2805 (narrow-to-region beg end)
2807 ((eq subtype 'pgp-encrypted)
2810 (goto-char (point-min))
2811 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2814 (save-window-excursion
2815 (pgg-decrypt-region (match-beginning 0)
2817 (delete-region (point-min)(point-max))))
2818 (insert-buffer-substring pgg-output-buffer)
2819 (mime-edit-decode-message-in-buffer
2820 nil not-decode-text)
2821 (delete-region (goto-char (point-min))
2822 (if (search-forward "\n\n" nil t)
2825 (goto-char (point-max))
2828 (mime-edit-decode-message-in-buffer
2829 (if (eq subtype 'digest)
2831 (make-mime-content-type 'message 'rfc822))
2834 (goto-char (point-max))
2838 (goto-char (point-min))
2839 (or (= (point-min) 1)
2840 (delete-region (point-min)
2841 (if (search-forward "\n\n" nil t)
2847 (defun mime-edit-decode-single-part-in-buffer
2848 (content-type not-decode-text &optional content-disposition)
2849 (let* ((type (mime-content-type-primary-type content-type))
2850 (subtype (mime-content-type-subtype content-type))
2851 (ctype (format "%s/%s" type subtype))
2853 (pstr (let ((bytes (+ 14 (length ctype))))
2854 (mapconcat (function
2856 (if (string= (car attr) "charset")
2858 (setq charset (cdr attr))
2860 (let* ((str (concat (car attr)
2863 (setq bytes (+ bytes bs 2))
2866 (setq bytes (+ bs 1))
2870 (mime-content-type-parameters content-type) "")))
2873 (limit (save-excursion
2874 (if (search-forward "\n\n" nil t)
2877 (mime-content-disposition-type content-disposition))
2879 (if disposition-type
2880 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2881 (mapconcat (function
2886 (if (string-equal "filename"
2888 (std11-wrap-as-quoted-string
2892 (setq bytes (+ bytes bs 2))
2895 (setq bytes (+ bs 1))
2899 (mime-content-disposition-parameters
2900 content-disposition)
2903 (if disposition-type
2904 (setq pstr (format "%s\nContent-Disposition: %s%s"
2905 pstr disposition-type disposition-str))
2908 (if (re-search-forward
2909 "^Content-Transfer-Encoding:" limit t)
2910 (let ((beg (match-beginning 0))
2911 (hbeg (match-end 0))
2912 (end (std11-field-end limit)))
2915 (eliminate-top-spaces
2916 (std11-unfold-string
2917 (buffer-substring hbeg end)))))
2918 (if (or charset (eq type 'text))
2920 (delete-region beg (1+ end))
2921 (goto-char (point-min))
2922 (if (search-forward "\n\n" nil t)
2925 (match-end 0)(point-max) encoding)
2929 (if (or encoded (not not-decode-text))
2932 (goto-char (point-min))
2933 (while (re-search-forward "\r\n" nil t)
2934 (replace-match "\n")
2936 (decode-mime-charset-region (point-min)(point-max)
2937 (or charset default-mime-charset))
2939 (let ((he (if (re-search-forward "^$" nil t)
2943 (if (and (eq type 'text)
2944 (eq subtype 'x-rot13-47-48))
2945 (mule-caesar-region he (point-max))
2947 (if (= (point-min) 1)
2953 (format "%s/%s%s" type subtype pstr)
2956 (delete-region (point-min) he)
2958 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2964 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2967 (goto-char (point-min))
2968 (let ((ctl (or (mime-read-Content-Type)
2969 default-content-type)))
2971 (let ((type (mime-content-type-primary-type ctl)))
2973 ((and (eq type 'application)
2974 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2975 (delete-region (point-min)(point-max))
2977 ((eq type 'multipart)
2978 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
2981 (mime-edit-decode-single-part-in-buffer
2982 ctl not-decode-text (mime-read-Content-Disposition))
2985 (decode-mime-charset-region (point-min) (point-max)
2986 default-mime-charset))
2988 (if (= (point-min) 1)
2991 (std11-narrow-to-header)
2992 (goto-char (point-min))
2993 (while (re-search-forward
2994 mime-edit-again-ignored-field-regexp nil t)
2995 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2997 (mime-decode-header-in-buffer (not not-decode-text))
3002 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
3003 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
3004 Content-Type and Content-Transfer-Encoding header fields will be
3005 converted to MIME-Edit tags."
3007 (goto-char (point-min))
3009 (concat "\n" (regexp-quote mail-header-separator) "\n")
3011 (replace-match "\n\n")
3013 (mime-edit-decode-message-in-buffer nil not-decode-text)
3014 (goto-char (point-min))
3016 (and (re-search-forward "^$")
3017 (replace-match mail-header-separator)
3027 (provide 'mime-edit)
3029 (run-hooks 'mime-edit-load-hook)
3031 ;;; mime-edit.el ends here