![]() |
Where text is converted | ![]() |
The general rule is that strings inside TeXmacs are in the universal encoding, and that every module which exchanges text with the outside world converts at its own boundary. This page lists the main boundaries. There is no central place which enforces the rule, and some interfaces carry UTF-8 or bytes of the operating system instead; these are pointed out below and in string utilities and pitfalls.
The labels of the tree are written and read as they are: a .tm file contains Cork bytes and universal symbols, not UTF-8. The serialization only escapes the characters which have a meaning in the file syntax (see default serialization). This is why .tm files edited by hand must not contain raw UTF-8: the bytes would be read as several Cork characters.
The converters of Data/Convert/ and
progs/convert/
convert explicitly: XML and HTML use cork->utf8 and utf8->cork (and named entities), LaTeX uses its own tables and the T2A encoding for Cyrillic, and the Scheme serialization (stm) keeps the universal encoding. See converters to other data formats and the LaTeX and HTML converters.
Import and export of verbatim text (
Data/Convert/Verbatim/verbatim.cpp
) follow the preferences verbatim->texmacs:encoding and texmacs->verbatim:encoding. On import, auto uses western_to_cork (see converters between encodings), utf-8 uses utf8_to_cork, SourceCode uses sourcecode_to_cork, and iso-8859-1 as well as any other value only apply tm_encode. On export, auto is first replaced by the character set of the locale (get_locale_charset); iso-8859-1 then applies tm_decode, cork applies nothing, SourceCode applies cork_text_to_sourcecode, and any other value applies cork_text_to_utf8 (var_cork_to_utf8): the macron and the dot accent of the strings of the document are first written <#AF> and <#2D9>, so that the tabs and newlines of the text are those of its structure. On import, the text is split into lines and its tabs and backspaces are read before the conversion of each piece to Cork. On Windows, line ends are then converted to CR LF.
Copying or pasting text in Croatian, Czech, Hungarian, Polish, Slovak or Slovene with the verbatim encoding set to iso-8859-2 converts with cork_to_il2 and il2_to_cork; Spanish and German text goes through spanish_to_ispanish and german_to_igerman and back (
Edit/Replace/edit_select.cpp
).
Keys reach the editor as strings in a small language of their own (see the event loop): a, S-a, C-x, return, alpha, … A character typed on the keyboard is therefore converted to the universal encoding without its angular brackets.
QTMKeyboardEvent::computeUnicodeToCork (
Plugins/Qt/QTMKeyboardEvent.cpp
) converts the text of the key event to UTF-8 and then with utf8_to_cork. A result of the form <name> (but not <#…>) loses its brackets, and less and gtr become < and >. Dead keys and combining accents are mapped to the key names grave, acute, hat, umlaut and tilde. Text committed by an input method (QTMWidget::inputMethodEvent) is converted with from_qstring, that is, from UTF-8 to Cork.
Xutf8LookupString is followed by utf8_to_cork (
Plugins/X11/x_loop.cpp
).
The text of an SDL text event is converted with utf8_to_cork and named as in Qt by cork_key (
sdl_gui.cpp
,
vue_gui.cpp
).
The text given to insertText: by the input system is named character by character by ns_key_name (
TMView.mm
), as computeUnicodeToCork does, through from_nsstring and utf8_to_cork.
When it shows keyboard shortcuts or handles pre-edit text, the editor converts back with cork_to_utf8 (
Edit/Interface/edit_keyboard.cpp
).
Converts a string for display in a Qt widget (
Plugins/Qt/qt_utilities.cpp
). Since many callers pass UTF-8 (file names, titles computed in Scheme) while most pass Cork, it guesses: a string which decodes as UTF-8 and is neither pure ASCII nor a universal string is taken as UTF-8, anything else is converted with cork_to_utf8. The comment in the code calls this a hack. Code which knows that its string is UTF-8 should call utf8_to_qstring instead.
The converse: UTF-8 from Qt, then utf8_to_cork. from_qstring_utf8 stops after the UTF-8 step.
When pasting, qt_gui_rep::get_selection (
Plugins/Qt/qt_gui.cpp
) takes the native TeXmacs format if available, and otherwise HTML or plain text as UTF-8 bytes, and passes them to the Scheme converters (for instance from verbatim-snippet or html-snippet to texmacs-snippet). When copying, qt_gui_rep::set_selection receives the bytes produced by the Scheme converters and wraps them with QString::fromUtf8 or QString::fromLatin1, depending on the target format and on the encoding preferences.
Output of a plug-in is converted line by line in
Data/Convert/Generic/input.cpp
: in utf8 mode with utf8_to_cork (texmacs_input_rep::utf8_flush), in verbatim mode with the auto guess of western_to_cork (verbatim_flush); structured output in TeXmacs or Scheme syntax is expected in the universal encoding. See sessions and connections.
URLs and file names are kept in the encoding of the operating system (in practice UTF-8), and shell commands are passed to the system as they are; see the system layer. Where file names are shown in the document or the interface, they are converted with utf8_to_cork (for instance for the default title metadata in edit_main_rep::get_metadata) or through the heuristic of to_qstring.
The glue passes strings between C++ and Scheme as byte sequences (string_to_tmscm, tmscm_to_string in
Scheme/Guile/guile_tm.cpp
; see the Scheme interpreter and the glue), so Scheme code sees Cork bytes and universal symbols, and must use tmstring-length and friends rather than string-length to count characters. Both supported dialects use scm_from_locale_stringn and scm_to_locale_stringn (
Scheme/Guile/guile_tm.hpp
). With Guile 1.8 (GUILE_C, which includes the default embedded interpreter) they copy bytes unchanged; with Guile 2 and 3 (GUILE_D) they decode and encode according to the locale, so Cork bytes above 127 are not guaranteed to survive the round trip.
Universal characters are mapped to glyphs by the fonts; Unicode fonts convert symbols and escapes to code points (see smart fonts).
Text strings in PDF files (outline and metadata) use utf8_to_pdf_hex_string.