From 80224ef879ba34d637ed9c538d813799321b6a3f Mon Sep 17 00:00:00 2001 From: Ryan Kavanagh Date: Sun, 16 Jun 2019 16:20:26 -0400 Subject: [PATCH] Fallback to date field when year field is absent in bibtex files The bibtex.el package automatically generates a key for a BibTeX entry when required. To do so, it extracts the year from the 'year' field. Instead of the 'year' field, the biblatex dialect uses the 'date' field to record the publication date. The bibtex-generate-autokey function should fallback to the date field when the year field is absent. --- lisp/textmodes/bibtex.el | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/lisp/textmodes/bibtex.el b/lisp/textmodes/bibtex.el index a560c2b097..62482e3ae8 100644 --- a/lisp/textmodes/bibtex.el +++ b/lisp/textmodes/bibtex.el @@ -2707,10 +2707,24 @@ and `bibtex-autokey-names-stretch'." (bibtex-autokey-abbrev name bibtex-autokey-name-length)))) (defun bibtex-autokey-get-year () - "Return year field contents as a string obeying `bibtex-autokey-year-length'." - (let ((yearfield (bibtex-autokey-get-field "year"))) - (substring yearfield (max 0 (- (length yearfield) - bibtex-autokey-year-length))))) + "Return year field contents as a string obeying `bibtex-autokey-year-length'. +If the year field is absent, extract the year from a valid ISO8601-2 +Extended Format date in the date field and return it as a string obeying +`bibtex-autokey-year-length'." + (let ((yearfield (bibtex-autokey-get-field "year")) + (datefield (bibtex-autokey-get-field "date")) + (shortener (lambda (year) + (substring year (max 0 (- (length year) + bibtex-autokey-year-length)))))) + (if (string= "" yearfield) + (cond ((string-match "[./]*\\(-?[[:digit:]]+X*\\)\\([-/.[:digit:]:T~?%X]*\\)" + datefield) + ;; Matches ISO8601-2 Extended Format specification level 1 + ;; examples listed in tables 3, 4, and 5 on pp. 38-40 of the + ;; biblatex package manual, version 3.12 + (funcall shortener (match-string 1 datefield))) + (t (error "Date field `%s' is incorrectly formed" datefield))) + (funcall shortener yearfield)))) (defun bibtex-autokey-get-title () "Get title field contents up to a terminator. @@ -2795,8 +2809,10 @@ The year part: 1. Build the year part of the key by truncating the content of the year field to the rightmost `bibtex-autokey-year-length' digits (useful values are 2 and 4). - 2. If the year field (or any other field required to generate the key) - is absent, but the entry has a valid crossref field and + 2. If the year field is absent, extract the year from the date field + and truncate in the same manner. + 3. If the both fields (or any other field required to generate the key) + are absent, but the entry has a valid crossref field and `bibtex-autokey-use-crossref' is non-nil, use the field of the crossreferenced entry instead. -- 2.20.1