]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/programming-interface.itely
Whole bunch of minor fixes from mailist.
[lilypond.git] / Documentation / user / programming-interface.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @node Interfaces for programmers
3 @chapter Interfaces for programmers
4
5
6
7 @menu
8 * Programmer interfaces for input ::  
9 * Markup programmer interface::  
10 * Contexts for programmers::    
11 @end menu
12
13 @node Programmer interfaces for input 
14 @section Programmer interfaces for input 
15
16 @menu
17 * Input variables and Scheme::  
18 * Internal music representation::  
19 * Extending music syntax::      
20 * Manipulating music expressions::  
21 * Displaying music expressions::  
22 * Using LilyPond syntax inside Scheme::  
23 @end menu
24
25 @node Input variables and Scheme
26 @subsection Input variables and Scheme
27
28
29 The input format supports the notion of variables: in the following
30 example, a music expression is assigned to a variable with the name
31 @code{traLaLa}.
32 @example
33 traLaLa = @{ c'4 d'4 @}
34 @end example
35
36 @noindent
37
38 There is also a form of scoping: in the following example, the
39 @code{\layout} block also contains a @code{traLaLa} variable, which is
40 independent of the outer @code{\traLaLa}.
41 @example
42 traLaLa = @{ c'4 d'4 @}
43 \layout @{ traLaLa = 1.0 @}
44 @end example
45 @c
46 In effect, each input file is a scope, and all @code{\header},
47 @code{\midi}, and @code{\layout} blocks are scopes nested inside that
48 toplevel scope.
49
50 Both variables and scoping are implemented in the GUILE module system.
51 An anonymous Scheme module is attached to each scope.  An assignment of
52 the form
53 @example
54 traLaLa = @{ c'4 d'4 @}
55 @end example
56
57 @noindent
58 is internally converted to a Scheme definition
59 @example
60 (define traLaLa @var{Scheme value of ``@code{... }''})
61 @end example
62
63 This means that input variables and Scheme variables may be freely
64 mixed.  In the following example, a music fragment is stored in the
65 variable @code{traLaLa}, and duplicated using Scheme.  The result is
66 imported in a @code{\score} block by means of a second variable
67 @code{twice}:
68 @example
69 traLaLa = @{ c'4 d'4 @}
70
71 #(define newLa (map ly:music-deep-copy
72   (list traLaLa traLaLa)))
73 #(define twice
74   (make-sequential-music newLa))
75
76 @{ \twice @}
77 @end example
78
79 In the above example, music expressions can be `exported' from the
80 input to the Scheme interpreter.  The opposite is also possible.  By
81 wrapping a Scheme value in the function @code{ly:export}, a Scheme
82 value is interpreted as if it were entered in LilyPond syntax.  Instead
83 of defining @code{\twice}, the example above could also have been
84 written as
85 @example
86 @dots{}
87 @{ #(ly:export (make-sequential-music (list newLa))) @}
88 @end example
89
90 Scheme code is evaluated as soon as the parser encounters it.  To
91 define some scheme code in a macro (to be called later), use
92
93 @example
94 #(define (nopc)
95   (ly:set-option 'point-and-click #f))
96
97 ...
98 #(nopc)
99 @{ c'4 @}
100 @end example
101
102
103 @refbugs
104
105 Mixing Scheme and LilyPond identifiers is not possible with the
106 @code{--safe} option.
107
108
109 @node Internal music representation
110 @subsection Internal music representation
111
112 When a music expression is parsed, it is converted into a set of
113 Scheme music objects.  The defining property of a music object is that
114 it takes up time.  Time is a rational number that measures the length
115 of a piece of music, in whole notes.
116
117 A music object has three kinds of types:
118 @itemize @bullet
119 @item
120 music name: Each music expression has a name, for example, a note
121 leads to a @internalsref{NoteEvent}, and @code{\simultaneous} leads to
122 a @internalsref{SimultaneousMusic}.  A list of all expressions
123 available is in the internals manual, under
124 @internalsref{Music expressions}.
125
126 @item
127 `type' or interface: Each music name has several `types' or
128 interfaces, for example, a note is an @code{event}, but it is also a
129 @code{note-event}, a @code{rhythmic-event}, and a @code{melodic-event}.
130
131 All classes of music are listed in the internals manual, under
132 @internalsref{Music classes}.
133
134 @item
135 C++ object: Each music object is represented by a C++ object.  For
136 technical reasons, different music objects may be represented by
137 different C++ object types.  For example, a note is @code{Event}
138 object, while @code{\grace} creates a @code{Grace_music} object.
139
140 We expect that distinctions between different C++ types will disappear
141 in the future.
142 @end itemize
143
144 The actual information of a music expression is stored in properties.
145 For example, a @internalsref{NoteEvent} has @code{pitch} and
146 @code{duration} properties that store the pitch and duration of that
147 note.  A list of all properties available is in the internals manual,
148 under @internalsref{Music properties}.
149
150 A compound music expression is a music object that contains other
151 music objects in its properties.  A list of objects can be stored in
152 the @code{elements} property of a music object, or a single `child'
153 music object in the @code{element} object.  For example,
154 @internalsref{SequentialMusic} has its children in @code{elements},
155 and @internalsref{GraceMusic} has its single argument in
156 @code{element}.  The body of a repeat is stored in the @code{element}
157 property of @internalsref{RepeatedMusic}, and the alternatives in
158 @code{elements}.
159
160
161
162
163 @node Extending music syntax
164 @subsection Extending music syntax
165
166 @c TODO: rewrite example.
167 @c The use of FUNC as example  argument is rather confusing.
168
169 The syntax of composite music expressions, like @code{\repeat},
170 @code{\transpose}, and @code{\context} follows the general form of
171
172 @example
173 \@code{keyword} @var{non-music-arguments} @var{music-arguments}
174 @end example
175
176 Such syntax can also be defined as user code.  To do this, it is
177 necessary to create a @emph{music function}.  This is a specially marked
178 Scheme function.  For example, the music function @code{\applyMusic} applies
179 a user-defined function to a music expression.  Its syntax is
180
181 @example
182 \applyMusic #@var{func} @var{music}
183 @end example
184
185 A music function is created with @code{ly:make-music-function},
186
187 @example
188 (ly:make-music-function
189 @end example
190
191 @code{\applyMusic} takes a Scheme function and a Music expression as
192 arguments.  This is encoded in its parameter list,
193
194 @example
195 (list procedure? ly:music?)
196 @end example
197
198 The function itself takes another argument, an Input location
199 object.  That object is used to provide error messages with file names
200 and line numbers.  The definition is the second argument of
201 @code{ly:make-music-function}.  The body simply calls the function
202
203 @example
204 (lambda (where func music)
205  (func music))
206 @end example
207
208 The above Scheme code only defines the functionality.  The tag
209 @code{\applyMusic} is selected by defining
210
211 @example
212 applyMusic = #(ly:make-music-function
213                 (list procedure? ly:music?)
214                 (lambda (parser location func music)
215                   (func music)))
216 @end example
217
218 A @code{define-music-function} macro is introduced on top of
219 @code{ly:make-music-function} to ease the definition of music
220 functions:
221
222 @example
223 applyMusic = #(define-music-function (parser location func music)
224                 (procedure? ly:music?)
225                 (func music))
226 @end example
227
228 Examples of the use of @code{\applyMusic} are in the next section.
229
230 @seealso
231 @file{ly/@/music@/-functions@/-init@/.ly}.
232
233 @node Manipulating music expressions
234 @subsection Manipulating music expressions
235
236 Music objects and their properties can be accessed and manipulated
237 directly, through the @code{\applyMusic} mechanism.
238 The syntax for @code{\applyMusic} is
239 @example
240 \applyMusic #@var{func} @var{music}
241 @end example
242
243 @noindent
244 This means that the Scheme function @var{func} is called with
245 @var{music} as its argument.  The return value of @var{func} is the
246 result of the entire expression.  @var{func} may read and write music
247 properties using the functions @code{ly:music-property} and
248 @code{ly:music-set-property!}.
249
250 An example is a function that reverses the order of elements in
251 its argument,
252 @lilypond[quote,verbatim,ragged-right]
253 #(define (rev-music-1 m)
254   (ly:music-set-property! m 'elements 
255     (reverse (ly:music-property m 'elements)))
256   m)
257
258 \applyMusic #rev-music-1 { c'4 d'4 } 
259 @end lilypond
260
261 The use of such a function is very limited.  The effect of this
262 function is void when applied to an argument that does not have
263 multiple children.  The following function application has no effect
264
265 @example
266 \applyMusic #rev-music-1 \grace @{ c4 d4 @}
267 @end example
268
269 @noindent
270 In this case, @code{\grace} is stored as @internalsref{GraceMusic}, which
271 has no @code{elements}, only a single @code{element}.  Every generally
272 applicable function for @code{\applyMusic} must -- like music expressions
273 themselves -- be recursive.
274
275 The following example is such a recursive function: It first extracts
276 the @code{elements} of an expression, reverses them and puts them
277 back.  Then it recurses, both on @code{elements} and @code{element}
278 children.
279 @example
280 #(define (reverse-music music)
281   (let* ((elements (ly:music-property music 'elements))
282          (child (ly:music-property music 'element))
283          (reversed (reverse elements)))
284
285     ; set children
286     (ly:music-set-property! music 'elements reversed)
287
288     ; recurse
289     (if (ly:music? child) (reverse-music child))
290     (map reverse-music reversed)
291
292     music))
293 @end example
294
295 A slightly more elaborate example is in
296 @inputfileref{input/@/test,reverse@/-music@/.ly}.
297
298 Some of the input syntax is also implemented as recursive music
299 functions.  For example, the syntax for polyphony
300 @example
301 <<a \\ b>>
302 @end example
303
304 @noindent
305 is actually implemented as a recursive function that replaces the
306 above by the internal equivalent of
307 @example
308 << \context Voice = "1" @{ \voiceOne a @}
309    \context Voice = "2" @{ \voiceTwo b @} >>
310 @end example
311
312 Other applications of @code{\applyMusic} are writing out repeats
313 automatically (@inputfileref{input/@/test,unfold@/-all@/-repeats@/.ly}),
314 saving keystrokes (@inputfileref{input/@/test,music@/-box@/.ly}) and
315 exporting LilyPond input to other formats
316 @c no @inputfileref{} here 
317 (eg. @file{input/@/no@/-notation/@/to@/-xml@/.ly}).
318
319 @seealso
320
321 @file{scm/@/music@/-functions@/.scm}, @file{scm/@/music@/-types@/.scm},
322 @inputfileref{input/@/test,add@/-staccato@/.ly},
323 @inputfileref{input/@/test,unfold@/-all@/-repeats@/.ly}, and
324 @inputfileref{input/@/test,music@/-box@/.ly}.
325
326
327 @node Displaying music expressions
328 @subsection Displaying music expressions
329
330 @cindex internal storage
331 @cindex @code{\displayMusic}
332 @cindex @code{\displayLilyMusic}
333
334 When writing a music function, it is often instructive to inspect how
335 a music expression is stored internally.  This can be done with the
336 music function @code{\displayMusic}.
337
338 @example
339 @{
340   \displayMusic @{ c'4\f @}
341 @}
342 @end example
343
344 Conversely, displaying a music expression in LilyPond notation can be
345 done using the music function @code{\displayLilyMusic}. For instance:
346
347 @example
348 @{
349   \displayLilyMusic \transpose c a, @{ c e g a bes @}
350 @}
351 @end example
352
353 will display:
354
355 @example
356 @{ a, cis e fis g @}
357 @end example
358
359 @node Using LilyPond syntax inside Scheme
360 @subsection Using LilyPond syntax inside Scheme
361
362 Creating music expressions in Scheme can be tedious, as they are
363 heavily nested and the resulting Scheme code is large.  For some
364 simple tasks, this can be avoided, using common LilyPond syntax inside
365 Scheme, with the dedicated @code{#@{ ... #@}} syntax.
366
367 The following two expressions give equivalent music expressions:
368 @example
369 mynotes = @{ \override Stem #'thickness = #4
370             @{ c'8 d' @} @}
371   
372 #(define mynotes #@{ \override Stem #'thickness = #4
373                     @{ c'8 d' @} #@})
374 @end example
375
376 The content of @code{#@{ ... #@}} is enclosed in an implicit @code{@{
377 ... @}} block, which is parsed.  The resulting music expression, a
378 @code{SequentialMusic} music object, is then returned and usable in Scheme.
379
380 Arbitrary Scheme forms, including variables, can be used in @code{#@{ ... #@}}
381 expressions with the @code{$} character (@code{$$} can be used to
382 produce a single @code{$} character).  This makes the creation of simple
383 functions straightforward.  In the following example, a function
384 setting the TextScript's padding is defined:
385
386 @lilypond[quote,verbatim,ragged-right]
387 #(use-modules (ice-9 optargs))
388 #(define* (textpad padding #:optional once?)
389   (ly:export   ; this is necessary for using the expression
390                ; directly inside a block
391     (if once?
392         #{ \once \override TextScript #'padding = #$padding #}
393         #{ \override TextScript #'padding = #$padding #})))
394
395  {
396    c'^"1"
397    #(textpad 3.0 #t) % only once
398    c'^"2"
399    c'^"3"
400    #(textpad 5.0)
401    c'^"4"
402    c'^"5"
403  }
404 @end lilypond
405
406 Here, the variable @code{padding} is a number; music expression
407 variables may also be used in a similar fashion, as in the following
408 example:
409
410 @lilypond[quote,verbatim,ragged-right]
411 #(define (with-padding padding)
412   (lambda (music)
413    #{ \override TextScript #'padding = #$padding
414       $music
415       \revert TextScript #'padding #}))
416
417 {
418   c'^"1"
419   \applyMusic #(with-padding 3) { c'^"2" c'^"3" }
420   c'^"4"
421 }
422 @end lilypond
423
424 The function created by @code{(with-padding 3)} adds @code{\override} and
425 @code{\revert} statements around the music given as an argument, and returns
426 this new expression.  Thus, this example is equivalent to:
427
428 @example
429 @{
430   c'^"1"
431   @{ \override TextScript #'padding = #3
432     @{ c'^"2" c'^"3"@}
433     \revert TextScript #'padding
434   @}
435   c'^"4"
436 @}
437 @end example
438
439 This function may also be defined as a music function:
440
441 @lilypond[quote,verbatim,ragged-right]
442 withPadding =
443   #(define-music-function (parser location padding music) (number? ly:music?)
444     #{ \override TextScript #'padding = #$padding
445        $music 
446        \revert TextScript #'padding #})
447
448 {
449   c'^"1"
450   \withPadding #3 { c'^"2" c'^"3"}
451   c'^"4"
452 }
453 @end lilypond
454
455
456 @node Markup programmer interface
457 @section Markup programmer interface
458
459 @c Please rewrite the second sentence; I don't understand its meaning. AS
460
461 Markups are implemented as special Scheme functions.  When applied
462 with as arguments an output definition (@code{\layout} or
463 @code{\paper}), and a list of properties and other arguments, produce
464 a Stencil object.
465
466 @menu
467 * Markup construction in Scheme::  
468 * How markups work internally ::  
469 * Markup command definition::   
470 @end menu
471
472 @node Markup construction in Scheme
473 @subsection Markup construction in Scheme
474
475 @cindex defining markup commands 
476
477 The @code{markup} macro builds markup expressions in Scheme while
478 providing a LilyPond-like syntax.  For example,
479 @example
480 (markup #:column (#:line (#:bold #:italic "hello" #:raise 0.4 "world")
481                   #:bigger #:line ("foo" "bar" "baz")))
482 @end example
483
484 @noindent
485 is equivalent to:
486 @example
487 \markup \column @{ \line @{ \bold \italic "hello" \raise #0.4 "world" @}
488                   \bigger \line @{ foo bar baz @} @}
489 @end example
490
491 @noindent
492 This example exposes the main translation rules between regular
493 LilyPond markup syntax and Scheme markup syntax, which are summed up
494 is this table:
495
496 @quotation
497 @multitable @columnfractions .3 .3
498 @item @b{LilyPond} @tab @b{Scheme}
499 @item @code{\markup markup1 @}} @tab @code{(markup markup1)}
500 @item @code{\markup @{ markup1 markup2 ... @}} @tab 
501         @code{(markup markup1 markup2 ... )}
502 @item @code{\command} @tab @code{#:command}
503 @item @code{\variable} @tab @code{variable}
504 @item @code{\center-align @{ ... @}} @tab @code{#:center-align ( ... )}
505 @item @code{string} @tab @code{"string"}
506 @item @code{#scheme-arg} @tab @code{scheme-arg}
507 @end multitable
508 @end quotation
509
510 Besides, the whole scheme language is accessible inside the
511 @code{markup} macro: thus, one may use function calls inside
512 @code{markup} in order to manipulate character strings for
513 instance.  This proves useful when defining new markup commands (see
514 @ref{Markup command definition}).
515
516 @refbugs
517
518 One can not feed the @code{#:line} (resp @code{#:center},
519 @code{#:column}) command with a variable or the result of a function
520 call.  Example:
521
522 @lisp
523 (markup #:line (fun-that-returns-markups))
524 @end lisp
525
526 @noindent
527 is invalid.  One should use the @code{make-line-markup} (resp.,
528 @code{make-center-markup} or @code{make-column-markup}) function
529 instead,
530 @lisp
531 (markup (make-line-markup (fun-that-returns-markups)))
532 @end lisp
533
534 @node How markups work internally 
535 @subsection How markups work internally 
536
537 In a markup like
538
539 @example
540 \raise #0.5 "foo"
541 @end example
542
543 @noindent
544 @code{\raise} is actually represented by the @code{raise-markup}
545 function.  The markup expression is stored as
546
547 @example
548 (list raise-markup 0.5 (list simple-markup "foo"))
549 @end example
550
551 When the markup is converted to printable objects (Stencils), the
552 @code{raise-markup} function is called as
553
554 @example
555 (apply raise-markup
556        @var{\layout object}
557        @var{list of property alists}
558        0.5
559        @var{the "foo" markup})
560 @end example
561
562 The @code{raise-markup} function first creates the stencil for the
563 @code{foo} string, and then it raises that Stencil by 0.5 staff space.
564 This is a rather simple example; more complex examples are in the rest
565 of this section, and in @file{scm/@/define@/-markup@/-commands@/.scm}.
566
567 @node Markup command definition
568 @subsection Markup command definition
569
570 New markup commands can be defined
571 with the @code{define-markup-command} scheme macro.
572 @lisp
573 (define-markup-command (@var{command-name} @var{layout} @var{props} @var{arg1} @var{arg2} ...)
574             (@var{arg1-type?} @var{arg2-type?} ...)
575   ..command body..)
576 @end lisp
577
578 The arguments signify
579
580 @table @var
581 @item argi
582 @var{i}th command argument
583 @item argi-type?
584 a type predicate for the i@var{th} argument
585 @item layout
586 the `layout' definition
587 @item props
588 a list of alists, containing all active properties. 
589 @end table
590
591 As a simple example, we show how to add a @code{\smallcaps} command,
592 which selects a small caps font.  Normally, we could select the
593 small caps font as follows:
594
595 @example
596 \markup @{ \override #'(font-shape . caps) Text-in-caps @}
597 @end example
598
599 This selects the caps font by setting the @code{font-shape} property to
600 @code{#'caps} for interpreting @code{Text-in-caps}.
601
602 To make the above available as @code{\smallcaps} command, we have to
603 define a function using @code{define-markup-command}.  The command should
604 take a single argument, of type markup.  Therefore, the start of the
605 definition should read
606 @example
607 (define-markup-command (smallcaps layout props argument) (markup?)
608 @end example
609
610 @noindent
611
612 What follows is the content of the command: we should interpret
613 the @code{argument} as a markup, i.e.,
614
615 @example
616 (interpret-markup layout @dots{} argument)
617 @end example
618
619 @noindent
620 This interpretation should add @code{'(font-shape . caps)} to the active
621 properties, so we substitute the following for the @dots{} in the
622 above example:
623
624 @example
625 (cons (list '(font-shape . caps) ) props)
626 @end example
627
628 @noindent
629 The variable @code{props} is a list of alists, and we prepend to it by
630 cons'ing a list with the extra setting.
631
632
633 Suppose that we are typesetting a recitative in an opera, and
634 we would like to define a command that will show character names in a
635 custom manner.  Names should be printed with small caps and translated a
636 bit to the left and top.  We will define a @code{\character} command
637 that takes into account the necessary translation, and uses the newly
638 defined @code{\smallcaps} command:
639
640 @example
641 #(define-markup-command (character layout props name) (string?)
642   "Print the character name in small caps, translated to the left and
643   top.  Syntax: \\character #\"name\""
644   (interpret-markup layout props 
645    (markup #:hspace 0 #:translate (cons -3 1) #:smallcaps name)))
646 @end example
647
648 There is one complication that needs explanation: texts above and below
649 the staff are moved vertically to be at a certain distance (the
650 @code{padding} property) from the staff and the notes.  To make sure
651 that this mechanism does not annihilate the vertical effect of our
652 @code{#:translate}, we add an empty string (@code{#:hspace 0}) before the
653 translated text.  Now the @code{#:hspace 0} will be put above the notes, and the
654 @code{name} is moved in relation to that empty string.  The net effect is
655 that the text is moved to the upper left.
656
657 The final result is as follows:
658 @example
659 @{
660   c''^\markup \character #"Cleopatra"
661   e'^\markup \character #"Giulio Cesare"
662 @}
663 @end example
664
665 @lilypond[quote,ragged-right]
666 #(define-markup-command (smallcaps layout props str) (string?)
667   "Print the string argument in small caps.  Syntax: \\smallcaps #\"string\""
668   (interpret-markup layout props
669    (make-line-markup
670     (map (lambda (s)
671           (if (= (string-length s) 0)
672               s
673               (markup #:large (string-upcase (substring s 0 1))
674                       #:translate (cons -0.6 0)
675                       #:tiny (string-upcase (substring s 1)))))
676          (string-split str #\Space)))))
677
678 #(define-markup-command (character layout props name) (string?)
679   "Print the character name in small caps, translated to the left and
680   top.  Syntax: \\character #\"name\""
681   (interpret-markup layout props 
682    (markup #:hspace 0 #:translate (cons -3 1) #:smallcaps name)))
683
684 {
685   c''^\markup \character #"Cleopatra" c'' c'' c''
686   e'^\markup \character #"Giulio Cesare" e' e' e'
687 }
688 @end lilypond
689
690 We have used the @code{caps} font shape, but suppose that our font
691 does not have a small-caps variant.  In that case we have to fake
692 the small caps font by setting a string in upcase with the first
693 letter a little larger:
694
695 @example
696 #(define-markup-command (smallcaps layout props str) (string?)
697   "Print the string argument in small caps."
698   (interpret-markup layout props
699    (make-line-markup
700     (map (lambda (s)
701           (if (= (string-length s) 0)
702               s
703               (markup #:large (string-upcase (substring s 0 1))
704                       #:translate (cons -0.6 0)
705                       #:tiny (string-upcase (substring s 1)))))
706          (string-split str #\Space)))))
707 @end example
708
709 The @code{smallcaps} command first splits its string argument into
710 tokens separated by spaces (@code{(string-split str #\Space)}); for
711 each token, a markup is built with the first letter made large and
712 upcased (@code{#:large (string-upcase (substring s 0 1))}), and a
713 second markup built with the following letters made tiny and upcased
714 (@code{#:tiny (string-upcase (substring s 1))}).  As LilyPond
715 introduces a space between markups on a line, the second markup is
716 translated to the left (@code{#:translate (cons -0.6 0) ...}).  Then,
717 the markups built for each token are put in a line by
718 @code{(make-line-markup ...)}.  Finally, the resulting markup is passed
719 to the @code{interpret-markup} function, with the @code{layout} and
720 @code{props} arguments.
721
722 Note: there is now an internal command @code{\smallCaps} which can
723 be used to set text in small caps.  See
724 @ref{Overview of text markup commands} for details.
725
726
727
728 @node Contexts for programmers
729 @section Contexts for programmers
730
731
732 @menu
733 * Context evaluation::          
734 * Running a function on all layout objects::  
735 @end menu
736
737 @node Context evaluation
738 @subsection Context evaluation
739
740 @cindex calling code during interpreting
741 @cindex @code{\applyContext}
742
743 Contexts can be modified during interpretation with Scheme code.  The
744 syntax for this is
745 @example
746 \applyContext @var{function}
747 @end example
748
749 @var{function} should be a Scheme function taking a single argument,
750 being the context to apply it to.  The following code will print the
751 current bar number on the standard output during the compile:
752
753 @example
754 \applyContext
755   #(lambda (x)
756     (format #t "\nWe were called in barnumber ~a.\n"
757      (ly:context-property x 'currentBarNumber)))
758 @end example
759
760
761
762 @node Running a function on all layout objects
763 @subsection Running a function on all layout objects
764
765
766 @cindex calling code on layout objects
767 @cindex @code{\applyOutput}
768
769
770 The most versatile way of tuning an object is @code{\applyOutput}.  Its
771 syntax is
772 @example
773 \applyOutput @var{proc}
774 @end example
775
776 @noindent
777 where @var{proc} is a Scheme function, taking three arguments.
778
779 When interpreted, the function @var{proc} is called for every layout
780 object found in the context, with the following arguments:
781 @itemize @bullet
782 @item the layout object itself,
783 @item the context where the layout object was created, and
784 @item the context where @code{\applyOutput} is processed.
785 @end itemize
786
787
788 In addition, the cause of the layout object, i.e., the music
789 expression or object that was responsible for creating it, is in the
790 object property @code{cause}.  For example, for a note head, this is a
791 @internalsref{NoteHead} event, and for a @internalsref{Stem} object,
792 this is a @internalsref{NoteHead} object.
793
794 Here is a function to use for @code{\applyOutput}; it blanks
795 note-heads on the center-line:
796
797 @example
798 (define (blanker grob grob-origin context)
799  (if (and (memq (ly:grob-property grob 'interfaces)
800                 note-head-interface)
801           (eq? (ly:grob-property grob 'staff-position) 0))
802      (set! (ly:grob-property grob 'transparent) #t)))
803 @end example
804