Docs: LM 4.4.3: Clarify overriding vertical positioning of spanners
[lilypond/mpolesky.git] / lily / simple-closure.cc
blobdc4c3f4f7fdffdcc28bcae04413b07e3f6b40b02
1 /*
2 closure.cc -- chained closures.
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9 #include "simple-closure.hh"
11 #include "grob.hh"
13 static scm_t_bits simple_closure_tag;
15 bool
16 is_simple_closure (SCM s)
18 return (SCM_NIMP (s) && SCM_CELL_TYPE (s) == simple_closure_tag);
21 SCM
22 simple_closure_expression (SCM smob)
24 assert (is_simple_closure (smob));
25 return (SCM) SCM_CELL_WORD_1(smob);
28 SCM
29 evaluate_args (SCM delayed_argument, SCM args, bool pure, int start, int end)
31 SCM new_args = SCM_EOL;
32 SCM *tail = &new_args;
33 for (SCM s = args; scm_is_pair (s); s = scm_cdr (s))
35 *tail = scm_cons (evaluate_with_simple_closure (delayed_argument, scm_car (s),
36 pure, start, end),
37 SCM_EOL);
38 if (scm_car (*tail) == SCM_UNSPECIFIED)
39 return SCM_UNSPECIFIED;
40 tail = SCM_CDRLOC (*tail);
43 return new_args;
46 SCM
47 evaluate_with_simple_closure (SCM delayed_argument,
48 SCM expr,
49 bool pure,
50 int start,
51 int end)
53 if (is_simple_closure (expr))
55 SCM inside = simple_closure_expression (expr);
56 SCM args = scm_cons (delayed_argument,
57 evaluate_args (delayed_argument, scm_cdr (inside),
58 pure, start, end));
59 if (scm_cdr (args) == SCM_UNSPECIFIED)
60 return SCM_UNSPECIFIED;
61 if (pure)
62 return call_pure_function (scm_car (inside), args, start, end);
63 return scm_apply_0 (scm_car (inside), args);
65 else if (!scm_is_pair (expr))
66 return expr;
67 else if (scm_car (expr) == ly_symbol2scm ("quote"))
68 return scm_cadr (expr);
69 else if (ly_is_procedure (scm_car (expr)))
71 SCM args = evaluate_args (delayed_argument, scm_cdr (expr), pure, start, end);
72 if (args == SCM_UNSPECIFIED)
73 return SCM_UNSPECIFIED;
74 if (pure)
75 return call_pure_function (scm_car (expr), args, start, end);
76 return scm_apply_0 (scm_car (expr), args);
78 else
79 // ugh. deviation from standard. Should print error?
80 return evaluate_args (delayed_argument, scm_cdr (expr), pure, start, end);
82 assert (false);
83 return SCM_EOL;
86 LY_DEFINE (ly_simple_closure_p, "ly:simple-closure?",
87 1, 0, 0, (SCM clos),
88 "Is @var{clos} a simple closure?")
90 return scm_from_bool (is_simple_closure (clos));
93 LY_DEFINE (ly_make_simple_closure, "ly:make-simple-closure",
94 1, 0, 0, (SCM expr),
95 "Make a simple closure. @var{expr} should be form of"
96 " @code{(@var{func} @var{a1} @var{A2} @dots{})}, and will be"
97 " invoked as @code{(@var{func} @var{delayed-arg} @var{a1}"
98 " @var{a2} @dots{})}.")
100 SCM z;
102 SCM_NEWSMOB (z, simple_closure_tag, expr);
103 return z;
106 LY_DEFINE (ly_eval_simple_closure, "ly:eval-simple-closure",
107 2, 2, 0, (SCM delayed, SCM closure, SCM scm_start, SCM scm_end),
108 "Evaluate a simple @var{closure} with the given @var{delayed}"
109 " argument. If @var{scm-start} and @var{scm-end} are defined,"
110 " evaluate it purely with those start and end points.")
112 bool pure = (scm_is_number (scm_start) && scm_is_number (scm_end));
113 int start = robust_scm2int (scm_start, 0);
114 int end = robust_scm2int (scm_end, 0);
115 SCM expr = simple_closure_expression (closure);
116 return evaluate_with_simple_closure (delayed, expr, pure, start, end);
120 print_simple_closure (SCM s, SCM port, scm_print_state *)
122 scm_puts ("#<simple-closure ", port);
123 scm_display (scm_cdr (s), port);
124 scm_puts (" >", port);
125 return 1;
129 void init_simple_closure ()
131 simple_closure_tag = scm_make_smob_type ("simple-closure", 0);
132 scm_set_smob_mark (simple_closure_tag, scm_markcdr);
133 scm_set_smob_print (simple_closure_tag, print_simple_closure);
138 ADD_SCM_INIT_FUNC (simple_closure, init_simple_closure);