merge from semi-1_14 branch:
[elisp/semi.git] / mime-edit.el
index 2553c4f..b7fb75e 100644 (file)
 (require 'sendmail)
 (require 'mail-utils)
 (require 'mel)
-(require 'eword-encode) ; eword-encode-field-body
 (require 'mime-view)
 (require 'signature)
 (require 'alist)
-(require 'invisible)
 (require 'pgg-def)
 (require 'pgg-parse)
 
   "PGP signature of current region." t)
 (autoload 'pgg-insert-key "pgg"
   "Insert PGP public key at point." t)
-(autoload 'smime-encrypt-region "smime"
-  "S/MIME encryption of current region.")
-(autoload 'smime-sign-region "smime"
-  "S/MIME signature of current region.")
-(defvar smime-output-buffer)
-(defvar smime-errors-buffer)
+(autoload 'smime-encrypt-buffer "smime"
+  "S/MIME encryption of current buffer.")
+(autoload 'smime-sign-buffer "smime"
+  "S/MIME signature of current buffer.")
 
 
 ;;; @ version
@@ -234,6 +230,8 @@ To insert a signature file automatically, call the function
     ("application"
      ("octet-stream" ("type" "" "tar" "shar"))
      ("postscript")
+     ("msword")
+     ("vnd.ms-excel")
      ("vnd.ms-powerpoint")
      ("x-kiss" ("x-cnf")))
     ("image"
@@ -306,7 +304,11 @@ To insert a signature file automatically, call the function
     ;;  Octect binary text
 
     ("\\.doc$"                         ;MS Word
-     "application" "winword" nil
+     "application" "msword" nil
+     "base64"
+     "attachment" (("filename" . file)))
+    ("\\.xls$"                         ; MS Excel
+     "application" "vnd.ms-excel" nil
      "base64"
      "attachment" (("filename" . file)))
     ("\\.ppt$"                         ; MS Power Point
@@ -325,7 +327,7 @@ To insert a signature file automatically, call the function
 
     ;;  Pure binary
 
-    ("\\.jpg$"
+    ("\\.jpg$\\|\\.jpeg$"
      "image"   "jpeg"          nil
      "base64"
      "inline"          (("filename" . file)))
@@ -460,6 +462,8 @@ If encoding is nil, it is determined from its contents."
     (iso-8859-7                8 "quoted-printable")
     (iso-8859-8                8 "quoted-printable")
     (iso-8859-9                8 "quoted-printable")
+    (iso-8859-14       8 "quoted-printable")
+    (iso-8859-15       8 "quoted-printable")
     (iso-2022-jp       7 "base64")
     (iso-2022-jp-3     7 "base64")
     (iso-2022-kr       7 "base64")
@@ -611,7 +615,10 @@ If it is not specified for a major-mode,
          (if (fboundp 'apel-version)
              (concat (apel-version) " "))
          (if (featurep 'xemacs)
-             (concat (cond ((featurep 'utf-2000)
+             (concat (cond ((and (featurep 'chise)
+                                 (boundp 'xemacs-chise-version))
+                            (concat "CHISE-MULE/" xemacs-chise-version))
+                           ((featurep 'utf-2000)
                             (concat "UTF-2000-MULE/" utf-2000-version))
                            ((featurep 'mule) "MULE"))
                      " XEmacs"
@@ -630,7 +637,14 @@ If it is not specified for a major-mode,
                                  ;; XEmacs versions earlier than 21.1.1.
                                  (format " (patch %d)" emacs-patch-level))
                                 (t ""))
-                          " (" xemacs-codename ") ("
+                          " (" xemacs-codename ")"
+                          ;; `xemacs-extra-name' has appeared in the
+                          ;; development version of XEmacs 21.5-b8.
+                          (if (and (boundp 'xemacs-extra-name)
+                                   (symbol-value 'xemacs-extra-name))
+                              (concat " " (symbol-value 'xemacs-extra-name))
+                            "")
+                          " ("
                           system-configuration ")")
                        " (" emacs-version ")"))
            (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
@@ -670,7 +684,7 @@ Tspecials means any character that matches with it in header must be quoted.")
 
 (defconst mime-edit-mime-version-field-for-message/partial
   (concat "MIME-Version:"
-         (eword-encode-field-body
+         (mime-encode-field-body
           (concat " 1.0 (split by " mime-edit-version ")\n")
           "MIME-Version:"))
   "MIME version field for message/partial.")
@@ -757,9 +771,9 @@ Tspecials means any character that matches with it in header must be quoted.")
     (encrypted "Enclose as encrypted"  mime-edit-enclose-pgp-encrypted-region)
     (quote     "Verbatim region"       mime-edit-enclose-quote-region)
     (key       "Insert Public Key"     mime-edit-insert-key)
-    (split     "About split"           mime-edit-set-split)
-    (sign      "About sign"            mime-edit-set-sign)
-    (encrypt   "About encryption"      mime-edit-set-encrypt)
+    (split     "Set splitting"         mime-edit-set-split)
+    (sign      "PGP sign"              mime-edit-set-sign)
+    (encrypt   "PGP encrypt"           mime-edit-set-encrypt)
     (preview   "Preview Message"       mime-edit-preview-message)
     (level     "Toggle transfer-level" mime-edit-toggle-transfer-level))
   "MIME-edit menubar entry.")
@@ -986,8 +1000,6 @@ User customizable variables (not documented all of them):
     (if (featurep 'xemacs)
        (mime-edit-define-menu-for-xemacs))
 
-    (enable-invisible)
-
     ;; I don't care about saving these.
     (setq paragraph-start
          (regexp-or mime-edit-single-part-tag-regexp
@@ -997,6 +1009,7 @@ User customizable variables (not documented all of them):
                     paragraph-separate))
     (run-hooks 'mime-edit-mode-hook)
     (message
+     "%s"
      (substitute-command-keys
       "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
 
@@ -1021,7 +1034,6 @@ just return to previous mode."
     (if (and (featurep 'xemacs)
             (featurep 'menubar))
        (delete-menu-item (list mime-edit-menu-title)))
-    (end-of-invisible)
     (set-buffer-modified-p (buffer-modified-p))
     (run-hooks 'mime-edit-exit-hook)
     (message "Exit MIME editor mode.")))
@@ -1131,12 +1143,13 @@ If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
     (mime-edit-insert-tag "audio" "basic" nil)
     (mime-edit-define-encoding encoding)
     (save-restriction
-      (narrow-to-region (1- (point))(point))
+      (narrow-to-region (point)(point))
       (unwind-protect
          (funcall mime-edit-voice-recorder encoding)
        (progn
          (insert "\n")
-         (invisible-region (point-min)(point-max))
+         (add-text-properties
+          (point-min)(point-max) '(invisible t mime-edit-invisible t))
          (goto-char (point-max)))))))
 
 (defun mime-edit-insert-signature (&optional arg)
@@ -1217,13 +1230,12 @@ Optional argument ENCODING specifies an encoding method such as base64."
                             (string-equal en "8bit")
                             (string-equal en "binary")))))))
     (save-restriction
-      (narrow-to-region tagend (point))
+      (narrow-to-region (point)(point))
       (mime-insert-encoded-file file encoding)
       (if hide-p
-         (progn
-           (invisible-region (point-min) (point-max))
-           (goto-char (point-max)))
-       (goto-char (point-max))))
+         (add-text-properties
+          (point-min)(point-max) '(invisible t mime-edit-invisible t)))
+      (goto-char (point-max)))
     (or hide-p
        (looking-at mime-edit-tag-regexp)
        (= (point)(point-max))
@@ -1282,9 +1294,10 @@ Optional argument ENCODING specifies an encoding method such as base64."
   (save-excursion
     (if (mime-edit-goto-tag)
        (progn
-         (goto-char (match-end 0))
-         (if (invisible-p (point))
-             (next-visible-point (point))
+         (goto-char (1+ (match-end 0)))
+         (if (get-text-property (point) 'mime-edit-invisible)
+             (or (next-single-property-change (point) 'mime-edit-invisible)
+                 (point-max))
            ;; Move to the end of this text.
            (if (re-search-forward mime-edit-tag-regexp nil 'move)
                ;; Don't forget a multiline tag.
@@ -1395,7 +1408,7 @@ Nil if no such parameter."
        ;; Change value
        (concat (substring ctype 0 (match-beginning 1))
                parameter "=" value
-               (substring contype (match-end 1))
+               (substring ctype (match-end 1))
                opt-fields)
       (concat ctype "; " parameter "=" value opt-fields)
       )))
@@ -1486,7 +1499,7 @@ Optional DELIMITER specifies parameter delimiter (';' by default)."
 
 (defun mime-prompt-for-parameter (parameter)
   "Ask for PARAMETER.
-Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
+Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
   (let* ((prompt (car parameter))
         (choices (mapcar (function
                           (lambda (e)
@@ -1538,7 +1551,7 @@ Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
 
 (defun mime-edit-translate-header ()
   "Encode the message header into network representation."
-  (eword-encode-header 'code-conversion)
+  (mime-encode-header-in-buffer 'code-conversion)
   (run-hooks 'mime-edit-translate-header-hook))
 
 (defun mime-edit-translate-buffer ()
@@ -1735,7 +1748,7 @@ Content-Transfer-Encoding: 7bit
           (if encoding
               (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
           (insert "\n")
-         (eword-encode-header)
+         (mime-encode-header-in-buffer)
          (or (let ((pgg-default-user-id 
                     (or mime-edit-pgp-user-id
                         (if from 
@@ -1830,27 +1843,15 @@ Content-Transfer-Encoding: 7bit
          (while (progn (end-of-line) (not (eobp)))
            (insert "\r")
            (forward-line 1))
-         (or (prog1 (smime-sign-region (point-min)(point-max))
-               (push nil buffer-undo-list)
-               (ignore-errors (undo)))
+         (or (smime-sign-buffer)
              (throw 'mime-edit-error 'pgp-error)))
        (goto-char beg)
-       (insert (format "--[[multipart/signed;
- boundary=\"%s\"; micalg=sha1;
- protocol=\"application/pkcs7-signature\"][7bit]]
---%s
-" smime-boundary smime-boundary))
-       (goto-char (point-max))
-       (insert (format "\n--%s
-Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
-Content-Transfer-Encoding: base64
-Content-Disposition: attachment; filename=\"smime.p7s\"
-Content-Description: S/MIME Cryptographic Signature
-
-"  smime-boundary))
-       (insert-buffer-substring smime-output-buffer)
-       (goto-char (point-max))
-       (insert (format "\n--%s--\n" smime-boundary))))))
+       (if (re-search-forward "^Content-Type:\\s-*" nil t)
+           (let* ((start (match-beginning 0))
+                  (body (buffer-substring (match-end 0) (std11-field-end))))
+             (delete-region start (line-beginning-position 2))
+             (goto-char beg)
+             (insert "--[[" body "][7bit]]\n")))))))
 
 (defun mime-edit-encrypt-smime (beg end boundary)
   (save-excursion
@@ -1869,13 +1870,15 @@ Content-Description: S/MIME Cryptographic Signature
        (while (progn (end-of-line) (not (eobp)))
          (insert "\r")
          (forward-line 1))
-       (or (smime-encrypt-region (point-min)(point-max))
+       (or (smime-encrypt-buffer)
            (throw 'mime-edit-error 'pgp-error))
-       (delete-region (point-min)(point-max))
-       (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
-Content-Disposition: attachment; filename=\"smime.p7m\"
-Content-Description: S/MIME Encrypted Message][base64]]\n")
-       (insert-buffer-substring smime-output-buffer)))))
+       (goto-char beg)
+       (if (re-search-forward "^Content-Type:\\s-*" nil t)
+           (let* ((start (match-beginning 0))
+                  (body (buffer-substring (match-end 0) (std11-field-end))))
+             (delete-region start (line-beginning-position 2))
+             (goto-char beg)
+             (insert "--[[" body "]]\n")))))))
 
 (defsubst replace-space-with-underline (str)
   (mapconcat (function
@@ -1961,7 +1964,7 @@ Content-Description: S/MIME Encrypted Message][base64]]\n")
            (insert "Content-Type: " contype "\n")
            (if encoding
                (insert "Content-Transfer-Encoding: " encoding "\n"))
-           (eword-encode-header))
+           (mime-encode-header-in-buffer))
          (cons (and contype
                     (downcase contype))
                (and encoding
@@ -2042,7 +2045,7 @@ Content-Description: S/MIME Encrypted Message][base64]]\n")
          (or (looking-at mime-edit-beginning-tag-regexp)
              (eobp)
              (insert (mime-make-text-tag) "\n")))
-       (visible-region beg end)
+       (remove-text-properties beg end '(invisible mime-edit-invisible))
        (goto-char beg))
       (cond
        ((mime-test-content-type contype "message")
@@ -2114,7 +2117,13 @@ Content-Description: S/MIME Encrypted Message][base64]]\n")
                    (narrow-to-region beg (mime-edit-content-end))
                    (goto-char beg)
                    (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
-                     (replace-match "\\1\r\n"))))
+                     ;; In a certain period, `replace-match' with "\\N"
+                     ;; converted 8-bit characters into multibyte string,
+                     ;; but it has been fixed at 2004-01-15.
+                     ;;(replace-match "\\1\r\n"))))
+                     (backward-char 1)
+                     (insert "\r")
+                     (forward-char 1))))
              (goto-char beg)
              (mime-encode-region beg (mime-edit-content-end)
                                  (or encoding "7bit"))
@@ -2436,7 +2445,7 @@ Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
          (message (format "Sending %d/%d..."
                           mime-edit-partial-number total))
          (call-interactively command)
-         (message (format "Sending %d/%d... done"
+         (message (format "Sending %d/%d...done"
                           mime-edit-partial-number total)))
        (setq mime-edit-partial-number
              (1+ mime-edit-partial-number)))
@@ -2451,7 +2460,7 @@ Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
       (save-excursion
        (message (format "Sending %d/%d..."
                         mime-edit-partial-number total))
-       (message (format "Sending %d/%d... done"
+       (message (format "Sending %d/%d...done"
                         mime-edit-partial-number total))))))
 
 (defun mime-edit-maybe-split-and-send (&optional cmd)
@@ -2679,14 +2688,15 @@ Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
                         (match-end 0)(point-max) encoding)
                        (setq encoded t
                              encoding nil))))))))
-    (if (or encoded (not not-decode-text))
-       (progn
-         (save-excursion
-           (goto-char (point-min))
-           (while (re-search-forward "\r\n" nil t)
-             (replace-match "\n")))
-         (decode-mime-charset-region (point-min)(point-max)
-                                     (or charset default-mime-charset))))
+    (if (and (eq type 'text)
+            (or encoded (not not-decode-text)))
+       (progn
+         (save-excursion
+           (goto-char (point-min))
+           (while (re-search-forward "\r\n" nil t)
+             (replace-match "\n")))
+         (decode-mime-charset-region (point-min)(point-max)
+                                     (or charset default-mime-charset))))
     (let ((he (if (re-search-forward "^$" nil t)
                  (match-end 0)
                (point-min))))