tm 7.35.
[elisp/tm.git] / tm-edit.el
1 ;;;
2 ;;; tm-edit.el --- Simple MIME Composer for GNU Emacs
3 ;;;
4
5 ;; Copyright (C) 1993 UMEDA Masanobu
6 ;; Copyright (C) 1994,1995 MORIOKA Tomohiko
7
8 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
9 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
10 ;; Version: $Revision: 7.35 $
11 ;; Keywords: mail, news, MIME, multimedia, multilingual
12
13 ;; This file is not part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING.  If not, write to
27 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28
29 ;;; Commentary:
30
31 ;; This is an Emacs minor mode for editing Internet multimedia
32 ;; messages formatted in MIME (RFC 1521 and RFC 1522). All messages in
33 ;; this mode are composed in the tagged MIME format, that are
34 ;; described in the following examples. The messages composed in the
35 ;; tagged MIME format are automatically translated into a MIME
36 ;; compliant message when exiting the mode.
37
38 ;; Mule (a multilingual extension to Emacs 18 and 19) has a capability
39 ;; of handling multilingual text in limited ISO-2022 manner that is
40 ;; based on early experiences in Japanese Internet community and
41 ;; resulted in RFC 1468 (ISO-2022-JP charset for MIME). In order to
42 ;; enable multilingual capability in single text message in MIME,
43 ;; charset of multilingual text written in Mule is declared as either
44 ;; `ISO-2022-JP-2' [RFC 1554] or `ISO-2022-INT-1'. Mule is required
45 ;; for reading the such messages.
46
47 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
48 ;; News mode.  First of all, you need the following autoload
49 ;; definition to load mime/editor-mode automatically:
50 ;;
51 ;; (autoload 'mime/editor-mode "tm-edit"
52 ;;      "Minor mode for editing MIME message." t)
53 ;;
54 ;; In case of Mail mode (includes VM mode), you need the following
55 ;; hook definition:
56 ;;
57 ;; (add-hook 'mail-mode-hook 'mime/editor-mode)
58 ;; (add-hook 'mail-send-hook 'mime-editor/maybe-translate)
59 ;;
60 ;; In case of MH-E, you need the following hook definition:
61 ;;
62 ;; (add-hook 'mh-letter-mode-hook
63 ;;           (function
64 ;;            (lambda ()
65 ;;              (mime/editor-mode)
66 ;;              (make-local-variable 'mail-header-separator)
67 ;;              (setq mail-header-separator "--------")
68 ;;              ))))
69 ;; (add-hook 'mh-before-send-letter-hook 'mime-editor/maybe-translate)
70 ;;
71 ;; In case of News mode, you need the following hook definition:
72 ;;
73 ;; (add-hook 'news-reply-mode-hook 'mime/editor-mode)
74 ;; (add-hook 'news-inews-hook 'mime-editor/maybe-translate)
75 ;;
76 ;; In case of Emacs 19, it is possible to emphasize the message tags
77 ;; using font-lock mode as follows:
78 ;;
79 ;; (add-hook 'mime/editor-mode-hook
80 ;;           (function
81 ;;            (lambda ()
82 ;;              (font-lock-mode 1)
83 ;;              (setq font-lock-keywords (list mime-editor/tag-regexp))
84 ;;              ))))
85
86 ;; The message tag looks like:
87 ;;
88 ;;      --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
89 ;;
90 ;; The tagged MIME message examples:
91 ;;
92 ;; This is a conventional plain text.  It should be translated into
93 ;; text/plain.
94 ;; 
95 ;;--[[text/plain]]
96 ;; This is also a plain text.  But, it is explicitly specified as is.
97 ;;
98 ;;--[[text/plain; charset=ISO-2022-JP]]
99 ;; \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.
100 ;;
101 ;;--[[text/richtext]]
102 ;; <center>This is a richtext.</center>
103 ;;
104 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
105 ;;
106 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
107
108 ;; LCD Archive Entry:
109 ;; mime|Masanobu UMEDA|umerin@mse.kyutech.ac.jp|
110 ;; Simple MIME Composer|
111 ;; $Date: 1995/12/15 15:02:14 $|$Revision: 7.35 $|~/misc/mime.el.Z|
112
113 ;;; Code:
114
115 (require 'sendmail)
116 (require 'mail-utils)
117 (require 'mel)
118 (require 'tl-822)
119 (require 'tl-list)
120 (require 'tm-view)
121 (require 'tm-ew-e)
122 (require 'signature)
123
124
125 ;;; @ version
126 ;;;
127
128 (defconst mime-editor/RCS-ID
129   "$Id: tm-edit.el,v 7.35 1995/12/15 15:02:14 morioka Exp $")
130
131 (defconst mime-editor/version (get-version-string mime-editor/RCS-ID))
132
133
134 ;;; @ variables
135 ;;;
136
137 (defvar mime-prefix "\C-c\C-x"
138   "*Keymap prefix for MIME commands.")
139
140 (defvar mime-signature-file "~/.signature.rtf"
141   "*Signature file to be included as a part of a multipart message.")
142
143 (defvar mime-ignore-preceding-spaces nil
144   "*Ignore preceding white spaces if non-nil.")
145
146 (defvar mime-ignore-trailing-spaces nil
147   "*Ignore trailing white spaces if non-nil.")
148
149 (defvar mime-ignore-same-text-tag t
150   "*Ignore preceding text content-type tag that is same with new one.
151 If non-nil, the text tag is not inserted unless something different.")
152
153 (defvar mime-auto-hide-body t
154   "*Hide non-textual body encoded in base64 after insertion if non-nil.")
155
156 (defvar mime-body-charset-chooser
157   (cond ((boundp 'NEMACS)
158          (function mime-body-charset-chooser-for-nemacs))
159         ((featurep 'mule)
160          (function mime-body-charset-chooser-for-mule))
161         ((string-match "^19\\." emacs-version)
162          (function mime-body-charset-chooser-for-emacs19))
163         (t                              ;ASCII only emacs
164          (function mime-body-charset-chooser-for-emacs18)))
165   "*Function to identify charset and encoding of a text in a given region.
166 The value is a form of (CHARSET . ENCODING), where ENCODING must be a
167 full name, such as base64.")
168
169 (defvar mime-string-encoder
170   (cond ((boundp 'NEMACS)
171          (function mime-string-encoder-for-nemacs))
172         ((featurep 'mule)
173          (function mime-string-encoder-for-mule))
174         ((string-match "^19\\." emacs-version)
175          (function mime-string-encoder-for-emacs19))
176         (t                              ;ASCII only emacs
177          (function mime-string-encoder-for-emacs18)))
178   "*Function to encode a string for given encoding method.
179 The method is a form of (CHARSET . ENCODING).")
180
181 (defvar mime-voice-recorder
182   (function mime-voice-recorder-for-sun)
183   "*Function to record a voice message and return a buffer that contains it.")
184
185 (defvar mime/editor-mode-hook nil
186   "*Hook called when enter MIME mode.")
187
188 (defvar mime-editor/translate-hook nil
189   "*Hook called before translating into a MIME compliant message.
190 To insert a signature file specified by mime-signature-file
191 (`.signature.rtf' by default) automatically, call the function
192 `mime-editor/insert-signature' from this hook.")
193
194 (defvar mime-editor/exit-hook nil
195   "*Hook called when exit MIME mode.")
196
197 (defvar mime-content-types
198   '(("text"
199      ;; Charset parameter need not to be specified, since it is
200      ;; defined automatically while translation.
201      ("plain"
202       ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
203       )
204      ("richtext"
205       ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
206       )
207      ("enriched"
208       ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
209       )
210      ("x-latex"
211       ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
212       )
213      ("html"
214       ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
215       )
216      ("x-rot13-47")
217      )
218     ("message"
219      ("external-body"
220       ("access-type"
221        ("anon-ftp"
222         ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
223         ("directory" "/pub/GNU/elisp/mime")
224         ("name")
225         ("mode" "image" "ascii" "local8"))
226        ("ftp"
227         ("site")
228         ("directory")
229         ("name")
230         ("mode" "image" "ascii" "local8"))
231        ("tftp"        ("site") ("name"))
232        ("afs"         ("site") ("name"))
233        ("local-file"  ("site") ("name"))
234        ("mail-server" ("server" "ftpmail@nic.karrn.ad.jp"))
235        ))
236      ("rfc822")
237      )
238     ("application"
239      ("octet-stream"
240       ("name")
241       ("type" "" "tar" "shar")
242       ("conversions"))
243      ("postscript")
244      ("x-kiss" ("x-cnf")))
245     ("image"
246      ("gif")
247      ("jpeg")
248      ("tiff")
249      ("x-pic")
250      ("x-xwd")
251      ("x-xbm")
252      )
253     ("audio" ("basic"))
254     ("video" ("mpeg"))
255     )
256   "*Alist of content-type, subtype, parameters and its values.")
257
258 (defvar mime-file-types
259   '(("\\.rtf$"
260      "text"     "richtext"      nil     nil)
261     ("\\.html$"
262      "text"     "html"          nil     nil)
263     ("\\.ps$"
264      "application" "postscript" nil     "quoted-printable")
265     ("\\.gif$"
266      "image"    "gif"           nil     "base64"
267      (("Content-Description" . file))
268      )
269     ("\\.jpg$"
270      "image"    "jpeg"          nil     "base64")
271     ("\\.xwd$"
272      "image"    "x-xwd"         nil     "base64")
273     ("\\.xbm$"
274      "image"    "x-xbm"         nil     "base64")
275     ("\\.pic$"
276      "image"    "x-pic"         nil     "base64"
277      (("Content-Description" . file))
278      )
279     ("\\.tiff$"
280      "image"    "tiff"          nil     "base64")
281     ("\\.au$"
282      "audio"    "basic"         nil     "base64")
283     ("\\.mpg$"
284      "video"    "mpeg"          nil     "base64")
285     ("\\.el$"
286      "application" "octet-stream" (("name" . file)
287                                    ("type" . "emacs-lisp")) "7bit")
288     ("\\.lsp$"
289      "application" "octet-stream" (("name" . file)
290                                    ("type" . "common-lisp")) "7bit")
291     ("\\.tar.gz$"
292      "application" "octet-stream" (("name" . file)
293                                    ("type" . "tar")
294                                    ("conversions" . "gzip")) nil)
295     ("\\.diff$"
296      "application" "octet-stream" (("name" . file)
297                                    ("type" . "patch")) nil)
298     ("\\.signature"
299      "text"     "plain"         nil     nil)
300     (".*"       nil             nil     nil     nil)
301     )
302   "*Alist of file name, types, parameters, and default encoding.
303 If encoding is nil, it is determined from its contents.")
304
305 ;;; @@ about message inserting
306 ;;;
307
308 (defvar mime-editor/yank-ignored-field-list
309   '("Received" "Sender" "Approved" "Path" "Status" "X-VM-.*" "X-UIDL")
310   "Delete these fields from original message when it is inserted
311 as message/rfc822 part.
312 Each elements are regexp of field-name. [tm-edit.el]")
313
314 (defvar mime-editor/yank-ignored-field-regexp
315   (concat "^"
316           (apply (function regexp-or) mime-editor/yank-ignored-field-list)
317           ":"))
318
319 (defvar mime-editor/message-inserter-alist nil)
320 (defvar mime-editor/mail-inserter-alist nil)
321
322 ;;; @@ about message splitting
323 ;;;
324
325 (defvar mime-editor/split-message t
326   "*Split large message if it is non-nil. [tm-edit.el]")
327
328 (defvar mime-editor/message-default-max-length 1000
329   "*Default maximum size of a message. [tm-edit.el]")
330
331 (defvar mime-editor/message-max-length-alist
332   '((news-reply-mode . 500)))
333
334 (defconst mime-editor/split-ignored-field-regexp
335   "\\(^Content-\\|^Subject:\\|^Mime-Version:\\)")
336
337 (defvar mime-editor/split-blind-field-regexp
338   "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)")
339
340 (defvar mime-editor/message-default-sender-alist
341   '((mail-mode . mail-send-and-exit)
342     (mh-letter-mode . mh-send-letter)
343     (news-reply-mode . gnus-inews-news)
344     ))
345
346 (defvar mime-editor/split-message-sender-alist
347   '((mail-mode
348      . (lambda ()
349          (interactive)
350          (sendmail-send-it)
351          ))
352     (mh-letter-mode
353      . (lambda (&optional arg)
354          (interactive "P")
355          (write-region (point-min) (point-max)
356                        mime-editor/draft-file-name nil 'no-message)
357          (cond (arg
358                 (pop-to-buffer "MH mail delivery")
359                 (erase-buffer)
360                 (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
361                                     "-nodraftfolder"
362                                     mh-send-args mime-editor/draft-file-name)
363                 (goto-char (point-max)) ; show the interesting part
364                 (recenter -1)
365                 (sit-for 1))
366                (t
367                 (apply 'mh-exec-cmd-quiet t mh-send-prog 
368                        (mh-list-to-string
369                         (list "-nopush" "-nodraftfolder"
370                               "-noverbose" "-nowatch"
371                               mh-send-args mime-editor/draft-file-name)))))
372          ))
373     ))
374
375 (defvar mime-editor/window-config-alist
376   '((mail-mode       . nil)
377     (mh-letter-mode  . mh-previous-window-config)
378     (news-reply-mode . (cond ((boundp 'gnus-winconf-post-news)
379                               (prog1
380                                   gnus-winconf-post-news
381                                 (setq gnus-winconf-post-news nil)
382                                 ))
383                              ((boundp 'gnus-prev-winconf)
384                               (prog1
385                                   gnus-prev-winconf
386                                 (setq gnus-prev-winconf nil)
387                                 ))
388                              ))
389     ))
390
391 (defvar mime-editor/news-reply-mode-server-running nil)
392
393
394 ;;; @@ about PGP
395 ;;;
396
397 (defvar mime-editor/signing-type nil
398   "*PGP signing type (pgp-elkins, pgp-kazu or nil). [tm-edit.el]")
399
400 (defvar mime-editor/encrypting-type nil
401   "*PGP encrypting type (pgp-elkins, pgp-kazu or nil).
402 Sorry `pgp-elkins' is not supported yet. [tm-edit.el]")
403
404 (if (or mime-editor/signing-type mime-editor/encrypting-type)
405     (require 'mailcrypt)
406   )
407
408
409 ;;; @@ about tag
410 ;;;
411
412 (defconst mime-editor/single-part-tag-regexp
413   "^--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
414   "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
415
416 (defconst mime-editor/multipart-beginning-regexp "^--<<\\([^<>]+\\)>>-{\n")
417
418 (defconst mime-editor/multipart-end-regexp "^--}-<<\\([^<>]+\\)>>\n")
419
420 (defconst mime-editor/beginning-tag-regexp
421   (regexp-or mime-editor/single-part-tag-regexp
422              mime-editor/multipart-beginning-regexp))
423
424 (defconst mime-editor/end-tag-regexp
425   (regexp-or mime-editor/single-part-tag-regexp
426              mime-editor/multipart-end-regexp))
427
428 (defconst mime-editor/tag-regexp
429   (regexp-or mime-editor/single-part-tag-regexp
430              mime-editor/multipart-beginning-regexp
431              mime-editor/multipart-end-regexp))
432
433 (defvar mime-tag-format "--[[%s]]"
434   "*Control-string making a MIME tag.")
435
436 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
437   "*Control-string making a MIME tag with encoding.")
438
439 ;;; @@ multipart boundary
440 ;;;
441
442 (defvar mime-multipart-boundary "Multipart"
443   "*Boundary of a multipart message.")
444
445
446 ;;; @@ buffer local variables
447 ;;;
448
449 (defvar mime/editor-mode-old-local-map nil)
450 (defvar mime/editor-mode-old-selective-display nil)
451 (defvar mime/editing-buffer nil)
452
453 \f
454 ;;; @ constants
455 ;;;
456
457 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
458   "*Specify MIME tspecials.
459 Tspecials means any character that matches with it in header must be quoted.")
460
461 (defconst mime-editor/mime-version-value
462   (format "1.0 (generated by tm-edit %s)" mime-editor/version)
463   "MIME version number.")
464
465
466 ;;; @ keymap and menu
467 ;;;
468
469 (defvar mime/editor-mode-flag nil)
470 (make-variable-buffer-local 'mime/editor-mode-flag)
471
472 (set-alist 'minor-mode-alist 'mime/editor-mode-flag '(" MIME-Edit"))
473
474 (defun mime-editor/define-keymap (keymap)
475   "Add mime-editor commands to KEYMAP."
476   (if (not (keymapp keymap))
477       nil
478     (define-key keymap "\C-t" 'mime-editor/insert-text)
479     (define-key keymap "\C-i" 'mime-editor/insert-file)
480     (define-key keymap "\C-e" 'mime-editor/insert-external)
481     (define-key keymap "\C-v" 'mime-editor/insert-voice)
482     (define-key keymap "\C-y" 'mime-editor/insert-message)
483     (define-key keymap "\C-m" 'mime-editor/insert-mail)
484     (define-key keymap "\C-w" 'mime-editor/insert-signature)
485     (define-key keymap "\C-s" 'mime-editor/insert-signature)
486     (define-key keymap "\C-k" 'mime-editor/insert-key)
487     (define-key keymap "t"    'mime-editor/insert-tag)
488     (define-key keymap "a"    'mime-editor/enclose-alternative-region)
489     (define-key keymap "p"    'mime-editor/enclose-parallel-region)
490     (define-key keymap "m"    'mime-editor/enclose-mixed-region)
491     (define-key keymap "d"    'mime-editor/enclose-digest-region)
492     (define-key keymap "s"    'mime-editor/enclose-signed-region)
493     (define-key keymap "e"    'mime-editor/enclose-encrypted-region)
494     (define-key keymap "\C-p" 'mime-editor/preview-message)
495     (define-key keymap "\C-z" 'mime-editor/exit)
496     (define-key keymap "?"    'mime-editor/help)
497     ))
498
499 (defconst mime-editor/menu-title "MIME-Edit")
500
501 (defconst mime-editor/menu-list
502   (nconc
503    '((mime-help "Describe MIME editor mode" mime-editor/help)
504      (file      "Insert File"           mime-editor/insert-file)
505      (external  "Insert External"       mime-editor/insert-external)
506      (voice     "Insert Voice"          mime-editor/insert-voice)
507      (message   "Insert Message"        mime-editor/insert-message)
508      (mail      "Insert Mail"           mime-editor/insert-mail)
509      (signature "Insert Signature"      mime-editor/insert-signature)
510      (text      "Insert Text"           mime-editor/insert-text)
511      (tag       "Insert Tag"            mime-editor/insert-tag)
512      (alternative "Enclose as alternative"
513                   mime-editor/enclose-alternative-region)
514      (parallel  "Enclose as parallel"   mime-editor/enclose-parallel-region)
515      (mixed     "Enclose as serial"     mime-editor/enclose-mixed-region)
516      (digest    "Enclose as digest"     mime-editor/enclose-digest-region)
517      (signed    "Enclose as signed"     mime-editor/enclose-signed-region)
518      (encrypted "Enclose as encrypted"  mime-editor/enclose-encrypted-region)
519      (key       "Insert Public Key"     mime-editor/insert-key)
520      (split     "About split"           mime-editor/set-split)
521      )
522    (if (and (featurep 'mailcrypt)
523             (not (or mime-editor/signing-type mime-editor/encrypting-type)))
524        '((sign    "About sign"       mime-editor/set-sign)
525          (encrypt "About encryption" mime-editor/set-encrypt)
526          ))
527    '((preview   "Preview Message"       mime-editor/preview-message))
528    )
529   "MIME-edit menubar entry.")
530
531 (defun mime-editor/define-menu-for-emacs19 ()
532   "Define menu for Emacs 19."
533   (define-key (current-local-map) [menu-bar mime-edit]
534     (cons mime-editor/menu-title
535           (make-sparse-keymap mime-editor/menu-title)))
536   (mapcar (function
537            (lambda (item)
538              (define-key (current-local-map)
539                (vector 'menu-bar 'mime-edit (car item))
540                (cons (nth 1 item)(nth 2 item))
541                )
542              ))
543           (reverse mime-editor/menu-list)
544           ))
545
546 ;;; modified by Pekka Marjola <pema@niksula.hut.fi>
547 ;;;     1995/9/5 (c.f. [tm-en:69])
548 (defun mime-editor/define-menu-for-xemacs ()
549   "Define menu for Emacs 19."
550   (cond ((featurep 'menubar)
551          (make-local-variable 'current-menubar)
552          (set-buffer-menubar current-menubar)
553          (add-submenu nil
554                       (cons mime-editor/menu-title
555                             (mapcar (function
556                                      (lambda (item)
557                                        (vector (nth 1 item)(nth 2 item)
558                                                mime/editor-mode-flag)
559                                        ))
560                                     mime-editor/menu-list)))
561          )))
562
563 ;;; modified by Steven L. Baur <steve@miranova.com>
564 ;;;     1995/12/6 (c.f. [tm-en:209])
565 (if (and (string-match "XEmacs\\|Lucid" emacs-version)
566          (not (boundp 'mime-editor/popup-menu-for-xemacs)))
567     (setq mime-editor/popup-menu-for-xemacs
568           (append '("MIME Commands" "---")
569                   (mapcar (function (lambda (item)
570                                       (vector (nth 1 item)
571                                               (nth 2 item)
572                                               t)))
573                           mime-editor/menu-list)))
574   )
575 ;;; end
576
577
578 ;;; @ functions
579 ;;;
580
581 ;;;###autoload
582 (defun mime/editor-mode ()
583   "MIME minor mode for editing the tagged MIME message.
584
585 In this mode, basically, the message is composed in the tagged MIME
586 format.  The message tag looks like:
587
588         `--[[text/plain; charset=ISO-2022-JP][7bit]]'.
589
590 The tag specifies the MIME content type, subtype, optional parameters
591 and transfer encoding of the message following the tag.  Messages
592 without any tag are treated as `text/plain' by default.  Charset and
593 transfer encoding are automatically defined unless explicitly
594 specified.  Binary messages such as audio and image are usually hidden
595 using selective-display facility.  The messages in the tagged MIME
596 format are automatically translated into a MIME compliant message when
597 exiting this mode.
598
599 Available charsets depend on Emacs version being used.  The following
600 lists the available charsets of each emacs.
601
602 Emacs18:        US-ASCII is only available.
603 NEmacs:         US-ASCII and ISO-2022-JP are available.
604 Emacs19:        US-ASCII and ISO-8859-1 are available.
605 Mule:           US-ASCII, ISO-8859-* (except for ISO-8859-6),
606                 ISO-2022-JP, ISO-2022-JP-2 and ISO-2022-INT-1 are available.
607
608 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in Mule is expected to
609 be used to represent multilingual text in intermixed manner.  Any
610 languages that has no registered charset are represented as either
611 ISO-2022-JP-2 or ISO-2022-INT-1 in Mule.
612
613 Following commands are available in addition to major mode commands:
614 \\[mime-editor/insert-text]     insert a text message.
615 \\[mime-editor/insert-file]     insert a (binary) file.
616 \\[mime-editor/insert-external] insert a reference to external body.
617 \\[mime-editor/insert-voice]    insert a voice message.
618 \\[mime-editor/insert-message]  insert a mail or news message.
619 \\[mime-editor/insert-mail]     insert a mail message.
620 \\[mime-editor/insert-signature]        insert a signature file at end.
621 \\[mime-editor/insert-tag]      insert a new MIME tag.
622 \\[mime-editor/enclose-alternative-region]      enclose as multipart/alternative.
623 \\[mime-editor/enclose-parallel-region] enclose as multipart/parallel.
624 \\[mime-editor/enclose-mixed-region]    enclose as multipart/mixed.
625 \\[mime-editor/enclose-digest-region]   enclose as multipart/digest.
626 \\[mime-editor/enclose-signed-region]   enclose as PGP signed.
627 \\[mime-editor/enclose-encrypted-region]        enclose as PGP encrypted.
628 \\[mime-editor/insert-key]      insert PGP public key.
629 \\[mime-editor/preview-message] preview editing MIME message.
630 \\[mime-editor/exit]    exit and translate into a MIME compliant message.
631 \\[mime-editor/maybe-translate] exit, translate and run the original command.
632 \\[mime-editor/help]    show this help.
633
634 Additional commands are available in some major modes:
635 C-c C-c         exit, translate and run the original command.
636 C-c C-s         exit, translate and run the original command.
637
638 The following is a message example written in the tagged MIME format.
639 TABs at the beginning of the line are not a part of the message:
640
641         This is a conventional plain text.  It should be translated
642         into text/plain.
643         --[[text/plain]]
644         This is also a plain text.  But, it is explicitly specified as
645         is.
646         --[[text/plain; charset=ISO-2022-JP]]
647         \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
648         \e$B%H$G$9\e(B.
649         --[[text/richtext]]
650         <center>This is a richtext.</center>
651         --[[image/gif][base64]]^M...image encoded in base64 here...
652         --[[audio/basic][base64]]^M...audio encoded in base64 here...
653
654 User customizable variables (not documented all of them):
655  mime-prefix
656     Specifies a key prefix for MIME minor mode commands.
657
658  mime-signature-file
659     Specifies a signature file to be included as part of a multipart
660     message.
661
662  mime-ignore-preceding-spaces
663     Preceding white spaces in a message body are ignored if non-nil.
664
665  mime-ignore-trailing-spaces
666     Trailing white spaces in a message body are ignored if non-nil.
667
668  mime-auto-fill-header
669     Fill header fields that contain encoded-words if non-nil.
670
671  mime-auto-hide-body
672     Hide a non-textual body message encoded in base64 after insertion
673     if non-nil.
674
675  mime-body-charset-chooser
676     Specifies a function to identify charset and encoding of a text in
677     a given region.  The value is a form of (CHARSET . ENCODING),
678     where ENCODING must be a full name, such as base64.
679
680  mime-string-encoder
681     Specifies a function to encode a string for given encoding method.
682     The method is a form of (CHARSET . ENCODING).
683
684  mime-voice-recorder
685     Specifies a function to record a voice message and return a buffer
686     that contains it.  The function mime-voice-recorder-for-sun is for
687     Sun SparcStations.
688
689  mime/editor-mode-hook
690     Turning on MIME mode calls the value of mime/editor-mode-hook, if
691     it is non-nil.
692
693  mime-editor/translate-hook
694     The value of mime-editor/translate-hook is called just before translating
695     the tagged MIME format into a MIME compliant message if it is
696     non-nil.  If the hook call the function mime-editor/insert-signature,
697     the signature file will be inserted automatically.
698
699  mime-editor/exit-hook
700     Turning off MIME mode calls the value of mime-editor/exit-hook, if it is
701     non-nil."
702   (interactive)
703   (if mime/editor-mode-flag
704       (error "You are already editing a MIME message.")
705     (setq mime/editor-mode-flag t)
706     ;; Remember old key bindings.
707     (make-local-variable 'mime/editor-mode-old-local-map)
708     (setq mime/editor-mode-old-local-map (current-local-map))
709     ;; Add MIME commands to current local map.
710     (use-local-map (copy-keymap (current-local-map)))
711     (if (not (lookup-key (current-local-map) mime-prefix))
712         (define-key (current-local-map) mime-prefix (make-sparse-keymap)))
713     (mime-editor/define-keymap (lookup-key (current-local-map) mime-prefix))
714     
715     ;; Define menu.  Menus for other emacs implementations are
716     ;; welcome.
717     ;;  modified by Pekka Marjola <pema@niksula.hut.fi>
718     ;;          1995/9/5 (c.f. [tm-eng:69])
719     (cond ((string-match "XEmacs\\|Lucid" emacs-version)
720            (mime-editor/define-menu-for-xemacs))
721           ((string-match "^19\\." emacs-version)
722            (mime-editor/define-menu-for-emacs19)
723            ))
724     ;; end
725     
726     ;; Remember old selective-display.
727     (make-local-variable 'mime/editor-mode-old-selective-display)
728     (setq mime/editor-mode-old-selective-display selective-display)
729     (setq selective-display t)
730     ;; I don't care about saving these.
731     (setq paragraph-start
732           (concat mime-editor/single-part-tag-regexp "\\|" paragraph-start))
733     (setq paragraph-separate
734           (concat mime-editor/single-part-tag-regexp "\\|" paragraph-separate))
735     (run-hooks 'mime/editor-mode-hook)
736     (message
737      (substitute-command-keys
738       "Type \\[mime-editor/exit] to exit MIME mode, and type \\[mime-editor/help] to get help."))
739     ))
740
741 ;;;###autoload
742 (defalias 'edit-mime 'mime/editor-mode)         ; for convenience
743 (defalias 'mime-mode 'mime/editor-mode)         ; for convenience
744
745 (defun mime-editor/exit (&optional nomime no-error)
746   "Translate the tagged MIME message into a MIME compliant message.
747 With no argument encode a message in the buffer into MIME, otherwise
748 just return to previous mode."
749   (interactive "P")
750   (if (not mime/editor-mode-flag)
751       (if (null no-error)
752           (error "You aren't editing a MIME message.")
753         )
754     (if (not nomime)
755         (progn
756           (run-hooks 'mime-editor/translate-hook)
757           (mime-editor/translate-buffer)))
758     ;; Restore previous state.
759     (setq mime/editor-mode-flag nil)
760     (use-local-map mime/editor-mode-old-local-map)
761     
762     ;; modified by Pekka Marjola <pema@niksula.hut.fi>
763     ;;  1995/9/5 (c.f. [tm-eng:69])
764     (if (string-match "XEmacs\\|Lucid" emacs-version)
765         (progn
766           (delete-menu-item (list mime-editor/menu-title))
767                                         ; should rather be const
768           ;; (while mime-editor/xemacs-old-bindings
769           ;;   (eval (pop mime-editor/xemacs-old-bindings)))
770           (local-unset-key mime-prefix)))
771     ;; end
772     
773     (setq selective-display mime/editor-mode-old-selective-display)
774     (set-buffer-modified-p (buffer-modified-p))
775     (run-hooks 'mime-editor/exit-hook)
776     (message "Exit MIME editor mode.")
777     ))
778
779 (defun mime-editor/maybe-translate ()
780   (interactive)
781   (mime-editor/exit nil t)
782   (call-interactively 'mime-editor/maybe-split-and-send)
783   )
784
785 (defun mime-editor/help ()
786   "Show help message about MIME mode."
787   (interactive)
788   (with-output-to-temp-buffer "*Help*"
789     (princ "MIME editor mode:\n")
790     (princ (documentation 'mime/editor-mode))
791     (print-help-return-message)))
792
793 (defun mime-editor/insert-text ()
794   "Insert a text message.
795 Charset is automatically obtained from the mime-body-charset-chooser."
796   (interactive)
797   (if (and (mime-editor/insert-tag "text" nil nil)
798            (looking-at mime-editor/single-part-tag-regexp))
799       (progn
800         ;; Make a space between the following message.
801         (insert "\n")
802         (forward-char -1)
803         )))
804
805 (defun mime-editor/insert-file (file)
806   "Insert a message from a file."
807   (interactive "fInsert file as MIME message: ")
808   (let*  ((guess (mime-find-file-type file))
809           (pritype (nth 0 guess))
810           (subtype (nth 1 guess))
811           (parameters (nth 2 guess))
812           (default (nth 3 guess))       ;Guess encoding from its file name.
813           (fields (nth 4 guess))
814           (encoding
815            (if (not (interactive-p))
816                default
817              (completing-read
818               (concat "What transfer encoding"
819                       (if default
820                           (concat " (default "
821                                   (if (string-equal default "")
822                                       "\"\""
823                                     default)
824                                   ")"
825                                   ))
826                       ": ")
827               mime-encoding-method-alist nil t nil))))
828     (if (string-equal encoding "")
829         (setq encoding default))
830     (if (or (consp parameters) (consp fields))
831         (let ((rest parameters) cell attribute value)
832           (setq parameters "")
833           (while rest
834             (setq cell (car rest))
835             (setq attribute (car cell))
836             (setq value (cdr cell))
837             (if (eq value 'file)
838                 (setq value (file-name-nondirectory file))
839               )
840             (setq parameters (concat parameters "; " attribute "=" value))
841             (setq rest (cdr rest))
842             )
843           (setq rest fields)
844           (while rest
845             (setq cell (car rest))
846             (setq attribute (car cell))
847             (setq value (cdr cell))
848             (if (eq value 'file)
849                 (setq value (file-name-nondirectory file))
850               )
851             (setq parameters (concat parameters "\n" attribute ": " value))
852             (setq rest (cdr rest))
853             )
854           ))
855     (mime-editor/insert-tag pritype subtype parameters)
856     (mime-editor/insert-binary-file file encoding)
857     ))
858
859 (defun mime-editor/insert-external ()
860   "Insert a reference to external body."
861   (interactive)
862   (mime-editor/insert-tag "message" "external-body" nil ";\n\t")
863   ;;(forward-char -1)
864   ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
865   ;;(forward-line 1)
866   (let* ((pritype (mime-prompt-for-type))
867          (subtype (mime-prompt-for-subtype pritype))
868          (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
869     (and pritype
870          subtype
871          (insert "Content-Type: "
872                  pritype "/" subtype (or parameters "") "\n")))
873   (if (and (not (eobp))
874            (not (looking-at mime-editor/single-part-tag-regexp)))
875       (insert (mime-make-text-tag) "\n")))
876
877 (defun mime-editor/insert-voice ()
878   "Insert a voice message."
879   (interactive)
880   (mime-editor/insert-tag "audio" "basic" nil)
881   (let ((buffer (funcall mime-voice-recorder)))
882     (unwind-protect
883         (mime-editor/insert-binary-buffer buffer "base64")
884       (kill-buffer buffer)
885       )))
886
887 (defun mime-editor/insert-signature (&optional arg)
888   "Insert a signature file specified by mime-signature-file."
889   (interactive "P")
890   (let ((signature
891          (expand-file-name
892           (if arg
893               (read-file-name "Insert your signature: "
894                               (concat signature-file-name "-")
895                               signature-file-name
896                               nil)
897             (signature/get-signature-file-name))))
898         )
899     (if signature-insert-at-eof
900         (goto-char (point-max))
901       )
902     (apply (function mime-editor/insert-tag)
903            (mime-find-file-type signature))
904     (if (file-readable-p signature)
905         (progn
906           (goto-char (point-max))
907           (if (not (bolp))
908               (insert "\n"))
909           (delete-blank-lines)
910           (insert-file-contents signature)
911           (set-buffer-modified-p (buffer-modified-p))
912                                         ; force mode line update
913           ))))
914
915 \f
916 ;; Insert a new tag around a point.
917
918 (defun mime-editor/insert-tag (&optional pritype subtype parameters delimiter)
919   "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
920 If nothing is inserted, return nil."
921   (interactive)
922   (let ((oldtag nil)
923         (newtag nil)
924         (current (point))
925         exist-prev-tag exist-next-tag)
926     (setq pritype
927           (or pritype
928               (mime-prompt-for-type)))
929     (setq subtype
930           (or subtype
931               (mime-prompt-for-subtype pritype)))
932     (setq parameters
933           (or parameters
934               (mime-prompt-for-parameters pritype subtype delimiter)))
935     ;; Make a new MIME tag.
936     (setq newtag (mime-make-tag pritype subtype parameters))
937     ;; Find an current MIME tag.
938     (setq oldtag
939           (save-excursion
940             (if (mime-editor/goto-tag)
941                 (progn
942                   (if (eq current (match-beginning 0))
943                       (setq exist-next-tag t)
944                     (setq exist-prev-tag t)
945                     )
946                   (buffer-substring (match-beginning 0) (match-end 0))
947                   )
948               ;; Assume content type is 'text/plan'.
949               (mime-make-tag "text" "plain")
950               )))
951     ;; We are only interested in TEXT.
952     (if (and oldtag
953              (not (mime-test-content-type
954                    (mime-editor/get-contype oldtag) "text")))
955         (setq oldtag nil))
956     (cond (exist-prev-tag (insert "\n"))
957           (exist-next-tag (save-excursion
958                             (insert "\n")
959                             )))
960     ;; (beginning-of-line)
961     ;; (cond ((and oldtag                  ;Text
962     ;;             (not (eobp))
963     ;;             (save-excursion
964     ;;               (forward-line -1)
965     ;;               (looking-at mime-editor/beginning-tag-regexp)
966     ;;               )
967     ;;             (or mime-ignore-same-text-tag
968     ;;                 (not (string-equal oldtag newtag))))
969     ;;        ;; If point is at the next of current tag, move to the
970     ;;        ;; beginning of the tag to disable insertion of extra tag.
971     ;;        (forward-line -1))
972     ;;       ((and oldtag                  ;Text
973     ;;             (not (eobp))
974     ;;             (not (looking-at mime-editor/tag-regexp))
975     ;;             (or mime-ignore-same-text-tag
976     ;;                 (not (string-equal oldtag newtag))))
977     ;;        ;; Copy current tag to break a text into two.
978     ;;        (save-excursion
979     ;;          (insert oldtag "\n")))
980     ;;       ((and (null oldtag)           ;Not text
981     ;;             (not (looking-at mime-editor/tag-regexp)))
982     ;;        ;; Adjust insertion point.  In the middle of text, it is
983     ;;        ;; okay to break the text into two.  However, it should not
984     ;;        ;; be broken into two, if otherwise.
985     ;;        (goto-char (mime-editor/content-end))
986     ;;        (if (eolp)
987     ;;            (forward-line 1))
988     ;;        (if (not (bolp))
989     ;;            (insert "\n"))
990     ;;        ))
991     (if (not (bolp))
992         (if exist-prev-tag
993             (forward-line 1)
994           (insert "\n")
995           ))
996     ;; Make a new tag.
997     (if (or (not oldtag)                ;Not text
998             (or mime-ignore-same-text-tag
999                 (not (string-equal oldtag newtag))))
1000         (progn
1001           ;; Mark the beginning of the tag for convenience.
1002           (push-mark (point) 'nomsg)
1003           (insert newtag "\n")
1004           (list pritype subtype parameters) ;New tag is created.
1005           )
1006       ;; Restore previous point.
1007       (goto-char current)
1008       nil                               ;Nothing is created.
1009       )
1010     ))
1011
1012 ;; Insert the binary content after MIME tag.
1013 ;;      modified by MORITA Masahiro <hiro@isl.ntt.JP>
1014 ;;      for x-uue
1015 (defun mime-editor/insert-binary-file (file &optional encoding)
1016   "Insert binary FILE at point.
1017 Optional argument ENCODING specifies an encoding method such as base64."
1018   (let ((tmpbuf (get-buffer-create " *MIME insert*")))
1019     (save-excursion
1020       (set-buffer tmpbuf)
1021       (erase-buffer)
1022       (let ((mc-flag nil)               ;Mule
1023             (file-coding-system-for-read
1024              (if (featurep 'mule) *noconv*))
1025             (kanji-flag nil)            ;NEmacs
1026             (emx-binary-mode t)         ;Stop CRLF to LF conversion in OS/2
1027             )
1028         (let (jka-compr-compression-info-list
1029               jam-zcat-filename-list)
1030           (insert-file-contents file))))
1031     (prog1
1032         (if (and (stringp encoding)
1033                  (string-equal (downcase encoding) "x-uue"))
1034             (progn
1035               (require 'mel-u)
1036               (let ((uuencode-external-encoder
1037                      (cons (car uuencode-external-encoder)
1038                            (list (file-name-nondirectory file))
1039                            )))
1040                 (mime-editor/insert-binary-buffer tmpbuf encoding)
1041                 ))
1042           (mime-editor/insert-binary-buffer tmpbuf encoding))
1043       (kill-buffer tmpbuf))))
1044
1045 ;; Insert the binary content after MIME tag.
1046 ;;      modified by MORITA Masahiro <hiro@isl.ntt.JP>
1047 ;;      for x-uue
1048 (defun mime-editor/insert-binary-buffer (buffer &optional encoding)
1049   "Insert binary BUFFER at point.
1050 Optional argument ENCODING specifies an encoding method such as base64."
1051   (let* ((tagend (1- (point)))          ;End of the tag
1052          (hide-p (and mime-auto-hide-body
1053                       (stringp encoding)
1054                       (let ((en (downcase encoding)))
1055                         (or (string-equal en "base64")
1056                             (string-equal en "x-uue")
1057                             ))))
1058          )
1059     (save-restriction
1060       (narrow-to-region (1- (point)) (point))
1061       (let ((start (point))
1062             (emx-binary-mode t))        ;Stop LF to CRLF conversion in OS/2
1063         (insert-buffer-substring buffer)
1064         ;; Encode binary message if necessary.
1065         (if encoding
1066             (mime-encode-region encoding start (point-max))))
1067       (if hide-p
1068           (progn
1069             (mime-flag-region (point-min) (1- (point-max)) ?\^M)
1070             (goto-char (point-max)))
1071         ))
1072     ;; Define encoding even if it is 7bit.
1073     (if (stringp encoding)
1074         (save-excursion
1075           (goto-char tagend)            ;Make sure which line the tag is on.
1076           (mime-editor/define-encoding encoding)))
1077     ))
1078
1079 \f
1080 ;; Commands work on a current message flagment.
1081
1082 (defun mime-editor/goto-tag ()
1083   "Search for the beginning of the tagged MIME message."
1084   (let ((current (point)) multipart)
1085     (if (looking-at mime-editor/tag-regexp)
1086         t
1087       ;; At first, go to the end.
1088       (cond ((re-search-forward mime-editor/beginning-tag-regexp nil t)
1089              (goto-char (match-beginning 0)) ;For multiline tag
1090              (forward-line -1)
1091              (end-of-line)
1092              )
1093             (t
1094              (goto-char (point-max))
1095              ))
1096       ;; Then search for the beginning. 
1097       (re-search-backward mime-editor/end-tag-regexp nil t)
1098       (beginning-of-line)
1099       (or (looking-at mime-editor/beginning-tag-regexp)
1100           ;; Restore previous point.
1101           (progn
1102             (goto-char current)
1103             nil
1104             ))
1105       )))
1106
1107 (defun mime-editor/content-beginning ()
1108   "Return the point of the beginning of content."
1109   (save-excursion
1110     (let ((beg (save-excursion
1111                  (beginning-of-line) (point))))
1112       (if (mime-editor/goto-tag)
1113           (let ((top (point)))
1114             (goto-char (match-end 0))
1115             (if (and (= beg top)
1116                      (= (following-char) ?\^M))
1117                 (point)
1118               (forward-line 1)
1119               (point)))
1120         ;; Default text/plain tag.
1121         (goto-char (point-min))
1122         (re-search-forward
1123          (concat "\n" (regexp-quote mail-header-separator)
1124                  (if mime-ignore-preceding-spaces
1125                      "[ \t\n]*\n" "\n")) nil 'move)
1126         (point))
1127       )))
1128
1129 (defun mime-editor/content-end ()
1130   "Return the point of the end of content."
1131   (save-excursion
1132     (let ((beg (save-excursion
1133                  (beginning-of-line) (point))))
1134       (if (mime-editor/goto-tag)
1135           (let ((top (point)))
1136             (goto-char (match-end 0))
1137             (if (and (= beg top)        ;Must be on the same line.
1138                      (= (following-char) ?\^M))
1139                 (progn
1140                   (end-of-line)
1141                   (point))
1142               ;; Move to the end of this text.
1143               (if (re-search-forward mime-editor/tag-regexp nil 'move)
1144                   ;; Don't forget a multiline tag.
1145                   (goto-char (match-beginning 0)))
1146               (point)
1147               ))
1148         ;; Assume the message begins with text/plain.
1149         (goto-char (mime-editor/content-beginning))
1150         (if (re-search-forward mime-editor/tag-regexp nil 'move)
1151             ;; Don't forget a multiline tag.
1152             (goto-char (match-beginning 0)))
1153         (point))
1154       )))
1155
1156 (defun mime-editor/define-charset (charset)
1157   "Set charset of current tag to CHARSET."
1158   (save-excursion
1159     (if (mime-editor/goto-tag)
1160         (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1161           (delete-region (match-beginning 0) (match-end 0))
1162           (insert
1163            (mime-create-tag (mime-set-parameter
1164                              (mime-editor/get-contype tag) "charset" charset)
1165                             (mime-editor/get-encoding tag))))
1166       )))
1167
1168 (defun mime-editor/define-encoding (encoding)
1169   "Set encoding of current tag to ENCODING."
1170   (save-excursion
1171     (if (mime-editor/goto-tag)
1172         (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1173           (delete-region (match-beginning 0) (match-end 0))
1174           (insert (mime-create-tag (mime-editor/get-contype tag) encoding)))
1175       )))
1176
1177 (defun mime-editor/choose-charset ()
1178   "Choose charset of a text following current point."
1179   (save-excursion
1180     (let* ((beg (point))
1181            (end (mime-editor/content-end)))
1182       (car (funcall mime-body-charset-chooser beg end)))))
1183
1184 (defun mime-editor/choose-encoding ()
1185   "Choose encoding of a text following current point."
1186   (save-excursion
1187     (let* ((beg (point))
1188            (end (mime-editor/content-end)))
1189       (cdr (funcall mime-body-charset-chooser beg end)))))
1190
1191 (defun mime-make-text-tag (&optional subtype)
1192   "Make a tag for a text after current point.
1193 Subtype of text type can be specified by an optional argument SUBTYPE.
1194 Otherwise, it is obtained from mime-content-types."
1195   (let* ((pritype "text")
1196          (subtype (or subtype
1197                       (car (car (cdr (assoc pritype mime-content-types)))))))
1198     ;; Charset should be defined later.
1199     (mime-make-tag pritype subtype)))
1200
1201 \f
1202 ;; Tag handling functions
1203
1204 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1205   "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1206   (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1207                            (or parameters ""))
1208                    encoding))
1209
1210 (defun mime-create-tag (contype &optional encoding)
1211   "Make a tag with CONTENT-TYPE and optional ENCODING."
1212   (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1213           contype encoding))
1214
1215 (defun mime-editor/get-contype (tag)
1216   "Return Content-Type (including parameters) of TAG."
1217   (and (stringp tag)
1218        (or (string-match mime-editor/single-part-tag-regexp tag)
1219            (string-match mime-editor/multipart-beginning-regexp tag)
1220            (string-match mime-editor/multipart-end-regexp tag)
1221            )
1222        (substring tag (match-beginning 1) (match-end 1))
1223        ))
1224
1225 (defun mime-editor/get-encoding (tag)
1226   "Return encoding of TAG."
1227   (and (stringp tag)
1228        (string-match mime-editor/single-part-tag-regexp tag)
1229        (match-beginning 3)
1230        (not (= (match-beginning 3) (match-end 3)))
1231        (substring tag (match-beginning 3) (match-end 3))))
1232
1233 (defun mime-get-parameter (contype parameter)
1234   "For given CONTYPE return value for PARAMETER.
1235 Nil if no such parameter."
1236   (if (string-match
1237        (concat
1238         ";[ \t\n]*"
1239         (regexp-quote parameter)
1240         "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1241        contype)
1242       (substring contype (match-beginning 1) (match-end 1))
1243     nil                                 ;No such parameter
1244     ))
1245
1246 (defun mime-set-parameter (contype parameter value)
1247   "For given CONTYPE set PARAMETER to VALUE."
1248   (if (string-match
1249        (concat
1250         ";[ \t\n]*\\("
1251         (regexp-quote parameter)
1252         "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1253        contype)
1254       ;; Change value
1255       (concat (substring contype 0 (match-beginning 1))
1256               parameter "=" value
1257               (substring contype (match-end 1)))
1258     (concat contype "; " parameter "=" value)))
1259
1260 (defun mime-strip-parameters (contype)
1261   "Return primary content-type and subtype without parameters for CONTYPE."
1262   (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1263       (substring contype (match-beginning 1) (match-end 1)) nil))
1264
1265 (defun mime-test-content-type (contype type &optional subtype)
1266   "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1267   (and (stringp contype)
1268        (stringp type)
1269        (string-match
1270         (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1271         (downcase contype))))
1272
1273 \f
1274 ;; Basic functions
1275
1276 (defun mime-find-file-type (file)
1277   "Guess Content-Type, subtype, and parameters from FILE."
1278   (let ((guess nil)
1279         (guesses mime-file-types))
1280     (while (and (not guess) guesses)
1281       (if (string-match (car (car guesses)) file)
1282           (setq guess (cdr (car guesses))))
1283       (setq guesses (cdr guesses)))
1284     guess
1285     ))
1286
1287 (defun mime-prompt-for-type ()
1288   "Ask for Content-type."
1289   (let ((type ""))
1290     ;; Repeat until primary content type is specified.
1291     (while (string-equal type "")
1292       (setq type
1293             (completing-read "What content type: "
1294                              mime-content-types
1295                              nil
1296                              'require-match ;Type must be specified.
1297                              nil
1298                              ))
1299       (if (string-equal type "")
1300           (progn
1301             (message "Content type is required.")
1302             (beep)
1303             (sit-for 1)
1304             ))
1305       )
1306     type
1307     ))
1308
1309 (defun mime-prompt-for-subtype (pritype)
1310   "Ask for Content-type subtype of Content-Type PRITYPE."
1311   (let* ((default (car (car (cdr (assoc pritype mime-content-types)))))
1312          (answer
1313           (completing-read
1314            (if default
1315                (concat
1316                 "What content subtype: (default " default ") ")
1317              "What content subtype: ")
1318            (cdr (assoc pritype mime-content-types))
1319            nil
1320            'require-match               ;Subtype must be specified.
1321            nil
1322            )))
1323     (if (string-equal answer "") default answer)))
1324
1325 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1326   "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1327 Optional DELIMITER specifies parameter delimiter (';' by default)."
1328   (let* ((delimiter (or delimiter "; "))
1329          (parameters
1330           (mapconcat
1331            (function identity)
1332            (delq nil
1333                  (mime-prompt-for-parameters-1
1334                   (cdr (assoc subtype
1335                               (cdr (assoc pritype mime-content-types))))))
1336            delimiter
1337            )))
1338     (if (and (stringp parameters)
1339              (not (string-equal parameters "")))
1340         (concat delimiter parameters)
1341       ""                                ;"" if no parameters
1342       )))
1343
1344 (defun mime-prompt-for-parameters-1 (optlist)
1345   (apply (function append)
1346          (mapcar (function mime-prompt-for-parameter) optlist)))
1347
1348 (defun mime-prompt-for-parameter (parameter)
1349   "Ask for PARAMETER.
1350 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1351   (let* ((prompt (car parameter))
1352          (choices (mapcar (function
1353                            (lambda (e)
1354                              (if (consp e) e (list e))))
1355                           (cdr parameter)))
1356          (default (car (car choices)))
1357          (answer nil))
1358     (if choices
1359         (progn
1360           (setq answer
1361                 (completing-read
1362                  (concat "What " prompt
1363                          ": (default "
1364                          (if (string-equal default "") "\"\"" default)
1365                          ") ")
1366                  choices nil nil ""))
1367           ;; If nothing is selected, use default.
1368           (if (string-equal answer "")
1369               (setq answer default)))
1370       (setq answer
1371             (read-string (concat "What " prompt ": "))))
1372     (cons (if (and answer
1373                    (not (string-equal answer "")))
1374               (concat prompt "="
1375                       ;; Note: control characters ignored!
1376                       (if (string-match mime-tspecials-regexp answer)
1377                           (concat "\"" answer "\"") answer)))
1378           (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1379     ))
1380
1381 (defun mime-encode-string (encoding string)
1382   "Using ENCODING encode a STRING.
1383 If the STRING is too long, the encoded string may be broken into
1384 several lines."
1385   (save-excursion
1386     (set-buffer (get-buffer-create " *MIME encoding*"))
1387     (erase-buffer)
1388     (insert string)
1389     (mime-encode-region encoding (point-min) (point-max))
1390     (prog1
1391         (buffer-substring (point-min) (point-max))
1392       (kill-buffer (current-buffer)))))
1393
1394 (defun mime-decode-string (encoding string)
1395   "Using ENCODING decode a STRING."
1396   (save-excursion
1397     (set-buffer (get-buffer-create " *MIME decoding*"))
1398     (erase-buffer)
1399     (insert string)
1400     (mime-decode-region encoding (point-min) (point-max))
1401     (prog1
1402         (buffer-substring (point-min) (point-max))
1403       (kill-buffer (current-buffer)))))
1404
1405 (defun mime-flag-region (from to flag)
1406   "Hides or shows lines from FROM to TO, according to FLAG.
1407 If FLAG is `\\n' (newline character) then text is shown,
1408 while if FLAG is `\\^M' (control-M) the text is hidden."
1409   (let ((buffer-read-only nil)          ;Okay even if write protected.
1410         (modp (buffer-modified-p)))
1411     (unwind-protect
1412         (subst-char-in-region from to
1413                               (if (= flag ?\n) ?\^M ?\n)
1414                               flag t)
1415       (set-buffer-modified-p modp))))
1416
1417 \f
1418 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1419 ;;;
1420
1421 (defun mime-editor/translate-buffer ()
1422   "Encode the tagged MIME message in current buffer in MIME compliant message."
1423   (interactive)
1424   (if (catch 'mime-editor/error
1425         (save-excursion
1426           (mime/encode-message-header)
1427           (mime-editor/translate-body)
1428           (mime-editor/pgp-processing)
1429           ))
1430       (progn
1431         (undo)
1432         (error "Translation error!")
1433         )))
1434
1435 (defun mime-editor/find-inmost ()
1436   (goto-char (point-min))
1437   (if (re-search-forward mime-editor/multipart-beginning-regexp nil t)
1438       (let ((bb (match-beginning 0))
1439             (be (match-end 0))
1440             (type (buffer-substring (match-beginning 1)(match-end 1)))
1441             end-exp eb ee)
1442         (setq end-exp (format "^--}-<<%s>>\n" type))
1443         (widen)
1444         (if (re-search-forward end-exp nil t)
1445             (progn
1446               (setq eb (match-beginning 0))
1447               (setq ee (match-end 0))
1448               )
1449           (setq eb (point-max))
1450           (setq ee (point-max))
1451           )
1452         (narrow-to-region be eb)
1453         (goto-char be)
1454         (if (re-search-forward mime-editor/multipart-beginning-regexp nil t)
1455             (let (ret)
1456               (narrow-to-region (match-beginning 0)(point-max))
1457               (mime-editor/find-inmost)
1458               )
1459           (widen)
1460           ;;(delete-region eb ee)
1461           (list type bb be eb)
1462           ))))
1463
1464 (defun mime-editor/process-multipart-1 (boundary)
1465   (let ((ret (mime-editor/find-inmost)))
1466     (if ret
1467         (let ((type (car ret))
1468               (bb (nth 1 ret))(be (nth 2 ret))
1469               (eb (nth 3 ret))
1470               )
1471           (narrow-to-region bb eb)
1472           (delete-region bb be)
1473           (setq bb (point-min))
1474           (setq eb (point-max))
1475           (widen)
1476           (goto-char eb)
1477           (if (looking-at mime-editor/multipart-end-regexp)
1478               (let ((beg (match-beginning 0))
1479                     (end (match-end 0))
1480                     )
1481                 (delete-region beg end)
1482                 (if (and (not (looking-at mime-editor/single-part-tag-regexp))
1483                          (not (eobp)))
1484                     (insert (concat (mime-make-text-tag) "\n"))
1485                   )))
1486           (cond ((string= type "signed")
1487                  (cond ((eq mime-editor/signing-type 'pgp-elkins)
1488                         (mime-editor/sign-pgp-elkins bb eb boundary)
1489                         )
1490                        ((eq mime-editor/signing-type 'pgp-kazu)
1491                         (mime-editor/process-pgp-kazu 'mc-sign
1492                                                       bb eb boundary)
1493                         )
1494                        ))
1495                 ((string= type "encrypted")
1496                  (cond ((eq mime-editor/signing-type 'pgp-elkins)
1497                         (message "Sorry, it is not supported yet")
1498                         )
1499                        ((eq mime-editor/signing-type 'pgp-kazu)
1500                         (mime-editor/process-pgp-kazu 'mc-encrypt
1501                                                       bb eb boundary)
1502                         )))
1503                 (t
1504                  (setq boundary
1505                        (nth 2 (mime-editor/translate-region bb eb
1506                                                             boundary t)))
1507                  (goto-char bb)
1508                  (insert
1509                   (format "--[[multipart/%s;
1510  boundary=\"%s\"][7bit]]\n"
1511                           type boundary))
1512                  ))
1513           boundary))))
1514
1515 (defun mc-tmpgp-generic-parser (result)
1516   (if (or (not (eq result 0))
1517           (mc-message "^\aError: +Bad pass phrase\\.$" (current-buffer))
1518           )
1519       (progn
1520         (mc-deactivate-passwd t)
1521         nil)
1522     result))
1523
1524 (defvar mc-tmpgp-path "tmpgp")
1525
1526 (defun tm:mc-pgp-sign-region (start end &optional id unclear boundary)
1527   (if (not (boundp 'mc-pgp-user-id))
1528       (load "mc-pgp")
1529     )
1530   (let ((process-environment process-environment)
1531         (buffer (get-buffer-create mc-buffer-name))
1532         passwd args key parser pgp-path
1533         signature-file)
1534     (setq key (mc-pgp-lookup-key (or id mc-pgp-user-id)))
1535     (setq passwd
1536           (mc-activate-passwd
1537            (cdr key)
1538            (format "PGP passphrase for %s (%s): " (car key) (cdr key))))
1539     (setenv "PGPPASSFD" "0")
1540     (setq args
1541           (list "+verbose=1" "+language=en"
1542                 (format "+clearsig=%s" (if unclear "off" "on"))
1543                 "+batchmode" "-u" (cdr key)))
1544     (if boundary
1545         (progn
1546           (setq parser 'mc-tmpgp-generic-parser
1547                 pgp-path mc-tmpgp-path
1548                 signature-file (make-temp-name
1549                                 (expand-file-name "tm-sign" mime/tmp-dir))
1550                 args (cons "-fbst" args))
1551           (if mc-pgp-comment
1552               (setq args (cons (format "+comment=%s" mc-pgp-comment) args))
1553             )
1554           (setq args (cons signature-file args))
1555           )
1556       (setq parser 'mc-pgp-generic-parser
1557             pgp-path mc-pgp-path
1558             args (cons "-fast" args)
1559             )
1560       (if mc-pgp-comment
1561           (setq args (cons (format "+comment=%s" mc-pgp-comment) args))
1562         )
1563       )
1564     (message "Signing as %s ..." (car key))
1565     (if (mc-process-region start end passwd pgp-path args parser buffer)
1566         (progn
1567           (if boundary
1568               (progn
1569                 (goto-char (point-min))
1570                 (insert
1571                  (format "\
1572 --[[multipart/signed; protocol=\"application/pgp-signature\";
1573  boundary=\"%s\"; micalg=pgp-md5][7bit]]
1574 --%s\n" boundary boundary))
1575                 (goto-char (point-max))
1576                 (insert (format "\n--%s\n" boundary))
1577                 (insert "Content-Type: application/pgp-signature
1578 Content-Transfer-Encoding: base64
1579
1580 ")
1581                 (insert-file-contents signature-file)
1582                 (goto-char (point-max))
1583                 (insert (format "\n--%s--\n" boundary))
1584                 ))
1585           (delete-file signature-file)
1586           (message "Signing as %s ... Done." (car key))
1587           t)
1588       nil)))
1589
1590 (defun mime-editor/sign-pgp-elkins (beg end boundary)
1591   (save-excursion
1592     (save-restriction
1593       (narrow-to-region beg end)
1594       (let* ((ret
1595               (mime-editor/translate-region beg end boundary))
1596              (ctype    (car ret))
1597              (encoding (nth 1 ret))
1598              (parts    (nth 3 ret))
1599              (pgp-boundary (concat "pgp-sign-" boundary))
1600              )
1601         (goto-char beg)
1602         (insert (format "Content-Type: %s\n" ctype))
1603         (if encoding
1604             (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1605           )
1606         (insert "\n")
1607         (or (tm:mc-pgp-sign-region (point-min)(point-max)
1608                                    nil nil pgp-boundary)
1609             (throw 'mime-editor/error 'pgp-error)
1610             )
1611         ))))
1612
1613 (defun mime-editor/process-pgp-kazu (type beg end boundary)
1614   (save-excursion
1615     (save-restriction
1616       (narrow-to-region beg end)
1617       (let* ((ret
1618               (mime-editor/translate-region beg end boundary))
1619              (ctype    (car ret))
1620              (encoding (nth 1 ret))
1621              (parts    (nth 3 ret))
1622              )
1623         (goto-char beg)
1624         (insert (format "Content-Type: %s\n" ctype))
1625         (if encoding
1626             (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1627           )
1628         (insert "\n")
1629         (if (null
1630              (progn
1631                (goto-char beg)
1632                (insert "=\n")
1633                (prog1
1634                    (let ((mail-header-separator "="))
1635                      (call-interactively type)
1636                      )
1637                  (goto-char beg)
1638                  (and (search-forward "=\n")
1639                       (replace-match ""))
1640                  )))
1641             (throw 'mime-editor/error 'pgp-error)
1642           )
1643         (goto-char beg)
1644         (insert
1645          "--[[application/pgp; format=mime][7bit]]\n")
1646         ))
1647     ))
1648
1649 (defun mime-editor/translate-body ()
1650   "Encode the tagged MIME body in current buffer in MIME compliant message."
1651   (interactive)
1652   (save-excursion
1653     (let ((boundary
1654            (concat mime-multipart-boundary " " (current-time-string)))
1655           (i 1)
1656           (time (current-time-string))
1657           ret)
1658       (while (mime-editor/process-multipart-1
1659               (format "%s %s-%d" mime-multipart-boundary time i))
1660         (setq i (1+ i))
1661         )
1662       (save-restriction
1663         ;; We are interested in message body.
1664         (let* ((beg
1665                 (progn
1666                   (goto-char (point-min))
1667                   (re-search-forward
1668                    (concat "\n" (regexp-quote mail-header-separator)
1669                            (if mime-ignore-preceding-spaces
1670                                "[ \t\n]*\n" "\n")) nil 'move)
1671                   (point)))
1672                (end
1673                 (progn
1674                   (goto-char (point-max))
1675                   (and mime-ignore-trailing-spaces
1676                        (re-search-backward "[^ \t\n]\n" beg t)
1677                        (forward-char 1))
1678                   (point))))
1679           (setq ret (mime-editor/translate-region
1680                      beg end
1681                      (format "%s %s-%d" mime-multipart-boundary time i)))
1682           ))
1683       (let ((contype (car ret))         ;Content-Type
1684             (encoding (nth 1 ret))      ;Content-Transfer-Encoding
1685             )
1686         ;; Make primary MIME headers.
1687         (or (mail-position-on-field "Mime-Version")
1688             (insert mime-editor/mime-version-value))
1689         ;; Remove old Content-Type and other fields.
1690         (save-restriction
1691           (goto-char (point-min))
1692           (search-forward (concat "\n" mail-header-separator "\n") nil t)
1693           (narrow-to-region (point-min) (point))
1694           (goto-char (point-min))
1695           (mime-delete-field "Content-Type")
1696           (mime-delete-field "Content-Transfer-Encoding"))
1697         ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1698         (mail-position-on-field "Content-Type")
1699         (insert contype)
1700         (if encoding
1701             (progn
1702               (mail-position-on-field "Content-Transfer-Encoding")
1703               (insert encoding)))
1704         ))))
1705
1706 (defun mime-editor/translate-region (beg end &optional boundary multipart)
1707   (if (null boundary)
1708       (setq boundary
1709             (concat mime-multipart-boundary " " (current-time-string)))
1710     )
1711   (save-excursion
1712     (save-restriction
1713       (narrow-to-region beg end)
1714       (let ((tag nil)                   ;MIME tag
1715             (contype nil)               ;Content-Type
1716             (encoding nil)              ;Content-Transfer-Encoding
1717             (nparts 0))                 ;Number of body parts
1718         ;; Normalize the body part by inserting appropriate message
1719         ;; tags for every message contents.
1720         (mime-editor/normalize-body)
1721         ;; Counting the number of Content-Type.
1722         (goto-char (point-min))
1723         (while (re-search-forward mime-editor/single-part-tag-regexp nil t)
1724           (setq nparts (1+ nparts)))
1725         ;; Begin translation.
1726         (cond ((and (<= nparts 1)(not multipart))
1727                ;; It's a singular message.
1728                (goto-char (point-min))
1729                (while (re-search-forward
1730                        mime-editor/single-part-tag-regexp nil t)
1731                  (setq tag
1732                        (buffer-substring (match-beginning 0) (match-end 0)))
1733                  (delete-region (match-beginning 0) (1+ (match-end 0)))
1734                  (setq contype (mime-editor/get-contype tag))
1735                  (setq encoding (mime-editor/get-encoding tag))
1736                  ))
1737               (t
1738                ;; It's a multipart message.
1739                (goto-char (point-min))
1740                (while (re-search-forward
1741                        mime-editor/single-part-tag-regexp nil t)
1742                  (setq tag
1743                        (buffer-substring (match-beginning 0) (match-end 0)))
1744                  (delete-region (match-beginning 0) (match-end 0))
1745                  (setq contype (mime-editor/get-contype tag))
1746                  (setq encoding (mime-editor/get-encoding tag))
1747                  (insert "--" boundary "\n")
1748                  (insert "Content-Type: " contype "\n")
1749                  (if encoding
1750                      (insert "Content-Transfer-Encoding: " encoding "\n"))
1751                  )
1752                ;; Define Content-Type as "multipart/mixed".
1753                (setq contype
1754                      (concat "multipart/mixed;\n boundary=\"" boundary "\""))
1755                ;; Content-Transfer-Encoding must be "7bit".
1756                ;; The following encoding can be `nil', but is
1757                ;; specified as is since there is no way that a user
1758                ;; specifies it.
1759                (setq encoding "7bit")
1760                ;; Insert the trailer.
1761                (goto-char (point-max))
1762                (if multipart
1763                    (insert "--" boundary "--\n")
1764                  (insert "\n--" boundary "--\n")
1765                  )))
1766         (list contype encoding boundary nparts)
1767         ))))
1768
1769 (defun mime-editor/normalize-body ()
1770   "Normalize the body part by inserting appropriate message tags."
1771   ;; Insert the first MIME tags if necessary.
1772   (goto-char (point-min))
1773   (if (not (looking-at mime-editor/single-part-tag-regexp))
1774       (insert (mime-make-text-tag) "\n"))
1775   ;; Check each tag, and add new tag or correct it if necessary.
1776   (goto-char (point-min))
1777   (while (re-search-forward mime-editor/single-part-tag-regexp nil t)
1778     (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
1779            (contype (mime-editor/get-contype tag))
1780            (charset (mime-get-parameter contype "charset"))
1781            (encoding (mime-editor/get-encoding tag)))
1782       ;; Remove extra whitespaces after the tag.
1783       (if (looking-at "[ \t]+$")
1784           (delete-region (match-beginning 0) (match-end 0)))
1785       (cond ((= (following-char) ?\^M)
1786              ;; It must be image, audio or video.
1787              (let ((beg (point))
1788                    (end (mime-editor/content-end)))
1789                ;; Insert explicit MIME tags after hidden messages.
1790                (forward-line 1)
1791                (if (and (not (eobp))
1792                         (not (looking-at mime-editor/single-part-tag-regexp)))
1793                    (progn
1794                      (insert (mime-make-text-tag) "\n")
1795                      (forward-line -1)  ;Process it again as text.
1796                      ))
1797                ;; Show a hidden message.  The point is not altered
1798                ;; after the conversion.
1799                (mime-flag-region beg end ?\n)))
1800             ((mime-test-content-type contype "message")
1801              ;; Content-type "message" should be sent as is.
1802              (forward-line 1))
1803             ((mime-test-content-type contype "text")
1804              ;; Define charset for text if necessary.
1805              (setq charset (or charset (mime-editor/choose-charset)))
1806              (mime-editor/define-charset charset)
1807              (if (string-equal contype "text/x-rot13-47")
1808                  (save-excursion
1809                    (forward-line)
1810                    (set-mark (point))
1811                    (goto-char (mime-editor/content-end))
1812                    (tm:caesar-region)
1813                    ))
1814              ;; Point is now on current tag.
1815              ;; Define encoding and encode text if necessary.
1816              (if (null encoding)        ;Encoding is not specified.
1817                  (let* ((encoding (mime-editor/choose-encoding))
1818                         (beg (mime-editor/content-beginning))
1819                         (end (mime-editor/content-end))
1820                         (body (buffer-substring beg end))
1821                         (encoded (funcall mime-string-encoder
1822                                           (cons charset encoding) body)))
1823                    (if (not (string-equal body encoded))
1824                        (progn
1825                          (goto-char beg)
1826                          (delete-region beg end)
1827                          (insert encoded)
1828                          (goto-char beg)))
1829                    (mime-editor/define-encoding encoding)))
1830              (forward-line 1))
1831             ((null encoding)            ;Encoding is not specified.
1832              ;; Application, image, audio, video, and any other
1833              ;; unknown content-type without encoding should be
1834              ;; encoded.
1835              (let* ((encoding "base64") ;Encode in BASE64 by default.
1836                     (beg (mime-editor/content-beginning))
1837                     (end (mime-editor/content-end))
1838                     (body (buffer-substring beg end))
1839                     (encoded (funcall mime-string-encoder
1840                                       (cons nil encoding) body)))
1841                (if (not (string-equal body encoded))
1842                    (progn
1843                      (goto-char beg)
1844                      (delete-region beg end)
1845                      (insert encoded)
1846                      (goto-char beg)))
1847                (mime-editor/define-encoding encoding))
1848              (forward-line 1))
1849             )
1850       )))
1851
1852 (defun mime-delete-field (field)
1853   "Delete header FIELD."
1854   (let ((regexp (format "^%s:[ \t]*" field)))
1855     (goto-char (point-min))
1856     (while (re-search-forward regexp nil t)
1857       (delete-region (match-beginning 0)
1858                      (progn (forward-line 1) (point)))
1859       )))
1860
1861 \f
1862 ;;;
1863 ;;; Platform dependent functions
1864 ;;;
1865
1866 ;; Emacs 18 implementations
1867
1868 (defun mime-body-charset-chooser-for-emacs18 (begin end)
1869   "Return a cons of charset and encoding of a message in a given region.
1870 Encoding name must be a canonical name, such as `base64'."
1871   '("US-ASCII" . nil)                   ;Default charset of MIME.
1872   )
1873
1874 (defun mime-string-encoder-for-emacs18 (method string)
1875   "For given METHOD that is a cons of charset and encoding, encode a STRING."
1876   (let ((charset (car method))
1877         (encoding (cdr method)))
1878     (cond ((stringp encoding)
1879            (mime-encode-string encoding string))
1880           ;; Return string without any encoding.
1881           (t string)
1882           )))
1883
1884 \f
1885 ;; Emacs 19 implementations
1886
1887 (defun mime-body-charset-chooser-for-emacs19 (begin end)
1888   "Return a cons of charset and encoding of a message in a given region.
1889 Encoding name must be a canonical name, such as `base64'.
1890 US-ASCII and ISO-8859-1 are supported on Emacs 19."
1891   (cond ((save-excursion
1892            (goto-char begin)
1893            (re-search-forward "[\200-\377]" end t))
1894          '("ISO-8859-1" . "quoted-printable"))
1895         (t
1896          '("US-ASCII" . nil))           ;Default charset of MIME.
1897         ))
1898
1899 (defun mime-string-encoder-for-emacs19 (method string)
1900   "For given METHOD that is a cons of charset and encoding, encode a STRING."
1901   (let ((charset (car method))
1902         (encoding (cdr method)))
1903     (cond ((stringp encoding)
1904            (mime-encode-string encoding string))
1905           ;; Return string without any encoding.
1906           (t string)
1907           )))
1908
1909 \f
1910 ;; NEmacs implementations
1911
1912 (defun mime-body-charset-chooser-for-nemacs (begin end)
1913   "Return a cons of charset and encoding of a message in a given region.
1914 Encoding name must be a canonical name, such as `base64'.
1915 US-ASCII and ISO-2022-JP are supported on NEmacs."
1916   (cond ((check-region-kanji-code begin end)
1917          ;; The following are safe encoding methods for use in
1918          ;; USENET News systems that strip off all ESCs.
1919          ;; '("ISO-2022-JP" . "quoted-printable")
1920          ;; '("ISO-2022-JP" . "base64")
1921          ;; The following expects transport systems are all MIME
1922          ;; compliants.  For instance, ESCs are never stripped off.
1923          '("ISO-2022-JP" . nil))
1924         (t
1925          '("US-ASCII" . nil))           ;Default charset of MIME.
1926         ))
1927
1928 (defun mime-string-encoder-for-nemacs (method string)
1929   "For given METHOD that is a cons of charset and encoding, encode a STRING.
1930 US-ASCII and ISO-2022-JP are supported on NEmacs."
1931   (let ((charset (car method))
1932         (encoding (cdr method)))
1933     (cond ((stringp encoding)
1934            (mime-encode-string encoding
1935                                ;; Convert internal (EUC) to JIS code.
1936                                (convert-string-kanji-code string 3 2)
1937                                ))
1938           ;; NEmacs can convert into ISO-2022-JP automatically,
1939           ;; but can do it myself as follows:
1940           ;;(t (convert-string-kanji-code string 3 2))
1941
1942           ;; Return string without any encoding.
1943           (t string)
1944           )))
1945
1946 \f
1947 ;; Mule implementations
1948 ;; Thanks to contributions by wkenji@flab.fujitsu.co.jp (Kenji
1949 ;; WAKAMIYA) and handa@etl.go.jp (Kenichi Handa).
1950
1951 (defun mime-body-charset-chooser-for-mule (begin end)
1952   "Return a cons of charset and encoding of a message in a given
1953 region.  Encoding name must be a canonical name, such as `base64'.
1954 US-ASCII, ISO-8859-* (except for ISO-8859-6), ISO-2022-JP,
1955 ISO-2022-JP-2 and ISO-2022-INT-1 are supported on Mule.  Either of
1956 charset ISO-2022-JP-2 or ISO-2022-INT-1 is used for multilingual text
1957 in Mule."
1958   (let ((lclist (find-charset-region begin end)))
1959     (cond ((null lclist)
1960            '("US-ASCII" . nil))         ;Default charset of MIME.
1961           ;; Multilingual capability requred.
1962           ((and (> (length lclist) 1)
1963                 (boundp '*iso-2022-int-1*))
1964            '("ISO-2022-INT-1" . nil))
1965           ((> (length lclist) 1)
1966            '("ISO-2022-JP-2" . nil))
1967           ;; Simple charset.
1968           ((memq lc-ltn1 lclist)
1969            '("ISO-8859-1" . "quoted-printable"))
1970           ((memq lc-ltn2 lclist)
1971            '("ISO-8859-2" . "quoted-printable"))
1972           ((memq lc-ltn3 lclist)
1973            '("ISO-8859-3" . "quoted-printable"))
1974           ((memq lc-ltn4 lclist)
1975            '("ISO-8859-4" . "quoted-printable"))
1976           ((memq lc-crl lclist)
1977            '("ISO-8859-5" . "quoted-printable"))
1978           ;;((memq lc-arb lclist)
1979           ;; '("ISO-8859-6" . "quoted-printable"))
1980           ((memq lc-grk lclist)
1981            '("ISO-8859-7" . "quoted-printable"))
1982           ((memq lc-hbw lclist)
1983            '("ISO-8859-8" . "quoted-printable"))
1984           ((memq lc-ltn5 lclist)
1985            '("ISO-8859-9" . "quoted-printable"))
1986           ((memq lc-jp lclist)
1987            '("ISO-2022-JP" . nil))
1988           ((memq lc-kr lclist)
1989            '("ISO-2022-KR" . nil))
1990           ;; Unknown charset.
1991           ((boundp '*iso-2022-int-1*)
1992            '("ISO-2022-INT-1" . nil))
1993           (t
1994            '("ISO-2022-JP-2" . nil))
1995           )))
1996
1997 (defun mime-string-encoder-for-mule (method string)
1998   "For given METHOD that is a cons of charset and encoding, encode a
1999 STRING.  US-ASCII, ISO-8859-* (except for ISO-8859-6), ISO-2022-JP,
2000 ISO-2022-JP-2 and ISO-2022-INT-1 are supported on Mule.  Either of
2001 charset ISO-2022-JP-2 or ISO-2022-INT-1 is used for multilingual
2002 text."
2003   (let* ((charset (car method))
2004          (encoding (cdr method))
2005          (coding-system
2006           (cdr (assoc (and (stringp charset) (upcase charset))
2007                       '(("ISO-8859-1" . *ctext*)
2008                         ("ISO-8859-2" . *iso-8859-2*)
2009                         ("ISO-8859-3" . *iso-8859-3*)
2010                         ("ISO-8859-4" . *iso-8859-4*)
2011                         ("ISO-8859-5" . *iso-8859-5*)
2012                         ;;("ISO-8859-6" . *iso-8859-6*)
2013                         ("ISO-8859-7" . *iso-8859-7*)
2014                         ("ISO-8859-8" . *iso-8859-8*)
2015                         ("ISO-8859-9" . *iso-8859-9*)
2016                         ("ISO-2022-JP" . *junet*)
2017                         ("ISO-2022-JP-2" . *iso-2022-ss2-7*)
2018                         ("ISO-2022-KR" . *korean-mail*)
2019                         ("ISO-2022-INT-1" . *iso-2022-int-1*)
2020                         )))))
2021     ;; In bilingual environment it may be unnecessary to convert the
2022     ;; coding system of the string unless transfer encoding is
2023     ;; required since such conversion may be performed by mule
2024     ;; automatically.
2025     (if (not (null coding-system))
2026         (setq string (code-convert-string string *internal* coding-system)))
2027     (if (stringp encoding)
2028         (setq string (mime-encode-string encoding string)))
2029     string
2030     ))
2031
2032 \f
2033 ;; Sun implementations
2034
2035 (defun mime-voice-recorder-for-sun ()
2036   "Record voice in a buffer using Sun audio device, and return the buffer.
2037 If the environment variable AUDIOHOST is defined, its value is used as
2038 a recording host instead of local host."
2039   (let ((buffer (get-buffer-create " *MIME audio*"))
2040         (host (getenv "AUDIOHOST")))
2041     (message "Start the recording on %s.  Type C-g to finish the recording..."
2042              (or host (system-name)))
2043     (save-excursion
2044       (set-buffer buffer)
2045       (erase-buffer)
2046       (condition-case errorcode
2047           (let ((selective-display nil) ;Disable ^M to nl translation.
2048                 (mc-flag nil)           ;Mule
2049                 (kanji-flag nil))       ;NEmacs
2050             ;; If AUDIOHOST is defined, use the value as recording host.
2051             (cond ((not (null host))
2052                    ;; Disable automatic conversion of coding system if Mule.
2053                    (if (featurep 'mule)
2054                        (define-program-coding-system nil "rsh" *noconv*))
2055                    (call-process "rsh"
2056                                  nil
2057                                  buffer
2058                                  nil
2059                                  host
2060                                  "cat"
2061                                  "/dev/audio"
2062                                  ))
2063                   (t
2064                    ;; Disable automatic conversion of coding system if Mule.
2065                    (if (featurep 'mule)
2066                        (define-program-coding-system nil "cat" *noconv*))
2067                    (call-process "cat"
2068                                  "/dev/audio"
2069                                  buffer
2070                                  nil
2071                                  ))))
2072         (quit (message "Type C-g to finish recording... done.")
2073               buffer                    ;Return the buffer
2074               )))))
2075
2076 \f
2077 ;;; @ Other useful commands.
2078 ;;;
2079
2080 ;; Message forwarding commands as content-type "message/rfc822".
2081
2082 (defun mime-editor/insert-message (&optional message)
2083   (interactive)
2084   (let ((inserter (assoc-value major-mode mime-editor/message-inserter-alist)))
2085     (if (and inserter (fboundp inserter))
2086         (progn
2087           (mime-editor/insert-tag "message" "rfc822")
2088           (funcall inserter message)
2089           )
2090       (message "Sorry, I don't have message inserter for your MUA.")
2091       )))
2092
2093 (defun mime-editor/insert-mail (&optional message)
2094   (interactive)
2095   (let ((inserter (assoc-value major-mode mime-editor/mail-inserter-alist)))
2096     (if (and inserter (fboundp inserter))
2097         (progn
2098           (mime-editor/insert-tag "message" "rfc822")
2099           (funcall inserter message)
2100           )
2101       (message "Sorry, I don't have mail inserter for your MUA.")
2102       )))
2103
2104 (defun mime-editor/inserted-message-filter ()
2105   (save-excursion
2106     (save-restriction
2107       (let ((header-start (point))
2108             (case-fold-search t)
2109             beg end)
2110         ;; for Emacs 18
2111         ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2112         (if (re-search-forward "^$" (mark t))
2113             (narrow-to-region header-start (match-beginning 0))
2114           )
2115         (goto-char header-start)
2116         (while (and (re-search-forward
2117                      mime-editor/yank-ignored-field-regexp nil t)
2118                     (setq beg (match-beginning 0))
2119                     (setq end (1+ (rfc822/field-end)))
2120                     )
2121           (delete-region beg end)
2122           )
2123         ))))
2124
2125
2126 ;;; @ multipart enclosure
2127 ;;;
2128
2129 (defun mime-editor/enclose-region (type beg end)
2130   (save-excursion
2131     (goto-char beg)
2132     (let ((current (point))
2133           exist-prev-tag)
2134       (save-excursion
2135         (if (mime-editor/goto-tag)
2136             (or (eq current (match-beginning 0))
2137                 (setq exist-prev-tag t)
2138                 )))
2139       (save-restriction
2140         (narrow-to-region beg end)
2141         (goto-char beg)
2142         (if exist-prev-tag
2143             (insert "\n")
2144           )
2145         (insert (format "--<<%s>>-{\n" type))
2146         (goto-char (point-max))
2147         (insert (format "\n--}-<<%s>>\n" type))
2148         (goto-char (point-max))
2149         )
2150       (if (and (not (looking-at mime-editor/single-part-tag-regexp))
2151                (not (eobp)))
2152           (insert (mime-make-text-tag) "\n")
2153         )
2154       )))
2155
2156 (defun mime-editor/enclose-mixed-region (beg end)
2157   (interactive "*r")
2158   (mime-editor/enclose-region "mixed" beg end)
2159   )
2160
2161 (defun mime-editor/enclose-parallel-region (beg end)
2162   (interactive "*r")
2163   (mime-editor/enclose-region "parallel" beg end)
2164   )
2165
2166 (defun mime-editor/enclose-digest-region (beg end)
2167   (interactive "*r")
2168   (mime-editor/enclose-region "digest" beg end)
2169   )
2170
2171 (defun mime-editor/enclose-alternative-region (beg end)
2172   (interactive "*r")
2173   (mime-editor/enclose-region "alternative" beg end)
2174   )
2175
2176 (defun mime-editor/enclose-signed-region (beg end)
2177   (interactive "*r")
2178   (if mime-editor/signing-type
2179       (mime-editor/enclose-region "signed" beg end)
2180     (message "Please specify signing type.")
2181     ))
2182
2183 (defun mime-editor/enclose-encrypted-region (beg end)
2184   (interactive "*r")
2185   (if mime-editor/signing-type
2186       (mime-editor/enclose-region "encrypted" beg end)
2187     (message "Please specify encrypting type.")
2188     ))
2189
2190 (defun mime-editor/insert-key (&optional arg)
2191   "Insert a pgp public key."
2192   (interactive "P")
2193   (mime-editor/insert-tag "application" "pgp-keys")
2194   (mime-editor/define-encoding "7bit")
2195   (mc-insert-public-key)
2196   )
2197
2198
2199 ;;; @ flag setting
2200 ;;;
2201
2202 (defun mime-editor/set-split (arg)
2203   (interactive
2204    (list
2205     (y-or-n-p "Do you want to enable split?")
2206     ))
2207   (setq mime-editor/split-message arg)
2208   (if arg
2209       (message "This message is enabled to split.")
2210     (message "This message is not enabled to split.")
2211     ))
2212
2213
2214 ;;; @ pgp
2215 ;;;
2216
2217 (defun mime-editor/set-sign (arg)
2218   (interactive
2219    (list
2220     (y-or-n-p "Do you want to sign?")
2221     ))
2222   (if arg
2223       (progn
2224         (setq mime-editor/pgp-processing 'sign)
2225         (message "This message will be signed.")
2226         )
2227     (if (eq mime-editor/pgp-processing 'sign)
2228         (setq mime-editor/pgp-processing nil)
2229       )
2230     (message "This message will not be signed.")
2231     ))
2232
2233 (defun mime-editor/set-encrypt (arg)
2234   (interactive
2235    (list
2236     (y-or-n-p "Do you want to encrypt?")
2237     ))
2238   (if arg
2239       (progn
2240         (setq mime-editor/pgp-processing 'encrypt)
2241         (message "This message will be encrypt.")
2242         )
2243     (if (eq mime-editor/pgp-processing 'encrypt)
2244         (setq mime-editor/pgp-processing nil)
2245       )
2246     (message "This message will not be encrypt.")
2247     ))
2248
2249 (defvar mime-editor/pgp-processing nil)
2250 (make-variable-buffer-local 'mime-editor/pgp-processing)
2251
2252 (defun mime-editor/call-mc (command)
2253   (let* ((header (rfc822/get-header-string-except
2254                   "^Content-Type:" mail-header-separator)))
2255     (goto-char (point-min))
2256     (if (search-forward mail-header-separator)
2257         (replace-match "")
2258       )
2259     (goto-char (point-min))
2260     (insert header)
2261     (insert "Content-Type: application/pgp; format=mime\n")
2262     (insert mail-header-separator)
2263     (insert "\n")
2264     (if (null (call-interactively command))
2265         (throw 'mime-editor/error 'pgp-error)
2266       )
2267     ))
2268
2269 (defun mime-editor/pgp-processing ()
2270   (let ((command
2271          (cdr (assq mime-editor/pgp-processing
2272                     '((sign    . mc-sign)
2273                       (encrypt . mc-encrypt)
2274                       )))))
2275     (and command
2276          (mime-editor/call-mc command)
2277          )))
2278
2279
2280 ;;; @ split
2281 ;;;
2282
2283 (defun mime-editor/insert-partial-header
2284   (fields subject id number total separator)
2285   (insert fields)
2286   (insert (format "Subject: %s (%d/%d)\n" subject number total))
2287   (insert (format "Mime-Version: 1.0 (split by tm-edit %s)\n"
2288                   mime-editor/version))
2289   (insert (format "\
2290 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2291                   id number total separator))
2292   )
2293
2294 (defun mime-editor/split-and-send
2295   (&optional cmd lines mime-editor/message-max-length)
2296   (interactive)
2297   (or lines
2298       (setq lines
2299             (count-lines (point-min) (point-max)))
2300       )
2301   (or mime-editor/message-max-length
2302       (setq mime-editor/message-max-length
2303             (or (cdr (assq major-mode mime-editor/message-max-length-alist))
2304                 mime-editor/message-default-max-length))
2305       )
2306   (let* ((mime-editor/draft-file-name 
2307           (or (buffer-file-name)
2308               (make-temp-name
2309                (expand-file-name "tm-draft" mime/tmp-dir))))
2310          (separator mail-header-separator)
2311          (config
2312           (eval (cdr (assq major-mode mime-editor/window-config-alist))))
2313          (id (concat "\""
2314                      (replace-space-with-underline (current-time-string))
2315                      "@" (system-name) "\"")))
2316     (run-hooks 'mime-editor/before-split-hook)
2317     (let* ((header (rfc822/get-header-string-except
2318                     mime-editor/split-ignored-field-regexp separator))
2319            (subject (mail-fetch-field "subject"))
2320            (total (+ (/ lines mime-editor/message-max-length)
2321                      (if (> (mod lines mime-editor/message-max-length) 0)
2322                          1)))
2323            (the-buf (current-buffer))
2324            (buf (get-buffer "*tmp-send*"))
2325            (command
2326             (or cmd
2327                 (cdr
2328                  (assq major-mode
2329                        mime-editor/split-message-sender-alist))
2330                 (cdr
2331                  (assq major-mode
2332                        mime-editor/message-default-sender-alist))
2333                 ))
2334            data)
2335       (goto-char (point-min))
2336       (if (re-search-forward (concat "^" (regexp-quote separator) "$")
2337                              nil t)
2338           (replace-match "")
2339         )
2340       (if buf
2341           (progn
2342             (switch-to-buffer buf)
2343             (erase-buffer)
2344             (switch-to-buffer the-buf)
2345             )
2346         (setq buf (get-buffer-create "*tmp-send*"))
2347         )
2348       (switch-to-buffer buf)
2349       (make-local-variable 'mail-header-separator)
2350       (setq mail-header-separator separator)
2351       (switch-to-buffer the-buf)
2352       (goto-char (point-min))
2353       (re-search-forward "^$" nil t)
2354       (let ((mime-editor/partial-number 1))
2355         (setq data (buffer-substring
2356                     (point-min)
2357                     (progn
2358                       (goto-line mime-editor/message-max-length)
2359                       (point))
2360                     ))
2361         (delete-region (point-min)(point))
2362         (switch-to-buffer buf)
2363         (mime-editor/insert-partial-header
2364          header subject id mime-editor/partial-number total separator)
2365         (insert data)
2366         (save-excursion
2367           (save-restriction
2368             (goto-char (point-min))
2369             (search-forward (concat "\n" mail-header-separator "\n"))
2370             (narrow-to-region
2371              (match-end 0)
2372              (if (re-search-forward "^$" nil t)
2373                  (match-beginning 0)
2374                (point-max)
2375                ))
2376             (goto-char (point-min))
2377             (while (re-search-forward
2378                     mime-editor/split-blind-field-regexp nil t)
2379               (delete-region (match-beginning 0)
2380                              (let ((e (rfc822/field-end)))
2381                                (if (< e (point-max))
2382                                    (1+ e)
2383                                  e)))
2384               )
2385             ))
2386         (save-excursion
2387           (message (format "Sending %d/%d..."
2388                            mime-editor/partial-number total))
2389           (call-interactively command)
2390           (message (format "Sending %d/%d... done"
2391                            mime-editor/partial-number total))
2392           )
2393         (erase-buffer)
2394         (switch-to-buffer the-buf)
2395         (setq mime-editor/partial-number 2)
2396         (while (< mime-editor/partial-number total)
2397           (setq data (buffer-substring
2398                       (point-min)
2399                       (progn
2400                         (goto-line mime-editor/message-max-length)
2401                         (point))
2402                       ))
2403           (delete-region (point-min)(point))
2404           (switch-to-buffer buf)
2405           (mime-editor/insert-partial-header
2406            header subject id mime-editor/partial-number total separator)
2407           (insert data)
2408           (save-excursion
2409             (message (format "Sending %d/%d..."
2410                              mime-editor/partial-number total))
2411             (call-interactively command)
2412             (message (format "Sending %d/%d... done"
2413                              mime-editor/partial-number total))
2414             )
2415           (erase-buffer)
2416           (switch-to-buffer the-buf)
2417           (setq mime-editor/partial-number
2418                 (1+ mime-editor/partial-number))
2419           )
2420         (goto-char (point-min))
2421         (mime-editor/insert-partial-header
2422          header subject id mime-editor/partial-number total separator)
2423         (message (format "Sending %d/%d..."
2424                          mime-editor/partial-number total))
2425         ))))
2426
2427 (defun mime-editor/maybe-split-and-send (&optional cmd)
2428   (interactive)
2429   (run-hooks 'mime-editor/before-send-hook)
2430   (let ((mime-editor/message-max-length
2431          (or (cdr (assq major-mode mime-editor/message-max-length-alist))
2432              mime-editor/message-default-max-length))
2433         (lines (count-lines (point-min) (point-max)))
2434         )
2435     (if (and (> lines mime-editor/message-max-length)
2436              mime-editor/split-message)
2437         (mime-editor/split-and-send cmd lines mime-editor/message-max-length)
2438       )))
2439
2440
2441 ;;; @ preview message
2442 ;;;
2443
2444 (defun mime-editor/preview-message ()
2445   "preview editing MIME message. [tm-edit.el]"
2446   (interactive)
2447   (let* ((str (buffer-string))
2448          (separator mail-header-separator)
2449          (the-buf (current-buffer))
2450          (buf-name (buffer-name))
2451          (temp-buf-name (concat "*temp-article:" buf-name "*"))
2452          (buf (get-buffer temp-buf-name))
2453          )
2454     (if buf
2455         (progn
2456           (switch-to-buffer buf)
2457           (erase-buffer)
2458           )
2459       (setq buf (get-buffer-create temp-buf-name))
2460       (switch-to-buffer buf)
2461       )
2462     (insert str)
2463     (setq major-mode 'mime/temporary-message-mode)
2464     (make-local-variable 'mail-header-separator)
2465     (setq mail-header-separator separator)
2466     (make-local-variable 'mime/editing-buffer)
2467     (setq mime/editing-buffer the-buf)
2468     
2469     (run-hooks 'mime-editor/translate-hook)
2470     (mime-editor/translate-buffer)
2471     (goto-char (point-min))
2472     (if (re-search-forward
2473          (concat "^" (regexp-quote separator) "$"))
2474         (replace-match "")
2475       )
2476     (mime/viewer-mode)
2477     ))
2478
2479 (defun mime-editor/quitting-method ()
2480   (let ((temp mime::preview/article-buffer)
2481         buf)
2482     (mime-viewer/kill-buffer)
2483     (set-buffer temp)
2484     (setq buf mime/editing-buffer)
2485     (kill-buffer temp)
2486     (switch-to-buffer buf)
2487     ))
2488
2489 (set-alist 'mime-viewer/quitting-method-alist
2490            'mime/temporary-message-mode
2491            (function mime-editor/quitting-method)
2492            )
2493
2494
2495 ;;; @ draft preview
2496 ;;; 
2497 ;; by "OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
2498 ;;       Mon, 10 Apr 1995 20:03:07 +0900
2499
2500 (defvar mime-editor/draft-header-separator-alist
2501   '((news-reply-mode . mail-header-separator)
2502     (mh-letter-mode . mail-header-separator)
2503     ))
2504
2505 (defvar mime::article/draft-header-separator nil)
2506
2507 (defun mime-editor/draft-preview ()
2508   (interactive)
2509   (let ((sep (cdr (assq major-mode mime-editor/draft-header-separator-alist))))
2510     (or (stringp sep) (setq sep (eval sep)))
2511     (make-variable-buffer-local 'mime::article/draft-header-separator)
2512     (goto-char (point-min))
2513     (re-search-forward
2514      (concat "^\\(" (regexp-quote sep) "\\)?$"))
2515     (setq mime::article/draft-header-separator
2516           (buffer-substring (match-beginning 0) (match-end 0)))
2517     (replace-match "")
2518     (mime/viewer-mode (current-buffer))
2519     (pop-to-buffer (current-buffer))
2520     ))
2521
2522 (defun mime-viewer::quitting-method/draft-preview ()
2523   (let ((mother mime::preview/mother-buffer))
2524     (save-excursion
2525       (switch-to-buffer mother)
2526       (goto-char (point-min))
2527       (if (and
2528            (re-search-forward
2529             (concat "^\\("
2530                     (regexp-quote mime::article/draft-header-separator)
2531                     "\\)?$") nil t)
2532            (bolp))
2533           (progn
2534             (insert mime::article/draft-header-separator)
2535             (set-buffer-modified-p (buffer-modified-p))
2536             )))
2537     (mime-viewer/kill-buffer)
2538     (pop-to-buffer mother)
2539     ))
2540
2541 (set-alist 'mime-viewer/quitting-method-alist
2542            'mh-letter-mode
2543            (function mime-viewer::quitting-method/draft-preview)
2544            )
2545
2546 (set-alist 'mime-viewer/quitting-method-alist
2547            'news-reply-mode
2548            (function mime-viewer::quitting-method/draft-preview)
2549            )
2550
2551
2552 ;;; @ etc
2553 ;;;
2554
2555 (defun replace-space-with-underline (str)
2556   (mapconcat (function
2557               (lambda (arg)
2558                 (char-to-string
2559                  (if (= arg 32)
2560                      ?_
2561                    arg)))) str "")
2562   )
2563
2564
2565 ;;; @ end
2566 ;;;
2567
2568 (provide 'tm-edit)
2569
2570 (run-hooks 'tm-edit-load-hook)
2571
2572 ;;; tm-edit.el ends here