2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--1999 Jan Nieuwenhuizen <janneke@gnu.org>
8 Han-Wen Nienhuys <hanwen@cs.uu.nl>
15 #include "libc-extension.hh"
16 #include "lily-guile.hh"
18 #include "simple-file-storage.hh"
19 #include "file-path.hh"
21 #include "direction.hh"
24 ly_str02scm (char const*c)
26 // this all really sucks, guile should take char const* arguments!
27 return gh_str02scm ((char*)c);
31 ly_eval_str (char const*c)
33 // this all really sucks, guile should take char const* arguments!
34 return gh_eval_str ((char*)c);
39 Pass string to scm parser, evaluate one expression.
40 Return result value and #chars read.
42 Thanks to Gary Houston <ghouston@freewire.co.uk>
44 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
47 ly_parse_scm (char const* s, int* n)
49 SCM str = gh_str02scm ((char*)s);
50 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
52 SCM from = scm_ftell (port);
55 SCM answer = SCM_UNSPECIFIED;
57 /* Read expression from port */
58 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
59 answer = scm_eval_x (form);
66 all seems fine, but after parsing
70 read_buf has been advanced to read_pos - 1,
71 so that scm_ftell returns 1, instead of #parsed chars
75 urg: reset read_buf for scm_ftell
76 shouldn't scm_read () do this for us?
78 scm_fill_input (port);
79 SCM to = scm_ftell (port);
80 *n = gh_scm2int (to) - gh_scm2int (from);
82 /* Don't close the port here; if we re-enter this function via a
83 continuation, then the next time we enter it, we'll get an error.
84 It's a string port anyway, so there's no advantage to closing it
87 scm_close_port (port);
94 scm_m_quote doesn't use any env, but needs one for a good signature in GUILE.
99 return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
104 ly_symbol2scm(const char *s)
106 return gh_symbol2scm ((char *)s);
110 ly_symbol2string (SCM s)
112 return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
117 Read a file, and shove it down GUILE. GUILE also has file read
118 functions, but you can't fiddle with the path of those.
121 read_lily_scm_file (String fn)
123 String s = global_path.find (fn);
126 String e = _f ("Can't find file: `%s'", fn);
128 e += _f ("(load path: `%s')", global_path.str ());
135 Simple_file_storage f(s);
137 ly_eval_str ((char *) f.ch_C());
138 *mlog << "]" << flush;
143 ly_gulp_file (SCM name)
145 String fn (ly_scm2string (name));
146 String s = global_path.find (fn);
149 String e = _f ("Can't find file: `%s'", fn);
151 e += _f ("(load path: `%s')", global_path.str ());
158 Simple_file_storage f(s);
159 SCM result = ly_str02scm (f.ch_C());
165 ly_display_scm (SCM s)
172 ly_scm2string (SCM s)
175 char * p = gh_scm2newstr (s , &len);
184 index_cell (SCM s, Direction d)
187 return (d == LEFT) ? gh_car (s) : gh_cdr (s);
191 index_set_cell (SCM s, Direction d, SCM v)
202 array_to_list (SCM *a , int l)
205 for (int i= l; i--; )
207 list = gh_cons (a[i], list);
216 assert (gh_string_p (str));
217 warning ("lily-guile: " + ly_scm2string (str));
226 int i = gh_scm2int (s);
227 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
236 scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning);
237 scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file);
238 scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p);
241 ADD_SCM_INIT_FUNC(funcs, init_functions);
244 typedef void (*Void_fptr)();
245 Array<Void_fptr> *scm_init_funcs_;
247 void add_scm_init_func (void (*f)())
249 if (!scm_init_funcs_)
250 scm_init_funcs_ = new Array<Void_fptr>;
252 scm_init_funcs_->push (f);
258 for (int i=scm_init_funcs_->size() ; i--;)
259 (scm_init_funcs_->elem (i)) ();
262 unsigned int ly_scm_hash (SCM s)
264 return scm_ihashv (s, ~1u);
274 int i = gh_scm2int (s);
275 return i>= -1 && i <= 1;
283 return (Direction) gh_scm2int (s);