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"))
235 ("vnd.ms-powerpoint")
236 ("x-kiss" ("x-cnf")))
248 "*Alist of content-type, subtype, parameters and its values.")
250 (defcustom mime-file-types
253 ;; Programming languages
256 "application" "octet-stream" (("type" . "C++"))
258 "attachment" (("filename" . file)))
261 "application" "octet-stream" (("type" . "emacs-lisp"))
263 "attachment" (("filename" . file)))
266 "application" "octet-stream" (("type" . "common-lisp"))
268 "attachment" (("filename" . file)))
271 "application" "octet-stream" (("type" . "perl"))
273 "attachment" (("filename" . file)))
275 ;; Text or translated text
280 "inline" (("filename" . file)))
282 ;; .rc : procmail modules pm-xxxx.rc
283 ;; *rc : other resource files
285 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
288 "attachment" (("filename" . file)))
295 ("\\.diff$\\|\\.patch$"
296 "application" "octet-stream" (("type" . "patch"))
298 "attachment" (("filename" . file)))
301 "text" "plain" nil nil nil nil)
304 ;; Octect binary text
307 "application" "msword" nil
309 "attachment" (("filename" . file)))
310 ("\\.xls$" ; MS Excel
311 "application" "vnd.ms-excel" 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)))
330 ("\\.jpg$\\|\\.jpeg$"
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)))))
451 ;;; @@ about charset, encoding and transfer-level
454 (defvar mime-charset-type-list
456 (iso-8859-1 8 "quoted-printable")
457 (iso-8859-2 8 "quoted-printable")
458 (iso-8859-3 8 "quoted-printable")
459 (iso-8859-4 8 "quoted-printable")
460 (iso-8859-5 8 "quoted-printable")
461 (koi8-r 8 "quoted-printable")
462 (iso-8859-7 8 "quoted-printable")
463 (iso-8859-8 8 "quoted-printable")
464 (iso-8859-9 8 "quoted-printable")
465 (iso-8859-14 8 "quoted-printable")
466 (iso-8859-15 8 "quoted-printable")
467 (iso-2022-jp 7 "base64")
468 (iso-2022-jp-3 7 "base64")
469 (iso-2022-kr 7 "base64")
475 (shift_jis 8 "base64")
477 (iso-2022-jp-2 7 "base64")
478 (iso-2022-int-1 7 "base64")))
480 (defvar mime-transfer-level 7
481 "*A number of network transfer level. It should be bigger than 7.")
482 (make-variable-buffer-local 'mime-transfer-level)
484 (defsubst mime-encoding-name (transfer-level &optional not-omit)
485 (cond ((> transfer-level 8) "binary")
486 ((= transfer-level 8) "8bit")
489 (defvar mime-transfer-level-string
490 (mime-encoding-name mime-transfer-level 'not-omit)
491 "A string formatted version of mime-transfer-level")
492 (make-variable-buffer-local 'mime-transfer-level-string)
494 ;;; @@ about content transfer encoding
496 (defvar mime-content-transfer-encoding-priority-list
497 '(nil "8bit" "binary"))
499 ;;; @@ about message inserting
502 (defvar mime-edit-yank-ignored-field-list
503 '("Received" "Approved" "Path" "Replied" "Status"
504 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
505 "Delete these fields from original message when it is inserted
506 as message/rfc822 part.
507 Each elements are regexp of field-name.")
509 (defvar mime-edit-yank-ignored-field-regexp
511 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
514 (defvar mime-edit-message-inserter-alist nil)
515 (defvar mime-edit-mail-inserter-alist nil)
518 ;;; @@ about message splitting
521 (defcustom mime-edit-split-message t
522 "*Split large message if it is non-nil."
526 (defcustom mime-edit-message-default-max-lines 1000
527 "*Default maximum lines of a message."
531 (defcustom mime-edit-message-max-lines-alist
532 '((news-reply-mode . 500))
533 "Alist of major-mode vs maximum lines of a message.
534 If it is not specified for a major-mode,
535 `mime-edit-message-default-max-lines' is used."
539 (defconst mime-edit-split-ignored-field-regexp
540 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
542 (defcustom mime-edit-split-blind-field-regexp
543 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
544 "*Regular expression to match field-name to be ignored when split sending."
548 (defvar mime-edit-split-message-sender-alist nil)
550 (defvar mime-edit-news-reply-mode-server-running nil)
556 (defconst mime-edit-single-part-tag-regexp
557 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
558 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
560 (defconst mime-edit-quoted-single-part-tag-regexp
561 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
563 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
565 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
567 (defconst mime-edit-beginning-tag-regexp
568 (regexp-or mime-edit-single-part-tag-regexp
569 mime-edit-multipart-beginning-regexp))
571 (defconst mime-edit-end-tag-regexp
572 (regexp-or mime-edit-single-part-tag-regexp
573 mime-edit-multipart-end-regexp))
575 (defconst mime-edit-tag-regexp
576 (regexp-or mime-edit-single-part-tag-regexp
577 mime-edit-multipart-beginning-regexp
578 mime-edit-multipart-end-regexp))
580 (defvar mime-tag-format "--[[%s]]"
581 "*Control-string making a MIME tag.")
583 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
584 "*Control-string making a MIME tag with encoding.")
587 ;;; @@ multipart boundary
590 (defvar mime-multipart-boundary "Multipart"
591 "*Boundary of a multipart message.")
594 ;;; @@ optional header fields
597 (defvar mime-edit-insert-user-agent-field t
598 "*If non-nil, insert User-Agent header field.")
600 (defvar mime-edit-user-agent-value
601 (concat (mime-product-name mime-user-interface-product)
603 (mapconcat #'number-to-string
604 (mime-product-version mime-user-interface-product) ".")
606 (mime-product-code-name mime-user-interface-product)
608 (mime-product-name mime-library-product)
610 (mapconcat #'number-to-string
611 (mime-product-version mime-library-product) ".")
613 (mime-product-code-name mime-library-product)
615 (if (fboundp 'apel-version)
616 (concat (apel-version) " "))
617 (if (featurep 'xemacs)
618 (concat (cond ((and (featurep 'chise)
619 (boundp 'xemacs-chise-version))
620 (concat "CHISE-MULE/" xemacs-chise-version))
621 ((featurep 'utf-2000)
622 (concat "UTF-2000-MULE/" utf-2000-version))
623 ((featurep 'mule) "MULE"))
625 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
628 (substring emacs-version 0 (match-end 0))
629 (cond ((and (boundp 'xemacs-betaname)
631 ;; It does not exist in XEmacs
632 ;; versions prior to 20.3.
633 (concat " " xemacs-betaname))
634 ((and (boundp 'emacs-patch-level)
636 ;; It does not exist in FSF Emacs or in
637 ;; XEmacs versions earlier than 21.1.1.
638 (format " (patch %d)" emacs-patch-level))
640 " (" xemacs-codename ")"
641 ;; `xemacs-extra-name' has appeared in the
642 ;; development version of XEmacs 21.5-b8.
643 (if (and (boundp 'xemacs-extra-name)
644 (symbol-value 'xemacs-extra-name))
645 (concat " " (symbol-value 'xemacs-extra-name))
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 ")")
683 "MIME version number.")
685 (defconst mime-edit-mime-version-field-for-message/partial
686 (concat "MIME-Version:"
687 (mime-encode-field-body
688 (concat " 1.0 (split by " mime-edit-version ")\n")
690 "MIME version field for message/partial.")
693 ;;; @ keymap and menu
696 (defvar mime-edit-mode-flag nil)
697 (make-variable-buffer-local 'mime-edit-mode-flag)
699 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
700 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
701 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
702 "Keymap for MIME-Edit mode commands to insert entity or set status.")
704 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
705 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
706 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
707 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
708 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
709 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
710 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
711 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
712 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
713 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
715 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
716 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
717 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
718 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
719 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
720 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
721 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
722 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
723 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
724 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
726 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
727 "Keymap prefix for MIME-Edit mode commands about enclosure.")
728 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
729 "Keymap for MIME-Edit mode commands about enclosure.")
731 (define-key mime-edit-mode-enclosure-map
732 "\C-a" 'mime-edit-enclose-alternative-region)
733 (define-key mime-edit-mode-enclosure-map
734 "\C-p" 'mime-edit-enclose-parallel-region)
735 (define-key mime-edit-mode-enclosure-map
736 "\C-m" 'mime-edit-enclose-mixed-region)
737 (define-key mime-edit-mode-enclosure-map
738 "\C-d" 'mime-edit-enclose-digest-region)
739 (define-key mime-edit-mode-enclosure-map
740 "\C-s" 'mime-edit-enclose-pgp-signed-region)
741 (define-key mime-edit-mode-enclosure-map
742 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
743 (define-key mime-edit-mode-enclosure-map
744 "\C-q" 'mime-edit-enclose-quote-region)
746 (defvar mime-edit-mode-map (make-sparse-keymap)
747 "Keymap for MIME-Edit mode commands.")
748 (define-key mime-edit-mode-map
749 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
750 (define-key mime-edit-mode-map
751 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
753 (defconst mime-edit-menu-title "MIME-Edit")
755 (defconst mime-edit-menu-list
756 '((mime-help "Describe MIME editor mode" mime-edit-help)
757 (file "Insert File" mime-edit-insert-file)
758 (external "Insert External" mime-edit-insert-external)
759 (voice "Insert Voice" mime-edit-insert-voice)
760 (message "Insert Message" mime-edit-insert-message)
761 (mail "Insert Mail" mime-edit-insert-mail)
762 (signature "Insert Signature" mime-edit-insert-signature)
763 (text "Insert Text" mime-edit-insert-text)
764 (tag "Insert Tag" mime-edit-insert-tag)
765 (alternative "Enclose as alternative"
766 mime-edit-enclose-alternative-region)
767 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
768 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
769 (digest "Enclose as digest" mime-edit-enclose-digest-region)
770 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
771 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
772 (quote "Verbatim region" mime-edit-enclose-quote-region)
773 (key "Insert Public Key" mime-edit-insert-key)
774 (split "Set splitting" mime-edit-set-split)
775 (sign "PGP sign" mime-edit-set-sign)
776 (encrypt "PGP encrypt" mime-edit-set-encrypt)
777 (preview "Preview Message" mime-edit-preview-message)
778 (level "Toggle transfer-level" mime-edit-toggle-transfer-level))
779 "MIME-edit menubar entry.")
781 (cond ((featurep 'xemacs)
782 ;; modified by Pekka Marjola <pema@iki.fi>
783 ;; 1995/9/5 (c.f. [tm-en:69])
784 (defun mime-edit-define-menu-for-xemacs ()
785 "Define menu for XEmacs."
786 (cond ((featurep 'menubar)
787 (make-local-variable 'current-menubar)
788 (set-buffer-menubar current-menubar)
791 (cons mime-edit-menu-title
794 (vector (nth 1 item)(nth 2 item)
795 mime-edit-mode-flag)))
796 mime-edit-menu-list))))))
798 ;; modified by Steven L. Baur <steve@miranova.com>
799 ;; 1995/12/6 (c.f. [tm-en:209])
800 (or (boundp 'mime-edit-popup-menu-for-xemacs)
801 (setq mime-edit-popup-menu-for-xemacs
802 (append '("MIME Commands" "---")
803 (mapcar (function (lambda (item)
807 mime-edit-menu-list)))))
808 ((>= emacs-major-version 19)
809 (define-key mime-edit-mode-map [menu-bar mime-edit]
810 (cons mime-edit-menu-title
811 (make-sparse-keymap mime-edit-menu-title)))
814 (define-key mime-edit-mode-map
815 (vector 'menu-bar 'mime-edit (car item))
816 (cons (nth 1 item)(nth 2 item)))))
817 (reverse mime-edit-menu-list))))
823 (defvar mime-edit-touched-flag nil)
826 (defun mime-edit-mode ()
827 "MIME minor mode for editing the tagged MIME message.
829 In this mode, basically, the message is composed in the tagged MIME
830 format. The message tag looks like:
832 --[[text/plain; charset=ISO-2022-JP][7bit]]
834 The tag specifies the MIME content type, subtype, optional parameters
835 and transfer encoding of the message following the tag. Messages
836 without any tag are treated as `text/plain' by default. Charset and
837 transfer encoding are automatically defined unless explicitly
838 specified. Binary messages such as audio and image are usually
839 hidden. The messages in the tagged MIME format are automatically
840 translated into a MIME compliant message when exiting this mode.
842 Available charsets depend on Emacs version being used. The following
843 lists the available charsets of each emacs.
845 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
846 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
847 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
848 CN-BIG5 and ISO-2022-INT-1 are available.
850 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
851 be used to represent multilingual text in intermixed manner. Any
852 languages that has no registered charset are represented as either
853 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
855 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
856 without mule, please set variable `default-mime-charset'. This
857 variable must be symbol of which name is a MIME charset.
859 If you want to add more charsets in mule, please set variable
860 `charsets-mime-charset-alist'. This variable must be alist of which
861 key is list of charset and value is symbol of MIME charset. If name
862 of coding-system is different as MIME charset, please set variable
863 `mime-charset-coding-system-alist'. This variable must be alist of
864 which key is MIME charset and value is coding-system.
866 Following commands are available in addition to major mode commands:
869 \\[mime-edit-insert-text] insert a text message.
870 \\[mime-edit-insert-file] insert a (binary) file.
871 \\[mime-edit-insert-external] insert a reference to external body.
872 \\[mime-edit-insert-voice] insert a voice message.
873 \\[mime-edit-insert-message] insert a mail or news message.
874 \\[mime-edit-insert-mail] insert a mail message.
875 \\[mime-edit-insert-signature] insert a signature file at end.
876 \\[mime-edit-insert-key] insert PGP public key.
877 \\[mime-edit-insert-tag] insert a new MIME tag.
879 \[make enclosure (maybe multipart)\]
880 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
881 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
882 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
883 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
884 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
885 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
886 \\[mime-edit-enclose-quote-region] enclose as verbose mode
887 (to avoid to expand tags)
890 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
891 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
892 \\[mime-edit-set-split] set message splitting mode.
893 \\[mime-edit-set-sign] set PGP-sign mode.
894 \\[mime-edit-set-encrypt] set PGP-encryption mode.
895 \\[mime-edit-preview-message] preview editing MIME message.
896 \\[mime-edit-exit] exit and translate into a MIME
898 \\[mime-edit-help] show this help.
899 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
902 Additional commands are available in some major modes:
903 C-c C-c exit, translate and run the original command.
904 C-c C-s exit, translate and run the original command.
906 The following is a message example written in the tagged MIME format.
907 TABs at the beginning of the line are not a part of the message:
909 This is a conventional plain text. It should be translated
912 This is also a plain text. But, it is explicitly specified as
914 --[[text/plain; charset=ISO-8859-1]]
915 This is also a plain text. But charset is specified as
918 ¡Hola! Buenos días. ¿Cómo está usted?
920 This is a <bold>enriched text</bold>.
921 --[[image/gif][base64]]...image encoded in base64 here...
922 --[[audio/basic][base64]]...audio encoded in base64 here...
924 User customizable variables (not documented all of them):
926 Specifies a key prefix for MIME minor mode commands.
928 mime-ignore-preceding-spaces
929 Preceding white spaces in a message body are ignored if non-nil.
931 mime-ignore-trailing-spaces
932 Trailing white spaces in a message body are ignored if non-nil.
935 Hide a non-textual body message encoded in base64 after insertion
939 A number of network transfer level. It should be bigger than 7.
940 If you are in 8bit-through environment, please set 8.
942 mime-edit-voice-recorder
943 Specifies a function to record a voice message and encode it.
944 The function `mime-edit-voice-recorder-for-sun' is for Sun
948 Turning on MIME mode calls the value of mime-edit-mode-hook, if
951 mime-edit-translate-hook
952 The value of mime-edit-translate-hook is called just before translating
953 the tagged MIME format into a MIME compliant message if it is
954 non-nil. If the hook call the function mime-edit-insert-signature,
955 the signature file will be inserted automatically.
958 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
961 (if mime-edit-mode-flag
963 (if mime-edit-touched-flag
965 (make-local-variable 'mime-edit-touched-flag)
966 (setq mime-edit-touched-flag t)
967 (turn-on-mime-edit))))
970 (cond ((featurep 'xemacs)
971 (add-minor-mode 'mime-edit-mode-flag
972 '((" MIME-Edit " mime-transfer-level-string))
977 (set-alist 'minor-mode-alist
979 '((" MIME-Edit " mime-transfer-level-string)))
980 (set-alist 'minor-mode-map-alist
982 mime-edit-mode-map)))
986 (defun turn-on-mime-edit ()
987 "Unconditionally turn on MIME-Edit mode."
989 (if mime-edit-mode-flag
990 (error "You are already editing a MIME message.")
991 (setq mime-edit-mode-flag t)
993 ;; Set transfer level into mode line
995 (setq mime-transfer-level-string
996 (mime-encoding-name mime-transfer-level 'not-omit))
997 (force-mode-line-update)
999 ;; Define menu for XEmacs.
1000 (if (featurep 'xemacs)
1001 (mime-edit-define-menu-for-xemacs))
1003 ;; I don't care about saving these.
1004 (setq paragraph-start
1005 (regexp-or mime-edit-single-part-tag-regexp
1007 (setq paragraph-separate
1008 (regexp-or mime-edit-single-part-tag-regexp
1009 paragraph-separate))
1010 (run-hooks 'mime-edit-mode-hook)
1013 (substitute-command-keys
1014 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
1017 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1020 (defun mime-edit-exit (&optional nomime no-error)
1021 "Translate the tagged MIME message into a MIME compliant message.
1022 With no argument encode a message in the buffer into MIME, otherwise
1023 just return to previous mode."
1025 (if (not mime-edit-mode-flag)
1027 (error "You aren't editing a MIME message."))
1030 (run-hooks 'mime-edit-translate-hook)
1031 (mime-edit-translate-buffer)))
1032 ;; Restore previous state.
1033 (setq mime-edit-mode-flag nil)
1034 (if (and (featurep 'xemacs)
1035 (featurep 'menubar))
1036 (delete-menu-item (list mime-edit-menu-title)))
1037 (set-buffer-modified-p (buffer-modified-p))
1038 (run-hooks 'mime-edit-exit-hook)
1039 (message "Exit MIME editor mode.")))
1041 (defun mime-edit-maybe-translate ()
1043 (mime-edit-exit nil t)
1044 (call-interactively 'mime-edit-maybe-split-and-send))
1046 (defun mime-edit-help ()
1047 "Show help message about MIME mode."
1049 (with-output-to-temp-buffer "*Help*"
1050 (princ "MIME editor mode:\n")
1051 (princ (documentation 'mime-edit-mode))
1052 (print-help-return-message)))
1054 (defun mime-edit-insert-text (&optional subtype)
1055 "Insert a text message.
1056 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1057 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1059 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1061 (if (looking-at mime-edit-single-part-tag-regexp)
1063 ;; Make a space between the following message.
1066 (if (and (member (cadr ret) '("enriched"))
1067 (fboundp 'enriched-mode))
1069 (if (boundp 'enriched-mode)
1070 (enriched-mode -1))))))
1072 (defun mime-edit-insert-file (file &optional verbose)
1073 "Insert a message from a file."
1074 (interactive "fInsert file as MIME message: \nP")
1075 (let* ((guess (mime-find-file-type file))
1076 (type (nth 0 guess))
1077 (subtype (nth 1 guess))
1078 (parameters (nth 2 guess))
1079 (encoding (nth 3 guess))
1080 (disposition-type (nth 4 guess))
1081 (disposition-params (nth 5 guess)))
1083 (setq type (mime-prompt-for-type type)
1084 subtype (mime-prompt-for-subtype type subtype)))
1085 (if (or (interactive-p) verbose)
1086 (setq encoding (mime-prompt-for-encoding encoding)))
1087 (if (or (consp parameters) (stringp disposition-type))
1088 (let ((rest parameters) cell attribute value)
1089 (setq parameters "")
1091 (setq cell (car rest))
1092 (setq attribute (car cell))
1093 (setq value (cdr cell))
1094 (if (eq value 'file)
1095 (setq value (std11-wrap-as-quoted-string
1096 (file-name-nondirectory file))))
1097 (setq parameters (concat parameters "; " attribute "=" value))
1098 (setq rest (cdr rest)))
1099 (if disposition-type
1102 (concat parameters "\n"
1103 "Content-Disposition: " disposition-type))
1104 (setq rest disposition-params)
1106 (setq cell (car rest))
1107 (setq attribute (car cell))
1108 (setq value (cdr cell))
1109 (if (eq value 'file)
1110 (setq value (std11-wrap-as-quoted-string
1111 (file-name-nondirectory file))))
1113 (concat parameters "; " attribute "=" value))
1114 (setq rest (cdr rest)))))))
1115 (mime-edit-insert-tag type subtype parameters)
1116 (mime-edit-insert-binary-file file encoding)))
1118 (defun mime-edit-insert-external ()
1119 "Insert a reference to external body."
1121 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1123 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1125 (let* ((pritype (mime-prompt-for-type))
1126 (subtype (mime-prompt-for-subtype pritype))
1127 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1130 (insert "Content-Type: "
1131 pritype "/" subtype (or parameters "") "\n")))
1132 (if (and (not (eobp))
1133 (not (looking-at mime-edit-single-part-tag-regexp)))
1134 (insert (mime-make-text-tag) "\n")))
1136 (defun mime-edit-insert-voice ()
1137 "Insert a voice message."
1141 "What transfer encoding: "
1142 (mime-encoding-alist) nil t nil)))
1143 (mime-edit-insert-tag "audio" "basic" nil)
1144 (mime-edit-define-encoding encoding)
1146 (narrow-to-region (point)(point))
1148 (funcall mime-edit-voice-recorder encoding)
1151 (add-text-properties
1152 (point-min)(point-max) '(invisible t mime-edit-invisible t))
1153 (goto-char (point-max)))))))
1155 (defun mime-edit-insert-signature (&optional arg)
1156 "Insert a signature file."
1158 (let ((signature-insert-hook
1161 (let ((items (mime-find-file-type signature-file-name)))
1162 (apply (function mime-edit-insert-tag)
1163 (car items) (cadr items) (list (caddr items))))))))
1164 (insert-signature arg)))
1167 ;; Insert a new tag around a point.
1169 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1170 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1171 If nothing is inserted, return nil."
1174 (mime-edit-goto-tag)
1175 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1176 (< (match-beginning 0) p)
1177 (< p (match-end 0)))
1178 (goto-char (match-beginning 0))
1185 (mime-prompt-for-type)))
1188 (mime-prompt-for-subtype pritype)))
1191 (mime-prompt-for-parameters pritype subtype delimiter)))
1192 ;; Make a new MIME tag.
1193 (setq newtag (mime-make-tag pritype subtype parameters))
1194 ;; Find an current MIME tag.
1197 (if (mime-edit-goto-tag)
1198 (buffer-substring (match-beginning 0) (match-end 0))
1199 ;; Assume content type is 'text/plan'.
1200 (mime-make-tag "text" "plain"))))
1201 ;; We are only interested in TEXT.
1203 (not (mime-test-content-type
1204 (mime-edit-get-contype oldtag) "text")))
1207 (if (or (not oldtag) ;Not text
1208 (or mime-ignore-same-text-tag
1209 (not (string-equal oldtag newtag))))
1211 ;; Mark the beginning of the tag for convenience.
1212 (push-mark (point) 'nomsg)
1213 (insert newtag "\n")
1214 (list pritype subtype parameters) ;New tag is created.
1216 ;; Restore previous point.
1218 nil ;Nothing is created.
1221 (defun mime-edit-insert-binary-file (file &optional encoding)
1222 "Insert binary FILE at point.
1223 Optional argument ENCODING specifies an encoding method such as base64."
1224 (let* ((tagend (1- (point))) ;End of the tag
1225 (hide-p (and mime-auto-hide-body
1228 (let ((en (downcase encoding)))
1229 (or (string-equal en "7bit")
1230 (string-equal en "8bit")
1231 (string-equal en "binary")))))))
1233 (narrow-to-region (point)(point))
1234 (mime-insert-encoded-file file encoding)
1236 (add-text-properties
1237 (point-min)(point-max) '(invisible t mime-edit-invisible t)))
1238 (goto-char (point-max)))
1240 (looking-at mime-edit-tag-regexp)
1241 (= (point)(point-max))
1242 (mime-edit-insert-tag "text" "plain"))
1243 ;; Define encoding even if it is 7bit.
1244 (if (stringp encoding)
1246 (goto-char tagend) ; Make sure which line the tag is on.
1247 (mime-edit-define-encoding encoding)))))
1250 ;; Commands work on a current message flagment.
1252 (defun mime-edit-goto-tag ()
1253 "Search for the beginning of the tagged MIME message."
1254 (let ((current (point)))
1255 (if (looking-at mime-edit-tag-regexp)
1257 ;; At first, go to the end.
1258 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1259 (goto-char (1- (match-beginning 0))) ;For multiline tag
1262 (goto-char (point-max))))
1263 ;; Then search for the beginning.
1264 (re-search-backward mime-edit-end-tag-regexp nil t)
1265 (or (looking-at mime-edit-beginning-tag-regexp)
1266 ;; Restore previous point.
1271 (defun mime-edit-content-beginning ()
1272 "Return the point of the beginning of content."
1274 (let ((beg (save-excursion
1275 (beginning-of-line) (point))))
1276 (if (mime-edit-goto-tag)
1277 (let ((top (point)))
1278 (goto-char (match-end 0))
1279 (if (and (= beg top)
1280 (= (following-char) ?\^M))
1284 ;; Default text/plain tag.
1285 (goto-char (point-min))
1287 (concat "\n" (regexp-quote mail-header-separator)
1288 (if mime-ignore-preceding-spaces
1289 "[ \t\n]*\n" "\n")) nil 'move)
1292 (defun mime-edit-content-end ()
1293 "Return the point of the end of content."
1295 (if (mime-edit-goto-tag)
1297 (goto-char (1+ (match-end 0)))
1298 (if (get-text-property (point) 'mime-edit-invisible)
1299 (or (next-single-property-change (point) 'mime-edit-invisible)
1301 ;; Move to the end of this text.
1302 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1303 ;; Don't forget a multiline tag.
1304 (goto-char (match-beginning 0)))
1306 ;; Assume the message begins with text/plain.
1307 (goto-char (mime-edit-content-beginning))
1308 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1309 ;; Don't forget a multiline tag.
1310 (goto-char (match-beginning 0)))
1313 (defun mime-edit-define-charset (charset)
1314 "Set charset of current tag to CHARSET."
1316 (if (mime-edit-goto-tag)
1317 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1318 (delete-region (match-beginning 0) (match-end 0))
1321 (mime-edit-set-parameter
1322 (mime-edit-get-contype tag)
1324 (let ((comment (get charset 'mime-charset-comment)))
1326 (concat (upcase (symbol-name charset)) " (" comment ")")
1327 (upcase (symbol-name charset)))))
1328 (mime-edit-get-encoding tag)))))))
1330 (defun mime-edit-define-encoding (encoding)
1331 "Set encoding of current tag to ENCODING."
1333 (if (mime-edit-goto-tag)
1334 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1335 (delete-region (match-beginning 0) (match-end 0))
1336 (insert (mime-create-tag (mime-edit-get-contype tag) encoding))))))
1338 (defun mime-edit-choose-charset ()
1339 "Choose charset of a text following current point."
1340 (detect-mime-charset-region (point) (mime-edit-content-end)))
1342 (defun mime-make-text-tag (&optional subtype)
1343 "Make a tag for a text after current point.
1344 Subtype of text type can be specified by an optional argument SUBTYPE.
1345 Otherwise, it is obtained from mime-content-types."
1346 (let* ((pritype "text")
1347 (subtype (or subtype
1348 (car (car (cdr (assoc pritype mime-content-types)))))))
1349 ;; Charset should be defined later.
1350 (mime-make-tag pritype subtype)))
1353 ;; Tag handling functions
1355 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1356 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1357 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1361 (defun mime-create-tag (contype &optional encoding)
1362 "Make a tag with CONTENT-TYPE and optional ENCODING."
1363 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1366 (defun mime-edit-get-contype (tag)
1367 "Return Content-Type (including parameters) of TAG."
1369 (or (string-match mime-edit-single-part-tag-regexp tag)
1370 (string-match mime-edit-multipart-beginning-regexp tag)
1371 (string-match mime-edit-multipart-end-regexp tag))
1372 (substring tag (match-beginning 1) (match-end 1))))
1374 (defun mime-edit-get-encoding (tag)
1375 "Return encoding of TAG."
1377 (string-match mime-edit-single-part-tag-regexp tag)
1379 (not (= (match-beginning 3) (match-end 3)))
1380 (substring tag (match-beginning 3) (match-end 3))))
1382 (defun mime-get-parameter (contype parameter)
1383 "For given CONTYPE return value for PARAMETER.
1384 Nil if no such parameter."
1388 (regexp-quote parameter)
1389 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1391 (substring contype (match-beginning 1) (match-end 1))
1392 nil ;No such parameter
1395 (defun mime-edit-set-parameter (contype parameter value)
1396 "For given CONTYPE set PARAMETER to VALUE."
1397 (let (ctype opt-fields)
1398 (if (string-match "\n[^ \t\n\r]+:" contype)
1399 (setq ctype (substring contype 0 (match-beginning 0))
1400 opt-fields (substring contype (match-beginning 0)))
1401 (setq ctype contype))
1405 (regexp-quote parameter)
1406 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1409 (concat (substring ctype 0 (match-beginning 1))
1411 (substring ctype (match-end 1))
1413 (concat ctype "; " parameter "=" value opt-fields)
1416 (defun mime-strip-parameters (contype)
1417 "Return primary content-type and subtype without parameters for CONTYPE."
1418 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1419 (substring contype (match-beginning 1) (match-end 1)) nil))
1421 (defun mime-test-content-type (contype type &optional subtype)
1422 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1423 (and (stringp contype)
1426 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1427 (downcase contype))))
1432 (defun mime-find-file-type (file)
1433 "Guess Content-Type, subtype, and parameters from FILE."
1435 (guesses mime-file-types))
1436 (while (and (not guess) guesses)
1437 (if (string-match (car (car guesses)) file)
1438 (setq guess (cdr (car guesses))))
1439 (setq guesses (cdr guesses)))
1442 (defun mime-prompt-for-type (&optional default)
1443 "Ask for Content-type."
1445 ;; Repeat until primary content type is specified.
1446 (while (string-equal type "")
1448 (completing-read "What content type: "
1451 'require-match ;Type must be specified.
1453 (if (string-equal type "")
1455 (message "Content type is required.")
1460 (defun mime-prompt-for-subtype (type &optional default)
1461 "Ask for subtype of media-type TYPE."
1462 (let ((subtypes (cdr (assoc type mime-content-types))))
1464 (assoc default subtypes))
1465 (setq default (car (car subtypes)))))
1470 "What content subtype: (default " default ") ")
1471 "What content subtype: ")
1472 (cdr (assoc type mime-content-types))
1474 'require-match ;Subtype must be specified.
1476 (if (string-equal answer "") default answer)))
1478 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1479 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1480 Optional DELIMITER specifies parameter delimiter (';' by default)."
1481 (let* ((delimiter (or delimiter "; "))
1486 (mime-prompt-for-parameters-1
1488 (cdr (assoc pritype mime-content-types))))))
1490 (if (and (stringp parameters)
1491 (not (string-equal parameters "")))
1492 (concat delimiter parameters)
1493 "" ;"" if no parameters
1496 (defun mime-prompt-for-parameters-1 (optlist)
1497 (apply (function append)
1498 (mapcar (function mime-prompt-for-parameter) optlist)))
1500 (defun mime-prompt-for-parameter (parameter)
1502 Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
1503 (let* ((prompt (car parameter))
1504 (choices (mapcar (function
1506 (if (consp e) e (list e))))
1508 (default (car (car choices)))
1514 (concat "What " prompt
1516 (if (string-equal default "") "\"\"" default)
1518 choices nil nil ""))
1519 ;; If nothing is selected, use default.
1520 (if (string-equal answer "")
1521 (setq answer default)))
1523 (read-string (concat "What " prompt ": "))))
1524 (cons (if (and answer
1525 (not (string-equal answer "")))
1527 ;; Note: control characters ignored!
1528 (if (string-match mime-tspecials-regexp answer)
1529 (concat "\"" answer "\"") answer)))
1530 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))))
1532 (defun mime-prompt-for-encoding (default)
1533 "Ask for Content-Transfer-Encoding."
1538 "What transfer encoding: "
1539 (mime-encoding-alist) nil t default))
1544 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1547 (defvar mime-edit-translate-buffer-hook
1548 '(mime-edit-pgp-enclose-buffer
1549 mime-edit-translate-body
1550 mime-edit-translate-header))
1552 (defun mime-edit-translate-header ()
1553 "Encode the message header into network representation."
1554 (mime-encode-header-in-buffer 'code-conversion)
1555 (run-hooks 'mime-edit-translate-header-hook))
1557 (defun mime-edit-translate-buffer ()
1558 "Encode the tagged MIME message in current buffer in MIME compliant message."
1561 (if (catch 'mime-edit-error
1563 (run-hooks 'mime-edit-translate-buffer-hook)))
1566 (error "Translation error!"))))
1568 (defun mime-edit-find-inmost ()
1569 (goto-char (point-min))
1570 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1571 (let ((bb (match-beginning 0))
1573 (type (buffer-substring (match-beginning 1)(match-end 1)))
1575 (setq end-exp (format "--}-<<%s>>\n" type))
1577 (if (re-search-forward end-exp nil t)
1578 (setq eb (match-beginning 0))
1579 (setq eb (point-max)))
1580 (narrow-to-region be eb)
1582 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1584 (narrow-to-region (match-beginning 0)(point-max))
1585 (mime-edit-find-inmost))
1587 (list type bb be eb)))))
1589 (defun mime-edit-process-multipart-1 (boundary)
1590 (let ((ret (mime-edit-find-inmost)))
1592 (let ((type (car ret))
1593 (bb (nth 1 ret))(be (nth 2 ret))
1595 (narrow-to-region bb eb)
1596 (delete-region bb be)
1597 (setq bb (point-min))
1598 (setq eb (point-max))
1601 (if (looking-at mime-edit-multipart-end-regexp)
1602 (let ((beg (match-beginning 0))
1603 (end (match-end 0)))
1604 (delete-region beg end)
1605 (or (looking-at mime-edit-beginning-tag-regexp)
1607 (insert (concat (mime-make-text-tag) "\n")))))
1608 (cond ((string-equal type "quote")
1609 (mime-edit-enquote-region bb eb))
1610 ((string-equal type "pgp-signed")
1611 (mime-edit-sign-pgp-mime bb eb boundary))
1612 ((string-equal type "pgp-encrypted")
1613 (mime-edit-encrypt-pgp-mime bb eb boundary))
1614 ((string-equal type "kazu-signed")
1615 (mime-edit-sign-pgp-kazu bb eb boundary))
1616 ((string-equal type "kazu-encrypted")
1617 (mime-edit-encrypt-pgp-kazu bb eb boundary))
1618 ((string-equal type "smime-signed")
1619 (mime-edit-sign-smime bb eb boundary))
1620 ((string-equal type "smime-encrypted")
1621 (mime-edit-encrypt-smime bb eb boundary))
1624 (nth 2 (mime-edit-translate-region bb eb
1628 (format "--[[multipart/%s;
1629 boundary=\"%s\"][7bit]]\n"
1633 (defun mime-edit-enquote-region (beg end)
1636 (narrow-to-region beg end)
1638 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1639 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1640 (replace-match (concat "- " (substring tag 1))))))))
1642 (defun mime-edit-dequote-region (beg end)
1645 (narrow-to-region beg end)
1647 (while (re-search-forward
1648 mime-edit-quoted-single-part-tag-regexp nil t)
1649 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1650 (replace-match (concat "-" (substring tag 2))))))))
1652 (defvar mime-edit-pgp-user-id nil)
1654 (defun mime-edit-sign-pgp-mime (beg end boundary)
1657 (let* ((from (std11-field-body "From" mail-header-separator))
1659 (narrow-to-region beg end)
1660 (mime-edit-translate-region beg end boundary)))
1662 (encoding (nth 1 ret))
1663 (pgp-boundary (concat "pgp-sign-" boundary))
1666 (insert (format "Content-Type: %s\n" ctype))
1668 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1670 (or (let ((pgg-default-user-id
1671 (or mime-edit-pgp-user-id
1673 (nth 1 (std11-extract-address-components from))
1674 pgg-default-user-id))))
1675 (pgg-sign-region (point-min)(point-max)))
1676 (throw 'mime-edit-error 'pgp-error))
1678 (cdr (assq 'hash-algorithm
1679 (cdar (with-current-buffer pgg-output-buffer
1680 (pgg-parse-armor-region
1681 (point-min)(point-max))))))
1684 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1687 (insert (format "--[[multipart/signed;
1689 protocol=\"application/pgp-signature\"][7bit]]
1691 " pgp-boundary micalg pgp-boundary))
1692 (goto-char (point-max))
1693 (insert (format "\n--%s
1694 Content-Type: application/pgp-signature
1695 Content-Transfer-Encoding: 7bit
1698 (insert-buffer-substring pgg-output-buffer)
1699 (goto-char (point-max))
1700 (insert (format "\n--%s--\n" pgp-boundary))))))
1702 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1704 (defun mime-edit-make-encrypt-recipient-header ()
1705 (let* ((names mime-edit-encrypt-recipient-fields-list)
1707 (std11-field-bodies (cons "From" names)
1708 nil mail-header-separator))
1711 (setq values (cdr values))))
1712 (header (and (stringp from)
1713 (if (string-equal from "")
1715 (format "From: %s\n" from))))
1717 (while (and names values)
1718 (let ((name (car names))
1719 (value (car values)))
1720 (and (stringp value)
1721 (or (string-equal value "")
1723 (setq header (concat header name ": " value "\n")
1724 recipients (if recipients
1725 (concat recipients " ," value)
1727 (setq names (cdr names)
1728 values (cdr values)))
1729 (vector from recipients header)))
1731 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1734 (let (from recipients header)
1735 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1736 (setq from (aref ret 0)
1737 recipients (aref ret 1)
1738 header (aref ret 2)))
1739 (narrow-to-region beg end)
1741 (mime-edit-translate-region beg end boundary))
1743 (encoding (nth 1 ret))
1744 (pgp-boundary (concat "pgp-" boundary)))
1747 (insert (format "Content-Type: %s\n" ctype))
1749 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1751 (mime-encode-header-in-buffer)
1752 (or (let ((pgg-default-user-id
1753 (or mime-edit-pgp-user-id
1755 (nth 1 (std11-extract-address-components from))
1756 pgg-default-user-id))))
1758 (point-min) (point-max)
1759 (mapcar (lambda (recipient)
1760 (nth 1 (std11-extract-address-components
1762 (split-string recipients
1763 "\\([ \t\n]*,[ \t\n]*\\)+"))))
1764 (throw 'mime-edit-error 'pgp-error))
1765 (delete-region (point-min)(point-max))
1767 (insert (format "--[[multipart/encrypted;
1769 protocol=\"application/pgp-encrypted\"][7bit]]
1771 Content-Type: application/pgp-encrypted
1774 Content-Type: application/octet-stream
1775 Content-Transfer-Encoding: 7bit
1777 " pgp-boundary pgp-boundary pgp-boundary))
1778 (insert-buffer-substring pgg-output-buffer)
1779 (goto-char (point-max))
1780 (insert (format "\n--%s--\n" pgp-boundary)))))))
1782 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1785 (narrow-to-region beg end)
1787 (mime-edit-translate-region beg end boundary))
1789 (encoding (nth 1 ret)))
1791 (insert (format "Content-Type: %s\n" ctype))
1793 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1795 (or (pgg-sign-region beg (point-max) 'clearsign)
1796 (throw 'mime-edit-error 'pgp-error))
1799 "--[[application/pgp; format=mime][7bit]]\n")
1802 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1804 (let (recipients header)
1805 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1806 (setq recipients (aref ret 1)
1807 header (aref ret 2)))
1809 (narrow-to-region beg end)
1811 (mime-edit-translate-region beg end boundary))
1813 (encoding (nth 1 ret)))
1816 (insert (format "Content-Type: %s\n" ctype))
1818 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1820 (or (pgg-encrypt-region beg (point-max) recipients)
1821 (throw 'mime-edit-error 'pgp-error))
1824 "--[[application/pgp; format=mime][7bit]]\n")
1827 (defun mime-edit-sign-smime (beg end boundary)
1831 (narrow-to-region beg end)
1832 (mime-edit-translate-region beg end boundary)))
1834 (encoding (nth 1 ret))
1835 (smime-boundary (concat "smime-sign-" boundary)))
1837 (insert (format "Content-Type: %s\n" ctype))
1839 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1841 (let (buffer-undo-list)
1842 (goto-char (point-min))
1843 (while (progn (end-of-line) (not (eobp)))
1846 (or (smime-sign-buffer)
1847 (throw 'mime-edit-error 'pgp-error)))
1849 (if (re-search-forward "^Content-Type:\\s-*" nil t)
1850 (let* ((start (match-beginning 0))
1851 (body (buffer-substring (match-end 0) (std11-field-end))))
1852 (delete-region start (line-beginning-position 2))
1854 (insert "--[[" body "][7bit]]\n")))))))
1856 (defun mime-edit-encrypt-smime (beg end boundary)
1860 (narrow-to-region beg end)
1861 (mime-edit-translate-region beg end boundary)))
1863 (encoding (nth 1 ret)))
1865 (insert (format "Content-Type: %s\n" ctype))
1867 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1869 (goto-char (point-min))
1870 (while (progn (end-of-line) (not (eobp)))
1873 (or (smime-encrypt-buffer)
1874 (throw 'mime-edit-error 'pgp-error))
1876 (if (re-search-forward "^Content-Type:\\s-*" nil t)
1877 (let* ((start (match-beginning 0))
1878 (body (buffer-substring (match-end 0) (std11-field-end))))
1879 (delete-region start (line-beginning-position 2))
1881 (insert "--[[" body "]]\n")))))))
1883 (defsubst replace-space-with-underline (str)
1884 (mapconcat (function
1891 (defun mime-edit-make-boundary ()
1892 (concat mime-multipart-boundary "_"
1893 (replace-space-with-underline (current-time-string))))
1895 (defun mime-edit-translate-body ()
1896 "Encode the tagged MIME body in current buffer in MIME compliant message."
1899 (let ((boundary (mime-edit-make-boundary))
1902 (while (mime-edit-process-multipart-1
1903 (format "%s-%d" boundary i))
1906 ;; We are interested in message body.
1909 (goto-char (point-min))
1911 (concat "\n" (regexp-quote mail-header-separator)
1912 (if mime-ignore-preceding-spaces
1913 "[ \t\n]*\n" "\n")) nil 'move)
1917 (goto-char (point-max))
1918 (and mime-ignore-trailing-spaces
1919 (re-search-backward "[^ \t\n]\n" beg t)
1922 (setq ret (mime-edit-translate-region
1924 (format "%s-%d" boundary i)))))
1925 (mime-edit-dequote-region (point-min)(point-max))
1926 (let ((contype (car ret)) ;Content-Type
1927 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1929 ;; Insert User-Agent field
1930 (and mime-edit-insert-user-agent-field
1931 (or (mail-position-on-field "User-Agent")
1932 (insert mime-edit-user-agent-value)))
1933 ;; Make primary MIME headers.
1934 (or (mail-position-on-field "MIME-Version")
1935 (insert mime-edit-mime-version-value))
1936 ;; Remove old Content-Type and other fields.
1938 (goto-char (point-min))
1939 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1940 (narrow-to-region (point-min) (point))
1941 (goto-char (point-min))
1942 (mime-delete-field "Content-Type")
1943 (mime-delete-field "Content-Transfer-Encoding"))
1944 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1945 (mail-position-on-field "Content-Type")
1949 (mail-position-on-field "Content-Transfer-Encoding")
1950 (insert encoding)))))))
1952 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
1953 "Translate single-part-tag to MIME header."
1954 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1955 (let* ((beg (match-beginning 0))
1957 (tag (buffer-substring beg end)))
1958 (delete-region beg end)
1959 (let ((contype (mime-edit-get-contype tag))
1960 (encoding (mime-edit-get-encoding tag)))
1961 (insert (concat prefix "--" boundary "\n"))
1963 (narrow-to-region (point)(point))
1964 (insert "Content-Type: " contype "\n")
1966 (insert "Content-Transfer-Encoding: " encoding "\n"))
1967 (mime-encode-header-in-buffer))
1971 (downcase encoding)))))))
1973 (defun mime-edit-translate-region (beg end &optional boundary multipart)
1975 (setq boundary (mime-edit-make-boundary)))
1978 (narrow-to-region beg end)
1979 (let ((tag nil) ;MIME tag
1980 (contype nil) ;Content-Type
1981 (encoding nil) ;Content-Transfer-Encoding
1982 (nparts 0)) ;Number of body parts
1983 ;; Normalize the body part by inserting appropriate message
1984 ;; tags for every message contents.
1985 (mime-edit-normalize-body)
1986 ;; Counting the number of Content-Type.
1987 (goto-char (point-min))
1988 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1989 (setq nparts (1+ nparts)))
1990 ;; Begin translation.
1992 ((and (<= nparts 1)(not multipart))
1993 ;; It's a singular message.
1994 (goto-char (point-min))
1995 (while (re-search-forward
1996 mime-edit-single-part-tag-regexp nil t)
1998 (buffer-substring (match-beginning 0) (match-end 0)))
1999 (delete-region (match-beginning 0) (1+ (match-end 0)))
2000 (setq contype (mime-edit-get-contype tag))
2001 (setq encoding (mime-edit-get-encoding tag))))
2003 ;; It's a multipart message.
2004 (goto-char (point-min))
2005 (let ((prio mime-content-transfer-encoding-priority-list)
2007 (when (setq part-info
2008 (mime-edit-translate-single-part-tag boundary))
2009 (and (setq nprio (member (cdr part-info) prio))
2011 (while (setq part-info
2012 (mime-edit-translate-single-part-tag boundary "\n"))
2013 (and (setq nprio (member (cdr part-info) prio))
2014 (setq prio nprio))))
2015 ;; Define Content-Type as "multipart/mixed".
2017 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2018 (setq encoding (car prio))
2019 ;; Insert the trailer.
2020 (goto-char (point-max))
2021 (insert "\n--" boundary "--\n"))))
2022 (list contype encoding boundary nparts)))))
2024 (defun mime-edit-normalize-body ()
2025 "Normalize the body part by inserting appropriate message tags."
2026 ;; Insert the first MIME tags if necessary.
2027 (goto-char (point-min))
2028 (if (not (looking-at mime-edit-single-part-tag-regexp))
2029 (insert (mime-make-text-tag) "\n"))
2030 ;; Check each tag, and add new tag or correct it if necessary.
2031 (goto-char (point-min))
2032 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2033 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2034 (contype (mime-edit-get-contype tag))
2035 (charset (mime-get-parameter contype "charset"))
2036 (encoding (mime-edit-get-encoding tag)))
2037 ;; Remove extra whitespaces after the tag.
2038 (if (looking-at "[ \t]+$")
2039 (delete-region (match-beginning 0) (match-end 0)))
2041 (end (mime-edit-content-end)))
2042 (if (= end (point-max))
2045 (or (looking-at mime-edit-beginning-tag-regexp)
2047 (insert (mime-make-text-tag) "\n")))
2048 (remove-text-properties beg end '(invisible mime-edit-invisible))
2051 ((mime-test-content-type contype "message")
2052 ;; Content-type "message" should be sent as is.
2054 ((mime-test-content-type contype "text")
2055 ;; Define charset for text if necessary.
2056 (setq charset (if charset
2057 (intern (downcase charset))
2058 (mime-edit-choose-charset)))
2059 (mime-edit-define-charset charset)
2060 (cond ((string-equal contype "text/x-rot13-47-48")
2063 (mule-caesar-region (point) (mime-edit-content-end))))
2064 ((string-equal contype "text/enriched")
2069 (end (mime-edit-content-end)))
2070 ;; Patch for hard newlines
2073 ;; (while (search-forward "\n" end t)
2074 ;; (put-text-property (match-beginning 0)
2077 ;; End patch for hard newlines
2078 (enriched-encode beg end nil)
2080 (if (search-forward "\n\n")
2081 (delete-region beg (match-end 0)))))))
2082 ;; Point is now on current tag.
2083 ;; Define encoding and encode text if necessary.
2084 (or encoding ;Encoding is not specified.
2087 (let ((ret (cdr (assq charset mime-charset-type-list))))
2089 (setq bits (car ret)
2092 conv "quoted-printable")))
2093 (if (<= bits mime-transfer-level)
2094 (mime-encoding-name bits)
2096 (beg (mime-edit-content-beginning)))
2097 (encode-mime-charset-region beg (mime-edit-content-end)
2099 ;; Protect "From " in beginning of line
2101 (narrow-to-region beg (mime-edit-content-end))
2103 (let (case-fold-search)
2104 (if (re-search-forward "^From " nil t)
2106 (if (memq charset '(iso-2022-jp
2111 (replace-match "\e(BFrom ")
2112 (re-search-forward "^From " nil t)))
2113 (setq encoding "quoted-printable"))))))
2114 ;; canonicalize line break code
2115 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2117 (narrow-to-region beg (mime-edit-content-end))
2119 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2120 ;; In a certain period, `replace-match' with "\\N"
2121 ;; converted 8-bit characters into multibyte string,
2122 ;; but it has been fixed at 2004-01-15.
2123 ;;(replace-match "\\1\r\n"))))
2128 (mime-encode-region beg (mime-edit-content-end)
2129 (or encoding "7bit"))
2130 (mime-edit-define-encoding encoding)))
2131 (goto-char (mime-edit-content-end)))
2132 ((null encoding) ;Encoding is not specified.
2133 ;; Application, image, audio, video, and any other
2134 ;; unknown content-type without encoding should be
2136 (let* ((encoding "base64") ;Encode in BASE64 by default.
2137 (beg (mime-edit-content-beginning))
2138 (end (mime-edit-content-end)))
2139 (mime-encode-region beg end encoding)
2140 (mime-edit-define-encoding encoding))
2141 (forward-line 1))))))
2143 (defun mime-delete-field (field)
2144 "Delete header FIELD."
2145 (let ((regexp (format "^%s:[ \t]*" field)))
2146 (goto-char (point-min))
2147 (while (re-search-forward regexp nil t)
2148 (delete-region (match-beginning 0)
2149 (1+ (std11-field-end))))))
2153 ;;; Platform dependent functions
2156 ;; Sun implementations
2158 (defun mime-edit-voice-recorder-for-sun (encoding)
2159 "Record voice in a buffer using Sun audio device,
2160 and insert data encoded as ENCODING."
2161 (message "Start the recording on %s. Type C-g to finish the recording..."
2163 (mime-insert-encoded-file "/dev/audio" encoding))
2166 ;;; @ Other useful commands.
2169 ;; Message forwarding commands as content-type "message/rfc822".
2171 (defun mime-edit-insert-message (&optional message)
2173 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2174 (if (and inserter (fboundp inserter))
2176 (mime-edit-insert-tag "message" "rfc822")
2177 (funcall inserter message))
2178 (message "Sorry, I don't have message inserter for your MUA."))))
2180 (defun mime-edit-insert-mail (&optional message)
2182 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2183 (if (and inserter (fboundp inserter))
2185 (mime-edit-insert-tag "message" "rfc822")
2186 (funcall inserter message))
2187 (message "Sorry, I don't have mail inserter for your MUA."))))
2189 (defun mime-edit-inserted-message-filter ()
2192 (let ((header-start (point))
2193 (case-fold-search t)
2196 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2197 (if (re-search-forward "^$" (mark t))
2198 (narrow-to-region header-start (match-beginning 0)))
2199 (goto-char header-start)
2200 (while (and (re-search-forward
2201 mime-edit-yank-ignored-field-regexp nil t)
2202 (setq beg (match-beginning 0))
2203 (setq end (1+ (std11-field-end))))
2204 (delete-region beg end))))))
2207 ;;; @ multipart enclosure
2210 (defun mime-edit-enclose-region-internal (type beg end)
2214 (narrow-to-region beg end)
2215 (insert (format "--<<%s>>-{\n" type))
2216 (goto-char (point-max))
2217 (insert (format "--}-<<%s>>\n" type))
2218 (goto-char (point-max)))
2219 (or (looking-at mime-edit-beginning-tag-regexp)
2221 (insert (mime-make-text-tag) "\n"))))
2223 (defun mime-edit-enclose-quote-region (beg end)
2225 (mime-edit-enclose-region-internal 'quote beg end))
2227 (defun mime-edit-enclose-mixed-region (beg end)
2229 (mime-edit-enclose-region-internal 'mixed beg end))
2231 (defun mime-edit-enclose-parallel-region (beg end)
2233 (mime-edit-enclose-region-internal 'parallel beg end))
2235 (defun mime-edit-enclose-digest-region (beg end)
2237 (mime-edit-enclose-region-internal 'digest beg end))
2239 (defun mime-edit-enclose-alternative-region (beg end)
2241 (mime-edit-enclose-region-internal 'alternative beg end))
2243 (defun mime-edit-enclose-pgp-signed-region (beg end)
2245 (mime-edit-enclose-region-internal 'pgp-signed beg end))
2247 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2249 (mime-edit-enclose-region-internal 'pgp-encrypted beg end))
2251 (defun mime-edit-enclose-kazu-signed-region (beg end)
2253 (mime-edit-enclose-region-internal 'kazu-signed beg end))
2255 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2257 (mime-edit-enclose-region-internal 'kazu-encrypted beg end))
2259 (defun mime-edit-enclose-smime-signed-region (beg end)
2261 (mime-edit-enclose-region-internal 'smime-signed beg end))
2263 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2265 (mime-edit-enclose-region-internal 'smime-encrypted beg end))
2267 (defun mime-edit-insert-key (&optional arg)
2268 "Insert a pgp public key."
2270 (mime-edit-insert-tag "application" "pgp-keys")
2271 (mime-edit-define-encoding "7bit")
2273 (if (and (not (eobp))
2274 (not (looking-at mime-edit-single-part-tag-regexp)))
2275 (insert (mime-make-text-tag) "\n")))
2281 (defun mime-edit-set-split (arg)
2284 (y-or-n-p "Do you want to enable split? ")))
2285 (setq mime-edit-split-message arg)
2287 (message "This message is enabled to split.")
2288 (message "This message is not enabled to split.")))
2290 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2291 "Toggle transfer-level is 7bit or 8bit through.
2293 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2295 (if (numberp transfer-level)
2296 (setq mime-transfer-level transfer-level)
2297 (if (< mime-transfer-level 8)
2298 (setq mime-transfer-level 8)
2299 (setq mime-transfer-level 7)))
2300 (message (format "Current transfer-level is %d bit"
2301 mime-transfer-level))
2302 (setq mime-transfer-level-string
2303 (mime-encoding-name mime-transfer-level 'not-omit))
2304 (force-mode-line-update))
2306 (defun mime-edit-set-transfer-level-7bit ()
2308 (mime-edit-toggle-transfer-level 7))
2310 (defun mime-edit-set-transfer-level-8bit ()
2312 (mime-edit-toggle-transfer-level 8))
2318 (defvar mime-edit-pgp-processing nil)
2319 (make-variable-buffer-local 'mime-edit-pgp-processing)
2321 (defun mime-edit-set-sign (arg)
2324 (y-or-n-p "Do you want to sign? ")))
2327 (or (memq 'sign mime-edit-pgp-processing)
2328 (setq mime-edit-pgp-processing
2329 (nconc mime-edit-pgp-processing
2330 (copy-sequence '(sign)))))
2331 (message "This message will be signed."))
2332 (setq mime-edit-pgp-processing
2333 (delq 'sign mime-edit-pgp-processing))
2334 (message "This message will not be signed.")))
2336 (defun mime-edit-set-encrypt (arg)
2339 (y-or-n-p "Do you want to encrypt? ")))
2342 (or (memq 'encrypt mime-edit-pgp-processing)
2343 (setq mime-edit-pgp-processing
2344 (nconc mime-edit-pgp-processing
2345 (copy-sequence '(encrypt)))))
2346 (message "This message will be encrypt."))
2347 (setq mime-edit-pgp-processing
2348 (delq 'encrypt mime-edit-pgp-processing))
2349 (message "This message will not be encrypt.")))
2351 (defun mime-edit-pgp-enclose-buffer ()
2352 (let ((beg (save-excursion
2353 (goto-char (point-min))
2354 (if (search-forward (concat "\n" mail-header-separator "\n"))
2357 (dolist (pgp-processing mime-edit-pgp-processing)
2358 (case pgp-processing
2360 (mime-edit-enclose-pgp-signed-region
2363 (mime-edit-enclose-pgp-encrypted-region
2364 beg (point-max))))))))
2370 (defun mime-edit-insert-partial-header (fields subject
2371 id number total separator)
2373 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2374 (insert mime-edit-mime-version-field-for-message/partial)
2376 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2377 id number total separator)))
2379 (defun mime-edit-split-and-send
2380 (&optional cmd lines mime-edit-message-max-length)
2384 (count-lines (point-min) (point-max))))
2385 (or mime-edit-message-max-length
2386 (setq mime-edit-message-max-length
2387 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2388 mime-edit-message-default-max-lines)))
2389 (let* ((mime-edit-draft-file-name
2390 (or (buffer-file-name)
2392 (expand-file-name "mime-draft" temporary-file-directory))))
2393 (separator mail-header-separator)
2395 (replace-space-with-underline (current-time-string))
2396 "@" (system-name) "\"")))
2397 (run-hooks 'mime-edit-before-split-hook)
2398 (let ((the-buf (current-buffer))
2399 (copy-buf (get-buffer-create " *Original Message*"))
2400 (header (std11-header-string-except
2401 mime-edit-split-ignored-field-regexp separator))
2402 (subject (mail-fetch-field "subject"))
2403 (total (+ (/ lines mime-edit-message-max-length)
2404 (if (> (mod lines mime-edit-message-max-length) 0)
2410 mime-edit-split-message-sender-alist))
2414 (error "Split sender is not specified for `%s'." major-mode)))))
2415 (mime-edit-partial-number 1)
2418 (set-buffer copy-buf)
2420 (insert-buffer the-buf)
2422 (if (re-search-forward
2423 (concat "^" (regexp-quote separator) "$") nil t)
2424 (let ((he (match-beginning 0)))
2426 (narrow-to-region (point-min) he)))
2427 (goto-char (point-min))
2428 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2429 (delete-region (match-beginning 0)
2430 (1+ (std11-field-end))))))
2431 (while (< mime-edit-partial-number total)
2434 (set-buffer copy-buf)
2435 (setq data (buffer-substring
2438 (goto-line mime-edit-message-max-length)
2440 (delete-region (point-min)(point)))
2441 (mime-edit-insert-partial-header
2442 header subject id mime-edit-partial-number total separator)
2445 (message (format "Sending %d/%d..."
2446 mime-edit-partial-number total))
2447 (call-interactively command)
2448 (message (format "Sending %d/%d...done"
2449 mime-edit-partial-number total)))
2450 (setq mime-edit-partial-number
2451 (1+ mime-edit-partial-number)))
2454 (set-buffer copy-buf)
2455 (setq data (buffer-string))
2457 (mime-edit-insert-partial-header
2458 header subject id mime-edit-partial-number total separator)
2461 (message (format "Sending %d/%d..."
2462 mime-edit-partial-number total))
2463 (message (format "Sending %d/%d...done"
2464 mime-edit-partial-number total))))))
2466 (defun mime-edit-maybe-split-and-send (&optional cmd)
2468 (run-hooks 'mime-edit-before-send-hook)
2469 (let ((mime-edit-message-max-length
2470 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2471 mime-edit-message-default-max-lines))
2472 (lines (count-lines (point-min) (point-max))))
2473 (if (and (> lines mime-edit-message-max-length)
2474 mime-edit-split-message)
2475 (mime-edit-split-and-send cmd lines mime-edit-message-max-length))))
2478 ;;; @ preview message
2481 (defvar mime-edit-buffer nil) ; buffer local variable
2483 (defun mime-edit-preview-message ()
2484 "preview editing MIME message."
2486 (let* ((str (buffer-string))
2487 (separator mail-header-separator)
2488 (the-buf (current-buffer))
2489 (buf-name (buffer-name))
2490 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2491 (buf (get-buffer temp-buf-name))
2492 (pgp-processing mime-edit-pgp-processing))
2495 (switch-to-buffer buf)
2497 (setq buf (get-buffer-create temp-buf-name))
2498 (switch-to-buffer buf))
2500 (setq major-mode 'mime-temp-message-mode)
2501 (make-local-variable 'mail-header-separator)
2502 (setq mail-header-separator separator)
2503 (make-local-variable 'mime-edit-buffer)
2504 (setq mime-edit-buffer the-buf)
2505 (setq mime-edit-pgp-processing pgp-processing)
2507 (run-hooks 'mime-edit-translate-hook)
2508 (mime-edit-translate-buffer)
2509 (goto-char (point-min))
2510 (if (re-search-forward
2511 (concat "^" (regexp-quote separator) "$"))
2514 (make-local-variable 'mime-edit-temp-message-buffer)
2515 (setq mime-edit-temp-message-buffer buf)))
2517 (defun mime-edit-quitting-method ()
2518 "Quitting method for mime-view."
2519 (let* ((temp mime-edit-temp-message-buffer)
2521 (mime-preview-kill-buffer)
2523 (setq buf mime-edit-buffer)
2525 (switch-to-buffer buf)))
2527 (set-alist 'mime-preview-quitting-method-alist
2528 'mime-temp-message-mode
2529 #'mime-edit-quitting-method)
2535 (defvar mime-edit-again-ignored-field-regexp
2536 (concat "^\\(" "Content-.*\\|Mime-Version"
2537 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2539 "Regexp for deleted header fields when `mime-edit-again' is called.")
2541 (defsubst eliminate-top-spaces (string)
2542 "Eliminate top sequence of space or tab in STRING."
2543 (if (string-match "^[ \t]+" string)
2544 (substring string (match-end 0))
2547 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2550 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2551 '(("application/pgp-encrypted" . pgp-encrypted)
2552 ("application/pgp-signature" . pgp-signed))))
2553 (mime-content-type-subtype content-type)))
2554 (boundary (mime-content-type-parameter content-type "boundary"))
2555 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2556 (re-search-forward boundary-pat nil t)
2557 (let ((bb (match-beginning 0)) eb tag)
2558 (setq tag (format "\n--<<%s>>-{\n" subtype))
2561 (setq bb (+ bb (length tag)))
2563 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2565 (setq eb (match-beginning 0))
2566 (replace-match (format "--}-<<%s>>\n" subtype))
2568 (narrow-to-region bb eb)
2569 (goto-char (point-min))
2570 (while (re-search-forward boundary-pat nil t)
2571 (let ((beg (match-beginning 0))
2573 (delete-region beg (match-end 0))
2575 (if (re-search-forward boundary-pat nil t)
2576 (setq end (match-beginning 0))
2577 (setq end (point-max)))
2579 (narrow-to-region beg end)
2581 ((eq subtype 'pgp-encrypted)
2584 (goto-char (point-min))
2585 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2588 (save-window-excursion
2589 (pgg-decrypt-region (match-beginning 0)
2591 (delete-region (point-min)(point-max))))
2592 (insert-buffer-substring pgg-output-buffer)
2593 (mime-edit-decode-message-in-buffer
2594 nil not-decode-text)
2595 (delete-region (goto-char (point-min))
2596 (if (search-forward "\n\n" nil t)
2599 (goto-char (point-max))))
2601 (mime-edit-decode-message-in-buffer
2602 (if (eq subtype 'digest)
2604 (make-mime-content-type 'message 'rfc822)))
2606 (goto-char (point-max))))))))))
2607 (goto-char (point-min))
2608 (or (= (point-min) 1)
2609 (delete-region (point-min)
2610 (if (search-forward "\n\n" nil t)
2614 (defun mime-edit-decode-single-part-in-buffer
2615 (content-type not-decode-text &optional content-disposition)
2616 (let* ((type (mime-content-type-primary-type content-type))
2617 (subtype (mime-content-type-subtype content-type))
2618 (ctype (format "%s/%s" type subtype))
2620 (pstr (let ((bytes (+ 14 (length ctype))))
2621 (mapconcat (function
2623 (if (string= (car attr) "charset")
2625 (setq charset (cdr attr))
2627 (let* ((str (concat (car attr)
2630 (setq bytes (+ bytes bs 2))
2633 (setq bytes (+ bs 1))
2636 (mime-content-type-parameters content-type) "")))
2639 (limit (save-excursion
2640 (if (search-forward "\n\n" nil t)
2643 (mime-content-disposition-type content-disposition))
2645 (if disposition-type
2646 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2647 (mapconcat (function
2652 (if (string-equal "filename"
2654 (std11-wrap-as-quoted-string
2658 (setq bytes (+ bytes bs 2))
2661 (setq bytes (+ bs 1))
2664 (mime-content-disposition-parameters
2665 content-disposition)
2667 (if disposition-type
2668 (setq pstr (format "%s\nContent-Disposition: %s%s"
2669 pstr disposition-type disposition-str)))
2671 (if (re-search-forward
2672 "^Content-Transfer-Encoding:" limit t)
2673 (let ((beg (match-beginning 0))
2674 (hbeg (match-end 0))
2675 (end (std11-field-end limit)))
2678 (eliminate-top-spaces
2679 (std11-unfold-string
2680 (buffer-substring hbeg end)))))
2681 (if (or charset (eq type 'text))
2683 (delete-region beg (1+ end))
2684 (goto-char (point-min))
2685 (if (search-forward "\n\n" nil t)
2688 (match-end 0)(point-max) encoding)
2690 encoding nil))))))))
2691 (if (and (eq type 'text)
2692 (or encoded (not not-decode-text)))
2695 (goto-char (point-min))
2696 (while (re-search-forward "\r\n" nil t)
2697 (replace-match "\n")))
2698 (decode-mime-charset-region (point-min)(point-max)
2699 (or charset default-mime-charset))))
2700 (let ((he (if (re-search-forward "^$" nil t)
2703 (if (and (eq type 'text)
2704 (eq subtype 'x-rot13-47-48))
2705 (mule-caesar-region he (point-max)))
2706 (if (= (point-min) 1)
2712 (format "%s/%s%s" type subtype pstr)
2714 (delete-region (point-min) he)
2716 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2720 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2723 (goto-char (point-min))
2724 (let ((ctl (or (mime-read-Content-Type)
2725 default-content-type)))
2727 (let ((type (mime-content-type-primary-type ctl)))
2729 ((and (eq type 'application)
2730 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2731 (delete-region (point-min)(point-max)))
2732 ((eq type 'multipart)
2733 (mime-edit-decode-multipart-in-buffer ctl not-decode-text))
2735 (mime-edit-decode-single-part-in-buffer
2736 ctl not-decode-text (mime-read-Content-Disposition)))))
2738 (decode-mime-charset-region (point-min) (point-max)
2739 default-mime-charset)))
2740 (if (= (point-min) 1)
2743 (std11-narrow-to-header)
2744 (goto-char (point-min))
2745 (while (re-search-forward
2746 mime-edit-again-ignored-field-regexp nil t)
2747 (delete-region (match-beginning 0) (1+ (std11-field-end)))))
2748 (mime-decode-header-in-buffer (not not-decode-text)))))))
2751 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2752 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2753 Content-Type and Content-Transfer-Encoding header fields will be
2754 converted to MIME-Edit tags."
2756 (goto-char (point-min))
2758 (concat "\n" (regexp-quote mail-header-separator) "\n")
2760 (replace-match "\n\n"))
2761 (mime-edit-decode-message-in-buffer nil not-decode-text)
2762 (goto-char (point-min))
2764 (and (re-search-forward "^$")
2765 (replace-match mail-header-separator)))
2767 (turn-on-mime-edit)))
2773 (provide 'mime-edit)
2775 (run-hooks 'mime-edit-load-hook)
2777 ;;; mime-edit.el ends here