From: Visuwesh Date: Sun, 6 Oct 2024 12:32:06 +0000 (+0530) Subject: Make imenu index generation for PDFs more reliable X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=7a4b05ff63b4fb44ffb1e283573e001455d7e80a;p=emacs.git Make imenu index generation for PDFs more reliable Do away with parsing the output of "mutool show FILE outline" since the URI reported in its output may not include the page number of the heading, and instead may contained "nameddest" elements which cannot be resolved using "mutool". Instead, use a MuPDF JS script to generate the PDF outline allowing to resolve such URIs. * lisp/doc-view.el (doc-view--outline-rx): Remove as no longer needed. (doc-view--outline): Reflect that outline can be generated for non-PDF files too. (doc-view--mutool-pdf-outline-script): Add new variable to hold the JS script used to generate the outline. (doc-view--pdf-outline): Use the script. (bug#73638) (cherry picked from commit 44aa385b9afe2f2d500f62fd679314150e859d77) --- diff --git a/lisp/doc-view.el b/lisp/doc-view.el index 2521502fd95..392f4b5e506 100644 --- a/lisp/doc-view.el +++ b/lisp/doc-view.el @@ -1966,14 +1966,26 @@ the document text." (doc-view-goto-page (caar (last doc-view--current-search-matches))))))) ;;;; Imenu support -(defconst doc-view--outline-rx - "[^\t]+\\(\t+\\)\"\\(.+\\)\"\t#\\(?:page=\\)?\\([0-9]+\\)") - (defvar-local doc-view--outline nil - "Cached PDF outline, so that it is only computed once per document. + "Cached document outline, so that it is only computed once per document. It can be the symbol `unavailable' to indicate that outline is unavailable for the document.") +(defvar doc-view--mutool-pdf-outline-script + "var document = new Document.openDocument(\"%s\", \"application/pdf\"); +var outline = document.loadOutline(); +if(!outline) quit(); +function pp(outl, level){print(\"((level . \" + level + \")\");\ +print(\"(title . \" + repr(outl.title) + \")\");\ +print(\"(page . \" + (document.resolveLink(outl.uri)+1) + \"))\");\ +if(outl.down){for(var i=0; i