Subject: bug#36252: closed (Re: bug#36252: 26.1; bibtex-generate-autokey does not use use date field)

From: [email-masked] (GNU bug Tracking System)
Date: Fri, 11 Dec 2020 15:05:01 +0000

Your bug report

#36252: 26.1; bibtex-generate-autokey does not use use date field

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
36252: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=36252
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#36252: 26.1; bibtex-generate-autokey does not use use date field
From: "Roland Winkler" <[email-masked]>
To: Lars Ingebrigtsen <[email-masked]>, Patrick M. Niedzielski <[email-masked]>, Ryan Kavanagh <[email-masked]>, [email-masked]
Date: Fri, 11 Dec 2020 09:04:24 -0600
List-Id: <debbugs-submit.debbugs.gnu.org>

On Mon Dec 7 2020 Roland Winkler wrote:
> A few days ago I installed in master a related patch that I had
> lying around for some time.  I believe it addresses the question
> of searching the date and year field in a cleaner way by passing a
> list of field names to bibtex-text-in-field.  But I didn't know
> the new iso8601 library.  So that's the main purpose of the new
> patch attached below.

Installed as commit 4a700a2f79d5cca64602b7cad30d6485cfe0e449.

Closing.

Subject: 26.1; bibtex-generate-autokey does not use use date field
From: Ryan Kavanagh <[email-masked]>
To: [email-masked]
Date: Sun, 16 Jun 2019 16:23:30 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

Package: emacs
Version: 26.1
Tags: patch

The bibtex.el package will automatically generate 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. This requires a bit of care because the 'date' field can contain
an arbitrary date satisfying the "ISO8601-2 Extended Format
Specification Level 1". Fortunately, a relatively simple regex can
extract the year from all of the examples listed in the biblatex
manual[0].

Please see attached for a patch adding support for the 'date' field.

[0] http://mirrors.ibiblio.org/CTAN/macros/latex/exptl/biblatex/doc/biblatex.pdf

-- 
|)|/  Ryan Kavanagh      | GPG: 4E46 9519 ED67 7734 268F
|\|\  https://rak.ac     |      BD95 8F7B F8FC 4A11 C97A
From 80224ef879ba34d637ed9c538d813799321b6a3f Mon Sep 17 00:00:00 2001
From: Ryan Kavanagh <[email-masked]>
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