![]() |
Example of a plug-in with Python code | ![]() |
Consider the example of the pyminimal plug-in in the directory
$TEXMACS_PATH/examples/plugins
It consists of the following files:
pyminimal/progs/init-pyminimal.scm
pyminimal/src/minimal.py
In order to try the plug-in, you first have to recursively copy the directory
$TEXMACS_PATH/examples/plugins/pyminimal
to $TEXMACS_PATH/plugins or $TEXMACS_HOME_PATH/plugins.
When relaunching TeXmacs, the plug-in should now be automatically recognized.
The pyminimal plug-in demonstrates a minimal interface between TeXmacs and an extern program in python. The initialization file
init-pyminimal.scm
essentially contains the following code:
(define (python-launcher)
(if (url-exists? "$TEXMACS_HOME_PATH/plugins/pyminimal")
(string-append "python \""
(getenv "TEXMACS_HOME_PATH")
"/plugins/pyminimal/src/minimal.py\"")
(string-append "python \""
(getenv "TEXMACS_PATH")
"/plugins/pyminimal/src/minimal.py\"")))
(plugin-configure pyminimal
(:require (url-exists-in-path? "python"))
(:launch ,(python-launcher))
(:session "PyMinimal")) |
The :require option checks whether python indeed exists in the path (so this will fail if you did not have python installed). The :launch option specifies how to launch the extern program. The :session option indicates that it will be possible to create sessions for the pyminimal plug-in using Insert\(\rightarrow\)Session\(\rightarrow\)PyMinimal.
The python-launcher function will be evaluated and return the proper command to launcher the extern program. If $TEXMACS_HOME_PATH/plugins/pyminimal exists, it would be
python "$TEXMACS_HOME_PATH/plugins/pyminimal/src/minimal.py" |
otherwise,
python "$TEXMACS_PATH/plugins/pyminimal/src/minimal.py" |
The environment variables will be replaced in runtime. Sometimes, $TEXMACS_HOME_PATH and $TEXMACS_PATH may contain spaces, as a result, we quote the path using the double quotes.
Using the Python interpreter, we do not need to compile the code. And most of the time, python code can be interpreted without any modification under multiple platforms. The built-in python libraries are really helpful and handy.
Many TeXmacs built-in plugins are written in Python, and we have carefully organized the code and reused the common part named tmpy.
import os
import sys
from os.path import exists
tmpy_home_path = os.environ.get("TEXMACS_HOME_PATH") + "/plugins/tmpy"
if (exists (tmpy_home_path)):
sys.path.append(os.environ.get("TEXMACS_HOME_PATH") + "/plugins/")
else:
sys.path.append(os.environ.get("TEXMACS_PATH") + "/plugins/") |
The first part of the code just add $TEXMACS_HOME_PATH/plugins or $TEXMACS_PATH/plugins to the python path for importing the tmpy package.
from tmpy.protocol import *
from tmpy.compat import *
flush_verbatim ("Hi there!")
while True:
line = tm_input()
if not line:
pass
else:
flush_verbatim ("You typed " + line) |
flush_verbatim is provided by tmpy.protocol which is a subpackage for interaction with TeXmacs server in Python. It writes a DATA_BEGIN-DATA_END block in the utf8 format on the standard output and flushes it. The same module also provides flush_prompt, flush_command, flush_scheme, flush_latex, flush_file, flush_ps and flush_err (the latter writes to the standard error, whose contents are displayed as error output), as well as the constants DATA_BEGIN, DATA_END, DATA_ESCAPE and DATA_COMMAND.
tm_input is provided by tmpy.compat which is a subpackage for compatibility within Python 2 and 3.
The example uses the command python both in its :require test and in its launcher. On systems where only python3 is installed, the plug-in will therefore not be detected. The plug-ins which are shipped with TeXmacs rather use the function (python-command) (defined in
kernel/library/base.scm
), which returns the first of python3, python and python2 which can be found in the path (or the empty string if there is none). For instance, the configuration of the python plug-in (in
plugins/python/progs/init-python.scm
) reads
(plugin-configure python
(:winpath "python*" ".")
(:winpath "Python*" ".")
(:winpath "Python/Python*" ".")
(:require (python-command))
(:launch ,(python-launcher))
(:preferences (supports-jupyter?))
(:tab-completion #t)
(:serializer ,python-serialize)
(:session "Python")
(:scripts "Python")) |
where python-launcher starts the script
plugins/tmpy/session/tm_python.py
with the interpreter returned by (python-command) and the option -X utf8. Notice that the empty string returned by (python-command) when no interpreter is found is a true value in Scheme, so this particular :require test always succeeds; a more robust test would be (!= (python-command) "").
This demo plugin is a Python implementation of the well-documented minimal plugin written in C++. Here is the summary of the differences:
pyminimal requires the Python interpreter, minimal needs to be compiled and linked
pyminimal is easier to install than minimal
pyminimal reuses the common part related to TeXmacs
pyminimal has better compatibility for Linux, Windows and macOS