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