mailing-list for TeXmacs Users

Text archives Help


Syntax highlighting of programs with TeXmacs


Chronological Thread 
  • From: Nicolas Ratier <address@hidden>
  • To: address@hidden
  • Subject: Syntax highlighting of programs with TeXmacs
  • Date: Sat, 7 Aug 2021 13:42:28 +0200

Hello,

I propose a solution to include programs in TeXmacs with syntax highlighting for about 100 languages. The programs can also contain Unicode characters.

This solution uses the GNU source-highlight program :
https://www.gnu.org/software/src-highlite/
https://www.gnu.org/software/src-highlite/source-highlight.html

---------------------------------------
For users (Linux) :
---------------------------------------

install the GNU tools, uni2ascii and source-highlight
copy the attached files into a directory
./src2tm.sh test01.ml
which generates a file test01.tm
In TeXmacs : Insert -> Link -> Include test01.tm

---------------------------------------
For developers
---------------------------------------

The output format for generating TeXmacs files is defined in the "texmacs.outlang" file. I preferred to generate the TeXmacs TM format because this format is very close to HTML and it was easier in this case than generating the TeXmacs format in scheme or s-expression STM.

To be precise, the TM format is complicated because of the indentation of the <xxx></xxx> tags, the limitation of the number of characters in width and finally the particular treatment of the blank lines. So I generate a simpler format, which is of course recognized without ambiguity by TeXmacs. The normalization is done at the first save of the file.

Explanations for the src2tm.sh file:

1/ The uni2ascii program allows to manipulate Unicode characters because neither source-highlight, nor TeXmacs take into account UTF8 files. My solution implies a limitation: the source file that we want to include must not contain the pattern:
"@@@_@@@[0-9A-F]+&&&_&&&"

2/ The sed program makes it easy to handle line breaks in the generated file, I just added a blank at the end of each line so that empty lines are no longer empty! (anyway, you can always remove these blanks by a script scheme inside TeXmacs)

Remarks:

1/ If you want to view TeXmacs source code, you have to describe the TeXmacs input format in a texmacs.lang file. This has to be done ... and in your rush, why not also add julia.lang.

2/ There is an API in C++ to call source-highlight, it's not necessarily a good idea to use it, significant development time and source of additional bugs for a minimal improvement. A script scheme to call uni2ascii, source-highlight and include the generated file is enough (do one thing and do it well ... UNIX).

Before asking to include the texmacs.outlang file in the source-highlight distribution, I prefer to propose it to you before, to receive your critics and improvements.

Have a nice vacation,
Nicolas

Attachment: source-highlight.tar.gz
Description: application/gzip




Archive powered by MHonArc 2.6.19.

Top of page