![]() |
Citations from Zotero | ![]() |
TeXmacs can take its bibliographic references from the library of the Zotero desktop application. Zotero (version 7 or later) serves its library at http://localhost:23119/api/, with the requests of the Zotero web API (version 3), once Allow other applications on this computer to communicate with Zotero is enabled in its advanced settings. Reading needs no key. TeXmacs only reads: it never writes to Zotero.
Zotero is one more source of references, after those of the user:
without the database tool, through a managed BibTEX file, which TeXmacs writes from Zotero, since the C++ bibliography generator only reads .bib files;
with the database tool, through a new source :zotero of bib-retrieve-entries, after the database of the user; the entries which enter the database from Zotero are then kept in sync with it.
The citations use the citation keys of Zotero (the field citationKey, filled by Zotero or by the Better BibTeX extension). The user side is described in Citations from Zotero; the bibliography machinery which this chapter extends in Bibliographies. The design, with the situations which it handles and the decisions taken, is in
src/docs/zotero-design.md
.
bibtex/zotero.scm
Requests to Zotero, the state of Zotero, libraries, items and citation keys, the resolution of keys, the export of BibTEX, the citations of a document or project, the managed BibTEX files, renamed keys, completion. It does not load the modules of the database, and uses
convert/bibtex/bibtextm.scm
to read BibTEX files.
bibtex/zotero-db.scm
Zotero as a source of the database: conversion of items into database entries, the source :zotero, the sync of imported entries, conflicts, adoption of copies, renamed entries, and the search window of references (in both modes). It uses
database/db-base.scm
,
db-convert.scm
,
bib-db.scm
,
bib-manage.scm
and
db-widgets.scm
, so it is only loaded when one of its functions is called.
bibtex/zotero-widgets.scm
The dialogs: the window of the entries changed on both sides, Check against Zotero… and the settings.
Everything else reaches these modules through lazy definitions in
init-texmacs.scm
(lazy-define for the functions, lazy-menu for the dialogs), so that nothing is loaded before Zotero is used. The hooks in other files are:
generic/document-edit.scm
update-document calls (zotero-before-update what) first, which refreshes a managed file and syncs the database.
database/bib-manage.scm
The source :zotero in bib-retrieve-entries-from-one and bib-get-db, and the order of the sources, bib-sources, used by bib-compile-sub and bib-attach.
database/db-format.scm
The meta attributes of the entries from Zotero, in db-meta-attributes.
database/db-widgets.scm
The results of Zotero and the sources line in the search window of references.
generic/generic-edit.scm,
database/bib-kbd.scm
Completion of keys with zotero-completion-suffixes, and the search window of citations without the database tool.
generic/generic-menu.scm,
generic/document-menu.scm
The entries Search references and Show in Zotero of the focus menus, and the Zotero entries of Document\(\rightarrow\)Bibliography.
Runs curl on server/api/path, where server is the preference "zotero server", with the header Zotero-API-Version: 3 and a time limit of 1.5 seconds for interactive requests (completion, search as you type) and of 20 seconds otherwise. It returns (status body version): the HTTP status, or 0 when Zotero cannot be reached; the body, in utf8; the version of the library (the header Last-Modified-Version, read with --write-out "%header{last-modified-version}", which needs curl 7.84), or #f. The suite of tests replaces this function.
The paths start with the library: users/0 for the library of the user, groups/id for a group library. The requests used are items/top?format=json&q=… (search, which also matches prefixes of citation keys), items/key, items?itemKey=… with the formats json, versions and bibtex (or biblatex), at most 50 keys per request, and users/0/groups.
The library may also be read from the web API of zotero.org (https://api.zotero.org/), with an API key of the user. The preference "zotero source" chooses: "local" (the application), "web", or "auto" (zotero-web?: zotero.org in a web browser, where zotero-in-browser? holds, the application elsewhere). The application cannot be used from a web page: its server closes every request which carries an Origin header (local API, connector, Better BibTeX), and the Zotero Connector offers nothing to pages.
The key is kept in the wallet when it is on, else in the preference "zotero api key" (zotero-api-key, zotero-set-api-key). The user it belongs to is asked once (keys/current) and remembered in "zotero user". The library of the user stays users/0 in TeXmacs (in the documents and the database), and becomes users/id in the requests. A request without key answers 401 (state no-key); a key refused gives forbidden.
As for the keys of the AI engines, the key is asked when it is needed (zotero-ask-key): when the wallet is there but closed, its dialog opens first (it may hold the key), else a dialog asks for the key (zotero-key-dialog); the operation which needed it is then run again. The commands ask before they run (zotero-command), the search window when it opens (zotero-search-opened), and update-document after it ran, only if it needed Zotero (zotero-key-wanted: zotero-ready? notes that the key lacked); once declined, the updates do not ask again. A key given while the wallet is closed opens it first, to keep the key there.
The requests are made by curl on the desktop, with the headers in a temporary file (--header @file), so that the key is never on a command line; in a browser, by a synchronous XMLHttpRequest written in JavaScript and run by web-javascript, which answers with the status, the header Last-Modified-Version and the body in base64 (decoded by decode-base64: the body stays the bytes of utf8). zotero.org sends the headers which this needs (CORS).
The searches of zotero.org (q=) only look at the titles, the creators and the years, all the words of the query being required, also with qmode=everything (which adds the full text, not the other fields): the citation keys, the field extra, the publishers and the URLs are never searched (checked on the public library users/475425). A citation key is therefore found by its item when it is known (see The items of a document), and otherwise by the words "author year" which start and end a key of Better BibTeX (key-author-year: barashkovF43MeasureGirsanovs2020 gives barashkov 2020, a letter after the year is skipped), keeping the item whose key is exactly the one asked for. The completion finds the keys which start with the name of an author for the same reason. Show in Zotero opens the page of the item on zotero.org (zotero-web-url).
A synchronous XMLHttpRequest stops the page until zotero.org answers, and a browser allows no time limit for it. So the operations which can wait run with a retry:
Runs thunk; in a web browser, its requests are made with fetch (zotero-start-request) and answer (pending "" #f) at once when their answer is not known yet. The answers come back through TeXmacs.later to zotero-async-answer, into a cache of answers (5 seconds for the request of the state, 60 seconds for the others, forgotten when a library changed); when nothing is awaited any more, the procedures retry of the operations which waited are called, and find their answers in the cache. A request made without retry is synchronous, as on the desktop.
The search window (which shows its search and its sources again), the completion (which completes again if the cursor did not move), zotero-before-update (which answers wait, so that update-document stops and is called again) and the commands (zotero-command) use it. An awaited answer is never taken for a fact: zotero-check-missing sees no deleted item, the sync changes nothing, and the caches of keys, completions and groups are not filled (zotero-asking?). The converted references are kept per item and version, so that the bibliography made with the database, which asks Zotero while it is made, finds them once zotero-before-update has asked for them.
While answers of zotero.org are awaited, the footer says what is asked (zotero-progress-message): the newest request, described by zotero-request-label from its URL (searching query, fetching, exporting or looking for changes of n references, checking the key, the library or the groups), the number of the others, the seconds spent after two seconds, and after ten seconds that zotero.org is slow. A ticker (delayed with :pause) shows it again each second while answers are awaited. When the last answer comes, zotero-answered-message gives the time spent since the first request of the series, or the failure (zotero-status-message of the first status which was not 200); the operations which waited run after it, and may set their own message.
The footer of a document is not drawn again while a dialog has the keyboard focus (edit_interface_rep::apply_changes updates the menus and the footer only when idle_time grows, which it does not without the focus). The search window therefore shows the progress itself: its line of sources gives zotero-progress-message while answers are awaited (the ticker refreshes it, refresh-now "db-search-sources"), and zotero-searching-results adds Searching zotero.org… to the results which wait. zotero-waiting? tells whether the operation being run (its retry) waits for answers: unlike zotero-asking?, which is true while any answer is awaited, it only holds for results which will be shown again.
zotero-status asks Zotero for one key, and remembers the answer: ready for 5 seconds, disabled (status 403: the local API is off) for a minute, not-running or error for 30 seconds. While Zotero is known not to answer, no request is made: menus, completion and typing never wait for it. zotero-forget-state forgets the state; the explicit commands call it first.
The keys found in Zotero are remembered (zotero-find-key) while no library changes: each answer carries the version of its library, and a new version forgets all the keys. The status request notes the version of the library of the user, and zotero-find-key asks for the status before using its cache, so that a key renamed in Zotero is not served from the cache for more than a few seconds.
json->tree gives objects as (attr key value …) and arrays as (tuple …) (zotero-json, zotero-attr-ref); its strings are in utf8, and are converted to Cork with utf8->cork. The search and the queries by key also return notes, attachments and annotations, which are left out.
An item is represented by the list
(key item title creators year version library) |
(accessors zotero-entry-key, zotero-entry-item, …, zotero-entry-library), in Cork. An item without citation key is cited as zotero:item in the library of the user, and as zotero:gid:item in a group library (zotero-derived-item); its exported BibTEX entry, to which Zotero gives a key of its own, is rewritten to that key.
The libraries searched are given by the preference "zotero libraries": "user" (the default) or "all", which adds the groups of the user (zotero-groups, remembered for a minute). The library of the user comes first, so its keys win; zotero-key-libraries lists the libraries which have a key.
The item with the citation key key, or #f: a direct request for a derived key; otherwise the item of the key when it is known (zotero-item-of, asked for by items/item and kept if it still has the key); otherwise a search of each library (find-in-library: on zotero.org first "author year", then the key itself, which the application matches), keeping the exact match. zotero-resolve resolves a list of keys: the keys whose item is known are asked for first, by library and at once (items?itemKey=…, resolve-by-items!), and nothing is searched while those answers are awaited (zotero-waiting?). zotero-export exports items as BibTEX (one request per library and per 50 items), zotero-item-versions gives the versions of items (an item which is not returned has been deleted).
The sources of a bibliography come in one order, given by bib-sources in
bib-manage.scm
:
:local, the entries of the document;
the BibTEX files of the user;
:default, the database of the user;
the managed BibTEX files;
:zotero;
:attached, the entries attached to the document, which serve when Zotero is not running.
Without the database tool, only the file of the bibliography is read, by C++: if it is managed, it holds the items of Zotero; if it is the user's, the references of Zotero which it lacks are added to it (see below).
A managed file starts with the line % Exported from Zotero by TeXmacs on date; replaced by … (zotero-managed-file?); any other file is the user's. It holds exactly the items which the document cites and which no earlier source has (zotero-write-bibliography). zotero-before-update refreshes it before the bibliography is generated; when Zotero is not available, the file is kept, and the message gives the date of the export. Update from Zotero (zotero-update-bibliography) inserts a bibliography when there is none: with a file document-zotero.bib without the database tool, and without file with it (its references are then in the document, see below).
Without the database tool, a bibliography without file (as Insert\(\rightarrow\)Automatic\(\rightarrow\)Bibliography may insert it) or whose file does not exist yet has no references. zotero-before-update then exports the references of Zotero into its file when Zotero has some of the citations (fill-missing-bibliography): a bibliography without file is given the file document-zotero.bib (the third child of the tag is set), a missing file is created under its name. Zotero is not asked about when the key of zotero.org is missing, so that a user without Zotero is never asked for it. zotero-file-citations puts given keys in the file of the bibliography by the same rules (added to a file of the user, exported into a file of Zotero).
The dates are written as YYYY-MM-DD by zotero-iso-date: pretty-date knows no ISO format.
(zotero-add-to-bib-file file keys) appends to the user's BibTEX file the references of Zotero which it lacks, among the cited keys, each after a line
% Added from Zotero by TeXmacs on date: zotero://select/library/items/item |
(zotero://select/groups/id/items/item for a group). The rest of the file is kept byte for byte, and an added reference is not changed later. zotero-before-update and zotero-update-bibliography call it without the database tool, unless the preference "zotero add to bib file" is off. zotero-bib-file-items reads the comments back as (key item library), which zotero-check-missing adds to the items recorded with the document: a key renamed in Zotero is found even from another document which uses the same file.
(zotero-db-entries names) resolves the names, exports the items and converts them with zealous-bib-import into database entries. Since bib-attach stores the cited entries in the attachment prefix-bibliography of the document, a document keeps its references from Zotero, which serve when Zotero is not running; with "auto bib import", they enter the database of the user when the document is opened.
The item key of Zotero never changes, while a citation key may, and cannot be searched on zotero.org: the item is the anchor of a citation. The attachment zotero-items of a document holds a (tuple key item library) for each key resolved through Zotero (zotero-record-items, zotero-recorded-items), in both modes; the attachment is only rewritten when a record changes. It is what allows to find an item again when its key changed, and Show in Zotero to work without asking Zotero.
(zotero-item-of key) gives the (item library) of a key from, in this order, the attachment, the comments of the user's BibTEX file (zotero-bib-file-items), and the items which Zotero gave during the session: every answer converted by items-entries (searches, completions, exports) notes the item of each key (seen-items, forgotten by zotero-forget-keys).
Called by the search window when a reference is chosen for a citation of buffer (the buffer is captured when the window opens: the callback runs while the results are the current buffer). When Zotero gave the item of key during the session (zotero-seen?), the item is recorded with the document and the reference is copied where the bibliography reads it: imported into the database with the database tool (import-entry, synced from then on), else put in the file of the bibliography (zotero-file-citations). It runs as a command (zotero-command), so that it waits for zotero.org in a browser. Defined in
zotero-db.scm
.
The entries from Zotero carry the contributor Zotero and the meta attributes
the key of the item;
its library (users/0 or groups/id; the first entries had user, read as users/0);
the version of the item when it was exported;
the fields as they were then, to tell later which side changed a field;
the key in Zotero, when it was renamed there (the entry keeps its name, so that the citations still work);
yes when the item is no longer in Zotero (the entry is kept).
Being meta attributes (db-meta-attributes), they never reach BibTEX, and two versions of an entry which only differ by them are the same for the versioning.
Unless force?, nothing happens when the versions of the libraries did not change since the last sync (preference "zotero sync version"). Otherwise, library by library, the imported entries whose item has a newer version are exported again: an entry left as imported gets a new version (db-update-entry, which supersedes the old one explicitly), an entry edited in TeXmacs (modus manual) is reported as a conflict. Returns the names updated, renamed and deleted, and the conflicts.
For a conflict, zotero-conflict-fields lists the fields which differ, with the TeXmacs value, the Zotero value and the value at the last sync; by default, a field changed only in Zotero takes its Zotero value. zotero-merge-entries saves the user's choices as a manual version, synced with the current item.
zotero-adopt-entries marks the copies of Zotero items which the database had without the marks (imported by hand), so that they are synced from then on; those whose fields differ become manual, and go through the same choice.
Zotero may change a key (Better BibTeX regenerates it when the title changes). (zotero-check-missing keys) looks up the items recorded for the keys which Zotero no longer has: an item with another key is renamed, an item which is gone is deleted. A renamed item is still exported under its old key, by the managed file and by the source :zotero, and the entry of a deleted item is kept from the previous managed file, until the user acts; the message says so.
zotero-citation-renames collects the renames of the document: those found this way and, with the database, those recorded by the sync (zotero-key). Update the citations (zotero-update-citations) renames the keys in all the citations of the document or of its project (zotero-rename-citations) and, with the database, gives the entry the new name (a new version). The changes to open documents pass through their undo history; the files of the project which were not open are saved. Check against Zotero… (zotero-check-document) reports the keys found in Zotero or elsewhere, renamed, deleted, missing, used by another source for another work, and the unmarked copies. Two references are the same work (zotero-same-work?) when they have the same DOI, if both have one (zotero-normalized-doi removes the prefixes such as https://doi.org/ and lowercases it), and otherwise when they have the same normalized title and year; the items carry their DOI (zotero-entry-doi).
In a project, the citations are those of the master document (zotero-master, project-get) and of the files it includes, recursively (zotero-project-files, a walk of the include tags, through the open buffers or the files), and the bibliography is that of the master (zotero-master-bibliography-file).
⇥ in a citation (kbd-variant, in
generic-edit.scm
without the database tool and in
bib-kbd.scm
with it) adds the keys of Zotero with the typed prefix (zotero-completion-suffixes, an interactive request), unless the preference "zotero completion" is off. The keys of each prefix are remembered while no library changes, and a longer prefix is answered from a shorter one whose answer was complete (fewer items than the limit of the search), so that typing makes one request per new prefix at most.
Besides the automatic import of the attached references, Document\(\rightarrow\)Bibliography\(\rightarrow\)Import the citations into the database (zotero-import-citations) and Focus\(\rightarrow\)Import into the database (zotero-import-entry, offered when zotero-can-import?) import references of Zotero by hand, through zotero-import-items.
The search window of the database (open-db-chooser, see Editing databases) is the search of citations in both modes. It is opened by focus-open-search-tool and the alternate tab key:
with the database tool, on (bib-database) (open-bib-chooser,
bib-manage.scm
, whose callback also calls zotero-cited); db-search-results appends the Zotero items which the database does not have (zotero-search-entries);
without it, on the marker :bib-file (zotero-open-search-tool, whose callback sets the key and calls zotero-cited): db-search-results then calls zotero-file-search-results, which lists the entries of the file of the bibliography matching all the words of the query (unless the file is managed), then the Zotero items. The entries of the file are converted with zealous-bib-import and remembered while the file does not change.
zotero-mark-results puts the source before each pretty result (zotero-source-text: Database, Database, from Zotero, the name of the file, Zotero, Zotero, group), and zotero-search-sources-text gives the line which names the sources at the top of the window. The preference "zotero in database search" (on by default) leaves Zotero out. The Zotero results are database entries converted from the exported BibTEX, so they are formatted by the same db-pretty.
focus-can-search? and focus-open-search-tool have definitions for citations without the database tool, in
generic-edit.scm
, after their default definitions. The focus menu and the focus bar of a citation offer Show in Zotero (zotero-citation-entry, zotero-show-item, which opens zotero://select/library/items/item or zotero://select/groups/id/items/item) when the key is known without asking Zotero (zotero-known-entry: the cache of keys, or the attachment zotero-items).
The messages are built with (zotero-tr template arg …), which translates the template and then puts the arguments for %1, %2…, so that the keys and the names of files are never translated; the dictionaries have the templates (in nine languages). The menu paths of the messages are translated item by item (zotero-menu-path).
the address of Zotero (http://localhost:23119);
"user" or "all";
"bibtex" or "biblatex";
completion of keys from Zotero;
Zotero in the search window of references;
the references added to the user's BibTEX file;
"auto", "local" or "web";
the key of zotero.org, when it is not in the wallet;
the user of the key (internal);
the versions of the libraries at the last sync (internal).
The first eight are in Document\(\rightarrow\)Bibliography\(\rightarrow\)Zotero settings… (the key as API key of zotero.org).
The suite zotero (
progs/check/zotero-test.scm
, run with src/tests/scheme/check.sh zotero) replaces zotero-request by a fake server, which answers from a small library with versions, a group library and the request kinds above, and records the requests. The groups which use the database work on a temporary database (bib-database is redefined), a new file each time, and load
zotero-db.scm
only when they run. The suite runs after the suites links and database in
check-master.scm
.
The fake library searches as the source does: the application also matches the citation keys (and the field extra with qmode=everything), zotero.org only the titles, the creators and the dates, each word of the query being required. A web browser is simulated by redefining zotero-in-browser? and zotero-start-request (the requests are recorded, and answered with zotero-async-answer one by one or all at once, sim-answer-all!); a synchronous request would then fail, which shows that an operation asked for something it had not prefetched. The groups by item (a key found only through its item, renamed items), cited (the copies into the user's file, a new file and the database), async database (Update with the database in a browser) and async messages (labels, progress) cover the work described above. Groups which reach zotero-command with the source "web" need the preference "zotero api key", else the key is asked for and nothing runs.
The searches of zotero.org do not look at the citation keys, even with qmode=everything (see above).
zotero-in-browser? is (defined? 'web-javascript): a test must never define web-javascript, or every group runs as in a browser.
set-message is invisible while a dialog has the focus (the footer of the document is not drawn again): feedback for a dialog goes into the dialog.
string-search-forwards answers -1 when nothing is found, which is true for or and and.
generate-all-aux in the suite crashes the offscreen Qt platform (the wait indicator takes the focus of a detached editor): the groups check zotero-db-entries, which the generation calls, rather than generate the bibliography.
The local API has no list of deleted items: an item which a query by key does not return was deleted (or moved to the trash). The same queries also return the children (attachments, notes) of the items.
The strings of json->tree are in utf8, those of TeXmacs in Cork; cork->utf8 also turns … into an ellipsis and -- into a dash.
The BibTEX import lowercases titles (except the first letter and braced parts), so that copies of an entry with different capitalization have the same fields once imported.
buffer-load returns #t when it fails, and buffer-pretend-modified only reaches buffers with a view: this is why the files of a project which were not open are saved after a rename.
A database stays in memory after its file was removed: a test which removes and recreates a database file sees the old entries.
An input field of a widget also runs its command when it loses the focus, and a list (choices) gives back its items converted to utf8 and back: compare them after the same conversion.
Under Vue, a dialog gave the keyboard to an embedded editor (texmacs-input) rather than to its first field: what was typed in the search window of references went into the list of results. A field now claims the focus of a window which has none (
vue_widget.cpp
).
A refreshable widget evaluates its items when the window is built: refreshed, it shows the same text. A line which changes is a promise in its refreshable, made again at each refresh (the sources of the search window, the state in the settings).
A definition with :require must come after the default definition (without condition) of the same function, otherwise the default replaces it.