As I try to figure out what the behavior of floating divs should be, I’ve been contemplating ways to make them more useful. And one of those ways would be to allow centering and right-justification, for things like quote boxes.
On the Z-machine side, this isn’t too difficult. Print the text to a buffer with output stream 3, look at the length of that buffer, then print the appropriate number of spaces before doing the text.
However, this has a problem. If the text is printed back from the buffer, it’ll lose all styling information. Previously that wasn’t a concern, but now I’ve enabled styling in the status bar! It would be a shame to lose that again. But if the text is printed a second time live, the contents might change, due to (select)
statements and other randomized output or side effects.
So my proposal is: make this the library’s problem instead! The fundamental ethos of Dialog is that as much as possible should be done in library code, accessible to authors, instead of being deep magic in the compiler (or a separate lower level, like I6 within I7, or assembly within I6). Why don’t we do the same for this?
But Linus has also adamantly refused to add string-processing into the language, and I want to respect that. Adding an entirely new data type for text buffers is also a bigger change than I’m comfortable biting off right now.
So, specifically, I propose a new special syntax, something like (count characters) ... (into $)
, which measures the length of whatever is printed inside it (without sending any of it to the screen). The library can then use this, along with (space $)
, to do whatever centering and right-justification it likes. And because this is happening in library code, it’s fully transparent to authors: they can see firsthand why it’s important not to put (select)
statements inside their centering, rather than just taking it on faith that it’s illegal for deep magic reasons.
Then, you could do something like:
(right justify $Closure)
(current div width $Width)
(count characters)
(query $Closure)
(into $Chars)
($Width minus $Chars into $Spaces)
(space $Spaces)
(query $Closure)
For centering, just divide $Spaces
by two first. For a quote box, draw an appropriate number of dashes on either side. And so on!
What do you all think of this idea? For now, the character-counting would be Z-machine only, until I figure out how best to tinker with the Å-machine. But on the Å-machine web interpreter, you can already use text-justify: right
instead.
(If I go with this proposal, I might as well add (count lines) ... (into $)
and (count words) ... (into $)
too, for maximum flexibility. It’s not that much harder to add all of those than to add just one. But counting characters is the easiest of those.)