]> git.donarmstrong.com Git - lilypond.git/blobdiff - Documentation/user/internals.itely
* Documentation/user/refman.itely: remove superfluous -'s
[lilypond.git] / Documentation / user / internals.itely
index 656b91a444cf630d7ff408a2614cba95f5f80d1b..519569f0b2798e9db583721eecf7ccf856fbd9ec 100644 (file)
 @c before saving changes
 
 
-@node Internals
-@chapter Internals
+@node Technical manual
+@chapter Technical manual
 
 
-When translating the input to notation, there are number of distinct
-phases.  We list them here:
+When LilyPond is run, it reads an input file which is parsed.  During
+parsing, Music objects are created. This music is interpreted, which
+is done by contexts, that produce graphical objects.  This section
+discusses details of these three concepts, and how they are glued
+together with the embedded Scheme interpreter.
+
+@menu
+* Interpretation context::      
+* Scheme integration::          
+* Music storage format::        
+* Lexical details::             
+* Output details::              
+@end menu
 
 
-@c todo: moved from refman. 
+@node Interpretation context
+@section Interpretation context
 
-The purpose of LilyPond is explained informally by the term `music
-typesetter'.  This is not a fully correct name: not only does the
-program print musical symbols, it also makes aesthetic decisions.
-Symbols and their placements are @emph{generated} from a high-level
-musical description.  In other words, LilyPond would be best described
-by `music compiler' or `music to notation compiler'.
+@menu
+* Creating contexts::           
+* Default contexts::            
+* Context properties::          
+* Context evaluation::          
+* Defining contexts::           
+* Engravers and performers::    
+* Defining new contexts::       
+@end menu
 
-LilyPond is linked to GUILE, GNU's Scheme library for extension
-programming. The Scheme library provides the glue that holds together
-the low-level routines and separate modules which are written in C++.
 
-When lilypond is run to typeset sheet music, the following happens:
+Interpretation contexts are objects that only exist during program
+run.  During the interpretation phase (when @code{interpreting music}
+is printed on the standard output), the music expression in a
+@code{\score} block is interpreted in time order, the same order in
+which we hear and play the music.  During this phase, the interpretation
+context holds the state for the current point within the music, for
+example:
 @itemize @bullet
-@item GUILE Initialization: various scheme files are read
-@item parsing: first standard @code{ly} initialization  files are read, and
-then the user @file{ly} file is read.
-@item interpretation: the music in the file is processed ``in playing
-order'', i.e. the order that  you  use to read sheet music, or the
-order in which notes are played. The result of this step is a typesetting
-specification.
-
-@item typesetting:
-The typesetting specification is solved: positions and formatting is
-calculated.
-
-@item the visible results ("virtual ink") are written to the output file.
+@item What notes are playing at this point?
+
+@item What symbols will be printed at this point?
+
+@item What is the current key signature, time signature, point within
+the measure, etc.?
 @end itemize
 
-During these stages different types of data play the the main role:
-during parsing, @strong{Music} objects are created.  During the
-interpretation, @strong{contexts} are constructed, and with these contexts
-a network of @strong{graphical objects} (``grobs'') is created. These
-grobs contain unknown variables, and the network forms a set of
-equations. After solving the equations and filling in these variables,
-the printed output (in the form of @strong{molecules}) is written to an
-output file.
+Contexts are grouped hierarchically: A @internalsref{Voice} context is
+contained in a @internalsref{Staff} context (because a staff can contain
+multiple voices at any point), a @internalsref{Staff} context is contained in
+@internalsref{Score}, @internalsref{StaffGroup}, or
+@internalsref{ChoirStaff} context.
 
-These threemanship of tasks (parsing, translating, typesetting) and
-data-structures (music, context, graphical objects) permeates the entire
-design of the program.
+Contexts associated with sheet music output are called @emph{notation
+contexts}, those for sound output are called @emph{performance
+contexts}.  The default definitions of the standard notation and
+performance contexts can be found in @file{ly/engraver-init.ly} and
+@file{ly/performer-init.ly}, respectively.
 
 
+@node Creating contexts
+@subsection Creating contexts
+@cindex @code{\context}
+@cindex context selection
 
-@table @b
+Contexts for a music expression can be selected manually, using the
+following music expression:
 
-@item Parsing:
+@example
+\context @var{contexttype} [= @var{contextname}] @var{musicexpr}
+@end example
 
-The LY file is read, and converted to a list of @code{Scores}, which
-each contain @code{Music} and paper/midi-definitions. Here @code{Music},
-@code{Pitch} and @code{Duration}  objects are created.
+@noindent
+This means that @var{musicexpr} should be interpreted within a context
+of type @var{contexttype} (with name @var{contextname} if specified).
+If no such context exists, it will be created:
 
-@item Interpreting music
-@cindex interpreting music
+@lilypond[verbatim,singleline]
+\score {
+  \notes \relative c'' {
+    c4 <d4 \context Staff = "another" e4> f
+  }
+}
+@end lilypond
 
