-(defun mm-insert-rfc822-headers (charset encoding)
- "Insert text/plain headers with CHARSET and ENCODING."
- (insert "MIME-Version: 1.0\n")
- (insert "Content-Type: text/plain; charset=\""
- (downcase (symbol-name charset)) "\"\n")
- (insert "Content-Transfer-Encoding: "
- (downcase (symbol-name encoding)) "\n"))
-
-(defun mm-content-type-charset (header)
- "Return the charset parameter from HEADER."
- (when (string-match "charset *= *\"? *\\([-0-9a-zA-Z_]+\\)\"? *$" header)
- (intern (downcase (match-string 1 header)))))
+(defsubst mm-disable-multibyte ()
+ "Disable multibyte in the current buffer."
+ (when (fboundp 'set-buffer-multibyte)
+ (set-buffer-multibyte nil)))
+
+(defun mm-mime-charset (charset b e)
+ (if (fboundp 'coding-system-get)
+ (or
+ (and
+ mm-default-coding-system
+ (let ((safe (coding-system-get mm-default-coding-system
+ 'safe-charsets)))
+ (or (eq safe t) (memq charset safe)))
+ (coding-system-get mm-default-coding-system 'mime-charset))
+ (coding-system-get
+ (get-charset-property charset 'prefered-coding-system)
+ 'mime-charset)
+ (car (memq charset (find-coding-systems-region
+ (point-min) (point-max)))))
+ (mm-mule-charset-to-mime-charset charset)))
+
+(defsubst mm-multibyte-p ()
+ "Say whether multibyte is enabled."
+ (and (boundp 'enable-multibyte-characters)
+ enable-multibyte-characters))
+
+(defmacro mm-with-unibyte-buffer (&rest forms)
+ "Create a temporary buffer, and evaluate FORMS there like `progn'.
+See also `with-temp-file' and `with-output-to-string'."
+ (let ((temp-buffer (make-symbol "temp-buffer"))
+ (multibyte (make-symbol "multibyte")))
+ `(if (not (boundp 'enable-multibyte-characters))
+ (with-temp-buffer ,@forms)
+ (let ((,multibyte (default-value 'enable-multibyte-characters))
+ ,temp-buffer)
+ (unwind-protect
+ (progn
+ (setq-default enable-multibyte-characters nil)
+ (setq ,temp-buffer
+ (get-buffer-create (generate-new-buffer-name " *temp*")))
+ (unwind-protect
+ (with-current-buffer ,temp-buffer
+ (let ((buffer-file-coding-system 'binary))
+ ,@forms))
+ (and (buffer-name ,temp-buffer)
+ (kill-buffer ,temp-buffer))))
+ (setq-default enable-multibyte-characters ,multibyte))))))
+(put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
+(put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
+
+(defun mm-find-charset-region (b e)
+ "Return a list of charsets in the region."
+ (cond
+ ((and (boundp 'enable-multibyte-characters)
+ enable-multibyte-characters)
+ (find-charset-region b e))
+ ((not (boundp 'current-language-environment))
+ (save-excursion
+ (save-restriction
+ (narrow-to-region b e)
+ (goto-char (point-min))
+ (skip-chars-forward "\0-\177")
+ (if (eobp)
+ '(ascii)
+ ;;;!!!bogus
+ (list 'ascii 'latin-iso8859-1)))))
+ (t
+ ;; We are in a unibyte buffer, so we futz around a bit.
+ (save-excursion
+ (save-restriction
+ (narrow-to-region b e)
+ (goto-char (point-min))
+ (let ((entry (assoc (capitalize current-language-environment)
+ language-info-alist)))
+ (skip-chars-forward "\0-\177")
+ (if (eobp)
+ '(ascii)
+ (list 'ascii (car (last (assq 'charset entry)))))))))))