\setuplayout[header=1cm, topspace=0.5cm, margin=0.5cm,  height=middle, width=middle]
%\showframe
%\showlayout
%\showsetups

\language[en]
\definebodyfontenvironment[default][em=italic]
\setupbodyfont[11pt]

\setuppagenumbering[location={footer}, left={-},right={-}]
\setuphead[title][align=center,style=bold, 
			   incrementnumber=yes,  % keep track of the number
                           number=no]
\setuphead[section][textstyle={\bold \underbar \smallbodyfont},after={\blank[1em]}]                          
                                                                           
\setupitemize[before=\nowhitespace]

\setuplist[title][align=center,style=bold, alternative=a, pagenumber=no, before={\blank[1cm]}, after={\blank[0.3cm]}]
\setuplist[subject,chapter,subject,subsubject,section,subsection][alternative=c]
\setuplist[subsection][margin=1.5em]
%\setuplist[title][align=center,style=bold, alternative=a]
%\setupheadtext[content=Sommaire]
\setupcombinedlist[content][list={title,chapter,subject,subsubject,section,subsection}]


\def\lilyspace{0.7cm}
\definehspace[lilyindent][0.7cm]
\definehspace[threelilyindent][2.1cm]
\definetyping[lily][margin=\lilyspace, page=yes, escape={&,&}]
\definetyping[mylily][margin=\lilyspace, space=fixed, before=\nowhitespace, after=\nowhitespace, escape={&,&}]

\def\myindent{\hphantom{0}\hspace[lilyindent]}

\defineframed[noframed][frame=no]

