GNU TeXmacsUser manualReferenceAboutEnglish
Sessions, connections and links: the plug-in machinery in C++

1.Introduction

This chapter describes the machinery which connects TeXmacs to extern programs, as seen from the source code. It is meant for developers who want to debug a misbehaving session, change the way output is processed, or add a new kind of connection (for instance the missing socket connections).

It assumes that the reader knows what a plug-in is and how it is configured; this is explained in the chapter on the plug-in system, whose last section, How the plug-in system works internally, already covers the discovery of plug-ins, the plug-in cache, the connection tables of

tm-plugins.scm

, the control characters and output formats of the pipe protocol, serialization of the input, special commands, background evaluation, remote plug-ins and the basic debugging techniques. The protocol is described from the point of view of the extern application in the chapter on interfacing TeXmacs with other programs, and all the options of plugin-configure are listed in the summary of configuration options. These topics are not repeated here; instead, this chapter goes one level deeper: it describes the C++ classes of

System/Link

, the output parser, the integration with the event loop, and the exact sequence of calls between Scheme and C++ during the life of a session.

2.Architecture overview

The communication with an extern program is organized in four layers. From the document down to the extern program, they are:

The session layer (Scheme)

The markup of sessions (the session tag with its input, unfolded-io, output and errput children) and the editing routines in

dynamic/session-edit.scm

. When an input field is evaluated, this layer builds a request and hands it over to the next layer. When output arrives, it inserts it into the document.

The request queue (Scheme)

The module

utils/plugins/plugin-eval.scm

keeps, for every pair (language, session), a queue of pending requests (plugin-pending). Each request carries four call-backs, which allows the same queue to serve interactive sessions (

session-edit.scm

), silent evaluations (silent-feed, used by scripts) and special commands (plugin-command, used by tab-completion). It is the only client of the Scheme glue functions connection-start, connection-write, connection-interrupt, etc., and it defines the two functions connection-notify and connection-notify-status which are called back from C++.

The connection layer (C++)

The resource connection in

System/Link/connection.cpp

. A connection is identified by the string name-session, owns a link, keeps track of the status of the extern program (waiting for input or for output), and owns two parsers of type texmacs_input (

Data/Convert/Generic/input.cpp

) which turn the byte streams of the standard output and the standard error into TeXmacs trees, one tree per channel.

The link layer (C++)

Subclasses of the abstract class tm_link_rep (

System/Link/tm_link.hpp

) which move bytes to and from the extern program: pipe_link_rep and qt_pipe_link_rep for processes, dyn_link_rep for shared libraries, cmdline_link_rep for one shell command per request and request_link_rep for HTTP requests.

The following diagram shows the main calls between the layers. Downward arrows are calls made on behalf of the user; upward arrows are notifications triggered by the event loop when the extern program produces output.

document: (session lan ses (document … (unfolded-io prompt in out) …))
  | session-feed (session-edit.scm)        ^ session-notify, session-next
  v                                        |
plugin-feed -> plugin-do -> plugin-write   connection-notify (lan ses ch tree)
  (plugin-eval.scm, queue per (lan ses))   connection-notify-status (lan ses st)
  | connection-start / -write / -stop      ^
  v                                        |
connection_rep (connection.cpp)            connection_rep::listen
  texmacs_input tm_out, tm_err             <- connection_callback
  | tm_link_rep::write (LINK_IN)           ^ tm_link_rep::feed_cmd
  v                                        |
pipe_link_rep, qt_pipe_link_rep, dyn_link_rep, cmdline_link_rep, …
  | stdin                                  ^ stdout, stderr
  v                                        |
extern process, shared library, shell command or HTTP request

The scheme language is special: it is handled entirely inside plugin-write, which evaluates the input with scheme-eval and calls connection-notify and connection-notify-status itself, without any C++ connection.

3.Map of the source files

C++ files are given relative to

src/src

, Scheme files relative to

src/TeXmacs/progs

.

System/Link/tm_link.hpp
,
tm_link.cpp

The abstract class tm_link_rep, the connection status constants, the control characters of the protocol, the factory functions make_pipe_link, make_dynamic_link, make_cmdline_link, make_request_link, and the packet functions tm_link_rep::write_packet and tm_link_rep::read_packet (only used by the client/server sockets of the collaboration tools, not by plug-ins).

