\version "2.17.25"

#(define (make-note-name-markup pitch)
  ;; Return pitch-name-markup for @var{pitch}.
  ;; Limitation: - works for default-language (dutch) only.
  ;;             - half-step-alterations are supported, but no quarter-steps or
  ;;               double-half-steps.
  ;; 
  ;; @lilypond[verbatim,quote]
  ;; \markup { #(make-note-name-markup #{ c #}) }
  ;; @end lilypond
  (let ((row-note-name-strg 
          (vector-ref #("C" "D" "E" "F" "G" "A" "B") 
                      (ly:pitch-notename pitch))))
  (make-line-markup
    (list
      (make-simple-markup
        (string-append
          (if (>= (ly:pitch-octave pitch) -1)
              (string-downcase row-note-name-strg)
              row-note-name-strg)
          (case (ly:pitch-alteration pitch)
             ((0) "")
             ((1/2) "is")
             ((-1/2) "es"))
          (case (ly:pitch-octave pitch)
             ((-4) ",,")
             ((-3) ",")
             ((-2) " ")
             ((-1) " ")
             ((0) "'")
             ((1) "''")
             ((2) "'''")
             ((3) "''''"))))))))

#(define-markup-command 
  (print-vertical-pitches layout props ls staff-space)
    (list? number?)
  "Draw columned strings taken via @code{make-note-name-markup} from @var{ls},
   which is supposed to be a list of pitches."

  (let* ((scale-factor (magstep (/ 1.5 staff-space)))
         (font-size (* -4.5 scale-factor))
         (text 
            (make-column-markup 
              (map
                (lambda (pitch)
                  (make-note-name-markup pitch))
                ls))))

    (interpret-markup layout props
                 (markup #:vcenter
                         #:bold
                         #:fontsize font-size
                         #:override (cons 'baseline-skip staff-space)
                         #:left-align text))))

#(define ((instrument-name::print-strings-note-names pitch-lst) grob)
   ;; Adds columned note-names to @code{instrumentName} only in the starting
   ;; Staff-line.
   ;; These note-names are taken via @code{print-vertical-pitches} from
   ;; @var{pitch-lst}
   (let* ((staff-symbol (ly:grob-object grob 'staff-symbol))
          (staff-space (ly:staff-symbol-staff-space grob))
          (string-indications-stencil
            (grob-interpret-markup grob 
               (make-print-vertical-pitches-markup 
                  pitch-lst staff-space)))
          (orig (ly:grob-original grob))
          (siblings (if (ly:grob? orig)
                        (ly:spanner-broken-into orig) 
                        '() )))

      (if (and (>= (length siblings) 1) (eq? grob (car siblings)))
          (let ((stil (or (system-start-text::print (car siblings)) 
                          point-stencil)))
            (ly:stencil-combine-at-edge
              string-indications-stencil
              X
              LEFT
              (ly:stencil-aligned-to stil Y CENTER)
              staff-space))
          (system-start-text::print grob))))

#(define* 
  (string-tuning-with-indication #:optional (string-tuning 'guitar-tuning))
    ;; Prints the names of the strings before the first line of a tabulature, by 
    ;; adding them to @code{instrumentName}.
    ;; If @code{instrumentName} isn't set separately in @code{\\with} of the 
    ;; relevant @code{TabStaff}, an empty string is used instead.
    ;; @code{shortInstrumentName} will not be modified.
    ;; The optional @var{string-tuning} may be specified, otherwise 
    ;; @code{guitar-tuning} will be used.
    (let* ((tuning-pitch-list (assoc-get string-tuning defaultStringTunings)))
    #{
      \with {
        \override InstrumentName #'stencil = 
          #(instrument-name::print-strings-note-names tuning-pitch-list)
        \override InstrumentName #'self-alignment-X = #RIGHT
        stringTunings = #tuning-pitch-list
        instrumentName = ""
      }
    #}))
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% EXAMPLES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#(set-global-staff-size 19.7)

% These settings in \paper are not needed.
% Only written to get an overall nice pdf :)

\paper { 
	indent = 0
	line-width = 50 
	markup-system-spacing #'padding = #2
        markup-markup-spacing #'padding = #2
	system-system-spacing=
           #'((basic-distance . 5)
              (minimum-distance . 5)
              (padding . 1)
              (stretchability . 12))
	score-markup-spacing =
           #'((basic-distance . 5)
              (minimum-distance . 5)
              (padding . 4)
              (stretchability . 12))
        page-count = 1
}

\header {
	title = \markup \center-column { 
		"Indicating" 
		"string-names with" 
		#(lilypond-version)
	}
}

%%%%%%%%%%%%%%%%%%%
% EXAMPLE 1
%%%%%%%%%%%%%%%%%%%

\markup \fontsize #-1 \bold
  \fill-line { 
      \center-column { 
        "Default-guitar-tab," 
        "no \"\\instrumentName\" is set" 
      }
  }

\new TabStaff 
  \with { #(string-tuning-with-indication) } 
  { c1 \break d }
  
%%%%%%%%%%%%%%%%%%%
% EXAMPLE 2
%%%%%%%%%%%%%%%%%%%
  
\markup \fontsize #-1 \bold
  \fill-line { 
      \center-column { 
        "Default-guitar-tab," 
        "\"\\instrumentName\" is set" 
        "Smaller staff-size"
      }
  }
  
\new TabStaff 
  \with { 
    #(string-tuning-with-indication)
    instrumentName = \markup 
                       \override #'(baseline-skip . 1)
                       \center-column { "classical" "guitar" }
    fontSize = #-3 
    \override StaffSymbol #'staff-space = #(magstep -3) 
    \override StaffSymbol #'thickness = #(magstep -3) 
  }
  { c1 \break d }
  
%%%%%%%%%%%%%%%%%%%
% EXAMPLE 3
%%%%%%%%%%%%%%%%%%%
  
\markup \fontsize #-1 \bold
  \fill-line { 
      \center-column { 
        "bass-tab," 
        "\"\\instrumentName\" and" 
        "\"\\shortInstrumentName\" is set" 
      }
  }

\new TabStaff 
  \with { 
    #(string-tuning-with-indication 'bass-four-string-tuning) 
    instrumentName = "bass"
    shortInstrumentName = \markup \line { "b" \hspace #3 }
  }
  { c1 \break d }
  
%%%%%%%%%%%%%%%%%%%
% EXAMPLE 4
%%%%%%%%%%%%%%%%%%%
  
\markup \fontsize #-1 \bold
  \fill-line { 
      \center-column { 
        "five-string-bass-tab," 
        "\"\\instrumentName\" is set" 
      }
  }
  
\new TabStaff 
  \with { 
    #(string-tuning-with-indication 'bass-five-string-tuning)
    instrumentName = \markup 
                          \override #'(baseline-skip . 2) 
                          \center-column { "five" "string" "bass"}
  }
  \relative c { c1 \break d }
  
%%%%%%%%%%%%%%%%%%%
% EXAMPLE 5
%%%%%%%%%%%%%%%%%%%
  
\markup \fontsize #-1 \bold
  \fill-line { 
      \center-column { 
        "seven-string-guitar-tab," 
        "\"\\instrumentName\" is set"
        "Larger staff-size"
        \override #'(font-name . "Purisa") "Changed font-name"
      }
  }
  
\new TabStaff 
  \with { 
    \override InstrumentName #'font-name = #"Purisa"
    #(string-tuning-with-indication 'guitar-seven-string-tuning)
    instrumentName = \markup 
                         \override #'(font-name . 'default) 
                         \center-column { "seven" "string" "guitar"}
    fontSize = #3 
    \override StaffSymbol #'staff-space = #(magstep 6) 
    \override StaffSymbol #'thickness = #(magstep 3) 
  }
  { c1 \break d }
