Skip to content

Commit

Permalink
refactor to internalize formatting
Browse files Browse the repository at this point in the history
Adapt the formatting logic from bibtex-completion to bibtex-actions, and
make prefix, target and suffix all configurable, using the same code and
config.

Also, change 'bibtex-actions-icons' to 'bibtex-actions-symbols'.
  • Loading branch information
bdarcus committed Mar 29, 2021
1 parent e59fb02 commit e97aa76
Showing 1 changed file with 80 additions and 12 deletions.
92 changes: 80 additions & 12 deletions bibtex-actions.el
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,24 @@ in previous versions."
"Face used to highlight suffixes in `bibtex-actions' candidates."
:group 'bibtex-actions)

(defcustom bibtex-actions-display-templates
; prefix will only work with rich ui, and on Emacs 28
; 'target' is the default entry display string
'((prefix . '((t. "${=has-pdf=:1} ${=has-note=:1} ")))
(target . '((t . "${author:20} ${title:48} ${year:4}")))
(suffix . '((t. " ${=key=:15} ${=type=:12} ${tags:*}"))))
"Configures display formatting for 'target', 'prefix' and 'suffix'.
The latter two are optional, but are used in the 'rich ui'."
:group 'bibtex-actions
:type '(alist :key-type symbol :value-type function))

(defcustom bibtex-actions-link-symbol "🔗"
"Symbol to indicate a DOI or URL link is available for a publication.
This should be a single character."
:group 'bibtex-actions
:type 'string)

(defcustom bibtex-actions-icon
(defcustom bibtex-actions-symbols
`((pdf . (,bibtex-completion-pdf-symbol . " "))
(note . (,bibtex-completion-notes-symbol . " "))
(link . (,bibtex-actions-link-symbol . " ")))
Expand Down Expand Up @@ -115,6 +126,7 @@ may be indicated with the same icon but a different face."
(lambda (string predicate action)
(if (eq action 'metadata)
`(metadata
; TODO add annotation-function?
,(when bibtex-actions-rich-ui
'(affixation-function . bibtex-actions--affixation))
(category . bibtex))
Expand Down Expand Up @@ -144,6 +156,7 @@ key associated with each one."
;; when using TAB-completion style multi selection interfaces.
(propertize
(s-append add (car candidate))
; TODO should width be configurable?
'display (bibtex-completion-format-entry candidate (1- (frame-width)))
;; Embed the suffix string as a custom property, for use in the affixation
;; function.
Expand All @@ -156,27 +169,82 @@ key associated with each one."
for candidate in cands
collect
(let ((pdf (if (string-match "has:pdf" candidate)
(car (cdr (assoc 'pdf bibtex-actions-icon)))
(cdr (cdr (assoc 'pdf bibtex-actions-icon)))))
(car (cdr (assoc 'pdf bibtex-actions-symbols)))
(cdr (cdr (assoc 'pdf bibtex-actions-symbols)))))
(link (if (string-match "has:link" candidate)
(car (cdr (assoc 'link bibtex-actions-icon)))
(cdr (cdr (assoc 'link bibtex-actions-icon)))))
(car (cdr (assoc 'link bibtex-actions-symbols)))
(cdr (cdr (assoc 'link bibtex-actions-symbols)))))
(note
(if (string-match "has:note" candidate)
(car (cdr (assoc 'note bibtex-actions-icon)))
(cdr (cdr (assoc 'note bibtex-actions-icon)))))
(car (cdr (assoc 'note bibtex-actions-symbols)))
(cdr (cdr (assoc 'note bibtex-actions-symbols)))))
; grab the custom suffix property
(suffix (propertize (get-text-property 1 'bibtex-actions-suffix candidate)
'face 'bibtex-actions-suffix)))
(list candidate (concat
(s-join bibtex-actions-icon-separator
(list pdf note))" ") suffix))))

;(defun bibtex-actions--make-suffix (entry)
; "Create the formatted ENTRY suffix string for the 'rich-ui'."
; ;;TODO unclear if needed, or how to do it if it is.
; ;; may need change in bibtex-completion-format-entry
; )
(defun bibtex-actions--process-display-formats (formats)
"Pre-calculate minimal widths needed by the FORMATS strings for various entry types."
; adapted from bibtex-completion
(cl-loop
for format in formats
collect
(let* ((format-string (cdr format))
(fields-width 0)
(string-width
(string-width
(s-format format-string
(lambda (field)
(setq fields-width
(+ fields-width
(string-to-number
(or (cadr (split-string field ":"))
""))))
"")))))
(-cons* (car format) format-string (+ fields-width string-width)))))

(defun bibtex-actions--format-entry (entry width template-name)
"Formats a BibTeX ENTRY for display in results list.
WIDTH is the width of the results list, and the display format is governed by
TEMPLATE-NAME."
; adapted from bibtex-completion
(let* ((template (bibtex-actions--process-display-format
(cdr (assoc template-name bibtex-actions-display-templates))))
(format
(or
; if there's a template specific to the type, use that
(assoc-string
(bibtex-completion-get-value "=type=" entry) template 'case-fold)
; if not, use the generic template
(assoc t template)))
(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)))))))

;;; Command wrappers for bibtex-completion functions

Expand Down

0 comments on commit e97aa76

Please sign in to comment.