1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,94,95,96,97,98,99,2000 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Daiki Ueno <ueno@unixuser.org>
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)
119 (autoload 'pgg-encrypt-region "pgg"
120 "PGP encryption of current region." t)
121 (autoload 'pgg-sign-region "pgg"
122 "PGP signature of current region." t)
123 (autoload 'pgg-insert-key "pgg"
124 "Insert PGP public key at point." t)
125 (autoload 'smime-encrypt-buffer "smime"
126 "S/MIME encryption of current buffer.")
127 (autoload 'smime-sign-buffer "smime"
128 "S/MIME signature of current buffer.")
135 (defconst mime-edit-version
137 (mime-product-name mime-user-interface-product) " "
138 (mapconcat #'number-to-string
139 (mime-product-version mime-user-interface-product) ".")
140 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
146 (defgroup mime-edit nil
150 (defcustom mime-ignore-preceding-spaces nil
151 "*Ignore preceding white spaces if non-nil."
155 (defcustom mime-ignore-trailing-spaces nil
156 "*Ignore trailing white spaces if non-nil."
160 (defcustom mime-ignore-same-text-tag t
161 "*Ignore preceding text content-type tag that is same with new one.
162 If non-nil, the text tag is not inserted unless something different."
166 (defcustom mime-auto-hide-body t
167 "*Hide non-textual body encoded in base64 after insertion if non-nil."
171 (defcustom mime-edit-voice-recorder
172 (function mime-edit-voice-recorder-for-sun)
173 "*Function to record a voice message and encode it."
177 (defcustom mime-edit-mode-hook nil
178 "*Hook called when enter MIME mode."
182 (defcustom mime-edit-translate-hook nil
183 "*Hook called before translating into a MIME compliant message.
184 To insert a signature file automatically, call the function
185 `mime-edit-insert-signature' from this hook."
189 (defcustom mime-edit-exit-hook nil
190 "*Hook called when exit MIME mode."
194 (defvar mime-content-types
196 ;; Charset parameter need not to be specified, since it is
197 ;; defined automatically while translation.
199 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
212 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
213 ("directory" "/pub/GNU/elisp/mime")
215 ("mode" "image" "ascii" "local8"))
220 ("mode" "image" "ascii" "local8"))
221 ("tftp" ("site") ("name"))
222 ("afs" ("site") ("name"))
223 ("local-file" ("site") ("name"))
225 ("server" "ftpmail@nic.karrn.ad.jp")
231 ("octet-stream" ("type" "" "tar" "shar"))
233 ("vnd.ms-powerpoint")
234 ("x-kiss" ("x-cnf")))
246 "*Alist of content-type, subtype, parameters and its values.")
248 (defcustom mime-file-types
251 ;; Programming languages
254 "application" "octet-stream" (("type" . "C++"))
256 "attachment" (("filename" . file)))
259 "application" "octet-stream" (("type" . "emacs-lisp"))
261 "attachment" (("filename" . file)))
264 "application" "octet-stream" (("type" . "common-lisp"))
266 "attachment" (("filename" . file)))
269 "application" "octet-stream" (("type" . "perl"))
271 "attachment" (("filename" . file)))
273 ;; Text or translated text
278 "inline" (("filename" . file)))
280 ;; .rc : procmail modules pm-xxxx.rc
281 ;; *rc : other resource files
283 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
286 "attachment" (("filename" . file)))
293 ("\\.diff$\\|\\.patch$"
294 "application" "octet-stream" (("type" . "patch"))
296 "attachment" (("filename" . file)))
299 "text" "plain" nil nil nil nil)
302 ;; Octect binary text
305 "application" "msword" nil
307 "attachment" (("filename" . file)))
308 ("\\.ppt$" ; MS Power Point
309 "application" "vnd.ms-powerpoint" nil
311 "attachment" (("filename" . file)))
316 "inline" (("filename" . file)))
318 "application" "postscript" nil
320 "attachment" (("filename" . file)))
324 ("\\.jpg$\\|\\.jpeg$"
327 "inline" (("filename" . file)))
331 "inline" (("filename" . file)))
335 "inline" (("filename" . file)))
339 "inline" (("filename" . file)))
343 "inline" (("filename" . file)))
347 "inline" (("filename" . file)))
351 "inline" (("filename" . file)))
355 "inline" (("filename" . file)))
359 "attachment" (("filename" . file)))
363 "attachment" (("filename" . file)))
365 "application" "octet-stream" (("type" . "tar+gzip"))
367 "attachment" (("filename" . file)))
369 "application" "octet-stream" (("type" . "tar+gzip"))
371 "attachment" (("filename" . file)))
373 "application" "octet-stream" (("type" . "tar+compress"))
375 "attachment" (("filename" . file)))
377 "application" "octet-stream" (("type" . "tar+compress"))
379 "attachment" (("filename" . file)))
381 "application" "octet-stream" (("type" . "gzip"))
383 "attachment" (("filename" . file)))
385 "application" "octet-stream" (("type" . "compress"))
387 "attachment" (("filename" . file)))
389 "application" "octet-stream" (("type" . "lha"))
391 "attachment" (("filename" . file)))
393 "application" "zip" nil
395 "attachment" (("filename" . file)))
400 "application" "octet-stream" nil
402 "attachment" (("filename" . file))))
403 "*Alist of file name, types, parameters, and default encoding.
404 If encoding is nil, it is determined from its contents."
408 (choice :tag "Primary-Type"
409 ,@(nconc (mapcar (lambda (cell)
410 (list 'item (car cell)))
414 (choice :tag "Sub-Type"
417 (mapcar (lambda (cell)
418 (mapcar (lambda (cell)
419 (list 'item (car cell)))
424 (repeat :tag "Parameters of Content-Type field"
425 (cons string (choice string symbol)))
426 ;; content-transfer-encoding
427 (choice :tag "Encoding"
430 (mapcar (lambda (cell)
432 (mime-encoding-list))))
434 (choice :tag "Disposition-Type"
440 (repeat :tag "Parameters of Content-Disposition field"
441 (cons string (choice string symbol)))))
445 ;;; @@ about charset, encoding and transfer-level
448 (defvar mime-charset-type-list
450 (iso-8859-1 8 "quoted-printable")
451 (iso-8859-2 8 "quoted-printable")
452 (iso-8859-3 8 "quoted-printable")
453 (iso-8859-4 8 "quoted-printable")
454 (iso-8859-5 8 "quoted-printable")
455 (koi8-r 8 "quoted-printable")
456 (iso-8859-7 8 "quoted-printable")
457 (iso-8859-8 8 "quoted-printable")
458 (iso-8859-9 8 "quoted-printable")
459 (iso-8859-14 8 "quoted-printable")
460 (iso-8859-15 8 "quoted-printable")
461 (iso-2022-jp 7 "base64")
462 (iso-2022-jp-3 7 "base64")
463 (iso-2022-kr 7 "base64")
469 (shift_jis 8 "base64")
471 (iso-2022-jp-2 7 "base64")
472 (iso-2022-int-1 7 "base64")))
474 (defvar mime-transfer-level 7
475 "*A number of network transfer level. It should be bigger than 7.")
476 (make-variable-buffer-local 'mime-transfer-level)
478 (defsubst mime-encoding-name (transfer-level &optional not-omit)
479 (cond ((> transfer-level 8) "binary")
480 ((= transfer-level 8) "8bit")
483 (defvar mime-transfer-level-string
484 (mime-encoding-name mime-transfer-level 'not-omit)
485 "A string formatted version of mime-transfer-level")
486 (make-variable-buffer-local 'mime-transfer-level-string)
488 ;;; @@ about content transfer encoding
490 (defvar mime-content-transfer-encoding-priority-list
491 '(nil "8bit" "binary"))
493 ;;; @@ about message inserting
496 (defvar mime-edit-yank-ignored-field-list
497 '("Received" "Approved" "Path" "Replied" "Status"
498 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
499 "Delete these fields from original message when it is inserted
500 as message/rfc822 part.
501 Each elements are regexp of field-name.")
503 (defvar mime-edit-yank-ignored-field-regexp
505 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
508 (defvar mime-edit-message-inserter-alist nil)
509 (defvar mime-edit-mail-inserter-alist nil)
512 ;;; @@ about message splitting
515 (defcustom mime-edit-split-message t
516 "*Split large message if it is non-nil."
520 (defcustom mime-edit-message-default-max-lines 1000
521 "*Default maximum lines of a message."
525 (defcustom mime-edit-message-max-lines-alist
526 '((news-reply-mode . 500))
527 "Alist of major-mode vs maximum lines of a message.
528 If it is not specified for a major-mode,
529 `mime-edit-message-default-max-lines' is used."
533 (defconst mime-edit-split-ignored-field-regexp
534 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
536 (defcustom mime-edit-split-blind-field-regexp
537 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
538 "*Regular expression to match field-name to be ignored when split sending."
542 (defvar mime-edit-split-message-sender-alist nil)
544 (defvar mime-edit-news-reply-mode-server-running nil)
550 (defconst mime-edit-single-part-tag-regexp
551 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
552 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
554 (defconst mime-edit-quoted-single-part-tag-regexp
555 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
557 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
559 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
561 (defconst mime-edit-beginning-tag-regexp
562 (regexp-or mime-edit-single-part-tag-regexp
563 mime-edit-multipart-beginning-regexp))
565 (defconst mime-edit-end-tag-regexp
566 (regexp-or mime-edit-single-part-tag-regexp
567 mime-edit-multipart-end-regexp))
569 (defconst mime-edit-tag-regexp
570 (regexp-or mime-edit-single-part-tag-regexp
571 mime-edit-multipart-beginning-regexp
572 mime-edit-multipart-end-regexp))
574 (defvar mime-tag-format "--[[%s]]"
575 "*Control-string making a MIME tag.")
577 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
578 "*Control-string making a MIME tag with encoding.")
581 ;;; @@ multipart boundary
584 (defvar mime-multipart-boundary "Multipart"
585 "*Boundary of a multipart message.")
588 ;;; @@ optional header fields
591 (defvar mime-edit-insert-user-agent-field t
592 "*If non-nil, insert User-Agent header field.")
594 (defvar mime-edit-user-agent-value
595 (concat (mime-product-name mime-user-interface-product)
597 (mapconcat #'number-to-string
598 (mime-product-version mime-user-interface-product) ".")
600 (mime-product-code-name mime-user-interface-product)
602 (mime-product-name mime-library-product)
604 (mapconcat #'number-to-string
605 (mime-product-version mime-library-product) ".")
607 (mime-product-code-name mime-library-product)
609 (if (fboundp 'apel-version)
610 (concat (apel-version) " "))
611 (if (featurep 'xemacs)
612 (concat (cond ((and (featurep 'chise)
613 (boundp 'xemacs-chise-version))
614 (concat "CHISE-MULE/" xemacs-chise-version))
615 ((featurep 'utf-2000)
616 (concat "UTF-2000-MULE/" utf-2000-version))
617 ((featurep 'mule) "MULE"))
619 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
622 (substring emacs-version 0 (match-end 0))
623 (cond ((and (boundp 'xemacs-betaname)
625 ;; It does not exist in XEmacs
626 ;; versions prior to 20.3.
627 (concat " " xemacs-betaname))
628 ((and (boundp 'emacs-patch-level)
630 ;; It does not exist in FSF Emacs or in
631 ;; XEmacs versions earlier than 21.1.1.
632 (format " (patch %d)" emacs-patch-level))
634 " (" xemacs-codename ")"
635 ;; `xemacs-extra-name' has appeared in the
636 ;; development version of XEmacs 21.5-b8.
637 (if (and (boundp 'xemacs-extra-name)
638 (symbol-value 'xemacs-extra-name))
639 (concat " " (symbol-value 'xemacs-extra-name))
642 system-configuration ")")
643 " (" emacs-version ")"))
644 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
645 (substring emacs-version 0 (match-beginning 0))
648 (if (boundp 'enable-multibyte-characters)
650 " (" system-configuration ")"
651 (if enable-multibyte-characters
652 (concat " MULE/" mule-version)
653 " (with unibyte mode)")
654 (if (featurep 'meadow)
655 (let ((mver (Meadow-version)))
656 (if (string-match "^Meadow-" mver)
660 (concat "MULE/" mule-version
661 " (based on Emacs " ver ")"))
662 (concat "Emacs/" ver " (" system-configuration ")")))))
663 "Body of User-Agent field.
664 If variable `mime-edit-insert-user-agent-field' is not nil, it is
665 inserted into message header.")
671 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
672 "*Specify MIME tspecials.
673 Tspecials means any character that matches with it in header must be quoted.")
675 (defconst mime-edit-mime-version-value
676 (concat "1.0 (generated by " mime-edit-version ")")
677 "MIME version number.")
679 (defconst mime-edit-mime-version-field-for-message/partial
680 (concat "MIME-Version:"
681 (mime-encode-field-body
682 (concat " 1.0 (split by " mime-edit-version ")\n")
684 "MIME version field for message/partial.")
687 ;;; @ keymap and menu
690 (defvar mime-edit-mode-flag nil)
691 (make-variable-buffer-local 'mime-edit-mode-flag)
693 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
694 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
695 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
696 "Keymap for MIME-Edit mode commands to insert entity or set status.")
698 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
699 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-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 "Set splitting" mime-edit-set-split)
769 (sign "PGP sign" mime-edit-set-sign)
770 (encrypt "PGP encrypt" 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 (cond ((featurep 'xemacs)
776 ;; modified by Pekka Marjola <pema@iki.fi>
777 ;; 1995/9/5 (c.f. [tm-en:69])
778 (defun mime-edit-define-menu-for-xemacs ()
779 "Define menu for XEmacs."
780 (cond ((featurep 'menubar)
781 (make-local-variable 'current-menubar)
782 (set-buffer-menubar current-menubar)
785 (cons mime-edit-menu-title
788 (vector (nth 1 item)(nth 2 item)
789 mime-edit-mode-flag)))
790 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 ((>= emacs-major-version 19)
803 (define-key mime-edit-mode-map [menu-bar mime-edit]
804 (cons mime-edit-menu-title
805 (make-sparse-keymap mime-edit-menu-title)))
808 (define-key mime-edit-mode-map
809 (vector 'menu-bar 'mime-edit (car item))
810 (cons (nth 1 item)(nth 2 item)))))
811 (reverse mime-edit-menu-list))))
817 (defvar mime-edit-touched-flag nil)
820 (defun mime-edit-mode ()
821 "MIME minor mode for editing the tagged MIME message.
823 In this mode, basically, the message is composed in the tagged MIME
824 format. The message tag looks like:
826 --[[text/plain; charset=ISO-2022-JP][7bit]]
828 The tag specifies the MIME content type, subtype, optional parameters
829 and transfer encoding of the message following the tag. Messages
830 without any tag are treated as `text/plain' by default. Charset and
831 transfer encoding are automatically defined unless explicitly
832 specified. Binary messages such as audio and image are usually
833 hidden. The messages in the tagged MIME format are automatically
834 translated into a MIME compliant message when exiting this mode.
836 Available charsets depend on Emacs version being used. The following
837 lists the available charsets of each emacs.
839 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
840 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
841 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
842 CN-BIG5 and ISO-2022-INT-1 are available.
844 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
845 be used to represent multilingual text in intermixed manner. Any
846 languages that has no registered charset are represented as either
847 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
849 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
850 without mule, please set variable `default-mime-charset'. This
851 variable must be symbol of which name is a MIME charset.
853 If you want to add more charsets in mule, please set variable
854 `charsets-mime-charset-alist'. This variable must be alist of which
855 key is list of charset and value is symbol of MIME charset. If name
856 of coding-system is different as MIME charset, please set variable
857 `mime-charset-coding-system-alist'. This variable must be alist of
858 which key is MIME charset and value is coding-system.
860 Following commands are available in addition to major mode commands:
863 \\[mime-edit-insert-text] insert a text message.
864 \\[mime-edit-insert-file] insert a (binary) file.
865 \\[mime-edit-insert-external] insert a reference to external body.
866 \\[mime-edit-insert-voice] insert a voice message.
867 \\[mime-edit-insert-message] insert a mail or news message.
868 \\[mime-edit-insert-mail] insert a mail message.
869 \\[mime-edit-insert-signature] insert a signature file at end.
870 \\[mime-edit-insert-key] insert PGP public key.
871 \\[mime-edit-insert-tag] insert a new MIME tag.
873 \[make enclosure (maybe multipart)\]
874 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
875 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
876 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
877 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
878 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
879 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
880 \\[mime-edit-enclose-quote-region] enclose as verbose mode
881 (to avoid to expand tags)
884 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
885 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
886 \\[mime-edit-set-split] set message splitting mode.
887 \\[mime-edit-set-sign] set PGP-sign mode.
888 \\[mime-edit-set-encrypt] set PGP-encryption mode.
889 \\[mime-edit-preview-message] preview editing MIME message.
890 \\[mime-edit-exit] exit and translate into a MIME
892 \\[mime-edit-help] show this help.
893 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
896 Additional commands are available in some major modes:
897 C-c C-c exit, translate and run the original command.
898 C-c C-s exit, translate and run the original command.
900 The following is a message example written in the tagged MIME format.
901 TABs at the beginning of the line are not a part of the message:
903 This is a conventional plain text. It should be translated
906 This is also a plain text. But, it is explicitly specified as
908 --[[text/plain; charset=ISO-8859-1]]
909 This is also a plain text. But charset is specified as
912 ¡Hola! Buenos días. ¿Cómo está usted?
914 This is a <bold>enriched text</bold>.
915 --[[image/gif][base64]]...image encoded in base64 here...
916 --[[audio/basic][base64]]...audio encoded in base64 here...
918 User customizable variables (not documented all of them):
920 Specifies a key prefix for MIME minor mode commands.
922 mime-ignore-preceding-spaces
923 Preceding white spaces in a message body are ignored if non-nil.
925 mime-ignore-trailing-spaces
926 Trailing white spaces in a message body are ignored if non-nil.
929 Hide a non-textual body message encoded in base64 after insertion
933 A number of network transfer level. It should be bigger than 7.
934 If you are in 8bit-through environment, please set 8.
936 mime-edit-voice-recorder
937 Specifies a function to record a voice message and encode it.
938 The function `mime-edit-voice-recorder-for-sun' is for Sun
942 Turning on MIME mode calls the value of mime-edit-mode-hook, if
945 mime-edit-translate-hook
946 The value of mime-edit-translate-hook is called just before translating
947 the tagged MIME format into a MIME compliant message if it is
948 non-nil. If the hook call the function mime-edit-insert-signature,
949 the signature file will be inserted automatically.
952 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
955 (if mime-edit-mode-flag
957 (if mime-edit-touched-flag
959 (make-local-variable 'mime-edit-touched-flag)
960 (setq mime-edit-touched-flag t)
961 (turn-on-mime-edit))))
964 (cond ((featurep 'xemacs)
965 (add-minor-mode 'mime-edit-mode-flag
966 '((" MIME-Edit " mime-transfer-level-string))
971 (set-alist 'minor-mode-alist
973 '((" MIME-Edit " mime-transfer-level-string)))
974 (set-alist 'minor-mode-map-alist
976 mime-edit-mode-map)))
980 (defun turn-on-mime-edit ()
981 "Unconditionally turn on MIME-Edit mode."
983 (if mime-edit-mode-flag
984 (error "You are already editing a MIME message.")
985 (setq mime-edit-mode-flag t)
987 ;; Set transfer level into mode line
989 (setq mime-transfer-level-string
990 (mime-encoding-name mime-transfer-level 'not-omit))
991 (force-mode-line-update)
993 ;; Define menu for XEmacs.
994 (if (featurep 'xemacs)
995 (mime-edit-define-menu-for-xemacs))
997 ;; I don't care about saving these.
998 (setq paragraph-start
999 (regexp-or mime-edit-single-part-tag-regexp
1001 (setq paragraph-separate
1002 (regexp-or mime-edit-single-part-tag-regexp
1003 paragraph-separate))
1004 (run-hooks 'mime-edit-mode-hook)
1007 (substitute-command-keys
1008 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
1011 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1014 (defun mime-edit-exit (&optional nomime no-error)
1015 "Translate the tagged MIME message into a MIME compliant message.
1016 With no argument encode a message in the buffer into MIME, otherwise
1017 just return to previous mode."
1019 (if (not mime-edit-mode-flag)
1021 (error "You aren't editing a MIME message."))
1024 (run-hooks 'mime-edit-translate-hook)
1025 (mime-edit-translate-buffer)))
1026 ;; Restore previous state.
1027 (setq mime-edit-mode-flag nil)
1028 (if (and (featurep 'xemacs)
1029 (featurep 'menubar))
1030 (delete-menu-item (list mime-edit-menu-title)))
1031 (set-buffer-modified-p (buffer-modified-p))
1032 (run-hooks 'mime-edit-exit-hook)
1033 (message "Exit MIME editor mode.")))
1035 (defun mime-edit-maybe-translate ()
1037 (mime-edit-exit nil t)
1038 (call-interactively 'mime-edit-maybe-split-and-send))
1040 (defun mime-edit-help ()
1041 "Show help message about MIME mode."
1043 (with-output-to-temp-buffer "*Help*"
1044 (princ "MIME editor mode:\n")
1045 (princ (documentation 'mime-edit-mode))
1046 (print-help-return-message)))
1048 (defun mime-edit-insert-text (&optional subtype)
1049 "Insert a text message.
1050 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1051 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1053 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1055 (if (looking-at mime-edit-single-part-tag-regexp)
1057 ;; Make a space between the following message.
1060 (if (and (member (cadr ret) '("enriched"))
1061 (fboundp 'enriched-mode))
1063 (if (boundp 'enriched-mode)
1064 (enriched-mode -1))))))
1066 (defun mime-edit-insert-file (file &optional verbose)
1067 "Insert a message from a file."
1068 (interactive "fInsert file as MIME message: \nP")
1069 (let* ((guess (mime-find-file-type file))
1070 (type (nth 0 guess))
1071 (subtype (nth 1 guess))
1072 (parameters (nth 2 guess))
1073 (encoding (nth 3 guess))
1074 (disposition-type (nth 4 guess))
1075 (disposition-params (nth 5 guess)))
1077 (setq type (mime-prompt-for-type type)
1078 subtype (mime-prompt-for-subtype type subtype)))
1079 (if (or (interactive-p) verbose)
1080 (setq encoding (mime-prompt-for-encoding encoding)))
1081 (if (or (consp parameters) (stringp disposition-type))
1082 (let ((rest parameters) cell attribute value)
1083 (setq parameters "")
1085 (setq cell (car rest))
1086 (setq attribute (car cell))
1087 (setq value (cdr cell))
1088 (if (eq value 'file)
1089 (setq value (std11-wrap-as-quoted-string
1090 (file-name-nondirectory file))))
1091 (setq parameters (concat parameters "; " attribute "=" value))
1092 (setq rest (cdr rest)))
1093 (if disposition-type
1096 (concat parameters "\n"
1097 "Content-Disposition: " disposition-type))
1098 (setq rest disposition-params)
1100 (setq cell (car rest))
1101 (setq attribute (car cell))
1102 (setq value (cdr cell))
1103 (if (eq value 'file)
1104 (setq value (std11-wrap-as-quoted-string
1105 (file-name-nondirectory file))))
1107 (concat parameters "; " attribute "=" value))
1108 (setq rest (cdr rest)))))))
1109 (mime-edit-insert-tag type subtype parameters)
1110 (mime-edit-insert-binary-file file encoding)))
1112 (defun mime-edit-insert-external ()
1113 "Insert a reference to external body."
1115 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1117 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1119 (let* ((pritype (mime-prompt-for-type))
1120 (subtype (mime-prompt-for-subtype pritype))
1121 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1124 (insert "Content-Type: "
1125 pritype "/" subtype (or parameters "") "\n")))
1126 (if (and (not (eobp))
1127 (not (looking-at mime-edit-single-part-tag-regexp)))
1128 (insert (mime-make-text-tag) "\n")))
1130 (defun mime-edit-insert-voice ()
1131 "Insert a voice message."
1135 "What transfer encoding: "
1136 (mime-encoding-alist) nil t nil)))
1137 (mime-edit-insert-tag "audio" "basic" nil)
1138 (mime-edit-define-encoding encoding)
1140 (narrow-to-region (point)(point))
1142 (funcall mime-edit-voice-recorder encoding)
1145 (add-text-properties
1146 (point-min)(point-max) '(invisible t mime-edit-invisible t))
1147 (goto-char (point-max)))))))
1149 (defun mime-edit-insert-signature (&optional arg)
1150 "Insert a signature file."
1152 (let ((signature-insert-hook
1155 (let ((items (mime-find-file-type signature-file-name)))
1156 (apply (function mime-edit-insert-tag)
1157 (car items) (cadr items) (list (caddr items))))))))
1158 (insert-signature arg)))
1161 ;; Insert a new tag around a point.
1163 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1164 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1165 If nothing is inserted, return nil."
1168 (mime-edit-goto-tag)
1169 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1170 (< (match-beginning 0) p)
1171 (< p (match-end 0)))
1172 (goto-char (match-beginning 0))
1179 (mime-prompt-for-type)))
1182 (mime-prompt-for-subtype pritype)))
1185 (mime-prompt-for-parameters pritype subtype delimiter)))
1186 ;; Make a new MIME tag.
1187 (setq newtag (mime-make-tag pritype subtype parameters))
1188 ;; Find an current MIME tag.
1191 (if (mime-edit-goto-tag)
1192 (buffer-substring (match-beginning 0) (match-end 0))
1193 ;; Assume content type is 'text/plan'.
1194 (mime-make-tag "text" "plain"))))
1195 ;; We are only interested in TEXT.
1197 (not (mime-test-content-type
1198 (mime-edit-get-contype oldtag) "text")))
1201 (if (or (not oldtag) ;Not text
1202 (or mime-ignore-same-text-tag
1203 (not (string-equal oldtag newtag))))
1205 ;; Mark the beginning of the tag for convenience.
1206 (push-mark (point) 'nomsg)
1207 (insert newtag "\n")
1208 (list pritype subtype parameters) ;New tag is created.
1210 ;; Restore previous point.
1212 nil ;Nothing is created.
1215 (defun mime-edit-insert-binary-file (file &optional encoding)
1216 "Insert binary FILE at point.
1217 Optional argument ENCODING specifies an encoding method such as base64."
1218 (let* ((tagend (1- (point))) ;End of the tag
1219 (hide-p (and mime-auto-hide-body
1222 (let ((en (downcase encoding)))
1223 (or (string-equal en "7bit")
1224 (string-equal en "8bit")
1225 (string-equal en "binary")))))))
1227 (narrow-to-region (point)(point))
1228 (mime-insert-encoded-file file encoding)
1230 (add-text-properties
1231 (point-min)(point-max) '(invisible t mime-edit-invisible t)))
1232 (goto-char (point-max)))
1234 (looking-at mime-edit-tag-regexp)
1235 (= (point)(point-max))
1236 (mime-edit-insert-tag "text" "plain"))
1237 ;; Define encoding even if it is 7bit.
1238 (if (stringp encoding)
1240 (goto-char tagend) ; Make sure which line the tag is on.
1241 (mime-edit-define-encoding encoding)))))
1244 ;; Commands work on a current message flagment.
1246 (defun mime-edit-goto-tag ()
1247 "Search for the beginning of the tagged MIME message."
1248 (let ((current (point)))
1249 (if (looking-at mime-edit-tag-regexp)
1251 ;; At first, go to the end.
1252 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1253 (goto-char (1- (match-beginning 0))) ;For multiline tag
1256 (goto-char (point-max))))
1257 ;; Then search for the beginning.
1258 (re-search-backward mime-edit-end-tag-regexp nil t)
1259 (or (looking-at mime-edit-beginning-tag-regexp)
1260 ;; Restore previous point.
1265 (defun mime-edit-content-beginning ()
1266 "Return the point of the beginning of content."
1268 (let ((beg (save-excursion
1269 (beginning-of-line) (point))))
1270 (if (mime-edit-goto-tag)
1271 (let ((top (point)))
1272 (goto-char (match-end 0))
1273 (if (and (= beg top)
1274 (= (following-char) ?\^M))
1278 ;; Default text/plain tag.
1279 (goto-char (point-min))
1281 (concat "\n" (regexp-quote mail-header-separator)
1282 (if mime-ignore-preceding-spaces
1283 "[ \t\n]*\n" "\n")) nil 'move)
1286 (defun mime-edit-content-end ()
1287 "Return the point of the end of content."
1289 (if (mime-edit-goto-tag)
1291 (goto-char (1+ (match-end 0)))
1292 (if (get-text-property (point) 'mime-edit-invisible)
1293 (or (next-single-property-change (point) 'mime-edit-invisible)
1295 ;; Move to the end of this text.
1296 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1297 ;; Don't forget a multiline tag.
1298 (goto-char (match-beginning 0)))
1300 ;; Assume the message begins with text/plain.
1301 (goto-char (mime-edit-content-beginning))
1302 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1303 ;; Don't forget a multiline tag.
1304 (goto-char (match-beginning 0)))
1307 (defun mime-edit-define-charset (charset)
1308 "Set charset of current tag to CHARSET."
1310 (if (mime-edit-goto-tag)
1311 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1312 (delete-region (match-beginning 0) (match-end 0))
1315 (mime-edit-set-parameter
1316 (mime-edit-get-contype tag)
1318 (let ((comment (get charset 'mime-charset-comment)))
1320 (concat (upcase (symbol-name charset)) " (" comment ")")
1321 (upcase (symbol-name charset)))))
1322 (mime-edit-get-encoding tag)))))))
1324 (defun mime-edit-define-encoding (encoding)
1325 "Set encoding of current tag to ENCODING."
1327 (if (mime-edit-goto-tag)
1328 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1329 (delete-region (match-beginning 0) (match-end 0))
1330 (insert (mime-create-tag (mime-edit-get-contype tag) encoding))))))
1332 (defun mime-edit-choose-charset ()
1333 "Choose charset of a text following current point."
1334 (detect-mime-charset-region (point) (mime-edit-content-end)))
1336 (defun mime-make-text-tag (&optional subtype)
1337 "Make a tag for a text after current point.
1338 Subtype of text type can be specified by an optional argument SUBTYPE.
1339 Otherwise, it is obtained from mime-content-types."
1340 (let* ((pritype "text")
1341 (subtype (or subtype
1342 (car (car (cdr (assoc pritype mime-content-types)))))))
1343 ;; Charset should be defined later.
1344 (mime-make-tag pritype subtype)))
1347 ;; Tag handling functions
1349 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1350 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1351 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1355 (defun mime-create-tag (contype &optional encoding)
1356 "Make a tag with CONTENT-TYPE and optional ENCODING."
1357 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1360 (defun mime-edit-get-contype (tag)
1361 "Return Content-Type (including parameters) of TAG."
1363 (or (string-match mime-edit-single-part-tag-regexp tag)
1364 (string-match mime-edit-multipart-beginning-regexp tag)
1365 (string-match mime-edit-multipart-end-regexp tag))
1366 (substring tag (match-beginning 1) (match-end 1))))
1368 (defun mime-edit-get-encoding (tag)
1369 "Return encoding of TAG."
1371 (string-match mime-edit-single-part-tag-regexp tag)
1373 (not (= (match-beginning 3) (match-end 3)))
1374 (substring tag (match-beginning 3) (match-end 3))))
1376 (defun mime-get-parameter (contype parameter)
1377 "For given CONTYPE return value for PARAMETER.
1378 Nil if no such parameter."
1382 (regexp-quote parameter)
1383 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1385 (substring contype (match-beginning 1) (match-end 1))
1386 nil ;No such parameter
1389 (defun mime-edit-set-parameter (contype parameter value)
1390 "For given CONTYPE set PARAMETER to VALUE."
1391 (let (ctype opt-fields)
1392 (if (string-match "\n[^ \t\n\r]+:" contype)
1393 (setq ctype (substring contype 0 (match-beginning 0))
1394 opt-fields (substring contype (match-beginning 0)))
1395 (setq ctype contype))
1399 (regexp-quote parameter)
1400 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1403 (concat (substring ctype 0 (match-beginning 1))
1405 (substring ctype (match-end 1))
1407 (concat ctype "; " parameter "=" value opt-fields)
1410 (defun mime-strip-parameters (contype)
1411 "Return primary content-type and subtype without parameters for CONTYPE."
1412 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1413 (substring contype (match-beginning 1) (match-end 1)) nil))
1415 (defun mime-test-content-type (contype type &optional subtype)
1416 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1417 (and (stringp contype)
1420 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1421 (downcase contype))))
1426 (defun mime-find-file-type (file)
1427 "Guess Content-Type, subtype, and parameters from FILE."
1429 (guesses mime-file-types))
1430 (while (and (not guess) guesses)
1431 (if (string-match (car (car guesses)) file)
1432 (setq guess (cdr (car guesses))))
1433 (setq guesses (cdr guesses)))
1436 (defun mime-prompt-for-type (&optional default)
1437 "Ask for Content-type."
1439 ;; Repeat until primary content type is specified.
1440 (while (string-equal type "")
1442 (completing-read "What content type: "
1445 'require-match ;Type must be specified.
1447 (if (string-equal type "")
1449 (message "Content type is required.")
1454 (defun mime-prompt-for-subtype (type &optional default)
1455 "Ask for subtype of media-type TYPE."
1456 (let ((subtypes (cdr (assoc type mime-content-types))))
1458 (assoc default subtypes))
1459 (setq default (car (car subtypes)))))
1464 "What content subtype: (default " default ") ")
1465 "What content subtype: ")
1466 (cdr (assoc type mime-content-types))
1468 'require-match ;Subtype must be specified.
1470 (if (string-equal answer "") default answer)))
1472 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1473 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1474 Optional DELIMITER specifies parameter delimiter (';' by default)."
1475 (let* ((delimiter (or delimiter "; "))
1480 (mime-prompt-for-parameters-1
1482 (cdr (assoc pritype mime-content-types))))))
1484 (if (and (stringp parameters)
1485 (not (string-equal parameters "")))
1486 (concat delimiter parameters)
1487 "" ;"" if no parameters
1490 (defun mime-prompt-for-parameters-1 (optlist)
1491 (apply (function append)
1492 (mapcar (function mime-prompt-for-parameter) optlist)))
1494 (defun mime-prompt-for-parameter (parameter)
1496 Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
1497 (let* ((prompt (car parameter))
1498 (choices (mapcar (function
1500 (if (consp e) e (list e))))
1502 (default (car (car choices)))
1508 (concat "What " prompt
1510 (if (string-equal default "") "\"\"" default)
1512 choices nil nil ""))
1513 ;; If nothing is selected, use default.
1514 (if (string-equal answer "")
1515 (setq answer default)))
1517 (read-string (concat "What " prompt ": "))))
1518 (cons (if (and answer
1519 (not (string-equal answer "")))
1521 ;; Note: control characters ignored!
1522 (if (string-match mime-tspecials-regexp answer)
1523 (concat "\"" answer "\"") answer)))
1524 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))))
1526 (defun mime-prompt-for-encoding (default)
1527 "Ask for Content-Transfer-Encoding."
1532 "What transfer encoding: "
1533 (mime-encoding-alist) nil t default))
1538 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1541 (defvar mime-edit-translate-buffer-hook
1542 '(mime-edit-pgp-enclose-buffer
1543 mime-edit-translate-body
1544 mime-edit-translate-header))
1546 (defun mime-edit-translate-header ()
1547 "Encode the message header into network representation."
1548 (mime-encode-header-in-buffer 'code-conversion)
1549 (run-hooks 'mime-edit-translate-header-hook))
1551 (defun mime-edit-translate-buffer ()
1552 "Encode the tagged MIME message in current buffer in MIME compliant message."
1555 (if (catch 'mime-edit-error
1557 (run-hooks 'mime-edit-translate-buffer-hook)))
1560 (error "Translation error!"))))
1562 (defun mime-edit-find-inmost ()
1563 (goto-char (point-min))
1564 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1565 (let ((bb (match-beginning 0))
1567 (type (buffer-substring (match-beginning 1)(match-end 1)))
1569 (setq end-exp (format "--}-<<%s>>\n" type))
1571 (if (re-search-forward end-exp nil t)
1572 (setq eb (match-beginning 0))
1573 (setq eb (point-max)))
1574 (narrow-to-region be eb)
1576 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1578 (narrow-to-region (match-beginning 0)(point-max))
1579 (mime-edit-find-inmost))
1581 (list type bb be eb)))))
1583 (defun mime-edit-process-multipart-1 (boundary)
1584 (let ((ret (mime-edit-find-inmost)))
1586 (let ((type (car ret))
1587 (bb (nth 1 ret))(be (nth 2 ret))
1589 (narrow-to-region bb eb)
1590 (delete-region bb be)
1591 (setq bb (point-min))
1592 (setq eb (point-max))
1595 (if (looking-at mime-edit-multipart-end-regexp)
1596 (let ((beg (match-beginning 0))
1597 (end (match-end 0)))
1598 (delete-region beg end)
1599 (or (looking-at mime-edit-beginning-tag-regexp)
1601 (insert (concat (mime-make-text-tag) "\n")))))
1602 (cond ((string-equal type "quote")
1603 (mime-edit-enquote-region bb eb))
1604 ((string-equal type "pgp-signed")
1605 (mime-edit-sign-pgp-mime bb eb boundary))
1606 ((string-equal type "pgp-encrypted")
1607 (mime-edit-encrypt-pgp-mime bb eb boundary))
1608 ((string-equal type "kazu-signed")
1609 (mime-edit-sign-pgp-kazu bb eb boundary))
1610 ((string-equal type "kazu-encrypted")
1611 (mime-edit-encrypt-pgp-kazu bb eb boundary))
1612 ((string-equal type "smime-signed")
1613 (mime-edit-sign-smime bb eb boundary))
1614 ((string-equal type "smime-encrypted")
1615 (mime-edit-encrypt-smime bb eb boundary))
1618 (nth 2 (mime-edit-translate-region bb eb
1622 (format "--[[multipart/%s;
1623 boundary=\"%s\"][7bit]]\n"
1627 (defun mime-edit-enquote-region (beg end)
1630 (narrow-to-region beg end)
1632 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1633 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1634 (replace-match (concat "- " (substring tag 1))))))))
1636 (defun mime-edit-dequote-region (beg end)
1639 (narrow-to-region beg end)
1641 (while (re-search-forward
1642 mime-edit-quoted-single-part-tag-regexp nil t)
1643 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1644 (replace-match (concat "-" (substring tag 2))))))))
1646 (defvar mime-edit-pgp-user-id nil)
1648 (defun mime-edit-sign-pgp-mime (beg end boundary)
1651 (let* ((from (std11-field-body "From" mail-header-separator))
1653 (narrow-to-region beg end)
1654 (mime-edit-translate-region beg end boundary)))
1656 (encoding (nth 1 ret))
1657 (pgp-boundary (concat "pgp-sign-" boundary))
1660 (insert (format "Content-Type: %s\n" ctype))
1662 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1664 (or (let ((pgg-default-user-id
1665 (or mime-edit-pgp-user-id
1667 (nth 1 (std11-extract-address-components from))
1668 pgg-default-user-id))))
1669 (pgg-sign-region (point-min)(point-max)))
1670 (throw 'mime-edit-error 'pgp-error))
1672 (cdr (assq 'hash-algorithm
1673 (cdar (with-current-buffer pgg-output-buffer
1674 (pgg-parse-armor-region
1675 (point-min)(point-max))))))
1678 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1681 (insert (format "--[[multipart/signed;
1683 protocol=\"application/pgp-signature\"][7bit]]
1685 " pgp-boundary micalg pgp-boundary))
1686 (goto-char (point-max))
1687 (insert (format "\n--%s
1688 Content-Type: application/pgp-signature
1689 Content-Transfer-Encoding: 7bit
1692 (insert-buffer-substring pgg-output-buffer)
1693 (goto-char (point-max))
1694 (insert (format "\n--%s--\n" pgp-boundary))))))
1696 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1698 (defun mime-edit-make-encrypt-recipient-header ()
1699 (let* ((names mime-edit-encrypt-recipient-fields-list)
1701 (std11-field-bodies (cons "From" names)
1702 nil mail-header-separator))
1705 (setq values (cdr values))))
1706 (header (and (stringp from)
1707 (if (string-equal from "")
1709 (format "From: %s\n" from))))
1711 (while (and names values)
1712 (let ((name (car names))
1713 (value (car values)))
1714 (and (stringp value)
1715 (or (string-equal value "")
1717 (setq header (concat header name ": " value "\n")
1718 recipients (if recipients
1719 (concat recipients " ," value)
1721 (setq names (cdr names)
1722 values (cdr values)))
1723 (vector from recipients header)))
1725 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1728 (let (from recipients header)
1729 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1730 (setq from (aref ret 0)
1731 recipients (aref ret 1)
1732 header (aref ret 2)))
1733 (narrow-to-region beg end)
1735 (mime-edit-translate-region beg end boundary))
1737 (encoding (nth 1 ret))
1738 (pgp-boundary (concat "pgp-" boundary)))
1741 (insert (format "Content-Type: %s\n" ctype))
1743 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1745 (mime-encode-header-in-buffer)
1746 (or (let ((pgg-default-user-id
1747 (or mime-edit-pgp-user-id
1749 (nth 1 (std11-extract-address-components from))
1750 pgg-default-user-id))))
1752 (point-min) (point-max)
1753 (mapcar (lambda (recipient)
1754 (nth 1 (std11-extract-address-components
1756 (split-string recipients
1757 "\\([ \t\n]*,[ \t\n]*\\)+"))))
1758 (throw 'mime-edit-error 'pgp-error))
1759 (delete-region (point-min)(point-max))
1761 (insert (format "--[[multipart/encrypted;
1763 protocol=\"application/pgp-encrypted\"][7bit]]
1765 Content-Type: application/pgp-encrypted
1768 Content-Type: application/octet-stream
1769 Content-Transfer-Encoding: 7bit
1771 " pgp-boundary pgp-boundary pgp-boundary))
1772 (insert-buffer-substring pgg-output-buffer)
1773 (goto-char (point-max))
1774 (insert (format "\n--%s--\n" pgp-boundary)))))))
1776 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1779 (narrow-to-region beg end)
1781 (mime-edit-translate-region beg end boundary))
1783 (encoding (nth 1 ret)))
1785 (insert (format "Content-Type: %s\n" ctype))
1787 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1789 (or (pgg-sign-region beg (point-max) 'clearsign)
1790 (throw 'mime-edit-error 'pgp-error))
1793 "--[[application/pgp; format=mime][7bit]]\n")
1796 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1798 (let (recipients header)
1799 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1800 (setq recipients (aref ret 1)
1801 header (aref ret 2)))
1803 (narrow-to-region beg end)
1805 (mime-edit-translate-region beg end boundary))
1807 (encoding (nth 1 ret)))
1810 (insert (format "Content-Type: %s\n" ctype))
1812 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1814 (or (pgg-encrypt-region beg (point-max) recipients)
1815 (throw 'mime-edit-error 'pgp-error))
1818 "--[[application/pgp; format=mime][7bit]]\n")
1821 (defun mime-edit-sign-smime (beg end boundary)
1825 (narrow-to-region beg end)
1826 (mime-edit-translate-region beg end boundary)))
1828 (encoding (nth 1 ret))
1829 (smime-boundary (concat "smime-sign-" boundary)))
1831 (insert (format "Content-Type: %s\n" ctype))
1833 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1835 (let (buffer-undo-list)
1836 (goto-char (point-min))
1837 (while (progn (end-of-line) (not (eobp)))
1840 (or (smime-sign-buffer)
1841 (throw 'mime-edit-error 'pgp-error)))
1843 (if (re-search-forward "^Content-Type:\\s-*" nil t)
1844 (let* ((start (match-beginning 0))
1845 (body (buffer-substring (match-end 0) (std11-field-end))))
1846 (delete-region start (line-beginning-position 2))
1848 (insert "--[[" body "][7bit]]\n")))))))
1850 (defun mime-edit-encrypt-smime (beg end boundary)
1854 (narrow-to-region beg end)
1855 (mime-edit-translate-region beg end boundary)))
1857 (encoding (nth 1 ret)))
1859 (insert (format "Content-Type: %s\n" ctype))
1861 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1863 (goto-char (point-min))
1864 (while (progn (end-of-line) (not (eobp)))
1867 (or (smime-encrypt-buffer)
1868 (throw 'mime-edit-error 'pgp-error))
1870 (if (re-search-forward "^Content-Type:\\s-*" nil t)
1871 (let* ((start (match-beginning 0))
1872 (body (buffer-substring (match-end 0) (std11-field-end))))
1873 (delete-region start (line-beginning-position 2))
1875 (insert "--[[" body "]]\n")))))))
1877 (defsubst replace-space-with-underline (str)
1878 (mapconcat (function
1885 (defun mime-edit-make-boundary ()
1886 (concat mime-multipart-boundary "_"
1887 (replace-space-with-underline (current-time-string))))
1889 (defun mime-edit-translate-body ()
1890 "Encode the tagged MIME body in current buffer in MIME compliant message."
1893 (let ((boundary (mime-edit-make-boundary))
1896 (while (mime-edit-process-multipart-1
1897 (format "%s-%d" boundary i))
1900 ;; We are interested in message body.
1903 (goto-char (point-min))
1905 (concat "\n" (regexp-quote mail-header-separator)
1906 (if mime-ignore-preceding-spaces
1907 "[ \t\n]*\n" "\n")) nil 'move)
1911 (goto-char (point-max))
1912 (and mime-ignore-trailing-spaces
1913 (re-search-backward "[^ \t\n]\n" beg t)
1916 (setq ret (mime-edit-translate-region
1918 (format "%s-%d" boundary i)))))
1919 (mime-edit-dequote-region (point-min)(point-max))
1920 (let ((contype (car ret)) ;Content-Type
1921 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1923 ;; Insert User-Agent field
1924 (and mime-edit-insert-user-agent-field
1925 (or (mail-position-on-field "User-Agent")
1926 (insert mime-edit-user-agent-value)))
1927 ;; Make primary MIME headers.
1928 (or (mail-position-on-field "MIME-Version")
1929 (insert mime-edit-mime-version-value))
1930 ;; Remove old Content-Type and other fields.
1932 (goto-char (point-min))
1933 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1934 (narrow-to-region (point-min) (point))
1935 (goto-char (point-min))
1936 (mime-delete-field "Content-Type")
1937 (mime-delete-field "Content-Transfer-Encoding"))
1938 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1939 (mail-position-on-field "Content-Type")
1943 (mail-position-on-field "Content-Transfer-Encoding")
1944 (insert encoding)))))))
1946 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
1947 "Translate single-part-tag to MIME header."
1948 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1949 (let* ((beg (match-beginning 0))
1951 (tag (buffer-substring beg end)))
1952 (delete-region beg end)
1953 (let ((contype (mime-edit-get-contype tag))
1954 (encoding (mime-edit-get-encoding tag)))
1955 (insert (concat prefix "--" boundary "\n"))
1957 (narrow-to-region (point)(point))
1958 (insert "Content-Type: " contype "\n")
1960 (insert "Content-Transfer-Encoding: " encoding "\n"))
1961 (mime-encode-header-in-buffer))
1965 (downcase encoding)))))))
1967 (defun mime-edit-translate-region (beg end &optional boundary multipart)
1969 (setq boundary (mime-edit-make-boundary)))
1972 (narrow-to-region beg end)
1973 (let ((tag nil) ;MIME tag
1974 (contype nil) ;Content-Type
1975 (encoding nil) ;Content-Transfer-Encoding
1976 (nparts 0)) ;Number of body parts
1977 ;; Normalize the body part by inserting appropriate message
1978 ;; tags for every message contents.
1979 (mime-edit-normalize-body)
1980 ;; Counting the number of Content-Type.
1981 (goto-char (point-min))
1982 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1983 (setq nparts (1+ nparts)))
1984 ;; Begin translation.
1986 ((and (<= nparts 1)(not multipart))
1987 ;; It's a singular message.
1988 (goto-char (point-min))
1989 (while (re-search-forward
1990 mime-edit-single-part-tag-regexp nil t)
1992 (buffer-substring (match-beginning 0) (match-end 0)))
1993 (delete-region (match-beginning 0) (1+ (match-end 0)))
1994 (setq contype (mime-edit-get-contype tag))
1995 (setq encoding (mime-edit-get-encoding tag))))
1997 ;; It's a multipart message.
1998 (goto-char (point-min))
1999 (let ((prio mime-content-transfer-encoding-priority-list)
2001 (when (setq part-info
2002 (mime-edit-translate-single-part-tag boundary))
2003 (and (setq nprio (member (cdr part-info) prio))
2005 (while (setq part-info
2006 (mime-edit-translate-single-part-tag boundary "\n"))
2007 (and (setq nprio (member (cdr part-info) prio))
2008 (setq prio nprio))))
2009 ;; Define Content-Type as "multipart/mixed".
2011 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2012 (setq encoding (car prio))
2013 ;; Insert the trailer.
2014 (goto-char (point-max))
2015 (insert "\n--" boundary "--\n"))))
2016 (list contype encoding boundary nparts)))))
2018 (defun mime-edit-normalize-body ()
2019 "Normalize the body part by inserting appropriate message tags."
2020 ;; Insert the first MIME tags if necessary.
2021 (goto-char (point-min))
2022 (if (not (looking-at mime-edit-single-part-tag-regexp))
2023 (insert (mime-make-text-tag) "\n"))
2024 ;; Check each tag, and add new tag or correct it if necessary.
2025 (goto-char (point-min))
2026 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2027 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2028 (contype (mime-edit-get-contype tag))
2029 (charset (mime-get-parameter contype "charset"))
2030 (encoding (mime-edit-get-encoding tag)))
2031 ;; Remove extra whitespaces after the tag.
2032 (if (looking-at "[ \t]+$")
2033 (delete-region (match-beginning 0) (match-end 0)))
2035 (end (mime-edit-content-end)))
2036 (if (= end (point-max))
2039 (or (looking-at mime-edit-beginning-tag-regexp)
2041 (insert (mime-make-text-tag) "\n")))
2042 (remove-text-properties beg end '(invisible mime-edit-invisible))
2045 ((mime-test-content-type contype "message")
2046 ;; Content-type "message" should be sent as is.
2048 ((mime-test-content-type contype "text")
2049 ;; Define charset for text if necessary.
2050 (setq charset (if charset
2051 (intern (downcase charset))
2052 (mime-edit-choose-charset)))
2053 (mime-edit-define-charset charset)
2054 (cond ((string-equal contype "text/x-rot13-47-48")
2057 (mule-caesar-region (point) (mime-edit-content-end))))
2058 ((string-equal contype "text/enriched")
2063 (end (mime-edit-content-end)))
2064 ;; Patch for hard newlines
2067 ;; (while (search-forward "\n" end t)
2068 ;; (put-text-property (match-beginning 0)
2071 ;; End patch for hard newlines
2072 (enriched-encode beg end nil)
2074 (if (search-forward "\n\n")
2075 (delete-region beg (match-end 0)))))))
2076 ;; Point is now on current tag.
2077 ;; Define encoding and encode text if necessary.
2078 (or encoding ;Encoding is not specified.
2081 (let ((ret (cdr (assq charset mime-charset-type-list))))
2083 (setq bits (car ret)
2086 conv "quoted-printable")))
2087 (if (<= bits mime-transfer-level)
2088 (mime-encoding-name bits)
2090 (beg (mime-edit-content-beginning)))
2091 (encode-mime-charset-region beg (mime-edit-content-end)
2093 ;; Protect "From " in beginning of line
2095 (narrow-to-region beg (mime-edit-content-end))
2097 (let (case-fold-search)
2098 (if (re-search-forward "^From " nil t)
2100 (if (memq charset '(iso-2022-jp
2105 (replace-match "\e(BFrom ")
2106 (re-search-forward "^From " nil t)))
2107 (setq encoding "quoted-printable"))))))
2108 ;; canonicalize line break code
2109 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2111 (narrow-to-region beg (mime-edit-content-end))
2113 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2114 (replace-match "\\1\r\n"))))
2116 (mime-encode-region beg (mime-edit-content-end)
2117 (or encoding "7bit"))
2118 (mime-edit-define-encoding encoding)))
2119 (goto-char (mime-edit-content-end)))
2120 ((null encoding) ;Encoding is not specified.
2121 ;; Application, image, audio, video, and any other
2122 ;; unknown content-type without encoding should be
2124 (let* ((encoding "base64") ;Encode in BASE64 by default.
2125 (beg (mime-edit-content-beginning))
2126 (end (mime-edit-content-end)))
2127 (mime-encode-region beg end encoding)
2128 (mime-edit-define-encoding encoding))
2129 (forward-line 1))))))
2131 (defun mime-delete-field (field)
2132 "Delete header FIELD."
2133 (let ((regexp (format "^%s:[ \t]*" field)))
2134 (goto-char (point-min))
2135 (while (re-search-forward regexp nil t)
2136 (delete-region (match-beginning 0)
2137 (1+ (std11-field-end))))))
2141 ;;; Platform dependent functions
2144 ;; Sun implementations
2146 (defun mime-edit-voice-recorder-for-sun (encoding)
2147 "Record voice in a buffer using Sun audio device,
2148 and insert data encoded as ENCODING."
2149 (message "Start the recording on %s. Type C-g to finish the recording..."
2151 (mime-insert-encoded-file "/dev/audio" encoding))
2154 ;;; @ Other useful commands.
2157 ;; Message forwarding commands as content-type "message/rfc822".
2159 (defun mime-edit-insert-message (&optional message)
2161 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2162 (if (and inserter (fboundp inserter))
2164 (mime-edit-insert-tag "message" "rfc822")
2165 (funcall inserter message))
2166 (message "Sorry, I don't have message inserter for your MUA."))))
2168 (defun mime-edit-insert-mail (&optional message)
2170 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2171 (if (and inserter (fboundp inserter))
2173 (mime-edit-insert-tag "message" "rfc822")
2174 (funcall inserter message))
2175 (message "Sorry, I don't have mail inserter for your MUA."))))
2177 (defun mime-edit-inserted-message-filter ()
2180 (let ((header-start (point))
2181 (case-fold-search t)
2184 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2185 (if (re-search-forward "^$" (mark t))
2186 (narrow-to-region header-start (match-beginning 0)))
2187 (goto-char header-start)
2188 (while (and (re-search-forward
2189 mime-edit-yank-ignored-field-regexp nil t)
2190 (setq beg (match-beginning 0))
2191 (setq end (1+ (std11-field-end))))
2192 (delete-region beg end))))))
2195 ;;; @ multipart enclosure
2198 (defun mime-edit-enclose-region-internal (type beg end)
2202 (narrow-to-region beg end)
2203 (insert (format "--<<%s>>-{\n" type))
2204 (goto-char (point-max))
2205 (insert (format "--}-<<%s>>\n" type))
2206 (goto-char (point-max)))
2207 (or (looking-at mime-edit-beginning-tag-regexp)
2209 (insert (mime-make-text-tag) "\n"))))
2211 (defun mime-edit-enclose-quote-region (beg end)
2213 (mime-edit-enclose-region-internal 'quote beg end))
2215 (defun mime-edit-enclose-mixed-region (beg end)
2217 (mime-edit-enclose-region-internal 'mixed beg end))
2219 (defun mime-edit-enclose-parallel-region (beg end)
2221 (mime-edit-enclose-region-internal 'parallel beg end))
2223 (defun mime-edit-enclose-digest-region (beg end)
2225 (mime-edit-enclose-region-internal 'digest beg end))
2227 (defun mime-edit-enclose-alternative-region (beg end)
2229 (mime-edit-enclose-region-internal 'alternative beg end))
2231 (defun mime-edit-enclose-pgp-signed-region (beg end)
2233 (mime-edit-enclose-region-internal 'pgp-signed beg end))
2235 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2237 (mime-edit-enclose-region-internal 'pgp-encrypted beg end))
2239 (defun mime-edit-enclose-kazu-signed-region (beg end)
2241 (mime-edit-enclose-region-internal 'kazu-signed beg end))
2243 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2245 (mime-edit-enclose-region-internal 'kazu-encrypted beg end))
2247 (defun mime-edit-enclose-smime-signed-region (beg end)
2249 (mime-edit-enclose-region-internal 'smime-signed beg end))
2251 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2253 (mime-edit-enclose-region-internal 'smime-encrypted beg end))
2255 (defun mime-edit-insert-key (&optional arg)
2256 "Insert a pgp public key."
2258 (mime-edit-insert-tag "application" "pgp-keys")
2259 (mime-edit-define-encoding "7bit")
2261 (if (and (not (eobp))
2262 (not (looking-at mime-edit-single-part-tag-regexp)))
2263 (insert (mime-make-text-tag) "\n")))
2269 (defun mime-edit-set-split (arg)
2272 (y-or-n-p "Do you want to enable split? ")))
2273 (setq mime-edit-split-message arg)
2275 (message "This message is enabled to split.")
2276 (message "This message is not enabled to split.")))
2278 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2279 "Toggle transfer-level is 7bit or 8bit through.
2281 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2283 (if (numberp transfer-level)
2284 (setq mime-transfer-level transfer-level)
2285 (if (< mime-transfer-level 8)
2286 (setq mime-transfer-level 8)
2287 (setq mime-transfer-level 7)))
2288 (message (format "Current transfer-level is %d bit"
2289 mime-transfer-level))
2290 (setq mime-transfer-level-string
2291 (mime-encoding-name mime-transfer-level 'not-omit))
2292 (force-mode-line-update))
2294 (defun mime-edit-set-transfer-level-7bit ()
2296 (mime-edit-toggle-transfer-level 7))
2298 (defun mime-edit-set-transfer-level-8bit ()
2300 (mime-edit-toggle-transfer-level 8))
2306 (defvar mime-edit-pgp-processing nil)
2307 (make-variable-buffer-local 'mime-edit-pgp-processing)
2309 (defun mime-edit-set-sign (arg)
2312 (y-or-n-p "Do you want to sign? ")))
2315 (or (memq 'sign mime-edit-pgp-processing)
2316 (setq mime-edit-pgp-processing
2317 (nconc mime-edit-pgp-processing
2318 (copy-sequence '(sign)))))
2319 (message "This message will be signed."))
2320 (setq mime-edit-pgp-processing
2321 (delq 'sign mime-edit-pgp-processing))
2322 (message "This message will not be signed.")))
2324 (defun mime-edit-set-encrypt (arg)
2327 (y-or-n-p "Do you want to encrypt? ")))
2330 (or (memq 'encrypt mime-edit-pgp-processing)
2331 (setq mime-edit-pgp-processing
2332 (nconc mime-edit-pgp-processing
2333 (copy-sequence '(encrypt)))))
2334 (message "This message will be encrypt."))
2335 (setq mime-edit-pgp-processing
2336 (delq 'encrypt mime-edit-pgp-processing))
2337 (message "This message will not be encrypt.")))
2339 (defun mime-edit-pgp-enclose-buffer ()
2340 (let ((beg (save-excursion
2341 (goto-char (point-min))
2342 (if (search-forward (concat "\n" mail-header-separator "\n"))
2345 (dolist (pgp-processing mime-edit-pgp-processing)
2346 (case pgp-processing
2348 (mime-edit-enclose-pgp-signed-region
2351 (mime-edit-enclose-pgp-encrypted-region
2352 beg (point-max))))))))
2358 (defun mime-edit-insert-partial-header (fields subject
2359 id number total separator)
2361 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2362 (insert mime-edit-mime-version-field-for-message/partial)
2364 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2365 id number total separator)))
2367 (defun mime-edit-split-and-send
2368 (&optional cmd lines mime-edit-message-max-length)
2372 (count-lines (point-min) (point-max))))
2373 (or mime-edit-message-max-length
2374 (setq mime-edit-message-max-length
2375 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2376 mime-edit-message-default-max-lines)))
2377 (let* ((mime-edit-draft-file-name
2378 (or (buffer-file-name)
2380 (expand-file-name "mime-draft" temporary-file-directory))))
2381 (separator mail-header-separator)
2383 (replace-space-with-underline (current-time-string))
2384 "@" (system-name) "\"")))
2385 (run-hooks 'mime-edit-before-split-hook)
2386 (let ((the-buf (current-buffer))
2387 (copy-buf (get-buffer-create " *Original Message*"))
2388 (header (std11-header-string-except
2389 mime-edit-split-ignored-field-regexp separator))
2390 (subject (mail-fetch-field "subject"))
2391 (total (+ (/ lines mime-edit-message-max-length)
2392 (if (> (mod lines mime-edit-message-max-length) 0)
2398 mime-edit-split-message-sender-alist))
2402 (error "Split sender is not specified for `%s'." major-mode)))))
2403 (mime-edit-partial-number 1)
2406 (set-buffer copy-buf)
2408 (insert-buffer the-buf)
2410 (if (re-search-forward
2411 (concat "^" (regexp-quote separator) "$") nil t)
2412 (let ((he (match-beginning 0)))
2414 (narrow-to-region (point-min) he)))
2415 (goto-char (point-min))
2416 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2417 (delete-region (match-beginning 0)
2418 (1+ (std11-field-end))))))
2419 (while (< mime-edit-partial-number total)
2422 (set-buffer copy-buf)
2423 (setq data (buffer-substring
2426 (goto-line mime-edit-message-max-length)
2428 (delete-region (point-min)(point)))
2429 (mime-edit-insert-partial-header
2430 header subject id mime-edit-partial-number total separator)
2433 (message (format "Sending %d/%d..."
2434 mime-edit-partial-number total))
2435 (call-interactively command)
2436 (message (format "Sending %d/%d...done"
2437 mime-edit-partial-number total)))
2438 (setq mime-edit-partial-number
2439 (1+ mime-edit-partial-number)))
2442 (set-buffer copy-buf)
2443 (setq data (buffer-string))
2445 (mime-edit-insert-partial-header
2446 header subject id mime-edit-partial-number total separator)
2449 (message (format "Sending %d/%d..."
2450 mime-edit-partial-number total))
2451 (message (format "Sending %d/%d...done"
2452 mime-edit-partial-number total))))))
2454 (defun mime-edit-maybe-split-and-send (&optional cmd)
2456 (run-hooks 'mime-edit-before-send-hook)
2457 (let ((mime-edit-message-max-length
2458 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2459 mime-edit-message-default-max-lines))
2460 (lines (count-lines (point-min) (point-max))))
2461 (if (and (> lines mime-edit-message-max-length)
2462 mime-edit-split-message)
2463 (mime-edit-split-and-send cmd lines mime-edit-message-max-length))))
2466 ;;; @ preview message
2469 (defvar mime-edit-buffer nil) ; buffer local variable
2471 (defun mime-edit-preview-message ()
2472 "preview editing MIME message."
2474 (let* ((str (buffer-string))
2475 (separator mail-header-separator)
2476 (the-buf (current-buffer))
2477 (buf-name (buffer-name))
2478 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2479 (buf (get-buffer temp-buf-name))
2480 (pgp-processing mime-edit-pgp-processing))
2483 (switch-to-buffer buf)
2485 (setq buf (get-buffer-create temp-buf-name))
2486 (switch-to-buffer buf))
2488 (setq major-mode 'mime-temp-message-mode)
2489 (make-local-variable 'mail-header-separator)
2490 (setq mail-header-separator separator)
2491 (make-local-variable 'mime-edit-buffer)
2492 (setq mime-edit-buffer the-buf)
2493 (setq mime-edit-pgp-processing pgp-processing)
2495 (run-hooks 'mime-edit-translate-hook)
2496 (mime-edit-translate-buffer)
2497 (goto-char (point-min))
2498 (if (re-search-forward
2499 (concat "^" (regexp-quote separator) "$"))
2502 (make-local-variable 'mime-edit-temp-message-buffer)
2503 (setq mime-edit-temp-message-buffer buf)))
2505 (defun mime-edit-quitting-method ()
2506 "Quitting method for mime-view."
2507 (let* ((temp mime-edit-temp-message-buffer)
2509 (mime-preview-kill-buffer)
2511 (setq buf mime-edit-buffer)
2513 (switch-to-buffer buf)))
2515 (set-alist 'mime-preview-quitting-method-alist
2516 'mime-temp-message-mode
2517 #'mime-edit-quitting-method)
2523 (defvar mime-edit-again-ignored-field-regexp
2524 (concat "^\\(" "Content-.*\\|Mime-Version"
2525 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2527 "Regexp for deleted header fields when `mime-edit-again' is called.")
2529 (defsubst eliminate-top-spaces (string)
2530 "Eliminate top sequence of space or tab in STRING."
2531 (if (string-match "^[ \t]+" string)
2532 (substring string (match-end 0))
2535 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2538 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2539 '(("application/pgp-encrypted" . pgp-encrypted)
2540 ("application/pgp-signature" . pgp-signed))))
2541 (mime-content-type-subtype content-type)))
2542 (boundary (mime-content-type-parameter content-type "boundary"))
2543 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2544 (re-search-forward boundary-pat nil t)
2545 (let ((bb (match-beginning 0)) eb tag)
2546 (setq tag (format "\n--<<%s>>-{\n" subtype))
2549 (setq bb (+ bb (length tag)))
2551 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2553 (setq eb (match-beginning 0))
2554 (replace-match (format "--}-<<%s>>\n" subtype))
2556 (narrow-to-region bb eb)
2557 (goto-char (point-min))
2558 (while (re-search-forward boundary-pat nil t)
2559 (let ((beg (match-beginning 0))
2561 (delete-region beg (match-end 0))
2563 (if (re-search-forward boundary-pat nil t)
2564 (setq end (match-beginning 0))
2565 (setq end (point-max)))
2567 (narrow-to-region beg end)
2569 ((eq subtype 'pgp-encrypted)
2572 (goto-char (point-min))
2573 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2576 (save-window-excursion
2577 (pgg-decrypt-region (match-beginning 0)
2579 (delete-region (point-min)(point-max))))
2580 (insert-buffer-substring pgg-output-buffer)
2581 (mime-edit-decode-message-in-buffer
2582 nil not-decode-text)
2583 (delete-region (goto-char (point-min))
2584 (if (search-forward "\n\n" nil t)
2587 (goto-char (point-max))))
2589 (mime-edit-decode-message-in-buffer
2590 (if (eq subtype 'digest)
2592 (make-mime-content-type 'message 'rfc822)))
2594 (goto-char (point-max))))))))))
2595 (goto-char (point-min))
2596 (or (= (point-min) 1)
2597 (delete-region (point-min)
2598 (if (search-forward "\n\n" nil t)
2602 (defun mime-edit-decode-single-part-in-buffer
2603 (content-type not-decode-text &optional content-disposition)
2604 (let* ((type (mime-content-type-primary-type content-type))
2605 (subtype (mime-content-type-subtype content-type))
2606 (ctype (format "%s/%s" type subtype))
2608 (pstr (let ((bytes (+ 14 (length ctype))))
2609 (mapconcat (function
2611 (if (string= (car attr) "charset")
2613 (setq charset (cdr attr))
2615 (let* ((str (concat (car attr)
2618 (setq bytes (+ bytes bs 2))
2621 (setq bytes (+ bs 1))
2624 (mime-content-type-parameters content-type) "")))
2627 (limit (save-excursion
2628 (if (search-forward "\n\n" nil t)
2631 (mime-content-disposition-type content-disposition))
2633 (if disposition-type
2634 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2635 (mapconcat (function
2640 (if (string-equal "filename"
2642 (std11-wrap-as-quoted-string
2646 (setq bytes (+ bytes bs 2))
2649 (setq bytes (+ bs 1))
2652 (mime-content-disposition-parameters
2653 content-disposition)
2655 (if disposition-type
2656 (setq pstr (format "%s\nContent-Disposition: %s%s"
2657 pstr disposition-type disposition-str)))
2659 (if (re-search-forward
2660 "^Content-Transfer-Encoding:" limit t)
2661 (let ((beg (match-beginning 0))
2662 (hbeg (match-end 0))
2663 (end (std11-field-end limit)))
2666 (eliminate-top-spaces
2667 (std11-unfold-string
2668 (buffer-substring hbeg end)))))
2669 (if (or charset (eq type 'text))
2671 (delete-region beg (1+ end))
2672 (goto-char (point-min))
2673 (if (search-forward "\n\n" nil t)
2676 (match-end 0)(point-max) encoding)
2678 encoding nil))))))))
2679 (if (and (eq type 'text)
2680 (or encoded (not not-decode-text)))
2683 (goto-char (point-min))
2684 (while (re-search-forward "\r\n" nil t)
2685 (replace-match "\n")))
2686 (decode-mime-charset-region (point-min)(point-max)
2687 (or charset default-mime-charset))))
2688 (let ((he (if (re-search-forward "^$" nil t)
2691 (if (and (eq type 'text)
2692 (eq subtype 'x-rot13-47-48))
2693 (mule-caesar-region he (point-max)))
2694 (if (= (point-min) 1)
2700 (format "%s/%s%s" type subtype pstr)
2702 (delete-region (point-min) he)
2704 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2708 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2711 (goto-char (point-min))
2712 (let ((ctl (or (mime-read-Content-Type)
2713 default-content-type)))
2715 (let ((type (mime-content-type-primary-type ctl)))
2717 ((and (eq type 'application)
2718 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2719 (delete-region (point-min)(point-max)))
2720 ((eq type 'multipart)
2721 (mime-edit-decode-multipart-in-buffer ctl not-decode-text))
2723 (mime-edit-decode-single-part-in-buffer
2724 ctl not-decode-text (mime-read-Content-Disposition)))))
2726 (decode-mime-charset-region (point-min) (point-max)
2727 default-mime-charset)))
2728 (if (= (point-min) 1)
2731 (std11-narrow-to-header)
2732 (goto-char (point-min))
2733 (while (re-search-forward
2734 mime-edit-again-ignored-field-regexp nil t)
2735 (delete-region (match-beginning 0) (1+ (std11-field-end)))))
2736 (mime-decode-header-in-buffer (not not-decode-text)))))))
2739 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2740 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2741 Content-Type and Content-Transfer-Encoding header fields will be
2742 converted to MIME-Edit tags."
2744 (goto-char (point-min))
2746 (concat "\n" (regexp-quote mail-header-separator) "\n")
2748 (replace-match "\n\n"))
2749 (mime-edit-decode-message-in-buffer nil not-decode-text)
2750 (goto-char (point-min))
2752 (and (re-search-forward "^$")
2753 (replace-match mail-header-separator)))
2755 (turn-on-mime-edit)))
2761 (provide 'mime-edit)
2763 (run-hooks 'mime-edit-load-hook)
2765 ;;; mime-edit.el ends here