]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/internals.itely
release commit
[lilypond.git] / Documentation / user / internals.itely
1 @c -*-texinfo-*-
2 @c Note:
3 @c
4 @c A menu is needed before every deeper *section nesting of @nodes
5 @c Run M-x texinfo-all-menus-update
6 @c to automagically fill in these menus
7 @c before saving changes
8
9
10 @node Technical manual
11 @chapter Technical manual
12
13
14 When LilyPond is run, it reads an input file which is parsed.  During
15 parsing, Music objects are created. This music is interpreted, which
16 is done by contexts, that produce graphical objects.  This section
17 discusses details of these three concepts, and how they are glued
18 together with the embedded Scheme interpreter.
19
20 @menu
21 * Interpretation context::      
22 * Scheme integration::          
23 * Music storage format::        
24 * Lexical details::             
25 * Output details::              
26 @end menu
27
28
29 @node Interpretation context
30 @section Interpretation context
31
32 @menu
33 * Creating contexts::           
34 * Default contexts::            
35 * Context properties::          
36 * Context evaluation::          
37 * Defining contexts::           
38 * Changing contexts locally::   
39 * Engravers and performers::    
40 * Defining new contexts::       
41 @end menu
42
43
44 Interpretation contexts are objects that only exist during program
45 run.  During the interpretation phase (when @code{interpreting music}
46 is printed on the standard output), the music expression in a
47 @code{\score} block is interpreted in time order, the same order in
48 which we hear and play the music.  During this phase, the interpretation
49 context holds the state for the current point within the music, for
50 example:
51 @itemize @bullet
52 @item What notes are playing at this point?
53
54 @item What symbols will be printed at this point?
55
56 @item What is the current key signature, time signature, point within
57 the measure, etc.?
58 @end itemize
59
60 Contexts are grouped hierarchically: A @internalsref{Voice} context is
61 contained in a @internalsref{Staff} context (because a staff can contain
62 multiple voices at any point), a @internalsref{Staff} context is contained in
63 @internalsref{Score}, @internalsref{StaffGroup}, or
64 @internalsref{ChoirStaff} context.
65
66 Contexts associated with sheet music output are called @emph{notation
67 contexts}, those for sound output are called @emph{performance
68 contexts}.  The default definitions of the standard notation and
69 performance contexts can be found in @file{ly/engraver-init.ly} and
70 @file{ly/performer-init.ly}, respectively.
71
72
73 @node Creating contexts
74 @subsection Creating contexts
75 @cindex @code{\context}
76 @cindex context selection
77
78 Contexts for a music expression can be selected manually, using one of
79 the following music expressions:
80
81 @example
82 \new @var{contexttype} @var{musicexpr}
83 \context @var{contexttype} [= @var{contextname}] @var{musicexpr}
84 @end example
85
86 @noindent
87 This means that @var{musicexpr} should be interpreted within a context
88 of type @var{contexttype} (with name @var{contextname} if specified).
89 If no such context exists, it will be created:
90
91 @lilypond[verbatim,singleline]
92 \score {
93   \notes \relative c'' {
94     c4 <<d4 \context Staff = "another" e4>> f
95   }
96 }
97 @end lilypond
98
99 @noindent
100 In this example, the @code{c} and @code{d} are printed on the default
101 staff.  For the @code{e}, a context @code{Staff} called @code{another}
102 is specified; since that does not exist, a new context is created.
103 Within @code{another}, a (default) Voice context is created for the
104 @code{e4}.  A context is ended when when all music referring it has
105 finished, so after the third quarter, @code{another} is removed.
106
107 The @code{\new} construction creates a context with a
108 generated, unique @var{contextname}. An expression with
109 @code{\new} always leads to a new context. This is convenient
110 for creating multiple staffs, multiple lyric lines, etc.
111
112 When using automatic staff changes, automatic phrasing, etc., the
113 context names have special meanings, so @code{\new} cannot be
114 used.
115
116
117 @node Default contexts
118 @subsection Default contexts
119
120 Every top level music is interpreted by the @code{Score} context; in
121 other words, you may think of @code{\score} working like
122
123 @example
124 \score @{
125   \context Score @var{music}
126 @}
127 @end example
128
129 Music expressions  inherit their context from the enclosing music
130 expression. Hence, it is not necessary to explicitly specify
131 @code{\context} for most expressions.  In
132 the following example, only the sequential expression has an explicit
133 context. The notes contained therein inherit the @code{goUp} context
134 from the enclosing music expression.
135
136 @lilypond[verbatim,singleline]
137   \notes \context Voice = goUp { c'4 d' e' }
138 @end lilypond
139
140
141 Second, contexts are created automatically to be able to interpret the
142 music expressions.  Consider the following example:
143
144 @lilypond[verbatim, singleline]
145   \score { \notes { c'4-( d' e'-) } }
146 @end lilypond
147
148 @noindent
149 The sequential music is interpreted by the Score context initially,
150 but when a note is encountered, contexts are setup to accept that
151 note.  In this case, a @code{Thread}, @code{Voice}, and @code{Staff}
152 context are created.  The rest of the sequential music is also
153 interpreted with the same @code{Thread}, @code{Voice}, and
154 @code{Staff} context, putting the notes on the same staff, in the same
155 voice.
156
157 @node Context properties
158 @subsection Context properties
159
160 Contexts have properties.  These properties are set from the @file{.ly}
161 file using the following expression:
162 @cindex @code{\property}
163 @cindex context properties
164 @cindex properties, context
165
166 @example
167 \property @var{contextname}.@var{propname} = @var{value}
168 @end example
169
170 @noindent
171 Sets the @var{propname} property of the context @var{contextname} to
172 the specified Scheme expression @var{value}.  Both @var{propname} and
173 @var{contextname} are strings, which can often be written unquoted.
174
175 @cindex inheriting
176 Properties that are set in one context are inherited by all of the
177 contained contexts.  This means that a property valid for the
178 @internalsref{Voice} context can be set in the @internalsref{Score} context
179 (for example) and thus take effect in all @internalsref{Voice} contexts.
180
181 @cindex @code{Current}
182 If you do not wish to specify the name of the context in the
183 @code{\property}-expression itself, you can refer to the abstract context
184 name, @code{Current}.  The @code{Current} context is the latest
185 used context.  This will typically mean the @internalsref{Thread} context, 
186 but you can force another context with the
187 @code{\property}-command.  Hence the expressions
188
189 @example
190 \property @var{contextname}.@var{propname} = @var{value}
191 @end example
192
193 @noindent
194 and
195
196 @example
197 \context @var{contextname}
198 \property Current.@var{propname} = @var{value}
199 @end example
200
201 @noindent
202 do the same thing.  The main use for this is in predefined variables.
203 This construction allows the specification of a property-setting
204 without restriction to a specific context.
205
206 Properties can be unset using the following statement.
207 @example
208 \property @var{contextname}.@var{propname} \unset
209 @end example
210
211 @cindex properties, unsetting
212 @cindex @code{\unset} 
213
214 @noindent
215 This removes the definition of @var{propname} in @var{contextname}.  If
216 @var{propname} was not defined in @var{contextname} (but was inherited
217 from a higher context), then this has no effect.
218
219 @refbugs
220
221 The syntax of @code{\unset} is asymmetric: @code{\property \unset} is not
222 the inverse of @code{\property \set}.
223
224
225 @node Context evaluation
226 @subsection Context evaluation
227
228 Contexts can be modified during interpretation with Scheme code. The
229 syntax for this is
230 @example
231   \applycontext @var{function}
232 @end example
233
234 @var{function} should be a Scheme function taking a single argument,
235 being the context to apply it to. The following code will print the
236 current bar number on the standard output during the compile:
237
238 @example
239     \applycontext
240       #(lambda (x)
241          (format #t "\nWe were called in barnumber ~a.\n"
242           (ly:get-context-property x 'currentBarNumber)))
243 @end example
244
245
246
247 @node Defining contexts
248 @subsection Defining contexts
249
250 @cindex context definition
251 @cindex translator definition
252
253 The most common way to create a new context definition is by extending
254 an existing one.  An existing context from the paper block is copied
255 by referencing a context identifier:
256
257 @example
258 \paper @{
259   \translator @{
260     @var{context-identifier}
261   @}
262 @}
263 @end example
264
265 @noindent
266 Every predefined context has a standard identifier. For example, the
267 @code{Staff} context can be referred to as @code{\StaffContext}.
268
269 The context can then be modified by setting or changing properties,
270 e.g.
271 @example
272 \translator @{
273   \StaffContext
274   Stem \set #'thickness = #2.0
275   defaultBarType = #"||"
276 @}
277 @end example
278 These assignments happen before interpretation starts, so a @code{\property}
279 command will override any predefined settings.
280
281 @cindex engraver
282
283 @refbugs
284
285 It is not possible to collect multiple property assignments in a
286 variable, and apply to one @code{\translator} definition by
287 referencing that variable.
288
289 @node Changing contexts locally
290 @subsection Changing contexts locally
291
292
293 Extending an existing context can also be done locally. A piece of
294 music can be interpreted in a changed context by using the following syntax 
295
296 @example
297   \with @{
298      @var{context modifications}
299   @}
300 @end example
301
302 These statements comes between @code{\new} or @code{\context} and the
303 music to be interpreted. The @var{context modifications} property
304 settings and @code{\remove}, @code{\consists} and @code{\consistsend}
305 commands. The syntax is similar to the @code{\translator} block.
306
307 The following example shows how a staff is created with bigger spaces:
308
309 @lilypond[relative=1,fragment]
310 <<
311   \new Staff { c4 es4 g2 }
312   \new Staff \with {
313         StaffSymbol \set #'staff-space = #(magstep 1.5)
314         fontSize = #1.5
315         \remove "Clef_engraver"
316   } {
317         c4 es4 g2
318   } >>
319 @end lilypond
320
321 @refbugs
322
323 @code{\with} has no effect on contexts that already exist. Neither can
324 it be used for @internalsref{Score} contexts.
325
326
327 @node Engravers and performers
328 @subsection  Engravers and performers
329
330
331 Each context is composed of a number of building blocks, or plug-ins
332 called engravers.  An engraver is a specialized C++ class that is
333 compiled into the executable. Typically, an engraver is responsible
334 for one function: the @code{Slur_engraver} creates only @code{Slur}
335 objects, and the @code{Skip_event_swallow_translator} only swallows
336 (silently gobbles) @code{SkipEvent}s.
337
338
339
340 @cindex engraver
341 @cindex plug-in
342
343 An existing context definition can be changed by adding or removing an
344 engraver. The syntax for these operations is 
345 @example
346 \consists @var{engravername}
347 \remove @var{engravername}
348 @end example
349
350 @cindex \consists
351 @cindex \remove
352
353 @noindent
354 Here @var{engravername} is a string, the name of an engraver in the
355 system. In the following example, the @code{Clef_engraver} is removed
356 from the Staff context. The result is a staff without a clef, where
357 the central C is at its default position, the center line:
358
359 @lilypond[verbatim,singleline]
360 \score {
361   \notes {
362     c'4 f'4
363   }
364   \paper {
365     \translator {
366       \StaffContext
367       \remove Clef_engraver
368     }
369   }
370 }
371 @end lilypond
372
373 A list of all engravers is in the internal documentation,
374 see @internalsref{All engravers}.
375
376 @node Defining new contexts
377 @subsection Defining new contexts
378
379
380 It is also possible to define new contexts from scratch.  To do this,
381 you must define give the new context a name.  In the following
382 example, a very simple Staff context is created: one that will put
383 note heads on a staff symbol.
384
385 @example
386 \translator @{
387   \type "Engraver_group_engraver"
388   \name "SimpleStaff"
389   \alias "Staff"
390   \consists "Staff_symbol_engraver"
391   \consists "Note_head_engraver"
392   \consistsend "Axis_group_engraver"
393 @}
394 @end example
395
396 @noindent
397 The argument of @code{\type} is the name for a special engraver that
398 handles cooperation between simple engravers such as
399 @code{Note_head_engraver} and @code{Staff_symbol_engraver}.  This
400 should always be  @code{Engraver_group_engraver} (unless you are
401 defining a Score context from scratch, in which case
402 @code{Score_engraver}   must be used).
403
404 The complete list of context  modifiers is the following:
405 @itemize @bullet
406 @item @code{\alias} @var{alternate-name}:
407 This specifies a different name.  In the above example,
408 @code{\property Staff.X = Y} will also work on @code{SimpleStaff}s.
409
410 @item @code{\consistsend} @var{engravername}:
411 Analogous to @code{\consists}, but makes sure that
412 @var{engravername} is always added to the end of the list of
413 engravers.
414
415 Engravers that group context objects into axis groups or alignments
416 need to be at the end of the list. @code{\consistsend} insures that
417 engravers stay at the end even if a user adds or removes engravers.
418     
419 @item @code{\accepts} @var{contextname}:
420 This context can contains @var{contextname} contexts.  The first
421 @code{\accepts} is created as a default context when events (e.g. notes
422 or rests) are encountered.
423
424 @item @code{\denies}:
425 The opposite of @code{\accepts}.
426
427 @item @code{\name} @var{contextname}:
428 This sets the type name of the context, e.g. @code{Staff},
429 @code{Voice}.  If the name is not specified, the translator will not
430 do anything.
431 @end itemize
432
433
434 @node Scheme integration
435 @section Scheme integration
436
437 @cindex Scheme
438 @cindex GUILE
439 @cindex Scheme, in-line code
440 @cindex accessing Scheme
441 @cindex evaluating Scheme
442 @cindex LISP
443
444 LilyPond internally uses GUILE, a Scheme-interpreter, to represent
445 data throughout the whole program, and glue together different program
446 modules. For advanced usage, it is sometimes necessary to access and
447 program the Scheme interpreter.
448
449 Scheme is a full-blown programming language, from the LISP
450 family. and a full discussion is outside the scope of this document.
451 Interested readers are referred to the website
452 @uref{http://www.schemers.org/} for more information on Scheme.
453
454 The GUILE library for extension is documented at
455 @uref{http://www.gnu.org/software/guile}.
456 @ifinfo
457 When it is installed, the following link should take you to its manual
458 @ref{(guile.info)guile}
459 @end ifinfo
460
461 @menu
462 * Inline Scheme::               
463 * Input variables and Scheme::  
464 * Scheme datatypes::            
465 * Assignments::                 
466 @end menu
467
468 @node Inline Scheme
469 @subsection Inline Scheme
470
471 Scheme expressions can be entered in the input file by entering a
472 hash-sign (@code{#}).  The expression following the hash-sign is
473 evaluated as Scheme. For example, the boolean value @var{true} is
474 @code{#t} in Scheme, so for LilyPond @var{true} looks like @code{##t},
475 and can be used in property assignments:
476 @example
477   \property Staff.autoBeaming = ##f
478 @end example
479
480
481 @node Input variables and Scheme
482 @subsection Input variables and Scheme
483
484
485 The input format supports the notion of variable: in the following
486 example, a music expression is assigned to a variable with the name
487 @code{traLaLa}.
488 @example
489   traLaLa = \notes @{ c'4 d'4 @}
490 @end example
491
492 @noindent
493
494 There is also a form of scoping: in the following example, the
495 @code{\paper} block also contains a @code{traLaLa} variable, which is
496 independent of the outer @code{\traLaLa}.
497 @example
498   traLaLa = \notes @{ c'4 d'4 @}
499   \paper @{ traLaLa = 1.0 @}
500 @end example
501 @c
502 In effect, each input file is a scope, and all @code{\header},
503 @code{\midi} and @code{\paper} blocks are scopes nested inside that
504 toplevel scope.
505
506 Both variables and scoping are implemented in the GUILE module system.
507 An anonymous Scheme module is attached to each scope. An assignment of
508 the form
509 @example
510  traLaLa = \notes @{ c'4 d'4 @} 
511 @end example
512
513 @noindent
514 is internally converted to a Scheme definition
515 @example
516  (define traLaLa @var{Scheme value of ``@code{\notes ... }''})
517 @end example
518
519 This means that input variables and Scheme variables may be freely
520 mixed.  In the following example, a music fragment is stored in the
521 variable @code{traLaLa}, and duplicated using Scheme. The result is
522 imported in a @code{\score} by means of a second variable
523 @code{twice}:
524 @example
525   traLaLa = \notes @{ c'4 d'4 @}
526   
527   #(define newLa (map ly:music-deep-copy
528     (list traLaLa traLaLa)))
529   #(define twice
530     (make-sequential-music newLa))
531
532   \score @{ \twice @}
533 @end example
534
535 In the above example, music expressions can be `exported' from the
536 input to the Scheme interpreter. The opposite is also possible. By
537 wrapping a Scheme value in the function @code{ly:export}, a Scheme
538 value is interpreted as if it were entered in LilyPond syntax: instead
539 of defining @code{\twice}, the example above could also have been
540 written as
541 @example
542   @dots{}
543   \score @{ #(ly:export (make-sequential-music newLa)) @}
544 @end example
545
546
547
548
549
550 @node Scheme datatypes
551 @subsection Scheme datatypes
552
553 Scheme is used to glue together different program modules. To aid this
554 glue function, many LilyPond specific object types can be passed as
555 Scheme value. 
556
557 The following list are all LilyPond specific types, that
558 can exist during parsing:
559 @table @code
560 @item Duration
561 @item Input
562 @item Moment
563 @item Music
564 @item Event
565 In C++ terms, an @code{Event} is a subtype of @code{Music}. However,
566 both have different functions in the syntax.
567 @item Music_output_def
568 @item Pitch
569 @item Score
570 @item Translator_def
571 @end table
572
573
574 During a run, transient objects are also created and destroyed.
575
576 @table @code
577 @item Grob: short for `Graphical object'.
578 @item Scheme_hash_table 
579 @item Music_iterator
580
581 @item Molecule: Device-independent page output object,
582 including dimensions.  
583
584 @item Syllable_group
585
586 @item Spring_smob
587
588 @item Translator: An object that produces audio objects or Grobs.
589 It may be accessed with @code{\applyoutput}.
590
591 @item Font_metric: An object representing a font.
592 @end table
593
594 Many functions are defined to manipulate these data structures. They
595 are all listed and documented in the internals manual, see
596 @internalsref{All scheme functions}.
597
598
599 @node Assignments
600 @subsection Assignments
601 @cindex Assignments
602
603 Variables allow objects to be assigned to names during the parse
604 stage.  To assign a variable, use
605 @example
606 @var{name}@code{=}@var{value}
607 @end example
608 To refer to a variable, precede its name with a backslash:
609 `@code{\}@var{name}'.  @var{value} is any valid Scheme value or any of
610 the input-types listed above.  Variable assignments can appear at top
611 level in the LilyPond file, but also in @code{\paper} blocks.
612
613 A variable can be created with any string for its name, but for
614 accessing it in the LilyPond syntax, its name must consist of
615 alphabetic characters only, and may not be a keyword of the syntax.
616 There are no restrictions for naming and accessing variables in the
617 Scheme interpreter,
618
619 The right hand side of a variable assignment is parsed completely
620 before the assignment is done, so variables may be  redefined in terms
621 of its old value, e.g.
622 @c
623 @example
624 foo = \foo * 2.0
625 @end example
626
627 When a variable is referenced in LilyPond syntax, the information it
628 points to is copied.  For this reason, an variable reference must
629 always be the first item in a block.
630
631 @example
632 \paper @{
633   foo = 1.0
634   \paperIdent % wrong and invalid
635 @}
636 @end example
637
638 @example
639 \paper @{
640   \paperIdent % correct
641   foo = 1.0
642 @}
643 @end example
644
645       
646
647 @node Music storage format
648 @section Music storage format
649
650 Music in LilyPond is entered as music expressions. This section
651 discusses different types of music expressions, and explains how
652 information is stored internally. This internal storage is accessible
653 through the Scheme interpreter, so music expressions may be
654 manipulated using Scheme functions. 
655
656 @menu
657 * Music expressions::           
658 * Internal music representation::  
659 * Manipulating music expressions::  
660 @end menu
661
662 @node Music expressions
663 @subsection Music expressions
664 @cindex music expressions
665
666 Notes, rests, lyric syllables are music expressions.  Small music
667 expressions may be combined to form larger ones, for example, by
668 enclosing a list of expressions in @code{\sequential @{ @}} or @code{<<
669 >>}.  In the following example, a compound expression is formed out of
670 the quarter note @code{c} and a quarter note @code{d}:
671
672 @example 
673 \sequential @{ c4 d4 @} 
674 @end example 
675
676 @cindex Sequential music
677 @cindex @code{\sequential}
678 @cindex sequential music
679 @cindex @code{<<}
680 @cindex @code{>>}
681 @cindex Simultaneous music
682 @cindex @code{\simultaneous}
683
684 The two basic compound music expressions are simultaneous and
685 sequential music:
686
687 @example
688 \sequential @code{@{} @var{musicexprlist} @code{@}}
689 \simultaneous @code{@{} @var{musicexprlist} @code{@}}
690 @end example
691
692 For both, there is a shorthand:
693
694 @example
695 @code{@{} @var{musicexprlist} @code{@}}
696 @end example
697
698 @noindent
699 for sequential and
700
701 @example
702 @code{<<} @var{musicexprlist} @code{>>}
703 @end example
704
705 @noindent
706 for simultaneous music.
707 In principle, the way in which you nest sequential and simultaneous to
708 produce music is not relevant.  In the following example, three chords
709 are expressed in two different ways:
710
711 @lilypond[fragment,verbatim,center,quote]
712 \notes \context Voice {
713   <<a c'>> <<b d'>> <<c' e'>>
714   << { a b c' } { c' d' e' } >>
715 }
716 @end lilypond
717 However, using @code{<<} and @code{>>} for entering chords leads to
718 various peculiarities. For this reason, a special syntax
719 for chords was introduced in version 1.7: @code{< >}.
720
721
722
723
724
725 Other compound music expressions include:
726 @example
727 \repeat @var{expr}
728 \transpose @var{from} @var{to} @var{expr}
729 \apply @var{func} @var{expr}
730 \context @var{type} = @var{id} @var{expr}
731 \times @var{fraction} @var{expr}
732 @end example
733
734 @node Internal music representation
735 @subsection Internal music representation
736
737
738
739
740
741
742 When a music expression is parsed, it is converted into a set of
743 Scheme music objects. The defining property of a music object is that
744 it takes up time. Time is a rational number that measures the length
745 of a piece of music, in whole notes.
746
747 A music object has three kinds of types:
748 @itemize @bullet
749 @item
750   music name: Each music expression has a name, for example, a note
751 leads to a @internalsref{NoteEvent}, and @code{\simultaneous} leads to
752 a @internalsref{SimultaneousMusic}. A list of all expressions
753 available is in the internals manual, under @internalsref{Music
754 expressions}.
755
756 @item
757   `type' or interface: Each music name has several `types' or interface,
758   for example, a note is an @code{event}, but it is also a @code{note-event}, 
759   a @code{rhythmic-event} and a @code{melodic-event}.
760
761   All classes of music are listed in the internals manual, under
762   @internalsref{Music classes}. 
763 @item
764 C++ object: Each music object is represented by a C++ object. For technical
765 reasons, different music objects may be represented by different C++
766 object types. For example, a note is @code{Event} object, while
767 @code{\grace} creates a @code{Grace_music} object.
768
769 We expect that distinctions between different C++ types will disappear
770 in the future.
771 @end itemize
772
773 The actual information of a music expression is stored in properties.
774 For example, a @internalsref{NoteEvent} has @code{pitch} and
775 @code{duration} properties that store the pitch and duration of that
776 note.  A list of all properties available is in the internals manual,
777 under @internalsref{Music properties}.
778
779 A compound music expression is a music object that contains other
780 music objects in its properties. A list of objects can be stored in
781 the @code{elements} property of a music object, or a single `child'
782 music object in the @code{element} object. For example,
783 @internalsref{SequentialMusic} has its children in @code{elements},
784 and @internalsref{GraceMusic} has its single argument in
785 @code{element}. The body of a repeat is in @code{element} property of
786 @internalsref{RepeatedMusic}, and the alternatives in @code{elements}.
787
788 @node Manipulating music expressions
789 @subsection Manipulating music expressions
790
791 Music objects and their properties can be accessed and manipulated
792 directly, through the @code{\apply} mechanism.  
793 The syntax for @code{\apply} is 
794 @example
795 \apply #@var{func} @var{music}
796 @end example
797
798 @noindent
799 This means that the scheme function @var{func} is called with
800 @var{music} as its argument.  The return value of @var{func} is the
801 result of the entire expression.  @var{func} may read and write music
802 properties using the functions @code{ly:get-mus-property} and
803 @code{ly:set-mus-property!}.
804
805 An example is a function that reverses the order of elements in
806 its argument:
807 @lilypond[verbatim,singleline]
808   #(define (rev-music-1 m)
809      (ly:set-mus-property! m 'elements (reverse
810        (ly:get-mus-property m 'elements)))
811      m)
812   \score { \notes \apply #rev-music-1 { c4 d4 } }
813 @end lilypond
814
815 The use of such a function is very limited. The effect of this
816 function is void when applied to an argument which is does not have
817 multiple children.  The following function application has no effect:
818
819 @example
820   \apply #rev-music-1 \grace @{ c4 d4 @}
821 @end example
822
823 @noindent
824 In this case, @code{\grace} is stored as @internalsref{GraceMusic}, which has no
825 @code{elements}, only a single @code{element}. Every generally
826 applicable function for @code{\apply} must -- like music expressions
827 themselves -- be recursive.
828
829 The following example is such a recursive function: It first extracts
830 the @code{elements} of an expression, reverses them and puts them
831 back. Then it recurses, both on @code{elements} and @code{element}
832 children.
833 @example
834 #(define (reverse-music music)
835   (let* ((elements (ly:get-mus-property music 'elements))
836          (child (ly:get-mus-property music 'element))
837          (reversed (reverse elements)))
838
839     ; set children
840     (ly:set-mus-property! music 'elements reversed)
841
842     ; recurse
843     (if (ly:music? child) (reverse-music child))
844     (map reverse-music reversed)
845     
846     music))
847 @end example
848
849 A slightly more elaborate example is in
850 @inputfileref{input/test,reverse-music.ly}.
851
852 Some of the input syntax is also implemented as recursive music
853 functions. For example, the syntax for polyphony
854 @example
855   <<a \\ b>>
856 @end example
857
858 @noindent
859 is actually  implemented as a recursive function that replaces the
860 above by the internal equivalent of
861 @example
862   << \context Voice = "1" @{ \voiceOne a @}
863     \context Voice = "2" @{ \voiceTwo b @} >>
864 @end example
865
866 Other applications of @code{\apply} are writing out repeats
867 automatically (@inputfileref{input/test,unfold-all-repeats.ly}),
868 saving keystrokes (@inputfileref{input/test,music-box.ly}) and
869 exporting
870 LilyPond input to other formats  (@inputfileref{input/test,to-xml.ly})
871
872 @seealso
873
874 @file{scm/music-functions.scm}, @file{scm/music-types.scm},
875 @inputfileref{input/test,add-staccato.ly},
876 @inputfileref{input/test,unfold-all-repeats.ly}, and
877 @inputfileref{input/test,music-box.ly}.
878
879 @node Lexical details
880 @section Lexical details
881
882
883 @cindex string
884 @cindex concatenate
885
886 Begins and ends with the @code{"} character.  To include a @code{"}
887 character in a string write @code{\"}.  Various other backslash
888 sequences have special interpretations as in the C language.  A string
889 that contains no spaces can be written without the quotes.  Strings can
890 be concatenated with the @code{+} operator.
891
892
893 @node Output details
894 @section Output details
895
896 LilyPond's default output format is @TeX{}.  Using the option @option{-f}
897 (or @option{--format}) other output formats can be selected also, but
898 currently none of them work reliably.
899
900 At the beginning of the output file, various global parameters are defined.
901 It also contains a large @code{\special} call to define PostScript routines
902 to draw items not representable with @TeX{}, mainly slurs and ties.  A DVI
903 driver must be able to understand such embedded PostScript, or the output
904 will be rendered incompletely.
905
906 Then the file @file{lilyponddefs.tex} is loaded to define the macros used
907 in the code which follows.  @file{lilyponddefs.tex} includes various other
908 files, partially depending on the global parameters.
909
910 Now the music is output system by system (a `system' consists of all
911 staves belonging together).  From @TeX{}'s point of view, a system is an
912 @code{\hbox} which contains a lowered @code{\vbox} so that it is centered
913 vertically on the baseline of the text.  Between systems,
914 @code{\interscoreline} is inserted vertically to have stretchable space.
915 The horizontal dimension of the @code{\hbox} is given by the
916 @code{linewidth} parameter from LilyPond's @code{\paper} block.
917
918
919 After the last system LilyPond emits a stronger variant of
920 @code{\interscoreline} only if the macro
921 @code{\lilypondpaperlastpagefill} is not defined (flushing the systems
922 to the top of the page).  You can avoid that by setting the variable
923 @code{lastpagefill} in LilyPond's @code{\paper} block.
924
925 It is possible to fine-tune the vertical offset further by defining the
926 macro @code{\lilypondscoreshift}:
927
928 @example
929 \def\lilypondscoreshift@{0.25\baselineskip@}
930 @end example
931
932 @noindent
933 where @code{\baselineskip} is the distance from one text line to the next.
934
935 The code produced by LilyPond should be run through La@TeX{}, not
936 plain @TeX{}.
937
938 Here an example how to embed a small LilyPond file @code{foo.ly} into
939 running La@TeX{} text without using the @code{lilypond-book} script
940 (@pxref{lilypond-book manual}):
941
942 @example
943 \documentclass@{article@}
944
945 \def\lilypondpaperlastpagefill@{@}
946 \lineskip 5pt
947 \def\lilypondscoreshift@{0.25\baselineskip@}
948
949 \begin@{document@}
950 This is running text which includes an example music file
951 \input@{foo.tex@}
952 right here.
953 \end@{document@}
954 @end example
955
956 The file @file{foo.tex} has been simply produced with
957
958 @example
959 lilypond foo.ly
960 @end example
961
962 It is important to set the @code{indent} parameter to zero in the
963 @code{\paper} block of @file{foo.ly}.
964
965 The call to @code{\lineskip} assures that there is enough vertical space
966 between the LilyPond box and the surrounding text lines.
967
968 @c EOF