\version "2.18.2"

#(ly:set-option 'point-and-click #f)

%--------------------
% Maintainer: Sven Kaiser / e-mail: noten(at)oemsel.de
% am_brunnen_vor_dem_tore_silcher_sa-tb.ly
% 2015-06-15

\header {
    poet =  "Text: Wilhelm Müller (1794 - 1827)"
    composer = "Friedrich Silcher (1789 - 1860)"
    title = "Am Brunnen vor dem Tore"
    subtitle = "(Der Lindenbaum)"
    tagline = \markup { \tiny \italic"Diese Noten sind gemeinfrei  -  Rev. 1  -  Notensatzprogrammierung S. Kaiser mit http://lilypond.org   -   Notendownload: www.oemsel.de/noten" }
}

#(set-global-staff-size 18.5)

\paper {
    paper-width = 21.0\cm
    paper-height = 29.7\cm
    top-margin = 0.8\cm
    bottom-margin = 1.3\cm
    left-margin = 1.8\cm
    right-margin = 1.8\cm
    print-page-number = ##f
    markup-system-spacing = #'((basic-distance . 20) (padding . 0.6) (stretchability . 30))
    system-system-spacing #'basic-distance = #15
    ragged-right = ##f
    ragged-last = ##f
}

\layout {
    \context { \Score autoBeaming = ##f}
}

PartPOneVoiceOne =  \relative c'' {
  \clef "treble" \key f \major \time 3/4
  \once \override TextScript #'padding = #2.0
  \once \override TextScript #'X-offset = #-5.0
  \partial 8 c8 ^\markup{ \bold\italic {Andante} \italic {(innig vorzutragen)}}
  c4. a8 a8 a8 | %1
  a4 ^\> f4 \! r8 f8 | %2
  g4. a8 bes8. g16 | %3
  a2 r8 c8 | %4
  c4. a8 a8 a8 | %5
  a4 ^\> f4 \! r8 f8  | %6
  g4. ^\<  a8 \! \times 2/3 {c8[ bes8 g8]} | %7  
  f2 r8 
  \once \override TextScript #'padding = #1.0
  \once \override TextScript #'X-offset = #-4.0
  f8 ^\markup{ \small {etwas langsamer}} \noBreak | %8
  g4. g8 ^\< g8 g8 \!  | % 9  
  a8. [ bes16 ] c4 r8 \override Hairpin.to-barline = ##f c8 ^\< | % 10
  d4. \! ^\> c8 \!  a8 f8 | % 11
  g2 r8 g8 | % 12
  g4. g8 ^\< g8 g8 \! | % 13
  a8. [ bes16 ] c4
  r8 c8 ^\tweak #'X-offset #-1.0 \tweak #'Y-offset #1.0 ^\f | % 14
  f4( ^> c8) a8 bes8 g8 | % 15
  c2 ^> r8 c8 ^\tweak #'X-offset #-2.5 \tweak #'Y-offset #1.5 \pp | % 16
  f4( ^> 
  \once \override TextScript #'padding = #2.0
  \once \override TextScript #'X-offset = #-3.3
  c8 ^\markup{ {rit.}}) a8 \times 2/3 {c8[ bes8 g8]} | % 17
  f2. \bar "|."  
}

PartPOneVoiceTwo =  \relative e' {
  \clef "treble" \key f \major \time 3/4 
  \partial 8 a8
  a4. f8 f8 f8 | %1
  f4 c4 r8 c8 | % 2
  e4. f8 e8. e16 | % 3
  f2 r8 a8 | % 4
  a4. f8 f8 f8 | % 5
  f4 c4 r8 c8 | %6
  e4. f8 \times 2/3 {e4 e8} | % 7
  f2 r8 f8 | %8
  e4. e8 e8 e8 | % 9
  f4 f4 r8 f8 | % 10
  f4. f8 f8 f8 | % 11
  e2 r8 e8 | % 12
  e4. e8 e8 e8 | % 13
  f4 f4 r8 a8 | % 14
  f4. f8 g8 e8 | % 15
  f2 r8 a8 | % 16
  a4. f8 \times 2/3 {e4 e8} | % 17
  c2. \bar "|."  
}

