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

Allow additional functions for format entry #434

Open
okomestudio opened this issue Jun 26, 2023 · 1 comment
Open

Allow additional functions for format entry #434

okomestudio opened this issue Jun 26, 2023 · 1 comment

Comments

@okomestudio
Copy link

This is a feature request, not a bug report.

We customize the completion display format using bibtex-completion-display-formats. For example, these templates can take additional fields by supplying the same fields in bibtex-completion-additional-search-fields and then use them.

Sometimes, we want to perform additional formatting on field values, but there is currently no hook to do this. The author and year field are exceptions, as their fallback fields, editor and date are defined, respectively, in case they are missing. In case of author, additional preprocessing such as shortening author names is performed.:

(defun bibtex-completion-format-entry (entry width)
"Formats a BibTeX ENTRY for display in results list.
WIDTH is the width of the results list. The display format is
governed by the variable `bibtex-completion-display-formats'."
(let* ((format
(or (assoc-string (bibtex-completion-get-value "=type=" entry)
bibtex-completion-display-formats-internal
'case-fold)
(assoc t bibtex-completion-display-formats-internal)))
(format-string (cadr format)))
(s-format
format-string
(lambda (field)
(let* ((field (split-string field ":"))
(field-name (car field))
(field-width (cadr field))
(field-value (bibtex-completion-get-value field-name entry)))
(when (and (string= field-name "author")
(not field-value))
(setq field-value (bibtex-completion-get-value "editor" entry)))
(when (and (string= field-name "year")
(not field-value))
(setq field-value (car (split-string (bibtex-completion-get-value "date" entry "") "-"))))
(setq field-value (bibtex-completion-clean-string (or field-value " ")))
(when (member field-name '("author" "editor"))
(setq field-value (bibtex-completion-shorten-authors field-value)))
(if (not field-width)
field-value
(setq field-width (string-to-number field-width))
(truncate-string-to-width
field-value
(if (> field-width 0)
field-width
(- width (cddr format)))
0 ?\s)))))))

I think that it would be nice to be able to write custom field formatters. For example, bibtex-completion-field-formatters would take a list of (field . formatter-function) such that the formatter-function taking entry as an argument gets applied before rendering the value.

Would that be a possibility? This seems like relatively low-lift work that would make the template format vastly more flexible.

@tmalsburg
Copy link
Owner

It would be relatively easy to add some ad-hoc solution, but the truth is that a lot of the relevant code has organically grown and is not super easy to maintain and extend. It would be best to refactor this part of bibtex-completion, perhaps with some sort of plugin infrastructure that allows users to choose between different formatting / display styles. Not sure what this would look like in detail. But feel free to propose something specific and we can discuss it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants