From 6e793f022edb47cf0d222a49dffd60fd282ec263 Mon Sep 17 00:00:00 2001 From: jao Date: Tue, 27 Oct 2020 02:13:32 +0000 Subject: org-links: use current section in captured pdf links --- org/jao-org-links.el | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/org/jao-org-links.el b/org/jao-org-links.el index b34bb06..614bfaa 100644 --- a/org/jao-org-links.el +++ b/org/jao-org-links.el @@ -1,4 +1,5 @@ (require 'jao-org-utils) +(require 'pdf-info) (defvar jao-org--sink-dir "./") @@ -51,14 +52,30 @@ (let ((base (file-name-base (pdf-view-buffer-file-name)))) (capitalize (replace-regexp-in-string "-" " " base)))) +(defvar-local jao--pdf-outline nil) + +(defun jao-org--pdf-section-title (&optional page) + (when (not jao--pdf-outline) + (setq-local jao--pdf-outline (pdf-info-outline))) + (let ((page (or page (pdf-view-current-page))) + (outline jao--pdf-outline) + (cur-page 0) + (cur-title (jao-org--pdf-title))) + (while (and (car outline) (< cur-page page)) + (setq cur-page (cdr (assoc 'page (car outline)))) + (when (<= cur-page page) + (setq cur-title (cdr (assoc 'title (car outline))))) + (setq outline (cdr outline))) + cur-title)) + (defun jao-org-links--store-pdf-link () - (when (eq major-mode 'pdf-view-mode) + (when (derived-mode-p 'pdf-view-mode) (let ((path buffer-file-name) (page (pdf-view-current-page))) (org-store-link-props :type "doc" :link (format "doc:%s::%d" (file-name-nondirectory path) page) - :description (format "%s (p. %d)" (jao-org--pdf-title) page))))) + :description (format "%s (p. %d)" (jao-org--pdf-section-title) page))))) ;;;###autoload (defun jao-org-insert-doc (title) @@ -67,6 +84,7 @@ ;;;###autoload (defun jao-org-links-setup (sink-dir) + (interactive) (org-link-set-parameters "doc" :follow #'jao-org-links--follow-doc :complete #'jao-org-links--complete-doc -- cgit v1.2.3