PartPTwoVoiceOne =  \relative c' {
  \clef "bass" \key f \major \time 3/4 
  \partial 8 f8
  f4. c8 c8 c8 | % 1
  c4 ^\> a4 \! r8 a8 | % 2
  bes4. a8 g8. c16 | % 3
  a2 r8 c8 | % 4
  f4. c8 c8 c8 | % 5
  c4 ^\> a4 \! r8 a8  | % 6
  bes4. ^\< a8 \! \times 2/3 {g4 bes8} | % 7
  a2 r8 
  \once \override TextScript #'padding = #2.0
  \once \override TextScript #'X-offset = #-4.0
  a8 ^\markup{ \small {etwas langsamer}} | % 8
  c4. c8 ^\< c8 c8 \! | % 9
  c4 c4 r8 \override Hairpin.to-barline = ##f a8 ^\< | % 10
  bes4. \! ^\> a8 \! c8 c8 | % 11
  c2 r8 c8 | % 12
  c4. c8 ^\< c8 c8 \! | % 13
  c4 c4 
  r8 c8 ^\tweak #'X-offset #-1.7 \tweak #'Y-offset #1.4 ^\f | % 14
  c4( ^> a8) c8 c8 c8 | % 15
  c2 ^> r8 c8 ^\tweak #'X-offset #-2.5 \tweak #'Y-offset #2.0 ^\pp | % 16
  c8 ^> [ a8 ^"rit." c8 ] c8 \times 2/3 {c4 bes8} | %17
  a2. \bar "|."
}

PartPTwoVoiceTwo =  \relative a {
  \clef "bass" \key f \major \time 3/4 
  \partial 8 f8
  f4. f,8 a8 c8 | % 1
  f4 f,4 r8 f'8 | % 2
  c4. c8 c8. c16 | % 3
  f4( f,4) r8 f'8 | % 4
  f4. f,8 a8 c8 | % 5
  f4 f,4 r8 f'8 | % 6
  c4. c8 \times 2/3 {c4 c8} | % 7
  f4 ( f,4 ) r8 a8 | % 8
  c4. c8 c8 c8 | % 9
  f4 f4 r8 f8 | % 10
  f4. f8 f8 a,8 | % 11
  c2 r8 c8 | % 12
  c4. c8 c8 c8 | % 13
  f8. [ g16 ] a4 r8 f8 | % 14
  f8 [a8 c8] c,8 c8 bes8 | % 15
  a8[ c8 f8 a8] r8 c8 | % 16
  c,4. c8 \times 2/3 {c4 c8} | % 17
  f,2. \bar "|."
}

PartPOneVoiceOneLyricsOne =  \lyricmode { \set stanza = "1."
  Am Brun -- nen vor dem To -- re, da steht ein Lin -- den -- baum, ich
  träumt' in sei -- nem Schat -- ten so man -- chen "sü - ßen" Traum; ich schnitt in sei -- ne
  Rin -- de so man -- ches lie -- be Wort, es zog in Freud' und Lei -- de zu
  ihm mich im -- mer fort, zu ihm mich "im - mer" fort.
}

PartPOneVoiceOneLyricsTwo =  \lyricmode { \set stanza = "2." 
  Ich mußt' auch heu -- te wan -- dern vor -- bei in tie -- fer Nacht, da
  hab ich noch im Dun -- keln die Au -- gen "zu - ge" -- macht; und sei -- ne Zwei -- ge
  rausch -- ten, als rie -- fen sie mir zu: komm her zu mir, Ge -- sel -- le, hier
  find'st du dei -- ne Ruh', hier find'st du "dei - ne" Ruh'!
}

PartPOneVoiceOneLyricsThree =  \lyricmode { \set stanza = "3." 
  Die kal -- ten Win -- de blie -- sen mir grad ins An -- ge -- sicht, der
  Hut flog mir vom Kop -- fe, ich wen -- de -- "te mich" nicht. Nun bin ich man -- che
  Stun -- de ent -- fernt von je -- nem Ort; und im -- mer hör ich's rau -- schen: du
  fän -- dest Ru -- he dort, du fän -- dest "Ru - he" dort.
}

