1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,1994,1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Maintainer: MORIOKA Tomohiko <morioka@jaist.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...
112 (require 'mail-utils)
115 (require 'eword-encode)
123 (defconst mime-edit-version-string
124 `,(concat (car mime-module-version) " "
125 (mapconcat #'number-to-string (cddr mime-module-version) ".")
126 " - \"" (cadr mime-module-version) "\""))
132 (defgroup mime-edit nil
136 (defcustom mime-ignore-preceding-spaces nil
137 "*Ignore preceding white spaces if non-nil."
141 (defcustom mime-ignore-trailing-spaces nil
142 "*Ignore trailing white spaces if non-nil."
146 (defcustom mime-ignore-same-text-tag t
147 "*Ignore preceding text content-type tag that is same with new one.
148 If non-nil, the text tag is not inserted unless something different."
152 (defcustom mime-auto-hide-body t
153 "*Hide non-textual body encoded in base64 after insertion if non-nil."
157 (defcustom mime-edit-voice-recorder
158 (function mime-edit-voice-recorder-for-sun)
159 "*Function to record a voice message and encode it."
163 (defcustom mime-edit-mode-hook nil
164 "*Hook called when enter MIME mode."
168 (defcustom mime-edit-translate-hook nil
169 "*Hook called before translating into a MIME compliant message.
170 To insert a signature file automatically, call the function
171 `mime-edit-insert-signature' from this hook."
175 (defcustom mime-edit-exit-hook nil
176 "*Hook called when exit MIME mode."
180 (defvar mime-content-types
182 ;; Charset parameter need not to be specified, since it is
183 ;; defined automatically while translation.
185 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
188 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
191 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
194 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
197 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
205 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
206 ("directory" "/pub/GNU/elisp/mime")
208 ("mode" "image" "ascii" "local8"))
213 ("mode" "image" "ascii" "local8"))
214 ("tftp" ("site") ("name"))
215 ("afs" ("site") ("name"))
216 ("local-file" ("site") ("name"))
217 ("mail-server" ("server" "ftpmail@nic.karrn.ad.jp"))
222 ("octet-stream" ("type" "" "tar" "shar"))
224 ("x-kiss" ("x-cnf")))
238 "*Alist of content-type, subtype, parameters and its values.")
240 (defcustom mime-file-types
244 "inline" (("filename" . file))
249 "inline" (("filename" . file))
252 "text" "richtext" nil
260 "application" "postscript" nil
262 "attachment" (("filename" . file))
267 "inline" (("filename" . file))
272 "inline" (("filename" . file))
277 "inline" (("filename" . file))
282 "inline" (("filename" . file))
287 "inline" (("filename" . file))
292 "inline" (("filename" . file))
297 "inline" (("filename" . file))
302 "inline" (("filename" . file))
307 "attachment" (("filename" . file))
312 "attachment" (("filename" . file))
315 "application" "octet-stream" (("type" . "emacs-lisp"))
317 "attachment" (("filename" . file))
320 "application" "octet-stream" (("type" . "common-lisp"))
322 "attachment" (("filename" . file))
325 "application" "octet-stream" (("type" . "tar+gzip"))
327 "attachment" (("filename" . file))
330 "application" "octet-stream" (("type" . "tar+gzip"))
332 "attachment" (("filename" . file))
335 "application" "octet-stream" (("type" . "tar+compress"))
337 "attachment" (("filename" . file))
340 "application" "octet-stream" (("type" . "tar+compress"))
342 "attachment" (("filename" . file))
345 "application" "octet-stream" (("type" . "gzip"))
347 "attachment" (("filename" . file))
350 "application" "octet-stream" (("type" . "compress"))
352 "attachment" (("filename" . file))
355 "application" "octet-stream" (("type" . "lha"))
357 "attachment" (("filename" . file))
360 "application" "zip" nil
362 "attachment" (("filename" . file))
365 "application" "octet-stream" (("type" . "patch"))
367 "attachment" (("filename" . file))
370 "application" "octet-stream" (("type" . "patch"))
372 "attachment" (("filename" . file))
375 "text" "plain" nil nil nil nil)
377 "application" "octet-stream" nil
379 "attachment" (("filename" . file)))
381 "*Alist of file name, types, parameters, and default encoding.
382 If encoding is nil, it is determined from its contents."
386 (choice :tag "Primary-Type"
387 ,@(nconc (mapcar (lambda (cell)
388 (list 'item (car cell))
393 (choice :tag "Sub-Type"
396 (mapcar (lambda (cell)
397 (mapcar (lambda (cell)
398 (list 'item (car cell))
404 (repeat :tag "Parameters of Content-Type field"
405 (cons string (choice string symbol)))
406 ;; content-transfer-encoding
407 (choice :tag "Encoding"
410 (mapcar (lambda (cell)
411 (list 'item (car cell))
413 mime-file-encoding-method-alist)))
415 (choice :tag "Disposition-Type"
421 (repeat :tag "Parameters of Content-Disposition field"
422 (cons string (choice string symbol)))
427 ;;; @@ about charset, encoding and transfer-level
430 (defvar mime-charset-type-list
432 (iso-8859-1 8 "quoted-printable")
433 (iso-8859-2 8 "quoted-printable")
434 (iso-8859-3 8 "quoted-printable")
435 (iso-8859-4 8 "quoted-printable")
436 (iso-8859-5 8 "quoted-printable")
437 (koi8-r 8 "quoted-printable")
438 (iso-8859-7 8 "quoted-printable")
439 (iso-8859-8 8 "quoted-printable")
440 (iso-8859-9 8 "quoted-printable")
441 (iso-2022-jp 7 "base64")
442 (iso-2022-kr 7 "base64")
444 (cn-gb2312 8 "quoted-printable")
446 (gb2312 8 "quoted-printable")
448 (iso-2022-jp-2 7 "base64")
449 (iso-2022-int-1 7 "base64")
452 (defvar mime-transfer-level 7
453 "*A number of network transfer level. It should be bigger than 7.")
454 (make-variable-buffer-local 'mime-transfer-level)
456 (defsubst mime-encoding-name (transfer-level &optional not-omit)
457 (cond ((> transfer-level 8) "binary")
458 ((= transfer-level 8) "8bit")
462 (defvar mime-transfer-level-string
463 (mime-encoding-name mime-transfer-level 'not-omit)
464 "A string formatted version of mime-transfer-level")
465 (make-variable-buffer-local 'mime-transfer-level-string)
467 (defun mime-make-charset-default-encoding-alist (transfer-level)
469 (lambda (charset-type)
470 (let ((charset (car charset-type))
471 (type (nth 1 charset-type))
472 (encoding (nth 2 charset-type))
474 (if (<= type transfer-level)
475 (cons charset (mime-encoding-name type))
476 (cons charset encoding)
478 mime-charset-type-list))
480 (defvar mime-edit-charset-default-encoding-alist
481 (mime-make-charset-default-encoding-alist mime-transfer-level))
482 (make-variable-buffer-local 'mime-edit-charset-default-encoding-alist)
485 ;;; @@ about message inserting
488 (defvar mime-edit-yank-ignored-field-list
489 '("Received" "Approved" "Path" "Replied" "Status"
490 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
491 "Delete these fields from original message when it is inserted
492 as message/rfc822 part.
493 Each elements are regexp of field-name.")
495 (defvar mime-edit-yank-ignored-field-regexp
497 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
500 (defvar mime-edit-message-inserter-alist nil)
501 (defvar mime-edit-mail-inserter-alist nil)
504 ;;; @@ about message splitting
507 (defcustom mime-edit-split-message t
508 "*Split large message if it is non-nil."
512 (defcustom mime-edit-message-default-max-lines 1000
513 "*Default maximum lines of a message."
517 (defcustom mime-edit-message-max-lines-alist
518 '((news-reply-mode . 500))
519 "Alist of major-mode vs maximum lines of a message.
520 If it is not specified for a major-mode,
521 `mime-edit-message-default-max-lines' is used."
525 (defconst mime-edit-split-ignored-field-regexp
526 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|Message-Id:\\)")
528 (defvar mime-edit-split-blind-field-regexp
529 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)")
531 (defvar mime-edit-split-message-sender-alist nil)
533 (defvar mime-edit-news-reply-mode-server-running nil)
539 (defvar mime-edit-signing-type 'pgp-mime
540 "*PGP signing type (pgp-mime, pgp-kazu or nil).")
542 (defvar mime-edit-encrypting-type 'pgp-mime
543 "*PGP encrypting type (pgp-mime, pgp-kazu or nil).")
549 (defconst mime-edit-single-part-tag-regexp
550 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
551 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
553 (defconst mime-edit-quoted-single-part-tag-regexp
554 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
556 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
558 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
560 (defconst mime-edit-beginning-tag-regexp
561 (regexp-or mime-edit-single-part-tag-regexp
562 mime-edit-multipart-beginning-regexp))
564 (defconst mime-edit-end-tag-regexp
565 (regexp-or mime-edit-single-part-tag-regexp
566 mime-edit-multipart-end-regexp))
568 (defconst mime-edit-tag-regexp
569 (regexp-or mime-edit-single-part-tag-regexp
570 mime-edit-multipart-beginning-regexp
571 mime-edit-multipart-end-regexp))
573 (defvar mime-tag-format "--[[%s]]"
574 "*Control-string making a MIME tag.")
576 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
577 "*Control-string making a MIME tag with encoding.")
580 ;;; @@ multipart boundary
583 (defvar mime-multipart-boundary "Multipart"
584 "*Boundary of a multipart message.")
587 ;;; @@ optional header fields
590 (defvar mime-edit-insert-x-emacs-field t
591 "*If non-nil, insert X-Emacs header field.")
593 (defvar mime-edit-x-emacs-value
594 (if (featurep 'xemacs)
595 (concat emacs-version (if (featurep 'mule)
598 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
599 (substring emacs-version 0 (match-beginning 0))
602 (if (boundp 'enable-multibyte-characters)
604 (if enable-multibyte-characters
605 (concat ", MULE " mule-version)
606 " (with raw setting)")
607 (if (featurep 'meadow)
608 (concat ", " (Meadow-version))
610 (concat "MULE " mule-version " based on Emacs " ver))
612 "Body of X-Emacs field.
613 If variable `mime-edit-insert-x-emacs-field' is not nil, it is
614 inserted into message header.")
620 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
621 "*Specify MIME tspecials.
622 Tspecials means any character that matches with it in header must be quoted.")
624 (defconst mime-edit-mime-version-value
625 (concat "1.0 (generated by " mime-edit-version-string ")")
626 "MIME version number.")
628 (defconst mime-edit-mime-version-field-for-message/partial
629 (concat "MIME-Version: 1.0 (split by " mime-edit-version-string ")\n")
630 "MIME version field for message/partial.")
633 ;;; @ keymap and menu
636 (defvar mime-edit-mode-flag nil)
637 (make-variable-buffer-local 'mime-edit-mode-flag)
639 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
640 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
641 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
642 "Keymap for MIME-Edit mode commands to insert entity or set status.")
644 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
645 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
646 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
647 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
648 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
649 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
650 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
651 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
652 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
653 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
655 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
656 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
657 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
658 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
659 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
660 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
661 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
662 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
663 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
664 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
666 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
667 "Keymap prefix for MIME-Edit mode commands about enclosure.")
668 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
669 "Keymap for MIME-Edit mode commands about enclosure.")
671 (define-key mime-edit-mode-enclosure-map
672 "\C-a" 'mime-edit-enclose-alternative-region)
673 (define-key mime-edit-mode-enclosure-map
674 "\C-p" 'mime-edit-enclose-parallel-region)
675 (define-key mime-edit-mode-enclosure-map
676 "\C-m" 'mime-edit-enclose-mixed-region)
677 (define-key mime-edit-mode-enclosure-map
678 "\C-d" 'mime-edit-enclose-digest-region)
679 (define-key mime-edit-mode-enclosure-map
680 "\C-s" 'mime-edit-enclose-signed-region)
681 (define-key mime-edit-mode-enclosure-map
682 "\C-e" 'mime-edit-enclose-encrypted-region)
683 (define-key mime-edit-mode-enclosure-map
684 "\C-q" 'mime-edit-enclose-quote-region)
686 (defvar mime-edit-mode-map (make-sparse-keymap)
687 "Keymap for MIME-Edit mode commands.")
688 (define-key mime-edit-mode-map
689 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
690 (define-key mime-edit-mode-map
691 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
693 (defconst mime-edit-menu-title "MIME-Edit")
695 (defconst mime-edit-menu-list
696 '((mime-help "Describe MIME editor mode" mime-edit-help)
697 (file "Insert File" mime-edit-insert-file)
698 (external "Insert External" mime-edit-insert-external)
699 (voice "Insert Voice" mime-edit-insert-voice)
700 (message "Insert Message" mime-edit-insert-message)
701 (mail "Insert Mail" mime-edit-insert-mail)
702 (signature "Insert Signature" mime-edit-insert-signature)
703 (text "Insert Text" mime-edit-insert-text)
704 (tag "Insert Tag" mime-edit-insert-tag)
705 (alternative "Enclose as alternative"
706 mime-edit-enclose-alternative-region)
707 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
708 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
709 (digest "Enclose as digest" mime-edit-enclose-digest-region)
710 (signed "Enclose as signed" mime-edit-enclose-signed-region)
711 (encrypted "Enclose as encrypted" mime-edit-enclose-encrypted-region)
712 (quote "Verbatim region" mime-edit-enclose-quote-region)
713 (key "Insert Public Key" mime-edit-insert-key)
714 (split "About split" mime-edit-set-split)
715 (sign "About sign" mime-edit-set-sign)
716 (encrypt "About encryption" mime-edit-set-encrypt)
717 (preview "Preview Message" mime-edit-preview-message)
718 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
720 "MIME-edit menubar entry.")
722 (cond (running-xemacs
723 ;; modified by Pekka Marjola <pema@iki.fi>
724 ;; 1995/9/5 (c.f. [tm-en:69])
725 (defun mime-edit-define-menu-for-xemacs ()
726 "Define menu for Emacs 19."
727 (cond ((featurep 'menubar)
728 (make-local-variable 'current-menubar)
729 (set-buffer-menubar current-menubar)
732 (cons mime-edit-menu-title
735 (vector (nth 1 item)(nth 2 item)
738 mime-edit-menu-list)))
741 ;; modified by Steven L. Baur <steve@miranova.com>
742 ;; 1995/12/6 (c.f. [tm-en:209])
743 (or (boundp 'mime-edit-popup-menu-for-xemacs)
744 (setq mime-edit-popup-menu-for-xemacs
745 (append '("MIME Commands" "---")
746 (mapcar (function (lambda (item)
750 mime-edit-menu-list)))
753 ((>= emacs-major-version 19)
754 (define-key mime-edit-mode-map [menu-bar mime-edit]
755 (cons mime-edit-menu-title
756 (make-sparse-keymap mime-edit-menu-title)))
759 (define-key mime-edit-mode-map
760 (vector 'menu-bar 'mime-edit (car item))
761 (cons (nth 1 item)(nth 2 item))
764 (reverse mime-edit-menu-list)
772 (defvar mime-edit-touched-flag nil)
775 (defun mime-edit-mode ()
776 "MIME minor mode for editing the tagged MIME message.
778 In this mode, basically, the message is composed in the tagged MIME
779 format. The message tag looks like:
781 --[[text/plain; charset=ISO-2022-JP][7bit]]
783 The tag specifies the MIME content type, subtype, optional parameters
784 and transfer encoding of the message following the tag. Messages
785 without any tag are treated as `text/plain' by default. Charset and
786 transfer encoding are automatically defined unless explicitly
787 specified. Binary messages such as audio and image are usually
788 hidden. The messages in the tagged MIME format are automatically
789 translated into a MIME compliant message when exiting this mode.
791 Available charsets depend on Emacs version being used. The following
792 lists the available charsets of each emacs.
794 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
795 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
796 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
797 CN-BIG5 and ISO-2022-INT-1 are available.
799 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
800 be used to represent multilingual text in intermixed manner. Any
801 languages that has no registered charset are represented as either
802 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
804 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
805 without mule, please set variable `default-mime-charset'. This
806 variable must be symbol of which name is a MIME charset.
808 If you want to add more charsets in mule, please set variable
809 `charsets-mime-charset-alist'. This variable must be alist of which
810 key is list of charset and value is symbol of MIME charset. If name
811 of coding-system is different as MIME charset, please set variable
812 `mime-charset-coding-system-alist'. This variable must be alist of
813 which key is MIME charset and value is coding-system.
815 Following commands are available in addition to major mode commands:
818 \\[mime-edit-insert-text] insert a text message.
819 \\[mime-edit-insert-file] insert a (binary) file.
820 \\[mime-edit-insert-external] insert a reference to external body.
821 \\[mime-edit-insert-voice] insert a voice message.
822 \\[mime-edit-insert-message] insert a mail or news message.
823 \\[mime-edit-insert-mail] insert a mail message.
824 \\[mime-edit-insert-signature] insert a signature file at end.
825 \\[mime-edit-insert-key] insert PGP public key.
826 \\[mime-edit-insert-tag] insert a new MIME tag.
828 \[make enclosure (maybe multipart)\]
829 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
830 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
831 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
832 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
833 \\[mime-edit-enclose-signed-region] enclose as PGP signed.
834 \\[mime-edit-enclose-encrypted-region] enclose as PGP encrypted.
835 \\[mime-edit-enclose-quote-region] enclose as verbose mode (to avoid to expand tags)
838 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
839 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
840 \\[mime-edit-set-split] set message splitting mode.
841 \\[mime-edit-set-sign] set PGP-sign mode.
842 \\[mime-edit-set-encrypt] set PGP-encryption mode.
843 \\[mime-edit-preview-message] preview editing MIME message.
844 \\[mime-edit-exit] exit and translate into a MIME compliant message.
845 \\[mime-edit-help] show this help.
846 \\[mime-edit-maybe-translate] exit and translate if in MIME mode, then split.
848 Additional commands are available in some major modes:
849 C-c C-c exit, translate and run the original command.
850 C-c C-s exit, translate and run the original command.
852 The following is a message example written in the tagged MIME format.
853 TABs at the beginning of the line are not a part of the message:
855 This is a conventional plain text. It should be translated
858 This is also a plain text. But, it is explicitly specified as
860 --[[text/plain; charset=ISO-8859-1]]
861 This is also a plain text. But charset is specified as
864 ¡Hola! Buenos días. ¿Cómo está usted?
866 This is a <bold>enriched text</bold>.
867 --[[image/gif][base64]]...image encoded in base64 here...
868 --[[audio/basic][base64]]...audio encoded in base64 here...
870 User customizable variables (not documented all of them):
872 Specifies a key prefix for MIME minor mode commands.
874 mime-ignore-preceding-spaces
875 Preceding white spaces in a message body are ignored if non-nil.
877 mime-ignore-trailing-spaces
878 Trailing white spaces in a message body are ignored if non-nil.
881 Hide a non-textual body message encoded in base64 after insertion
885 A number of network transfer level. It should be bigger than 7.
886 If you are in 8bit-through environment, please set 8.
888 mime-edit-voice-recorder
889 Specifies a function to record a voice message and encode it.
890 The function `mime-edit-voice-recorder-for-sun' is for Sun
894 Turning on MIME mode calls the value of mime-edit-mode-hook, if
897 mime-edit-translate-hook
898 The value of mime-edit-translate-hook is called just before translating
899 the tagged MIME format into a MIME compliant message if it is
900 non-nil. If the hook call the function mime-edit-insert-signature,
901 the signature file will be inserted automatically.
904 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
907 (if mime-edit-mode-flag
909 (if mime-edit-touched-flag
911 (make-local-variable 'mime-edit-touched-flag)
912 (setq mime-edit-touched-flag t)
917 (cond (running-xemacs
918 (add-minor-mode 'mime-edit-mode-flag
919 '((" MIME-Edit " mime-transfer-level-string))
925 (set-alist 'minor-mode-alist
927 '((" MIME-Edit " mime-transfer-level-string)))
928 (set-alist 'minor-mode-map-alist
935 (defun turn-on-mime-edit ()
936 "Unconditionally turn on MIME-Edit mode."
938 (if mime-edit-mode-flag
939 (error "You are already editing a MIME message.")
940 (setq mime-edit-mode-flag t)
942 ;; Set transfer level into mode line
944 (setq mime-transfer-level-string
945 (mime-encoding-name mime-transfer-level 'not-omit))
946 (force-mode-line-update)
948 ;; Define menu for XEmacs.
950 (mime-edit-define-menu-for-xemacs)
955 ;; I don't care about saving these.
956 (setq paragraph-start
957 (regexp-or mime-edit-single-part-tag-regexp
959 (setq paragraph-separate
960 (regexp-or mime-edit-single-part-tag-regexp
962 (run-hooks 'mime-edit-mode-hook)
964 (substitute-command-keys
965 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
969 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
972 (defun mime-edit-exit (&optional nomime no-error)
973 "Translate the tagged MIME message into a MIME compliant message.
974 With no argument encode a message in the buffer into MIME, otherwise
975 just return to previous mode."
977 (if (not mime-edit-mode-flag)
979 (error "You aren't editing a MIME message.")
983 (run-hooks 'mime-edit-translate-hook)
984 (mime-edit-translate-buffer)))
985 ;; Restore previous state.
986 (setq mime-edit-mode-flag nil)
987 (if (and running-xemacs
989 (delete-menu-item (list mime-edit-menu-title))
992 (set-buffer-modified-p (buffer-modified-p))
993 (run-hooks 'mime-edit-exit-hook)
994 (message "Exit MIME editor mode.")
997 (defun mime-edit-maybe-translate ()
999 (mime-edit-exit nil t)
1000 (call-interactively 'mime-edit-maybe-split-and-send)
1003 (defun mime-edit-help ()
1004 "Show help message about MIME mode."
1006 (with-output-to-temp-buffer "*Help*"
1007 (princ "MIME editor mode:\n")
1008 (princ (documentation 'mime-edit-mode))
1009 (print-help-return-message)))
1011 (defun mime-edit-insert-text (&optional subtype)
1012 "Insert a text message.
1013 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1014 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1016 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1018 (if (looking-at mime-edit-single-part-tag-regexp)
1020 ;; Make a space between the following message.
1024 (if (and (member (cadr ret) '("enriched" "richtext"))
1025 (fboundp 'enriched-mode)
1028 (if (boundp 'enriched-mode)
1033 (defun mime-edit-insert-file (file &optional verbose)
1034 "Insert a message from a file."
1035 (interactive "fInsert file as MIME message: \nP")
1036 (let* ((guess (mime-find-file-type file))
1037 (type (nth 0 guess))
1038 (subtype (nth 1 guess))
1039 (parameters (nth 2 guess))
1040 (encoding (nth 3 guess))
1041 (disposition-type (nth 4 guess))
1042 (disposition-params (nth 5 guess))
1045 (setq type (mime-prompt-for-type type)
1046 subtype (mime-prompt-for-subtype type subtype)
1048 (if (or (interactive-p) verbose)
1049 (setq encoding (mime-prompt-for-encoding encoding))
1051 (if (or (consp parameters) (stringp disposition-type))
1052 (let ((rest parameters) cell attribute value)
1053 (setq parameters "")
1055 (setq cell (car rest))
1056 (setq attribute (car cell))
1057 (setq value (cdr cell))
1058 (if (eq value 'file)
1059 (setq value (std11-wrap-as-quoted-string
1060 (file-name-nondirectory file)))
1062 (setq parameters (concat parameters "; " attribute "=" value))
1063 (setq rest (cdr rest))
1065 (if disposition-type
1068 (concat parameters "\n"
1069 "Content-Disposition: " disposition-type))
1070 (setq rest disposition-params)
1072 (setq cell (car rest))
1073 (setq attribute (car cell))
1074 (setq value (cdr cell))
1075 (if (eq value 'file)
1076 (setq value (std11-wrap-as-quoted-string
1077 (file-name-nondirectory file)))
1080 (concat parameters "; " attribute "=" value))
1081 (setq rest (cdr rest))
1085 (mime-edit-insert-tag type subtype parameters)
1086 (mime-edit-insert-binary-file file encoding)
1089 (defun mime-edit-insert-external ()
1090 "Insert a reference to external body."
1092 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1094 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1096 (let* ((pritype (mime-prompt-for-type))
1097 (subtype (mime-prompt-for-subtype pritype))
1098 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1101 (insert "Content-Type: "
1102 pritype "/" subtype (or parameters "") "\n")))
1103 (if (and (not (eobp))
1104 (not (looking-at mime-edit-single-part-tag-regexp)))
1105 (insert (mime-make-text-tag) "\n")))
1107 (defun mime-edit-insert-voice ()
1108 "Insert a voice message."
1112 "What transfer encoding: "
1113 mime-file-encoding-method-alist nil t nil)))
1114 (mime-edit-insert-tag "audio" "basic" nil)
1115 (mime-edit-define-encoding encoding)
1117 (narrow-to-region (1- (point))(point))
1119 (funcall mime-edit-voice-recorder encoding)
1122 (invisible-region (point-min)(point-max))
1123 (goto-char (point-max))
1126 (defun mime-edit-insert-signature (&optional arg)
1127 "Insert a signature file."
1129 (let ((signature-insert-hook
1132 (let ((items (mime-find-file-type signature-file-name)))
1133 (apply (function mime-edit-insert-tag)
1134 (car items) (cadr items) (list (caddr items))))
1137 (insert-signature arg)
1141 ;; Insert a new tag around a point.
1143 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1144 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1145 If nothing is inserted, return nil."
1148 (mime-edit-goto-tag)
1149 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1150 (< (match-beginning 0) p)
1153 (goto-char (match-beginning 0))
1162 (mime-prompt-for-type)))
1165 (mime-prompt-for-subtype pritype)))
1168 (mime-prompt-for-parameters pritype subtype delimiter)))
1169 ;; Make a new MIME tag.
1170 (setq newtag (mime-make-tag pritype subtype parameters))
1171 ;; Find an current MIME tag.
1174 (if (mime-edit-goto-tag)
1175 (buffer-substring (match-beginning 0) (match-end 0))
1176 ;; Assume content type is 'text/plan'.
1177 (mime-make-tag "text" "plain")
1179 ;; We are only interested in TEXT.
1181 (not (mime-test-content-type
1182 (mime-edit-get-contype oldtag) "text")))
1185 (if (or (not oldtag) ;Not text
1186 (or mime-ignore-same-text-tag
1187 (not (string-equal oldtag newtag))))
1189 ;; Mark the beginning of the tag for convenience.
1190 (push-mark (point) 'nomsg)
1191 (insert newtag "\n")
1192 (list pritype subtype parameters) ;New tag is created.
1194 ;; Restore previous point.
1196 nil ;Nothing is created.
1200 (defun mime-edit-insert-binary-file (file &optional encoding)
1201 "Insert binary FILE at point.
1202 Optional argument ENCODING specifies an encoding method such as base64."
1203 (let* ((tagend (1- (point))) ;End of the tag
1204 (hide-p (and mime-auto-hide-body
1207 (let ((en (downcase encoding)))
1208 (or (string-equal en "7bit")
1209 (string-equal en "8bit")
1210 (string-equal en "binary")
1214 (narrow-to-region tagend (point))
1215 (mime-insert-encoded-file file encoding)
1218 (invisible-region (point-min) (point-max))
1219 (goto-char (point-max))
1221 (goto-char (point-max))
1224 (looking-at mime-edit-tag-regexp)
1225 (= (point)(point-max))
1226 (mime-edit-insert-tag "text" "plain")
1228 ;; Define encoding even if it is 7bit.
1229 (if (stringp encoding)
1231 (goto-char tagend) ; Make sure which line the tag is on.
1232 (mime-edit-define-encoding encoding)
1237 ;; Commands work on a current message flagment.
1239 (defun mime-edit-goto-tag ()
1240 "Search for the beginning of the tagged MIME message."
1241 (let ((current (point)))
1242 (if (looking-at mime-edit-tag-regexp)
1244 ;; At first, go to the end.
1245 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1246 (goto-char (1- (match-beginning 0))) ;For multiline tag
1249 (goto-char (point-max))
1251 ;; Then search for the beginning.
1252 (re-search-backward mime-edit-end-tag-regexp nil t)
1253 (or (looking-at mime-edit-beginning-tag-regexp)
1254 ;; Restore previous point.
1261 (defun mime-edit-content-beginning ()
1262 "Return the point of the beginning of content."
1264 (let ((beg (save-excursion
1265 (beginning-of-line) (point))))
1266 (if (mime-edit-goto-tag)
1267 (let ((top (point)))
1268 (goto-char (match-end 0))
1269 (if (and (= beg top)
1270 (= (following-char) ?\^M))
1274 ;; Default text/plain tag.
1275 (goto-char (point-min))
1277 (concat "\n" (regexp-quote mail-header-separator)
1278 (if mime-ignore-preceding-spaces
1279 "[ \t\n]*\n" "\n")) nil 'move)
1283 (defun mime-edit-content-end ()
1284 "Return the point of the end of content."
1286 (if (mime-edit-goto-tag)
1288 (goto-char (match-end 0))
1289 (if (invisible-p (point))
1290 (next-visible-point (point))
1291 ;; Move to the end of this text.
1292 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1293 ;; Don't forget a multiline tag.
1294 (goto-char (match-beginning 0))
1298 ;; Assume the message begins with text/plain.
1299 (goto-char (mime-edit-content-beginning))
1300 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1301 ;; Don't forget a multiline tag.
1302 (goto-char (match-beginning 0)))
1306 (defun mime-edit-define-charset (charset)
1307 "Set charset of current tag to CHARSET."
1309 (if (mime-edit-goto-tag)
1310 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1311 (delete-region (match-beginning 0) (match-end 0))
1314 (mime-edit-set-parameter
1315 (mime-edit-get-contype tag)
1316 "charset" (upcase (symbol-name charset)))
1317 (mime-edit-get-encoding tag)))
1320 (defun mime-edit-define-encoding (encoding)
1321 "Set encoding of current tag to ENCODING."
1323 (if (mime-edit-goto-tag)
1324 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1325 (delete-region (match-beginning 0) (match-end 0))
1326 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1329 (defun mime-edit-choose-charset ()
1330 "Choose charset of a text following current point."
1331 (detect-mime-charset-region (point) (mime-edit-content-end))
1334 (defun mime-make-text-tag (&optional subtype)
1335 "Make a tag for a text after current point.
1336 Subtype of text type can be specified by an optional argument SUBTYPE.
1337 Otherwise, it is obtained from mime-content-types."
1338 (let* ((pritype "text")
1339 (subtype (or subtype
1340 (car (car (cdr (assoc pritype mime-content-types)))))))
1341 ;; Charset should be defined later.
1342 (mime-make-tag pritype subtype)))
1345 ;; Tag handling functions
1347 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1348 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1349 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1353 (defun mime-create-tag (contype &optional encoding)
1354 "Make a tag with CONTENT-TYPE and optional ENCODING."
1355 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1358 (defun mime-edit-get-contype (tag)
1359 "Return Content-Type (including parameters) of TAG."
1361 (or (string-match mime-edit-single-part-tag-regexp tag)
1362 (string-match mime-edit-multipart-beginning-regexp tag)
1363 (string-match mime-edit-multipart-end-regexp tag)
1365 (substring tag (match-beginning 1) (match-end 1))
1368 (defun mime-edit-get-encoding (tag)
1369 "Return encoding of TAG."
1371 (string-match mime-edit-single-part-tag-regexp tag)
1373 (not (= (match-beginning 3) (match-end 3)))
1374 (substring tag (match-beginning 3) (match-end 3))))
1376 (defun mime-get-parameter (contype parameter)
1377 "For given CONTYPE return value for PARAMETER.
1378 Nil if no such parameter."
1382 (regexp-quote parameter)
1383 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1385 (substring contype (match-beginning 1) (match-end 1))
1386 nil ;No such parameter
1389 (defun mime-edit-set-parameter (contype parameter value)
1390 "For given CONTYPE set PARAMETER to VALUE."
1391 (let (ctype opt-fields)
1392 (if (string-match "\n[^ \t\n\r]+:" contype)
1393 (setq ctype (substring contype 0 (match-beginning 0))
1394 opt-fields (substring contype (match-beginning 0)))
1395 (setq ctype contype)
1400 (regexp-quote parameter)
1401 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1404 (concat (substring ctype 0 (match-beginning 1))
1406 (substring contype (match-end 1))
1408 (concat ctype "; " parameter "=" value opt-fields)
1411 (defun mime-strip-parameters (contype)
1412 "Return primary content-type and subtype without parameters for CONTYPE."
1413 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1414 (substring contype (match-beginning 1) (match-end 1)) nil))
1416 (defun mime-test-content-type (contype type &optional subtype)
1417 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1418 (and (stringp contype)
1421 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1422 (downcase contype))))
1427 (defun mime-find-file-type (file)
1428 "Guess Content-Type, subtype, and parameters from FILE."
1430 (guesses mime-file-types))
1431 (while (and (not guess) guesses)
1432 (if (string-match (car (car guesses)) file)
1433 (setq guess (cdr (car guesses))))
1434 (setq guesses (cdr guesses)))
1438 (defun mime-prompt-for-type (&optional default)
1439 "Ask for Content-type."
1441 ;; Repeat until primary content type is specified.
1442 (while (string-equal type "")
1444 (completing-read "What content type: "
1447 'require-match ;Type must be specified.
1450 (if (string-equal type "")
1452 (message "Content type is required.")
1459 (defun mime-prompt-for-subtype (type &optional default)
1460 "Ask for subtype of media-type TYPE."
1461 (let ((subtypes (cdr (assoc type mime-content-types))))
1463 (assoc default subtypes))
1464 (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.
1477 (if (string-equal answer "") default answer)))
1479 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1480 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1481 Optional DELIMITER specifies parameter delimiter (';' by default)."
1482 (let* ((delimiter (or delimiter "; "))
1487 (mime-prompt-for-parameters-1
1489 (cdr (assoc pritype mime-content-types))))))
1492 (if (and (stringp parameters)
1493 (not (string-equal parameters "")))
1494 (concat delimiter parameters)
1495 "" ;"" if no parameters
1498 (defun mime-prompt-for-parameters-1 (optlist)
1499 (apply (function append)
1500 (mapcar (function mime-prompt-for-parameter) optlist)))
1502 (defun mime-prompt-for-parameter (parameter)
1504 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1505 (let* ((prompt (car parameter))
1506 (choices (mapcar (function
1508 (if (consp e) e (list e))))
1510 (default (car (car choices)))
1516 (concat "What " prompt
1518 (if (string-equal default "") "\"\"" default)
1520 choices nil nil ""))
1521 ;; If nothing is selected, use default.
1522 (if (string-equal answer "")
1523 (setq answer default)))
1525 (read-string (concat "What " prompt ": "))))
1526 (cons (if (and answer
1527 (not (string-equal answer "")))
1529 ;; Note: control characters ignored!
1530 (if (string-match mime-tspecials-regexp answer)
1531 (concat "\"" answer "\"") answer)))
1532 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1535 (defun mime-prompt-for-encoding (default)
1536 "Ask for Content-Transfer-Encoding."
1541 "What transfer encoding: "
1542 mime-file-encoding-method-alist nil t default)
1548 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1551 (defvar mime-edit-translate-buffer-hook
1552 '(mime-edit-pgp-enclose-buffer
1553 mime-edit-translate-body
1554 mime-edit-translate-header))
1556 (defun mime-edit-translate-header ()
1557 "Encode the message header into network representation."
1558 (eword-encode-header 'code-conversion)
1559 (run-hooks 'mime-edit-translate-header-hook)
1562 (defun mime-edit-translate-buffer ()
1563 "Encode the tagged MIME message in current buffer in MIME compliant message."
1565 (if (catch 'mime-edit-error
1567 (run-hooks 'mime-edit-translate-buffer-hook)
1571 (error "Translation error!")
1574 (defun mime-edit-find-inmost ()
1575 (goto-char (point-min))
1576 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1577 (let ((bb (match-beginning 0))
1579 (type (buffer-substring (match-beginning 1)(match-end 1)))
1581 (setq end-exp (format "--}-<<%s>>\n" type))
1583 (if (re-search-forward end-exp nil t)
1584 (setq eb (match-beginning 0))
1585 (setq eb (point-max))
1587 (narrow-to-region be eb)
1589 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1591 (narrow-to-region (match-beginning 0)(point-max))
1592 (mime-edit-find-inmost)
1595 (list type bb be eb)
1598 (defun mime-edit-process-multipart-1 (boundary)
1599 (let ((ret (mime-edit-find-inmost)))
1601 (let ((type (car ret))
1602 (bb (nth 1 ret))(be (nth 2 ret))
1605 (narrow-to-region bb eb)
1606 (delete-region bb be)
1607 (setq bb (point-min))
1608 (setq eb (point-max))
1611 (if (looking-at mime-edit-multipart-end-regexp)
1612 (let ((beg (match-beginning 0))
1615 (delete-region beg end)
1616 (or (looking-at mime-edit-beginning-tag-regexp)
1618 (insert (concat (mime-make-text-tag) "\n"))
1620 (cond ((string-equal type "quote")
1621 (mime-edit-enquote-region bb eb)
1623 ((string-equal type "signed")
1624 (cond ((eq mime-edit-signing-type 'pgp-mime)
1625 (mime-edit-sign-pgp-mime bb eb boundary)
1627 ((eq mime-edit-signing-type 'pgp-kazu)
1628 (mime-edit-sign-pgp-kazu bb eb boundary)
1631 ((string-equal type "encrypted")
1632 (cond ((eq mime-edit-encrypting-type 'pgp-mime)
1633 (mime-edit-encrypt-pgp-mime bb eb boundary)
1635 ((eq mime-edit-encrypting-type 'pgp-kazu)
1636 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1640 (nth 2 (mime-edit-translate-region bb eb
1644 (format "--[[multipart/%s;
1645 boundary=\"%s\"][7bit]]\n"
1650 (defun mime-edit-enquote-region (beg end)
1653 (narrow-to-region beg end)
1655 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1656 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1657 (replace-match (concat "- " (substring tag 1)))
1660 (defun mime-edit-dequote-region (beg end)
1663 (narrow-to-region beg end)
1665 (while (re-search-forward
1666 mime-edit-quoted-single-part-tag-regexp nil t)
1667 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1668 (replace-match (concat "-" (substring tag 2)))
1671 (defun mime-edit-sign-pgp-mime (beg end boundary)
1674 (narrow-to-region beg end)
1676 (mime-edit-translate-region beg end boundary))
1678 (encoding (nth 1 ret))
1679 (pgp-boundary (concat "pgp-sign-" boundary)))
1681 (insert (format "Content-Type: %s\n" ctype))
1683 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1686 (or (as-binary-process
1687 (funcall (pgp-function 'mime-sign)
1688 (point-min)(point-max) nil nil pgp-boundary))
1689 (throw 'mime-edit-error 'pgp-error)
1693 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1695 (defun mime-edit-make-encrypt-recipient-header ()
1696 (let* ((names mime-edit-encrypt-recipient-fields-list)
1698 (std11-field-bodies (cons "From" names)
1699 nil mail-header-separator))
1702 (setq values (cdr values))))
1703 (header (and (stringp from)
1704 (if (string-equal from "")
1706 (format "From: %s\n" from)
1709 (while (and names values)
1710 (let ((name (car names))
1711 (value (car values))
1713 (and (stringp value)
1714 (or (string-equal value "")
1716 (setq header (concat header name ": " value "\n")
1717 recipients (if recipients
1718 (concat recipients " ," value)
1721 (setq names (cdr names)
1722 values (cdr values))
1724 (vector from recipients header)
1727 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1730 (let (from recipients header)
1731 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1732 (setq from (aref ret 0)
1733 recipients (aref ret 1)
1734 header (aref ret 2))
1736 (narrow-to-region beg end)
1738 (mime-edit-translate-region beg end boundary))
1740 (encoding (nth 1 ret))
1741 (pgp-boundary (concat "pgp-" boundary)))
1744 (insert (format "Content-Type: %s\n" ctype))
1746 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1749 (or (funcall (pgp-function 'encrypt)
1750 recipients (point-min) (point-max) from)
1751 (throw 'mime-edit-error 'pgp-error)
1754 (insert (format "--[[multipart/encrypted;
1756 protocol=\"application/pgp-encrypted\"][7bit]]
1758 Content-Type: application/pgp-encrypted
1761 Content-Type: application/octet-stream
1762 Content-Transfer-Encoding: 7bit
1764 " pgp-boundary pgp-boundary pgp-boundary))
1765 (goto-char (point-max))
1766 (insert (format "\n--%s--\n" pgp-boundary))
1769 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1772 (narrow-to-region beg end)
1774 (mime-edit-translate-region beg end boundary))
1776 (encoding (nth 1 ret))
1780 (insert (format "Content-Type: %s\n" ctype))
1782 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1785 (or (as-binary-process
1786 (funcall (pgp-function 'traditional-sign)
1788 (throw 'mime-edit-error 'pgp-error)
1792 "--[[application/pgp; format=mime][7bit]]\n")
1796 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1798 (let (from recipients header)
1799 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1800 (setq from (aref ret 0)
1801 recipients (aref ret 1)
1802 header (aref ret 2))
1805 (narrow-to-region beg end)
1807 (mime-edit-translate-region beg end boundary))
1809 (encoding (nth 1 ret))
1814 (insert (format "Content-Type: %s\n" ctype))
1816 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1819 (or (as-binary-process
1820 (funcall (pgp-function 'encrypt)
1821 recipients beg (point-max) nil 'maybe)
1823 (throw 'mime-edit-error 'pgp-error)
1827 "--[[application/pgp; format=mime][7bit]]\n")
1831 (defsubst replace-space-with-underline (str)
1832 (mapconcat (function
1840 (defun mime-edit-make-boundary ()
1841 (concat mime-multipart-boundary "_"
1842 (replace-space-with-underline (current-time-string))
1845 (defun mime-edit-translate-body ()
1846 "Encode the tagged MIME body in current buffer in MIME compliant message."
1849 (let ((boundary (mime-edit-make-boundary))
1852 (while (mime-edit-process-multipart-1
1853 (format "%s-%d" boundary i))
1857 ;; We are interested in message body.
1860 (goto-char (point-min))
1862 (concat "\n" (regexp-quote mail-header-separator)
1863 (if mime-ignore-preceding-spaces
1864 "[ \t\n]*\n" "\n")) nil 'move)
1868 (goto-char (point-max))
1869 (and mime-ignore-trailing-spaces
1870 (re-search-backward "[^ \t\n]\n" beg t)
1873 (setq ret (mime-edit-translate-region
1875 (format "%s-%d" boundary i)))
1877 (mime-edit-dequote-region (point-min)(point-max))
1878 (let ((contype (car ret)) ;Content-Type
1879 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1881 ;; Insert X-Emacs field
1882 (and mime-edit-insert-x-emacs-field
1883 (or (mail-position-on-field "X-Emacs")
1884 (insert mime-edit-x-emacs-value)
1886 ;; Make primary MIME headers.
1887 (or (mail-position-on-field "MIME-Version")
1888 (insert mime-edit-mime-version-value))
1889 ;; Remove old Content-Type and other fields.
1891 (goto-char (point-min))
1892 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1893 (narrow-to-region (point-min) (point))
1894 (goto-char (point-min))
1895 (mime-delete-field "Content-Type")
1896 (mime-delete-field "Content-Transfer-Encoding"))
1897 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1898 (mail-position-on-field "Content-Type")
1902 (mail-position-on-field "Content-Transfer-Encoding")
1906 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
1907 "Translate single-part-tag to MIME header."
1908 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1909 (let* ((beg (match-beginning 0))
1911 (tag (buffer-substring beg end)))
1912 (delete-region beg end)
1913 (let ((contype (mime-edit-get-contype tag))
1914 (encoding (mime-edit-get-encoding tag)))
1915 (insert (concat prefix "--" boundary "\n"))
1917 (narrow-to-region (point)(point))
1918 (insert "Content-Type: " contype "\n")
1920 (insert "Content-Transfer-Encoding: " encoding "\n"))
1921 (eword-encode-header)
1925 (defun mime-edit-translate-region (beg end &optional boundary multipart)
1927 (setq boundary (mime-edit-make-boundary))
1931 (narrow-to-region beg end)
1932 (let ((tag nil) ;MIME tag
1933 (contype nil) ;Content-Type
1934 (encoding nil) ;Content-Transfer-Encoding
1935 (nparts 0)) ;Number of body parts
1936 ;; Normalize the body part by inserting appropriate message
1937 ;; tags for every message contents.
1938 (mime-edit-normalize-body)
1939 ;; Counting the number of Content-Type.
1940 (goto-char (point-min))
1941 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1942 (setq nparts (1+ nparts)))
1943 ;; Begin translation.
1945 ((and (<= nparts 1)(not multipart))
1946 ;; It's a singular message.
1947 (goto-char (point-min))
1948 (while (re-search-forward
1949 mime-edit-single-part-tag-regexp nil t)
1951 (buffer-substring (match-beginning 0) (match-end 0)))
1952 (delete-region (match-beginning 0) (1+ (match-end 0)))
1953 (setq contype (mime-edit-get-contype tag))
1954 (setq encoding (mime-edit-get-encoding tag))
1957 ;; It's a multipart message.
1958 (goto-char (point-min))
1959 (and (mime-edit-translate-single-part-tag boundary)
1960 (while (mime-edit-translate-single-part-tag boundary "\n")))
1961 ;; Define Content-Type as "multipart/mixed".
1963 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
1964 ;; Content-Transfer-Encoding must be "7bit".
1965 ;; The following encoding can be `nil', but is
1966 ;; specified as is since there is no way that a user
1968 (setq encoding "7bit")
1969 ;; Insert the trailer.
1970 (goto-char (point-max))
1971 (insert "\n--" boundary "--\n")
1973 (list contype encoding boundary nparts)
1976 (defun mime-edit-normalize-body ()
1977 "Normalize the body part by inserting appropriate message tags."
1978 ;; Insert the first MIME tags if necessary.
1979 (goto-char (point-min))
1980 (if (not (looking-at mime-edit-single-part-tag-regexp))
1981 (insert (mime-make-text-tag) "\n"))
1982 ;; Check each tag, and add new tag or correct it if necessary.
1983 (goto-char (point-min))
1984 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1985 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
1986 (contype (mime-edit-get-contype tag))
1987 (charset (mime-get-parameter contype "charset"))
1988 (encoding (mime-edit-get-encoding tag)))
1989 ;; Remove extra whitespaces after the tag.
1990 (if (looking-at "[ \t]+$")
1991 (delete-region (match-beginning 0) (match-end 0)))
1993 (end (mime-edit-content-end))
1995 (if (= end (point-max))
1998 (or (looking-at mime-edit-beginning-tag-regexp)
2000 (insert (mime-make-text-tag) "\n")
2002 (visible-region beg end)
2006 ((mime-test-content-type contype "message")
2007 ;; Content-type "message" should be sent as is.
2010 ((mime-test-content-type contype "text")
2011 ;; Define charset for text if necessary.
2012 (setq charset (if charset
2013 (intern (downcase charset))
2014 (mime-edit-choose-charset)))
2015 (mime-edit-define-charset charset)
2016 (cond ((string-equal contype "text/x-rot13-47-48")
2019 (mule-caesar-region (point) (mime-edit-content-end))
2021 ((string-equal contype "text/enriched")
2026 (end (mime-edit-content-end))
2028 ;; Patch for hard newlines
2031 ;; (while (search-forward "\n" end t)
2032 ;; (put-text-property (match-beginning 0)
2035 ;; End patch for hard newlines
2036 (enriched-encode beg end)
2038 (if (search-forward "\n\n")
2039 (delete-region beg (match-end 0))
2042 ;; Point is now on current tag.
2043 ;; Define encoding and encode text if necessary.
2044 (or encoding ;Encoding is not specified.
2048 mime-edit-charset-default-encoding-alist)
2050 (beg (mime-edit-content-beginning))
2052 (encode-mime-charset-region beg (mime-edit-content-end)
2054 ;; Protect "From " in beginning of line
2056 (narrow-to-region beg (mime-edit-content-end))
2058 (let (case-fold-search)
2059 (if (re-search-forward "^From " nil t)
2061 (if (memq charset '(iso-2022-jp
2066 (replace-match "\e(BFrom ")
2067 (re-search-forward "^From " nil t)
2069 (setq encoding "quoted-printable")
2071 ;; canonicalize line break code
2072 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2074 (narrow-to-region beg (mime-edit-content-end))
2076 (while (re-search-forward "\\([^\r]\\)\n" nil t)
2078 (concat (buffer-substring (match-beginning 0)
2079 (match-end 1)) "\r\n"))
2082 (mime-encode-region beg (mime-edit-content-end) encoding)
2083 (mime-edit-define-encoding encoding)
2085 (goto-char (mime-edit-content-end))
2087 ((null encoding) ;Encoding is not specified.
2088 ;; Application, image, audio, video, and any other
2089 ;; unknown content-type without encoding should be
2091 (let* ((encoding "base64") ;Encode in BASE64 by default.
2092 (beg (mime-edit-content-beginning))
2093 (end (mime-edit-content-end)))
2094 (mime-encode-region beg end encoding)
2095 (mime-edit-define-encoding encoding))
2100 (defun mime-delete-field (field)
2101 "Delete header FIELD."
2102 (let ((regexp (format "^%s:[ \t]*" field)))
2103 (goto-char (point-min))
2104 (while (re-search-forward regexp nil t)
2105 (delete-region (match-beginning 0)
2106 (progn (forward-line 1) (point)))
2111 ;;; Platform dependent functions
2114 ;; Sun implementations
2116 (defun mime-edit-voice-recorder-for-sun (encoding)
2117 "Record voice in a buffer using Sun audio device,
2118 and insert data encoded as ENCODING."
2119 (message "Start the recording on %s. Type C-g to finish the recording..."
2121 (mime-insert-encoded-file "/dev/audio" encoding)
2125 ;;; @ Other useful commands.
2128 ;; Message forwarding commands as content-type "message/rfc822".
2130 (defun mime-edit-insert-message (&optional message)
2132 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2133 (if (and inserter (fboundp inserter))
2135 (mime-edit-insert-tag "message" "rfc822")
2136 (funcall inserter message)
2138 (message "Sorry, I don't have message inserter for your MUA.")
2141 (defun mime-edit-insert-mail (&optional message)
2143 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2144 (if (and inserter (fboundp inserter))
2146 (mime-edit-insert-tag "message" "rfc822")
2147 (funcall inserter message)
2149 (message "Sorry, I don't have mail inserter for your MUA.")
2152 (defun mime-edit-inserted-message-filter ()
2155 (let ((header-start (point))
2156 (case-fold-search t)
2159 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2160 (if (re-search-forward "^$" (mark t))
2161 (narrow-to-region header-start (match-beginning 0))
2163 (goto-char header-start)
2164 (while (and (re-search-forward
2165 mime-edit-yank-ignored-field-regexp nil t)
2166 (setq beg (match-beginning 0))
2167 (setq end (1+ (std11-field-end)))
2169 (delete-region beg end)
2174 ;;; @ multipart enclosure
2177 (defun mime-edit-enclose-region-internal (type beg end)
2180 (let ((current (point)))
2182 (narrow-to-region beg end)
2183 (insert (format "--<<%s>>-{\n" type))
2184 (goto-char (point-max))
2185 (insert (format "--}-<<%s>>\n" type))
2186 (goto-char (point-max))
2188 (or (looking-at mime-edit-beginning-tag-regexp)
2190 (insert (mime-make-text-tag) "\n")
2194 (defun mime-edit-enclose-quote-region (beg end)
2196 (mime-edit-enclose-region-internal 'quote beg end)
2199 (defun mime-edit-enclose-mixed-region (beg end)
2201 (mime-edit-enclose-region-internal 'mixed beg end)
2204 (defun mime-edit-enclose-parallel-region (beg end)
2206 (mime-edit-enclose-region-internal 'parallel beg end)
2209 (defun mime-edit-enclose-digest-region (beg end)
2211 (mime-edit-enclose-region-internal 'digest beg end)
2214 (defun mime-edit-enclose-alternative-region (beg end)
2216 (mime-edit-enclose-region-internal 'alternative beg end)
2219 (defun mime-edit-enclose-signed-region (beg end)
2221 (if mime-edit-signing-type
2222 (mime-edit-enclose-region-internal 'signed beg end)
2223 (message "Please specify signing type.")
2226 (defun mime-edit-enclose-encrypted-region (beg end)
2228 (if mime-edit-signing-type
2229 (mime-edit-enclose-region-internal 'encrypted beg end)
2230 (message "Please specify encrypting type.")
2233 (defun mime-edit-insert-key (&optional arg)
2234 "Insert a pgp public key."
2236 (mime-edit-insert-tag "application" "pgp-keys")
2237 (mime-edit-define-encoding "7bit")
2238 (funcall (pgp-function 'insert-key))
2245 (defun mime-edit-set-split (arg)
2248 (y-or-n-p "Do you want to enable split? ")
2250 (setq mime-edit-split-message arg)
2252 (message "This message is enabled to split.")
2253 (message "This message is not enabled to split.")
2256 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2257 "Toggle transfer-level is 7bit or 8bit through.
2259 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2261 (if (numberp transfer-level)
2262 (setq mime-transfer-level transfer-level)
2263 (if (< mime-transfer-level 8)
2264 (setq mime-transfer-level 8)
2265 (setq mime-transfer-level 7)
2267 (setq mime-edit-charset-default-encoding-alist
2268 (mime-make-charset-default-encoding-alist mime-transfer-level))
2269 (message (format "Current transfer-level is %d bit"
2270 mime-transfer-level))
2271 (setq mime-transfer-level-string
2272 (mime-encoding-name mime-transfer-level 'not-omit))
2273 (force-mode-line-update)
2276 (defun mime-edit-set-transfer-level-7bit ()
2278 (mime-edit-toggle-transfer-level 7)
2281 (defun mime-edit-set-transfer-level-8bit ()
2283 (mime-edit-toggle-transfer-level 8)
2290 (defun mime-edit-set-sign (arg)
2293 (y-or-n-p "Do you want to sign? ")
2296 (if mime-edit-signing-type
2298 (setq mime-edit-pgp-processing 'sign)
2299 (message "This message will be signed.")
2301 (message "Please specify signing type.")
2303 (if (eq mime-edit-pgp-processing 'sign)
2304 (setq mime-edit-pgp-processing nil)
2306 (message "This message will not be signed.")
2309 (defun mime-edit-set-encrypt (arg)
2312 (y-or-n-p "Do you want to encrypt? ")
2315 (if mime-edit-encrypting-type
2317 (setq mime-edit-pgp-processing 'encrypt)
2318 (message "This message will be encrypt.")
2320 (message "Please specify encrypting type.")
2322 (if (eq mime-edit-pgp-processing 'encrypt)
2323 (setq mime-edit-pgp-processing nil)
2325 (message "This message will not be encrypt.")
2328 (defvar mime-edit-pgp-processing nil)
2329 (make-variable-buffer-local 'mime-edit-pgp-processing)
2331 (defun mime-edit-pgp-enclose-buffer ()
2332 (let ((beg (save-excursion
2333 (goto-char (point-min))
2334 (if (search-forward (concat "\n" mail-header-separator "\n"))
2340 (cond ((eq mime-edit-pgp-processing 'sign)
2341 (mime-edit-enclose-signed-region beg end)
2343 ((eq mime-edit-pgp-processing 'encrypt)
2344 (mime-edit-enclose-encrypted-region beg end)
2352 (defun mime-edit-insert-partial-header (fields subject
2353 id number total separator)
2355 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2356 (insert mime-edit-mime-version-field-for-message/partial)
2358 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2359 id number total separator))
2362 (defun mime-edit-split-and-send
2363 (&optional cmd lines mime-edit-message-max-length)
2367 (count-lines (point-min) (point-max)))
2369 (or mime-edit-message-max-length
2370 (setq mime-edit-message-max-length
2371 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2372 mime-edit-message-default-max-lines))
2374 (let* ((mime-edit-draft-file-name
2375 (or (buffer-file-name)
2377 (expand-file-name "mime-draft" mime-temp-directory))))
2378 (separator mail-header-separator)
2380 (replace-space-with-underline (current-time-string))
2381 "@" (system-name) "\"")))
2382 (run-hooks 'mime-edit-before-split-hook)
2383 (let ((the-buf (current-buffer))
2384 (copy-buf (get-buffer-create " *Original Message*"))
2385 (header (std11-header-string-except
2386 mime-edit-split-ignored-field-regexp separator))
2387 (subject (mail-fetch-field "subject"))
2388 (total (+ (/ lines mime-edit-message-max-length)
2389 (if (> (mod lines mime-edit-message-max-length) 0)
2395 mime-edit-split-message-sender-alist))
2399 (error "Split sender is not specified for `%s'." major-mode)
2402 (mime-edit-partial-number 1)
2405 (set-buffer copy-buf)
2407 (insert-buffer the-buf)
2409 (if (re-search-forward
2410 (concat "^" (regexp-quote separator) "$") nil t)
2411 (let ((he (match-beginning 0)))
2413 (narrow-to-region (point-min) he)
2415 (goto-char (point-min))
2416 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2417 (delete-region (match-beginning 0)
2418 (1+ (std11-field-end)))
2420 (while (< mime-edit-partial-number total)
2423 (set-buffer copy-buf)
2424 (setq data (buffer-substring
2427 (goto-line mime-edit-message-max-length)
2430 (delete-region (point-min)(point))
2432 (mime-edit-insert-partial-header
2433 header subject id mime-edit-partial-number total separator)
2436 (message (format "Sending %d/%d..."
2437 mime-edit-partial-number total))
2438 (call-interactively command)
2439 (message (format "Sending %d/%d... done"
2440 mime-edit-partial-number total))
2442 (setq mime-edit-partial-number
2443 (1+ mime-edit-partial-number))
2447 (set-buffer copy-buf)
2448 (setq data (buffer-string))
2451 (mime-edit-insert-partial-header
2452 header subject id mime-edit-partial-number total separator)
2455 (message (format "Sending %d/%d..."
2456 mime-edit-partial-number total))
2457 (message (format "Sending %d/%d... done"
2458 mime-edit-partial-number total))
2462 (defun mime-edit-maybe-split-and-send (&optional cmd)
2464 (run-hooks 'mime-edit-before-send-hook)
2465 (let ((mime-edit-message-max-length
2466 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2467 mime-edit-message-default-max-lines))
2468 (lines (count-lines (point-min) (point-max)))
2470 (if (and (> lines mime-edit-message-max-length)
2471 mime-edit-split-message)
2472 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2476 ;;; @ preview message
2479 (defvar mime-edit-buffer nil) ; buffer local variable
2481 (defun mime-edit-preview-message ()
2482 "preview editing MIME message."
2484 (let* ((str (buffer-string))
2485 (separator mail-header-separator)
2486 (the-buf (current-buffer))
2487 (buf-name (buffer-name))
2488 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2489 (buf (get-buffer temp-buf-name))
2493 (switch-to-buffer buf)
2496 (setq buf (get-buffer-create temp-buf-name))
2497 (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)
2506 (run-hooks 'mime-edit-translate-hook)
2507 (mime-edit-translate-buffer)
2508 (goto-char (point-min))
2509 (if (re-search-forward
2510 (concat "^" (regexp-quote separator) "$"))
2516 (defun mime-edit-quitting-method ()
2517 "Quitting method for mime-view."
2518 (let ((temp mime-raw-buffer)
2520 (mime-preview-kill-buffer)
2522 (setq buf mime-edit-buffer)
2524 (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-x-emacs-field "\\|X-Emacs")
2539 "Regexp for deleted header fields when `mime-edit-again' is called.")
2541 (defun mime-edit-decode-buffer (not-decode-text)
2543 (goto-char (point-min))
2544 (let ((ctl (mime-read-Content-Type)))
2546 (let ((type (mime-content-type-primary-type ctl))
2547 (stype (mime-content-type-subtype ctl))
2548 (params (mime-content-type-parameters ctl)))
2550 ((and (eq type 'application)(eq stype 'pgp-signature))
2551 (delete-region (point-min)(point-max))
2553 ((eq type 'multipart)
2554 (let* ((boundary (cdr (assoc "boundary" params)))
2556 (concat "\n--" (regexp-quote boundary) "[ \t]*\n"))
2558 (re-search-forward boundary-pat nil t)
2559 (let ((bb (match-beginning 0)) eb tag)
2560 (setq tag (format "\n--<<%s>>-{\n" stype))
2563 (setq bb (+ bb (length tag)))
2565 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2567 (setq eb (match-beginning 0))
2568 (replace-match (format "--}-<<%s>>\n" stype))
2570 (narrow-to-region bb eb)
2571 (goto-char (point-min))
2572 (while (re-search-forward boundary-pat nil t)
2573 (let ((beg (match-beginning 0))
2575 (delete-region beg (match-end 0))
2577 (if (re-search-forward boundary-pat nil t)
2578 (setq end (match-beginning 0))
2579 (setq end (point-max))
2582 (narrow-to-region beg end)
2583 (mime-edit-decode-buffer not-decode-text)
2584 (goto-char (point-max))
2587 (goto-char (point-min))
2588 (or (= (point-min) 1)
2589 (delete-region (point-min)
2590 (if (search-forward "\n\n" nil t)
2596 (let* ((ctype (format "%s/%s" type stype))
2599 (let ((bytes (+ 14 (length ctype))))
2600 (mapconcat (function
2602 (if (string= (car attr) "charset")
2604 (setq charset (cdr attr))
2612 (setq bytes (+ bytes bs 2))
2615 (setq bytes (+ bs 1))
2623 (if (re-search-forward
2624 "Content-Transfer-Encoding:" nil t)
2625 (let ((beg (match-beginning 0))
2626 (hbeg (match-end 0))
2627 (end (std11-field-end)))
2629 (eliminate-top-spaces
2630 (std11-unfold-string
2631 (buffer-substring hbeg end))))
2632 (if (or charset (eq type 'text))
2634 (delete-region beg (1+ end))
2635 (goto-char (point-min))
2636 (if (search-forward "\n\n" nil t)
2639 (match-end 0)(point-max) encoding)
2643 (if (or encoded (not not-decode-text))
2644 (decode-mime-charset-region
2645 (point-min)(point-max)
2646 (or charset default-mime-charset))
2649 (if (re-search-forward "^$" nil t)
2653 (if (= (point-min) 1)
2659 (format "%s/%s%s" type stype pstr)
2662 (delete-region (point-min) he)
2665 (format "%s/%s%s" type stype pstr)
2670 (decode-mime-charset-region (point-min) (point-max)
2671 default-mime-charset)
2675 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2676 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2677 Content-Type and Content-Transfer-Encoding header fields will be
2678 converted to MIME-Edit tags."
2680 (goto-char (point-min))
2682 (concat "\n" (regexp-quote mail-header-separator) "\n")
2684 (replace-match "\n\n")
2686 (mime-edit-decode-buffer not-decode-text)
2687 (goto-char (point-min))
2689 (std11-narrow-to-header)
2690 (goto-char (point-min))
2691 (while (re-search-forward mime-edit-again-ignored-field-regexp nil t)
2692 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2695 (and (re-search-forward "^$")
2696 (replace-match mail-header-separator)
2706 (provide 'mime-edit)
2708 (run-hooks 'mime-edit-load-hook)
2710 ;;; mime-edit.el ends here