- (cond
- ;; No encoding.
- ((null charsets)
- nil)
- ;; Too many charsets.
- ((> (length charsets) 1)
- charsets)
- ;; We encode.
- (t
- (let ((mime-charset
- (mm-mime-charset (car charsets) (point-min) (point-max)))
- start)
- (when (or t
- ;; We always decode.
- (not (mm-coding-system-equal
- mime-charset buffer-file-coding-system)))
- (while (not (eobp))
- (if (eq (char-charset (char-after)) 'ascii)
- (when start
- (save-restriction
- (narrow-to-region start (point))
- (mm-encode-coding-region start (point) mime-charset)
- (goto-char (point-max)))
- (setq start nil))
- (unless start
- (setq start (point))))
- (forward-char 1))
- (when start
- (mm-encode-coding-region start (point) mime-charset)
- (setq start nil)))
- mime-charset)))))))
-
-(defun mm-body-encoding ()
- "Return the encoding of the current buffer."
- (cond
- ((not (featurep 'mule))
- (if (save-excursion
- (goto-char (point-min))
- (re-search-forward mm-8bit-char-regexp nil t))
- '8bit
- '7bit))
- (t
- ;; Mule version
- (if (and (null (delq 'ascii
- (mm-find-charset-region (point-min) (point-max))))
- ;;!!!The following is necessary because the function
- ;;!!!above seems to return the wrong result under
- ;;!!!Emacs 20.3. Sometimes.
- (save-excursion
- (goto-char (point-min))
- (skip-chars-forward "\0-\177")
- (eobp)))
- '7bit
- '8bit))))
+ (goto-char (point-min))
+ (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
+ mm-hack-charsets)))
+ (cond
+ ;; No encoding.
+ ((null charsets)
+ nil)
+ ;; Too many charsets.
+ ((> (length charsets) 1)
+ charsets)
+ ;; We encode.
+ (t
+ (prog1
+ (setq charset (car charsets))
+ (mm-encode-coding-region (point-min) (point-max)
+ (mm-charset-to-coding-system charset))))
+ ))))))
+
+(defun mm-long-lines-p (length)
+ "Say whether any of the lines in the buffer is longer than LENGTH."
+ (save-excursion
+ (goto-char (point-min))
+ (end-of-line)
+ (while (and (not (eobp))
+ (not (> (current-column) length)))
+ (forward-line 1)
+ (end-of-line))
+ (and (> (current-column) length)
+ (current-column))))
+
+(defvar message-posting-charset)
+
+(defun mm-body-encoding (charset &optional encoding)
+ "Do Content-Transfer-Encoding and return the encoding of the current buffer."
+ (when (stringp encoding)
+ (setq encoding (intern (downcase encoding))))
+ (let ((bits (mm-body-7-or-8))
+ (longp (mm-long-lines-p 1000)))
+ (require 'message)
+ (cond
+ ((and (not longp)
+ (not (and mm-use-ultra-safe-encoding
+ (or (save-excursion (re-search-forward " $" nil t))
+ (save-excursion (re-search-forward "^From " nil t)))))
+ (eq bits '7bit))
+ bits)
+ ((and (not mm-use-ultra-safe-encoding)
+ (not longp)
+ (not (cdr (assq charset mm-body-charset-encoding-alist)))
+ (or (eq t (cdr message-posting-charset))
+ (memq charset (cdr message-posting-charset))
+ (eq charset mail-parse-charset)))
+ bits)
+ (t
+ (let ((encoding (or encoding
+ (cdr (assq charset mm-body-charset-encoding-alist))
+ (mm-qp-or-base64))))
+ (when mm-use-ultra-safe-encoding
+ (setq encoding (mm-safer-encoding encoding)))
+ (mm-encode-content-transfer-encoding encoding "text/plain")
+ encoding)))))
+
+(defun mm-body-7-or-8 ()
+ "Say whether the body is 7bit or 8bit."
+ (if (save-excursion
+ (goto-char (point-min))
+ (skip-chars-forward mm-7bit-chars)
+ (eobp))
+ '7bit
+ '8bit))