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

From: Morgan Smith <[email-masked]>
Date: Fri, 07 Jul 2023 11:40:20 -0400

Sorry, here is a V2.  The previous patch would fail for files with funny
filenames that need escaping.  This I have removed the call to
'shell-quote-argument' as 'call-process' doesn't want escapes.

>From 1f4ef75d0ed4fedb3be1ef67bb32fd89a3577db5 Mon Sep 17 00:00:00 2001
From: Morgan Smith <[email-masked]>
Date: Fri, 7 Jul 2023 11:17:15 -0400
Subject: [PATCH] docview: Only enable imenu when supported

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.  Simplify a little by removing a 'let' statement.
(doc-view-imenu-setup): Don't check for 'mutool' as that is already
done by 'doc-view-imenu-enabled'.  Only enable imenu for PDF
documents. (bug#64516)
---
 lisp/doc-view.el | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index b14655fb274..693a5ae5509 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -1907,20 +1907,20 @@ doc-view--pdf-outline
 Each element in the returned list contains information about a section's
 title, nesting level and page number.  The list is flat: its tree
 structure is extracted by `doc-view--imenu-subtree'."
-  (let ((fn (or file-name (buffer-file-name))))
+  (let ((fn (expand-file-name (or file-name (buffer-file-name))))
+        outline)
     (when fn
-      (let ((outline nil)
-            (fn (shell-quote-argument (expand-file-name fn))))
-        (with-temp-buffer
-          (insert (shell-command-to-string (format "mutool show %s outline" fn)))
-          (goto-char (point-min))
-          (while (re-search-forward doc-view--outline-rx nil t)
-            (push `((level . ,(length (match-string 1)))
-                    (title . ,(replace-regexp-in-string "\\\\[rt]" " "
-                                                        (match-string 2)))
-                    (page . ,(string-to-number (match-string 3))))
-                  outline)))
-        (nreverse outline)))))
+      (with-temp-buffer
+        (unless (= 0 (call-process "mutool" nil (current-buffer) nil "show" fn "outline"))
+          (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)))
+                  (title . ,(replace-regexp-in-string "\\\\[rt]" " "
+                                                      (match-string 2)))
+                  (page . ,(string-to-number (match-string 3))))
+                outline)))
+      (nreverse outline))))
 
 (defun doc-view--imenu-subtree (outline act)
   "Construct a tree of imenu items for the given outline list and action.
@@ -1961,7 +1961,7 @@ doc-view-imenu-index
 
 (defun doc-view-imenu-setup ()
   "Set up local state in the current buffer for imenu, if needed."
-  (when (and doc-view-imenu-enabled (executable-find "mutool"))
+  (when (and doc-view-imenu-enabled (eq 'pdf doc-view-doc-type))
     (setq-local imenu-create-index-function #'doc-view-imenu-index
                 imenu-submenus-on-top nil
                 imenu-sort-function nil
-- 
2.40.1