From f4b7bea779a3a4ea1e07d5d98c917a6c79f7a1ea Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Wed, 5 Feb 2014 01:51:45 -0500 Subject: ENH: Allow citekeys to be properties Now citekeys can be taken either from properties or from headings. This provides greater compatibility with org-bibtex.el. --- README.org | 5 ++--- bog-tests.el | 36 ++++++++++++++++++++++++++++++------ bog-todo.org | 3 ++- bog.el | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 76 insertions(+), 18 deletions(-) diff --git a/README.org b/README.org index 5a7b8cf..ba2dc9c 100644 --- a/README.org +++ b/README.org @@ -67,9 +67,8 @@ file studies under. "name2000word" is the study header. (The default format for the citekey is the first author's last name, the year, and then the first -non-trivial word.) Currently, Bog expects the study citekey to be -heading text, but I plan to add support for storing the citekeys as -properties instead (see [[./bog-todo.org][bog-todo.org]]). +non-trivial word.) Bog expects the study citekey to be the title or +property of a heading. If the study was added when checking journals online, I tend to add the URL right under the heading. However, I often add citekeys from the diff --git a/bog-tests.el b/bog-tests.el index d2a3894..822238a 100644 --- a/bog-tests.el +++ b/bog-tests.el @@ -74,17 +74,41 @@ (re-search-backward bog-citekey-format) (should (equal (bog-citekey-from-heading-title) citekey))))) -;; `bog-citekey-action' +;; `bog-citekey-from-property' -(ert-deftest bog-citekey-action-in-normal-text () +(ert-deftest bog-citekey-from-property-current-level () (let ((citekey "name2010word")) (with-temp-buffer - (insert (format "\n* top level\n\n** %s\n\nsome text\n" - citekey)) + (insert "\n* top level\n\n** subhead\n" + (format ":PROPERTIES:\n:CUSTOM_ID: %s\n" citekey) + ":END:\nsome text\n") (org-mode) (show-all) - (flet ((funcall (action citekey) citekey)) - (should (equal (bog-citekey-action nil nil nil) citekey)))))) + (should (equal (bog-citekey-from-property) citekey))))) + +(ert-deftest bog-citekey-from-property-in-parent () + (let ((citekey "name2010word")) + (with-temp-buffer + (insert "\n* top level\n" + (format ":PROPERTIES:\n:CUSTOM_ID: %s\n" citekey) + ":END:\nsome text\n" + "** subhead\n\n") + (org-mode) + (show-all) + (should (equal (bog-citekey-from-property) citekey))))) + +(ert-deftest bog-citekey-from-property-on-heading () + (let ((citekey "name2010word")) + (with-temp-buffer + (insert "\n* top level\n\n** subhead\n" + (format ":PROPERTIES:\n:CUSTOM_ID: %s\n" citekey) + ":END:\nsome text\n") + (org-mode) + (show-all) + (org-back-to-heading) + (should (equal (bog-citekey-from-property) citekey))))) + +;; `bog-citekey-action' (ert-deftest bog-citekey-action-on-heading () (let ((citekey "name2010word")) diff --git a/bog-todo.org b/bog-todo.org index c911fbb..067f3a7 100644 --- a/bog-todo.org +++ b/bog-todo.org @@ -32,7 +32,8 @@ Perhaps use a cache file. This could be used in * Notes -** ENH [#A] Allow citekeys to be properties instead of headings +** DONE [#A] Allow citekeys to be properties instead of headings + CLOSED: [2014-02-06 Thu 00:31] ** ENH Find in-text citekeys that don't have a header diff --git a/bog.el b/bog.el index 69a5bdc..14777bc 100644 --- a/bog.el +++ b/bog.el @@ -48,6 +48,27 @@ then lower case letters." :type 'string :group 'bog) +(defcustom bog-citekey-func 'bog-citekey-from-heading-title + "Function used to get citekey from study notes. + +By default, this is `bog-citekey-from-heading-title', which +selects the citekey from the first parent heading whose title +matches `bog-citekey-format'. + +The other option is `bog-citekey-from-property', which selects +the citekey from the first parent that has the property +`bog-citekey-property'." + :group 'bog + :type 'function) + +(defcustom bog-citekey-property "CUSTOM_ID" + "Property name used to store citekey. +This is only used if `bog-citekey-func' is set to +`bog-citekey-from-property'. The default corresponds to the +default value of `org-bibtex-key-property'." + :group 'bog + :type 'string) + (defcustom bog-notes-directory "~/bib" "The name of the directory that Org note are stored in." :group 'bog @@ -109,12 +130,12 @@ It should contain the placeholder \"%s\" for the query." ASK-FUNC should be a function that queries the user for a citekey when ASK-CITEKEY is non-nil. Otherwise, the citekey will be taken from the text under point if it matches `bog-citekey-format' or -from the first parent heading that matches `bog-citekey-format'. +using `bog-citekey-func'. ACTION will be called with the resulting citekey as an argument." (let* ((citekey (and ask-citekey (funcall ask-func))) (citekey (or (bog-citekey-at-point) - (bog-citekey-from-heading-title)))) + (funcall bog-citekey-func)))) (funcall action citekey))) (defun bog-select-citekey (citekeys) @@ -155,6 +176,20 @@ year, and the first meaningful word in the title)." (error "Citekey not found")) heading)))) +(defun bog-citekey-from-property () + "Retrieve citekey from first parent heading that has the + property `bog-citekey-property'." + (save-excursion + (save-restriction + (widen) + (let ((citekey (org-entry-get (point) bog-citekey-property))) + (while (and (not citekey) + (org-up-heading-safe)) + (setq citekey (org-entry-get (point) bog-citekey-property))) + (when (not citekey) + (error "Citekey not found")) + citekey)))) + (defun bog-citekey-p (text) "Indicate if TEXT matches `bog-citekey-format'." (when (string-match bog-citekey-format text) @@ -174,8 +209,8 @@ year, and the first meaningful word in the title)." "Open PDF file for a citekey. If a prefix argument is given, a prompt will open to select from available citekeys. Otherwise, the citekey will be taken from the -text under point if it matches `bog-citekey-format' or from the -first parent heading that matches `bog-citekey-format'." +text under point if it matches `bog-citekey-format' or using +`bog-citekey-func'." (interactive "P") (bog-citekey-action 'bog-open-citekey-pdf '(lambda () (bog-select-citekey (bog-pdf-citekeys))) @@ -191,8 +226,7 @@ first parent heading that matches `bog-citekey-format'." (defun bog-rename-staged-pdf-to-citekey () "Rename PDF in `bog-pdf-directory-stage' to `bog-pdf-directory'/.pdf. The citekey will be taken from the text under point if it matches -`bog-citekey-format' or from the first parent heading that -matches `bog-citekey-format'." +`bog-citekey-format' or using `bog-citekey-func'." (interactive) (bog-citekey-action 'bog-rename-staged-pdf nil @@ -236,8 +270,8 @@ matches `bog-citekey-format'." "Open BibTeX file for a citekey. If a prefix argument is given, a prompt will open to select from available citekeys. Otherwise, the citekey will be taken from the -text under point if it matches `bog-citekey-format' or from the -first parent heading that matches `bog-citekey-format'." +text under point if it matches `bog-citekey-format' or using +`bog-citekey-func'." (interactive "P") (bog-citekey-action 'bog-open-citekey-bib '(lambda () (bog-select-citekey (bog-bib-citekeys))) -- cgit v1.2.3