Jul 10, 2024
I've been slowly reading "The Nature of Order" by Christopher Alexander and slowly thinking about how to make my editor for text and line drawings more timeless. (And mostly sleeping a lot, if I'm honest.) Today the combination of the two led me to draw this shape for the line-wrapping algorithm.

photograph of a page from a notebook showing a strange shape a little bit like an axe or an old-style double-edge safety razor blade. It's straight on the left with rounded corners. On the right, is a bumpy, irregular 'blade' with 5 protrusions of irregular lengths. Four of them form two pairs of long+short protrusions, and a fifth is off to the bottom. The four protrusions are for code related to lines of text, labeled:
- at word boundary, need to wrap
- at word boundary, no need to wrap
- word, need to chop
- word, no need to chop

Until now I've been developing the editor the "usual" way, which for me consists of needing some computation, figuring out the most convenient place to perform the computation, then squirreling away the result somewhere it's available when needed. In an effort to get myself out of the rut of the inevitable problems of indirection and cache invalidation that result, I've been trying to replace all my ad hoc data structures with on-demand computation based on the base state of the program. And what I've been ending up with is umpteen variations of this pictured algorithm, just with different code stuck on to the protrusions.

There may be an abstraction that comes out of all this, but I don't see it yet. And as CA says, a flower isn't made up of identical petals. Each one evolves uniquely as a part of the whole.

Here's the Lua code skeleton corresponding to that drawing. The ellipses correspond to protrusions in the drawing:

for line_index, line in array.each(State.lines, State.screen_top.line) do
  if line.mode == 'text' then
    local initpos = 1
    if line_index == State.screen_top.line then
      -- top screen line
      initpos = State.screen_top.pos
    end
    for pos, char in utf8chars(line.data, initpos) do
      if char:match('%s') then
        if line_wrap_at_word_boundary(State) then
          ...
        else
          ...
        end
      else
        if x+w > State.right then
          ...
        else
          ...
        end
      end
    end
  else  -- drawing
    ...
  end
end

This post is part of my Freewheeling Apps Devlog.

Comments gratefully appreciated. Please send them to me by any method of your choice and I'll include them here.

archive
projects
writings
videos
subscribe
Mastodon
RSS (?)
twtxt (?)
Station (?)