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