]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/internals.itely
* scm/paper.scm (set-staff-size): new function: set default
[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 and without a @code{Clef_engraver}.
309
310 @lilypond[relative=1,fragment,verbatim]
311 <<
312   \new Staff { c4 es4 g2 }
313   \new Staff \with {
314         StaffSymbol \set #'staff-space = #(magstep 1.5)
315         fontSize = #1.5
316         \remove "Clef_engraver"
317   } {
318         c4 es4 g2
319   } >>
320 @end lilypond
321
322 @refbugs
323
324 The command @code{\with} has no effect on contexts that already
325 exist. Neither can it be used for @internalsref{Score} contexts.
326
327
328 @node Engravers and performers
329 @subsection  Engravers and performers
330
331
332 Each context is composed of a number of building blocks, or plug-ins
333 called engravers.  An engraver is a specialized C++ class that is
334 compiled into the executable. Typically, an engraver is responsible
335 for one function: the @code{Slur_engraver} creates only @code{Slur}
336 objects, and the @code{Skip_event_swallow_translator} only swallows
337 (silently gobbles) @code{SkipEvent}s.
338
339
340
341 @cindex engraver
342 @cindex plug-in
343
344 An existing context definition can be changed by adding or removing an
345 engraver. The syntax for these operations is 
346 @example
347 \consists @var{engravername}
348 \remove @var{engravername}
349 @end example
350
351 @cindex \consists
352 @cindex \remove
353
354 @noindent
355 Here @var{engravername} is a string, the name of an engraver in the
356 system. In the following example, the @code{Clef_engraver} is removed
357 from the Staff context. The result is a staff without a clef, where
358 the central C is at its default position, the center line:
359
360 @lilypond[verbatim,singleline]
361 \score {
362   \notes {
363     c'4 f'4
364   }
365   \paper {
366     \translator {
367       \StaffContext
368       \remove Clef_engraver
369     }
370   }
371 }
372 @end lilypond
373
374 A list of all engravers is in the internal documentation,
375 see @internalsref{All engravers}.
376
377 @node Defining new contexts
378 @subsection Defining new contexts
379
380
381 It is also possible to define new contexts from scratch.  To do this,
382 you must define give the new context a name.  In the following
383 example, a very simple Staff context is created: one that will put
384 note heads on a staff symbol.
385
386 @example
387 \translator @{
388   \type "Engraver_group_engraver"
389   \name "SimpleStaff"
390   \alias "Staff"
391   \consists "Staff_symbol_engraver"
392   \consists "Note_head_engraver"
393   \consistsend "Axis_group_engraver"
394 @}
395 @end example
396
397 @noindent
398 The argument of @code{\type} is the name for a special engraver that
399 handles cooperation between simple engravers such as
400 @code{Note_head_engraver} and @code{Staff_symbol_engraver}.  This
401 should always be  @code{Engraver_group_engraver} (unless you are
402 defining a Score context from scratch, in which case
403 @code{Score_engraver}   must be used).
404
405 The complete list of context  modifiers is the following:
406 @itemize @bullet
407 @item @code{\alias} @var{alternate-name}:
408 This specifies a different name.  In the above example,
409 @code{\property Staff.X = Y} will also work on @code{SimpleStaff}s.
410
411 @item @code{\consistsend} @var{engravername}:
412 Analogous to @code{\consists}, but makes sure that
413 @var{engravername} is always added to the end of the list of
414 engravers.
415
416 Engravers that group context objects into axis groups or alignments
417 need to be at the end of the list. @code{\consistsend} insures that
418 engravers stay at the end even if a user adds or removes engravers.
419     
420 @item @code{\accepts} @var{contextname}:
421 This context can contains @var{contextname} contexts.  The first
422 @code{\accepts} is created as a default context when events (e.g. notes
423 or rests) are encountered.
424
425 @item @code{\denies}:
426 The opposite of @code{\accepts}.
427
428 @item @code{\name} @var{contextname}:
429 This sets the type name of the context, e.g. @code{Staff},
430 @code{Voice}.  If the name is not specified, the translator will not
431 do anything.
432 @end itemize
433
434
435 @node Scheme integration
436 @section Scheme integration
437
438 @cindex Scheme
439 @cindex GUILE
440 @cindex Scheme, in-line code
441 @cindex accessing Scheme
442 @cindex evaluating Scheme
443 @cindex LISP
444
445 LilyPond internally uses GUILE, a Scheme-interpreter, to represent
446 data throughout the whole program, and glue together different program
447 modules. For advanced usage, it is sometimes necessary to access and
448 program the Scheme interpreter.
449
450 Scheme is a full-blown programming language, from the LISP
451 family. and a full discussion is outside the scope of this document.
452 Interested readers are referred to the website
453 @uref{http://www.schemers.org/} for more information on Scheme.
454
455 The GUILE library for extension is documented at
456 @uref{http://www.gnu.org/software/guile}.
457 @ifinfo
458 When it is installed, the following link should take you to its manual
459 @ref{(guile.info)guile}
460 @end ifinfo
461
462 @menu
463 * Inline Scheme::               
464 * Input variables and Scheme::  
465 * Scheme datatypes::            
466 * Assignments::                 
467 @end menu
468
469 @node Inline Scheme
470 @subsection Inline Scheme
471
472 Scheme expressions can be entered in the input file by entering a
473 hash-sign (@code{#}).  The expression following the hash-sign is
474 evaluated as Scheme. For example, the boolean value @var{true} is
475 @code{#t} in Scheme, so for LilyPond @var{true} looks like @code{##t},
476 and can be used in property assignments:
477 @example
478   \property Staff.autoBeaming = ##f
479 @end example
480
481
482 @node Input variables and Scheme
483 @subsection Input variables and Scheme
484
485
486 The input format supports the notion of variable: in the following
487 example, a music expression is assigned to a variable with the name
488 @code{traLaLa}.
489 @example
490   traLaLa = \notes @{ c'4 d'4 @}
491 @end example
492
493 @noindent
494
495 There is also a form of scoping: in the following example, the
496 @code{\paper} block also contains a @code{traLaLa} variable, which is
497 independent of the outer @code{\traLaLa}.
498 @example
499   traLaLa = \notes @{ c'4 d'4 @}
500   \paper @{ traLaLa = 1.0 @}
501 @end example
502 @c
503 In effect, each input file is a scope, and all @code{\header},
504 @code{\midi} and @code{\paper} blocks are scopes nested inside that
505 toplevel scope.
506
507 Both variables and scoping are implemented in the GUILE module system.
508 An anonymous Scheme module is attached to each scope. An assignment of
509 the form
510 @example
511  traLaLa = \notes @{ c'4 d'4 @} 
512 @end example
513
514 @noindent
515 is internally converted to a Scheme definition
516 @example
517  (define traLaLa @var{Scheme value of ``@code{\notes ... }''})
518 @end example
519
520 This means that input variables and Scheme variables may be freely
521 mixed.  In the following example, a music fragment is stored in the
522 variable @code{traLaLa}, and duplicated using Scheme. The result is
523 imported in a @code{\score} by means of a second variable
524 @code{twice}:
525 @example
526   traLaLa = \notes @{ c'4 d'4 @}
527   
528   #(define newLa (map ly:music-deep-copy
529     (list traLaLa traLaLa)))
530   #(define twice
531     (make-sequential-music newLa))
532
533   \score @{ \twice @}
534 @end example
535
536 In the above example, music expressions can be `exported' from the
537 input to the Scheme interpreter. The opposite is also possible. By
538 wrapping a Scheme value in the function @code{ly:export}, a Scheme
539 value is interpreted as if it were entered in LilyPond syntax: instead
540 of defining @code{\twice}, the example above could also have been
541 written as
542 @example
543   @dots{}
544   \score @{ #(ly:export (make-sequential-music newLa)) @}
545 @end example
546
547
548
549
550
551 @node Scheme datatypes
552 @subsection Scheme datatypes
553
554 Scheme is used to glue together different program modules. To aid this
555 glue function, many LilyPond specific object types can be passed as
556 Scheme value. 
557
558 The following list are all LilyPond specific types, that
559 can exist during parsing:
560 @table @code
561 @item Duration
562 @item Input
563 @item Moment
564 @item Music
565 @item Event
566 In C++ terms, an @code{Event} is a subtype of @code{Music}. However,
567 both have different functions in the syntax.
568 @item Music_output_def
569 @item Pitch
570 @item Score
571 @item Translator_def
572 @end table
573
574
575 During a run, transient objects are also created and destroyed.
576
577 @table @code
578 @item Grob: short for `Graphical object'.
579 @item Scheme_hash_table 
580 @item Music_iterator
581
582 @item Molecule: Device-independent page output object,
583 including dimensions.  
584
585 @item Syllable_group
586
587 @item Spring_smob
588
589 @item Translator: An object that produces audio objects or Grobs.
590 It may be accessed with @code{\applyoutput}.
591
592 @item Font_metric: An object representing a font.
593 @end table
594
595 Many functions are defined to manipulate these data structures. They
596 are all listed and documented in the internals manual, see
597 @internalsref{All scheme functions}.
598
599
600 @node Assignments
601 @subsection Assignments
602 @cindex Assignments
603
604 Variables allow objects to be assigned to names during the parse
605 stage.  To assign a variable, use
606 @example
607 @var{name}@code{=}@var{value}
608 @end example
609 To refer to a variable, precede its name with a backslash:
610 `@code{\}@var{name}'.  @var{value} is any valid Scheme value or any of
611 the input-types listed above.  Variable assignments can appear at top
612 level in the LilyPond file, but also in @code{\paper} blocks.
613
614 A variable can be created with any string for its name, but for
615 accessing it in the LilyPond syntax, its name must consist of
616 alphabetic characters only, and may not be a keyword of the syntax.
617 There are no restrictions for naming and accessing variables in the
618 Scheme interpreter,
619
620 The right hand side of a variable assignment is parsed completely
621 before the assignment is done, so variables may be  redefined in terms
622 of its old value, e.g.
623 @c
624 @example
625 foo = \foo * 2.0
626 @end example
627
628 When a variable is referenced in LilyPond syntax, the information it
629 points to is copied.  For this reason, an variable reference must
630 always be the first item in a block.
631
632 @example
633 \paper @{
634   foo = 1.0
635   \paperIdent % wrong and invalid
636 @}
637 @end example
638
639 @example
640 \paper @{
641   \paperIdent % correct
642   foo = 1.0
643 @}
644 @end example
645
646       
647
648 @node Music storage format
649 @section Music storage format
650
651 Music in LilyPond is entered as music expressions. This section
652 discusses different types of music expressions, and explains how
653 information is stored internally. This internal storage is accessible
654 through the Scheme interpreter, so music expressions may be
655 manipulated using Scheme functions. 
656
657 @menu
658 * Music expressions::           
659 * Internal music representation::  
660 * Manipulating music expressions::  
661 @end menu
662
663 @node Music expressions
664 @subsection Music expressions
665 @cindex music expressions
666
667 Notes, rests, lyric syllables are music expressions.  Small music
668 expressions may be combined to form larger ones, for example, by
669 enclosing a list of expressions in @code{\sequential @{ @}} or @code{<<
670 >>}.  In the following example, a compound expression is formed out of
671 the quarter note @code{c} and a quarter note @code{d}:
672
673 @example 
674 \sequential @{ c4 d4 @} 
675 @end example 
676
677 @cindex Sequential music
678 @cindex @code{\sequential}
679 @cindex sequential music
680 @cindex @code{<<}
681 @cindex @code{>>}
682 @cindex Simultaneous music
683 @cindex @code{\simultaneous}
684
685 The two basic compound music expressions are simultaneous and
686 sequential music:
687
688 @example
689 \sequential @code{@{} @var{musicexprlist} @code{@}}
690 \simultaneous @code{@{} @var{musicexprlist} @code{@}}
691 @end example
692
693 For both, there is a shorthand:
694
695 @example
696 @code{@{} @var{musicexprlist} @code{@}}
697 @end example
698
699 @noindent
700 for sequential and
701
702 @example
703 @code{<<} @var{musicexprlist} @code{>>}
704 @end example
705
706 @noindent
707 for simultaneous music.
708 In principle, the way in which you nest sequential and simultaneous to
709 produce music is not relevant.  In the following example, three chords
710 are expressed in two different ways:
711
712 @lilypond[fragment,verbatim,center,quote]
713 \notes \context Voice {
714   <<a c'>> <<b d'>> <<c' e'>>
715   << { a b c' } { c' d' e' } >>
716 }
717 @end lilypond
718 However, using @code{<<} and @code{>>} for entering chords leads to
719 various peculiarities. For this reason, a special syntax
720 for chords was introduced in version 1.7: @code{< >}.
721
722
723
724
725
726 Other compound music expressions include:
727 @example
728 \repeat @var{expr}
729 \transpose @var{from} @var{to} @var{expr}
730 \apply @var{func} @var{expr}
731 \context @var{type} = @var{id} @var{expr}
732 \times @var{fraction} @var{expr}
733 @end example
734
735 @node Internal music representation
736 @subsection Internal music representation
737
738
739
740
741
742
743 When a music expression is parsed, it is converted into a set of
744 Scheme music objects. The defining property of a music object is that
745 it takes up time. Time is a rational number that measures the length
746 of a piece of music, in whole notes.
747
748 A music object has three kinds of types:
749 @itemize @bullet
750 @item
751   music name: Each music expression has a name, for example, a note
752 leads to a @internalsref{NoteEvent}, and @code{\simultaneous} leads to
753 a @internalsref{SimultaneousMusic}. A list of all expressions
754 available is in the internals manual, under @internalsref{Music
755 expressions}.
756
757 @item
758   `type' or interface: Each music name has several `types' or interface,
759   for example, a note is an @code{event}, but it is also a @code{note-event}, 
760   a @code{rhythmic-event} and a @code{melodic-event}.
761
762   All classes of music are listed in the internals manual, under
763   @internalsref{Music classes}. 
764 @item
765 C++ object: Each music object is represented by a C++ object. For technical
766 reasons, different music objects may be represented by different C++
767 object types. For example, a note is @code{Event} object, while
768 @code{\grace} creates a @code{Grace_music} object.
769
770 We expect that distinctions between different C++ types will disappear
771 in the future.
772 @end itemize
773
774 The actual information of a music expression is stored in properties.
775 For example, a @internalsref{NoteEvent} has @code{pitch} and
776 @code{duration} properties that store the pitch and duration of that
777 note.  A list of all properties available is in the internals manual,
778 under @internalsref{Music properties}.
779
780 A compound music expression is a music object that contains other
781 music objects in its properties. A list of objects can be stored in
782 the @code{elements} property of a music object, or a single `child'
783 music object in the @code{element} object. For example,
784 @internalsref{SequentialMusic} has its children in @code{elements},
785 and @internalsref{GraceMusic} has its single argument in
786 @code{element}. The body of a repeat is in @code{element} property of
787 @internalsref{RepeatedMusic}, and the alternatives in @code{elements}.
788
789 @node Manipulating music expressions
790 @subsection Manipulating music expressions
791
792 Music objects and their properties can be accessed and manipulated
793 directly, through the @code{\apply} mechanism.  
794 The syntax for @code{\apply} is 
795 @example
796 \apply #@var{func} @var{music}
797 @end example
798
799 @noindent
800 This means that the scheme function @var{func} is called with
801 @var{music} as its argument.  The return value of @var{func} is the
802 result of the entire expression.  @var{func} may read and write music
803 properties using the functions @code{ly:get-mus-property} and
804 @code{ly:set-mus-property!}.
805
806 An example is a function that reverses the order of elements in
807 its argument:
808 @lilypond[verbatim,singleline]
809   #(define (rev-music-1 m)
810      (ly:set-mus-property! m 'elements (reverse
811        (ly:get-mus-property m 'elements)))
812      m)
813   \score { \notes \apply #rev-music-1 { c4 d4 } }
814 @end lilypond
815
816 The use of such a function is very limited. The effect of this
817 function is void when applied to an argument which is does not have
818 multiple children.  The following function application has no effect:
819
820 @example
821   \apply #rev-music-1 \grace @{ c4 d4 @}
822 @end example
823
824 @noindent
825 In this case, @code{\grace} is stored as @internalsref{GraceMusic}, which has no
826 @code{elements}, only a single @code{element}. Every generally
827 applicable function for @code{\apply} must -- like music expressions
828 themselves -- be recursive.
829
830 The following example is such a recursive function: It first extracts
831 the @code{elements} of an expression, reverses them and puts them
832 back. Then it recurses, both on @code{elements} and @code{element}
833 children.
834 @example
835 #(define (reverse-music music)
836   (let* ((elements (ly:get-mus-property music 'elements))
837          (child (ly:get-mus-property music 'element))
838          (reversed (reverse elements)))
839
840     ; set children
841     (ly:set-mus-property! music 'elements reversed)
842
843     ; recurse
844     (if (ly:music? child) (reverse-music child))
845     (map reverse-music reversed)
846     
847     music))
848 @end example
849
850 A slightly more elaborate example is in
851 @inputfileref{input/test,reverse-music.ly}.
852
853 Some of the input syntax is also implemented as recursive music
854 functions. For example, the syntax for polyphony
855 @example
856   <<a \\ b>>
857 @end example
858
859 @noindent
860 is actually  implemented as a recursive function that replaces the
861 above by the internal equivalent of
862 @example
863   << \context Voice = "1" @{ \voiceOne a @}
864     \context Voice = "2" @{ \voiceTwo b @} >>
865 @end example
866
867 Other applications of @code{\apply} are writing out repeats
868 automatically (@inputfileref{input/test,unfold-all-repeats.ly}),
869 saving keystrokes (@inputfileref{input/test,music-box.ly}) and
870 exporting
871 LilyPond input to other formats  (@inputfileref{input/test,to-xml.ly})
872
873 @seealso
874
875 @file{scm/music-functions.scm}, @file{scm/music-types.scm},
876 @inputfileref{input/test,add-staccato.ly},
877 @inputfileref{input/test,unfold-all-repeats.ly}, and
878 @inputfileref{input/test,music-box.ly}.
879
880 @node Lexical details
881 @section Lexical details
882
883
884 @cindex string
885 @cindex concatenate
886
887 By enclosing text in quotes (@code{"}), strings are formed.  To
888 include a @code{"} character in a string write @code{\"}.  Various
889 other backslash sequences have special interpretations as in the C
890 language.  A string that does not contain spaces or special characters
891 can be written without the quotes. The exact form of such unquoted
892 strings depends on the input mode; there are different rules for
893 lyrics, notes and markups.  Strings can be concatenated with the
894 @code{+} operator.
895
896
897 @node Output details
898 @section Output details
899
900 LilyPond's default output format is @TeX{}.  Using the option @option{-f}
901 (or @option{--format}) other output formats can be selected also, but
902 currently none of them work reliably.
903
904 At the beginning of the output file, various global parameters are defined.
905 It also contains a large @code{\special} call to define PostScript routines
906 to draw items not representable with @TeX{}, mainly slurs and ties.  A DVI
907 driver must be able to understand such embedded PostScript, or the output
908 will be rendered incompletely.
909
910 Then the file @file{lilyponddefs.tex} is loaded to define the macros used
911 in the code which follows.  @file{lilyponddefs.tex} includes various other
912 files, partially depending on the global parameters.
913
914 Now the music is output system by system (a `system' consists of all
915 staves belonging together).  From @TeX{}'s point of view, a system is an
916 @code{\hbox} which contains a lowered @code{\vbox} so that it is centered
917 vertically on the baseline of the text.  Between systems,
918 @code{\interscoreline} is inserted vertically to have stretchable space.
919 The horizontal dimension of the @code{\hbox} is given by the
920 @code{linewidth} parameter from LilyPond's @code{\paper} block.
921
922
923 After the last system LilyPond emits a stronger variant of
924 @code{\interscoreline} only if the macro
925 @code{\lilypondpaperlastpagefill} is not defined (flushing the systems
926 to the top of the page).  You can avoid that by setting the variable
927 @code{lastpagefill} in LilyPond's @code{\paper} block.
928
929 It is possible to fine-tune the vertical offset further by defining the
930 macro @code{\lilypondscoreshift}:
931
932 @example
933 \def\lilypondscoreshift@{0.25\baselineskip@}
934 @end example
935
936 @noindent
937 where @code{\baselineskip} is the distance from one text line to the next.
938
939 The code produced by LilyPond should be run through La@TeX{}, not
940 plain @TeX{}.
941
942 Here an example how to embed a small LilyPond file @code{foo.ly} into
943 running La@TeX{} text without using the @code{lilypond-book} script
944 (@pxref{lilypond-book manual}):
945
946 @example
947 \documentclass@{article@}
948
949 \def\lilypondpaperlastpagefill@{@}
950 \lineskip 5pt
951 \def\lilypondscoreshift@{0.25\baselineskip@}
952
953 \begin@{document@}
954 This is running text which includes an example music file
955 \input@{foo.tex@}
956 right here.
957 \end@{document@}
958 @end example
959
960 The file @file{foo.tex} has been simply produced with
961
962 @example
963 lilypond foo.ly
964 @end example
965
966 It is important to set the @code{indent} parameter to zero in the
967 @code{\paper} block of @file{foo.ly}.
968
969 The call to @code{\lineskip} assures that there is enough vertical space
970 between the LilyPond box and the surrounding text lines.
971
972 @c EOF