Fix the last change.
[elisp/semi.git] / mime-view.el
index ebf3935..f6f7b8c 100644 (file)
@@ -359,34 +359,37 @@ mother-buffer."
   
 (defvar mime-acting-situation-example-list nil)
 (defvar mime-acting-situation-example-list-max-size 16)
+(defvar mime-situation-examples-file-coding-system nil)
 
 (defun mime-save-situation-examples ()
   (if (or mime-preview-situation-example-list
          mime-acting-situation-example-list)
-      (let* ((file mime-situation-examples-file)
-            (buffer (get-buffer-create " *mime-example*")))
-       (unwind-protect
-           (save-excursion
-             (set-buffer buffer)
-             (setq buffer-file-name file)
-             (erase-buffer)
-             (insert ";;; " (file-name-nondirectory file) "\n")
-             (insert "\n;; This file is generated automatically by "
-                     mime-view-version "\n\n")
-             (insert ";;; Code:\n\n")
-             (if mime-preview-situation-example-list
-                 (pp `(setq mime-preview-situation-example-list
-                            ',mime-preview-situation-example-list)
-                     (current-buffer)))
-             (if mime-acting-situation-example-list
-                 (pp `(setq mime-acting-situation-example-list
-                            ',mime-acting-situation-example-list)
-                     (current-buffer)))
-             (insert "\n;;; "
-                     (file-name-nondirectory file)
-                     " ends here.\n")
-             (save-buffer))
-         (kill-buffer buffer)))))
+      (let ((file mime-situation-examples-file))
+       (with-temp-buffer
+         (insert ";;; " (file-name-nondirectory file) "\n")
+         (insert "\n;; This file is generated automatically by "
+                 mime-view-version "\n\n")
+         (insert ";;; Code:\n\n")
+         (if mime-preview-situation-example-list
+             (pp `(setq mime-preview-situation-example-list
+                        ',mime-preview-situation-example-list)
+                 (current-buffer)))
+         (if mime-acting-situation-example-list
+             (pp `(setq mime-acting-situation-example-list
+                        ',mime-acting-situation-example-list)
+                 (current-buffer)))
+         (insert "\n;;; "
+                 (file-name-nondirectory file)
+                 " ends here.\n")
+         (static-cond
+          ((boundp 'buffer-file-coding-system)
+           (setq buffer-file-coding-system
+                 mime-situation-examples-file-coding-system))
+          ((boundp 'file-coding-system)
+           (setq file-coding-system
+                 mime-situation-examples-file-coding-system)))
+         (setq buffer-file-name file)
+         (save-buffer)))))
 
 (add-hook 'kill-emacs-hook 'mime-save-situation-examples)
 
@@ -646,6 +649,12 @@ Each elements are regexp of field-name.")
  '((body . visible)
    (body-presentation-method . mime-display-text/plain)))
 
