From 26cb162d42e7fd234506a5f847fbfb73adb68105 Mon Sep 17 00:00:00 2001 From: "Patrick M. Niedzielski" Date: Sun, 29 Nov 2020 02:09:47 +0000 Subject: [PATCH] Prefer date field to year field in BibTex entry The bibtex.el package contains functionality to automatically generate a key for a BibTeX entry using author/editor, year, and title information in the entry. The BibLaTeX dialect has deprecated the 'year' field (and 'month') in favor of an ISO8601-formatted 'date' field. This patch teaches the 'bibtex-generate-autokey' function to prefer a 'date' field when present, and fall back to a 'year' field for bibtex compatibility. --- lisp/textmodes/bibtex.el | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lisp/textmodes/bibtex.el b/lisp/textmodes/bibtex.el index c9e21e58f6..fe468d261d 100644 --- a/lisp/textmodes/bibtex.el +++ b/lisp/textmodes/bibtex.el @@ -2751,10 +2751,16 @@ 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))))) + "If date field exists, return year component of the ISO8601-formatted +date field contents as a string obeying `bibtex-autokey-year-length'. +Otherwise, return year field contents as a string obeying +`bibtex-autokey-year-length'." + (let* ((yearfield (bibtex-autokey-get-field "year\\|date")) + (dateyear (if (iso8601-valid-p yearfield) + (decoded-time-year (iso8601-parse yearfield)) + nil)) + (year (if dateyear (number-to-string dateyear) ""))) + (substring year (max 0 (- (length year) bibtex-autokey-year-length))))) (defun bibtex-autokey-get-title () "Get title field contents up to a terminator. @@ -2837,12 +2843,16 @@ The name part: 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 - `bibtex-autokey-use-crossref' is non-nil, use the field of the - crossreferenced entry instead. + component of the ISO8601-formatted date field to the rightmost + `bibtex-autokey-year-length' digits (useful values are 2 and 4). + 2. If the date field is absent, but the entry has a year field, build the + year part of the key by truncating the year field to the rightmost + `bibtex-autokey-year-length' digits. + 3. If both the year field and the date field (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 date or year field of the crossreferenced entry + instead. The title part 1. Change the content of the title field according to -- 2.29.2