Skip to content

Commit

Permalink
API: resource getter functions now return hash tables.
Browse files Browse the repository at this point in the history
The `citar-get-files`, `citar-get-links`, and `citar-get-notes` functions now
return hash tables mapping each key to a list of associated resources. They
used to just return lists of resources for all the given keys, but that
interface doesn't allow callers to determine which key corresponds to each
resource.

The `:items` function in the notes source API has also been modified, and must
now return a hash table instead of a list of notes.
  • Loading branch information
roshanshariff committed Jul 24, 2022
1 parent c5ed78e commit 88f7d95
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 114 deletions.
59 changes: 27 additions & 32 deletions citar-file.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
(make-obsolete-variable 'citar-file-extensions
'citar-library-file-extensions "1.0")

(declare-function citar-get-value "citar")
(declare-function citar--bibliography-files "citar")
(declare-function citar--check-configuration "citar")
(declare-function citar--get-notes-config "citar")
(declare-function citar-get-value "citar" (field key-or-entry))
(declare-function citar--bibliography-files "citar" (&rest buffers))
(declare-function citar--check-configuration "citar" (&rest variables))
(declare-function citar--get-resources-using-function "citar" (func &optional keys))

;;;; File related variables

Expand Down Expand Up @@ -163,13 +163,15 @@ Note: this function is intended to be used in
`citar-has-files-functions'. Use `citar-has-files' to test
whether entries have associated files."
(when citar-file-variable
(apply-partially #'citar-get-value citar-file-variable)))
(lambda (citekey) (and (citar-get-value citar-file-variable citekey) t))))

(defun citar-file--get-from-file-field (keys)
"Return list of files for KEYS.
(defun citar-file--get-from-file-field (&optional keys)
"Return files for KEYS by parsing the `citar-file-variable' field.
Parse and return files given in the bibliography field named by
`citar-file-variable'.
Return a hash table mapping each element of KEYS to a list of
files given in the bibliography entry named by
`citar-file-variable'. If KEYS is nil, return files for all
entries.
Note: this function is intended to be used in
`citar-get-files-functions'. Use `citar-get-files' to get all
Expand All @@ -179,30 +181,27 @@ files associated with KEYS."
'citar-file-parser-functions)
(let ((dirs (append citar-library-paths
(mapcar #'file-name-directory (citar--bibliography-files)))))
(mapcan
(lambda (citekey)
(when-let ((fieldvalue (citar-get-value filefield citekey)))
(citar--get-resources-using-function
(lambda (citekey entry)
(when-let ((fieldvalue (citar-get-value filefield entry)))
(citar-file--parse-file-field fieldvalue dirs citekey)))
keys))))

;;;; Scanning library directories

(defun citar-file--has-library-files ()
"Return predicate testing whether cite key has library files."
(citar--check-configuration 'citar-library-paths 'citar-library-file-extensions)
(let ((files (citar-file--directory-files
citar-library-paths nil citar-library-file-extensions
citar-file-additional-files-separator)))
(lambda (key)
(gethash key files))))
(let ((files (citar-file--get-library-files)))
(unless (hash-table-empty-p files)
(lambda (citekey)
(and (gethash citekey files) t)))))

(defun citar-file--get-library-files (keys)
(defun citar-file--get-library-files (&optional keys)
"Return list of files for KEYS in ENTRIES."
(citar--check-configuration 'citar-library-paths)
(let ((files (citar-file--directory-files citar-library-paths keys
citar-library-file-extensions
citar-file-additional-files-separator)))
(mapcan (lambda (key) (gethash key files)) keys)))
(citar--check-configuration 'citar-library-paths 'citar-library-file-extensions)
(citar-file--directory-files
citar-library-paths keys citar-library-file-extensions
citar-file-additional-files-separator))

(defun citar-file--make-filename-regexp (keys extensions &optional additional-sep)
"Regexp matching file names starting with KEYS and ending with EXTENSIONS.
Expand Down Expand Up @@ -307,7 +306,7 @@ need to scan the contents of DIRS in this case."

;;;; Note files

(defun citar-file--note-directory-files (&optional keys)
(defun citar-file--get-notes (&optional keys)
"Return note files associated with KEYS.
Return hash table whose keys are elements of KEYS and values are
lists of note file names found in `citar-notes-paths' having
Expand All @@ -319,8 +318,9 @@ extensions in `citar-file-note-extensions'."

(defun citar-file--has-notes ()
"Return predicate testing whether cite key has associated notes."
(let ((files (citar-file--note-directory-files)))
(lambda (key) (gethash key files))))
(let ((notes (citar-file--get-notes)))
(unless (hash-table-empty-p notes)
(lambda (citekey) (and (gethash citekey notes) t)))))

(defun citar-file--create-note (key entry)
"Create a note file from KEY and ENTRY."
Expand All @@ -331,11 +331,6 @@ extensions in `citar-file-note-extensions'."
(funcall citar-note-format-function key entry)))
(user-error "Make sure `citar-notes-paths' and `citar-file-note-extensions' are non-nil")))

(defun citar-file--get-notes (keys)
"Return list of notes associated with KEYS."
(let ((notes (citar-file--note-directory-files keys)))
(apply #'append (map-values notes))))

(defun citar-file--get-note-filename (key)
"Return existing or new note filename for KEY.
Expand Down
Loading

0 comments on commit 88f7d95

Please sign in to comment.