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

« back to all changes in this revision

Viewing changes to src/Texmacs/Server/tm_server.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     : tm_server.cpp
 
4
* DESCRIPTION: The TeXmacs server
 
5
* COPYRIGHT  : (C) 1999  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 "tm_server.hpp"
 
14
#include "convert.hpp"
 
15
#include "connect.hpp"
 
16
#include "sys_utils.hpp"
 
17
#include "file.hpp"
 
18
#include "pipe_link.hpp"
 
19
#include "socket_link.hpp"
 
20
 
 
21
server* the_server= NULL;
 
22
url tm_init_file= url_none ();
 
23
url my_init_file= url_none ();
 
24
string my_init_cmds= "";
 
25
 
 
26
void reset_inclusions ();
 
27
extern string printing_dpi;
 
28
extern string printing_cmd;
 
29
extern string printing_on;
 
30
extern int nr_windows;
 
31
extern window (*get_current_window) (void);
 
32
 
 
33
/******************************************************************************
 
34
* Creation of TeXmacs window
 
35
******************************************************************************/
 
36
 
 
37
int geometry_w= 800, geometry_h= 600;
 
38
int geometry_x= 0  , geometry_y= 0;
 
39
 
 
40
window
 
41
texmacs_window (widget wid) {
 
42
  int W, H;
 
43
  int w= geometry_w, h= geometry_h;
 
44
  int x= geometry_x, y= geometry_y;
 
45
  wid->dis->get_extents (W, H); W /= PIXEL; H /= PIXEL;
 
46
  if (x < 0) x= W + x + 1 - w;
 
47
  if (y < 0) y= H + y + 1 - h;
 
48
  return plain_window (wid, "TeXmacs", w*PIXEL, h*PIXEL, x*PIXEL, (-y)*PIXEL);
 
49
}
 
50
 
 
51
/******************************************************************************
 
52
* Texmacs server constructor and destructor
 
53
******************************************************************************/
 
54
 
 
55
void
 
56
texmacs_interpose_handler () {
 
57
  if (the_server != NULL)
 
58
    (*the_server)->interpose_handler ();
 
59
}
 
60
 
 
61
void
 
62
texmacs_wait_handler (string message, string arg, int level) {
 
63
  (void) level;
 
64
  if (the_server != NULL)
 
65
    (*the_server)->wait_handler (message, arg);
 
66
}
 
67
 
 
68
window
 
69
texmacs_current_window () {
 
70
  if (the_server == NULL)
 
71
    fatal_error ("No server", "texmacs_current_window", "tm_server.cpp");
 
72
  return (*the_server)->get_window()->win;
 
73
}
 
74
 
 
75
server
 
76
get_server () {
 
77
  if (the_server == NULL)
 
78
    fatal_error ("TeXmacs server not yet started", "get_server");
 
79
  return *the_server;
 
80
}
 
81
 
 
82
server_rep::server_rep () {}
 
83
server_rep::~server_rep () {}
 
84
 
 
85
tm_server_rep::tm_server_rep (display dis2):
 
86
  dis (dis2), vw (NULL), banner_nr (-1), full_screen (false), def_sfactor (5),
 
87
  style_cache (hashmap<string,tree> (UNINIT)),
 
88
  style_drd (tree (COLLECTION))
 
89
{
 
90
  the_server= new server (this);
 
91
  initialize_guile ();
 
92
  set_interpose_handler (texmacs_interpose_handler);
 
93
  set_wait_handler (texmacs_wait_handler);
 
94
  get_current_window= texmacs_current_window;
 
95
  out_lan= dis->get_output_language ();
 
96
  if (is_none (tm_init_file))
 
97
    tm_init_file= "$TEXMACS_PATH/progs/init-texmacs.scm";
 
98
  if (is_none (my_init_file))
 
99
    my_init_file= "$TEXMACS_HOME_PATH/progs/my-init-texmacs.scm";
 
100
  if (exists (tm_init_file)) exec_file (tm_init_file);
 
101
  if (exists (my_init_file)) exec_file (my_init_file);
 
102
  if (my_init_cmds != "") {
 
103
    my_init_cmds= "(begin" * my_init_cmds * ")";
 
104
    exec_delayed (my_init_cmds);
 
105
  }
 
106
  style_update_menu ();
 
107
#ifdef OS_GNU_LINUX
 
108
  return; // in order to avoid segmentation faults
 
109
#elif defined OS_POWERPC_GNU_LINUX
 
110
  return; // in order to avoid segmentation faults
 
111
#endif
 
112
}
 
