![]() |
Spell checking and grammar checking | ![]() |
Spell checking is organized in four layers, from the external engine up to the user interface:
a low level interface to an external spell checker, with three implementations;
a cache and session layer in
System/Language/language.cpp
;
the spell checking of whole trees in
Data/Tree/tree_spell.cpp
;
the Scheme tools: continuous spell checking, the spell tool and toolbar, and grammar checking with LanguageTool.
The low level interface is declared in
Plugins/Ispell/ispell.hpp
:
string ispell_start (string lan);
tree ispell_check (string lan, string s);
void ispell_accept (string lan, string s);
void ispell_insert (string lan, string s);
void ispell_done (string lan); |
Languages are TeXmacs language names; the Aspell and Hunspell backends translate them into dictionary names with language_to_locale (for instance fr_FR). Words are passed in the Cork encoding and converted to UTF-8 for the engine. ispell_start returns "ok" or a message starting with "Error: ". ispell_check returns "ok" for a correct word, a message "Error: …" if the engine is not available, and otherwise a tuple whose first element is the number of suggestions, followed by the suggestions. ispell_accept accepts a word for the rest of the session, ispell_insert adds it to the personal dictionary of the engine, and ispell_done saves the personal dictionary.
There are three implementations, chosen at compile time:
When MACOSX_EXTENSIONS is defined,
language.cpp
and
edit_spell.cpp
map the five functions to mac_spell_start, … in
Plugins/MacOS/mac_spellservice.mm
, which use NSSpellChecker.
Otherwise, if USE_ASPELL is set (the CMake option of the same name, on by default, which is effective only if the library is found),
ispell.cpp
links with libaspell. It looks for dictionaries in $TEXMACS_PATH/aspell-0.60 if that directory exists (as in bundled distributions), and otherwise uses the system ones.
Otherwise
ispell_exe.cpp
starts hunspell -a -i utf-8 -d locale or, failing that, aspell -a --encoding=utf-8 --language-tag=locale through a pipe (on Windows it also looks in the usual installation directories), and speaks the ispell -a protocol: ^word to check a word, @word to accept it, *word to insert it and # to save the personal dictionary. A dictionary is considered available if the process answers with its @(#) banner.
In all cases there is one engine instance per language, stored as a resource (ispeller in the two Aspell/Hunspell implementations).
The rest of TeXmacs does not call the engine directly but the functions at the end of
language.cpp
, exported to Scheme as follows:
spell_start () and spell_done () open and close a session in which engines are kept running. Outside a session, every check is wrapped in spell_start and spell_done, so that the personal dictionary of the engine is saved after every word, which is slow.
Start and stop the engine for one language.
spell_check (lan, s) returns the engine's answer. Words are checked in lower case, unless they are capitalized; for words in capitals the suggestions are converted to capitals. If no engine can be started, the answer is "ok", so that documents in languages without a dictionary are simply not checked.
check_word (lan, s) returns a boolean, using a cache spell_cache indexed by lan:word.
spell_accept (lan, s, permanent) marks a word as correct in the cache (for the current session only unless permanent) and tells the engine.
spell_insert adds a word to the personal dictionary of the engine and clears the cache of
tree_spell.cpp
.
spell_notify_insert only marks a word as correct in the cache; it is used by the grammar tool for words which it stores in its own personal dictionary (see below).
The first time a language is used, spell_initialize calls the Scheme function spell-user-words, which returns the words of the TeXmacs personal dictionary $TEXMACS_HOME_PATH/langs/natural/spell/lan.scm, and marks them as correct.
Data/Tree/tree_spell.cpp
finds all misspelled words of a tree and returns them as a range_set (a flat array of start and end paths). The front ends, exported as tree-spell, tree-spell-at and tree-spell-selection, take the language, the tree, its path and a maximal number of hits; tree-spell-at starts at a given position and proceeds outwards, so that the errors closest to the cursor are found first.
The traversal follows the DRD: it only descends into accessible children (and the children of hidden, and in source mode all children except those of raw-data), and it tracks the mode and language variables through get_env_child, so that only text is checked, each part in its own language. The tags abbr, name, bib-list and explain-macro are skipped. Within a string, words are separated at spaces; leading and trailing non letters are stripped, words which begin or end with a digit and contain no lower case letters (such as postal codes) are accepted, and if a word with punctuation inside fails, its letter runs are checked separately.
tree-spell* (spell_with_cache) adds a cache indexed by language and subtree. For a document, each paragraph is looked up separately, so that only modified paragraphs are rechecked. The cache is renewed every five minutes, keeping the previous generation for one more period.
When the preference continuous spell checking is on, the editor calls the Scheme function continuous-spell-check (
tools/spell/spell-edit.scm
) from edit_interface_rep::apply_changes whenever the tree or the environment changed. After 100 ms of idle time, this function runs tree-spell* on the whole buffer in the language of the document and stores the result as the alternative selection "spell-errors", notifying THE_SPELL_ERRORS. The editor then computes the rectangles of the errors, restricted to about one hundred errors around the visible part of the document, and draws them (
Edit/Interface/edit_repaint.cpp
).
The Edit\(\rightarrow\)Spell command, interactive-spell (
generic/spell-widgets.scm
), computes the errors from the cursor position (or in the selection), and then opens either a toolbar at the bottom of the window (preference toolbar spell, the default) or a spell tool in the side tools or in a dialog. While it is open, a spell session is active (multi-spell-start). The tool shows the current error, the suggestions and buttons to replace, accept (spell-accept-word, spell-keep-word) or insert the word into the engine's personal dictionary (spell-insert-word). The replacement is typed in an embedded TeXmacs buffer tmfs://aux/spell.
The editor also contains an older, key driven spell checking mode (
Edit/Replace/edit_spell.cpp
, spell-start and key-press-spell): it walks through the document with ispell_check directly, bypassing the cache layer, and asks for an action in the footer (a accept, r replace, i insert, a digit for a suggestion). It is no longer reachable from the menus.
Grammar checking uses an external LanguageTool server, either a local one (by default http://localhost:8081; the script
src/TeXmacs/misc/scripts/languagetool-server.ps1
starts one on Windows, by default on port 8085 rather than 8081, so the preference must be adapted) or the public service with an optional premium account. It is enabled by the preference grammar checking; the languagetool plug-in (
src/plugins/languagetool/
) only declares the preferences languagetool server, languagetool premium, languagetool username, languagetool API key and languagetool use widgets, and the corresponding preferences widget.
tools/spell/spell-lantool.scm
) is shown when supports-lantool? succeeds, that is, when a test query to the server returns a valid answer (the result is cached for the session). It starts an asynchronous process (
utils/library/process.scm
) which visits the paragraphs of the document, or of the selection, one by one. For each paragraph:
The paragraph is converted to a compressed HTML string with compress-html (
Data/Convert/AI/compress.cpp
): text is kept, and every piece of non textual markup is replaced by an opaque identifier xn in an <a id> or <div id> element, so that the server only sees text.
The string is posted to server/v2/check with async-http-post-query, with the locale of the paragraph's language and the rule UPPERCASE_SENTENCE_START disabled. Answers are cached by language and string.
The JSON answer is merged into the string by lantool-correct (
Data/Convert/AI/lantool.cpp
): every match whose text is not part of the markup becomes a spell-error element with the original text, the message and up to nine replacements. Before it is inserted, the Scheme function spell-replace-cached may resolve it at once, if the user already chose a replacement for the same error or accepted the word.
The result is decompressed with decompress-html and, if the paragraph has not changed in the meantime and contains at least one error, replaces the paragraph.
src/TeXmacs/packages/standard/std-fold.ts
, and grouped as spell-tag in
version/version-drd.scm
) is rendered by the Scheme function ext-spell-error as the original text with a balloon listing the message and the proposals.
spell-edit.scm
provides the navigation between errors (spell-go-to-next, …), the resolution of an error by one of its alternatives (spell-retain) or by a typed replacement (spell-replace), the personal dictionary of the grammar tool (spell-retain-permanent, stored in $TEXMACS_HOME_PATH/langs/natural/spell/lan.scm), and the removal of all remaining spell-error tags (Edit\(\rightarrow\)Terminate grammar, spell-terminate). The correction tool
tools/spell/correct-widgets.scm
(Edit\(\rightarrow\)Correct grammar, open-correct) shows the current error with its message and proposals, either in a bottom toolbar or in a widget.
The Aspell library backend returns the checked word, not the number of suggestions, as the first element of the tuple (
Plugins/Ispell/ispell.cpp:122
). The Scheme tools ignore this element, but the old key driven mode (
Edit/Replace/edit_spell.cpp:182
) reads it as a number: with this backend a digit key accepts the word instead of choosing a suggestion.
In the external process backend, ispell_check uses the local handle sc after calling ispell_start without retrieving it again (Plugins/Ispell/ispell_exe.cpp:270-275); if the resource did not exist yet, it dereferences a nil handle. The usual call path through spell_check always starts the engine first, which hides the problem.
There are two personal dictionaries: the engine's (written by spell-insert) and the TeXmacs one of the grammar tool (written by spell-retain-permanent). Words added to the second are known to the spell checker through spell-user-words, but words added to the first are unknown to the grammar tool.
When an engine cannot be started, spell_check also clears the global session flag (
language.cpp:521
), which ends a session opened by another tool.
spell-lantool.scm
declares its dependencies without the :use keyword (tools/spell/spell-lantool.scm:13-16), so texmacs-module ignores them; the module only works because
spell-kbd.scm
, which is loaded at startup, imports
spell-edit.scm
.
The tables of compressed markup in
compress.cpp
are global and are never emptied, so they grow during a session.
The locale codes used to select dictionaries and the LanguageTool language are partly wrong; see locales.