~ubuntu-branches/ubuntu/hardy/texmacs/hardy

« back to all changes in this revision

Viewing changes to src/Data/Convert/Generic/input.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Treinen
  • Date: 2004-04-19 20:34:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040419203400-g4e34ih0315wcn8v
Tags: upstream-1.0.3-R2
ImportĀ upstreamĀ versionĀ 1.0.3-R2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************************************
 
3
* MODULE     : input.cpp
 
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
******************************************************************************/
 
12
 
 
13
#include "path.hpp"
 
14
#include "convert.hpp"
 
15
#include "hashmap.hpp"
 
16
#include "tm_link.hpp"
 
17
#include "Generic/input.hpp"
 
18
#include "scheme.hpp"
 
19
 
 
20
#define STATUS_NORMAL 0
 
21
#define STATUS_ESCAPE 1
 
22
#define STATUS_BEGIN  2
 
23
 
 
24
#define MODE_VERBATIM 0
 
25
#define MODE_SCHEME   1
 
26
#define MODE_LATEX    2
 
27
#define MODE_HTML     3
 
28
#define MODE_PS       4
 
29
#define MODE_CHANNEL  5
 
30
#define MODE_COMMAND  6
 
31
#define MODE_XFORMAT  7
 
32
 
 
33
/******************************************************************************
 
34
* Universal data input
 
35
******************************************************************************/
 
36
 
 
37
texmacs_input_rep::texmacs_input_rep (string type2):
 
38
  type (type2),
 
39
  status (STATUS_NORMAL),
 
40
  buf (""),
 
41
  format ("verbatim"),
 
42
  mode (get_mode (format)),
 
43
  channel (type),
 
44
  stack (""),
 
45
  docs (tree (DOCUMENT, "")) { bof (); }
 
46
 
 
47
texmacs_input::texmacs_input (string type):
 
48
  rep (new texmacs_input_rep (type)) {}
 
49
 
 
50
/******************************************************************************
 
51
* Mode and channel handling
 
52
******************************************************************************/
 
53
 
 
54
int
 
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;
 
64
  return MODE_VERBATIM;
 
65
}
 
66
 
 
67
void
 
68
texmacs_input_rep::begin_mode (string s) {
 
69
  stack = tuple (format, channel, stack);
 
70
  format= s;
 
71
  mode  = get_mode (format);
 
72
}
 
73
 
 
74
void
 
75
texmacs_input_rep::begin_channel (string s) {
 
76
  stack  = tuple (format, channel, stack);
 
77
  channel= s;
 
78
  if ((channel == "prompt") || (channel == "input"))
 
79
    docs (channel)= tree (DOCUMENT, "");
 
80
}
 
81
 
 
82
void
 
83
texmacs_input_rep::end () {
 
84
  if (stack != "") {
 
85
    format = stack[0]->label;
 
86
    mode   = get_mode (format);
 
87
    channel= stack[1]->label;
 
88
    stack  = stack[2];
 
89
  }
 
90
}
 
91
 
 
92
/******************************************************************************
 
93
* Main routines
 
94
******************************************************************************/
 
95
 
 
96
bool
 
97
texmacs_input_rep::put (char c) { // returns true when expecting input
 
98
  /*
 
99
  if (c == DATA_BEGIN) cout << "[BEGIN]";
 
100
  else if (c == DATA_END) cout << "[END]";
 
101
  else if (c == DATA_ESCAPE) cout << "[ESCAPE]";
 
102
  else cout << c;
 
103
  */
 
104
 
 
105
  bool block_done= false;
 
106
  switch (status) {
 
107
  case STATUS_NORMAL:
 
108
    if (c == DATA_ESCAPE) status= STATUS_ESCAPE;
 
109
    else if (c == DATA_BEGIN) {
 
110
      flush (true);
 
111
      status= STATUS_BEGIN;
 
112
    }
 
113
    else if (c == DATA_END) {
 
114
      flush (true);
 
115
      end ();
 
116
      block_done= (stack == "");
 
117
    }
 
118
    else buf << c;
 
119
    break;
 
120
  case STATUS_ESCAPE:
 
121
    buf << c;
 
122
    status= STATUS_NORMAL;
 
123
    break;
 
124
  case STATUS_BEGIN:
 
125
    if (c == ':') {
 
126
      begin_mode (buf);
 
127
      buf   = "";
 
128
      status= STATUS_NORMAL;
 
129
    }
 
130
    else if (c == '#') {
 
131
      begin_channel (buf);
 
132
      buf   = "";
 
133
      status= STATUS_NORMAL;
 
134
    }
 
135
    else buf << c;
 
136
    break;
 
137
  }
 
138
  if (status == STATUS_NORMAL) flush ();
 
139
  return block_done;
 
140
}
 
