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