Merge flim-1_11_0.
[elisp/flim.git] / mel-q.el
index 6539033..0e80c16 100644 (file)
--- a/mel-q.el
+++ b/mel-q.el
-;;;
-;;; $Id: mel-q.el,v 2.0 1995/09/11 11:33:47 morioka Exp $
-;;;
+;;; mel-q.el: Quoted-Printable and Q-encoding encoder/decoder for GNU Emacs
 
-;;; @ constants
-;;;
+;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
+
+;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
+;; Created: 1995/6/25
+;; Keywords: MIME, Quoted-Printable, Q-encoding
+
+;; This file is part of MEL (MIME Encoding Library).
+
+;; 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.
 
-(defconst quoted-printable-hex-chars "0123456789ABCDEF")
-(defconst quoted-printable-octet-regexp
-  (concat "=[" quoted-printable-hex-chars
-         "][" quoted-printable-hex-chars "]"))
+;; 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 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.
 
-;;; @ variables
+;;; Code:
+
+(require 'emu)
+(require 'mime-def)
+
+
+;;; @ Quoted-Printable encoder
 ;;;
 
+(defsubst quoted-printable-quote-char (character)
+  (concat
+   "="
+   (char-to-string (aref quoted-printable-hex-chars (ash character -4)))
+   (char-to-string (aref quoted-printable-hex-chars (logand character 15)))
+   ))
+
+(defun quoted-printable-internal-encode-region (start end)
+  (save-excursion
+    (save-restriction
+      (narrow-to-region start end)
+      (goto-char start)
+      (let ((col 0))
+       (while (< (point)(point-max))
+         (cond ((>= col 75)
+                (insert "=\n")
+                (setq col 0)
+                )
+               ((looking-at-as-unibyte "^From ")
+                (replace-match "=46rom ")
+                (backward-char 1)
+                (setq col (+ col 6))
+                )
+               ((looking-at-as-unibyte "[ \t]\n")
+                (forward-char 1)
+                (insert "=\n")
+                (forward-char 1)
+                (setq col 0)
+                )
+               (t
+                (let ((chr (char-after (point))))
+                  (cond ((= chr ?\n)
+                         (forward-char 1)
+                         (setq col 0)
+                         )
+                        ((or (= chr ?\t)
+                             (and (<= 32 chr)(/= chr ?=)(< chr 127))
+                             )
+                         (forward-char 1)
+                         (setq col (1+ col))
+                         )
+                        ((>= col 73)
+                         (insert "=\n")
+                         (setq col 0)
+                         )
+                        (t
+                         (delete-char 1)
+                         (insert (quoted-printable-quote-char chr))
+                         (setq col (+ col 3))
+                         ))
+                  )))
+         )))))
+
+
 (defvar quoted-printable-external-encoder '("mmencode" "-q")
   "*list of quoted-printable encoder program name and its arguments.")
 
-(defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
-  "*list of quoted-printable decoder program name and its arguments.")
+(defun quoted-printable-external-encode-region (start end)
+  (save-excursion
+    (save-restriction
+      (narrow-to-region start end)
+      (as-binary-process
+       (apply (function call-process-region)
+             start end (car quoted-printable-external-encoder)
+             t t nil (cdr quoted-printable-external-encoder))
+       )
+      ;; for OS/2
+      ;;   regularize line break code
+      (goto-char (point-min))
+      (while (re-search-forward "\r$" nil t)
+       (replace-match "")
+       )
+      )))
 
-(defvar quoted-printable-internal-encoding-limit 10000
+
+(defvar quoted-printable-internal-encoding-limit
+  (if (and (featurep 'xemacs)(featurep 'mule))
+      0
+    (require 'path-util)
+    (if (exec-installed-p "mmencode")
+       1000
+      (message "Don't found external encoder for Quoted-Printable!")
+      nil))
   "*limit size to use internal quoted-printable encoder.
 If size of input to encode is larger than this limit,
 external encoder is called.")
 
-(defvar quoted-printable-internal-decoding-limit nil
-  "*limit size to use internal quoted-printable decoder.
-If size of input to decode is larger than this limit,
-external decoder is called.")
-
+(defun quoted-printable-encode-region (start end)
+  "Encode current region by quoted-printable.
+START and END are buffer positions.
+This function calls internal quoted-printable encoder if size of
+region is smaller than `quoted-printable-internal-encoding-limit',
+otherwise it calls external quoted-printable encoder specified by
+`quoted-printable-external-encoder'.  In this case, you must install
+the program (maybe mmencode included in metamail or XEmacs package)."
+  (interactive "r")
+  (if (and quoted-printable-internal-encoding-limit
+          (> (- end start) quoted-printable-internal-encoding-limit))
+      (quoted-printable-external-encode-region start end)
+    (quoted-printable-internal-encode-region start end)
+    ))
 
-;;; @ Quoted-Printable (Q-encode) encoder/decoder
-;;;
 
-(defun quoted-printable-quote-char (chr)
-  (concat "="
-         (char-to-string (elt quoted-printable-hex-chars (ash chr -4)))
-         (char-to-string (elt quoted-printable-hex-chars (logand chr 15)))
-         ))
+(defun quoted-printable-encode-string (string)
+  "Encode STRING to quoted-printable, and return the result."
+  (with-temp-buffer
+    (insert string)
+    (quoted-printable-encode-region (point-min)(point-max))
+    (buffer-string)
+    ))
 
 
-;;; @@ Quoted-Printable encode/decode string
-;;;
+(mel-define-method-function
+ (mime-encode-string string (nil "quoted-printable"))
+ 'quoted-printable-encode-string)
 
-(defun quoted-printable-encode-string (str)
-  (let ((i 0))
-    (mapconcat (function
-               (lambda (chr)
-                 (cond ((or (< chr 32) (< 126 chr) (eq chr ?=))
-                        (if (>= i 73)
-                            (progn
-                              (setq i 0)
-                              (concat "=\n" (quoted-printable-quote-char chr))
-                              )
-                          (progn
-                            (setq i (+ i 3))
-                            (quoted-printable-quote-char chr)
-                            )))
-                       (t (if (>= i 75)
-                              (progn
-                                (setq i 0)
-                                (concat "=\n" (char-to-string chr))
-                                )
-                            (progn
-                              (setq i (1+ i))
-                              (char-to-string chr)
-                              )))
-                       )))
-              str "")))
+(mel-define-method-function
+ (mime-encode-region start end (nil "quoted-printable"))
+ 'quoted-printable-encode-region)
 
-(defun quoted-printable-decode-string (str)
-  (let (q h l)
-    (mapconcat (function
-               (lambda (chr)
-                 (cond ((eq chr ?=)
-                        (setq q t)
-                        "")
-                       (q (setq h
-                                (cond ((<= ?a chr) (+ (- chr ?a) 10))
-                                      ((<= ?A chr) (+ (- chr ?A) 10))
-                                      ((<= ?0 chr) (- chr ?0))
-                                      ))
-                          (setq q nil)
-                          "")
-                       (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
-                                        ((<= ?A chr) (+ (- chr ?A) 10))
-                                        ((<= ?0 chr) (- chr ?0))
-                                        ))
-                          (prog1
-                              (char-to-string (logior (ash h 4) l))
-                            (setq h nil)
-                            )
-                          )
-                       (t (char-to-string chr))
-                       )))
-              str "")))
+(mel-define-method mime-insert-encoded-file (filename
+                                            (nil "quoted-printable"))
+  "Encode contents of file FILENAME to quoted-printable, and insert the result.
+It calls external quoted-printable encoder specified by
+`quoted-printable-external-encoder'.  So you must install the program
+\(maybe mmencode included in metamail or XEmacs package)."
+  (interactive (list (read-file-name "Insert encoded file: ")))
+  (apply (function call-process) (car quoted-printable-external-encoder)
+        filename t nil (cdr quoted-printable-external-encoder))
+  )
 
 
-;;; @@ Quoted-Printable encode/decode region
+;;; @ Quoted-Printable decoder
 ;;;
 
-(defun quoted-printable-internal-encode-region (beg end)
-  (save-excursion
-    (save-restriction
-      (narrow-to-region beg end)
-      (goto-char (point-min))
-      (catch 'tag
-       (let (b e str)
-         (while t
-           (beginning-of-line) (setq b (point))
-           (end-of-line)       (setq e (point))
-           (if (< b e)
-               (progn
-                 (setq str (buffer-substring b e))
-                 (delete-region b e)
-                 (insert (quoted-printable-encode-string str))
-                 ))
-           (if (eobp)
-               (throw 'tag nil)
-             )
-           (forward-char 1)
-           )))
-      )))
+(defsubst quoted-printable-hex-char-to-num (chr)
+  (cond ((<= ?a chr) (+ (- chr ?a) 10))
+       ((<= ?A chr) (+ (- chr ?A) 10))
+       ((<= ?0 chr) (- chr ?0))
+       ))
 
-(defun quoted-printable-internal-decode-region (beg end)
+(defun quoted-printable-internal-decode-region (start end)
   (save-excursion
     (save-restriction
-      (narrow-to-region beg end)
+      (narrow-to-region start end)
       (goto-char (point-min))
-      (while (re-search-forward "=\n" nil t)
-       (replace-match "")
-       )
-      (goto-char (point-min))
-      (let (b e str)
-       (while (re-search-forward quoted-printable-octet-regexp nil t)
-         (setq b (match-beginning 0))
-         (setq e (match-end 0))
-         (setq str (buffer-substring b e))
-         (delete-region b e)
-         (insert (quoted-printable-decode-string str))
-         ))
-      )))
+      (while (search-forward "=" nil t)
+       (let ((beg (match-beginning 0)))
+         (cond ((looking-at "\n")
+                (delete-region beg (match-end 0))
+                )
+               ((looking-at
+                 `,(concat "[" quoted-printable-hex-chars
+                           "][" quoted-printable-hex-chars "]"))
+                (let* ((end (match-end 0))
+                       (hex (buffer-substring (match-beginning 0) end)))
+                  (delete-region beg end)
+                  (insert
+                   (logior
+                    (ash (quoted-printable-hex-char-to-num (aref hex 0)) 4)
+                    (quoted-printable-hex-char-to-num (aref hex 1))))
+                  ))
+               (t
+                ;; invalid
+                ))
+         )))))
 
