]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-function.cc
* lily/include/lily-guile.hh: rename ly_c_X_p -> ly_is_X
[lilypond.git] / lily / music-function.cc
1 /*
2   music-function.cc -- implement music_function
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "music-function.hh"
10
11 #include "music.hh"
12
13 static scm_t_bits music_function_tag;
14
15 /* Print a textual represenation of the smob to a given port.  */
16 static int
17 print_music_function (SCM b, SCM port, scm_print_state *)
18 {
19   SCM value = SCM_CELL_OBJECT_1 (b);
20
21   scm_puts ("#<Music function ", port);
22   scm_write (value, port);
23   scm_puts (">", port);
24
25   /* Non-zero means success.  */
26   return 1;
27 }
28
29 LY_DEFINE (ly_make_music_function, "ly:make-music-function", 2, 0, 0,
30            (SCM signature, SCM func),
31            "Make a function to process music, to be used for the "
32            "parser. @code{func} is the function, and @code{signature} describes "
33            "Its arguments. @code{signature} is a list containing either "
34            "@code{ly:music?} predicates or other type predicates.")
35 {
36   String str = "";
37   for (SCM s = signature; scm_is_pair (s); s = scm_cdr (s))
38     {
39       if (str != "")
40         str += "-";
41
42       if (scm_car (s) == Music_type_p_proc)
43         str += "music";
44       else if (scm_car (s) == ly_lily_module_constant ("markup?"))
45         str += "markup";
46       else if (ly_is_procedure (scm_car (s)))
47         str += "scm";
48     }
49   if (str == "") str = "noarg";
50   scm_set_object_property_x (func, ly_symbol2scm ("music-function-signature"),
51                              signature);
52
53   scm_set_object_property_x (func, ly_symbol2scm ("music-function-signature-keyword"),
54                              ly_symbol2scm (str.to_str0 ()));
55
56   SCM_RETURN_NEWSMOB (music_function_tag, func);
57 }
58
59 bool
60 is_music_function (SCM music_function)
61 {
62   return (SCM_NIMP (music_function) && SCM_CELL_TYPE (music_function) == music_function_tag);
63 }
64
65 SCM
66 get_music_function_transform (SCM music_function)
67 {
68   if (!is_music_function (music_function))
69     return SCM_UNDEFINED;
70
71   return SCM_CELL_OBJECT_1 (music_function);
72 }
73
74 static void
75 init_music_function (void)
76 {
77   music_function_tag = scm_make_smob_type ("music-function", 0);
78   scm_set_smob_mark (music_function_tag, scm_markcdr);
79   scm_set_smob_print (music_function_tag, print_music_function);
80 }
81
82 ADD_SCM_INIT_FUNC (music_function_tag, init_music_function);