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