release commit
[lilypond.git] / Documentation / user / internals.itely
blobd8d9d3d14b51653b1b85e63747940fda05d64771
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
10 @node Technical manual
11 @chapter Technical manual
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.
20 @menu
21 * Interpretation context::      
22 * Scheme integration::          
23 * Music storage format::        
24 * Lexical details::             
25 * Output details::              
26 @end menu
29 @node Interpretation context
30 @section Interpretation context
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
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?
54 @item What symbols will be printed at this point?
56 @item What is the current key signature, time signature, point within
57 the measure, etc.?
58 @end itemize
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.
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.
73 @node Creating contexts
74 @subsection Creating contexts
75 @cindex @code{\context}
76 @cindex context selection
78 Contexts for a music expression can be selected manually, using one of
79 the following music expressions:
81 @example
82 \new @var{contexttype} @var{musicexpr}
83 \context @var{contexttype} [= @var{contextname}] @var{musicexpr}
84 @end example
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:
91 @lilypond[verbatim,singleline]
92 \score {
93   \notes \relative c'' {
94     c4 <<d4 \context Staff = "another" e4>> f
95   }
97 @end lilypond
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.
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.
112 When using automatic staff changes, automatic phrasing, etc., the
113 context names have special meanings, so @code{\new} cannot be
114 used.
117 @node Default contexts
118 @subsection Default contexts
120 Every top level music is interpreted by the @code{Score} context; in
121 other words, you may think of @code{\score} working like
123 @example
124 \score @{
125   \context Score @var{music}
127 @end example
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.
136 @lilypond[verbatim,singleline]
137   \notes \context Voice = goUp { c'4 d' e' }
138 @end lilypond
141 Second, contexts are created automatically to be able to interpret the
142 music expressions.  Consider the following example:
144 @lilypond[verbatim, singleline]
145   \score { \notes { c'4-( d' e'-) } }
146 @end lilypond
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.
157 @node Context properties
158 @subsection Context properties
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
166 @example
167 \property @var{contextname}.@var{propname} = @var{value}
168 @end example
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.
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.
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
189 @example
190 \property @var{contextname}.@var{propname} = @var{value}
191 @end example
193 @noindent
196 @example
197 \context @var{contextname}
198 \property Current.@var{propname} = @var{value}
199 @end example
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.
206 Properties can be unset using the following statement.
207 @example
208 \property @var{contextname}.@var{propname} \unset
209 @end example
211 @cindex properties, unsetting
212 @cindex @code{\unset} 
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.
219 @refbugs
221 The syntax of @code{\unset} is asymmetric: @code{\property \unset} is not
222 the inverse of @code{\property \set}.
225 @node Context evaluation
226 @subsection Context evaluation
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
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:
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
247 @node Defining contexts
248 @subsection Defining contexts
250 @cindex context definition
251 @cindex translator definition
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:
257 @example
258 \paper @{
259   \translator @{
260     @var{context-identifier}
261   @}
263 @end example
265 @noindent
266 Every predefined context has a standard identifier. For example, the
267 @code{Staff} context can be referred to as @code{\StaffContext}.
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 = #"||"
277 @end example
278 These assignments happen before interpretation starts, so a @code{\property}
279 command will override any predefined settings.
281 @cindex engraver
283 @refbugs
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.
289 @node Changing contexts locally
290 @subsection Changing contexts locally
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 
296 @example
297   \with @{
298      @var{context modifications}
299   @}
300 @end example
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.
307 The following example shows how a staff is created with bigger spaces:
309 @lilypond[relative=1,fragment]
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
321 @refbugs
323 @code{\with} has no effect on contexts that already exist. Neither can
324 it be used for @internalsref{Score} contexts.
327 @node Engravers and performers
328 @subsection  Engravers and performers
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.
340 @cindex engraver
341 @cindex plug-in
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
350 @cindex \consists
351 @cindex \remove
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:
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   }
371 @end lilypond
373 A list of all engravers is in the internal documentation,
374 see @internalsref{All engravers}.
376 @node Defining new contexts
377 @subsection Defining new contexts
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.
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"
394 @end example
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).
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.
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.
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.
424 @item @code{\denies}:
425 The opposite of @code{\accepts}.
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
434 @node Scheme integration
435 @section Scheme integration
437 @cindex Scheme
438 @cindex GUILE
439 @cindex Scheme, in-line code
440 @cindex accessing Scheme
441 @cindex evaluating Scheme
442 @cindex LISP
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.
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.
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
461 @menu
462 * Inline Scheme::               
463 * Input variables and Scheme::  
464 * Scheme datatypes::            
465 * Assignments::                 
466 @end menu
468 @node Inline Scheme
469 @subsection Inline Scheme
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
481 @node Input variables and Scheme
482 @subsection Input variables and Scheme
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
492 @noindent
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
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.
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
513 @noindent
514 is internally converted to a Scheme definition
515 @example
516  (define traLaLa @var{Scheme value of ``@code{\notes ... }''})
517 @end example
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))
532   \score @{ \twice @}
533 @end example
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
550 @node Scheme datatypes
551 @subsection Scheme datatypes
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. 
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
574 During a run, transient objects are also created and destroyed.
576 @table @code
577 @item Grob: short for `Graphical object'.
578 @item Scheme_hash_table 
579 @item Music_iterator
581 @item Molecule: Device-independent page output object,
582 including dimensions.  
584 @item Syllable_group
586 @item Spring_smob
588 @item Translator: An object that produces audio objects or Grobs.
589 It may be accessed with @code{\applyoutput}.
591 @item Font_metric: An object representing a font.
592 @end table
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}.
599 @node Assignments
600 @subsection Assignments
601 @cindex Assignments
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.
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,
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.
623 @example
624 foo = \foo * 2.0
625 @end example
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.
631 @example
632 \paper @{
633   foo = 1.0
634   \paperIdent % wrong and invalid
636 @end example
638 @example
639 \paper @{
640   \paperIdent % correct
641   foo = 1.0
643 @end example
645       
647 @node Music storage format
648 @section Music storage format
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. 
656 @menu
657 * Music expressions::           
658 * Internal music representation::  
659 * Manipulating music expressions::  
660 @end menu
662 @node Music expressions
663 @subsection Music expressions
664 @cindex music expressions
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}:
672 @example 
673 \sequential @{ c4 d4 @} 
674 @end example 
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}
684 The two basic compound music expressions are simultaneous and
685 sequential music:
687 @example
688 \sequential @code{@{} @var{musicexprlist} @code{@}}
689 \simultaneous @code{@{} @var{musicexprlist} @code{@}}
690 @end example
692 For both, there is a shorthand:
694 @example
695 @code{@{} @var{musicexprlist} @code{@}}
696 @end example
698 @noindent
699 for sequential and
701 @example
702 @code{<<} @var{musicexprlist} @code{>>}
703 @end example
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:
711 @lilypond[fragment,verbatim,center,quote]
712 \notes \context Voice {
713   <<a c'>> <<b d'>> <<c' e'>>
714   << { a b c' } { c' d' e' } >>
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{< >}.
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
734 @node Internal music representation
735 @subsection Internal music representation
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.
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}.
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}.
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.
769 We expect that distinctions between different C++ types will disappear
770 in the future.
771 @end itemize
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}.
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}.
788 @node Manipulating music expressions
789 @subsection Manipulating music expressions
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
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!}.
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
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:
819 @example
820   \apply #rev-music-1 \grace @{ c4 d4 @}
821 @end example
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.
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)))
839     ; set children
840     (ly:set-mus-property! music 'elements reversed)
842     ; recurse
843     (if (ly:music? child) (reverse-music child))
844     (map reverse-music reversed)
845     
846     music))
847 @end example
849 A slightly more elaborate example is in
850 @inputfileref{input/test,reverse-music.ly}.
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
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
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})
872 @seealso
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}.
879 @node Lexical details
880 @section Lexical details
883 @cindex string
884 @cindex concatenate
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.
893 @node Output details
894 @section Output details
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.
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.
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.
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.
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.
925 It is possible to fine-tune the vertical offset further by defining the
926 macro @code{\lilypondscoreshift}:
928 @example
929 \def\lilypondscoreshift@{0.25\baselineskip@}
930 @end example
932 @noindent
933 where @code{\baselineskip} is the distance from one text line to the next.
935 The code produced by LilyPond should be run through La@TeX{}, not
936 plain @TeX{}.
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}):
942 @example
943 \documentclass@{article@}
945 \def\lilypondpaperlastpagefill@{@}
946 \lineskip 5pt
947 \def\lilypondscoreshift@{0.25\baselineskip@}
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
956 The file @file{foo.tex} has been simply produced with
958 @example
959 lilypond foo.ly
960 @end example
962 It is important to set the @code{indent} parameter to zero in the
963 @code{\paper} block of @file{foo.ly}.
965 The call to @code{\lineskip} assures that there is enough vertical space
966 between the LilyPond box and the surrounding text lines.
968 @c EOF