+
+(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)
+ ))
+
+
+(defun quoted-printable-insert-encoded-file (filename)
+ "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 decoder
+;;;
+
+(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 (start end)
+ (save-excursion
+ (save-restriction
+ (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
+ ))
+ )))))
+
+(defvar quoted-printable-external-decoder '("mmencode" "-q" "-u")
+ "*list of quoted-printable decoder program name and its arguments.")
+
+(defun quoted-printable-external-decode-region (start end)
+ (save-excursion
+ (as-binary-process
+ (apply (function call-process-region)
+ start end (car quoted-printable-external-decoder)
+ t t nil (cdr quoted-printable-external-decoder))
+ )))
+
+
+(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)."