Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(29)

Delta Between Two Patch Sets: Documentation/extending/scheme-tutorial.itely

Issue 6295098: Allow immediate Scheme expressions with splicing and multiple values (Closed) Base URL: http://git.savannah.gnu.org/gitweb/?p=lilypond.git/trunk/
Left Patch Set: Created 12 years, 10 months ago
Right Patch Set: A bit more explanation in the docs Created 12 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « Documentation/changes.tely ('k') | lily/include/lily-lexer.hh » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 @c -*- coding: utf-8; mode: texinfo; -*- 1 @c -*- coding: utf-8; mode: texinfo; -*-
2 2
3 @ignore 3 @ignore
4 Translation of GIT committish: FILL-IN-HEAD-COMMITTISH 4 Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
5 5
6 When revising a translation, copy the HEAD committish of the 6 When revising a translation, copy the HEAD committish of the
7 version that you are working on. For details, see the Contributors' 7 version that you are working on. For details, see the Contributors'
8 Guide, node Updating translation committishes.. 8 Guide, node Updating translation committishes..
9 @end ignore 9 @end ignore
10 10
(...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 @end lisp 604 @end lisp
605 605
606 @node Scheme in LilyPond 606 @node Scheme in LilyPond
607 @section Scheme in LilyPond 607 @section Scheme in LilyPond
608 608
609 609
610 @menu 610 @menu
611 * LilyPond Scheme syntax:: 611 * LilyPond Scheme syntax::
612 * LilyPond variables:: 612 * LilyPond variables::
613 * Input variables and Scheme:: 613 * Input variables and Scheme::
614 * Importing Scheme in LilyPond::
614 * Object properties:: 615 * Object properties::
615 * LilyPond compound variables:: 616 * LilyPond compound variables::
616 * Internal music representation:: 617 * Internal music representation::
617 @end menu 618 @end menu
618 619
619 @node LilyPond Scheme syntax 620 @node LilyPond Scheme syntax
620 @subsection LilyPond Scheme syntax 621 @subsection LilyPond Scheme syntax
621 @funindex $ 622 @funindex $
622 @funindex # 623 @funindex #
623 624
(...skipping 30 matching lines...) Expand all
654 expressions. In this case, Lilypond evaluates the code right after the 655 expressions. In this case, Lilypond evaluates the code right after the
655 lexer has read it. It checks the resulting type of the Scheme 656 lexer has read it. It checks the resulting type of the Scheme
656 expression and then picks a token type (one of several 657 expression and then picks a token type (one of several
657 @code{xxx_IDENTIFIER} in the syntax) for it. It creates a @emph{copy} 658 @code{xxx_IDENTIFIER} in the syntax) for it. It creates a @emph{copy}
658 of the value and uses that for the value of the token. If the value of 659 of the value and uses that for the value of the token. If the value of
659 the expression is void (Guile's value of @code{*unspecified*}), nothing 660 the expression is void (Guile's value of @code{*unspecified*}), nothing
660 at all is passed to the parser. 661 at all is passed to the parser.
661 662
662 This is, in fact, exactly the same mechanism that Lilypond employs when 663 This is, in fact, exactly the same mechanism that Lilypond employs when
663 you call any variable or music function by name, as @code{\name}, with 664 you call any variable or music function by name, as @code{\name}, with
664 the only difference that its end is determined by the Lilypond lexer 665 the only difference that the name is determined by the Lilypond lexer
665 without consulting the Scheme reader, and thus only variable names 666 without consulting the Scheme reader, and thus only variable names
666 consistent with the current Lilypond mode are accepted. 667 consistent with the current Lilypond mode are accepted.
667 668
668 The immediate action of @code{$} can lead to surprises, @ref{Input 669 The immediate action of @code{$} can lead to surprises, @ref{Input
669 variables and Scheme}. Using @code{#} where the parser supports it is 670 variables and Scheme}. Using @code{#} where the parser supports it is
670 usually preferable. 671 usually preferable.
672
673 @funindex $@@
674 @funindex #@@
675 There are also @q{list splicing} operators @code{$@@} and @code{#@@}
676 that insert all elements of a list in the surrounding context.
671 677
672 Now let's take a look at some actual Scheme code. Scheme procedures can 678 Now let's take a look at some actual Scheme code. Scheme procedures can
673 be defined in LilyPond input files: 679 be defined in LilyPond input files:
674 680
675 @example 681 @example
676 #(define (average a b c) (/ (+ a b c) 3)) 682 #(define (average a b c) (/ (+ a b c) 3))
677 @end example 683 @end example
678 684
679 Note that LilyPond comments (@code{%} and @code{%@{ %@}}) cannot 685 Note that LilyPond comments (@code{%} and @code{%@{ %@}}) cannot
680 be used within Scheme code, even in a LilyPond input file, because 686 be used within Scheme code, even in a LilyPond input file, because
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
793 @code{twice}: 799 @code{twice}:
794 800
795 @lilypond[verbatim] 801 @lilypond[verbatim]
796 traLaLa = { c'4 d'4 } 802 traLaLa = { c'4 d'4 }
797 803
798 #(define newLa (map ly:music-deep-copy 804 #(define newLa (map ly:music-deep-copy
799 (list traLaLa traLaLa))) 805 (list traLaLa traLaLa)))
800 #(define twice 806 #(define twice
801 (make-sequential-music newLa)) 807 (make-sequential-music newLa))
802 808
803 { \twice } 809 \twice
804 @end lilypond 810 @end lilypond
805 811
806 @c Due to parser lookahead 812 @c Due to parser lookahead
807 813
808 This is actually a rather interesting example. The assignment will only 814 This is actually a rather interesting example. The assignment will only
809 take place after the parser has ascertained that nothing akin to 815 take place after the parser has ascertained that nothing akin to
810 @code{\addlyrics} follows, so it needs to check what comes next. It 816 @code{\addlyrics} follows, so it needs to check what comes next. It
811 reads @code{#} and the following Scheme expression @emph{without} 817 reads @code{#} and the following Scheme expression @emph{without}
812 evaluating it, so it can go ahead with the assignment, and 818 evaluating it, so it can go ahead with the assignment, and
813 @emph{afterwards} execute the Scheme code without problem. 819 @emph{afterwards} execute the Scheme code without problem.
814 820
821 @node Importing Scheme in LilyPond
822 @subsection Importing Scheme in LilyPond
823 @funindex $
824 @funindex #
825
815 The above example shows how to @q{export} music expressions from the 826 The above example shows how to @q{export} music expressions from the
816 input to the Scheme interpreter. The opposite is also possible. By 827 input to the Scheme interpreter. The opposite is also possible. By
817 placing it after @code{$}, a Scheme 828 placing it after @code{$}, a Scheme
818 value is interpreted as if it were entered in LilyPond syntax. 829 value is interpreted as if it were entered in LilyPond syntax.
819 Instead of defining @code{\twice}, the example above could also have 830 Instead of defining @code{\twice}, the example above could also have
820 been written as 831 been written as
821 832
822 @example 833 @example
823 ... 834 ...
824 @{ $(make-sequential-music (list newLa)) @} 835 $(make-sequential-music newLa)
825 @end example 836 @end example
826 837
827 You can use @code{$} with a Scheme expression anywhere you could use 838 You can use @code{$} with a Scheme expression anywhere you could use
828 @code{\@var{name}} after having assigned the Scheme expression to a 839 @code{\@var{name}} after having assigned the Scheme expression to a
829 variable @var{name}. This replacement happens in the @q{lexer}, so 840 variable @var{name}. This replacement happens in the @q{lexer}, so
830 Lilypond is not even aware of the difference. 841 Lilypond is not even aware of the difference.
831 842
832 One drawback, however, is that of timing. If we had been using @code{$} 843 One drawback, however, is that of timing. If we had been using @code{$}
833 instead of @code{#} for defining @code{newLa} in the above example, the 844 instead of @code{#} for defining @code{newLa} in the above example, the
834 following Scheme definition would have failed because @code{traLaLa} 845 following Scheme definition would have failed because @code{traLaLa}
835 would not yet have been defined. For an explanation of this timing 846 would not yet have been defined. For an explanation of this timing
836 problem, @ref{LilyPond Scheme syntax}. 847 problem, @ref{LilyPond Scheme syntax}.
837 848
838 In any case, evaluation of Scheme code happens in the parser at latest. 849 @funindex $@@
839 If you need it to be executed at a later point of time, @ref{Void scheme 850 @funindex #@@
840 functions}, or store it in a macro: 851 A further convenience can be the @q{list splicing} operators @code{$@@}
852 and @code{#@@} for inserting the elements of a list in the surrounding
853 context. Using those, the last part of the example could have been
854 written as
855
856 @example
857 ...
858 @{ $@@newLa @}
859 @end example
860
861 Here, every element of the list stored in @code{newLa} is taken in
862 sequence and inserted into the list, as if we had written
863
864 @example
865 @{ $(first newLa) $(second newLa) @}
866 @end example
867
868 Now in all of these forms, the Scheme code is evaluated while the
869 input is still being consumed, either in the lexer or in the parser.
870 If you need it to be executed at a later point of time, check out
871 @ref{Void scheme functions}, or store it in a procedure:
841 872
842 @example 873 @example
843 #(define (nopc) 874 #(define (nopc)
844 (ly:set-option 'point-and-click #f)) 875 (ly:set-option 'point-and-click #f))
845 876
846 ... 877 ...
847 #(nopc) 878 #(nopc)
848 @{ c'4 @} 879 @{ c'4 @}
849 @end example 880 @end example
850 881
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after
1515 #{ 1546 #{
1516 $x e8 a b $y b a e 1547 $x e8 a b $y b a e
1517 #}) 1548 #})
1518 1549
1519 \relative c''{ 1550 \relative c''{
1520 \pattern c8 c8\f 1551 \pattern c8 c8\f
1521 \pattern {d16 dis} { ais16-> b\p } 1552 \pattern {d16 dis} { ais16-> b\p }
1522 } 1553 }
1523 @end lilypond 1554 @end lilypond
1524 @end ignore 1555 @end ignore
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b