Regardless, I will leave you with my final contribution of a "cleaned
up" version with some logic and usage updates. In particular, I
reordered the arguments to enable default values for the alignments.
And the main refactoring uses a different approach to assembling the
markup, which makes handling the parentheses simpler.
Wow, thanks a million, Aaron! This works perfectly! This Scheme crash course is leaving me with much to chew for the next few weeks ;-) and your script is a great contribution!
Now, it is as if all
you need is a normal MetronomeMark aligned to the end of a TextSpanner.
After all, reusing is preferred to reinventing.
That's right. It sounds simple but it isn't. There's an open issue about the subject since at least 2013
https://code.google.com/archive/p/lilypond/issues/3176. There are two unsatisfactory problems for usability involved from my point of view. One is the impossibility to use proper markup syntax for text spanners. I opened a thread about it in July
https://lists.gnu.org/archive/html/lilypond-user/2020-07/msg00263.html. Right now there is what I consider an abuse of \overrides. An \override should be used to be able to change "sensible" defaults. In the case of text spanners, the default has no musical use: a dashed line with no text on either side. The syntax of a text spanner has the serious omission of starting something without user input (\startTextSpan starts what? The user should have an "interface" to give an initial input. This interface is what I call \markupSpan in my feature request, a function that I'm also implementing at least for personal use for the moment). That an \override has to be used for making a basic input (and mostly for actually overriding a property) is unsatisfactory. The second issue is the mentioned 2013 issue. Lilypond lacks the possibility of using text spanners not only with a \tempo syntax (which thanks to your script is very well solved now!), something that makes the "music" in the code more legible (because there is proper markup of musical events) but also to format the whole thing as a \tempo marking in the score (tempo indications are not attached to the staff but to the system, and horizontally metronome marks are not attached to the first note of the measure but to the time signature above which they are centered, that's why I included \halign properties in the script as workaround).
So while you could continue to develop the function above, perhaps time
would be better spent solving the underlying issues that necessitated
creating this function in the first place. Though, that discussion
would likely merit its own thread.
It would be awesome if this thread is the seed for waters to start moving to close the 2013 issue and perhaps to have a proper markup syntax for allowing user input for text spanners (\markupSpan, \textSpan or whatever is adopted, leaving overrides for actual properties). Both issues already have their own threads in the archive.
Best regards,
Martín.