![]() |
Languages, hyphenation and spell checking | ![]() |
TeXmacs needs to know about natural languages for four rather different purposes:
the typesetter must cut strings into words, decide where spaces may be stretched or broken, apply language specific spacing rules (such as the thin spaces before ; and ? in French) and hyphenate words at the end of lines;
the spell checker must find misspelled words in a document and propose corrections, and the grammar checker (based on the external LanguageTool server) must find and correct grammatical errors;
the user interface must be translated into the language of the user, and so must the parts of documents which are generated automatically (“Chapter”, “Theorem”, “Table of contents”, …);
dates and other locale dependent data must be formatted.
This chapter describes the C++ classes and the Scheme code behind these tasks. Two closely related subjects are described elsewhere: the mathematical “languages”, which assign operator types to mathematical symbols, are part of mathematical typesetting, and the programming languages, which are mainly used for syntax highlighting, are the subject of syntax highlighting and programming languages. The line breaking algorithm which consumes the hyphenation points is described in the typesetting algorithm.
All C++ file names are relative to
src/src/
, and all Scheme file names to
src/TeXmacs/progs/
, unless stated otherwise.
The central abstraction is the class language_rep (
System/Language/language.hpp
). A language is a named resource: an object which is created once per name and then found again by name. There are three families of languages, selected by the current mode of the typesetter:
text_language (name) returns the language for the value of the language environment variable (english, french, chinese, …). It knows how to split strings into words, spaces and punctuation, and how to hyphenate words using TeX hyphenation patterns.
math_language (name) for the math-language variable, which assigns operator types, priorities and spacing to mathematical symbols.
prog_language (name) for the prog-language variable, which mainly drives syntax highlighting.
Spell checking is not part of the language objects: it is a separate layer of free functions in
language.cpp
which forwards to one of three external engines (the Aspell library, a Hunspell or Aspell subprocess, or the macOS spell service) and caches the results. On top of it,
Data/Tree/tree_spell.cpp
finds all misspelled words of a tree, and the Scheme modules in
generic/spell-widgets.scm
and
tools/spell/
implement the spell checking and grammar checking tools of the user interface.
Translation of the user interface is done by dictionaries (
System/Language/dictionary.cpp
), which are also resources, loaded from Scheme files of pairs of strings in
src/TeXmacs/langs/natural/dic/
.
System/Language/language.hpp,
language.cpp
The classes text_property_rep and language_rep, the predefined text properties, the table of successions of mathematical operators, the wrappers hyphenless_language and ad_hoc_language, the colors used for syntax highlighting, and the spell checking front end with its cache.
System/Language/impl_language.hpp
Declarations shared by the implementations: the predefined tp_…_rep properties and the classes of the verbatim and programming languages.
System/Language/text_language.cpp
The text languages: the default Western implementation, French typography, the languages written with Unicode entities (Cyrillic), and the oriental languages (Chinese, Japanese, Korean, Taiwanese); the list of supported languages and the factory text_language.
System/Language/verb_language.cpp
The verbatim language, used for unknown languages and for source code without a specific language.
System/Language/hyphenate.hpp,
hyphenate.cpp
Loading of TeX hyphenation pattern files and Liang's hyphenation algorithm.
System/Language/dictionary.hpp,
dictionary.cpp
Translation dictionaries, the input and output languages, and the translation of strings and trees.
System/Language/locale.hpp,
locale.cpp
Conversions between TeXmacs language names and system locales, detection of the language of the user, and formatting of dates.
System/Language/math_language.cpp,
prog_language.cpp, *_language.cpp, packrat_*.cpp
Mathematical and programming languages and the packrat parser; see the chapters mentioned in the introduction.
Plugins/Ispell/ispell.hpp,
ispell.cpp,
ispell_exe.cpp
The two implementations of the low level spell checking interface: the Aspell library (when USE_ASPELL is set) and a pipe to a Hunspell or Aspell process.
Plugins/MacOS/mac_spellservice.mm
The same interface on top of the macOS spell checker, used instead of the above when MACOSX_EXTENSIONS is defined.
Data/Tree/tree_spell.cpp
Spell checking of whole trees, with a cache.
Edit/Replace/edit_spell.cpp
The historical, key driven spell checking mode of the editor.
Data/Convert/AI/lantool.cpp,
compress.cpp
Merging the answers of LanguageTool into documents, and the compressed HTML representation of documents which is sent to LanguageTool (and to AI models).
generic/spell-widgets.scm
The spell checking tool and toolbar.
tools/spell/
Grammar checking: the interface with LanguageTool (
spell-lantool.scm
), the editing of spell-error markup and personal dictionaries (
spell-edit.scm
), the correction tool and toolbar (
correct-widgets.scm
) and the keyboard bindings (
spell-kbd.scm
).
src/plugins/languagetool/
The languagetool plug-in, which only declares the preferences for the server.
language/natural.scm,
utils/misc/translation-list.scm
Tools for maintaining the translation dictionaries.
src/TeXmacs/langs/natural/
The data:
hyphen/
(hyphenation patterns),
dic/
(translations) and
miss/
(lists of missing translations).
src/TeXmacs/packages/customize/language/
One style package per language, which sets the language variable and language specific typography.