lilypond-1.3.19
[lilypond.git] / lily / lily-guile.cc
blob2edd23ac88ae3e337c48b1b465aeed74f7562781
1 /*
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>
9 */
12 #include <stdio.h>
13 #include <stdlib.h>
15 #include "libc-extension.hh"
16 #include "lily-guile.hh"
17 #include "main.hh"
18 #include "simple-file-storage.hh"
19 #include "file-path.hh"
20 #include "debug.hh"
21 #include "direction.hh"
23 SCM
24 ly_str02scm (char const*c)
26 // this all really sucks, guile should take char const* arguments!
27 return gh_str02scm ((char*)c);
30 SCM
31 ly_eval_str (String s)
33 // this all really sucks, guile should take char const* arguments!
34 return gh_eval_str ((char*)s.ch_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
46 SCM
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,
51 "scm_eval_0str");
52 SCM from = scm_ftell (port);
54 SCM form;
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);
62 After parsing
64 (begin (foo 1 2))
66 all seems fine, but after parsing
68 (foo 1 2)
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
85 early.
87 scm_close_port (port);
90 return answer;
94 scm_m_quote doesn't use any env, but needs one for a good signature in GUILE.
96 SCM
97 ly_quote_scm (SCM s)
99 return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
104 ly_symbol2scm(const char *s)
106 return gh_symbol2scm ((char *)s);
109 String
110 ly_symbol2string (SCM s)
112 assert (gh_symbol_p (s));
113 return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
118 Read a file, and shove it down GUILE. GUILE also has file read
119 functions, but you can't fiddle with the path of those.
121 void
122 read_lily_scm_file (String fn)
124 String s = global_path.find (fn);
125 if (s == "")
127 String e = _f ("Can't find file: `%s'", fn);
128 e += " ";
129 e += _f ("(load path: `%s')", global_path.str ());
130 error (e);
132 else
133 progress_indication ("[" + s);
136 Simple_file_storage f(s);
138 ly_eval_str ((char *) f.ch_C());
139 progress_indication ("]");
144 ly_gulp_file (SCM name)
146 String fn (ly_scm2string (name));
147 String s = global_path.find (fn);
148 if (s == "")
150 String e = _f ("Can't find file: `%s'", fn);
151 e += " ";
152 e += _f ("(load path: `%s')", global_path.str ());
153 error (e);
155 else
156 progress_indication ("[" + s );
159 Simple_file_storage f(s);
160 SCM result = ly_str02scm (f.ch_C());
161 progress_indication ("]");
162 return result;
165 void
166 ly_display_scm (SCM s)
168 gh_display (s);
169 gh_newline ();
172 String
173 ly_scm2string (SCM s)
175 assert (gh_string_p (s));
176 int len;
177 char * p = gh_scm2newstr (s , &len);
179 String r (p);
181 free (p);
182 return r;
186 index_cell (SCM s, Direction d)
188 assert (d);
189 return (d == LEFT) ? gh_car (s) : gh_cdr (s);
193 index_set_cell (SCM s, Direction d, SCM v)
195 if (d == LEFT)
196 gh_set_car_x (s, v);
197 else if (d == RIGHT)
198 gh_set_cdr_x (s, v);
199 return s;
203 ly_warning (SCM str)
205 assert (gh_string_p (str));
206 warning ("lily-guile: " + ly_scm2string (str));
207 return SCM_BOOL_T;
211 ly_isdir_p (SCM s)
213 if (gh_number_p (s))
215 int i = gh_scm2int (s);
216 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
218 return SCM_BOOL_F;
222 void
223 init_functions ()
225 scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning);
226 scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file);
227 scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p);
230 ADD_SCM_INIT_FUNC(funcs, init_functions);
233 typedef void (*Void_fptr)();
234 Array<Void_fptr> *scm_init_funcs_;
236 void add_scm_init_func (void (*f)())
238 if (!scm_init_funcs_)
239 scm_init_funcs_ = new Array<Void_fptr>;
241 scm_init_funcs_->push (f);
244 void
245 init_lily_guile ()
247 for (int i=scm_init_funcs_->size() ; i--;)
248 (scm_init_funcs_->elem (i)) ();
251 unsigned int ly_scm_hash (SCM s)
253 return scm_ihashv (s, ~1u);
258 bool
259 isdir_b (SCM s)
261 if (gh_number_p (s))
263 int i = gh_scm2int (s);
264 return i>= -1 && i <= 1;
266 return false;
269 Direction
270 to_dir (SCM s)
272 return (Direction) gh_scm2int (s);
277 to_scm (int i)
279 return gh_int2scm (i);
282 void
283 scm_to (SCM s, int* i)
285 // urg
286 *i = gh_number_p (s) ? gh_scm2int (s) : 0;
290 to_scm (Real r)
292 return gh_double2scm (r);
295 void
296 scm_to (SCM s, Real* r)
298 // urg
299 *r = gh_number_p (s) ? gh_scm2double (s) : 0;
302 bool
303 to_boolean (SCM s)
305 return gh_boolean_p (s) && gh_scm2bool (s);
309 Appendable list L: the cdr contains the list, the car the last cons
310 in the list.
315 appendable_list ()
317 SCM s = gh_cons (SCM_EOL, SCM_EOL);
318 gh_set_car_x (s, s);
320 return s;
323 void
324 appendable_list_append (SCM l, SCM elt)
326 SCM newcons = gh_cons (elt, SCM_EOL);
328 gh_set_cdr_x (gh_car (l), newcons);
329 gh_set_car_x (l, newcons);