113
 
 
114
void
 
115
tm_server_rep::advance_banner () {
 
116
  banner_nr++;
 
117
  if (get_editor()->et != tree (DOCUMENT, "")) banner_nr=5;
 
118
  if (banner_nr < 4) {
 
119
    static string banner[4]= {
 
120
      "Welcome to GNU TeXmacs",
 
121
      "GNU TeXmacs falls under the GNU general public license",
 
122
      "GNU TeXmacs comes without any form of legal warranty",
 
123
      "More information about GNU TeXmacs can be found in the Help->About menu"
 
124
    };
 
125
    set_message (banner[banner_nr], "GNU TeXmacs " TEXMACS_VERSION);
 
126
    dis->delayed_message (get_meta(), "banner", 2500);
 
127
  }
 
128
 
 
129
  else if (banner_nr == 4) set_message ("", "");
 
130
}
 
131
 
 
132
tm_server_rep::~tm_server_rep () {}
 
133
server::server (display dis): rep (new tm_server_rep (dis)) {}
 
134
 
 
135
/******************************************************************************
 
136
* Get and set objects associated to server
 
137
******************************************************************************/
 
138
 
 
139
server_rep*
 
140
tm_server_rep::get_server () {
 
141
  return this;
 
142
}
 
143
 
 
144
display
 
145
tm_server_rep::get_display () {
 
146
  return dis;
 
147
}
 
148
 
 
149
bool
 
150
tm_server_rep::has_view () {
 
151
  return vw != NULL;
 
152
}
 
153
 
 
154
tm_view
 
155
tm_server_rep::get_view (bool must_be_valid) {
 
156
  if (must_be_valid && (vw==NULL))
 
157
    fatal_error ("No active view", "tm_server_rep::get_meta");
 
158
  return vw;
 
159
}
 
160
 
 
161
void
 
162
tm_server_rep::set_view (tm_view vw2) {
 
163
  vw= vw2;
 
164
}
 
165
 
 
166
tm_buffer
 
167
tm_server_rep::get_buffer () {
 
168
  tm_view vw= get_view ();
 
169
  return vw->buf;
 
170
}
 
171
 
 
172
editor
 
173
tm_server_rep::get_editor () {
 
174
  tm_view vw= get_view ();
 
175
  // cout << "Get editor" << vw->ed << "\n";
 
176
  return vw->ed;
 
177
}
 
178
 
 
179
tm_window
 
180
tm_server_rep::get_window () {
 
181
  tm_view vw= get_view ();
 
182
  if (vw->win==NULL)
 
183
    fatal_error ("No window attached to view", "tm_server_rep::get_meta");
 
184
  return vw->win;
 
185
}
 
186
 
 
187
tm_widget
 
188
tm_server_rep::get_meta () {
 
189
  tm_window win= get_window ();
 
190
  return win->wid;
 
191
}
 
192
 
 
193
color
 
194
tm_server_rep::get_color (string s) {
 
195
  return get_display () -> get_color (s);
 
196
}
 
197
 
 
198
int
 
199
tm_server_rep::get_nr_windows () {
 
200
  return nr_windows;
 
201
}
 
202
 
 
203
/******************************************************************************
 
204
* The style and package menus
 
205
******************************************************************************/
 
206
 
 
207
static string
 
208
compute_style_menu (url u, bool package) {
 
209
  if (is_or (u)) {
 
210
    string sep= "\n";
 
211
    if (is_atomic (u[1]) &&
 
212
        ((is_concat (u[2]) && (u[2][1] != "CVS")) ||
 
213
         (is_or (u[2]) && is_concat (u[2][1]))))
 
214
      sep= "\n---\n";
 
215
    return
 
216
      compute_style_menu (u[1], package) * sep *
 
217
      compute_style_menu (u[2], package);
 
218
  }
 
219
  if (is_concat (u)) {
 
220
    string dir= upcase_first (as_string (u[1]));
 
221
    string sub= compute_style_menu (u[2], package);
 
222
    if ((dir == "Test") || (dir == "Obsolete") || (dir == "CVS")) return "";
 
223
    return "(-> \"" * dir * "\" " * sub * ")";
 
224
  }
 
225
  if (is_atomic (u)) {
 
226
    string l  = as_string (u);
 
227
    if (!ends (l, ".ts")) return "";
 
228
    l= l(0, N(l)-3);
 
229
    string cmd= package? string ("init-extra-style"): string ("init-style");
 
230
    return "(\"" * l * "\" (" * cmd * " \"" * l * "\"))";
 
231
  }
 
232
  return "";
 
233
}
 
