summaryrefslogtreecommitdiff
path: root/parsebib.el
diff options
context:
space:
mode:
authorJoost Kremers <joostkremers@fastmail.fm>2017-03-10 20:07:25 +0100
committerJoost Kremers <joostkremers@fastmail.fm>2017-03-10 20:07:25 +0100
commit8ba097de5c169f0623cc30d702baddbf0cf40da9 (patch)
tree3f508b3688c0153d3624b367aa5b3d2dcaa8b30c /parsebib.el
parent277d3ce121aa5913413b53abe41c627c1ced8491 (diff)
Add `parsebib-parse-buffer'.
Diffstat (limited to 'parsebib.el')
-rw-r--r--parsebib.el43
1 files changed, 43 insertions, 0 deletions
diff --git a/parsebib.el b/parsebib.el
index 2fcc643..12b2cf2 100644
--- a/parsebib.el
+++ b/parsebib.el
@@ -385,6 +385,49 @@ in the entries."
(puthash (cdr (assoc-string "=key=" entry)) entry res))))
hash)))
+(defun parsebib-parse-buffer (&optional entries-hash strings-hash expand-strings)
+ "Parse the current buffer and return all BibTeX data.
+Return list of four elements: a hash table with the entries, a
+hash table with the @String definitions, a list of @Preamble
+definitions, and a list of @Comments.
+
+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 EXPAND-STRINGS is non-nil, abbreviations in the entries and
+@String definitions are expanded using the @String definitions
+already in STRINGS."
+ (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)))
+ preambles comments)
+ (cl-loop for item = (parsebib-find-next-item)
+ while item do
+ (cond
+ ((cl-equalp item "string") ; `cl-equalp' compares strings case-insensitively.
+ (let ((string (parsebib-read-string nil (if expand-strings strings))))
+ (if string
+ (puthash (car string) (cdr string) strings))))
+ ((cl-equalp item "preamble")
+ (push (parsebib-read-preamble) preambles))
+ ((cl-equalp item "comment")
+ (push (parsebib-read-comment) comments))
+ ((stringp item)
+ (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))))
+
(defun parsebib-find-bibtex-dialect ()
"Find the BibTeX dialect of a file if one is set.
This function looks for a local value of the variable