+(defvar mime-preview-fill-flowed-text
+  (module-installed-p 'flow-fill)
+  "If non-nil, fill RFC2646 \"flowed\" text.")
+
+(autoload 'fill-flowed "flow-fill")
+
 (ctree-set-calist-strictly
  'mime-preview-condition
  '((type . nil)
@@ -719,12 +728,19 @@ Each elements are regexp of field-name.")
 (defun mime-display-text/plain (entity situation)
   (save-restriction
     (narrow-to-region (point-max)(point-max))
-    (mime-insert-text-content entity)
+    (condition-case nil
+       (mime-insert-text-content entity)
+      (error (progn
+              (message "Can't decode current entity.")
+              (sit-for 1))))
     (run-hooks 'mime-text-decode-hook)
     (goto-char (point-max))
     (if (not (eq (char-after (1- (point))) ?\n))
        (insert "\n")
       )
+    (if (and mime-preview-fill-flowed-text
+            (equal (cdr (assoc "format" situation)) "flowed"))
+       (fill-flowed))
     (mime-add-url-buttons)
     (run-hooks 'mime-display-text/plain-hook)
     ))
@@ -749,7 +765,6 @@ Each elements are regexp of field-name.")
       (enriched-decode beg (point-max))
       )))
 
-
 (defvar mime-view-announcement-for-message/partial
   (if (and (>= emacs-major-version 19) window-system)
       "\
@@ -1136,12 +1151,28 @@ MEDIA-TYPE must be (TYPE . SUBTYPE), TYPE or t.  t means default."
       "e"        (function mime-preview-extract-current-entity))
     (define-key mime-view-mode-map
       "\C-c\C-p" (function mime-preview-print-current-entity))
+
     (define-key mime-view-mode-map
       "\C-c\C-t\C-f" (function mime-preview-toggle-header))
     (define-key mime-view-mode-map
       "\C-c\C-th" (function mime-preview-toggle-header))
     (define-key mime-view-mode-map
       "\C-c\C-t\C-c" (function mime-preview-toggle-content))
+
+    (define-key mime-view-mode-map
+      "\C-c\C-v\C-f" (function mime-preview-show-header))
+    (define-key mime-view-mode-map
+      "\C-c\C-vh" (function mime-preview-show-header))
+    (define-key mime-view-mode-map
+      "\C-c\C-v\C-c" (function mime-preview-show-content))
+
+    (define-key mime-view-mode-map
+      "\C-c\C-d\C-f" (function mime-preview-hide-header))
+    (define-key mime-view-mode-map
+      "\C-c\C-dh" (function mime-preview-hide-header))
+    (define-key mime-view-mode-map
+      "\C-c\C-d\C-c" (function mime-preview-hide-content))
+
     (define-key mime-view-mode-map
       "a"        (function mime-preview-follow-current-entity))
     (define-key mime-view-mode-map
@@ -1340,11 +1371,10 @@ button-2        Move to point under the mouse cursor
          )
        (setq mime-message-structure (mime-open-entity type raw-buffer))
        (or (mime-entity-content-type mime-message-structure)
-           (mime-entity-set-content-type-internal
-            mime-message-structure ctl))
+           (mime-entity-set-content-type mime-message-structure ctl))
        )
       (or (mime-entity-encoding mime-message-structure)
-         (mime-entity-set-encoding-internal mime-message-structure encoding))
+         (mime-entity-set-encoding mime-message-structure encoding))
       ))
   (mime-display-message mime-message-structure preview-buffer
                        mother default-keymap-or-function)
@@ -1683,20 +1713,26 @@ If LINES is negative, scroll up LINES lines."
 ;;; @@ display
 ;;;
 
