2 @node Interfaces for programmers
3 @appendix Interfaces for programmers
8 * Programmer interfaces for input ::
9 * Markup programmer interface::
10 * Contexts for programmers::
13 @node Programmer interfaces for input
14 @appendixsec Programmer interfaces for input
17 * Input variables and Scheme::
18 * Internal music representation::
19 * Extending music syntax::
20 * Manipulating music expressions::
23 @node Input variables and Scheme
24 @appendixsubsec Input variables and Scheme
27 The input format supports the notion of variable: in the following
28 example, a music expression is assigned to a variable with the name
31 traLaLa = \notes @{ c'4 d'4 @}
36 There is also a form of scoping: in the following example, the
37 @code{\paper} block also contains a @code{traLaLa} variable, which is
38 independent of the outer @code{\traLaLa}.
40 traLaLa = \notes @{ c'4 d'4 @}
41 \paper @{ traLaLa = 1.0 @}
44 In effect, each input file is a scope, and all @code{\header},
45 @code{\midi} and @code{\paper} blocks are scopes nested inside that
48 Both variables and scoping are implemented in the GUILE module system.
49 An anonymous Scheme module is attached to each scope. An assignment of
52 traLaLa = \notes @{ c'4 d'4 @}
56 is internally converted to a Scheme definition
58 (define traLaLa @var{Scheme value of ``@code{\notes ... }''})
61 This means that input variables and Scheme variables may be freely
62 mixed. In the following example, a music fragment is stored in the
63 variable @code{traLaLa}, and duplicated using Scheme. The result is
64 imported in a @code{\score} by means of a second variable
67 traLaLa = \notes @{ c'4 d'4 @}
69 #(define newLa (map ly:music-deep-copy
70 (list traLaLa traLaLa)))
72 (make-sequential-music newLa))
77 In the above example, music expressions can be `exported' from the
78 input to the Scheme interpreter. The opposite is also possible. By
79 wrapping a Scheme value in the function @code{ly:export}, a Scheme
80 value is interpreted as if it were entered in LilyPond syntax: instead
81 of defining @code{\twice}, the example above could also have been
85 \score @{ #(ly:export (make-sequential-music newLa)) @}
89 @node Internal music representation
90 @appendixsubsec Internal music representation
92 When a music expression is parsed, it is converted into a set of
93 Scheme music objects. The defining property of a music object is that
94 it takes up time. Time is a rational number that measures the length
95 of a piece of music, in whole notes.
97 A music object has three kinds of types:
100 music name: Each music expression has a name, for example, a note
101 leads to a @internalsref{NoteEvent}, and @code{\simultaneous} leads to
102 a @internalsref{SimultaneousMusic}. A list of all expressions
103 available is in the internals manual, under @internalsref{Music
107 `type' or interface: Each music name has several `types' or interface,
108 for example, a note is an @code{event}, but it is also a @code{note-event},
109 a @code{rhythmic-event} and a @code{melodic-event}.
111 All classes of music are listed in the internals manual, under
112 @internalsref{Music classes}.
114 C++ object: Each music object is represented by a C++ object. For technical
115 reasons, different music objects may be represented by different C++
116 object types. For example, a note is @code{Event} object, while
117 @code{\grace} creates a @code{Grace_music} object.
119 We expect that distinctions between different C++ types will disappear
123 The actual information of a music expression is stored in properties.
124 For example, a @internalsref{NoteEvent} has @code{pitch} and
125 @code{duration} properties that store the pitch and duration of that
126 note. A list of all properties available is in the internals manual,
127 under @internalsref{Music properties}.
129 A compound music expression is a music object that contains other
130 music objects in its properties. A list of objects can be stored in
131 the @code{elements} property of a music object, or a single `child'
132 music object in the @code{element} object. For example,
133 @internalsref{SequentialMusic} has its children in @code{elements},
134 and @internalsref{GraceMusic} has its single argument in
135 @code{element}. The body of a repeat is in @code{element} property of
136 @internalsref{RepeatedMusic}, and the alternatives in @code{elements}.
141 @node Extending music syntax
142 @appendixsubsec Extending music syntax
144 The syntax of composite music expressions, like
145 @code{\repeat}, @code{\transpose} and @code{\context}
146 follows the general form of
149 \@code{keyword} @var{non-music-arguments} @var{music-arguments}
152 Such syntax can also be defined as user code. To do this, it is
153 necessary to create a @emph{music function}. This is a specially marked
154 Scheme function. For example, the music function @code{\applymusic} applies
155 a user-defined function to a music expression. Its syntax is
158 \applymusic #@var{func} @var{music}
161 A music function is created with @code{ly:make-music-function},
164 (ly:make-music-function
167 @code{\applymusic} takes a Scheme function and a Music expression as
168 argument. This is encoded in its first argument,
171 (list procedure? ly:music?)
174 The function itself takes another argument, an Input location
175 object. That object is used to provide error messages with file names
176 and line numbers. The definition is the second argument of
177 @code{ly:make-music-function}. The body is function simply calls the
181 (lambda (where func music)
185 The above Scheme code only defines the functionality. The tag
186 @code{\applymusic} is selected by defining
189 apply = #(ly:make-music-function
190 (list procedure? ly:music?)
191 (lambda (where func music)
195 Examples of the use of @code{\applymusic} are in the next section.
198 @node Manipulating music expressions
199 @appendixsubsec Manipulating music expressions
201 Music objects and their properties can be accessed and manipulated
202 directly, through the @code{\applymusic} mechanism.
203 The syntax for @code{\applymusic} is
205 \applymusic #@var{func} @var{music}
209 This means that the scheme function @var{func} is called with
210 @var{music} as its argument. The return value of @var{func} is the
211 result of the entire expression. @var{func} may read and write music
212 properties using the functions @code{ly:music-property} and
213 @code{ly:music-set-property!}.
215 An example is a function that reverses the order of elements in
217 @lilypond[verbatim,raggedright]
218 #(define (rev-music-1 m)
219 (ly:music-set-property! m 'elements (reverse
220 (ly:music-property m 'elements)))
222 \score { \notes \applymusic #rev-music-1 { c4 d4 } }
225 The use of such a function is very limited. The effect of this
226 function is void when applied to an argument which is does not have
227 multiple children. The following function application has no effect:
230 \applymusic #rev-music-1 \grace @{ c4 d4 @}
234 In this case, @code{\grace} is stored as @internalsref{GraceMusic}, which has no
235 @code{elements}, only a single @code{element}. Every generally
236 applicable function for @code{\applymusic} must -- like music expressions
237 themselves -- be recursive.
239 The following example is such a recursive function: It first extracts
240 the @code{elements} of an expression, reverses them and puts them
241 back. Then it recurses, both on @code{elements} and @code{element}
244 #(define (reverse-music music)
245 (let* ((elements (ly:music-property music 'elements))
246 (child (ly:music-property music 'element))
247 (reversed (reverse elements)))
250 (ly:music-set-property! music 'elements reversed)
253 (if (ly:music? child) (reverse-music child))
254 (map reverse-music reversed)
259 A slightly more elaborate example is in
260 @inputfileref{input/test,reverse-music.ly}.
262 Some of the input syntax is also implemented as recursive music
263 functions. For example, the syntax for polyphony
269 is actually implemented as a recursive function that replaces the
270 above by the internal equivalent of
272 << \context Voice = "1" @{ \voiceOne a @}
273 \context Voice = "2" @{ \voiceTwo b @} >>
276 Other applications of @code{\applymusic} are writing out repeats
277 automatically (@inputfileref{input/test,unfold-all-repeats.ly}),
278 saving keystrokes (@inputfileref{input/test,music-box.ly}) and
280 LilyPond input to other formats (@inputfileref{input/test,to-xml.ly})
284 @file{scm/music-functions.scm}, @file{scm/music-types.scm},
285 @inputfileref{input/test,add-staccato.ly},
286 @inputfileref{input/test,unfold-all-repeats.ly}, and
287 @inputfileref{input/test,music-box.ly}.
291 @node Markup programmer interface
292 @appendixsec Markup programmer interface
296 * Markup construction in scheme::
297 * Markup command definition::
300 @node Markup construction in scheme
301 @appendixsubsec Markup construction in scheme
303 @cindex defining markup commands
305 The @code{markup} macro builds markup expressions in Scheme while
306 providing a LilyPond-like syntax. For example,
308 (markup #:column (#:line (#:bold #:italic "hello" #:raise 0.4 "world")
309 #:bigger #:line ("foo" "bar" "baz")))
315 \markup \column < @{ \bold \italic "hello" \raise #0.4 "world" @}
316 \bigger @{ foo bar baz @} >
320 This example exposes the main translation rules between regular
321 LilyPond markup syntax and scheme markup syntax, which are summed up
323 @multitable @columnfractions .5 .5
324 @item @b{LilyPond} @tab @b{Scheme}
325 @item @code{\command} @tab @code{#:command}
326 @item @code{\variable} @tab @code{variable}
327 @item @code{@{ ... @}} @tab @code{#:line ( ... )}
328 @item @code{\center-align < ... >} @tab @code{#:center ( ... )}
329 @item @code{string} @tab @code{"string"}
330 @item @code{#scheme-arg} @tab @code{scheme-arg}
333 Besides, the whole scheme language is accessible inside the
334 @code{markup} macro: thus, one may use function calls inside
335 @code{markup} in order to manipulate character strings for
336 instance. This proves useful when defining new markup commands (see
337 @ref{Markup command definition}).
341 One can not feed the @code{#:line} (resp @code{#:center},
342 @code{#:column}) command with a variable or the result of a function
345 (markup #:line (fun-that-returns-markups))
347 is illegal. One should use the @code{make-line-markup} (resp
348 @code{make-center-markup}, @code{make-column-markup}) function
351 (markup (make-line-markup (fun-that-returns-markups)))
354 @node Markup command definition
355 @appendixsubsec Markup command definition
357 New markup commands can be defined
358 with the @code{def-markup-command} scheme macro.
360 (def-markup-command (@var{command-name} @var{paper} @var{props} @var{arg1} @var{arg2} ...)
361 (@var{arg1-type?} @var{arg2-type?} ...)
365 The arguments signify
369 @var{i}th command argument
371 a type predicate for the i@var{th} argument
373 the `paper' definition
375 a list of alists, containing all active properties.
378 As a simple example, we show how to add a @code{\smallcaps} command,
379 which selects @TeX{}'s small caps font. Normally, we could select the
380 small caps font as follows:
383 \markup { \override #'(font-shape . caps) Text-in-caps }
386 This selects the caps font by setting the @code{font-shape} property to
387 @code{#'caps} for interpreting @code{Text-in-caps}.
389 To make the above available as @code{\smallcaps} command, we have to
390 define a function using @code{def-markup-command}. The command should
391 take a single argument, of markup type. Therefore, the start of the
392 definition should read
394 (def-markup-command (smallcaps paper props argument) (markup?)
399 What follows is the content of the command: we should interpret
400 the @code{argument} as a markup, i.e.
403 (interpret-markup paper @dots{} argument)
407 This interpretation should add @code{'(font-shape . caps)} to the active
408 properties, so we substitute the following for the @dots{} in the
412 (cons (list '(font-shape . caps) ) props)
416 The variable @code{props} is a list of alists, and we prepend to it by
417 consing a list with the extra setting.
420 Suppose that we are typesetting a recitative in an opera, and
421 we would like to define a command that will show character names in a
422 custom manner. Names should be printed with small caps and translated a
423 bit to the left and top. We will define a @code{\character} command
424 that takes into account the needed translation, and uses the newly
425 defined @code{\smallcaps} command:
428 #(def-markup-command (character paper props name) (string?)
429 "Print the character name in small caps, translated to the left and
430 top. Syntax: \\character #\"name\""
431 (interpret-markup paper props
432 (markup "" #:translate (cons -4 2) #:smallcaps name)))
435 There is one complication that needs explanation: texts above and below
436 the staff are moved vertically to be at a certain distance (the
437 @code{padding} property) from the staff and the notes. To make sure
438 that this mechanism does not annihilate the vertical effect of our
439 @code{#:translate}, we add an empty string (@code{""}) before the
440 translated text. Now the @code{""} will be put above the notes, and the
441 @code{name} is moved in relation to that empty string. The net effect is
442 that the text is moved to the upper left.
444 The final result is as follows:
448 c''^\markup \character #"Cleopatra"
449 e'^\markup \character #"Giulio Cesare"
454 @lilypond[raggedright]
455 #(def-markup-command (smallcaps paper props str) (string?)
456 "Print the string argument in small caps. Syntax: \\smallcaps #\"string\""
457 (interpret-markup paper props
460 (if (= (string-length s) 0)
462 (markup #:large (string-upcase (substring s 0 1))
463 #:translate (cons -0.6 0)
464 #:tiny (string-upcase (substring s 1)))))
465 (string-split str #\Space)))))
467 #(def-markup-command (character paper props name) (string?)
468 "Print the character name in small caps, translated to the left and
469 top. Syntax: \\character #\"name\""
470 (interpret-markup paper props
471 (markup "" #:translate (cons -4 0) #:smallcaps name)))
475 c''^\markup \character #"Cleopatra"
476 e'^\markup \character #"Giulio Cesare"
481 We have used the @code{caps} font shape, but suppose that our font
482 that does not have a small-caps variant. In that case, we have to fake
483 the small caps font, by setting a string in upcase, with the first
484 letter a little larger:
487 #(def-markup-command (smallcaps paper props str) (string?)
488 "Print the string argument in small caps."
489 (interpret-markup paper props
492 (if (= (string-length s) 0)
494 (markup #:large (string-upcase (substring s 0 1))
495 #:translate (cons -0.6 0)
496 #:tiny (string-upcase (substring s 1)))))
497 (string-split str #\Space)))))
500 The @code{smallcaps} command first splits its string argument into
501 tokens separated by spaces (@code{(string-split str #\Space)}); for
502 each token, a markup is built with the first letter made large and
503 upcased (@code{#:large (string-upcase (substring s 0 1))}), and a
504 second markup built with the following letters made tiny and upcased
505 (@code{#:tiny (string-upcase (substring s 1))}). As LilyPond
506 introduces a space between markups on a line, the second markup is
507 translated to the left (@code{#:translate (cons -0.6 0) ...}). Then,
508 the markups built for each token are put in a line by
509 @code{(make-line-markup ...)}. Finally, the resulting markup is passed
510 to the @code{interpret-markup} function, with the @code{paper} and
511 @code{props} arguments.
515 @node Contexts for programmers
516 @appendixsec Contexts for programmers
520 * Context evaluation::
521 * Running a function on all layout objects::
524 @node Context evaluation
525 @appendixsubsec Context evaluation
527 Contexts can be modified during interpretation with Scheme code. The
530 \applycontext @var{function}
533 @var{function} should be a Scheme function taking a single argument,
534 being the context to apply it to. The following code will print the
535 current bar number on the standard output during the compile:
540 (format #t "\nWe were called in barnumber ~a.\n"
541 (ly:context-property x 'currentBarNumber)))
546 @node Running a function on all layout objects
547 @appendixsubsec Running a function on all layout objects
549 The most versatile way of tuning an object is @code{\applyoutput}. Its
552 \applyoutput @var{proc}
556 where @var{proc} is a Scheme function, taking three arguments.
558 When interpreted, the function @var{proc} is called for every layout
559 object found in the context, with the following arguments:
561 @item the layout object itself,
562 @item the context where the layout object was created, and
563 @item the context where @code{\applyoutput} is processed.
567 In addition, the cause of the layout object, i.e. the music
568 expression or object that was responsible for creating it, is in the
569 object property @code{cause}. For example, for a note head, this is a
570 @internalsref{NoteHead} event, and for a @internalsref{Stem} object,
571 this is a @internalsref{NoteHead} object.
573 Here is a function to use for @code{\applyoutput}; it blanks
574 note-heads on the center-line:
577 (define (blanker grob grob-origin context)
578 (if (and (memq (ly:grob-property grob 'interfaces)
580 (eq? (ly:grob-property grob 'staff-position) 0))
582 (set! (ly:grob-property grob 'transparent) #t)))