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

pre-1.0-cleanup branch #651

Merged
merged 12 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ clean:
lint:
eldev -C --unstable -T lint

.PHONY: test
test:
eldev -C --unstable -T test

20 changes: 3 additions & 17 deletions citar-cache.el
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
;;; citar-cache.el --- Cache functions for citar -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Bruce D'Arcus, Roshan Shariff
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;; SPDX-FileCopyrightText: 2022 Bruce D'Arcus, Roshan Shariff
;; SPDX-License-Identifier: GPL-3.0-or-later

;;; Commentary:
;;
;; Functions for caching bibliography files.
Expand Down
5 changes: 2 additions & 3 deletions citar-capf.el
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
;;; citar-capf.el --- citar completion-at-point -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Bruce D'Arcus, Colin McLear
;; SPDX-FileCopyrightText: 2022 Bruce D'Arcus, Colin McLear
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; This file is not part of GNU Emacs.
;;
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;;; Commentary:
;;
;; Citation key 'completion-at-point' for org, markdown, or latex.
Expand Down
16 changes: 2 additions & 14 deletions citar-citeproc.el
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
;;; citar-citeproc.el --- Citeproc reference support for citar -*- lexical-binding: t; -*-
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; SPDX-FileCopyrightText: 2021-2022 Bruce D'Arcus
;; SPDX-License-Identifier: GPL-3.0-or-later

;;; Commentary:

Expand Down
8 changes: 5 additions & 3 deletions citar-embark.el
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
;;; citar-embark.el --- Citar/Embark integration -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Bruce D'Arcus
;;
;; Author: Bruce D'Arcus <[email protected]>
;; Maintainer: Bruce D'Arcus <[email protected]>
;; Created: June 22, 2022
;; Modified: June 22, 2022
;;
;; SPDX-FileCopyrightText: 2021-2022 Bruce D'Arcus
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; Version: 1.0
;; Keywords: bib extensions
;; Homepage: https://github.com/emacs-citar/citar-embark
Expand Down Expand Up @@ -52,7 +53,8 @@
(defvar citar-embark--multitarget-actions
(list #'citar-open #'citar-open-files #'citar-attach-files #'citar-open-links
#'citar-insert-bibtex #'citar-insert-citation #'citar-insert-reference
#'citar-copy-reference #'citar-insert-keys #'citar-run-default-action))
#'citar-copy-reference #'citar-insert-keys #'citar-run-default-action
#'citar-open-notes))

(defvar citar-embark--target-injection-hooks
(list (list #'citar-insert-edit #'embark--ignore-target)))
Expand Down
151 changes: 67 additions & 84 deletions citar-file.el
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
;;; citar-file.el --- File functions for citar -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2021 Bruce D'Arcus
;;
;; SPDX-FileCopyrightText: 2021-2022 Bruce D'Arcus
;; SPDX-License-Identifier: GPL-3.0-or-later

;; This file is not part of GNU Emacs.
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; Functions for opening and creating bibliographic files related to a source.
Expand Down Expand Up @@ -95,9 +83,9 @@ separator that does not otherwise occur in citation keys."
(regexp :tag "Filename separator")))

(defvar citar-notes-paths)
(defvar citar-create-note-function)
(defvar citar-library-paths)
(defvar citar-library-file-extensions)
(defvar citar-note-format-function)

;;;; Convenience functions for files and paths

Expand Down Expand Up @@ -140,70 +128,66 @@ Example: ':/path/to/test.pdf:PDF'."
(push escaped filenames))))))
(nreverse filenames)))

