From 19b2c24040c30dee2da30d2294404256e7187cbc Mon Sep 17 00:00:00 2001 From: jao Date: Tue, 27 Sep 2022 20:40:30 +0100 Subject: attic: doc-view-imenu is now part of emacs29 --- attic/elisp/jao-doc-view-imenu.el | 74 +++++++++++++++++++++++++++++++++++++++ custom/jao-custom-exwm.el | 2 +- lib/doc/jao-doc-view.el | 17 +++------ lib/doc/jao-pdf.el | 39 ++------------------- 4 files changed, 83 insertions(+), 49 deletions(-) create mode 100644 attic/elisp/jao-doc-view-imenu.el diff --git a/attic/elisp/jao-doc-view-imenu.el b/attic/elisp/jao-doc-view-imenu.el new file mode 100644 index 0000000..8b27c38 --- /dev/null +++ b/attic/elisp/jao-doc-view-imenu.el @@ -0,0 +1,74 @@ +;; jao-doc-view-imenu.el --- old docview/imenu -*- lexical-binding: t; -*- + +;; Copyright (C) 2022 jao + +;; Author: jao +;; Keywords: + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; Old code that made its way into emacs 29. It defines imenu entries for +;; docview. + +;;; Code: + +(defvar jao-pdf--outline-rx + "[^\t]+\\(\t+\\)\"\\(.+\\)\"\t#\\(?:page=\\)?\\([0-9]+\\)") + +(defun jao-pdf-outline (&optional file-name) + "Return an alist describing the given FILE-NAME (or current if nil). +The result is cached as a local buffer variable." + (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 jao-pdf--outline-rx nil t) + (push `((level . ,(length (match-string 1))) + (title . ,(match-string 2)) + (page . ,(string-to-number (match-string 3)))) + outline))) + (setq jao-pdf--outline (nreverse outline)))) + +(defun jao-pdf-imenu--index (items act) + (let ((level (alist-get 'level (car items))) + (index nil)) + (while (and (car items) (<= level (alist-get 'level (car items)))) + (let-alist (car items) + (let ((title (format "%s%s (%s)" "" .title .page))) + (if (> .level level) + (let ((sub (jao-pdf-imenu--index items act)) + (fst (car index))) + (setq index (cdr index)) + (push (cons (car fst) (cons fst (car sub))) index) + (setq items (cdr sub))) + (push `(,title 0 ,act ,.page) index) + (setq items (cdr items)))))) + (cons (nreverse index) items))) + +(defun jao-pdf-imenu-index (&optional goto-page-fn file-name) + "Create an imenu index using `jao-pdf-outline'." + (let* ((goto (or goto-page-fn 'doc-view-goto-page)) + (act (lambda (_name _pos page) (funcall goto page))) + (items (jao-pdf-outline file-name))) + (car (jao-pdf-imenu--index items act)))) + +(defun jao-pdf-set-up-imenu () + (setq-local imenu-create-index-function #'jao-pdf-imenu-index + imenu-submenus-on-top nil + imenu-sort-function nil) + (imenu-add-to-menubar "Outline")) diff --git a/custom/jao-custom-exwm.el b/custom/jao-custom-exwm.el index a73355a..0a727ad 100644 --- a/custom/jao-custom-exwm.el +++ b/custom/jao-custom-exwm.el @@ -324,7 +324,7 @@ (let ((info (jao-zathura--file-info (current-buffer)))) (jao-doc-session-mark (car info)) (jao-doc-session-save) - (jao-doc-view--enable-imenu (car info) #'jao-zathura-goto-page)))) + (doc-view-imenu-index (car info) #'jao-zathura-goto-page)))) (add-hook 'exwm-update-title-hook #'jao-exwm--zathura-setup t) diff --git a/lib/doc/jao-doc-view.el b/lib/doc/jao-doc-view.el index 3631e98..f4c7c8b 100644 --- a/lib/doc/jao-doc-view.el +++ b/lib/doc/jao-doc-view.el @@ -38,18 +38,11 @@ (round (/ (* height (cdr (pdf-view-image-size))) (frame-char-height)))))) ;;; imenu -(defvar-local jao-doc-view--imenu-file nil) -(defvar-local jao-doc-view--goer 'jao-doc-view-goto-page) - -(defun jao-doc-view--enable-imenu (&optional file-name goto-page) - (setq-local imenu-create-index-function #'jao-doc-view--imenu-create-index - jao-doc-view--imenu-file (or file-name jao-doc-view--imenu-file) - jao-doc-view--goer (or goto-page 'jao-doc-view-goto-page))) - -(defun jao-doc-view--imenu-create-index () - (jao-pdf-imenu-index 'jao-doc-view--goer 'jao-doc-view--imenu-file)) - -(add-hook 'doc-view-mode-hook #'jao-pdf-set-up-imenu) +(defun jao-doc-view-enable-imenu (file-name goto-page) + (let ((ifun (lambda () (doc-view-imenu-index file-name goto-page))) + (doc-view-imenu-enabled t)) + (doc-view-imenu-setup) + (setq-local imenu-create-index-function ifun))) ;;; Page trailing (defvar-local jao-doc-view--trail-back ()) diff --git a/lib/doc/jao-pdf.el b/lib/doc/jao-pdf.el index 0cff06c..5ab3f24 100644 --- a/lib/doc/jao-pdf.el +++ b/lib/doc/jao-pdf.el @@ -42,41 +42,6 @@ "Convert a title, possibly with embedded spaces, to a PDF filename." (concat (mapconcat 'downcase (split-string title nil t) "-") ".pdf")) -(defvar jao-pdf--outline-rx - "[^\t]+\\(\t+\\)\"\\(.+\\)\"\t#\\(?:page=\\)?\\([0-9]+\\)") - -(defun jao-pdf-outline (&optional file-name) - "Return an alist describing the given FILE-NAME (or current if nil). -The result is cached as a local buffer variable." - (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 jao-pdf--outline-rx nil t) - (push `((level . ,(length (match-string 1))) - (title . ,(match-string 2)) - (page . ,(string-to-number (match-string 3)))) - outline))) - (setq jao-pdf--outline (nreverse outline)))) - -(defun jao-pdf-imenu-index (&optional goto-page-fn file-name) - "Create an imenu index using `jao-pdf-outline'." - (let* ((goto (or goto-page-fn 'doc-view-goto-page)) - (act (lambda (_name _pos page) (funcall goto page))) - (index nil)) - (dolist (item (or jao-pdf--outline (jao-pdf-outline file-name))) - (let-alist item - (let* ((lvl (make-string (max 0 (1- .level)) ?\ )) - (title (format "%s%s (%s)" "" .title .page))) - (push `(,title 0 ,act ,.page) index)))) - (nreverse index))) - -(defun jao-pdf-set-up-imenu () - (setq-local imenu-create-index-function #'jao-pdf-imenu-index) - (imenu-add-to-menubar "Outline")) - (defun jao-pdf-title (&optional fname) (if (or fname (not (derived-mode-p 'doc-view-mode 'pdf-view-mode))) (let ((base (file-name-base (or fname (buffer-file-name))))) @@ -84,9 +49,11 @@ The result is cached as a local buffer variable." (or (jao-pdf-section-title) (when buffer-file-name (jao-pdf-title buffer-file-name))))) +(defvar-local jao-pdf--outline nil) + (defun jao-pdf-section-title (&optional page file-name) (when (not jao-pdf--outline) - (setq-local jao-pdf--outline (jao-pdf-outline file-name))) + (setq-local jao-pdf--outline (doc-view--pdf-outline file-name))) (let ((page (or page (and (derived-mode-p 'doc-view-mode) (doc-view-current-page)) (and (derived-mode-p 'pdf-view) (pdf-view-current-page)))) -- cgit v1.2.3