-(defun mime-preview-toggle-header ()
-  (interactive)
+(defun mime-preview-toggle-display (type &optional display)
   (let ((situation (mime-preview-find-boundary-info))
+       (sym (intern (concat "*" (symbol-name type))))
        entity p-beg p-end)
     (setq p-beg (aref situation 0)
          p-end (aref situation 1)
          entity (aref situation 2)
          situation (get-text-property p-beg 'mime-view-situation))
-    (let ((cell (assq '*header situation)))
-      (if (null cell)
-         (setq cell (assq 'header situation)))
-      (if (eq (cdr cell) 'visible)
-         (setq situation (put-alist '*header 'invisible situation))
-       (setq situation (put-alist '*header 'visible situation))))
+    (cond ((eq display 'invisible)
+          (setq display nil))
+         (display)
+         (t
+          (setq display
+                (eq (cdr (or (assq sym situation)
+                             (assq type situation)))
+                    'invisible))))
+    (setq situation (put-alist sym (if display
+                                      'visible
+                                    'invisible)
+                              situation))
     (save-excursion
       (let ((inhibit-read-only t))
        (delete-region p-beg p-end)
@@ -1707,30 +1743,29 @@ If LINES is negative, scroll up LINES lines."
        (add-to-list 'mime-preview-situation-example-list
                     (cons situation 0))))))
 
-(defun mime-preview-toggle-content ()
+(defun mime-preview-toggle-header (&optional force-visible)
+  (interactive "P")
+  (mime-preview-toggle-display 'header force-visible))
+
+(defun mime-preview-toggle-content (&optional force-visible)
+  (interactive "P")
+  (mime-preview-toggle-display 'body force-visible))
+
+(defun mime-preview-show-header ()
   (interactive)
-  (let ((situation (mime-preview-find-boundary-info))
-       entity p-beg p-end)
-    (setq p-beg (aref situation 0)
-         p-end (aref situation 1)
-         entity (aref situation 2)
-         situation (get-text-property p-beg 'mime-view-situation))
-    (let ((cell (assq '*body situation)))
-      (if (null cell)
-         (setq cell (assq 'body situation)))
-      (if (eq (cdr cell) 'visible)
-         (setq situation (put-alist '*body 'invisible situation))
-       (setq situation (put-alist '*body 'visible situation))))
-    (save-excursion
-      (let ((inhibit-read-only t))
-       (delete-region p-beg p-end)
-       (mime-display-entity entity situation)
-       ))
-    (let ((ret (assoc situation mime-preview-situation-example-list)))
-      (if ret
-         (setcdr ret (1+ (cdr ret)))
-       (add-to-list 'mime-preview-situation-example-list
-                    (cons situation 0))))))
+  (mime-preview-toggle-display 'header 'visible))
+
+(defun mime-preview-show-content ()
+  (interactive)
+  (mime-preview-toggle-display 'body 'visible))
+
+(defun mime-preview-hide-header ()
+  (interactive)
+  (mime-preview-toggle-display 'header 'invisible))
+
+(defun mime-preview-hide-content ()
+  (interactive)
+  (mime-preview-toggle-display 'body 'invisible))
 
     
 ;;; @@ quitting
@@ -1758,44 +1793,43 @@ It calls function registered in variable
 
 (provide 'mime-view)
 
-(let* ((file mime-situation-examples-file)
-       (buffer (get-buffer-create " *mime-example*")))
+(let ((file mime-situation-examples-file))
   (if (file-readable-p file)
-      (unwind-protect
-         (save-excursion
-           (set-buffer buffer)
-           (erase-buffer)
-           (insert-file-contents file)
-           (eval-buffer)
-           ;; format check
-           (condition-case nil
-               (let ((i 0))
-                 (while (and (> (length mime-preview-situation-example-list)
-                                mime-preview-situation-example-list-max-size)
-                             (< i 16))
-                   (setq mime-preview-situation-example-list
-                         (mime-reduce-situation-examples
-                          mime-preview-situation-example-list))
-                   (setq i (1+ i))
-                   ))
-             (error (setq mime-preview-situation-example-list nil)))
-            ;; (let ((rest mime-preview-situation-example-list))
-            ;;   (while rest
-            ;;     (ctree-set-calist-strictly 'mime-preview-condition
-            ;;                                (caar rest))
-            ;;     (setq rest (cdr rest))))
-           (condition-case nil
-               (let ((i 0))
-                 (while (and (> (length mime-acting-situation-example-list)
-                                mime-acting-situation-example-list-max-size)
-                             (< i 16))
-                   (setq mime-acting-situation-example-list
-                         (mime-reduce-situation-examples
-                          mime-acting-situation-example-list))
-                   (setq i (1+ i))
-                   ))
-             (error (setq mime-acting-situation-example-list nil)))
-           )
-       (kill-buffer buffer))))
+      (with-temp-buffer
+       (insert-file-contents file)
+       (setq mime-situation-examples-file-coding-system
+             (static-cond
+              ((boundp 'buffer-file-coding-system)
+               (symbol-value 'buffer-file-coding-system))
+              ((boundp 'file-coding-system)
+               (symbol-value 'file-coding-system))
+              (t nil)))
+       (eval-buffer)
+       ;; format check
+       (condition-case nil
+           (let ((i 0))
+             (while (and (> (length mime-preview-situation-example-list)
+                            mime-preview-situation-example-list-max-size)
+                         (< i 16))
+               (setq mime-preview-situation-example-list
+                     (mime-reduce-situation-examples
+                      mime-preview-situation-example-list))
+               (setq i (1+ i))))
+         (error (setq mime-preview-situation-example-list nil)))
+       ;; (let ((rest mime-preview-situation-example-list))
+       ;;   (while rest
+       ;;     (ctree-set-calist-strictly 'mime-preview-condition
+       ;;                                (caar rest))
+       ;;     (setq rest (cdr rest))))
+       (condition-case nil
+           (let ((i 0))
+             (while (and (> (length mime-acting-situation-example-list)
+                            mime-acting-situation-example-list-max-size)
+                         (< i 16))
+               (setq mime-acting-situation-example-list
+                     (mime-reduce-situation-examples
+                      mime-acting-situation-example-list))
+               (setq i (1+ i))))
+         (error (setq mime-acting-situation-example-list nil))))))
 
 ;;; mime-view.el ends here