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"
23 ly_str02scm (char const*c)
25 // this all really sucks, guile should take char const* arguments!
26 return gh_str02scm ((char*)c);
30 ly_eval_str (char const*c)
32 // this all really sucks, guile should take char const* arguments!
33 return gh_eval_str ((char*)c);
38 Pass string to scm parser, evaluate one expression.
39 Return result value and #chars read.
41 Thanks to Gary Houston <ghouston@freewire.co.uk>
43 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
46 ly_parse_scm (char const* s, int* n)
48 SCM str = gh_str02scm ((char*)s);
49 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
51 SCM from = scm_ftell (port);
54 SCM answer = SCM_UNSPECIFIED;
56 /* Read expression from port */
57 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
58 answer = scm_eval_x (form);
65 all seems fine, but after parsing
69 read_buf has been advanced to read_pos - 1,
70 so that scm_ftell returns 1, instead of #parsed chars
74 urg: reset read_buf for scm_ftell
75 shouldn't scm_read () do this for us?
77 scm_fill_input (port);
78 SCM to = scm_ftell (port);
79 *n = gh_scm2int (to) - gh_scm2int (from);
81 /* Don't close the port here; if we re-enter this function via a
82 continuation, then the next time we enter it, we'll get an error.
83 It's a string port anyway, so there's no advantage to closing it
86 scm_close_port (port);
93 scm_m_quote doesn't use any env, but needs one for a good signature in GUILE.
95 // apparently env arg is ignored.
101 This internal quote symbol breaks list->string and display,
104 return scm_m_quote (scm_cons2 (SCM_EOL, s, SCM_EOL) ,SCM_EOL);
106 return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
112 ly_symbol2scm(const char *s)
114 return gh_symbol2scm ((char *)s);
118 ly_symbol2string (SCM s)
120 return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
125 ly_set_x (String name, SCM val)
127 return scm_sysintern ((char*)name.ch_C(), val);
133 Read a file, and shove it down GUILE. GUILE also has file read
134 functions, but you can't fiddle with the path of those.
137 read_lily_scm_file (String fn)
139 String s = global_path.find (fn);
142 String e = _f ("Can't find file: `%s'", fn);
144 e += _f ("(load path: `%s')", global_path.str ());
151 Simple_file_storage f(s);
153 ly_eval_str ((char *) f.ch_C());
154 *mlog << "]" << flush;
159 ly_gulp_file (SCM name)
161 String fn (ly_scm2string (name));
162 String s = global_path.find (fn);
165 String e = _f ("Can't find file: `%s'", fn);
167 e += _f ("(load path: `%s')", global_path.str ());
174 Simple_file_storage f(s);
175 SCM result = ly_str02scm (f.ch_C());
181 ly_display_scm (SCM s)
188 ly_scm2string (SCM s)
191 char * p = gh_scm2newstr (s , &len);
200 index_cell (SCM s, Direction d)
203 return (d == LEFT) ? SCM_CAR (s) : SCM_CDR (s);
208 array_to_list (SCM *a , int l)
211 for (int i= l; i--; )
213 list = gh_cons (a[i], list);
221 assert (gh_string_p (str));
222 warning ("lily-guile: " + ly_scm2string (str));
231 int i = gh_scm2int (s);
232 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
241 scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning);
242 scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file);
243 scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p);
246 ADD_SCM_INIT_FUNC(funcs, init_functions);
249 typedef void (*Void_fptr)();
250 Array<Void_fptr> *scm_init_funcs_;
252 void add_scm_init_func (void (*f)())
254 if (!scm_init_funcs_)
255 scm_init_funcs_ = new Array<Void_fptr>;
257 scm_init_funcs_->push (f);
263 for (int i=scm_init_funcs_->size() ; i--;)
264 (scm_init_funcs_->elem (i)) ();
267 unsigned int ly_scm_hash (SCM s)
269 return scm_ihashv (s, ~1u);