234
 
 
235
void
 
236
tm_server_rep::style_update_menu () {
 
237
  string sty= compute_style_menu (descendance ("$TEXMACS_STYLE_ROOT"), false);
 
238
  string pck= compute_style_menu (descendance ("$TEXMACS_PACKAGE_ROOT"), true);
 
239
  (void) eval ("(menu-bind style-menu " * sty * ")");
 
240
  (void) eval ("(menu-bind use-package-menu " * pck * ")");
 
241
}
 
242
 
 
243
/******************************************************************************
 
244
* Caching style files
 
245
******************************************************************************/
 
246
 
 
247
static string
 
248
cache_file_name (tree t) {
 
249
  if (is_atomic (t)) return t->label;
 
250
  else {
 
251
    string s;
 
252
    int i, n= N(t);
 
253
    for (i=0; i<n; i++)
 
254
      s << "__" << cache_file_name (t[i]);
 
255
    return s * "__";
 
256
  }
 
257
}
 
258
 
 
259
void
 
260
tm_server_rep::style_clear_cache () {
 
261
  style_cache=
 
262
    hashmap<tree,hashmap<string,tree> > (hashmap<string,tree> (UNINIT));
 
263
  remove ("$TEXMACS_HOME_PATH/system/cache" * url_wildcard ("*"));
 
264
 
 
265
  int i, j, n= N(bufs);
 
266
  for (i=0; i<n; i++) {
 
267
    tm_buffer buf= ((tm_buffer) bufs[i]);
 
268
    for (j=0; j<N(buf->vws); j++)
 
269
      ((tm_view) (buf->vws[j]))->ed->init_style ();
 
270
  }
 
271
 
 
272
  style_update_menu ();
 
273
}
 
274
 
 
275
void
 
276
tm_server_rep::style_set_cache (tree style, hashmap<string,tree> H, tree t) {
 
277
  // cout << "set cache " << style << LF;
 
278
  style_cache (copy (style))= H;
 
279
  style_drd   (copy (style))= t;
 
280
  url name ("$TEXMACS_HOME_PATH/system/cache", cache_file_name (style));
 
281
  if (!exists (name)) {
 
282
    save_string (name, tree_to_scheme (tuple ((tree) H, t)));
 
283
    // cout << "saved " << name << LF;
 
284
  }
 
285
}
 
286
 
 
287
void
 
288
tm_server_rep::style_get_cache (
 
289
  tree style, hashmap<string,tree>& H, tree& t, bool& f)
 
290
{
 
291
  // cout << "get cache " << style << LF;
 
292
  if ((style == "") || (style == tree (TUPLE))) { f= false; return; }
 
293
  f= style_cache->contains (style);
 
294
  if (f) {
 
295
    H= style_cache [style];
 
296
    t= style_drd   [style];
 
297
  }
 
298
  else {
 
299
    string s;
 
300
    url name ("$TEXMACS_HOME_PATH/system/cache", cache_file_name (style));
 
301
    if (exists (name) && (!load_string (name, s))) {
 
302
      // cout << "loaded " << name << LF;
 
303
      tree pair= scheme_to_tree (s);
 
304
      H= hashmap<string,tree> (UNINIT, pair[0]);
 
305
      t= pair[1];
 
306
      style_cache (copy (style))= H;
 
307
      style_drd   (copy (style))= t;
 
308
      f= true;
 
309
    }
 
310
  }
 
311
}
 
312
 
 
313
/******************************************************************************
 
314
* Routines concerning the widget
 
315
******************************************************************************/
 
316
 
 
317
void
 
