unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#36252: 26.1; bibtex-generate-autokey does not use use date field
@ 2019-06-16 20:23 Ryan Kavanagh
  2019-07-06 14:52 ` Lars Ingebrigtsen
  2019-07-06 14:55 ` Lars Ingebrigtsen
  0 siblings, 2 replies; 15+ messages in thread
From: Ryan Kavanagh @ 2019-06-16 20:23 UTC (permalink / raw)
  To: 36252


[-- Attachment #1.1: Type: text/plain, Size: 953 bytes --]

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

[-- Attachment #1.2: 0001-Fallback-to-date-field-when-year-field-is-absent-in-.patch --]
[-- Type: text/x-diff, Size: 3114 bytes --]

From 80224ef879ba34d637ed9c538d813799321b6a3f Mon Sep 17 00:00:00 2001
From: Ryan Kavanagh <rak@debian.org>
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


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1873 bytes --]

^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2020-12-11 15:04 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-16 20:23 bug#36252: 26.1; bibtex-generate-autokey does not use use date field Ryan Kavanagh
2019-07-06 14:52 ` Lars Ingebrigtsen
2019-07-06 14:55 ` Lars Ingebrigtsen
2019-07-15  0:34   ` Ryan Kavanagh
2019-07-15  7:24     ` Lars Ingebrigtsen
2019-09-16 21:01       ` Lars Ingebrigtsen
2020-08-10 11:01         ` Lars Ingebrigtsen
2020-09-14 15:02           ` Lars Ingebrigtsen
2020-12-05  9:20             ` Patrick M. Niedzielski
2020-12-06  9:25               ` Colin Baxter
2020-12-07  6:18                 ` Roland Winkler
2020-12-06 13:19               ` Lars Ingebrigtsen
2020-12-07  6:20                 ` Roland Winkler
2020-12-07 15:14                   ` Lars Ingebrigtsen
2020-12-11 15:04                   ` Roland Winkler

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).