Basic input/output using pipes

The configuration and the compilation of the minimal plug-in is described in the chapter about plug-ins. We will now study the source file minimal/src/minimal.cpp. Essentially, the main routine is given by

int
main () {
  display-startup-banner
  while (true) {
    read-input
    display-output
  }
  return 0;
}

By default, TeXmacs just send a '\n'-terminated string to the application as the input. Consequently, the code for read-input is given by

char buffer[100];
cin.getline (buffer, 100, '\n');

The output part is more complicated, since TeXmacs needs to have a secure way for knowing whether the output has finished. This is accomplished by encapsulating each piece of output (in our case both the display banner and the interactive output) inside a block of the form

DATA_BEGINformat:messageDATA_END

Here DATA_BEGIN and DATA_END stand for special control characters:

#define DATA_BEGIN   ((char) 2)
#define DATA_END     ((char) 5)
#define DATA_ESCAPE  ((char) 27)

The DATA_ESCAPE is used for producing the DATA_BEGIN and DATA_END characters in the message using the rewriting rules

DATA_ESCAPEDATA_BEGIN DATA_BEGIN
DATA_ESCAPEDATA_END DATA_END
DATA_ESCAPEDATA_ESCAPE DATA_ESCAPE

The format specifies the format of the message. For instance, in our example, the code of display-startup-banner is given by

cout << DATA_BEGIN << "verbatim:";
cout << "Hi there!";
cout << DATA_END;
fflush (stdout);

Similarly, the code of display-output is given by

cout << DATA_BEGIN << "verbatim:";
cout << "You typed " << buffer;
cout << DATA_END;
fflush (stdout);

Remark 1. For synchronization purposes, TeXmacs will assume that the output is finished as soon as it encounters the DATA_END which closes the initial DATA_BEGIN. So all output has to be inside one single outer DATA_BEGIN-DATA_END block: if you send more blocks, then TeXmacs will retake control before reading all your output. It is possible to nest DATA_BEGIN-DATA_END blocks though, as we will see below.

Remark 2. In our example, the C++ code for the application is included in the plug-in. In the case when you are writing a TeXmacs interface for an existing application myapp, the convention is to create a –texmacs option for this program. Then it is no longer necessary to have myapp/src and myapp/bin directories for your plug-in and it suffices to configure the plug-in by putting something like the following in myapp/progs/init-myapp.scm:

(plugin-configure myapp

(:require (url-exists-in-path? "myapp"))

(:launch "myapp –texmacs")

(:session "Myapp"))

In the case when you do not have the possibility to modify the source code of myapp, you typically have to write an input/output filter tm_myapp for performing the appropriate rewritings. By looking at the standard plug-ins distributed with TeXmacs in

    $TEXMACS_PATH/plugins

you can find several examples of how this can be done.

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