-(cond ((boundp 'MULE)
-       (define-program-coding-system
-        nil (car quoted-printable-external-encoder) *noconv*)
-       (define-program-coding-system
-        nil (car quoted-printable-external-decoder) *noconv*)
-       )
-      ((boundp 'NEMACS)
-       (define-program-kanji-code
-        nil (car quoted-printable-external-encoder) 0)
-       (define-program-kanji-code
-        nil (car quoted-printable-external-decoder) 0)
-       ))
-
-(defun quoted-printable-external-encode-region (beg end)
-  (save-excursion
-    (apply (function call-process-region)
-          beg end (car quoted-printable-external-encoder)
-          t t nil (cdr quoted-printable-external-encoder))
-    ))
+(defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
+  "*list of quoted-printable decoder program name and its arguments.")
 
-(defun quoted-printable-external-decode-region (beg end)
+(defun quoted-printable-external-decode-region (start end)
   (save-excursion
-    (apply (function call-process-region)
-          beg end (car quoted-printable-external-decoder)
-          t t nil (cdr quoted-printable-external-decoder))
-    ))
+    (as-binary-process
+     (apply (function call-process-region)
+           start end (car quoted-printable-external-decoder)
+           t t nil (cdr quoted-printable-external-decoder))
+     )))
 
-(defun quoted-printable-encode-region (beg end)
-  (interactive "r")
-  (if (and quoted-printable-internal-encoding-limit
-          (> (- end beg) quoted-printable-internal-encoding-limit))
-      (quoted-printable-external-encode-region beg end)
-    (quoted-printable-internal-encode-region beg end)
-    ))
 
-(defun quoted-printable-decode-region (beg end)
+(defvar quoted-printable-internal-decoding-limit nil
+  "*limit size to use internal quoted-printable decoder.
+If size of input to decode is larger than this limit,
+external decoder is called.")
+
+(defun quoted-printable-decode-region (start end)
+  "Decode current region by quoted-printable.
+START and END are buffer positions.
+This function calls internal quoted-printable decoder if size of
+region is smaller than `quoted-printable-internal-decoding-limit',
+otherwise it calls external quoted-printable decoder specified by
+`quoted-printable-external-decoder'.  In this case, you must install
+the program (maybe mmencode included in metamail or XEmacs package)."
   (interactive "r")
   (if (and quoted-printable-internal-decoding-limit
-          (> (- end beg) quoted-printable-internal-decoding-limit))
-      (quoted-printable-external-decode-region beg end)
-    (quoted-printable-internal-decode-region beg end)
+          (> (- end start) quoted-printable-internal-decoding-limit))
+      (quoted-printable-external-decode-region start end)
+    (quoted-printable-internal-decode-region start end)
     ))
 
-
+(defun quoted-printable-decode-string (string)
+  "Decode STRING which is encoded in quoted-printable, and return the result."
+  (with-temp-buffer
+    (insert string)
+    (quoted-printable-decode-region (point-min)(point-max))
+    (buffer-string)))
+
+
+(mel-define-method-function
+ (mime-decode-string string (nil "quoted-printable"))
+ 'quoted-printable-decode-string)
+
+(mel-define-method-function
+ (mime-decode-region start end (nil "quoted-printable"))
+ 'quoted-printable-decode-region)
+
+
+(defvar quoted-printable-external-decoder-option-to-specify-file '("-o")
+  "*list of options of quoted-printable decoder program to specify file.")
+
+(mel-define-method mime-write-decoded-region (start end filename
+                                                   (nil "quoted-printable"))
+  "Decode and write current region encoded by quoted-printable into FILENAME.
+START and END are buffer positions."
+  (interactive
+   (list (region-beginning) (region-end)
+        (read-file-name "Write decoded region to file: ")))
+  (as-binary-process
+   (apply (function call-process-region)
+         start end (car quoted-printable-external-decoder)
+         nil nil nil
+         (append (cdr quoted-printable-external-decoder)
+                 quoted-printable-external-decoder-option-to-specify-file
+                 (list filename))
+         )))
+
+\f
 ;;; @ Q-encoding encode/decode string
 ;;;
 
-(defun q-encoding-encode-string-for-text (str)
-  (mapconcat (function
-             (lambda (chr)
-               (cond ((eq chr 32) "_")
-                     ((or (< chr 32) (< 126 chr) (eq chr ?=))
-                      (quoted-printable-quote-char chr)
-                      )
-                     (t (char-to-string chr))
-                     )))
-            str ""))
-
-(defun q-encoding-encode-string-for-comment (str)
-  (mapconcat (function
-             (lambda (chr)
-               (cond ((eq chr 32) "_")
-                     ((or (< chr 32) (< 126 chr)
-                          (memq chr '(?= ?\( ?\) ?\\))
-                          )
-                      (quoted-printable-quote-char chr)
-                      )
-                     (t (char-to-string chr))
-                     )))
-            str ""))
-
-(defun q-encoding-encode-string-for-phrase (str)
-  (mapconcat (function
-             (lambda (chr)
-               (cond ((eq chr 32) "_")
-                     ((or (and (<= ?A chr)(<= chr ?Z))
-                          (and (<= ?a chr)(<= chr ?z))
-                          (and (<= ?0 chr)(<= chr ?9))
-                          (memq chr '(?! ?* ?+ ?- ?/))
-                          )
-                      (char-to-string chr)
-                      )
-                     (t (quoted-printable-quote-char chr))
-                     )))
-            str ""))
-
-(defun q-encoding-encode-string (str &optional mode)
-  (cond ((eq mode 'text)
-        (q-encoding-encode-string-for-text str)
-        )
-       ((eq mode 'comment)
-        (q-encoding-encode-string-for-comment str)
-        )
-       (t
-        (q-encoding-encode-string-for-phrase str)
-        )))
-
-(defun q-encoding-decode-string (str)
+(defconst q-encoding-special-chars-alist
+  '((text      ?= ?? ?_)
+    (comment   ?= ?? ?_ ?\( ?\) ?\\)
+    (phrase    ?= ?? ?_ ?\( ?\) ?\\ ?\" ?# ?$ ?% ?& ?' ?, ?. ?/
+               ?: ?\; ?< ?> ?@ ?\[ ?\] ?^ ?` ?{ ?| ?} ?~)
+    ))
+
+(defun q-encoding-encode-string (string &optional mode)
+  "Encode STRING to Q-encoding of encoded-word, and return the result.
+MODE allows `text', `comment', `phrase' or nil.  Default value is
+`phrase'."
+  (let ((specials (cdr (or (assq mode q-encoding-special-chars-alist)
+                          (assq 'phrase q-encoding-special-chars-alist)
+                          ))))
+    (mapconcat (function
+               (lambda (chr)
+                 (cond ((eq chr ? ) "_")
+                       ((or (< chr 32) (< 126 chr)
+                            (memq chr specials)
+                            )
+                        (quoted-printable-quote-char chr)
+                        )
+                       (t
+                        (char-to-string chr)
+                        ))
+                 ))
+              string "")
+    ))
+
+(defun q-encoding-decode-string (string)
+  "Decode STRING which is encoded in Q-encoding and return the result."
   (let (q h l)
     (mapconcat (function
                (lambda (chr)
@@ -248,16 +309,10 @@ external decoder is called.")
                        ((eq chr ?=)
                         (setq q t)
                         "")
-                       (q (setq h (cond ((<= ?a chr) (+ (- chr ?a) 10))
-                                        ((<= ?A chr) (+ (- chr ?A) 10))
-                                        ((<= ?0 chr) (- chr ?0))
-                                        ))
+                       (q (setq h (quoted-printable-hex-char-to-num chr))
                           (setq q nil)
                           "")
-                       (h (setq l (cond ((<= ?a chr) (+ (- chr ?a) 10))
-                                        ((<= ?A chr) (+ (- chr ?A) 10))
-                                        ((<= ?0 chr) (- chr ?0))
-                                        ))
+                       (h (setq l (quoted-printable-hex-char-to-num chr))
                           (prog1
                               (char-to-string (logior (ash h 4) l))
                             (setq h nil)
@@ -265,25 +320,21 @@ external decoder is called.")
                           )
                        (t (char-to-string chr))
                        )))
-              str "")))
+              string "")))
 
+(mel-define-method-function (encoded-text-encode-string string (nil "Q"))
+                           'q-encoding-encode-string)
 
-;;; @@ etc
-;;;
-
-(defun q-encoding-encoded-length (string &optional mode)
-  (let ((l 0)(i 0)(len (length string)) chr)
-    (while (< i len)
-      (setq chr (elt string i))
-      (if (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
-         (setq l (+ l 1))
-       (setq l (+ l 3))
-       )
-      (setq i (+ i 1)) )
-    l))
+(mel-define-method encoded-text-decode-string (string (nil "Q"))
+  (if (and (string-match Q-encoded-text-regexp string)
+          (string= string (match-string 0 string)))
+      (q-encoding-decode-string string)
+    (error "Invalid encoded-text %s" string)))
 
 
 ;;; @ end
 ;;;
 
 (provide 'mel-q)
+
+;;; mel-q.el ends here