![]() |
The TeXmacs database and bibliographies | ![]() |
TeXmacs contains a small embedded database engine which is used for several unrelated purposes: the bibliographic database of the user, the list of user identities, the state of the TeXmacs server, the bookkeeping of file and database synchronization, a registry of AI agents and a few internal caches. The engine itself is written in C++ and is deliberately minimalistic: it stores triples (identifier, attribute, value) of strings together with a creation and an expiration date, it can search them, and it keeps the whole history of all modifications. All higher level notions (entry types, encodings of values as TeXmacs snippets, users and permissions, versions of entries, editing of entries as documents) are implemented in Scheme as successive layers on top of the engine.
Bibliographies are the main client of this machinery, but they are also older than it: TeXmacs could already produce bibliographies from BibTEX files, either by running the external bibtex program or with an internal reimplementation of the standard BibTEX styles in Scheme, before the database existed. As a consequence, the bibliography pipeline has two main modes, depending on whether the database tool is enabled (preference "database tool", Tools\(\rightarrow\)Database tool), and within each mode it may use either the external BibTEX or the internal style engine.
This chapter describes the implementation of both. It complements the older Scheme API reference TeXmacs databases and the guide Writing TeXmacs bibliography styles, which are not repeated here; see also the user manual page Compiling a bibliography and the documentation of the citation markup. The synchronization of databases between a client and a TeXmacs server is described in Synchronization of files and databases, and the use of the database by the server in The remote file system.
From bottom to top, the implementation consists of the following layers.
The class database in
Plugins/Database/
holds an in-memory table of lines (db_line), each line associating a value to an attribute of an identifier during a time interval. All strings are interned as integer atoms. The engine maintains indices from identifiers and values to lines, a keyword index for full text search, and a prefix index for the completion of entry names. Modifications are journaled into an append only binary file with extension .tmdb. A small set of functions taking the URL of the database as their first argument (set_field, get_entry, query, …) are exported to Scheme as tmdb-set-field, tmdb-get-entry, tmdb-query, etc.
The module
database/db-base.scm
wraps the glue into the functions db-set-field, db-get-entry, db-search, etc., whose implicit arguments (the current database, the current time, a limit on the number of results, extra fields to add to new entries) are specified with the context macros with-database, with-time, with-limit, with-extra-fields and with-time-stamp.
The modules
db-format.scm
(encoding of values, entry formats and kinds of databases),
db-users.scm
(users, groups and permissions) and
db-version.scm
(versions of entries and importation) redefine the basic functions using tm-define and former. Each layer adds one context variable (with-encoding, with-user) while keeping the semantics of the basic routines.
The module
db-convert.scm
converts between database entries and the markup db-entry/db-field;
db-edit.scm
implements the structured editing of this markup;
db-tmfs.scm
presents a database as a virtual document with URL tmfs://db/kind/file;
db-menu.scm
and
db-widgets.scm
implement the Data menu, the search toolbar, the search dialogue and the identities dialogue.
The bibliographic kind ("bib") is implemented by
bib-db.scm
(entry formats and conversions between BibTEX and database entries),
bib-manage.scm
(caching of .bib files as databases, importation, exportation, retrieval of entries and compilation of bibliographies),
bib-local.scm
,
bib-menu.scm
and
bib-kbd.scm
. A second, much smaller kind, "ai-agents", is implemented by
ai-agents-db.scm
and
ai-agents-menu.scm
.
Independently of the database, the C++ files
Data/Convert/BibTeX/parsebib.cpp
(parser) and
conservative_bib.cpp
(conservative import and export),
Plugins/Bibtex/bibtex.cpp
(running the external program and reading .bbl files) and
bibtex_functions.cpp
(C++ versions of the BibTEX built-in functions such as purify$ or format.name$), and the Scheme style engine in
progs/bibtex/
(
bib-utils.scm
and one file per style) implement the BibTEX side.
The typesetter collects the keys of citations into the auxiliary data of the buffer; the C++ editor routine edit_process_rep::generate_bibliography in
Edit/Process/edit_process.cpp
selects one of the strategies described in Bibliographies and inserts the resulting bib-list into the body of the bibliography tag.
src/src/)
Plugins/Database/database.hpp
(data structures and public functions),
database.cpp
(atoms, basic operations, table of open databases),
db_disk.cpp
(journal, persistence, compression, concurrent access),
db_index.cpp
(keywords and completions),
db_query.cpp
(queries) and
db_sort.cpp
(sorting of results). The glue is declared in
Scheme/Glue/build-glue-basic.scm
(section ;; native TeXmacs databases).
Plugins/Sqlite3/
contains an unrelated and currently unused interface to SQLite.
src/TeXmacs/progs/database/)
db-base.scm
,
db-format.scm
,
db-users.scm
,
db-version.scm
,
db-edit.scm
,
db-convert.scm
,
db-markup.scm
,
db-tmfs.scm
,
db-widgets.scm
,
db-menu.scm
,
bib-db.scm
,
bib-manage.scm
,
bib-local.scm
,
bib-menu.scm
,
bib-kbd.scm
,
ai-agents-db.scm
,
ai-agents-menu.scm
. The same directory also contains
title-markup.scm
and
title-transform.scm
, which have nothing to do with databases (they implement the rendering of document titles and author data).
The editing styles
database.ts
,
database-bib.ts
,
database-ai-agents.ts
and the BibTEX presentation style
bibliography.ts
live in
src/TeXmacs/styles/test/
. The citation and bibliography markup is defined in
packages/standard/std-automatic.ts
and
packages/section/section-base.ts
.
Data/Convert/BibTeX/parsebib.cpp
,
Data/Convert/BibTeX/conservative_bib.cpp
,
Plugins/Bibtex/bibtex.cpp
,
Plugins/Bibtex/bibtex_functions.cpp
, and
Edit/Process/edit_process.cpp
(generation of the bibliography).
progs/convert/bibtex/
(the bibtex and tmbib formats and their converters),
progs/bibtex/bib-utils.scm
(style engine),
plain.scm
,
abbrv.scm
,
abstract.scm
,
acm.scm
,
alpha.scm
,
elsart-num.scm
,
ieeetr.scm
,
siam.scm
,
unsrt.scm
(styles),
bib-complete.scm
(completion of citation keys without the database) and
bib-widgets.scm
(the bibliography insertion dialogue). The file
src/TeXmacs/misc/bib/texmacs.bib
contains the entries with keys TeXmacs:… used by cite-TeXmacs.
All databases are ordinary files with extension .tmdb. The following files are created on demand below $TEXMACS_HOME_PATH (usually ~/.TeXmacs):
User identities, the default user and, for each user and each database kind, the preferred database file (
db-users.scm
).
The default per-user database of a given kind, for instance users/jdoe/jdoe-bib.tmdb for the bibliographic database. The user may select another file in Data\(\rightarrow\)Storage.
The cache of imported .bib files: for each cached file, a copy id.bib of the source, its conversion id.tm into a TeXmacs document, and a database id.tmdb containing the entries that were actually cited (
bib-manage.scm
).
Time stamps used by the literate programming tools (
utils/literate/lp-build.scm
).
The state of a TeXmacs server (global-database).
Not a database, but the working directory of the external bibtex program: temp.aux, temp.bbl, temp.log, auto.bib and copies of .bst files.
In addition, a document may carry bibliographic entries as attachments named prefix-bibliography (entries retrieved when the bibliography was last compiled) and prefix-biblio (entries edited locally), see Bibliographies.
Stable and used by default: the BibTEX parser, the conversion between .bib files and TeXmacs documents, the internal style engine with the styles tm-plain, tm-abbrv, tm-alpha, etc., and the invocation of the external bibtex program. These do not need the database tool.
Stable but only active with the database tool: the engine, the Scheme layers, the bibliographic database with its editing interface, the caching of .bib files, the attachment of bibliographies to documents and the automatic importation of attached entries. The preference "database tool" is "off" by default (
texmacs/texmacs/tm-server.scm
). The engine itself is always compiled in and is used unconditionally by the server, the client synchronization code, the identities dialogue and
lp-build.scm
.
Experimental or recent: the "ai-agents" kind, the local bibliography editor (tmfs://biblio/…, open-biblio), the synchronization of the bibliographic database with a server, and the permission layer (which is only enforced for reading and for owners, see The Scheme database layers).