%% URL
\useURL[chord][http://gillesth.free.fr/Lilypond/chord/]
\useURL[changePitch][http://gillesth.free.fr/Lilypond/changePitch/]
\useURL[copyArticulations][http://gillesth.free.fr/Lilypond/copyArticulations/]
\useURL[addAt][http://gillesth.free.fr/Lilypond/addAt/]
\useURL[extractMusic][http://gillesth.free.fr/Lilypond/extractMusic/]
\useURL[checkPitch][http://gillesth.free.fr/Lilypond/checkPitch/]
\useURL[copyArticulations-LSR][http://lsr.di.unimi.it/LSR/Item?id=769]
\setupinteraction[state=start,color=black,contrastcolor=black, style=\tf]

\setupexternalfigures[directory={exemples}]

%%%%% fonction \syntax
\define[2]\syntax
{% Affiche => syntaxe : #1 #2
\blank
\hbox{\hspace[lilyindent]{\em {\small $\triangleright$} syntax }{: }% 
{\inframed{\type{#1}\type{#2}}}%
}%
\blank
}

%%%%% fonction \func
\define[1]\func
{ % Affiche => La fonction #1
 %% \index{#1}   % <- #1 peut se terminer par le caractère + qui ne sera alors pas affiché :-(
 \ctxlua{context.index((string.gsub("#1","+", "†")))}  %%  remplace dans l'index tout caractère + par †
              %% Noter les doubles parenthèses pour forcer une seule valeur de retour de gsub
 \startsubsection[number=no,list=#1,reference=#1]{}
 %\startsubsubject[list=#1,reference=#1]{}   % ne marche pas !
 {\switchtobodyfont[20pt] \checkmark} 
 {\sca the function \cap{\bf #1}}
 %\stopsubsubject
 \stopsubsection
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\starttext

%%%%%%
\startstandardmakeup
\midaligned{\tfd\sc changePitch.ly}
\blank[2cm]
\midaligned{\tfd ---}
\stopstandardmakeup

\midaligned{\bf \underbar Contents}
\placecontent[criterium=all]
\page

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
\index{changePitch}\index{pattern}\index{newnotes}
\section[description]{Description}
The syntax of \type {\changePitch } is :
\syntax { \changePitch} { pattern newnotes }
This will replace each notes (or chords) in \type {pattern} by notes or chords from \type {newnotes}. If the notes count in \type {newnotes} is greater, the pattern is copied repeatedly and truncated if needed. 

\setupcaptions[location=none]
\startfiguretext[right][exemple1]{}
{\externalfigure[exemple1.pdf][top={\blank[1.2cm]}, wfactor=230, align=flushright]}
\underbar{\sc\Words example 1}
%\blank[1cm]
\startlily
pattern = { c8.-> c16-. } 
newnotes = \relative c' { 
  c d e f 
  <e g> <d f> < c e> < b d>
  c }
\changePitch \pattern \newnotes 
\stoplily %\blank[2.1cm]%
\stopfiguretext


%%%%%%%%%%%%%%%%%
\index{articulations}\index{scripts}\index{dynamics}
\section {Articulations}
All events attached to a note or a chord in \type {newnotes} (such as scripts, articulations, dynamics etc...) are mixed with those, contained in the current note of pattern.\crlf
Here is the result, when replacing the \type {newnotes} definitions of the example 1 :

\startfiguretext[right][exemple2]{}
{\externalfigure[exemple2.pdf][top={\blank[0.5cm]}, wfactor=230, align=flushright]}
\underbar{\sc\Words example 2}
%\blank[1cm]
\startlily
newnotes = \relative c' { 
  c^-\p( d) e\< f 
  g^-( f) e^"hello" d
  c^-\f
}
\stoplily %\blank[2.1cm]%
\stopfiguretext

%%%%%%%%%%%%%%%%%
\section {Events beetween 2 notes in \type{pattern}}
All events after the current note (rests, overrides) in \type{pattern} are added unless the last notes of \type {newnotes} is reached.

\startfiguretext[right][exemple3]{}
{\externalfigure[exemple3.pdf][top={\blank[0.0cm]}, wfactor=190, align=flushright]}
\underbar{\sc\Words example 3}
%\blank[1cm]
\startlily
pattern = { c8 r r4 c8 r c r} 
newnotes = \relative c' { f e d c}
\stoplily %\blank[2.1cm]%
\stopfiguretext
If you do want those events at the end, see 4) with the usage of \type {\skip} in  newnotes.

%%%%%%%%%%%%%%%%%
\index{rest} \index{skip}
\section {Using {\em rest}s and \type{\skip} events in \type {newnotes}}
Rests in \type {newnotes} are allowed (the note in \type {pattern} is just replaced by the rest) but \type{\skip} events in \type {newnotes} have a special meaning :
\hairline
when a \type{\skip} event is encountered in \type {newnotes}, the current note in \type {pattern} (and all not-note events after it) will be skipped. \type {\changePitch } will jump to the next note of the pattern.
\hairline
Here is example 3 with a \type{\skip} added to the end of \type {newnotes}

\startfiguretext[right][exemple4]{}
{\externalfigure[exemple4.pdf][top={\blank[0.3cm]}, wfactor=190, align=flushright]}
\underbar{\sc\Words example 4}
\startlily
pattern = { c8 r r4 c8 r c r} 
newnotes = \relative c' { f e d c s}
\stoplily %\blank[2.1cm]%
\stopfiguretext

%%%%%%%%%%%%%%%%%
\index{insert}
\section {Using a function \type{\insert} in \type {newnotes}}
\syntax { \insert}{ music }
The \type{\insert} function has to be used in \type {newnotes}. It will insert a piece of music {\em between the current notes  of } \type {pattern} {\em and his following}. All extra existing music (rests for example) between these 2 notes will be replaced.\crlf
It is also possible to insert a music even if the current note of \type {pattern} is the last one.

\startfiguretext[right][exemple5]{}
{\externalfigure[exemple5.pdf][top={\blank[0.2cm]}, wfactor=220, align=flushright]}
\underbar{\sc\Words example 5}
\startlily[space=fixed]
pattern = { c8.-> c16-. } 
newnotes = \relative {
  c d e f
  \insert { \override Voice.NoteHead #'color = #red }
  g f e d
  c }
\stoplily %\blank[2.1cm]%
\stopfiguretext

%%%%%%%%%%%%%%%%%
\index{samePitch}\index{tied notes}\index{fakeTie} \index {automatic tie grouping}
\section {Using a function \type{\samePitch} in \type {pattern} - Automatic tie grouping}
\syntax { \samePitch}{ music }
- The \type{\samePitch} function has to be used in the \type {pattern}. It allows you to group several notes and assign all notes of this group to the same current pitch in \type {newnotes}. 

\startfiguretext[right][exemple6a]{}
{\externalfigure[exemple6a.pdf][top={\blank[1.3cm]}, wfactor=200, align=flushright]}
\underbar{\sc\Words example 6a}
\startlily[space=fixed]
pattern = { c4 \tuplet 3/2 \samePitch { c8 c c } } 
newnotes = \relative c' {
  c d e f g  }
\stoplily
\stopfiguretext
\blank[5mm]

- Since version year 2016, \type{\changePitch } has a new behaviour with tied notes.
Now, two tied notes are automatically grouped in a \type{\samePitch} section.

\startfiguretext[right][exemple6b]{}
{\externalfigure[exemple6b.pdf][top={\blank[1.3cm]}, wfactor=258, align=flushright]}
\underbar{\sc\Words example 6b}
\startlily[space=fixed]
pattern = { c4 c4~ \tuplet 3/2 { c8 c c } c4 }      
newnotes = \relative c' { 
  c d e f g a b c d e }
\stoplily
\stopfiguretext
\blank[5mm]

- In some situations, you may want to mimic this behaviour but with two not-tied notes. In such case, you can tag the first note with a special \type{\tag} : \type{fakeTie} as follow :

\startfiguretext[right][exemple6c]{}
{\externalfigure[exemple6c.pdf][top={\blank[1.3cm]}, wfactor=258, align=flushright]}
\underbar{\sc\Words example 6c}
\startlily[space=fixed]
pattern = { c4 \tag #fakeTie c4 \tuplet 3/2 { c8 c c } c4 }      
newnotes = \relative c' { 
  c d e f g a b c d e }
\stoplily
\stopfiguretext
\blank[5mm]

- For compatibility for pieces done with previous version of {\em changePitch.ly} (before 2016), you can add the following special instruction to the very beginning of these pieces, just before the~ \type{\include} :
\startlily
#(define cPCheckForTies #f)
\include "changePitch.ly"
\stoplily


%%%%%%%%%%%%%%%%%
\index{nCopy}\index{repeat percent}
\section {Using a function \type{\nCopy} in \type {newnotes} - Speeding notes entries  }
\syntax { \nCopy }{ n music }
\type{\nCopy n music} \\ has the same effect than \\ \type { \repeat percent n music} \\ but \type { \repeat } doesn't work inside \type {newnotes}. \type{\nCopy} does.
\startlily[space=fixed]
\changePitch {c8. c16}
  { \nCopy 2 {a b c' d'} 
    \nCopy 2 {e' f' g' a'}}
\stoplily
will produce
\startlily[space=fixed]
{ a8. b16 c'8. d'16
  a8. b16 c'8. d'16
  e'8. f'16 g'8. a'16
  e'8. f'16 g'8. a'16
  }
\stoplily
In relative mode, you have to repeat \type {\relative} to each call
\startlily[space=fixed]
\changePitch {c8. c16}
  { \nCopy 2 \relative {a b c d} 
    \nCopy 2 \relative {e' f g a}}
\stoplily
\page

%%%%%%%%%%%%%%%%%
\index{cP}\index{cPI}\index{cPII}
\section {shortcuts}
Three shortcuts have been defined : \type{\cP} \type{\cPI} and \type{\cPII} \crlf
For shorcuts \type{\cPI} et \type{\cPII} you have first to define patterns called respectively patI and patII\crlf

\vtop{\hbox{
  \hspace[lilyindent]
  \starttable[|l|l|]
\HL
\NC \JustCenter\bf shortcut \NC \JustCenter \bf effect \NC\SR
\HL
\NC \type{\cP} \type{pattern} \type{newnotes} \VL \type{\changePitch} \type{pattern} \type{newnotes} \NC\FR
\NC \type{\cPI} \type{newnotes}               \VL \type{\changePitch} \type{\patI} \type{newnotes}   \NC\MR
\NC \type{\cPII} \type{newnotes}              \VL \type{\changePitch} \type{\patII} \type{newnotes}   \NC\LR
  \stoptable
}}

Here is the last example (in a  more complex situation )

\startlily[space=fixed]
patI = \repeat unfold 2 {r8 \repeat unfold 2 {g16 c e}} 
patII = \repeat unfold 2 { << 
                \new Voice {\voiceTwo c2 }
                \new Voice {\voiceOne r16 e8.~ e4 }                
                >> }         
\score { \new PianoStaff <<
  \new Staff \cPI \relative c' { 
        g' c e
        a, d f
        g, d' f
        g, c e
        a, e' a
        fis, a d
        g, d' g
        e, g c
        e, g c
        d, fis c' %etc 
   }      
  \new Staff { 
        \clef bass 
        \cPII \relative c' {
           c e
           c d
           b d
           c e
           c e
           c d
           b d
           b c
           a c
           d, a'  %etc
        }         
    } 
  >> 
} 
\stoplily

\page
\midaligned {\underbar{\sc\Words example 7}}
\blank[1.5cm]
\placefigure
[][exemple7]
{}
{\externalfigure[exemple7.pdf]}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
 
\title[index]{INDEX}
\placeindex[level=title]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\stoptext