Aug 2, 2024
Inspired by CristΓ³bal Sciutto, here's a little piece of purely client-side html for creating tables.

Save a copy for yourself. You'll need to edit the html to tailor the table dimensions for a specific context.

Saving the table downloads a new copy. (You'll probably want to rename it to the original; that bit is kinda janky.)

As I said before, just view source on this beauty! 😍

permalink

* *
Jul 22, 2024
For the past month I've been doing something very unnatural to me: throwing the first one away. Going back and rewriting some aspect of a working program just to improve one property of the code, here eliminating all derived data structures and re-deriving everything all the time in a performant way.

The problem: implementing text editor operations as lines might wrap or scroll.

e.g. clicking with the mouse to reposition the cursor, or pressing the down arrow (which might cause a scroll)

The key new solution: an API of primitives that make such operations fairly self-evident to build.

  • to_loc: (x, y) -> loc
    Identify the location at pixel coordinates (x,y) on screen.
    Returns nil if (x,y) is not on screen.
  • to_coord: loc -> x, y
    Identify the top-left coordinate on screen of location loc.
    Returns nil if loc is not on screen.
  • down: loc, dy -> loc
    Find the location at the start of a screen line dy pixels down from loc.
    Returns nil if dy is taller than the screen.
    Returns bottom of file if we hit it.
  • up: loc, dy -> loc Find the location at the start of a screen line dy pixels up from loc.
    Returns nil if dy is taller than the screen.
    Returns top of file if we hit it.
  • hor: loc, x -> loc
    Find the location at x=x0 on the same screen line as loc.

I think they might be applicable to any pixel-based editors that use proportional fonts. They seem independent of the data structure used by the editor. I use an array of lines, and so locations are defined as (line_index, pos) tuples, where pos counts in utf-8 code-points.

There's probably a few bugs but hopefully it'll stabilize quickly. I'd appreciate people trying it out.

Lessons from this experience:

  • There's a "hard part" to programming beyond the reach of tools or methods. Sometimes a problem needs the right "algebraic" abstraction, designed around an invariant and preserving it across any composition of operations.
  • Not all programs get this hard.
  • It's useful to notice when some part calls out for doing this hard, focused work.

I think I now better understand the "abyss".

permalink

* *
Jul 11, 2024
Quick and dirty prototype of the previous algo/shape/code using Vim syntax highlighting.

The code in the screenshot is a function to convert a mouse click (mx, my) into the location (line_index, pos) of the character at that point on the screen.

The problem is much of this function is boilerplate shared with several other places, such as the code to draw text on screen, compute the height of a wrapped line, etc. The boilerplate makes it difficult to see the business logic unique to this particular function, and so creates pressure to prematurely create an abstraction to "DRY things out". Highlighting the shape of the boilerplate in pink helps the eye to focus on the unique business logic in the protrusions, and so counters the pressure to hide the boilerplate before I've figured out the best way to do so.


β”Œ local y = State.top
  for line_index,line in array.each(State.lines, State.screen_top.line) do
    if line.mode == 'text' then
      local x = State.left
      local initpos = 1
      if line_index == State.screen_top.line then
        initpos = State.screen_top.pos
      end
      for pos, char in utf8chars(line.data, initpos) do
        local w = State.font:getWidth(char)  -- width of char
        if char:match('%s') then
          if line_wrap_at_word_boundary(State, x, line.data, pos) then β”˜
            if my < y+State.line_height then return line_index, pos end
         β””  x = State.left
            y = y + State.line_height
          else β”˜
            if my < y+State.line_height and mx < x+w then return line_index, pos end
          β”” x = x + w
          end
        else
          if x+w > State.right then
            x = State.left
            y = y + State.line_height β”˜
            if my < y+State.line_height then return line_index, pos end
        β”” else β”˜
            if my < y+State.line_height and mx < x+w then return line_index, pos end
        β”” end
          x = x+w
        end
      end
      y = y + State.line_height
    elseif line.mode == 'drawing' then β”˜
      if my < y+State.drawing_height then return line_index end
    β”” y = y + h
    end
  end β”˜

(As an aside, this is an example of what I think of as "programmer-configured highlighting". We've gotten used to our editors deciding what to highlight for us, and we just pick the colors. One little tool I use everyday is the ability to highlight specific identifiers which flips this around: I pick a word, and the editor picks a color at random to highlight it with. And the highlight persists across sessions. The color of the State variable in the screenshot was selected in this manner.)

permalink

* *
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

permalink

* *
Jul 3, 2024
This morning I'm thinking about turning my previous paper notation into a visual notation for something we typically use keyword args for. For example, the following glyph:

a drawing labeling the top, left, right and bottom coordinates of a rectangle in grey, and the height of a single letter 'h' within the rectangle

…might represent a function for initializing a text editor widget with the following signature:

edit.initialize(top, left, right, bottom, font_size)

And the numbers indicate a specific call to this function:

edit.initialize(15, 15, 115, 215, 20)

Interestingly, these alternative semantics would make for a more pleasing glyph.

edit.initialize(margin-top, margin-left, margin-right, margin-bottom, font-size)

a drawing labeling the top, left, right and bottom _margins_ of a rectangle in grey, and the height of a single letter 'h' within the rectangle

permalink

* *
Jun 30, 2024
Umpteenth attempt at stripping away unnecessary rules when teaching programming.

(Just a mockup to convey the idea. Plan is just to use this notation with pen and paper.)

Is this picture intelligible without any explanation?

permalink

* *
Jun 9, 2024
Here's the most recent bugfix I had to make, along with a fairly lengthy but still incomplete post-mortem in the commit description.

Still several things left to investigate.

  • Really small windows probably crash.
  • Really wide and short windows probably crash.
  • Pressing and releasing mouse really quickly within a single frame will probably crash.

I want to delete some data structures and just recompute them all the time.

permalink

* *
Jun 7, 2024
All the bugs in my software that others run into have the following story template:

  • t = n: my program was responsible for x and y
  • t = nn: my program became responsible for x, y and z
  • t = nnnnnn: bug in z (interacting with x/y)

Adding z required rethinking the entire program. I tried to patch it in. Implications were not fully worked out.

permalink

* *
Jun 5, 2024
Trying to build software for others is extremely disheartening. I can be eating my own dogfood on a daily basis for years and still newcomers hit bugs in their first 10 minutes.

I wonder if this is the major reason to huddle together on top of jenga stacks with tons of dependencies, terrified of fragmentation: You always need more testing than you think, and there's no way to compete with something that's been through that much testing.

I come stare at this abyss every year or two.

Doesn't do me any good, though.

(This post spawned a sprawling, constructive conversation.)

permalink

* *
May 4, 2024
Visualizing the digits of Ο€

The following program lets you scrub the mouse downward to find more and more precise approximations of Ο€ within the red optical sight in the center of the screen.

permalink

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