From fec699a762bf5b0b94f27da77a7d8c44501bbf41 Mon Sep 17 00:00:00 2001 From: Bruce D'Arcus Date: Sat, 11 Sep 2021 13:24:20 -0400 Subject: [PATCH 01/11] fix: change files functions to take entry --- bibtex-actions-file.el | 14 +++---- bibtex-actions.el | 83 ++++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 51 deletions(-) diff --git a/bibtex-actions-file.el b/bibtex-actions-file.el index 18e0f453..97f56950 100644 --- a/bibtex-actions-file.el +++ b/bibtex-actions-file.el @@ -91,29 +91,29 @@ use 'orb-edit-note' for this value." (expand-file-name file dir)) files)) dirs))) -(defun bibtex-actions-file--possible-names (key dirs extensions) - "Possible names for files correponding to KEY with EXTENSIONS in DIRS." +(defun bibtex-actions-file--possible-names (entry dirs extensions) + "Possible names for files correponding to ENTRY with EXTENSIONS in DIRS." (cl-flet ((possible-file-names-with-extension (extension) (seq-map (lambda (directory) (expand-file-name - (concat key "." extension) directory)) + (concat + (bibtex-actions-get-value "=Key=" entry) "." extension) directory)) dirs))) (let* ((results-key (seq-mapcat #'possible-file-names-with-extension extensions)) - (entry (bibtex-actions-get-entry key)) (file-field (bibtex-actions-get-value bibtex-actions-file-variable entry)) (results-file (when file-field (funcall bibtex-actions-file-parser-function dirs file-field)))) (append results-key results-file)))) -(defun bibtex-actions-file--files-for-key (key dirs extensions) - "Find files related to KEY in DIRS with extension in EXTENSIONS." +(defun bibtex-actions-file--files-for-key (entry dirs extensions) + "Find files related to ENTRY in DIRS with extension in EXTENSIONS." (seq-filter #'file-exists-p - (bibtex-actions-file--possible-names key dirs extensions))) + (bibtex-actions-file--possible-names entry dirs extensions))) (defun bibtex-actions-file--files-to-open-or-create (keys dirs extensions) "Find files related to a list of KEYS in DIRS with extension in EXTENSIONS." diff --git a/bibtex-actions.el b/bibtex-actions.el index 2b695abd..7031f44b 100644 --- a/bibtex-actions.el +++ b/bibtex-actions.el @@ -351,13 +351,13 @@ personal names of the form 'family, given'." (bibtex-actions--fields-in-formats) (list "doi" "url" bibtex-actions-file-variable))) -(defun bibtex-actions--format-candidates (files &optional context) - "Format candidates from FILES, with optional hidden CONTEXT metadata. +(defun bibtex-actions--format-candidates (bib-files &optional context) + "Format candidates from BIB-FILES, with optional hidden CONTEXT metadata. This both propertizes the candidates for display, and grabs the key associated with each one." (let* ((candidates ()) (raw-candidates - (parsebib-parse files :fields (bibtex-actions--fields-to-parse))) + (parsebib-parse bib-files :fields (bibtex-actions--fields-to-parse))) (main-width (bibtex-actions--format-width (bibtex-actions-get-template 'main))) (suffix-width (bibtex-actions--format-width (bibtex-actions-get-template 'suffix))) (symbols-width (string-width (bibtex-actions--symbols-string t t t))) @@ -368,11 +368,11 @@ key associated with each one." (when (or (bibtex-actions-get-value bibtex-actions-file-variable entry) (bibtex-actions-file--files-for-key - citekey bibtex-actions-library-paths bibtex-actions-file-extensions)) + entry bibtex-actions-library-paths bibtex-actions-file-extensions)) " has:files")) (notes (when (bibtex-actions-file--files-for-key - citekey bibtex-actions-notes-paths bibtex-actions-file-extensions) + entry bibtex-actions-notes-paths bibtex-actions-file-extensions) " has:notes")) (link (when (bibtex-actions-has-a-value '("doi" "url") entry) "has:link")) @@ -450,17 +450,27 @@ has not yet been created") ;; We use defvar-local so can maintain per-buffer candidate caches. "Store the local (per-buffer) candidates list.") -(defun bibtex-actions-get-entry (key) - "Return the cached entry for KEY." - ;; FIX without this check, get a hard recursion error. - ;; But I don't think this should be needed. - (if (and (eq 'uninitialized bibtex-actions--candidates-cache) - (eq 'uninitialized bibtex-actions--local-candidates-cache)) - (message "Something is wrong; your library is not initialized.") - (cddr (seq-find - (lambda (entry) - (string-equal key (cadr entry))) - (bibtex-actions--get-candidates))))) +;;;###autoload +(defun bibtex-actions-refresh (&optional force-rebuild-cache scope) + "Reload the candidates cache. + +If called interactively with a prefix or if FORCE-REBUILD-CACHE +is non-nil, also run the `bibtex-actions-before-refresh-hook' hook. + +If SCOPE is `global' only global cache is refreshed, if it is +`local' only local cache is refreshed. With any other value both +are refreshed." + (interactive (list current-prefix-arg nil)) + (when force-rebuild-cache + (run-hooks 'bibtex-actions-force-refresh-hook)) + (unless (eq 'local scope) + (setq bibtex-actions--candidates-cache + (bibtex-actions--format-candidates + bibtex-actions-bibliography))) + (unless (eq 'global scope) + (setq bibtex-actions--local-candidates-cache + (bibtex-actions--format-candidates + (bibtex-actions--local-files-to-cache) "is:local")))) (defun bibtex-actions-get-template (template-name) "Return template string for TEMPLATE-NAME." @@ -470,16 +480,23 @@ has not yet been created") "Get the cached candidates. If the cache is unintialized, this will load the cache. If FORCE-REBUILD-CACHE is t, force reload the cache." - (if force-rebuild-cache - (bibtex-actions-refresh force-rebuild-cache) - (when (eq 'uninitialized bibtex-actions--candidates-cache) - (bibtex-actions-refresh nil 'global)) - (when (eq 'uninitialized bibtex-actions--local-candidates-cache) - (bibtex-actions-refresh nil 'local))) + (when force-rebuild-cache + (bibtex-actions-refresh force-rebuild-cache)) + (when (eq 'uninitialized bibtex-actions--candidates-cache) + (bibtex-actions-refresh nil 'global)) + (when (eq 'uninitialized bibtex-actions--local-candidates-cache) + (bibtex-actions-refresh nil 'local)) (seq-concatenate 'list bibtex-actions--local-candidates-cache bibtex-actions--candidates-cache)) +(defun bibtex-actions-get-entry (key) + "Return the cached entry for KEY." + (cddr (seq-find + (lambda (entry) + (string-equal key (cadr entry))) + (bibtex-actions--get-candidates)))) + (defun bibtex-actions-get-link (key) "Return a link for a KEY." (let* ((entry (bibtex-actions-get-entry key)) @@ -491,28 +508,6 @@ If FORCE-REBUILD-CACHE is t, force reload the cache." (when field (concat base-url (bibtex-actions-get-value field entry))))) -;;;###autoload -(defun bibtex-actions-refresh (&optional force-rebuild-cache scope) - "Reload the candidates cache. - -If called interactively with a prefix or if FORCE-REBUILD-CACHE -is non-nil, also run the `bibtex-actions-before-refresh-hook' hook. - -If SCOPE is `global' only global cache is refreshed, if it is -`local' only local cache is refreshed. With any other value both -are refreshed." - (interactive (list current-prefix-arg nil)) - (when force-rebuild-cache - (run-hooks 'bibtex-actions-force-refresh-hook)) - (unless (eq 'local scope) - (setq bibtex-actions--candidates-cache - (bibtex-actions--format-candidates - bibtex-actions-bibliography))) - (unless (eq 'global scope) - (setq bibtex-actions--local-candidates-cache - (bibtex-actions--format-candidates - (bibtex-actions--local-files-to-cache) "is:local")))) - ;;;###autoload (defun bibtex-actions-insert-preset () "Prompt for and insert a predefined search." From 88f79f12aae08ca6a6e7704d8e261127b1fd6e8e Mon Sep 17 00:00:00 2001 From: Bruce D'Arcus Date: Sat, 11 Sep 2021 15:56:50 -0400 Subject: [PATCH 02/11] keys -> keys-entries --- bibtex-actions-file.el | 10 +-- bibtex-actions.el | 143 +++++++++++++++++++++++------------------ 2 files changed, 85 insertions(+), 68 deletions(-) diff --git a/bibtex-actions-file.el b/bibtex-actions-file.el index 97f56950..a57dc545 100644 --- a/bibtex-actions-file.el +++ b/bibtex-actions-file.el @@ -137,7 +137,7 @@ use 'orb-edit-note' for this value." "Find files related to a list of KEYS in DIRS with extension in EXTENSIONS." (seq-mapcat (lambda (key) - (bibtex-actions-file--files-for-key key dirs extensions)) keys)) + (bibtex-actions-file--files-for-key (car key) dirs extensions)) keys)) ;;;; Opening and creating files functions @@ -158,16 +158,16 @@ use 'orb-edit-note' for this value." nil 0 nil file))) -(defun bibtex-actions-file-open-notes-default-org (key) - "Open a note file from KEY." +(defun bibtex-actions-file-open-notes-default-org (key-entry) + "Open a note file from KEY-ENTRY." (if-let* ((file (caar (bibtex-actions-file--files-to-open-or-create - (list key) + (list key-entry) bibtex-actions-notes-paths '("org")))) (file-exists (file-exists-p file))) (funcall bibtex-actions-file-open-function file) (let* ((uuid (org-id-new)) - (entry (bibtex-actions-get-entry key)) + (entry (cdr key-entry)) (note-meta (bibtex-actions--format-entry-no-widths entry diff --git a/bibtex-actions.el b/bibtex-actions.el index 7031f44b..867d4b74 100644 --- a/bibtex-actions.el +++ b/bibtex-actions.el @@ -231,13 +231,15 @@ and nil means no action." ;;; Completion functions (cl-defun bibtex-actions-select-keys (&optional &key rebuild-cache) - "Read bibliographic entries for completing citekeys. + "Read bibliographic entries for completing bibliographic entries. This provides a wrapper around 'completing-read-multiple', with the following optional arguments: 'REBUILD-CACHE' if t, forces rebuilding the cache before -offering the selection candidates" +offering the selection candidates. + +It returns an alist of key-entry, where the entry is a field-value alist." (let* ((crm-separator "\\s-*&\\s-*") (candidates (bibtex-actions--get-candidates rebuild-cache)) (chosen @@ -254,7 +256,7 @@ offering the selection candidates" (seq-map (lambda (choice) ;; Collect citation keys of selected candidate(s). - (or (cadr (assoc choice candidates)) + (or (cdr (assoc choice candidates)) ;; Key is literal coming from embark, just pass it on choice)) chosen))) @@ -490,16 +492,9 @@ If FORCE-REBUILD-CACHE is t, force reload the cache." bibtex-actions--local-candidates-cache bibtex-actions--candidates-cache)) -(defun bibtex-actions-get-entry (key) - "Return the cached entry for KEY." - (cddr (seq-find - (lambda (entry) - (string-equal key (cadr entry))) - (bibtex-actions--get-candidates)))) - (defun bibtex-actions-get-link (key) "Return a link for a KEY." - (let* ((entry (bibtex-actions-get-entry key)) + (let* ((entry (cdr key)) (field (bibtex-actions-has-a-value '(doi pmid pmcid url) entry)) (base-url (pcase field ('doi "https://doi.org/") @@ -519,9 +514,6 @@ If FORCE-REBUILD-CACHE is t, force reload the cache." (insert search))) ;;; Formatting functions -;; NOTE this section will be removed, or dramatically simplified, if and -;; when this PR is merged: -;; https://github.com/tmalsburg/helm-bibtex/pull/367 (defun bibtex-actions--format-width (format-string) "Calculate minimal width needed by the FORMAT-STRING." @@ -597,21 +589,21 @@ FORMAT-STRING." ;;; Commands ;;;###autoload -(defun bibtex-actions-open (keys) - "Open related resource (link or file) for KEYS." +(defun bibtex-actions-open (keys-entries) + "Open related resource (link or file) for KEYS-ENTRIES." ;; TODO add links (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) (let* ((files (bibtex-actions-file--files-for-multiple-keys - keys + (car keys-entries) (append bibtex-actions-library-paths bibtex-actions-notes-paths) bibtex-actions-file-extensions)) (links (seq-map (lambda (key) - (bibtex-actions-get-link key)) - keys)) + (bibtex-actions-get-link (cdr key))) + keys-entries)) (resources (completing-read-multiple "Related resources: " (append files (remq nil links))))) @@ -623,118 +615,136 @@ FORMAT-STRING." (t (bibtex-actions-file-open-external resource)))))) ;;;###autoload -(defun bibtex-actions-open-library-files (keys) - "Open library files associated with the KEYS. +(defun bibtex-actions-open-library-files (keys-entries) + "Open library files associated with the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) (let ((files (bibtex-actions-file--files-for-multiple-keys - keys bibtex-actions-library-paths bibtex-actions-file-extensions))) + keys-entries + bibtex-actions-library-paths bibtex-actions-file-extensions))) (if files (dolist (file files) (if bibtex-actions-open-library-file-external (bibtex-actions-file-open-external file) (funcall bibtex-actions-file-open-function file))) - (message "No file(s) found for %s" keys)))) + (message "No file(s) found for %s" + (bibtex-actions--extract-keys keys-entries))))) (make-obsolete 'bibtex-actions-open-pdf 'bibtex-actions-open-library-files "1.0") ;;;###autoload -(defun bibtex-actions-open-notes (keys) - "Open notes associated with the KEYS. +(defun bibtex-actions-open-notes (keys-entries) + "Open notes associated with the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) - (dolist (key keys) - (funcall bibtex-actions-file-open-note-function key))) + (dolist (key-entry keys-entries) + ;; REVIEW doing this means the function won't be compatible with, for + ;; example, 'orb-edit-note'. + (funcall bibtex-actions-file-open-note-function key-entry))) ;;;###autoload -(defun bibtex-actions-open-entry (keys) - "Open bibliographic entry associated with the KEYS. +(defun bibtex-actions-open-entry (keys-entries) + "Open bibliographic entry associated with the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) - (bibtex-completion-show-entry keys)) + (bibtex-completion-show-entry (caar keys-entries))) ;;;###autoload -(defun bibtex-actions-open-link (keys) - "Open URL or DOI link associated with the KEYS in a browser. +(defun bibtex-actions-open-link (keys-entries) + "Open URL or DOI link associated with the KEYS-ENTRIES in a browser. + With prefix, rebuild the cache before offering candidates." ;; (browse-url-default-browser "https://google.com") (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) - (dolist (key keys) - (let* ((entry (bibtex-actions-get-entry key)) - (doi - (bibtex-actions-get-value "doi" entry)) + (dolist (key-entry keys-entries) + (let* ((doi + (bibtex-actions-get-value "doi" (cdr key-entry))) (doi-url (when doi (concat "https://doi.org/" doi))) - (url (bibtex-actions-get-value "url" entry)) + (url (bibtex-actions-get-value "url" (cdr key-entry))) (link (or doi-url url))) (if link (browse-url-default-browser link) - (message "No link found for %s" key))))) + (message "No link found for %s" key-entry))))) ;;;###autoload -(defun bibtex-actions-insert-citation (keys) - "Insert citation for the KEYS. +(defun bibtex-actions-insert-citation (keys-entries) + "Insert citation for the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) - (bibtex-completion-insert-citation keys)) + ;; TODO + (bibtex-completion-insert-citation + (bibtex-actions--extract-keys + keys-entries))) ;;;###autoload -(defun bibtex-actions-insert-reference (keys) - "Insert formatted reference(s) associated with the KEYS. +(defun bibtex-actions-insert-reference (keys-entries) + "Insert formatted reference(s) associated with the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) - (bibtex-completion-insert-reference keys)) + (bibtex-completion-insert-reference + (bibtex-actions--extract-keys + keys-entries))) ;;;###autoload -(defun bibtex-actions-insert-key (keys) - "Insert BibTeX KEYS. +(defun bibtex-actions-insert-key (keys-entries) + "Insert BibTeX KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) - (bibtex-completion-insert-key keys)) + (bibtex-completion-insert-key + (bibtex-actions--extract-keys + keys-entries))) ;;;###autoload -(defun bibtex-actions-insert-bibtex (keys) - "Insert bibliographic entry associated with the KEYS. +(defun bibtex-actions-insert-bibtex (keys-entries) + "Insert bibliographic entry associated with the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) - (bibtex-completion-insert-bibtex keys)) + (bibtex-completion-insert-bibtex + (bibtex-actions--extract-keys + keys-entries))) ;;;###autoload -(defun bibtex-actions-add-pdf-attachment (keys) - "Attach PDF(s) associated with the KEYS to email. +(defun bibtex-actions-add-pdf-attachment (keys-entries) + "Attach PDF(s) associated with the KEYS-ENTRIES to email. With prefix, rebuild the cache before offering candidates." (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) - (bibtex-completion-add-PDF-attachment keys)) + (bibtex-completion-add-PDF-attachment + (bibtex-actions--extract-keys + keys-entries))) ;;;###autoload -(defun bibtex-actions-add-pdf-to-library (keys) - "Add PDF associated with the KEYS to library. +(defun bibtex-actions-add-pdf-to-library (keys-entries) + "Add PDF associated with the KEYS-ENTRIES to library. The PDF can be added either from an open buffer, a file, or a URL. With prefix, rebuild the cache before offering candidates." (interactive (list (bibtex-actions-select-keys :rebuild-cache current-prefix-arg))) - (bibtex-completion-add-pdf-to-library keys)) - -(defun bibtex-actions-run-default-action (keys) - "Run the default action `bibtex-actions-default-action' on KEYS." - (funcall bibtex-actions-default-action - (if (stringp keys) - (split-string keys " & ") - (split-string (cdr keys) " & ")))) + (bibtex-completion-add-pdf-to-library + (bibtex-actions--extract-keys + keys-entries))) + +(defun bibtex-actions-run-default-action (keys-entries) + "Run the default action `bibtex-actions-default-action' on KEYS-ENTRIES." + (let ((keys (bibtex-actions--extract-keys keys-entries))) + (funcall bibtex-actions-default-action + (if (stringp keys) + (split-string keys " & ") + (split-string (cdr keys) " & "))))) ;;;###autoload (defun bibtex-actions-dwim () @@ -743,5 +753,12 @@ With prefix, rebuild the cache before offering candidates." (if-let ((keys (cdr (bibtex-actions-citation-key-at-point)))) (bibtex-actions-run-default-action keys))) +(defun bibtex-actions--extract-keys (keys-entries) + "Extract list of keys from KEYS-ENTRIES alist." + (seq-map + (lambda (key-entry) + (car key-entry)) + keys-entries)) + (provide 'bibtex-actions) ;;; bibtex-actions.el ends here From 3301ddb1ab3ec19e74f2b7c566cb4168cecfd3fb Mon Sep 17 00:00:00 2001 From: Bruce D'Arcus Date: Sun, 12 Sep 2021 08:04:18 -0400 Subject: [PATCH 03/11] Modify select-key embark bit --- bibtex-actions.el | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/bibtex-actions.el b/bibtex-actions.el index 867d4b74..00ea0205 100644 --- a/bibtex-actions.el +++ b/bibtex-actions.el @@ -239,7 +239,9 @@ the following optional arguments: 'REBUILD-CACHE' if t, forces rebuilding the cache before offering the selection candidates. -It returns an alist of key-entry, where the entry is a field-value alist." +It returns an alist of key-entry, where the entry is a +field-value alist. Therefore, for each returned candidate, 'car' +is the citekey, and 'cdr' is the alist of structured data." (let* ((crm-separator "\\s-*&\\s-*") (candidates (bibtex-actions--get-candidates rebuild-cache)) (chosen @@ -257,8 +259,8 @@ It returns an alist of key-entry, where the entry is a field-value alist." (lambda (choice) ;; Collect citation keys of selected candidate(s). (or (cdr (assoc choice candidates)) - ;; Key is literal coming from embark, just pass it on - choice)) + ;; REVIEW for embark at-point; how to explain? + (cdr (seq-find (lambda (cand) (equal choice (cadr cand))) candidates)))) chosen))) (defun bibtex-actions-select-file (files) @@ -367,10 +369,10 @@ key associated with each one." (maphash (lambda (citekey entry) (let* ((files - (when (or (bibtex-actions-get-value - bibtex-actions-file-variable entry) - (bibtex-actions-file--files-for-key - entry bibtex-actions-library-paths bibtex-actions-file-extensions)) + (when (bibtex-actions-file--files-for-key + entry + bibtex-actions-library-paths + bibtex-actions-file-extensions) " has:files")) (notes (when (bibtex-actions-file--files-for-key @@ -624,7 +626,8 @@ With prefix, rebuild the cache before offering candidates." (let ((files (bibtex-actions-file--files-for-multiple-keys keys-entries - bibtex-actions-library-paths bibtex-actions-file-extensions))) + bibtex-actions-library-paths + bibtex-actions-file-extensions))) (if files (dolist (file files) (if bibtex-actions-open-library-file-external From d88069dce8eb65c46b8bc2f158fee992b272360f Mon Sep 17 00:00:00 2001 From: Bruce D'Arcus Date: Sun, 12 Sep 2021 08:57:13 -0400 Subject: [PATCH 04/11] select-keys -> select-refs --- bibtex-actions.el | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/bibtex-actions.el b/bibtex-actions.el index 00ea0205..66f8ecf3 100644 --- a/bibtex-actions.el +++ b/bibtex-actions.el @@ -230,18 +230,19 @@ and nil means no action." ;;; Completion functions -(cl-defun bibtex-actions-select-keys (&optional &key rebuild-cache) - "Read bibliographic entries for completing bibliographic entries. +(cl-defun bibtex-actions-select-refs (&optional &key rebuild-cache) + "Select bibliographic references. -This provides a wrapper around 'completing-read-multiple', with -the following optional arguments: +Provides a wrapper around 'completing-read-multiple, and returns +an alist of key-entry, where the entry is a field-value alist. -'REBUILD-CACHE' if t, forces rebuilding the cache before -offering the selection candidates. +Therefore, for each returned candidate, 'car' is the citekey, and +'cdr' is an alist of structured data. + +Includes the following optional argument: -It returns an alist of key-entry, where the entry is a -field-value alist. Therefore, for each returned candidate, 'car' -is the citekey, and 'cdr' is the alist of structured data." +'REBUILD-CACHE' if t, forces rebuilding the cache before +offering the selection candidates." (let* ((crm-separator "\\s-*&\\s-*") (candidates (bibtex-actions--get-candidates rebuild-cache)) (chosen @@ -594,7 +595,7 @@ FORMAT-STRING." (defun bibtex-actions-open (keys-entries) "Open related resource (link or file) for KEYS-ENTRIES." ;; TODO add links - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (let* ((files (bibtex-actions-file--files-for-multiple-keys @@ -621,7 +622,7 @@ FORMAT-STRING." "Open library files associated with the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (let ((files (bibtex-actions-file--files-for-multiple-keys @@ -643,7 +644,7 @@ With prefix, rebuild the cache before offering candidates." (defun bibtex-actions-open-notes (keys-entries) "Open notes associated with the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (dolist (key-entry keys-entries) ;; REVIEW doing this means the function won't be compatible with, for @@ -654,7 +655,7 @@ With prefix, rebuild the cache before offering candidates." (defun bibtex-actions-open-entry (keys-entries) "Open bibliographic entry associated with the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (bibtex-completion-show-entry (caar keys-entries))) @@ -664,7 +665,7 @@ With prefix, rebuild the cache before offering candidates." With prefix, rebuild the cache before offering candidates." ;; (browse-url-default-browser "https://google.com") - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (dolist (key-entry keys-entries) (let* ((doi @@ -682,7 +683,7 @@ With prefix, rebuild the cache before offering candidates." (defun bibtex-actions-insert-citation (keys-entries) "Insert citation for the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) ;; TODO (bibtex-completion-insert-citation @@ -693,7 +694,7 @@ With prefix, rebuild the cache before offering candidates." (defun bibtex-actions-insert-reference (keys-entries) "Insert formatted reference(s) associated with the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (bibtex-completion-insert-reference (bibtex-actions--extract-keys @@ -703,7 +704,7 @@ With prefix, rebuild the cache before offering candidates." (defun bibtex-actions-insert-key (keys-entries) "Insert BibTeX KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (bibtex-completion-insert-key (bibtex-actions--extract-keys @@ -713,7 +714,7 @@ With prefix, rebuild the cache before offering candidates." (defun bibtex-actions-insert-bibtex (keys-entries) "Insert bibliographic entry associated with the KEYS-ENTRIES. With prefix, rebuild the cache before offering candidates." - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (bibtex-completion-insert-bibtex (bibtex-actions--extract-keys @@ -723,7 +724,7 @@ With prefix, rebuild the cache before offering candidates." (defun bibtex-actions-add-pdf-attachment (keys-entries) "Attach PDF(s) associated with the KEYS-ENTRIES to email. With prefix, rebuild the cache before offering candidates." - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (bibtex-completion-add-PDF-attachment (bibtex-actions--extract-keys @@ -735,7 +736,7 @@ With prefix, rebuild the cache before offering candidates." The PDF can be added either from an open buffer, a file, or a URL. With prefix, rebuild the cache before offering candidates." - (interactive (list (bibtex-actions-select-keys + (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (bibtex-completion-add-pdf-to-library (bibtex-actions--extract-keys @@ -754,6 +755,7 @@ With prefix, rebuild the cache before offering candidates." "Run the default action on citation keys found at point." (interactive) (if-let ((keys (cdr (bibtex-actions-citation-key-at-point)))) + ;; FIX how? (bibtex-actions-run-default-action keys))) (defun bibtex-actions--extract-keys (keys-entries) From 82366106b4d973ef34f20580c66c589c44863c08 Mon Sep 17 00:00:00 2001 From: Bruce D'Arcus Date: Sun, 12 Sep 2021 09:09:15 -0400 Subject: [PATCH 05/11] minor readme update to promote org-cite support --- README.org | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.org b/README.org index 1027f1d8..3650bf9f 100644 --- a/README.org +++ b/README.org @@ -17,7 +17,7 @@ :CUSTOM_ID: features :END: -This package provides a completing-read front-end to browse and act on BibTeX, BibLaTeX, and CSL JSON bibliographic data. +This package provides a completing-read front-end to browse and act on BibTeX, BibLaTeX, and CSL JSON bibliographic data, and LaTeX, markdown, and org-cite editing support. When used with vertico (or selectrum), embark, and marginalia, it provides similar functionality to helm-bibtex and ivy-bibtex: quick filtering and selecting of bibliographic entries from the minibuffer, and the option to run different commands against them. From 22f66eaab84513e45f8d45c60ea72f10d2331c6e Mon Sep 17 00:00:00 2001 From: Bruce D'Arcus Date: Sun, 12 Sep 2021 09:43:04 -0400 Subject: [PATCH 06/11] Add comments for embark-at-point detail --- bibtex-actions.el | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bibtex-actions.el b/bibtex-actions.el index 66f8ecf3..09fb9bfe 100644 --- a/bibtex-actions.el +++ b/bibtex-actions.el @@ -258,9 +258,11 @@ offering the selection candidates." 'bibtex-actions-history bibtex-actions-presets nil))) (seq-map (lambda (choice) - ;; Collect citation keys of selected candidate(s). + ;; Collect citation key-entry of selected candidate(s). (or (cdr (assoc choice candidates)) - ;; REVIEW for embark at-point; how to explain? + ;; When calling embark at-point, use keys to look up and return the + ;; selected candidates. + ;; See https://github.com/bdarcus/bibtex-actions/issues/233#issuecomment-901536901 (cdr (seq-find (lambda (cand) (equal choice (cadr cand))) candidates)))) chosen))) From 32dc99d385431e0c234f76e9e079667227f05c34 Mon Sep 17 00:00:00 2001 From: Bruce D'Arcus Date: Sun, 12 Sep 2021 20:36:05 -0400 Subject: [PATCH 07/11] more README additions --- README.org | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.org b/README.org index 3650bf9f..08212604 100644 --- a/README.org +++ b/README.org @@ -113,7 +113,10 @@ In other supported modes, it will work the same as the embark option above. :after (embark oc) :config (setq bibtex-actions-bibliography my/bibs - org-cite-global-bibliography my/bibs)) + org-cite-global-bibliography my/bibs + org-cite-insert-processor 'oc-bibtex-actions + org-cite-follow-processor 'oc-bibtex-actions + org-cite-activate-processor 'basic)) ;; Use consult-completing-read for enhanced interface. (advice-add #'completing-read-multiple :override #'consult-completing-read-multiple) From 4947f64f051fa9557fbcbcd7669dd5f9376f9642 Mon Sep 17 00:00:00 2001 From: Anders Johansson Date: Mon, 13 Sep 2021 20:59:15 +0200 Subject: [PATCH 08/11] Adapt oc-bibtex-actions-insert to changed format (#284) --- oc-bibtex-actions.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/oc-bibtex-actions.el b/oc-bibtex-actions.el index 042ca92a..1ef2f11c 100644 --- a/oc-bibtex-actions.el +++ b/oc-bibtex-actions.el @@ -149,7 +149,8 @@ With PROC list, limits to specific processors." (defun oc-bibtex-actions-insert (&optional multiple) "Return a list of keys when MULTIPLE, or else a key string." - (let ((references (bibtex-actions-select-keys))) + (let ((references (bibtex-actions--extract-keys + (bibtex-actions-select-refs)))) (if multiple references (car references)))) From 276af4c9567c25e306731f7e26bd0af664540a03 Mon Sep 17 00:00:00 2001 From: Bruce D'Arcus Date: Tue, 14 Sep 2021 07:41:09 -0400 Subject: [PATCH 09/11] remove lambda from extract-keys --- bibtex-actions.el | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/bibtex-actions.el b/bibtex-actions.el index 09fb9bfe..0c60c22b 100644 --- a/bibtex-actions.el +++ b/bibtex-actions.el @@ -508,6 +508,10 @@ If FORCE-REBUILD-CACHE is t, force reload the cache." (when field (concat base-url (bibtex-actions-get-value field entry))))) +(defun bibtex-actions--extract-keys (keys-entries) + "Extract list of keys from KEYS-ENTRIES alist." + (seq-map #'car keys-entries)) + ;;;###autoload (defun bibtex-actions-insert-preset () "Prompt for and insert a predefined search." @@ -760,12 +764,5 @@ With prefix, rebuild the cache before offering candidates." ;; FIX how? (bibtex-actions-run-default-action keys))) -(defun bibtex-actions--extract-keys (keys-entries) - "Extract list of keys from KEYS-ENTRIES alist." - (seq-map - (lambda (key-entry) - (car key-entry)) - keys-entries)) - (provide 'bibtex-actions) ;;; bibtex-actions.el ends here From 3e470f9abba6ddf5b31f064c293d297f4fb128fb Mon Sep 17 00:00:00 2001 From: Bruce D'Arcus Date: Tue, 14 Sep 2021 08:06:30 -0400 Subject: [PATCH 10/11] add comment to default-org note fn --- bibtex-actions-file.el | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bibtex-actions-file.el b/bibtex-actions-file.el index a57dc545..bcc5a2ce 100644 --- a/bibtex-actions-file.el +++ b/bibtex-actions-file.el @@ -160,6 +160,8 @@ use 'orb-edit-note' for this value." (defun bibtex-actions-file-open-notes-default-org (key-entry) "Open a note file from KEY-ENTRY." + ;; modify when this addressed: + ;; https://github.com/org-roam/org-roam-bibtex/issues/211 (if-let* ((file (caar (bibtex-actions-file--files-to-open-or-create (list key-entry) From 6cf05a3e57961fa6495a78395eb3bf62bff52d94 Mon Sep 17 00:00:00 2001 From: Bruce D'Arcus Date: Tue, 14 Sep 2021 09:36:34 -0400 Subject: [PATCH 11/11] cleanup --- bibtex-actions-file.el | 13 ++++++------- bibtex-actions.el | 30 ++++++++++++++++-------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/bibtex-actions-file.el b/bibtex-actions-file.el index bcc5a2ce..d998546f 100644 --- a/bibtex-actions-file.el +++ b/bibtex-actions-file.el @@ -99,7 +99,7 @@ use 'orb-edit-note' for this value." (lambda (directory) (expand-file-name (concat - (bibtex-actions-get-value "=Key=" entry) "." extension) directory)) + (bibtex-actions-get-value "=key=" entry) "." extension) directory)) dirs))) (let* ((results-key (seq-mapcat #'possible-file-names-with-extension @@ -110,7 +110,7 @@ use 'orb-edit-note' for this value." (when file-field (funcall bibtex-actions-file-parser-function dirs file-field)))) (append results-key results-file)))) -(defun bibtex-actions-file--files-for-key (entry dirs extensions) +(defun bibtex-actions-file--files-for-entry (entry dirs extensions) "Find files related to ENTRY in DIRS with extension in EXTENSIONS." (seq-filter #'file-exists-p (bibtex-actions-file--possible-names entry dirs extensions))) @@ -132,12 +132,11 @@ use 'orb-edit-note' for this value." possible-files))))) (seq-mapcat #'files-for-key keys))) - -(defun bibtex-actions-file--files-for-multiple-keys (keys dirs extensions) - "Find files related to a list of KEYS in DIRS with extension in EXTENSIONS." +(defun bibtex-actions-file--files-for-multiple-entries (keys-entries dirs extensions) + "Find files related to a list of KEYS-ENTRIES in DIRS with extension in EXTENSIONS." (seq-mapcat - (lambda (key) - (bibtex-actions-file--files-for-key (car key) dirs extensions)) keys)) + (lambda (key-entry) + (bibtex-actions-file--files-for-entry (cdr key-entry) dirs extensions)) keys-entries)) ;;;; Opening and creating files functions diff --git a/bibtex-actions.el b/bibtex-actions.el index 0c60c22b..4adb0053 100644 --- a/bibtex-actions.el +++ b/bibtex-actions.el @@ -372,17 +372,20 @@ key associated with each one." (maphash (lambda (citekey entry) (let* ((files - (when (bibtex-actions-file--files-for-key + (when (bibtex-actions-file--files-for-entry entry bibtex-actions-library-paths bibtex-actions-file-extensions) " has:files")) (notes - (when (bibtex-actions-file--files-for-key - entry bibtex-actions-notes-paths bibtex-actions-file-extensions) + (when (bibtex-actions-file--files-for-entry + entry + bibtex-actions-notes-paths + bibtex-actions-file-extensions) " has:notes")) - (link (when (bibtex-actions-has-a-value '("doi" "url") entry) - "has:link")) + (link + (when (bibtex-actions-has-a-value '("doi" "url") entry) + "has:link")) (candidate-main (bibtex-actions--format-entry entry @@ -497,10 +500,9 @@ If FORCE-REBUILD-CACHE is t, force reload the cache." bibtex-actions--local-candidates-cache bibtex-actions--candidates-cache)) -(defun bibtex-actions-get-link (key) - "Return a link for a KEY." - (let* ((entry (cdr key)) - (field (bibtex-actions-has-a-value '(doi pmid pmcid url) entry)) +(defun bibtex-actions-get-link (entry) + "Return a link for an ENTRY." + (let* ((field (bibtex-actions-has-a-value '(doi pmid pmcid url) entry)) (base-url (pcase field ('doi "https://doi.org/") ('pmid "https://www.ncbi.nlm.nih.gov/pubmed/") @@ -604,14 +606,14 @@ FORMAT-STRING." (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (let* ((files - (bibtex-actions-file--files-for-multiple-keys - (car keys-entries) + (bibtex-actions-file--files-for-multiple-entries + keys-entries (append bibtex-actions-library-paths bibtex-actions-notes-paths) bibtex-actions-file-extensions)) (links (seq-map - (lambda (key) - (bibtex-actions-get-link (cdr key))) + (lambda (key-entry) + (bibtex-actions-get-link (cdr key-entry))) keys-entries)) (resources (completing-read-multiple "Related resources: " @@ -631,7 +633,7 @@ With prefix, rebuild the cache before offering candidates." (interactive (list (bibtex-actions-select-refs :rebuild-cache current-prefix-arg))) (let ((files - (bibtex-actions-file--files-for-multiple-keys + (bibtex-actions-file--files-for-multiple-entries keys-entries bibtex-actions-library-paths bibtex-actions-file-extensions)))