By default, the accidentals used for key cancellations are placed adjacent to those for key signature changes. This behavior can be changed by overriding the break-align-orders
property of the BreakAlignment
grob.
The value of break-align-orders
is a vector of length 3, with quoted lists of breakable items as elements. Each list describes the default order of prefatory matter at the end, in the middle, and at the beginning of a line, respectively. We are only interested in changing the behaviour in the middle of a line.
If you look up the definition of break-align-orders
in LilyPond's Internal Reference (see the BreakAlignment
grob), you get the following order in the second element:
... staff-bar key-cancellation key-signature ...
We want to change that, moving key-cancellation
before staff-bar
. To make this happen we use the grob-transformer
function, which gives us access to the original vector as the second argument of the lambda function, here called orig (we don't need the first argument, grob). We return a new vector, with unchanged first and last elements. For the middle element, we first remove key-cancellation
from the list, then adding it again before staff-bar
.