+(define-public (invalidate-alterations context)
+ "Invalidate alterations in @var{context}.
+
+Elements of @code{'localKeySignature} corresponding to local
+alterations of the key signature have the form
+@code{'((octave . notename) . (alter barnum . measurepos))}.
+Replace them with a version where @code{alter} is set to @code{'clef}
+to force a repetition of accidentals.
+
+Entries that conform with the current key signature are not invalidated."
+ (let* ((keysig (ly:context-property context 'keySignature)))
+ (set! (ly:context-property context 'localKeySignature)
+ (map-in-order
+ (lambda (entry)
+ (let* ((localalt (key-entry-alteration entry))
+ (localoct (key-entry-octave entry)))
+ (if (or (accidental-invalid? localalt)
+ (not localoct)
+ (= localalt
+ (key-entry-alteration
+ (find-pitch-entry
+ keysig
+ (ly:make-pitch localoct
+ (key-entry-notename entry)
+ 0)
+ #t #t))))
+ entry
+ (cons (car entry) (cons 'clef (cddr entry))))))
+ (ly:context-property context 'localKeySignature)))))
+