]> git.donarmstrong.com Git - lilypond.git/blobdiff - Documentation/user/programming-interface.itely
input/test fixes
[lilypond.git] / Documentation / user / programming-interface.itely
index 7b08550748a60fbb4a6209a8ebd6a065923ee6d0..b8d66709d536e5d7f9a007836c736d12f7c13d1a 100644 (file)
@@ -1,6 +1,6 @@
 @c -*-texinfo-*-
 @node Interfaces for programmers
-@appendix Interfaces for programmers
+@chapter Interfaces for programmers
 
 
 
 @end menu
 
 @node Programmer interfaces for input 
-@appendixsec Programmer interfaces for input 
+@section Programmer interfaces for input 
 
 @menu
 * Input variables and Scheme::  
 * Internal music representation::  
 * Extending music syntax::      
-* Manipulating music expressions::  
+* Manipulating music expressions:: 
+* Using LilyPond syntax inside Scheme::   
 @end menu
 
 @node Input variables and Scheme
-@appendixsubsec 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 @}
+  traLaLa = @{ c'4 d'4 @}
 @end example
 
 @noindent
@@ -37,7 +38,7 @@ 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 @}
+  traLaLa = @{ c'4 d'4 @}
   \paper @{ traLaLa = 1.0 @}
 @end example
 @c
@@ -49,13 +50,13 @@ 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 @}
+ traLaLa = @{ c'4 d'4 @}
 @end example
 
 @noindent
 is internally converted to a Scheme definition
 @example
- (define traLaLa @var{Scheme value of ``@code{\notes ... }''})
+ (define traLaLa @var{Scheme value of ``@code{... }''})
 @end example
 
 This means that input variables and Scheme variables may be freely
@@ -64,14 +65,14 @@ 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 @}
+  traLaLa = @{ c'4 d'4 @}
 
   #(define newLa (map ly:music-deep-copy
     (list traLaLa traLaLa)))
   #(define twice
     (make-sequential-music newLa))
 
-  \score @{ \twice @}
+  @{ \twice @}
 @end example
 
 In the above example, music expressions can be `exported' from the
@@ -82,12 +83,15 @@ of defining @code{\twice}, the example above could also have been
 written as
 @example
   @dots{}
-  \score @{ #(ly:export (make-sequential-music newLa)) @}
+  @{ #(ly:export (make-sequential-music newLa)) @}
 @end example
 
+@refbugs
+
+Mixing Scheme and lily identifiers is not possible with @code{--safe}.
 
 @node Internal music representation
-@appendixsubsec 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
@@ -100,21 +104,22 @@ A music object has three kinds of types:
   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}.
+available is in the internals manual, under
+@hyphenatedinternalsref{Music expressions,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}.
+  `type' or interface: Each music name has several `types' or
+interfaces, 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.
+  @hyphenatedinternalsref{Music classes,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.
@@ -139,7 +144,7 @@ and @internalsref{GraceMusic} has its single argument in
 
 
 @node Extending music syntax
-@appendixsubsec Extending music syntax
+@subsection Extending music syntax
 
 The syntax of composite music expressions, like
 @code{\repeat}, @code{\transpose} and @code{\context}
@@ -186,17 +191,28 @@ The above Scheme code only defines the functionality. The tag
 @code{\applymusic} is selected by defining
 
 @example
-  apply = #(ly:make-music-function
-   (list procedure? ly:music?)
-   (lambda (where func music)
-     (func music)))
+  applymusic = #(ly:make-music-function
+                  (list procedure? ly:music?)
+                  (lambda (location func music)
+                    (func music)))
+@end example
+
+A @code{def-music-function} macro is introduced on top of
+@code{ly:make-music-function} to ease the definition of music
+functions:
+
+@example
+  applymusic = #(def-music-function (location func music) (procedure? ly:music?)
+                  (func music))
 @end example
 
 Examples of the use of @code{\applymusic} are in the next section.
 
+@seealso
+@file{ly/music-functions-init.ly}.
 
 @node Manipulating music expressions
-@appendixsubsec Manipulating music expressions
+@subsection Manipulating music expressions
 
 Music objects and their properties can be accessed and manipulated
 directly, through the @code{\applymusic} mechanism.
@@ -219,7 +235,7 @@ its argument:
      (ly:music-set-property! m 'elements (reverse
        (ly:music-property m 'elements)))
      m)
-  \score { \notes \applymusic #rev-music-1 { c4 d4 } }
+  \applymusic #rev-music-1 { c4 d4 } 
 @end lilypond
 
 The use of such a function is very limited. The effect of this
@@ -276,8 +292,8 @@ above by the internal equivalent of
 Other applications of @code{\applymusic} 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})
+exporting LilyPond input to other formats
+(@inputfileref{input/test,to-xml.ly})
 
 @seealso
 
@@ -287,9 +303,107 @@ LilyPond input to other formats  (@inputfileref{input/test,to-xml.ly})
 @inputfileref{input/test,music-box.ly}.
 
 
