GNU TeXmacsUser manualReferenceAboutEnglish
Adding a language, performance and pitfalls

1.Adding a new language

This section walks through the addition of a hypothetical language lua with the generic highlighter prog_language_rep, which requires no C++ changes. The file names follow the conventions of the existing languages of the code plugin; all code shown is an example to be adapted, not existing code.

1.1.Step 1: the language definition

Create src/plugins/code/progs/lua-lang.scm. The module name must be (lua-lang), since prog_language_rep loads it with (use-modules (lua-lang)), and the file must be found by prog_lang_exists (here: in $TEXMACS_PATH/plugins/code/progs/ after installation).

(texmacs-module (lua-lang)
  (:use (prog default-lang)))

(tm-define (parser-feature lan key)
  (:require (and (== lan "lua") (== key "keyword")))
  ‘(,(string->symbol key)
    (constant "true" "false" "nil")
    (declare_function "function")
    (keyword "local" "end" "in" "and" "or" "not")
    (keyword_conditional "if" "then" "else" "elseif" "for" "while"
      "do" "repeat" "until" "break")
    (keyword_control "return" "goto")))

(tm-define (parser-feature lan key)
  (:require (and (== lan "lua") (== key "operator")))
  ‘(,(string->symbol key)
    (operator "+" "-" "*" "/" "%" "^" "#" ".."
      "==" "~=" "<=" ">=" "<" ">" "=")
    (operator_field "." ":")
    (operator_openclose "{" "[" "(" ")" "]" "}")))

(tm-define (parser-feature lan key)
  (:require (and (== lan "lua") (== key "number")))
  ‘(,(string->symbol key)
    (bool_features "prefix_0x" "sci_notation")))

(tm-define (parser-feature lan key)
  (:require (and (== lan "lua") (== key "string")))
  ‘(,(string->symbol key)
    (bool_features "hex_with_8_bits")
    (escape_sequences "\\" "\"" "'" "a" "b" "f" "n" "r" "t" "v")))

(tm-define (parser-feature lan key)
  (:require (and (== lan "lua") (== key "comment")))
  ‘(,(string->symbol key)
    (inline "--")))

(define (notify-lua-syntax var val)
  (syntax-read-preferences "lua"))

(define-preferences
  ("syntax:lua:comment" "brown" notify-lua-syntax)
  ("syntax:lua:keyword" "#309090" notify-lua-syntax))

Note that the word operators and, or and not are declared as keywords rather than operators: operators are matched before keywords and without regard to word boundaries (see the pitfalls below). Lua block comments --[[ … ]] cannot be expressed; only /* … */ is supported for multi-line comments.

1.2.Step 2: the format

prog_language only builds a prog_language_rep if the format exists. Declare it next to the other formats of the plugin in

src/plugins/code/progs/code-format.scm

:

(define-format lua
  (:name "Lua source code")
  (:suffix "lua"))

(define (texmacs->lua x . opts)
  (texmacs->verbatim x (acons "texmacs->verbatim:encoding" "SourceCode" '())))

(define (lua->texmacs x . opts)
  (code->texmacs x))

(define (lua-snippet->texmacs x . opts)
  (code-snippet->texmacs x))

(converter texmacs-tree lua-document (:function texmacs->lua))
(converter lua-document texmacs-tree (:function lua->texmacs))
(converter texmacs-tree lua-snippet (:function texmacs->lua))
(converter lua-snippet texmacs-tree (:function lua-snippet->texmacs))

and add lua to the corresponding line of

init-texmacs.scm

:

(lazy-format (code-format) cpp julia scala java json csv lua)

With the format in place, opening a .lua file gives a buffer in programming mode with prog-language set to lua (attach_subformat), and the snippet converters are used by copy and paste in programming mode.

1.3.Step 3: markup

Add an inline and a block macro to

src/TeXmacs/packages/environment/env-program.ts

:

<assign|lua-lang|<macro|body|<with|mode|prog|prog-language|lua|font-family|rm|body>>>

<assign|lua-code|<macro|body|<pseudo-code|<lua-lang|body>>>>

(in the existing file, the block macros are written in block form). Then register the tags in the groups inline-code-tag and block-code-tag of

text/text-drd.scm

, so that generic editing functions recognize them as code, and add entries ("Lua" (make 'lua-lang)) and ("Lua" (make 'lua-code)) to code-menu in

text/text-menu.scm

.

1.4.Step 4: editing support (optional)

Add mode predicates to

kernel/texmacs/tm-modes.scm

:

(in-lua% (== (get-env "prog-language") "lua"))
(in-prog-lua% #t in-prog% in-lua%)

Create prog/lua-edit.scm and add (prog lua-edit) to the :use list of

prog/prog-kbd.scm

:

(texmacs-module (prog lua-edit)
  (:use (prog prog-edit)))

(tm-define (program-compute-indentation doc row col)
  (:mode in-prog-lua?)
  (if (<= row 0) 0
      (let* ((r (or (program-row (- row 1)) ""))
             (i (string-get-indent r))
             (tr (tm-string-trim r)))
        (if (or (string-ends? tr "then") (string-ends? tr "do")
                (string-prefix? "function" tr))
            (+ i (get-tabstop))
            i))))

(tm-define (notify-cursor-moved status)
  (:require prog-highlight-brackets?)
  (:mode in-prog-lua?)
  (select-brackets-after-movement "([{" ")]}" "\\"))

(kbd-map
  (:mode in-prog-lua?)
  ("(" (bracket-open "(" ")" "\\"))
  (")" (bracket-close "(" ")" "\\"))
  ("\"" (bracket-open "\"" "\"" "\\")))

1.5.Step 5: checking

After rebuilding and installing (the plugin files and packages are copied by the build), evaluate (format? "lua") in a Scheme session: it must return #t. Insert a lua-code block and type some code; if nothing is colored, the language most probably fell back to verb_language_rep. Enabling (debug-set "parser" #t) before the language is first used prints the configuration of the string and preprocessor parsers on the debugging console. Remember that the language object, and hence the result of the format test, is cached for the whole session: restart TeXmacs after changing the definitions.

1.6.Alternatives

A user can also provide a language without modifying TeXmacs, as a plugin in $TEXMACS_HOME_PATH/plugins/lua/progs/: the file lua-lang.scm there is found by prog_lang_exists, and the plugin initialization must declare the format with define-format and define the markup in a style package of the plugin.

If the generic highlighter is not expressive enough (other string delimiters, nested comments, context dependent coloring, colors that follow themes), write a C++ class derived from abstract_language_rep as for

cpp_language.cpp

: declare it in

System/Language/impl_language.hpp

, implement advance, get_hyphens, hyphenate and get_color, and add a test for its name to prog_language before the generic case. The sources of

System/Language/

are collected by a glob in

src/CMakeLists.txt

, so it suffices to re-run CMake. Return names of environment variables from get_color to obtain theme-aware colors.

2.Performance considerations

Highlighting happens during typesetting, so its cost is paid for every line which is (re)typeset. The typesetter only re-typesets modified paragraphs, and every line of a program is a paragraph, so editing usually costs little; the cost matters when a long file is typeset as a whole (loading, changing the style or the page width, zooming).

3.Pitfalls and known problems

The following list is based on reading the current code; it is meant to help developers interpret surprising colors.

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