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