This commit was generated by cvs2svn to compensate for changes in r6565,
[elisp/gnus.git-] / lisp / nnheader.el
index cb6a44c..7ef13f2 100644 (file)
@@ -40,7 +40,6 @@
 (eval-when-compile (require 'cl))
 
 (require 'mail-utils)
-(require 'mm-util)
 
 (defvar nnheader-max-head-length 4096
   "*Max length of the head of articles.")
@@ -62,7 +61,8 @@ on your system, you could say something like:
  (autoload 'cancel-function-timers "timers")
  (autoload 'gnus-point-at-eol "gnus-util")
  (autoload 'gnus-delete-line "gnus-util")
- (autoload 'gnus-buffer-live-p "gnus-util"))
+ (autoload 'gnus-buffer-live-p "gnus-util")
+ (autoload 'gnus-encode-coding-string "gnus-ems"))
 
 ;;; Header access macros.
 
@@ -498,6 +498,52 @@ the line could be found."
     (erase-buffer))
   (current-buffer))
 
+(defmacro nnheader-temp-write (file &rest forms)
+  "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
+Return the value of FORMS.
+If FILE is nil, just evaluate FORMS and don't save anything.
+If FILE is t, return the buffer contents as a string."
+  (let ((temp-file (make-symbol "temp-file"))
+       (temp-buffer (make-symbol "temp-buffer"))
+       (temp-results (make-symbol "temp-results")))
+    `(save-excursion
+       (let* ((,temp-file ,file)
+             (default-major-mode 'fundamental-mode)
+             (,temp-buffer
+              (set-buffer
+               (get-buffer-create
+                (generate-new-buffer-name " *nnheader temp*"))))
+             ,temp-results)
+        (unwind-protect
+            (progn
+              (setq ,temp-results (progn ,@forms))
+              (cond
+               ;; Don't save anything.
+               ((null ,temp-file)
+                ,temp-results)
+               ;; Return the buffer contents.
+               ((eq ,temp-file t)
+                (set-buffer ,temp-buffer)
+                (buffer-string))
+               ;; Save a file.
+               (t
+                (set-buffer ,temp-buffer)
+                ;; Make sure the directory where this file is
+                ;; to be saved exists.
+                (when (not (file-directory-p
+                            (file-name-directory ,temp-file)))
+                  (make-directory (file-name-directory ,temp-file) t))
+                ;; Save the file.
+                (write-region (point-min) (point-max)
+                              ,temp-file nil 'nomesg)
+                ,temp-results)))
+          ;; Kill the buffer.
+          (when (buffer-name ,temp-buffer)
+            (kill-buffer ,temp-buffer)))))))
+
+(put 'nnheader-temp-write 'lisp-indent-function 1)
+(put 'nnheader-temp-write 'edebug-form-spec '(form body))
+
 (defvar jka-compr-compression-info-list)
 (defvar nnheader-numerical-files
   (if (boundp 'jka-compr-compression-info-list)
@@ -642,7 +688,7 @@ without formatting."
   (or (not (numberp gnus-verbose-backends))
       (<= level gnus-verbose-backends)))
 
-(defvar nnheader-pathname-coding-system 'binary
+(defvar nnheader-pathname-coding-system 'iso-8859-1
   "*Coding system for pathname.")
 
 (defun nnheader-group-pathname (group dir &optional file)
@@ -654,7 +700,7 @@ without formatting."
         (concat dir group "/")
        ;; If not, we translate dots into slashes.
        (concat dir
-              (mm-encode-coding-string
+              (gnus-encode-coding-string
                (nnheader-replace-chars-in-string group ?. ?/)
                nnheader-pathname-coding-system)
               "/")))
@@ -727,20 +773,20 @@ find-file-hooks, etc.
        (auto-mode-alist (nnheader-auto-mode-alist))
        (default-major-mode 'fundamental-mode)
        (enable-local-variables nil)
-        (after-insert-file-functions nil)
-       (find-file-hooks nil)
-       (coding-system-for-read nnheader-file-coding-system))
-    (insert-file-contents filename visit beg end replace)))
+       (after-insert-file-functions nil)
+       (find-file-hooks nil))
+    (insert-file-contents-as-coding-system
+     nnheader-file-coding-system filename visit beg end replace)))
 
 (defun nnheader-find-file-noselect (&rest args)
   (let ((format-alist nil)
        (auto-mode-alist (nnheader-auto-mode-alist))
        (default-major-mode 'fundamental-mode)
        (enable-local-variables nil)
-        (after-insert-file-functions nil)
-       (find-file-hooks nil)
-       (coding-system-for-read nnheader-file-coding-system))
-    (apply 'find-file-noselect args)))
+       (after-insert-file-functions nil)
+       (find-file-hooks nil))
+    (apply 'find-file-noselect-as-coding-system
+          nnheader-file-coding-system args)))
 
 (defun nnheader-auto-mode-alist ()
   "Return an `auto-mode-alist' with only the .gz (etc) thingies."
@@ -810,6 +856,23 @@ find-file-hooks, etc.
 (fset 'nnheader-cancel-timer 'cancel-timer)
 (fset 'nnheader-cancel-function-timers 'cancel-function-timers)
 
+(defun nnheader-Y-or-n-p (prompt)
+  "Ask user a \"Y/n\" question. Return t if answer is neither \"n\", \"N\" nor \"C-g\"."
+  (let ((cursor-in-echo-area t)
+       (echo-keystrokes 0)
+       (inhibit-quit t)
+       ans)
+    (let (message-log-max)
+      (while (not (memq ans '(?\  ?N ?Y ?\C-g ?\e ?\n ?\r ?n ?y)))
+       (message "%s(Y/n) " prompt)
+       (setq ans (read-char-exclusive))))
+    (if (memq ans '(?\C-g ?N ?n))
+       (progn
+         (message "%s(Y/n) No" prompt)
+         nil)
+      (message "%s(Y/n) Yes" prompt)
+      t)))
+
 (when (string-match "XEmacs\\|Lucid" emacs-version)
   (require 'nnheaderxm))