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