]> git.donarmstrong.com Git - lilypond.git/blob - lily/general-scheme.cc
Minor documentation improvements for Scheme functions.
[lilypond.git] / lily / general-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2010 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.hh"
22
23 #include <cstdio>
24 #include <ctype.h>
25 #include <cstring>  /* memset */
26 using namespace std;
27
28 #include "dimensions.hh"
29 #include "file-name.hh"
30 #include "file-path.hh"
31 #include "international.hh"
32 #include "libc-extension.hh"
33 #include "lily-guile.hh"
34 #include "main.hh"
35 #include "misc.hh"
36 #include "program-option.hh"
37 #include "relocate.hh"
38 #include "string-convert.hh"
39 #include "version.hh"
40 #include "warn.hh"
41
42 LY_DEFINE (ly_start_environment, "ly:start-environment",
43            0, 0, 0, (),
44            "Return the environment (a list of strings) that was in"
45            " effect at program start.")
46 {
47   SCM l = SCM_EOL;
48   SCM *tail = &l;
49
50   for (vsize i = 0; i < start_environment_global.size (); i++)
51     {
52       *tail = scm_cons (ly_string2scm (start_environment_global[i]),
53                         SCM_EOL);
54       tail = SCM_CDRLOC(*tail);
55     }
56
57   return l;
58 }
59
60
61 LY_DEFINE (ly_find_file, "ly:find-file",
62            1, 0, 0, (SCM name),
63            "Return the absolute file name of @var{name},"
64            " or @code{#f} if not found.")
65 {
66   LY_ASSERT_TYPE (scm_is_string, name, 1);
67
68   string nm = ly_scm2string (name);
69   string file_name = global_path.find (nm);
70   if (file_name.empty ())
71     return SCM_BOOL_F;
72
73   return ly_string2scm (file_name);
74 }
75
76 /*
77   Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
78   buffering.)
79 */
80 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
81            1, 1, 0, (SCM name, SCM size),
82            "Read the file @var{name}, and return its contents in a string."
83            "  The file is looked up using the search path.")
84 {
85   LY_ASSERT_TYPE (scm_is_string, name, 1);
86   int sz = INT_MAX;
87   if (size != SCM_UNDEFINED)
88     {
89       LY_ASSERT_TYPE (scm_is_number, size, 2);
90       sz = scm_to_int (size);
91     }
92
93   string contents = gulp_file_to_string (ly_scm2string (name), true, sz);
94   return scm_from_locale_stringn (contents.c_str (), contents.length ());
95 }
96
97 LY_DEFINE (ly_error, "ly:error",
98            1, 0, 1, (SCM str, SCM rest),
99            "A Scheme callable function to issue the error @var{str}."
100            "  The error is formatted with @code{format} and @var{rest}.")
101 {
102   LY_ASSERT_TYPE (scm_is_string, str, 1);
103   str = scm_simple_format (SCM_BOOL_F, str, rest);
104   error (ly_scm2string (str));
105   return SCM_UNSPECIFIED;
106 }
107
108 LY_DEFINE (ly_message, "ly:message",
109            1, 0, 1, (SCM str, SCM rest),
110            "A Scheme callable function to issue the message @var{str}."
111            "  The message is formatted with @code{format} and @var{rest}.")
112 {
113   LY_ASSERT_TYPE (scm_is_string, str, 1);
114   str = scm_simple_format (SCM_BOOL_F, str, rest);
115   message (ly_scm2string (str));
116   return SCM_UNSPECIFIED;
117 }
118
119 LY_DEFINE (ly_progress, "ly:progress",
120            1, 0, 1, (SCM str, SCM rest),
121            "A Scheme callable function to print progress @var{str}."
122            "  The message is formatted with @code{format} and @var{rest}.")
123 {
124   LY_ASSERT_TYPE (scm_is_string, str, 1);
125   str = scm_simple_format (SCM_BOOL_F, str, rest);
126   progress_indication (ly_scm2string (str));
127   return SCM_UNSPECIFIED;
128 }
129
130 LY_DEFINE (ly_programming_error, "ly:programming-error",
131            1, 0, 1, (SCM str, SCM rest),
132            "A Scheme callable function to issue the internal warning"
133            "  @var{str}.  The message is formatted with @code{format}"
134            " and @var{rest}.")
135 {
136   LY_ASSERT_TYPE (scm_is_string, str, 1);
137   str = scm_simple_format (SCM_BOOL_F, str, rest);
138
139   if (get_program_option ("warning-as-error"))
140     error (ly_scm2string (str));
141   else
142     programming_error (ly_scm2string (str));
143
144   return SCM_UNSPECIFIED;
145 }
146
147 LY_DEFINE (ly_success, "ly:success",
148            1, 0, 1, (SCM str, SCM rest),
149            "A Scheme callable function to issue a success message @var{str}."
150            "  The message is formatted with @code{format} and @var{rest}.")
151 {
152   LY_ASSERT_TYPE (scm_is_string, str, 1);
153   str = scm_simple_format (SCM_BOOL_F, str, rest);
154   successful (ly_scm2string (str));
155   return SCM_UNSPECIFIED;
156
157 }
158 LY_DEFINE (ly_warning, "ly:warning",
159            1, 0, 1, (SCM str, SCM rest),
160            "A Scheme callable function to issue the warning @var{str}."
161            "  The message is formatted with @code{format} and @var{rest}.")
162 {
163   LY_ASSERT_TYPE (scm_is_string, str, 1);
164   str = scm_simple_format (SCM_BOOL_F, str, rest);
165
166   if (get_program_option ("warning-as-error"))
167     error (ly_scm2string (str));
168   else
169     warning (ly_scm2string (str));
170
171   return SCM_UNSPECIFIED;
172 }
173
174 LY_DEFINE (ly_dir_p, "ly:dir?",
175            1, 0, 0, (SCM s),
176            "Is @var{s} a direction?  Valid directions are @code{-1},"
177            " @code{0}, or@tie{}@code{1}, where @code{-1} represents"
178            " left or down, @code{1}@tie{}represents right or up, and @code{0}"
179            " represents a neutral direction.")
180 {
181   if (scm_is_number (s))
182     {
183       int i = scm_to_int (s);
184       return (i >= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
185     }
186   return SCM_BOOL_F;
187 }
188
189 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
190            2, 2, 0,
191            (SCM key, SCM alist, SCM default_value, SCM strict_checking),
192            "Return value if @var{key} in @var{alist}, else @var{default-value}"
193            " (or @code{#f} if not specified).  If @var{strict-checking} is set"
194            " to @code{#t} and @var{key} is not in @var{alist}, a programming_error"
195            " is output.")
196 {
197   LY_ASSERT_TYPE(ly_cheap_is_list, alist, 2);
198
199   SCM handle = scm_assoc (key, alist);
200   if (scm_is_pair (handle))
201     return scm_cdr (handle);
202
203   if (default_value == SCM_UNDEFINED)
204     default_value = SCM_BOOL_F;
205
206   if (strict_checking == SCM_BOOL_T)
207     {
208       string key_string = ly_scm2string
209                             (scm_object_to_string (key, SCM_UNDEFINED));
210       string default_value_string = ly_scm2string
211                                       (scm_object_to_string (default_value,
212                                                              SCM_UNDEFINED));
213       programming_error ("Cannot find key `" +
214                          key_string +
215                          "' in alist, setting to `" +
216                          default_value_string + "'.");
217     }
218
219   return default_value;
220 }
221
222 LY_DEFINE (ly_string_substitute, "ly:string-substitute",
223            3, 0, 0, (SCM a, SCM b, SCM s),
224            "Replace string@tie{}@var{a} by string@tie{}@var{b} in"
225            " string@tie{}@var{s}.")
226 {
227   LY_ASSERT_TYPE (scm_is_string, s, 1);
228   LY_ASSERT_TYPE (scm_is_string, b, 2);
229   LY_ASSERT_TYPE (scm_is_string, s, 3);
230
231   string ss = ly_scm2string (s);
232   replace_all (&ss, ly_scm2string (a),
233                ly_scm2string (b));
234
235   return ly_string2scm (ss);
236 }
237
238 bool
239 is_not_escape_character (Byte c)
240 {
241   switch (c)
242     {
243     case '-':
244     case '.':
245     case '/':
246     case '0'...'9':
247     case ':':
248     case 'A'...'Z':
249     case '_':
250     case 'a'...'z':
251       return true;
252     }
253
254   return false;
255 }
256
257 LY_DEFINE (ly_string_percent_encode, "ly:string-percent-encode",
258            1, 0, 0, (SCM str),
259            "Encode all characters in string @var{str} with hexadecimal"
260            " percent escape sequences, with the following exceptions:"
261            " characters @code{-}, @code{.}, @code{/}, and @code{_}; and"
262            " characters in ranges @code{0-9}, @code{A-Z}, and @code{a-z}.")
263 {
264   LY_ASSERT_TYPE (scm_is_string, str, 1);
265
266   string orig_str = ly_scm2string (str);
267   string new_str = "";
268
269   vsize i = 0;
270   vsize n = orig_str.size ();
271
272   while (i < n)
273     {
274       Byte cur = orig_str[i];
275
276       if (is_not_escape_character (cur))
277         new_str += cur;
278       else
279         {
280           new_str += '%';
281           new_str += String_convert::bin2hex (cur);
282         }
283
284       i++;
285     }
286
287   return ly_string2scm (new_str);
288 }
289
290 LY_DEFINE (ly_number_2_string, "ly:number->string",
291            1, 0, 0, (SCM s),
292            "Convert @var{s} to a string without generating many decimals.")
293 {
294   LY_ASSERT_TYPE (scm_is_number, s, 1);
295
296   char str[400];                        // ugh.
297
298   if (scm_exact_p (s) == SCM_BOOL_F)
299     {
300       Real r (scm_to_double (s));
301         if (isinf (r) || isnan (r))
302           {
303             programming_error (_ ("infinity or NaN encountered while converting Real number"));
304             programming_error (_ ("setting to zero"));
305
306             r = 0.0;
307           }
308
309       snprintf (str, sizeof (str), "%.4f", r);
310     }
311   else
312     snprintf (str, sizeof (str), "%d", int (scm_to_int (s)));
313
314   return scm_from_locale_string (str);
315 }
316
317 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
318            "Return the current lilypond version as a list, e.g.,"
319            " @code{(1 3 127 uu1)}.")
320 {
321   char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
322
323   return scm_c_eval_string ((char *)vs);
324 }
325
326 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
327            "Return the unit used for lengths as a string.")
328 {
329   return scm_from_locale_string (INTERNAL_UNIT);
330 }
331
332 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
333            "Return @var{d} as a number.  Used to distinguish length"
334            " variables from normal numbers.")
335 {
336   return scm_number_p (d);
337 }
338
339 /*
340   Debugging mem leaks:
341 */
342 LY_DEFINE (ly_protects, "ly:protects",
343            0, 0, 0, (),
344            "Return hash of protected objects.")
345 {
346   return scm_protects;
347 }
348
349 LY_DEFINE (ly_gettext, "ly:gettext",
350            1, 0, 0, (SCM original),
351            "A Scheme wrapper function for @code{gettext}.")
352 {
353   LY_ASSERT_TYPE (scm_is_string, original, 1);
354   return ly_string2scm (_ (ly_scm2string (original).c_str ()));
355 }
356
357 LY_DEFINE (ly_output_formats, "ly:output-formats",
358            0, 0, 0, (),
359            "Formats passed to @option{--format} as a list of strings,"
360            " used for the output.")
361 {
362   vector<string> output_formats = string_split (output_format_global, ',');
363
364   SCM lst = SCM_EOL;
365   int output_formats_count = output_formats.size ();
366   for (int i = 0; i < output_formats_count; i++)
367     lst = scm_cons (ly_string2scm (output_formats[i]), lst);
368
369   return lst;
370 }
371
372 LY_DEFINE (ly_wide_char_2_utf_8, "ly:wide-char->utf-8",
373            1, 0, 0, (SCM wc),
374            "Encode the Unicode codepoint @var{wc}, an integer, as UTF-8.")
375 {
376   char buf[5];
377
378   LY_ASSERT_TYPE (scm_is_integer, wc, 1);
379   unsigned wide_char = (unsigned) scm_to_int (wc);
380   char *p = buf;
381
382   if (wide_char < 0x0080)
383     *p++ = (char)wide_char;
384   else if (wide_char < 0x0800)
385     {
386       *p++ = (char) (((wide_char >> 6)) | 0xC0);
387       *p++ = (char) (((wide_char) & 0x3F) | 0x80);
388     }
389   else if (wide_char < 0x10000)
390     {
391       *p++ = (char) (((wide_char >> 12)) | 0xE0);
392       *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
393       *p++ = (char) (((wide_char) & 0x3F) | 0x80);
394     }
395   else
396     {
397       *p++ = (char) (((wide_char >> 18)) | 0xF0);
398       *p++ = (char) (((wide_char >> 12) & 0x3F) | 0x80);
399       *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
400       *p++ = (char) (((wide_char) & 0x3F) | 0x80);
401     }
402   *p = 0;
403
404   return scm_from_locale_string (buf);
405 }
406
407 LY_DEFINE (ly_effective_prefix, "ly:effective-prefix",
408            0, 0, 0, (),
409            "Return effective prefix.")
410 {
411   return ly_string2scm (lilypond_datadir);
412 }
413
414 LY_DEFINE (ly_chain_assoc_get, "ly:chain-assoc-get",
415            2, 2, 0, (SCM key, SCM achain, SCM default_value, SCM strict_checking),
416            "Return value for @var{key} from a list of alists @var{achain}."
417            "  If no entry is found, return @var{default-value} or @code{#f} if"
418            " @var{default-value} is not specified.  With @var{strict-checking}"
419            " set to @code{#t}, a programming_error is output in such cases.")
420 {
421   if (scm_is_pair (achain))
422     {
423       SCM handle = scm_assoc (key, scm_car (achain));
424       if (scm_is_pair (handle))
425         return scm_cdr (handle);
426       else
427         return ly_chain_assoc_get (key, scm_cdr (achain), default_value);
428     }
429
430   if (strict_checking == SCM_BOOL_T)
431     {
432       string key_string = ly_scm2string
433                             (scm_object_to_string (key, SCM_UNDEFINED));
434       string default_value_string = ly_scm2string
435                                       (scm_object_to_string (default_value,
436                                                              SCM_UNDEFINED));
437       programming_error ("Cannot find key `" +
438                          key_string +
439                          "' in achain, setting to `" +
440                          default_value_string + "'.");
441     }
442
443   return default_value == SCM_UNDEFINED ? SCM_BOOL_F : default_value;
444 }
445
446
447 LY_DEFINE (ly_stderr_redirect, "ly:stderr-redirect",
448            1, 1, 0, (SCM file_name, SCM mode),
449            "Redirect stderr to @var{file-name}, opened with @var{mode}.")
450 {
451   LY_ASSERT_TYPE (scm_is_string, file_name, 1);
452
453   string m = "w";
454   FILE *stderrfile;
455   if (mode != SCM_UNDEFINED && scm_string_p (mode))
456     m = ly_scm2string (mode);
457   /* dup2 and (fileno (current-error-port)) do not work with mingw'c
458      gcc -mwindows.  */
459   fflush (stderr);
460   stderrfile = freopen (ly_scm2string (file_name).c_str (), m.c_str (), stderr);
461   return SCM_UNSPECIFIED;
462 }
463
464 static SCM
465 accumulate_symbol (void * /* closure */,
466                    SCM key,
467                    SCM /* val */,
468                    SCM result)
469 {
470   return scm_cons (key, result);
471 }
472
473 LY_DEFINE (ly_hash_table_keys, "ly:hash-table-keys",
474           1,0,0, (SCM tab),
475           "Return a list of keys in @var{tab}.")
476 {
477   return scm_internal_hash_fold ((scm_t_hash_fold_fn) &accumulate_symbol,
478                                  NULL, SCM_EOL, tab);
479 }
480
481 LY_DEFINE (ly_camel_case_2_lisp_identifier, "ly:camel-case->lisp-identifier",
482            1, 0, 0, (SCM name_sym),
483            "Convert @code{FooBar_Bla} to @code{foo-bar-bla} style symbol.")
484 {
485   LY_ASSERT_TYPE (ly_is_symbol, name_sym, 1);
486
487   /*
488     TODO: should use strings instead?
489   */
490
491   const string in = ly_symbol2string (name_sym);
492   string result = camel_case_to_lisp_identifier (in);
493
494   return ly_symbol2scm (result.c_str ());
495 }
496
497 LY_DEFINE (ly_expand_environment, "ly:expand-environment",
498            1, 0, 0, (SCM str),
499            "Expand @code{$VAR} and @code{$@{VAR@}} in @var{str}.")
500 {
501   LY_ASSERT_TYPE (scm_is_string, str, 1);
502
503   return ly_string2scm (expand_environment_variables (ly_scm2string (str)));
504 }
505
506
507 LY_DEFINE (ly_truncate_list_x, "ly:truncate-list!",
508            2, 0, 0, (SCM lst, SCM i),
509            "Take at most the first @var{i} of list @var{lst}.")
510 {
511   LY_ASSERT_TYPE (scm_is_integer, i, 1);
512
513   int k = scm_to_int (i);
514   if (k == 0)
515     lst = SCM_EOL;
516   else
517     {
518       SCM s = lst;
519       k--;
520       for (; scm_is_pair (s) && k--; s = scm_cdr (s))
521         ;
522
523       if (scm_is_pair (s))
524         scm_set_cdr_x (s, SCM_EOL);
525     }
526   return lst;
527 }
528
529 string
530 format_single_argument (SCM arg, int precision, bool escape = false)
531 {
532   if (scm_is_integer (arg) && scm_exact_p (arg) == SCM_BOOL_T)
533     return (String_convert::int_string (scm_to_int (arg)));
534   else if (scm_is_number (arg))
535     {
536       Real val = scm_to_double (arg);
537
538       if (isnan (val) || isinf (val))
539         {
540           warning (_ ("Found infinity or nan in output. Substituting 0.0"));
541           return ("0.0");
542           if (strict_infinity_checking)
543             abort ();
544         }
545       else
546         return (String_convert::form_string ("%.*lf", precision, val));
547     }
548   else if (scm_is_string (arg))
549     {
550       string s = ly_scm2string (arg);
551       if (escape)
552         {
553           // Escape backslashes and double quotes, wrap it in double quotes
554           replace_all (&s, "\\", "\\\\");
555           replace_all (&s, "\"", "\\\"");
556           // don't replace percents, since the png backend uses %d as escape sequence
557           // replace_all (&s, "%", "\\%");
558           replace_all (&s, "$", "\\$");
559           s = "\"" + s + "\"";
560         }
561       return s;
562     }
563   else if (scm_is_symbol (arg))
564     return (ly_symbol2string (arg));
565   else
566     {
567       ly_progress (scm_from_locale_string ("Unsupported SCM value for format: ~a"),
568                    scm_list_1 (arg));
569     }
570
571
572   return "";
573 }
574
575 LY_DEFINE (ly_format, "ly:format",
576            1, 0, 1, (SCM str, SCM rest),
577            "LilyPond specific format, supporting @code{~a} and @code{~[0-9]f}."
578            "  Basic support for @code{~s} is also provided.")
579 {
580   LY_ASSERT_TYPE (scm_is_string, str, 1);
581
582   string format = ly_scm2string (str);
583   vector<string> results;
584
585   vsize i = 0;
586   while (i < format.size ())
587     {
588       vsize tilde = format.find ('~', i);
589
590       results.push_back (format.substr (i, (tilde-i)));
591
592       if (tilde == NPOS)
593         break ;
594
595       tilde ++;
596
597       char spec = format.at (tilde ++);
598       if (spec == '~')
599         results.push_back ("~");
600       else
601         {
602           if (!scm_is_pair (rest))
603             {
604               programming_error (string (__FUNCTION__)
605                                  + ": not enough arguments for format.");
606               return ly_string2scm ("");
607             }
608
609           SCM arg = scm_car (rest);
610           rest = scm_cdr (rest);
611
612           int precision = 8;
613
614           if (spec == '$')
615             precision = 2;
616           else if (isdigit (spec))
617             {
618               precision = spec - '0';
619               spec = format.at (tilde ++);
620             }
621
622           if (spec == 'a' || spec == 'A' || spec == 'f' || spec == '$')
623             results.push_back (format_single_argument (arg, precision));
624           else if (spec == 's' || spec == 'S')
625             results.push_back (format_single_argument (arg, precision, true));
626           else if (spec == 'l')
627             {
628               SCM s = arg;
629               for (; scm_is_pair (s); s = scm_cdr (s))
630                 {
631                   results.push_back (format_single_argument (scm_car (s), precision));
632                   if (scm_cdr (s) != SCM_EOL)
633                     results.push_back (" ");
634                 }
635
636               if (s != SCM_EOL)
637                 results.push_back (format_single_argument (s, precision));
638
639             }
640         }
641
642       i = tilde;
643     }
644
645   if (scm_is_pair (rest))
646     programming_error (string (__FUNCTION__)
647                        + ": too many arguments");
648
649   vsize len = 0;
650   for (vsize i = 0; i < results.size (); i++)
651     len += results[i].size ();
652
653   char *result = (char*) scm_malloc (len + 1);
654   char *ptr = result;
655   for (vsize i = 0; i < results.size (); i++)
656     {
657       strncpy (ptr, results[i].c_str (), results[i].size ());
658       ptr += results[i].size ();
659     }
660   *ptr = '\0';
661
662   return scm_take_locale_stringn (result, len);
663 }