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