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

Highlight bibtex entry sections with faces. #375

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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
103 changes: 93 additions & 10 deletions bibtex-completion.el
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,48 @@
"Helm plugin for searching entries in a BibTeX bibliography."
:group 'completion)

(defface bibtex-completion-author-face
'((t :inherit font-lock-keyword-face))
"Face for authors in `bibtex-completion-format-entry'."
:group 'bibtex-completion)

(defface bibtex-completion-year-face
'((t :inherit font-lock-function-name-face))
"Face for bibtex entry year in `bibtex-completion-format-entry'."
:group 'bibtex-completion)

(defface bibtex-completion-title-face
'((t :inherit default))
"Face for bibtex entry title in `bibtex-completion-format-entry'."
:group 'bibtex-completion)

(defface bibtex-completion-type-face
'((t :inherit font-lock-string-face))
"Face for bibtex entry type in `bibtex-completion-format-entry'."
:group 'bibtex-completion)

(defface bibtex-completion-pdf-face
'((t :inherit font-lock-type-face))
"Face for `bibtex-completion-pdf-symbol' when entry has a pdf."
:group 'bibtex-completion)

(defface bibtex-completion-pdf-missing-face
mohkale marked this conversation as resolved.
Show resolved Hide resolved
'((t :inherit shadow))
"Face for `bibtex-completion-pdf-symbol' when entry doesn't have a pdf.
Only used when `bibtex-completion-format-always-show-symbols' is non-nil."
:group 'bibtex-completion)

(defface bibtex-completion-note-face
'((t :inherit font-lock-type-face))
"Face for `bibtex-completion-notes-symbol' when entry has a note."
:group 'bibtex-completion)

(defface bibtex-completion-note-missing-face
'((t :inherit shadow))
"Face for `bibtex-completion-notes-symbol' when entry doesn't have a note.
Only used when `bibtex-completion-format-always-show-symbols' is non-nil."
:group 'bibtex-completion)

(defcustom bibtex-completion-bibliography nil
"The BibTeX file or list of BibTeX files.
Org-bibtex users can also specify org mode bibliography files, in
Expand Down Expand Up @@ -109,6 +151,15 @@ This should be a single character."
:group 'bibtex-completion
:type 'string)

(defcustom bibtex-completion-format-always-show-symbols nil
"When true `bibtex-completion-format-entry' will always render a symbol for
`bibtex-completion-pdf-symbol' and `bibtex-completion-notes-symbol'. If the
corresponding note or pdf doesn't exist the symbol will be propertized with
the secondary `bibtex-completion-pdf-missing-face' and
`bibtex-completion-pdf-missing-face' faces."
:group 'bibtex-completion
:type 'boolean)

(defcustom bibtex-completion-format-citation-functions
'((org-mode . bibtex-completion-format-citation-ebib)
(latex-mode . bibtex-completion-format-citation-cite)
Expand Down Expand Up @@ -866,16 +917,48 @@ governed by the variable `bibtex-completion-display-formats'."
(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)))
field-value
(field-value-exact (bibtex-completion-get-value field-name entry)))
;; Extract the field from the object or choose an appropriate fallback.
(setq field-value
(bibtex-completion-clean-string
(or
field-value-exact
(cond ((string= field-name "author")
(bibtex-completion-get-value "editor" entry))
((string= field-name "year")
(car (split-string (bibtex-completion-get-value "date" entry "") "-"))))
" ")))
;; Apply any post-processing and face propertizing to the field-value.
(setq field-value
(cond ((member field-name '("author" "editor"))
(propertize
(bibtex-completion-shorten-authors field-value)
'face 'bibtex-completion-author-face))
((string= field-name "year")
(propertize field-value 'face 'bibtex-completion-year-face))
((string= field-name "title")
(propertize field-value 'face 'bibtex-completion-title-face))
((string= field-name "=type=")
(propertize field-value 'face 'bibtex-completion-type-face))
((string= field-name "=has-pdf=")
(propertize (if bibtex-completion-format-always-show-symbols
bibtex-completion-pdf-symbol
field-value)
'face
(if field-value-exact
'bibtex-completion-pdf-face
'bibtex-completion-pdf-missing-face)))
((string= field-name "=has-note=")
(propertize (if bibtex-completion-format-always-show-symbols
bibtex-completion-notes-symbol
field-value)
'face
(if field-value-exact
'bibtex-completion-note-face
'bibtex-completion-note-missing-face)))
(t field-value)))
;; Ensure field-value doesn't take up more than desired width.
(if (not field-width)
field-value
(setq field-width (string-to-number field-width))
Expand Down