141
 
 
142
void
 
143
texmacs_input_rep::bof () {
 
144
  format = "verbatim";
 
145
  channel= type;
 
146
  docs (channel)= tree (DOCUMENT, "");
 
147
}
 
148
 
 
149
void
 
150
texmacs_input_rep::eof () {
 
151
  flush (true);
 
152
}
 
153
 
 
154
void
 
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];
 
161
  else {
 
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]);
 
165
  }
 
166
  t << A (u (1, N(u)));
 
167
}
 
168
 
 
169
tree
 
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 "";
 
175
  tree t= doc;
 
176
  doc= tree (DOCUMENT, "");
 
177
  return t;
 
178
}
 
179
 
 
180
/******************************************************************************
 
181
* Flushing
 
182
******************************************************************************/
 
183
 
 
184
void
 
185
texmacs_input_rep::flush (bool force) {
 
186
  if ((!force) && (channel == "error") && (stack != "")) return;
 
187
  switch (mode) {
 
188
  case MODE_VERBATIM:
 
189
    verbatim_flush (force);
 
190
    break;
 
191
  case MODE_SCHEME:
 
192
    scheme_flush (force);
 
193
    break;
 
194
  case MODE_LATEX:
 
195
    latex_flush (force);
 
196
    break;
 
197
  case MODE_HTML:
 
198
    html_flush (force);
 
199
    break;
 
200
  case MODE_PS:
 
201
    ps_flush (force);
 
202
    break;
 
203
  case MODE_CHANNEL:
 
204
    channel_flush (force);
 
205
    break;
 
206
  case MODE_COMMAND:
 
207
    command_flush (force);
 
208
    break;
 
209
  case MODE_XFORMAT:
 
210
    xformat_flush (force);
 
211
    break;
 
212
  default:
 
213
    fatal_error ("invalid mode", "texmacs_input_rep::flush", "input.cpp");
 
214
    break;
 
215
  }
 
216
}
 
217
 
 
218
void
 
219
texmacs_input_rep::verbatim_flush (bool force) {
 
220
  if (force || ends (buf, "\n")) {
 
221
    write (verbatim_to_tree (buf));
 
222
    buf= "";
 
223
  }
 
224
}
 
225
 
 
226
void
 
227
texmacs_input_rep::scheme_flush (bool force) {
 
228
  if (force) {
 
229
    write (scheme_to_tree (buf));
 
230
    buf= "";
 
231
  }
 
232
}
 
233
 
 
234
void
 
235
texmacs_input_rep::latex_flush (bool force) {
 
236
  if (force || ends (buf, "\n\n")) {
 
237
    write (generic_to_tree (buf, "latex-snippet"));
 
238
    buf= "";
 
239
  }
 
240
}
 
241
 
 
242
void
 
243
texmacs_input_rep::html_flush (bool force) {
 
244
  if (force || ends (buf, "</P>")) {
 
245
    write (generic_to_tree (buf, "html-snippet"));
 
246
    buf= "";
 
247
  }
 
248
}
 
249
 
 
250
void
 
251
texmacs_input_rep::ps_flush (bool force) {
 
252
  if (force) {
 
253
    tree t (POSTSCRIPT, tuple (tree (RAW_DATA, copy (buf)), "ps"));
 
254
    t << "" << "" << "" << "" << "" << "";
 
255
    write (t);
 
256
    buf= "";
 
257
  }
 
258
}
 
259
 
 
260
void
 
261
texmacs_input_rep::channel_flush (bool force) {
 
262
  if (force) {
 
263
    if ((buf == "prompt") || (buf == "input"))
 
264
      docs (buf)= tree (DOCUMENT, "");
 
265
    stack[1]= buf;
 
266
    buf= "";
 
267
  }
 
268
}
 
269
 
 
270
void
 
271
texmacs_input_rep::command_flush (bool force) {
 
272
  if (force) {
 
273
    eval ("(begin " * buf * ")");
 
274
    buf= "";
 
275
  }
 
276
}
 
277
 
 
278
void
 
279
texmacs_input_rep::xformat_flush (bool force) {
 
280
  if (force) {
 
281
    write (generic_to_tree (buf, format * "-snippet"));
 
282
    buf= "";
 
283
  }
 
284
}