;; * 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)
(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.
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)))))