aboutsummaryrefslogtreecommitdiff
path: root/nov.el
diff options
context:
space:
mode:
authorVasilij Schneidermann <mail@vasilij.de>2020-02-14 20:38:43 +0100
committerVasilij Schneidermann <mail@vasilij.de>2020-02-14 22:18:35 +0100
commit261d8c31458945c64a5fcca7201f5080fee17b48 (patch)
treed6e68b3a081fa4218791c7916e0df4874a9fd9ac /nov.el
parentecbdecc927a3b3f7e0927d225e6e6464c244c2ae (diff)
Implement Org link support
Closes #17
Diffstat (limited to 'nov.el')
-rw-r--r--nov.el37
1 files changed, 36 insertions, 1 deletions
diff --git a/nov.el b/nov.el
index 7389607..3cf8d04 100644
--- a/nov.el
+++ b/nov.el
@@ -32,6 +32,7 @@
;; - Basic navigation (jump to TOC, previous/next chapter)
;; - Remembering and restoring the last read position
;; - Jump to next chapter when scrolling beyond end
+;; - Storing and following Org links to EPUB files
;; - Renders EPUB2 (.ncx) and EPUB3 (<nav>) TOCs
;; - Hyperlinks to internal and external targets
;; - Supports textual and image documents
@@ -427,6 +428,7 @@ This function honors `shr-max-image-proportion' if possible."
(cond
((not (display-graphic-p))
(insert alt))
+ ;; TODO: add native resizing support once it's official
((fboundp 'imagemagick-types)
;; adapted from `shr-rescale-image'
(let ((edges (window-inside-pixel-edges
@@ -732,7 +734,7 @@ Saving is only done if `nov-save-place-file' is set."
(nov-render-document))))
-;;; interop
+;;; recentf interop
(require 'recentf)
(defun nov-add-to-recentf ()
@@ -742,5 +744,38 @@ Saving is only done if `nov-save-place-file' is set."
(add-hook 'nov-mode-hook 'nov-add-to-recentf)
(add-hook 'nov-mode-hook 'hack-dir-local-variables-non-file-buffer)
+
+;;; org interop
+
+(require 'org)
+
+(defun nov-org-link-follow (path)
+ (if (string-match "^\\(.*\\)::\\([0-9]+\\):\\([0-9]+\\)$" path)
+ (let ((file (match-string 1 path))
+ (index (string-to-number (match-string 2 path)))
+ (point (string-to-number (match-string 3 path))))
+ (find-file file)
+ (when (not (nov--index-valid-p nov-documents index))
+ (error "Invalid documents index"))
+ (setq nov-documents-index index)
+ (nov-render-document)
+ (goto-char point))
+ (error "Invalid nov.el link")))
+
+(defun nov-org-link-store ()
+ (when (not (and (eq major-mode 'nov-mode) nov-file-name))
+ (error "Not in a nov.el buffer"))
+ (when (not (integerp nov-documents-index))
+ (setq nov-documents-index 0))
+ (org-store-link-props
+ :type "nov"
+ :link (format "nov:%s::%d:%d" nov-file-name nov-documents-index (point))
+ :description (format "EPUB file at %s" nov-file-name)))
+
+(org-link-set-parameters
+ "nov"
+ :follow 'nov-org-link-follow
+ :store 'nov-org-link-store)
+
(provide 'nov)
;;; nov.el ends here