1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,94,95,96,97,98,99 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
8 ;; Created: 1994/08/21 renamed from mime.el
9 ;; Renamed: 1997/2/21 from tm-edit.el
10 ;; Keywords: MIME, multimedia, multilingual, mail, news
12 ;; This file is part of SEMI (Sophisticated Emacs MIME Interfaces).
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; This is an Emacs minor mode for editing Internet multimedia
32 ;; messages formatted in MIME (RFC 2045, 2046, 2047, 2048 and 2049).
33 ;; All messages in this mode are composed in the tagged MIME format,
34 ;; that are described in the following examples. The messages
35 ;; composed in the tagged MIME format are automatically translated
36 ;; into a MIME compliant message when exiting the mode.
38 ;; Mule (multilingual feature of Emacs 20 and multilingual extension
39 ;; for XEmacs 20) has a capability of handling multilingual text in
40 ;; limited ISO-2022 manner that is based on early experiences in
41 ;; Japanese Internet community and resulted in RFC 1468 (ISO-2022-JP
42 ;; charset for MIME). In order to enable multilingual capability in
43 ;; single text message in MIME, charset of multilingual text written
44 ;; in Mule is declared as either `ISO-2022-JP-2' [RFC 1554]. Mule is
45 ;; required for reading the such messages.
47 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
48 ;; News mode. First of all, you need the following autoload
49 ;; definition to load mime-edit-mode automatically:
51 ;; (autoload 'turn-on-mime-edit "mime-edit"
52 ;; "Minor mode for editing MIME message." t)
54 ;; In case of Mail mode (includes VM mode), you need the following
57 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
58 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
60 ;; In case of MH-E, you need the following hook definition:
62 ;; (add-hook 'mh-letter-mode-hook
65 ;; (turn-on-mime-edit)
66 ;; (make-local-variable 'mail-header-separator)
67 ;; (setq mail-header-separator "--------")
69 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
71 ;; In case of News mode, you need the following hook definition:
73 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
74 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
76 ;; In case of Emacs 19, it is possible to emphasize the message tags
77 ;; using font-lock mode as follows:
79 ;; (add-hook 'mime-edit-mode-hook
83 ;; (setq font-lock-keywords (list mime-edit-tag-regexp))
86 ;; The message tag looks like:
88 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
90 ;; The tagged MIME message examples:
92 ;; This is a conventional plain text. It should be translated into
96 ;; This is also a plain text. But, it is explicitly specified as is.
97 ;;--[[text/plain; charset=ISO-8859-1]]
98 ;; This is also a plain text. But charset is specified as iso-8859-1.
100 ;; ¡Hola! Buenos días. ¿Cómo está usted?
101 ;;--[[text/enriched]]
102 ;; <center>This is a richtext.</center>
104 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
106 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
110 (eval-when-compile (require 'static))
113 (require 'mail-utils)
115 (require 'eword-encode) ; eword-encode-field-body
123 (autoload 'pgg-encrypt-region "pgg"
124 "PGP encryption of current region." t)
125 (autoload 'pgg-sign-region "pgg"
126 "PGP signature of current region." t)
127 (autoload 'pgg-insert-key "pgg"
128 "Insert PGP public key at point." t)
129 (autoload 'smime-encrypt-region "smime"
130 "S/MIME encryption of current region.")
131 (autoload 'smime-sign-region "smime"
132 "S/MIME signature of current region.")
133 (defvar smime-output-buffer)
134 (defvar smime-errors-buffer)
141 (defconst mime-edit-version
143 (mime-product-name mime-user-interface-product) " "
144 (mapconcat #'number-to-string
145 (mime-product-version mime-user-interface-product) ".")
146 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
152 (defgroup mime-edit nil
156 (defcustom mime-ignore-preceding-spaces nil
157 "*Ignore preceding white spaces if non-nil."
161 (defcustom mime-ignore-trailing-spaces nil
162 "*Ignore trailing white spaces if non-nil."
166 (defcustom mime-ignore-same-text-tag t
167 "*Ignore preceding text content-type tag that is same with new one.
168 If non-nil, the text tag is not inserted unless something different."
172 (defcustom mime-auto-hide-body t
173 "*Hide non-textual body encoded in base64 after insertion if non-nil."
177 (defcustom mime-edit-voice-recorder
178 (function mime-edit-voice-recorder-for-sun)
179 "*Function to record a voice message and encode it."
183 (defcustom mime-edit-mode-hook nil
184 "*Hook called when enter MIME mode."
188 (defcustom mime-edit-translate-hook nil
189 "*Hook called before translating into a MIME compliant message.
190 To insert a signature file automatically, call the function
191 `mime-edit-insert-signature' from this hook."
195 (defcustom mime-edit-exit-hook nil
196 "*Hook called when exit MIME mode."
200 (defvar mime-content-types
202 ;; Charset parameter need not to be specified, since it is
203 ;; defined automatically while translation.
205 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
218 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
219 ("directory" "/pub/GNU/elisp/mime")
221 ("mode" "image" "ascii" "local8"))
226 ("mode" "image" "ascii" "local8"))
227 ("tftp" ("site") ("name"))
228 ("afs" ("site") ("name"))
229 ("local-file" ("site") ("name"))
231 ("server" "ftpmail@nic.karrn.ad.jp")
237 ("octet-stream" ("type" "" "tar" "shar"))
239 ("vnd.ms-powerpoint")
240 ("x-kiss" ("x-cnf")))
252 "*Alist of content-type, subtype, parameters and its values.")
254 (defcustom mime-file-types
257 ;; Programming languages
260 "application" "octet-stream" (("type" . "C++"))
262 "attachment" (("filename" . file)))
265 "application" "octet-stream" (("type" . "emacs-lisp"))
267 "attachment" (("filename" . file)))
270 "application" "octet-stream" (("type" . "common-lisp"))
272 "attachment" (("filename" . file)))
275 "application" "octet-stream" (("type" . "perl"))
277 "attachment" (("filename" . file)))
279 ;; Text or translated text
284 "inline" (("filename" . file)))
286 ;; .rc : procmail modules pm-xxxx.rc
287 ;; *rc : other resource files
289 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
292 "attachment" (("filename" . file)))
299 ("\\.diff$\\|\\.patch$"
300 "text" "plain" (("type" . "patch"))
302 "inline" (("filename" . file)))
305 "text" "plain" nil nil nil nil)
308 ;; Octect binary text
311 "application" "winword" nil
313 "attachment" (("filename" . file)))
314 ("\\.ppt$" ; MS Power Point
315 "application" "vnd.ms-powerpoint" nil
317 "attachment" (("filename" . file)))
322 "inline" (("filename" . file)))
324 "application" "postscript" nil
326 "attachment" (("filename" . file)))
333 "inline" (("filename" . file)))
337 "inline" (("filename" . file)))
341 "inline" (("filename" . file)))
345 "inline" (("filename" . file)))
349 "inline" (("filename" . file)))
353 "inline" (("filename" . file)))
357 "inline" (("filename" . file)))
361 "inline" (("filename" . file)))
365 "attachment" (("filename" . file)))
369 "attachment" (("filename" . file)))
371 "application" "octet-stream" (("type" . "tar+gzip"))
373 "attachment" (("filename" . file)))
375 "application" "octet-stream" (("type" . "tar+gzip"))
377 "attachment" (("filename" . file)))
379 "application" "octet-stream" (("type" . "tar+compress"))
381 "attachment" (("filename" . file)))
383 "application" "octet-stream" (("type" . "tar+compress"))
385 "attachment" (("filename" . file)))
387 "application" "octet-stream" (("type" . "gzip"))
389 "attachment" (("filename" . file)))
391 "application" "octet-stream" (("type" . "compress"))
393 "attachment" (("filename" . file)))
395 "application" "octet-stream" (("type" . "lha"))
397 "attachment" (("filename" . file)))
399 "application" "zip" nil
401 "attachment" (("filename" . file)))
406 "application" "octet-stream" nil
408 "attachment" (("filename" . file))))
409 "*Alist of file name, types, parameters, and default encoding.
410 If encoding is nil, it is determined from its contents."
414 (choice :tag "Primary-Type"
415 ,@(nconc (mapcar (lambda (cell)
416 (list 'item (car cell)))
420 (choice :tag "Sub-Type"
423 (mapcar (lambda (cell)
424 (mapcar (lambda (cell)
425 (list 'item (car cell)))
430 (repeat :tag "Parameters of Content-Type field"
431 (cons string (choice string symbol)))
432 ;; content-transfer-encoding
433 (choice :tag "Encoding"
436 (mapcar (lambda (cell)
438 (mime-encoding-list))))
440 (choice :tag "Disposition-Type"
446 (repeat :tag "Parameters of Content-Disposition field"
447 (cons string (choice string symbol)))))
450 (defvar mime-content-disposition-types
451 '(("attachment") ("inline")))
453 (defcustom mime-edit-attach-at-end-type nil
454 "*List of MIME types to be attached at the end of a message.
455 Values must be strings indicates MIME types. You can specify
456 either type/subtype or type only."
458 :type '(choice (const :tag "Nothing" nil)
459 (list (repeat symbol))))
461 ;;; @@ about charset, encoding and transfer-level
464 (defvar mime-charset-type-list
466 (iso-8859-1 8 "quoted-printable")
467 (iso-8859-2 8 "quoted-printable")
468 (iso-8859-3 8 "quoted-printable")
469 (iso-8859-4 8 "quoted-printable")
470 (iso-8859-5 8 "quoted-printable")
471 (koi8-r 8 "quoted-printable")
472 (iso-8859-7 8 "quoted-printable")
473 (iso-8859-8 8 "quoted-printable")
474 (iso-8859-9 8 "quoted-printable")
475 (iso-2022-jp 7 "base64")
476 (iso-2022-kr 7 "base64")
482 (shift_jis 8 "base64")
484 (iso-2022-jp-2 7 "base64")
485 (iso-2022-int-1 7 "base64")))
487 (defcustom mime-transfer-level 7
488 "*A number of network transfer level. It should be bigger than 7.
489 Currently it has no effect except mode-line display."
491 :type '(choice (const 7)
493 (const :tag "Binary" 9)))
494 (make-variable-buffer-local 'mime-transfer-level)
496 (defsubst mime-encoding-name (transfer-level &optional not-omit)
497 (cond ((> transfer-level 8) "binary")
498 ((= transfer-level 8) "8bit")
501 (defvar mime-transfer-level-string
502 (mime-encoding-name mime-transfer-level 'not-omit)
503 "A string formatted version of `mime-transfer-level'.")
504 (make-variable-buffer-local 'mime-transfer-level-string)
506 ;;; @@ about content transfer encoding
508 (defvar mime-content-transfer-encoding-priority-list
509 '(nil "8bit" "binary"))
511 ;;; @@ about message inserting
514 (defcustom mime-edit-yank-ignored-field-list
515 '("Received" "Approved" "Path" "Replied" "Status"
516 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
517 "List of ignored header fields when inserting message/rfc822.
518 Each elements are regexp of field-name."
520 :type '(repeat regexp))
522 (defvar mime-edit-message-inserter-alist nil)
523 (defvar mime-edit-mail-inserter-alist nil)
526 ;;; @@ about message splitting
529 (defcustom mime-edit-split-message t
530 "*Split large message if it is non-nil."
534 (defcustom mime-edit-message-default-max-lines 5000
535 "*Default maximum lines of a message."
539 (defcustom mime-edit-message-max-lines-alist
540 '((news-reply-mode . 500))
541 "Alist of `major-mode' vs maximum lines of a message.
542 If it is not specified for a `major-mode',
543 `mime-edit-message-default-max-lines' is used."
547 (defconst mime-edit-split-ignored-field-regexp
548 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
550 (defcustom mime-edit-split-blind-field-regexp
551 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
552 "*Regular expression to match field-name to be ignored when split sending."
556 (defvar mime-edit-split-message-sender-alist nil)
558 (defvar mime-edit-news-reply-mode-server-running nil)
564 (defconst mime-edit-single-part-tag-regexp
565 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
566 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
568 (defconst mime-edit-quoted-single-part-tag-regexp
569 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
571 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
573 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
575 (defconst mime-edit-beginning-tag-regexp
576 (regexp-or mime-edit-single-part-tag-regexp
577 mime-edit-multipart-beginning-regexp))
579 (defconst mime-edit-end-tag-regexp
580 (regexp-or mime-edit-single-part-tag-regexp
581 mime-edit-multipart-end-regexp))
583 (defconst mime-edit-tag-regexp
584 (regexp-or mime-edit-single-part-tag-regexp
585 mime-edit-multipart-beginning-regexp
586 mime-edit-multipart-end-regexp))
588 (defconst mime-tag-format "--[[%s]]"
589 "Control-string making a MIME tag.")
591 (defconst mime-tag-format-with-encoding "--[[%s][%s]]"
592 "Control-string making a MIME tag with encoding.")
595 ;;; @@ multipart boundary
598 (defvar mime-multipart-boundary "Multipart"
599 "*Boundary of a multipart message.")
602 ;;; @@ optional header fields
605 (defvar mime-edit-insert-user-agent-field t
606 "*If non-nil, insert User-Agent header field.")
608 (defvar mime-edit-user-agent-value
609 (concat (mime-product-name mime-user-interface-product)
611 (mapconcat #'number-to-string
612 (mime-product-version mime-user-interface-product) ".")
614 (mime-product-code-name mime-user-interface-product)
616 (mime-product-name mime-library-product)
618 (mapconcat #'number-to-string
619 (mime-product-version mime-library-product) ".")
621 (mime-product-code-name mime-library-product)
623 (if (fboundp 'apel-version)
624 (concat (apel-version) " "))
625 (if (featurep 'xemacs)
626 (concat (cond ((featurep 'utf-2000)
627 (concat "UTF-2000-MULE/" utf-2000-version))
628 ((featurep 'mule) "MULE"))
630 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
633 (substring emacs-version 0 (match-end 0))
634 (cond ((and (boundp 'xemacs-betaname)
636 ;; It does not exist in XEmacs
637 ;; versions prior to 20.3.
638 (concat " " xemacs-betaname))
639 ((and (boundp 'emacs-patch-level)
641 ;; It does not exist in FSF Emacs or in
642 ;; XEmacs versions earlier than 21.1.1.
643 (format " (patch %d)" emacs-patch-level))
645 " (" xemacs-codename ") ("
646 system-configuration ")")
647 " (" emacs-version ")"))
648 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
649 (substring emacs-version 0 (match-beginning 0))
652 (if (boundp 'enable-multibyte-characters)
654 " (" system-configuration ")"
655 (if enable-multibyte-characters
656 (concat " MULE/" mule-version)
657 " (with unibyte mode)")
658 (if (featurep 'meadow)
659 (let ((mver (Meadow-version)))
660 (if (string-match "^Meadow-" mver)
664 (concat "MULE/" mule-version
665 " (based on Emacs " ver ")"))
666 (concat "Emacs/" ver " (" system-configuration ")")))))
667 "Body of User-Agent field.
668 If variable `mime-edit-insert-user-agent-field' is not nil, it is
669 inserted into message header.")
675 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
676 "Specify MIME tspecials.
677 Tspecials means any character that matches with it in header must be quoted.")
679 (defconst mime-edit-mime-version-value
680 ; (concat "1.0 (generated by " mime-edit-version ")")
682 "MIME version number.")
684 (defconst mime-edit-mime-version-field-for-message/partial
685 "MIME-Version: 1.0\n"
686 ;; (concat "MIME-Version:"
687 ;; (eword-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 "About split" mime-edit-set-split)
775 (sign "About sign" mime-edit-set-sign)
776 (encrypt "About encryption" 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 (static-if (featurep 'xemacs)
782 ;; modified by Pekka Marjola <pema@iki.fi>
783 ;; 1995/9/5 (c.f. [tm-en:69])
785 (defun mime-edit-define-menu-for-xemacs ()
786 "Define menu for XEmacs."
787 (cond ((featurep 'menubar)
788 (make-local-variable 'current-menubar)
789 (set-buffer-menubar current-menubar)
792 (cons mime-edit-menu-title
795 (vector (nth 1 item)(nth 2 item)
796 mime-edit-mode-flag)))
797 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 ;; Bogus check. Nemacs is not supported.
809 ;;(>= emacs-major-version 19)
810 (define-key mime-edit-mode-map [menu-bar mime-edit]
811 (cons mime-edit-menu-title
812 (make-sparse-keymap mime-edit-menu-title)))
815 (define-key mime-edit-mode-map
816 (vector 'menu-bar 'mime-edit (car item))
817 (cons (nth 1 item)(nth 2 item)))))
818 (reverse mime-edit-menu-list)))
823 (defmacro mime-edit-insert-place (type-list &rest body)
825 (if (or (member (intern (concat (car ,type-list) "/" (cadr ,type-list)))
826 mime-edit-attach-at-end-type)
827 (member (intern (car ,type-list))
828 mime-edit-attach-at-end-type))
829 (goto-char (point-max)))
835 (defvar mime-edit-touched-flag nil)
838 (defun mime-edit-mode ()
839 "MIME minor mode for editing the tagged MIME message.
841 In this mode, basically, the message is composed in the tagged MIME
842 format. The message tag looks like:
844 --[[text/plain; charset=ISO-2022-JP][7bit]]
846 The tag specifies the MIME content type, subtype, optional parameters
847 and transfer encoding of the message following the tag. Messages
848 without any tag are treated as `text/plain' by default. Charset and
849 transfer encoding are automatically defined unless explicitly
850 specified. Binary messages such as audio and image are usually
851 hidden. The messages in the tagged MIME format are automatically
852 translated into a MIME compliant message when exiting this mode.
854 Available charsets depend on Emacs version being used. The following
855 lists the available charsets of each emacs.
857 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
858 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
859 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
860 CN-BIG5 and ISO-2022-INT-1 are available.
862 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
863 be used to represent multilingual text in intermixed manner. Any
864 languages that has no registered charset are represented as either
865 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
867 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
868 without mule, please set variable `default-mime-charset'. This
869 variable must be symbol of which name is a MIME charset.
871 If you want to add more charsets in mule, please set variable
872 `charsets-mime-charset-alist'. This variable must be alist of which
873 key is list of charset and value is symbol of MIME charset. If name
874 of coding-system is different as MIME charset, please set variable
875 `mime-charset-coding-system-alist'. This variable must be alist of
876 which key is MIME charset and value is coding-system.
878 Following commands are available in addition to major mode commands:
881 \\[mime-edit-insert-text] insert a text message.
882 \\[mime-edit-insert-file] insert a (binary) file.
883 \\[mime-edit-insert-external] insert a reference to external body.
884 \\[mime-edit-insert-voice] insert a voice message.
885 \\[mime-edit-insert-message] insert a mail or news message.
886 \\[mime-edit-insert-mail] insert a mail message.
887 \\[mime-edit-insert-signature] insert a signature file at end.
888 \\[mime-edit-insert-key] insert PGP public key.
889 \\[mime-edit-insert-tag] insert a new MIME tag.
891 \[make enclosure (maybe multipart)\]
892 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
893 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
894 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
895 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
896 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
897 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
898 \\[mime-edit-enclose-quote-region] enclose as verbose mode
899 (to avoid to expand tags)
902 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
903 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
904 \\[mime-edit-set-split] set message splitting mode.
905 \\[mime-edit-set-sign] set PGP-sign mode.
906 \\[mime-edit-set-encrypt] set PGP-encryption mode.
907 \\[mime-edit-preview-message] preview editing MIME message.
908 \\[mime-edit-exit] exit and translate into a MIME
910 \\[mime-edit-help] show this help.
911 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
914 Additional commands are available in some major modes:
915 C-c C-c exit, translate and run the original command.
916 C-c C-s exit, translate and run the original command.
918 The following is a message example written in the tagged MIME format.
919 TABs at the beginning of the line are not a part of the message:
921 This is a conventional plain text. It should be translated
924 This is also a plain text. But, it is explicitly specified as
926 --[[text/plain; charset=ISO-8859-1]]
927 This is also a plain text. But charset is specified as
930 ¡Hola! Buenos días. ¿Cómo está usted?
932 This is a <bold>enriched text</bold>.
933 --[[image/gif][base64]]...image encoded in base64 here...
934 --[[audio/basic][base64]]...audio encoded in base64 here...
936 User customizable variables (not documented all of them):
938 Specifies a key prefix for MIME minor mode commands.
940 mime-ignore-preceding-spaces
941 Preceding white spaces in a message body are ignored if non-nil.
943 mime-ignore-trailing-spaces
944 Trailing white spaces in a message body are ignored if non-nil.
947 Hide a non-textual body message encoded in base64 after insertion
951 A number of network transfer level. It should be bigger than 7.
952 If you are in 8bit-through environment, please set to 8.
954 mime-edit-voice-recorder
955 Specifies a function to record a voice message and encode it.
956 The function `mime-edit-voice-recorder-for-sun' is for Sun
960 Turning on MIME mode calls the value of mime-edit-mode-hook, if
963 mime-edit-translate-hook
964 The value of mime-edit-translate-hook is called just before translating
965 the tagged MIME format into a MIME compliant message if it is
966 non-nil. If the hook call the function mime-edit-insert-signature,
967 the signature file will be inserted automatically.
970 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
973 (if mime-edit-mode-flag
975 (if mime-edit-touched-flag
977 (make-local-variable 'mime-edit-touched-flag)
978 (setq mime-edit-touched-flag t)
979 (turn-on-mime-edit))))
982 (static-if (featurep 'xemacs)
983 (add-minor-mode 'mime-edit-mode-flag
984 '((" MIME-Edit " mime-transfer-level-string))
988 (set-alist 'minor-mode-alist
990 '((" MIME-Edit " mime-transfer-level-string)))
991 (set-alist 'minor-mode-map-alist
997 (defun turn-on-mime-edit ()
998 "Unconditionally turn on MIME-Edit mode."
1000 (if mime-edit-mode-flag
1001 (error "You are already editing a MIME message")
1002 (setq mime-edit-mode-flag t)
1004 ;; Set transfer level into mode line
1006 (setq mime-transfer-level-string
1007 (mime-encoding-name mime-transfer-level 'not-omit))
1008 (force-mode-line-update)
1010 ;; Define menu for XEmacs.
1011 (if (featurep 'xemacs)
1012 (mime-edit-define-menu-for-xemacs))
1016 ;; I don't care about saving these.
1017 (setq paragraph-start
1018 (regexp-or mime-edit-single-part-tag-regexp
1020 (setq paragraph-separate
1021 (regexp-or mime-edit-single-part-tag-regexp
1022 paragraph-separate))
1023 (run-hooks 'mime-edit-mode-hook)
1025 (substitute-command-keys
1026 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
1029 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1032 (defun mime-edit-exit (&optional nomime no-error)
1033 "Translate the tagged MIME message into a MIME compliant message.
1034 When NOMIME is nil, encode a message in the buffer into MIME.
1035 Otherwise, just returns to previous mode. If NO-ERROR is non-nil,
1036 no errors will be signaled even if it is not MIME-Edit mode."
1038 (if (not mime-edit-mode-flag)
1040 (error "You aren't editing a MIME message"))
1043 (run-hooks 'mime-edit-translate-hook)
1044 (mime-edit-translate-buffer)))
1045 ;; Restore previous state.
1046 (setq mime-edit-mode-flag nil)
1047 (if (and (featurep 'xemacs)
1048 (featurep 'menubar))
1049 (delete-menu-item (list mime-edit-menu-title)))
1051 (set-buffer-modified-p (buffer-modified-p))
1052 (run-hooks 'mime-edit-exit-hook)
1053 (message "Exit MIME editor mode.")))
1055 (defun mime-edit-maybe-translate ()
1057 (mime-edit-exit nil t)
1058 (call-interactively 'mime-edit-maybe-split-and-send))
1060 (defun mime-edit-help ()
1061 "Show help message about MIME mode."
1063 (with-output-to-temp-buffer "*Help*"
1064 (princ "MIME editor mode:\n")
1065 (princ (documentation 'mime-edit-mode))
1066 (print-help-return-message)))
1068 (defun mime-edit-insert-text (&optional subtype)
1069 "Insert a text message.
1070 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1071 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1073 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1075 (if (looking-at mime-edit-single-part-tag-regexp)
1077 ;; Make a space between the following message.
1080 (if (and (member (cadr ret) '("enriched"))
1081 (fboundp 'enriched-mode))
1083 (if (boundp 'enriched-mode)
1084 (enriched-mode -1))))))
1086 (defun mime-edit-insert-file (file &optional verbose)
1087 "Insert a message from a FILE.
1088 If VERBOSE is non-nil, it will prompt for Content-Type,
1089 Content-Transfer-Encoding and Content-Disposition headers."
1090 (interactive "fInsert file as MIME message: \nP")
1091 (let* ((guess (mime-find-file-type file))
1092 (type (nth 0 guess))
1093 (subtype (nth 1 guess))
1094 (parameters (nth 2 guess))
1095 (encoding (nth 3 guess))
1096 (disposition-type (nth 4 guess))
1097 (disposition-params (nth 5 guess)))
1099 (setq type (mime-prompt-for-type type)
1100 subtype (mime-prompt-for-subtype type subtype)))
1101 (if (or (interactive-p) verbose)
1102 (setq encoding (mime-prompt-for-encoding encoding)))
1104 (setq disposition-type (mime-prompt-for-disposition disposition-type)))
1105 (if (or (consp parameters) (stringp disposition-type))
1106 (let ((rest parameters) cell attribute value)
1107 (setq parameters "")
1109 (setq cell (car rest))
1110 (setq attribute (car cell))
1111 (setq value (cdr cell))
1112 (if (eq value 'file)
1113 (setq value (std11-wrap-as-quoted-string
1114 (file-name-nondirectory file))))
1115 (setq parameters (concat parameters "; " attribute "=" value))
1116 (setq rest (cdr rest)))
1117 (if disposition-type
1120 (concat parameters "\n"
1121 "Content-Disposition: " disposition-type))
1122 (setq rest disposition-params)
1124 (setq cell (car rest))
1125 (setq attribute (car cell))
1126 (setq value (cdr cell))
1127 (if (eq value 'file)
1128 (setq value (std11-wrap-as-quoted-string
1129 (file-name-nondirectory file))))
1131 (concat parameters "; " attribute "=" value))
1132 (setq rest (cdr rest)))))))
1133 (mime-edit-insert-place
1135 (mime-edit-insert-tag type subtype parameters)
1136 (mime-edit-insert-binary-file file encoding))))
1138 (defun mime-edit-insert-external ()
1139 "Insert a reference to external body."
1141 (let* ((pritype (mime-prompt-for-type))
1142 (subtype (mime-prompt-for-subtype pritype))
1143 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1144 (mime-edit-insert-place
1145 '("message" "external-body")
1146 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1148 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1152 (insert "Content-Type: "
1153 pritype "/" subtype (or parameters "") "\n"))
1154 (if (and (not (eobp))
1155 (not (looking-at mime-edit-single-part-tag-regexp)))
1156 (insert (mime-make-text-tag) "\n")))))
1158 (defun mime-edit-insert-voice ()
1159 "Insert a voice message."
1163 "What transfer encoding: "
1164 (mime-encoding-alist) nil t nil)))
1165 (mime-edit-insert-place
1167 (mime-edit-insert-tag "audio" "basic" nil)
1168 (mime-edit-define-encoding encoding)
1170 (narrow-to-region (1- (point))(point))
1172 (funcall mime-edit-voice-recorder encoding)
1175 (invisible-region (point-min)(point-max))
1176 (goto-char (point-max))))))))
1178 (defun mime-edit-insert-signature (&optional arg)
1179 "Insert a signature file."
1181 (let ((signature-insert-hook
1184 (let ((items (mime-find-file-type signature-file-name)))
1185 (apply (function mime-edit-insert-tag)
1186 (car items) (cadr items) (list (caddr items))))))))
1187 (insert-signature arg)))
1190 ;; Insert a new tag around a point.
1192 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1193 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1194 If nothing is inserted, return nil."
1197 (mime-edit-goto-tag)
1198 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1199 (< (match-beginning 0) p)
1200 (< p (match-end 0)))
1201 (goto-char (match-beginning 0))
1208 (mime-prompt-for-type)))
1211 (mime-prompt-for-subtype pritype)))
1214 (mime-prompt-for-parameters pritype subtype delimiter)))
1215 ;; Make a new MIME tag.
1216 (setq newtag (mime-make-tag pritype subtype parameters))
1217 ;; Find an current MIME tag.
1220 (if (mime-edit-goto-tag)
1221 (buffer-substring (match-beginning 0) (match-end 0))
1222 ;; Assume content type is 'text/plan'.
1223 (mime-make-tag "text" "plain"))))
1224 ;; We are only interested in TEXT.
1226 (not (mime-test-content-type
1227 (mime-edit-get-contype oldtag) "text")))
1230 (if (or (not oldtag) ;Not text
1231 (or mime-ignore-same-text-tag
1232 (not (string-equal oldtag newtag))))
1234 ;; Mark the beginning of the tag for convenience.
1235 (push-mark (point) 'nomsg)
1236 (insert newtag "\n")
1237 (list pritype subtype parameters) ;New tag is created.
1239 ;; Restore previous point.
1241 nil ;Nothing is created.
1244 (defun mime-edit-insert-binary-file (file &optional encoding)
1245 "Insert binary FILE at point.
1246 Optional argument ENCODING specifies an encoding method such as base64."
1247 (let* ((tagend (1- (point))) ;End of the tag
1248 (hide-p (and mime-auto-hide-body
1251 (let ((en (downcase encoding)))
1252 (or (string-equal en "7bit")
1253 (string-equal en "8bit")
1254 (string-equal en "binary")))))))
1256 (narrow-to-region tagend (point))
1257 (mime-insert-encoded-file file encoding)
1260 (invisible-region (point-min) (point-max))
1261 (goto-char (point-max)))
1262 (goto-char (point-max))))
1264 (looking-at mime-edit-tag-regexp)
1265 (= (point)(point-max))
1266 (mime-edit-insert-tag "text" "plain"))
1267 ;; Define encoding even if it is 7bit.
1268 (if (stringp encoding)
1270 (goto-char tagend) ; Make sure which line the tag is on.
1271 (mime-edit-define-encoding encoding)))))
1274 ;; Commands work on a current message flagment.
1276 (defun mime-edit-goto-tag ()
1277 "Search for the beginning of the tagged MIME message."
1278 (let ((current (point)))
1279 (if (looking-at mime-edit-tag-regexp)
1281 ;; At first, go to the end.
1282 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1283 (goto-char (1- (match-beginning 0))) ;For multiline tag
1286 (goto-char (point-max))))
1287 ;; Then search for the beginning.
1288 (re-search-backward mime-edit-end-tag-regexp nil t)
1289 (or (looking-at mime-edit-beginning-tag-regexp)
1290 ;; Restore previous point.
1295 (defun mime-edit-content-beginning ()
1296 "Return the point of the beginning of content."
1298 (let ((beg (save-excursion
1299 (beginning-of-line) (point))))
1300 (if (mime-edit-goto-tag)
1301 (let ((top (point)))
1302 (goto-char (match-end 0))
1303 (if (and (= beg top)
1304 (= (following-char) ?\^M))
1308 ;; Default text/plain tag.
1309 (goto-char (point-min))
1311 (concat "\n" (regexp-quote mail-header-separator)
1312 (if mime-ignore-preceding-spaces
1313 "[ \t\n]*\n" "\n")) nil 'move)
1316 (defun mime-edit-content-end ()
1317 "Return the point of the end of content."
1319 (if (mime-edit-goto-tag)
1321 (goto-char (match-end 0))
1322 (if (invisible-p (point))
1323 (next-visible-point (point))
1324 ;; Move to the end of this text.
1325 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1326 ;; Don't forget a multiline tag.
1327 (goto-char (match-beginning 0)))
1329 ;; Assume the message begins with text/plain.
1330 (goto-char (mime-edit-content-beginning))
1331 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1332 ;; Don't forget a multiline tag.
1333 (goto-char (match-beginning 0)))
1336 (defun mime-edit-define-charset (charset)
1337 "Set charset of current tag to CHARSET."
1339 (if (mime-edit-goto-tag)
1340 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1341 (delete-region (match-beginning 0) (match-end 0))
1344 (mime-edit-set-parameter
1345 (mime-edit-get-contype tag)
1347 (let ((comment (get charset 'mime-charset-comment)))
1349 (concat (upcase (symbol-name charset)) " (" comment ")")
1350 (upcase (symbol-name charset)))))
1351 (mime-edit-get-encoding tag)))))))
1353 (defun mime-edit-define-encoding (encoding)
1354 "Set encoding of current tag to ENCODING."
1356 (if (mime-edit-goto-tag)
1357 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1358 (delete-region (match-beginning 0) (match-end 0))
1359 (insert (mime-create-tag (mime-edit-get-contype tag) encoding))))))
1361 (defun mime-edit-choose-charset ()
1362 "Choose charset of a text following current point."
1363 (detect-mime-charset-region (point) (mime-edit-content-end)))
1365 (defun mime-make-text-tag (&optional subtype)
1366 "Make a tag for a text after current point.
1367 Subtype of text type can be specified by an optional argument SUBTYPE.
1368 Otherwise, it is obtained from `mime-content-types'."
1369 (let* ((pritype "text")
1370 (subtype (or subtype
1371 (car (car (cdr (assoc pritype mime-content-types)))))))
1372 ;; Charset should be defined later.
1373 (mime-make-tag pritype subtype)))
1376 ;; Tag handling functions
1378 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1379 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1380 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1384 (defun mime-create-tag (contype &optional encoding)
1385 "Make a tag with CONTENT-TYPE and optional ENCODING."
1386 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1389 (defun mime-edit-get-contype (tag)
1390 "Return Content-Type (including parameters) of TAG."
1392 (or (string-match mime-edit-single-part-tag-regexp tag)
1393 (string-match mime-edit-multipart-beginning-regexp tag)
1394 (string-match mime-edit-multipart-end-regexp tag))
1395 (substring tag (match-beginning 1) (match-end 1))))
1397 (defun mime-edit-get-encoding (tag)
1398 "Return encoding of TAG."
1400 (string-match mime-edit-single-part-tag-regexp tag)
1402 (not (= (match-beginning 3) (match-end 3)))
1403 (substring tag (match-beginning 3) (match-end 3))))
1405 (defun mime-get-parameter (contype parameter)
1406 "For given CONTYPE return value for PARAMETER.
1407 Nil if no such parameter."
1411 (regexp-quote parameter)
1412 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1414 (substring contype (match-beginning 1) (match-end 1))
1415 nil ;No such parameter
1418 (defun mime-edit-set-parameter (contype parameter value)
1419 "For given CONTYPE set PARAMETER to VALUE."
1420 (let (ctype opt-fields)
1421 (if (string-match "\n[^ \t\n\r]+:" contype)
1422 (setq ctype (substring contype 0 (match-beginning 0))
1423 opt-fields (substring contype (match-beginning 0)))
1424 (setq ctype contype))
1428 (regexp-quote parameter)
1429 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1432 (concat (substring ctype 0 (match-beginning 1))
1434 (substring contype (match-end 1))
1436 (concat ctype "; " parameter "=" value opt-fields)
1439 (defun mime-strip-parameters (contype)
1440 "Return primary content-type and subtype without parameters for CONTYPE."
1441 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1442 (substring contype (match-beginning 1) (match-end 1)) nil))
1444 (defun mime-test-content-type (contype type &optional subtype)
1445 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1446 (and (stringp contype)
1449 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1450 (downcase contype))))
1455 (defun mime-find-file-type (file)
1456 "Guess Content-Type, subtype, and parameters from FILE."
1458 (guesses mime-file-types))
1459 (while (and (not guess) guesses)
1460 (if (string-match (car (car guesses)) file)
1461 (setq guess (cdr (car guesses))))
1462 (setq guesses (cdr guesses)))
1465 (defun mime-prompt-for-type (&optional default)
1466 "Ask for Content-type."
1468 ;; Repeat until primary content type is specified.
1469 (while (string-equal type "")
1471 (completing-read "What content type: "
1474 'require-match ;Type must be specified.
1476 (if (string-equal type "")
1478 (message "Content type is required.")
1483 (defun mime-prompt-for-subtype (type &optional default)
1484 "Ask for subtype of media-type TYPE."
1485 (let ((subtypes (cdr (assoc type mime-content-types))))
1487 (assoc default subtypes))
1488 (setq default (car (car subtypes)))))
1493 "What content subtype: (default " default ") ")
1494 "What content subtype: ")
1495 (cdr (assoc type mime-content-types))
1497 'require-match ;Subtype must be specified.
1499 (if (string-equal answer "") default answer)))
1501 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1502 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1503 Optional DELIMITER specifies parameter delimiter (';' by default)."
1504 (let* ((delimiter (or delimiter "; "))
1509 (mime-prompt-for-parameters-1
1511 (cdr (assoc pritype mime-content-types))))))
1513 (if (and (stringp parameters)
1514 (not (string-equal parameters "")))
1515 (concat delimiter parameters)
1516 "" ;"" if no parameters
1519 (defun mime-prompt-for-parameters-1 (optlist)
1520 (apply (function append)
1521 (mapcar (function mime-prompt-for-parameter) optlist)))
1523 (defun mime-prompt-for-parameter (parameter)
1525 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1526 (let* ((prompt (car parameter))
1527 (choices (mapcar (function
1529 (if (consp e) e (list e))))
1531 (default (car (car choices)))
1537 (concat "What " prompt
1539 (if (string-equal default "") "\"\"" default)
1541 choices nil nil ""))
1542 ;; If nothing is selected, use default.
1543 (if (string-equal answer "")
1544 (setq answer default)))
1546 (read-string (concat "What " prompt ": "))))
1547 (cons (if (and answer
1548 (not (string-equal answer "")))
1550 ;; Note: control characters ignored!
1551 (if (string-match mime-tspecials-regexp answer)
1552 (concat "\"" answer "\"") answer)))
1553 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))))
1555 (defun mime-prompt-for-encoding (default)
1556 "Ask for Content-Transfer-Encoding."
1561 "What transfer encoding: "
1562 (mime-encoding-alist) nil t default))
1566 (defun mime-prompt-for-disposition (default)
1567 "Prompt Content-Disposition"
1568 (completing-read (concat "What disposition type (default "
1570 mime-content-disposition-types
1575 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1578 (defvar mime-edit-translate-buffer-hook
1579 '(mime-edit-pgp-enclose-buffer
1580 mime-edit-translate-body
1581 mime-edit-translate-header))
1583 (defun mime-edit-translate-header ()
1584 "Encode the message header into network representation."
1585 (eword-encode-header 'code-conversion)
1586 (run-hooks 'mime-edit-translate-header-hook))
1588 (defun mime-edit-translate-buffer ()
1589 "Encode the tagged MIME message in current buffer in MIME compliant message."
1592 (if (catch 'mime-edit-error
1594 (run-hooks 'mime-edit-translate-buffer-hook)))
1597 (error "Translation error!"))))
1599 (defun mime-edit-find-inmost ()
1600 (goto-char (point-min))
1601 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1602 (let ((bb (match-beginning 0))
1604 (type (buffer-substring (match-beginning 1)(match-end 1)))
1606 (setq end-exp (format "--}-<<%s>>\n" type))
1608 (if (re-search-forward end-exp nil t)
1609 (setq eb (match-beginning 0))
1610 (setq eb (point-max)))
1611 (narrow-to-region be eb)
1613 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1615 (narrow-to-region (match-beginning 0)(point-max))
1616 (mime-edit-find-inmost))
1618 (list type bb be eb)))))
1620 (defun mime-edit-process-multipart-1 (boundary)
1621 (let ((ret (mime-edit-find-inmost)))
1623 (let ((type (car ret))
1624 (bb (nth 1 ret))(be (nth 2 ret))
1626 (narrow-to-region bb eb)
1627 (delete-region bb be)
1628 (setq bb (point-min))
1629 (setq eb (point-max))
1632 (if (looking-at mime-edit-multipart-end-regexp)
1633 (let ((beg (match-beginning 0))
1634 (end (match-end 0)))
1635 (delete-region beg end)
1636 (or (looking-at mime-edit-beginning-tag-regexp)
1638 (insert (concat (mime-make-text-tag) "\n")))))
1639 (cond ((string-equal type "quote")
1640 (mime-edit-enquote-region bb eb))
1641 ((string-equal type "pgp-signed")
1642 (mime-edit-sign-pgp-mime bb eb boundary))
1643 ((string-equal type "pgp-encrypted")
1644 (mime-edit-encrypt-pgp-mime bb eb boundary))
1645 ((string-equal type "kazu-signed")
1646 (mime-edit-sign-pgp-kazu bb eb boundary))
1647 ((string-equal type "kazu-encrypted")
1648 (mime-edit-encrypt-pgp-kazu bb eb boundary))
1649 ((string-equal type "smime-signed")
1650 (mime-edit-sign-smime bb eb boundary))
1651 ((string-equal type "smime-encrypted")
1652 (mime-edit-encrypt-smime bb eb boundary))
1655 (nth 2 (mime-edit-translate-region bb eb
1659 (format "--[[multipart/%s;
1660 boundary=\"%s\"][7bit]]\n"
1664 (defun mime-edit-enquote-region (beg end)
1667 (narrow-to-region beg end)
1669 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1670 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1671 (replace-match (concat "- " (substring tag 1))))))))
1673 (defun mime-edit-dequote-region (beg end)
1676 (narrow-to-region beg end)
1678 (while (re-search-forward
1679 mime-edit-quoted-single-part-tag-regexp nil t)
1680 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1681 (replace-match (concat "-" (substring tag 2))))))))
1683 (defvar mime-edit-pgp-user-id nil)
1685 (defun mime-edit-sign-pgp-mime (beg end boundary)
1688 (let* ((from (std11-field-body "From" mail-header-separator))
1690 (narrow-to-region beg end)
1691 (mime-edit-translate-region beg end boundary)))
1693 (encoding (nth 1 ret))
1694 (pgp-boundary (concat "pgp-sign-" boundary))
1697 (insert (format "Content-Type: %s\n" ctype))
1699 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1701 (or (let ((pgg-default-user-id
1702 (or mime-edit-pgp-user-id
1704 (nth 1 (std11-extract-address-components from))
1705 pgg-default-user-id))))
1706 (pgg-sign-region (point-min)(point-max)))
1707 (throw 'mime-edit-error 'pgp-error))
1709 (cdr (assq 'hash-algorithm
1710 (cdar (with-current-buffer pgg-output-buffer
1711 (pgg-parse-armor-region
1712 (point-min)(point-max))))))
1715 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1718 (insert (format "--[[multipart/signed;
1720 protocol=\"application/pgp-signature\"][7bit]]
1722 " pgp-boundary micalg pgp-boundary))
1723 (goto-char (point-max))
1724 (insert (format "\n--%s
1725 Content-Type: application/pgp-signature
1726 Content-Transfer-Encoding: 7bit
1729 (insert-buffer-substring pgg-output-buffer)
1730 (goto-char (point-max))
1731 (insert (format "\n--%s--\n" pgp-boundary))))))
1733 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1735 (defun mime-edit-make-encrypt-recipient-header ()
1736 (let* ((names mime-edit-encrypt-recipient-fields-list)
1738 (std11-field-bodies (cons "From" names)
1739 nil mail-header-separator))
1742 (setq values (cdr values))))
1743 (header (and (stringp from)
1744 (if (string-equal from "")
1746 (format "From: %s\n" from))))
1748 (while (and names values)
1749 (let ((name (car names))
1750 (value (car values)))
1751 (and (stringp value)
1752 (or (string-equal value "")
1754 (setq header (concat header name ": " value "\n")
1755 recipients (if recipients
1756 (concat recipients " ," value)
1758 (setq names (cdr names)
1759 values (cdr values)))
1760 (vector from recipients header)))
1762 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1765 (let (from recipients header)
1766 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1767 (setq from (aref ret 0)
1768 recipients (aref ret 1)
1769 header (aref ret 2)))
1770 (narrow-to-region beg end)
1772 (mime-edit-translate-region beg end boundary))
1774 (encoding (nth 1 ret))
1775 (pgp-boundary (concat "pgp-" boundary)))
1778 (insert (format "Content-Type: %s\n" ctype))
1780 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1782 (eword-encode-header)
1783 (or (let ((pgg-default-user-id
1784 (or mime-edit-pgp-user-id
1786 (nth 1 (std11-extract-address-components from))
1787 pgg-default-user-id))))
1789 (point-min) (point-max)
1790 (mapcar (lambda (recipient)
1791 (nth 1 (std11-extract-address-components
1793 (split-string recipients
1794 "\\([ \t\n]*,[ \t\n]*\\)+"))))
1795 (throw 'mime-edit-error 'pgp-error))
1796 (delete-region (point-min)(point-max))
1798 (insert (format "--[[multipart/encrypted;
1800 protocol=\"application/pgp-encrypted\"][7bit]]
1802 Content-Type: application/pgp-encrypted
1805 Content-Type: application/octet-stream
1806 Content-Transfer-Encoding: 7bit
1808 " pgp-boundary pgp-boundary pgp-boundary))
1809 (insert-buffer-substring pgg-output-buffer)
1810 (goto-char (point-max))
1811 (insert (format "\n--%s--\n" pgp-boundary)))))))
1813 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1816 (narrow-to-region beg end)
1818 (mime-edit-translate-region beg end boundary))
1820 (encoding (nth 1 ret)))
1822 (insert (format "Content-Type: %s\n" ctype))
1824 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1826 (or (pgg-sign-region beg (point-max) 'clearsign)
1827 (throw 'mime-edit-error 'pgp-error))
1830 "--[[application/pgp; format=mime][7bit]]\n")
1833 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1835 (let (recipients header)
1836 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1837 (setq recipients (aref ret 1)
1838 header (aref ret 2)))
1840 (narrow-to-region beg end)
1842 (mime-edit-translate-region beg end boundary))
1844 (encoding (nth 1 ret)))
1847 (insert (format "Content-Type: %s\n" ctype))
1849 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1851 (or (pgg-encrypt-region beg (point-max) recipients)
1852 (throw 'mime-edit-error 'pgp-error))
1855 "--[[application/pgp; format=mime][7bit]]\n")
1858 (defun mime-edit-sign-smime (beg end boundary)
1862 (narrow-to-region beg end)
1863 (mime-edit-translate-region beg end boundary)))
1865 (encoding (nth 1 ret))
1866 (smime-boundary (concat "smime-sign-" boundary)))
1868 (insert (format "Content-Type: %s\n" ctype))
1870 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1872 (let (buffer-undo-list)
1873 (goto-char (point-min))
1874 (while (progn (end-of-line) (not (eobp)))
1877 (or (prog1 (smime-sign-region (point-min)(point-max))
1878 (push nil buffer-undo-list)
1879 (ignore-errors (undo)))
1880 (throw 'mime-edit-error 'pgp-error)))
1882 (insert (format "--[[multipart/signed;
1883 boundary=\"%s\"; micalg=sha1;
1884 protocol=\"application/pkcs7-signature\"][7bit]]
1886 " smime-boundary smime-boundary))
1887 (goto-char (point-max))
1888 (insert (format "\n--%s
1889 Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
1890 Content-Transfer-Encoding: base64
1891 Content-Disposition: attachment; filename=\"smime.p7s\"
1892 Content-Description: S/MIME Cryptographic Signature
1895 (insert-buffer-substring smime-output-buffer)
1896 (goto-char (point-max))
1897 (insert (format "\n--%s--\n" smime-boundary))))))
1899 (defun mime-edit-encrypt-smime (beg end boundary)
1903 (narrow-to-region beg end)
1904 (mime-edit-translate-region beg end boundary)))
1906 (encoding (nth 1 ret)))
1908 (insert (format "Content-Type: %s\n" ctype))
1910 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1912 (goto-char (point-min))
1913 (while (progn (end-of-line) (not (eobp)))
1916 (or (smime-encrypt-region (point-min)(point-max))
1917 (throw 'mime-edit-error 'pgp-error))
1918 (delete-region (point-min)(point-max))
1919 (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
1920 Content-Disposition: attachment; filename=\"smime.p7m\"
1921 Content-Description: S/MIME Encrypted Message][base64]]\n")
1922 (insert-buffer-substring smime-output-buffer)))))
1924 (defsubst replace-space-with-underline (str)
1925 (mapconcat (function
1932 (defun mime-edit-make-boundary ()
1933 (concat mime-multipart-boundary "_"
1934 (replace-space-with-underline (current-time-string))))
1936 (defun mime-edit-translate-body ()
1937 "Encode the tagged MIME body in current buffer in MIME compliant message."
1940 (let ((boundary (mime-edit-make-boundary))
1943 (while (mime-edit-process-multipart-1
1944 (format "%s-%d" boundary i))
1947 ;; We are interested in message body.
1950 (goto-char (point-min))
1952 (concat "\n" (regexp-quote mail-header-separator)
1953 (if mime-ignore-preceding-spaces
1954 "[ \t\n]*\n" "\n")) nil 'move)
1958 (goto-char (point-max))
1959 (and mime-ignore-trailing-spaces
1960 (re-search-backward "[^ \t\n]\n" beg t)
1963 (setq ret (mime-edit-translate-region
1965 (format "%s-%d" boundary i)))))
1966 (mime-edit-dequote-region (point-min)(point-max))
1967 (let ((contype (car ret)) ;Content-Type
1968 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1970 ;; Insert User-Agent field
1971 (and mime-edit-insert-user-agent-field
1972 (or (mail-position-on-field "User-Agent")
1973 (insert mime-edit-user-agent-value)))
1974 ;; Make primary MIME headers.
1975 (or (mail-position-on-field "MIME-Version")
1976 (insert mime-edit-mime-version-value))
1977 ;; Remove old Content-Type and other fields.
1979 (goto-char (point-min))
1980 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1981 (narrow-to-region (point-min) (point))
1982 (goto-char (point-min))
1983 (mime-delete-field "Content-Type")
1984 (mime-delete-field "Content-Transfer-Encoding"))
1985 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1986 (mail-position-on-field "Content-Type")
1990 (mail-position-on-field "Content-Transfer-Encoding")
1991 (insert encoding)))))))
1993 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
1994 "Translate single-part-tag to MIME header."
1995 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1996 (let* ((beg (match-beginning 0))
1998 (tag (buffer-substring beg end)))
1999 (delete-region beg end)
2000 (let ((contype (mime-edit-get-contype tag))
2001 (encoding (mime-edit-get-encoding tag)))
2002 (insert (concat prefix "--" boundary "\n"))
2004 (narrow-to-region (point)(point))
2005 (insert "Content-Type: " contype "\n")
2007 (insert "Content-Transfer-Encoding: " encoding "\n"))
2008 (eword-encode-header))
2012 (downcase encoding)))))))
2014 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2016 (setq boundary (mime-edit-make-boundary)))
2019 (narrow-to-region beg end)
2020 (let ((tag nil) ;MIME tag
2021 (contype nil) ;Content-Type
2022 (encoding nil) ;Content-Transfer-Encoding
2023 (nparts 0)) ;Number of body parts
2024 ;; Normalize the body part by inserting appropriate message
2025 ;; tags for every message contents.
2026 (mime-edit-normalize-body)
2027 ;; Counting the number of Content-Type.
2028 (goto-char (point-min))
2029 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2030 (setq nparts (1+ nparts)))
2031 ;; Begin translation.
2033 ((and (<= nparts 1)(not multipart))
2034 ;; It's a singular message.
2035 (goto-char (point-min))
2036 (while (re-search-forward
2037 mime-edit-single-part-tag-regexp nil t)
2039 (buffer-substring (match-beginning 0) (match-end 0)))
2040 (delete-region (match-beginning 0) (1+ (match-end 0)))
2041 (setq contype (mime-edit-get-contype tag))
2042 (setq encoding (mime-edit-get-encoding tag))))
2044 ;; It's a multipart message.
2045 (goto-char (point-min))
2046 (let ((prio mime-content-transfer-encoding-priority-list)
2048 (when (setq part-info
2049 (mime-edit-translate-single-part-tag boundary))
2050 (and (setq nprio (member (cdr part-info) prio))
2052 (while (setq part-info
2053 (mime-edit-translate-single-part-tag boundary "\n"))
2054 (and (setq nprio (member (cdr part-info) prio))
2055 (setq prio nprio))))
2056 ;; Define Content-Type as "multipart/mixed".
2058 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2059 (setq encoding (car prio))
2060 ;; Insert the trailer.
2061 (goto-char (point-max))
2062 (insert "\n--" boundary "--\n"))))
2063 (list contype encoding boundary nparts)))))
2065 (defun mime-edit-normalize-body ()
2066 "Normalize the body part by inserting appropriate message tags."
2067 ;; Insert the first MIME tags if necessary.
2068 (goto-char (point-min))
2069 (if (not (looking-at mime-edit-single-part-tag-regexp))
2070 (insert (mime-make-text-tag) "\n"))
2071 ;; Check each tag, and add new tag or correct it if necessary.
2072 (goto-char (point-min))
2073 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2074 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2075 (contype (mime-edit-get-contype tag))
2076 (charset (mime-get-parameter contype "charset"))
2077 (encoding (mime-edit-get-encoding tag)))
2078 ;; Remove extra whitespaces after the tag.
2079 (if (looking-at "[ \t]+$")
2080 (delete-region (match-beginning 0) (match-end 0)))
2082 (end (mime-edit-content-end)))
2083 (if (= end (point-max))
2086 (or (looking-at mime-edit-beginning-tag-regexp)
2088 (insert (mime-make-text-tag) "\n")))
2089 (visible-region beg end)
2092 ((mime-test-content-type contype "message")
2093 ;; Content-type "message" should be sent as is.
2095 ((mime-test-content-type contype "text")
2096 ;; Define charset for text if necessary.
2097 (setq charset (if charset
2098 (intern (downcase charset))
2099 (mime-edit-choose-charset)))
2100 (mime-edit-define-charset charset)
2101 (cond ((string-equal contype "text/x-rot13-47-48")
2104 (mule-caesar-region (point) (mime-edit-content-end))))
2105 ((string-equal contype "text/enriched")
2110 (end (mime-edit-content-end)))
2111 ;; Patch for hard newlines
2114 ;; (while (search-forward "\n" end t)
2115 ;; (put-text-property (match-beginning 0)
2118 ;; End patch for hard newlines
2119 (enriched-encode beg end nil)
2121 (if (search-forward "\n\n")
2122 (delete-region beg (match-end 0)))))))
2123 ;; Point is now on current tag.
2124 ;; Define encoding and encode text if necessary.
2125 (or encoding ;Encoding is not specified.
2128 (let ((ret (cdr (assq charset mime-charset-type-list))))
2130 (setq bits (car ret)
2133 conv "quoted-printable")))
2134 (if (<= bits mime-transfer-level)
2135 (mime-encoding-name bits)
2137 (beg (mime-edit-content-beginning)))
2138 (encode-mime-charset-region beg (mime-edit-content-end)
2140 ;; Protect "From " in beginning of line
2142 (narrow-to-region beg (mime-edit-content-end))
2144 (let (case-fold-search)
2145 (if (re-search-forward "^From " nil t)
2147 (if (memq charset '(iso-2022-jp
2152 (replace-match "\e(BFrom ")
2153 (re-search-forward "^From " nil t)))
2154 (setq encoding "quoted-printable"))))))
2155 ;; canonicalize line break code
2156 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2158 (narrow-to-region beg (mime-edit-content-end))
2160 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2161 (replace-match "\\1\r\n"))))
2163 (mime-encode-region beg (mime-edit-content-end)
2164 (or encoding "7bit"))
2165 (mime-edit-define-encoding encoding)))
2166 (goto-char (mime-edit-content-end)))
2167 ((null encoding) ;Encoding is not specified.
2168 ;; Application, image, audio, video, and any other
2169 ;; unknown content-type without encoding should be
2171 (let* ((encoding "base64") ;Encode in BASE64 by default.
2172 (beg (mime-edit-content-beginning))
2173 (end (mime-edit-content-end)))
2174 (mime-encode-region beg end encoding)
2175 (mime-edit-define-encoding encoding))
2176 (forward-line 1))))))
2178 (defun mime-delete-field (field)
2179 "Delete header FIELD."
2180 (let ((regexp (format "^%s:[ \t]*" field)))
2181 (goto-char (point-min))
2182 (while (re-search-forward regexp nil t)
2183 (delete-region (match-beginning 0)
2184 (1+ (std11-field-end))))))
2188 ;;; Platform dependent functions
2191 ;; Sun implementations
2193 (defun mime-edit-voice-recorder-for-sun (encoding)
2194 "Record voice in a buffer using Sun audio device,
2195 and insert data encoded as ENCODING."
2196 (message "Start the recording on %s. Type C-g to finish the recording..."
2198 (mime-insert-encoded-file "/dev/audio" encoding))
2201 ;;; @ Other useful commands.
2204 ;; Message forwarding commands as content-type "message/rfc822".
2206 (defun mime-edit-insert-message (&optional message)
2208 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2209 (if (and inserter (fboundp inserter))
2211 (mime-edit-insert-place
2212 '("message" "rfc822")
2213 (mime-edit-insert-tag "message" "rfc822")
2214 (funcall inserter message)))
2215 (message "Sorry, I don't have message inserter for your MUA."))))
2217 (defun mime-edit-insert-mail (&optional message)
2219 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2220 (if (and inserter (fboundp inserter))
2222 (mime-edit-insert-place
2223 '("message" "rfc822")
2224 (mime-edit-insert-tag "message" "rfc822")
2225 (funcall inserter message)))
2226 (message "Sorry, I don't have mail inserter for your MUA."))))
2228 (defun mime-edit-inserted-message-filter ()
2231 (let ((header-start (point))
2232 (case-fold-search t)
2235 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2236 (if (re-search-forward "^$" (mark t))
2237 (narrow-to-region header-start (match-beginning 0)))
2238 (goto-char header-start)
2239 (while (and (re-search-forward
2241 (apply (function regexp-or)
2242 mime-edit-yank-ignored-field-list)
2244 (setq beg (match-beginning 0))
2245 (setq end (1+ (std11-field-end))))
2246 (delete-region beg end))))))
2249 ;;; @ multipart enclosure
2252 (defun mime-edit-enclose-region-internal (type beg end)
2256 (narrow-to-region beg end)
2257 (insert (format "--<<%s>>-{\n" type))
2258 (goto-char (point-max))
2259 (insert (format "--}-<<%s>>\n" type))
2260 (goto-char (point-max)))
2261 (or (looking-at mime-edit-beginning-tag-regexp)
2263 (insert (mime-make-text-tag) "\n"))))
2265 (defun mime-edit-enclose-quote-region (beg end)
2267 (mime-edit-enclose-region-internal 'quote beg end))
2269 (defun mime-edit-enclose-mixed-region (beg end)
2271 (mime-edit-enclose-region-internal 'mixed beg end))
2273 (defun mime-edit-enclose-parallel-region (beg end)
2275 (mime-edit-enclose-region-internal 'parallel beg end))
2277 (defun mime-edit-enclose-digest-region (beg end)
2279 (mime-edit-enclose-region-internal 'digest beg end))
2281 (defun mime-edit-enclose-alternative-region (beg end)
2283 (mime-edit-enclose-region-internal 'alternative beg end))
2285 (defun mime-edit-enclose-pgp-signed-region (beg end)
2287 (mime-edit-enclose-region-internal 'pgp-signed beg end))
2289 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2291 (mime-edit-enclose-region-internal 'pgp-encrypted beg end))
2293 (defun mime-edit-enclose-kazu-signed-region (beg end)
2295 (mime-edit-enclose-region-internal 'kazu-signed beg end))
2297 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2299 (mime-edit-enclose-region-internal 'kazu-encrypted beg end))
2301 (defun mime-edit-enclose-smime-signed-region (beg end)
2303 (mime-edit-enclose-region-internal 'smime-signed beg end))
2305 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2307 (mime-edit-enclose-region-internal 'smime-encrypted beg end))
2309 (defun mime-edit-insert-key (&optional arg)
2310 "Insert a pgp public key."
2312 (mime-edit-insert-tag "application" "pgp-keys")
2313 (mime-edit-define-encoding "7bit")
2315 (if (and (not (eobp))
2316 (not (looking-at mime-edit-single-part-tag-regexp)))
2317 (insert (mime-make-text-tag) "\n")))
2323 (defun mime-edit-set-split (arg)
2326 (y-or-n-p "Do you want to enable split? ")))
2327 (setq mime-edit-split-message arg)
2329 (message "This message is enabled to split.")
2330 (message "This message is not enabled to split.")))
2332 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2333 "Toggle transfer-level is 7bit or 8bit through.
2335 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2337 (if (numberp transfer-level)
2338 (setq mime-transfer-level transfer-level)
2339 (if (< mime-transfer-level 8)
2340 (setq mime-transfer-level 8)
2341 (setq mime-transfer-level 7)))
2342 (message (format "Current transfer-level is %d bit"
2343 mime-transfer-level))
2344 (setq mime-transfer-level-string
2345 (mime-encoding-name mime-transfer-level 'not-omit))
2346 (force-mode-line-update))
2348 (defun mime-edit-set-transfer-level-7bit ()
2350 (mime-edit-toggle-transfer-level 7))
2352 (defun mime-edit-set-transfer-level-8bit ()
2354 (mime-edit-toggle-transfer-level 8))
2360 (defvar mime-edit-pgp-processing nil)
2361 (make-variable-buffer-local 'mime-edit-pgp-processing)
2363 (defun mime-edit-set-sign (arg)
2366 (y-or-n-p "Do you want to sign? ")))
2369 (or (memq 'sign mime-edit-pgp-processing)
2370 (setq mime-edit-pgp-processing
2371 (nconc mime-edit-pgp-processing
2372 (copy-sequence '(sign)))))
2373 (message "This message will be signed."))
2374 (setq mime-edit-pgp-processing
2375 (delq 'sign mime-edit-pgp-processing))
2376 (message "This message will not be signed.")))
2378 (defun mime-edit-set-encrypt (arg)
2381 (y-or-n-p "Do you want to encrypt? ")))
2384 (or (memq 'encrypt mime-edit-pgp-processing)
2385 (setq mime-edit-pgp-processing
2386 (nconc mime-edit-pgp-processing
2387 (copy-sequence '(encrypt)))))
2388 (message "This message will be encrypt."))
2389 (setq mime-edit-pgp-processing
2390 (delq 'encrypt mime-edit-pgp-processing))
2391 (message "This message will not be encrypt.")))
2393 (defun mime-edit-pgp-enclose-buffer ()
2394 (let ((beg (save-excursion
2395 (goto-char (point-min))
2396 (if (search-forward (concat "\n" mail-header-separator "\n"))
2399 (dolist (pgp-processing mime-edit-pgp-processing)
2400 (case pgp-processing
2402 (mime-edit-enclose-pgp-signed-region
2405 (mime-edit-enclose-pgp-encrypted-region
2406 beg (point-max))))))))
2412 (defun mime-edit-insert-partial-header (fields subject
2413 id number total separator)
2415 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2416 (insert mime-edit-mime-version-field-for-message/partial)
2418 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2419 id number total separator)))
2421 (defun mime-edit-split-and-send
2422 (&optional cmd lines mime-edit-message-max-length)
2426 (count-lines (point-min) (point-max))))
2427 (or mime-edit-message-max-length
2428 (setq mime-edit-message-max-length
2429 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2430 mime-edit-message-default-max-lines)))
2432 ;; (mime-edit-draft-file-name
2433 ;; (or (buffer-file-name)
2435 ;; (expand-file-name "mime-draft" temporary-file-directory))))
2436 (separator mail-header-separator)
2438 (replace-space-with-underline (current-time-string))
2439 "@" (system-name) "\"")))
2440 (run-hooks 'mime-edit-before-split-hook)
2441 (let ((the-buf (current-buffer))
2442 (copy-buf (get-buffer-create " *Original Message*"))
2443 (header (std11-header-string-except
2444 mime-edit-split-ignored-field-regexp separator))
2445 (subject (mail-fetch-field "subject"))
2446 (total (+ (/ lines mime-edit-message-max-length)
2447 (if (> (mod lines mime-edit-message-max-length) 0)
2453 mime-edit-split-message-sender-alist))
2457 (error "Split sender is not specified for `%s'." major-mode)))))
2458 (mime-edit-partial-number 1)
2461 (set-buffer copy-buf)
2463 (insert-buffer the-buf)
2465 (if (re-search-forward
2466 (concat "^" (regexp-quote separator) "$") nil t)
2467 (let ((he (match-beginning 0)))
2469 (narrow-to-region (point-min) he)))
2470 (goto-char (point-min))
2471 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2472 (delete-region (match-beginning 0)
2473 (1+ (std11-field-end))))))
2474 (while (< mime-edit-partial-number total)
2477 (set-buffer copy-buf)
2478 (setq data (buffer-substring
2481 (goto-line mime-edit-message-max-length)
2483 (delete-region (point-min)(point)))
2484 (mime-edit-insert-partial-header
2485 header subject id mime-edit-partial-number total separator)
2488 (message (format "Sending %d/%d..."
2489 mime-edit-partial-number total))
2490 (call-interactively command)
2491 (message (format "Sending %d/%d... done"
2492 mime-edit-partial-number total)))
2493 (setq mime-edit-partial-number
2494 (1+ mime-edit-partial-number)))
2497 (set-buffer copy-buf)
2498 (setq data (buffer-string))
2500 (mime-edit-insert-partial-header
2501 header subject id mime-edit-partial-number total separator)
2504 (message (format "Sending %d/%d..."
2505 mime-edit-partial-number total))
2506 (message (format "Sending %d/%d... done"
2507 mime-edit-partial-number total))))))
2509 (defun mime-edit-maybe-split-and-send (&optional cmd)
2511 (run-hooks 'mime-edit-before-send-hook)
2512 (let ((mime-edit-message-max-length
2513 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2514 mime-edit-message-default-max-lines))
2515 (lines (count-lines (point-min) (point-max))))
2516 (if (and (> lines mime-edit-message-max-length)
2517 mime-edit-split-message)
2518 (mime-edit-split-and-send cmd lines mime-edit-message-max-length))))
2521 ;;; @ preview message
2524 (defvar mime-edit-buffer nil) ; buffer local variable
2526 (defun mime-edit-preview-message ()
2527 "preview editing MIME message."
2529 (let* ((str (buffer-string))
2530 (separator mail-header-separator)
2531 (the-buf (current-buffer))
2532 (buf-name (buffer-name))
2533 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2534 (buf (get-buffer temp-buf-name))
2535 (pgp-processing mime-edit-pgp-processing))
2538 (switch-to-buffer buf)
2540 (setq buf (get-buffer-create temp-buf-name))
2541 (switch-to-buffer buf))
2543 (setq major-mode 'mime-temp-message-mode)
2544 (make-local-variable 'mail-header-separator)
2545 (setq mail-header-separator separator)
2546 (make-local-variable 'mime-edit-buffer)
2547 (setq mime-edit-buffer the-buf)
2548 (setq mime-edit-pgp-processing pgp-processing)
2550 (run-hooks 'mime-edit-translate-hook)
2551 (mime-edit-translate-buffer)
2552 (goto-char (point-min))
2553 (if (re-search-forward
2554 (concat "^" (regexp-quote separator) "$"))
2556 (mime-view-buffer)))
2558 (defun mime-edit-quitting-method ()
2559 "Quitting method for mime-view."
2560 (let* ((entity (get-text-property (point-min) 'mime-view-entity))
2561 (temp (mime-entity-buffer entity))
2563 (mime-preview-kill-buffer)
2565 (setq buf mime-edit-buffer)
2567 (switch-to-buffer buf)))
2569 (set-alist 'mime-preview-quitting-method-alist
2570 'mime-temp-message-mode
2571 #'mime-edit-quitting-method)
2577 (defvar mime-edit-again-ignored-field-regexp
2578 (concat "^\\(" "Content-.*\\|Mime-Version"
2579 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2581 "Regexp for deleted header fields when `mime-edit-again' is called.")
2583 (defsubst eliminate-top-spaces (string)
2584 "Eliminate top sequence of space or tab in STRING."
2585 (if (string-match "^[ \t]+" string)
2586 (substring string (match-end 0))
2589 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2592 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2593 '(("application/pgp-encrypted" . pgp-encrypted)
2594 ("application/pgp-signature" . pgp-signed))))
2595 (mime-content-type-subtype content-type)))
2596 (boundary (mime-content-type-parameter content-type "boundary"))
2597 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2598 (re-search-forward boundary-pat nil t)
2599 (let ((bb (match-beginning 0)) eb tag)
2600 (setq tag (format "\n--<<%s>>-{\n" subtype))
2603 (setq bb (+ bb (length tag)))
2605 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2607 (setq eb (match-beginning 0))
2608 (replace-match (format "--}-<<%s>>\n" subtype))
2610 (narrow-to-region bb eb)
2611 (goto-char (point-min))
2612 (while (re-search-forward boundary-pat nil t)
2613 (let ((beg (match-beginning 0))
2615 (delete-region beg (match-end 0))
2617 (if (re-search-forward boundary-pat nil t)
2618 (setq end (match-beginning 0))
2619 (setq end (point-max)))
2621 (narrow-to-region beg end)
2623 ((eq subtype 'pgp-encrypted)
2626 (goto-char (point-min))
2627 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2630 (save-window-excursion
2631 (pgg-decrypt-region (match-beginning 0)
2633 (delete-region (point-min)(point-max))))
2634 (insert-buffer-substring pgg-output-buffer)
2635 (mime-edit-decode-message-in-buffer
2636 nil not-decode-text)
2637 (delete-region (goto-char (point-min))
2638 (if (search-forward "\n\n" nil t)
2641 (goto-char (point-max))))
2643 (mime-edit-decode-message-in-buffer
2644 (if (eq subtype 'digest)
2646 (make-mime-content-type 'message 'rfc822)))
2648 (goto-char (point-max))))))))))
2649 (goto-char (point-min))
2650 (or (= (point-min) 1)
2651 (delete-region (point-min)
2652 (if (search-forward "\n\n" nil t)
2656 (defun mime-edit-decode-single-part-in-buffer
2657 (content-type not-decode-text &optional content-disposition)
2658 (let* ((type (mime-content-type-primary-type content-type))
2659 (subtype (mime-content-type-subtype content-type))
2660 (ctype (format "%s/%s" type subtype))
2662 (pstr (let ((bytes (+ 14 (length ctype))))
2663 (mapconcat (function
2665 (if (string= (car attr) "charset")
2667 (setq charset (cdr attr))
2669 (let* ((str (concat (car attr)
2672 (setq bytes (+ bytes bs 2))
2675 (setq bytes (+ bs 1))
2678 (mime-content-type-parameters content-type) "")))
2681 (limit (save-excursion
2682 (if (search-forward "\n\n" nil t)
2685 (mime-content-disposition-type content-disposition))
2687 (if disposition-type
2688 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2689 (mapconcat (function
2694 (if (string-equal "filename"
2696 (std11-wrap-as-quoted-string
2700 (setq bytes (+ bytes bs 2))
2703 (setq bytes (+ bs 1))
2706 (mime-content-disposition-parameters
2707 content-disposition)
2709 (if disposition-type
2710 (setq pstr (format "%s\nContent-Disposition: %s%s"
2711 pstr disposition-type disposition-str)))
2713 (if (re-search-forward
2714 "^Content-Transfer-Encoding:" limit t)
2715 (let ((beg (match-beginning 0))
2716 (hbeg (match-end 0))
2717 (end (std11-field-end limit)))
2720 (eliminate-top-spaces
2721 (std11-unfold-string
2722 (buffer-substring hbeg end)))))
2723 (if (or charset (eq type 'text))
2725 (delete-region beg (1+ end))
2726 (goto-char (point-min))
2727 (if (search-forward "\n\n" nil t)
2730 (match-end 0)(point-max) encoding)
2732 encoding nil))))))))
2733 (if (or encoded (not not-decode-text))
2736 (goto-char (point-min))
2737 (while (re-search-forward "\r\n" nil t)
2738 (replace-match "\n")))
2739 (decode-mime-charset-region (point-min)(point-max)
2740 (or charset default-mime-charset))))
2741 (let ((he (if (re-search-forward "^$" nil t)
2744 (if (and (eq type 'text)
2745 (eq subtype 'x-rot13-47-48))
2746 (mule-caesar-region he (point-max)))
2747 (if (= (point-min) 1)
2753 (format "%s/%s%s" type subtype pstr)
2755 (delete-region (point-min) he)
2757 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2761 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2764 (goto-char (point-min))
2765 (let ((ctl (or (mime-read-Content-Type)
2766 default-content-type)))
2768 (let ((type (mime-content-type-primary-type ctl)))
2770 ((and (eq type 'application)
2771 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2772 (delete-region (point-min)(point-max)))
2773 ((eq type 'multipart)
2774 (mime-edit-decode-multipart-in-buffer ctl not-decode-text))
2776 (mime-edit-decode-single-part-in-buffer
2777 ctl not-decode-text (mime-read-Content-Disposition)))))
2779 (decode-mime-charset-region (point-min) (point-max)
2780 default-mime-charset)))
2781 (if (= (point-min) 1)
2784 (std11-narrow-to-header)
2785 (goto-char (point-min))
2786 (while (re-search-forward
2787 mime-edit-again-ignored-field-regexp nil t)
2788 (delete-region (match-beginning 0) (1+ (std11-field-end)))))
2789 (mime-decode-header-in-buffer (not not-decode-text)))))))
2792 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2793 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2794 Content-Type and Content-Transfer-Encoding header fields will be
2795 converted to MIME-Edit tags."
2797 (goto-char (point-min))
2799 (concat "\n" (regexp-quote mail-header-separator) "\n")
2801 (replace-match "\n\n"))
2802 (mime-edit-decode-message-in-buffer nil not-decode-text)
2803 (goto-char (point-min))
2805 (and (re-search-forward "^$")
2806 (replace-match mail-header-separator)))
2808 (turn-on-mime-edit)))
2814 (provide 'mime-edit)
2816 (run-hooks 'mime-edit-load-hook)
2818 ;;; mime-edit.el ends here