help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: DocView: process ps->pdf changed status to killed.


From: Tassilo Horn
Subject: Re: DocView: process ps->pdf changed status to killed.
Date: Tue, 21 Oct 2014 11:02:47 +0200
User-agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (gnu/linux)

Hi Pierre & Stefan,

you can stop debugging.  I've been able to reproduce the problem
locally.

The problem is that if your emacs doesn't satisfy the doc-view
requirements (PNG support, graphical frame, conversion utilities) but
you have pdf2text installed, then you're asked if you want to view the
document as plain text instead.  If you say yes, then doc-view will
start a process to do the conversion.

But right after starting that process, `doc-view-toggle-display' is
called which will kill all running doc-view processes (including the
conversion to TXT) and put the document's buffer back in its normal
editing mode.  That's why you've got the message that the process was
killed.

To solve that issue, I first tried excluding the processes responsible
to convert the document to text.  However, that didn't work because then
you get different errors because in the meantime the buffer is back to
ps-mode or fundamental-mode and all buffer-local variables that are used
by doc-view are gone.  I've tried to remember those by adding optional
arguments so several functions that could be used to override things
like `doc-view--buffer-file-name' but IMHO that caused too many changes
and complications for the conceptually simple problem we're facing.

So my second try (patch attached) was to just do the document-to-text
conversion synchronously in case (doc-view-mode-p type) is nil.  That
works fine now for PS and PDF documents.  It doesn't work for ODF and
DVI right now because `doc-view-doc->txt' currently assumes that in
those cases the PDF is already there, but that can be fixed easily.

So Stefan, is the attached patch ok, or do you have a better suggestion?

--8<---------------cut here---------------start------------->8---
=== modified file 'lisp/doc-view.el'
--- lisp/doc-view.el    2014-07-28 09:39:09 +0000
+++ lisp/doc-view.el    2014-10-21 08:58:30 +0000
@@ -886,19 +886,24 @@
                   (format ":%s" (car doc-view--current-converter-processes))))
         (funcall (process-get proc 'callback))))))
 
-(defun doc-view-start-process (name program args callback)
+(defun doc-view-start-process (name program args callback &optional sync)
   ;; Make sure the process is started in an existing directory, (rather than
   ;; some file-name-handler-managed dir, for example).
   (let* ((default-directory (or (unhandled-file-name-directory
                                  default-directory)
-                             (expand-file-name "~/")))
-         (proc (apply 'start-process name doc-view-conversion-buffer
-                      program args)))
-    (push proc doc-view--current-converter-processes)
-    (setq mode-line-process (list (format ":%s" proc)))
-    (set-process-sentinel proc 'doc-view-sentinel)
-    (process-put proc 'buffer   (current-buffer))
-    (process-put proc 'callback callback)))
+                             (expand-file-name "~/"))))
+    (if sync
+       (let ((stat (apply 'call-process program nil doc-view-conversion-buffer 
nil args)))
+         (if (= 0 stat)
+             (funcall callback)
+           (error "Program %s returned exit code %s." program stat)))
+      (let ((proc (apply 'start-process name doc-view-conversion-buffer
+                        program args)))
+       (push proc doc-view--current-converter-processes)
+       (setq mode-line-process (list (format ":%s" proc)))
+       (set-process-sentinel proc 'doc-view-sentinel)
+       (process-put proc 'buffer   (current-buffer))
+       (process-put proc 'callback callback)))))
 
 (defun doc-view-dvi->pdf (dvi pdf callback)
   "Convert DVI to PDF asynchronously and call CALLBACK when finished."
@@ -1039,12 +1044,18 @@
            (doc-view-pdf/ps->png pdf png)))))))
 
 (defun doc-view-pdf->txt (pdf txt callback)
-  "Convert PDF to TXT asynchronously and call CALLBACK when finished."
+  "Convert PDF to TXT and call CALLBACK when finished."
   (or (executable-find doc-view-pdftotext-program)
       (error "You need the `pdftotext' program to convert a PDF to text"))
   (doc-view-start-process "pdf->txt" doc-view-pdftotext-program
                           (list "-raw" pdf txt)
-                          callback))
+                          callback
+                         ;; If we cannot view the doc, do it
+                         ;; synchronously.  In this case, we are just
+                         ;; before switching to the editing mode, so
+                         ;; we need to wait until the conversion is
+                         ;; done.
+                         (not (doc-view-mode-p doc-view-doc-type))))
 
 (defun doc-view-current-cache-doc-pdf ()
   "Return the name of the doc.pdf in the current cache dir.
@@ -1075,7 +1086,7 @@
     (_ (error "DocView doesn't know what to do"))))
 
 (defun doc-view-ps->pdf (ps pdf callback)
-  "Convert PS to PDF asynchronously and call CALLBACK when finished."
+  "Convert PS to PDF and call CALLBACK when finished."
   (or (executable-find doc-view-ps2pdf-program)
       (error "You need the `ps2pdf' program to convert PS to PDF"))
   (doc-view-start-process "ps->pdf" doc-view-ps2pdf-program
@@ -1085,7 +1096,11 @@
                            "-dSAFER"
                            ;; in-file and out-file
                            ps pdf)
-                          callback))
+                          callback
+                         ;; If we cannot view the doc, do it
+                         ;; synchronously.  In this case, this
+                         ;; conversion is followed by pdftotext.
+                         (not (doc-view-mode-p doc-view-doc-type))))
 
 (defun doc-view-active-pages ()
   (let ((pages ()))
@@ -1616,7 +1631,7 @@
   "Figure out the current document type (`doc-view-doc-type')."
   (let ((name-types
         (when buffer-file-name
-          (cdr (assoc-ignore-case
+          (cdr (assoc-string
                  (file-name-extension buffer-file-name)
                  '(
                    ;; DVI
@@ -1634,7 +1649,8 @@
                    ;; Microsoft Office formats (also handled by the odf
                    ;; conversion chain).
                    ("doc" odf) ("docx" odf) ("xls" odf) ("xlsx" odf)
-                   ("ppt" odf) ("pps" odf) ("pptx" odf))))))
+                   ("ppt" odf) ("pps" odf) ("pptx" odf))
+                t))))
        (content-types
         (save-excursion
           (goto-char (point-min))
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]