(defun citar-file--parse-file-field (entry dirs &optional citekey)
"Return files found in file field of ENTRY.
(defun citar-file--parse-file-field (fieldvalue dirs &optional citekey)
"Return files found in file field FIELDVALUE.
Relative file names are expanded from the first directory in DIRS
in which they are found. Omit non-existing absolute file names
and relative file names not found in DIRS. On failure, print a
message explaining the cause; CITEKEY is included in this failure
message."
(when-let* ((fieldname citar-file-variable)
(fieldvalue (citar-get-value fieldname entry)))
(if-let ((files (delete-dups (mapcan (lambda (parser)
(funcall parser fieldvalue))
citar-file-parser-functions))))
(if-let ((foundfiles (citar-file--find-files-in-dirs files dirs)))
(if (null citar-library-file-extensions)
foundfiles
(or (seq-filter (lambda (file)
(member (file-name-extension file) citar-library-file-extensions))
foundfiles)
(ignore
(message "No files for `%s' with `citar-library-file-extensions': %S"
citekey foundfiles))))
(ignore
(message (concat "None of the files for `%s' exist; check `citar-library-paths' and "
"`citar-file-parser-functions': %S")
citekey files)))
(ignore
(if (string-empty-p (string-trim fieldvalue))
(message "Empty `%s' field: %s" fieldname citekey)
(message "Could not parse `%s' field of `%s'; check `citar-file-parser-functions': %s"
fieldname citekey fieldvalue))))))

(defun citar-file--has-file-field (entries)
(if-let ((files (delete-dups (mapcan (lambda (parser)
(funcall parser fieldvalue))
citar-file-parser-functions))))
(if-let ((foundfiles (citar-file--find-files-in-dirs files dirs)))
(if (null citar-library-file-extensions)
foundfiles
(or (seq-filter (lambda (file)
(member (file-name-extension file) citar-library-file-extensions))
foundfiles)
(ignore
(message "No files for `%s' with `citar-library-file-extensions': %S"
citekey foundfiles))))
(ignore
(message (concat "None of the files for `%s' exist; check `citar-library-paths' and "
"`citar-file-parser-functions': %S")
citekey files)))
(ignore
(if (string-empty-p (string-trim fieldvalue))
(message "Empty `%s' field: %s" citar-file-variable citekey)
(message "Could not parse `%s' field of `%s'; check `citar-file-parser-functions': %s"
citar-file-variable citekey fieldvalue)))))

(defun citar-file--has-file-field ()
"Return predicate to test if bibliography entry in ENTRIES has a file field.
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-let ((filefield citar-file-variable))
(lambda (key)
(when-let ((entry (gethash key entries)))
(citar-get-value filefield entry)))))
(when citar-file-variable
(apply-partially #'citar-get-value citar-file-variable)))

(defun citar-file--get-from-file-field (keys entries)
"Return list of FILES for KEYS given in ENTRIES.
(defun citar-file--get-from-file-field (keys)
"Return list of files for KEYS.

Parse and return files given in the bibliography field named by
`citar-file-variable'.

Note: this function is intended to be used in
`citar-get-files-functions'. Use `citar-get-files' to get all
files associated with KEYS."
(when citar-file-variable
(when-let ((filefield citar-file-variable))
(citar--check-configuration 'citar-library-paths 'citar-library-file-extensions
'citar-file-parser-functions)
(let ((dirs (append citar-library-paths
(mapcar #'file-name-directory (citar--bibliography-files)))))
(mapcan
(lambda (citekey)
(when-let ((entry (gethash citekey entries)))
(citar-file--parse-file-field entry dirs citekey)))
(when-let ((fieldvalue (citar-get-value filefield citekey)))
(citar-file--parse-file-field fieldvalue dirs citekey)))
keys))))

;;;; Scanning library directories

(defun citar-file--has-library-files (&optional _entries)
(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
Expand All @@ -212,7 +196,7 @@ files associated with KEYS."
(lambda (key)
(gethash key files))))

(defun citar-file--get-library-files (keys &optional _entries)
(defun citar-file--get-library-files (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
Expand Down Expand Up @@ -323,51 +307,50 @@ need to scan the contents of DIRS in this case."

;;;; Note files

(defun citar-file--get-notes-hash (&optional keys)
"Return hash-table with KEYS with file notes."
(defun citar-file--note-directory-files (&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
extensions in `citar-file-note-extensions'."
(citar--check-configuration 'citar-notes-paths 'citar-file-note-extensions)
(citar-file--directory-files
citar-notes-paths keys citar-file-note-extensions
citar-file-additional-files-separator))

(defun citar-file-has-notes (&optional _entries)
(defun citar-file--has-notes ()
"Return predicate testing whether cite key has associated notes."
;; REVIEW why this optional arg when not needed?
(let ((files (citar-file--get-notes-hash)))
(lambda (key)
(gethash key files))))

(defun citar-file--open-note (key entry)
"Open a note file from KEY and ENTRY."
(if-let* ((file (citar-file--get-note-filename key
citar-notes-paths
citar-file-note-extensions))
(file-exists (file-exists-p file)))
(find-file file)
(if (and (null citar-notes-paths)
(equal (citar--get-notes-config :action)
'citar-org-format-note-default))
(error "You must set 'citar-notes-paths'")
(funcall
(citar--get-notes-config :create) key entry))))

(defun citar-file--get-note-files (keys)
(let ((files (citar-file--note-directory-files)))
(lambda (key) (gethash key files))))

(defun citar-file--create-note (key entry)
"Create a note file from KEY and ENTRY."
(if-let ((filename (citar-file--get-note-filename key)))
(prog1 (find-file filename)
(unless (file-exists-p filename)
(citar--check-configuration 'citar-note-format-function)
(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 ((notehash (citar-file--get-notes-hash keys)))
(flatten-list (map-values notehash))))
(let ((notes (citar-file--note-directory-files keys)))
(apply #'append (map-values notes))))

(defun citar-file--get-note-filename (key dirs extensions)
"Return existing or new filename for KEY in DIRS with extension in EXTENSIONS.
(defun citar-file--get-note-filename (key)
"Return existing or new note filename for KEY.

This is for use in a note function where notes are one-per-file,
with citekey as filename.

Returns the filename whether or not the file exists, to support a
function that will open a new file if the note is not present."
(let ((files (citar-file--directory-files dirs (list key) extensions
citar-file-additional-files-separator)))
(citar--check-configuration 'citar-notes-paths 'citar-file-note-extensions)
(let* ((dirs citar-notes-paths)
(exts citar-file-note-extensions)
(files (citar-file--directory-files dirs (list key) exts citar-file-additional-files-separator)))
(or (car (gethash key files))
(when-let ((dir (car dirs))
(ext (car extensions)))
(ext (car exts)))
(expand-file-name (concat key "." ext) dir)))))

;;;; Utility functions
Expand Down
18 changes: 2 additions & 16 deletions citar-format.el
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
;;; citar-format.el --- Formatting functions for citar -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Bruce D'Arcus, Roshan Shariff
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; SPDX-FileCopyrightText: 2021-2022 Bruce D'Arcus, Roshan Shariff
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;;; Commentary:
;;
Expand Down
20 changes: 3 additions & 17 deletions citar-latex.el
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
;;; citar-latex.el --- Latex adapter for citar -*- lexical-binding: t; -*-

;; Copyright (C) 2021 Bruce D'Arcus

;; This file is not part of GNU Emacs.

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; SPDX-FileCopyrightText: 2021-2022 Bruce D'Arcus
;; SPDX-License-Identifier: GPL-3.0-or-later

;;; Commentary:

;; A small package that provides the functions required to use citar
;; with latex.

;; Simply loading this file will enable manipulating the citations with
;; Loading this file will enable manipulating the citations with
;; commands provided by citar.

;;; Code:
Expand Down
Loading