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