GNU TeXmacsUser manualReferenceAboutEnglish
Language objects and the typesetter

1.The class language_rep

struct language_rep: rep<language>
(a language)

Declared in

System/Language/language.hpp

. The macro RESOURCE(language) makes language a handle to a named resource: the constructor language_rep (name) registers the new object in the table language::instances, and language (name) retrieves it later. Language objects are therefore created once and live for the rest of the session; the factories below always first look for an existing instance.

The fields are the name lan_name, the abbreviation hl_lan of the packrat grammar used for syntax highlighting (0 if there is none), and a table color_decoding of highlighting colors. The pure virtual methods are:

text_property advance (tree t, int& pos)

Given an atomic tree t and a position pos in its string, move pos to the end of the next lexical unit (a word, a run of spaces, a run of punctuation, a number, a symbol <…>) and return its text property.

array<int> get_hyphens (string s)

The penalties for breaking the word s after each of its characters; see hyphenation.

void hyphenate (string s, int after, string& l, string& r)

Split s after position after, adding a hyphen to the left part.

The virtual methods get_group, get_members, highlight and get_color have default implementations; they are used by mathematical languages (symbol groups) and by programming languages (syntax highlighting).

struct text_property_rep
(what the typesetter needs to know about a lexical unit)

Its fields are

type

One of the TP_* constants: normal text, hyphen, the various spaces (thin, normal, double, and their non-breaking versions), period, the CJK variants, operator, other.

spc_before, spc_after

The kind of space to insert before and after the unit (SPC_* constants), looked up in the spacing tables of the current font.

pen_before, pen_after

The line breaking penalties before and after the unit: 0 means a normal break point, HYPH_STD (10000) a hyphenation point, HYPH_PANIC a break which is only allowed as a last resort and HYPH_INVALID a forbidden break.

op_type, priority, limits, macro

Only used by mathematical languages.

The text languages do not allocate properties: they return pointers to the predefined global objects declared in

impl_language.hpp

, such as tp_normal_rep, tp_space_rep (a breakable space), tp_nb_space_rep (a space before punctuation, which may not be broken), tp_nb_thin_space_rep, tp_period_rep (a space after a full stop, which may be wider), tp_hyph_rep (a run of dashes, after which a line may be broken) and the CJK properties.

2.Text languages

text_language (name) (

text_language.cpp

) maps a language name to an implementation and to the name of a hyphenation pattern file. There are five implementations:

text_language_rep

The default for Western languages written in the Cork encoding. Spaces are breakable unless followed by punctuation; punctuation followed by a space produces a normal space, or a period space after ., ! and ?; runs of - are hyphens; letters, digits and symbols form words.

french_language_rep

The same, but with French typography: a non-breaking thin space before ;, !, ? and the closing guillemet, and after the opening guillemet (the Cork characters \23 and \24).

ucs_text_language_rep

For languages whose letters are written as Unicode entities <#…> in TeXmacs strings (Bulgarian, Russian, Ukrainian): entities are treated as letters, and the hyphenation patterns are kept in UTF-8.

oriental_language_rep

Chinese, Japanese, Korean and Taiwanese. Every character is a potential break point (tp_cjk_normal_rep), except before punctuation, which is recognized from a fixed table of ASCII and CJK punctuation marks. There is no hyphenation: get_hyphens forbids all breaks inside a unit.

verb_language_rep

The verbatim language (

verb_language.cpp

): no hyphenation; breaks are allowed at spaces and after the separators - / \ , ?. It is also returned, with a warning, for unknown language names.

The supported text languages, with their pattern files, are: american and english (us), british (ukenglish), bulgarian, croatian, czech, danish, dutch, esperanto, finnish, french, german, greek, hungarian, italian, polish, portuguese, romanian, russian, slovak, slovene, spanish, swedish and ukrainian, plus the four oriental languages. get_supported_languages () returns the same list. To add a language, add its hyphenation file to

src/TeXmacs/langs/natural/hyphen/

, a case to text_language and get_supported_languages, a style package in

src/TeXmacs/packages/customize/language/

, an entry to supported-languages in

kernel/texmacs/tm-modes.scm

, the locale codes in

locale.cpp

and, for the user interface, a translation dictionary (see translation).

3.Wrappers

Two functions of

language.cpp

derive new languages from an existing one. Both forward advance to the base language and are created once per name.

hyphenless_language (base)

Named base-hyphenless; get_hyphens forbids all breaks. It implements the primitive hgroup (concater_rep::typeset_hgroup,

Typeset/Concat/concat_text.cpp

), which also forbids breaks between the boxes of its body.

ad_hoc_language (base, hyphs)

Uses an explicit hyphenation such as "hy-phen-ation" for the corresponding word, and the base language for all other words. Each distinct hyphenation gets a number, so the language is named base-n. It implements the primitive hyphenate-as (concater_rep::typeset_hyphenate_as,

Typeset/Concat/concat_active.cpp

).

4.How the typesetter uses languages

The typesetting environment holds the current language in edit_env_rep::lan. It is recomputed by edit_env_rep::update_language (

Typeset/Env/env_semantics.cpp

) whenever the language, math-language, prog-language or mode variable changes:

switch (mode) {
case 0: case 1: lan= text_language (get_string (LANGUAGE)); break;
case 2:         lan= math_language (get_string (MATH_LANGUAGE)); break;
case 3:         lan= prog_language (get_string (PROG_LANGUAGE)); break;
}

where mode 1 is text, 2 math, 3 prog and 0 any other mode (such as src). The defaults are english, std-math and scheme (

Typeset/Env/env_default.cpp

).

The concatenation typesetter (

Typeset/Concat/concat_text.cpp

) calls env->lan->advance repeatedly to cut each string into units. For each unit it typesets the substring as a text box, records the language in the resulting line item, inserts the spaces requested by spc_before and spc_after, and sets the break penalties from pen_before and pen_after. Later, the line breaker (

Typeset/Line/line_breaker.cpp

) calls get_hyphens and hyphenate on the language stored in the item when a word has to be split; see line breaking.

At the document level, the language is chosen with a style package: the submenu Document\(\rightarrow\)Language (shown with detailed menus; set-document-language in

generic/document-edit.scm

) adds the package named after the language to the style list, or removes it for English. These packages (

src/TeXmacs/packages/customize/language/

) set the language variable together with language specific typography such as the dots used in tables of contents. Inside a document, a different language can be set locally by changing the language variable, for instance with a with.

5.Mathematical and programming languages

For completeness: mathematical languages (

math_language.cpp

) classify symbols by operator type, priority and spacing using the packrat grammar

language/std-math.scm

; their advance returns properties with a meaningful op_type, and the table succession_status_table (

language.cpp

, initialized by init_succession_status_table at startup) tells the typesetter which spaces to remove between two successive operators. They are described in mathematical typesetting. Programming languages (

prog_language.cpp

and the *_language.cpp files) are described in syntax highlighting and programming languages.

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