Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / simple-closure.cc
blob7c46c0198b728fbbdd83c7cd226f43885def37e7
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "simple-closure.hh"
22 #include "grob.hh"
24 static scm_t_bits simple_closure_tag;
26 bool
27 is_simple_closure (SCM s)
29 return (SCM_NIMP (s) && SCM_CELL_TYPE (s) == simple_closure_tag);
32 SCM
33 simple_closure_expression (SCM smob)
35 assert (is_simple_closure (smob));
36 return (SCM) SCM_CELL_WORD_1(smob);
39 SCM
40 evaluate_args (SCM delayed_argument, SCM args, bool pure, int start, int end)
42 SCM new_args = SCM_EOL;
43 SCM *tail = &new_args;
44 for (SCM s = args; scm_is_pair (s); s = scm_cdr (s))
46 *tail = scm_cons (evaluate_with_simple_closure (delayed_argument, scm_car (s),
47 pure, start, end),
48 SCM_EOL);
49 if (scm_car (*tail) == SCM_UNSPECIFIED)
50 return SCM_UNSPECIFIED;
51 tail = SCM_CDRLOC (*tail);
54 return new_args;
57 SCM
58 evaluate_with_simple_closure (SCM delayed_argument,
59 SCM expr,
60 bool pure,
61 int start,
62 int end)
64 if (is_simple_closure (expr))
66 SCM inside = simple_closure_expression (expr);
67 SCM args = scm_cons (delayed_argument,
68 evaluate_args (delayed_argument, scm_cdr (inside),
69 pure, start, end));
70 if (scm_cdr (args) == SCM_UNSPECIFIED)
71 return SCM_UNSPECIFIED;
72 if (pure)
73 return call_pure_function (scm_car (inside), args, start, end);
74 return scm_apply_0 (scm_car (inside), args);
76 else if (!scm_is_pair (expr))
77 return expr;
78 else if (scm_car (expr) == ly_symbol2scm ("quote"))
79 return scm_cadr (expr);
80 else if (ly_is_procedure (scm_car (expr)))
82 SCM args = evaluate_args (delayed_argument, scm_cdr (expr), pure, start, end);
83 if (args == SCM_UNSPECIFIED)
84 return SCM_UNSPECIFIED;
85 if (pure)
86 return call_pure_function (scm_car (expr), args, start, end);
87 return scm_apply_0 (scm_car (expr), args);
89 else
90 // ugh. deviation from standard. Should print error?
91 return evaluate_args (delayed_argument, scm_cdr (expr), pure, start, end);
93 assert (false);
94 return SCM_EOL;
97 LY_DEFINE (ly_simple_closure_p, "ly:simple-closure?",
98 1, 0, 0, (SCM clos),
99 "Is @var{clos} a simple closure?")
101 return scm_from_bool (is_simple_closure (clos));
104 LY_DEFINE (ly_make_simple_closure, "ly:make-simple-closure",
105 1, 0, 0, (SCM expr),
106 "Make a simple closure. @var{expr} should be form of"
107 " @code{(@var{func} @var{a1} @var{a2} @dots{})}, and will be"
108 " invoked as @code{(@var{func} @var{delayed-arg} @var{a1}"
109 " @var{a2} @dots{})}.")
111 SCM z;
113 SCM_NEWSMOB (z, simple_closure_tag, expr);
114 return z;
117 LY_DEFINE (ly_eval_simple_closure, "ly:eval-simple-closure",
118 2, 2, 0, (SCM delayed, SCM closure, SCM scm_start, SCM scm_end),
119 "Evaluate a simple @var{closure} with the given @var{delayed}"
120 " argument. If @var{scm-start} and @var{scm-end} are defined,"
121 " evaluate it purely with those start and end points.")
123 bool pure = (scm_is_number (scm_start) && scm_is_number (scm_end));
124 int start = robust_scm2int (scm_start, 0);
125 int end = robust_scm2int (scm_end, 0);
126 SCM expr = simple_closure_expression (closure);
127 return evaluate_with_simple_closure (delayed, expr, pure, start, end);
131 print_simple_closure (SCM s, SCM port, scm_print_state *)
133 scm_puts ("#<simple-closure ", port);
134 scm_display (scm_cdr (s), port);
135 scm_puts (" >", port);
136 return 1;
140 void init_simple_closure ()
142 simple_closure_tag = scm_make_smob_type ("simple-closure", 0);
143 scm_set_smob_mark (simple_closure_tag, scm_markcdr);
144 scm_set_smob_print (simple_closure_tag, print_simple_closure);
149 ADD_SCM_INIT_FUNC (simple_closure, init_simple_closure);