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)
122 (defconst mime-edit-version-string
123 `,(concat (car mime-user-interface-version) " "
124 (mapconcat #'number-to-string
125 (cddr mime-user-interface-version) ".")
126 " - \"" (cadr mime-user-interface-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"))
218 ("server" "ftpmail@nic.karrn.ad.jp")
226 ("octet-stream" ("type" "" "tar" "shar"))
228 ("x-kiss" ("x-cnf")))
242 "*Alist of content-type, subtype, parameters and its values.")
244 (defcustom mime-file-types
248 "inline" (("filename" . file))
253 "inline" (("filename" . file))
256 "text" "richtext" nil
264 "application" "postscript" nil
266 "attachment" (("filename" . file))
271 "inline" (("filename" . file))
276 "inline" (("filename" . file))
281 "inline" (("filename" . file))
286 "inline" (("filename" . file))
291 "inline" (("filename" . file))
296 "inline" (("filename" . file))
301 "inline" (("filename" . file))
306 "inline" (("filename" . file))
311 "attachment" (("filename" . file))
316 "attachment" (("filename" . file))
319 "application" "octet-stream" (("type" . "emacs-lisp"))
321 "attachment" (("filename" . file))
324 "application" "octet-stream" (("type" . "common-lisp"))
326 "attachment" (("filename" . file))
329 "application" "octet-stream" (("type" . "tar+gzip"))
331 "attachment" (("filename" . file))
334 "application" "octet-stream" (("type" . "tar+gzip"))
336 "attachment" (("filename" . file))
339 "application" "octet-stream" (("type" . "tar+compress"))
341 "attachment" (("filename" . file))
344 "application" "octet-stream" (("type" . "tar+compress"))
346 "attachment" (("filename" . file))
349 "application" "octet-stream" (("type" . "gzip"))
351 "attachment" (("filename" . file))
354 "application" "octet-stream" (("type" . "compress"))
356 "attachment" (("filename" . file))
359 "application" "octet-stream" (("type" . "lha"))
361 "attachment" (("filename" . file))
364 "application" "zip" nil
366 "attachment" (("filename" . file))
369 "application" "octet-stream" (("type" . "patch"))
371 "attachment" (("filename" . file))
374 "application" "octet-stream" (("type" . "patch"))
376 "attachment" (("filename" . file))
379 "text" "plain" nil nil nil nil)
381 "application" "octet-stream" nil
383 "attachment" (("filename" . file)))
385 "*Alist of file name, types, parameters, and default encoding.
386 If encoding is nil, it is determined from its contents."
390 (choice :tag "Primary-Type"
391 ,@(nconc (mapcar (lambda (cell)
392 (list 'item (car cell))
397 (choice :tag "Sub-Type"
400 (mapcar (lambda (cell)
401 (mapcar (lambda (cell)
402 (list 'item (car cell))
408 (repeat :tag "Parameters of Content-Type field"
409 (cons string (choice string symbol)))
410 ;; content-transfer-encoding
411 (choice :tag "Encoding"
414 (mapcar (lambda (cell)
415 (list 'item (car cell))
417 mime-file-encoding-method-alist)))
419 (choice :tag "Disposition-Type"
425 (repeat :tag "Parameters of Content-Disposition field"
426 (cons string (choice string symbol)))
431 ;;; @@ about charset, encoding and transfer-level
434 (defvar mime-charset-type-list
436 (iso-8859-1 8 "quoted-printable")
437 (iso-8859-2 8 "quoted-printable")
438 (iso-8859-3 8 "quoted-printable")
439 (iso-8859-4 8 "quoted-printable")
440 (iso-8859-5 8 "quoted-printable")
441 (koi8-r 8 "quoted-printable")
442 (iso-8859-7 8 "quoted-printable")
443 (iso-8859-8 8 "quoted-printable")
444 (iso-8859-9 8 "quoted-printable")
445 (iso-2022-jp 7 "base64")
446 (iso-2022-kr 7 "base64")
448 (cn-gb2312 8 "base64")
452 (shift_jis 8 "base64")
453 (iso-2022-jp-2 7 "base64")
454 (iso-2022-int-1 7 "base64")
457 (defvar mime-transfer-level 7
458 "*A number of network transfer level. It should be bigger than 7.")
459 (make-variable-buffer-local 'mime-transfer-level)
461 (defsubst mime-encoding-name (transfer-level &optional not-omit)
462 (cond ((> transfer-level 8) "binary")
463 ((= transfer-level 8) "8bit")
467 (defvar mime-transfer-level-string
468 (mime-encoding-name mime-transfer-level 'not-omit)
469 "A string formatted version of mime-transfer-level")
470 (make-variable-buffer-local 'mime-transfer-level-string)
473 ;;; @@ about message inserting
476 (defvar mime-edit-yank-ignored-field-list
477 '("Received" "Approved" "Path" "Replied" "Status"
478 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
479 "Delete these fields from original message when it is inserted
480 as message/rfc822 part.
481 Each elements are regexp of field-name.")
483 (defvar mime-edit-yank-ignored-field-regexp
485 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
488 (defvar mime-edit-message-inserter-alist nil)
489 (defvar mime-edit-mail-inserter-alist nil)
492 ;;; @@ about message splitting
495 (defcustom mime-edit-split-message t
496 "*Split large message if it is non-nil."
500 (defcustom mime-edit-message-default-max-lines 1000
501 "*Default maximum lines of a message."
505 (defcustom mime-edit-message-max-lines-alist
506 '((news-reply-mode . 500))
507 "Alist of major-mode vs maximum lines of a message.
508 If it is not specified for a major-mode,
509 `mime-edit-message-default-max-lines' is used."
513 (defconst mime-edit-split-ignored-field-regexp
514 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|Message-Id:\\)")
516 (defcustom mime-edit-split-blind-field-regexp
517 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
518 "*Regular expression to match field-name to be ignored when split sending."
522 (defvar mime-edit-split-message-sender-alist nil)
524 (defvar mime-edit-news-reply-mode-server-running nil)
530 (defconst mime-edit-single-part-tag-regexp
531 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
532 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
534 (defconst mime-edit-quoted-single-part-tag-regexp
535 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
537 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
539 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
541 (defconst mime-edit-beginning-tag-regexp
542 (regexp-or mime-edit-single-part-tag-regexp
543 mime-edit-multipart-beginning-regexp))
545 (defconst mime-edit-end-tag-regexp
546 (regexp-or mime-edit-single-part-tag-regexp
547 mime-edit-multipart-end-regexp))
549 (defconst mime-edit-tag-regexp
550 (regexp-or mime-edit-single-part-tag-regexp
551 mime-edit-multipart-beginning-regexp
552 mime-edit-multipart-end-regexp))
554 (defvar mime-tag-format "--[[%s]]"
555 "*Control-string making a MIME tag.")
557 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
558 "*Control-string making a MIME tag with encoding.")
561 ;;; @@ multipart boundary
564 (defvar mime-multipart-boundary "Multipart"
565 "*Boundary of a multipart message.")
568 ;;; @@ optional header fields
571 (defvar mime-edit-insert-user-agent-field t
572 "*If non-nil, insert User-Agent header field.")
574 (defvar mime-edit-user-agent-value
575 (concat (car mime-user-interface-version)
577 (mapconcat #'number-to-string
578 (cddr mime-user-interface-version) ".")
580 (cadr mime-user-interface-version)
582 (car mime-library-version)
584 (mapconcat #'number-to-string
585 (cddr mime-library-version) ".")
587 (cadr mime-library-version)
589 (if (featurep 'xemacs)
591 (if (string-match "\\s +\\\"" emacs-version)
593 (substring emacs-version 0
595 " (" xemacs-codename ")")
596 " (" emacs-version ")")
597 (if (featurep 'mule) " 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 unibyte mode)")
607 (if (featurep 'meadow)
608 (let ((mver (Meadow-version)))
609 (if (string-match "^Meadow-" mver)
614 (concat "MULE/" mule-version
615 " (based on Emacs " ver ")"))
617 "Body of User-Agent field.
618 If variable `mime-edit-insert-user-agent-field' is not nil, it is
619 inserted into message header.")
625 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
626 "*Specify MIME tspecials.
627 Tspecials means any character that matches with it in header must be quoted.")
629 (defconst mime-edit-mime-version-value
630 (concat "1.0 (generated by " mime-edit-version-string ")")
631 "MIME version number.")
633 (defconst mime-edit-mime-version-field-for-message/partial
634 (concat "MIME-Version: 1.0 (split by " mime-edit-version-string ")\n")
635 "MIME version field for message/partial.")
638 ;;; @ keymap and menu
641 (defvar mime-edit-mode-flag nil)
642 (make-variable-buffer-local 'mime-edit-mode-flag)
644 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
645 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
646 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
647 "Keymap for MIME-Edit mode commands to insert entity or set status.")
649 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
650 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
651 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
652 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
653 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
654 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
655 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
656 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
657 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
658 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
660 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
661 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
662 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
663 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
664 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
665 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
666 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
667 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
668 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
669 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
671 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
672 "Keymap prefix for MIME-Edit mode commands about enclosure.")
673 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
674 "Keymap for MIME-Edit mode commands about enclosure.")
676 (define-key mime-edit-mode-enclosure-map
677 "\C-a" 'mime-edit-enclose-alternative-region)
678 (define-key mime-edit-mode-enclosure-map
679 "\C-p" 'mime-edit-enclose-parallel-region)
680 (define-key mime-edit-mode-enclosure-map
681 "\C-m" 'mime-edit-enclose-mixed-region)
682 (define-key mime-edit-mode-enclosure-map
683 "\C-d" 'mime-edit-enclose-digest-region)
684 (define-key mime-edit-mode-enclosure-map
685 "\C-s" 'mime-edit-enclose-pgp-signed-region)
686 (define-key mime-edit-mode-enclosure-map
687 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
688 (define-key mime-edit-mode-enclosure-map
689 "\C-q" 'mime-edit-enclose-quote-region)
691 (defvar mime-edit-mode-map (make-sparse-keymap)
692 "Keymap for MIME-Edit mode commands.")
693 (define-key mime-edit-mode-map
694 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
695 (define-key mime-edit-mode-map
696 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
698 (defconst mime-edit-menu-title "MIME-Edit")
700 (defconst mime-edit-menu-list
701 '((mime-help "Describe MIME editor mode" mime-edit-help)
702 (file "Insert File" mime-edit-insert-file)
703 (external "Insert External" mime-edit-insert-external)
704 (voice "Insert Voice" mime-edit-insert-voice)
705 (message "Insert Message" mime-edit-insert-message)
706 (mail "Insert Mail" mime-edit-insert-mail)
707 (signature "Insert Signature" mime-edit-insert-signature)
708 (text "Insert Text" mime-edit-insert-text)
709 (tag "Insert Tag" mime-edit-insert-tag)
710 (alternative "Enclose as alternative"
711 mime-edit-enclose-alternative-region)
712 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
713 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
714 (digest "Enclose as digest" mime-edit-enclose-digest-region)
715 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
716 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
717 (quote "Verbatim region" mime-edit-enclose-quote-region)
718 (key "Insert Public Key" mime-edit-insert-key)
719 (split "About split" mime-edit-set-split)
720 (sign "About sign" mime-edit-set-sign)
721 (encrypt "About encryption" mime-edit-set-encrypt)
722 (preview "Preview Message" mime-edit-preview-message)
723 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
725 "MIME-edit menubar entry.")
727 (cond (running-xemacs
728 ;; modified by Pekka Marjola <pema@iki.fi>
729 ;; 1995/9/5 (c.f. [tm-en:69])
730 (defun mime-edit-define-menu-for-xemacs ()
731 "Define menu for XEmacs."
732 (cond ((featurep 'menubar)
733 (make-local-variable 'current-menubar)
734 (set-buffer-menubar current-menubar)
737 (cons mime-edit-menu-title
740 (vector (nth 1 item)(nth 2 item)
743 mime-edit-menu-list)))
746 ;; modified by Steven L. Baur <steve@miranova.com>
747 ;; 1995/12/6 (c.f. [tm-en:209])
748 (or (boundp 'mime-edit-popup-menu-for-xemacs)
749 (setq mime-edit-popup-menu-for-xemacs
750 (append '("MIME Commands" "---")
751 (mapcar (function (lambda (item)
755 mime-edit-menu-list)))
758 ((>= emacs-major-version 19)
759 (define-key mime-edit-mode-map [menu-bar mime-edit]
760 (cons mime-edit-menu-title
761 (make-sparse-keymap mime-edit-menu-title)))
764 (define-key mime-edit-mode-map
765 (vector 'menu-bar 'mime-edit (car item))
766 (cons (nth 1 item)(nth 2 item))
769 (reverse mime-edit-menu-list)
777 (defvar mime-edit-touched-flag nil)
780 (defun mime-edit-mode ()
781 "MIME minor mode for editing the tagged MIME message.
783 In this mode, basically, the message is composed in the tagged MIME
784 format. The message tag looks like:
786 --[[text/plain; charset=ISO-2022-JP][7bit]]
788 The tag specifies the MIME content type, subtype, optional parameters
789 and transfer encoding of the message following the tag. Messages
790 without any tag are treated as `text/plain' by default. Charset and
791 transfer encoding are automatically defined unless explicitly
792 specified. Binary messages such as audio and image are usually
793 hidden. The messages in the tagged MIME format are automatically
794 translated into a MIME compliant message when exiting this mode.
796 Available charsets depend on Emacs version being used. The following
797 lists the available charsets of each emacs.
799 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
800 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
801 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
802 CN-BIG5 and ISO-2022-INT-1 are available.
804 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
805 be used to represent multilingual text in intermixed manner. Any
806 languages that has no registered charset are represented as either
807 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
809 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
810 without mule, please set variable `default-mime-charset'. This
811 variable must be symbol of which name is a MIME charset.
813 If you want to add more charsets in mule, please set variable
814 `charsets-mime-charset-alist'. This variable must be alist of which
815 key is list of charset and value is symbol of MIME charset. If name
816 of coding-system is different as MIME charset, please set variable
817 `mime-charset-coding-system-alist'. This variable must be alist of
818 which key is MIME charset and value is coding-system.
820 Following commands are available in addition to major mode commands:
823 \\[mime-edit-insert-text] insert a text message.
824 \\[mime-edit-insert-file] insert a (binary) file.
825 \\[mime-edit-insert-external] insert a reference to external body.
826 \\[mime-edit-insert-voice] insert a voice message.
827 \\[mime-edit-insert-message] insert a mail or news message.
828 \\[mime-edit-insert-mail] insert a mail message.
829 \\[mime-edit-insert-signature] insert a signature file at end.
830 \\[mime-edit-insert-key] insert PGP public key.
831 \\[mime-edit-insert-tag] insert a new MIME tag.
833 \[make enclosure (maybe multipart)\]
834 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
835 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
836 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
837 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
838 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
839 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
840 \\[mime-edit-enclose-quote-region] enclose as verbose mode
841 (to avoid to expand tags)
844 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
845 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
846 \\[mime-edit-set-split] set message splitting mode.
847 \\[mime-edit-set-sign] set PGP-sign mode.
848 \\[mime-edit-set-encrypt] set PGP-encryption mode.
849 \\[mime-edit-preview-message] preview editing MIME message.
850 \\[mime-edit-exit] exit and translate into a MIME
852 \\[mime-edit-help] show this help.
853 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
856 Additional commands are available in some major modes:
857 C-c C-c exit, translate and run the original command.
858 C-c C-s exit, translate and run the original command.
860 The following is a message example written in the tagged MIME format.
861 TABs at the beginning of the line are not a part of the message:
863 This is a conventional plain text. It should be translated
866 This is also a plain text. But, it is explicitly specified as
868 --[[text/plain; charset=ISO-8859-1]]
869 This is also a plain text. But charset is specified as
872 ¡Hola! Buenos días. ¿Cómo está usted?
874 This is a <bold>enriched text</bold>.
875 --[[image/gif][base64]]...image encoded in base64 here...
876 --[[audio/basic][base64]]...audio encoded in base64 here...
878 User customizable variables (not documented all of them):
880 Specifies a key prefix for MIME minor mode commands.
882 mime-ignore-preceding-spaces
883 Preceding white spaces in a message body are ignored if non-nil.
885 mime-ignore-trailing-spaces
886 Trailing white spaces in a message body are ignored if non-nil.
889 Hide a non-textual body message encoded in base64 after insertion
893 A number of network transfer level. It should be bigger than 7.
894 If you are in 8bit-through environment, please set 8.
896 mime-edit-voice-recorder
897 Specifies a function to record a voice message and encode it.
898 The function `mime-edit-voice-recorder-for-sun' is for Sun
902 Turning on MIME mode calls the value of mime-edit-mode-hook, if
905 mime-edit-translate-hook
906 The value of mime-edit-translate-hook is called just before translating
907 the tagged MIME format into a MIME compliant message if it is
908 non-nil. If the hook call the function mime-edit-insert-signature,
909 the signature file will be inserted automatically.
912 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
915 (if mime-edit-mode-flag
917 (if mime-edit-touched-flag
919 (make-local-variable 'mime-edit-touched-flag)
920 (setq mime-edit-touched-flag t)
925 (cond (running-xemacs
926 (add-minor-mode 'mime-edit-mode-flag
927 '((" MIME-Edit " mime-transfer-level-string))
933 (set-alist 'minor-mode-alist
935 '((" MIME-Edit " mime-transfer-level-string)))
936 (set-alist 'minor-mode-map-alist
943 (defun turn-on-mime-edit ()
944 "Unconditionally turn on MIME-Edit mode."
946 (if mime-edit-mode-flag
947 (error "You are already editing a MIME message.")
948 (setq mime-edit-mode-flag t)
950 ;; Set transfer level into mode line
952 (setq mime-transfer-level-string
953 (mime-encoding-name mime-transfer-level 'not-omit))
954 (force-mode-line-update)
956 ;; Define menu for XEmacs.
958 (mime-edit-define-menu-for-xemacs)
963 ;; I don't care about saving these.
964 (setq paragraph-start
965 (regexp-or mime-edit-single-part-tag-regexp
967 (setq paragraph-separate
968 (regexp-or mime-edit-single-part-tag-regexp
970 (run-hooks 'mime-edit-mode-hook)
972 (substitute-command-keys
973 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
977 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
980 (defun mime-edit-exit (&optional nomime no-error)
981 "Translate the tagged MIME message into a MIME compliant message.
982 With no argument encode a message in the buffer into MIME, otherwise
983 just return to previous mode."
985 (if (not mime-edit-mode-flag)
987 (error "You aren't editing a MIME message.")
991 (run-hooks 'mime-edit-translate-hook)
992 (mime-edit-translate-buffer)))
993 ;; Restore previous state.
994 (setq mime-edit-mode-flag nil)
995 (if (and running-xemacs
997 (delete-menu-item (list mime-edit-menu-title))
1000 (set-buffer-modified-p (buffer-modified-p))
1001 (run-hooks 'mime-edit-exit-hook)
1002 (message "Exit MIME editor mode.")
1005 (defun mime-edit-maybe-translate ()
1007 (mime-edit-exit nil t)
1008 (call-interactively 'mime-edit-maybe-split-and-send)
1011 (defun mime-edit-help ()
1012 "Show help message about MIME mode."
1014 (with-output-to-temp-buffer "*Help*"
1015 (princ "MIME editor mode:\n")
1016 (princ (documentation 'mime-edit-mode))
1017 (print-help-return-message)))
1019 (defun mime-edit-insert-text (&optional subtype)
1020 "Insert a text message.
1021 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1022 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1024 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1026 (if (looking-at mime-edit-single-part-tag-regexp)
1028 ;; Make a space between the following message.
1032 (if (and (member (cadr ret) '("enriched" "richtext"))
1033 (fboundp 'enriched-mode)
1036 (if (boundp 'enriched-mode)
1041 (defun mime-edit-insert-file (file &optional verbose)
1042 "Insert a message from a file."
1043 (interactive "fInsert file as MIME message: \nP")
1044 (let* ((guess (mime-find-file-type file))
1045 (type (nth 0 guess))
1046 (subtype (nth 1 guess))
1047 (parameters (nth 2 guess))
1048 (encoding (nth 3 guess))
1049 (disposition-type (nth 4 guess))
1050 (disposition-params (nth 5 guess))
1053 (setq type (mime-prompt-for-type type)
1054 subtype (mime-prompt-for-subtype type subtype)
1056 (if (or (interactive-p) verbose)
1057 (setq encoding (mime-prompt-for-encoding encoding))
1059 (if (or (consp parameters) (stringp disposition-type))
1060 (let ((rest parameters) cell attribute value)
1061 (setq parameters "")
1063 (setq cell (car rest))
1064 (setq attribute (car cell))
1065 (setq value (cdr cell))
1066 (if (eq value 'file)
1067 (setq value (std11-wrap-as-quoted-string
1068 (file-name-nondirectory file)))
1070 (setq parameters (concat parameters "; " attribute "=" value))
1071 (setq rest (cdr rest))
1073 (if disposition-type
1076 (concat parameters "\n"
1077 "Content-Disposition: " disposition-type))
1078 (setq rest disposition-params)
1080 (setq cell (car rest))
1081 (setq attribute (car cell))
1082 (setq value (cdr cell))
1083 (if (eq value 'file)
1084 (setq value (std11-wrap-as-quoted-string
1085 (file-name-nondirectory file)))
1088 (concat parameters "; " attribute "=" value))
1089 (setq rest (cdr rest))
1093 (mime-edit-insert-tag type subtype parameters)
1094 (mime-edit-insert-binary-file file encoding)
1097 (defun mime-edit-insert-external ()
1098 "Insert a reference to external body."
1100 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1102 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1104 (let* ((pritype (mime-prompt-for-type))
1105 (subtype (mime-prompt-for-subtype pritype))
1106 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1109 (insert "Content-Type: "
1110 pritype "/" subtype (or parameters "") "\n")))
1111 (if (and (not (eobp))
1112 (not (looking-at mime-edit-single-part-tag-regexp)))
1113 (insert (mime-make-text-tag) "\n")))
1115 (defun mime-edit-insert-voice ()
1116 "Insert a voice message."
1120 "What transfer encoding: "
1121 mime-file-encoding-method-alist nil t nil)))
1122 (mime-edit-insert-tag "audio" "basic" nil)
1123 (mime-edit-define-encoding encoding)
1125 (narrow-to-region (1- (point))(point))
1127 (funcall mime-edit-voice-recorder encoding)
1130 (invisible-region (point-min)(point-max))
1131 (goto-char (point-max))
1134 (defun mime-edit-insert-signature (&optional arg)
1135 "Insert a signature file."
1137 (let ((signature-insert-hook
1140 (let ((items (mime-find-file-type signature-file-name)))
1141 (apply (function mime-edit-insert-tag)
1142 (car items) (cadr items) (list (caddr items))))
1145 (insert-signature arg)
1149 ;; Insert a new tag around a point.
1151 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1152 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1153 If nothing is inserted, return nil."
1156 (mime-edit-goto-tag)
1157 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1158 (< (match-beginning 0) p)
1161 (goto-char (match-beginning 0))
1170 (mime-prompt-for-type)))
1173 (mime-prompt-for-subtype pritype)))
1176 (mime-prompt-for-parameters pritype subtype delimiter)))
1177 ;; Make a new MIME tag.
1178 (setq newtag (mime-make-tag pritype subtype parameters))
1179 ;; Find an current MIME tag.
1182 (if (mime-edit-goto-tag)
1183 (buffer-substring (match-beginning 0) (match-end 0))
1184 ;; Assume content type is 'text/plan'.
1185 (mime-make-tag "text" "plain")
1187 ;; We are only interested in TEXT.
1189 (not (mime-test-content-type
1190 (mime-edit-get-contype oldtag) "text")))
1193 (if (or (not oldtag) ;Not text
1194 (or mime-ignore-same-text-tag
1195 (not (string-equal oldtag newtag))))
1197 ;; Mark the beginning of the tag for convenience.
1198 (push-mark (point) 'nomsg)
1199 (insert newtag "\n")
1200 (list pritype subtype parameters) ;New tag is created.
1202 ;; Restore previous point.
1204 nil ;Nothing is created.
1208 (defun mime-edit-insert-binary-file (file &optional encoding)
1209 "Insert binary FILE at point.
1210 Optional argument ENCODING specifies an encoding method such as base64."
1211 (let* ((tagend (1- (point))) ;End of the tag
1212 (hide-p (and mime-auto-hide-body
1215 (let ((en (downcase encoding)))
1216 (or (string-equal en "7bit")
1217 (string-equal en "8bit")
1218 (string-equal en "binary")
1222 (narrow-to-region tagend (point))
1223 (mime-insert-encoded-file file encoding)
1226 (invisible-region (point-min) (point-max))
1227 (goto-char (point-max))
1229 (goto-char (point-max))
1232 (looking-at mime-edit-tag-regexp)
1233 (= (point)(point-max))
1234 (mime-edit-insert-tag "text" "plain")
1236 ;; Define encoding even if it is 7bit.
1237 (if (stringp encoding)
1239 (goto-char tagend) ; Make sure which line the tag is on.
1240 (mime-edit-define-encoding encoding)
1245 ;; Commands work on a current message flagment.
1247 (defun mime-edit-goto-tag ()
1248 "Search for the beginning of the tagged MIME message."
1249 (let ((current (point)))
1250 (if (looking-at mime-edit-tag-regexp)
1252 ;; At first, go to the end.
1253 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1254 (goto-char (1- (match-beginning 0))) ;For multiline tag
1257 (goto-char (point-max))
1259 ;; Then search for the beginning.
1260 (re-search-backward mime-edit-end-tag-regexp nil t)
1261 (or (looking-at mime-edit-beginning-tag-regexp)
1262 ;; Restore previous point.
1269 (defun mime-edit-content-beginning ()
1270 "Return the point of the beginning of content."
1272 (let ((beg (save-excursion
1273 (beginning-of-line) (point))))
1274 (if (mime-edit-goto-tag)
1275 (let ((top (point)))
1276 (goto-char (match-end 0))
1277 (if (and (= beg top)
1278 (= (following-char) ?\^M))
1282 ;; Default text/plain tag.
1283 (goto-char (point-min))
1285 (concat "\n" (regexp-quote mail-header-separator)
1286 (if mime-ignore-preceding-spaces
1287 "[ \t\n]*\n" "\n")) nil 'move)
1291 (defun mime-edit-content-end ()
1292 "Return the point of the end of content."
1294 (if (mime-edit-goto-tag)
1296 (goto-char (match-end 0))
1297 (if (invisible-p (point))
1298 (next-visible-point (point))
1299 ;; Move to the end of this text.
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 ;; Assume the message begins with text/plain.
1307 (goto-char (mime-edit-content-beginning))
1308 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1309 ;; Don't forget a multiline tag.
1310 (goto-char (match-beginning 0)))
1314 (defun mime-edit-define-charset (charset)
1315 "Set charset of current tag to CHARSET."
1317 (if (mime-edit-goto-tag)
1318 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1319 (delete-region (match-beginning 0) (match-end 0))
1322 (mime-edit-set-parameter
1323 (mime-edit-get-contype tag)
1324 "charset" (upcase (symbol-name charset)))
1325 (mime-edit-get-encoding tag)))
1328 (defun mime-edit-define-encoding (encoding)
1329 "Set encoding of current tag to ENCODING."
1331 (if (mime-edit-goto-tag)
1332 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1333 (delete-region (match-beginning 0) (match-end 0))
1334 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1337 (defun mime-edit-choose-charset ()
1338 "Choose charset of a text following current point."
1339 (detect-mime-charset-region (point) (mime-edit-content-end))
1342 (defun mime-make-text-tag (&optional subtype)
1343 "Make a tag for a text after current point.
1344 Subtype of text type can be specified by an optional argument SUBTYPE.
1345 Otherwise, it is obtained from mime-content-types."
1346 (let* ((pritype "text")
1347 (subtype (or subtype
1348 (car (car (cdr (assoc pritype mime-content-types)))))))
1349 ;; Charset should be defined later.
1350 (mime-make-tag pritype subtype)))
1353 ;; Tag handling functions
1355 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1356 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1357 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1361 (defun mime-create-tag (contype &optional encoding)
1362 "Make a tag with CONTENT-TYPE and optional ENCODING."
1363 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1366 (defun mime-edit-get-contype (tag)
1367 "Return Content-Type (including parameters) of TAG."
1369 (or (string-match mime-edit-single-part-tag-regexp tag)
1370 (string-match mime-edit-multipart-beginning-regexp tag)
1371 (string-match mime-edit-multipart-end-regexp tag)
1373 (substring tag (match-beginning 1) (match-end 1))
1376 (defun mime-edit-get-encoding (tag)
1377 "Return encoding of TAG."
1379 (string-match mime-edit-single-part-tag-regexp tag)
1381 (not (= (match-beginning 3) (match-end 3)))
1382 (substring tag (match-beginning 3) (match-end 3))))
1384 (defun mime-get-parameter (contype parameter)
1385 "For given CONTYPE return value for PARAMETER.
1386 Nil if no such parameter."
1390 (regexp-quote parameter)
1391 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1393 (substring contype (match-beginning 1) (match-end 1))
1394 nil ;No such parameter
1397 (defun mime-edit-set-parameter (contype parameter value)
1398 "For given CONTYPE set PARAMETER to VALUE."
1399 (let (ctype opt-fields)
1400 (if (string-match "\n[^ \t\n\r]+:" contype)
1401 (setq ctype (substring contype 0 (match-beginning 0))
1402 opt-fields (substring contype (match-beginning 0)))
1403 (setq ctype contype)
1408 (regexp-quote parameter)
1409 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1412 (concat (substring ctype 0 (match-beginning 1))
1414 (substring contype (match-end 1))
1416 (concat ctype "; " parameter "=" value opt-fields)
1419 (defun mime-strip-parameters (contype)
1420 "Return primary content-type and subtype without parameters for CONTYPE."
1421 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1422 (substring contype (match-beginning 1) (match-end 1)) nil))
1424 (defun mime-test-content-type (contype type &optional subtype)
1425 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1426 (and (stringp contype)
1429 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1430 (downcase contype))))
1435 (defun mime-find-file-type (file)
1436 "Guess Content-Type, subtype, and parameters from FILE."
1438 (guesses mime-file-types))
1439 (while (and (not guess) guesses)
1440 (if (string-match (car (car guesses)) file)
1441 (setq guess (cdr (car guesses))))
1442 (setq guesses (cdr guesses)))
1446 (defun mime-prompt-for-type (&optional default)
1447 "Ask for Content-type."
1449 ;; Repeat until primary content type is specified.
1450 (while (string-equal type "")
1452 (completing-read "What content type: "
1455 'require-match ;Type must be specified.
1458 (if (string-equal type "")
1460 (message "Content type is required.")
1467 (defun mime-prompt-for-subtype (type &optional default)
1468 "Ask for subtype of media-type TYPE."
1469 (let ((subtypes (cdr (assoc type mime-content-types))))
1471 (assoc default subtypes))
1472 (setq default (car (car subtypes)))
1478 "What content subtype: (default " default ") ")
1479 "What content subtype: ")
1480 (cdr (assoc type mime-content-types))
1482 'require-match ;Subtype must be specified.
1485 (if (string-equal answer "") default answer)))
1487 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1488 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1489 Optional DELIMITER specifies parameter delimiter (';' by default)."
1490 (let* ((delimiter (or delimiter "; "))
1495 (mime-prompt-for-parameters-1
1497 (cdr (assoc pritype mime-content-types))))))
1500 (if (and (stringp parameters)
1501 (not (string-equal parameters "")))
1502 (concat delimiter parameters)
1503 "" ;"" if no parameters
1506 (defun mime-prompt-for-parameters-1 (optlist)
1507 (apply (function append)
1508 (mapcar (function mime-prompt-for-parameter) optlist)))
1510 (defun mime-prompt-for-parameter (parameter)
1512 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1513 (let* ((prompt (car parameter))
1514 (choices (mapcar (function
1516 (if (consp e) e (list e))))
1518 (default (car (car choices)))
1524 (concat "What " prompt
1526 (if (string-equal default "") "\"\"" default)
1528 choices nil nil ""))
1529 ;; If nothing is selected, use default.
1530 (if (string-equal answer "")
1531 (setq answer default)))
1533 (read-string (concat "What " prompt ": "))))
1534 (cons (if (and answer
1535 (not (string-equal answer "")))
1537 ;; Note: control characters ignored!
1538 (if (string-match mime-tspecials-regexp answer)
1539 (concat "\"" answer "\"") answer)))
1540 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1543 (defun mime-prompt-for-encoding (default)
1544 "Ask for Content-Transfer-Encoding."
1549 "What transfer encoding: "
1550 mime-file-encoding-method-alist nil t default)
1556 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1559 (defvar mime-edit-translate-buffer-hook
1560 '(mime-edit-pgp-enclose-buffer
1561 mime-edit-translate-body
1562 mime-edit-translate-header))
1564 (defun mime-edit-translate-header ()
1565 "Encode the message header into network representation."
1566 (eword-encode-header 'code-conversion)
1567 (run-hooks 'mime-edit-translate-header-hook)
1570 (defun mime-edit-translate-buffer ()
1571 "Encode the tagged MIME message in current buffer in MIME compliant message."
1573 (if (catch 'mime-edit-error
1575 (run-hooks 'mime-edit-translate-buffer-hook)
1579 (error "Translation error!")
1582 (defun mime-edit-find-inmost ()
1583 (goto-char (point-min))
1584 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1585 (let ((bb (match-beginning 0))
1587 (type (buffer-substring (match-beginning 1)(match-end 1)))
1589 (setq end-exp (format "--}-<<%s>>\n" type))
1591 (if (re-search-forward end-exp nil t)
1592 (setq eb (match-beginning 0))
1593 (setq eb (point-max))
1595 (narrow-to-region be eb)
1597 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1599 (narrow-to-region (match-beginning 0)(point-max))
1600 (mime-edit-find-inmost)
1603 (list type bb be eb)
1606 (defun mime-edit-process-multipart-1 (boundary)
1607 (let ((ret (mime-edit-find-inmost)))
1609 (let ((type (car ret))
1610 (bb (nth 1 ret))(be (nth 2 ret))
1613 (narrow-to-region bb eb)
1614 (delete-region bb be)
1615 (setq bb (point-min))
1616 (setq eb (point-max))
1619 (if (looking-at mime-edit-multipart-end-regexp)
1620 (let ((beg (match-beginning 0))
1623 (delete-region beg end)
1624 (or (looking-at mime-edit-beginning-tag-regexp)
1626 (insert (concat (mime-make-text-tag) "\n"))
1628 (cond ((string-equal type "quote")
1629 (mime-edit-enquote-region bb eb)
1631 ((string-equal type "pgp-signed")
1632 (mime-edit-sign-pgp-mime bb eb boundary)
1634 ((string-equal type "pgp-encrypted")
1635 (mime-edit-encrypt-pgp-mime bb eb boundary)
1637 ((string-equal type "kazu-signed")
1638 (mime-edit-sign-pgp-kazu bb eb boundary)
1640 ((string-equal type "kazu-encrypted")
1641 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1645 (nth 2 (mime-edit-translate-region bb eb
1649 (format "--[[multipart/%s;
1650 boundary=\"%s\"][7bit]]\n"
1655 (defun mime-edit-enquote-region (beg end)
1658 (narrow-to-region beg end)
1660 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1661 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1662 (replace-match (concat "- " (substring tag 1)))
1665 (defun mime-edit-dequote-region (beg end)
1668 (narrow-to-region beg end)
1670 (while (re-search-forward
1671 mime-edit-quoted-single-part-tag-regexp nil t)
1672 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1673 (replace-match (concat "-" (substring tag 2)))
1676 (defun mime-edit-sign-pgp-mime (beg end boundary)
1679 (narrow-to-region beg end)
1681 (mime-edit-translate-region beg end boundary))
1683 (encoding (nth 1 ret))
1684 (pgp-boundary (concat "pgp-sign-" boundary)))
1686 (insert (format "Content-Type: %s\n" ctype))
1688 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1691 (or (as-binary-process
1692 (funcall (pgp-function 'mime-sign)
1693 (point-min)(point-max) nil nil pgp-boundary))
1694 (throw 'mime-edit-error 'pgp-error)
1698 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1700 (defun mime-edit-make-encrypt-recipient-header ()
1701 (let* ((names mime-edit-encrypt-recipient-fields-list)
1703 (std11-field-bodies (cons "From" names)
1704 nil mail-header-separator))
1707 (setq values (cdr values))))
1708 (header (and (stringp from)
1709 (if (string-equal from "")
1711 (format "From: %s\n" from)
1714 (while (and names values)
1715 (let ((name (car names))
1716 (value (car values))
1718 (and (stringp value)
1719 (or (string-equal value "")
1721 (setq header (concat header name ": " value "\n")
1722 recipients (if recipients
1723 (concat recipients " ," value)
1726 (setq names (cdr names)
1727 values (cdr values))
1729 (vector from recipients header)
1732 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1735 (let (from recipients header)
1736 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1737 (setq from (aref ret 0)
1738 recipients (aref ret 1)
1739 header (aref ret 2))
1741 (narrow-to-region beg end)
1743 (mime-edit-translate-region beg end boundary))
1745 (encoding (nth 1 ret))
1746 (pgp-boundary (concat "pgp-" boundary)))
1749 (insert (format "Content-Type: %s\n" ctype))
1751 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1754 (or (funcall (pgp-function 'encrypt)
1755 recipients (point-min) (point-max) from)
1756 (throw 'mime-edit-error 'pgp-error)
1759 (insert (format "--[[multipart/encrypted;
1761 protocol=\"application/pgp-encrypted\"][7bit]]
1763 Content-Type: application/pgp-encrypted
1766 Content-Type: application/octet-stream
1767 Content-Transfer-Encoding: 7bit
1769 " pgp-boundary pgp-boundary pgp-boundary))
1770 (goto-char (point-max))
1771 (insert (format "\n--%s--\n" pgp-boundary))
1774 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1777 (narrow-to-region beg end)
1779 (mime-edit-translate-region beg end boundary))
1781 (encoding (nth 1 ret)))
1783 (insert (format "Content-Type: %s\n" ctype))
1785 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1788 (or (as-binary-process
1789 (funcall (pgp-function 'traditional-sign)
1791 (throw 'mime-edit-error 'pgp-error)
1795 "--[[application/pgp; format=mime][7bit]]\n")
1799 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1801 (let (recipients header)
1802 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1803 (setq recipients (aref ret 1)
1804 header (aref ret 2))
1807 (narrow-to-region beg end)
1809 (mime-edit-translate-region beg end boundary))
1811 (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 User-Agent field
1882 (and mime-edit-insert-user-agent-field
1883 (or (mail-position-on-field "User-Agent")
1884 (insert mime-edit-user-agent-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.
2047 (let ((ret (cdr (assq charset mime-charset-type-list))))
2049 (setq bits (car ret)
2052 conv "quoted-printable")))
2053 (if (<= bits mime-transfer-level)
2054 (mime-encoding-name bits)
2056 (beg (mime-edit-content-beginning)))
2057 (encode-mime-charset-region beg (mime-edit-content-end)
2059 ;; Protect "From " in beginning of line
2061 (narrow-to-region beg (mime-edit-content-end))
2063 (let (case-fold-search)
2064 (if (re-search-forward "^From " nil t)
2066 (if (memq charset '(iso-2022-jp
2071 (replace-match "\e(BFrom ")
2072 (re-search-forward "^From " nil t)
2074 (setq encoding "quoted-printable")
2076 ;; canonicalize line break code
2077 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2079 (narrow-to-region beg (mime-edit-content-end))
2081 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2082 (replace-match "\\1\r\n"))))
2084 (mime-encode-region beg (mime-edit-content-end) encoding)
2085 (mime-edit-define-encoding encoding)
2087 (goto-char (mime-edit-content-end))
2089 ((null encoding) ;Encoding is not specified.
2090 ;; Application, image, audio, video, and any other
2091 ;; unknown content-type without encoding should be
2093 (let* ((encoding "base64") ;Encode in BASE64 by default.
2094 (beg (mime-edit-content-beginning))
2095 (end (mime-edit-content-end)))
2096 (mime-encode-region beg end encoding)
2097 (mime-edit-define-encoding encoding))
2102 (defun mime-delete-field (field)
2103 "Delete header FIELD."
2104 (let ((regexp (format "^%s:[ \t]*" field)))
2105 (goto-char (point-min))
2106 (while (re-search-forward regexp nil t)
2107 (delete-region (match-beginning 0)
2108 (progn (forward-line 1) (point)))
2113 ;;; Platform dependent functions
2116 ;; Sun implementations
2118 (defun mime-edit-voice-recorder-for-sun (encoding)
2119 "Record voice in a buffer using Sun audio device,
2120 and insert data encoded as ENCODING."
2121 (message "Start the recording on %s. Type C-g to finish the recording..."
2123 (mime-insert-encoded-file "/dev/audio" encoding)
2127 ;;; @ Other useful commands.
2130 ;; Message forwarding commands as content-type "message/rfc822".
2132 (defun mime-edit-insert-message (&optional message)
2134 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2135 (if (and inserter (fboundp inserter))
2137 (mime-edit-insert-tag "message" "rfc822")
2138 (funcall inserter message)
2140 (message "Sorry, I don't have message inserter for your MUA.")
2143 (defun mime-edit-insert-mail (&optional message)
2145 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2146 (if (and inserter (fboundp inserter))
2148 (mime-edit-insert-tag "message" "rfc822")
2149 (funcall inserter message)
2151 (message "Sorry, I don't have mail inserter for your MUA.")
2154 (defun mime-edit-inserted-message-filter ()
2157 (let ((header-start (point))
2158 (case-fold-search t)
2161 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2162 (if (re-search-forward "^$" (mark t))
2163 (narrow-to-region header-start (match-beginning 0))
2165 (goto-char header-start)
2166 (while (and (re-search-forward
2167 mime-edit-yank-ignored-field-regexp nil t)
2168 (setq beg (match-beginning 0))
2169 (setq end (1+ (std11-field-end)))
2171 (delete-region beg end)
2176 ;;; @ multipart enclosure
2179 (defun mime-edit-enclose-region-internal (type beg end)
2183 (narrow-to-region beg end)
2184 (insert (format "--<<%s>>-{\n" type))
2185 (goto-char (point-max))
2186 (insert (format "--}-<<%s>>\n" type))
2187 (goto-char (point-max))
2189 (or (looking-at mime-edit-beginning-tag-regexp)
2191 (insert (mime-make-text-tag) "\n")
2195 (defun mime-edit-enclose-quote-region (beg end)
2197 (mime-edit-enclose-region-internal 'quote beg end)
2200 (defun mime-edit-enclose-mixed-region (beg end)
2202 (mime-edit-enclose-region-internal 'mixed beg end)
2205 (defun mime-edit-enclose-parallel-region (beg end)
2207 (mime-edit-enclose-region-internal 'parallel beg end)
2210 (defun mime-edit-enclose-digest-region (beg end)
2212 (mime-edit-enclose-region-internal 'digest beg end)
2215 (defun mime-edit-enclose-alternative-region (beg end)
2217 (mime-edit-enclose-region-internal 'alternative beg end)
2220 (defun mime-edit-enclose-pgp-signed-region (beg end)
2222 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2225 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2227 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2230 (defun mime-edit-enclose-kazu-signed-region (beg end)
2232 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2235 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2237 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2240 (defun mime-edit-insert-key (&optional arg)
2241 "Insert a pgp public key."
2243 (mime-edit-insert-tag "application" "pgp-keys")
2244 (mime-edit-define-encoding "7bit")
2245 (funcall (pgp-function 'insert-key))
2252 (defun mime-edit-set-split (arg)
2255 (y-or-n-p "Do you want to enable split? ")
2257 (setq mime-edit-split-message arg)
2259 (message "This message is enabled to split.")
2260 (message "This message is not enabled to split.")
2263 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2264 "Toggle transfer-level is 7bit or 8bit through.
2266 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2268 (if (numberp transfer-level)
2269 (setq mime-transfer-level transfer-level)
2270 (if (< mime-transfer-level 8)
2271 (setq mime-transfer-level 8)
2272 (setq mime-transfer-level 7)
2274 (message (format "Current transfer-level is %d bit"
2275 mime-transfer-level))
2276 (setq mime-transfer-level-string
2277 (mime-encoding-name mime-transfer-level 'not-omit))
2278 (force-mode-line-update)
2281 (defun mime-edit-set-transfer-level-7bit ()
2283 (mime-edit-toggle-transfer-level 7)
2286 (defun mime-edit-set-transfer-level-8bit ()
2288 (mime-edit-toggle-transfer-level 8)
2295 (defvar mime-edit-pgp-processing nil)
2296 (make-variable-buffer-local 'mime-edit-pgp-processing)
2298 (defun mime-edit-set-sign (arg)
2301 (y-or-n-p "Do you want to sign? ")
2305 (setq mime-edit-pgp-processing 'sign)
2306 (message "This message will be signed.")
2308 (if (eq mime-edit-pgp-processing 'sign)
2309 (setq mime-edit-pgp-processing nil)
2311 (message "This message will not be signed.")
2314 (defun mime-edit-set-encrypt (arg)
2317 (y-or-n-p "Do you want to encrypt? ")
2321 (setq mime-edit-pgp-processing 'encrypt)
2322 (message "This message will be encrypt.")
2324 (if (eq mime-edit-pgp-processing 'encrypt)
2325 (setq mime-edit-pgp-processing nil)
2327 (message "This message will not be encrypt.")
2330 (defun mime-edit-pgp-enclose-buffer ()
2331 (let ((beg (save-excursion
2332 (goto-char (point-min))
2333 (if (search-forward (concat "\n" mail-header-separator "\n"))
2339 (cond ((eq mime-edit-pgp-processing 'sign)
2340 (mime-edit-enclose-pgp-signed-region beg end)
2342 ((eq mime-edit-pgp-processing 'encrypt)
2343 (mime-edit-enclose-pgp-encrypted-region beg end)
2351 (defun mime-edit-insert-partial-header (fields subject
2352 id number total separator)
2354 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2355 (insert mime-edit-mime-version-field-for-message/partial)
2357 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2358 id number total separator))
2361 (defun mime-edit-split-and-send
2362 (&optional cmd lines mime-edit-message-max-length)
2366 (count-lines (point-min) (point-max)))
2368 (or mime-edit-message-max-length
2369 (setq mime-edit-message-max-length
2370 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2371 mime-edit-message-default-max-lines))
2373 (let* ((mime-edit-draft-file-name
2374 (or (buffer-file-name)
2376 (expand-file-name "mime-draft" mime-temp-directory))))
2377 (separator mail-header-separator)
2379 (replace-space-with-underline (current-time-string))
2380 "@" (system-name) "\"")))
2381 (run-hooks 'mime-edit-before-split-hook)
2382 (let ((the-buf (current-buffer))
2383 (copy-buf (get-buffer-create " *Original Message*"))
2384 (header (std11-header-string-except
2385 mime-edit-split-ignored-field-regexp separator))
2386 (subject (mail-fetch-field "subject"))
2387 (total (+ (/ lines mime-edit-message-max-length)
2388 (if (> (mod lines mime-edit-message-max-length) 0)
2394 mime-edit-split-message-sender-alist))
2398 (error "Split sender is not specified for `%s'." major-mode)
2401 (mime-edit-partial-number 1)
2404 (set-buffer copy-buf)
2406 (insert-buffer the-buf)
2408 (if (re-search-forward
2409 (concat "^" (regexp-quote separator) "$") nil t)
2410 (let ((he (match-beginning 0)))
2412 (narrow-to-region (point-min) he)
2414 (goto-char (point-min))
2415 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2416 (delete-region (match-beginning 0)
2417 (1+ (std11-field-end)))
2419 (while (< mime-edit-partial-number total)
2422 (set-buffer copy-buf)
2423 (setq data (buffer-substring
2426 (goto-line mime-edit-message-max-length)
2429 (delete-region (point-min)(point))
2431 (mime-edit-insert-partial-header
2432 header subject id mime-edit-partial-number total separator)
2435 (message (format "Sending %d/%d..."
2436 mime-edit-partial-number total))
2437 (call-interactively command)
2438 (message (format "Sending %d/%d... done"
2439 mime-edit-partial-number total))
2441 (setq mime-edit-partial-number
2442 (1+ mime-edit-partial-number))
2446 (set-buffer copy-buf)
2447 (setq data (buffer-string))
2450 (mime-edit-insert-partial-header
2451 header subject id mime-edit-partial-number total separator)
2454 (message (format "Sending %d/%d..."
2455 mime-edit-partial-number total))
2456 (message (format "Sending %d/%d... done"
2457 mime-edit-partial-number total))
2461 (defun mime-edit-maybe-split-and-send (&optional cmd)
2463 (run-hooks 'mime-edit-before-send-hook)
2464 (let ((mime-edit-message-max-length
2465 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2466 mime-edit-message-default-max-lines))
2467 (lines (count-lines (point-min) (point-max)))
2469 (if (and (> lines mime-edit-message-max-length)
2470 mime-edit-split-message)
2471 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2475 ;;; @ preview message
2478 (defvar mime-edit-buffer nil) ; buffer local variable
2480 (defun mime-edit-preview-message ()
2481 "preview editing MIME message."
2483 (let* ((str (buffer-string))
2484 (separator mail-header-separator)
2485 (the-buf (current-buffer))
2486 (buf-name (buffer-name))
2487 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2488 (buf (get-buffer temp-buf-name))
2492 (switch-to-buffer buf)
2495 (setq buf (get-buffer-create temp-buf-name))
2496 (switch-to-buffer buf)
2499 (setq major-mode 'mime-temp-message-mode)
2500 (make-local-variable 'mail-header-separator)
2501 (setq mail-header-separator separator)
2502 (make-local-variable 'mime-edit-buffer)
2503 (setq mime-edit-buffer the-buf)
2505 (run-hooks 'mime-edit-translate-hook)
2506 (mime-edit-translate-buffer)
2507 (goto-char (point-min))
2508 (if (re-search-forward
2509 (concat "^" (regexp-quote separator) "$"))
2515 (defun mime-edit-quitting-method ()
2516 "Quitting method for mime-view."
2517 (let ((temp mime-raw-buffer)
2519 (mime-preview-kill-buffer)
2521 (setq buf mime-edit-buffer)
2523 (switch-to-buffer buf)
2526 (set-alist 'mime-preview-quitting-method-alist
2527 'mime-temp-message-mode
2528 #'mime-edit-quitting-method)
2534 (defvar mime-edit-again-ignored-field-regexp
2535 (concat "^\\(" "Content-.*\\|Mime-Version"
2536 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2538 "Regexp for deleted header fields when `mime-edit-again' is called.")
2540 (defun mime-edit-decode-buffer (not-decode-text)
2542 (goto-char (point-min))
2543 (let ((ctl (mime-read-Content-Type)))
2545 (let ((type (mime-content-type-primary-type ctl))
2546 (stype (mime-content-type-subtype ctl))
2547 (params (mime-content-type-parameters ctl)))
2549 ((and (eq type 'application)(eq stype 'pgp-signature))
2550 (delete-region (point-min)(point-max))
2552 ((eq type 'multipart)
2553 (let* ((boundary (cdr (assoc "boundary" params)))
2555 (concat "\n--" (regexp-quote boundary) "[ \t]*\n"))
2557 (re-search-forward boundary-pat nil t)
2558 (let ((bb (match-beginning 0)) eb tag)
2559 (setq tag (format "\n--<<%s>>-{\n" stype))
2562 (setq bb (+ bb (length tag)))
2564 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2566 (setq eb (match-beginning 0))
2567 (replace-match (format "--}-<<%s>>\n" stype))
2569 (narrow-to-region bb eb)
2570 (goto-char (point-min))
2571 (while (re-search-forward boundary-pat nil t)
2572 (let ((beg (match-beginning 0))
2574 (delete-region beg (match-end 0))
2576 (if (re-search-forward boundary-pat nil t)
2577 (setq end (match-beginning 0))
2578 (setq end (point-max))
2581 (narrow-to-region beg end)
2582 (mime-edit-decode-buffer not-decode-text)
2583 (goto-char (point-max))
2586 (goto-char (point-min))
2587 (or (= (point-min) 1)
2588 (delete-region (point-min)
2589 (if (search-forward "\n\n" nil t)
2595 (let* ((ctype (format "%s/%s" type stype))
2598 (let ((bytes (+ 14 (length ctype))))
2599 (mapconcat (function
2601 (if (string= (car attr) "charset")
2603 (setq charset (cdr attr))
2611 (setq bytes (+ bytes bs 2))
2614 (setq bytes (+ bs 1))
2622 (if (re-search-forward
2623 "Content-Transfer-Encoding:" nil t)
2624 (let ((beg (match-beginning 0))
2625 (hbeg (match-end 0))
2626 (end (std11-field-end)))
2628 (eliminate-top-spaces
2629 (std11-unfold-string
2630 (buffer-substring hbeg end))))
2631 (if (or charset (eq type 'text))
2633 (delete-region beg (1+ end))
2634 (goto-char (point-min))
2635 (if (search-forward "\n\n" nil t)
2638 (match-end 0)(point-max) encoding)
2642 (if (or encoded (not not-decode-text))
2643 (decode-mime-charset-region
2644 (point-min)(point-max)
2645 (or charset default-mime-charset))
2648 (if (re-search-forward "^$" nil t)
2652 (if (= (point-min) 1)
2658 (format "%s/%s%s" type stype pstr)
2661 (delete-region (point-min) he)
2664 (format "%s/%s%s" type stype pstr)
2669 (decode-mime-charset-region (point-min) (point-max)
2670 default-mime-charset)
2674 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2675 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2676 Content-Type and Content-Transfer-Encoding header fields will be
2677 converted to MIME-Edit tags."
2679 (goto-char (point-min))
2681 (concat "\n" (regexp-quote mail-header-separator) "\n")
2683 (replace-match "\n\n")
2685 (mime-edit-decode-buffer not-decode-text)
2686 (goto-char (point-min))
2688 (std11-narrow-to-header)
2689 (goto-char (point-min))
2690 (while (re-search-forward mime-edit-again-ignored-field-regexp nil t)
2691 (delete-region (match-beginning 0) (1+ (std11-field-end)))
2694 (and (re-search-forward "^$")
2695 (replace-match mail-header-separator)
2705 (provide 'mime-edit)
2707 (run-hooks 'mime-edit-load-hook)
2709 ;;; mime-edit.el ends here