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