+tweak =
+#(define-music-function (parser location prop value item)
+ (symbol-list-or-symbol? scheme? symbol-list-or-music?)
+ (_i "Add a tweak to the following @var{item}, usually music.
+Layout objects created by @var{item} get their property @var{prop}
+set to @var{value}. If @var{prop} has the form @samp{Grob.property}, like with
+@example
+\\tweak Accidental.color #red cis'
+@end example
+an indirectly created grob (@samp{Accidental} is caused by
+@samp{NoteHead}) can be tweaked; otherwise only directly created grobs
+are affected.
+
+As a special case, @var{item} may be a symbol list specifying a grob
+path, in which case @code{\\override} is called on it instead of
+creating tweaked music. This is mainly useful when using
+@code{\\tweak} as as a component for building other functions.
+
+If this use case would call for @code{\\once \\override} rather than a
+plain @code{\\override}, writing @code{\\once \\tweak @dots{}} can be
+convenient.
+
+@var{prop} can contain additional elements in which case a nested
+property (inside of an alist) is tweaked.")
+ (if (ly:music? item)
+ (let ((p (check-grob-path prop parser location
+ #:start 1
+ #:default #t
+ #:min 2)))
+ (if p
+ (set! (ly:music-property item 'tweaks)
+ (acons (cond ((pair? (cddr p)) p)
+ ((symbol? (car p))
+ (cons (car p) (cadr p)))
+ (else (cadr p)))
+ value
+ (ly:music-property item 'tweaks))))
+ item)
+ ;; We could just throw this at \override and let it sort this
+ ;; out on its own, but this way we should get better error
+ ;; diagnostics.
+ (let ((p (check-grob-path
+ (append item (if (symbol? prop) (list prop) prop))
+ parser location
+ #:default 'Bottom #:min 3)))
+ (if p
+ #{ \override #p = #value #}
+ (make-music 'Music)))))
+
+undo =
+#(define-music-function (parser location music)
+ (ly:music?)
+ (_i "Convert @code{\\override} and @code{\\set} in @var{music} to
+@code{\\revert} and @code{\\unset}, respectively. Any reverts and
+unsets already in @var{music} cause a warning. Non-property-related music is ignored.")
+ (define warned #f)
+ (let loop
+ ((music music))
+ (let
+ ((lst
+ (fold-some-music
+ (lambda (m) (or (music-is-of-type? m 'layout-instruction-event)
+ (music-is-of-type? m 'context-specification)
+ (music-is-of-type? m 'apply-context)
+ (music-is-of-type? m 'time-signature-music)))
+ (lambda (m overrides)
+ (case (ly:music-property m 'name)
+ ((OverrideProperty)
+ (cons
+ (make-music 'RevertProperty
+ 'symbol (ly:music-property m 'symbol)
+ 'grob-property-path
+ (cond
+ ((ly:music-property m 'grob-property #f) => list)
+ (else
+ (ly:music-property m 'grob-property-path))))
+ overrides))
+ ((PropertySet)
+ (cons
+ (make-music 'PropertyUnset
+ 'symbol (ly:music-property m 'symbol))
+ overrides))
+ ((ContextSpeccedMusic)
+ (cons
+ (make-music 'ContextSpeccedMusic
+ 'element (loop (ly:music-property m 'element))
+ 'context-type (ly:music-property m 'context-type))
+ overrides))
+ (else
+ (if (not warned)
+ (begin
+ (ly:input-warning location (_ "Cannot revert ~a")
+ (ly:music-property m 'name))
+ (set! warned #t)))
+ overrides)))
+ '()
+ music)))
+ (cond
+ ((null? lst) (make-music 'Music))
+ ((null? (cdr lst)) (car lst))
+ (else (make-sequential-music lst))))))