tm 7.90.
[elisp/tm.git] / tm-ew-d.el
index f7cfd87..e69cc5c 100644 (file)
@@ -5,8 +5,11 @@
 ;; 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.25 $
+;; Created: 1995/10/03
+;; Original: 1992/07/20 ENAMI Tsugutomo's `mime.el'.
+;;     Renamed: 1993/06/03 to tiny-mime.el.
+;;     Renamed: 1995/10/03 from tiny-mime.el. (split off encoder)
+;; Version: $Revision: 7.29 $
 ;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
 
 ;; This file is part of tm (Tools for MIME).
@@ -22,8 +25,8 @@
 ;; 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,
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 ;; Boston, MA 02111-1307, USA.
 
 ;;; Code:
@@ -38,7 +41,7 @@
 ;;;
 
 (defconst tm-ew-d/RCS-ID
-  "$Id: tm-ew-d.el,v 7.25 1996/09/13 02:39:01 morioka Exp $")
+  "$Id: tm-ew-d.el,v 7.29 1996/10/13 18:44:49 morioka Exp $")
 (defconst mime/eword-decoder-version (get-version-string tm-ew-d/RCS-ID))
 
 
 ;;; @ for string
 ;;;
 
-(defun mime-eword/decode-string (str &optional unfolding)
-  (setq str (std11-unfold-string str))
+(defun mime-eword/decode-string (string &optional must-unfold)
+  "Decode MIME encoded-words in STRING.
+
+STRING is unfolded before decoding.
+
+If an encoded-word is broken or your emacs implementation can not
+decode the charset included in it, it is not decoded.
+
+If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
+if there are in decoded encoded-words (generated by bad manner MUA
+such as a version of Net$cape). [tm-ew-d.el]"
+  (setq string (std11-unfold-string string))
   (let ((dest "")(ew nil)
        beg end)
-    (while (and (string-match mime/encoded-word-regexp str)
+    (while (and (string-match mime/encoded-word-regexp string)
                (setq beg (match-beginning 0)
                      end (match-end 0))
                )
       (if (> beg 0)
          (if (not
               (and (eq ew t)
-                   (string-match "^[ \t]+$" (substring str 0 beg))
+                   (string-match "^[ \t]+$" (substring string 0 beg))
                    ))
-             (setq dest (concat dest (substring str 0 beg)))
+             (setq dest (concat dest (substring string 0 beg)))
            )
        )
       (setq dest
            (concat dest
                    (mime/decode-encoded-word
-                    (substring str beg end) unfolding)
+                    (substring string beg end) must-unfold)
                    ))
-      (setq str (substring str end))
+      (setq string (substring string end))
       (setq ew t)
       )
-    (concat dest str)
+    (concat dest string)
     ))
 
 
 ;;; @ for region
 ;;;
 
-(defun mime-eword/decode-region (beg end &optional unfolding must-unfold)
-  "Decode MIME encoded-words in region between BEG and END.
+(defun mime-eword/decode-region (start end &optional unfolding must-unfold)
+  "Decode MIME encoded-words in region between START 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]"
+
+If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
+if there are in decoded encoded-words (generated by bad manner MUA
+such as a version of Net$cape). [tm-ew-d.el]"
   (interactive "*r")
   (save-excursion
     (save-restriction
-      (narrow-to-region beg end)
+      (narrow-to-region start end)
       (if unfolding
          (mime/unfolding)
        )
@@ -156,7 +173,15 @@ If MUST-UNFOLD is not nil, it unfolds encoded results. [tm-ew-d.el]"
 ;;; @ encoded-word decoder
 ;;;
 
-(defun mime/decode-encoded-word (word &optional unfolding)
+(defun mime/decode-encoded-word (word &optional must-unfold)
+  "Decode WORD if it is an encoded-word.
+
+If your emacs implementation can not decode the charset of WORD, it
+returns WORD.  Similarly the encoded-word is broken, it returns WORD.
+
+If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
+if there are in decoded encoded-word (generated by bad manner MUA such
+as a version of Net$cape). [tm-ew-d.el]"
   (or (if (string-match mime/encoded-word-regexp word)
          (let ((charset
                 (substring word (match-beginning 1) (match-end 1))
@@ -168,7 +193,7 @@ If MUST-UNFOLD is not nil, it unfolds encoded results. [tm-ew-d.el]"
                (text
                 (substring word (match-beginning 3) (match-end 3))
                 ))
-           (mime/decode-encoded-text charset encoding text unfolding)
+           (mime/decode-encoded-text charset encoding text must-unfold)
            ))
       word))
 
@@ -176,20 +201,30 @@ If MUST-UNFOLD is not nil, it unfolds encoded results. [tm-ew-d.el]"
 ;;; @ encoded-text decoder
 ;;;
 
-(defun mime/decode-encoded-text (charset encoding str &optional unfolding)
+(defun mime/decode-encoded-text (charset encoding string &optional must-unfold)
+  "Decode STRING as an encoded-text.
+
+If your emacs implementation can not decode CHARSET, it returns nil.
+
+If ENCODING is not \"B\" or \"Q\", it occurs error.
+So you should write error-handling code if you don't want break by errors.
+
+If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
+if there are in decoded encoded-text (generated by bad manner MUA such
+as a version of Net$cape). [tm-ew-d.el]"
   (let ((cs (mime-charset-to-coding-system charset)))
     (if cs
        (let ((dest
               (cond ((string-equal "B" encoding)
-                     (base64-decode-string str))
+                     (base64-decode-string string))
                     ((string-equal "Q" encoding)
-                     (q-encoding-decode-string str))
+                     (q-encoding-decode-string string))
                     (t (message "unknown encoding %s" encoding)
                        nil))))
          (if dest
              (progn
                (setq dest (decode-coding-string dest cs))
-               (if unfolding
+               (if must-unfold
                    (mapconcat (function
                                (lambda (chr)
                                  (if (eq chr ?\n)