2 ;;; tm-edit.el --- Simple MIME Composer for GNU Emacs
5 ;; Copyright (C) 1993 UMEDA Masanobu
6 ;; Copyright (C) 1994,1995 MORIOKA Tomohiko
8 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
9 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
10 ;; Keywords: mail, news, MIME, multimedia, multilingual
12 ;; This file is not part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU 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
26 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 ;; This is an Emacs minor mode for editing Internet multimedia
31 ;; messages formatted in MIME (RFC 1521 and RFC 1522). All messages in
32 ;; this mode are composed in the tagged MIME format, that are
33 ;; described in the following examples. The messages composed in the
34 ;; tagged MIME format are automatically translated into a MIME
35 ;; compliant message when exiting the mode.
37 ;; Mule (a multilingual extension to Emacs 18 and 19) has a capability
38 ;; of handling multilingual text in limited ISO-2022 manner that is
39 ;; based on early experiences in Japanese Internet community and
40 ;; resulted in RFC 1468 (ISO-2022-JP charset for MIME). In order to
41 ;; enable multilingual capability in single text message in MIME,
42 ;; charset of multilingual text written in Mule is declared as either
43 ;; `ISO-2022-JP-2' [RFC 1554] or `ISO-2022-INT-1'. Mule is required
44 ;; for reading the such messages.
46 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
47 ;; News mode. First of all, you need the following autoload
48 ;; definition to load mime-mode automatically:
50 ;; (autoload 'mime-mode "mime" "Minor mode for editing MIME message." t)
52 ;; In case of Mail mode (includes VM mode), you need the following
55 ;; (setq mail-mode-hook
61 ;; In case of MH-E, you need the following hook definition:
63 ;; (setq mh-letter-mode-hook
68 ;; (make-local-variable 'mail-header-separator)
69 ;; (setq mail-header-separator "--------")))))
71 ;; In case of News mode, you need the following hook definition:
73 ;; (setq news-reply-mode-hook
79 ;; Followings are for message forwarding as content-type
82 ;; (setq rmail-mode-hook
86 ;; ;; Forward mail using MIME.
88 ;; (substitute-key-definition 'rmail-forward
89 ;; 'mime-forward-from-rmail-using-mail
90 ;; (current-local-map))
93 ;; (setq gnus-mail-forward-method 'mime-forward-from-gnus-using-mail)
94 ;; (setq gnus-summary-mode-hook
98 ;; ;; Forward article using MIME.
102 ;; In case of Emacs 19, it is possible to emphasize the message tags
103 ;; using font-lock mode as follows:
105 ;; (setq mime-mode-hook
109 ;; (font-lock-mode 1)
110 ;; (setq font-lock-keywords (list tm-edit/tag-regexp))))))
112 ;; The message tag looks like:
114 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
116 ;; The tagged MIME message examples:
118 ;; This is a conventional plain text. It should be translated into
122 ;; This is also a plain text. But, it is explicitly specified as is.
124 ;;--[[text/plain; charset=ISO-2022-JP]]
125 ;;
\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.
127 ;;--[[text/richtext]]
128 ;; <center>This is a richtext.</center>
130 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
132 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
134 ;; LCD Archive Entry:
135 ;; mime|Masanobu UMEDA|umerin@mse.kyutech.ac.jp|
136 ;; Simple MIME Composer|
137 ;; $Date: 1995/10/26 11:12:05 $|$Revision: 7.11 $|~/misc/mime.el.Z|
142 (require 'mail-utils)
154 (defconst tm-edit/RCS-ID
155 "$Id: tm-edit.el,v 7.11 1995/10/26 11:12:05 morioka Exp $")
157 (defconst tm-edit/version (get-version-string tm-edit/RCS-ID))
163 (defvar mime-prefix "\C-c\C-x"
164 "*Keymap prefix for MIME commands.")
166 (defvar mime-signature-file "~/.signature.rtf"
167 "*Signature file to be included as a part of a multipart message.")
169 (defvar mime-ignore-preceding-spaces nil
170 "*Ignore preceding white spaces if non-nil.")
172 (defvar mime-ignore-trailing-spaces nil
173 "*Ignore trailing white spaces if non-nil.")
175 (defvar mime-ignore-same-text-tag t
176 "*Ignore preceding text content-type tag that is same with new one.
177 If non-nil, the text tag is not inserted unless something different.")
179 (defvar mime-auto-hide-body t
180 "*Hide non-textual body encoded in base64 after insertion if non-nil.")
182 (defvar mime-body-charset-chooser
183 (cond ((boundp 'NEMACS)
184 (function mime-body-charset-chooser-for-nemacs))
186 (function mime-body-charset-chooser-for-mule))
187 ((string-match "^19\\." emacs-version)
188 (function mime-body-charset-chooser-for-emacs19))
190 (function mime-body-charset-chooser-for-emacs18)))
191 "*Function to identify charset and encoding of a text in a given region.
192 The value is a form of (CHARSET . ENCODING), where ENCODING must be a
193 full name, such as base64.")
195 (defvar mime-string-encoder
196 (cond ((boundp 'NEMACS)
197 (function mime-string-encoder-for-nemacs))
199 (function mime-string-encoder-for-mule))
200 ((string-match "^19\\." emacs-version)
201 (function mime-string-encoder-for-emacs19))
203 (function mime-string-encoder-for-emacs18)))
204 "*Function to encode a string for given encoding method.
205 The method is a form of (CHARSET . ENCODING).")
207 (defvar mime-voice-recorder
208 (function mime-voice-recorder-for-sun)
209 "*Function to record a voice message and return a buffer that contains it.")
211 (defvar mime-mode-hook nil
212 "*Hook called when enter MIME mode.")
214 (defvar mime-translate-hook nil
215 "*Hook called before translating into a MIME compliant message.
216 To insert a signature file specified by mime-signature-file
217 (`.signature.rtf' by default) automatically, call the function
218 `tm-edit/insert-signature' from this hook.")
220 (defvar mime-exit-hook nil
221 "*Hook called when exit MIME mode.")
223 (defvar mime-content-types
225 ;; Charset parameter need not to be specified, since it is
226 ;; defined automatically while translation.
228 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
231 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
234 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
237 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
240 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
246 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
247 ("directory" "/pub/GNU/elisp/mime")
249 ("mode" "binary" "ascii"))
250 ("ftp" ("site") ("directory") ("name") ("mode" "binary" "ascii"))
251 ("tftp" ("site") ("name"))
252 ("afs" ("site") ("name"))
253 ("local-file" ("site") ("name"))
254 ("mail-server" ("server" "ftpmail@nic.karrn.ad.jp"))
261 ("type" "" "tar" "shar")
264 ("x-kiss" ("x-cnf")))
275 "*Alist of content-type, subtype, parameters and its values.")
277 (defvar mime-file-types
279 "text" "richtext" nil nil)
281 "text" "html" nil nil)
283 "application" "postscript" nil "quoted-printable")
285 "image" "gif" nil "base64"
286 (("Content-Description" . file))
289 "image" "jpeg" nil "base64")
291 "image" "x-xwd" nil "base64")
293 "image" "x-xbm" nil "base64")
295 "image" "x-pic" nil "base64"
296 (("Content-Description" . file))
299 "image" "tiff" nil "base64")
301 "audio" "basic" nil "base64")
303 "video" "mpeg" nil "base64")
305 "application" "octet-stream" (("name" . file)
306 ("type" . "emacs-lisp")) "7bit")
308 "application" "octet-stream" (("name" . file)
310 ("conversions" . "gzip")) nil)
312 "application" "octet-stream" (("name" . file)
313 ("type" . "patch")) nil)
315 "text" "plain" nil nil)
316 (".*" nil nil nil nil)
318 "*Alist of file name, types, parameters, and default encoding.
319 If encoding is nil, it is determined from its contents.")
321 (defvar tm-edit/split-message t)
323 (defvar tm-edit/message-default-max-length 1000)
325 (defvar tm-edit/message-max-length-alist
326 '((news-reply-mode . 500)))
328 (defconst tm-edit/message-nuke-headers
329 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\)")
331 (defvar tm-edit/message-blind-headers "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)")
333 (defvar tm-edit/message-default-sender-alist
334 '((mail-mode . mail-send-and-exit)
335 (mh-letter-mode . mh-send-letter)
336 (news-reply-mode . gnus-inews-news)))
338 (defvar tm-edit/message-sender-alist
345 . (lambda (&optional arg)
347 (write-region (point-min) (point-max)
348 tm-edit/draft-file-name)
350 (format "Sending %d/%d..." (+ i 1) total))
352 (pop-to-buffer "MH mail delivery")
354 (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
356 mh-send-args tm-edit/draft-file-name)
357 (goto-char (point-max)) ; show the interesting part
361 (apply 'mh-exec-cmd-quiet t mh-send-prog
363 (list "-nopush" "-nodraftfolder"
364 "-noverbose" "-nowatch"
365 mh-send-args tm-edit/draft-file-name)))))
367 (format "Sending %d/%d... done" (+ i 1) total))
371 (defvar tm-edit/window-config-alist
373 (mh-letter-mode . mh-previous-window-config)
374 (news-reply-mode . (cond ((boundp 'gnus-winconf-post-news)
376 gnus-winconf-post-news
377 (setq gnus-winconf-post-news nil)
379 ((boundp 'gnus-prev-winconf)
382 (setq gnus-prev-winconf nil)
387 (defvar tm-edit/news-reply-mode-server-running nil)
389 (defvar tm-edit/message-before-send-hook-alist
390 '((mh-letter-mode . mh-before-send-letter-hook)))
392 (defvar tm-edit/message-after-send-hook-alist
396 (mh-annotate-msg mh-sent-from-msg
399 "-component" mh-annotate-field
403 (mh-get-field "Cc:"))))))
406 (defvar tm-edit/message-inserter-alist nil)
408 (defvar mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
409 "*Specify MIME tspecials.
410 Tspecials means any character that matches with it in header must be quoted.")
412 (defconst tm-edit/single-part-tag-regexp
413 "^--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
414 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
416 (defconst tm-edit/multipart-beginning-regexp "^--<<\\([^<>]+\\)>>-{\n")
418 (defconst tm-edit/multipart-end-regexp "^--}-<<\\([^<>]+\\)>>\n")
420 (defconst tm-edit/beginning-tag-regexp
421 (regexp-or tm-edit/single-part-tag-regexp
422 tm-edit/multipart-beginning-regexp))
424 (defconst tm-edit/end-tag-regexp
425 (regexp-or tm-edit/single-part-tag-regexp
426 tm-edit/multipart-end-regexp))
428 (defconst tm-edit/tag-regexp
429 (regexp-or tm-edit/single-part-tag-regexp
430 tm-edit/multipart-beginning-regexp
431 tm-edit/multipart-end-regexp))
433 (defvar mime-tag-format "--[[%s]]"
434 "*Control-string making a MIME tag.")
436 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
437 "*Control-string making a MIME tag with encoding.")
439 (defvar mime-multipart-boundary "Multipart"
440 "*Boundary of a multipart message.")
443 (defconst tm-edit/mime-version-value
444 (format "1.0 (generated by tm-edit %s)" tm-edit/version)
445 "MIME version number.")
447 (defvar mime-mode-flag nil)
448 (make-variable-buffer-local 'mime-mode-flag)
450 (or (assq 'mime-mode-flag minor-mode-alist)
451 (setq minor-mode-alist
452 (cons (list 'mime-mode-flag " MIME") minor-mode-alist)))
454 (defun mime-define-keymap (keymap)
455 "Add MIME commands to KEYMAP."
456 (if (not (keymapp keymap))
458 (define-key keymap "\C-t" 'tm-edit/insert-text)
459 (define-key keymap "\C-i" 'tm-edit/insert-file)
460 (define-key keymap "\C-e" 'tm-edit/insert-external)
461 (define-key keymap "\C-v" 'tm-edit/insert-voice)
462 (define-key keymap "\C-y" 'tm-edit/insert-message)
463 (define-key keymap "\C-w" 'tm-edit/insert-signature)
464 (define-key keymap "\C-s" 'tm-edit/insert-signature)
465 (define-key keymap "\C-m" 'tm-edit/insert-tag)
466 (define-key keymap "a" 'tm-edit/enclose-alternative-region)
467 (define-key keymap "p" 'tm-edit/enclose-parallel-region)
468 (define-key keymap "m" 'tm-edit/enclose-mixed-region)
469 (define-key keymap "d" 'tm-edit/enclose-digest-region)
470 (define-key keymap "\C-p" 'tm-edit/preview-message)
471 (define-key keymap "\C-z" 'mime-mode-exit)
472 (define-key keymap "?" 'help-mime-mode)
475 (defconst tm-edit/menu
477 ["Describe MIME Mode" help-mime-mode mime-mode-flag]
478 ["Insert File" tm-edit/insert-file mime-mode-flag]
479 ["Insert External" tm-edit/insert-external mime-mode-flag]
480 ["Insert Voice" tm-edit/insert-voice mime-mode-flag]
481 ["Insert Mail" tm-edit/insert-message mime-mode-flag]
482 ["Insert Signature" tm-edit/insert-signature mime-mode-flag]
483 ["Insert Text" tm-edit/insert-text mime-mode-flag]
484 ["Insert Tag" tm-edit/insert-tag mime-mode-flag]
485 ["Enclose as alternative"
486 tm-edit/enclose-alternative-region mime-mode-flag]
487 ["Enclose as parallel"
488 tm-edit/enclose-parallel-region mime-mode-flag]
490 tm-edit/enclose-mixed-region mime-mode-flag]
492 tm-edit/enclose-digest-region mime-mode-flag]
493 ["Preview Message" tm-edit/preview-message mime-mode-flag]
495 "MIME menubar entry.")
497 (defun tm-edit/define-menu-for-emacs19 ()
498 "Define menu for Emacs 19."
499 (define-key (current-local-map) [menu-bar mime]
500 (cons "MIME" (make-sparse-keymap "MIME")))
503 (define-key (current-local-map)
504 (vector 'menu-bar 'mime (aref item 1))
505 (cons (aref item 0)(aref item 1))
508 (reverse (cdr tm-edit/menu))
511 ;;; modified by Pekka Marjola <pema@niksula.hut.fi>
512 ;;; 1995/9/5 (c.f. [tm-eng:69])
513 (defun tm-edit/define-menu-for-xemacs ()
514 "Define menu for Emacs 19."
515 (cond ((featurep 'menubar)
516 (make-local-variable 'current-menubar)
517 (set-buffer-menubar current-menubar)
518 (add-submenu nil mime-menu)
521 (defvar mime-xemacs-old-bindings nil
522 "A list of commands to restore old bindings.")
524 (defun mime-xemacs-save-old-bindings (keymap funct)
525 "Save key bindings to a list for setting it back."
526 (let* ((key-bindings (where-is-internal funct keymap))
529 (setq key-binding (pop key-bindings))
530 (setq mime-xemacs-old-bindings
531 (append mime-xemacs-old-bindings
532 (list (list 'define-key keymap key-binding
533 (list 'function funct))))))))
538 "MIME minor mode for editing the tagged MIME message.
540 In this mode, basically, the message is composed in the tagged MIME
541 format. The message tag looks like:
543 `--[[text/plain; charset=ISO-2022-JP][7bit]]'.
545 The tag specifies the MIME content type, subtype, optional parameters
546 and transfer encoding of the message following the tag. Messages
547 without any tag are treated as `text/plain' by default. Charset and
548 transfer encoding are automatically defined unless explicitly
549 specified. Binary messages such as audio and image are usually hidden
550 using selective-display facility. The messages in the tagged MIME
551 format are automatically translated into a MIME compliant message when
554 Available charsets depend on Emacs version being used. The following
555 lists the available charsets of each emacs.
557 Emacs18: US-ASCII is only available.
558 NEmacs: US-ASCII and ISO-2022-JP are available.
559 Emacs19: US-ASCII and ISO-8859-1 are available.
560 Mule: US-ASCII, ISO-8859-* (except for ISO-8859-6),
561 ISO-2022-JP, ISO-2022-JP-2 and ISO-2022-INT-1 are available.
563 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in Mule is expected to
564 be used to represent multilingual text in intermixed manner. Any
565 languages that has no registered charset are represented as either
566 ISO-2022-JP-2 or ISO-2022-INT-1 in Mule.
568 Following commands are available in addition to major mode commands:
569 \\[tm-edit/insert-text] insert a text message.
570 \\[tm-edit/insert-file] insert a (binary) file.
571 \\[tm-edit/insert-external] insert a reference to external body.
572 \\[tm-edit/insert-voice] insert a voice message.
573 \\[tm-edit/insert-message] insert a mail or news message.
574 \\[tm-edit/insert-signature] insert a signature file at end.
575 \\[tm-edit/insert-tag] insert a new MIME tag.
576 \\[tm-edit/enclose-alternative-region] Enclose as multipart/alternative.
577 \\[tm-edit/enclose-parallel-region] Enclose as multipart/parallel.
578 \\[tm-edit/enclose-mixed-region] Enclose as multipart/mixed.
579 \\[tm-edit/enclose-digest-region] Enclose as multipart/digest.
580 \\[tm-edit/preview-message] preview editing MIME message.
581 \\[mime-mode-exit] exit and translate into a MIME compliant message.
582 \\[tm-edit/exit-and-run] exit, translate and run the original command.
583 \\[help-mime-mode] show this help.
585 Additional commands are available in some major modes:
586 C-c C-c exit, translate and run the original command.
587 C-c C-s exit, translate and run the original command.
589 The following is a message example written in the tagged MIME format.
590 TABs at the beginning of the line are not a part of the message:
592 This is a conventional plain text. It should be translated
595 This is also a plain text. But, it is explicitly specified as
597 --[[text/plain; charset=ISO-2022-JP]]
598 \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
601 <center>This is a richtext.</center>
602 --[[image/gif][base64]]^M...image encoded in base64 here...
603 --[[audio/basic][base64]]^M...audio encoded in base64 here...
605 User customizable variables (not documented all of them):
607 Specifies a key prefix for MIME minor mode commands.
610 Specifies a signature file to be included as part of a multipart
613 mime-ignore-preceding-spaces
614 Preceding white spaces in a message body are ignored if non-nil.
616 mime-ignore-trailing-spaces
617 Trailing white spaces in a message body are ignored if non-nil.
619 mime-auto-fill-header
620 Fill header fields that contain encoded-words if non-nil.
623 Hide a non-textual body message encoded in base64 after insertion
626 mime-body-charset-chooser
627 Specifies a function to identify charset and encoding of a text in
628 a given region. The value is a form of (CHARSET . ENCODING),
629 where ENCODING must be a full name, such as base64.
632 Specifies a function to encode a string for given encoding method.
633 The method is a form of (CHARSET . ENCODING).
636 Specifies a function to record a voice message and return a buffer
637 that contains it. The function mime-voice-recorder-for-sun is for
641 Turning on MIME mode calls the value of mime-mode-hook, if it is
645 The value of mime-translate-hook is called just before translating
646 the tagged MIME format into a MIME compliant message if it is
647 non-nil. If the hook call the function tm-edit/insert-signature,
648 the signature file will be inserted automatically.
651 Turning off MIME mode calls the value of mime-exit-hook, if it is
655 (error "You are already editing a MIME message.")
656 (setq mime-mode-flag t)
657 ;; Remember old key bindings.
658 (make-local-variable 'mime-mode-old-local-map)
659 (setq mime-mode-old-local-map (current-local-map))
660 ;; Add MIME commands to current local map.
661 ;; modified by Pekka Marjola <pema@niksula.hut.fi>
662 ;; 1995/9/5 (c.f. [tm-eng:69])
663 (or (string-match "XEmacs\\|Lucid" emacs-version) ; can't use w/ XEmacs
664 (use-local-map (copy-keymap (current-local-map))))
667 (if (not (lookup-key (current-local-map) mime-prefix))
668 (define-key (current-local-map) mime-prefix (make-sparse-keymap)))
669 (mime-define-keymap (lookup-key (current-local-map) mime-prefix))
670 ;; Replace key definitions to avoid sending a message without
671 ;; conversion into a MIME compliant message.
672 ;; modified by Pekka Marjola <pema@niksula.hut.fi>
673 ;; 1995/9/5 (c.f. [tm-eng:69])
674 ;; copy-keymap behaves strangely in XEmacs
675 (cond ((string-match "XEmacs\\|Lucid" emacs-version)
676 (make-variable-buffer-local 'mime-xemacs-old-bindings)
677 (setq mime-xemacs-old-bindings nil)
679 (keymaps (accessible-keymaps (current-local-map))))
681 (setq keymap (cdr (car keymaps)))
682 (setq keymaps (cdr keymaps))
683 (if (not (keymapp keymap))
686 (mime-xemacs-save-old-bindings keymap 'mail-send)
687 (mime-xemacs-save-old-bindings keymap 'mail-send-and-exit)
689 (mime-xemacs-save-old-bindings keymap 'mh-send-letter)
690 ;; Mail mode called from VM:
691 (mime-xemacs-save-old-bindings keymap 'vm-mail-send)
692 (mime-xemacs-save-old-bindings keymap 'vm-mail-send-and-exit)
694 (mime-xemacs-save-old-bindings keymap 'news-inews)
700 (keymaps (accessible-keymaps (current-local-map))))
702 (setq keymap (cdr (car keymaps)))
703 (setq keymaps (cdr keymaps))
704 (if (not (keymapp keymap))
707 (substitute-key-definition
708 'mail-send 'tm-edit/exit-and-run keymap)
709 (substitute-key-definition
710 'mail-send-and-exit 'tm-edit/exit-and-run keymap)
712 (substitute-key-definition
713 'mh-send-letter 'tm-edit/exit-and-run keymap)
714 ;; Mail mode called from VM:
715 (substitute-key-definition
716 'vm-mail-send 'tm-edit/exit-and-run keymap)
717 (substitute-key-definition
718 'vm-mail-send-and-exit 'tm-edit/exit-and-run keymap)
720 (substitute-key-definition
721 'news-inews 'tm-edit/exit-and-run keymap)
723 ;; Define menu. Menus for other emacs implementations are
725 ;; modified by Pekka Marjola <pema@niksula.hut.fi>
726 ;; 1995/9/5 (c.f. [tm-eng:69])
727 (cond ((string-match "XEmacs\\|Lucid" emacs-version)
728 (tm-edit/define-menu-for-xemacs))
729 ((string-match "^19\\." emacs-version)
730 (tm-edit/define-menu-for-emacs19)
734 ;; Remember old selective-display.
735 (make-local-variable 'mime-mode-old-selective-display)
736 (setq mime-mode-old-selective-display selective-display)
737 (setq selective-display t)
738 ;; I don't care about saving these.
739 (setq paragraph-start
740 (concat tm-edit/single-part-tag-regexp "\\|" paragraph-start))
741 (setq paragraph-separate
742 (concat tm-edit/single-part-tag-regexp "\\|" paragraph-separate))
743 (run-hooks 'mime-mode-hook)
745 (substitute-command-keys
746 "Type \\[mime-mode-exit] to exit MIME mode, and type \\[help-mime-mode] to get help."))
750 (fset 'edit-mime 'mime-mode) ; for convenience
752 (defun mime-mode-exit (&optional nomime)
753 "Translate the tagged MIME message into a MIME compliant message.
754 With no argument encode a message in the buffer into MIME, otherwise
755 just return to previous mode."
757 (if (not mime-mode-flag)
758 (error "You aren't editing a MIME message.")
761 (run-hooks 'mime-translate-hook)
762 (tm-edit/translate-buffer)))
763 ;; Restore previous state.
764 (setq mime-mode-flag nil)
765 (use-local-map mime-mode-old-local-map)
767 ;; modified by Pekka Marjola <pema@niksula.hut.fi>
768 ;; 1995/9/5 (c.f. [tm-eng:69])
769 (if (string-match "XEmacs\\|Lucid" emacs-version)
771 (delete-menu-item '("MIME")) ; should rather be const
772 (while mime-xemacs-old-bindings
773 (eval (pop mime-xemacs-old-bindings)))
774 (local-unset-key mime-prefix)))
777 (setq selective-display mime-mode-old-selective-display)
778 (set-buffer-modified-p (buffer-modified-p))
779 (run-hooks 'mime-exit-hook)
780 (message "Exit MIME mode.")
783 (defun tm-edit/exit-and-run ()
786 (call-interactively 'tm-edit/split-and-send)
789 (defun help-mime-mode ()
790 "Show help message about MIME mode."
792 (with-output-to-temp-buffer "*Help*"
793 (princ "Edit MIME Mode:\n")
794 (princ (documentation 'mime-mode))
795 (print-help-return-message)))
797 (defun tm-edit/insert-text ()
798 "Insert a text message.
799 Charset is automatically obtained from the mime-body-charset-chooser."
801 (if (and (tm-edit/insert-tag "text" nil nil)
802 (looking-at tm-edit/single-part-tag-regexp))
804 ;; Make a space between the following message.
809 (defun tm-edit/insert-file (file)
810 "Insert a message from a file."
811 (interactive "fInsert file as MIME message: ")
812 (let* ((guess (mime-find-file-type file))
813 (pritype (nth 0 guess))
814 (subtype (nth 1 guess))
815 (parameters (nth 2 guess))
816 (default (nth 3 guess)) ;Guess encoding from its file name.
817 (fields (nth 4 guess))
819 (if (not (interactive-p))
822 (concat "What transfer encoding"
825 (if (string-equal default "") "\"\"" default)
829 mime-encoding-method-alist nil t nil))))
830 (if (string-equal encoding "")
831 (setq encoding default))
832 (if (or (consp parameters) (consp fields))
833 (let ((rest parameters) cell attribute value)
836 (setq cell (car rest))
837 (setq attribute (car cell))
838 (setq value (cdr cell))
840 (setq value (file-name-nondirectory file))
842 (setq parameters (concat parameters "; " attribute "=" value))
843 (setq rest (cdr rest))
847 (setq cell (car rest))
848 (setq attribute (car cell))
849 (setq value (cdr cell))
851 (setq value (file-name-nondirectory file))
853 (setq parameters (concat parameters "\n" attribute ": " value))
854 (setq rest (cdr rest))
857 (tm-edit/insert-tag pritype subtype parameters)
858 (tm-edit/insert-binary-file file encoding)
861 (defun tm-edit/insert-external ()
862 "Insert a reference to external body."
864 (tm-edit/insert-tag "message" "external-body" nil ";\n\t")
866 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
868 (let* ((pritype (mime-prompt-for-type))
869 (subtype (mime-prompt-for-subtype pritype))
870 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
873 (insert "Content-Type: "
874 pritype "/" subtype (or parameters "") "\n")))
875 (if (and (not (eobp))
876 (not (looking-at tm-edit/single-part-tag-regexp)))
877 (insert (mime-make-text-tag) "\n")))
879 (defun tm-edit/insert-voice ()
880 "Insert a voice message."
882 (tm-edit/insert-tag "audio" "basic" nil)
883 (let ((buffer (funcall mime-voice-recorder)))
885 (tm-edit/insert-binary-buffer buffer "base64")
889 (defun tm-edit/insert-signature ()
890 "Insert a signature file specified by mime-signature-file."
893 (apply (function tm-edit/insert-tag)
895 (mime-find-file-type (insert-signature))
896 (narrow-to-region (point-min)(point))
900 ;; Insert a new tag around a point.
902 (defun tm-edit/insert-tag (&optional pritype subtype parameters delimiter)
903 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
904 If nothing is inserted, return nil."
911 (mime-prompt-for-type)))
914 (mime-prompt-for-subtype pritype)))
917 (mime-prompt-for-parameters pritype subtype delimiter)))
918 ;; Make a new MIME tag.
919 (setq newtag (mime-make-tag pritype subtype parameters))
920 ;; Find an current MIME tag.
923 (if (tm-edit/goto-tag)
924 (buffer-substring (match-beginning 0) (match-end 0))
925 ;; Assume content type is 'text/plan'.
926 (mime-make-tag "text" "plain")
928 ;; We are only interested in TEXT.
930 (not (mime-test-content-type (tm-edit/get-contype oldtag) "text")))
933 (cond ((and oldtag ;Text
937 (looking-at tm-edit/beginning-tag-regexp)
939 (or mime-ignore-same-text-tag
940 (not (string-equal oldtag newtag))))
941 ;; If point is at the next of current tag, move to the
942 ;; beginning of the tag to disable insertion of extra tag.
946 (not (looking-at tm-edit/tag-regexp))
947 (or mime-ignore-same-text-tag
948 (not (string-equal oldtag newtag))))
949 ;; Copy current tag to break a text into two.
951 (insert oldtag "\n")))
952 ((and (null oldtag) ;Not text
953 (not (looking-at tm-edit/tag-regexp)))
954 ;; Adjust insertion point. In the middle of text, it is
955 ;; okay to break the text into two. However, it should not
956 ;; be broken into two, if otherwise.
957 (goto-char (tm-edit/content-end))
964 (if (or (not oldtag) ;Not text
965 (or mime-ignore-same-text-tag
966 (not (string-equal oldtag newtag))))
968 ;; Mark the beginning of the tag for convenience.
969 (push-mark (point) 'nomsg)
971 (list pritype subtype parameters) ;New tag is created.
973 ;; Restore previous point.
975 nil ;Nothing is created.
979 ;; Insert the binary content after MIME tag.
980 ;; modified by MORITA Masahiro <hiro@isl.ntt.JP>
982 (defun tm-edit/insert-binary-file (file &optional encoding)
983 "Insert binary FILE at point.
984 Optional argument ENCODING specifies an encoding method such as base64."
985 (let ((tmpbuf (get-buffer-create " *MIME insert*")))
989 (let ((mc-flag nil) ;Mule
990 (file-coding-system-for-read
991 (if (featurep 'mule) *noconv*))
992 (kanji-flag nil) ;NEmacs
993 (emx-binary-mode t) ;Stop CRLF to LF conversion in OS/2
995 (let (jka-compr-compression-info-list
996 jam-zcat-filename-list)
997 (insert-file-contents file))))
999 (if (and (stringp encoding)
1000 (string-equal (downcase encoding) "x-uue"))
1003 (let ((uuencode-external-encoder
1004 (cons (car uuencode-external-encoder)
1005 (list (file-name-nondirectory file))
1007 (tm-edit/insert-binary-buffer tmpbuf encoding)
1009 (tm-edit/insert-binary-buffer tmpbuf encoding))
1010 (kill-buffer tmpbuf))))
1012 ;; Insert the binary content after MIME tag.
1013 ;; modified by MORITA Masahiro <hiro@isl.ntt.JP>
1015 (defun tm-edit/insert-binary-buffer (buffer &optional encoding)
1016 "Insert binary BUFFER at point.
1017 Optional argument ENCODING specifies an encoding method such as base64."
1018 (let* ((tagend (1- (point))) ;End of the tag
1019 (hide-p (and mime-auto-hide-body
1021 (let ((en (downcase encoding)))
1022 (or (string-equal en "base64")
1023 (string-equal en "x-uue")
1027 (narrow-to-region (1- (point)) (point))
1028 (let ((start (point))
1029 (emx-binary-mode t)) ;Stop LF to CRLF conversion in OS/2
1030 (insert-buffer-substring buffer)
1031 ;; Encode binary message if necessary.
1033 (mime-encode-region encoding start (point-max))))
1036 (mime-flag-region (point-min) (1- (point-max)) ?\^M)
1037 (goto-char (point-max)))
1039 ;; Define encoding even if it is 7bit.
1040 (if (stringp encoding)
1042 (goto-char tagend) ;Make sure which line the tag is on.
1043 (tm-edit/define-encoding encoding)))
1046 ;; Commands work on a current message flagment.
1048 (defun tm-edit/goto-tag ()
1049 "Search for the beginning of the tagged MIME message."
1050 (let ((current (point)) multipart)
1051 (if (looking-at tm-edit/tag-regexp)
1053 ;; At first, go to the end.
1054 (cond ((re-search-forward tm-edit/beginning-tag-regexp nil t)
1055 (goto-char (match-beginning 0)) ;For multiline tag
1060 (goto-char (point-max))
1062 ;; Then search for the beginning.
1063 (re-search-backward tm-edit/end-tag-regexp nil t)
1065 (or (looking-at tm-edit/beginning-tag-regexp)
1066 ;; Restore previous point.
1073 (defun tm-edit/content-beginning ()
1074 "Return the point of the beginning of content."
1076 (let ((beg (save-excursion
1077 (beginning-of-line) (point))))
1078 (if (tm-edit/goto-tag)
1079 (let ((top (point)))
1080 (goto-char (match-end 0))
1081 (if (and (= beg top)
1082 (= (following-char) ?\^M))
1086 ;; Default text/plain tag.
1087 (goto-char (point-min))
1089 (concat "\n" (regexp-quote mail-header-separator)
1090 (if mime-ignore-preceding-spaces
1091 "[ \t\n]*\n" "\n")) nil 'move)
1095 (defun tm-edit/content-end ()
1096 "Return the point of the end of content."
1098 (let ((beg (save-excursion
1099 (beginning-of-line) (point))))
1100 (if (tm-edit/goto-tag)
1101 (let ((top (point)))
1102 (goto-char (match-end 0))
1103 (if (and (= beg top) ;Must be on the same line.
1104 (= (following-char) ?\^M))
1108 ;; Move to the end of this text.
1109 (if (re-search-forward tm-edit/tag-regexp nil 'move)
1110 ;; Don't forget a multiline tag.
1111 (goto-char (match-beginning 0)))
1114 ;; Assume the message begins with text/plain.
1115 (goto-char (tm-edit/content-beginning))
1116 (if (re-search-forward tm-edit/tag-regexp nil 'move)
1117 ;; Don't forget a multiline tag.
1118 (goto-char (match-beginning 0)))
1122 (defun tm-edit/define-charset (charset)
1123 "Set charset of current tag to CHARSET."
1125 (if (tm-edit/goto-tag)
1126 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1127 (delete-region (match-beginning 0) (match-end 0))
1129 (mime-create-tag (mime-set-parameter
1130 (tm-edit/get-contype tag) "charset" charset)
1131 (tm-edit/get-encoding tag))))
1134 (defun tm-edit/define-encoding (encoding)
1135 "Set encoding of current tag to ENCODING."
1137 (if (tm-edit/goto-tag)
1138 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1139 (delete-region (match-beginning 0) (match-end 0))
1140 (insert (mime-create-tag (tm-edit/get-contype tag) encoding)))
1143 (defun tm-edit/choose-charset ()
1144 "Choose charset of a text following current point."
1146 (let* ((beg (point))
1147 (end (tm-edit/content-end)))
1148 (car (funcall mime-body-charset-chooser beg end)))))
1150 (defun tm-edit/choose-encoding ()
1151 "Choose encoding of a text following current point."
1153 (let* ((beg (point))
1154 (end (tm-edit/content-end)))
1155 (cdr (funcall mime-body-charset-chooser beg end)))))
1157 (defun mime-make-text-tag (&optional subtype)
1158 "Make a tag for a text after current point.
1159 Subtype of text type can be specified by an optional argument SUBTYPE.
1160 Otherwise, it is obtained from mime-content-types."
1161 (let* ((pritype "text")
1162 (subtype (or subtype
1163 (car (car (cdr (assoc pritype mime-content-types)))))))
1164 ;; Charset should be defined later.
1165 (mime-make-tag pritype subtype)))
1168 ;; Tag handling functions
1170 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1171 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1172 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1176 (defun mime-create-tag (contype &optional encoding)
1177 "Make a tag with CONTENT-TYPE and optional ENCODING."
1178 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1181 (defun tm-edit/get-contype (tag)
1182 "Return Content-Type (including parameters) of TAG."
1184 (or (string-match tm-edit/single-part-tag-regexp tag)
1185 (string-match tm-edit/multipart-beginning-regexp tag)
1186 (string-match tm-edit/multipart-end-regexp tag)
1188 (substring tag (match-beginning 1) (match-end 1))
1191 (defun tm-edit/get-encoding (tag)
1192 "Return encoding of TAG."
1194 (string-match tm-edit/single-part-tag-regexp tag)
1196 (not (= (match-beginning 3) (match-end 3)))
1197 (substring tag (match-beginning 3) (match-end 3))))
1199 (defun mime-get-parameter (contype parameter)
1200 "For given CONTYPE return value for PARAMETER.
1201 Nil if no such parameter."
1205 (regexp-quote parameter)
1206 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1208 (substring contype (match-beginning 1) (match-end 1))
1209 nil ;No such parameter
1212 (defun mime-set-parameter (contype parameter value)
1213 "For given CONTYPE set PARAMETER to VALUE."
1217 (regexp-quote parameter)
1218 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1221 (concat (substring contype 0 (match-beginning 1))
1223 (substring contype (match-end 1)))
1224 (concat contype "; " parameter "=" value)))
1226 (defun mime-strip-parameters (contype)
1227 "Return primary content-type and subtype without parameters for CONTYPE."
1228 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1229 (substring contype (match-beginning 1) (match-end 1)) nil))
1231 (defun mime-test-content-type (contype type &optional subtype)
1232 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1233 (and (stringp contype)
1236 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1237 (downcase contype))))
1242 (defun mime-find-file-type (file)
1243 "Guess Content-Type, subtype, and parameters from FILE."
1245 (guesses mime-file-types))
1246 (while (and (not guess) guesses)
1247 (if (string-match (car (car guesses)) file)
1248 (setq guess (cdr (car guesses))))
1249 (setq guesses (cdr guesses)))
1253 (defun mime-prompt-for-type ()
1254 "Ask for Content-type."
1256 ;; Repeat until primary content type is specified.
1257 (while (string-equal type "")
1259 (completing-read "What content type: "
1262 'require-match ;Type must be specified.
1265 (if (string-equal type "")
1267 (message "Content type is required.")
1275 (defun mime-prompt-for-subtype (pritype)
1276 "Ask for Content-type subtype of Content-Type PRITYPE."
1277 (let* ((default (car (car (cdr (assoc pritype mime-content-types)))))
1282 "What content subtype: (default " default ") ")
1283 "What content subtype: ")
1284 (cdr (assoc pritype mime-content-types))
1286 'require-match ;Subtype must be specified.
1289 (if (string-equal answer "") default answer)))
1291 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1292 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1293 Optional DELIMITER specifies parameter delimiter (';' by default)."
1294 (let* ((delimiter (or delimiter "; "))
1299 (mime-prompt-for-parameters-1
1301 (cdr (assoc pritype mime-content-types))))))
1304 (if (and (stringp parameters)
1305 (not (string-equal parameters "")))
1306 (concat delimiter parameters)
1307 "" ;"" if no parameters
1310 (defun mime-prompt-for-parameters-1 (optlist)
1311 (apply (function append)
1312 (mapcar (function mime-prompt-for-parameter) optlist)))
1314 (defun mime-prompt-for-parameter (parameter)
1316 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1317 (let* ((prompt (car parameter))
1318 (choices (mapcar (function
1320 (if (consp e) e (list e))))
1322 (default (car (car choices)))
1328 (concat "What " prompt
1330 (if (string-equal default "") "\"\"" default)
1332 choices nil nil ""))
1333 ;; If nothing is selected, use default.
1334 (if (string-equal answer "")
1335 (setq answer default)))
1337 (read-string (concat "What " prompt ": "))))
1338 (cons (if (and answer
1339 (not (string-equal answer "")))
1341 ;; Note: control characters ignored!
1342 (if (string-match mime-tspecials-regexp answer)
1343 (concat "\"" answer "\"") answer)))
1344 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1347 (defun mime-encode-string (encoding string)
1348 "Using ENCODING encode a STRING.
1349 If the STRING is too long, the encoded string may be broken into
1352 (set-buffer (get-buffer-create " *MIME encoding*"))
1355 (mime-encode-region encoding (point-min) (point-max))
1357 (buffer-substring (point-min) (point-max))
1358 (kill-buffer (current-buffer)))))
1360 (defun mime-decode-string (encoding string)
1361 "Using ENCODING decode a STRING."
1363 (set-buffer (get-buffer-create " *MIME decoding*"))
1366 (mime-decode-region encoding (point-min) (point-max))
1368 (buffer-substring (point-min) (point-max))
1369 (kill-buffer (current-buffer)))))
1371 (defun mime-flag-region (from to flag)
1372 "Hides or shows lines from FROM to TO, according to FLAG.
1373 If FLAG is `\\n' (newline character) then text is shown,
1374 while if FLAG is `\\^M' (control-M) the text is hidden."
1375 (let ((buffer-read-only nil) ;Okay even if write protected.
1376 (modp (buffer-modified-p)))
1378 (subst-char-in-region from to
1379 (if (= flag ?\n) ?\^M ?\n)
1381 (set-buffer-modified-p modp))))
1384 ;; Translate the tagged MIME messages into a MIME compliant message.
1386 (defun tm-edit/translate-buffer ()
1387 "Encode the tagged MIME message in current buffer in MIME compliant message."
1389 (mime/encode-message-header)
1390 (tm-edit/translate-body)
1393 (defun tm-edit/translate-body ()
1394 "Encode the tagged MIME body in current buffer in MIME compliant message."
1398 (concat mime-multipart-boundary " " (current-time-string)))
1400 (time (current-time-string))
1402 (while (tm-edit/process-multipart-1
1403 (format "%s %s-%d" mime-multipart-boundary time i))
1407 ;; We are interested in message body.
1410 (goto-char (point-min))
1412 (concat "\n" (regexp-quote mail-header-separator)
1413 (if mime-ignore-preceding-spaces
1414 "[ \t\n]*\n" "\n")) nil 'move)
1418 (goto-char (point-max))
1419 (and mime-ignore-trailing-spaces
1420 (re-search-backward "[^ \t\n]\n" beg t)
1423 (setq ret (tm-edit/translate-region
1425 (format "%s %s-%d" mime-multipart-boundary time i)))
1427 (let ((contype (car ret)) ;Content-Type
1428 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1430 ;; Make primary MIME headers.
1431 (or (mail-position-on-field "Mime-Version")
1432 (insert tm-edit/mime-version-value))
1433 ;; Remove old Content-Type and other fields.
1435 (goto-char (point-min))
1436 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1437 (narrow-to-region (point-min) (point))
1438 (goto-char (point-min))
1439 (mime-delete-field "Content-Type")
1440 (mime-delete-field "Content-Transfer-Encoding"))
1441 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1442 (mail-position-on-field "Content-Type")
1446 (mail-position-on-field "Content-Transfer-Encoding")
1450 (defun tm-edit/normalize-body ()
1451 "Normalize the body part by inserting appropriate message tags."
1452 ;; Insert the first MIME tags if necessary.
1453 (goto-char (point-min))
1454 (if (not (looking-at tm-edit/single-part-tag-regexp))
1455 (insert (mime-make-text-tag) "\n"))
1456 ;; Check each tag, and add new tag or correct it if necessary.
1457 (goto-char (point-min))
1458 (while (re-search-forward tm-edit/single-part-tag-regexp nil t)
1459 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
1460 (contype (tm-edit/get-contype tag))
1461 (charset (mime-get-parameter contype "charset"))
1462 (encoding (tm-edit/get-encoding tag)))
1463 ;; Remove extra whitespaces after the tag.
1464 (if (looking-at "[ \t]+$")
1465 (delete-region (match-beginning 0) (match-end 0)))
1466 (cond ((= (following-char) ?\^M)
1467 ;; It must be image, audio or video.
1469 (end (tm-edit/content-end)))
1470 ;; Insert explicit MIME tags after hidden messages.
1472 (if (and (not (eobp))
1473 (not (looking-at tm-edit/single-part-tag-regexp)))
1475 (insert (mime-make-text-tag) "\n")
1476 (forward-line -1) ;Process it again as text.
1478 ;; Show a hidden message. The point is not altered
1479 ;; after the conversion.
1480 (mime-flag-region beg end ?\n)))
1481 ((mime-test-content-type contype "message")
1482 ;; Content-type "message" should be sent as is.
1484 ((mime-test-content-type contype "text")
1485 ;; Define charset for text if necessary.
1486 (setq charset (or charset (tm-edit/choose-charset)))
1487 (tm-edit/define-charset charset)
1488 ;; Point is now on current tag.
1489 ;; Define encoding and encode text if necessary.
1490 (if (null encoding) ;Encoding is not specified.
1491 (let* ((encoding (tm-edit/choose-encoding))
1492 (beg (tm-edit/content-beginning))
1493 (end (tm-edit/content-end))
1494 (body (buffer-substring beg end))
1495 (encoded (funcall mime-string-encoder
1496 (cons charset encoding) body)))
1497 (if (not (string-equal body encoded))
1500 (delete-region beg end)
1503 (tm-edit/define-encoding encoding)))
1505 ((null encoding) ;Encoding is not specified.
1506 ;; Application, image, audio, video, and any other
1507 ;; unknown content-type without encoding should be
1509 (let* ((encoding "base64") ;Encode in BASE64 by default.
1510 (beg (tm-edit/content-beginning))
1511 (end (tm-edit/content-end))
1512 (body (buffer-substring beg end))
1513 (encoded (funcall mime-string-encoder
1514 (cons nil encoding) body)))
1515 (if (not (string-equal body encoded))
1518 (delete-region beg end)
1521 (tm-edit/define-encoding encoding))
1526 (defun mime-delete-field (field)
1527 "Delete header FIELD."
1528 (let ((regexp (format "^%s:[ \t]*" field)))
1529 (goto-char (point-min))
1530 (while (re-search-forward regexp nil t)
1531 (delete-region (match-beginning 0)
1532 (progn (forward-line 1) (point)))
1537 ;;; Platform dependent functions
1540 ;; Emacs 18 implementations
1542 (defun mime-body-charset-chooser-for-emacs18 (begin end)
1543 "Return a cons of charset and encoding of a message in a given region.
1544 Encoding name must be a canonical name, such as `base64'."
1545 '("US-ASCII" . nil) ;Default charset of MIME.
1548 (defun mime-string-encoder-for-emacs18 (method string)
1549 "For given METHOD that is a cons of charset and encoding, encode a STRING."
1550 (let ((charset (car method))
1551 (encoding (cdr method)))
1552 (cond ((stringp encoding)
1553 (mime-encode-string encoding string))
1554 ;; Return string without any encoding.
1559 ;; Emacs 19 implementations
1561 (defun mime-body-charset-chooser-for-emacs19 (begin end)
1562 "Return a cons of charset and encoding of a message in a given region.
1563 Encoding name must be a canonical name, such as `base64'.
1564 US-ASCII and ISO-8859-1 are supported on Emacs 19."
1565 (cond ((save-excursion
1567 (re-search-forward "[\200-\377]" end t))
1568 '("ISO-8859-1" . "quoted-printable"))
1570 '("US-ASCII" . nil)) ;Default charset of MIME.
1573 (defun mime-string-encoder-for-emacs19 (method string)
1574 "For given METHOD that is a cons of charset and encoding, encode a STRING."
1575 (let ((charset (car method))
1576 (encoding (cdr method)))
1577 (cond ((stringp encoding)
1578 (mime-encode-string encoding string))
1579 ;; Return string without any encoding.
1584 ;; NEmacs implementations
1586 (defun mime-body-charset-chooser-for-nemacs (begin end)
1587 "Return a cons of charset and encoding of a message in a given region.
1588 Encoding name must be a canonical name, such as `base64'.
1589 US-ASCII and ISO-2022-JP are supported on NEmacs."
1590 (cond ((check-region-kanji-code begin end)
1591 ;; The following are safe encoding methods for use in
1592 ;; USENET News systems that strip off all ESCs.
1593 ;; '("ISO-2022-JP" . "quoted-printable")
1594 ;; '("ISO-2022-JP" . "base64")
1595 ;; The following expects transport systems are all MIME
1596 ;; compliants. For instance, ESCs are never stripped off.
1597 '("ISO-2022-JP" . nil))
1599 '("US-ASCII" . nil)) ;Default charset of MIME.
1602 (defun mime-string-encoder-for-nemacs (method string)
1603 "For given METHOD that is a cons of charset and encoding, encode a STRING.
1604 US-ASCII and ISO-2022-JP are supported on NEmacs."
1605 (let ((charset (car method))
1606 (encoding (cdr method)))
1607 (cond ((stringp encoding)
1608 (mime-encode-string encoding
1609 ;; Convert internal (EUC) to JIS code.
1610 (convert-string-kanji-code string 3 2)
1612 ;; NEmacs can convert into ISO-2022-JP automatically,
1613 ;; but can do it myself as follows:
1614 ;;(t (convert-string-kanji-code string 3 2))
1616 ;; Return string without any encoding.
1621 ;; Mule implementations
1622 ;; Thanks to contributions by wkenji@flab.fujitsu.co.jp (Kenji
1623 ;; WAKAMIYA) and handa@etl.go.jp (Kenichi Handa).
1625 (defun mime-body-charset-chooser-for-mule (begin end)
1626 "Return a cons of charset and encoding of a message in a given
1627 region. Encoding name must be a canonical name, such as `base64'.
1628 US-ASCII, ISO-8859-* (except for ISO-8859-6), ISO-2022-JP,
1629 ISO-2022-JP-2 and ISO-2022-INT-1 are supported on Mule. Either of
1630 charset ISO-2022-JP-2 or ISO-2022-INT-1 is used for multilingual text
1632 (let ((lclist (find-charset-region begin end)))
1633 (cond ((null lclist)
1634 '("US-ASCII" . nil)) ;Default charset of MIME.
1635 ;; Multilingual capability requred.
1636 ((and (> (length lclist) 1)
1637 (boundp '*iso-2022-int-1*))
1638 '("ISO-2022-INT-1" . nil))
1639 ((> (length lclist) 1)
1640 '("ISO-2022-JP-2" . nil))
1642 ((memq lc-ltn1 lclist)
1643 '("ISO-8859-1" . "quoted-printable"))
1644 ((memq lc-ltn2 lclist)
1645 '("ISO-8859-2" . "quoted-printable"))
1646 ((memq lc-ltn3 lclist)
1647 '("ISO-8859-3" . "quoted-printable"))
1648 ((memq lc-ltn4 lclist)
1649 '("ISO-8859-4" . "quoted-printable"))
1650 ((memq lc-crl lclist)
1651 '("ISO-8859-5" . "quoted-printable"))
1652 ;;((memq lc-arb lclist)
1653 ;; '("ISO-8859-6" . "quoted-printable"))
1654 ((memq lc-grk lclist)
1655 '("ISO-8859-7" . "quoted-printable"))
1656 ((memq lc-hbw lclist)
1657 '("ISO-8859-8" . "quoted-printable"))
1658 ((memq lc-ltn5 lclist)
1659 '("ISO-8859-9" . "quoted-printable"))
1660 ((memq lc-jp lclist)
1661 '("ISO-2022-JP" . nil))
1663 ((boundp '*iso-2022-int-1*)
1664 '("ISO-2022-INT-1" . nil))
1666 '("ISO-2022-JP-2" . nil))
1669 (defun mime-string-encoder-for-mule (method string)
1670 "For given METHOD that is a cons of charset and encoding, encode a
1671 STRING. US-ASCII, ISO-8859-* (except for ISO-8859-6), ISO-2022-JP,
1672 ISO-2022-JP-2 and ISO-2022-INT-1 are supported on Mule. Either of
1673 charset ISO-2022-JP-2 or ISO-2022-INT-1 is used for multilingual
1675 (let* ((charset (car method))
1676 (encoding (cdr method))
1678 (cdr (assoc (and (stringp charset) (upcase charset))
1679 '(("ISO-8859-1" . *ctext*)
1680 ("ISO-8859-2" . *iso-8859-2*)
1681 ("ISO-8859-3" . *iso-8859-3*)
1682 ("ISO-8859-4" . *iso-8859-4*)
1683 ("ISO-8859-5" . *iso-8859-5*)
1684 ;;("ISO-8859-6" . *iso-8859-6*)
1685 ("ISO-8859-7" . *iso-8859-7*)
1686 ("ISO-8859-8" . *iso-8859-8*)
1687 ("ISO-8859-9" . *iso-8859-9*)
1688 ("ISO-2022-JP" . *junet*)
1689 ("ISO-2022-JP-2" . *iso-2022-ss2-7*)
1690 ("ISO-2022-INT-1" . *iso-2022-int-1*)
1692 ;; In bilingual environment it may be unnecessary to convert the
1693 ;; coding system of the string unless transfer encoding is
1694 ;; required since such conversion may be performed by mule
1696 (if (not (null coding-system))
1697 (setq string (code-convert-string string *internal* coding-system)))
1698 (if (stringp encoding)
1699 (setq string (mime-encode-string encoding string)))
1704 ;; Sun implementations
1706 (defun mime-voice-recorder-for-sun ()
1707 "Record voice in a buffer using Sun audio device, and return the buffer.
1708 If the environment variable AUDIOHOST is defined, its value is used as
1709 a recording host instead of local host."
1710 (let ((buffer (get-buffer-create " *MIME audio*"))
1711 (host (getenv "AUDIOHOST")))
1712 (message "Start the recording on %s. Type C-g to finish the recording..."
1713 (or host (system-name)))
1717 (condition-case errorcode
1718 (let ((selective-display nil) ;Disable ^M to nl translation.
1720 (kanji-flag nil)) ;NEmacs
1721 ;; If AUDIOHOST is defined, use the value as recording host.
1722 (cond ((not (null host))
1723 ;; Disable automatic conversion of coding system if Mule.
1724 (if (featurep 'mule)
1725 (define-program-coding-system nil "rsh" *noconv*))
1735 ;; Disable automatic conversion of coding system if Mule.
1736 (if (featurep 'mule)
1737 (define-program-coding-system nil "cat" *noconv*))
1743 (quit (message "Type C-g to finish recording... done.")
1744 buffer ;Return the buffer
1749 ;;; Other useful commands.
1752 ;; Message forwarding commands as content-type "message/rfc822".
1754 (defun tm-edit/insert-message (&optional message)
1756 (let ((inserter (assoc-value major-mode tm-edit/message-inserter-alist)))
1757 (if (and inserter (fboundp inserter))
1759 (tm-edit/insert-tag "message" "rfc822")
1760 (funcall inserter message)
1762 (message "Sorry, I don't have message inserter for your MUA.")
1766 ;;; (defun mime-forward-from-rmail-using-mail ()
1767 ;;; "Forward current message in message/rfc822 content-type message from rmail.
1768 ;;; The message will be appended if being composed."
1770 ;;; ;;>> this gets set even if we abort. Can't do anything about it, though.
1771 ;;; (rmail-set-attribute "forwarded" t)
1772 ;;; (let ((initialized nil)
1774 ;;; (forwarding-buffer (current-buffer))
1775 ;;; (subject (concat "["
1776 ;;; (mail-strip-quoted-names (mail-fetch-field "From"))
1777 ;;; ": " (or (mail-fetch-field "Subject") "") "]")))
1778 ;;; ;; If only one window, use it for the mail buffer.
1779 ;;; ;; Otherwise, use another window for the mail buffer
1780 ;;; ;; so that the Rmail buffer remains visible
1781 ;;; ;; and sending the mail will get back to it.
1782 ;;; (setq initialized
1783 ;;; (if (one-window-p t)
1784 ;;; (mail nil nil subject)
1785 ;;; (mail-other-window nil nil subject)))
1787 ;;; (goto-char (point-max))
1788 ;;; (forward-line 1)
1789 ;;; (setq beginning (point))
1790 ;;; (tm-edit/insert-tag "message" "rfc822")
1791 ;;; (insert-buffer forwarding-buffer))
1792 ;;; (if (not initialized)
1793 ;;; (goto-char beginning))
1797 ;;; (defun mime-forward-from-gnus-using-mail ()
1798 ;;; "Forward current article in message/rfc822 content-type message from GNUS.
1799 ;;; The message will be appended if being composed."
1800 ;;; (let ((initialized nil)
1802 ;;; (forwarding-buffer (current-buffer))
1804 ;;; (concat "[" gnus-newsgroup-name "] "
1805 ;;; ;;(mail-strip-quoted-names (gnus-fetch-field "From")) ": "
1806 ;;; (or (gnus-fetch-field "Subject") ""))))
1807 ;;; ;; If only one window, use it for the mail buffer.
1808 ;;; ;; Otherwise, use another window for the mail buffer
1809 ;;; ;; so that the Rmail buffer remains visible
1810 ;;; ;; and sending the mail will get back to it.
1811 ;;; (setq initialized
1812 ;;; (if (one-window-p t)
1813 ;;; (mail nil nil subject)
1814 ;;; (mail-other-window nil nil subject)))
1816 ;;; (goto-char (point-max))
1817 ;;; (setq beginning (point))
1818 ;;; (tm-edit/insert-tag "message" "rfc822")
1819 ;;; (insert-buffer forwarding-buffer)
1820 ;;; ;; You have a chance to arrange the message.
1821 ;;; (run-hooks 'gnus-mail-forward-hook)
1823 ;;; (if (not initialized)
1824 ;;; (goto-char beginning))
1827 ;;; mime.el ends here
1828 (defun tm-edit/translate-region (beg end &optional boundary multipart)
1831 (concat mime-multipart-boundary " " (current-time-string)))
1835 (narrow-to-region beg end)
1836 (let ((tag nil) ;MIME tag
1837 (contype nil) ;Content-Type
1838 (encoding nil) ;Content-Transfer-Encoding
1839 (nparts 0)) ;Number of body parts
1840 ;; Normalize the body part by inserting appropriate message
1841 ;; tags for every message contents.
1842 (tm-edit/normalize-body)
1843 ;; Counting the number of Content-Type.
1844 (goto-char (point-min))
1845 (while (re-search-forward tm-edit/single-part-tag-regexp nil t)
1846 (setq nparts (1+ nparts)))
1847 ;; Begin translation.
1848 (cond ((and (<= nparts 1)(not multipart))
1849 ;; It's a singular message.
1850 (goto-char (point-min))
1851 (while (re-search-forward tm-edit/single-part-tag-regexp nil t)
1853 (buffer-substring (match-beginning 0) (match-end 0)))
1854 (delete-region (match-beginning 0) (1+ (match-end 0)))
1855 (setq contype (tm-edit/get-contype tag))
1856 (setq encoding (tm-edit/get-encoding tag))
1859 ;; It's a multipart message.
1860 (goto-char (point-min))
1861 (while (re-search-forward tm-edit/single-part-tag-regexp nil t)
1863 (buffer-substring (match-beginning 0) (match-end 0)))
1864 (delete-region (match-beginning 0) (match-end 0))
1865 (setq contype (tm-edit/get-contype tag))
1866 (setq encoding (tm-edit/get-encoding tag))
1867 (insert "--" boundary "\n")
1868 (insert "Content-Type: " contype "\n")
1870 (insert "Content-Transfer-Encoding: " encoding "\n"))
1872 ;; Define Content-Type as "multipart/mixed".
1874 (concat "multipart/mixed; boundary=\"" boundary "\""))
1875 ;; Content-Transfer-Encoding must be "7bit".
1876 ;; The following encoding can be `nil', but is
1877 ;; specified as is since there is no way that a user
1879 (setq encoding "7bit")
1880 ;; Insert the trailer.
1881 (goto-char (point-max))
1882 (if (not (= (preceding-char) ?\n))
1883 ;; Boundary must start with a newline.
1885 (insert "--" boundary "--\n")))
1886 (list contype encoding boundary nparts)
1890 (defun tm-edit/find-inmost ()
1891 (goto-char (point-min))
1892 (if (re-search-forward tm-edit/multipart-beginning-regexp nil t)
1893 (let ((bb (match-beginning 0))
1895 (type (buffer-substring (match-beginning 1)(match-end 1)))
1897 (setq end-exp (format "^--}-<<%s>>\n" type))
1899 (if (re-search-forward end-exp nil t)
1901 (setq eb (match-beginning 0))
1902 (setq ee (match-end 0))
1904 (setq eb (point-max))
1905 (setq ee (point-max))
1907 (narrow-to-region be eb)
1909 (if (re-search-forward tm-edit/multipart-beginning-regexp nil t)
1911 (narrow-to-region (match-beginning 0)(point-max))
1912 (tm-edit/find-inmost)
1915 ;;(delete-region eb ee)
1916 (list type bb be eb)
1919 (defun tm-edit/process-multipart-1 (boundary)
1920 (let ((ret (tm-edit/find-inmost)))
1922 (let ((type (car ret))
1923 (bb (nth 1 ret))(be (nth 2 ret))
1926 (narrow-to-region bb eb)
1927 (delete-region bb be)
1928 (setq bb (point-min))
1929 (setq eb (point-max))
1932 (if (looking-at tm-edit/multipart-end-regexp)
1933 (let ((beg (match-beginning 0))
1936 (delete-region beg end)
1937 (if (not (looking-at tm-edit/single-part-tag-regexp))
1938 (insert (concat (mime-make-text-tag) "\n"))
1940 (setq boundary (nth 2 (tm-edit/translate-region bb eb boundary t)))
1943 (format "--[[multipart/%s; boundary=\"%s\"][7bit]]\n"
1949 ;;; @ multipart enclosure
1952 (defun tm-edit/enclose-region (type beg end)
1957 (narrow-to-region beg end)
1962 (insert (format "--<<%s>>-{\n" type))
1963 (goto-char (point-max))
1966 (insert (format "\n--}-<<%s>>" type))
1967 (insert (format "--}-<<%s>>\n" type))
1969 (goto-char (point-max))
1979 (if (not (looking-at tm-edit/single-part-tag-regexp))
1980 (insert (mime-make-text-tag) "\n")
1988 (defun tm-edit/enclose-mixed-region (beg end)
1990 (tm-edit/enclose-region "mixed" beg end)
1993 (defun tm-edit/enclose-parallel-region (beg end)
1995 (tm-edit/enclose-region "parallel" beg end)
1998 (defun tm-edit/enclose-digest-region (beg end)
2000 (tm-edit/enclose-region "digest" beg end)
2003 (defun tm-edit/enclose-alternative-region (beg end)
2005 (tm-edit/enclose-region "alternative" beg end)
2012 (defun tm-edit/split-and-send (&optional cmd)
2014 (let ((tm-edit/message-max-length
2015 (or (cdr (assq major-mode tm-edit/message-max-length-alist))
2016 tm-edit/message-default-max-length))
2017 (lines (count-lines (point-min) (point-max)))
2019 (if (or (<= lines tm-edit/message-max-length)
2020 (not tm-edit/split-message))
2023 (cdr (assq major-mode tm-edit/message-default-sender-alist))
2025 (let* ((tm-edit/draft-file-name
2026 (or (buffer-file-name)
2027 (make-temp-name (expand-file-name "tm-draft" mime/tmp-dir))))
2028 (separator mail-header-separator)
2030 (eval (cdr (assq major-mode tm-edit/window-config-alist))))
2032 (replace-space-with-underline (current-time-string))
2033 "@" (system-name) "\"")))
2035 (let ((hook (cdr (assq major-mode
2036 tm-edit/message-before-send-hook-alist))))
2038 (let* ((header (rfc822/get-header-string-except
2039 tm-edit/message-nuke-headers separator))
2040 (orig-header (rfc822/get-header-string-except
2041 tm-edit/message-blind-headers separator))
2042 (subject (mail-fetch-field "subject"))
2043 (total (+ (/ lines tm-edit/message-max-length)
2044 (if (> (mod lines tm-edit/message-max-length) 0)
2047 (l tm-edit/message-max-length)
2048 (the-buf (current-buffer))
2049 (buf (get-buffer "*tmp-send*"))
2052 (cdr (assq major-mode tm-edit/message-sender-alist))
2053 (cdr (assq major-mode tm-edit/message-default-sender-alist))))
2055 (goto-char (point-min))
2056 (if (re-search-forward (concat "^" (regexp-quote separator) "$")
2062 (switch-to-buffer buf)
2064 (switch-to-buffer the-buf)
2066 (setq buf (get-buffer-create "*tmp-send*"))
2068 (switch-to-buffer buf)
2069 (make-variable-buffer-local 'mail-header-separator)
2070 (setq mail-header-separator separator)
2071 (switch-to-buffer the-buf)
2072 (goto-char (point-min))
2073 (re-search-forward "^$" nil t)
2075 (setq buf (get-buffer "*tmp-send*"))
2076 (setq data (buffer-substring
2082 (switch-to-buffer buf)
2085 (format "Subject: %s (%d/%d)\n" subject (+ i 1) total))
2087 (format "Mime-Version: 1.0 (split by tm-edit %s)\n"
2091 "Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2092 id (+ i 1) total separator))
2094 (insert orig-header))
2097 (call-interactively command))
2099 (switch-to-buffer the-buf)
2100 (setq l (+ l tm-edit/message-max-length))
2105 (cdr (assq major-mode tm-edit/message-after-send-hook-alist))))
2107 (set-buffer-modified-p nil)
2108 (cond ((y-or-n-p "Kill draft buffer? ")
2109 (kill-buffer (current-buffer))
2111 (set-window-configuration config))))
2116 ;;; @ preview message
2119 (defun tm-edit/preview-message ()
2120 "preview editing MIME message. [tm-edit.el]"
2122 (let* ((str (buffer-string))
2123 (separator mail-header-separator)
2124 (the-buf (current-buffer))
2125 (buf-name (buffer-name))
2126 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2127 (buf (get-buffer temp-buf-name))
2131 (switch-to-buffer buf)
2134 (setq buf (get-buffer-create temp-buf-name))
2135 (switch-to-buffer buf)
2138 (setq major-mode 'mime/temporary-message-mode)
2139 (make-local-variable 'mail-header-separator)
2140 (setq mail-header-separator separator)
2141 (make-local-variable 'mime/editing-buffer)
2142 (setq mime/editing-buffer the-buf)
2144 (run-hooks 'mime-translate-hook)
2145 (tm-edit/translate-buffer)
2146 (goto-char (point-min))
2147 (if (re-search-forward
2148 (concat "^" (regexp-quote separator) "$"))
2154 (defun tm-edit/quitting-method ()
2155 (let ((temp mime::preview/article-buffer)
2157 (mime-viewer/kill-buffer)
2159 (setq buf mime/editing-buffer)
2161 (switch-to-buffer buf)
2164 (set-alist 'mime-viewer/quitting-method-alist
2165 'mime/temporary-message-mode
2166 (function tm-edit/quitting-method)
2172 ;; by "OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
2173 ;; Mon, 10 Apr 1995 20:03:07 +0900
2175 (defvar tm-edit/draft-header-separator-alist
2176 '((news-reply-mode . mail-header-separator)
2177 (mh-letter-mode . mail-header-separator)
2180 (defvar mime::article/draft-header-separator nil)
2182 (defun tm-edit/draft-preview ()
2184 (let ((sep (assoc-value major-mode tm-edit/draft-header-separator-alist)))
2185 (or (stringp sep) (setq sep (eval sep)))
2186 (make-variable-buffer-local 'mime::article/draft-header-separator)
2187 (goto-char (point-min))
2189 (concat "^\\(" (regexp-quote sep) "\\)?$"))
2190 (setq mime::article/draft-header-separator
2191 (buffer-substring (match-beginning 0) (match-end 0)))
2193 (mime/viewer-mode (current-buffer))
2194 (pop-to-buffer (current-buffer))
2197 (defun mime-viewer::quitting-method/draft-preview ()
2198 (let ((mother mime/mother-buffer))
2200 (switch-to-buffer mother)
2201 (goto-char (point-min))
2205 (regexp-quote mime::article/draft-header-separator)
2209 (insert mime::article/draft-header-separator)
2210 (set-buffer-modified-p (buffer-modified-p))
2212 (mime-viewer/kill-buffer)
2213 (pop-to-buffer mother)
2216 (set-alist 'mime-viewer/quitting-method-alist
2218 (function mime-viewer::quitting-method/draft-preview)
2221 (set-alist 'mime-viewer/quitting-method-alist
2223 (function mime-viewer::quitting-method/draft-preview)
2230 (defun rfc822/get-header-string-except (pat boundary)
2231 (let ((case-fold-search t))
2234 (narrow-to-region (goto-char (point-min))
2237 (concat "^\\(" (regexp-quote boundary) "\\)?$")
2241 (goto-char (point-min))
2243 (while (re-search-forward rfc822/field-top-regexp nil t)
2244 (setq field (buffer-substring (match-beginning 0)
2247 (if (not (string-match pat field))
2248 (setq header (concat header field "\n"))
2253 (defun replace-space-with-underline (str)
2254 (mapconcat (function
2268 (run-hooks 'tm-edit-load-hook)