System/Link/connect.hpp
,
connection.cpp

The connection resource and the functions connection_start, connection_write, connection_read, connection_interrupt, connection_stop, connection_status, connection_eval and connection_cmd.

System/Link/pipe_link.cpp

Pipes implemented with fork, execve of /bin/sh and select. Only compiled when QTTEXMACS is not defined, or when neither OS_MINGW nor QTPIPES is defined.

Plugins/Qt/qt_pipe_link.cpp
,
Plugins/Qt/QTMPipeLink.cpp

Pipes implemented with QProcess; the alternative to

pipe_link.cpp

for Qt builds on Windows or with QTPIPES. The CMake option QTPIPES is ON by default, so this is the implementation used by most current builds.

System/Link/dyn_link.hpp
,
dyn_link.cpp

Dynamic linking of shared libraries (symbol_install, dyn_link_rep), using the interface declared in

src/TeXmacs/include/TeXmacs.h

.

System/Link/cmdline_link.cpp
,
request_link.cpp

The links behind the :cmdline and :request options.

System/Link/socket_notifier.hpp
,
socket_notifier.cpp

A minimal registry of file descriptors with call-backs, polled by perform_select.

Data/Convert/Generic/input.hpp
,
input.cpp

The parser texmacs_input_rep which converts the output of a plug-in into trees.

Texmacs/Server/tm_server.cpp

The interpose handler tm_server_rep::interpose_handler, from which pending pipe output is processed, and close_all_pipes at exit.

Scheme/Glue/build-glue-basic.scm

The declarations of the glue functions connection-start, connection-status, connection-write-string, connection-write, connection-cmd, connection-eval, connection-interrupt and connection-stop, and of plugin-list.

Edit/Interface/edit_complete.cpp

The C++ part of tab-completion in sessions (edit_interface_rep::session_complete_command and edit_interface_rep::custom_complete).

kernel/texmacs/tm-plugins.scm

Translation of the options of plugin-configure into launcher descriptions, and the functions called from C++: connection-defined?, connection-info, connection-get-handlers, connection-cmdline, connection-request and connection-result.

utils/plugins/plugin-eval.scm

The request queue and the notification call-backs.

utils/plugins/plugin-cmd.scm

Serialization (plugin-serialize) and formatting of special commands (format-command), both called from C++.

dynamic/session-edit.scm
,
dynamic/session-menu.scm

Interactive sessions.

dynamic/scripts-edit.scm

Evaluation of scripts and executable fields in ordinary documents.

Two names are misleading. The function init_plugins in

System/Boot/init_texmacs.cpp

does not initialize plug-ins in the above sense: it loads the user settings and sets up TeX (the directories of plug-ins are added to the search paths by init_env_vars, see search paths). Similarly, the file

Edit/Process/edit_process.cpp

deals with the generation of bibliographies, tables of contents and indexes, not with sessions.

4.Threading and the event loop

Everything described in this chapter runs in the main thread of TeXmacs. There is no reader thread per plug-in: the output of extern programs is collected when the event loop polls for it, and the notifications into Scheme are made synchronously from the poll. The only exceptions are the HTTP requests of request_link_rep, which are run by a detached POSIX thread created in async_eval_system (

System/Misc/sys_utils.cpp

); the result is handed back to the main thread by async_eval_pending.

The central hook is tm_server_rep::interpose_handler, which the GUI calls regularly:

void
tm_server_rep::interpose_handler () {
#ifdef QTTEXMACS
#ifndef QTPIPES
  perform_select ();
#endif
  process_all_pipes ();
#else
  perform_select ();
  exec_pending_commands ();
#endif
  async_eval_pending ();
  … // apply pending changes to all views
}

In the Qt port, it is called from qt_gui_rep::update, which is rescheduled by a single-shot timer every 90 / 6 = 15 milliseconds when the editor is idle (

Plugins/Qt/qt_gui.cpp

), and is skipped while keyboard events are being processed. Consequently:

How each kind of link participates in the polling is described in links and the event loop.

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