summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoost Kremers <joostkremers@fastmail.fm>2017-03-23 01:23:43 +0100
committerJoost Kremers <joostkremers@fastmail.fm>2017-03-23 01:23:43 +0100
commit3233b4e68a4948abe69bf8739d02b0468f9cd554 (patch)
treed63377f3ed010029d7e211dcd2e89864b2fb18fd
parent96e9320c3d09923fd4bd3bfbca3d4d4891273cfd (diff)
Add cross-reference resolving.
-rw-r--r--README.md53
-rw-r--r--parsebib.el323
2 files changed, 306 insertions, 70 deletions
diff --git a/README.md b/README.md
index 9a303e2..8b37fac 100644
--- a/README.md
+++ b/README.md
@@ -8,26 +8,38 @@ Parsebib
Both APIs parse the current buffer. If you wish to combine multiple `.bib` files, you need to parse each separately.
+Resolving `@string` abbreviations and cross-references
+------------------------------------------------------
+
+Parsebib can resolve `@string` abbrevs and cross-references while reading the contents of a `.bib` file. When `@string` abbrevs are resolved, abbreviations in field values (or `@string` definitions) are replaced with their expansion. In addition, the braces or double quotes around field values are removed, and multiple spaces and newlines in sequence are reduced to a single space. In essence, the field values are modified in such a way that they are suitable for display, but they no longer reliably represent the contents of the `.bib` file. When `@string` abbrevs are not resolved, no modifications are applied to the field values, so that the parsing results reflect the contents of the `.bib` file accurately.
+
+Cross-references can also be resolved. This means that if an entry that has a `crossref` field, fields in the cross-referenced entry that are not already part of the cross-referencing entry are added to it. Both BibTeX's (rather simplistic) inheritance rule and BibLaTeX's more sophisticated inheritance schema are supported. It is also possible to specify a custom inheritance schema. Note that resolving cross-references can be done independently from resolving `@string` abbrevs, but the former generally won't make sense without the latter.
+
+Resolving `@string` abbrevs can be done with both the higher-level and the lower-level API. Resolving cross-references can only be done with the higher-level API. This is mainly because cross-referenced entries appear *after* cross-referencing entries in the `.bib` file, so that when an entry with a `crossref` field is read, its cross-referenced entry is not known yet, while `@string` definitions appear in the `.bib` file before they are used. It is possible, however, to resolve cross-references after all entries have been read.
+
+
Higher-level API
----------------
The higher-level API consists of functions that read and return all items of a specific type in the current buffer. They do not move point.
-### `parsebib-collect-entries (&optional hash strings)` ###
+### `parsebib-collect-entries (&optional hash strings inheritance)` ###
+
+Collect all entries in the current buffer and return them as a hash table, where the keys correspond to the BibTeX keys and the values are alists consisting of `(<field> . <value>)` pairs of the relevant entries. In this alist, the BibTeX key and the entry type are stored under `=key=` and `=type=`, respectively.
-Collect all entries in the current buffer and returns them as a hash table, where the keys correspond to the BibTeX keys and the values are alists consisting of `(<field> . <value>)` pairs of the relevant entries. In this alist, the BibTeX key and the entry type are stored under `=key=` and `=type=`, respectively.
+The variable `hash` can be used to pass a hash table in which the entries are stored. This can be used to combine multiple `.bib` files into a single hash table, or to update an existing hash table by rereading its `.bib` file.
-The variable `hash` can be used to pass a hash table in which the entries are stored. This can be used to combine multiple `.bib` files into a single hash table, or to update an existing hash table by rereading its `.bib` file. If an entry is read from the buffer that has the same key as an entry in `hash`, the new entry overrides the old one.
+If the variable `strings` is present, `@string` abbreviations are expanded. `strings` should be a hash table of `@string` definitions as returned by `parsebib-collect-strings`.
-The variable `strings` is a hash table of `@string` definitions, where the keys are the `@string` abbreviations and the values their expansions. If the variable `strings` is present, abbreviations occurring in the field values of the entries being read are expanded. Furthermore, the (outer) braces or double quotes are removed from field values and newlines and sequences of spaces are reduced to a single space.
+If the variable `inheritance` is present, cross-references among entries are resolved. It can be `t`, in which case the file-local or global value of `bibtex-dialect` is used to determine which inheritance schema is used. It can also be one of the symbols `BibTeX` or `biblatex`, or it can be a custom inheritance schema.
### `parsebib-collect-strings (&optional hash expand-strings)` ###
-Collect all `@string` definitions in the current buffer. Again, the variable `hash` can be used to provide a hash table to store the definitions in. If it is `nil`, a new hash table is created and returned.
+Collect all `@string` definitions in the current buffer and return them as a hash table. The variable `hash` can be used to provide a hash table to store the definitions in. If it is `nil`, a new hash table is created.
-The argument `expand-strings` is a boolean value. If non-nil, any abbreviations found in the string expansions are expanded. You do not need to pass a hash table to the function for this to work. Every `@string` definition is added to the hash table as soon as it is read, which means that a `@string` definition can use an expansion defined earlier in the same file.
+The argument `expand-strings` is a boolean value. If non-nil, any abbreviations found in the string expansions are expanded against the `@string` definitions appearing earlier in the `.bib` file and against `@string` definitions in `hash`, if provided.
### `parsebib-collect-preambles` ###
@@ -45,35 +57,48 @@ Collect all `@comments` in the current buffer and return them as a list.
Find and return the BibTeX dialect for the current buffer. The BibTeX dialect is either `BibTeX` or `biblatex` and can be defined in a local-variable block at the end of the file.
-### `parsebib-parse-buffer (&optional entries-hash strings-hash expand-strings)` ###
+### `parsebib-parse-buffer (&optional entries strings expand-strings inheritance)` ###
Collect all BibTeX data in the current buffer. Return a five-element list:
(<entries> <strings> <preambles> <comments> <BibTeX dialect>)
-The `<entries>` and `<strings>` are hash tables, `<preambles>` and `<comments>` are lists, `<BibTeX dialect>` is a symbol (either `BibTeX` or `biblatex`).
+The `<entries>` and `<strings>` are hash tables, `<preambles>` and `<comments>` are lists, `<BibTeX dialect>` is a symbol (either `BibTeX` or `biblatex`) or `nil`.
+
+If the arguments `entries` and `strings` are present, they should be hash tables with `equal` as the `:test` function. They are then used to store the entries and strings, respectively.
-The arguments `entries-hash` and `strings-hash` can be passed to store the entries and strings, respectively, in the same manner described above. The argument `expand-strings` is a boolean and has the same effect as the same-name argument in `parsebib-collect-strings`.
+The argument `expand-strings` functions as the same-name argument in `parsebib-collect-strings`, and `inheritance` functions as the same-name argument in `parsebib-collect-entries`.
-Note that `parsebib-parse-buffer` only makes one pass through the buffer. It should therefore be a bit faster that calling all the `parsebib-collect-*` functions above in a row, since that would require making four passes through the buffer.
+Note that `parsebib-parse-buffer` only makes one pass through the buffer. It is therefore a bit faster than calling all the `parsebib-collect-*` functions above in a row, since that would require making four passes through the buffer.
+
+
+### `parsebib-expand-xrefs (entries inheritance)` ###
+
+Expand cross-references in `entries` according to inheritance schema `inheritance`. `entries` should be a hash table as returned by `parsebib-collect-entries`. Each entry with a `crossref` field is expanded as described above. The results are stored in the hash table `entries` again, the return value of this function is always `nil`.
Lower-level API
---------------
-The lower-level API consists of functions that do the actual reading of a BibTeX item. Unlike the higher-level API, the functions here are dependent on the position of point. They are designed in such a way that calling them multiple times in succession will yield the contents of the entire `.bib` file. All functions here take an optional position argument, which is the position in the buffer from which they should start reading. In each function, the default value is `(point)`.
+The lower-level API consists of functions that do the actual reading of a BibTeX item. Unlike the higher-level API, the functions here are dependent on the position of `point`. They are meant to be used in a `while` loop in which `parsebib-find-next-item` is used to move `point` to the next item and then use one of the `parsebib-read-*` functions to read the contents of the item.
+
+All functions here take an optional position argument, which is the position in the buffer from which they should start reading. The default value is `(point)`.
+
### `parsebib-find-next-item (&optional pos)` ###
-Find the first BibTeX item following point, where an item is either an entry, or a `@Preamble`, `@String`, or `@Comment`. This function returns the item's type as a string, i.e., either `"preamble"`, `"string"`, or `"comment"`, or the entry type. Note that the `@` is *not* part of the returned string. This function moves point into the correct position to start reading the actual contents of the item, which is done by one of the following functions.
+Find the first BibTeX item following `pos`, where an item is either a BibTeX entry, or a `@Preamble`, `@String`, or `@Comment`. This function returns the item's type as a string, i.e., either `"preamble"`, `"string"`, or `"comment"`, or the entry type. Note that the `@` is *not* part of the returned string. This function moves point into the correct position to start reading the actual contents of the item, which is done by one of the following functions.
+
### `parsebib-read-string (&optional pos strings)` ###
### `parsebib-read-entry (type &optional pos strings)` ###
### `parsebib-read-preamble (&optional pos)` ###
### `parsebib-read-comment (&optional pos)` ###
-These functions do what their names suggest: read one single item of the type specified. Each takes the `pos` argument just mentioned. In addition, `parsebib-read-string` and `parsebiib-read-entry` take an extra argument, a hash table of `@string` definitions. When provided, abbreviations in the `@string` expansions or in field values are expanded. Furthermore, the outermost braces or double quotes are removed and newlines and sequences of white space are reduced to a single space. Note that `parsebib-read-entry` takes the entry type (as returned by `parsebib-find-next-entry`) as argument.
+These functions do what their names suggest: read one single item of the type specified. Each takes the `pos` argument just mentioned. In addition, `parsebib-read-string` and `parsebiib-read-entry` take an extra argument, a hash table of `@string` definitions. When provided, abbreviations in the `@string` definitions or in field values are expanded. Note that `parsebib-read-entry` takes the entry type (as returned by `parsebib-find-next-entry`) as argument.
+
+The reading functions return the contents of the item they read: `parsebib-read-preamble` and `parsebib-read-comment` return the text as a string. `parsebib-read-string` returns a cons cell of the form `(<abbrev> . <string>)`, and `parsebib-read-entry` returns the entry as an alist of `(<field> . <value>)` pairs. One of these pairs contains the entry type `=type=`, and one contains the entry key. These have the keys `"=key="` and `"=type="`, respectively.
-The reading functions return the contents of the item they read: `parsebib-read-preamble` and `parsebib-read-comment` return the text as a string. `parsebib-read-string` returns a cons cell of the form `(<abbrev> . <string>)`, and `parsebib-read-entry` returns the entry as an alist of `(<field> . <value>)` pairs. The alist contains an element with the key `=type=`, which holds the entry type, and an element with the key `=key=`, which holds the entry key. All functions move point to the end of the entry.
+Note that all `parsebib-read*` functions move point to the end of the entry.
The reading functions return `nil` if they do not find the element they should be reading at the line point is on. Point is nonetheless moved, however. Similarly, `parsebib-find-next-item` returns `nil` if it finds no next entry, leaving point at the end of the buffer. Additionally, it will signal an error of type `parsebib-entry-type-error` if it finds something that it deems to be an invalid item name. What is considered to be a valid name is determined by the regexp `parsebib-bibtex-identifier`, which is set to `"[^^\"@\\&$#%',={}() \t\n\f]*"`, meaning that any string not containing whitespace or any of the characters `^"@\&$#%',={}()` is considered a valid identifier.
diff --git a/parsebib.el b/parsebib.el
index 0754ab9..ab02061 100644
--- a/parsebib.el
+++ b/parsebib.el
@@ -43,6 +43,114 @@
(require 'cl-lib)
(require 'subr-x) ; for `string-join'.
+(defvar parsebib--biblatex-inheritances '(("all"
+ "all"
+ (("ids" . none)
+ ("crossref" . none)
+ ("xref" . none)
+ ("entryset" . none)
+ ("entrysubtype" . none)
+ ("execute" . none)
+ ("label" . none)
+ ("options" . none)
+ ("presort" . none)
+ ("related" . none)
+ ("relatedoptions" . none)
+ ("relatedstring" . none)
+ ("relatedtype" . none)
+ ("shorthand" . none)
+ ("shorthandintro" . none)
+ ("sortkey" . none)))
+
+ ("mvbook, book"
+ "inbook, bookinbook, suppbook"
+ (("author" . "author")
+ ("author" . "bookauthor")))
+
+ ("mvbook"
+ "book, inbook, bookinbook, suppbook"
+ (("title" . "maintitle")
+ ("subtitle" . "mainsubtitle")
+ ("titleaddon" . "maintitleaddon")
+ ("shorttitle" . none)
+ ("sorttitle" . none)
+ ("indextitle" . none)
+ ("indexsorttitle" . none)))
+
+ ("mvcollection, mvreference"
+ "collection, reference, incollection, inreference, suppcollection"
+ (("title" . "maintitle")
+ ("subtitle" . "mainsubtitle")
+ ("titleaddon" . "maintitleaddon")
+ ("shorttitle" . none)
+ ("sorttitle" . none)
+ ("indextitle" . none)
+ ("indexsorttitle" . none)))
+
+ ("mvproceedings"
+ "proceedings, inproceedings"
+ (("title" . "maintitle")
+ ("subtitle" . "mainsubtitle")
+ ("titleaddon" . "maintitleaddon")
+ ("shorttitle" . none)
+ ("sorttitle" . none)
+ ("indextitle" . none)
+ ("indexsorttitle" . none)))
+
+ ("book"
+ "inbook, bookinbook, suppbook"
+ (("title" . "booktitle")
+ ("subtitle" . "booksubtitle")
+ ("titleaddon" . "booktitleaddon")
+ ("shorttitle" . none)
+ ("sorttitle" . none)
+ ("indextitle" . none)
+ ("indexsorttitle" . none)))
+
+ ("collection, reference"
+ "incollection, inreference, suppcollection"
+ (("title" . "booktitle")
+ ("subtitle" . "booksubtitle")
+ ("titleaddon" . "booktitleaddon")
+ ("shorttitle" . none)
+ ("sorttitle" . none)
+ ("indextitle" . none)
+ ("indexsorttitle" . none)))
+
+ ("proceedings"
+ "inproceedings"
+ (("title" . "booktitle")
+ ("subtitle" . "booksubtitle")
+ ("titleaddon" . "booktitleaddon")
+ ("shorttitle" . none)
+ ("sorttitle" . none)
+ ("indextitle" . none)
+ ("indexsorttitle" . none)))
+
+ ("periodical"
+ "article, suppperiodical"
+ (("title" . "journaltitle")
+ ("subtitle" . "journalsubtitle")
+ ("shorttitle" . none)
+ ("sorttitle" . none)
+ ("indextitle" . none)
+ ("indexsorttitle" . none))))
+ "Inheritance scheme for BibLaTeX cross-referencing.
+Inheritances are specified for pairs of source and target entry
+type, where the target is the cross-referencing entry and the
+source the cross-referenced entry. Each pair specifies the
+fields in the source and the fields in the target that they
+correspond with.
+
+Inheritances valid for all entry types are defined by specifying
+the entry type as \"all\". The entry type may also be a
+comma-separated list of entry types.
+
+If no inheritance rule is set up for a given entry type+field
+combination, the field inherits from the same-name field in the
+cross-referenced entry. If no inheritance should take place, the
+target field is set to the symbol `none'.")
+
(defconst parsebib--bibtex-identifier "[^^\"@\\&$#%',={}() \t\n\f]+" "Regexp describing a licit BibTeX identifier.")
(defconst parsebib--key-regexp "[^^\"@\\&$#%',={} \t\n\f]+" "Regexp describing a licit key.")
(defconst parsebib--entry-start "^[ \t]*@" "Regexp describing the start of an entry.")
@@ -72,9 +180,9 @@ Defaults to `error'."
(define-error 'parsebib-entry-type-error "Illegal entry type" 'error)
-;;;;;;;;;;;;;;;;;;;;
-;; matching stuff ;;
-;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; matching and parsing stuff ;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun parsebib--looking-at-goto-end (str &optional match)
"Like `looking-at' but move point to the end of the matching string STR.
@@ -171,6 +279,10 @@ double quotes around field values are removed."
(string-join (parsebib--expand-strings (nreverse res) strings))
(string-join (nreverse res) " # "))))
+;;;;;;;;;;;;;;;;;;;;;
+;; expanding stuff ;;
+;;;;;;;;;;;;;;;;;;;;;
+
(defun parsebib--expand-strings (strings abbrevs)
"Expand strings in STRINGS using expansions in ABBREVS.
STRINGS is a list of strings. If a string in STRINGS has an
@@ -187,6 +299,71 @@ multiple spaces in the string are replaced with a single space."
(t str)))
strings))
+(defun parsebib-expand-xrefs (entries inheritance)
+ "Expand cross-referencing items in ENTRIES.
+BibTeX entries in ENTRIES that have a `crossref' field are
+expanded with the fields in the cross-referenced entry. ENTRIES
+is a hash table with entries, which is updated. INHERITANCE
+indicated the inheritance schema. It can be a symbol `BibTeX' or
+`biblatex', or it can be an explicit inheritance schema. See the
+variable `parsebib--biblatex-inheritances' for details on the
+structure of such an inheritance schema."
+ (maphash (lambda (key fields)
+ (let ((xref (cdr (assoc-string "crossref" fields))))
+ (when xref
+ (if (string-match-p (concat "\\b[\"{]" parsebib--key-regexp "[\"}]\\b") xref)
+ (setq xref (substring xref 1 -1)))
+ (let* ((source (gethash xref entries))
+ (updated-entry (parsebib--get-xref-fields fields source inheritance)))
+ (when updated-entry
+ (puthash key updated-entry entries))))))
+ entries))
+
+(defun parsebib--get-xref-fields (target-entry source-entry inheritance)
+ "Return TARGET-ENTRY supplemented with fields inherited from SOURCE-ENTRY.
+TARGET-ENTRY and SOURCE-ENTRY are entry alists. Fields in
+SOURCE-ENTRY for which TARGET-ENTRY has no value are added to
+TARGET-ENTRY. Return value is the modified TARGET-ENTRY.
+
+INHERITANCE is an inheritance schema. It can either be one of
+the symbols `BibTeX' or `biblatex', or it can be an explicit
+inheritance schema. See the variable
+`parsebib--biblatex-inheritances' for details on the structure of
+such an inheritance schema."
+ (when (and target-entry source-entry)
+ (when (eq inheritance 'biblatex)
+ (setq inheritance parsebib--biblatex-inheritances))
+ (let* ((inheritable-fields (unless (eq inheritance 'BibTeX)
+ (cl-third (cl-find-if (lambda (elem)
+ (and (string-match-p (concat "\\b" (cdr (assoc-string "=type=" source-entry)) "\\b") (cl-first elem))
+ (string-match-p (concat "\\b" (cdr (assoc-string "=type=" target-entry)) "\\b") (cl-second elem))))
+ inheritance))))
+ (new-fields (delq nil (mapcar (lambda (field)
+ (let ((target-field (parsebib--get-target-field (car field) inheritable-fields)))
+ (if (and target-field
+ (not (assoc-string target-field target-entry 'case-fold)))
+ (cons target-field (cdr field)))))
+ source-entry))))
+ (append target-entry new-fields))))
+
+(defun parsebib--get-target-field (source-field inheritances)
+ "Return the target field for inheritance from SOURCE-FIELD.
+Inheritance is determined by INHERITANCES, which is an alist of
+source/target pairs. If no inheritance should take place for
+SOURCE-FIELD, the target in the relevant item in INHERITANCES is
+the symbol `none'. If there is no item for SOURCE-FIELD in
+INHERITANCES, SOURCE-FIELD is returned."
+ ;; Note: the argument INHERITANCES differs from the INHERITANCE argument in
+ ;; the previous two functions. It is a simple alist of (source-field
+ ;; . target-field) pairs.
+ (let ((target-field (cdr (assoc-string source-field inheritances 'case-fold))))
+ (cond
+ ((null target-field)
+ source-field)
+ ((eq target-field 'none)
+ nil)
+ (t target-field))))
+
;;;;;;;;;;;;;;;;;;;
;; low-level API ;;
;;;;;;;;;;;;;;;;;;;
@@ -292,7 +469,7 @@ is otherwise not limited to any set of possible entry types. If
so required, the calling function has to ensure that the entry
type is valid.
-If STRINGS is provided it should be a hash table with string
+If STRINGS is provided, it should be a hash table with string
abbreviations, which are used to expand abbrevs in the entry's
fields."
(unless (member-ignore-case type '("comment" "preamble" "string"))
@@ -337,27 +514,6 @@ value."
;; high-level API ;;
;;;;;;;;;;;;;;;;;;;;
-(defun parsebib-collect-strings (&optional hash expand-strings)
- "Collect all @String definitions in the current buffer.
-Return value is a hash with the abbreviations as keys and the
-expansions as values. If HASH is a hash table with test function
-`equal', it is used to store the @String definitions. If
-EXPAND-STRINGS is non-nil, @String expansions are expanded
-themselves using the @String definitions already stored in HASH."
- (save-excursion
- (goto-char (point-min))
- (let* ((res (if (and (hash-table-p hash)
- (eq 'equal (hash-table-test hash)))
- hash
- (make-hash-table :test #'equal)))
- string)
- (cl-loop for item = (parsebib-find-next-item)
- while item do
- (when (cl-equalp item "string")
- (setq string (parsebib-read-string nil (if expand-strings hash)))
- (puthash (car string) (cdr string) res)))
- hash)))
-
(defun parsebib-collect-preambles ()
"Collect all @Preamble definitions in the current buffer.
Return a list of strings, each string a separate @Preamble."
@@ -382,27 +538,66 @@ Return a list of strings, each string a separate @Comment."
(push (parsebib-read-comment) res)))
(nreverse res))))
-(defun parsebib-collect-entries (&optional hash strings)
- "Collect all entries is the current buffer.
+(defun parsebib-collect-strings (&optional hash expand-strings)
+ "Collect all @String definitions in the current buffer.
+Return value is a hash with the abbreviations as keys and the
+expansions as values. If HASH is a hash table with test function
+`equal', it is used to store the @String definitions. If
+EXPAND-STRINGS is non-nil, @String expansions are expanded
+themselves using the @String definitions already stored in HASH."
+ (or (and (hash-table-p hash)
+ (eq 'equal (hash-table-test hash)))
+ (setq hash (make-hash-table :test #'equal)))
+ (save-excursion
+ (goto-char (point-min))
+ (cl-loop with string = nil
+ for item = (parsebib-find-next-item)
+ while item do
+ (when (cl-equalp item "string")
+ (setq string (parsebib-read-string nil (if expand-strings hash)))
+ (puthash (car string) (cdr string) hash)))
+ hash))
+
+(defun parsebib-collect-entries (&optional hash strings inheritance)
+ "Collect all entries in the current buffer.
Return value is a hash table containing the entries. If HASH is
a hash table, with test function `equal', it is used to store the
entries. If STRINGS is non-nil, it should be a hash table of
string definitions, which are used to expand abbreviations used
-in the entries."
+in the entries.
+
+If INHERITANCE is non-nil, cross-references in the entries are
+resolved: if the crossref field of an entry points to an entry
+already in HASH, the fields of the latter that do not occur in
+the entry are added to it. INHERITANCE indicates the inheritance
+schema used for determining which fields inherit from which
+fields. It can be a symbol `BibTeX' or `biblatex', or it can be
+an explicit inheritance schema. (See the variable
+`parsebib--biblatex-inheritances' for details on the structure of
+such an inheritance schema.) It can also be the symbol t, in
+which case the local variable block is checked for a
+dialect (using the variable `bibtex-dialect'), or, if no such
+local variable is found, the value of the variable
+`bibtex-dialect'."
+ (or (and (hash-table-p hash)
+ (eq 'equal (hash-table-test hash)))
+ (setq hash (make-hash-table :test #'equal)))
+ (if (eq inheritance t)
+ (setq inheritance (or (parsebib-find-bibtex-dialect)
+ bibtex-dialect
+ 'BibTeX)))
(save-excursion
(goto-char (point-min))
- (let* ((res (if (and (hash-table-p hash)
- (eq 'equal (hash-table-test hash)))
- hash
- (make-hash-table :test #'equal)))
- entry)
- (cl-loop for entry-type = (parsebib-find-next-item)
- while entry-type do
- (unless (member-ignore-case entry-type '("preamble" "string" "comment"))
- (setq entry (parsebib-read-entry entry-type nil strings))
- (if entry
- (puthash (cdr (assoc-string "=key=" entry)) entry res))))
- hash)))
+ (cl-loop with entry = nil
+ for entry-type = (parsebib-find-next-item)
+ while entry-type do
+ (unless (member-ignore-case entry-type '("preamble" "string" "comment"))
+ (setq entry (parsebib-read-entry entry-type nil strings))
+ (if entry
+ (puthash (cdr (assoc-string "=key=" entry)) entry hash))))
+ (when inheritance
+ (parsebib-expand-xrefs hash inheritance))
+ hash))
(defun parsebib-find-bibtex-dialect ()
"Find the BibTeX dialect of a file if one is set.
@@ -420,32 +615,47 @@ file. Return nil if no dialect is found."
(string-match (concat "bibtex-dialect: " (regexp-opt (mapcar #'symbol-name bibtex-dialect-list) t)) comment))
(intern (match-string 1 comment))))))))
-(defun parsebib-parse-buffer (&optional entries-hash strings-hash expand-strings)
+(defun parsebib-parse-buffer (&optional entries strings expand-strings inheritance)
"Parse the current buffer and return all BibTeX data.
Return list of five elements: a hash table with the entries, a
hash table with the @String definitions, a list of @Preamble
definitions, a list of @Comments and the BibTeX dialect, if
present in the file.
-If ENTRIES-HASH is a hash table with test function `equal', it is
-used to store the entries. Any existing entries with identical
-keys are overwritten. Similarly, if STRINGS-HASH is a hash table
-with test function `equal', the @String definitions are stored in
-it.
+If ENTRIES is a hash table with test function `equal', it is used
+to store the entries. Any existing entries with identical keys
+are overwritten. Similarly, if STRINGS is a hash table with test
+function `equal', the @String definitions are stored in it.
If EXPAND-STRINGS is non-nil, abbreviations in the entries and
@String definitions are expanded using the @String definitions
-already in STRINGS."
+already in STRINGS.
+
+If INHERITANCE is non-nil, cross-references in the entries are
+resolved: if the crossref field of an entry points to an entry
+already in ENTRIES, the fields of the latter that do not occur in
+the entry are added to it. INHERITANCE indicates the inheritance
+schema used for determining which fields inherit from which
+fields. It can be a symbol `BibTeX' or `biblatex', which means
+to use the default inheritance schema for either dialect, or it
+can be an explicit inheritance schema. (See the variable
+`parsebib--biblatex-inheritances' for details on the structure of
+such an inheritance schema.) It can also be the symbol t, in
+which case the local variable block is checked for a
+dialect (using the variable `bibtex-dialect'), or, if no such
+local variable is found, the value of the variable
+`bibtex-dialect'."
(save-excursion
(goto-char (point-min))
- (let ((entries (if (and (hash-table-p entries-hash)
- (eq (hash-table-test entries-hash) 'equal))
- entries-hash
- (make-hash-table :test #'equal)))
- (strings (if (and (hash-table-p strings-hash)
- (eq (hash-table-test strings-hash) 'equal))
- strings-hash
- (make-hash-table :test #'equal)))
+ (or (and (hash-table-p entries)
+ (eq (hash-table-test entries) 'equal))
+ (setq entries (make-hash-table :test #'equal)))
+ (or (and (hash-table-p strings)
+ (eq (hash-table-test strings) 'equal))
+ (setq strings (make-hash-table :test #'equal)))
+ (let ((dialect (or (parsebib-find-bibtex-dialect)
+ bibtex-dialect
+ 'BibTeX))
preambles comments)
(cl-loop for item = (parsebib-find-next-item)
while item do
@@ -462,7 +672,8 @@ already in STRINGS."
(let ((entry (parsebib-read-entry item nil (if expand-strings strings))))
(when entry
(puthash (cdr (assoc-string "=key=" entry)) entry entries))))))
- (list entries strings preambles comments (parsebib-find-bibtex-dialect)))))
+ (when inheritance (parsebib-expand-xrefs entries (if (eq inheritance t) dialect inheritance)))
+ (list entries strings preambles comments dialect))))
(provide 'parsebib)