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