Skip to content

Allow additional functions for format entry #434

@okomestudio

Description

@okomestudio

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions