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