Subject: [PATCH] docview: Only enable imenu when supported
From: Morgan Smith <[email-masked]>
To: [email-masked]
Date: Fri, 07 Jul 2023 11:24:01 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello,

More info is in the commit message.

To re-create the error open up an EPUB file (or possibly any non-PDF
file in doc-view) and press 'M-g i' to run 'imenu'.  Doing that I get
the following error:

Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  string-match("\\`PK\\'" nil)
  imenu-find-default("PK" (("*Rescan*" . -99) nil))
  imenu--completion-buffer((("*Rescan*" . -99) nil) nil)
  imenu-choose-buffer-index()
  byte-code("\300 C\207" [imenu-choose-buffer-index] 1)
  command-execute(imenu)

From cd73281f324a5b21c304789ca7b828295e8718d9 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.
(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.
---
 lisp/doc-view.el | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index b14655fb274..d9c12fc49c0 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -1912,7 +1912,8 @@ doc-view--pdf-outline
       (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)))
+          (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)))
@@ -1961,7 +1962,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