/*
- lily-guile.cc -- implement assorted guile functions
+ lily-guile.cc -- implement assorted SCM interface functions
source file of the GNU LilyPond music typesetter
*/
-#include <stdio.h>
-#include <stdlib.h>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring> /* strdup, strchr */
+#include <cctype>
#include <libintl.h> // gettext on macos x
-#include <math.h> /* isinf */
-#include <string.h> /* strdup, strchr */
-#include <ctype.h>
-#include "lily-proto.hh"
#include "version.hh"
-
-/* MacOS S fix:
- source-file.hh includes cmath which undefines isinf and isnan
-
- FIXME: #ifdef MACOS_X?
-*/
-inline int my_isinf (Real r) { return isinf (r); }
-inline int my_isnan (Real r) { return isnan (r); }
-
-
-#include "libc-extension.hh"
#include "lily-guile.hh"
+#include "libc-extension.hh"
#include "main.hh"
#include "file-path.hh"
#include "warn.hh"
#include "direction.hh"
#include "offset.hh"
-#include "interval.hh"
#include "pitch.hh"
#include "dimensions.hh"
#include "source-file.hh"
+#include "misc.hh"
// #define TEST_GC
+SCM
+ly_to_symbol (SCM scm)
+{
+ return scm_string_to_symbol (ly_to_string (scm));
+}
+
+SCM
+ly_to_string (SCM scm)
+{
+ return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
+ scm_makfrom0str ("~S"), scm);
+}
+
SCM
ly_last (SCM list)
{
return scm_car (scm_last_pair (list));
}
-
SCM
ly_write2scm (SCM s)
{
return scm_strport_to_string (port);
}
-
SCM
ly_quote_scm (SCM s)
{
}
String
-gulp_file_to_string (String fn)
+gulp_file_to_string (String fn, bool must_exist)
{
String s = global_path.find (fn);
if (s == "")
{
- String e = _f ("can't find file: `%s'", fn);
- e += " ";
- e += _f ("(load path: `%s')", global_path.to_string ());
- error (e);
+ if (must_exist)
+ {
+ String e = _f ("can't find file: `%s'", fn);
+ e += " ";
+ e += _f ("(load path: `%s')", global_path.to_string ());
+ error (e);
+ /* unreachable */
+ }
+ return s;
}
- else if (verbose_global_b)
+
+ if (be_verbose_global)
progress_indication ("[" + s);
int n;
- char * str = gulp_file (s, &n);
- String result (str);
+ char *str = gulp_file (s, &n);
+ String result ((Byte*) str, n);
delete[] str;
- if (verbose_global_b)
+ if (be_verbose_global)
progress_indication ("]");
return result;
}
-LY_DEFINE (ly_gulp_file, "ly:gulp-file",
- 1, 0, 0, (SCM name),
- "Read the file @var{name}, and return its contents in a string. "
- "The file is looked up using the search path.")
-{
- SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
- return scm_makfrom0str (gulp_file_to_string (ly_scm2string (name)).to_str0 ());
-}
extern "C" {
String
ly_scm2string (SCM str)
{
+ assert (scm_is_string (str));
return String ((Byte*)scm_i_string_chars (str),
(int) scm_i_string_length (str));
}
return s;
}
-LY_DEFINE (ly_warn, "ly:warn",
- 1, 0, 1, (SCM str, SCM rest),
- "Scheme callable function to issue the warning @code{msg}. "
- "The message is formatted with @code{format} and @code{rest}.")
-{
- SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
- progress_indication ("\n");
-
- str = scm_simple_format (SCM_BOOL_F, str, rest);
- warning ("lily-guile: " + ly_scm2string (str));
- return SCM_UNSPECIFIED;
-}
-
-LY_DEFINE (ly_programming_error, "ly:programming-error",
- 1, 0, 1, (SCM str, SCM rest),
- "Scheme callable function to issue the warning @code{msg}. "
- "The message is formatted with @code{format} and @code{rest}.")
-{
- SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
- progress_indication ("\n");
-
- str = scm_simple_format (SCM_BOOL_F, str, rest);
- programming_error (ly_scm2string (str));
- return SCM_UNSPECIFIED;
-}
-
-LY_DEFINE (ly_dir_p, "ly:dir?",
- 1, 0, 0, (SCM s),
- "type predicate. A direction is @code{-1}, @code{0} or "
- "@code{1}, where @code{-1} represents "
- "left or down and @code{1} represents right or up.")
-{
- if (scm_is_number (s))
- {
- int i = scm_to_int (s);
- return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
- }
- return SCM_BOOL_F;
-}
-
bool
is_number_pair (SCM p)
{
void
ly_init_ly_module (void *)
{
- for (int i=scm_init_funcs_->size () ; i--;)
+ for (int i = scm_init_funcs_->size () ; i--;)
(scm_init_funcs_->elem (i)) ();
- if (verbose_global_b)
+ if (be_verbose_global)
progress_indication ("\n");
scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
return false;
}
-LY_DEFINE(ly_assoc_get, "ly:assoc-get",
- 2, 1, 0,
- (SCM key, SCM alist, SCM default_value),
- "Return value if KEY in ALIST, else DEFAULT-VALUE (or #f if not specified).")
-{
- SCM handle = scm_assoc (key, alist);
-
- if (default_value == SCM_UNDEFINED)
- default_value = SCM_BOOL_F;
-
- if (scm_is_pair (handle))
- return scm_cdr (handle);
- else
- return default_value;
-}
-
bool
is_axis (SCM s)
{
if (scm_is_number (s))
{
int i = scm_to_int (s);
- return i== 0 || i == 1;
+ return i == 0 || i == 1;
}
return false;
}
scm_to_double (scm_cdr (s)));
}
-LY_DEFINE (ly_number2string, "ly:number->string",
- 1, 0, 0, (SCM s),
- "Convert @var{num} to a string without generating many decimals.")
-{
- SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
-
- char str[400]; // ugh.
-
- if (scm_exact_p (s) == SCM_BOOL_F)
- {
- Real r (scm_to_double (s));
-
- if (my_isinf (r) || my_isnan (r))
- {
- programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
- r = 0.0;
- }
-
- sprintf (str, "%08.4f", r);
- }
- else
- sprintf (str, "%d", scm_to_int (s));
-
- return scm_makfrom0str (str);
-}
-
-
-
-LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
- "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
-{
- char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
-
- return scm_c_eval_string ((char*)vs);
-}
-
-LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
- "Return the unit used for lengths as a string.")
-{
- return scm_makfrom0str (INTERNAL_UNIT);
-}
-
-
-
-LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
- "Return @var{d} is a number. Used to distinguish length "
- "variables from normal numbers.")
-{
- return scm_number_p (d);
-}
-
SCM
ly_deep_copy (SCM src)
{
{
int len = SCM_VECTOR_LENGTH (src);
SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
- for (int i =0 ; i < len ; i++)
+ for (int i = 0 ;i < len ; i++)
{
SCM si = scm_int2num (i);
scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
return src;
}
-
-
+SCM
+ly_chain_assoc_get (SCM key, SCM achain, SCM dfault)
+{
+ if (scm_is_pair (achain))
+ {
+ SCM handle = scm_assoc (key, scm_car (achain));
+ if (scm_is_pair (handle))
+ return scm_cdr (handle);
+ else
+ return ly_chain_assoc (key, scm_cdr (achain));
+ }
+ else
+ return dfault;
+}
SCM
-ly_assoc_chain (SCM key, SCM achain)
+ly_chain_assoc (SCM key, SCM achain)
{
if (scm_is_pair (achain))
{
if (scm_is_pair (handle))
return handle;
else
- return ly_assoc_chain (key, scm_cdr (achain));
+ return ly_chain_assoc (key, scm_cdr (achain));
}
else
return SCM_BOOL_F;
+ " " + _ ("Perhaps you made a typing error?"));
/* Be strict when being anal :) */
- if (internal_type_checking_global_b)
+ if (do_internal_type_checking_global)
abort ();
warning (_ ("Doing assignment anyway."));
{
SCM errport = scm_current_error_port ();
ok = false;
- SCM typefunc = ly_scheme_function ("type-name");
+ SCM typefunc = ly_lily_module_constant ("type-name");
SCM type_name = scm_call_1 (typefunc, type);
SCM p = scm_current_output_port ();
scm_puts ("(", p);
- for (; scm_is_pair (s); s =scm_cdr (s))
+ for (; scm_is_pair (s); s = scm_cdr (s))
{
scm_display (scm_car (s), p);
scm_puts (" ", p);
}
return tab;
}
-
-#if 1
-/*
- Debugging mem leaks:
- */
-LY_DEFINE (ly_protects, "ly:protects",
- 0, 0, 0, (),
- "Return hash of protected objects.")
-{
- return scm_protects;
-}
-#endif
-
-
-#if HAVE_PANGO_FC_FONT_MAP_ADD_DECODER_FIND_FUNC
-
-#include "pangofc-afm-decoder.hh"
-
-LY_DEFINE (ly_pango_add_afm_decoder, "ly:pango-add-afm-decoder",
- 1, 0, 0, (SCM font_family),
- "Add pango afm decoder for FONT-FAMILY.")
-{
- SCM_ASSERT_TYPE (scm_is_string (font_family), font_family, SCM_ARG1,
- __FUNCTION__, "font_family");
- pango_fc_afm_add_decoder (ly_scm2newstr (font_family, 0));
- return SCM_UNSPECIFIED;
-}
-
-#endif
-
-LY_DEFINE (ly_gettext, "ly:gettext",
- 1, 0, 0, (SCM string),
- "Gettext wrapper.")
-{
- SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
- __FUNCTION__, "string");
- return scm_makfrom0str (gettext (scm_i_string_chars (string)));
-}
-