+@node Using LilyPond syntax inside Scheme
+@subsection Using LilyPond syntax inside Scheme
+
+Creating music expressions in scheme can be tedious, as they are
+heavily nested and the resulting scheme code is large. For some
+simple tasks, this can be avoided, using LilyPond usual syntax inside
+scheme, with the dedicated @code{#@{ ... #@}} syntax.
+
+The following two expressions give equivalent music expressions:
+@example
+  mynotes = @{ \override Stem #'thickness = #4
+              @{ c'8 d' @} @}
+  
+  #(define mynotes #@{ \override Stem #'thickness = #4
+                      @{ c'8 d' @} #@})
+@end example
+
+The content of @code{#@{ ... #@}} is enclosed in an implicit @code{@{
+... @}} block, which is parsed. The resulting music expression, a
+@code{SequentialMusic} music object, is then returned and usable in scheme.
+
+Arbitrary scheme forms, including variables, can be used in @code{#@{ ... #@}}
+expressions with the @code{$} character (@code{$$} can be used to
+produce a single $ character). This makes the creation of simple
+functions straightforward. In the following example, a function
+setting the TextScript's padding is defined:
+
+@lilypond[verbatim,raggedright]
+  #(use-modules (ice-9 optargs))
+  #(define* (textpad padding #:optional once?)
+    (ly:export   ; this is necessary for using the expression
+                 ; directly inside a block
+      (if once?
+          #{ \once \override TextScript #'padding = #$padding #}
+          #{ \override TextScript #'padding = #$padding #})))
+  
+      {
+          c'^"1"
+          #(textpad 3.0 #t) % only once
+          c'^"2"
+          c'^"3"
+          #(textpad 5.0)
+          c'^"4"
+          c'^"5"
+          
+      }
+@end lilypond
+
+Here, the variable @code{padding} is a number; music expression
+variables may also be used in a similar fashion, as in the following
+example:
+
+@lilypond[verbatim,raggedright]
+  #(define (with-padding padding)
+     (lambda (music)
+       #{ \override TextScript #'padding = #$padding
+          $music
+          \revert TextScript #'padding #}))
+  
+      {
+          c'^"1"
+          \applymusic #(with-padding 3)
+            { c'^"2" c'^"3"}
+          c'^"4"
+      }
+@end lilypond
+
+The function created by @code{(with-padding 3)} adds @code{\override} and
+@code{\revert} statements around the music given as an argument, and returns
+this new expression. Thus, this example is equivalent to:
+
+@example
+      @{
+          c'^"1"
+          @{ \override TextScript #'padding = #3
+            @{ c'^"2" c'^"3"@}
+            \revert TextScript #'padding
+          @}
+          c'^"4"
+      @}
+@end example
+
+This function may also be defined as a music function:
+
+@lilypond[verbatim,raggedright]
+  withPadding = #(def-music-function (location padding music) (number? ly:music?)
+                   #{ \override TextScript #'padding = #$padding
+                      $music 
+                      \revert TextScript #'padding #})
+  
+      {
+          c'^"1"
+          \withPadding #3
+            { c'^"2" c'^"3"}
+          c'^"4"
+      }
+@end lilypond
+
 
 @node Markup programmer interface
-@appendixsec Markup programmer interface
+@section Markup programmer interface
 
 
 @menu
@@ -298,7 +412,7 @@ LilyPond input to other formats  (@inputfileref{input/test,to-xml.ly})
 @end menu
 
 @node Markup construction in scheme
-@appendixsubsec Markup construction in scheme
+@subsection Markup construction in scheme
 
 @cindex defining markup commands 
 
@@ -352,7 +466,7 @@ instead:
 @end lisp
 
 @node Markup command definition
-@appendixsubsec Markup command definition
+@subsection Markup command definition
 
 New markup commands can be defined
 with  the @code{def-markup-command} scheme macro.
@@ -443,12 +557,10 @@ that the text is moved to the upper left.
 
 The final result is as follows:
 @verbatim
-\score {
-    \notes { \fatText
+    { \fatText
         c''^\markup \character #"Cleopatra"
         e'^\markup \character #"Giulio Cesare"
     }
-}
 @end verbatim
 
 @lilypond[raggedright]
@@ -470,12 +582,10 @@ The final result is as follows:
    (interpret-markup paper props 
     (markup "" #:translate (cons -4 0) #:smallcaps name)))
 
-\score {
-    \notes { \fatText
+    { \fatText
         c''^\markup \character #"Cleopatra"
         e'^\markup \character #"Giulio Cesare"
     }
-}
 @end lilypond
 
 We have used the @code{caps} font shape, but suppose that our font
@@ -513,7 +623,7 @@ to the @code{interpret-markup} function, with the @code{paper} and
 
 
 @node Contexts for programmers
-@appendixsec Contexts for programmers
+@section Contexts for programmers
 
 
 @menu
@@ -522,7 +632,10 @@ to the @code{interpret-markup} function, with the @code{paper} and
 @end menu
 
 @node Context evaluation
-@appendixsubsec Context evaluation
+@subsection Context evaluation
+
+@cindex calling code during interpreting
+@cindex @code{\applycontext}
 
 Contexts can be modified during interpretation with Scheme code. The
 syntax for this is
@@ -544,7 +657,12 @@ current bar number on the standard output during the compile:
 
 
 @node Running a function on all layout objects
-@appendixsubsec Running a function on all layout objects
+@subsection Running a function on all layout objects
+
+
+@cindex calling code on layout objects
+@cindex @code{\applyoutput}
+
 
 The most versatile way of tuning an object is @code{\applyoutput}. Its
 syntax is