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