2
/******************************************************************************
4
* DESCRIPTION: Glue for linking TeXmacs commands to guile
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
******************************************************************************/
13
#include "Glue/glue.hpp"
15
#include "connect.hpp"
16
#include "convert.hpp"
18
#include "sys_utils.hpp"
19
#include "tex_files.hpp"
20
#include "analyze.hpp"
21
#include "tm_layout.hpp"
22
#include "converter.hpp"
24
#include "Freetype/free_type.hpp"
25
#include "Freetype/tt_file.hpp"
33
typedef SCM (*FN)(...);
38
extern void initialize_glue_basic ();
39
extern void initialize_glue_editor ();
40
extern void initialize_glue_server ();
56
texmacs_version (string which) {
57
if (which == "tgz") return TEXMACS_TGZ;
58
if (which == "rpm") return TEXMACS_RPM;
59
if (which == "stgz") return TEXMACS_STGZ;
60
if (which == "srpm") return TEXMACS_SRPM;
61
return TEXMACS_VERSION;
64
/******************************************************************************
65
* Direct access to scheme objects
66
******************************************************************************/
68
#define SCM_ASSERT_OBJECT(flag,arg,rout)
71
object_to_scm (object obj) {
76
scm_to_object (SCM obj) {
80
/******************************************************************************
82
******************************************************************************/
84
#define SCM_ASSERT_BOOL(flag,arg,rout) \
85
SCM_ASSERT (scm_is_bool (flag), flag, arg, rout)
88
bool_to_scm (bool flag) {
89
return scm_bool2scm (flag);
93
scm_to_bool (SCM flag) {
94
return scm_scm2bool (flag);
97
/******************************************************************************
99
******************************************************************************/
101
#define SCM_ASSERT_INT(i,arg,rout) \
102
SCM_ASSERT (SCM_INUMP (i), i, arg, rout);
106
return scm_long2scm ((long) i);
111
return (int) scm_scm2long (i);
114
/******************************************************************************
115
* Floating point numbers
116
******************************************************************************/
118
#define SCM_ASSERT_DOUBLE(i,arg,rout) \
119
SCM_ASSERT (SCM_REALP (i), i, arg, rout);
122
double_to_scm (double i) {
123
return scm_double2scm (i);
127
scm_to_double (SCM i) {
128
return scm_scm2double (i);
131
/******************************************************************************
133
******************************************************************************/
135
#define SCM_MYSTRINGP(s) (SCM_NIMP (s) && SCM_STRINGP (s))
137
#define SCM_ASSERT_STRING(s,arg,rout) \
138
SCM_ASSERT (SCM_MYSTRINGP (s), s, arg, rout)
141
string_to_scm (string s) {
142
char* _s= as_charp (s);
143
SCM r= scm_str2scm (_s, N(s));
149
scm_to_string (SCM s) {
150
guile_str_size_t len_r;
151
char* _r= scm_scm2str (s, &len_r);
152
string r (_r, len_r);
157
/******************************************************************************
159
******************************************************************************/
161
#define SCM_ASSERT_SYMBOL(s,arg,rout) \
162
SCM_ASSERT (SCM_NFALSEP (scm_symbol_p (s)), s, arg, rout)
165
symbol_to_scm (string s) {
166
char* _s= as_charp (s);
167
SCM r= scm_symbol2scm (_s);
173
scm_to_symbol (SCM s) {
174
guile_str_size_t len_r;
175
char* _r= scm_scm2symbol (s, &len_r);
176
string r (_r, len_r);
181
/******************************************************************************
183
******************************************************************************/
185
#define SCM_ASSERT_TREE_LABEL(p,arg,rout) SCM_ASSERT_SYMBOL(p,arg,rout)
188
tree_label_to_scm (tree_label l) {
189
string s= as_string (l);
190
return symbol_to_scm (s);
194
scm_to_tree_label (SCM p) {
195
string s= scm_to_symbol (p);
196
return make_tree_label (s);
199
/******************************************************************************
201
******************************************************************************/
203
static long tree_tag;
205
#define SCM_TREEP(t) \
206
(SCM_NIMP (t) && (((long) SCM_CAR (t)) == tree_tag))
207
#define SCM_ASSERT_TREE(t,arg,rout) \
208
SCM_ASSERT (SCM_TREEP (t), t, arg, rout)
211
scm_is_tree (SCM t) {
212
return SCM_TREEP (t);
216
tree_to_scm (tree t) {
218
SCM_NEWCELL (tree_smob);
219
SCM_SETCDR (tree_smob, (SCM) ((void*) (new tree (t))));
220
SCM_SETCAR (tree_smob, tree_tag);
225
scm_to_tree (SCM tree_smob) {
226
return *((tree*) SCM_CDR (tree_smob));
230
mark_tree (SCM tree_smob) {
236
free_tree (SCM tree_smob) {
237
tree *ptr = (tree *) SCM_CDR (tree_smob);
239
return sizeof (tree); // should be replaced by total size of the tree
243
print_tree (SCM tree_smob, SCM port, scm_print_state *pstate) {
245
tree t= scm_to_tree (tree_smob);
246
string s= "<tree " * tree_to_texmacs (t) * ">";
247
scm_display (string_to_scm (s), port);
252
cmp_tree (SCM t1, SCM t2) {
253
return scm_bool2scm (scm_to_tree (t1) == scm_to_tree (t2));
257
coerce_string_tree (string s) {
262
coerce_tree_string (tree t) {
263
return as_string (t);
267
tree_ref (tree t, int i) {
272
tree_set (tree t, int i, tree u) {
276
/******************************************************************************
278
******************************************************************************/
280
#define SCM_ASSERT_SCHEME_TREE(p,arg,rout)
283
scheme_tree_to_scm (scheme_tree t) {
286
if (s == "#t") return SCM_BOOL_T;
287
if (s == "#f") return SCM_BOOL_F;
288
if (is_int (s)) return int_to_scm (as_int (s));
289
if ((N(s)>=2) && (s[0]=='\42') && (s[N(s)-1]=='\42'))
290
return string_to_scm (s (1, N(s)-1));
291
return symbol_to_scm (s);
296
for (i=N(t)-1; i>=0; i--)
297
p= scm_cons (scheme_tree_to_scm (t[i]), p);
303
scm_to_scheme_tree (SCM p) {
304
if (scm_is_list (p)) {
306
while (!scm_is_null (p)) {
307
t << scm_to_scheme_tree (SCM_CAR (p));
312
if (gh_symbol_p (p)) return scm_to_symbol (p);
313
if (scm_is_string (p)) return "\"" * scm_to_string (p) * "\"";
314
if (SCM_INUMP (p)) return as_string (scm_to_int (p));
315
if (scm_is_bool (p)) return (scm_to_bool (p)? string ("#t"): string ("#f"));
316
if (scm_is_tree (p)) return tree_to_scheme_tree (scm_to_tree (p));
320
/******************************************************************************
322
******************************************************************************/
324
#define texmacs_tree tree
325
#define SCM_ASSERT_TEXMACS_TREE SCM_ASSERT_TREE
326
#define texmacs_tree_to_scm tree_to_scm
327
#define scm_to_texmacs_tree scm_to_tree
329
/******************************************************************************
331
******************************************************************************/
334
scm_is_path (SCM p) {
335
if (scm_is_null (p)) return true;
336
else return SCM_INUMP (SCM_CAR (p)) && scm_is_path (SCM_CDR (p));
339
#define SCM_ASSERT_PATH(p,arg,rout) \
340
SCM_ASSERT (scm_is_path (p), p, arg, rout)
343
path_to_scm (path p) {
344
if (nil (p)) return SCM_NULL;
345
else return scm_cons (int_to_scm (p->item), path_to_scm (p->next));
349
scm_to_path (SCM p) {
350
if (scm_is_null (p)) return path ();
351
else return path (scm_to_int (SCM_CAR (p)), scm_to_path (SCM_CDR (p)));
354
/******************************************************************************
356
******************************************************************************/
358
static long display_tag;
360
#define scm_is_display(t) \
361
(SCM_NIMP (t) && (((long) SCM_CAR (t)) == display_tag))
362
#define SCM_ASSERT_DISPLAY(t,arg,rout) \
363
SCM_ASSERT (scm_is_display (t), t, arg, rout)
366
display_to_scm (display t) {
368
SCM_NEWCELL (display_smob);
369
SCM_SETCDR (display_smob, (SCM) ((void*) (new display (t))));
370
SCM_SETCAR (display_smob, display_tag);
375
scm_to_display (SCM display_smob) {
376
return *((display*) SCM_CDR (display_smob));
380
mark_display (SCM display_smob) {
386
free_display (SCM display_smob) {
387
display *ptr = (display *) SCM_CDR (display_smob);
389
return sizeof (display); // should be replaced by total size of the display
393
print_display (SCM display_smob, SCM port, scm_print_state *pstate) {
394
(void) display_smob; (void) pstate;
395
string s= "<display>";
396
scm_display (string_to_scm (s), port);
401
cmp_display (SCM t1, SCM t2) {
402
return scm_bool2scm (scm_to_display (t1) == scm_to_display (t2));
405
/******************************************************************************
407
******************************************************************************/
409
static long widget_tag;
411
#define SCM_WIDGETP(t) \
412
(SCM_NIMP (t) && (((long) SCM_CAR (t)) == widget_tag))
413
#define SCM_ASSERT_WIDGET(t,arg,rout) \
414
SCM_ASSERT (scm_is_widget (t), t, arg, rout)
417
scm_is_widget (SCM t) {
418
return SCM_WIDGETP (t);
422
widget_to_scm (widget t) {
424
SCM_NEWCELL (widget_smob);
425
SCM_SETCDR (widget_smob, (SCM) ((void*) (new widget (t))));
426
SCM_SETCAR (widget_smob, widget_tag);
431
scm_to_widget (SCM widget_smob) {
432
return *((widget*) SCM_CDR (widget_smob));
436
mark_widget (SCM widget_smob) {
442
free_widget (SCM widget_smob) {
443
widget *ptr = (widget *) SCM_CDR (widget_smob);
445
return sizeof (widget); // should be replaced by total size of the widget
449
print_widget (SCM widget_smob, SCM port, scm_print_state *pstate) {
450
(void) widget_smob; (void) pstate;
451
string s= "<widget>";
452
scm_display (string_to_scm (s), port);
457
cmp_widget (SCM t1, SCM t2) {
458
return scm_bool2scm (scm_to_widget (t1) == scm_to_widget (t2));
461
/******************************************************************************
463
******************************************************************************/
465
static long make_widget_tag;
467
#define SCM_ASSERT_MAKE_WIDGET(t,arg,rout) \
468
SCM_ASSERT (scm_is_make_widget (t), t, arg, rout)
470
#define scm_is_make_widget(t) \
471
(SCM_NIMP (t) && (((long) SCM_CAR (t)) == make_widget_tag))
474
make_widget_to_scm (make_widget t) {
475
SCM make_widget_smob;
476
SCM_NEWCELL (make_widget_smob);
477
SCM_SETCDR (make_widget_smob, (SCM) ((void*) (new make_widget (t))));
478
SCM_SETCAR (make_widget_smob, make_widget_tag);
479
return make_widget_smob;
483
scm_to_make_widget (SCM make_widget_smob) {
484
return *((make_widget*) SCM_CDR (make_widget_smob));
488
mark_make_widget (SCM make_widget_smob) {
489
(void) make_widget_smob;
494
free_make_widget (SCM make_widget_smob) {
495
make_widget *ptr = (make_widget *) SCM_CDR (make_widget_smob);
497
// should be replaced by total size of the widget factory
498
return sizeof (make_widget);
502
print_make_widget (SCM make_widget_smob, SCM port, scm_print_state *pstate) {
503
(void) make_widget_smob; (void) pstate;
504
string s= "<make-widget>";
505
scm_display (string_to_scm (s), port);
510
cmp_make_widget (SCM t1, SCM t2) {
511
return scm_bool2scm (scm_to_make_widget (t1) == scm_to_make_widget (t2));
514
/******************************************************************************
516
******************************************************************************/
518
static long command_tag;
520
#define scm_is_command(t) \
521
(SCM_NIMP (t) && (((long) SCM_CAR (t)) == command_tag))
522
#define SCM_ASSERT_COMMAND(t,arg,rout) \
523
SCM_ASSERT (scm_is_command (t), t, arg, rout)
526
command_to_scm (command t) {
528
SCM_NEWCELL (command_smob);
529
SCM_SETCDR (command_smob, (SCM) ((void*) (new command (t))));
530
SCM_SETCAR (command_smob, command_tag);
535
scm_to_command (SCM command_smob) {
536
return *((command*) SCM_CDR (command_smob));
540
mark_command (SCM command_smob) {
546
free_command (SCM command_smob) {
547
command *ptr = (command *) SCM_CDR (command_smob);
549
return sizeof (command); // should be replaced by total size of the command
553
print_command (SCM command_smob, SCM port, scm_print_state *pstate) {
554
(void) command_smob; (void) pstate;
555
string s= "<command>";
556
scm_display (string_to_scm (s), port);
561
cmp_command (SCM t1, SCM t2) {
562
return scm_bool2scm (scm_to_command (t1) == scm_to_command (t2));
565
/******************************************************************************
567
******************************************************************************/
571
#define SCM_URLP(u) \
572
(SCM_NIMP (u) && (((long) SCM_CAR (u)) == url_tag))
573
#define SCM_ASSERT_URL(u,arg,rout) \
574
SCM_ASSERT (scm_is_url (u) || SCM_MYSTRINGP (u), u, arg, rout)
584
SCM_NEWCELL (url_smob);
585
SCM_SETCDR (url_smob, (SCM) ((void*) (new url (u))));
586
SCM_SETCAR (url_smob, url_tag);
591
scm_to_url (SCM url_smob) {
592
if (scm_is_string (url_smob))
593
return scm_to_string (url_smob);
594
return *((url*) SCM_CDR (url_smob));
598
mark_url (SCM url_smob) {
604
free_url (SCM url_smob) {
605
url *ptr = (url *) SCM_CDR (url_smob);
607
return sizeof (url); // should be replaced by total size of the url
611
print_url (SCM url_smob, SCM port, scm_print_state *pstate) {
613
url u= scm_to_url (url_smob);
614
string s= "<url " * as_string (u) * ">";
615
scm_display (string_to_scm (s), port);
620
cmp_url (SCM u1, SCM u2) {
621
return scm_bool2scm (scm_to_url (u1) == scm_to_url (u2));
624
url url_concat (url u1, url u2) { return u1 * u2; }
625
url url_or (url u1, url u2) { return u1 | u2; }
626
void string_save (string s, url u) { (void) save_string (u, s); }
627
string string_load (url u) { string s; (void) load_string (u, s); return s; }
628
url url_ref (url u, int i) { return u[i]; }
630
/******************************************************************************
631
* Several array types
632
******************************************************************************/
634
typedef array<string> array_string;
635
typedef array<tree> array_tree;
636
typedef array<widget> array_widget;
639
scm_is_array_string (SCM p) {
640
if (scm_is_null (p)) return true;
641
else return SCM_MYSTRINGP (SCM_CAR (p)) && scm_is_array_string (SCM_CDR (p));
644
#define SCM_ASSERT_ARRAY_STRING(p,arg,rout) \
645
SCM_ASSERT (scm_is_array_string (p), p, arg, rout)
648
array_string_to_scm (array<string> a) {
651
for (i=n-1; i>=0; i--) p= scm_cons (string_to_scm (a[i]), p);
655
/* static */ array<string>
656
scm_to_array_string (SCM p) {
658
while (!scm_is_null (p)) {
659
a << scm_to_string (SCM_CAR (p));
666
scm_is_array_tree (SCM p) {
667
if (scm_is_null (p)) return true;
668
else return SCM_TREEP (SCM_CAR (p)) && scm_is_array_tree (SCM_CDR (p));
671
#define SCM_ASSERT_ARRAY_TREE(p,arg,rout) \
672
SCM_ASSERT (scm_is_array_tree (p), p, arg, rout)
675
array_tree_to_scm (array<tree> a) {
678
for (i=n-1; i>=0; i--) p= scm_cons (tree_to_scm (a[i]), p);
682
/* static */ array<tree>
683
scm_to_array_tree (SCM p) {
685
while (!scm_is_null (p)) {
686
a << scm_to_tree (SCM_CAR (p));
693
scm_is_array_widget (SCM p) {
694
if (scm_is_null (p)) return true;
695
else return scm_is_widget (SCM_CAR (p)) && scm_is_array_widget (SCM_CDR (p));
698
#define SCM_ASSERT_ARRAY_WIDGET(p,arg,rout) \
699
SCM_ASSERT (scm_is_array_widget (p), p, arg, rout)
702
array_widget_to_scm (array<widget> a) {
705
for (i=n-1; i>=0; i--) p= scm_cons (widget_to_scm (a[i]), p);
709
/* static */ array<widget>
710
scm_to_array_widget (SCM p) {
712
while (!scm_is_null (p)) {
713
a << scm_to_widget (SCM_CAR (p));
719
/******************************************************************************
721
******************************************************************************/
727
tree_tag= scm_make_smob_type ("tree", 0);
728
scm_set_smob_mark (tree_tag, mark_tree);
729
scm_set_smob_free (tree_tag, free_tree);
730
scm_set_smob_print (tree_tag, print_tree);
731
scm_set_smob_equalp (tree_tag, cmp_tree);
732
display_tag= scm_make_smob_type ("display", 0);
733
scm_set_smob_mark (display_tag, mark_display);
734
scm_set_smob_free (display_tag, free_display);
735
scm_set_smob_print (display_tag, print_display);
736
scm_set_smob_equalp (display_tag, cmp_display);
737
widget_tag= scm_make_smob_type ("widget", 0);
738
scm_set_smob_mark (widget_tag, mark_widget);
739
scm_set_smob_free (widget_tag, free_widget);
740
scm_set_smob_print (widget_tag, print_widget);
741
scm_set_smob_equalp (widget_tag, cmp_widget);
742
make_widget_tag= scm_make_smob_type ("make-widget", 0);
743
scm_set_smob_mark (make_widget_tag, mark_make_widget);
744
scm_set_smob_free (make_widget_tag, free_make_widget);
745
scm_set_smob_print (make_widget_tag, print_make_widget);
746
scm_set_smob_equalp (make_widget_tag, cmp_make_widget);
747
command_tag= scm_make_smob_type ("command", 0);
748
scm_set_smob_mark (command_tag, mark_command);
749
scm_set_smob_free (command_tag, free_command);
750
scm_set_smob_print (command_tag, print_command);
751
scm_set_smob_equalp (command_tag, cmp_command);
752
url_tag= scm_make_smob_type ("url", 0);
753
scm_set_smob_mark (url_tag, mark_url);
754
scm_set_smob_free (url_tag, free_url);
755
scm_set_smob_print (url_tag, print_url);
756
scm_set_smob_equalp (url_tag, cmp_url);
757
initialize_glue_basic ();
758
initialize_glue_editor ();
759
initialize_glue_server ();
764
scm_smobfuns tree_smob_funcs = {
765
mark_tree, free_tree, print_tree, cmp_tree
768
scm_smobfuns display_smob_funcs = {
769
mark_display, free_display, print_display, cmp_display
772
scm_smobfuns widget_smob_funcs = {
773
mark_widget, free_widget, print_widget, cmp_widget
776
scm_smobfuns make_widget_smob_funcs = {
777
mark_make_widget, free_make_widget, print_make_widget, cmp_make_widget
780
scm_smobfuns command_smob_funcs = {
781
mark_command, free_command, print_command, cmp_command
784
scm_smobfuns url_smob_funcs = {
785
mark_url, free_url, print_url, cmp_url
790
tree_tag= scm_newsmob (&tree_smob_funcs);
791
display_tag= scm_newsmob (&display_smob_funcs);
792
widget_tag= scm_newsmob (&widget_smob_funcs);
793
make_widget_tag= scm_newsmob (&make_widget_smob_funcs);
794
command_tag= scm_newsmob (&command_smob_funcs);
795
url_tag= scm_newsmob (&url_smob_funcs);
796
initialize_glue_basic ();
797
initialize_glue_editor ();
798
initialize_glue_server ();