% Hello emacs, this is -*- coding:utf-8 -*-
\version "2.18.2"

\include "anglican-chant.ily"

#(define (equal-length-sub-lsts lst to-insert)
  ;; lst is supposed to be a list of lists.
  ;; 'equal-length-sub-lsts' returns a new list adding amounts of 'to-insert'
  ;; where needed to ensure equal length of every sublist.
  (if (every list? lst)
      (let* ((lngth-ordered-lst
               (sort-list lst (lambda (p q) (> (length p) (length q)))))
             (eq-lngth-sub-lsts
               (map
                 (lambda (ll)
                   (let* ((length-ll (length ll))
                          (max-length (length (car lngth-ordered-lst)))
                          (ls-to-add
                            (make-list (- max-length length-ll) to-insert)))
                     (append ll ls-to-add)))
                 lst)))
           eq-lngth-sub-lsts)
      (ly:error "Every element of ~a needs to be a list" lst)))

#(define (reorder-list-elements l1 l2)
  ;; l1 is supposed to be a list of list, each sublist of equal length.
  ;; A new list is accumulated in l2 and returned, containing the the first
  ;; elements of every sublist in a new sublist, the second elements, etc
  ;;
  ;; (list-elements '((a b c) (1 2 3) (x y z)) '())
  ;; -> '((a 1 x) (b 2 y) (c 3 z))
  (if (every null? l1)
      (reverse l2)
      (reorder-list-elements (map cdr l1) (cons (unzip1 l1) l2))))

#(define-markup-list-command (rows layout props args)
  (markup-list?)
  #:properties ((x-off '()))
  "Stacks the args horizontally relying on 'x-offs', returning multi-columns. 
