\version "2.14.2"
           
#(define (define-grob-custom-property symbol type? description)
  (if (not (equal? (object-property symbol 'backend-doc) #f))
      (ly:error (_ "symbol ~S redefined") symbol))

    (set-object-property! symbol 'backend-type? type?)
    (set-object-property! symbol 'backend-doc description)
    symbol)

#(define all-user-grob-custom-properties
   (map
     (lambda (x)
       (apply define-grob-custom-property x))

     `(
       (smaller-not-parenthesize-note-heads ,boolean? "boolean for making smaller parenthesized NoteHeads")
       (color-not-parenthesize-note-heads ,boolean? "boolean for coloring parenthesized NoteHeads")
       (transparent-not-parenthesize-note-heads ,boolean? "boolean for making transparent parenthesized NoteHeads")
       (new-stencil-not-parenthesize-note-heads ,boolean? "boolean for making a new stencil for parenthesized NoteHeads")
       (un-parenthesize ,boolean? "boolean for un-parenthesize NoteHeads")
     )))
     
#(define (new-stil grob) 
   (grob-interpret-markup grob (markup 
   		  #:with-color red 
   		  #:fontsize -2
   		  #:rotate -90 ":)")))

#(define change-parenthesized-note-heads-alist `(
  ("font-size" . -3)
  ("color" . ,(x11-color 'grey66))
  ("transparent" . #t)
  ("stencil" . ,new-stil) 
  ))

AffectParenthesizedNoteHeadsEngraver =
#(lambda (context)
    `((acknowledgers
      (note-head-interface .
        ,(lambda (engraver grob source-engraver) 
          (if (not (null? (ly:event-property (event-cause grob) 'parenthesize)))
            (begin
              (cond ((eq? #t (ly:grob-property grob 'smaller-not-parenthesize-note-heads))
                     (set! (ly:grob-property grob 'font-size) (assoc-ref change-parenthesized-note-heads-alist "font-size"))))
              (cond ((eq? #t (ly:grob-property grob 'color-not-parenthesize-note-heads))
                     (set! (ly:grob-property grob 'color) (assoc-ref change-parenthesized-note-heads-alist "color"))))
              (cond ((eq? #t (ly:grob-property grob 'transparent-not-parenthesize-note-heads))
                     (set! (ly:grob-property grob 'transparent) (assoc-ref change-parenthesized-note-heads-alist "transparent"))))
              (cond ((eq? #t (ly:grob-property grob 'new-stencil-not-parenthesize-note-heads))
                     (set! (ly:grob-property grob 'stencil) (assoc-ref change-parenthesized-note-heads-alist "stencil")))
                    (else #f)))
              #f)))
      (parentheses-interface .
        ,(lambda (engraver grob source-engraver)   
           (let* ((par (ly:grob-parent grob Y))
                  (grob-name (lambda (x) (assq-ref (ly:grob-property x 'meta) 'name)))
                  (note-head? (eq? 'NoteHead (grob-name par))))
           (cond ((eq? #t (ly:grob-property par 'un-parenthesize))
                  (ly:grob-suicide! grob))
                 ((eq? #f (ly:grob-property par 'un-parenthesize))
                  #f)
                 ((and note-head?
                      (not (null? (ly:event-property (event-cause par) 'parenthesize))))
                  (ly:grob-suicide! grob))
                 (else #f)))))
         )))

%--- Test --%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

myLayoutI = \layout {
  \context {
    \Voice
    \override NoteHead #'smaller-not-parenthesize-note-heads = ##t
    \consists \AffectParenthesizedNoteHeadsEngraver
  }
}

myLayoutII = \layout {
  \context {
    \Voice
    \override NoteHead #'color-not-parenthesize-note-heads = ##t
    \consists \AffectParenthesizedNoteHeadsEngraver
  }
}

myLayoutIII = \layout {
  \context {
    \Voice
    \override NoteHead #'transparent-not-parenthesize-note-heads = ##t
    \consists \AffectParenthesizedNoteHeadsEngraver
  }
}

myLayoutIV = \layout {
  \context {
    \Voice
    \override NoteHead #'new-stencil-not-parenthesize-note-heads = ##t
    \consists \AffectParenthesizedNoteHeadsEngraver
  }
}

myLayoutV = \layout {
  \context {
    \DrumStaff
    \override NoteHead #'smaller-not-parenthesize-note-heads = ##t
    \consists \AffectParenthesizedNoteHeadsEngraver
  }
}

myLayoutVI = \layout {
  \context {
    \DrumStaff
    \override NoteHead #'color-not-parenthesize-note-heads = ##t
    \consists \AffectParenthesizedNoteHeadsEngraver
  }
}

myLayoutVII = \layout {
  \context {
    \DrumStaff
    \override NoteHead #'transparent-not-parenthesize-note-heads = ##t
    \consists \AffectParenthesizedNoteHeadsEngraver
  }
}

myLayoutVIII = \layout {
  \context {
    \DrumStaff
    \override NoteHead #'new-stencil-not-parenthesize-note-heads = ##t
    \consists \AffectParenthesizedNoteHeadsEngraver
  }
}

music = \relative c' {
  <fis \parenthesize d'>4
  <g \parenthesize d'>
  \parenthesize r
  \parenthesize r
}

\score {
        \new Staff
            \relative c' {
              \once\override NoteHead #'smaller-not-parenthesize-note-heads = ##t
              <fis \parenthesize d'>4
              \once\override NoteHead #'un-parenthesize = ##f
              <g \parenthesize d'>
              \parenthesize r
              \parenthesize r
              
              \once\override NoteHead #'color-not-parenthesize-note-heads = ##t
              \music
              
              \once\override NoteHead #'transparent-not-parenthesize-note-heads = ##t
              \music
              
              \once\override NoteHead #'new-stencil-not-parenthesize-note-heads = ##t
              \music
            }
        \layout {
          \context {
            \Voice
            \consists \AffectParenthesizedNoteHeadsEngraver
          }
        }
}

\score {
        \new Staff <<
        \new Voice \transpose c c' { \voiceOne \music }
        \new Voice { \voiceTwo \music }
        >>	
        \layout { 
                \myLayoutI
        }
}

#(define mydrums '(
      (bassdrum      default            #f         -5)
      (snare         default            #f          0)
      (hihat         cross            #f          5)
      (pedalhihat      cross            #f         -3)
      (openhihat      xcircle            #f          5)
      (lowtom         default            #f          1)
      (hightom      default            #f          3)
      (lowfloortom   default            #f         -1)
      (ridecymbal      harmonic-black      #f          6)
      (ridebell      harmonic-black      #f          5)
      (crashcymbal   harmonic-black      #f          7)
      )
)

oben = \drummode { 
\numericTimeSignature
\time 4/4
   {
   \override TextScript #'script-priority = #100
   cymc8\upbow rb
   rb16 rb16 rb8
   rb8 rb16 rb16
   rb8 rb16 rb16
   }
}

unten = \drummode
   {
   %\override Beam #'damping = #+inf.0 
   \override Beam #'positions = #'(-5 . -5)
   bd16 hhp sn hhp
   sn16 hhp8 < sn hhp >16
   bd16 <  \parenthesize  sn hhp >16 << \parenthesize sn >> hhp
   r16 << sn8 hhp >> hhp16
   }

\score {
        \new DrumStaff <<
          \set DrumStaff.drumStyleTable = #(alist->hash-table mydrums)
          \new DrumVoice { \voiceOne \oben }
          \new DrumVoice { \voiceTwo \unten }
        >>
        
        \layout { 
                \myLayoutVIII
        }
}

\score {
        \new Staff
        \relative c' {
        	<\tweak #'un-parenthesize ##f \parenthesize e,
                 \tweak #'smaller-not-parenthesize-note-heads ##t \parenthesize b'
                 \tweak #'smaller-not-parenthesize-note-heads ##t \tweak #'color-not-parenthesize-note-heads ##t \parenthesize e
                 \tweak #'transparent-not-parenthesize-note-heads ##t \tweak #'un-parenthesize ##f \parenthesize gis
                 \tweak #'new-stencil-not-parenthesize-note-heads ##t \parenthesize b
                 \parenthesize e>4-\parenthesize ->
                 
        	\parenthesize
        	<\tweak #'un-parenthesize ##f e
                 \tweak #'smaller-not-parenthesize-note-heads ##t b'
                 \tweak #'smaller-not-parenthesize-note-heads ##t \tweak #'color-not-parenthesize-note-heads ##t e
                 \tweak #'transparent-not-parenthesize-note-heads ##t \tweak #'un-parenthesize ##f gis
                 \tweak #'new-stencil-not-parenthesize-note-heads ##t b
                 e>4-\parenthesize ->
        }
        \layout {
          \context {
            \Voice
            \consists \AffectParenthesizedNoteHeadsEngraver
          }
        }
}
