Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change functions to take entry, decouple cache from formatting #281

Merged
merged 11 commits into from
Sep 14, 2021
14 changes: 7 additions & 7 deletions bibtex-actions-file.el
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
83 changes: 39 additions & 44 deletions bibtex-actions.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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
bdarcus marked this conversation as resolved.
Show resolved Hide resolved
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"))
Expand Down Expand Up @@ -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."
Expand All @@ -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))
Expand All @@ -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."
Expand Down