From 2c87fa26f3a6abb9f12673bc633db11f0cb1b1c7 Mon Sep 17 00:00:00 2001 From: jao Date: Tue, 29 Dec 2020 19:42:03 +0000 Subject: improvements to pdf org links --- org/jao-org-links.el | 81 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/org/jao-org-links.el b/org/jao-org-links.el index 614bfaa..ab9b2d6 100644 --- a/org/jao-org-links.el +++ b/org/jao-org-links.el @@ -2,6 +2,17 @@ (require 'pdf-info) (defvar jao-org--sink-dir "./") +(defvar jao-org-open-pdf-fun 'jao-org--pdf-tools-open) + +(defun jao-org--pdf-tools-open (path page &optional height) + (org-open-file path 1) + (pdf-view-goto-page page) + (when height + (image-set-window-vscroll + (round (/ (* height (cdr (pdf-view-image-size))) (frame-char-height)))))) + +(defun jao-org--pdf-open (path page &optional height) + (funcall (or jao-org-open-pdf-fun 'jao-org--pdf-tools-open) path page height)) (defun jao-org--pdf-p (file) (string-match-p ".*\\.pdf$" file)) @@ -12,17 +23,12 @@ (let* ((path (match-string 1 link)) (page (string-to-number (match-string 2 link))) (height (string-to-number (match-string 3 link)))) - (org-open-file path 1) - (pdf-view-goto-page page) - (image-set-window-vscroll - (round (/ (* height (cdr (pdf-view-image-size))) (frame-char-height)))))) + (jao-org--pdf-open path page height))) ((string-match "\\(.*\\)::\\([0-9]+\\)$" link) (let* ((path (match-string 1 link)) (page (string-to-number (match-string 2 link)))) - (org-open-file path 1) - (pdf-view-goto-page page))) - (t - (org-open-file link 1)))) + (jao-org--pdf-open path page))) + (t (org-open-file link 1)))) (defun jao-org-links--follow-doc (link) (let* ((full-link (concat org-directory "/doc/" link)) @@ -41,15 +47,15 @@ (let ((default-directory jao-org--sink-dir)) (let ((f (replace-regexp-in-string "^file:" "doc:" (org-file-complete-link arg)))) - (if (jao-org--pdf-p p) + (if (jao-org--pdf-p f) (concat f "::" (read-from-minibuffer "Page: " "1")) f)))) (defsubst jao-org--title->file (title) (concat (mapconcat 'downcase (split-string title nil t) "-") ".pdf")) -(defun jao-org--pdf-title () - (let ((base (file-name-base (pdf-view-buffer-file-name)))) +(defun jao-org--pdf-title (&optional fname) + (let ((base (file-name-base (or fname (pdf-view-buffer-file-name))))) (capitalize (replace-regexp-in-string "-" " " base)))) (defvar-local jao--pdf-outline nil) @@ -66,16 +72,25 @@ (when (<= cur-page page) (setq cur-title (cdr (assoc 'title (car outline))))) (setq outline (cdr outline))) - cur-title)) + (replace-regexp-in-string "[[:blank:]]+" " " cur-title))) + +;;;###autoload +(defvar jao-org-links-pdf-store-fun nil) (defun jao-org-links--store-pdf-link () - (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-section-title) page))))) + (or (when (fboundp jao-org-links-pdf-store-fun) + (funcall jao-org-links-pdf-store-fun)) + (when (derived-mode-p 'pdf-view-mode) + (jao-org-links-store-pdf-link buffer-file-name + (pdf-view-current-page) + (jao-org--pdf-section-title))))) + +;;;###autoload +(defun jao-org-links-store-pdf-link (path page title) + (org-store-link-props + :type "doc" + :link (format "doc:%s::%d" (file-name-nondirectory path) page) + :description (format "%s (p. %d)" title page))) ;;;###autoload (defun jao-org-insert-doc (title) @@ -94,19 +109,39 @@ (defun jao-org--open-other (otp) (let ((rxs )))) +;;;###autoload +(defun jao-org-org-to-pdf-file () + (replace-regexp-in-string "/notes/\\(.+\\)\\.org$" + "/doc/\\1.org" + buffer-file-name)) + +;;;###autoload +(defun jao-org-pdf-to-org-file (&optional file-name) + (replace-regexp-in-string "/doc/\\(.+\\)\\.pdf$" + "/notes/\\1.org" + (or file-name buffer-file-name))) + +;;;###autoload +(defun jao-org-insert-doc-skeleton (&optional title) + (insert "#+title: " (or title (jao-org--pdf-title (buffer-file-name))) + "\n#+author:\n#+startup: latexpreview\n\n")) + ;;;###autoload (defun jao-org-pdf-goto-org (arg) (interactive "P") (when (jao-org--pdf-p buffer-file-name) - (let* ((file (replace-regexp-in-string "/doc/\\(.+\\)\\.pdf$" - "/notes/\\1.org" - buffer-file-name)) + (let* ((file (jao-org-pdf-to-org-file)) (new (not (file-exists-p file))) (title (jao-org--pdf-title))) (when (or arg new) (org-store-link nil t)) (find-file-other-window file) (when new - (insert "#+title: " title "\n#+author:\n#+startup: latexpreview\n\n") + (jao-org-insert-doc-skeleton title) (org-insert-link))))) +;;;###autoload +(defun jao-org-pdf-goto-org* () + (interactive) + (jao-org-pdf-goto-org t)) + (provide 'jao-org-links) -- cgit v1.2.3