318
tm_server_rep::get_visible (SI& x1, SI& y1, SI& x2, SI& y2) {
 
319
  widget meta= (widget) get_meta ();
 
320
  meta["canvas"] << ::get_visible (x1, y1, x2, y2);
 
321
}
 
322
 
 
323
void
 
324
tm_server_rep::scroll_where (SI& x, SI& y) {
 
325
  widget meta= (widget) get_meta ();
 
326
  meta["canvas"] << get_coord2 ("scroll position", x, y);
 
327
}
 
328
 
 
329
void
 
330
tm_server_rep::scroll_to (SI x, SI y) {
 
331
  widget meta= (widget) get_meta ();
 
332
  meta["canvas"] << set_scroll_pos (x, y);
 
333
}
 
334
 
 
335
void
 
336
tm_server_rep::set_extents (SI x1, SI y1, SI x2, SI y2) {
 
337
  widget meta= (widget) get_meta ();
 
338
  meta["canvas"] << ::set_extents (x1, y1, x2, y2);
 
339
}
 
340
 
 
341
void
 
342
tm_server_rep::set_left_footer (string s) {
 
343
  if ((vw == NULL) || (vw->win == NULL)) return;
 
344
  get_meta()->set_left_footer (s);
 
345
}
 
346
 
 
347
void
 
348
tm_server_rep::set_right_footer (string s) {
 
349
  if ((vw == NULL) || (vw->win == NULL)) return;
 
350
  get_meta()->set_right_footer (s);
 
351
}
 
352
 
 
353
void
 
354
tm_server_rep::set_message (string left, string right) {
 
355
  get_editor()->set_message (left, right);
 
356
}
 
357
 
 
358
void
 
359
tm_server_rep::interactive (string name, string& s, command call_back) {
 
360
  get_meta()->interactive (name, s, call_back);
 
361
}
 
362
 
 
363
void
 
364
tm_server_rep::full_screen_mode (bool on) {
 
365
  widget meta= (widget) get_meta ();
 
366
  if (on) {
 
367
    show_header (false);
 
368
    show_footer (false);
 
369
    meta ["canvas"] << set_integer ("scrollbars", false);
 
370
  }
 
371
  else {
 
372
    show_header (true);
 
373
    show_footer (true);
 
374
    meta ["canvas"] << set_integer ("scrollbars", true);
 
375
  }
 
376
  meta->win->full_screen (on);
 
377
  get_editor()->full_screen_mode (on);
 
378
  full_screen = on;
 
379
}
 
380
 
 
381
bool
 
382
tm_server_rep::in_full_screen_mode () {
 
383
  return full_screen;
 
384
}
 
385
 
 
386
/******************************************************************************
 
387
* Miscellaneous routines
 
388
******************************************************************************/
 
389
 
 
390
void
 
391
tm_server_rep::interpose_handler () {
 
392
  listen_to_pipes ();
 
393
  listen_to_sockets ();
 
394
  listen_to_connections ();
 
395
  exec_pending_commands ();
 
396
 
 
397
  int i,j;
 
398
  for (i=0; i<N(bufs); i++) {
 
399
    tm_buffer buf= (tm_buffer) bufs[i];
 
400
    for (j=0; j<N(buf->vws); j++) {
 
401
      tm_view vw= (tm_view) buf->vws[j];
 
402
      if (vw->win != NULL) vw->ed->process_extern_input ();
 
403
    }
 
404
    for (j=0; j<N(buf->vws); j++) {
 
405
      tm_view vw= (tm_view) buf->vws[j];
 
406
      if (vw->win != NULL) vw->ed->apply_changes ();
 
407
    }
 
408
  }
 
409
}
 
410
 
 
411
void
 
412
tm_server_rep::wait_handler (string message, string arg) {
 
413
  dis->set_wait_indicator (message, arg);
 
414
}
 
415
 
 
416
void
 
417
tm_server_rep::set_script_status (int i) {
 
418
  script_status= i;
 
419
}
 
420
 
 
421
void
 
422
tm_server_rep::focus_on_editor (editor ed) {
 
423
  int i,j;
 
424
  for (i=0; i<N(bufs); i++) {
 
425
    tm_buffer buf= (tm_buffer) bufs[i];
 
426
    for (j=0; j<N(buf->vws); j++) {
 
427
      tm_view vw= (tm_view) buf->vws[j];
 
428
      if (vw->ed == ed) {
 
429
        set_view (vw);
 
430
        return;
 
431
      }
 
432
    }
 
433
  }
 
434
  fatal_error ("Invalid situation", "tm_server_rep::focus_on_editor");
 
435
}
 
