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

feat: add new format-entry function, note template #274

Merged
merged 7 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 8 additions & 2 deletions bibtex-actions-file.el
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

(declare-function bibtex-actions-get-entry "bibtex-actions")
(declare-function bibtex-actions-get-value "bibtex-actions")
(declare-function bibtex-actions-get-template "bibtex-actions")
(declare-function bibtex-actions--format-entry-no-widths "bibtex-actions")

;;;; File related variables

Expand Down Expand Up @@ -165,9 +167,13 @@ use 'orb-edit-note' for this value."
(file-exists (file-exists-p file)))
(funcall bibtex-actions-file-open-function file)
(let* ((uuid (org-id-new))
(title (bibtex-actions-get-value "title" (bibtex-actions-get-entry key)))
(entry (bibtex-actions-get-entry key))
(note-title
(bibtex-actions--format-entry-no-widths
entry
(bibtex-actions-get-template 'note)))
(content
(concat ":PROPERTIES:\n:ID: " uuid "\n:END:\n#+title: Notes on " title "\n")))
(concat ":PROPERTIES:\n:ID: " uuid "\n:END:\n" note-title "\n")))
bdarcus marked this conversation as resolved.
Show resolved Hide resolved
(funcall bibtex-actions-file-open-function file)
(insert content))))

Expand Down
39 changes: 26 additions & 13 deletions bibtex-actions.el
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,16 @@
:type '(boolean))


(defcustom bibtex-actions-template
(cons
"${author editor:30} ${date year issued:4} ${title:48}"
" ${=key= id:15} ${=type=:12} ${tags keywords keywords:*}")
(defcustom bibtex-actions-templates
'((main . "${author editor:30} ${date year issued:4} ${title:48}")
(suffix . " ${=key= id:15} ${=type=:12} ${tags keywords keywords:*}")
(note . "#+title: Notes on ${author editor}, ${title}"))
"Configures formatting for the bibliographic entry.
car is for the main body of the candidate and cdr is for suffix.
The same string is used for display and for search."

The main and suffix templates are for candidate display, and note
for the title field for new notes."
:group 'bibtex-actions
:type '(cons string string))
:type '(alist :key-type string))

(defcustom bibtex-actions-display-transform-functions
;; TODO change this name, as it might be confusing?
Expand Down Expand Up @@ -338,8 +339,8 @@ personal names of the form 'family, given'."
(lambda (fields-string) (car (split-string fields-string ":"))))
"[ ]+")))
(seq-mapcat #'fields-for-format
(list (car bibtex-actions-template)
(cdr bibtex-actions-template)))))
(list (bibtex-actions-get-template 'main)
(bibtex-actions-get-template 'suffix)))))

(defun bibtex-actions--fields-to-parse ()
"Determine the fields to parse from the template."
Expand All @@ -354,8 +355,8 @@ key associated with each one."
(let* ((candidates ())
(raw-candidates
(parsebib-parse files :fields (bibtex-actions--fields-to-parse)))
(main-width (bibtex-actions--format-width (car bibtex-actions-template)))
(suffix-width (bibtex-actions--format-width (cdr bibtex-actions-template)))
(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)))
(star-width (- (frame-width) (+ 2 symbols-width main-width suffix-width))))
(maphash
Expand All @@ -376,12 +377,12 @@ key associated with each one."
(bibtex-actions--format-entry
entry
star-width
(car bibtex-actions-template)))
(bibtex-actions-get-template 'main)))
(candidate-suffix
(bibtex-actions--format-entry
entry
star-width
(cdr bibtex-actions-template)))
(bibtex-actions-get-template 'suffix)))
;; We display this content already using symbols; here we add back
;; text to allow it to be searched, and citekey to ensure uniqueness
;; of the candidate.
Expand Down Expand Up @@ -456,6 +457,10 @@ has not yet been created")
(string-equal key (cadr entry)))
(bibtex-actions--get-candidates)))))

(defun bibtex-actions-get-template (template-name)
"Return template string for TEMPLATE-NAME."
(cdr (assoc template-name bibtex-actions-templates)))

(defun bibtex-actions--get-candidates (&optional force-rebuild-cache)
"Get the cached candidates.
If the cache is unintialized, this will load the cache.
Expand Down Expand Up @@ -555,6 +560,14 @@ FORMAT-STRING."
(display-value (bibtex-actions-display-value field-names entry)))
(bibtex-actions--fit-to-width display-value display-width)))))

(defun bibtex-actions--format-entry-no-widths (entry format-string)
"Format ENTRY for display per FORMAT-STRING."
(s-format
format-string
(lambda (raw-field)
(let ((field-names (split-string raw-field "[ ]+")))
(bibtex-actions-display-value field-names entry)))))

;;; At-point functions

;;; Org-cite
Expand Down