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