-All music events are "read" in the same order as they would be played
-(or read from paper). At every step of the interpretation, musical
-events are delivered to
-interpretation contexts,
-@cindex engraver
-which use them to build @code{Grob}s (or MIDI objects, for MIDI output).
+@noindent
+In this example, the @code{c} and @code{d} are printed on the default
+staff.  For the @code{e}, a context @code{Staff} called @code{another}
+is specified; since that does not exist, a new context is created.
+Within @code{another}, a (default) Voice context is created for the
+@code{e4}.  A context is ended when when all music referring it has
+finished, so after the third quarter, @code{another} is removed.
 
-In this stage @code{Music_iterators} do a traversal of the @code{Music}
-structure. The music events thus encountered are reported to
-@code{Translator}s, a set of objects that collectively form interpretation
-contexts.
 
+@node Default contexts
+@subsection Default contexts
 
-@item Prebreaking
+Every top level music is interpreted by the @code{Score} context; in
+other words, you may think of @code{\score} working like
 
-@cindex prebreaking
+@example
+\score @{
+  \context Score @var{music}
+@}
+@end example
 
-At places where line breaks may occur, clefs and bars are prepared for
-a possible line break. 
+Music expressions  inherit their context from the enclosing music
+expression. Hence, it is not necessary to explicitly specify
+@code{\context} for most expressions.  In
+the following example, only the sequential expression has an explicit
+context. The notes contained therein inherit the @code{goUp} context
+from the enclosing music expression.
 
