diff lisp/code-files.el @ 371:cc15677e0335 r21-2b1

Import from CVS: tag r21-2b1
author cvs
date Mon, 13 Aug 2007 11:03:08 +0200
parents a4f53d9b3154
children 8626e4521993
line wrap: on
line diff
--- a/lisp/code-files.el	Mon Aug 13 11:01:58 2007 +0200
+++ b/lisp/code-files.el	Mon Aug 13 11:03:08 2007 +0200
@@ -71,13 +71,7 @@
     ;; ("\\.\\(el\\|emacs\\|info\\(-[0-9]+\\)?\\|texi\\)$" . iso-2022-8)
     ;; ("\\(ChangeLog\\|CHANGES-beta\\)$" . iso-2022-8)
     ("\\.\\(gz\\|Z\\)$" . binary)
-
-    ;; This idea is totally broken, and the code didn't work anyway.
-    ;; Mailboxes should be decoded by mail clients, who actually know
-    ;; how to deal with them.  Otherwise, their contents should be
-    ;; treated as `binary'.
-    ;("/spool/mail/.*$" . convert-mbox-coding-system)
-    )
+    ("/spool/mail/.*$" . convert-mbox-coding-system))
   "Alist to decide a coding system to use for a file I/O operation.
 The format is ((PATTERN . VAL) ...),
 where PATTERN is a regular expression matching a file name,
@@ -200,12 +194,22 @@
 	    ((find-coding-system codesys))
 	    ))))
 
-;; This is completely broken, not only in implementation (does not
-;; understand MIME), but in concept -- such high-level decoding should
-;; be done by mail readers, not by IO code!
-
-;(defun convert-mbox-coding-system (filename visit start end)
-;...
+(defun convert-mbox-coding-system (filename visit start end)
+  "Decoding function for Unix mailboxes.
+Does separate detection and decoding on each message, since each
+message might be in a different encoding."
+  (let ((buffer-read-only nil))
+    (save-restriction
+      (narrow-to-region start end)
+      (goto-char (point-min))
+      (while (not (eobp))
+	(let ((start (point))
+	      end)
+	  (forward-char 1)
+	  (if (re-search-forward "^From" nil 'move)
+	      (beginning-of-line))
+	  (setq end (point))
+	  (decode-coding-region start end 'undecided))))))
 
 (defun find-coding-system-magic-cookie ()
   "Look for the coding-system magic cookie in the current buffer.\n"
@@ -373,6 +377,9 @@
 the whole thing because (1) it preserves some marker positions
 and (2) it puts less data in the undo list.
 
+NOTE: When Mule support is enabled, the REPLACE argument is
+currently ignored.
+
 The coding system used for decoding the file is determined as follows:
 
 1. `coding-system-for-read', if non-nil.
@@ -401,8 +408,9 @@
 		 ;; #1.
 		 coding-system-for-read
 		 ;; #2.
-		 (run-special-hook-with-args 'insert-file-contents-pre-hook
-					     filename visit)
+		 (run-hook-with-args-until-success
+		  'insert-file-contents-pre-hook
+		  filename visit)
 		 ;; #3.
 		 (find-file-coding-system-for-read-from-filename filename)
 		 ;; #4.
@@ -445,8 +453,8 @@
 		  (set-buffer-modified-p nil)))))
       (setcar (cdr return-val) (- (marker-position endmark) (point))))
     ;; now finally set the buffer's `buffer-file-coding-system'.
-    (if (run-special-hook-with-args 'insert-file-contents-post-hook
-				    filename visit return-val)
+    (if (run-hook-with-args-until-success 'insert-file-contents-post-hook
+					  filename visit return-val)
 	nil
       (if (local-variable-p 'buffer-file-coding-system (current-buffer))
 	  ;; if buffer-file-coding-system is already local, just
@@ -514,7 +522,7 @@
   (interactive "r\nFWrite region to file: \ni\ni\ni\nZCoding-system: ")
   (setq coding-system
 	(or coding-system-for-write
-	    (run-special-hook-with-args
+	    (run-hook-with-args-until-success
 	     'write-region-pre-hook start end filename append visit lockname)
 	    coding-system
 	    buffer-file-coding-system