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