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