unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#58180: [PATCH] docview: fixes for imenu generation
@ 2022-09-29 19:51 Jose A Ortega Ruiz
  2022-09-30 13:08 ` Lars Ingebrigtsen
  2022-09-30 15:32 ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 2 replies; 3+ messages in thread
From: Jose A Ortega Ruiz @ 2022-09-29 19:51 UTC (permalink / raw)
  To: 58180; +Cc: Daniel Martín

[-- Attachment #1: Type: text/plain, Size: 668 bytes --]

Tags: patch


As mentioned in the commit log, a fix for a bug i introduced in my
previous changes, and also for the bad behaviour observed by Daniel for
documents without an outline (or an associated buffer-file-name for that
matter).


In GNU Emacs 29.0.50 (build 20, x86_64-pc-linux-gnu, cairo version
 1.16.0) of 2022-09-27 built on rivendell
Repository revision: 7368cdd359325cb6ed83688178ae4b4eaf22f4d5
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101004
System Description: Debian GNU/Linux bookworm/sid

Configured using:
 'configure --prefix=/usr/local/stow/emacs29 --with-x-toolkit=no
 --with-imagemagick -C'


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-docview-fixes-for-imenu-generation.patch --]
[-- Type: text/patch, Size: 4510 bytes --]

From d2d2d829c9b8daf2cb0aca15cd58bca4679ea192 Mon Sep 17 00:00:00 2001
From: Jose A Ortega Ruiz <jao@gnu.org>
Date: Thu, 29 Sep 2022 20:19:11 +0100
Subject: [PATCH] docview: fixes for imenu generation

* lisp/doc-view.el: (doc-view--pdf-outline):
(doc-view-imenu-index):
(doc-view-imenu-setup): fix multiple empty index generation for
documents without an outline, caching the result (see discussion in
bug#58103).
(doc-view--imenu-subtree): fix for nested imenus (bug introduced in
commit fe002cc8ce)
---
 lisp/doc-view.el | 43 +++++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 18 deletions(-)

diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index 80c4fd21de..b1ea90c212 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -1900,6 +1900,9 @@ doc-view-search-previous-match
 (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.")
+
 (defun doc-view--pdf-outline (&optional file-name)
   "Return a list describing the outline of FILE-NAME.
 Return a list describing the current file if FILE-NAME is nil.
@@ -1907,19 +1910,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* ((outline nil)
-         (fn (or file-name (buffer-file-name)))
-         (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)))
+  (let ((fn (or file-name (buffer-file-name))))
+    (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)))))
 
 (defun doc-view--imenu-subtree (outline act)
   "Construct a tree of imenu items for the given outline list and action.
@@ -1932,7 +1936,8 @@ doc-view--imenu-subtree
         (nested (not doc-view-imenu-flatten))
         (index nil))
     (while (and (car outline)
-                (or nested (<= level (alist-get 'level (car outline)))))
+                (or (not nested)
+                    (<= level (alist-get 'level (car outline)))))
       (let-alist (car outline)
         (let ((title (format-spec doc-view-imenu-title-format
                                   `((?t . ,.title) (?p . ,.page)))))
@@ -1953,16 +1958,18 @@ doc-view-imenu-index
 the buffer other than the current buffer, and a jumping function
 GOTO-PAGE-FN other than `doc-view-goto-page'."
   (let* ((goto (or goto-page-fn 'doc-view-goto-page))
-         (act (lambda (_name _pos page) (funcall goto page))))
-    (car (doc-view--imenu-subtree (doc-view--pdf-outline file-name) act))))
+         (act (lambda (_name _pos page) (funcall goto page)))
+         (outline (or doc-view--outline (doc-view--pdf-outline file-name))))
+    (car (doc-view--imenu-subtree outline act))))
 
 (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"))
     (setq-local imenu-create-index-function #'doc-view-imenu-index
                 imenu-submenus-on-top nil
-                imenu-sort-function nil)
-    (imenu-add-to-menubar "Outline")))
+                imenu-sort-function nil
+                doc-view--outline (doc-view--pdf-outline))
+    (when doc-view--outline (imenu-add-to-menubar "Outline"))))
 
 ;;;; User interface commands and the mode
 
-- 
2.37.2


[-- Attachment #3: Type: text/plain, Size: 96 bytes --]


-- 
Some stories are true that never happened.
 -Elie Wiesel, writer, Nobel laureate (b. 1928)

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#58180: [PATCH] docview: fixes for imenu generation
  2022-09-29 19:51 bug#58180: [PATCH] docview: fixes for imenu generation Jose A Ortega Ruiz
@ 2022-09-30 13:08 ` Lars Ingebrigtsen
  2022-09-30 15:32 ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 0 replies; 3+ messages in thread
From: Lars Ingebrigtsen @ 2022-09-30 13:08 UTC (permalink / raw)
  To: Jose A Ortega Ruiz; +Cc: 58180, Daniel Martín

Jose A Ortega Ruiz <jao@gnu.org> writes:

> As mentioned in the commit log, a fix for a bug i introduced in my
> previous changes, and also for the bad behaviour observed by Daniel for
> documents without an outline (or an associated buffer-file-name for that
> matter).

Thanks; pushed to Emacs 29.





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#58180: [PATCH] docview: fixes for imenu generation
  2022-09-29 19:51 bug#58180: [PATCH] docview: fixes for imenu generation Jose A Ortega Ruiz
  2022-09-30 13:08 ` Lars Ingebrigtsen
@ 2022-09-30 15:32 ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-09-30 15:32 UTC (permalink / raw)
  To: Jose A Ortega Ruiz; +Cc: 58180

Jose A Ortega Ruiz <jao@gnu.org> writes:

> Tags: patch
>
>
> As mentioned in the commit log, a fix for a bug i introduced in my
> previous changes, and also for the bad behaviour observed by Daniel for
> documents without an outline (or an associated buffer-file-name for that
> matter).

Thanks, with this patch it correctly reports that imenu is unavailable.





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-30 15:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-29 19:51 bug#58180: [PATCH] docview: fixes for imenu generation Jose A Ortega Ruiz
2022-09-30 13:08 ` Lars Ingebrigtsen
2022-09-30 15:32 ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).