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