mel-u.el (mime-write-decoded-region): Use temporary filename.
[elisp/flim.git] / mel-b-el.el
index 076f2f6..eac60b5 100644 (file)
@@ -1,9 +1,9 @@
 ;;; mel-b-el.el --- Base64 encoder/decoder.
 
-;; Copyright (C) 1992,1995,1996,1997,1998 Free Software Foundation, Inc.
+;; Copyright (C) 1992,95,96,97,98,99,2001 Free Software Foundation, Inc.
 
 ;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
-;;     MORIOKA Tomohiko <morioka@jaist.ac.jp>
+;;         MORIOKA Tomohiko <tomo@m17n.org>
 ;; Created: 1995/6/24
 ;; Keywords: MIME, Base64
 
@@ -27,6 +27,9 @@
 ;;; Code:
 
 (require 'mime-def)
+(eval-when-compile
+  ;; XXX: the macro `as-binary-process' should be provided when compiling.
+  (require 'pces))
 
 
 ;;; @ variables
@@ -47,7 +50,8 @@
   :type '(cons (file :tag "Command")(repeat :tag "Arguments" string)))
 
 (defcustom base64-external-decoder-option-to-specify-file '("-o")
-  "*list of options of base64 decoder program to specify file."
+  "*list of options of base64 decoder program to specify file.
+If the base64 decoder program does not have such option, set this as nil."
   :group 'base64
   :type '(repeat :tag "Arguments" string))
 
@@ -123,8 +127,10 @@ external decoder is called."
                   (ash (logand (car pack) 3) 4))))
     buf))
 
-(defun-maybe base64-encode-string (string)
-  "Encode STRING to base64, and return the result."
+(defun-maybe base64-encode-string (string &optional no-line-break)
+  "Base64-encode STRING and return the result.
+Optional second argument NO-LINE-BREAK means do not break long lines
+into shorter lines."
   (let* ((len (length string))
         (b 0)(e 57)
         (dest ""))
@@ -135,7 +141,7 @@ external decoder is called."
                     (function base64-encode-1)
                     (pack-sequence (substring string b e) 3)
                     "")
-                   "\n"))
+                   (if (not no-line-break) "\n")))
       (setq b e
            e (+ e 57)))
     (concat dest
@@ -144,16 +150,14 @@ external decoder is called."
             (pack-sequence (substring string b) 3)
             ""))))
 
-(defun base64-internal-encode-region (beg end)
+(defun base64-internal-encode-region (beg end &optional no-line-break)
   (save-excursion
     (save-restriction
       (narrow-to-region beg end)
       (insert
        (prog1
-          (base64-encode-string
-           (buffer-substring beg end))
-        (delete-region beg end)))
-      (or (bolp) (insert ?\n)))))
+          (base64-encode-string (buffer-substring beg end) no-line-break)
+        (delete-region beg end))))))
 
 
 ;;; @ internal base64 decoder
@@ -231,7 +235,7 @@ external decoder is called."
 ;;; @ external encoder/decoder
 ;;;
 
-(defun base64-external-encode-region (beg end)
+(defun base64-external-encode-region (beg end &optional no-line-break)
   (save-excursion
     (save-restriction
       (narrow-to-region beg end)
@@ -244,7 +248,12 @@ external decoder is called."
       ;;   regularize line break code
       (goto-char (point-min))
       (while (re-search-forward "\r$" nil t)
-       (replace-match "")))))
+       (replace-match ""))
+      (if no-line-break
+         (progn
+           (goto-char (point-min))
+           (while (search-forward "\n" nil t)
+             (replace-match "")))))))
 
 (defun base64-external-decode-region (beg end)
   (save-excursion
@@ -268,9 +277,11 @@ external decoder is called."
 ;;; @ application interfaces
 ;;;
 
-(defun-maybe base64-encode-region (start end)
-  "Encode current region by base64.
-START and END are buffer positions.
+(defun-maybe base64-encode-region (start end &optional no-line-break)
+  "Base64-encode the region between START and END.
+Return the length of the encoded text.
+Optional third argument NO-LINE-BREAK means do not break long lines
+into shorter lines.
 This function calls internal base64 encoder if size of region is
 smaller than `base64-internal-encoding-limit', otherwise it calls
 external base64 encoder specified by `base64-external-encoder'.  In
@@ -279,8 +290,8 @@ metamail or XEmacs package)."
   (interactive "*r")
   (if (and base64-internal-encoding-limit
           (> (- end start) base64-internal-encoding-limit))
-      (base64-external-encode-region start end)
-    (base64-internal-encode-region start end)))
+      (base64-external-encode-region start end no-line-break)
+    (base64-internal-encode-region start end no-line-break)))
 
 (defun-maybe base64-decode-region (start end)
   "Decode current region by base64.
@@ -358,13 +369,22 @@ START and END are buffer positions."
   (interactive "*r\nFWrite decoded region to file: ")
   (if (and base64-internal-decoding-limit
           (> (- end start) base64-internal-decoding-limit))
-      (as-binary-process
-       (apply (function call-process-region)
-             start end (car base64-external-decoder)
-             nil nil nil
-             (append (cdr base64-external-decoder)
-                     base64-external-decoder-option-to-specify-file
-                     (list filename))))
+      (progn
+       (as-binary-process
+        (apply (function call-process-region)
+               start end (car base64-external-decoder)
+               (null base64-external-decoder-option-to-specify-file)
+               (unless base64-external-decoder-option-to-specify-file
+                 (list (current-buffer) nil))
+               nil
+               (delq nil
+                     (append
+                      (cdr base64-external-decoder)
+                      base64-external-decoder-option-to-specify-file
+                      (when base64-external-decoder-option-to-specify-file
+                        (list filename))))))
+       (unless base64-external-decoder-option-to-specify-file
+         (write-region-as-binary (point-min) (point-max) filename)))
     (let ((str (buffer-substring start end)))
       (with-temp-buffer
        (insert (base64-internal-decode-string str))
@@ -374,7 +394,7 @@ START and END are buffer positions."
  (mime-write-decoded-region start end filename (nil "base64"))
  'base64-write-decoded-region)
 
-       
+
 ;;; @ end
 ;;;