\version "2.16.0"

% #(ly:set-option 'debug-skylines #t) 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% definition of the new combined stencil
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#(define (combine-markup-default-rehearsal-mark alist text)
   (lambda (grob)
     (let* ((default-stencil (ly:grob-property grob 'stencil))
            (sky-l (ly:grob-property grob 'skyline-horizontal-padding ))
            (other-axis (lambda (a) (remainder (+ a 1) 2)))
            (markup-stencil 
              (grob-interpret-markup grob text))
            (axis (assoc-ref alist 'axis))
            (order (assoc-ref alist 'order))
            (align-direction (assoc-ref alist 'align-direction))
            (center-on-first-stencil (assoc-ref alist 'center-on-first-stencil))
            (stil-lst (list markup-stencil default-stencil))
            (stencil-list (if (= order 1) stil-lst (reverse stil-lst)))
            (new-stencil (ly:stencil-combine-at-edge
                           (ly:stencil-aligned-to (car stencil-list) (other-axis axis) align-direction)
                             axis
                             RIGHT
                             (ly:stencil-aligned-to (cadr stencil-list) (other-axis axis) align-direction)
                             1))
            (first-stencil-length (interval-length (ly:stencil-extent (car stencil-list) X)))
            (new-stencil-length (interval-length (ly:stencil-extent new-stencil X))))
            
     (if (and (eq? axis X) center-on-first-stencil)
       (ly:grob-set-property! grob 'self-alignment-X 
            (/ (- new-stencil-length first-stencil-length) (* -1 new-stencil-length)))
       #f)
          
     (ly:grob-set-property! grob 'stencil new-stencil))))
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
% definition of a music-function to use the new stencil
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
         
addMarkupToDefaultRehearsalMark =
#(define-music-function (parser location alist mrkp)(list? markup?)
#{
        \once \override Score.RehearsalMark #'before-line-breaking =
          #(combine-markup-default-rehearsal-mark alist mrkp)
#})

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% definition of the test-markup to use with the function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

segnoMarkup =
\markup { \musicglyph #"scripts.segno" }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% definitions of shortcuts of various combinitions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

segnoDefault = {
\addMarkupToDefaultRehearsalMark
  #`((axis . ,X)
     (order . 1)
     (align-direction . ,CENTER)
     (center-on-first-stencil . #t))
  \segnoMarkup
  \mark \default
}

defaultSegno = {
\addMarkupToDefaultRehearsalMark
  #`((axis . ,X)
     (order . -1)
     (align-direction . ,CENTER)
     (center-on-first-stencil . #t))
  \segnoMarkup
  \mark \default
}

defaultOnTopOfSegno = {
\addMarkupToDefaultRehearsalMark
  #`((axis . ,Y)
     (order . -1)
     (align-direction . ,CENTER)
     ;; if `center-on-first-stencil´ is set to #f
     ;; it can be omitted!
     ;; see `segnoOnTopOfDefault´
     (center-on-first-stencil . #f))
  \segnoMarkup
  \mark \default
}

segnoOnTopOfDefault = {
\addMarkupToDefaultRehearsalMark
  #`((axis . ,Y)
     (order . 1)
     (align-direction . ,CENTER))
  \segnoMarkup
  \mark \default
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% ----- TEST ----
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

\paper {
        indent = 0
        line-width = 50
}

\relative c' {
        \set Score.markFormatter = #format-mark-box-alphabet
% A
        \mark \default
        c'1
        \break   
% B
        \segnoDefault
        b4-. \mf a2.
        \break
% C
        \mark \default
        a1
        \break
% D
        \defaultSegno
        g
        \break 
% E
        \mark \default
        f1
        \break 
% F
        \defaultOnTopOfSegno
        e
        \break 
% G
        \mark \default
        d1
        \break 
% H
        \segnoOnTopOfDefault
        e
        \break  
% I
        \mark \default
        d1
}