-@item Preprocessing
+@lilypond[verbatim,singleline]
+  \notes \context Voice = goUp { c'4 d' e' }
+@end lilypond
 
-@cindex preprocessing
 
-In this stage, all information that is needed to determine line breaking
-is computed. 
+Second, contexts are created automatically to be able to interpret the
+music expressions.  Consider the following example:
 
-@item Break calculation:
+@lilypond[verbatim, singleline]
+  \score { \notes { c'4-( d' e'-) } }
+@end lilypond
 
-The lines and horizontal positions of the columns are determined.
+@noindent
+The sequential music is interpreted by the Score context initially,
+but when a note is encountered, contexts are setup to accept that
+note.  In this case, a @code{Thread}, @code{Voice}, and @code{Staff}
+context are created.  The rest of the sequential music is also
+interpreted with the same @code{Thread}, @code{Voice}, and
+@code{Staff} context, putting the notes on the same staff, in the same
+voice.
 
-@item Breaking
+@node Context properties
+@subsection Context properties
 
-Relations between all grobs are modified to reflect line breaks: When a
-spanner, e.g. a slur, crosses a line-break, then the spanner is "broken
-into pieces", for every line that the spanner is in, a copy of the grob
-is made. A substitution process redirects all grob-reference so that
-each spanner grob will only reference other grobs in the same line.
+Contexts have properties.  These properties are set from the @file{.ly}
+file using the following expression:
+@cindex @code{\property}
+@cindex context properties
+@cindex properties, context
 
-@item Outputting:
+@example
+\property @var{contextname}.@var{propname} = @var{value}
+@end example
 
-All vertical dimensions and spanning objects are computed, and all grobs
-are output, line by line. The output is encoded in the form of
-@code{Molecule}s
+@noindent
+Sets the @var{propname} property of the context @var{contextname} to
+the specified Scheme expression @var{value}.  Both @var{propname} and
+@var{contextname} are strings, which can often be written unquoted.
+
+@cindex inheriting
+Properties that are set in one context are inherited by all of the
+contained contexts.  This means that a property valid for the
+@internalsref{Voice} context can be set in the @internalsref{Score} context
+(for example) and thus take effect in all @internalsref{Voice} contexts.
+
+@cindex @code{Current}
+If you do not wish to specify the name of the context in the
+@code{\property}-expression itself, you can refer to the abstract context
+name, @code{Current}.  The @code{Current} context is the latest
+used context.  This will typically mean the @internalsref{Thread} context, 
+but you can force another context with the
+@code{\property}-command.  Hence the expressions
 
-@end table
+@example
+\property @var{contextname}.@var{propname} = @var{value}
+@end example
 
-The data types that are mentioned here are all discussed in this
-section.
+@noindent
+and
 
-@menu
-* Grobs::                       Graphical object  
-* Molecules::                   Molecules are stand-alone descriptions of output
-@end menu
+@example
+\context @var{contextname}
+\property Current.@var{propname} = @var{value}
+@end example
+
+@noindent
+do the same thing.  The main use for this is in predefined variables.
+This construction allows the specification of a property-setting
+without restriction to a specific context.
+
+Properties can be unset using the following statement.
+@example
+\property @var{contextname}.@var{propname} \unset
+@end example
+
+@cindex properties, unsetting
+@cindex @code{\unset} 
+
+@noindent
+This removes the definition of @var{propname} in @var{contextname}.  If
+@var{propname} was not defined in @var{contextname} (but was inherited
+from a higher context), then this has no effect.
 
-@node Grobs
-@section Grobs
+@refbugs
 
-This section is about Grobs (short for Graphical Objects), which are
-formatting objects used to create the final output. This material is
-normally the domain of LilyPond gurus, but occasionally, a normal user
-also has to deal with grobs.
+The syntax of @code{\unset} is asymmetric: @code{\property \unset} is not
+the inverse of @code{\property \set}.
 
-The most simple interaction with Grobs are when you use
-@code{\override}:
 
+@node Context evaluation
+@subsection Context evaluation
+
+Contexts can be modified during interpretation with Scheme code. The
+syntax for this is
 @example
-        \property Voice.Stem \override #'direction = #1
+  \applycontext @var{function}
 @end example
 
-This piece of lily input causes all stem objects to be stem-up
-henceforth.  In effect, you are telling lilypond to extend the definition
-of the `Stem' grob with the setting @code{direction := 1}.
+@var{function} should be a Scheme function taking a single argument,
+being the context to apply it to. The following code will print the
+current bar number on the standard output during the compile:
 
-@menu
-* What is a grob?::             
-* Callbacks::                   
-* Setting grob properties::     
-* Grob interfaces::             
-* Items and Spanners::          
-* Grob Scheme functions::       
-@end menu
+@example
+    \applycontext
+      #(lambda (x)
+         (format #t "\nWe were called in barnumber ~a.\n"
+          (ly:get-context-property x 'currentBarNumber)))
+@end example
+
+
+
+@node Defining contexts
+@subsection Defining contexts
 
+@cindex context definition
+@cindex translator definition
 
+The most common way to create a new context definition is by extending
+an existing one.  An existing context from the paper block is copied
+by referencing a context identifier:
 
-@node What is a grob?
-@subsection What is a grob?
+@example
+\paper @{
+  \translator @{
+    @var{context-identifier}
+  @}
+@}
+@end example
 
-In music notation, lots of symbols are related in some way.  You can
-think of music notation as a graph where nodes are formed by the
-symbols, and the arcs by their relations. A grob is a node in that graph.
-The directed edges in the graph are formed by references to other grobs
-(i.e. pointers).
-This big graph of grobs specifies the notation problem. The solution of
-this problem is a description of the printout in closed form, i.e. a
-list of values.  These values are Molecules. (see @ref{Molecules})
+@noindent
+Every predefined context has a standard identifier. For example, the
+@code{Staff} context can be referred to as @code{\StaffContext}.
 
-All grobs have an X and Y-position on the page.  These X and Y positions
-are stored in a relative format, so they can easily be combined by
-stacking them, hanging one grob to the side of another, and coupling
-them into a grouping-grob.
+The context can then be modified by setting or changing properties,
+e.g.
+@example
+\translator @{
+  \StaffContext
+  Stem \set #'thickness = #2.0
+  defaultBarType = #"||"
+@}
+@end example
+These assignments happen before interpretation starts, so a @code{\property}
+command will override any predefined settings.
 
-Each grob has a reference point (a.k.a.  parent): the position of a grob
-is stored relative to that reference point. For example the X-reference
-point of a staccato dot usually is the note head that it applies
-to. When the note head is moved, the staccato dot moves along
-automatically.
+@cindex engraver
 
-If you keep following offset reference points, you will always end up at
-the root object. This root object is called @code{Line_of_score}, and it
-represents a system (i.e. a line of music).
+@refbugs
 
-All grobs carry a set of grob-properties.  In the Stem example above,
-the property @code{direction} is set to value @code{1}.  The function
-that draws the symbol (@code{Stem::brew_molecule}) uses the value of
-@code{direction} to determine how to print the stem and the flag.  The
-appearance of a grob is determined solely by the values of its
-properties.
+It is not possible to collect multiple property assignments in a
+variable, and apply to one @code{\translator} definition by
+referencing that variable.
 
-A grob is often associated with a symbol, but some grobs do not print
-any symbols. They take care of grouping objects. For example, there is a
-separate grob that stacks staves vertically. The @code{NoteCollision}
-is also an abstract grob: it only moves around chords, but doesn't print
-anything.
+@node Engravers and performers
+@subsection  Engravers and performers
 
-A complete list of grob types is found in the generated documentation.
 
+Each context is composed of a number of building blocks, or plug-ins
+called engravers.  An engraver is a specialized C++ class that is
+compiled into the executable. Typically, an engraver is responsible
+for one function: the @code{Slur_engraver} creates only @code{Slur}
+objects, and the @code{Skip_event_swallow_translator} only swallows
+(silently gobbles) @code{SkipEvent}s.
 
-@node Callbacks
-@subsection Callbacks
 
-Offsets of grobs are relative to a parent reference point. Most
-positions are not known when an object is created, so these are
-calculated as needed. This is done by adding a callback for a specific
-direction.
 
-Suppose you have the following code in a .ly file.
+@cindex engraver
+@cindex plug-in
+
+An existing context definition can be changed by adding or removing an
+engraver. The syntax for these operations is 
 @example
-        #(define (my-callback gr axis)
-                (*  2.0 (get-grob-property gr 'direction))
-        )
+\consists @var{engravername}
+\remove @var{engravername}
+@end example
 
-....
+@cindex \consists
+@cindex \remove
+
+@noindent
+Here @var{engravername} is a string, the name of an engraver in the
+system. In the following example, the @code{Clef_engraver} is removed
+from the Staff context. The result is a staff without a clef, where
+the central C is at its default position, the center line:
+
+@lilypond[verbatim,singleline]
+\score {
+  \notes {
+    c'4 f'4
+  }
+  \paper {
+    \translator {
+      \StaffContext
+      \remove Clef_engraver
+    }
+  }
+}
+@end lilypond
+
+A list of all engravers is in the internal documentation,
+see @internalsref{All engravers}.
+
+@node Defining new contexts
+@subsection Defining new contexts
+
+
+It is also possible to define new contexts from scratch.  To do this,
+you must define give the new context a name.  In the following
+example, a very simple Staff context is created: one that will put
+note heads on a staff symbol.
 
-        \property Voice.Stem \override #'Y-offset-callbacks = #(list
-                        my-callback)
+@example
+\translator @{
+  \type "Engraver_group_engraver"
+  \name "SimpleStaff"
+  \alias "Staff"
+  \consists "Staff_symbol_engraver"
+  \consists "Note_head_engraver"
+  \consistsend "Axis_group_engraver"
+@}
 @end example
 
-When the Y-offset of a Stem object is needed, LilyPond will
-automatically execute all callbacks for that object. In this case, it
-will find @code{my-callback}, and execute that. The result is that the
-stem is translated by two staff spaces in its direction.
+@noindent
+The argument of @code{\type} is the name for a special engraver that
+handles cooperation between simple engravers such as
+@code{Note_head_engraver} and @code{Staff_symbol_engraver}.  This
+should always be  @code{Engraver_group_engraver} (unless you are
+defining a Score context from scratch, in which case
+@code{Score_engraver}   must be used).
 
-(note: @code{Y-offset-callbacks} is also a property)
+The complete list of context  modifiers is the following:
+@itemize @bullet
+@item @code{\alias} @var{alternate-name}:
+This specifies a different name.  In the above example,
+@code{\property Staff.X = Y} will also work on @code{SimpleStaff}s.
+
+@item @code{\consistsend} @var{engravername}:
+Analogous to @code{\consists}, but makes sure that
+@var{engravername} is always added to the end of the list of
+engravers.
+
+Engravers that group context objects into axis groups or alignments
+need to be at the end of the list. @code{\consistsend} insures that
+engravers stay at the end even if a user adds or removes engravers.
+    
+@item @code{\accepts} @var{contextname}:
+This context can contains @var{contextname} contexts.  The first
+@code{\accepts} is created as a default context when events (e.g. notes
+or rests) are encountered.
+
+@item @code{\denies}:
+The opposite of @code{\accepts}.
+
+@item @code{\name} @var{contextname}:
+This sets the type name of the context, e.g. @code{Staff},
+@code{Voice}.  If the name is not specified, the translator will not
+do anything.
+@end itemize
+
+
+@node Scheme integration
+@section Scheme integration
+
+@cindex Scheme
+@cindex GUILE
+@cindex Scheme, in-line code
+@cindex accessing Scheme
+@cindex evaluating Scheme
+@cindex LISP
 
+LilyPond internally uses GUILE, a Scheme-interpreter, to represent
+data throughout the whole program, and glue together different program
+modules. For advanced usage, it is sometimes necessary to access and
+program the Scheme interpreter.
 
+Scheme is a full-blown programming language, from the LISP
+family. and a full discussion is outside the scope of this document.
+Interested readers are referred to the website
+@uref{http://www.schemers.org/} for more information on Scheme.
+
+The GUILE library for extension is documented at
+@uref{http://www.gnu.org/software/guile}.
+@ifinfo
+When it is installed, the following link should take you to its manual
+@ref{(guile.info)guile}
+@end ifinfo
+
+@menu
+* Inline Scheme::               
+* Input variables and Scheme::  
+* Scheme datatypes::            
+* Assignments::                 
+@end menu
 
-Offset callbacks can be stacked, i.e.
+@node Inline Scheme
+@subsection Inline Scheme
 
+Scheme expressions can be entered in the input file by entering a
+hash-sign (@code{#}).  The expression following the hash-sign is
+evaluated as Scheme. For example, the boolean value @var{true} is
+@code{#t} in Scheme, so for LilyPond @var{true} looks like @code{##t},
+and can be used in property assignments:
 @example
-        \property .... \override #'Y-offset-callbacks = #(list
-                callback1 callback2 callback3)
+  \property Staff.autoBeaming = ##f
+@end example
+
+
+@node Input variables and Scheme
+@subsection Input variables and Scheme
 
+
+The input format supports the notion of variable: in the following
+example, a music expression is assigned to a variable with the name
+@code{traLaLa}.
+@example
+  traLaLa = \notes @{ c'4 d'4 @}
 @end example
 
-The callbacks will be executed in the order @code{callback3 callback2
-callback1}. This is used for quantized positioning: the staccato dot is
-above or below a note head, and it must not be on a staff-line.  To
-achieve this, the staccato dot has two callbacks: one that positions the
-grob above or below the note head, and one that rounds the Y-position of
-the grob to the nearest open space.
+@noindent
 
-Similarly, the size of a grob are determined through callbacks, settable
-with grob properties @code{X-extent-callback} and
-@code{Y-extent-callback}.  There can be only one extent-callback for
-each axis. No callback (Scheme value @code{#f}) means: "empty in this
-direction". If you fill in a pair of numbers, that pair hard-codes the
-extent in that coordinate.
+There is also a form of scoping: in the following example, the
+@code{\paper} block also contains a @code{traLaLa} variable, which is
+independent of the outer @code{\traLaLa}.
+@example
+  traLaLa = \notes @{ c'4 d'4 @}
+  \paper @{ traLaLa = 1.0 @}
+@end example
+@c
+In effect, each input file is a scope, and all @code{\header},
+@code{\midi} and @code{\paper} blocks are scopes nested inside that
+toplevel scope.
 
+Both variables and scoping are implemented in the GUILE module system.
+An anonymous Scheme module is attached to each scope. An assignment of
+the form
+@example
+ traLaLa = \notes @{ c'4 d'4 @} 
+@end example
 
-@node Setting grob properties
-@subsection Setting grob properties
+@noindent
+is internally converted to a Scheme definition
+@example
+ (define traLaLa @var{Scheme value of ``@code{\notes ... }''})
+@end example
 
-Grob properties are stored as GUILE association lists, with symbols as
-keys.  In GUILE you can access these using functions described in
-Section @ref{Grob Scheme functions}.  From C++, grob properties can be
-accessed using these functions:
+This means that input variables and Scheme variables may be freely
+mixed.  In the following example, a music fragment is stored in the
+variable @code{traLaLa}, and duplicated using Scheme. The result is
+imported in a @code{\score} by means of a second variable
+@code{twice}:
+@example
+  traLaLa = \notes @{ c'4 d'4 @}
+  
+  #(define newLa (map ly:music-deep-copy
+    (list traLaLa traLaLa)))
+  #(define twice
+    (make-sequential-music newLa))
+
+  \score @{ \twice @}
+@end example
 
+In the above example, music expressions can be `exported' from the
+input to the Scheme interpreter. The opposite is also possible. By
+wrapping a Scheme value in the function @code{ly:export}, a Scheme
+value is interpreted as if it were entered in LilyPond syntax: instead
+of defining @code{\twice}, the example above could also have been
+written as
 @example
-  SCM  get_grob_property (SCM) const;
-  void set_grob_property (const char * , SCM val);
-  void set_immutable_grob_property (const char * , SCM val);
-  void set_immutable_grob_property (SCM key, SCM val);  
-  void set_grob_property (SCM , SCM val);  
-  void set_grob_pointer (const char*, SCM val);
-  SCM  remove_grob_property (const char* nm);
+  @dots{}
+  \score @{ #(ly:export (make-sequential-music newLa)) @}
 @end example
 
-All lookup functions identify undefined properties with end-of-list
-(i.e. @code{'()} in Scheme or @code{SCM_EOL} in C)
 
-Properties are stored in two ways:
-@itemize @bullet
-@item mutable properties.
-Grob properties that change from object to object. The storage of
-these are private to a grob. For example pointers to other grobs are
-always stored in the mutable properties.
-
-@item immutable properties.
-Grob properties that are shared across different grobs of the same
-type. The storage is shared, and hence it is read-only. Typically, this
-is used to store function callbacks, and default settings. They are
-initially read from @file{scm/grob-description.scm}.
-@end itemize
 
-You can change immutable grob properties with the \override syntax:
+
+
+@node Scheme datatypes
+@subsection Scheme datatypes
+
+Scheme is used to glue together different program modules. To aid this
+glue function, many LilyPond specific object types can be passed as
+Scheme value. 
+
+The following list are all LilyPond specific types, that
+can exist during parsing:
+@table @code
+@item Duration
+@item Input
+@item Moment
+@item Music
+@item Event
+In C++ terms, an @code{Event} is a subtype of @code{Music}. However,
+both have different functions in the syntax.
+@item Music_output_def
+@item Pitch
+@item Score
+@item Translator_def
+@end table
+
+
+During a run, transient objects are also created and destroyed.
+
+@table @code
+@item Grob: short for `Graphical object'.
+@item Scheme_hash_table 
+@item Music_iterator
+
+@item Molecule: Device-independent page output object,
+including dimensions.  
+
+@item Syllable_group
+
+@item Spring_smob
+
+@item Translator: An object that produces audio objects or Grobs.
+It may be accessed with @code{\applyoutput}.
+
+@item Font_metric: An object representing a font.
+@end table
+
+Many functions are defined to manipulate these data structures. They
+are all listed and documented in the internals manual, see
+@internalsref{All scheme functions}.
+
+
+@node Assignments
+@subsection Assignments
+@cindex Assignments
+
+Variables allow objects to be assigned to names during the parse
+stage.  To assign a variable, use
+@example
+@var{name}@code{=}@var{value}
+@end example
+To refer to a variable, precede its name with a backslash:
+`@code{\}@var{name}'.  @var{value} is any valid Scheme value or any of
+the input-types listed above.  Variable assignments can appear at top
+level in the LilyPond file, but also in @code{\paper} blocks.
+
+A variable can be created with any string for its name, but for
+accessing it in the LilyPond syntax, its name must consist of
+alphabetic characters only, and may not be a keyword of the syntax.
+There are no restrictions for naming and accessing variables in the
+Scheme interpreter,
+
+The right hand side of a variable assignment is parsed completely
+before the assignment is done, so variables may be  redefined in terms
+of its old value, e.g.
+@c
+@example
+foo = \foo * 2.0
+@end example
+
+When a variable is referenced in LilyPond syntax, the information it
+points to is copied.  For this reason, an variable reference must
+always be the first item in a block.
+
+@example
+\paper @{
+  foo = 1.0
+  \paperIdent % wrong and invalid
+@}
+@end example
+
+@example
+\paper @{
+  \paperIdent % correct
+  foo = 1.0
+@}
+@end example
+
+      
+
+@node Music storage format
+@section Music storage format
+
+Music in LilyPond is entered as music expressions. This section
+discusses different types of music expressions, and explains how
+information is stored internally. This internal storage is accessible
+through the Scheme interpreter, so music expressions may be
+manipulated using Scheme functions. 
+
+@menu
+* Music expressions::           
+* Internal music representation::  
+* Manipulating music expressions::  
+@end menu
+
+@node Music expressions
+@subsection Music expressions
+@cindex music expressions
+
+Notes, rests, lyric syllables are music expressions.  Small music
+expressions may be combined to form larger ones, for example, by
+enclosing a list of expressions in @code{\sequential @{ @}} or @code{<
+>}.  In the following example, a compound expression is formed out of
+the quarter note @code{c} and a quarter note @code{d}:
+
+@example 
+\sequential @{ c4 d4 @} 
+@end example 
+
+@cindex Sequential music
+@cindex @code{\sequential}
+@cindex sequential music
+@cindex @code{<}
+@cindex @code{>}
+@cindex Simultaneous music
+@cindex @code{\simultaneous}
+
+The two basic compound music expressions are simultaneous and
+sequential music:
 
 @example
-        \property Voice.Stem \override #'direction = #1
+\sequential @code{@{} @var{musicexprlist} @code{@}}
+\simultaneous @code{@{} @var{musicexprlist} @code{@}}
 @end example
 
-This will push the entry @code{'(direction . 1)} on the immutable
-property list for stems, in effect overriding the setting from
-@file{scm/grob-description.scm}. This can be undone by 
+For both, there is a shorthand:
 
 @example
-        \property Voice.stem \revert #'direction
+@code{@{} @var{musicexprlist} @code{@}}
 @end example
 
-There is also a shorthand,
+@noindent
+for sequential and
 
 @example
-        \property Context.GrobType \set #'prop = #VAL
+@code{<} @var{musicexprlist} @code{>}
 @end example
 
-this does a @code{\revert} followed by a @code{\override}
+@noindent
+for simultaneous music.
+In principle, the way in which you nest sequential and simultaneous to
+produce music is not relevant.  In the following example, three chords
+are expressed in two different ways:
+
+@lilypond[fragment,verbatim,center,quote]
+\notes \context Voice {
+  <a c'> <b d'> <c' e'>
+  < { a b c' } { c' d' e' } >
+}
+@end lilypond
+However, using @code{<} and @code{>} for entering chords leads to
+various peculiarities. For this reason, a special syntax
+for chords was introduced in version 1.7: @code{<< >>}.
+
+
+
 
-You can change mutable properties with \outputproperty. This construct
-looks like
 
+Other compound music expressions include:
 @example
-        \context ContextName \outputproperty @var{pred} #@var{sym} = #@var{val}
+\repeat @var{expr}
+\transpose @var{from} @var{to} @var{expr}
+\apply @var{func} @var{expr}
+\context @var{type} = @var{id} @var{expr}
+\times @var{fraction} @var{expr}
 @end example
 
-In this case, in every grob that satisfies @var{pred}, the grob property
- @var{sym} is set to @var{val}.  For example
+@node Internal music representation
+@subsection Internal music representation
+
+
+
+
+
+
+When a music expression is parsed, it is converted into a set of
+Scheme music objects. The defining property of a music object is that
+it takes up time. Time is a rational number that measures the length
+of a piece of music, in whole notes.
+
+A music object has three kinds of types:
+@itemize @bullet
+@item
+  music name: Each music expression has a name, for example, a note
+leads to a @internalsref{NoteEvent}, and @code{\simultaneous} leads to
+a @internalsref{SimultaneousMusic}. A list of all expressions
+available is in the internals manual, under @internalsref{Music
+expressions}.
+
+@item
+  `type' or interface: Each music name has several `types' or interface,
+  for example, a note is an @code{event}, but it is also a @code{note-event}, 
+  a @code{rhythmic-event} and a @code{melodic-event}.
+
+  All classes of music are listed in the internals manual, under
+  @internalsref{Music classes}. 
+@item
+C++ object: Each music object is represented by a C++ object. For technical
+reasons, different music objects may be represented by different C++
+object types. For example, a note is @code{Event} object, while
+@code{\grace} creates a @code{Grace_music} object.
+
+We expect that distinctions between different C++ types will disappear
+in the future.
+@end itemize
 
+The actual information of a music expression is stored in properties.
+For example, a @internalsref{NoteEvent} has @code{pitch} and
+@code{duration} properties that store the pitch and duration of that
+note.  A list of all properties available is in the internals manual,
+under @internalsref{Music properties}.
+
+A compound music expression is a music object that contains other
+music objects in its properties. A list of objects can be stored in
+the @code{elements} property of a music object, or a single `child'
+music object in the @code{element} object. For example,
+@internalsref{SequentialMusic} has its children in @code{elements},
+and @internalsref{GraceMusic} has its single argument in
+@code{element}. The body of a repeat is in @code{element} property of
+@internalsref{RepeatedMusic}, and the alternatives in @code{elements}.
+
+@node Manipulating music expressions
+@subsection Manipulating music expressions
+
+Music objects and their properties can be accessed and manipulated
+directly, through the @code{\apply} mechanism.  
+The syntax for @code{\apply} is 
 @example
-        \outputproperty
-                #(lambda (gr) (string? (ly-get-grob-property gr
-                        'text)))
-                #'extra-offset = #'(-1.0 . 0.0)
+\apply #@var{func} @var{music}
 @end example
 
-This shifts all grobs that have a @code{text} property one staff
-space to the left. This mechanism is rather clumsy to use, but it allows
-you tweak any setting of any grob.
+@noindent
+This means that the scheme function @var{func} is called with
+@var{music} as its argument.  The return value of @var{func} is the
+result of the entire expression.  @var{func} may read and write music
+properties using the functions @code{ly:get-mus-property} and
+@code{ly:set-mus-property!}.
+
+An example is a function that reverses the order of elements in
+its argument:
+@lilypond[verbatim,singleline]
+  #(define (rev-music-1 m)
+     (ly:set-mus-property! m 'elements (reverse
+       (ly:get-mus-property m 'elements)))
+     m)
+  \score {  \notes \apply #rev-music-1 { c4 d4 } }
+@end lilypond
+
+The use of such a function is very limited. The effect of this
+function is void when applied to an argument which is does not have
+multiple children.  The following function application has no effect:
 
+@example
+  \apply #rev-music-1 \grace @{ c4 d4 @}
+@end example
 
-@node Grob interfaces
-@unnumberedsubsec Grob interfaces
+@noindent
+In this case, @code{\grace} is stored as @internalsref{GraceMusic}, which has no
+@code{elements}, only a single @code{element}. Every generally
+applicable function for @code{\apply} must -- like music expressions
+themselves -- be recursive.
 
-Grob properties form a name space where you can set variables per
-object.  Each object however, may have multiple functions. For example,
-consider a dynamic symbol, such @code{\ff} (fortissimo). It is printed
-above or below the staff, it is a dynamic sign, and it is a kind of
-text.
+The following example is such a recursive function: It first extracts
+the @code{elements} of an expression, reverses them and puts them
+back. Then it recurses, both on @code{elements} and @code{element}
+children.
+@example
+#(define (reverse-music music)
+  (let* ((elements (ly:get-mus-property music 'elements))
+         (child (ly:get-mus-property music 'element))
+         (reversed (reverse elements)))
+
+    ; set children
+    (ly:set-mus-property! music 'elements reversed)
+
+    ; recurse
+    (if (ly:music? child) (reverse-music child))
+    (map reverse-music reversed)
+    
+    music))
+@end example
 
-To reflect this different functions of a grob, procedures and variables
-are grouped into so-called interfaces.  The dynamic text for example
-supports the  following interfaces:
-@table @code 
-@item font-interface
-  The glyph is built from characters from a font, hence the
-@code{font-interface}. For objects supporting @code{font-interface}, you
-can select alternate fonts by setting @code{font-style},
-@code{font-point-size}, etc.
+A slightly more elaborate example is in
+@inputfileref{input/test,reverse-music.ly}.
 
-@item dynamic-interface
-  Dynamic interface is not associated with any variable or function in
-particular, but this makes it possible to distinguish this grob from
-other similar grobs (like @code{TextScript}), that have no meaning of
-dynamics.
+Some of the input syntax is also implemented as recursive music
+functions. For example, the syntax for polyphony
+@example
+  < a \\ b>
+@end example
 
-@item text-interface
-  This interface is for texts that are to be set using special routines
-to stack text into lines, using kerning, etc.
+@noindent
+is actually  implemented as a recursive function that replaces the
+above by the internal equivalent of
+@example
+  < \context Voice = "1" @{ \voiceOne a @}
+    \context Voice = "2" @{ \voiceTwo b @} >
+@end example
 
-@item general-grob-interface
-  This interface is supported by all grob types.
-@end table
+Other applications of @code{\apply} are writing out repeats
+automatically (@inputfileref{input/test,unfold-all-repeats.ly}),
+saving keystrokes (@inputfileref{input/test,music-box.ly}) and
+exporting
+LilyPond input to other formats  (@inputfileref{input/test,to-xml.ly})
 
+@seealso
 
+@file{scm/music-functions.scm}, @file{scm/music-types.scm},
+@inputfileref{input/test,add-staccato.ly},
+@inputfileref{input/test,unfold-all-repeats.ly}, and
+@inputfileref{input/test,music-box.ly}.
 
-@node Items and Spanners
-@unnumberedsubsec Items and Spanners
+@node Lexical details
+@section Lexical details
 
-Grobs can also be distinguished in their role in the horizontal spacing.
-Many grobs define constraints on the spacing by their sizes. For
-example, note heads, clefs, stems, and all other symbols with a fixed
-shape.  These grobs form a subtype called @code{Item}.
 
-Other grobs have a shape that depends on the horizontal spacing. For
-example, slur, beam, tie, etc. These grobs form a subtype called
-@code{Spanner}. All spanners have two span-points (these must be
-@code{Item}s), one on the left and one on the right. The left bound is
-also the X-reference point of the spanner.
+@cindex string
+@cindex concatenate
 
-Some items need special treatment for line breaking. For example, a
-clef is normally only printed at the start of a line (i.e. after a line
-break).  To model this, `breakable' items (clef, key signature, bar lines,
-etc.) are copied twice. Then we have three versions of each breakable
-item: one version if there is no line break, one version that is printed
-before the line break (at the end of a system), one version that is
-printed after the line break.
+Begins and ends with the @code{"} character.  To include a @code{"}
+character in a string write @code{\"}.  Various other backslash
+sequences have special interpretations as in the C language.  A string
+that contains no spaces can be written without the quotes.  Strings can
+be concatenated with the @code{+} operator.
 
-Whether these versions are visible and take up space, is determined by
-the outcome of the @code{visibility-lambda}. This grob property is a
-function taking a direction (-1, 0 or 1) as argument. It returns a cons
-of booleans, signifying whether this grob should be transparent and have
-no extent.
 
-@node Grob Scheme functions
-@unnumberedsubsec Grob Scheme functions
 
-Grob properties can be manipulated from Scheme. In practice, most
-manipulations are coded in C++ because of tradition.
+@node Output details
+@section Output details
 
+LilyPond's default output format is @TeX{}.  Using the option @option{-f}
+(or @option{--format}) other output formats can be selected also, but
+currently none of them work reliably.
 
+At the beginning of the output file, various global parameters are defined.
+It also contains a large @code{\special} call to define PostScript routines
+to draw items not representable with @TeX{}, mainly slurs and ties.  A DVI
+driver must be able to understand such embedded PostScript, or the output
+will be rendered incompletely.
 
-@node Molecules
-@section Molecules
+Then the file @file{lilyponddefs.tex} is loaded to define the macros used
+in the code which follows.  @file{lilyponddefs.tex} includes various other
+files, partially depending on the global parameters.
 
-@cindex Molecule
-@cindex Atom
-@cindex Output description
+Now the music is output system by system (a `system' consists of all
+staves belonging together).  From @TeX{}'s point of view, a system is an
+@code{\hbox} which contains a lowered @code{\vbox} so that it is centered
+vertically on the baseline of the text.  Between systems,
+@code{\interscoreline} is inserted vertically to have stretchable space.
+The horizontal dimension of the @code{\hbox} is given by the
+@code{linewidth} parameter from LilyPond's @code{\paper} block.
 
-The objective of any typesetting system is to put ink on paper in the
-right places. For LilyPond, this final stage is left to the @TeX{} and
-the printer subsystem. For lily, the last stage in processing a score is
-outputting a description of what to put where.  This description roughly
-looks like
+
+After the last system LilyPond emits a stronger variant of
+@code{\interscoreline} only if the macro
+@code{\lilypondpaperlastpagefill} is not defined (flushing the systems
+to the top of the page).  You can avoid that by setting the variable
+@code{lastpagefill} in LilyPond's @code{\paper} block.
+
+It is possible to fine-tune the vertical offset further by defining the
+macro @code{\lilypondscoreshift}:
 
 @example
-        PUT glyph AT (x,y)
-        PUT glyph AT (x,y)
-        PUT glyph AT (x,y) 
+\def\lilypondscoreshift@{0.25\baselineskip@}
 @end example
 
-you merely have to look at the tex output of lily to see this.
-Internally these instructions are encoded in Molecules.@footnote{At some
-point LilyPond also contained Atom-objects, but they have been replaced
-by Scheme expressions, making the name outdated.}  A molecule is
-what-to-print-where information that also contains dimension information
-(how large is this glyph?).
+@noindent
+where @code{\baselineskip} is the distance from one text line to the next.
 
-Conceptually, Molecules can be constructed from Scheme code, by
-translating a Molecule and by combining two molecules. In BNF
-notation:
+The code produced by LilyPond should be run through La@TeX{}, not
+plain @TeX{}.
+
+Here an example how to embed a small LilyPond file @code{foo.ly} into
+running La@TeX{} text without using the @code{lilypond-book} script
+(@pxref{lilypond-book manual}):
 
 @example
-Molecule  :: COMBINE Molecule Molecule
-           | TRANSLATE Offset Molecule
-           | GLYPH-DESCRIPTION
-           ;
+\documentclass@{article@}
+
+\def\lilypondpaperlastpagefill@{@}
+\lineskip 5pt
+\def\lilypondscoreshift@{0.25\baselineskip@}
+
+\begin@{document@}
+This is running text which includes an example music file
+\input@{foo.tex@}
+right here.
+\end@{document@}
 @end example
 
-If you are interested in seeing how this information is stored, you
-can run with the @code{-f scm} option. The scheme expressions are then
-dumped in the output file.
+The file @file{foo.tex} has been simply produced with
+
+@example
+lilypond foo.ly
+@end example
 
-All visible, i.e. non-transparent, grobs have a callback to create a
-Molecule. The name of the property is @code{molecule-callback}, and its
-value should be a Scheme function taking one argument (the grob) and
-returning a Molecule.  Most molecule callbacks are written in C++, but
-you can also write them in Scheme. An example is provided in
-@code{input/regression/molecule-hacking.ly}.
+It is important to set the @code{indent} parameter to zero in the
+@code{\paper} block of @file{foo.ly}.
 
+The call to @code{\lineskip} assures that there is enough vertical space
+between the LilyPond box and the surrounding text lines.
 
+@c EOF