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...
110 (eval-when-compile (require 'static))
113 (require 'mail-utils)
115 (require 'eword-encode) ; eword-encode-field-body
123 (autoload 'pgg-encrypt-region "pgg"
124 "PGP encryption of current region." t)
125 (autoload 'pgg-sign-region "pgg"
126 "PGP signature of current region." t)
127 (autoload 'pgg-insert-key "pgg"
128 "Insert PGP public key at point." t)
129 (autoload 'smime-encrypt-region "smime"
130 "S/MIME encryption of current region.")
131 (autoload 'smime-sign-region "smime"
132 "S/MIME signature of current region.")
133 (defvar smime-output-buffer)
134 (defvar smime-errors-buffer)
141 (defconst mime-edit-version
143 (mime-product-name mime-user-interface-product) " "
144 (mapconcat #'number-to-string
145 (mime-product-version mime-user-interface-product) ".")
146 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
152 (defgroup mime-edit nil
156 (defcustom mime-ignore-preceding-spaces nil
157 "*Ignore preceding white spaces if non-nil."
161 (defcustom mime-ignore-trailing-spaces nil
162 "*Ignore trailing white spaces if non-nil."
166 (defcustom mime-ignore-same-text-tag t
167 "*Ignore preceding text content-type tag that is same with new one.
168 If non-nil, the text tag is not inserted unless something different."
172 (defcustom mime-auto-hide-body t
173 "*Hide non-textual body encoded in base64 after insertion if non-nil."
177 (defcustom mime-edit-voice-recorder
178 (function mime-edit-voice-recorder-for-sun)
179 "*Function to record a voice message and encode it."
183 (defcustom mime-edit-mode-hook nil
184 "*Hook called when enter MIME mode."
188 (defcustom mime-edit-translate-hook nil
189 "*Hook called before translating into a MIME compliant message.
190 To insert a signature file automatically, call the function
191 `mime-edit-insert-signature' from this hook."
195 (defcustom mime-edit-exit-hook nil
196 "*Hook called when exit MIME mode."
200 (defvar mime-content-types
202 ;; Charset parameter need not to be specified, since it is
203 ;; defined automatically while translation.
205 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
218 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
219 ("directory" "/pub/GNU/elisp/mime")
221 ("mode" "image" "ascii" "local8"))
226 ("mode" "image" "ascii" "local8"))
227 ("tftp" ("site") ("name"))
228 ("afs" ("site") ("name"))
229 ("local-file" ("site") ("name"))
231 ("server" "ftpmail@nic.karrn.ad.jp")
237 ("octet-stream" ("type" "" "tar" "shar"))
239 ("vnd.ms-powerpoint")
240 ("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)))
265 "application" "octet-stream" (("type" . "emacs-lisp"))
267 "attachment" (("filename" . file)))
270 "application" "octet-stream" (("type" . "common-lisp"))
272 "attachment" (("filename" . file)))
275 "application" "octet-stream" (("type" . "perl"))
277 "attachment" (("filename" . file)))
279 ;; Text or translated text
284 "inline" (("filename" . file)))
286 ;; .rc : procmail modules pm-xxxx.rc
287 ;; *rc : other resource files
289 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
292 "attachment" (("filename" . file)))
299 ("\\.diff$\\|\\.patch$"
300 "text" "plain" (("type" . "patch"))
302 "inline" (("filename" . file)))
305 "text" "plain" nil nil nil nil)
308 ;; Octect binary text
311 "application" "winword" nil
313 "attachment" (("filename" . file)))
314 ("\\.ppt$" ; MS Power Point
315 "application" "vnd.ms-powerpoint" nil
317 "attachment" (("filename" . file)))
322 "inline" (("filename" . file)))
324 "application" "postscript" nil
326 "attachment" (("filename" . file)))
333 "inline" (("filename" . file)))
337 "inline" (("filename" . file)))
341 "inline" (("filename" . file)))
345 "inline" (("filename" . file)))
349 "inline" (("filename" . file)))
353 "inline" (("filename" . file)))
357 "inline" (("filename" . file)))
361 "inline" (("filename" . file)))
365 "attachment" (("filename" . file)))
369 "attachment" (("filename" . file)))
371 "application" "octet-stream" (("type" . "tar+gzip"))
373 "attachment" (("filename" . file)))
375 "application" "octet-stream" (("type" . "tar+gzip"))
377 "attachment" (("filename" . file)))
379 "application" "octet-stream" (("type" . "tar+compress"))
381 "attachment" (("filename" . file)))
383 "application" "octet-stream" (("type" . "tar+compress"))
385 "attachment" (("filename" . file)))
387 "application" "octet-stream" (("type" . "gzip"))
389 "attachment" (("filename" . file)))
391 "application" "octet-stream" (("type" . "compress"))
393 "attachment" (("filename" . file)))
395 "application" "octet-stream" (("type" . "lha"))
397 "attachment" (("filename" . file)))
399 "application" "zip" nil
401 "attachment" (("filename" . file)))
406 "application" "octet-stream" nil
408 "attachment" (("filename" . file))))
409 "*Alist of file name, types, parameters, and default encoding.
410 If encoding is nil, it is determined from its contents."
414 (choice :tag "Primary-Type"
415 ,@(nconc (mapcar (lambda (cell)
416 (list 'item (car cell)))
420 (choice :tag "Sub-Type"
423 (mapcar (lambda (cell)
424 (mapcar (lambda (cell)
425 (list 'item (car cell)))
430 (repeat :tag "Parameters of Content-Type field"
431 (cons string (choice string symbol)))
432 ;; content-transfer-encoding
433 (choice :tag "Encoding"
436 (mapcar (lambda (cell)
438 (mime-encoding-list))))
440 (choice :tag "Disposition-Type"
446 (repeat :tag "Parameters of Content-Disposition field"
447 (cons string (choice string symbol)))))
450 (defvar mime-content-disposition-types
451 '(("attachment") ("inline")))
453 (defcustom mime-edit-attach-at-end-type nil
454 "*List of MIME types to be attached at the end of a message.
455 Values must be strings indicates MIME types. You can specify
456 either type/subtype or type only."
458 :type '(choice (const :tag "Nothing" nil)
459 (list (repeat symbol))))
461 ;;; @@ about charset, encoding and transfer-level
464 (defvar mime-charset-type-list
466 (iso-8859-1 8 "quoted-printable")
467 (iso-8859-2 8 "quoted-printable")
468 (iso-8859-3 8 "quoted-printable")
469 (iso-8859-4 8 "quoted-printable")
470 (iso-8859-5 8 "quoted-printable")
471 (koi8-r 8 "quoted-printable")
472 (iso-8859-7 8 "quoted-printable")
473 (iso-8859-8 8 "quoted-printable")
474 (iso-8859-9 8 "quoted-printable")
475 (iso-2022-jp 7 "base64")
476 (iso-2022-kr 7 "base64")
482 (shift_jis 8 "base64")
484 (iso-2022-jp-2 7 "base64")
485 (iso-2022-int-1 7 "base64")))
487 (defcustom mime-transfer-level 7
488 "*A number of network transfer level. It should be bigger than 7.
489 Currently it has no effect except mode-line display."
491 :type '(choice (const 7)
493 (const :tag "Binary" 9)))
494 (make-variable-buffer-local 'mime-transfer-level)
496 (defsubst mime-encoding-name (transfer-level &optional not-omit)
497 (cond ((> transfer-level 8) "binary")
498 ((= transfer-level 8) "8bit")
501 (defvar mime-transfer-level-string
502 (mime-encoding-name mime-transfer-level 'not-omit)
503 "A string formatted version of `mime-transfer-level'.")
504 (make-variable-buffer-local 'mime-transfer-level-string)
506 ;;; @@ about content transfer encoding
508 (defvar mime-content-transfer-encoding-priority-list
509 '(nil "8bit" "binary"))
511 ;;; @@ about message inserting
514 (defcustom mime-edit-yank-ignored-field-list
515 '("Received" "Approved" "Path" "Replied" "Status"
516 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
517 "List of ignored header fields when inserting message/rfc822.
518 Each elements are regexp of field-name."
520 :type '(repeat regexp))
522 (defvar mime-edit-message-inserter-alist nil)
523 (defvar mime-edit-mail-inserter-alist nil)
526 ;;; @@ about message splitting
529 (defcustom mime-edit-split-message t
530 "*Split large message if it is non-nil."
534 (defcustom mime-edit-message-default-max-lines 5000
535 "*Default maximum lines of a message."
539 (defcustom mime-edit-message-max-lines-alist
540 '((news-reply-mode . 500))
541 "Alist of `major-mode' vs maximum lines of a message.
542 If it is not specified for a `major-mode',
543 `mime-edit-message-default-max-lines' is used."
547 (defconst mime-edit-split-ignored-field-regexp
548 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
550 (defcustom mime-edit-split-blind-field-regexp
551 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
552 "*Regular expression to match field-name to be ignored when split sending."
556 (defvar mime-edit-split-message-sender-alist nil)
558 (defvar mime-edit-news-reply-mode-server-running nil)
564 (defconst mime-edit-single-part-tag-regexp
565 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
566 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
568 (defconst mime-edit-quoted-single-part-tag-regexp
569 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
571 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
573 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
575 (defconst mime-edit-beginning-tag-regexp
576 (regexp-or mime-edit-single-part-tag-regexp
577 mime-edit-multipart-beginning-regexp))
579 (defconst mime-edit-end-tag-regexp
580 (regexp-or mime-edit-single-part-tag-regexp
581 mime-edit-multipart-end-regexp))
583 (defconst mime-edit-tag-regexp
584 (regexp-or mime-edit-single-part-tag-regexp
585 mime-edit-multipart-beginning-regexp
586 mime-edit-multipart-end-regexp))
588 (defconst mime-tag-format "--[[%s]]"
589 "Control-string making a MIME tag.")
591 (defconst mime-tag-format-with-encoding "--[[%s][%s]]"
592 "Control-string making a MIME tag with encoding.")
595 ;;; @@ multipart boundary
598 (defvar mime-multipart-boundary "Multipart"
599 "*Boundary of a multipart message.")
602 ;;; @@ optional header fields
605 (defcustom mime-edit-insert-user-agent-field t
606 "*If non-nil, insert User-Agent header field."
610 (defvar mime-edit-user-agent-value
611 (concat (mime-product-name mime-user-interface-product)
613 (mapconcat #'number-to-string
614 (mime-product-version mime-user-interface-product) ".")
616 (mime-product-code-name mime-user-interface-product)
618 (mime-product-name mime-library-product)
620 (mapconcat #'number-to-string
621 (mime-product-version mime-library-product) ".")
623 (mime-product-code-name mime-library-product)
625 (if (fboundp 'apel-version)
626 (concat (apel-version) " "))
627 (if (featurep 'xemacs)
628 (concat (cond ((featurep 'utf-2000)
629 (concat "UTF-2000-MULE/" utf-2000-version))
630 ((featurep 'mule) "MULE"))
632 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
635 (substring emacs-version 0 (match-end 0))
636 (cond ((and (boundp 'xemacs-betaname)
638 ;; It does not exist in XEmacs
639 ;; versions prior to 20.3.
640 (concat " " xemacs-betaname))
641 ((and (boundp 'emacs-patch-level)
643 ;; It does not exist in FSF Emacs or in
644 ;; XEmacs versions earlier than 21.1.1.
645 (format " (patch %d)" emacs-patch-level))
647 " (" xemacs-codename ") ("
648 system-configuration ")")
649 " (" emacs-version ")"))
650 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
651 (substring emacs-version 0 (match-beginning 0))
654 (if (boundp 'enable-multibyte-characters)
656 " (" system-configuration ")"
657 (if enable-multibyte-characters
658 (concat " MULE/" mule-version)
659 " (with unibyte mode)")
660 (if (featurep 'meadow)
661 (let ((mver (Meadow-version)))
662 (if (string-match "^Meadow-" mver)
666 (concat "MULE/" mule-version
667 " (based on Emacs " ver ")"))
668 (concat "Emacs/" ver " (" system-configuration ")")))))
669 "Body of User-Agent field.
670 If variable `mime-edit-insert-user-agent-field' is not nil, it is
671 inserted into message header.")
677 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
678 "Specify MIME tspecials.
679 Tspecials means any character that matches with it in header must be quoted.")
681 (defconst mime-edit-mime-version-value
682 ; (concat "1.0 (generated by " mime-edit-version ")")
684 "MIME version number.")
686 ;;; @ keymap and menu
689 (defvar mime-edit-mode-flag nil)
690 (make-variable-buffer-local 'mime-edit-mode-flag)
692 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
693 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
694 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
695 "Keymap for MIME-Edit mode commands to insert entity or set status.")
697 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
698 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
699 (define-key mime-edit-mode-entity-map "i" 'mime-edit-insert-text-file)
700 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
701 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
702 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
703 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
704 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
705 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
706 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
707 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
709 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
710 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
711 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
712 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
713 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
714 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
715 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
716 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
717 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
718 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
720 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
721 "Keymap prefix for MIME-Edit mode commands about enclosure.")
722 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
723 "Keymap for MIME-Edit mode commands about enclosure.")
725 (define-key mime-edit-mode-enclosure-map
726 "\C-a" 'mime-edit-enclose-alternative-region)
727 (define-key mime-edit-mode-enclosure-map
728 "\C-p" 'mime-edit-enclose-parallel-region)
729 (define-key mime-edit-mode-enclosure-map
730 "\C-m" 'mime-edit-enclose-mixed-region)
731 (define-key mime-edit-mode-enclosure-map
732 "\C-d" 'mime-edit-enclose-digest-region)
733 (define-key mime-edit-mode-enclosure-map
734 "\C-s" 'mime-edit-enclose-pgp-signed-region)
735 (define-key mime-edit-mode-enclosure-map
736 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
737 (define-key mime-edit-mode-enclosure-map
738 "\C-q" 'mime-edit-enclose-quote-region)
740 (defvar mime-edit-mode-map (make-sparse-keymap)
741 "Keymap for MIME-Edit mode commands.")
742 (define-key mime-edit-mode-map
743 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
744 (define-key mime-edit-mode-map
745 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
747 (defconst mime-edit-menu-title "MIME-Edit")
749 (defconst mime-edit-menu-list
750 '((mime-help "Describe MIME editor mode" mime-edit-help)
751 (file "Insert File" mime-edit-insert-file)
752 (external "Insert External" mime-edit-insert-external)
753 (voice "Insert Voice" mime-edit-insert-voice)
754 (message "Insert Message" mime-edit-insert-message)
755 (mail "Insert Mail" mime-edit-insert-mail)
756 (signature "Insert Signature" mime-edit-insert-signature)
757 (text "Insert Text" mime-edit-insert-text)
758 (tag "Insert Tag" mime-edit-insert-tag)
759 (alternative "Enclose as alternative"
760 mime-edit-enclose-alternative-region)
761 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
762 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
763 (digest "Enclose as digest" mime-edit-enclose-digest-region)
764 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
765 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
766 (quote "Verbatim region" mime-edit-enclose-quote-region)
767 (key "Insert Public Key" mime-edit-insert-key)
768 (split "About split" mime-edit-set-split)
769 (sign "About sign" mime-edit-set-sign)
770 (encrypt "About encryption" mime-edit-set-encrypt)
771 (preview "Preview Message" mime-edit-preview-message)
772 (level "Toggle transfer-level" mime-edit-toggle-transfer-level))
773 "MIME-edit menubar entry.")
775 (static-if (featurep 'xemacs)
776 ;; modified by Pekka Marjola <pema@iki.fi>
777 ;; 1995/9/5 (c.f. [tm-en:69])
779 (defun mime-edit-define-menu-for-xemacs ()
780 "Define menu for XEmacs."
781 (cond ((featurep 'menubar)
782 (make-local-variable 'current-menubar)
783 (set-buffer-menubar current-menubar)
786 (cons mime-edit-menu-title
789 (vector (nth 1 item)(nth 2 item)
790 mime-edit-mode-flag)))
791 mime-edit-menu-list))))))
792 ;; modified by Steven L. Baur <steve@miranova.com>
793 ;; 1995/12/6 (c.f. [tm-en:209])
794 (or (boundp 'mime-edit-popup-menu-for-xemacs)
795 (setq mime-edit-popup-menu-for-xemacs
796 (append '("MIME Commands" "---")
797 (mapcar (function (lambda (item)
801 mime-edit-menu-list)))))
802 ;; Bogus check. Nemacs is not supported.
803 ;;(>= emacs-major-version 19)
804 (define-key mime-edit-mode-map [menu-bar mime-edit]
805 (cons mime-edit-menu-title
806 (make-sparse-keymap mime-edit-menu-title)))
809 (define-key mime-edit-mode-map
810 (vector 'menu-bar 'mime-edit (car item))
811 (cons (nth 1 item)(nth 2 item)))))
812 (reverse mime-edit-menu-list)))
817 (defmacro mime-edit-insert-place (type-list &rest body)
819 (if (or (member (intern (concat (car ,type-list) "/" (cadr ,type-list)))
820 mime-edit-attach-at-end-type)
821 (member (intern (car ,type-list))
822 mime-edit-attach-at-end-type))
823 (goto-char (point-max)))
829 (defvar mime-edit-touched-flag nil)
832 (defun mime-edit-mode ()
833 "MIME minor mode for editing the tagged MIME message.
835 In this mode, basically, the message is composed in the tagged MIME
836 format. The message tag looks like:
838 --[[text/plain; charset=ISO-2022-JP][7bit]]
840 The tag specifies the MIME content type, subtype, optional parameters
841 and transfer encoding of the message following the tag. Messages
842 without any tag are treated as `text/plain' by default. Charset and
843 transfer encoding are automatically defined unless explicitly
844 specified. Binary messages such as audio and image are usually
845 hidden. The messages in the tagged MIME format are automatically
846 translated into a MIME compliant message when exiting this mode.
848 Available charsets depend on Emacs version being used. The following
849 lists the available charsets of each emacs.
851 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
852 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
853 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
854 CN-BIG5 and ISO-2022-INT-1 are available.
856 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
857 be used to represent multilingual text in intermixed manner. Any
858 languages that has no registered charset are represented as either
859 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
861 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
862 without mule, please set variable `default-mime-charset'. This
863 variable must be symbol of which name is a MIME charset.
865 If you want to add more charsets in mule, please set variable
866 `charsets-mime-charset-alist'. This variable must be alist of which
867 key is list of charset and value is symbol of MIME charset. If name
868 of coding-system is different as MIME charset, please set variable
869 `mime-charset-coding-system-alist'. This variable must be alist of
870 which key is MIME charset and value is coding-system.
872 Following commands are available in addition to major mode commands:
875 \\[mime-edit-insert-text] insert a text message.
876 \\[mime-edit-insert-file] insert a (binary) file.
877 \\[mime-edit-insert-external] insert a reference to external body.
878 \\[mime-edit-insert-voice] insert a voice message.
879 \\[mime-edit-insert-message] insert a mail or news message.
880 \\[mime-edit-insert-mail] insert a mail message.
881 \\[mime-edit-insert-signature] insert a signature file at end.
882 \\[mime-edit-insert-key] insert PGP public key.
883 \\[mime-edit-insert-tag] insert a new MIME tag.
885 \[make enclosure (maybe multipart)\]
886 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
887 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
888 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
889 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
890 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
891 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
892 \\[mime-edit-enclose-quote-region] enclose as verbose mode
893 (to avoid to expand tags)
896 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
897 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
898 \\[mime-edit-set-split] set message splitting mode.
899 \\[mime-edit-set-sign] set PGP-sign mode.
900 \\[mime-edit-set-encrypt] set PGP-encryption mode.
901 \\[mime-edit-preview-message] preview editing MIME message.
902 \\[mime-edit-exit] exit and translate into a MIME
904 \\[mime-edit-help] show this help.
905 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
908 Additional commands are available in some major modes:
909 C-c C-c exit, translate and run the original command.
910 C-c C-s exit, translate and run the original command.
912 The following is a message example written in the tagged MIME format.
913 TABs at the beginning of the line are not a part of the message:
915 This is a conventional plain text. It should be translated
918 This is also a plain text. But, it is explicitly specified as
920 --[[text/plain; charset=ISO-8859-1]]
921 This is also a plain text. But charset is specified as
924 ¡Hola! Buenos días. ¿Cómo está usted?
926 This is a <bold>enriched text</bold>.
927 --[[image/gif][base64]]...image encoded in base64 here...
928 --[[audio/basic][base64]]...audio encoded in base64 here...
930 User customizable variables (not documented all of them):
932 Specifies a key prefix for MIME minor mode commands.
934 mime-ignore-preceding-spaces
935 Preceding white spaces in a message body are ignored if non-nil.
937 mime-ignore-trailing-spaces
938 Trailing white spaces in a message body are ignored if non-nil.
941 Hide a non-textual body message encoded in base64 after insertion
945 A number of network transfer level. It should be bigger than 7.
946 If you are in 8bit-through environment, please set to 8.
948 mime-edit-voice-recorder
949 Specifies a function to record a voice message and encode it.
950 The function `mime-edit-voice-recorder-for-sun' is for Sun
954 Turning on MIME mode calls the value of mime-edit-mode-hook, if
957 mime-edit-translate-hook
958 The value of mime-edit-translate-hook is called just before translating
959 the tagged MIME format into a MIME compliant message if it is
960 non-nil. If the hook call the function mime-edit-insert-signature,
961 the signature file will be inserted automatically.
964 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
967 (if mime-edit-mode-flag
969 (if mime-edit-touched-flag
971 (make-local-variable 'mime-edit-touched-flag)
972 (setq mime-edit-touched-flag t)
973 (turn-on-mime-edit))))
976 (static-if (featurep 'xemacs)
977 (add-minor-mode 'mime-edit-mode-flag
978 '((" MIME-Edit " mime-transfer-level-string))
982 (set-alist 'minor-mode-alist
984 '((" MIME-Edit " mime-transfer-level-string)))
985 (set-alist 'minor-mode-map-alist
991 (defun turn-on-mime-edit ()
992 "Unconditionally turn on MIME-Edit mode."
994 (if mime-edit-mode-flag
995 (error "You are already editing a MIME message")
996 (setq mime-edit-mode-flag t)
998 ;; Set transfer level into mode line
1000 (setq mime-transfer-level-string
1001 (mime-encoding-name mime-transfer-level 'not-omit))
1002 (force-mode-line-update)
1004 ;; Define menu for XEmacs.
1005 (if (featurep 'xemacs)
1006 (mime-edit-define-menu-for-xemacs))
1010 ;; I don't care about saving these.
1011 (setq paragraph-start
1012 (regexp-or mime-edit-single-part-tag-regexp
1014 (setq paragraph-separate
1015 (regexp-or mime-edit-single-part-tag-regexp
1016 paragraph-separate))
1017 (run-hooks 'mime-edit-mode-hook)
1019 (substitute-command-keys
1020 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
1023 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1026 (defun mime-edit-exit (&optional nomime no-error)
1027 "Translate the tagged MIME message into a MIME compliant message.
1028 When NOMIME is nil, encode a message in the buffer into MIME.
1029 Otherwise, just returns to previous mode. If NO-ERROR is non-nil,
1030 no errors will be signaled even if it is not MIME-Edit mode."
1032 (if (not mime-edit-mode-flag)
1034 (error "You aren't editing a MIME message"))
1037 (run-hooks 'mime-edit-translate-hook)
1038 (mime-edit-translate-buffer)))
1039 ;; Restore previous state.
1040 (setq mime-edit-mode-flag nil)
1041 (if (and (featurep 'xemacs)
1042 (featurep 'menubar))
1043 (delete-menu-item (list mime-edit-menu-title)))
1045 (set-buffer-modified-p (buffer-modified-p))
1046 (run-hooks 'mime-edit-exit-hook)
1047 (message "Exit MIME editor mode.")))
1049 (defun mime-edit-maybe-translate ()
1051 (mime-edit-exit nil t)
1052 (call-interactively 'mime-edit-maybe-split-and-send))
1054 (defun mime-edit-help ()
1055 "Show help message about MIME mode."
1057 (with-output-to-temp-buffer "*Help*"
1058 (princ "MIME editor mode:\n")
1059 (princ (documentation 'mime-edit-mode))
1060 (print-help-return-message)))
1062 (defun mime-edit-insert-text (&optional subtype)
1063 "Insert a text message.
1064 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1065 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1067 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1069 (if (looking-at mime-edit-single-part-tag-regexp)
1071 ;; Make a space between the following message.
1074 (if (and (member (cadr ret) '("enriched"))
1075 (fboundp 'enriched-mode))
1077 (if (boundp 'enriched-mode)
1078 (enriched-mode -1))))))
1080 (defun mime-edit-insert-text-file (file &optional verbose)
1081 "Insert a text message from a FILE.
1082 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1083 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1084 (interactive "fInsert file as a MIME text: \nP")
1085 (let* ((guess (mime-find-file-type file))
1088 (parameters (nth 2 guess))
1089 ;; (encoding (nth 3 guess))
1090 (disposition-type (nth 4 guess))
1091 (disposition-params (nth 5 guess))
1093 (setq subtype (mime-prompt-for-subtype type subtype))
1094 ;; (if (or (interactive-p) verbose)
1095 ;; (setq encoding (mime-prompt-for-encoding encoding)))
1097 (setq disposition-type (mime-prompt-for-disposition disposition-type)))
1098 (if (or (consp parameters) (stringp disposition-type))
1099 (let ((rest parameters) cell attribute value)
1100 (setq parameters "")
1102 (setq cell (car rest))
1103 (setq attribute (car cell))
1104 (setq value (cdr cell))
1105 (if (eq value 'file)
1106 (setq value (std11-wrap-as-quoted-string
1107 (file-name-nondirectory file))))
1108 (setq parameters (concat parameters "; " attribute "=" value))
1109 (setq rest (cdr rest)))
1110 (if disposition-type
1113 (concat parameters "\n"
1114 "Content-Disposition: " disposition-type))
1115 (setq rest disposition-params)
1117 (setq cell (car rest))
1118 (setq attribute (car cell))
1119 (setq value (cdr cell))
1120 (if (eq value 'file)
1121 (setq value (std11-wrap-as-quoted-string
1122 (file-name-nondirectory file))))
1124 (concat parameters "; " attribute "=" value))
1125 (setq rest (cdr rest)))))))
1126 (mime-edit-insert-place
1128 (mime-edit-insert-tag type subtype parameters)
1129 ;; (if (stringp encoding)
1130 ;; (mime-edit-define-encoding encoding))
1132 (let ((ret (insert-file-contents file)))
1133 (forward-char (cadr ret))
1134 (if (and (not (eobp))
1135 (not (looking-at mime-edit-single-part-tag-regexp)))
1136 (insert (mime-make-text-tag) "\n")))))))
1138 (defun mime-edit-insert-file (file &optional verbose)
1139 "Insert a message from a FILE.
1140 If VERBOSE is non-nil, it will prompt for Content-Type,
1141 Content-Transfer-Encoding and Content-Disposition headers."
1142 (interactive "fInsert file as MIME message: \nP")
1143 (let* ((guess (mime-find-file-type file))
1144 (type (nth 0 guess))
1145 (subtype (nth 1 guess))
1146 (parameters (nth 2 guess))
1147 (encoding (nth 3 guess))
1148 (disposition-type (nth 4 guess))
1149 (disposition-params (nth 5 guess))
1152 (setq type (mime-prompt-for-type type)
1153 subtype (mime-prompt-for-subtype type subtype)))
1154 (if (or (interactive-p) verbose)
1155 (setq encoding (mime-prompt-for-encoding encoding)))
1157 (setq disposition-type (mime-prompt-for-disposition disposition-type)))
1158 (if (or (consp parameters) (stringp disposition-type))
1159 (let ((rest parameters) cell attribute value)
1160 (setq parameters "")
1161 (when (string= type "text")
1163 (let (candidates candidate eol eol-string)
1164 (set-buffer-multibyte nil)
1165 (insert-file-contents-as-binary file)
1166 (setq candidates (detect-coding-region (point-min) (point-max)))
1167 (setq candidate (if (listp candidates)
1170 (if (memq (static-if (featurep 'xemacs)
1171 (coding-system-name candidate)
1173 '(undecided undecided-unix
1174 undecided-dos undecided-mac))
1175 (setq candidate 'raw-text))
1176 (setq eol (coding-system-eol-type candidate))
1178 (static-if (featurep 'xemacs)
1181 (setq eol-string "\n"))
1183 (static-if (featurep 'xemacs)
1186 (setq eol-string "\r")))
1187 (goto-char (point-min))
1189 (while (search-forward eol-string nil t)
1190 (replace-match "\r\n")))
1191 (setq string (buffer-string))
1193 (concat parameters "; charset="
1195 (coding-system-to-mime-charset
1198 (setq cell (car rest))
1199 (setq attribute (car cell))
1200 (setq value (cdr cell))
1201 (if (eq value 'file)
1202 (setq value (std11-wrap-as-quoted-string
1203 (file-name-nondirectory file))))
1204 (setq parameters (concat parameters "; " attribute "=" value))
1205 (setq rest (cdr rest)))
1206 (if disposition-type
1209 (concat parameters "\n"
1210 "Content-Disposition: " disposition-type))
1211 (setq rest disposition-params)
1213 (setq cell (car rest))
1214 (setq attribute (car cell))
1215 (setq value (cdr cell))
1216 (if (eq value 'file)
1217 (setq value (std11-wrap-as-quoted-string
1218 (file-name-nondirectory file))))
1220 (concat parameters "; " attribute "=" value))
1221 (setq rest (cdr rest)))))))
1222 (mime-edit-insert-place
1224 (mime-edit-insert-tag type subtype parameters)
1226 (mime-edit-insert-binary-string string encoding)
1227 (mime-edit-insert-binary-file file encoding)))))
1229 (defun mime-edit-insert-external ()
1230 "Insert a reference to external body."
1232 (let* ((pritype (mime-prompt-for-type))
1233 (subtype (mime-prompt-for-subtype pritype))
1234 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1235 (mime-edit-insert-place
1236 '("message" "external-body")
1237 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1239 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1243 (insert "Content-Type: "
1244 pritype "/" subtype (or parameters "") "\n"))
1245 (if (and (not (eobp))
1246 (not (looking-at mime-edit-single-part-tag-regexp)))
1247 (insert (mime-make-text-tag) "\n")))))
1249 (defun mime-edit-insert-voice ()
1250 "Insert a voice message."
1254 "What transfer encoding: "
1255 (mime-encoding-alist) nil t nil)))
1256 (mime-edit-insert-place
1258 (mime-edit-insert-tag "audio" "basic" nil)
1259 (mime-edit-define-encoding encoding)
1261 (narrow-to-region (1- (point))(point))
1263 (funcall mime-edit-voice-recorder encoding)
1266 (invisible-region (point-min)(point-max))
1267 (goto-char (point-max))))))))
1269 (defun mime-edit-insert-signature (&optional arg)
1270 "Insert a signature file."
1272 (let ((signature-insert-hook
1275 (let ((items (mime-find-file-type signature-file-name)))
1276 (apply (function mime-edit-insert-tag)
1277 (car items) (cadr items) (list (caddr items))))))))
1278 (insert-signature arg)))
1281 ;; Insert a new tag around a point.
1283 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1284 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1285 If nothing is inserted, return nil."
1288 (mime-edit-goto-tag)
1289 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1290 (< (match-beginning 0) p)
1291 (< p (match-end 0)))
1292 (goto-char (match-beginning 0))
1299 (mime-prompt-for-type)))
1302 (mime-prompt-for-subtype pritype)))
1305 (mime-prompt-for-parameters pritype subtype delimiter)))
1306 ;; Make a new MIME tag.
1307 (setq newtag (mime-make-tag pritype subtype parameters))
1308 ;; Find an current MIME tag.
1311 (if (mime-edit-goto-tag)
1312 (buffer-substring (match-beginning 0) (match-end 0))
1313 ;; Assume content type is 'text/plan'.
1314 (mime-make-tag "text" "plain"))))
1315 ;; We are only interested in TEXT.
1317 (not (mime-test-content-type
1318 (mime-edit-get-contype oldtag) "text")))
1321 (if (or (not oldtag) ;Not text
1322 (or mime-ignore-same-text-tag
1323 (not (string-equal oldtag newtag))))
1325 ;; Mark the beginning of the tag for convenience.
1326 (push-mark (point) 'nomsg)
1327 (insert newtag "\n")
1328 (list pritype subtype parameters) ;New tag is created.
1330 ;; Restore previous point.
1332 nil ;Nothing is created.
1335 ;; #### This should be merged into the function below but for now,
1336 ;; don't change APIs.
1337 (defun mime-edit-insert-binary-string (string &optional encoding)
1338 "Insert binary STRING at point.
1339 Optional argument ENCODING specifies an encoding method such as base64."
1340 (let* ((tagend (1- (point))) ;End of the tag
1341 (hide-p (and mime-auto-hide-body
1344 (let ((en (downcase encoding)))
1345 (or (string-equal en "7bit")
1346 (string-equal en "8bit")
1347 (string-equal en "binary")))))))
1349 (narrow-to-region tagend (point))
1352 ;; #### @!#$%@!${$@}
1353 (set-buffer-multibyte nil)
1355 ;; #### Why mime-encode-string doesn't exist?
1356 (mime-encode-region (point-min) (point-max)
1357 (or encoding "7bit"))
1361 (invisible-region (point-min) (point-max))
1362 (goto-char (point-max)))
1363 (goto-char (point-max))))
1364 (unless (or (looking-at mime-edit-tag-regexp)
1365 (= (point)(point-max)))
1367 (mime-edit-insert-tag "text" "plain"))
1368 ;; Define encoding even if it is 7bit.
1369 (if (stringp encoding)
1371 (goto-char tagend) ; Make sure which line the tag is on.
1372 (mime-edit-define-encoding encoding)))))
1374 (defun mime-edit-insert-binary-file (file &optional encoding)
1375 "Insert binary FILE at point.
1376 Optional argument ENCODING specifies an encoding method such as base64."
1377 (let* ((tagend (1- (point))) ;End of the tag
1378 (hide-p (and mime-auto-hide-body
1381 (let ((en (downcase encoding)))
1382 (or (string-equal en "7bit")
1383 (string-equal en "8bit")
1384 (string-equal en "binary")))))))
1386 (narrow-to-region tagend (point))
1387 (mime-insert-encoded-file file encoding)
1390 (invisible-region (point-min) (point-max))
1391 (goto-char (point-max)))
1392 (goto-char (point-max))))
1393 (unless (or (looking-at mime-edit-tag-regexp)
1394 (= (point)(point-max)))
1396 (mime-edit-insert-tag "text" "plain"))
1397 ;; Define encoding even if it is 7bit.
1398 (if (stringp encoding)
1400 (goto-char tagend) ; Make sure which line the tag is on.
1401 (mime-edit-define-encoding encoding)))))
1404 ;; Commands work on a current message flagment.
1406 (defun mime-edit-goto-tag ()
1407 "Search for the beginning of the tagged MIME message."
1408 (let ((current (point)))
1409 (if (looking-at mime-edit-tag-regexp)
1411 ;; At first, go to the end.
1412 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1413 (goto-char (1- (match-beginning 0))) ;For multiline tag
1416 (goto-char (point-max))))
1417 ;; Then search for the beginning.
1418 (re-search-backward mime-edit-end-tag-regexp nil t)
1419 (or (looking-at mime-edit-beginning-tag-regexp)
1420 ;; Restore previous point.
1425 (defun mime-edit-content-beginning ()
1426 "Return the point of the beginning of content."
1428 (let ((beg (save-excursion
1429 (beginning-of-line) (point))))
1430 (if (mime-edit-goto-tag)
1431 (let ((top (point)))
1432 (goto-char (match-end 0))
1433 (if (and (= beg top)
1434 (= (following-char) ?\^M))
1438 ;; Default text/plain tag.
1439 (goto-char (point-min))
1441 (concat "\n" (regexp-quote mail-header-separator)
1442 (if mime-ignore-preceding-spaces
1443 "[ \t\n]*\n" "\n")) nil 'move)
1446 (defun mime-edit-content-end ()
1447 "Return the point of the end of content."
1449 (if (mime-edit-goto-tag)
1451 (goto-char (match-end 0))
1452 (if (invisible-p (point))
1453 (next-visible-point (point))
1454 ;; Move to the end of this text.
1455 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1456 ;; Don't forget a multiline tag.
1457 (goto-char (match-beginning 0)))
1459 ;; Assume the message begins with text/plain.
1460 (goto-char (mime-edit-content-beginning))
1461 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1462 ;; Don't forget a multiline tag.
1463 (goto-char (match-beginning 0)))
1466 (defun mime-edit-define-charset (charset)
1467 "Set charset of current tag to CHARSET."
1469 (if (mime-edit-goto-tag)
1470 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1471 (delete-region (match-beginning 0) (match-end 0))
1474 (mime-edit-set-parameter
1475 (mime-edit-get-contype tag)
1477 (let ((comment (get charset 'mime-charset-comment)))
1479 (concat (upcase (symbol-name charset)) " (" comment ")")
1480 (upcase (symbol-name charset)))))
1481 (mime-edit-get-encoding tag)))))))
1483 (defun mime-edit-define-encoding (encoding)
1484 "Set encoding of current tag to ENCODING."
1486 (if (mime-edit-goto-tag)
1487 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1488 (delete-region (match-beginning 0) (match-end 0))
1489 (insert (mime-create-tag (mime-edit-get-contype tag) encoding))))))
1491 (defun mime-edit-choose-charset ()
1492 "Choose charset of a text following current point."
1493 (detect-mime-charset-region (point) (mime-edit-content-end)))
1495 (defun mime-make-text-tag (&optional subtype)
1496 "Make a tag for a text after current point.
1497 Subtype of text type can be specified by an optional argument SUBTYPE.
1498 Otherwise, it is obtained from `mime-content-types'."
1499 (let* ((pritype "text")
1500 (subtype (or subtype
1501 (car (car (cdr (assoc pritype mime-content-types)))))))
1502 ;; Charset should be defined later.
1503 (mime-make-tag pritype subtype)))
1506 ;; Tag handling functions
1508 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1509 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1510 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1514 (defun mime-create-tag (contype &optional encoding)
1515 "Make a tag with CONTENT-TYPE and optional ENCODING."
1516 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1519 (defun mime-edit-get-contype (tag)
1520 "Return Content-Type (including parameters) of TAG."
1522 (or (string-match mime-edit-single-part-tag-regexp tag)
1523 (string-match mime-edit-multipart-beginning-regexp tag)
1524 (string-match mime-edit-multipart-end-regexp tag))
1525 (substring tag (match-beginning 1) (match-end 1))))
1527 (defun mime-edit-get-encoding (tag)
1528 "Return encoding of TAG."
1530 (string-match mime-edit-single-part-tag-regexp tag)
1532 (not (= (match-beginning 3) (match-end 3)))
1533 (substring tag (match-beginning 3) (match-end 3))))
1535 (defun mime-get-parameter (contype parameter)
1536 "For given CONTYPE return value for PARAMETER.
1537 Nil if no such parameter."
1541 (regexp-quote parameter)
1542 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1544 (substring contype (match-beginning 1) (match-end 1))
1545 nil ;No such parameter
1548 (defun mime-edit-set-parameter (contype parameter value)
1549 "For given CONTYPE set PARAMETER to VALUE."
1550 (let (ctype opt-fields)
1551 (if (string-match "\n[^ \t\n\r]+:" contype)
1552 (setq ctype (substring contype 0 (match-beginning 0))
1553 opt-fields (substring contype (match-beginning 0)))
1554 (setq ctype contype))
1558 (regexp-quote parameter)
1559 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1562 (concat (substring ctype 0 (match-beginning 1))
1564 (substring contype (match-end 1)))
1565 ;; This field makes two CDP header when charset parameter is present.
1567 (concat ctype "; " parameter "=" value opt-fields))))
1569 (defun mime-strip-parameters (contype)
1570 "Return primary content-type and subtype without parameters for CONTYPE."
1571 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1572 (substring contype (match-beginning 1) (match-end 1)) nil))
1574 (defun mime-test-content-type (contype type &optional subtype)
1575 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1576 (and (stringp contype)
1579 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1580 (downcase contype))))
1585 (defun mime-find-file-type (file)
1586 "Guess Content-Type, subtype, and parameters from FILE."
1588 (guesses mime-file-types))
1589 (while (and (not guess) guesses)
1590 (if (string-match (car (car guesses)) file)
1591 (setq guess (cdr (car guesses))))
1592 (setq guesses (cdr guesses)))
1595 (defun mime-prompt-for-type (&optional default)
1596 "Ask for Content-type."
1598 ;; Repeat until primary content type is specified.
1599 (while (string-equal type "")
1601 (completing-read "What content type: "
1604 'require-match ;Type must be specified.
1606 (if (string-equal type "")
1608 (message "Content type is required.")
1613 (defun mime-prompt-for-subtype (type &optional default)
1614 "Ask for subtype of media-type TYPE."
1615 (let ((subtypes (cdr (assoc type mime-content-types))))
1617 (assoc default subtypes))
1618 (setq default (car (car subtypes)))))
1623 "What content subtype: (default " default ") ")
1624 "What content subtype: ")
1625 (cdr (assoc type mime-content-types))
1627 'require-match ;Subtype must be specified.
1629 (if (string-equal answer "") default answer)))
1631 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1632 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1633 Optional DELIMITER specifies parameter delimiter (';' by default)."
1634 (let* ((delimiter (or delimiter "; "))
1639 (mime-prompt-for-parameters-1
1641 (cdr (assoc pritype mime-content-types))))))
1643 (if (and (stringp parameters)
1644 (not (string-equal parameters "")))
1645 (concat delimiter parameters)
1646 "" ;"" if no parameters
1649 (defun mime-prompt-for-parameters-1 (optlist)
1650 (apply (function append)
1651 (mapcar (function mime-prompt-for-parameter) optlist)))
1653 (defun mime-prompt-for-parameter (parameter)
1655 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1656 (let* ((prompt (car parameter))
1657 (choices (mapcar (function
1659 (if (consp e) e (list e))))
1661 (default (car (car choices)))
1667 (concat "What " prompt
1669 (if (string-equal default "") "\"\"" default)
1671 choices nil nil ""))
1672 ;; If nothing is selected, use default.
1673 (if (string-equal answer "")
1674 (setq answer default)))
1676 (read-string (concat "What " prompt ": "))))
1677 (cons (if (and answer
1678 (not (string-equal answer "")))
1680 ;; Note: control characters ignored!
1681 (if (string-match mime-tspecials-regexp answer)
1682 (concat "\"" answer "\"") answer)))
1683 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))))
1685 (defun mime-prompt-for-encoding (default)
1686 "Ask for Content-Transfer-Encoding."
1691 "What transfer encoding: "
1692 (mime-encoding-alist) nil t default))
1696 (defun mime-prompt-for-disposition (default)
1697 "Prompt Content-Disposition"
1698 (completing-read (concat "What disposition type (default "
1700 mime-content-disposition-types
1705 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1708 (defvar mime-edit-translate-buffer-hook
1709 '(mime-edit-pgp-enclose-buffer
1710 mime-edit-translate-body
1711 mime-edit-translate-header))
1713 (defun mime-edit-translate-header ()
1714 "Encode the message header into network representation."
1715 (eword-encode-header 'code-conversion)
1716 (run-hooks 'mime-edit-translate-header-hook))
1718 (defun mime-edit-translate-buffer ()
1719 "Encode the tagged MIME message in current buffer in MIME compliant message."
1722 (if (catch 'mime-edit-error
1724 (run-hooks 'mime-edit-translate-buffer-hook)))
1727 (error "Translation error!"))))
1729 (defun mime-edit-find-inmost ()
1730 (goto-char (point-min))
1731 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1732 (let ((bb (match-beginning 0))
1734 (type (buffer-substring (match-beginning 1)(match-end 1)))
1736 (setq end-exp (format "--}-<<%s>>\n" type))
1738 (if (re-search-forward end-exp nil t)
1739 (setq eb (match-beginning 0))
1740 (setq eb (point-max)))
1741 (narrow-to-region be eb)
1743 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1745 (narrow-to-region (match-beginning 0)(point-max))
1746 (mime-edit-find-inmost))
1748 (list type bb be eb)))))
1750 (defun mime-edit-process-multipart-1 (boundary)
1751 (let ((ret (mime-edit-find-inmost)))
1753 (let ((type (car ret))
1754 (bb (nth 1 ret))(be (nth 2 ret))
1756 (narrow-to-region bb eb)
1757 (delete-region bb be)
1758 (setq bb (point-min))
1759 (setq eb (point-max))
1762 (if (looking-at mime-edit-multipart-end-regexp)
1763 (let ((beg (match-beginning 0))
1764 (end (match-end 0)))
1765 (delete-region beg end)
1766 (or (looking-at mime-edit-beginning-tag-regexp)
1768 (insert (concat (mime-make-text-tag) "\n")))))
1769 (cond ((string-equal type "quote")
1770 (mime-edit-enquote-region bb eb))
1771 ((string-equal type "pgp-signed")
1772 (mime-edit-sign-pgp-mime bb eb boundary))
1773 ((string-equal type "pgp-encrypted")
1774 (mime-edit-encrypt-pgp-mime bb eb boundary))
1775 ((string-equal type "kazu-signed")
1776 (mime-edit-sign-pgp-kazu bb eb boundary))
1777 ((string-equal type "kazu-encrypted")
1778 (mime-edit-encrypt-pgp-kazu bb eb boundary))
1779 ((string-equal type "smime-signed")
1780 (mime-edit-sign-smime bb eb boundary))
1781 ((string-equal type "smime-encrypted")
1782 (mime-edit-encrypt-smime bb eb boundary))
1785 (nth 2 (mime-edit-translate-region bb eb
1789 (format "--[[multipart/%s;
1790 boundary=\"%s\"][7bit]]\n"
1794 (defun mime-edit-enquote-region (beg end)
1797 (narrow-to-region beg end)
1799 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1800 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1801 (replace-match (concat "- " (substring tag 1))))))))
1803 (defun mime-edit-dequote-region (beg end)
1806 (narrow-to-region beg end)
1808 (while (re-search-forward
1809 mime-edit-quoted-single-part-tag-regexp nil t)
1810 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1811 (replace-match (concat "-" (substring tag 2))))))))
1813 (defvar mime-edit-pgp-user-id nil)
1815 (defun mime-edit-sign-pgp-mime (beg end boundary)
1818 (let* ((from (std11-field-body "From" mail-header-separator))
1820 (narrow-to-region beg end)
1821 (mime-edit-translate-region beg end boundary)))
1823 (encoding (nth 1 ret))
1824 (pgp-boundary (concat "pgp-sign-" boundary))
1827 (insert (format "Content-Type: %s\n" ctype))
1829 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1831 (or (let ((pgg-default-user-id
1832 (or mime-edit-pgp-user-id
1834 (nth 1 (std11-extract-address-components from))
1835 pgg-default-user-id))))
1836 (pgg-sign-region (point-min)(point-max)))
1837 (throw 'mime-edit-error 'pgp-error))
1839 (cdr (assq 'hash-algorithm
1840 (cdar (with-current-buffer pgg-output-buffer
1841 (pgg-parse-armor-region
1842 (point-min)(point-max))))))
1845 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1848 (insert (format "--[[multipart/signed;
1850 protocol=\"application/pgp-signature\"][7bit]]
1852 " pgp-boundary micalg pgp-boundary))
1853 (goto-char (point-max))
1854 (insert (format "\n--%s
1855 Content-Type: application/pgp-signature
1856 Content-Transfer-Encoding: 7bit
1859 (insert-buffer-substring pgg-output-buffer)
1860 (goto-char (point-max))
1861 (insert (format "\n--%s--\n" pgp-boundary))))))
1863 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1865 (defun mime-edit-make-encrypt-recipient-header ()
1866 (let* ((names mime-edit-encrypt-recipient-fields-list)
1868 (std11-field-bodies (cons "From" names)
1869 nil mail-header-separator))
1872 (setq values (cdr values))))
1873 (header (and (stringp from)
1874 (if (string-equal from "")
1876 (format "From: %s\n" from))))
1878 (while (and names values)
1879 (let ((name (car names))
1880 (value (car values)))
1881 (and (stringp value)
1882 (or (string-equal value "")
1884 (setq header (concat header name ": " value "\n")
1885 recipients (if recipients
1886 (concat recipients " ," value)
1888 (setq names (cdr names)
1889 values (cdr values)))
1890 (vector from recipients header)))
1892 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1895 (let (from recipients header)
1896 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1897 (setq from (aref ret 0)
1898 recipients (aref ret 1)
1899 header (aref ret 2)))
1900 (narrow-to-region beg end)
1902 (mime-edit-translate-region beg end boundary))
1904 (encoding (nth 1 ret))
1905 (pgp-boundary (concat "pgp-" boundary)))
1908 (insert (format "Content-Type: %s\n" ctype))
1910 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1912 (eword-encode-header)
1913 (or (let ((pgg-default-user-id
1914 (or mime-edit-pgp-user-id
1916 (nth 1 (std11-extract-address-components from))
1917 pgg-default-user-id))))
1919 (point-min) (point-max)
1920 (mapcar (lambda (recipient)
1921 (nth 1 (std11-extract-address-components
1923 (split-string recipients
1924 "\\([ \t\n]*,[ \t\n]*\\)+"))))
1925 (throw 'mime-edit-error 'pgp-error))
1926 (delete-region (point-min)(point-max))
1928 (insert (format "--[[multipart/encrypted;
1930 protocol=\"application/pgp-encrypted\"][7bit]]
1932 Content-Type: application/pgp-encrypted
1935 Content-Type: application/octet-stream
1936 Content-Transfer-Encoding: 7bit
1938 " pgp-boundary pgp-boundary pgp-boundary))
1939 (insert-buffer-substring pgg-output-buffer)
1940 (goto-char (point-max))
1941 (insert (format "\n--%s--\n" pgp-boundary)))))))
1943 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1946 (narrow-to-region beg end)
1948 (mime-edit-translate-region beg end boundary))
1950 (encoding (nth 1 ret)))
1952 (insert (format "Content-Type: %s\n" ctype))
1954 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1956 (or (pgg-sign-region beg (point-max) 'clearsign)
1957 (throw 'mime-edit-error 'pgp-error))
1960 "--[[application/pgp; format=mime][7bit]]\n")
1963 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1965 (let (recipients header)
1966 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1967 (setq recipients (aref ret 1)
1968 header (aref ret 2)))
1970 (narrow-to-region beg end)
1972 (mime-edit-translate-region beg end boundary))
1974 (encoding (nth 1 ret)))
1977 (insert (format "Content-Type: %s\n" ctype))
1979 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1981 (or (pgg-encrypt-region beg (point-max) recipients)
1982 (throw 'mime-edit-error 'pgp-error))
1985 "--[[application/pgp; format=mime][7bit]]\n")
1988 (defun mime-edit-sign-smime (beg end boundary)
1992 (narrow-to-region beg end)
1993 (mime-edit-translate-region beg end boundary)))
1995 (encoding (nth 1 ret))
1996 (smime-boundary (concat "smime-sign-" boundary)))
1998 (insert (format "Content-Type: %s\n" ctype))
2000 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
2002 (let (buffer-undo-list)
2003 (goto-char (point-min))
2004 (while (progn (end-of-line) (not (eobp)))
2007 (or (prog1 (smime-sign-region (point-min)(point-max))
2008 (push nil buffer-undo-list)
2009 (ignore-errors (undo)))
2010 (throw 'mime-edit-error 'pgp-error)))
2012 (insert (format "--[[multipart/signed;
2013 boundary=\"%s\"; micalg=sha1;
2014 protocol=\"application/pkcs7-signature\"][7bit]]
2016 " smime-boundary smime-boundary))
2017 (goto-char (point-max))
2018 (insert (format "\n--%s
2019 Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
2020 Content-Transfer-Encoding: base64
2021 Content-Disposition: attachment; filename=\"smime.p7s\"
2022 Content-Description: S/MIME Cryptographic Signature
2025 (insert-buffer-substring smime-output-buffer)
2026 (goto-char (point-max))
2027 (insert (format "\n--%s--\n" smime-boundary))))))
2029 (defun mime-edit-encrypt-smime (beg end boundary)
2033 (narrow-to-region beg end)
2034 (mime-edit-translate-region beg end boundary)))
2036 (encoding (nth 1 ret)))
2038 (insert (format "Content-Type: %s\n" ctype))
2040 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
2042 (goto-char (point-min))
2043 (while (progn (end-of-line) (not (eobp)))
2046 (or (smime-encrypt-region (point-min)(point-max))
2047 (throw 'mime-edit-error 'pgp-error))
2048 (delete-region (point-min)(point-max))
2049 (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
2050 Content-Disposition: attachment; filename=\"smime.p7m\"
2051 Content-Description: S/MIME Encrypted Message][base64]]\n")
2052 (insert-buffer-substring smime-output-buffer)))))
2054 (defsubst replace-space-with-underline (str)
2055 (mapconcat (function
2062 (defun mime-edit-make-boundary ()
2063 (concat mime-multipart-boundary "_"
2064 (replace-space-with-underline (current-time-string))))
2066 (defun mime-edit-translate-body ()
2067 "Encode the tagged MIME body in current buffer in MIME compliant message."
2070 (let ((boundary (mime-edit-make-boundary))
2073 (while (mime-edit-process-multipart-1
2074 (format "%s-%d" boundary i))
2077 ;; We are interested in message body.
2080 (goto-char (point-min))
2082 (concat "\n" (regexp-quote mail-header-separator)
2083 (if mime-ignore-preceding-spaces
2084 "[ \t\n]*\n" "\n")) nil 'move)
2088 (goto-char (point-max))
2089 (and mime-ignore-trailing-spaces
2090 (re-search-backward "[^ \t\n]\n" beg t)
2093 (setq ret (mime-edit-translate-region
2095 (format "%s-%d" boundary i)))))
2096 (mime-edit-dequote-region (point-min)(point-max))
2097 (let ((contype (car ret)) ;Content-Type
2098 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
2100 ;; Insert User-Agent field
2101 (and mime-edit-insert-user-agent-field
2102 (or (mail-position-on-field "User-Agent")
2103 (insert mime-edit-user-agent-value)))
2104 ;; Make primary MIME headers.
2105 (or (mail-position-on-field "MIME-Version")
2106 (insert mime-edit-mime-version-value))
2107 ;; Remove old Content-Type and other fields.
2109 (goto-char (point-min))
2110 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2111 (narrow-to-region (point-min) (point))
2112 (goto-char (point-min))
2113 (mime-delete-field "Content-Type")
2114 (mime-delete-field "Content-Transfer-Encoding"))
2115 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2116 (mail-position-on-field "Content-Type")
2120 (mail-position-on-field "Content-Transfer-Encoding")
2121 (insert encoding)))))))
2123 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2124 "Translate single-part-tag to MIME header."
2125 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2126 (let* ((beg (match-beginning 0))
2128 (tag (buffer-substring beg end)))
2129 (delete-region beg end)
2130 (let ((contype (mime-edit-get-contype tag))
2131 (encoding (mime-edit-get-encoding tag)))
2132 (insert (concat prefix "--" boundary "\n"))
2134 (narrow-to-region (point)(point))
2135 (insert "Content-Type: " contype "\n")
2137 (insert "Content-Transfer-Encoding: " encoding "\n"))
2138 (eword-encode-header))
2142 (downcase encoding)))))))
2144 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2146 (setq boundary (mime-edit-make-boundary)))
2149 (narrow-to-region beg end)
2150 (let ((tag nil) ;MIME tag
2151 (contype nil) ;Content-Type
2152 (encoding nil) ;Content-Transfer-Encoding
2153 (nparts 0)) ;Number of body parts
2154 ;; Normalize the body part by inserting appropriate message
2155 ;; tags for every message contents.
2156 (mime-edit-normalize-body)
2157 ;; Counting the number of Content-Type.
2158 (goto-char (point-min))
2159 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2160 (setq nparts (1+ nparts)))
2161 ;; Begin translation.
2163 ((and (<= nparts 1)(not multipart))
2164 ;; It's a singular message.
2165 (goto-char (point-min))
2166 (while (re-search-forward
2167 mime-edit-single-part-tag-regexp nil t)
2169 (buffer-substring (match-beginning 0) (match-end 0)))
2170 (delete-region (match-beginning 0) (1+ (match-end 0)))
2171 (setq contype (mime-edit-get-contype tag))
2172 (setq encoding (mime-edit-get-encoding tag))))
2174 ;; It's a multipart message.
2175 (goto-char (point-min))
2176 (let ((prio mime-content-transfer-encoding-priority-list)
2178 (when (setq part-info
2179 (mime-edit-translate-single-part-tag boundary))
2180 (and (setq nprio (member (cdr part-info) prio))
2182 (while (setq part-info
2183 (mime-edit-translate-single-part-tag boundary "\n"))
2184 (and (setq nprio (member (cdr part-info) prio))
2185 (setq prio nprio))))
2186 ;; Define Content-Type as "multipart/mixed".
2188 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2189 (setq encoding (car prio))
2190 ;; Insert the trailer.
2191 (goto-char (point-max))
2192 (insert "\n--" boundary "--\n"))))
2193 (list contype encoding boundary nparts)))))
2195 (defun mime-edit-normalize-body ()
2196 "Normalize the body part by inserting appropriate message tags."
2197 ;; Insert the first MIME tags if necessary.
2198 (goto-char (point-min))
2199 (if (not (looking-at mime-edit-single-part-tag-regexp))
2200 (insert (mime-make-text-tag) "\n"))
2201 ;; Check each tag, and add new tag or correct it if necessary.
2202 (goto-char (point-min))
2203 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2204 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2205 (contype (mime-edit-get-contype tag))
2206 (charset (mime-get-parameter contype "charset"))
2207 (encoding (mime-edit-get-encoding tag)))
2208 ;; Remove extra whitespaces after the tag.
2209 (if (looking-at "[ \t]+$")
2210 (delete-region (match-beginning 0) (match-end 0)))
2212 (end (mime-edit-content-end)))
2213 (if (= end (point-max))
2216 (or (looking-at mime-edit-beginning-tag-regexp)
2218 (insert (mime-make-text-tag) "\n")))
2219 (visible-region beg end)
2222 ((mime-test-content-type contype "message")
2223 ;; Content-type "message" should be sent as is.
2225 ((mime-test-content-type contype "text")
2226 ;; Define charset for text if necessary.
2227 (setq charset (if charset
2228 (intern (downcase charset))
2229 (mime-edit-choose-charset)))
2230 (mime-edit-define-charset charset)
2231 (cond ((string-equal contype "text/x-rot13-47-48")
2234 (mule-caesar-region (point) (mime-edit-content-end))))
2235 ((string-equal contype "text/enriched")
2240 (end (mime-edit-content-end)))
2241 ;; Patch for hard newlines
2244 ;; (while (search-forward "\n" end t)
2245 ;; (put-text-property (match-beginning 0)
2248 ;; End patch for hard newlines
2249 (enriched-encode beg end nil)
2251 (if (search-forward "\n\n")
2252 (delete-region beg (match-end 0)))))))
2253 ;; Point is now on current tag.
2254 ;; Define encoding and encode text if necessary.
2255 (or encoding ;Encoding is not specified.
2258 (let ((ret (cdr (assq charset mime-charset-type-list))))
2260 (setq bits (car ret)
2263 conv "quoted-printable")))
2264 (if (<= bits mime-transfer-level)
2265 (mime-encoding-name bits)
2267 (beg (mime-edit-content-beginning)))
2268 (encode-mime-charset-region beg (mime-edit-content-end)
2270 ;; Protect "From " in beginning of line
2272 (narrow-to-region beg (mime-edit-content-end))
2274 (let (case-fold-search)
2275 (if (re-search-forward "^From " nil t)
2277 (if (memq charset '(iso-2022-jp
2282 (replace-match "\e(BFrom ")
2283 (re-search-forward "^From " nil t)))
2284 (setq encoding "quoted-printable"))))))
2285 ;; canonicalize line break code
2286 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2288 (narrow-to-region beg (mime-edit-content-end))
2290 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2291 (replace-match "\\1\r\n"))))
2293 (mime-encode-region beg (mime-edit-content-end)
2294 (or encoding "7bit"))
2295 (mime-edit-define-encoding encoding)))
2296 (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))
2306 (forward-line 1))))))
2308 (defun mime-delete-field (field)
2309 "Delete header FIELD."
2310 (let ((regexp (format "^%s:[ \t]*" field)))
2311 (goto-char (point-min))
2312 (while (re-search-forward regexp nil t)
2313 (delete-region (match-beginning 0)
2314 (1+ (std11-field-end))))))
2318 ;;; Platform dependent functions
2321 ;; Sun implementations
2323 (defun mime-edit-voice-recorder-for-sun (encoding)
2324 "Record voice in a buffer using Sun audio device,
2325 and insert data encoded as ENCODING."
2326 (message "Start the recording on %s. Type C-g to finish the recording..."
2328 (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-place
2342 '("message" "rfc822")
2343 (mime-edit-insert-tag "message" "rfc822")
2344 (funcall inserter message)))
2345 (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-place
2353 '("message" "rfc822")
2354 (mime-edit-insert-tag "message" "rfc822")
2355 (funcall inserter message)))
2356 (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)))
2368 (goto-char header-start)
2369 (while (and (re-search-forward
2371 (apply (function regexp-or)
2372 mime-edit-yank-ignored-field-list)
2374 (setq beg (match-beginning 0))
2375 (setq end (1+ (std11-field-end))))
2376 (delete-region beg end))))))
2379 ;;; @ multipart enclosure
2382 (defun mime-edit-enclose-region-internal (type beg end)
2386 (narrow-to-region beg end)
2387 (insert (format "--<<%s>>-{\n" type))
2388 (goto-char (point-max))
2389 (insert (format "--}-<<%s>>\n" type))
2390 (goto-char (point-max)))
2391 (or (looking-at mime-edit-beginning-tag-regexp)
2393 (insert (mime-make-text-tag) "\n"))))
2395 (defun mime-edit-enclose-quote-region (beg end)
2397 (mime-edit-enclose-region-internal 'quote beg end))
2399 (defun mime-edit-enclose-mixed-region (beg end)
2401 (mime-edit-enclose-region-internal 'mixed beg end))
2403 (defun mime-edit-enclose-parallel-region (beg end)
2405 (mime-edit-enclose-region-internal 'parallel beg end))
2407 (defun mime-edit-enclose-digest-region (beg end)
2409 (mime-edit-enclose-region-internal 'digest beg end))
2411 (defun mime-edit-enclose-alternative-region (beg end)
2413 (mime-edit-enclose-region-internal 'alternative beg end))
2415 (defun mime-edit-enclose-pgp-signed-region (beg end)
2417 (mime-edit-enclose-region-internal 'pgp-signed beg end))
2419 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2421 (mime-edit-enclose-region-internal 'pgp-encrypted beg end))
2423 (defun mime-edit-enclose-kazu-signed-region (beg end)
2425 (mime-edit-enclose-region-internal 'kazu-signed beg end))
2427 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2429 (mime-edit-enclose-region-internal 'kazu-encrypted beg end))
2431 (defun mime-edit-enclose-smime-signed-region (beg end)
2433 (mime-edit-enclose-region-internal 'smime-signed beg end))
2435 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2437 (mime-edit-enclose-region-internal 'smime-encrypted beg end))
2439 (defun mime-edit-insert-key (&optional arg)
2440 "Insert a pgp public key."
2442 (mime-edit-insert-tag "application" "pgp-keys")
2443 (mime-edit-define-encoding "7bit")
2445 (if (and (not (eobp))
2446 (not (looking-at mime-edit-single-part-tag-regexp)))
2447 (insert (mime-make-text-tag) "\n")))
2453 (defun mime-edit-set-split (arg)
2456 (y-or-n-p "Do you want to enable split? ")))
2457 (setq mime-edit-split-message arg)
2459 (message "This message is enabled to split.")
2460 (message "This message is not enabled to split.")))
2462 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2463 "Toggle transfer-level is 7bit or 8bit through.
2465 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2467 (if (numberp transfer-level)
2468 (setq mime-transfer-level transfer-level)
2469 (if (< mime-transfer-level 8)
2470 (setq mime-transfer-level 8)
2471 (setq mime-transfer-level 7)))
2472 (message (format "Current transfer-level is %d bit"
2473 mime-transfer-level))
2474 (setq mime-transfer-level-string
2475 (mime-encoding-name mime-transfer-level 'not-omit))
2476 (force-mode-line-update))
2478 (defun mime-edit-set-transfer-level-7bit ()
2480 (mime-edit-toggle-transfer-level 7))
2482 (defun mime-edit-set-transfer-level-8bit ()
2484 (mime-edit-toggle-transfer-level 8))
2490 (defvar mime-edit-pgp-processing nil)
2491 (make-variable-buffer-local 'mime-edit-pgp-processing)
2493 (defun mime-edit-set-sign (arg)
2496 (y-or-n-p "Do you want to sign? ")))
2499 (or (memq 'sign mime-edit-pgp-processing)
2500 (setq mime-edit-pgp-processing
2501 (nconc mime-edit-pgp-processing
2502 (copy-sequence '(sign)))))
2503 (message "This message will be signed."))
2504 (setq mime-edit-pgp-processing
2505 (delq 'sign mime-edit-pgp-processing))
2506 (message "This message will not be signed.")))
2508 (defun mime-edit-set-encrypt (arg)
2511 (y-or-n-p "Do you want to encrypt? ")))
2514 (or (memq 'encrypt mime-edit-pgp-processing)
2515 (setq mime-edit-pgp-processing
2516 (nconc mime-edit-pgp-processing
2517 (copy-sequence '(encrypt)))))
2518 (message "This message will be encrypt."))
2519 (setq mime-edit-pgp-processing
2520 (delq 'encrypt mime-edit-pgp-processing))
2521 (message "This message will not be encrypt.")))
2523 (defun mime-edit-pgp-enclose-buffer ()
2524 (let ((beg (save-excursion
2525 (goto-char (point-min))
2526 (if (search-forward (concat "\n" mail-header-separator "\n"))
2529 (dolist (pgp-processing mime-edit-pgp-processing)
2530 (case pgp-processing
2532 (mime-edit-enclose-pgp-signed-region
2535 (mime-edit-enclose-pgp-encrypted-region
2536 beg (point-max))))))))
2542 (defun mime-edit-insert-partial-header (fields subject
2543 id number total separator)
2545 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2546 (insert (format "Mime-Version: %s\n" mime-edit-mime-version-value))
2548 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2549 id number total separator)))
2551 (defun mime-edit-split-and-send
2552 (&optional cmd lines mime-edit-message-max-length)
2556 (count-lines (point-min) (point-max))))
2557 (or mime-edit-message-max-length
2558 (setq mime-edit-message-max-length
2559 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2560 mime-edit-message-default-max-lines)))
2562 ;; (mime-edit-draft-file-name
2563 ;; (or (buffer-file-name)
2565 ;; (expand-file-name "mime-draft" temporary-file-directory))))
2566 (separator mail-header-separator)
2568 (replace-space-with-underline (current-time-string))
2569 "@" (system-name) "\"")))
2570 (run-hooks 'mime-edit-before-split-hook)
2571 (let ((the-buf (current-buffer))
2572 (copy-buf (get-buffer-create " *Original Message*"))
2573 (header (std11-header-string-except
2574 mime-edit-split-ignored-field-regexp separator))
2575 (subject (mail-fetch-field "subject"))
2576 (total (+ (/ lines mime-edit-message-max-length)
2577 (if (> (mod lines mime-edit-message-max-length) 0)
2583 mime-edit-split-message-sender-alist))
2587 (error "Split sender is not specified for `%s'." major-mode)))))
2588 (mime-edit-partial-number 1)
2591 (set-buffer copy-buf)
2593 (insert-buffer the-buf)
2595 (if (re-search-forward
2596 (concat "^" (regexp-quote separator) "$") nil t)
2597 (let ((he (match-beginning 0)))
2599 (narrow-to-region (point-min) he)))
2600 (goto-char (point-min))
2601 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2602 (delete-region (match-beginning 0)
2603 (1+ (std11-field-end))))))
2604 (while (< mime-edit-partial-number total)
2607 (set-buffer copy-buf)
2608 (setq data (buffer-substring
2611 (goto-line mime-edit-message-max-length)
2613 (delete-region (point-min)(point)))
2614 (mime-edit-insert-partial-header
2615 header subject id mime-edit-partial-number total separator)
2618 (message (format "Sending %d/%d..."
2619 mime-edit-partial-number total))
2620 (call-interactively command)
2621 (message (format "Sending %d/%d... done"
2622 mime-edit-partial-number total)))
2623 (setq mime-edit-partial-number
2624 (1+ mime-edit-partial-number)))
2627 (set-buffer copy-buf)
2628 (setq data (buffer-string))
2630 (mime-edit-insert-partial-header
2631 header subject id mime-edit-partial-number total separator)
2634 (message (format "Sending %d/%d..."
2635 mime-edit-partial-number total))
2636 (message (format "Sending %d/%d... done"
2637 mime-edit-partial-number total))))))
2639 (defun mime-edit-maybe-split-and-send (&optional cmd)
2641 (run-hooks 'mime-edit-before-send-hook)
2642 (let ((mime-edit-message-max-length
2643 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2644 mime-edit-message-default-max-lines))
2645 (lines (count-lines (point-min) (point-max))))
2646 (if (and (> lines mime-edit-message-max-length)
2647 mime-edit-split-message)
2648 (mime-edit-split-and-send cmd lines mime-edit-message-max-length))))
2651 ;;; @ preview message
2654 (defvar mime-edit-buffer nil) ; buffer local variable
2656 (defun mime-edit-preview-message ()
2657 "preview editing MIME message."
2659 (let* ((str (buffer-string))
2660 (separator mail-header-separator)
2661 (the-buf (current-buffer))
2662 (buf-name (buffer-name))
2663 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2664 (buf (get-buffer temp-buf-name))
2665 (pgp-processing mime-edit-pgp-processing))
2668 (switch-to-buffer buf)
2670 (setq buf (get-buffer-create temp-buf-name))
2671 (switch-to-buffer buf))
2673 (setq major-mode 'mime-temp-message-mode)
2674 (make-local-variable 'mail-header-separator)
2675 (setq mail-header-separator separator)
2676 (make-local-variable 'mime-edit-buffer)
2677 (setq mime-edit-buffer the-buf)
2678 (setq mime-edit-pgp-processing pgp-processing)
2680 (run-hooks 'mime-edit-translate-hook)
2681 (mime-edit-translate-buffer)
2682 (goto-char (point-min))
2683 (if (re-search-forward
2684 (concat "^" (regexp-quote separator) "$"))
2686 (mime-view-buffer)))
2688 (defun mime-edit-quitting-method ()
2689 "Quitting method for mime-view."
2690 (let* ((entity (get-text-property (point-min) 'mime-view-entity))
2691 (temp (mime-buffer-entity-buffer-internal entity))
2693 (mime-preview-kill-buffer)
2695 (setq buf mime-edit-buffer)
2697 (switch-to-buffer buf)))
2699 (set-alist 'mime-preview-quitting-method-alist
2700 'mime-temp-message-mode
2701 #'mime-edit-quitting-method)
2707 (defvar mime-edit-again-ignored-field-regexp
2708 (concat "^\\(" "Content-.*\\|Mime-Version"
2709 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2711 "Regexp for deleted header fields when `mime-edit-again' is called.")
2713 (defsubst eliminate-top-spaces (string)
2714 "Eliminate top sequence of space or tab in STRING."
2715 (if (string-match "^[ \t]+" string)
2716 (substring string (match-end 0))
2719 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2722 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2723 '(("application/pgp-encrypted" . pgp-encrypted)
2724 ("application/pgp-signature" . pgp-signed))))
2725 (mime-content-type-subtype content-type)))
2726 (boundary (mime-content-type-parameter content-type "boundary"))
2727 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2728 (re-search-forward boundary-pat nil t)
2729 (let ((bb (match-beginning 0)) eb tag)
2730 (setq tag (format "\n--<<%s>>-{\n" subtype))
2733 (setq bb (+ bb (length tag)))
2735 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2737 (setq eb (match-beginning 0))
2738 (replace-match (format "--}-<<%s>>\n" subtype))
2740 (narrow-to-region bb eb)
2741 (goto-char (point-min))
2742 (while (re-search-forward boundary-pat nil t)
2743 (let ((beg (match-beginning 0))
2745 (delete-region beg (match-end 0))
2747 (if (re-search-forward boundary-pat nil t)
2748 (setq end (match-beginning 0))
2749 (setq end (point-max)))
2751 (narrow-to-region beg end)
2753 ((eq subtype 'pgp-encrypted)
2756 (goto-char (point-min))
2757 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2760 (save-window-excursion
2761 (pgg-decrypt-region (match-beginning 0)
2763 (delete-region (point-min)(point-max))))
2764 (insert-buffer-substring pgg-output-buffer)
2765 (mime-edit-decode-message-in-buffer
2766 nil not-decode-text)
2767 (delete-region (goto-char (point-min))
2768 (if (search-forward "\n\n" nil t)
2771 (goto-char (point-max))))
2773 (mime-edit-decode-message-in-buffer
2774 (if (eq subtype 'digest)
2776 (make-mime-content-type 'message 'rfc822)))
2778 (goto-char (point-max))))))))))
2779 (goto-char (point-min))
2780 (or (= (point-min) 1)
2781 (delete-region (point-min)
2782 (if (search-forward "\n\n" nil t)
2786 (defun mime-edit-decode-single-part-in-buffer
2787 (content-type not-decode-text &optional content-disposition)
2788 (let* ((type (mime-content-type-primary-type content-type))
2789 (subtype (mime-content-type-subtype content-type))
2790 (ctype (format "%s/%s" type subtype))
2792 (pstr (let ((bytes (+ 14 (length ctype))))
2793 (mapconcat (function
2795 (if (string= (car attr) "charset")
2797 (setq charset (cdr attr))
2799 (let* ((str (concat (car attr)
2802 (setq bytes (+ bytes bs 2))
2805 (setq bytes (+ bs 1))
2808 (mime-content-type-parameters content-type) "")))
2811 (limit (save-excursion
2812 (if (search-forward "\n\n" nil t)
2815 (mime-content-disposition-type content-disposition))
2817 (if disposition-type
2818 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2819 (mapconcat (function
2824 (if (string-equal "filename"
2826 (std11-wrap-as-quoted-string
2830 (setq bytes (+ bytes bs 2))
2833 (setq bytes (+ bs 1))
2836 (mime-content-disposition-parameters
2837 content-disposition)
2839 (if disposition-type
2840 (setq pstr (format "%s\nContent-Disposition: %s%s"
2841 pstr disposition-type disposition-str)))
2843 (if (re-search-forward
2844 "^Content-Transfer-Encoding:" limit t)
2845 (let ((beg (match-beginning 0))
2846 (hbeg (match-end 0))
2847 (end (std11-field-end limit)))
2850 (eliminate-top-spaces
2851 (std11-unfold-string
2852 (buffer-substring hbeg end)))))
2853 (if (or charset (eq type 'text))
2855 (delete-region beg (1+ end))
2856 (goto-char (point-min))
2857 (if (search-forward "\n\n" nil t)
2860 (match-end 0)(point-max) encoding)
2862 encoding nil))))))))
2863 (if (or encoded (not not-decode-text))
2866 (goto-char (point-min))
2867 (while (re-search-forward "\r\n" nil t)
2868 (replace-match "\n")))
2869 (decode-mime-charset-region (point-min)(point-max)
2870 (or charset default-mime-charset))))
2871 (let ((he (if (re-search-forward "^$" nil t)
2874 (if (and (eq type 'text)
2875 (eq subtype 'x-rot13-47-48))
2876 (mule-caesar-region he (point-max)))
2877 (if (= (point-min) 1)
2883 (format "%s/%s%s" type subtype pstr)
2885 (delete-region (point-min) he)
2887 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2891 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2894 (goto-char (point-min))
2895 (let ((ctl (or (mime-read-Content-Type)
2896 default-content-type)))
2898 (let ((type (mime-content-type-primary-type ctl)))
2900 ((and (eq type 'application)
2901 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2902 (delete-region (point-min)(point-max)))
2903 ((eq type 'multipart)
2904 (mime-edit-decode-multipart-in-buffer ctl not-decode-text))
2906 (mime-edit-decode-single-part-in-buffer
2907 ctl not-decode-text (mime-read-Content-Disposition)))))
2909 (decode-mime-charset-region (point-min) (point-max)
2910 default-mime-charset)))
2911 (if (= (point-min) 1)
2914 (std11-narrow-to-header)
2915 (goto-char (point-min))
2916 (while (re-search-forward
2917 mime-edit-again-ignored-field-regexp nil t)
2918 (delete-region (match-beginning 0) (1+ (std11-field-end)))))
2919 (mime-decode-header-in-buffer (not not-decode-text)))))))
2922 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2923 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2924 Content-Type and Content-Transfer-Encoding header fields will be
2925 converted to MIME-Edit tags."
2927 (goto-char (point-min))
2929 (concat "\n" (regexp-quote mail-header-separator) "\n")
2931 (replace-match "\n\n"))
2932 (mime-edit-decode-message-in-buffer nil not-decode-text)
2933 (goto-char (point-min))
2935 (and (re-search-forward "^$")
2936 (replace-match mail-header-separator)))
2938 (turn-on-mime-edit)))
2944 (provide 'mime-edit)
2946 (run-hooks 'mime-edit-load-hook)
2948 ;;; mime-edit.el ends here