-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ily
258 lines (240 loc) · 7.99 KB
/
util.ily
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
%! File: util.ily
%! This file contains utility functions in Scheme that can be used to simplify some
%! advanced tasks in LilyPond.
\version "2.24.0"
%!--------------------------------------------------------------------------------------
%! Group: LilyPond Functions
%! LilyPond functions can be used directly within your LilyPond source code.
%!--------------------------------------------------------------------------------------
%! Function: escalate-warnings
%! --- Prototype
%! \escalate-warnings
%! ---
%! Makes LilyPond treat all (user-issued) warnings as errors.
escalate-warnings = #(define-void-function (parser location) ()
(set! ly:warning ly:error))
%!--------------------------------------------------------------------------------------
%! Group: Scheme Functions
%! Scheme functions must be called in a Scheme environment (and aren't really all that
%! useful elsewhere).
%!--------------------------------------------------------------------------------------
%! Function: defined?
%! --- Prototype
%! (define (defined? symbol) ...)
%! ---
%! The defined? predicate tells you wether a specific symbol was previously
%! defined. This may not work for standard functions but it works for custom
%! definitions.
%!
%! Parameters:
%! symbol - A quoted symbol.
#(define (defined? symbol) (not (null? (ly:parser-lookup symbol))))
%! Function: get-option
%! --- Prototype
%! (define (get-option symbol default) ...)
%! ---
%! The test-option predicate tests wether the specified symbol was defined and
%! set to a value that evaluates to #t (a value other that #f). This can be used
%! to test whether package options have been specified. If a (package) option is
%! set to a non-boolean value this predicate will return that value.
%!
%! Parameters:
%! symbol - A quoted symbol (the name of the option).
%! default - A default value that is returned if no definition for symbol is found.
#(define (get-option symbol default) (if (defined? symbol)
(eval symbol (current-module))
default))
%! Function: any?
%! --- Prototype
%! (define (any? object) ...)
%! ---
%! A type predicate that is always true.
%!
%! Parameters:
%! object - The object to test. This parameter is ignored.
#(define (any? object) #t)
%! Function: andmap
%! --- Prototype
%! (define (andmap f xs) ...)
%! ---
%! A predicate that returns true iff f returns true for all elements in xs. If xs is
%! empty this predicates evaluates to #t.
%!
%! Parameters:
%! f - A predicate function to apply to all xs.
%! xs - A list of values.
%!
%! See Also:
%! <ormap>
#(define (andmap f xs)
(cond ((null? xs) #t)
((f (car xs)) (andmap f (cdr xs)))
(else #f)))
%! Function: ormap
%! --- Prototype
%! (define (ormap f xs) ...)
%! ---
%! A predicate that returns true iff f returns true for any of the elements in xs. If
%! xs is empty #f is returned.
%!
%! Parameters:
%! f - A predicate function to apply to all xs.
%! xs - A list of values.
%!
%! See Also:
%! <andmap>
#(define (ormap f xs)
(cond ((null? xs) #f)
((f (car xs)) #t)
(else (ormap f (cdr xs)))))
%! Function: custom-script-tweaks
%! --- Prototype
%! (define ((custom-script-tweaks ls) ...) ...)
%! ---
%! Enables custom tweaks for single grobs.
%!
%! Usage:
%! > \override Script.before-line-breaking = #(custom-script-tweaks alist)
%!
%! Parameters:
%! ls - An alist of tweaks. Keys are strings and values are lists of pairs.
#(define ((custom-script-tweaks ls) grob)
(let* ((type (ly:prob-property (ly:grob-property grob 'cause)
'articulation-type))
(tweaks (assoc-ref ls type)))
(if tweaks
(for-each (lambda (x) (ly:grob-set-property! grob (car x) (cdr x))) tweaks)
'())))
%!--------------------------------------------------------------------------------------
%! Group: Markup Commands
%! Markup commands can be used inside of a \markup { ... } block. They provide
%! additional features such as styling options.
%!--------------------------------------------------------------------------------------
%! Function: warn
%! --- Prototype
%! \warn <text>
%! ---
%! A markup command that can be used to emit a warning. This may for example be useful
%! if you want to mark a function as deprecated and emit a warning.
%!
%! Parameters:
%! text - The warning text that should be emitted.
%!
%! Returns:
%! Empty markup.
#(define-markup-command (warn layout props text) (string?)
(ly:warning text)
empty-markup)
%! Function: if-true
%! --- Prototype
%! \if-true <predicate> <markp>
%! ---
%! This markup command conditionally outputs markup based on a boolean value.
%!
%! Parameters:
%! predicate - A boolean value.
%! markp - Any markup.
%!
%! Returns:
%! If predicate is #t markp is returned, otherwise an empty stencil.
%!
%! See Also:
%! <if-false>, <if-else>
#(define-markup-command (if-true layout props predicate markp) (any? markup?)
(if predicate (interpret-markup layout props markp) empty-stencil))
%! Function: if-false
%! --- Prototype
%! \if-false <predicate> <markp>
%! ---
%! This markup command conditionally outputs markup based on a boolean value.
%!
%! Parameters:
%! predicate - A boolean value.
%! markp - Any markup.
%!
%! Returns:
%! If predicate is #f markp is returned, otherwise an empty stencil.
%!
%! See Also:
%! <if-true>, <if-else>
#(define-markup-command (if-false layout props predicate markp) (any? markup?)
(if predicate empty-stencil (interpret-markup layout props markp)))
%! Function: if-else
%! --- Prototype
%! \if-else <predicate> <tmarkp> <fmarkp>
%! ---
%! Conditionally outputs one of two markups depending on a boolean value.
%!
%! Parameters:
%! predicate - A boolean value.
%! tmarkp - The markup returned if predicate is #t.
%! fmarkp - The markup returned if predicate is #f.
%!
%! See Also:
%! <if-true>, <if-false>
#(define-markup-command (if-else layout props predicate tmarkp fmarkp) (any? markup? markup?)
(if predicate
(interpret-markup layout props tmarkp)
(interpret-markup layout props fmarkp)))
%! Function: when-property
%! --- Prototype
%! \when-property <symbol> <markp>
%! ---
%! The \when-property markup command allows you to conditionally output markup.
%! If symbol exists markp is returned. Otherwise an empty markup block is
%! returned.
%!
%! Parameters:
%! symbol - A symbol.
%! marp - Some markup.
%!
%! Returns:
%! markp if symbol exists in the current context, an empty stencil otherwise.
%!
%! See Also:
%! <when-not-property>, <when-some-properties>
#(define-markup-command (when-property layout props symbol markp) (any? markup?)
(if (chain-assoc-get symbol props)
(interpret-markup layout props markp)
empty-stencil))
%! Function: when-not-property
%! --- Prototype
%! \when-not-property <symbol> <markp>
%! ---
%! Behaves like \when-property but returns the markup block if symbol does not
%! exist. If the symbol exists an empty markup block is returned.
%!
%! Parameters:
%! symbol - A symbol.
%! marp - Some markup.
%!
%! Returns:
%! markp if symbol does not exist in the current context, an empty stencil otherwise.
%!
%! See Also:
%! <when-property>, <when-some-properties>
#(define-markup-command (when-not-property layout props symbol markp) (symbol? markup?)
(if (chain-assoc-get symbol props)
empty-stencil
(interpret-markup layout props markp)))
%! Function: when-some-properties
%! --- Prototype
%! \when-some-properties <symbols> <markp>
%! ---
%! Behaves like \when-property but accepts a list of properties instead of a
%! single property.
%!
%! Parameters:
%! symbols - A list of symbol.
%! marp - Some markup.
%!
%! Returns:
%! markp if any of the symbols exist in the current context, an empty stencil
%! otherwise.
%!
%! See Also:
%! <when-property>, <when-not-properties>
#(define-markup-command (when-some-properties layout props symbols markp) (list? markup?)
(if (ormap (lambda (symbol) (chain-assoc-get symbol props)) symbols)
(interpret-markup layout props markp)
empty-stencil))