![]() |
Editing databases | ![]() |
A database is edited as a TeXmacs document whose body is a list of db-entry tags (see The Scheme database layers). There are three kinds of such documents:
the virtual document tmfs://db/kind/file shows the result of a query on the user database of the given kind. Saving it stores the modified entries back into the database. This is what Data\(\rightarrow\)Open bibliography and Data\(\rightarrow\)Open AI agents open.
any document with the style database-bib (for instance a .bib file opened through the tmbib format) can be edited with the same tools. Its entries are not in the database, but can be imported into it.
the virtual document tmfs://biblio/prefix/file shows the bibliographic entries attached to a document (see Bibliographies).
All the database UI is only reachable when the preference "database tool" is on: the Data menu (both in the menu bar and in the compact main menu) is guarded by with-database-tool? in
texmacs/menus/main-menu.scm
, and many actions test supports-db?. The modules are loaded lazily from
init-texmacs.scm
(lazy-menu, lazy-define, lazy-tmfs-handler and lazy-keyboard).
The package database (file
styles/test/database.ts
) defines the rendering of db-entry, db-folded-entry, db-pretty-entry, db-field, db-field-optional, db-field-alternative, db-pretty and db-result, and the variable db-kind. The styles database-bib and database-ai-agents use this package and set db-kind; database-bib moreover defines the markup for names in author fields (name-von, name-jr, name-sep) and for the raw BibTEX constructs that may occur in a converted .bib file (bib-comment, bib-preamble, bib-string, …). For a database kind kind, db-get-style returns the style name database-kind.
The modes are defined in
kernel/texmacs/tm-modes.scm
and in the kind modules:
the style has the variable database-style, i.e. uses the database package;
the style is database-bib;
moreover inside an author or editor field (
bib-menu.scm
);
the style is database-ai-agents (
ai-agents-menu.scm
).
In these modes
db-menu.scm
replaces the Insert menu and the mode icons by database specific ones (new entry of each type of the kind, import or confirmation of entries), and adds Focus menus to insert and remove fields.
db-tmfs.scm
registers the handlers for the db class of tmfs URLs. The name part of the URL is parsed by name->query: leading components of the form var=val are query parameters, the next component is the kind and the rest is a file name. The file name is only used for the title (global yields “My bibliographic database”); the documents always show (user-database kind).
Builds the query from the parameters of the URL and the per-document preferences returned by db-get-current-query (search, order, direction, limit, present, stored as preferences named param,url by db-set-query-preference). The search string is split at commas into :match constraints (words of less than two characters are ignored), a "type" constraint restricts the results to the types of the kind, and each order attribute gives an :order constraint. The resulting entries are presented as db-entry, db-folded-entry or db-pretty-entry depending on the present parameter, in a document with style database-kind.
Calls db-confirm-entries-in on the document, which commits all complete entries (see below).
The toolbar of
db-menu.scm
(db-toolbar, shown at the bottom of the window by db-show-toolbar for tmfs://db buffers) edits the query preferences and reverts the buffer. Hence changing the search string or the order simply reloads the virtual document.
(make-db-entry type) inserts a new entry after the current paragraph, with a fresh identifier from db-create-id, meta fields contributor (the default user), modus ("manual") and date, and calls db-complete-fields, which uses the format of the type in db-format-table to add empty mandatory fields (db-field), alternatives (db-field-alternative) and optional fields (db-field-optional). The cursor is placed in the name of the entry.
The keyboard handlers of
db-edit.scm
implement a “fill out the form” interaction through kbd-enter:
in the name, ↩ moves to the first field (an empty name is refused);
in a mandatory field, ↩ refuses empty values and moves to the next field;
in an optional field, ↩ removes the field if it is empty, and otherwise turns it into an ordinary field;
in an alternative field, ↩ checks that exactly one of the alternatives is filled out and removes the other ones;
after the last field, keep-completing either moves to the first empty field or, when the entry is complete and the buffer is a database view, confirms the entry.
In author and editor fields of bibliographic entries,
bib-kbd.scm
adds shortcuts: a comma or “and” starts a new name (name-sep), ↩ converts a plain list A. Einstein and N. Bohr into structured names with name markup for the last names, and Insert\(\rightarrow\)Particle or Insert\(\rightarrow\)Title suffix insert name-von and name-jr.
An entry of a database view is committed to the database when it is confirmed, either explicitly (⌥↩, kbd-alternate-enter, or the confirmation icon), or when the buffer is saved. confirm-entry computes the new entry with entry->assoc-list and calls db-update-entry with extra fields contributor and modus ("manual") and with time stamps. If the entry changed, it gets a new identifier, which is written back into the document together with the new meta fields. Before committing, detach-buffer-entries gives fresh identifiers to entries which occur several times in the buffer with the same identifier (for instance after copy and paste), so that the copies become independent entries.
Structured deletion of an entry (⌥⌫, ⌥⌦, structured-remove-horizontal) removes it from the database (remove-entry) and from the document. Removal only sets the expiration date of the lines, so an entry can be recovered with with-time.
In an ordinary document (not a database view), Insert\(\rightarrow\)Import entry and Insert\(\rightarrow\)Import selected entries import the entries into the user database of the kind, through db-import-this-entry and db-import-selection, which are redefined by the bibliographic kind.
db-widgets.scm
implements a search dialogue:
Opens a dialogue (or a side tool if side tools are enabled) with an input field and a result area. Each keystroke schedules, after 200 ms of inactivity, a query ((:completes text) ("type" …)) limited to 20 results on db. The results are pretty-printed with db-pretty in the auxiliary buffer tmfs://aux/db-search-results; clicking a result (db-confirm-result) calls call-back with the name of the entry. Queries and loaded entries are cached for the life of the dialogue. The result areas can be resized.
For bibliographies, open-bib-chooser opens this dialogue on (bib-database). It is used by the focus search tool inside citations (focus-open-search-tool in
bib-menu.scm
, Focus\(\rightarrow\)Search references) and by the alternate tab key (kbd-alternate-tab, which calls kbd-alternate-variant) in a citation, while ⇥ (kbd-variant in
bib-kbd.scm
) completes the citation key with index-get-name-completions. For the kind "bib", the dialogue also lists the matching references of Zotero after those of the database, puts the source of each reference before it, and names its sources in a line above the input field (see Citations from Zotero).
When the database tool is off, ⇥ in a citation completes keys from the .bib file of the document (citekey-completions in
bibtex/bib-complete.scm
) and from Zotero, and the same dialogue is opened on the marker :bib-file instead of a database (zotero-open-search-tool, through the definitions of focus-can-search? and focus-open-search-tool for citations in
generic-edit.scm
): db-search-results then lists the entries of the .bib file of the bibliography, and those of Zotero.
The "Pretty" presentation and the search results use the function (db-pretty l kind fm), which is the identity by default. For bibliographies (
bib-manage.scm
) it converts the entries to BibTEX entries and formats them with the internal siam style, turning each item into (db-result name text). In a database view, db-pretty-entry calls the secure Scheme function ext-db-pretty-entry through extern, which caches the result per kind (
db-markup.scm
); clicking the key of a pretty entry (db-pretty-notify) turns it back into an editable db-entry.
(open-identities) edit the users of users-master.tmdb; the pseudo and full name of the default user are used as contributor of new entries.
(open-db-preferences) currently only contain the preference "auto bib import", which controls the automatic importation of bibliographies attached to documents.
(Data\(\rightarrow\)Storage) selects the file used as the user database of the current kind (use-database).
(Data\(\rightarrow\)Import, Data\(\rightarrow\)Export) are dispatched through db-import-select, db-export-select, db-import-file and db-export-file, which do nothing by default in
db-convert.scm
and are redefined in the mode in-bib? by
bib-manage.scm
. Recently used files are remembered with learn-interactive (db-recent-imports, db-recent-exports).
Saving a database view commits all complete entries of the buffer; incomplete entries are kept in the buffer and the user is asked to complete them.
Every confirmed modification creates a new identifier. Code that keeps identifiers of entries across edits (for instance in other documents) should refer to entries by name rather than by identifier.
Query preferences are stored in the user preferences file, one set per tmfs://db URL.
The lazy declaration (lazy-define (database db-widget) open-db-chooser) in
init-texmacs.scm
names a module db-widget which does not exist (the file is
db-widgets.scm
). In practice
db-widgets.scm
is loaded through
db-menu.scm
before the chooser is needed, and, without the database tool, by
bibtex/zotero-db.scm
.
No lazy tmfs handler is declared for biblio: tmfs://biblio/… URLs only work once
bib-local.scm
has been loaded, normally through open-biblio.