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)
652 (concat (apel-version) " "))
654 (if (featurep 'xemacs)
655 (concat (cond ((featurep 'utf-2000)
656 (concat "UTF-2000-MULE/" utf-2000-version))
657 ((featurep 'mule) "MULE"))
659 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
662 (substring emacs-version 0 (match-end 0))
663 (cond ((and (boundp 'xemacs-betaname)
665 ;; It does not exist in XEmacs
666 ;; versions prior to 20.3.
667 (concat " " xemacs-betaname))
668 ((and (boundp 'emacs-patch-level)
670 ;; It does not exist in FSF Emacs or in
671 ;; XEmacs versions earlier than 21.1.1.
672 (format " (patch %d)" emacs-patch-level))
674 " (" xemacs-codename ") ("
675 system-configuration ")")
676 " (" emacs-version ")"))
677 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
678 (substring emacs-version 0 (match-beginning 0))
681 (if (boundp 'enable-multibyte-characters)
683 " (" system-configuration ")"
684 (if enable-multibyte-characters
685 (concat " MULE/" mule-version)
686 " (with unibyte mode)")
687 (if (featurep 'meadow)
688 (let ((mver (Meadow-version)))
689 (if (string-match "^Meadow-" mver)
694 (concat "MULE/" mule-version
695 " (based on Emacs " ver ")"))
696 (concat "Emacs/" ver " (" system-configuration ")")))))
697 "Body of User-Agent field.
698 If variable `mime-edit-insert-user-agent-field' is not nil, it is
699 inserted into message header.")
705 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
706 "*Specify MIME tspecials.
707 Tspecials means any character that matches with it in header must be quoted.")
709 (defconst mime-edit-mime-version-value
710 (concat "1.0 (generated by " mime-edit-version ")")
711 "MIME version number.")
713 (defconst mime-edit-mime-version-field-for-message/partial
714 (concat "MIME-Version:"
715 (eword-encode-field-body
716 (concat " 1.0 (split by " mime-edit-version ")\n")
718 "MIME version field for message/partial.")
721 ;;; @ keymap and menu
724 (defvar mime-edit-mode-flag nil)
725 (make-variable-buffer-local 'mime-edit-mode-flag)
727 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
728 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
729 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
730 "Keymap for MIME-Edit mode commands to insert entity or set status.")
732 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
733 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
734 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
735 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
736 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
737 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
738 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
739 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
740 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
741 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
743 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
744 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
745 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
746 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
747 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
748 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
749 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
750 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
751 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
752 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
754 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
755 "Keymap prefix for MIME-Edit mode commands about enclosure.")
756 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
757 "Keymap for MIME-Edit mode commands about enclosure.")
759 (define-key mime-edit-mode-enclosure-map
760 "\C-a" 'mime-edit-enclose-alternative-region)
761 (define-key mime-edit-mode-enclosure-map
762 "\C-p" 'mime-edit-enclose-parallel-region)
763 (define-key mime-edit-mode-enclosure-map
764 "\C-m" 'mime-edit-enclose-mixed-region)
765 (define-key mime-edit-mode-enclosure-map
766 "\C-d" 'mime-edit-enclose-digest-region)
767 (define-key mime-edit-mode-enclosure-map
768 "\C-s" 'mime-edit-enclose-pgp-signed-region)
769 (define-key mime-edit-mode-enclosure-map
770 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
771 (define-key mime-edit-mode-enclosure-map
772 "\C-q" 'mime-edit-enclose-quote-region)
774 (defvar mime-edit-mode-map (make-sparse-keymap)
775 "Keymap for MIME-Edit mode commands.")
776 (define-key mime-edit-mode-map
777 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
778 (define-key mime-edit-mode-map
779 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
781 (defconst mime-edit-menu-title "MIME-Edit")
783 (defconst mime-edit-menu-list
784 '((mime-help "Describe MIME editor mode" mime-edit-help)
785 (file "Insert File" mime-edit-insert-file)
786 (external "Insert External" mime-edit-insert-external)
787 (voice "Insert Voice" mime-edit-insert-voice)
788 (message "Insert Message" mime-edit-insert-message)
789 (mail "Insert Mail" mime-edit-insert-mail)
790 (signature "Insert Signature" mime-edit-insert-signature)
791 (text "Insert Text" mime-edit-insert-text)
792 (tag "Insert Tag" mime-edit-insert-tag)
793 (alternative "Enclose as alternative"
794 mime-edit-enclose-alternative-region)
795 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
796 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
797 (digest "Enclose as digest" mime-edit-enclose-digest-region)
798 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
799 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
800 (quote "Verbatim region" mime-edit-enclose-quote-region)
801 (key "Insert Public Key" mime-edit-insert-key)
802 (split "About split" mime-edit-set-split)
803 (sign "About sign" mime-edit-set-sign)
804 (encrypt "About encryption" mime-edit-set-encrypt)
805 (preview "Preview Message" mime-edit-preview-message)
806 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
808 "MIME-edit menubar entry.")
810 (cond ((featurep 'xemacs)
811 ;; modified by Pekka Marjola <pema@iki.fi>
812 ;; 1995/9/5 (c.f. [tm-en:69])
813 (defun mime-edit-define-menu-for-xemacs ()
814 "Define menu for XEmacs."
815 (cond ((featurep 'menubar)
816 (make-local-variable 'current-menubar)
817 (set-buffer-menubar current-menubar)
820 (cons mime-edit-menu-title
823 (vector (nth 1 item)(nth 2 item)
826 mime-edit-menu-list)))
829 ;; modified by Steven L. Baur <steve@miranova.com>
830 ;; 1995/12/6 (c.f. [tm-en:209])
831 (or (boundp 'mime-edit-popup-menu-for-xemacs)
832 (setq mime-edit-popup-menu-for-xemacs
833 (append '("MIME Commands" "---")
834 (mapcar (function (lambda (item)
838 mime-edit-menu-list)))
841 ((>= emacs-major-version 19)
842 (define-key mime-edit-mode-map [menu-bar mime-edit]
843 (cons mime-edit-menu-title
844 (make-sparse-keymap mime-edit-menu-title)))
847 (define-key mime-edit-mode-map
848 (vector 'menu-bar 'mime-edit (car item))
849 (cons (nth 1 item)(nth 2 item))
852 (reverse mime-edit-menu-list)
860 (defvar mime-edit-touched-flag nil)
863 (defun mime-edit-mode ()
864 "MIME minor mode for editing the tagged MIME message.
866 In this mode, basically, the message is composed in the tagged MIME
867 format. The message tag looks like:
869 --[[text/plain; charset=ISO-2022-JP][7bit]]
871 The tag specifies the MIME content type, subtype, optional parameters
872 and transfer encoding of the message following the tag. Messages
873 without any tag are treated as `text/plain' by default. Charset and
874 transfer encoding are automatically defined unless explicitly
875 specified. Binary messages such as audio and image are usually
876 hidden. The messages in the tagged MIME format are automatically
877 translated into a MIME compliant message when exiting this mode.
879 Available charsets depend on Emacs version being used. The following
880 lists the available charsets of each emacs.
882 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
883 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
884 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
885 CN-BIG5 and ISO-2022-INT-1 are available.
887 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
888 be used to represent multilingual text in intermixed manner. Any
889 languages that has no registered charset are represented as either
890 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
892 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
893 without mule, please set variable `default-mime-charset'. This
894 variable must be symbol of which name is a MIME charset.
896 If you want to add more charsets in mule, please set variable
897 `charsets-mime-charset-alist'. This variable must be alist of which
898 key is list of charset and value is symbol of MIME charset. If name
899 of coding-system is different as MIME charset, please set variable
900 `mime-charset-coding-system-alist'. This variable must be alist of
901 which key is MIME charset and value is coding-system.
903 Following commands are available in addition to major mode commands:
906 \\[mime-edit-insert-text] insert a text message.
907 \\[mime-edit-insert-file] insert a (binary) file.
908 \\[mime-edit-insert-external] insert a reference to external body.
909 \\[mime-edit-insert-voice] insert a voice message.
910 \\[mime-edit-insert-message] insert a mail or news message.
911 \\[mime-edit-insert-mail] insert a mail message.
912 \\[mime-edit-insert-signature] insert a signature file at end.
913 \\[mime-edit-insert-key] insert PGP public key.
914 \\[mime-edit-insert-tag] insert a new MIME tag.
916 \[make enclosure (maybe multipart)\]
917 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
918 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
919 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
920 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
921 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
922 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
923 \\[mime-edit-enclose-quote-region] enclose as verbose mode
924 (to avoid to expand tags)
927 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
928 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
929 \\[mime-edit-set-split] set message splitting mode.
930 \\[mime-edit-set-sign] set PGP-sign mode.
931 \\[mime-edit-set-encrypt] set PGP-encryption mode.
932 \\[mime-edit-preview-message] preview editing MIME message.
933 \\[mime-edit-exit] exit and translate into a MIME
935 \\[mime-edit-help] show this help.
936 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
939 Additional commands are available in some major modes:
940 C-c C-c exit, translate and run the original command.
941 C-c C-s exit, translate and run the original command.
943 The following is a message example written in the tagged MIME format.
944 TABs at the beginning of the line are not a part of the message:
946 This is a conventional plain text. It should be translated
949 This is also a plain text. But, it is explicitly specified as
951 --[[text/plain; charset=ISO-8859-1]]
952 This is also a plain text. But charset is specified as
955 ¡Hola! Buenos días. ¿Cómo está usted?
957 This is a <bold>enriched text</bold>.
958 --[[image/gif][base64]]...image encoded in base64 here...
959 --[[audio/basic][base64]]...audio encoded in base64 here...
961 User customizable variables (not documented all of them):
963 Specifies a key prefix for MIME minor mode commands.
965 mime-ignore-preceding-spaces
966 Preceding white spaces in a message body are ignored if non-nil.
968 mime-ignore-trailing-spaces
969 Trailing white spaces in a message body are ignored if non-nil.
972 Hide a non-textual body message encoded in base64 after insertion
976 A number of network transfer level. It should be bigger than 7.
977 If you are in 8bit-through environment, please set 8.
979 mime-edit-voice-recorder
980 Specifies a function to record a voice message and encode it.
981 The function `mime-edit-voice-recorder-for-sun' is for Sun
985 Turning on MIME mode calls the value of mime-edit-mode-hook, if
988 mime-edit-translate-hook
989 The value of mime-edit-translate-hook is called just before translating
990 the tagged MIME format into a MIME compliant message if it is
991 non-nil. If the hook call the function mime-edit-insert-signature,
992 the signature file will be inserted automatically.
995 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
998 (if mime-edit-mode-flag
1000 (if mime-edit-touched-flag
1002 (make-local-variable 'mime-edit-touched-flag)
1003 (setq mime-edit-touched-flag t)
1008 (cond ((featurep 'xemacs)
1009 (add-minor-mode 'mime-edit-mode-flag
1010 '((" MIME-Edit " mime-transfer-level-string))
1016 (set-alist 'minor-mode-alist
1017 'mime-edit-mode-flag
1018 '((" MIME-Edit " mime-transfer-level-string)))
1019 (set-alist 'minor-mode-map-alist
1020 'mime-edit-mode-flag
1026 (defun turn-on-mime-edit ()
1027 "Unconditionally turn on MIME-Edit mode."
1029 (if mime-edit-mode-flag
1030 (error "You are already editing a MIME message.")
1031 (setq mime-edit-mode-flag t)
1033 ;; Set transfer level into mode line
1035 (setq mime-transfer-level-string
1036 (mime-encoding-name mime-transfer-level 'not-omit))
1037 (force-mode-line-update)
1039 ;; Define menu for XEmacs.
1040 (if (featurep 'xemacs)
1041 (mime-edit-define-menu-for-xemacs)
1046 ;; I don't care about saving these.
1047 (setq paragraph-start
1048 (regexp-or mime-edit-single-part-tag-regexp
1050 (setq paragraph-separate
1051 (regexp-or mime-edit-single-part-tag-regexp
1052 paragraph-separate))
1053 (run-hooks 'mime-edit-mode-hook)
1055 (substitute-command-keys
1056 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1060 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1063 (defun mime-edit-exit (&optional nomime no-error)
1064 "Translate the tagged MIME message into a MIME compliant message.
1065 With no argument encode a message in the buffer into MIME, otherwise
1066 just return to previous mode."
1068 (if (not mime-edit-mode-flag)
1070 (error "You aren't editing a MIME message.")
1074 (run-hooks 'mime-edit-translate-hook)
1075 (mime-edit-translate-buffer)))
1076 ;; Restore previous state.
1077 (setq mime-edit-mode-flag nil)
1078 (if (and (featurep 'xemacs)
1079 (featurep 'menubar))
1080 (delete-menu-item (list mime-edit-menu-title))
1083 (set-buffer-modified-p (buffer-modified-p))
1084 (run-hooks 'mime-edit-exit-hook)
1085 (message "Exit MIME editor mode.")
1088 (defun mime-edit-maybe-translate ()
1090 (mime-edit-exit nil t)
1091 (call-interactively 'mime-edit-maybe-split-and-send)
1094 (defun mime-edit-help ()
1095 "Show help message about MIME mode."
1097 (with-output-to-temp-buffer "*Help*"
1098 (princ "MIME editor mode:\n")
1099 (princ (documentation 'mime-edit-mode))
1100 (print-help-return-message)))
1102 (defun mime-edit-insert-text (&optional subtype)
1103 "Insert a text message.
1104 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1105 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1107 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1109 (if (looking-at mime-edit-single-part-tag-regexp)
1111 ;; Make a space between the following message.
1115 (if (and (member (cadr ret) '("enriched"))
1116 (fboundp 'enriched-mode))
1118 (if (boundp 'enriched-mode)
1123 (defun mime-edit-insert-file (file &optional verbose)
1124 "Insert a message from a file."
1125 (interactive "fInsert file as MIME message: \nP")
1126 (let* ((guess (mime-find-file-type file))
1127 (type (nth 0 guess))
1128 (subtype (nth 1 guess))
1129 (parameters (nth 2 guess))
1130 (encoding (nth 3 guess))
1131 (disposition-type (nth 4 guess))
1132 (disposition-params (nth 5 guess))
1135 (setq type (mime-prompt-for-type type)
1136 subtype (mime-prompt-for-subtype type subtype)
1138 (if (or (interactive-p) verbose)
1139 (setq encoding (mime-prompt-for-encoding encoding))
1141 (if (or (consp parameters) (stringp disposition-type))
1142 (let ((rest parameters) cell attribute value)
1143 (setq parameters "")
1145 (setq cell (car rest))
1146 (setq attribute (car cell))
1147 (setq value (cdr cell))
1148 (if (eq value 'file)
1149 (setq value (std11-wrap-as-quoted-string
1150 (file-name-nondirectory file)))
1152 (setq parameters (concat parameters "; " attribute "=" value))
1153 (setq rest (cdr rest))
1155 (if disposition-type
1158 (concat parameters "\n"
1159 "Content-Disposition: " disposition-type))
1160 (setq rest disposition-params)
1162 (setq cell (car rest))
1163 (setq attribute (car cell))
1164 (setq value (cdr cell))
1165 (if (eq value 'file)
1166 (setq value (std11-wrap-as-quoted-string
1167 (file-name-nondirectory file)))
1170 (concat parameters "; " attribute "=" value))
1171 (setq rest (cdr rest))
1175 (mime-edit-insert-tag type subtype parameters)
1176 (mime-edit-insert-binary-file file encoding)
1179 (defun mime-edit-insert-external ()
1180 "Insert a reference to external body."
1182 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1184 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1186 (let* ((pritype (mime-prompt-for-type))
1187 (subtype (mime-prompt-for-subtype pritype))
1188 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1191 (insert "Content-Type: "
1192 pritype "/" subtype (or parameters "") "\n")))
1193 (if (and (not (eobp))
1194 (not (looking-at mime-edit-single-part-tag-regexp)))
1195 (insert (mime-make-text-tag) "\n")))
1197 (defun mime-edit-insert-voice ()
1198 "Insert a voice message."
1202 "What transfer encoding: "
1203 (mime-encoding-alist) nil t nil)))
1204 (mime-edit-insert-tag "audio" "basic" nil)
1205 (mime-edit-define-encoding encoding)
1207 (narrow-to-region (1- (point))(point))
1209 (funcall mime-edit-voice-recorder encoding)
1212 (invisible-region (point-min)(point-max))
1213 (goto-char (point-max))
1216 (defun mime-edit-insert-signature (&optional arg)
1217 "Insert a signature file."
1219 (let ((signature-insert-hook
1222 (let ((items (mime-find-file-type signature-file-name)))
1223 (apply (function mime-edit-insert-tag)
1224 (car items) (cadr items) (list (caddr items))))
1227 (insert-signature arg)
1231 ;; Insert a new tag around a point.
1233 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1234 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1235 If nothing is inserted, return nil."
1238 (mime-edit-goto-tag)
1239 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1240 (< (match-beginning 0) p)
1243 (goto-char (match-beginning 0))
1252 (mime-prompt-for-type)))
1255 (mime-prompt-for-subtype pritype)))
1258 (mime-prompt-for-parameters pritype subtype delimiter)))
1259 ;; Make a new MIME tag.
1260 (setq newtag (mime-make-tag pritype subtype parameters))
1261 ;; Find an current MIME tag.
1264 (if (mime-edit-goto-tag)
1265 (buffer-substring (match-beginning 0) (match-end 0))
1266 ;; Assume content type is 'text/plan'.
1267 (mime-make-tag "text" "plain")
1269 ;; We are only interested in TEXT.
1271 (not (mime-test-content-type
1272 (mime-edit-get-contype oldtag) "text")))
1275 (if (or (not oldtag) ;Not text
1276 (or mime-ignore-same-text-tag
1277 (not (string-equal oldtag newtag))))
1279 ;; Mark the beginning of the tag for convenience.
1280 (push-mark (point) 'nomsg)
1281 (insert newtag "\n")
1282 (list pritype subtype parameters) ;New tag is created.
1284 ;; Restore previous point.
1286 nil ;Nothing is created.
1290 (defun mime-edit-insert-binary-file (file &optional encoding)
1291 "Insert binary FILE at point.
1292 Optional argument ENCODING specifies an encoding method such as base64."
1293 (let* ((tagend (1- (point))) ;End of the tag
1294 (hide-p (and mime-auto-hide-body
1297 (let ((en (downcase encoding)))
1298 (or (string-equal en "7bit")
1299 (string-equal en "8bit")
1300 (string-equal en "binary")
1304 (narrow-to-region tagend (point))
1305 (mime-insert-encoded-file file encoding)
1308 (invisible-region (point-min) (point-max))
1309 (goto-char (point-max))
1311 (goto-char (point-max))
1314 (looking-at mime-edit-tag-regexp)
1315 (= (point)(point-max))
1316 (mime-edit-insert-tag "text" "plain")
1318 ;; Define encoding even if it is 7bit.
1319 (if (stringp encoding)
1321 (goto-char tagend) ; Make sure which line the tag is on.
1322 (mime-edit-define-encoding encoding)
1327 ;; Commands work on a current message flagment.
1329 (defun mime-edit-goto-tag ()
1330 "Search for the beginning of the tagged MIME message."
1331 (let ((current (point)))
1332 (if (looking-at mime-edit-tag-regexp)
1334 ;; At first, go to the end.
1335 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1336 (goto-char (1- (match-beginning 0))) ;For multiline tag
1339 (goto-char (point-max))
1341 ;; Then search for the beginning.
1342 (re-search-backward mime-edit-end-tag-regexp nil t)
1343 (or (looking-at mime-edit-beginning-tag-regexp)
1344 ;; Restore previous point.
1351 (defun mime-edit-content-beginning ()
1352 "Return the point of the beginning of content."
1354 (let ((beg (save-excursion
1355 (beginning-of-line) (point))))
1356 (if (mime-edit-goto-tag)
1357 (let ((top (point)))
1358 (goto-char (match-end 0))
1359 (if (and (= beg top)
1360 (= (following-char) ?\^M))
1364 ;; Default text/plain tag.
1365 (goto-char (point-min))
1367 (concat "\n" (regexp-quote mail-header-separator)
1368 (if mime-ignore-preceding-spaces
1369 "[ \t\n]*\n" "\n")) nil 'move)
1373 (defun mime-edit-content-end ()
1374 "Return the point of the end of content."
1376 (if (mime-edit-goto-tag)
1378 (goto-char (match-end 0))
1379 (if (invisible-p (point))
1380 (next-visible-point (point))
1381 ;; Move to the end of this text.
1382 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1383 ;; Don't forget a multiline tag.
1384 (goto-char (match-beginning 0))
1388 ;; Assume the message begins with text/plain.
1389 (goto-char (mime-edit-content-beginning))
1390 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1391 ;; Don't forget a multiline tag.
1392 (goto-char (match-beginning 0)))
1396 (defun mime-edit-define-charset (charset)
1397 "Set charset of current tag to CHARSET."
1399 (if (mime-edit-goto-tag)
1400 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1401 (delete-region (match-beginning 0) (match-end 0))
1404 (mime-edit-set-parameter
1405 (mime-edit-get-contype tag)
1406 "charset" (upcase (symbol-name charset)))
1407 (mime-edit-get-encoding tag)))
1410 (defun mime-edit-define-encoding (encoding)
1411 "Set encoding of current tag to ENCODING."
1413 (if (mime-edit-goto-tag)
1414 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1415 (delete-region (match-beginning 0) (match-end 0))
1416 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1419 (defun mime-edit-choose-charset ()
1420 "Choose charset of a text following current point."
1421 (detect-mime-charset-region (point) (mime-edit-content-end))
1424 (defun mime-make-text-tag (&optional subtype)
1425 "Make a tag for a text after current point.
1426 Subtype of text type can be specified by an optional argument SUBTYPE.
1427 Otherwise, it is obtained from mime-content-types."
1428 (let* ((pritype "text")
1429 (subtype (or subtype
1430 (car (car (cdr (assoc pritype mime-content-types)))))))
1431 ;; Charset should be defined later.
1432 (mime-make-tag pritype subtype)))
1435 ;; Tag handling functions
1437 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1438 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1439 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1443 (defun mime-create-tag (contype &optional encoding)
1444 "Make a tag with CONTENT-TYPE and optional ENCODING."
1445 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1448 (defun mime-edit-get-contype (tag)
1449 "Return Content-Type (including parameters) of TAG."
1451 (or (string-match mime-edit-single-part-tag-regexp tag)
1452 (string-match mime-edit-multipart-beginning-regexp tag)
1453 (string-match mime-edit-multipart-end-regexp tag)
1455 (substring tag (match-beginning 1) (match-end 1))
1458 (defun mime-edit-get-encoding (tag)
1459 "Return encoding of TAG."
1461 (string-match mime-edit-single-part-tag-regexp tag)
1463 (not (= (match-beginning 3) (match-end 3)))
1464 (substring tag (match-beginning 3) (match-end 3))))
1466 (defun mime-get-parameter (contype parameter)
1467 "For given CONTYPE return value for PARAMETER.
1468 Nil if no such parameter."
1472 (regexp-quote parameter)
1473 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1475 (substring contype (match-beginning 1) (match-end 1))
1476 nil ;No such parameter
1479 (defun mime-edit-set-parameter (contype parameter value)
1480 "For given CONTYPE set PARAMETER to VALUE."
1481 (let (ctype opt-fields)
1482 (if (string-match "\n[^ \t\n\r]+:" contype)
1483 (setq ctype (substring contype 0 (match-beginning 0))
1484 opt-fields (substring contype (match-beginning 0)))
1485 (setq ctype contype)
1490 (regexp-quote parameter)
1491 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1494 (concat (substring ctype 0 (match-beginning 1))
1496 (substring contype (match-end 1))
1498 (concat ctype "; " parameter "=" value opt-fields)
1501 (defun mime-strip-parameters (contype)
1502 "Return primary content-type and subtype without parameters for CONTYPE."
1503 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1504 (substring contype (match-beginning 1) (match-end 1)) nil))
1506 (defun mime-test-content-type (contype type &optional subtype)
1507 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1508 (and (stringp contype)
1511 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1512 (downcase contype))))
1517 (defun mime-find-file-type (file)
1518 "Guess Content-Type, subtype, and parameters from FILE."
1520 (guesses mime-file-types))
1521 (while (and (not guess) guesses)
1522 (if (string-match (car (car guesses)) file)
1523 (setq guess (cdr (car guesses))))
1524 (setq guesses (cdr guesses)))
1528 (defun mime-prompt-for-type (&optional default)
1529 "Ask for Content-type."
1531 ;; Repeat until primary content type is specified.
1532 (while (string-equal type "")
1534 (completing-read "What content type: "
1537 'require-match ;Type must be specified.
1540 (if (string-equal type "")
1542 (message "Content type is required.")
1549 (defun mime-prompt-for-subtype (type &optional default)
1550 "Ask for subtype of media-type TYPE."
1551 (let ((subtypes (cdr (assoc type mime-content-types))))
1553 (assoc default subtypes))
1554 (setq default (car (car subtypes)))
1560 "What content subtype: (default " default ") ")
1561 "What content subtype: ")
1562 (cdr (assoc type mime-content-types))
1564 'require-match ;Subtype must be specified.
1567 (if (string-equal answer "") default answer)))
1569 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1570 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1571 Optional DELIMITER specifies parameter delimiter (';' by default)."
1572 (let* ((delimiter (or delimiter "; "))
1577 (mime-prompt-for-parameters-1
1579 (cdr (assoc pritype mime-content-types))))))
1582 (if (and (stringp parameters)
1583 (not (string-equal parameters "")))
1584 (concat delimiter parameters)
1585 "" ;"" if no parameters
1588 (defun mime-prompt-for-parameters-1 (optlist)
1589 (apply (function append)
1590 (mapcar (function mime-prompt-for-parameter) optlist)))
1592 (defun mime-prompt-for-parameter (parameter)
1594 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1595 (let* ((prompt (car parameter))
1596 (choices (mapcar (function
1598 (if (consp e) e (list e))))
1600 (default (car (car choices)))
1606 (concat "What " prompt
1608 (if (string-equal default "") "\"\"" default)
1610 choices nil nil ""))
1611 ;; If nothing is selected, use default.
1612 (if (string-equal answer "")
1613 (setq answer default)))
1615 (read-string (concat "What " prompt ": "))))
1616 (cons (if (and answer
1617 (not (string-equal answer "")))
1619 ;; Note: control characters ignored!
1620 (if (string-match mime-tspecials-regexp answer)
1621 (concat "\"" answer "\"") answer)))
1622 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1625 (defun mime-prompt-for-encoding (default)
1626 "Ask for Content-Transfer-Encoding."
1631 "What transfer encoding: "
1632 (mime-encoding-alist) nil t default)
1638 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1641 (defvar mime-edit-translate-buffer-hook
1642 '(mime-edit-pgp-enclose-buffer
1643 mime-edit-translate-body
1644 mime-edit-translate-header))
1646 (defun mime-edit-translate-header ()
1647 "Encode the message header into network representation."
1648 (eword-encode-header 'code-conversion)
1649 (run-hooks 'mime-edit-translate-header-hook)
1652 (defun mime-edit-translate-buffer ()
1653 "Encode the tagged MIME message in current buffer in MIME compliant message."
1656 (if (catch 'mime-edit-error
1658 (run-hooks 'mime-edit-translate-buffer-hook)
1662 (error "Translation error!")
1665 (defun mime-edit-find-inmost ()
1666 (goto-char (point-min))
1667 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1668 (let ((bb (match-beginning 0))
1670 (type (buffer-substring (match-beginning 1)(match-end 1)))
1672 (setq end-exp (format "--}-<<%s>>\n" type))
1674 (if (re-search-forward end-exp nil t)
1675 (setq eb (match-beginning 0))
1676 (setq eb (point-max))
1678 (narrow-to-region be eb)
1680 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1682 (narrow-to-region (match-beginning 0)(point-max))
1683 (mime-edit-find-inmost)
1686 (list type bb be eb)
1689 (defun mime-edit-process-multipart-1 (boundary)
1690 (let ((ret (mime-edit-find-inmost)))
1692 (let ((type (car ret))
1693 (bb (nth 1 ret))(be (nth 2 ret))
1696 (narrow-to-region bb eb)
1697 (delete-region bb be)
1698 (setq bb (point-min))
1699 (setq eb (point-max))
1702 (if (looking-at mime-edit-multipart-end-regexp)
1703 (let ((beg (match-beginning 0))
1706 (delete-region beg end)
1707 (or (looking-at mime-edit-beginning-tag-regexp)
1709 (insert (concat (mime-make-text-tag) "\n"))
1711 (cond ((string-equal type "quote")
1712 (mime-edit-enquote-region bb eb)
1714 ((string-equal type "pgp-signed")
1715 (mime-edit-sign-pgp-mime bb eb boundary)
1717 ((string-equal type "pgp-encrypted")
1718 (mime-edit-encrypt-pgp-mime bb eb boundary)
1720 ((string-equal type "kazu-signed")
1721 (mime-edit-sign-pgp-kazu bb eb boundary)
1723 ((string-equal type "kazu-encrypted")
1724 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1726 ((string-equal type "smime-signed")
1727 (mime-edit-sign-smime bb eb boundary)
1729 ((string-equal type "smime-encrypted")
1730 (mime-edit-encrypt-smime bb eb boundary)
1734 (nth 2 (mime-edit-translate-region bb eb
1738 (format "--[[multipart/%s;
1739 boundary=\"%s\"][7bit]]\n"
1744 (defun mime-edit-enquote-region (beg end)
1747 (narrow-to-region beg end)
1749 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1750 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1751 (replace-match (concat "- " (substring tag 1)))
1754 (defun mime-edit-dequote-region (beg end)
1757 (narrow-to-region beg end)
1759 (while (re-search-forward
1760 mime-edit-quoted-single-part-tag-regexp nil t)
1761 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1762 (replace-match (concat "-" (substring tag 2)))
1765 (defun mime-edit-sign-pgp-mime (beg end boundary)
1768 (let* ((from (std11-field-body "From" mail-header-separator))
1770 (narrow-to-region beg end)
1771 (mime-edit-translate-region beg end boundary)))
1773 (encoding (nth 1 ret))
1774 (pgp-boundary (concat "pgp-sign-" boundary))
1777 (insert (format "Content-Type: %s\n" ctype))
1779 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1782 (or (let ((pgg-default-user-id
1783 (or mime-edit-pgp-user-id
1785 (nth 1 (std11-extract-address-components from))
1786 pgg-default-user-id))))
1787 (pgg-sign-region (point-min)(point-max)))
1788 (throw 'mime-edit-error 'pgp-error)
1791 (cdr (assq 'hash-algorithm
1792 (cdar (with-current-buffer pgg-output-buffer
1793 (pgg-parse-armor-region
1794 (point-min)(point-max))))))
1797 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1800 (insert (format "--[[multipart/signed;
1802 protocol=\"application/pgp-signature\"][7bit]]
1804 " pgp-boundary micalg pgp-boundary))
1805 (goto-char (point-max))
1806 (insert (format "\n--%s
1807 Content-Type: application/pgp-signature
1808 Content-Transfer-Encoding: 7bit
1811 (insert-buffer-substring pgg-output-buffer)
1812 (goto-char (point-max))
1813 (insert (format "\n--%s--\n" pgp-boundary))
1816 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1818 (defun mime-edit-make-encrypt-recipient-header ()
1819 (let* ((names mime-edit-encrypt-recipient-fields-list)
1821 (std11-field-bodies (cons "From" names)
1822 nil mail-header-separator))
1825 (setq values (cdr values))))
1826 (header (and (stringp from)
1827 (if (string-equal from "")
1829 (format "From: %s\n" from)
1832 (while (and names values)
1833 (let ((name (car names))
1834 (value (car values))
1836 (and (stringp value)
1837 (or (string-equal value "")
1839 (setq header (concat header name ": " value "\n")
1840 recipients (if recipients
1841 (concat recipients " ," value)
1844 (setq names (cdr names)
1845 values (cdr values))
1847 (vector from recipients header)
1850 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1853 (let (from recipients header)
1854 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1855 (setq from (aref ret 0)
1856 recipients (aref ret 1)
1857 header (aref ret 2))
1859 (narrow-to-region beg end)
1861 (mime-edit-translate-region beg end boundary))
1863 (encoding (nth 1 ret))
1864 (pgp-boundary (concat "pgp-" boundary)))
1867 (insert (format "Content-Type: %s\n" ctype))
1869 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1872 (eword-encode-header)
1873 (or (let ((pgg-default-user-id
1874 (or mime-edit-pgp-user-id
1876 (nth 1 (std11-extract-address-components from))
1877 pgg-default-user-id))))
1879 (point-min) (point-max)
1880 (mapcar (lambda (recipient)
1881 (nth 1 (std11-extract-address-components
1883 (split-string recipients
1884 "\\([ \t\n]*,[ \t\n]*\\)+")))
1886 (throw 'mime-edit-error 'pgp-error)
1888 (delete-region (point-min)(point-max))
1890 (insert (format "--[[multipart/encrypted;
1892 protocol=\"application/pgp-encrypted\"][7bit]]
1894 Content-Type: application/pgp-encrypted
1897 Content-Type: application/octet-stream
1898 Content-Transfer-Encoding: 7bit
1900 " pgp-boundary pgp-boundary pgp-boundary))
1901 (insert-buffer-substring pgg-output-buffer)
1902 (goto-char (point-max))
1903 (insert (format "\n--%s--\n" pgp-boundary))
1906 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1909 (narrow-to-region beg end)
1911 (mime-edit-translate-region beg end boundary))
1913 (encoding (nth 1 ret)))
1915 (insert (format "Content-Type: %s\n" ctype))
1917 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1920 (or (pgg-sign-region beg (point-max) 'clearsign)
1921 (throw 'mime-edit-error 'pgp-error)
1925 "--[[application/pgp; format=mime][7bit]]\n")
1929 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1931 (let (recipients header)
1932 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1933 (setq recipients (aref ret 1)
1934 header (aref ret 2))
1937 (narrow-to-region beg end)
1939 (mime-edit-translate-region beg end boundary))
1941 (encoding (nth 1 ret)))
1944 (insert (format "Content-Type: %s\n" ctype))
1946 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1949 (or (pgg-encrypt-region beg (point-max) recipients)
1950 (throw 'mime-edit-error 'pgp-error)
1954 "--[[application/pgp; format=mime][7bit]]\n")
1958 (defun mime-edit-sign-smime (beg end boundary)
1962 (narrow-to-region beg end)
1963 (mime-edit-translate-region beg end boundary)))
1965 (encoding (nth 1 ret))
1966 (smime-boundary (concat "smime-sign-" boundary)))
1968 (insert (format "Content-Type: %s\n" ctype))
1970 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1973 (let (buffer-undo-list)
1974 (goto-char (point-min))
1975 (while (progn (end-of-line) (not (eobp)))
1978 (or (prog1 (smime-sign-region (point-min)(point-max))
1979 (push nil buffer-undo-list)
1980 (ignore-errors (undo)))
1981 (throw 'mime-edit-error 'pgp-error)
1984 (insert (format "--[[multipart/signed;
1985 boundary=\"%s\"; micalg=sha1;
1986 protocol=\"application/pkcs7-signature\"][7bit]]
1988 " smime-boundary smime-boundary))
1989 (goto-char (point-max))
1990 (insert (format "\n--%s
1991 Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
1992 Content-Transfer-Encoding: base64
1993 Content-Disposition: attachment; filename=\"smime.p7s\"
1994 Content-Description: S/MIME Cryptographic Signature
1997 (insert-buffer-substring smime-output-buffer)
1998 (goto-char (point-max))
1999 (insert (format "\n--%s--\n" smime-boundary))
2002 (defun mime-edit-encrypt-smime (beg end boundary)
2006 (narrow-to-region beg end)
2007 (mime-edit-translate-region beg end boundary)))
2009 (encoding (nth 1 ret)))
2011 (insert (format "Content-Type: %s\n" ctype))
2013 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2016 (goto-char (point-min))
2017 (while (progn (end-of-line) (not (eobp)))
2020 (or (smime-encrypt-region (point-min)(point-max))
2021 (throw 'mime-edit-error 'pgp-error)
2023 (delete-region (point-min)(point-max))
2024 (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
2025 Content-Disposition: attachment; filename=\"smime.p7m\"
2026 Content-Description: S/MIME Encrypted Message][base64]]\n")
2027 (insert-buffer-substring smime-output-buffer)
2030 (defsubst replace-space-with-underline (str)
2031 (mapconcat (function
2039 (defun mime-edit-make-boundary ()
2040 (concat mime-multipart-boundary "_"
2041 (replace-space-with-underline (current-time-string))
2044 (defun mime-edit-translate-body ()
2045 "Encode the tagged MIME body in current buffer in MIME compliant message."
2048 (let ((boundary (mime-edit-make-boundary))
2051 (while (mime-edit-process-multipart-1
2052 (format "%s-%d" boundary i))
2056 ;; We are interested in message body.
2059 (goto-char (point-min))
2061 (concat "\n" (regexp-quote mail-header-separator)
2062 (if mime-ignore-preceding-spaces
2063 "[ \t\n]*\n" "\n")) nil 'move)
2067 (goto-char (point-max))
2068 (and mime-ignore-trailing-spaces
2069 (re-search-backward "[^ \t\n]\n" beg t)
2072 (setq ret (mime-edit-translate-region
2074 (format "%s-%d" boundary i)))
2076 (mime-edit-dequote-region (point-min)(point-max))
2077 (let ((contype (car ret)) ;Content-Type
2078 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
2080 ;; Insert User-Agent field
2081 (and mime-edit-insert-user-agent-field
2082 (or (mail-position-on-field "User-Agent")
2083 (insert mime-edit-user-agent-value)
2085 ;; Make primary MIME headers.
2086 (or (mail-position-on-field "MIME-Version")
2087 (insert mime-edit-mime-version-value))
2088 ;; Remove old Content-Type and other fields.
2090 (goto-char (point-min))
2091 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2092 (narrow-to-region (point-min) (point))
2093 (goto-char (point-min))
2094 (mime-delete-field "Content-Type")
2095 (mime-delete-field "Content-Transfer-Encoding"))
2096 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2097 (mail-position-on-field "Content-Type")
2101 (mail-position-on-field "Content-Transfer-Encoding")
2105 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2106 "Translate single-part-tag to MIME header."
2107 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2108 (let* ((beg (match-beginning 0))
2110 (tag (buffer-substring beg end)))
2111 (delete-region beg end)
2112 (let ((contype (mime-edit-get-contype tag))
2113 (encoding (mime-edit-get-encoding tag)))
2114 (insert (concat prefix "--" boundary "\n"))
2116 (narrow-to-region (point)(point))
2117 (insert "Content-Type: " contype "\n")
2119 (insert "Content-Transfer-Encoding: " encoding "\n"))
2120 (eword-encode-header)
2125 (downcase encoding))))
2128 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2130 (setq boundary (mime-edit-make-boundary))
2134 (narrow-to-region beg end)
2135 (let ((tag nil) ;MIME tag
2136 (contype nil) ;Content-Type
2137 (encoding nil) ;Content-Transfer-Encoding
2138 (nparts 0)) ;Number of body parts
2139 ;; Normalize the body part by inserting appropriate message
2140 ;; tags for every message contents.
2141 (mime-edit-normalize-body)
2142 ;; Counting the number of Content-Type.
2143 (goto-char (point-min))
2144 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2145 (setq nparts (1+ nparts)))
2146 ;; Begin translation.
2148 ((and (<= nparts 1)(not multipart))
2149 ;; It's a singular message.
2150 (goto-char (point-min))
2151 (while (re-search-forward
2152 mime-edit-single-part-tag-regexp nil t)
2154 (buffer-substring (match-beginning 0) (match-end 0)))
2155 (delete-region (match-beginning 0) (1+ (match-end 0)))
2156 (setq contype (mime-edit-get-contype tag))
2157 (setq encoding (mime-edit-get-encoding tag))
2160 ;; It's a multipart message.
2161 (goto-char (point-min))
2162 (let ((prio mime-content-transfer-encoding-priority-list)
2164 (when (setq part-info
2165 (mime-edit-translate-single-part-tag boundary))
2166 (and (setq nprio (member (cdr part-info) prio))
2168 (while (setq part-info
2169 (mime-edit-translate-single-part-tag boundary "\n"))
2170 (and (setq nprio (member (cdr part-info) prio))
2171 (setq prio nprio))))
2172 ;; Define Content-Type as "multipart/mixed".
2174 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2175 (setq encoding (car prio))
2176 ;; Insert the trailer.
2177 (goto-char (point-max))
2178 (insert "\n--" boundary "--\n")
2180 (list contype encoding boundary nparts)
2183 (defun mime-edit-normalize-body ()
2184 "Normalize the body part by inserting appropriate message tags."
2185 ;; Insert the first MIME tags if necessary.
2186 (goto-char (point-min))
2187 (if (not (looking-at mime-edit-single-part-tag-regexp))
2188 (insert (mime-make-text-tag) "\n"))
2189 ;; Check each tag, and add new tag or correct it if necessary.
2190 (goto-char (point-min))
2191 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2192 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2193 (contype (mime-edit-get-contype tag))
2194 (charset (mime-get-parameter contype "charset"))
2195 (encoding (mime-edit-get-encoding tag)))
2196 ;; Remove extra whitespaces after the tag.
2197 (if (looking-at "[ \t]+$")
2198 (delete-region (match-beginning 0) (match-end 0)))
2200 (end (mime-edit-content-end))
2202 (if (= end (point-max))
2205 (or (looking-at mime-edit-beginning-tag-regexp)
2207 (insert (mime-make-text-tag) "\n")
2209 (visible-region beg end)
2213 ((mime-test-content-type contype "message")
2214 ;; Content-type "message" should be sent as is.
2217 ((mime-test-content-type contype "text")
2218 ;; Define charset for text if necessary.
2219 (setq charset (if charset
2220 (intern (downcase charset))
2221 (mime-edit-choose-charset)))
2222 (mime-edit-define-charset charset)
2223 (cond ((string-equal contype "text/x-rot13-47-48")
2226 (mule-caesar-region (point) (mime-edit-content-end))
2228 ((string-equal contype "text/enriched")
2233 (end (mime-edit-content-end))
2235 ;; Patch for hard newlines
2238 ;; (while (search-forward "\n" end t)
2239 ;; (put-text-property (match-beginning 0)
2242 ;; End patch for hard newlines
2243 (enriched-encode beg end)
2245 (if (search-forward "\n\n")
2246 (delete-region beg (match-end 0))
2249 ;; Point is now on current tag.
2250 ;; Define encoding and encode text if necessary.
2251 (or encoding ;Encoding is not specified.
2254 (let ((ret (cdr (assq charset mime-charset-type-list))))
2256 (setq bits (car ret)
2259 conv "quoted-printable")))
2260 (if (<= bits mime-transfer-level)
2261 (mime-encoding-name bits)
2263 (beg (mime-edit-content-beginning)))
2264 (encode-mime-charset-region beg (mime-edit-content-end)
2266 ;; Protect "From " in beginning of line
2268 (narrow-to-region beg (mime-edit-content-end))
2270 (let (case-fold-search)
2271 (if (re-search-forward "^From " nil t)
2273 (if (memq charset '(iso-2022-jp
2278 (replace-match "\e(BFrom ")
2279 (re-search-forward "^From " nil t)
2281 (setq encoding "quoted-printable")
2283 ;; canonicalize line break code
2284 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2286 (narrow-to-region beg (mime-edit-content-end))
2288 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2289 (replace-match "\\1\r\n"))))
2291 (mime-encode-region beg (mime-edit-content-end)
2292 (or encoding "7bit"))
2293 (mime-edit-define-encoding encoding)
2295 (goto-char (mime-edit-content-end))
2297 ((null encoding) ;Encoding is not specified.
2298 ;; Application, image, audio, video, and any other
2299 ;; unknown content-type without encoding should be
2301 (let* ((encoding "base64") ;Encode in BASE64 by default.
2302 (beg (mime-edit-content-beginning))
2303 (end (mime-edit-content-end)))
2304 (mime-encode-region beg end encoding)
2305 (mime-edit-define-encoding encoding))
2310 (defun mime-delete-field (field)
2311 "Delete header FIELD."
2312 (let ((regexp (format "^%s:[ \t]*" field)))
2313 (goto-char (point-min))
2314 (while (re-search-forward regexp nil t)
2315 (delete-region (match-beginning 0)
2316 (1+ (std11-field-end))))))
2320 ;;; Platform dependent functions
2323 ;; Sun implementations
2325 (defun mime-edit-voice-recorder-for-sun (encoding)
2326 "Record voice in a buffer using Sun audio device,
2327 and insert data encoded as ENCODING."
2328 (message "Start the recording on %s. Type C-g to finish the recording..."
2330 (mime-insert-encoded-file "/dev/audio" encoding)
2334 ;;; @ Other useful commands.
2337 ;; Message forwarding commands as content-type "message/rfc822".
2339 (defun mime-edit-insert-message (&optional message)
2341 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2342 (if (and inserter (fboundp inserter))
2344 (mime-edit-insert-tag "message" "rfc822")
2345 (funcall inserter message)
2347 (message "Sorry, I don't have message inserter for your MUA.")
2350 (defun mime-edit-insert-mail (&optional message)
2352 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2353 (if (and inserter (fboundp inserter))
2355 (mime-edit-insert-tag "message" "rfc822")
2356 (funcall inserter message)
2358 (message "Sorry, I don't have mail inserter for your MUA.")
2361 (defun mime-edit-inserted-message-filter ()
2364 (let ((header-start (point))
2365 (case-fold-search t)
2368 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2369 (if (re-search-forward "^$" (mark t))
2370 (narrow-to-region header-start (match-beginning 0))
2372 (goto-char header-start)
2373 (while (and (re-search-forward
2374 mime-edit-yank-ignored-field-regexp nil t)
2375 (setq beg (match-beginning 0))
2376 (setq end (1+ (std11-field-end)))
2378 (delete-region beg end)
2383 ;;; @ multipart enclosure
2386 (defun mime-edit-enclose-region-internal (type beg end)
2390 (narrow-to-region beg end)
2391 (insert (format "--<<%s>>-{\n" type))
2392 (goto-char (point-max))
2393 (insert (format "--}-<<%s>>\n" type))
2394 (goto-char (point-max))
2396 (or (looking-at mime-edit-beginning-tag-regexp)
2398 (insert (mime-make-text-tag) "\n")
2402 (defun mime-edit-enclose-quote-region (beg end)
2404 (mime-edit-enclose-region-internal 'quote beg end)
2407 (defun mime-edit-enclose-mixed-region (beg end)
2409 (mime-edit-enclose-region-internal 'mixed beg end)
2412 (defun mime-edit-enclose-parallel-region (beg end)
2414 (mime-edit-enclose-region-internal 'parallel beg end)
2417 (defun mime-edit-enclose-digest-region (beg end)
2419 (mime-edit-enclose-region-internal 'digest beg end)
2422 (defun mime-edit-enclose-alternative-region (beg end)
2424 (mime-edit-enclose-region-internal 'alternative beg end)
2427 (defun mime-edit-enclose-pgp-signed-region (beg end)
2429 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2432 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2434 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2437 (defun mime-edit-enclose-kazu-signed-region (beg end)
2439 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2442 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2444 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2447 (defun mime-edit-enclose-smime-signed-region (beg end)
2449 (mime-edit-enclose-region-internal 'smime-signed beg end)
2452 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2454 (mime-edit-enclose-region-internal 'smime-encrypted beg end)
2457 (defun mime-edit-insert-key (&optional arg)
2458 "Insert a pgp public key."
2460 (mime-edit-insert-tag "application" "pgp-keys")
2461 (mime-edit-define-encoding "7bit")
2469 (defun mime-edit-set-split (arg)
2472 (y-or-n-p "Do you want to enable split? ")
2474 (setq mime-edit-split-message arg)
2476 (message "This message is enabled to split.")
2477 (message "This message is not enabled to split.")
2480 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2481 "Toggle transfer-level is 7bit or 8bit through.
2483 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2485 (if (numberp transfer-level)
2486 (setq mime-transfer-level transfer-level)
2487 (if (< mime-transfer-level 8)
2488 (setq mime-transfer-level 8)
2489 (setq mime-transfer-level 7)
2491 (message (format "Current transfer-level is %d bit"
2492 mime-transfer-level))
2493 (setq mime-transfer-level-string
2494 (mime-encoding-name mime-transfer-level 'not-omit))
2495 (force-mode-line-update)
2498 (defun mime-edit-set-transfer-level-7bit ()
2500 (mime-edit-toggle-transfer-level 7)
2503 (defun mime-edit-set-transfer-level-8bit ()
2505 (mime-edit-toggle-transfer-level 8)
2512 (defvar mime-edit-pgp-processing nil)
2513 (make-variable-buffer-local 'mime-edit-pgp-processing)
2515 (defvar mime-edit-pgp-user-id nil)
2517 (defun mime-edit-set-sign (arg)
2520 (y-or-n-p "Do you want to sign? ")
2524 (or (memq 'sign mime-edit-pgp-processing)
2525 (setq mime-edit-pgp-processing
2526 (nconc mime-edit-pgp-processing
2527 (copy-sequence '(sign)))))
2528 (message "This message will be signed.")
2530 (setq mime-edit-pgp-processing
2531 (delq 'sign mime-edit-pgp-processing))
2532 (message "This message will not be signed.")
2535 (defun mime-edit-set-encrypt (arg)
2538 (y-or-n-p "Do you want to encrypt? ")
2542 (or (memq 'encrypt mime-edit-pgp-processing)
2543 (setq mime-edit-pgp-processing
2544 (nconc mime-edit-pgp-processing
2545 (copy-sequence '(encrypt)))))
2546 (message "This message will be encrypt.")
2548 (setq mime-edit-pgp-processing
2549 (delq 'encrypt mime-edit-pgp-processing))
2550 (message "This message will not be encrypt.")
2553 (defun mime-edit-pgp-enclose-buffer ()
2554 (let ((beg (save-excursion
2555 (goto-char (point-min))
2556 (if (search-forward (concat "\n" mail-header-separator "\n"))
2561 (dolist (pgp-processing mime-edit-pgp-processing)
2562 (case pgp-processing
2564 (mime-edit-enclose-pgp-signed-region
2568 (mime-edit-enclose-pgp-encrypted-region
2577 (defun mime-edit-insert-partial-header (fields subject
2578 id number total separator)
2580 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2581 (insert mime-edit-mime-version-field-for-message/partial)
2583 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2584 id number total separator))
2587 (defun mime-edit-split-and-send
2588 (&optional cmd lines mime-edit-message-max-length)
2592 (count-lines (point-min) (point-max)))
2594 (or mime-edit-message-max-length
2595 (setq mime-edit-message-max-length
2596 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2597 mime-edit-message-default-max-lines))
2599 (let* ((mime-edit-draft-file-name
2600 (or (buffer-file-name)
2602 (expand-file-name "mime-draft" temporary-file-directory))))
2603 (separator mail-header-separator)
2605 (replace-space-with-underline (current-time-string))
2606 "@" (system-name) "\"")))
2607 (run-hooks 'mime-edit-before-split-hook)
2608 (let ((the-buf (current-buffer))
2609 (copy-buf (get-buffer-create " *Original Message*"))
2610 (header (std11-header-string-except
2611 mime-edit-split-ignored-field-regexp separator))
2612 (subject (mail-fetch-field "subject"))
2613 (total (+ (/ lines mime-edit-message-max-length)
2614 (if (> (mod lines mime-edit-message-max-length) 0)
2620 mime-edit-split-message-sender-alist))
2624 (error "Split sender is not specified for `%s'." major-mode)
2627 (mime-edit-partial-number 1)
2630 (set-buffer copy-buf)
2632 (insert-buffer the-buf)
2634 (if (re-search-forward
2635 (concat "^" (regexp-quote separator) "$") nil t)
2636 (let ((he (match-beginning 0)))
2638 (narrow-to-region (point-min) he)
2640 (goto-char (point-min))
2641 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2642 (delete-region (match-beginning 0)
2643 (1+ (std11-field-end)))
2645 (while (< mime-edit-partial-number total)
2648 (set-buffer copy-buf)
2649 (setq data (buffer-substring
2652 (goto-line mime-edit-message-max-length)
2655 (delete-region (point-min)(point))
2657 (mime-edit-insert-partial-header
2658 header subject id mime-edit-partial-number total separator)
2661 (message (format "Sending %d/%d..."
2662 mime-edit-partial-number total))
2663 (call-interactively command)
2664 (message (format "Sending %d/%d... done"
2665 mime-edit-partial-number total))
2667 (setq mime-edit-partial-number
2668 (1+ mime-edit-partial-number))
2672 (set-buffer copy-buf)
2673 (setq data (buffer-string))
2676 (mime-edit-insert-partial-header
2677 header subject id mime-edit-partial-number total separator)
2680 (message (format "Sending %d/%d..."
2681 mime-edit-partial-number total))
2682 (message (format "Sending %d/%d... done"
2683 mime-edit-partial-number total))
2687 (defun mime-edit-maybe-split-and-send (&optional cmd)
2689 (run-hooks 'mime-edit-before-send-hook)
2690 (let ((mime-edit-message-max-length
2691 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2692 mime-edit-message-default-max-lines))
2693 (lines (count-lines (point-min) (point-max)))
2695 (if (and (> lines mime-edit-message-max-length)
2696 mime-edit-split-message)
2697 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2701 ;;; @ preview message
2704 (defvar mime-edit-buffer nil) ; buffer local variable
2706 (defun mime-edit-preview-message ()
2707 "preview editing MIME message."
2709 (let* ((str (buffer-string))
2710 (separator mail-header-separator)
2711 (the-buf (current-buffer))
2712 (buf-name (buffer-name))
2713 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2714 (buf (get-buffer temp-buf-name))
2715 (pgp-processing mime-edit-pgp-processing)
2719 (switch-to-buffer buf)
2722 (setq buf (get-buffer-create temp-buf-name))
2723 (switch-to-buffer buf)
2726 (setq major-mode 'mime-temp-message-mode)
2727 (make-local-variable 'mail-header-separator)
2728 (setq mail-header-separator separator)
2729 (make-local-variable 'mime-edit-buffer)
2730 (setq mime-edit-buffer the-buf)
2731 (setq mime-edit-pgp-processing pgp-processing)
2733 (run-hooks 'mime-edit-translate-hook)
2734 (mime-edit-translate-buffer)
2735 (goto-char (point-min))
2736 (if (re-search-forward
2737 (concat "^" (regexp-quote separator) "$"))
2743 (defun mime-edit-quitting-method ()
2744 "Quitting method for mime-view."
2745 (let* ((entity (get-text-property (point-min) 'mime-view-entity))
2746 (temp (mime-entity-buffer entity))
2748 (mime-preview-kill-buffer)
2750 (setq buf mime-edit-buffer)
2752 (switch-to-buffer buf)
2755 (set-alist 'mime-preview-quitting-method-alist
2756 'mime-temp-message-mode
2757 #'mime-edit-quitting-method)
2763 (defvar mime-edit-again-ignored-field-regexp
2764 (concat "^\\(" "Content-.*\\|Mime-Version"
2765 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2767 "Regexp for deleted header fields when `mime-edit-again' is called.")
2769 (defsubst eliminate-top-spaces (string)
2770 "Eliminate top sequence of space or tab in STRING."
2771 (if (string-match "^[ \t]+" string)
2772 (substring string (match-end 0))
2775 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2778 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2779 '(("application/pgp-encrypted" . pgp-encrypted)
2780 ("application/pgp-signature" . pgp-signed))))
2781 (mime-content-type-subtype content-type)))
2782 (boundary (mime-content-type-parameter content-type "boundary"))
2783 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2784 (re-search-forward boundary-pat nil t)
2785 (let ((bb (match-beginning 0)) eb tag)
2786 (setq tag (format "\n--<<%s>>-{\n" subtype))
2789 (setq bb (+ bb (length tag)))
2791 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2793 (setq eb (match-beginning 0))
2794 (replace-match (format "--}-<<%s>>\n" subtype))
2796 (narrow-to-region bb eb)
2797 (goto-char (point-min))
2798 (while (re-search-forward boundary-pat nil t)
2799 (let ((beg (match-beginning 0))
2801 (delete-region beg (match-end 0))
2803 (if (re-search-forward boundary-pat nil t)
2804 (setq end (match-beginning 0))
2805 (setq end (point-max))
2808 (narrow-to-region beg end)
2810 ((eq subtype 'pgp-encrypted)
2813 (goto-char (point-min))
2814 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2817 (save-window-excursion
2818 (pgg-decrypt-region (match-beginning 0)
2820 (delete-region (point-min)(point-max))))
2821 (insert-buffer-substring pgg-output-buffer)
2822 (mime-edit-decode-message-in-buffer
2823 nil not-decode-text)
2824 (delete-region (goto-char (point-min))
2825 (if (search-forward "\n\n" nil t)
2828 (goto-char (point-max))
2831 (mime-edit-decode-message-in-buffer
2832 (if (eq subtype 'digest)
2834 (make-mime-content-type 'message 'rfc822))
2837 (goto-char (point-max))
2841 (goto-char (point-min))
2842 (or (= (point-min) 1)
2843 (delete-region (point-min)
2844 (if (search-forward "\n\n" nil t)
2850 (defun mime-edit-decode-single-part-in-buffer
2851 (content-type not-decode-text &optional content-disposition)
2852 (let* ((type (mime-content-type-primary-type content-type))
2853 (subtype (mime-content-type-subtype content-type))
2854 (ctype (format "%s/%s" type subtype))
2856 (pstr (let ((bytes (+ 14 (length ctype))))
2857 (mapconcat (function
2859 (if (string= (car attr) "charset")
2861 (setq charset (cdr attr))
2863 (let* ((str (concat (car attr)
2866 (setq bytes (+ bytes bs 2))
2869 (setq bytes (+ bs 1))
2873 (mime-content-type-parameters content-type) "")))
2876 (limit (save-excursion
2877 (if (search-forward "\n\n" nil t)
2880 (mime-content-disposition-type content-disposition))
2882 (if disposition-type
2883 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2884 (mapconcat (function
2889 (if (string-equal "filename"
2891 (std11-wrap-as-quoted-string
2895 (setq bytes (+ bytes bs 2))
2898 (setq bytes (+ bs 1))
2902 (mime-content-disposition-parameters
2903 content-disposition)
2906 (if disposition-type
2907 (setq pstr (format "%s\nContent-Disposition: %s%s"
2908 pstr disposition-type disposition-str))
2911 (if (re-search-forward
2912 "^Content-Transfer-Encoding:" limit t)
2913 (let ((beg (match-beginning 0))
2914 (hbeg (match-end 0))
2915 (end (std11-field-end limit)))
2918 (eliminate-top-spaces
2919 (std11-unfold-string
2920 (buffer-substring hbeg end)))))
2921 (if (or charset (eq type 'text))
2923 (delete-region beg (1+ end))
2924 (goto-char (point-min))
2925 (if (search-forward "\n\n" nil t)
2928 (match-end 0)(point-max) encoding)
2932 (if (or encoded (not not-decode-text))
2935 (goto-char (point-min))
2936 (while (re-search-forward "\r\n" nil t)
2937 (replace-match "\n")
2939 (decode-mime-charset-region (point-min)(point-max)
2940 (or charset default-mime-charset))
2942 (let ((he (if (re-search-forward "^$" nil t)
2946 (if (and (eq type 'text)
2947 (eq subtype 'x-rot13-47-48))
2948 (mule-caesar-region he (point-max))
2950 (if (= (point-min) 1)
2956 (format "%s/%s%s" type subtype pstr)
2959 (delete-region (point-min) he)
2961 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2967 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2970 (goto-char (point-min))
2971 (let ((ctl (or (mime-read-Content-Type)
2972 default-content-type)))
2974 (let ((type (mime-content-type-primary-type ctl)))
2976 ((and (eq type 'application)
2977 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2978 (delete-region (point-min)(point-max))
2980 ((eq type 'multipart)
2981 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
2984 (mime-edit-decode-single-part-in-buffer
2985 ctl not-decode-text (mime-read-Content-Disposition))
2988 (decode-mime-charset-region (point-min) (point-max)
2989 default-mime-charset))
2991 (if (= (point-min) 1)
2994 (std11-narrow-to-header)
2995 (goto-char (point-min))
2996 (while (re-search-forward
2997 mime-edit-again-ignored-field-regexp nil t)
2998 (delete-region (match-beginning 0) (1+ (std11-field-end)))
3000 (mime-decode-header-in-buffer (not not-decode-text))
3005 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
3006 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
3007 Content-Type and Content-Transfer-Encoding header fields will be
3008 converted to MIME-Edit tags."
3010 (goto-char (point-min))
3012 (concat "\n" (regexp-quote mail-header-separator) "\n")
3014 (replace-match "\n\n")
3016 (mime-edit-decode-message-in-buffer nil not-decode-text)
3017 (goto-char (point-min))
3019 (and (re-search-forward "^$")
3020 (replace-match mail-header-separator)
3030 (provide 'mime-edit)
3032 (run-hooks 'mime-edit-load-hook)
3034 ;;; mime-edit.el ends here