* wl/wl-summary.el (wl-summary-mode): Check with fboundp before calling `make-local...
[elisp/wanderlust.git] / elmo / utf7.el
index ae050f9..dd38408 100644 (file)
@@ -67,6 +67,9 @@
 ;; * Don't use mm-with-unibyte-current-buffer etc.
 ;; * Do nothing if utf-16 coding system is not found.
 
+;; Modified 31 Aug 2004 by Yuuichi Teranishi so that it can avoid the bug of
+;; Emacs 21.3 release version.
+
 ;;; Code:
 
 (eval-when-compile (require 'cl))
   (concat utf7-direct-encoding-chars "+\\~")
   "Character ranges which do not need escaping in the IMAP variant of UTF-7.")
 
-(defconst utf7-utf-16-coding-system (and (fboundp 'find-coding-system)
-                                        (find-coding-system 'utf-16-be))
+
+(eval-and-compile
+  (defun utf7-find-coding-system-without-bom (cs)
+    (and (fboundp 'find-coding-system)
+        (find-coding-system cs)
+        ;; Avoid versions with BOM.
+        (= 2 (length (encode-coding-string "a" cs)))
+        cs)))
+
+(defconst utf7-utf-16-coding-system
+  (or
+   ;; Emacs 22, Emacs 23
+   (utf7-find-coding-system-without-bom 'utf-16be)
+   ;;
+   (utf7-find-coding-system-without-bom 'utf-16-be)
+   ;; Mule-UCS
+   (utf7-find-coding-system-without-bom 'utf-16-be-no-signature))
   "Coding system which encodes big endian UTF-16.")
 
 (defsubst utf7-imap-get-pad-length (len modulus)
@@ -118,28 +136,33 @@ Use IMAP modification if FOR-IMAP is non-nil."
                 (skip-chars-forward not-direct-encoding-chars)))
            (if (and (= fc esc-char)
                     (= run-length 1))  ; Lone esc-char?
-               (delete-backward-char 1) ; Now there's one too many
+               (delete-char -1)        ; Now there's one too many
              (utf7-fragment-encode p (point) for-imap))
            (insert "-")))))))
 
 (defun utf7-fragment-encode (start end &optional for-imap)
   "Encode text from START to END in buffer as UTF-7 escape fragment.
 Use IMAP modification if FOR-IMAP is non-nil."
-  (save-restriction
-    (narrow-to-region start end)
-    (let ((converter (utf7-get-u16char-converter 'to-utf-16))
-         pm)
-      (when converter
-       (funcall converter)
-       (set-buffer-multibyte nil)
-       (utf7-base64-encode-region start (point-max))
-       (goto-char start)
-       (setq pm (point-max))
-       (when for-imap
-         (while (search-forward "/" nil t)
-           (replace-match ",")))
-       (skip-chars-forward "^= \t\n" pm)
-       (delete-region (point) pm)))))
+  (let ((converter (utf7-get-u16char-converter 'to-utf-16))
+       (str (buffer-substring start end))
+       pm)
+    (when converter
+      (delete-region start end)
+      (goto-char start)
+      (insert
+       (with-temp-buffer
+        (insert str)
+        (funcall converter)
+        (set-buffer-multibyte nil)
+        (utf7-base64-encode-region (point-min) (point-max))
+        (goto-char (point-min))
+        (setq pm (point-max))
+        (when for-imap
+          (while (search-forward "/" nil t)
+            (replace-match ",")))
+        (skip-chars-forward "^= \t\n" pm)
+        (delete-region (point) pm)
+        (buffer-string))))))
 
 (defun utf7-decode-internal (&optional for-imap)
   "Decode UTF-7 text in (temporary) buffer.
@@ -162,7 +185,7 @@ Use IMAP modification if FOR-IMAP is non-nil."
              (save-excursion
                (utf7-fragment-decode p (point) for-imap)
                (goto-char p)
-               (delete-backward-char 1)))))))))
+               (delete-char -1)))))))))
 
 (defun utf7-fragment-decode (start end &optional for-imap)
   "Decode base64 encoded fragment from START to END of UTF-7 text in buffer.
@@ -188,13 +211,11 @@ Use IMAP modification if FOR-IMAP is non-nil."
                                  utf7-utf-16-coding-system)
            (set-buffer-multibyte nil)
            (goto-char (point-min))
-           ;; Remove BOM (Big-endian UTF-16 FE FF)
+           ;; Remove BOM (Big-endian UTF-16 FE FF) for Mule-UCS
            (while (re-search-forward "\376\377" nil t)
-             (delete-region (match-beginning 0)(match-end 0))))
+             (delete-region (match-beginning 0) (match-end 0))))
        (lambda ()
          (goto-char (point-min))
-         ;; Add BOM (Big-endian UTF-16 FE FF)
-         (insert "\376\377")
          (decode-coding-region (point-min) (point-max)
                                utf7-utf-16-coding-system)))))