The displacement of each column may be specified by overriding the
@var{x-off}-property. 
To avoid overlapping, every element of args should have an appropiate
line-width."
  (let* ((stils-list
           (map
             (lambda (arg) (interpret-markup-list layout props (list arg)))
             args))
         (new-listed-stils
           (reorder-list-elements
             (equal-length-sub-lsts stils-list point-stencil) '()))
         (line-width (ly:output-def-lookup layout 'line-width))
         (calculated-x-off
           (if (null? x-off)
               (map
                 (lambda (i) (* i (/ line-width (- (length args) 0))))
                 (iota (length args)))
               x-off))
         (list-of-stils-to-return
           (map
             (lambda (x)
               (apply ly:stencil-add
                 (map
                   (lambda (y e) (ly:stencil-translate-axis y e X))
                   x
                   calculated-x-off)))
             new-listed-stils)))
    list-of-stils-to-return))

#(define-markup-list-command (paragraph layout props args)
  (markup-list?)
  #:properties ((par-indent '(0 . 0))
                (word-space))
  "Returns a markuplist with justified-lines applied.
Overriding par-indent '(<horiznatl> . <vertical>) will create some horizontal
space at begin and/or vertical space to the ptinted object before."
  (interpret-markup-list layout props
    #{
      \markuplist
        \justified-lines {
          \hspace #(+ (- word-space) (car par-indent))
          \vspace #(cdr par-indent)
          #args
        }
    #}))


\paper {
%  print-all-headers = ##t
  print-page-number = ##f
}

\header {
  % The following fields are centered
  title = "Psalm 148"
  poet = "20"
  composer = "Matthew Camidge (1758 – 1844)"	% en dash
  % The following fields are centered at the bottom
  tagline = ##f
}


keyTime = {
  \key es \major
  \time 2/2
}

SopranoMusic = \relative g' {
  \mark \markup { \circle 1 }
  g1 | as2 g | f1 | \bar "||"
  \mark \markup { \circle 2 }
  es1 | c'2 d | es as, | g f | \bar "||"
  \mark \markup { \circle 3 }
  bes1 | d2 bes | es1 | \bar "||"
  \mark \markup { \circle 4 }
  c1 | bes2 g | f f | es1 | \bar "|."
}

AltoMusic = \relative es' {
  es1 | f2 es | d1 |
  es1 | es2 as | g f | es d |
  f1 | f2 f | es1 |
  es1 | f2 es | es d | es1 |
}

TenorMusic = \relative bes {
  bes1 | bes2 bes | bes1 |
  g1 | as2 as | bes c | bes1 |
  d1 | bes2 bes | bes1 |
  as1 | f2 bes | c bes | g1 |
}

BassMusic =  \relative es {
  es1 | d2 es | bes1 |
  c1 | as2 f' | g as | bes1 |
  bes1 | as2 as | g1 |
  as1 | d,2 es | as, bes | es1 |
}


% Use markup to center the chant on the page
\markup {
  \fill-line {
    \score {
      % centered
      %\transpose e es
      <<
        \new ChoirStaff <<
          \new Staff <<
            \keyTime
            \clef "treble"
            \new Voice = "Soprano" <<
              \voiceOne
              \SopranoMusic
            >>
            \new Voice = "Alto" <<
              \voiceTwo
              \AltoMusic
            >>
          >>
          \new Staff <<
            \clef "bass"
            \keyTime
            \new Voice = "Tenor" <<
              \voiceOne
              \TenorMusic
            >>
            \new Voice = "Bass" <<
              \voiceTwo
              \BassMusic
            >>
          >>
        >>
      >>
      \layout {
        \context {
          \Score
          \override SpacingSpanner.base-shortest-duration = #(ly:make-moment 1/2)
        }
        \context {
          \Staff
          \remove "Time_signature_engraver"
        }
        \context {
          \Voice
          \consists "Horizontal_bracket_engraver"
        }
      }
    }  % End score
  }
}  % End markup

\markup {
  \fill-line {
    \column {
      \center-align { \null \line \caps { Psalm 148 } \vspace #0.4 }
    }
  } % \fill-line
} % \markup


first-column =
\markuplist
  \center-align \dynamic
  \column-lines  {
    f
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
  }

second-column =
\markuplist
  \right-align \italic
  \column-lines   {
    Full
    ""
    Full
    ""
    Full
    ""
    Full
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    ""
    \line { \italic {\concat{2 \super nd} part } }
    ""
    ""
    ""
    ""
    ""
  }

third-column =
\markuplist
  \right-align
  \column-lines  {
    1
    ""
    2
    ""
    3
    ""
    4
    ""
    5
    ""
    ""
    6
    ""
    7
    ""
    8
    ""
    9
    ""
    10
    ""
    11
    ""
    12
    ""
    ""
    ""
    13
    ""
    ""
    ""
    ""
    ""
  }

fourth-column =
\markuplist
  \left-align
  \column-lines  {
    \override #'(par-indent . (0 . 1.05))
    \paragraph { O praise the | Lord \emphasize #"*o*f" \vertSep heav'n : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { praise \vertSep ― him \vertSep \emphasize #"*i*n th*e*" \vertSep height. }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { Praise him, all ye \vertSep \single-hbracket \line { angels \dot of } \vertSep his : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { praise \vertSep ― him \vertSep all his \vertSep host. }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { Praise him, \vertSep sun \emphasize #"*a*nd" \vertSep moon : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { praise him, \vertSep all ye \vertSep stars \emphasize #"*a*nd" \vertSep light. }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { Praise him, \vertSep all ye \vertSep heav'ns : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { and ye wáters that \vertSep are a- \vertSep bove \emphasize #"th*e*" \vertSep heav'ns. }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { Let them praise the \vertSep \single-hbracket \line { Name of \dot the } \vertSep Lord : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { for he spake the word and they were made, * he com- \vertSep }
    \override #'(par-indent . (10 . 0))
    \paragraph { \single-hbracket \line { manded \dot and } \vertSep \single-hbracket \line { they were \dot cre- } \vertSep ated. }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { He hath made them fást for \vertSep \single-hbracket \line { ever \dot and } \vertSep ever : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { he hath given them a \vertSep law which \vertSep \single-hbracket \line { shall not \dot be } \vertSep broken. }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { Praise the \vertSep \single-hbracket \line { Lord up-on } \vertSep earth : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { \emphasize #"y*e*" \vertSep \single-hbracket \line { dragons \dot and } \vertSep all ― \vertSep deeps ; }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { Fire and hail, \vertSep snow \emphasize #"*a*nd" \vertSep vapours : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { wind and \vertSep storm ful- \vertSep \single-hbracket \line { filling \dot his } \vertSep word ; }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { Mountains \emphasize #"*a*nd" \vertSep all ― \vertSep hills : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { fruitful \vertSep trees \emphasize #"*a*nd" \vertSep all ― \vertSep cedars ; }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { Beasts \emphasize #"*a*nd" \vertSep all ― \vertSep cattle : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { worms \vertSep ― \emphasize #"*a*nd" \vertSep feath-er'd \vertSep fowls ; }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { Kings of the earth \emphasize #"*a*nd" \vertSep all ― \vertSep people : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { princes and all \vertSep \emphasize #"judg-*e*s" \vertSep \emphasize #"*o*f th*e*" \vertSep world ; }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { Young men and maidens, old men and children, * praise the \vertSep }
    \override #'(par-indent . (10 . 0))
    \paragraph { \single-hbracket \line { Name of \dot the } \vertSep Lord : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { for his Name only is excellent, * and his \vertSep \single-hbracket \line { praise a-bove } \vertSep }
    \override #'(par-indent . (10 . 0))
    \paragraph { heav'n \emphasize #"*a*nd" \vertSep earth. \hspace #4 \bold {[ \concat{2 \super nd} part ➛ ]} }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { He shall exalt the horn of his people, * all his \vertSep saints shall \vertSep praise him : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { even the children of Israel, \vertSep \single-hbracket \line { even \dot the } \vertSep \single-hbracket \line { people \dot that } \vertSep serveth him. }

    \override #'(par-indent . (0 . 1.7))
    \paragraph \italic { Glory \vertSep be \dot to the \vertSep father : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { and to the Son, \vertSep and \dot to the \vertSep Ho-ly \vertSep Ghost ; }

    \override #'(par-indent . (0 . 1.05))
    \paragraph { As it was in the beginning, * is now, and \vertSep ever \dot shall \vertSep be : }

    \override #'(par-indent . (3.5 . 0))
    \paragraph { world without \vertSep end, A- \vertSepDot ― \vertSepDot men. }
  }

\markuplist
  \override #'(x-off . (4 16 22 24 78))
  %\override #'(baseline-skip . 5)
  %\box
  \rows {
    \first-column
    \second-column
    \third-column
    \fourth-column
  }


