mm-mime-mule-charset-alist)
nil t))))
(subst-char-in-string
- . (lambda (from to string &optional inplace) ;; stolen (and renamed) from nnheader.el
+ . (lambda (from to string &optional inplace)
+ ;; stolen (and renamed) from nnheader.el
"Replace characters in STRING from FROM to TO.
Unless optional argument INPLACE is non-nil, return a new string."
(let ((string (if inplace string (copy-sequence string)))
(replace-regexp-in-string regexp rep string nil literal)))
(string-as-unibyte . identity)
(string-make-unibyte . identity)
+ ;; string-as-multibyte often doesn't really do what you think it does.
+ ;; Example:
+ ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
+ ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
+ ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
+ ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
+ ;; but
+ ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
+ ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
+ ;; Better use string-to-multibyte or encode-coding-string.
+ ;; If you really need string-as-multibyte somewhere it's usually
+ ;; because you're using the internal emacs-mule representation (maybe
+ ;; because you're using string-as-unibyte somewhere), which is
+ ;; generally a problem in itself.
+ ;; Here is an approximate equivalence table to help think about it:
+ ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
+ ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
+ ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
(string-as-multibyte . identity)
(string-to-multibyte
. (lambda (string)
If CS is available, return CS itself in Emacs, and return a coding
system object in XEmacs."
(if (fboundp 'find-coding-system)
- (find-coding-system cs)
+ (and cs (find-coding-system cs))
(if (fboundp 'coding-system-p)
(when (coding-system-p cs)
cs)
(defmacro mm-xemacs-find-mime-charset (begin end)
(when (featurep 'xemacs)
- `(mm-xemacs-find-mime-charset-1 ,begin ,end)))
+ `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
(defun mm-find-mime-charset-region (b e &optional hack-charsets)
"Return the MIME charsets needed to encode the region between B and E.
`find-file-hooks', etc.
If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
This function ensures that none of these modifications will take place."
- (let ((format-alist nil)
- (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
- (default-major-mode 'fundamental-mode)
- (enable-local-variables nil)
- (after-insert-file-functions nil)
- (enable-local-eval nil)
- (find-file-hooks nil)
- (inhibit-file-name-operation (if inhibit
- 'insert-file-contents
- inhibit-file-name-operation))
- (inhibit-file-name-handlers
- (if inhibit
- (append mm-inhibit-file-name-handlers
- inhibit-file-name-handlers)
- inhibit-file-name-handlers)))
- (insert-file-contents filename visit beg end replace)))
+ (let* ((format-alist nil)
+ (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
+ (default-major-mode 'fundamental-mode)
+ (enable-local-variables nil)
+ (after-insert-file-functions nil)
+ (enable-local-eval nil)
+ (inhibit-file-name-operation (if inhibit
+ 'insert-file-contents
+ inhibit-file-name-operation))
+ (inhibit-file-name-handlers
+ (if inhibit
+ (append mm-inhibit-file-name-handlers
+ inhibit-file-name-handlers)
+ inhibit-file-name-handlers))
+ (ffh (if (boundp 'find-file-hook)
+ 'find-file-hook
+ 'find-file-hooks))
+ (val (symbol-value ffh)))
+ (set ffh nil)
+ (unwind-protect
+ (insert-file-contents filename visit beg end replace)
+ (set ffh val))))
(defun mm-append-to-file (start end filename &optional codesys inhibit)
"Append the contents of the region to the end of file FILENAME.
(file-directory-p
(setq dir (concat (file-name-directory
(directory-file-name path))
- "etc/" (or package "gnus/")))))
+ "etc/images/" (or package "gnus/")))))
(push dir result))
(push path result))))
(defun mm-detect-coding-region (start end)
"Like `detect-coding-region' except returning the best one."
(let ((coding-systems
- (detect-coding-region (point) (point-max))))
+ (detect-coding-region start end)))
(or (car-safe coding-systems)
coding-systems)))
(defun mm-detect-coding-region (start end)
"Return the MIME charset corresponding to CODING-SYSTEM.
To make this function work with XEmacs, the APEL package is required."
(when coding-system
- (or (coding-system-get coding-system :mime-charset)
- (coding-system-get coding-system 'mime-charset)
+ (or (and (fboundp 'coding-system-get)
+ (or (coding-system-get coding-system :mime-charset)
+ (coding-system-get coding-system 'mime-charset)))
(and (featurep 'xemacs)
(or (and (fboundp 'coding-system-to-mime-charset)
(not (eq (symbol-function 'coding-system-to-mime-charset)
(fboundp 'coding-system-to-mime-charset)))
(coding-system-to-mime-charset coding-system)))))
-(defun mm-decompress-buffer (filename &optional inplace)
- "Decompress buffer's contents according to the extension of FILENAME.
-If INPLACE is nil, return a decompressed string or nil, and the buffer
-will not be modified. Otherwise, replace the buffer's contents with
-the decompressed one. Decompression is done only when the extension
-is \".gz\" or \".bz2\" which does not follow \".tar\"."
- (let ((decomp (cond ((or (not filename)
- (string-match "\\.tar\\.[^.]+\\'" filename))
- nil)
- ((string-match "\\.gz\\'" filename)
- '("gzip" "-c" "-d" "-q"))
- ((string-match "\\.bz2\\'" filename)
- '("bzip2" "-d")))))
- (when decomp
- (let ((coding-system-for-read mm-binary-coding-system)
- (coding-system-for-write mm-binary-coding-system)
- cur mod)
- (if inplace
- (prog1
- nil
- (setq cur (buffer-string)
- mod (buffer-modified-p))
- (condition-case nil
- (apply 'call-process-region (point-min) (point-max)
- (car decomp) t t nil (cdr decomp))
- (error
- (erase-buffer)
- (insert cur)
- (set-buffer-modified-p mod))))
- (setq cur (current-buffer))
- (mm-with-unibyte-buffer
+(eval-when-compile
+ (require 'jka-compr))
+
+(defun mm-decompress-buffer (filename &optional inplace force)
+ "Decompress buffer's contents, depending on jka-compr.
+Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
+agrees with `jka-compr-compression-info-list', decompression is done.
+Signal an error if FORCE is neither nil nor t and compressed data are
+not decompressed because `auto-compression-mode' is disabled.
+If INPLACE is nil, return decompressed data or nil without modifying
+the buffer. Otherwise, replace the buffer's contents with the
+decompressed data. The buffer's multibyteness must be turned off."
+ (when (and filename
+ (if force
+ (prog1 t (require 'jka-compr))
+ (and (fboundp 'jka-compr-installed-p)
+ (jka-compr-installed-p))))
+ (let ((info (jka-compr-get-compression-info filename)))
+ (when info
+ (unless (or (memq force (list nil t))
+ (jka-compr-installed-p))
+ (error ""))
+ (let ((prog (jka-compr-info-uncompress-program info))
+ (args (jka-compr-info-uncompress-args info))
+ (msg (format "%s %s..."
+ (jka-compr-info-uncompress-message info)
+ filename))
+ (err-file (jka-compr-make-temp-name))
+ (cur (current-buffer))
+ (coding-system-for-read mm-binary-coding-system)
+ (coding-system-for-write mm-binary-coding-system)
+ retval err-msg)
+ (message "%s" msg)
+ (with-temp-buffer
(insert-buffer-substring cur)
- (condition-case nil
+ (condition-case err
(progn
- (apply 'call-process-region (point-min) (point-max)
- (car decomp) t t nil (cdr decomp))
- (buffer-string))
- (error nil))))))))
+ (unless (memq (apply 'call-process-region
+ (point-min) (point-max)
+ prog t (list t err-file) nil args)
+ jka-compr-acceptable-retval-list)
+ (erase-buffer)
+ (insert (mapconcat
+ 'identity
+ (delete "" (split-string
+ (prog2
+ (insert-file-contents err-file)
+ (buffer-string)
+ (erase-buffer))))
+ " ")
+ "\n")
+ (setq err-msg
+ (format "Error while executing \"%s %s < %s\""
+ prog (mapconcat 'identity args " ")
+ filename)))
+ (setq retval (buffer-string)))
+ (error
+ (setq err-msg (error-message-string err)))))
+ (when (file-exists-p err-file)
+ (ignore-errors (jka-compr-delete-temp-file err-file)))
+ (when inplace
+ (unless err-msg
+ (delete-region (point-min) (point-max))
+ (insert retval))
+ (setq retval nil))
+ (message "%s" (or err-msg (concat msg "done")))
+ retval)))))
(eval-when-compile
(unless (fboundp 'coding-system-name)
"Find coding system used to decode the contents of the current buffer.
This function looks for the coding system magic cookie or examines the
coding system specified by `file-coding-system-alist' being associated
-with FILENAME which defaults to `buffer-file-name'."
+with FILENAME which defaults to `buffer-file-name'. Data compressed by
+gzip, bzip2, etc. are allowed."
(unless filename
(setq filename buffer-file-name))
(save-excursion
- (let ((decomp (mm-decompress-buffer filename)))
+ (let ((decomp (mm-decompress-buffer filename nil t)))
(when decomp
(set-buffer (let (default-enable-multibyte-characters)
(generate-new-buffer " *temp*")))