#(define (rest-score r)
  (let ((score 0)
	(yoff (ly:grob-property-data r 'Y-offset))
	(sp (ly:grob-property-data r 'staff-position)))
    (if (number? yoff)
	(set! score (+ score 2))
	(if (eq? yoff 'calculation-in-progress)
	    (set! score (- score 3))))
    (and (number? sp)
	 (<= 0 2 sp)
	 (set! score (+ score 2))
	 (set! score (- score (abs (- 1 sp)))))
    score))

#(define (merge-rests-on-positioning grob)
  (let* ((can-merge #f)
	 (elts (ly:grob-object grob 'elements))
	 (num-elts (and (ly:grob-array? elts)
			(ly:grob-array-length elts)))
	 (two-voice? (= num-elts 2)))
    (if two-voice?
	(let* ((v1-grob (ly:grob-array-ref elts 0))
	       (v2-grob (ly:grob-array-ref elts 1))
	       (v1-rest (ly:grob-object v1-grob 'rest))
	       (v2-rest (ly:grob-object v2-grob 'rest)))
	  (and
	   (ly:grob? v1-rest)
	   (ly:grob? v2-rest)	     	   
	   (let* ((v1-duration-log (ly:grob-property v1-rest 'duration-log))
		  (v2-duration-log (ly:grob-property v2-rest 'duration-log))
		  (v1-dot (ly:grob-object v1-rest 'dot))
		  (v2-dot (ly:grob-object v2-rest 'dot))
		  (v1-dot-count (and (ly:grob? v1-dot)
				     (ly:grob-property v1-dot 'dot-count -1)))
		  (v2-dot-count (and (ly:grob? v2-dot)
				     (ly:grob-property v2-dot 'dot-count -1))))
	     (set! can-merge
		   (and 
		    (number? v1-duration-log)
		    (number? v2-duration-log)
		    (= v1-duration-log v2-duration-log)
		    (eq? v1-dot-count v2-dot-count)))
	     (if can-merge
		 ;; keep the rest that looks best:
		 (let* ((keep-v1? (>= (rest-score v1-rest)
				      (rest-score v2-rest)))
			(rest-to-keep (if keep-v1? v1-rest v2-rest))
			(dot-to-kill (if keep-v1? v2-dot v1-dot)))
		   ;; uncomment if you're curious of which rest was chosen:
		   ;;(ly:grob-set-property! v1-rest 'color green)
		   ;;(ly:grob-set-property! v2-rest 'color blue)
		   (ly:grob-suicide! (if keep-v1? v2-rest v1-rest))
		   (if (ly:grob? dot-to-kill)
		       (ly:grob-suicide! dot-to-kill))
		   (ly:grob-set-property! rest-to-keep 'direction 0)
		   (ly:rest::y-offset-callback rest-to-keep)))))))
    (if can-merge
	#t
	(ly:rest-collision::calc-positioning-done grob))))


% The score definition
\score {
    <<
        \new ChoirStaff <<
            \new Staff <<
            \set Staff.instrumentName = \markup { \center-column { \line {"Sopran"} \line {"Alt"} } }
                \context Staff << 
                    \context Voice = "PartPOneVoiceOne" { \voiceOne \PartPOneVoiceOne }
                    \new Lyrics \lyricsto "PartPOneVoiceOne" \PartPOneVoiceOneLyricsOne
                    \new Lyrics \lyricsto "PartPOneVoiceOne" \PartPOneVoiceOneLyricsTwo
                    \new Lyrics \lyricsto "PartPOneVoiceOne" \PartPOneVoiceOneLyricsThree
                    \context Voice = "PartPOneVoiceTwo" { \voiceTwo \PartPOneVoiceTwo }
                    >>
                >>
            \new Staff <<
            \set Staff.instrumentName = \markup { \center-column { \line {"Tenor"} \line {"Bass"} } }
                \context Staff << 
                    \context Voice = "PartPTwoVoiceOne" { \voiceOne \PartPTwoVoiceOne }
                    \context Voice = "PartPTwoVoiceTwo" { \voiceTwo \PartPTwoVoiceTwo }
                      >>
                >>
          >>
    >>
        \layout { 
  	  % Steuert die Höhe der Taktnummerierung über dem System        
            \context { \Score \override BarNumber.padding = #2.0 } 
          % steuert die Einrückung des ersten Systems; Standardwert = automatisch eingerückt
            indent = #12
          % Pausenzeichen aus zwei Stimmen zusammenfassen
            \context { \Staff \override RestCollision #'positioning-done = #merge-rests-on-positioning }
        }   
}

\score {
    <<
      \new Voice = "Sopran" { \autoBeamOff \PartPOneVoiceOne }
      \new Voice = "Alt"    { \autoBeamOff \PartPOneVoiceTwo }
      \new Voice = "Tenor"  { \autoBeamOff \PartPTwoVoiceOne }
      \new Voice = "Bass"   { \autoBeamOff \PartPTwoVoiceTwo }
    >>
  \midi { }
}