Commit 2de514a3 authored by Erik Hetzner's avatar Erik Hetzner

allow easykey link text in org links

parent 7a41e8d0
......@@ -29,6 +29,13 @@
(require 'org-element)
(require 'zotxt)
(defcustom org-zotxt-link-text-style
:citation
"Style to use for org zotxt link texts."
:group 'org-zotxt
:type '(choice (const :tag "easykey" :easykey)
(const :tag "citation" :citation)))
(defun org-zotxt-extract-link-id-from-link (path)
"Return the zotxt ID from a link PATH."
(if (string-match "^zotero://select/items/\\(.*\\)$" path)
......@@ -39,7 +46,9 @@
"Insert link to Zotero ITEM in buffer."
(insert (org-make-link-string (format "zotero://select/items/%s"
(plist-get item :key))
(plist-get item :citation))))
(if (eq org-zotxt-link-text-style :easykey)
(concat "@" (plist-get item :easykey))
(plist-get item :citation)))))
(defun org-zotxt-insert-reference-links-to-items (items)
"Insert links to Zotero ITEMS in buffer."
......@@ -61,7 +70,7 @@
(deferred:next (lambda () `(:key ,item-id)))
(deferred:nextc it
(lambda (item)
(zotxt-get-item-bibliography-deferred item)))
(org-zotxt-get-item-link-text-deferred item)))
(deferred:nextc it
(lambda (item)
(save-excursion
......@@ -89,6 +98,14 @@
(goto-char end))
(setq next-link (org-element-link-successor))))))
(defun org-zotxt-get-item-link-text-deferred (item)
"Get the link text for ITEM.
May be either an easy key or bibliography, depending on the value
of `org-zotxt-link-text-style'."
(if (eq org-zotxt-link-text-style :easykey)
(zotxt-get-item-easykey-deferred item)
(zotxt-get-item-bibliography-deferred item)))
(defun org-zotxt-insert-reference-link (arg)
"Insert a zotero link in the org-mode document. Prompts for
search to choose item. If prefix argument (C-u) is used, will
......@@ -101,7 +118,7 @@ insert the currently selected item from Zotero."
(zotxt-choose-deferred))
(deferred:nextc it
(lambda (items)
(zotxt-mapcar-deferred #'zotxt-get-item-bibliography-deferred items)))
(zotxt-mapcar-deferred #'org-zotxt-get-item-link-text-deferred items)))
(deferred:nextc it
(lambda (items)
(with-current-buffer (marker-buffer mk)
......
......@@ -64,11 +64,36 @@
(deferred:nextc it
(lambda (item) (zotxt-get-item-bibliography-deferred item)))
(deferred:sync! it))))
(should (equal text (plist-get item :bibliography)))
(should (equal text (plist-get item :citation)))
(should (equal text (plist-get item :chicago-note-bibliography)))))
(ert-deftest org-zotxt-test-get-item-link-text-deferred ()
(let ((text "Doe, John. First Book. Cambridge: Cambridge University Press, 2005.")
(item
(deferred:$
(deferred:next (lambda () '(:key "0_ZBZQ4KMP")))
(deferred:nextc it
(lambda (item)
(let ((org-zotxt-link-text-style :citation))
(org-zotxt-get-item-link-text-deferred item))))
(deferred:sync! it))))
(should (equal text (plist-get item :citation)))))
(ert-deftest org-zotxt-test-get-item-link-text-deferred-with-easykey ()
(let ((text "doe:2005first")
(item
(deferred:$
(deferred:next (lambda () '(:key "0_ZBZQ4KMP")))
(deferred:nextc it
(lambda (item)
(let ((org-zotxt-link-text-style :easykey))
(org-zotxt-get-item-link-text-deferred item))))
(deferred:sync! it))))
(should (equal text (plist-get item :easykey)))))
(ert-deftest org-zotxt-test-update-reference-link-at-point ()
(let ((start-text "[[zotero://select/items/0_ZBZQ4KMP][foo]]")
(let ((org-zotxt-link-text-style :citation)
(start-text "[[zotero://select/items/0_ZBZQ4KMP][foo]]")
(end-text "[[zotero://select/items/0_ZBZQ4KMP][Doe, John. First Book. Cambridge: Cambridge University Press, 2005.]]"))
(with-temp-buffer
(insert start-text)
......@@ -79,7 +104,8 @@
(should (equal (buffer-string) end-text)))))
(ert-deftest org-zotxt-test-update-all-reference-links ()
(let ((start-text "[[zotero://select/items/0_ZBZQ4KMP][foo]]
(let ((org-zotxt-link-text-style :citation)
(start-text "[[zotero://select/items/0_ZBZQ4KMP][foo]]
[[zotero://select/items/0_TWCW4IJ7][bar]]")
(end-text "[[zotero://select/items/0_ZBZQ4KMP][Doe, John. First Book. Cambridge: Cambridge University Press, 2005.]]
[[zotero://select/items/0_TWCW4IJ7][Doe, John, and Jenny Roe. “Why Water Is Wet.” In Third Book, edited by Sam Smith. Oxford: Oxford University Press, 2007.]]"))
......@@ -92,14 +118,24 @@
(should (equal (buffer-string) end-text)))))
(ert-deftest org-zotxt-test-insert-reference-link-to-item ()
(let ((text "[[zotero://select/items/foo][Foo Bar]]")
(let ((org-zotxt-link-text-style :citation)
(text "[[zotero://select/items/foo][Foo Bar]]")
(item '(:key "foo" :citation "Foo Bar")))
(with-temp-buffer
(org-zotxt-insert-reference-link-to-item item)
(should (equal (buffer-string) text)))))
(ert-deftest org-zotxt-test-insert-reference-link-to-item-with-easykey ()
(let ((org-zotxt-link-text-style :easykey)
(text "[[zotero://select/items/foo][@foo:2014bar]]")
(item '(:key "foo" :easykey "foo:2014bar")))
(with-temp-buffer
(org-zotxt-insert-reference-link-to-item item)
(should (equal (buffer-string) text)))))
(ert-deftest org-zotxt-test-insert-reference-links-to-items ()
(let ((text "[[zotero://select/items/foo][Foo Bar]]
(let ((org-zotxt-link-text-style :citation)
(text "[[zotero://select/items/foo][Foo Bar]]
[[zotero://select/items/bar][Bar Foo]]
")
(items '((:key "foo" :citation "Foo Bar")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment