GNU TeXmacsUser manualReferenceAboutEnglish
Citations from Zotero

1.Overview

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:

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

.

2.Modules

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.

3.Talking to Zotero

3.1.Requests

(zotero-request path interactive?)
(ask Zotero)

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.

3.2.zotero.org

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).

3.3.Asynchronous requests in a web browser

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:

(zotero-with-retry retry thunk)
(run with asynchronous requests)

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.

3.4.Following the requests

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.

3.5.State and caches

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.

3.6.Answers

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.

4.Items, keys and libraries

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.

(zotero-find-key key)
(resolve 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).

5.Sources of references

The sources of a bibliography come in one order, given by bib-sources in

bib-manage.scm

:

  1. :local, the entries of the document;

  2. the BibTEX files of the user;

  3. :default, the database of the user;

  4. the managed BibTEX files;

  5. :zotero;

  6. :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).

5.1.Managed BibTEX files

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.

5.2.References added to the user's file

(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.

5.3.The source :zotero

(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.

5.4.The items of a document

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).

(zotero-cited key buffer)
(take a citation from Zotero)

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

.

6.Entries of the database from Zotero

The entries from Zotero carry the contributor Zotero and the meta attributes

zotero-item

the key of the item;

zotero-library

its library (users/0 or groups/id; the first entries had user, read as users/0);

zotero-version

the version of the item when it was exported;

zotero-synced

the fields as they were then, to tell later which side changed a field;

zotero-key

the key in Zotero, when it was renamed there (the entry keeps its name, so that the citations still work);

zotero-deleted

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.

(zotero-sync-database . force?)
(follow Zotero)

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.

7.Renamed and deleted keys

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).

8.Projects

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).

9.User interface

9.1.Completion

⇥ 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.

9.2.Importing into the database

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.

9.3.The search window of references

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:

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.

9.4.Menus

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).

9.5.Messages

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).

10.Preferences

"zotero server"

the address of Zotero (http://localhost:23119);

"zotero libraries"

"user" or "all";

"zotero export format"

"bibtex" or "biblatex";

"zotero completion"

completion of keys from Zotero;

"zotero in database search"

Zotero in the search window of references;

"zotero add to bib file"

the references added to the user's BibTEX file;

"zotero source"

"auto", "local" or "web";

"zotero api key"

the key of zotero.org, when it is not in the wallet;

"zotero user"

the user of the key (internal);

"zotero sync version"

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).

11.Tests

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.

12.Pitfalls

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".