]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/internals.itely
* server.el.patch: new file.
[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 Advanced Topics
11 @chapter Advanced Topics
12
13
14 When translating the input to notation, there are number of distinct
15 phases.  We list them here:
16
17 @c todo: moved from refman. 
18
19 The purpose of LilyPond is explained informally by the term `music
20 typesetter'.  This is not a fully correct name: not only does the
21 program print musical symbols, it also makes aesthetic decisions.
22 Symbols and their placements are @emph{generated} from a high-level
23 musical description.  In other words, LilyPond would be best described
24 by `music compiler' or `music to notation compiler'.
25
26 LilyPond is linked to GUILE, GNU's Scheme library for extension
27 programming. The Scheme library provides the glue that holds together
28 the low-level routines and separate modules which are written in C++.
29
30 When lilypond is run to typeset sheet music, the following happens:
31 @itemize @bullet
32 @item GUILE Initialization: various scheme files are read
33 @item parsing: first standard @code{ly} initialization  files are read, and
34 then the user @file{ly} file is read.
35 @item interpretation: the music in the file is processed ``in playing
36 order'', i.e. the order that  you  use to read sheet music, or the
37 order in which notes are played. The result of this step is a typesetting
38 specification.
39
40 @item typesetting:
41 The typesetting specification is solved: positions and formatting is
42 calculated.
43
44 @item the visible results ("virtual ink") are written to the output file.
45 @end itemize
46
47 During these stages different types of data play the the main role:
48 during parsing, @strong{Music} objects are created.  During the
49 interpretation, @strong{contexts} are constructed, and with these
50 contexts a network of @strong{graphical objects} (``grobs'') is
51 created. These grobs contain unknown variables, and the network forms a
52 set of equations. After solving the equations and filling in these
53 variables, the printed output is written to an output file.
54
55 These threemanship of tasks (parsing, translating, typesetting) and
56 data-structures (music, context, graphical objects) permeates the entire
57 design of the program.
58
59
60
61 @table @b
62
63 @item Parsing:
64
65 The LY file is read, and converted to a list of @code{Scores}, which
66 each contain @code{Music} and paper/midi-definitions. Here @code{Music},
67 @code{Pitch} and @code{Duration}  objects are created.
68
69 @item Interpreting music
70 @cindex interpreting music
71
72 All music events are "read" in the same order as they would be played
73 (or read from paper). At every step of the interpretation, musical
74 events are delivered to
75 interpretation contexts,
76 @cindex engraver
77 which use them to build @code{Grob}s (or MIDI objects, for MIDI output).
78
79 In this stage @code{Music_iterators} do a traversal of the @code{Music}
80 structure. The music events thus encountered are reported to
81 @code{Translator}s, a set of objects that collectively form interpretation
82 contexts.
83
84
85 @item Prebreaking
86
87 @cindex prebreaking
88
89 At places where line breaks may occur, clefs and bars are prepared for
90 a possible line break. 
91
92 @item Preprocessing
93
94 @cindex preprocessing
95
96 In this stage, all information that is needed to determine line breaking
97 is computed. 
98
99 @item Break calculation:
100
101 The lines and horizontal positions of the columns are determined.
102
103 @item Breaking
104
105 Relations between all grobs are modified to reflect line breaks: When a
106 spanner, e.g. a slur, crosses a line-break, then the spanner is "broken
107 into pieces", for every line that the spanner is in, a copy of the grob
108 is made. A substitution process redirects all grob-reference so that
109 each spanner grob will only reference other grobs in the same line.
110
111 @item Outputting:
112
113 All vertical dimensions and spanning objects are computed, and all grobs
114 are output, line by line. The output is encoded in the form of
115 @code{Molecule}s
116
117 @end table
118
119 The data types that are mentioned here are all discussed in this
120 section.
121
122
123
124 @c FIXME: Note entry vs Music entry at top level menu is confusing.
125 @c . {Music entry}
126 @menu
127 * Output formats::              
128 * Interpretation context::      
129 * Syntactic details::           
130 * Lexical details::             
131 @end menu
132
133 @c . {Output formats}
134 @node Output formats
135 @section Output formats
136
137 LilyPond can output processed music in different output formats.  They
138 can be used by invoking lilypond directly, i.e. not through ly2dvi,
139 and supplying the @code{-f} command line option.  See also
140 @ref{Invoking LilyPond}.
141
142 @menu
143 * @TeX{} output::               
144 * PostScript output::           
145 * Scheme output::               
146 * ASCIIScript output::          
147 * Sketch output::               
148 @end menu
149
150 @node @TeX{} output
151 @subsection @TeX{} output
152 @cindex @TeX{} output
153
154 LilyPond will use @TeX{} by default.  Even if you want to produce
155 PostScript output for viewing or printing, you should normally have
156 LilyPond produce @TeX{} first.  The .tex output must be processed by
157 @TeX{} (@strong{not} La@TeX{}) to generate a .dvi.  Then, @file{Dvips}
158 is used to generate PostScript.  Alternatively, @file{ly2dvi} can be
159 used to generate the .dvi for you.
160
161 @refbugs
162
163 Titling is not generated unless you use @file{ly2dvi}. 
164
165
166 @node PostScript output
167 @subsection PostScript output
168 @cindex PostScript output
169 @cindex direct PostScript output
170
171 LilyPond can produce PostScript directly, without going through @TeX{}.
172 Currently, this is mainly useful if you cannot use TeX, because direct
173 PostScript output has some problems; see Bugs below.
174
175 @example
176 $ lilypond -fps foo.ly
177 GNU LilyPond 1.3.144
178 Now processing: `foo.ly'
179 Parsing...
180 Interpreting music...[3]
181 Preprocessing elements... 
182 Calculating column positions... 
183 paper output to foo.ps...
184
185 $ cat /usr/share/lilypond/pfa/feta20.pfa foo.ps | lpr
186 @end example
187
188
189 @refbugs
190
191 Text font selection is broken.
192
193 The .ps file does not contain the .pfa font files.  To print a .ps
194 created through direct postscript output, you should prepend the
195 necessary .pfa files to LilyPond's .ps output, or upload them to the
196 printer before printing.
197
198 The line height calculation is broken, you must set @var{lineheight} in
199 the paperblock if you have more than one staff in your score, e.g.
200
201 @example
202   ...
203   \paper @{
204     % Set line height to 40 staff spaces
205     lineheight = 40    
206   @}
207 @end example
208
209 @node Scheme output
210 @subsection Scheme output
211 @cindex Scheme output
212
213 In the typesetting stage, LilyPond builds a page description, which is
214 then written to disk in postscript, @TeX{} or ASCII art. Before it is
215 written, the page description is represented as Scheme expressions.  You
216 can also dump these  Scheme expressions to a file, which may be
217 convenient for debugging output routines.  This is done with the Scheme
218 output format
219
220 @example
221 $ lilypond -fscm foo.ly
222 GNU LilyPond 1.3.144
223 Now processing: `foo.ly'
224 Parsing...
225 Interpreting music...[3]
226 Preprocessing elements... 
227 Calculating column positions... 
228 paper output to foo.scm...
229
230 $ head -4 foo.scm 
231 ;;; Usage: guile -s x.scm > x.tex
232  (primitive-load-path 'standalone.scm)
233 ; (scm-tex-output)
234  (scm-ps-output)
235
236 $ guile -s foo.scm > foo.tex
237 @end example
238
239
240 @node ASCIIScript output
241 @subsection ASCIIScript output
242 @cindex ASCIIScript output
243 @cindex ascii script
244 @cindex ascii art
245
246 LilyPond can output ASCII Art.  This is a two step process, LilyPond
247 produces an ASCII description file, dubbed ASCIIScript (extension
248 @file{.as}).  ASCIIScript has a small and simple command set that
249 includes font selection, character and string printing and line drawing
250 commands.  The program @file{as2text} is used to translate an .as file
251 to text.
252
253 To produce ASCII Art, you must include an ASCII Art paper definition
254 file in your .ly, one of:
255 @example
256 \include "paper-as5.ly"
257 \include "paper-as9.ly"
258 @end example
259
260 Here's an example use for ASCII Art output (the example file
261 @file{as-email.ly} is included in the LilyPond distribution), the staff
262 symbol has been made invisible:
263
264 @example
265 $ lilypond -fas as-email.ly
266 GNU LilyPond 1.3.144
267 Now processing: `as-email.ly'
268 Parsing...
269 Interpreting music...[3]
270 Preprocessing elements... 
271 Calculating column positions... [2]
272 paper output to as-email.as...
273
274 $ as2text as-email.as 2>/dev/null
275           |\
276           |/     |##|##|      |  |  |  |  |
277          /|      |  |  | |    |\ |\ |\ |\ |\ |
278         / |_  3  |  |  | | 5  | )| )| )| )| )|
279        | /| \ 8 *  *  *  | 8 *  *  *  *  *   |
280         \_|_/            |                   |
281         *_|
282
283                                                lily
284 @end example
285
286
287 @refbugs
288
289 The ASCII Art fonts are far from complete and not very well designed.
290 It's easy to change the glyphs, though; if you think you can do better,
291 have a look at @file{mf/*.af}.
292
293 Lots of resizable symbols such as slurs, ties and tuplets are missing.
294
295 The poor looks of most ASCII Art output and its limited general
296 usefulness gives ASCII Art output a low priority; it may be
297 dropped in future versions.
298
299
300 @node Sketch output
301 @subsection Sketch output
302
303 @uref{http://sketch.sourceforge.net,Sketch} is a Free vector drawing
304 program. LilyPond includes bare bones output for Sketch version
305 0.7.
306
307 @cindex Sketch
308 @cindex vector drawing
309 @cindex drawing program
310
311
312 @node Interpretation context
313 @section Interpretation context
314
315 @menu
316 * Creating contexts::           
317 * Default contexts::            
318 * Context properties::          
319 * Engravers and performers::    
320 * Changing context definitions::  
321 * Defining new contexts::       
322 @end menu
323
324
325 Interpretation contexts are objects that only exist during a run of
326 LilyPond.  During the interpretation phase of LilyPond (when it prints
327 "interpreting music"), the music expression in a @code{\score} block is
328 interpreted in time order. This is the same order that humans hear and
329 play the music.
330
331 During this interpretation, the interpretation context holds the
332 state for the current point within the music. It contains information
333 like
334
335 @itemize @bullet
336   @item What notes are playing at this point?
337   @item What symbols will be printed at this point?
338   @item What is the current key signature, time signature, point within
339        the measure, etc.?
340 @end itemize
341
342 Contexts are grouped hierarchically: A @internalsref{Voice} context is
343 contained in a @internalsref{Staff} context (because a staff can contain
344 multiple voices at any point), a @internalsref{Staff} context is contained in
345 @internalsref{Score}, @internalsref{StaffGroup}, or @internalsref{ChoirStaff} context.
346
347 Contexts associated with sheet music output are called @emph{notation
348 contexts}, those for sound output are called @emph{performance
349 contexts}. The default definitions of the standard notation and
350 performance contexts can be found in @file{ly/engraver-init.ly} and
351 @file{ly/performer-init.ly}, respectively.
352
353 @node Creating contexts
354 @subsection Creating contexts
355
356 @cindex @code{\context}
357 @cindex context selection
358
359 Contexts for a music expression can be selected manually, using the
360 following music expression.
361
362 @example
363   \context @var{contexttype} [= @var{contextname}] @var{musicexpr}
364 @end example
365
366 This instructs lilypond to interpret @var{musicexpr} within the context
367  of type @var{contexttype} and with name @var{contextname}.  If this
368 context does not exist, it will be created.  
369
370 @lilypond[verbatim,singleline]
371 \score {
372   \notes \relative c'' {
373     c4 <d4 \context Staff = "another" e4> f
374   }
375 }
376
377 @end lilypond
378
379 In this example, the @code{c} and @code{d} are printed on the
380 default staff.  For the @code{e}, a context Staff called
381 @code{another} is specified; since that does not exist, a new
382 context is created.  Within @code{another}, a (default) Voice context
383 is created for the @code{e4}.  When all music referring to a
384 context is finished, the context is ended as well.  So after the
385 third quarter, @code{another} is removed.
386
387
388
389 @node Default contexts
390 @subsection Default contexts
391
392 Most music expressions don't need an explicit @code{\context}
393 declaration: they inherit the 
394 notation context from their parent. Each note is a music expression, and
395 as you can see in the following example, only the sequential music
396 enclosing the three notes has an explicit context. 
397
398 @lilypond[verbatim,singleline]
399 \score { \notes \context Voice = goUp { c'4 d' e' } } 
400 @end lilypond
401
402 There are some quirks that you must keep in mind when dealing with
403 defaults:
404
405 First, every top level music is interpreted by the Score context, in other
406 words, you may think of @code{\score} working like
407 @example
408         \score @{
409                 \context Score @var{music}
410         @}
411 @end example
412
413 Second, contexts are created automatically to be able to interpret the
414 music expressions. Consider the following example.
415
416 @lilypond[verbatim, singleline]
417 \score { \context Score \notes { c'4 (  d' )e' } }
418 @end lilypond
419
420 The sequential music is interpreted by the Score context initially
421 (notice that the @code{\context} specification is redundant), but when a
422 note is encountered, contexts are setup to accept that note. In this
423 case, a Thread, Voice and Staff are created. The rest of the sequential
424 music is also interpreted with the same Thread, Voice and Staff context,
425 putting the notes on the same staff, in the same voice.
426
427 This is a convenient mechanism, but do not expect opening chords to work
428 without @code{\context}. For every note, a separate staff is
429 instantiated.
430
431 @cindex explicit context
432 @cindex starting with chords
433 @cindex chords, starting with
434
435 @lilypond[verbatim, singleline]
436 \score { \notes <c'4 es'> } 
437 @end lilypond
438
439 Of course, if the chord is preceded by a normal note in sequential
440 music, the chord will be interpreted by the Thread of the preceding
441 note:
442 @lilypond[verbatim,singleline]
443 \score { \notes { c'4 <c'4 es'> }  }
444 @end lilypond
445
446
447
448 @node Context properties
449 @subsection Context properties
450
451 Notation contexts have properties. These properties are from
452 the @file{.ly} file using the following  expression:
453 @cindex @code{\property}
454 @example
455   \property @var{contextname}.@var{propname} =  @var{value}
456 @end example
457
458 Sets the @var{propname} property of the context @var{contextname} to the
459 specified Scheme expression @var{value}.  All @var{propname} and
460 @var{contextname} are strings, which are typically unquoted.
461
462 Properties that are set in one context are inherited by all of the
463 contained contexts.  This means that a property valid for the
464 @internalsref{Voice} context can be set in the @internalsref{Score} context (for
465 example) and thus take effect in all @internalsref{Voice} contexts.
466
467 Properties can be unset using the following expression:
468 @example
469   \property @var{contextname}.@var{propname} \unset
470 @end example
471
472 @cindex properties, unsetting
473 @cindex @code{\unset} 
474
475 This removes the definition of @var{propname} in @var{contextname}. If
476 @var{propname} was not defined in @var{contextname} (but was inherited
477 from a higher context), then this has no effect.
478
479
480 @refbugs
481
482 The syntax of @code{\unset} is asymmetric: @code{\property \unset} is not
483 the inverse of @code{\property \set}.
484
485 @node Engravers and performers
486 @subsection Engravers and performers
487
488 [TODO]
489
490 Basic building blocks of translation are called engravers; they are
491 special C++ classes.
492
493
494
495 @node Changing context definitions
496 @subsection Changing context definitions
497
498 @cindex context definition
499 @cindex translator definition
500
501 The most common way to define a context is by extending an existing
502 context.  You can change an existing context from the paper block, by
503 first initializing a translator with an existing context identifier:
504 @example
505 \paper @{
506   \translator @{
507     @var{context-identifier}
508   @} @}
509 @end example
510 Then you can add and remove engravers using the following syntax:
511 @example
512  \remove @var{engravername}
513  \consists @var{engravername}
514 @end example
515
516
517 Here @var{engravername} is a string, the name of an engraver in the
518 system.
519
520
521 @lilypond[verbatim,singleline]
522 \score {  \notes {
523         c'4 c'4 }
524   \paper {
525     \translator  { \StaffContext
526         \remove Clef_engraver
527        } } }
528 @end lilypond
529
530 @cindex engraver
531
532 You can also set properties in a translator definition. The syntax is as
533 follows:
534 @example
535  @var{propname} = @var{value}
536  @var{propname} \set  @var{grob-propname} = @var{pvalue}
537  @var{propname} \override @var{grob-propname} =  @var{pvalue}
538  @var{propname} \revert @var{grob-propname} 
539 @end example
540 @var{propname} is a string, @var{grob-propname} a symbol, @var{value}
541 and @code{pvalue} are Scheme expressions. These type of property
542 assignments happen before interpretation starts, so a @code{\property}
543 command will override any predefined settings.
544
545
546  To simplify editing translators, all standard contexts have standard
547 identifiers called @var{name}@code{Context}, e.g. @code{StaffContext},
548 @code{VoiceContext}, see @file{ly/engraver-init.ly}.
549
550 @node Defining new contexts
551 @subsection Defining new contexts
552
553 If you want to build a context from scratch, you must also supply the
554 following extra information:
555 @itemize @bullet
556   @item  A name, specified by @code{\name @var{contextname}}.
557
558   @item A cooperation module. This is specified by   @code{\type
559 @var{typename}}.
560 @end itemize
561
562 This is an example:
563 @example
564 \translator @code{
565   \type "Engraver_group_engraver"
566   \name "SimpleStaff"
567   \alias "Staff"
568   \consists "Staff_symbol_engraver"
569   \consists "Note_head_engraver"
570   \consistsend "Axis_group_engraver"
571 }@
572 @end example
573
574 The argument of @code{\type} is the name for a special engraver that
575 handles cooperation between simple engravers such as
576 @code{Note_head_engraver} and @code{Staff_symbol_engraver}. Alternatives
577 for this engraver are the following:
578 @table @code
579 @cindex @code{Engraver_group_engraver}
580   @item @code{Engraver_group_engraver}  
581     The standard cooperation engraver.
582
583 @cindex @code{Score_engraver}
584
585   @item @code{Score_engraver}  
586     This is cooperation module that should be in the top level context,
587 and only the top level context.
588
589 @end table 
590
591 Other modifiers   are
592
593 @itemize @bullet
594   @item @code{\alias} @var{alternate-name}
595     This specifies a different name. In the above example,
596 @code{\property Staff.X = Y} will also work on @code{SimpleStaff}s
597
598   @item  @code{\consistsend} @var{engravername} 
599     Analogous to @code{\consists}, but makes sure that
600     @var{engravername} is always added to the end of the list of
601     engravers.
602
603     Some engraver types need to be at the end of the list; this
604     insures they stay there even if a user adds or removes engravers.
605 End-users generally don't need this command.
606     
607   @item  @code{\accepts} @var{contextname}
608     Add @var{contextname} to the list of contexts this context can
609     contain in the context hierarchy. The first listed context is the
610     context to create by default.
611
612   @item @code{\denies}. The opposite of @code{\accepts}. Added for
613 completeness, but is never used in practice.
614  
615   
616   @item  @code{\name} @var{contextname} 
617     This sets the type name of the context, e.g. @internalsref{Staff},
618     @internalsref{Voice}.  If the name is not specified, the translator won't do
619     anything. 
620 @end itemize
621
622 In the @code{\paper} block, it is also possible to define translator
623 identifiers.  Like other block identifiers, the identifier can only
624 be used as the very first item of a translator.  In order to define
625 such an identifier outside of @code{\score}, you must do
626
627 @quotation
628 @example 
629 \paper @{
630   foo = \translator @{ @dots{} @}
631 @}
632 \score @{
633   \notes @{
634     @dots{}
635   @}
636   \paper @{
637     \translator @{ \foo @dots{} @}
638   @}
639 @} 
640 @end example 
641
642 @end quotation
643
644
645 @cindex paper types, engravers, and pre-defined translators
646
647       
648 @node Syntactic details
649 @section Syntactic details
650 @cindex Syntactic details
651
652 This section describes details that were too boring to be put elsewhere.
653
654 @menu
655 * Identifiers::                 
656 * Music expressions::           
657 * Manipulating music expressions::  
658 * Span requests::               
659 * Assignments::                 
660 * Lexical modes::               
661 * Ambiguities::                 
662 @end menu
663
664 @c .  {Identifiers}
665 @node Identifiers
666 @subsection Identifiers
667 @cindex  Identifiers
668
669 @ignore
670  What has this section got to do with identifiers?
671  It seems more appropriate in the introduction to Chapter 4,
672  "Internals".
673
674    /MB
675 @end ignore
676
677 All of the information in a LilyPond input file, is internally
678 represented as a Scheme value. In addition to normal Scheme data types
679 (such as pair, number, boolean, etc.), LilyPond has a number of
680 specialized data types,
681
682 @itemize @bullet
683 @item Input
684 @item c++-function
685 @item Music
686 @item Identifier
687 @item Translator_def
688 @item Duration
689 @item Pitch
690 @item Score
691 @item Music_output_def
692 @item Moment (rational number)
693 @end itemize
694
695 LilyPond also includes some transient object types. Objects of these
696 types are built during a LilyPond run, and do not `exist' per se within
697 your input file. These objects are created as a result of your input
698 file, so you can include commands in the input to manipulate them,
699 during a lilypond run.
700
701 @itemize @bullet
702 @item Grob: short for Graphical object. 
703 @item Molecule: device-independent page output object,
704 including dimensions.  Produced by some Grob functions
705 @item Translator: object that produces audio objects or Grobs. This is
706 not yet user accessible.
707 @item Font_metric: object representing a font.
708 @end itemize
709
710
711 @node Music expressions
712 @subsection Music expressions
713
714 @cindex music expressions
715
716 Music in LilyPond is entered as a music expression.  Notes, rests, lyric
717 syllables are music expressions, and you can combine music expressions
718 to form new ones, for example by enclosing a list of expressions in
719 @code{\sequential @{ @}} or @code{< >}.  In the following example, a
720 compound expression is formed out of the quarter note @code{c} and a
721 quarter note @code{d}:
722
723 @example 
724 \sequential @{ c4 d4 @} 
725 @end example 
726
727 @cindex Sequential music
728 @cindex @code{\sequential}
729 @cindex sequential music
730 @cindex @code{<}
731 @cindex @code{>}
732 @cindex Simultaneous music
733 @cindex @code{\simultaneous}
734
735 The two basic compound  music expressions are simultaneous  and
736 sequential music.
737
738 @example
739   \sequential @code{@{} @var{musicexprlist} @code{@}}
740   \simultaneous @code{@{} @var{musicexprlist} @code{@}}
741 @end example
742 For both, there is a shorthand:
743 @example
744   @code{@{} @var{musicexprlist} @code{@}}
745 @end example
746 for sequential and
747 @example
748   @code{<} @var{musicexprlist} @code{>}
749 @end example
750 for simultaneous music.
751 In principle, the way in which you nest sequential and simultaneous to
752 produce music is not relevant.  In the following example, three chords
753 are expressed in two different ways:
754
755 @lilypond[fragment,verbatim,center]
756   \notes \context Voice {
757     <a c'> <b  d' > <c' e'>
758     < { a b  c' } { c' d' e' } >
759   }
760 @end lilypond
761
762
763 Other compound music expressions include
764 @example
765  \repeat @var{expr}
766  \transpose @var{pitch} @var{expr}
767  \apply @var{func} @var{expr}
768  \context @var{type} = @var{id} @var{expr}
769  \times @var{fraction} @var{expr}
770 @end example
771
772
773 @c . {Manipulating music expressions}
774 @node Manipulating music expressions
775 @subsection  Manipulating music expressions
776
777 The @code{\apply} mechanism gives you access to the internal
778 representation of music. You can write Scheme-functions that operate
779 directly on it. The syntax is 
780 @example
781         \apply #@var{func} @var{music}
782 @end example
783 This means that @var{func} is applied to @var{music}.  The function
784 @var{func} should return a music expression.
785
786 This example replaces the text string of a script. It also shows a dump
787 of the music it processes, which is useful if you want to know more
788 about how music is stored.
789
790 @lilypond[verbatim,singleline]
791 #(define (testfunc x)
792         (if (equal? (ly-get-mus-property x 'text) "foo")
793                 (ly-set-mus-property! x 'text "bar"))
794         ;; recurse
795         (ly-set-mus-property! x 'elements
796           (map testfunc (ly-get-mus-property x 'elements)))
797         (display x)
798         x        
799 )
800 \score { \notes
801   \apply #testfunc { c'4_"foo" }
802
803 @end lilypond
804
805 For more information on what is possible, see the automatically
806 generated documentation.
807
808
809 Directly accessing internal representations is dangerous: the
810 implementation is subject to changes, so you should avoid this feature
811 if possible.
812
813 A final example is a function that reverses a piece of music in time:
814
815 @lilypond[verbatim,singleline]
816 #(define (reverse-music music)
817   (let* ((elements (ly-get-mus-property music 'elements))
818          (reversed (reverse elements))
819          (span-dir (ly-get-mus-property music 'span-direction)))
820     (ly-set-mus-property! music 'elements reversed)
821     (if (dir? span-dir)
822         (ly-set-mus-property! music 'span-direction (- span-dir)))
823     (map reverse-music reversed)
824     music))
825
826 music = \notes { c'4 d'4( e'4 f'4 }
827
828 \score { \context Voice {
829     \music
830     \apply #reverse-music \music
831   }
832 }
833 @end lilypond
834
835 More examples are given in the distributed example files in
836 @code{input/test/}.
837
838 @c .   {Span requests}
839 @menu
840 * Span requests::               
841 @end menu
842
843 @node Span requests
844 @subsection Span requests
845 @cindex Span requests
846
847 Notational constructs that start and end on different notes can be
848 entered using span requests. The syntax is as follows:
849
850
851 @example
852   \spanrequest @var{startstop} @var{type}
853 @end example
854
855
856 @cindex @code{\start}
857 @cindex @code{\stop}
858
859 This defines a spanning request. The @var{startstop} parameter is either
860 -1 (@code{\start}) or 1 (@code{\stop}) and @var{type} is a string that
861 describes what should be started.  Much of the syntactic sugar is a
862 shorthand for @code{\spanrequest}, for example,
863
864 @lilypond[fragment,verbatim,center]
865   c'4-\spanrequest \start "slur"
866   c'4-\spanrequest \stop "slur"
867 @end lilypond
868
869 Among the supported types are @code{crescendo}, @code{decrescendo},
870 @code{beam}, @code{slur}. This is an internal command.  Users are
871 encouraged to use the shorthands which are defined in the initialization
872 file @file{spanners.ly}.
873
874
875 @c .   {Assignments}
876 @node Assignments
877 @subsection Assignments
878 @cindex Assignments
879
880 Identifiers allow objects to be assigned to names during the parse
881 stage.  To assign an identifier, you use @var{name}@code{=}@var{value}
882 and to refer to an identifier, you precede its name with a backslash:
883 `@code{\}@var{name}'.  @var{value} is any valid Scheme value or any of
884 the input-types listed above.  Identifier assignments can appear at top
885 level in the LilyPond file, but also in @code{\paper} blocks.
886
887 An identifier can be created with any string for its name, but you will
888 only be able to refer to identifiers whose names begin with a letter,
889 being entirely alphabetical.  It is impossible to refer to an identifier
890 whose name is the same as the name of a keyword.
891
892 The right hand side of an identifier assignment is parsed completely
893 before the assignment is done, so it is allowed to redefine an
894 identifier in terms of its old value, e.g.
895
896 @example
897 foo = \foo * 2.0
898 @end example
899
900 When an identifier is referenced, the information it points to is
901 copied.  For this reason, an identifier reference must always be the
902 first item in a block.
903 @example
904 \paper  @{
905   foo = 1.0
906   \paperIdent % wrong and invalid
907 @}
908
909 \paper @{
910   \paperIdent % correct
911   foo = 1.0 @}
912 @end example
913
914
915 @c .  {Lexical modes}
916 @node Lexical modes
917 @subsection Lexical modes
918 @cindex Lexical modes
919 @cindex input mode
920 @cindex mode, input 
921 @cindex @code{\notes}
922 @cindex @code{\chords}
923 @cindex @code{\lyrics}
924
925 To simplify entering notes, lyrics, and chords, LilyPond has three
926 special input modes in addition to the default mode: note, lyrics and
927 chords mode.  These input modes change the way that normal, unquoted
928 words are interpreted: for example, the word @code{cis} may be
929 interpreted as a C-sharp, as a lyric syllable `cis' or as a C-sharp
930 major triad respectively.
931
932 A mode switch is entered as a compound music expression
933 @example
934 @code{\notes} @var{musicexpr}
935 @code{\chords} @var{musicexpr}
936 @code{\lyrics} @var{musicexpr}.
937 @end example
938
939 In each of these cases, these expressions do not add anything to the
940 meaning of their arguments.  They just instruct the parser in what mode
941 to parse their arguments.
942
943 Different input modes may be nested.
944
945 @c .  {Ambiguities}
946 @node Ambiguities
947 @subsection Ambiguities
948 @cindex ambiguities
949 @cindex grammar
950
951
952 The grammar contains a number of ambiguities. We hope to resolve them at
953 some time.
954
955 @itemize @bullet
956   @item  The assignment
957
958 @example 
959 foo = bar 
960 @end example 
961          is interpreted as the string identifier assignment. However,
962 it can also be interpreted as making a string identifier @code{\foo}
963 containing @code{"bar"}, or a music identifier @code{\foo} containing
964 the syllable `bar'.  The former interpretation is chosen.
965
966   @item  If you do a nested repeat like
967
968        @quotation
969
970 @example 
971 \repeat @dots{}
972 \repeat @dots{}
973 \alternative 
974 @end example 
975
976        @end quotation
977
978        then it is ambiguous to which @code{\repeat} the
979        @code{\alternative} belongs.  This is the classic if-then-else
980        dilemma.  It may be solved by using braces.
981
982 @end itemize
983
984 @c .  {Lexical details}
985 @node Lexical details
986 @section Lexical details
987
988 Even more boring details, now on lexical side of the input parser.
989
990 @menu
991 * Direct Scheme::               
992 * Reals::                       
993 * Strings::                     
994 @end menu
995
996
997 @node Direct Scheme
998 @subsection Direct Scheme
999
1000 @cindex Scheme
1001 @cindex GUILE
1002 @cindex Scheme, in-line code
1003
1004
1005
1006 @cindex GUILE
1007 @cindex Scheme
1008 @cindex accessing Scheme
1009 @cindex evaluating Scheme
1010 @cindex LISP
1011
1012 LilyPond internally uses GUILE, a Scheme-interpreter. Scheme is a
1013 language from the LISP family. You can learn more about Scheme at
1014 @uref{http://www.scheme.org}. It is used to represent data throughout
1015 the whole program. The hash-sign (@code{#}) accesses GUILE directly: the
1016 code following the hash-sign is evaluated as Scheme.  The boolean value
1017 @var{true} is @code{#t} in Scheme, so for LilyPond @var{true} looks like
1018 @code{##t}.
1019
1020 LilyPond contains a Scheme interpreter (the GUILE library) for
1021 internal use. In some places, Scheme expressions also form valid syntax:
1022 wherever it is allowed,
1023 @example
1024   #@var{scheme}
1025 @end example
1026 evaluates the specified Scheme code.  Example:
1027 @example
1028   \property Staff.TestObject \override #'foobar =  #(+ 1 2)
1029 @end example
1030 @code{\override} expects two Scheme expressions, so there are two Scheme
1031 expressions. The first one is a symbol (@code{foobar}), the second one
1032 an integer (namely, 3).
1033
1034 In-line scheme may be used at the top level. In this case the result is
1035 discarded.
1036
1037 Scheme is a full-blown programming language, and a full discussion is
1038 outside the scope of this document. Interested readers are referred to
1039 the website @uref{http://www.schemers.org/} for more information on
1040 Scheme.
1041
1042
1043 @node Reals
1044 @subsection Reals
1045 @cindex real numbers
1046
1047 Formed from an optional minus sign and a sequence of digits followed
1048 by a @emph{required} decimal point and an optional exponent such as
1049 @code{-1.2e3}.  Reals can be built up using the usual operations:
1050 `@code{+}', `@code{-}', `@code{*}', and
1051 `@code{/}', with parentheses for grouping.
1052
1053 @cindex @code{\mm},
1054 @cindex @code{\in}
1055 @cindex @code{\cm}
1056 @cindex @code{\pt}
1057 @cindex dimensions
1058
1059 A real constant can be followed by one of the dimension keywords:
1060 @code{\mm} @code{\pt}, @code{\in}, or @code{\cm}, for millimeters,
1061 points, inches and centimeters, respectively.  This converts the number
1062 a number that is the internal representation of that dimension.
1063
1064
1065 @node Strings
1066 @subsection Strings
1067 @cindex string
1068 @cindex concatenate
1069
1070 Begins and ends with the @code{"} character.  To include a @code{"}
1071 character in a string write @code{\"}.  Various other backslash
1072 sequences have special interpretations as in the C language.  A string
1073 that contains no spaces can be written without the quotes.  Strings can
1074 be concatenated with the @code{+} operator.
1075