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