Subject: bug#64516: [PATCH] docview: Only enable imenu when supported

From: Morgan Smith <[email-masked]>
Date: Tue, 11 Jul 2023 13:49:25 -0400

Hello,

Tassilo Horn <[email-masked]> writes:

>
> I couldn't test it because all epub files I found result in
>
>   imenu unavailable: "No items suitable for an index found in this buffer"
>
> and not the described error.

I feel like my reviewers often can't reproduce my errors.  I'm running
Emacs from a very recent commit and I have mutool installed.  I really
feel like the error should be reproducible for anyone that meets those
two requirements.  Do I need to include more information in my bug
reports?  If so, what information?

Anyways I modified my patch with Tassilo's feedback.  However, I
realized that it partially reverts commit
b23e062d7463b76d25dfd9ba4a80c1848a448e42

>From 5b24d23e3317491c31ae1392c29ac7cdf303f9d4 Mon Sep 17 00:00:00 2001
From: Morgan Smith <[email-masked]>
Date: Tue, 11 Jul 2023 13:31:40 -0400
Subject: [PATCH] docview: imenu: check return value of 'mutool'

While 'mutool' supports many filetypes, 'mutool show' only supports
PDF files.  This would lead to cryptic imenu errors when opening other
file types (like EPUB) since we would parse the error output.  During
my testing this caused 'imenu--index-alist' to have a value of '(nil).

* lisp/doc-view.el (doc-view--pdf-outline): Error when 'mutool' returns
an error.  Use 'call-process' to get the return value and remove the
call to 'shell-quote-argument' as 'call-process' doesn't want any
escapes.
(doc-view-imenu-setup): Do not preemptively run 'doc-view--pdf-outline'
as an error here would inhibit display of the document.
---
 lisp/doc-view.el | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index b14655fb274..d5ccfc19853 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -147,6 +147,8 @@
 (require 'filenotify)
 (eval-when-compile (require 'subr-x))
 
+(autoload 'imenu-unavailable-error "imenu")
+
 ;;;; Customization Options
 
 (defgroup doc-view nil
@@ -1910,9 +1912,10 @@ doc-view--pdf-outline
   (let ((fn (or file-name (buffer-file-name))))
     (when fn
       (let ((outline nil)
-            (fn (shell-quote-argument (expand-file-name fn))))
+            (fn (expand-file-name fn)))
         (with-temp-buffer
-          (insert (shell-command-to-string (format "mutool show %s outline" fn)))
+          (unless (= 0 (call-process "mutool" nil (current-buffer) nil "show" fn "outline"))
+            (imenu-unavailable-error "Unable to create imenu index using `mutool'"))
           (goto-char (point-min))
           (while (re-search-forward doc-view--outline-rx nil t)
             (push `((level . ,(length (match-string 1)))
@@ -1964,8 +1967,7 @@ doc-view-imenu-setup
   (when (and doc-view-imenu-enabled (executable-find "mutool"))
     (setq-local imenu-create-index-function #'doc-view-imenu-index
                 imenu-submenus-on-top nil
-                imenu-sort-function nil
-                doc-view--outline (doc-view--pdf-outline))
+                imenu-sort-function nil)
     (when doc-view--outline (imenu-add-to-menubar "Outline"))))
 
 ;;;; User interface commands and the mode
-- 
2.40.1

I can't really seem to figure out why we call 'doc-view--pdf-outline' in
doc-view-imenu-setup even after reading the two bug reports that are
linked in the commit message.  For this reason I CC'd jao