Update header.
authorshuhei-k <shuhei-k>
Sat, 22 May 1999 16:52:39 +0000 (16:52 +0000)
committershuhei-k <shuhei-k>
Sat, 22 May 1999 16:52:39 +0000 (16:52 +0000)
Require 'path-util instead of 'emu.
(quoted-printable-internal-encode-region): Rewrite without regexp.
(quoted-printable-internal-decode-region): Ditto.
(quoted-printable-encode-region): Use read-only `interactive' spec.
(quoted-printable-decode-region): Ditto.
(mime-insert-encoded-file): Use built-in `interactive' spec.
(mime-write-decoded-region): Ditto.
(encoded-text-decode-string): Anchor regexp with "\\`" and "\\'".

mel-q.el

index 0e80c16..6200a74 100644 (file)
--- a/mel-q.el
+++ b/mel-q.el
@@ -1,12 +1,12 @@
-;;; mel-q.el: Quoted-Printable and Q-encoding encoder/decoder for GNU Emacs
+;;; mel-q.el --- Quoted-Printable encoder/decoder.
 
-;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
+;; Copyright (C) 1995,1996,1997,1998,1999 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 file is part of FLIM (Faithful Library about Internet Message).
 
 ;; This program is free software; you can redistribute it and/or
 ;; modify it under the terms of the GNU General Public License as
 ;; 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
+;; 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 'mime-def)
+(require 'path-util)
 
 
 ;;; @ Quoted-Printable encoder
   (concat
    "="
    (char-to-string (aref quoted-printable-hex-chars (ash character -4)))
-   (char-to-string (aref quoted-printable-hex-chars (logand character 15)))
-   ))
+   (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))
-                         ))
-                  )))
-         )))))
+      (narrow-to-region (goto-char start) end)
+      (let ((col 0)
+           chr)
+       (while (not (eobp))
+         (cond
+          ((>= col 75)                 ; soft line break.
+           (insert "=\n")
+           (setq col 0))
+          ((eolp)                      ; end of line.
+           (forward-char)
+           (setq col 0))
+          (t
+           (setq chr (char-after (point)))
+           (cond
+            ((and (memq chr '(?  ?\t)) ; encode WSP char before CRLF.
+                  (eq (char-after (1+ (point))) ?\n))
+             (forward-char)
+             (insert "=\n")
+             (forward-char)
+             (setq col 0))
+            ((and (bolp)               ; "^From " is not safe.
+                  (eq chr                        ?F)
+                  (eq (char-after (1+  (point))) ?r)
+                  (eq (char-after (+ 2 (point))) ?o)
+                  (eq (char-after (+ 3 (point))) ?m)
+                  (eq (char-after (+ 4 (point))) ? ))
+             (delete-region (point)(1+ (point)))
+             (insert "=46")            ; moved to ?r.
+             (forward-char 4)          ; skip "rom ".
+             (setq col 7))
+            ((or (= chr ?\t)           ; skip safe char.
+                 (and (<= 32 chr)(/= chr ?=)(< chr 127)))
+             (forward-char)
+             (setq col (1+ col)))
+            ((>= col 73)               ; soft line break.
+             (insert "=\n")
+             (setq col 0))
+            (t                         ; encode unsafe char.
+             (delete-region (point)(1+ (point)))
+             ;; (insert (quoted-printable-quote-char chr))
+             (insert
+              ?=
+              (aref quoted-printable-hex-chars (ash chr -4))
+              (aref quoted-printable-hex-chars (logand chr 15)))
+             (setq col (+ col 3)))))))))))
 
 
 (defvar quoted-printable-external-encoder '("mmencode" "-q")
       (as-binary-process
        (apply (function call-process-region)
              start end (car quoted-printable-external-encoder)
-             t t nil (cdr 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 "")
-       )
-      )))
+       (replace-match "")))))
 
 
 (defvar quoted-printable-internal-encoding-limit
     (require 'path-util)
     (if (exec-installed-p "mmencode")
        1000
-      (message "Don't found external encoder for Quoted-Printable!")
+      ;; XXX: Fix this message, or simply remove it.
+      ;; (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,
@@ -127,21 +128,18 @@ 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")
+  (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-internal-encode-region start end)))
 
 (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)
-    ))
+    (buffer-string)))
 
 
 (mel-define-method-function
@@ -152,16 +150,16 @@ the program (maybe mmencode included in metamail or XEmacs package)."
  (mime-encode-region start end (nil "quoted-printable"))
  'quoted-printable-encode-region)
 
-(mel-define-method mime-insert-encoded-file (filename
-                                            (nil "quoted-printable"))
+(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))
-  )
+  (interactive "*fInsert encoded file: ")
+  (apply (function call-process)
+        (car quoted-printable-external-encoder)
+        filename t nil
+        (cdr quoted-printable-external-encoder)))
 
 
 ;;; @ Quoted-Printable decoder
