X-Git-Url: http://git.chise.org/gitweb/?a=blobdiff_plain;f=lisp%2Fmm-encode.el;h=ddb5b0bc4406a0a6780d386bae50c40f441f0b8c;hb=ed5859e1e7c85dee6841ff01b44bdd132a265de9;hp=e3bd0af6a6b750551ac76829385a6987b06bf251;hpb=67ebfaaccdebf3a32fdcb37d3a06faf620b4f651;p=elisp%2Fgnus.git- diff --git a/lisp/mm-encode.el b/lisp/mm-encode.el index e3bd0af..ddb5b0b 100644 --- a/lisp/mm-encode.el +++ b/lisp/mm-encode.el @@ -1,5 +1,5 @@ ;;; mm-encode.el --- Functions for encoding MIME things -;; Copyright (C) 1998 Free Software Foundation, Inc. +;; Copyright (C) 1998,99 Free Software Foundation, Inc. ;; Author: Lars Magne Ingebrigtsen ;; MORIOKA Tomohiko @@ -25,55 +25,18 @@ ;;; Code: (require 'mail-parse) - -(defvar mm-mime-file-types - '(("\\.rtf$" "text/richtext") - ("\\.\\(html\\|htm\\)$" "text/html") - ("\\.ps$" "application/postscript" - (encoding quoted-printable) - (disposition "attachment")) - ("\\.\\(jpeg\\|jpg\\)$" "image/jpeg") - ("\\.gif$" "image/gif") - ("\\.png$" "image/png") - ("\\.\\(tiff\\|tif\\)$" "image/tiff") - ("\\.pic$" "image/x-pic") - ("\\.mag$" "image/x-mag") - ("\\.xbm$" "image/x-xbm") - ("\\.xwd$" "image/x-xwd") - ("\\.au$" "audio/basic") - ("\\.mpg$" "video/mpeg") - ("\\.txt$" "text/plain") - ("\\.el$" "application/octet-stream" - ("type" ."emacs-lisp")) - ("\\.lsp$" "application/octet-stream" - ("type" "common-lisp")) - ("\\.tar\\.gz$" "application/octet-stream" - ("type" "tar+gzip")) - ("\\.tgz$" "application/octet-stream" - ("type" "tar+gzip")) - ("\\.tar\\.Z$" "application/octet-stream" - ("type" "tar+compress")) - ("\\.taz$" "application/octet-stream" - ("type" "tar+compress")) - ("\\.gz$" "application/octet-stream" - ("type" "gzip")) - ("\\.Z$" "application/octet-stream" - ("type" "compress")) - ("\\.lzh$" "application/octet-stream" - ("type" . "lha")) - ("\\.zip$" "application/zip") - ("\\.diffs?$" "text/plain" - ("type" . "patch")) - ("\\.patch$" "application/octet-stream" - ("type" "patch")) - ("\\.signature" "text/plain") - (".*" "application/octet-stream")) - "*Alist of regexps and MIME types.") +(require 'gnus-mailcap) (defvar mm-content-transfer-encoding-defaults - '(("text/.*" quoted-printable) - (".*" base64)) - "Alist of regexps that match MIME types and their encodings.") + '(("text/x-patch" 8bit) + ("text/.*" qp-or-base64) + ("message/rfc822" 8bit) + ("application/emacs-lisp" 8bit) + ("application/x-patch" 8bit) + (".*" qp-or-base64)) + "Alist of regexps that match MIME types and their encodings. +If the encoding is `qp-or-base64', then either quoted-printable +or base64 will be used, depending on what is more efficient.") (defun mm-insert-rfc822-headers (charset encoding) "Insert text/plain headers with CHARSET and ENCODING." @@ -93,13 +56,9 @@ (defun mm-default-file-encoding (file) "Return a default encoding for FILE." - (let ((types mm-mime-file-types) - type) - (catch 'found - (while (setq type (pop types)) - (when (string-match (car type) file) - (throw 'found (cdr type))) - (pop types))))) + (if (not (string-match "\\.[^.]+$" file)) + "application/octet-stream" + (mailcap-extension-to-mime (match-string 0 file)))) (defun mm-encode-content-transfer-encoding (encoding &optional type) (cond @@ -110,21 +69,17 @@ (goto-char (point-min)) (while (search-forward "\n" nil t) (replace-match "\r\n" t t))) - (condition-case () + (condition-case error (base64-encode-region (point-min) (point-max)) - (error nil))) + (error + (message "Error while decoding: %s" error) + nil))) ((memq encoding '(7bit 8bit binary)) ) ((null encoding) ) - ((eq encoding 'x-uuencode) - (condition-case () - (uudecode-encode-region (point-min) (point-max)) - (error nil))) ((functionp encoding) - (condition-case () - (funcall encoding (point-min) (point-max)) - (error nil))) + (ignore-errors (funcall encoding (point-min) (point-max)))) (t (message "Unknown encoding %s; defaulting to 8bit" encoding)))) @@ -135,13 +90,18 @@ The encoding used is returned." (encoding (or (and (listp type) (cadr (assq 'encoding type))) - (mm-content-transfer-encoding mime-type)))) + (mm-content-transfer-encoding mime-type))) + (bits (mm-body-7-or-8))) + ;; We force buffers that are 7bit to be unencoded, no matter + ;; what the preferred encoding is. + (when (eq bits '7bit) + (setq encoding bits)) (mm-encode-content-transfer-encoding encoding mime-type) encoding)) (defun mm-insert-headers (type encoding &optional file) "Insert headers for TYPE." - (insert "Content-Type: " (car type)) + (insert "Content-Type: " type) (when file (insert ";\n\tname=\"" (file-name-nondirectory file) "\"")) (insert "\n") @@ -153,14 +113,38 @@ The encoding used is returned." (insert "\n")) (defun mm-content-transfer-encoding (type) - "Return a CTE suitable for TYPE." + "Return a CTE suitable for TYPE to encode the current buffer." (let ((rules mm-content-transfer-encoding-defaults)) (catch 'found (while rules (when (string-match (caar rules) type) - (throw 'found (cadar rules))) + (throw 'found + (if (eq (cadar rules) 'qp-or-base64) + (mm-qp-or-base64) + (cadar rules)))) (pop rules))))) +(defun mm-qp-or-base64 () + (save-excursion + (save-restriction + (narrow-to-region (point-min) (min (+ (point-min) 1000) (point-max))) + (goto-char (point-min)) + (let ((8bit 0)) + (cond + ((not (featurep 'mule)) + (while (re-search-forward "[^\x00-\x7f]" nil t) + (incf 8bit))) + (t + ;; Mule version + (while (not (eobp)) + (skip-chars-forward "\0-\177") + (unless (eobp) + (forward-char 1) + (incf 8bit))))) + (if (> (/ (* 8bit 1.0) (buffer-size)) 0.166) + 'quoted-printable + 'base64))))) + (provide 'mm-encode) ;;; mm-encode.el ends here