GNU TeXmacsUser manualReferenceAboutEnglish
The TeXmacs database and bibliographies

1.Introduction

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.

2.Architecture overview

2.1.The layers

From bottom to top, the implementation consists of the following layers.

The engine (C++)

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 basic Scheme API

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.

Customization layers (Scheme)

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.

Databases as documents

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.

Database kinds

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

.

BibTEX support

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 bibliography pipeline

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.

2.2.Map of the source files

Engine (C++,
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.

Scheme database modules (
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).

Styles

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

.

BibTEX (C++)
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).

BibTEX (Scheme)
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.

2.3.Where the data lives

All databases are ordinary files with extension .tmdb. The following files are created on demand below $TEXMACS_HOME_PATH (usually ~/.TeXmacs):

users/users-master.tmdb

User identities, the default user and, for each user and each database kind, the preferred database file (

db-users.scm

).

users/uid/pseudo-kind.tmdb

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.

system/database/bib-master.tmdb and system/database/bib/

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

).

system/database/lp-master.tmdb

Time stamps used by the literate programming tools (

utils/literate/lp-build.scm

).

server/global.tmdb

The state of a TeXmacs server (global-database).

system/bib/

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.

2.4.Status

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".