tm 7.79.
[elisp/tm.git] / tm-ew-d.el
index 6797b9c..271180b 100644 (file)
@@ -1,15 +1,32 @@
-;;;
-;;; tm-ew-d.el --- RFC 1522 based multilingual MIME message header
-;;;                decoder for GNU Emacs
-;;;
-;;; Copyright (C) 1995 Free Software Foundation, Inc.
-;;; Copyright (C) 1992 ENAMI Tsugutomo
-;;; Copyright (C) 1993,1994,1995 MORIOKA Tomohiko
-;;;
-;;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
-;;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
-;;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
-;;;
+;;; tm-ew-d.el --- RFC 1522 based MIME encoded-word decoder for GNU Emacs
+
+;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
+
+;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
+;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
+;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
+;; Created: 1993/6/3 (1995/10/3 obsolete tiny-mime.el)
+;; Version: $Revision: 7.21 $
+;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
+
+;; This file is part of tm (Tools for MIME).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or (at
+;; your option) any later version.
+
+;; This program is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to
+;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Code:
 
 (require 'emu)
 (require 'tl-822)
@@ -21,7 +38,7 @@
 ;;;
 
 (defconst tm-ew-d/RCS-ID
-  "$Id: tm-ew-d.el,v 7.5 1995/10/18 11:18:03 morioka Exp $")
+  "$Id: tm-ew-d.el,v 7.21 1996/08/17 02:41:20 morioka Exp $")
 (defconst mime/eword-decoder-version (get-version-string tm-ew-d/RCS-ID))
 
 
 ;;; @ for string
 ;;;
 
-(defun mime-eword/decode-string (str)
+(defun mime-eword/decode-string (str &optional unfolding)
   (setq str (rfc822/unfolding-string str))
   (let ((dest "")(ew nil)
        beg end)
-    (while (setq beg (string-match mime/encoded-word-regexp str))
+    (while (and (string-match mime/encoded-word-regexp str)
+               (setq beg (match-beginning 0)
+                     end (match-end 0))
+               )
       (if (> beg 0)
-         (if (not (and (eq ew t) (string= (substring str 0 beg) " ")))
-             (setq dest (concat dest (substring str 0 beg)
-                                ))
+         (if (not
+              (and (eq ew t)
+                   (string-match "^[ \t]+$" (substring str 0 beg))
+                   ))
+             (setq dest (concat dest (substring str 0 beg)))
            )
        )
-      (setq end (match-end 0))
-      (setq dest (concat dest
-                        (mime/decode-encoded-word (substring str beg end))
-                        ))
+      (setq dest
+           (concat dest
+                   (mime/decode-encoded-word
+                    (substring str beg end) unfolding)
+                   ))
       (setq str (substring str end))
       (setq ew t)
       )
 ;;; @ for region
 ;;;
 
-(defun mime-eword/decode-region (beg end &optional unfolding)
+(defun mime-eword/decode-region (beg end &optional unfolding must-unfold)
+  "Decode MIME encoded-words in region between BEG and END.
+If UNFOLDING is not nil, it unfolds before decoding.
+If MUST-UNFOLD is not nil, it unfolds encoded results. [tm-ew-d.el]"
   (interactive "*r")
   (save-excursion
     (save-restriction
       (goto-char (point-min))
       (let (charset encoding text)
        (while (re-search-forward mime/encoded-word-regexp nil t)
-         (insert (mime/decode-encoded-word 
+         (insert (mime/decode-encoded-word
                   (prog1
                       (buffer-substring (match-beginning 0) (match-end 0))
                     (delete-region (match-beginning 0) (match-end 0))
-                    )
-                  ))
+                    ) must-unfold))
          ))
       )))
 
 ;;;
 
 (defun mime/decode-message-header ()
+  "Decode MIME encoded-words in message header. [tm-ew-d.el]"
   (interactive "*")
   (save-excursion
     (save-restriction
 ;;; @ encoded-word decoder
 ;;;
 
-(defun mime/decode-encoded-word (word)
+(defun mime/decode-encoded-word (word &optional unfolding)
   (or (if (string-match mime/encoded-word-regexp word)
          (let ((charset
-                (upcase
-                 (substring word (match-beginning 1) (match-end 1))
-                 ))
+                (substring word (match-beginning 1) (match-end 1))
+                )
                (encoding
                 (upcase
                  (substring word (match-beginning 2) (match-end 2))
                (text
                 (substring word (match-beginning 3) (match-end 3))
                 ))
-           (mime/decode-encoded-text charset encoding text)
+           (mime/decode-encoded-text charset encoding text unfolding)
            ))
       word))
 
 ;;; @ encoded-text decoder
 ;;;
 
-(defun mime/decode-encoded-text (charset encoding str)
-  (let ((dest
-        (cond ((string= "B" encoding)
-               (base64-decode-string str))
-              ((string= "Q" encoding)
-               (q-encoding-decode-string str))
-              (t (message "unknown encoding %s" encoding)
-                 nil))))
-    (if dest
-       (mime/convert-string-to-emacs charset dest)
-      )))
+(defun mime/decode-encoded-text (charset encoding str &optional unfolding)
+  (let ((cs (mime-charset-to-coding-system charset)))
+    (if cs
+       (let ((dest
+              (cond ((string-equal "B" encoding)
+                     (base64-decode-string str))
+                    ((string-equal "Q" encoding)
+                     (q-encoding-decode-string str))
+                    (t (message "unknown encoding %s" encoding)
+                       nil))))
+         (if dest
+             (progn
+               (setq dest (decode-coding-string dest cs))
+               (if unfolding
+                   (rfc822/unfolding-string dest)
+                 dest)
+               ))))))
 
 
 ;;; @ end
 ;;;
 
 (provide 'tm-ew-d)
+
+;;; tm-ew-d.el ends here