@@ -179,25 +177,28 @@ It calls external quoted-printable encoder specified by
       (narrow-to-region start end)
       (goto-char (point-min))
       (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
+        ((eolp)
+         ;; unfold soft line break.
+         (delete-region (1- (point))(1+ (point))))
+        ((and (memq (char-after (point))
+                    (eval-when-compile
+                      ;; XXX: should provide char-list instead.
+                      (string-to-char-list quoted-printable-hex-chars)))
+              (memq (char-after (1+ (point)))
+                    (eval-when-compile
+                      ;; XXX: should provide char-list instead.
+                      (string-to-char-list quoted-printable-hex-chars))))
+         ;; encoded char.
+         (insert
+          (prog1
+              (logior
+               (ash (quoted-printable-hex-char-to-num (char-after (point))) 4)
+               (quoted-printable-hex-char-to-num (char-after (1+ (point)))))
+            (delete-region (1- (point))(+ 2 (point))))))
+        (t
+         ;; invalid encoding.
+         ))))))
 
 (defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
   "*list of quoted-printable decoder program name and its arguments.")
@@ -207,8 +208,8 @@ It calls external quoted-printable encoder specified by
     (as-binary-process
      (apply (function call-process-region)
            start end (car quoted-printable-external-decoder)
-           t t nil (cdr quoted-printable-external-decoder))
-     )))
+           t t nil
+           (cdr quoted-printable-external-decoder)))))
 
 
 (defvar quoted-printable-internal-decoding-limit nil
@@ -224,12 +225,11 @@ 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")
+  (interactive "*r")
   (if (and quoted-printable-internal-decoding-limit
           (> (- end start) quoted-printable-internal-decoding-limit))
       (quoted-printable-external-decode-region start end)
-    (quoted-printable-internal-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."
@@ -255,17 +255,14 @@ the program (maybe mmencode included in metamail or XEmacs package)."
                                                    (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: ")))
+  (interactive "*r\nFWrite 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))
-         )))
+                 (list filename)))))
 
 \f
 ;;; @ Q-encoding encode/decode string
@@ -283,22 +280,16 @@ START and END are buffer positions."
 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)
-                          ))))
+                          (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)
-                        )
+                            (memq chr specials))
+                        (quoted-printable-quote-char chr))
                        (t
-                        (char-to-string chr)
-                        ))
-                 ))
-              string "")
-    ))
+                        (char-to-string chr)))))
+              string "")))
 
 (defun q-encoding-decode-string (string)
   "Decode STRING which is encoded in Q-encoding and return the result."
@@ -315,19 +306,17 @@ MODE allows `text', `comment', `phrase' or nil.  Default value is
                        (h (setq l (quoted-printable-hex-char-to-num chr))
                           (prog1
                               (char-to-string (logior (ash h 4) l))
-                            (setq h nil)
-                            )
-                          )
-                       (t (char-to-string chr))
-                       )))
+                            (setq h nil)))
+                       (t (char-to-string chr)))))
               string "")))
 
 (mel-define-method-function (encoded-text-encode-string string (nil "Q"))
                            'q-encoding-encode-string)
 
 (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)))
+  (if (string-match (eval-when-compile
+                     (concat "\\`" Q-encoded-text-regexp "\\'"))
+                   string)
       (q-encoding-decode-string string)
     (error "Invalid encoded-text %s" string)))
 
@@ -337,4 +326,4 @@ MODE allows `text', `comment', `phrase' or nil.  Default value is
 
 (provide 'mel-q)
 
-;;; mel-q.el ends here
+;;; mel-q.el ends here.