![]() |
Tab-completion | ![]() |
By default, TeXmacs looks into your document for possible tab-completions. Inside sessions for your application, you might wish to customize this behaviour, so as to complete built-in commands. In order to do this, you have to specify the configuration option
(:tab-completion #t) |
in your init-myplugin.scm file, so that TeXmacs will send special tab-completion requests to your application whenever you press ⇥ inside a session. These commands are of the form
DATA_COMMAND(complete input-string cursor-position)↩ |
Here DATA_COMMAND stands for the special character '\20' (ASCII 16). The input-string is the complete string in which the ⇥ occurred, written as a quoted Scheme string, and the cursor-position is an integer which specifies the position of the cursor when you pressed ⇥. TeXmacs expects your application to return a tuple with all possible tab-completions of the form
DATA_BEGINscheme:(tuple root completion-1 \(\cdots\) completion-n)DATA_END |
Here root corresponds to a substring before the cursor for which completions could be found. The strings completion-1 until completion-n are the list of completions as they might be inserted at the current cursor position. If no completions could be found, then you may also return the empty string.
Remark 1. In principle, the tab-completion mechanism should still work in mathematical input mode. In that case, the input-string will correspond to the serialization of the TeXmacs input. Notice that the command is computed by edit_interface_rep::session_complete_command (in
src/Edit/Interface/edit_complete.cpp
) using the default serializer verbatim-serialize, and not the custom serializer of the plug-in. The answer of the application is passed to the Scheme function custom-complete, which ignores answers which are not a tuple with at least one completion.
Remark 2. The way TeXmacs sends commands to your application can be customized in a similar way as for the input: we provide a :commander configuration option for this, which works in a similar way as the :serializer option.
A very rudimentary example of how the tab-completion mechanism works is given by the complete plug-in, which consists of the following files:
complete/Makefile
complete/progs/init-complete.scm
complete/src/complete.cpp
The startup banner in
complete.cpp
takes care of part of the configuration:
cout << DATA_BEGIN << "verbatim:";
format_plugin ();
cout << "We know how to complete 'h'";
cout << DATA_END;
fflush (stdout); |
Here format_plugin is given by
void
format_plugin () {
// The configuration of a plugin can be completed at startup time.
// This may be interesting for adding tab-completion a posteriori.
cout << DATA_BEGIN << "command:";
cout << "(plugin-configure complete (:tab-completion #t))";
cout << DATA_END;
} |
In the main loop, we first deal with regular input:
char buffer[100];
cin.getline (buffer, 100, '\n');
if (buffer[0] != DATA_COMMAND) {
cout << DATA_BEGIN << "verbatim:";
cout << "You typed " << buffer;
cout << DATA_END;
} |
We next treat the case when a tab-completion command is sent to the application:
else {
cout << DATA_BEGIN << "scheme:";
cout << "(tuple \"h\" \"ello\" \"i there\" \"ola\" \"opsakee\")";
cout << DATA_END;
}
cout.flush (); |
As you notice, the actual command is ignored, so our example is really very rudimentary.