2 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
4 ;;; Copyright (C) 1993 UMEDA Masanobu
5 ;;; Copyright (C) 1994 .. 1996 MORIOKA Tomohiko
7 ;;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
8 ;;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
10 ;;; Created: 1994/08/21 renamed from mime.el by UMEDA Masanobu;
11 ;;; 1996/05/24 renamed from tm-edit.el
12 ;;; Version: $Revision: 0.4 $
13 ;;; Keywords: mail, news, MIME, multimedia, multilingual
15 ;;; This file is part of SEMI (September, Emacs MIME Interface)
17 ;;; This program is free software; you can redistribute it and/or
18 ;;; modify it under the terms of the GNU General Public License as
19 ;;; published by the Free Software Foundation; either version 2, or
20 ;;; (at your option) any later version.
22 ;;; This program is distributed in the hope that it will be useful,
23 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 ;;; General Public License for more details.
27 ;;; You should have received a copy of the GNU General Public License
28 ;;; along with This program. If not, write to the Free Software
29 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
33 ;; This is an Emacs minor mode for editing Internet multimedia
34 ;; messages formatted in MIME (RFC 1521 and RFC 1522). All messages in
35 ;; this mode are composed in the tagged MIME format, that are
36 ;; described in the following examples. The messages composed in the
37 ;; tagged MIME format are automatically translated into a MIME
38 ;; compliant message when exiting the mode.
40 ;; Mule (a multilingual extension to Emacs 18 and 19) has a capability
41 ;; of handling multilingual text in limited ISO-2022 manner that is
42 ;; based on early experiences in Japanese Internet community and
43 ;; resulted in RFC 1468 (ISO-2022-JP charset for MIME). In order to
44 ;; enable multilingual capability in single text message in MIME,
45 ;; charset of multilingual text written in Mule is declared as either
46 ;; `ISO-2022-JP-2' [RFC 1554] or `ISO-2022-INT-1'. Mule is required
47 ;; for reading the such messages.
49 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
50 ;; News mode. First of all, you need the following autoload
51 ;; definition to load mime/editor-mode automatically:
53 ;; (autoload 'mime/editor-mode "mime-edit"
54 ;; "Minor mode for editing MIME message." t)
56 ;; In case of Mail mode (includes VM mode), you need the following
59 ;; (add-hook 'mail-mode-hook 'mime/editor-mode)
60 ;; (add-hook 'mail-send-hook 'mime-editor/maybe-translate)
62 ;; In case of MH-E, you need the following hook definition:
64 ;; (add-hook 'mh-letter-mode-hook
68 ;; (make-local-variable 'mail-header-separator)
69 ;; (setq mail-header-separator "--------")
71 ;; (add-hook 'mh-before-send-letter-hook 'mime-editor/maybe-translate)
73 ;; In case of News mode, you need the following hook definition:
75 ;; (add-hook 'news-reply-mode-hook 'mime/editor-mode)
76 ;; (add-hook 'news-inews-hook 'mime-editor/maybe-translate)
78 ;; In case of Emacs 19, it is possible to emphasize the message tags
79 ;; using font-lock mode as follows:
81 ;; (add-hook 'mime/editor-mode-hook
85 ;; (setq font-lock-keywords (list mime-editor/tag-regexp))
88 ;; The message tag looks like:
90 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
92 ;; The tagged MIME message examples:
94 ;; This is a conventional plain text. It should be translated into
98 ;; This is also a plain text. But, it is explicitly specified as is.
100 ;;--[[text/plain; charset=ISO-2022-JP]]
101 ;;
\e$B$3$l$O
\e(B charset
\e$B$r
\e(B ISO-2022-JP
\e$B$K;XDj$7$?F|K\8l$N
\e(B plain
\e$B%F%-%9%H$G$9
\e(B.
103 ;;--[[text/richtext]]
104 ;; <center>This is a richtext.</center>
106 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
108 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
113 (require 'mail-utils)
125 (defconst mime-editor/RCS-ID
126 "$Id: mime-edit.el,v 0.4 1996-05-25 14:42:47 morioka Exp $")
128 (defconst mime-editor/version (get-version-string mime-editor/RCS-ID))
130 (defconst mime-editor/version-name
131 (concat "SEMI mime-edit " mime-editor/version))
137 (defvar mime-prefix "\C-c\C-x"
138 "*Keymap prefix for MIME commands.")
140 (defvar mime-ignore-preceding-spaces nil
141 "*Ignore preceding white spaces if non-nil.")
143 (defvar mime-ignore-trailing-spaces nil
144 "*Ignore trailing white spaces if non-nil.")
146 (defvar 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.")
150 (defvar mime-auto-hide-body t
151 "*Hide non-textual body encoded in base64 after insertion if non-nil.")
153 (defvar mime-voice-recorder
154 (function mime-voice-recorder-for-sun)
155 "*Function to record a voice message and return a buffer that contains it.")
157 (defvar mime/editor-mode-hook nil
158 "*Hook called when enter MIME mode.")
160 (defvar mime-editor/translate-hook nil
161 "*Hook called before translating into a MIME compliant message.
162 To insert a signature file automatically, call the function
163 `mime-editor/insert-signature' from this hook.")
165 (defvar mime-editor/exit-hook nil
166 "*Hook called when exit MIME mode.")
168 (defvar mime-content-types
170 ;; Charset parameter need not to be specified, since it is
171 ;; defined automatically while translation.
173 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
176 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
179 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
182 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
185 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
193 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
194 ("directory" "/pub/GNU/elisp/mime")
196 ("mode" "image" "ascii" "local8"))
201 ("mode" "image" "ascii" "local8"))
202 ("tftp" ("site") ("name"))
203 ("afs" ("site") ("name"))
204 ("local-file" ("site") ("name"))
205 ("mail-server" ("server" "ftpmail@nic.karrn.ad.jp"))
210 ("octet-stream" ("type" "" "tar" "shar"))
212 ("x-kiss" ("x-cnf")))
225 "*Alist of content-type, subtype, parameters and its values.")
227 (defvar mime-file-types
229 "text" "richtext" nil
237 "application" "postscript" nil
239 "attachment" (("filename" . file))
244 "inline" (("filename" . file))
249 "inline" (("filename" . file))
254 "inline" (("filename" . file))
259 "inline" (("filename" . file))
264 "inline" (("filename" . file))
269 "inline" (("filename" . file))
274 "inline" (("filename" . file))
279 "attachment" (("filename" . file))
284 "attachment" (("filename" . file))
287 "application" "octet-stream" (("type" . "emacs-lisp"))
289 "attachment" (("filename" . file))
292 "application" "octet-stream" (("type" . "common-lisp"))
294 "attachment" (("filename" . file))
297 "application" "octet-stream" (("type" . "tar+gzip"))
299 "attachment" (("filename" . file))
302 "application" "octet-stream" (("type" . "tar+gzip"))
304 "attachment" (("filename" . file))
307 "application" "octet-stream" (("type" . "tar+compress"))
309 "attachment" (("filename" . file))
312 "application" "octet-stream" (("type" . "tar+compress"))
314 "attachment" (("filename" . file))
317 "application" "octet-stream" (("type" . "gzip"))
319 "attachment" (("filename" . file))
322 "application" "octet-stream" (("type" . "compress"))
324 "attachment" (("filename" . file))
327 "application" "octet-stream" (("type" . "lha"))
329 "attachment" (("filename" . file))
332 "application" "zip" nil
334 "attachment" (("filename" . file))
337 "application" "octet-stream" (("type" . "patch"))
339 "attachment" (("filename" . file))
342 "text" "plain" nil nil)
344 "application" "octet-stream" nil
346 "attachment" (("filename" . file))
349 "*Alist of file name, types, parameters, and default encoding.
350 If encoding is nil, it is determined from its contents.")
352 ;;; @@ about charset, encoding and transfer-level
355 (defvar mime-editor/transfer-level 7
356 "*A number of network transfer level. It should be bigger than 7.")
357 (make-variable-buffer-local 'mime-editor/transfer-level)
359 (defvar mime-editor/transfer-level-string
360 (mime/encoding-name mime-editor/transfer-level 'not-omit)
361 "*A string formatted version of mime/defaul-transfer-level")
362 (make-variable-buffer-local 'mime-editor/transfer-level-string)
364 (defvar mime-editor/charset-default-encoding-alist
365 (mime/make-charset-default-encoding-alist mime-editor/transfer-level))
366 (make-variable-buffer-local 'mime-editor/charset-default-encoding-alist)
368 ;;; @@ about message inserting
371 (defvar mime-editor/yank-ignored-field-list
372 '("Received" "Approved" "Path" "Replied" "Status" "X-VM-.*" "X-UIDL")
373 "Delete these fields from original message when it is inserted
374 as message/rfc822 part.
375 Each elements are regexp of field-name. [mime-edit.el]")
377 (defvar mime-editor/yank-ignored-field-regexp
379 (apply (function regexp-or) mime-editor/yank-ignored-field-list)
382 (defvar mime-editor/message-inserter-alist nil)
383 (defvar mime-editor/mail-inserter-alist nil)
385 ;;; @@ about message splitting
388 (defvar mime-editor/split-message t
389 "*Split large message if it is non-nil. [mime-edit.el]")
391 (defvar mime-editor/message-default-max-length 1000
392 "*Default maximum size of a message. [mime-edit.el]")
394 (defvar mime-editor/message-max-length-alist
395 '((news-reply-mode . 500)))
397 (defconst mime-editor/split-ignored-field-regexp
398 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\)")
400 (defvar mime-editor/split-blind-field-regexp
401 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)")
403 (defvar mime-editor/message-default-sender-alist
404 '((mail-mode . mail-send-and-exit)
405 (mh-letter-mode . mh-send-letter)
406 (news-reply-mode . gnus-inews-news)
409 (defvar mime-editor/split-message-sender-alist
416 . (lambda (&optional arg)
418 (write-region (point-min) (point-max)
419 mime-editor/draft-file-name nil 'no-message)
421 (pop-to-buffer "MH mail delivery")
423 (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
425 mh-send-args mime-editor/draft-file-name)
426 (goto-char (point-max)) ; show the interesting part
430 (apply 'mh-exec-cmd-quiet t mh-send-prog
432 (list "-nopush" "-nodraftfolder"
433 "-noverbose" "-nowatch"
434 mh-send-args mime-editor/draft-file-name)))))
438 (defvar mime-editor/window-config-alist
440 (mh-letter-mode . mh-previous-window-config)
441 (news-reply-mode . (cond ((boundp 'gnus-winconf-post-news)
443 gnus-winconf-post-news
444 (setq gnus-winconf-post-news nil)
446 ((boundp 'gnus-prev-winconf)
449 (setq gnus-prev-winconf nil)
454 (defvar mime-editor/news-reply-mode-server-running nil)
460 (defvar mime-editor/signing-type nil
461 "*PGP signing type (pgp-elkins, pgp-kazu or nil). [mime-edit.el]")
463 (defvar mime-editor/encrypting-type nil
464 "*PGP encrypting type (pgp-elkins, pgp-kazu or nil). [mime-edit.el]")
466 (if (or mime-editor/signing-type mime-editor/encrypting-type)
474 (defconst mime-editor/single-part-tag-regexp
475 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
476 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
478 (defconst mime-editor/quoted-single-part-tag-regexp
479 (concat "- " (substring mime-editor/single-part-tag-regexp 1)))
481 (defconst mime-editor/multipart-beginning-regexp "^--<<\\([^<>]+\\)>>-{\n")
483 (defconst mime-editor/multipart-end-regexp "^--}-<<\\([^<>]+\\)>>\n")
485 (defconst mime-editor/beginning-tag-regexp
486 (regexp-or mime-editor/single-part-tag-regexp
487 mime-editor/multipart-beginning-regexp))
489 (defconst mime-editor/end-tag-regexp
490 (regexp-or mime-editor/single-part-tag-regexp
491 mime-editor/multipart-end-regexp))
493 (defconst mime-editor/tag-regexp
494 (regexp-or mime-editor/single-part-tag-regexp
495 mime-editor/multipart-beginning-regexp
496 mime-editor/multipart-end-regexp))
498 (defvar mime-tag-format "--[[%s]]"
499 "*Control-string making a MIME tag.")
501 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
502 "*Control-string making a MIME tag with encoding.")
504 ;;; @@ multipart boundary
507 (defvar mime-multipart-boundary "Multipart"
508 "*Boundary of a multipart message.")
511 ;;; @@ buffer local variables
514 (defvar mime/editor-mode-old-local-map nil)
515 (defvar mime/editor-mode-old-selective-display nil)
516 (defvar mime/editing-buffer nil)
522 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
523 "*Specify MIME tspecials.
524 Tspecials means any character that matches with it in header must be quoted.")
526 (defconst mime-editor/mime-version-value
527 (concat "1.0 (" mime-editor/version-name ")")
528 "MIME version number.")
530 (defconst mime-editor/mime-map (make-sparse-keymap)
531 "Keymap for MIME commands.")
534 ;;; @ keymap and menu
537 (defvar mime/editor-mode-flag nil)
538 (make-variable-buffer-local 'mime/editor-mode-flag)
540 (set-alist 'minor-mode-alist
541 'mime/editor-mode-flag
542 '((" MIME-Edit " mime-editor/transfer-level-string)))
544 (defun mime-editor/define-keymap (keymap)
545 "Add mime-editor commands to KEYMAP."
546 (if (not (keymapp keymap))
548 (define-key keymap "\C-t" 'mime-editor/insert-text)
549 (define-key keymap "\C-i" 'mime-editor/insert-file)
550 (define-key keymap "\C-e" 'mime-editor/insert-external)
551 (define-key keymap "\C-v" 'mime-editor/insert-voice)
552 (define-key keymap "\C-y" 'mime-editor/insert-message)
553 (define-key keymap "\C-m" 'mime-editor/insert-mail)
554 (define-key keymap "\C-w" 'mime-editor/insert-signature)
555 (define-key keymap "\C-s" 'mime-editor/insert-signature)
556 (define-key keymap "\C-k" 'mime-editor/insert-key)
557 (define-key keymap "t" 'mime-editor/insert-tag)
558 (define-key keymap "a" 'mime-editor/enclose-alternative-region)
559 (define-key keymap "p" 'mime-editor/enclose-parallel-region)
560 (define-key keymap "m" 'mime-editor/enclose-mixed-region)
561 (define-key keymap "d" 'mime-editor/enclose-digest-region)
562 (define-key keymap "s" 'mime-editor/enclose-signed-region)
563 (define-key keymap "e" 'mime-editor/enclose-encrypted-region)
564 (define-key keymap "\C-p" 'mime-editor/preview-message)
565 (define-key keymap "\C-z" 'mime-editor/exit)
566 (define-key keymap "?" 'mime-editor/help)
569 (mime-editor/define-keymap mime-editor/mime-map)
571 (defconst mime-editor/menu-title "MIME-Edit")
573 (defconst mime-editor/menu-list
574 '((mime-help "Describe MIME editor mode" mime-editor/help)
575 (file "Insert File" mime-editor/insert-file)
576 (external "Insert External" mime-editor/insert-external)
577 (voice "Insert Voice" mime-editor/insert-voice)
578 (message "Insert Message" mime-editor/insert-message)
579 (mail "Insert Mail" mime-editor/insert-mail)
580 (signature "Insert Signature" mime-editor/insert-signature)
581 (text "Insert Text" mime-editor/insert-text)
582 (tag "Insert Tag" mime-editor/insert-tag)
583 (alternative "Enclose as alternative"
584 mime-editor/enclose-alternative-region)
585 (parallel "Enclose as parallel" mime-editor/enclose-parallel-region)
586 (mixed "Enclose as serial" mime-editor/enclose-mixed-region)
587 (digest "Enclose as digest" mime-editor/enclose-digest-region)
588 (signed "Enclose as signed" mime-editor/enclose-signed-region)
589 (encrypted "Enclose as encrypted" mime-editor/enclose-encrypted-region)
590 (key "Insert Public Key" mime-editor/insert-key)
591 (split "About split" mime-editor/set-split)
592 (sign "About sign" mime-editor/set-sign)
593 (encrypt "About encryption" mime-editor/set-encrypt)
594 (preview "Preview Message" mime-editor/preview-message)
595 (level "Toggle transfer-level" mime-editor/toggle-transfer-level)
597 "MIME-edit menubar entry.")
599 (defun mime-editor/define-menu-for-emacs19 ()
600 "Define menu for Emacs 19."
601 (define-key (current-local-map) [menu-bar mime-edit]
602 (cons mime-editor/menu-title
603 (make-sparse-keymap mime-editor/menu-title)))
606 (define-key (current-local-map)
607 (vector 'menu-bar 'mime-edit (car item))
608 (cons (nth 1 item)(nth 2 item))
611 (reverse mime-editor/menu-list)
614 ;;; modified by Pekka Marjola <pema@iki.fi>
615 ;;; 1995/9/5 (c.f. [tm-en:69])
616 (defun mime-editor/define-menu-for-xemacs ()
617 "Define menu for Emacs 19."
618 (cond ((featurep 'menubar)
619 (make-local-variable 'current-menubar)
620 (set-buffer-menubar current-menubar)
622 (cons mime-editor/menu-title
625 (vector (nth 1 item)(nth 2 item)
626 mime/editor-mode-flag)
628 mime-editor/menu-list)))
631 ;;; modified by Steven L. Baur <steve@miranova.com>
632 ;;; 1995/12/6 (c.f. [tm-en:209])
633 (if (and running-xemacs (not (boundp 'mime-editor/popup-menu-for-xemacs)))
634 (setq mime-editor/popup-menu-for-xemacs
635 (append '("MIME Commands" "---")
636 (mapcar (function (lambda (item)
640 mime-editor/menu-list)))
649 (defun mime/editor-mode ()
650 "MIME minor mode for editing the tagged MIME message.
652 In this mode, basically, the message is composed in the tagged MIME
653 format. The message tag looks like:
655 `--[[text/plain; charset=ISO-2022-JP][7bit]]'.
657 The tag specifies the MIME content type, subtype, optional parameters
658 and transfer encoding of the message following the tag. Messages
659 without any tag are treated as `text/plain' by default. Charset and
660 transfer encoding are automatically defined unless explicitly
661 specified. Binary messages such as audio and image are usually hidden
662 using selective-display facility. The messages in the tagged MIME
663 format are automatically translated into a MIME compliant message when
666 Available charsets depend on Emacs version being used. The following
667 lists the available charsets of each emacs.
669 Emacs18: US-ASCII is only available.
670 NEmacs: US-ASCII and ISO-2022-JP are available.
671 Emacs19: US-ASCII and ISO-8859-1 are available.
672 Mule: US-ASCII, ISO-8859-* (except for ISO-8859-6),
673 ISO-2022-JP, ISO-2022-JP-2 and ISO-2022-INT-1 are available.
675 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in Mule is expected to
676 be used to represent multilingual text in intermixed manner. Any
677 languages that has no registered charset are represented as either
678 ISO-2022-JP-2 or ISO-2022-INT-1 in Mule.
680 Following commands are available in addition to major mode commands:
681 \\[mime-editor/insert-text] insert a text message.
682 \\[mime-editor/insert-file] insert a (binary) file.
683 \\[mime-editor/insert-external] insert a reference to external body.
684 \\[mime-editor/insert-voice] insert a voice message.
685 \\[mime-editor/insert-message] insert a mail or news message.
686 \\[mime-editor/insert-mail] insert a mail message.
687 \\[mime-editor/insert-signature] insert a signature file at end.
688 \\[mime-editor/insert-tag] insert a new MIME tag.
689 \\[mime-editor/enclose-alternative-region] enclose as multipart/alternative.
690 \\[mime-editor/enclose-parallel-region] enclose as multipart/parallel.
691 \\[mime-editor/enclose-mixed-region] enclose as multipart/mixed.
692 \\[mime-editor/enclose-digest-region] enclose as multipart/digest.
693 \\[mime-editor/enclose-signed-region] enclose as PGP signed.
694 \\[mime-editor/enclose-encrypted-region] enclose as PGP encrypted.
695 \\[mime-editor/insert-key] insert PGP public key.
696 \\[mime-editor/preview-message] preview editing MIME message.
697 \\[mime-editor/exit] exit and translate into a MIME compliant message.
698 \\[mime-editor/maybe-translate] exit and translate if in MIME mode, then split.
699 \\[mime-editor/help] show this help.
701 Additional commands are available in some major modes:
702 C-c C-c exit, translate and run the original command.
703 C-c C-s exit, translate and run the original command.
705 The following is a message example written in the tagged MIME format.
706 TABs at the beginning of the line are not a part of the message:
708 This is a conventional plain text. It should be translated
711 This is also a plain text. But, it is explicitly specified as
713 --[[text/plain; charset=ISO-2022-JP]]
714 \e$B$3$l$O
\e(B charset
\e$B$r
\e(B ISO-2022-JP
\e$B$K;XDj$7$?F|K\8l$N
\e(B plain
\e$B%F%-%9
\e(B
717 <center>This is a richtext.</center>
718 --[[image/gif][base64]]^M...image encoded in base64 here...
719 --[[audio/basic][base64]]^M...audio encoded in base64 here...
721 User customizable variables (not documented all of them):
723 Specifies a key prefix for MIME minor mode commands.
725 mime-ignore-preceding-spaces
726 Preceding white spaces in a message body are ignored if non-nil.
728 mime-ignore-trailing-spaces
729 Trailing white spaces in a message body are ignored if non-nil.
731 mime-auto-fill-header
732 Fill header fields that contain encoded-words if non-nil.
735 Hide a non-textual body message encoded in base64 after insertion
739 Specifies a function to record a voice message and return a buffer
740 that contains it. The function mime-voice-recorder-for-sun is for
743 mime/editor-mode-hook
744 Turning on MIME mode calls the value of mime/editor-mode-hook, if
747 mime-editor/translate-hook
748 The value of mime-editor/translate-hook is called just before translating
749 the tagged MIME format into a MIME compliant message if it is
750 non-nil. If the hook call the function mime-editor/insert-signature,
751 the signature file will be inserted automatically.
753 mime-editor/exit-hook
754 Turning off MIME mode calls the value of mime-editor/exit-hook, if it is
757 (if mime/editor-mode-flag
758 (error "You are already editing a MIME message.")
759 (setq mime/editor-mode-flag t)
760 ;; Remember old key bindings.
763 (make-local-variable 'mime/editor-mode-old-local-map)
764 (setq mime/editor-mode-old-local-map (current-local-map))
765 ;; Add MIME commands to current local map.
766 (use-local-map (copy-keymap (current-local-map)))
768 (if (not (lookup-key (current-local-map) mime-prefix))
769 (define-key (current-local-map) mime-prefix mime-editor/mime-map))
771 ;; Set transfer level into mode line
773 (setq mime-editor/transfer-level-string
774 (mime/encoding-name mime-editor/transfer-level 'not-omit))
775 (force-mode-line-update)
777 ;; Define menu. Menus for other emacs implementations are
779 (cond (running-xemacs
780 (mime-editor/define-menu-for-xemacs))
781 ((>= emacs-major-version 19)
782 (mime-editor/define-menu-for-emacs19)
786 ;; Remember old selective-display.
787 (make-local-variable 'mime/editor-mode-old-selective-display)
788 (setq mime/editor-mode-old-selective-display selective-display)
789 (setq selective-display t)
790 ;; I don't care about saving these.
791 (setq paragraph-start
792 (regexp-or mime-editor/single-part-tag-regexp
794 (setq paragraph-separate
795 (regexp-or mime-editor/single-part-tag-regexp
797 (run-hooks 'mime/editor-mode-hook)
799 (substitute-command-keys
800 "Type \\[mime-editor/exit] to exit MIME mode, and type \\[mime-editor/help] to get help."))
804 (defalias 'edit-mime 'mime/editor-mode) ; for convenience
805 (defalias 'mime-mode 'mime/editor-mode) ; for convenience
807 (defun mime-editor/exit (&optional nomime no-error)
808 "Translate the tagged MIME message into a MIME compliant message.
809 With no argument encode a message in the buffer into MIME, otherwise
810 just return to previous mode."
812 (if (not mime/editor-mode-flag)
814 (error "You aren't editing a MIME message.")
818 (run-hooks 'mime-editor/translate-hook)
819 (mime-editor/translate-buffer)))
820 ;; Restore previous state.
821 (setq mime/editor-mode-flag nil)
822 (cond (running-xemacs
823 ;; mime-prefix only defined if binding was nil
824 (if (eq (lookup-key (current-local-map) mime-prefix)
825 mime-editor/mime-map)
826 (define-key (current-local-map) mime-prefix nil))
827 (delete-menu-item (list mime-editor/menu-title)))
829 (use-local-map mime/editor-mode-old-local-map)))
831 (setq selective-display mime/editor-mode-old-selective-display)
832 (set-buffer-modified-p (buffer-modified-p))
833 (run-hooks 'mime-editor/exit-hook)
834 (message "Exit MIME editor mode.")
837 (defun mime-editor/maybe-translate ()
839 (mime-editor/exit nil t)
840 (call-interactively 'mime-editor/maybe-split-and-send)
843 (defun mime-editor/help ()
844 "Show help message about MIME mode."
846 (with-output-to-temp-buffer "*Help*"
847 (princ "MIME editor mode:\n")
848 (princ (documentation 'mime/editor-mode))
849 (print-help-return-message)))
851 (defun mime-editor/insert-text ()
852 "Insert a text message.
853 Charset is automatically obtained from the `mime/lc-charset-alist'."
855 (if (and (mime-editor/insert-tag "text" nil nil)
856 (looking-at mime-editor/single-part-tag-regexp))
858 ;; Make a space between the following message.
863 (defun mime-editor/insert-file (file)
864 "Insert a message from a file."
865 (interactive "fInsert file as MIME message: ")
866 (let* ((guess (mime-find-file-type file))
867 (pritype (nth 0 guess))
868 (subtype (nth 1 guess))
869 (parameters (nth 2 guess))
870 (default (nth 3 guess)) ;Guess encoding from its file name.
871 (disposition-type (nth 4 guess))
872 (disposition-params (nth 5 guess))
874 (if (not (interactive-p))
877 (concat "What transfer encoding"
880 (if (string-equal default "")
886 mime-encoding-method-alist nil t nil))))
887 (if (string-equal encoding "")
888 (setq encoding default))
889 (if (or (consp parameters) (stringp disposition-type))
890 (let ((rest parameters) cell attribute value)
893 (setq cell (car rest))
894 (setq attribute (car cell))
895 (setq value (cdr cell))
897 (setq value (rfc822/wrap-as-quoted-string
898 (file-name-nondirectory file)))
900 (setq parameters (concat parameters "; " attribute "=" value))
901 (setq rest (cdr rest))
906 (concat parameters "\n"
907 "Content-Disposition: " disposition-type))
908 (setq rest disposition-params)
910 (setq cell (car rest))
911 (setq attribute (car cell))
912 (setq value (cdr cell))
914 (setq value (rfc822/wrap-as-quoted-string
915 (file-name-nondirectory file)))
918 (concat parameters "; " attribute "=" value))
919 (setq rest (cdr rest))
923 (mime-editor/insert-tag pritype subtype parameters)
924 (mime-editor/insert-binary-file file encoding)
927 (defun mime-editor/insert-external ()
928 "Insert a reference to external body."
930 (mime-editor/insert-tag "message" "external-body" nil ";\n\t")
932 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
934 (let* ((pritype (mime-prompt-for-type))
935 (subtype (mime-prompt-for-subtype pritype))
936 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
939 (insert "Content-Type: "
940 pritype "/" subtype (or parameters "") "\n")))
941 (if (and (not (eobp))
942 (not (looking-at mime-editor/single-part-tag-regexp)))
943 (insert (mime-make-text-tag) "\n")))
945 (defun mime-editor/insert-voice ()
946 "Insert a voice message."
948 (mime-editor/insert-tag "audio" "basic" nil)
949 (let ((buffer (funcall mime-voice-recorder)))
951 (mime-editor/insert-binary-buffer buffer "base64")
955 (defun mime-editor/insert-signature (&optional arg)
956 "Insert a signature file."
958 (let ((signature-insert-hook
961 (apply (function mime-editor/insert-tag)
962 (mime-find-file-type signature))
965 (insert-signature arg)
969 ;; Insert a new tag around a point.
971 (defun mime-editor/insert-tag (&optional pritype subtype parameters delimiter)
972 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
973 If nothing is inserted, return nil."
978 exist-prev-tag exist-next-tag)
981 (mime-prompt-for-type)))
984 (mime-prompt-for-subtype pritype)))
987 (mime-prompt-for-parameters pritype subtype delimiter)))
988 ;; Make a new MIME tag.
989 (setq newtag (mime-make-tag pritype subtype parameters))
990 ;; Find an current MIME tag.
993 (if (mime-editor/goto-tag)
995 (if (eq current (match-beginning 0))
996 (setq exist-next-tag t)
997 (setq exist-prev-tag t)
999 (buffer-substring (match-beginning 0) (match-end 0))
1001 ;; Assume content type is 'text/plan'.
1002 (mime-make-tag "text" "plain")
1004 ;; We are only interested in TEXT.
1006 (not (mime-test-content-type
1007 (mime-editor/get-contype oldtag) "text")))
1009 (cond (exist-prev-tag (insert "\n"))
1010 (exist-next-tag (save-excursion
1019 (if (or (not oldtag) ;Not text
1020 (or mime-ignore-same-text-tag
1021 (not (string-equal oldtag newtag))))
1023 ;; Mark the beginning of the tag for convenience.
1024 (push-mark (point) 'nomsg)
1025 (insert newtag "\n")
1026 (list pritype subtype parameters) ;New tag is created.
1028 ;; Restore previous point.
1030 nil ;Nothing is created.
1034 ;; Insert the binary content after MIME tag.
1035 ;; modified by MORITA Masahiro <hiro@isl.ntt.JP>
1037 (defun mime-editor/insert-binary-file (file &optional encoding)
1038 "Insert binary FILE at point.
1039 Optional argument ENCODING specifies an encoding method such as base64."
1040 (let ((tmpbuf (get-buffer-create " *MIME insert*")))
1044 (let ((mc-flag nil) ;Mule
1045 (file-coding-system-for-read
1046 (if (featurep 'mule) *noconv*))
1047 (kanji-flag nil) ;NEmacs
1048 (emx-binary-mode t) ;Stop CRLF to LF conversion in OS/2
1050 (let (jka-compr-compression-info-list
1051 jam-zcat-filename-list)
1052 (insert-file-contents file))))
1054 (if (and (stringp encoding)
1055 (string-equal (downcase encoding) "x-uue"))
1058 (let ((uuencode-external-encoder
1059 (cons (car uuencode-external-encoder)
1060 (list (file-name-nondirectory file))
1062 (mime-editor/insert-binary-buffer tmpbuf encoding)
1064 (mime-editor/insert-binary-buffer tmpbuf encoding))
1065 (kill-buffer tmpbuf))))
1067 ;; Insert the binary content after MIME tag.
1068 ;; modified by MORITA Masahiro <hiro@isl.ntt.JP>
1070 (defun mime-editor/insert-binary-buffer (buffer &optional encoding)
1071 "Insert binary BUFFER at point.
1072 Optional argument ENCODING specifies an encoding method such as base64."
1073 (let* ((tagend (1- (point))) ;End of the tag
1074 (hide-p (and mime-auto-hide-body
1076 (let ((en (downcase encoding)))
1077 (or (string-equal en "base64")
1078 (string-equal en "x-uue")
1082 (narrow-to-region (1- (point)) (point))
1083 (let ((start (point))
1084 (emx-binary-mode t)) ;Stop LF to CRLF conversion in OS/2
1085 (insert-buffer-substring buffer)
1086 ;; Encode binary message if necessary.
1088 (mime-encode-region start (point-max) encoding)
1092 (mime-flag-region (point-min) (1- (point-max)) ?\^M)
1093 (goto-char (point-max)))
1095 ;; Define encoding even if it is 7bit.
1096 (if (stringp encoding)
1098 (goto-char tagend) ;Make sure which line the tag is on.
1099 (mime-editor/define-encoding encoding)))
1103 ;; Commands work on a current message flagment.
1105 (defun mime-editor/goto-tag ()
1106 "Search for the beginning of the tagged MIME message."
1107 (let ((current (point)) multipart)
1108 (if (looking-at mime-editor/tag-regexp)
1110 ;; At first, go to the end.
1111 (cond ((re-search-forward mime-editor/beginning-tag-regexp nil t)
1112 (goto-char (1- (match-beginning 0))) ;For multiline tag
1115 (goto-char (point-max))
1117 ;; Then search for the beginning.
1118 (re-search-backward mime-editor/end-tag-regexp nil t)
1119 (or (looking-at mime-editor/beginning-tag-regexp)
1120 ;; Restore previous point.
1127 (defun mime-editor/content-beginning ()
1128 "Return the point of the beginning of content."
1130 (let ((beg (save-excursion
1131 (beginning-of-line) (point))))
1132 (if (mime-editor/goto-tag)
1133 (let ((top (point)))
1134 (goto-char (match-end 0))
1135 (if (and (= beg top)
1136 (= (following-char) ?\^M))
1140 ;; Default text/plain tag.
1141 (goto-char (point-min))
1143 (concat "\n" (regexp-quote mail-header-separator)
1144 (if mime-ignore-preceding-spaces
1145 "[ \t\n]*\n" "\n")) nil 'move)
1149 (defun mime-editor/content-end ()
1150 "Return the point of the end of content."
1152 (let ((beg (save-excursion
1153 (beginning-of-line) (point))))
1154 (if (mime-editor/goto-tag)
1155 (let ((top (point)))
1156 (goto-char (match-end 0))
1157 (if (and (= beg top) ;Must be on the same line.
1158 (= (following-char) ?\^M))
1162 ;; Move to the end of this text.
1163 (if (re-search-forward mime-editor/tag-regexp nil 'move)
1164 ;; Don't forget a multiline tag.
1165 (goto-char (match-beginning 0)))
1168 ;; Assume the message begins with text/plain.
1169 (goto-char (mime-editor/content-beginning))
1170 (if (re-search-forward mime-editor/tag-regexp nil 'move)
1171 ;; Don't forget a multiline tag.
1172 (goto-char (match-beginning 0)))
1176 (defun mime-editor/define-charset (charset)
1177 "Set charset of current tag to CHARSET."
1179 (if (mime-editor/goto-tag)
1180 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1181 (delete-region (match-beginning 0) (match-end 0))
1183 (mime-create-tag (mime-set-parameter
1184 (mime-editor/get-contype tag) "charset" charset)
1185 (mime-editor/get-encoding tag))))
1188 (defun mime-editor/define-encoding (encoding)
1189 "Set encoding of current tag to ENCODING."
1191 (if (mime-editor/goto-tag)
1192 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1193 (delete-region (match-beginning 0) (match-end 0))
1194 (insert (mime-create-tag (mime-editor/get-contype tag) encoding)))
1197 (defun mime-editor/choose-charset ()
1198 "Choose charset of a text following current point."
1199 (mime/find-charset-region (point) (mime-editor/content-end))
1202 (defun mime-make-text-tag (&optional subtype)
1203 "Make a tag for a text after current point.
1204 Subtype of text type can be specified by an optional argument SUBTYPE.
1205 Otherwise, it is obtained from mime-content-types."
1206 (let* ((pritype "text")
1207 (subtype (or subtype
1208 (car (car (cdr (assoc pritype mime-content-types)))))))
1209 ;; Charset should be defined later.
1210 (mime-make-tag pritype subtype)))
1213 ;; Tag handling functions
1215 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1216 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1217 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1221 (defun mime-create-tag (contype &optional encoding)
1222 "Make a tag with CONTENT-TYPE and optional ENCODING."
1223 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1226 (defun mime-editor/get-contype (tag)
1227 "Return Content-Type (including parameters) of TAG."
1229 (or (string-match mime-editor/single-part-tag-regexp tag)
1230 (string-match mime-editor/multipart-beginning-regexp tag)
1231 (string-match mime-editor/multipart-end-regexp tag)
1233 (substring tag (match-beginning 1) (match-end 1))
1236 (defun mime-editor/get-encoding (tag)
1237 "Return encoding of TAG."
1239 (string-match mime-editor/single-part-tag-regexp tag)
1241 (not (= (match-beginning 3) (match-end 3)))
1242 (substring tag (match-beginning 3) (match-end 3))))
1244 (defun mime-get-parameter (contype parameter)
1245 "For given CONTYPE return value for PARAMETER.
1246 Nil if no such parameter."
1250 (regexp-quote parameter)
1251 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1253 (substring contype (match-beginning 1) (match-end 1))
1254 nil ;No such parameter
1257 (defun mime-set-parameter (contype parameter value)
1258 "For given CONTYPE set PARAMETER to VALUE."
1262 (regexp-quote parameter)
1263 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1266 (concat (substring contype 0 (match-beginning 1))
1268 (substring contype (match-end 1)))
1269 (concat contype "; " parameter "=" value)))
1271 (defun mime-strip-parameters (contype)
1272 "Return primary content-type and subtype without parameters for CONTYPE."
1273 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1274 (substring contype (match-beginning 1) (match-end 1)) nil))
1276 (defun mime-test-content-type (contype type &optional subtype)
1277 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1278 (and (stringp contype)
1281 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1282 (downcase contype))))
1287 (defun mime-find-file-type (file)
1288 "Guess Content-Type, subtype, and parameters from FILE."
1290 (guesses mime-file-types))
1291 (while (and (not guess) guesses)
1292 (if (string-match (car (car guesses)) file)
1293 (setq guess (cdr (car guesses))))
1294 (setq guesses (cdr guesses)))
1298 (defun mime-prompt-for-type ()
1299 "Ask for Content-type."
1301 ;; Repeat until primary content type is specified.
1302 (while (string-equal type "")
1304 (completing-read "What content type: "
1307 'require-match ;Type must be specified.
1310 (if (string-equal type "")
1312 (message "Content type is required.")
1320 (defun mime-prompt-for-subtype (pritype)
1321 "Ask for Content-type subtype of Content-Type PRITYPE."
1322 (let* ((default (car (car (cdr (assoc pritype mime-content-types)))))
1327 "What content subtype: (default " default ") ")
1328 "What content subtype: ")
1329 (cdr (assoc pritype mime-content-types))
1331 'require-match ;Subtype must be specified.
1334 (if (string-equal answer "") default answer)))
1336 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1337 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1338 Optional DELIMITER specifies parameter delimiter (';' by default)."
1339 (let* ((delimiter (or delimiter "; "))
1344 (mime-prompt-for-parameters-1
1346 (cdr (assoc pritype mime-content-types))))))
1349 (if (and (stringp parameters)
1350 (not (string-equal parameters "")))
1351 (concat delimiter parameters)
1352 "" ;"" if no parameters
1355 (defun mime-prompt-for-parameters-1 (optlist)
1356 (apply (function append)
1357 (mapcar (function mime-prompt-for-parameter) optlist)))
1359 (defun mime-prompt-for-parameter (parameter)
1361 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1362 (let* ((prompt (car parameter))
1363 (choices (mapcar (function
1365 (if (consp e) e (list e))))
1367 (default (car (car choices)))
1373 (concat "What " prompt
1375 (if (string-equal default "") "\"\"" default)
1377 choices nil nil ""))
1378 ;; If nothing is selected, use default.
1379 (if (string-equal answer "")
1380 (setq answer default)))
1382 (read-string (concat "What " prompt ": "))))
1383 (cons (if (and answer
1384 (not (string-equal answer "")))
1386 ;; Note: control characters ignored!
1387 (if (string-match mime-tspecials-regexp answer)
1388 (concat "\"" answer "\"") answer)))
1389 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1392 (defun mime-flag-region (from to flag)
1393 "Hides or shows lines from FROM to TO, according to FLAG.
1394 If FLAG is `\\n' (newline character) then text is shown,
1395 while if FLAG is `\\^M' (control-M) the text is hidden."
1396 (let ((buffer-read-only nil) ;Okay even if write protected.
1397 (modp (buffer-modified-p)))
1399 (subst-char-in-region from to
1400 (if (= flag ?\n) ?\^M ?\n)
1402 (set-buffer-modified-p modp))))
1405 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1408 (defvar mime-editor/translate-buffer-hook
1409 '(mime-editor/pgp-enclose-buffer
1410 mime/encode-message-header
1411 mime-editor/translate-body))
1413 (defun mime-editor/translate-buffer ()
1414 "Encode the tagged MIME message in current buffer in MIME compliant message."
1416 (if (catch 'mime-editor/error
1418 (run-hooks 'mime-editor/translate-buffer-hook)
1422 (error "Translation error!")
1425 (defun mime-editor/find-inmost ()
1426 (goto-char (point-min))
1427 (if (re-search-forward mime-editor/multipart-beginning-regexp nil t)
1428 (let ((bb (match-beginning 0))
1430 (type (buffer-substring (match-beginning 1)(match-end 1)))
1432 (setq end-exp (format "^--}-<<%s>>\n" type))
1434 (if (re-search-forward end-exp nil t)
1436 (setq eb (match-beginning 0))
1437 (setq ee (match-end 0))
1439 (setq eb (point-max))
1440 (setq ee (point-max))
1442 (narrow-to-region be eb)
1444 (if (re-search-forward mime-editor/multipart-beginning-regexp nil t)
1446 (narrow-to-region (match-beginning 0)(point-max))
1447 (mime-editor/find-inmost)
1450 (list type bb be eb)
1453 (defun mime-editor/process-multipart-1 (boundary)
1454 (let ((ret (mime-editor/find-inmost)))
1456 (let ((type (car ret))
1457 (bb (nth 1 ret))(be (nth 2 ret))
1460 (narrow-to-region bb eb)
1461 (delete-region bb be)
1462 (setq bb (point-min))
1463 (setq eb (point-max))
1466 (if (looking-at mime-editor/multipart-end-regexp)
1467 (let ((beg (match-beginning 0))
1470 (delete-region beg end)
1471 (if (and (not (looking-at mime-editor/single-part-tag-regexp))
1473 (insert (concat (mime-make-text-tag) "\n"))
1475 (cond ((string-equal type "quote")
1476 (mime-editor/enquote-region bb eb)
1478 ((string= type "signed")
1479 (cond ((eq mime-editor/signing-type 'pgp-elkins)
1480 (mime-editor/sign-pgp-elkins bb eb boundary)
1482 ((eq mime-editor/signing-type 'pgp-kazu)
1483 (mime-editor/sign-pgp-kazu bb eb boundary)
1486 ((string= type "encrypted")
1487 (cond ((eq mime-editor/encrypting-type 'pgp-elkins)
1488 (mime-editor/encrypt-pgp-elkins bb eb boundary)
1490 ((eq mime-editor/encrypting-type 'pgp-kazu)
1491 (mime-editor/encrypt-pgp-kazu bb eb boundary)
1495 (nth 2 (mime-editor/translate-region bb eb
1499 (format "--[[multipart/%s;
1500 boundary=\"%s\"][7bit]]\n"
1505 (defun mime-editor/enquote-region (beg end)
1508 (narrow-to-region beg end)
1510 (while (re-search-forward mime-editor/single-part-tag-regexp nil t)
1511 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1512 (replace-match (concat "- " (substring tag 1)))
1515 (defun mime-editor/dequote-region (beg end)
1518 (narrow-to-region beg end)
1520 (while (re-search-forward
1521 mime-editor/quoted-single-part-tag-regexp nil t)
1522 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1523 (replace-match (concat "-" (substring tag 2)))
1526 (autoload 'mc-pgp-lookup-key "mc-pgp")
1527 (autoload 'mc-pgp-sign-region "mc-pgp")
1528 (autoload 'mc-pgp-encrypt-region "mc-pgp")
1530 (defun tm:mc-pgp-generic-parser (result)
1531 (let ((ret (mc-pgp-generic-parser result)))
1533 (vector (car ret)(cdr ret))
1536 (defun tm:mc-process-region
1537 (beg end passwd program args parser &optional buffer boundary)
1538 (let ((obuf (current-buffer))
1539 (process-connection-type nil)
1540 mybuf result rgn proc)
1543 (setq mybuf (or buffer (generate-new-buffer " *mailcrypt temp")))
1547 (buffer-disable-undo mybuf)
1549 (apply 'start-process "*PGP*" mybuf program args))
1552 (process-send-string proc (concat passwd "\n"))
1553 (or mc-passwd-timeout (mc-deactivate-passwd t))))
1554 (process-send-region proc beg end)
1555 (process-send-eof proc)
1556 (while (eq 'run (process-status proc))
1557 (accept-process-output proc 5))
1558 (setq result (process-exit-status proc))
1559 ;; Hack to force a status_notify() in Emacs 19.29
1560 (delete-process proc)
1562 (goto-char (point-max))
1563 (if (re-search-backward "\nProcess \\*PGP.*\n\\'" nil t)
1564 (delete-region (match-beginning 0) (match-end 0)))
1565 (goto-char (point-min))
1567 (while (search-forward "\r\n" nil t)
1568 (replace-match "\n"))
1569 ;; Hurm. FIXME; must get better result codes.
1570 (if (stringp result)
1571 (error "%s exited abnormally: '%s'" program result)
1572 (setq rgn (funcall parser result))
1573 ;; If the parser found something, migrate it
1579 (narrow-to-region beg end)
1581 (insert (format "--%s\n" boundary))
1582 (goto-char (point-max))
1583 (insert (format "\n--%s
1584 Content-Type: application/pgp-signature
1585 Content-Transfer-Encoding: 7bit
1588 (insert-buffer-substring mybuf (car rgn) (cdr rgn))
1589 (goto-char (point-max))
1590 (insert (format "\n--%s--\n" boundary))
1592 (delete-region beg end)
1594 (insert-buffer-substring mybuf (car rgn) (cdr rgn))
1597 (delete-region (car rgn) (cdr rgn)))))
1598 ;; Return nil on failure and exit code on success
1600 ;; Cleanup even on nonlocal exit
1601 (if (and proc (eq 'run (process-status proc)))
1602 (interrupt-process proc))
1604 (or buffer (null mybuf) (kill-buffer mybuf)))))
1606 (defun tm:mc-pgp-sign-region (start end &optional id unclear boundary)
1607 (if (not (boundp 'mc-pgp-user-id))
1610 (let ((process-environment process-environment)
1611 (buffer (get-buffer-create mc-buffer-name))
1613 (parser (function mc-pgp-generic-parser))
1614 (pgp-path mc-pgp-path)
1616 (setq key (mc-pgp-lookup-key (or id mc-pgp-user-id)))
1620 (format "PGP passphrase for %s (%s): " (car key) (cdr key))))
1621 (setenv "PGPPASSFD" "0")
1627 (list "+verbose=1" "+language=en"
1628 (format "+clearsig=%s" (if unclear "off" "on"))
1629 "+batchmode" "-u" (cdr key))))
1631 (setq args (cons (format "+comment=%s" mc-pgp-comment) args))
1633 (message "Signing as %s ..." (car key))
1634 (if (tm:mc-process-region
1635 start end passwd pgp-path args parser buffer boundary)
1639 (goto-char (point-min))
1642 --[[multipart/signed; protocol=\"application/pgp-signature\";
1643 boundary=\"%s\"; micalg=pgp-md5][7bit]]\n" boundary))
1645 (message "Signing as %s ... Done." (car key))
1649 (defun mime-editor/sign-pgp-elkins (beg end boundary)
1652 (narrow-to-region beg end)
1654 (mime-editor/translate-region beg end boundary))
1656 (encoding (nth 1 ret))
1658 (pgp-boundary (concat "pgp-sign-" boundary))
1661 (insert (format "Content-Type: %s\n" ctype))
1663 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1666 (or (tm:mc-pgp-sign-region (point-min)(point-max)
1667 nil nil pgp-boundary)
1668 (throw 'mime-editor/error 'pgp-error)
1672 (defun mime-editor/encrypt-pgp-elkins (beg end boundary)
1675 (let ((from (rfc822/get-field-body "From"))
1676 (to (rfc822/get-field-body "To"))
1677 (cc (rfc822/get-field-body "cc"))
1679 (narrow-to-region beg end)
1681 (mime-editor/translate-region beg end boundary))
1683 (encoding (nth 1 ret))
1685 (pgp-boundary (concat "pgp-" boundary))
1688 (if (and (stringp from)
1689 (not (string-equal from "")))
1690 (insert (format "From: %s\n" from))
1692 (if (and (stringp to)
1693 (not (string-equal to "")))
1695 (insert (format "To: %s\n" to))
1696 (setq recipients to)
1698 (if (and (stringp cc)
1699 (not (string-equal cc "")))
1701 (insert (format "cc: %s\n" cc))
1703 (setq recipients (concat recipients "," cc))
1704 (setq recipients cc)
1706 (insert (format "Content-Type: %s\n" ctype))
1708 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1712 (let ((mc-pgp-always-sign 'never))
1713 (mc-pgp-encrypt-region
1714 (mc-split "\\([ \t\n]*,[ \t\n]*\\)+" recipients)
1715 (point-min) (point-max) from nil)
1717 (throw 'mime-editor/error 'pgp-error)
1720 (insert (format "--[[multipart/encrypted;
1722 protocol=\"application/pgp-encrypted\"][7bit]]
1724 Content-Type: application/pgp-encrypted
1727 Content-Type: application/octet-stream
1728 Content-Transfer-Encoding: 7bit
1730 " pgp-boundary pgp-boundary pgp-boundary))
1731 (goto-char (point-max))
1732 (insert (format "\n--%s--\n" pgp-boundary))
1735 (defun mime-editor/sign-pgp-kazu (beg end boundary)
1738 (narrow-to-region beg end)
1740 (mime-editor/translate-region beg end boundary))
1742 (encoding (nth 1 ret))
1746 (insert (format "Content-Type: %s\n" ctype))
1748 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1751 (or (let ((program-coding-system-alist
1752 (cons (cons (cons nil ".*pgp.*")
1753 (cons *noconv* *noconv*))
1754 program-coding-system-alist))
1756 (mc-pgp-sign-region beg (point-max))
1758 (throw 'mime-editor/error 'pgp-error)
1762 "--[[application/pgp; format=mime][7bit]]\n")
1766 (defun mime-editor/encrypt-pgp-kazu (beg end boundary)
1768 (let ((from (rfc822/get-field-body "From"))
1769 (to (rfc822/get-field-body "To"))
1770 (cc (rfc822/get-field-body "cc"))
1773 (narrow-to-region beg end)
1775 (mime-editor/translate-region beg end boundary))
1777 (encoding (nth 1 ret))
1781 (if (and (stringp from)
1782 (not (string-equal from "")))
1783 (insert (format "From: %s\n" from))
1785 (if (and (stringp to)
1786 (not (string-equal to "")))
1788 (insert (format "To: %s\n" to))
1789 (setq recipients to)
1791 (if (and (stringp cc)
1792 (not (string-equal cc "")))
1794 (insert (format "cc: %s\n" cc))
1796 (setq recipients (concat recipients "," cc))
1797 (setq recipients cc)
1799 (insert (format "Content-Type: %s\n" ctype))
1801 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1804 (or (let ((program-coding-system-alist
1805 (cons (cons (cons nil ".*pgp.*")
1806 (cons *noconv* *noconv*))
1807 program-coding-system-alist))
1809 (mc-pgp-encrypt-region
1810 (mc-split "\\([ \t\n]*,[ \t\n]*\\)+" recipients)
1813 (throw 'mime-editor/error 'pgp-error)
1817 "--[[application/pgp; format=mime][7bit]]\n")
1821 (defun mime-editor/translate-body ()
1822 "Encode the tagged MIME body in current buffer in MIME compliant message."
1826 (concat mime-multipart-boundary "_"
1827 (replace-space-with-underline (current-time-string))
1831 (while (mime-editor/process-multipart-1
1832 (format "%s-%d" boundary i))
1836 ;; We are interested in message body.
1839 (goto-char (point-min))
1841 (concat "\n" (regexp-quote mail-header-separator)
1842 (if mime-ignore-preceding-spaces
1843 "[ \t\n]*\n" "\n")) nil 'move)
1847 (goto-char (point-max))
1848 (and mime-ignore-trailing-spaces
1849 (re-search-backward "[^ \t\n]\n" beg t)
1852 (setq ret (mime-editor/translate-region
1854 (format "%s-%d" boundary i)))
1856 (mime-editor/dequote-region (point-min)(point-max))
1857 (let ((contype (car ret)) ;Content-Type
1858 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1860 ;; Make primary MIME headers.
1861 (or (mail-position-on-field "Mime-Version")
1862 (insert mime-editor/mime-version-value))
1863 ;; Remove old Content-Type and other fields.
1865 (goto-char (point-min))
1866 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1867 (narrow-to-region (point-min) (point))
1868 (goto-char (point-min))
1869 (mime-delete-field "Content-Type")
1870 (mime-delete-field "Content-Transfer-Encoding"))
1871 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1872 (mail-position-on-field "Content-Type")
1876 (mail-position-on-field "Content-Transfer-Encoding")
1880 (defun mime-editor/translate-region (beg end &optional boundary multipart)
1883 (concat mime-multipart-boundary "_"
1884 (replace-space-with-underline (current-time-string))))
1888 (narrow-to-region beg end)
1889 (let ((tag nil) ;MIME tag
1890 (contype nil) ;Content-Type
1891 (encoding nil) ;Content-Transfer-Encoding
1892 (nparts 0)) ;Number of body parts
1893 ;; Normalize the body part by inserting appropriate message
1894 ;; tags for every message contents.
1895 (mime-editor/normalize-body)
1896 ;; Counting the number of Content-Type.
1897 (goto-char (point-min))
1898 (while (re-search-forward mime-editor/single-part-tag-regexp nil t)
1899 (setq nparts (1+ nparts)))
1900 ;; Begin translation.
1902 ((and (<= nparts 1)(not multipart))
1903 ;; It's a singular message.
1904 (goto-char (point-min))
1905 (while (re-search-forward
1906 mime-editor/single-part-tag-regexp nil t)
1908 (buffer-substring (match-beginning 0) (match-end 0)))
1909 (delete-region (match-beginning 0) (1+ (match-end 0)))
1910 (setq contype (mime-editor/get-contype tag))
1911 (setq encoding (mime-editor/get-encoding tag))
1914 ;; It's a multipart message.
1915 (goto-char (point-min))
1916 (if (re-search-forward
1917 mime-editor/single-part-tag-regexp nil t)
1921 (match-beginning 0) (match-end 0)))
1922 (delete-region (match-beginning 0) (match-end 0))
1923 (setq contype (mime-editor/get-contype tag))
1924 (setq encoding (mime-editor/get-encoding tag))
1925 (insert "--" boundary "\n")
1926 (insert "Content-Type: " contype "\n")
1928 (insert "Content-Transfer-Encoding: " encoding "\n"))
1930 (while (re-search-forward
1931 mime-editor/single-part-tag-regexp nil t)
1933 (buffer-substring (match-beginning 0) (match-end 0)))
1934 (delete-region (match-beginning 0) (match-end 0))
1935 (setq contype (mime-editor/get-contype tag))
1936 (setq encoding (mime-editor/get-encoding tag))
1937 (insert "\n--" boundary "\n")
1938 (insert "Content-Type: " contype "\n")
1940 (insert "Content-Transfer-Encoding: " encoding "\n"))
1942 ;; Define Content-Type as "multipart/mixed".
1944 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
1945 ;; Content-Transfer-Encoding must be "7bit".
1946 ;; The following encoding can be `nil', but is
1947 ;; specified as is since there is no way that a user
1949 (setq encoding "7bit")
1950 ;; Insert the trailer.
1951 (goto-char (point-max))
1953 (insert "--" boundary "--\n")
1954 (insert "\n--" boundary "--\n")
1956 (list contype encoding boundary nparts)
1959 (defun mime-editor/normalize-body ()
1960 "Normalize the body part by inserting appropriate message tags."
1961 ;; Insert the first MIME tags if necessary.
1962 (goto-char (point-min))
1963 (if (not (looking-at mime-editor/single-part-tag-regexp))
1964 (insert (mime-make-text-tag) "\n"))
1965 ;; Check each tag, and add new tag or correct it if necessary.
1966 (goto-char (point-min))
1967 (while (re-search-forward mime-editor/single-part-tag-regexp nil t)
1968 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
1969 (contype (mime-editor/get-contype tag))
1970 (charset (mime-get-parameter contype "charset"))
1971 (encoding (mime-editor/get-encoding tag)))
1972 ;; Remove extra whitespaces after the tag.
1973 (if (looking-at "[ \t]+$")
1974 (delete-region (match-beginning 0) (match-end 0)))
1976 ((= (following-char) ?\^M)
1977 ;; It must be image, audio or video.
1979 (end (mime-editor/content-end)))
1980 ;; Insert explicit MIME tags after hidden messages.
1982 (if (and (not (eobp))
1983 (not (looking-at mime-editor/single-part-tag-regexp)))
1985 (insert (mime-make-text-tag) "\n")
1986 (forward-line -1) ;Process it again as text.
1988 ;; Show a hidden message. The point is not altered
1989 ;; after the conversion.
1990 (mime-flag-region beg end ?\n)
1992 ((mime-test-content-type contype "message")
1993 ;; Content-type "message" should be sent as is.
1996 ((mime-test-content-type contype "text")
1997 ;; Define charset for text if necessary.
1998 (setq charset (or charset (mime-editor/choose-charset)))
1999 (mime-editor/define-charset charset)
2000 (cond ((string-equal contype "text/x-rot13-47")
2004 (goto-char (mime-editor/content-end))
2007 ((string-equal contype "text/enriched")
2012 (end (mime-editor/content-end))
2014 (enriched-encode beg end)
2016 (if (search-forward "\n\n")
2017 (delete-region beg (match-end 0))
2020 ;; Point is now on current tag.
2021 ;; Define encoding and encode text if necessary.
2022 (or encoding ;Encoding is not specified.
2026 mime-editor/charset-default-encoding-alist)
2028 (beg (mime-editor/content-beginning))
2030 (mime-charset-encode-region beg (mime-editor/content-end)
2032 (mime-encode-region beg (mime-editor/content-end) encoding)
2033 (mime-editor/define-encoding encoding)
2035 (goto-char (mime-editor/content-end))
2037 ((null encoding) ;Encoding is not specified.
2038 ;; Application, image, audio, video, and any other
2039 ;; unknown content-type without encoding should be
2041 (let* ((encoding "base64") ;Encode in BASE64 by default.
2042 (beg (mime-editor/content-beginning))
2043 (end (mime-editor/content-end))
2044 (body (buffer-substring beg end))
2046 (mime-encode-region beg end encoding)
2047 (mime-editor/define-encoding encoding))
2053 (defun mime-delete-field (field)
2054 "Delete header FIELD."
2055 (let ((regexp (format "^%s:[ \t]*" field)))
2056 (goto-char (point-min))
2057 (while (re-search-forward regexp nil t)
2058 (delete-region (match-beginning 0)
2059 (progn (forward-line 1) (point)))
2064 ;;; Platform dependent functions
2067 ;; Sun implementations
2069 (defun mime-voice-recorder-for-sun ()
2070 "Record voice in a buffer using Sun audio device, and return the buffer.
2071 If the environment variable AUDIOHOST is defined, its value is used as
2072 a recording host instead of local host."
2073 (let ((buffer (get-buffer-create " *MIME audio*"))
2074 (host (getenv "AUDIOHOST")))
2075 (message "Start the recording on %s. Type C-g to finish the recording..."
2076 (or host (system-name)))
2080 (condition-case errorcode
2081 (let ((selective-display nil) ;Disable ^M to nl translation.
2083 (kanji-flag nil)) ;NEmacs
2084 ;; If AUDIOHOST is defined, use the value as recording host.
2085 (cond ((not (null host))
2086 ;; Disable automatic conversion of coding system if Mule.
2087 (if (featurep 'mule)
2088 (define-program-coding-system nil "rsh" *noconv*))
2098 ;; Disable automatic conversion of coding system if Mule.
2099 (if (featurep 'mule)
2100 (define-program-coding-system nil "cat" *noconv*))
2106 (quit (message "Type C-g to finish recording... done.")
2107 buffer ;Return the buffer
2111 ;;; @ Other useful commands.
2114 ;; Message forwarding commands as content-type "message/rfc822".
2116 (defun mime-editor/insert-message (&optional message)
2118 (let ((inserter (assoc-value major-mode mime-editor/message-inserter-alist)))
2119 (if (and inserter (fboundp inserter))
2121 (mime-editor/insert-tag "message" "rfc822")
2122 (funcall inserter message)
2124 (message "Sorry, I don't have message inserter for your MUA.")
2127 (defun mime-editor/insert-mail (&optional message)
2129 (let ((inserter (assoc-value major-mode mime-editor/mail-inserter-alist)))
2130 (if (and inserter (fboundp inserter))
2132 (mime-editor/insert-tag "message" "rfc822")
2133 (funcall inserter message)
2135 (message "Sorry, I don't have mail inserter for your MUA.")
2138 (defun mime-editor/inserted-message-filter ()
2141 (let ((header-start (point))
2142 (case-fold-search t)
2145 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2146 (if (re-search-forward "^$" (mark t))
2147 (narrow-to-region header-start (match-beginning 0))
2149 (goto-char header-start)
2150 (while (and (re-search-forward
2151 mime-editor/yank-ignored-field-regexp nil t)
2152 (setq beg (match-beginning 0))
2153 (setq end (1+ (rfc822/field-end)))
2155 (delete-region beg end)
2160 ;;; @ multipart enclosure
2163 (defun mime-editor/enclose-region (type beg end)
2166 (let ((current (point))
2169 (if (mime-editor/goto-tag)
2170 (or (eq current (match-beginning 0))
2171 (setq exist-prev-tag t)
2174 (narrow-to-region beg end)
2179 (insert (format "--<<%s>>-{\n" type))
2180 (goto-char (point-max))
2181 (insert (format "\n--}-<<%s>>\n" type))
2182 (goto-char (point-max))
2184 (if (and (not (looking-at mime-editor/single-part-tag-regexp))
2186 (insert (mime-make-text-tag) "\n")
2190 (defun mime-editor/enclose-mixed-region (beg end)
2192 (mime-editor/enclose-region "mixed" beg end)
2195 (defun mime-editor/enclose-parallel-region (beg end)
2197 (mime-editor/enclose-region "parallel" beg end)
2200 (defun mime-editor/enclose-digest-region (beg end)
2202 (mime-editor/enclose-region "digest" beg end)
2205 (defun mime-editor/enclose-alternative-region (beg end)
2207 (mime-editor/enclose-region "alternative" beg end)
2210 (defun mime-editor/enclose-signed-region (beg end)
2212 (if mime-editor/signing-type
2213 (mime-editor/enclose-region "signed" beg end)
2214 (message "Please specify signing type.")
2217 (defun mime-editor/enclose-encrypted-region (beg end)
2219 (if mime-editor/signing-type
2220 (mime-editor/enclose-region "encrypted" beg end)
2221 (message "Please specify encrypting type.")
2224 (defun mime-editor/insert-key (&optional arg)
2225 "Insert a pgp public key."
2227 (mime-editor/insert-tag "application" "pgp-keys")
2228 (mime-editor/define-encoding "7bit")
2229 (mc-insert-public-key)
2236 (defun mime-editor/set-split (arg)
2239 (y-or-n-p "Do you want to enable split?")
2241 (setq mime-editor/split-message arg)
2243 (message "This message is enabled to split.")
2244 (message "This message is not enabled to split.")
2247 (defun mime-editor/toggle-transfer-level (&optional transfer-level)
2248 "Toggle transfer-level is 7bit or 8bit through.
2250 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2252 (if (numberp transfer-level)
2253 (setq mime-editor/transfer-level transfer-level)
2254 (if (< mime-editor/transfer-level 8)
2255 (setq mime-editor/transfer-level 8)
2256 (setq mime-editor/transfer-level 7)
2258 (setq mime-editor/charset-default-encoding-alist
2259 (mime/make-charset-default-encoding-alist
2260 mime-editor/transfer-level))
2261 (message (format "Current transfer-level is %d bit"
2262 mime-editor/transfer-level))
2263 (setq mime-editor/transfer-level-string
2264 (mime/encoding-name mime-editor/transfer-level 'not-omit))
2265 (force-mode-line-update)
2272 (defun mime-editor/set-sign (arg)
2275 (y-or-n-p "Do you want to sign?")
2278 (if mime-editor/signing-type
2280 (setq mime-editor/pgp-processing 'sign)
2281 (message "This message will be signed.")
2283 (message "Please specify signing type.")
2285 (if (eq mime-editor/pgp-processing 'sign)
2286 (setq mime-editor/pgp-processing nil)
2288 (message "This message will not be signed.")
2291 (defun mime-editor/set-encrypt (arg)
2294 (y-or-n-p "Do you want to encrypt?")
2297 (if mime-editor/encrypting-type
2299 (setq mime-editor/pgp-processing 'encrypt)
2300 (message "This message will be encrypt.")
2302 (message "Please specify encrypting type.")
2304 (if (eq mime-editor/pgp-processing 'encrypt)
2305 (setq mime-editor/pgp-processing nil)
2307 (message "This message will not be encrypt.")
2310 (defvar mime-editor/pgp-processing nil)
2311 (make-variable-buffer-local 'mime-editor/pgp-processing)
2313 (defun mime-editor/pgp-enclose-buffer ()
2314 (let ((beg (save-excursion
2315 (goto-char (point-min))
2316 (if (search-forward (concat "\n" mail-header-separator "\n"))
2322 (cond ((eq mime-editor/pgp-processing 'sign)
2323 (mime-editor/enclose-signed-region beg end)
2325 ((eq mime-editor/pgp-processing 'encrypt)
2326 (mime-editor/enclose-encrypted-region beg end)
2334 (defun mime-editor/insert-partial-header
2335 (fields subject id number total separator)
2337 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2338 (insert (format "Mime-Version: 1.0 (%s)\n" mime-editor/version-name))
2340 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2341 id number total separator))
2344 (defun mime-editor/split-and-send
2345 (&optional cmd lines mime-editor/message-max-length)
2349 (count-lines (point-min) (point-max)))
2351 (or mime-editor/message-max-length
2352 (setq mime-editor/message-max-length
2353 (or (cdr (assq major-mode mime-editor/message-max-length-alist))
2354 mime-editor/message-default-max-length))
2356 (let* ((mime-editor/draft-file-name
2357 (or (buffer-file-name)
2359 (expand-file-name "tm-draft" mime/tmp-dir))))
2360 (separator mail-header-separator)
2362 (eval (cdr (assq major-mode mime-editor/window-config-alist))))
2364 (replace-space-with-underline (current-time-string))
2365 "@" (system-name) "\"")))
2366 (run-hooks 'mime-editor/before-split-hook)
2367 (let* ((header (rfc822/get-header-string-except
2368 mime-editor/split-ignored-field-regexp separator))
2369 (subject (mail-fetch-field "subject"))
2370 (total (+ (/ lines mime-editor/message-max-length)
2371 (if (> (mod lines mime-editor/message-max-length) 0)
2373 (the-buf (current-buffer))
2374 (buf (get-buffer "*tmp-send*"))
2379 mime-editor/split-message-sender-alist))
2382 mime-editor/message-default-sender-alist))
2385 (goto-char (point-min))
2386 (if (re-search-forward (concat "^" (regexp-quote separator) "$")
2392 (switch-to-buffer buf)
2394 (switch-to-buffer the-buf)
2396 (setq buf (get-buffer-create "*tmp-send*"))
2398 (switch-to-buffer buf)
2399 (make-local-variable 'mail-header-separator)
2400 (setq mail-header-separator separator)
2401 (switch-to-buffer the-buf)
2402 (goto-char (point-min))
2403 (re-search-forward "^$" nil t)
2404 (let ((mime-editor/partial-number 1))
2405 (setq data (buffer-substring
2408 (goto-line mime-editor/message-max-length)
2411 (delete-region (point-min)(point))
2412 (switch-to-buffer buf)
2413 (mime-editor/insert-partial-header
2414 header subject id mime-editor/partial-number total separator)
2418 (goto-char (point-min))
2419 (search-forward (concat "\n" mail-header-separator "\n"))
2422 (if (re-search-forward "^$" nil t)
2426 (goto-char (point-min))
2427 (while (re-search-forward
2428 mime-editor/split-blind-field-regexp nil t)
2429 (delete-region (match-beginning 0)
2430 (let ((e (rfc822/field-end)))
2431 (if (< e (point-max))
2437 (message (format "Sending %d/%d..."
2438 mime-editor/partial-number total))
2439 (call-interactively command)
2440 (message (format "Sending %d/%d... done"
2441 mime-editor/partial-number total))
2444 (switch-to-buffer the-buf)
2445 (setq mime-editor/partial-number 2)
2446 (while (< mime-editor/partial-number total)
2447 (setq data (buffer-substring
2450 (goto-line mime-editor/message-max-length)
2453 (delete-region (point-min)(point))
2454 (switch-to-buffer buf)
2455 (mime-editor/insert-partial-header
2456 header subject id mime-editor/partial-number total separator)
2459 (message (format "Sending %d/%d..."
2460 mime-editor/partial-number total))
2461 (call-interactively command)
2462 (message (format "Sending %d/%d... done"
2463 mime-editor/partial-number total))
2466 (switch-to-buffer the-buf)
2467 (setq mime-editor/partial-number
2468 (1+ mime-editor/partial-number))
2470 (goto-char (point-min))
2471 (mime-editor/insert-partial-header
2472 header subject id mime-editor/partial-number total separator)
2473 (message (format "Sending %d/%d..."
2474 mime-editor/partial-number total))
2477 (defun mime-editor/maybe-split-and-send (&optional cmd)
2479 (run-hooks 'mime-editor/before-send-hook)
2480 (let ((mime-editor/message-max-length
2481 (or (cdr (assq major-mode mime-editor/message-max-length-alist))
2482 mime-editor/message-default-max-length))
2483 (lines (count-lines (point-min) (point-max)))
2485 (if (and (> lines mime-editor/message-max-length)
2486 mime-editor/split-message)
2487 (mime-editor/split-and-send cmd lines mime-editor/message-max-length)
2491 ;;; @ preview message
2494 (defun mime-editor/preview-message ()
2495 "preview editing MIME message. [mime-edit.el]"
2497 (let* ((str (buffer-string))
2498 (separator mail-header-separator)
2499 (the-buf (current-buffer))
2500 (buf-name (buffer-name))
2501 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2502 (buf (get-buffer temp-buf-name))
2506 (switch-to-buffer buf)
2509 (setq buf (get-buffer-create temp-buf-name))
2510 (switch-to-buffer buf)
2513 (setq major-mode 'mime/temporary-message-mode)
2514 (make-local-variable 'mail-header-separator)
2515 (setq mail-header-separator separator)
2516 (make-local-variable 'mime/editing-buffer)
2517 (setq mime/editing-buffer the-buf)
2519 (run-hooks 'mime-editor/translate-hook)
2520 (mime-editor/translate-buffer)
2521 (goto-char (point-min))
2522 (if (re-search-forward
2523 (concat "^" (regexp-quote separator) "$"))
2529 (defun mime-editor/quitting-method ()
2530 (let ((temp mime::preview/article-buffer)
2532 (mime-viewer/kill-buffer)
2534 (setq buf mime/editing-buffer)
2536 (switch-to-buffer buf)
2539 (set-alist 'mime-viewer/quitting-method-alist
2540 'mime/temporary-message-mode
2541 (function mime-editor/quitting-method)
2547 ;; by "OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
2548 ;; Mon, 10 Apr 1995 20:03:07 +0900
2550 (defvar mime-editor/draft-header-separator-alist
2551 '((news-reply-mode . mail-header-separator)
2552 (mh-letter-mode . mail-header-separator)
2555 (defvar mime::article/draft-header-separator nil)
2557 (defun mime-editor/draft-preview ()
2559 (let ((sep (cdr (assq major-mode mime-editor/draft-header-separator-alist))))
2560 (or (stringp sep) (setq sep (eval sep)))
2561 (make-variable-buffer-local 'mime::article/draft-header-separator)
2562 (goto-char (point-min))
2564 (concat "^\\(" (regexp-quote sep) "\\)?$"))
2565 (setq mime::article/draft-header-separator
2566 (buffer-substring (match-beginning 0) (match-end 0)))
2568 (mime/viewer-mode (current-buffer))
2569 (pop-to-buffer (current-buffer))
2572 (defun mime-viewer::quitting-method/draft-preview ()
2573 (let ((mother mime::preview/mother-buffer))
2575 (switch-to-buffer mother)
2576 (goto-char (point-min))
2580 (regexp-quote mime::article/draft-header-separator)
2584 (insert mime::article/draft-header-separator)
2585 (set-buffer-modified-p (buffer-modified-p))
2587 (mime-viewer/kill-buffer)
2588 (pop-to-buffer mother)
2591 (set-alist 'mime-viewer/quitting-method-alist
2593 (function mime-viewer::quitting-method/draft-preview)
2596 (set-alist 'mime-viewer/quitting-method-alist
2598 (function mime-viewer::quitting-method/draft-preview)
2605 (defun mime-editor::edit-again (code-conversion)
2607 (goto-char (point-min))
2608 (let ((ctl (mime/Content-Type)))
2610 (let ((ctype (car ctl))
2613 (if (string-match "/" ctype)
2615 (setq type (substring ctype 0 (match-beginning 0)))
2616 (setq stype (substring ctype (match-end 0)))
2621 ((string-equal type "multipart")
2622 (let ((boundary (assoc-value "boundary" params)))
2623 (re-search-forward (concat "\n--" boundary) nil t)
2624 (let ((bb (match-beginning 0)) eb tag)
2625 (setq tag (format "\n--<<%s>>-{" stype))
2628 (setq bb (+ bb (length tag)))
2629 (re-search-forward (concat "\n--" boundary "--") nil t)
2630 (setq eb (match-beginning 0))
2631 (replace-match (format "\n--}-<<%s>>" stype))
2633 (narrow-to-region bb eb)
2634 (goto-char (point-min))
2635 (while (re-search-forward
2636 (concat "\n--" boundary "\n") nil t)
2637 (let ((beg (match-beginning 0))
2639 (delete-region beg (match-end 0))
2641 (if (re-search-forward
2642 (concat "\n--" boundary) nil t)
2643 (setq end (match-beginning 0))
2644 (setq end (point-max))
2647 (narrow-to-region beg end)
2648 (mime-editor::edit-again code-conversion)
2649 (goto-char (point-max))
2652 (goto-char (point-min))
2653 (or (= (point-min) 1)
2654 (delete-region (point-min)
2655 (if (re-search-forward "^$" nil t)
2663 (mapconcat (function
2665 (if (string-equal (car attr)
2668 (setq charset (cdr attr))
2670 (concat ";" (car attr)
2678 (if (re-search-forward
2679 "Content-Transfer-Encoding:" nil t)
2680 (let ((beg (match-beginning 0))
2681 (hbeg (match-end 0))
2682 (end (rfc822/field-end)))
2684 (eliminate-top-spaces
2685 (rfc822/unfolding-string
2686 (buffer-substring hbeg end))))
2687 (if (or charset (string-equal type "text"))
2689 (delete-region beg (1+ end))
2690 (goto-char (point-min))
2691 (if (search-forward "\n\n" nil t)
2694 (match-end 0)(point-max) encoding)
2698 (if (or code-conversion encoded)
2700 (mime-charset-decode-region (point-min)(point-max)
2702 (character-decode-region (point-min)(point-max)
2703 mime/default-coding-system)
2706 (if (re-search-forward "^$" nil t)
2710 (if (= (point-min) 1)
2717 (concat type "/" stype pstr) encoding)
2720 (delete-region (point-min) he)
2724 (concat type "/" stype pstr) encoding)
2729 (character-decode-region (point-min) (point-max)
2730 mime/default-coding-system)
2734 (defun mime/edit-again (&optional code-conversion no-separator no-mode)
2736 (mime-editor::edit-again code-conversion)
2737 (goto-char (point-min))
2739 (narrow-to-region (point-min)
2740 (if (re-search-forward "^$" nil t)
2744 (goto-char (point-min))
2745 (while (re-search-forward
2746 "^\\(Content-.*\\|Mime-Version\\):" nil t)
2747 (delete-region (match-beginning 0) (1+ (rfc822/field-end)))
2750 (and (re-search-forward "^$")
2751 (replace-match mail-header-separator)
2761 (provide 'mime-edit)
2763 (run-hooks 'mime-edit-load-hook)
2765 ;;; mime-edit.el ends here