436
 
 
437
void
 
438
tm_server_rep::set_printing_command (string cmd) {
 
439
  printing_cmd= cmd;
 
440
}
 
441
 
 
442
void
 
443
tm_server_rep::set_printer_page_type (string type) {
 
444
  printing_on= type;
 
445
}
 
446
 
 
447
void
 
448
tm_server_rep::set_printer_dpi (string dpi) {
 
449
  printing_dpi= dpi;
 
450
}
 
451
 
 
452
void
 
453
tm_server_rep::set_default_shrinking_factor (int sf) {
 
454
  def_sfactor= sf;
 
455
}
 
456
 
 
457
int
 
458
tm_server_rep::get_default_shrinking_factor () {
 
459
  return def_sfactor;
 
460
}
 
461
 
 
462
void
 
463
tm_server_rep::postscript_gc (string which) {
 
464
  dis->postscript_gc (which);
 
465
  int i,j;
 
466
  for (i=0; i<N(bufs); i++) {
 
467
    tm_buffer buf= (tm_buffer) bufs[i];
 
468
    for (j=0; j<N(buf->vws); j++) {
 
469
      tm_view vw= (tm_view) buf->vws[j];
 
470
      vw->ed->typeset_invalidate_all ();
 
471
    }
 
472
  }  
 
473
}
 
474
 
 
475
void
 
476
tm_server_rep::inclusions_gc (string which) {
 
477
  (void) which;
 
478
  reset_inclusions ();
 
479
  int i,j;
 
480
  for (i=0; i<N(bufs); i++) {
 
481
    tm_buffer buf= (tm_buffer) bufs[i];
 
482
    for (j=0; j<N(buf->vws); j++) {
 
483
      tm_view vw= (tm_view) buf->vws[j];
 
484
      vw->ed->typeset_invalidate_all ();
 
485
    }
 
486
  }  
 
487
}
 
488
 
 
489
string
 
490
tm_server_rep::translate (string which, string from, string to) {
 
491
  display dis= get_display ();
 
492
  return dis->translate (which, from, to);
 
493
}
 
494
 
 
495
bool
 
496
tm_server_rep::is_yes (string s) {
 
497
  s= locase_all (s);
 
498
  return
 
499
    (s == "ano") || (s == "a") ||
 
500
    (s == "yes") || (s == "y") ||
 
501
    (s == "oui") || (s == "o") ||
 
502
    (s == "ja") || (s == "j") ||
 
503
    (s == "si") || (s == "s") ||
 
504
    (s == "sim") || (s == "s") ||
 
505
    (s == "tak") || (s == "t");
 
506
}
 
507
 
 
508
void
 
509
tm_server_rep::quit () {
 
510
  close_all_pipes ();
 
511
  exit (0);
 
512
}
 
513
 
 
514
/******************************************************************************
 
515
* Extern packages
 
516
******************************************************************************/
 
517
 
 
518
tree
 
519
tm_server_rep::evaluate (string name, string session, tree expr) {
 
520
  if (name == "scheme") {
 
521
    string s= tree_to_verbatim (expr);
 
522
    string r= object_to_string (::eval (s));
 
523
    if (r == "#<unspecified>") r= "";
 
524
    return verbatim_to_tree (r);
 
525
  }
 
526
  if (!connection_declared (name)) {
 
527
    set_message ("Package#'" * name * "'#not declared",
 
528
                 "Evaluate#'" * name * "'#expression");
 
529
    return "";
 
530
  }
 
531
  if (connection_status (name, session) == CONNECTION_DEAD) {
 
532
    string r= connection_start (name, session);
 
533
    set_message (r, "Started#'" * name * "'");
 
534
    if (connection_status (name, session) == CONNECTION_DEAD) return "";
 
535
  }
 
536
  return connection_eval (name, session, expr);
 
537
}
 
538
 
 
539
/******************************************************************************
 
540
* System commands
 
541
******************************************************************************/
 
542
 
 
543
void
 
544
tm_server_rep::shell (string s) {
 
545
  system (s);
 
546
}