2
/******************************************************************************
4
* DESCRIPTION: Generic TeXmacs input
5
* COPYRIGHT : (C) 2000 Joris van der Hoeven
6
*******************************************************************************
7
* This software falls under the GNU general public license and comes WITHOUT
8
* ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
9
* If you don't have this file, write to the Free Software Foundation, Inc.,
10
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
11
******************************************************************************/
14
#include "convert.hpp"
15
#include "hashmap.hpp"
16
#include "tm_link.hpp"
17
#include "Generic/input.hpp"
20
#define STATUS_NORMAL 0
21
#define STATUS_ESCAPE 1
22
#define STATUS_BEGIN 2
24
#define MODE_VERBATIM 0
29
#define MODE_CHANNEL 5
30
#define MODE_COMMAND 6
31
#define MODE_XFORMAT 7
33
/******************************************************************************
34
* Universal data input
35
******************************************************************************/
37
texmacs_input_rep::texmacs_input_rep (string type2):
39
status (STATUS_NORMAL),
42
mode (get_mode (format)),
45
docs (tree (DOCUMENT, "")) { bof (); }
47
texmacs_input::texmacs_input (string type):
48
rep (new texmacs_input_rep (type)) {}
50
/******************************************************************************
51
* Mode and channel handling
52
******************************************************************************/
55
texmacs_input_rep::get_mode (string s) {
56
if (s == "verbatim") return MODE_VERBATIM;
57
if (s == "latex") return MODE_LATEX;
58
if (s == "scheme") return MODE_SCHEME;
59
if (s == "html") return MODE_HTML;
60
if (s == "ps") return MODE_PS;
61
if (s == "channel") return MODE_CHANNEL;
62
if (s == "command") return MODE_COMMAND;
63
if (as_bool (call ("format?", s))) return MODE_XFORMAT;
68
texmacs_input_rep::begin_mode (string s) {
69
stack = tuple (format, channel, stack);
71
mode = get_mode (format);
75
texmacs_input_rep::begin_channel (string s) {
76
stack = tuple (format, channel, stack);
78
if ((channel == "prompt") || (channel == "input"))
79
docs (channel)= tree (DOCUMENT, "");
83
texmacs_input_rep::end () {
85
format = stack[0]->label;
86
mode = get_mode (format);
87
channel= stack[1]->label;
92
/******************************************************************************
94
******************************************************************************/
97
texmacs_input_rep::put (char c) { // returns true when expecting input
99
if (c == DATA_BEGIN) cout << "[BEGIN]";
100
else if (c == DATA_END) cout << "[END]";
101
else if (c == DATA_ESCAPE) cout << "[ESCAPE]";
105
bool block_done= false;
108
if (c == DATA_ESCAPE) status= STATUS_ESCAPE;
109
else if (c == DATA_BEGIN) {
111
status= STATUS_BEGIN;
113
else if (c == DATA_END) {
116
block_done= (stack == "");
122
status= STATUS_NORMAL;
128
status= STATUS_NORMAL;
133
status= STATUS_NORMAL;
138
if (status == STATUS_NORMAL) flush ();
143
texmacs_input_rep::bof () {
146
docs (channel)= tree (DOCUMENT, "");
150
texmacs_input_rep::eof () {
155
texmacs_input_rep::write (tree u) {
156
if (!docs->contains (channel))
157
docs (channel)= tree (DOCUMENT, "");
158
tree& t= docs (channel);
159
if (!is_document (u)) u= tree (DOCUMENT, u);
160
if (t[N(t)-1] == "") t[N(t)-1]= u[0];
162
if (!is_concat (t[N(t)-1])) t[N(t)-1]= tree (CONCAT, t[N(t)-1]);
163
if (!is_concat (u[0])) u[0]= tree (CONCAT, u[0]);
164
t[N(t)-1] << A(u[0]);
166
t << A (u (1, N(u)));
170
texmacs_input_rep::get (string ch) {
171
if (!docs->contains (channel))
172
docs (channel)= tree (DOCUMENT, "");
173
tree& doc= docs (ch);
174
if (doc == tree (DOCUMENT, "")) return "";
176
doc= tree (DOCUMENT, "");
180
/******************************************************************************
182
******************************************************************************/
185
texmacs_input_rep::flush (bool force) {
186
if ((!force) && (channel == "error") && (stack != "")) return;
189
verbatim_flush (force);
192
scheme_flush (force);
204
channel_flush (force);
207
command_flush (force);
210
xformat_flush (force);
213
fatal_error ("invalid mode", "texmacs_input_rep::flush", "input.cpp");
219
texmacs_input_rep::verbatim_flush (bool force) {
220
if (force || ends (buf, "\n")) {
221
write (verbatim_to_tree (buf));
227
texmacs_input_rep::scheme_flush (bool force) {
229
write (scheme_to_tree (buf));
235
texmacs_input_rep::latex_flush (bool force) {
236
if (force || ends (buf, "\n\n")) {
237
write (generic_to_tree (buf, "latex-snippet"));
243
texmacs_input_rep::html_flush (bool force) {
244
if (force || ends (buf, "</P>")) {
245
write (generic_to_tree (buf, "html-snippet"));
251
texmacs_input_rep::ps_flush (bool force) {
253
tree t (POSTSCRIPT, tuple (tree (RAW_DATA, copy (buf)), "ps"));
254
t << "" << "" << "" << "" << "" << "";
261
texmacs_input_rep::channel_flush (bool force) {
263
if ((buf == "prompt") || (buf == "input"))
264
docs (buf)= tree (DOCUMENT, "");
271
texmacs_input_rep::command_flush (bool force) {
273
eval ("(begin " * buf * ")");
279
texmacs_input_rep::xformat_flush (bool force) {
281
write (generic_to_tree (buf, format * "-snippet"));