(mm-display-external): Use nametemplate (defined in RFC1524) if it is in
authorKatsumi Yamaoka <yamaoka@jpl.org>
Wed, 7 Dec 2005 07:35:34 +0000 (07:35 +0000)
committerKatsumi Yamaoka <yamaoka@jpl.org>
Wed, 7 Dec 2005 07:35:34 +0000 (07:35 +0000)
 mailcap or add a suffix according to mailcap-mime-extensions when generating
 a temp filename;
 postpone deleting a temp file for 2 seconds for some wrappers, shell scripts,
 and so on, which might exit right after having started a viewer command as a
 background job.

lisp/ChangeLog
lisp/mm-decode.el

index caba3de..31789a7 100644 (file)
@@ -1,3 +1,12 @@
+2005-12-07  Katsumi Yamaoka  <yamaoka@jpl.org>
+
+       * mm-decode.el (mm-display-external): Use nametemplate (defined in
+       RFC1524) if it is in mailcap or add a suffix according to
+       mailcap-mime-extensions when generating a temp filename; postpone
+       deleting a temp file for 2 seconds for some wrappers, shell
+       scripts, and so on, which might exit right after having started a
+       viewer command as a background job.
+
 2005-12-06  Reiner Steib  <Reiner.Steib@gmx.de>
 
        * nntp.el (nntp-marks-directory): Fix custom group.
index ec60b54..fea1c95 100644 (file)
@@ -755,7 +755,19 @@ external if displayed external."
                          (gnus-map-function mm-file-name-rewrite-functions
                                             (file-name-nondirectory filename))
                          dir))
-           (setq file (mm-make-temp-file (expand-file-name "mm." dir))))
+           (setq file (mm-make-temp-file (expand-file-name "mm." dir)))
+           (let ((newname
+                  ;; Use nametemplate (defined in RFC1524) if it is
+                  ;; specified in mailcap.
+                  (if (assoc "nametemplate" mime-info)
+                      (format (assoc "nametemplate" mime-info) file)
+                    ;; Add a suffix according to `mailcap-mime-extensions'.
+                    (concat file (car (rassoc (mm-handle-media-type handle)
+                                              mailcap-mime-extensions))))))
+             (unless (string-equal file newname)
+               (when (file-exists-p file)
+                 (rename-file file newname))
+               (setq file newname))))
          (let ((coding-system-for-write mm-binary-coding-system))
            (write-region (point-min) (point-max) file nil 'nomesg))
          (message "Viewing with %s" method)
@@ -813,6 +825,9 @@ external if displayed external."
                   (ignore-errors (kill-buffer buffer))))))
            'inline)
           (t
+           ;; Deleting the temp file should be postponed for some wrappers,
+           ;; shell scripts, and so on, which might exit right after having
+           ;; started a viewer command as a background job.
            (let ((command (mm-mailcap-command
                            method file (mm-handle-type handle))))
              (unwind-protect
@@ -824,24 +839,38 @@ external if displayed external."
                                   shell-command-switch command)
                    (set-process-sentinel
                     (get-buffer-process buffer)
-                    `(lambda (process state)
-                       (when (eq 'exit (process-status process))
-                         ;; Don't use `ignore-errors'.
-                         (condition-case nil
-                             (delete-file ,file)
-                           (error))
-                         (condition-case nil
-                             (delete-directory ,(file-name-directory file))
-                           (error))
-                         (condition-case nil
-                             (kill-buffer ,buffer)
-                           (error))
-                         (condition-case nil
-                             ,(macroexpand (list 'mm-handle-set-undisplayer
-                                                 (list 'quote handle)
-                                                 nil))
-                           (error))
-                         (message "Displaying %s...done" ,command)))))
+                    (lexical-let ;; Don't use `let'.
+                        ;; Function used to remove temp file and directory.
+                        ((fn `(lambda nil
+                                ;; Don't use `ignore-errors'.
+                                (condition-case nil
+                                    (delete-file ,file)
+                                  (error))
+                                (condition-case nil
+                                    (delete-directory
+                                     ,(file-name-directory file))
+                                  (error))))
+                         ;; Form uses to kill the process buffer and
+                         ;; remove the undisplayer.
+                         (fm `(progn
+                                (kill-buffer ,buffer)
+                                ,(macroexpand
+                                  (list 'mm-handle-set-undisplayer
+                                        (list 'quote handle)
+                                        nil))))
+                         ;; Message to be issued when the process exits.
+                         (done (format "Displaying %s...done" command))
+                         ;; In particular, the timer object (which is
+                         ;; a vector in Emacs but is a list in XEmacs)
+                         ;; requires that it is lexically scoped.
+                         (timer (run-at-time 2.0 nil 'ignore)))
+                      (lambda (process state)
+                        (when (eq 'exit (process-status process))
+                          (if (memq timer timer-list)
+                              (timer-set-function timer fn)
+                            (funcall fn))
+                          (ignore-errors (eval fm))
+                          (message "%s" done))))))
                (mm-handle-set-external-undisplayer
                 handle (cons file buffer)))
              (message "Displaying %s..." command))