Hjalfi writes a vi for CP/M
Channel: Poking Technology
Date: 2019-06-28

In which yr hmbl svt decides that there aren't enough shabby vi clones around, and writes one from scratch in a single sitting (not counting a dinner break).

This is part of my cpmish project, which is an open source CP/M distribution --- the seminal 1977 8080/Z80 operating system which inspired MS-DOS. http://cowlark.com/cpmish

This was a marathon. My original plan turned out to simply be not fast
enough: I'm using the Amsterdam Compiler Kit (http://tack.sourceforge.net/)
compiler, because it's the only one which will generate 8080 machine code,
but the code quality isn't good and is very slow. So partway in I had to redo
the entire display layer when I realised that I couldn't do screen updates
fast enough. (It would totally have worked had I written it in raw machine
code...) I also took several other wrong turns later, all of which wasted
time. But I did eventually produce an adequate, working text editor,
supporting most of the basic vi editing commands I use.

  - 00:01:30 Boilerplate
  - 00:05:30 First, broken version of the refresh code
  - 00:58:35 Start work on the editor
  - 01:36:00 Initial text rendering works; start work on command loop
  - 01:36:30 Realise that I need direct BIOS access and I add support to the ACK
  - 01:54:40 Continue on the command loop and cursor movement
  - 02:39:00 Realisation that the screen refresh code won't work
  - 02:44:00 Start ripping out the refresh code
  - 03:07:00 First redraw with the new refresh code
  - 03:22:00 Horizontal cursor movement works
  - 03:48:30 Vertical movement works, slowly
  - 03:56:57 Dinner break, decision to drastically simplify editor model
  - 04:28:05 Cursor movement with the new model works
  - 04:51:10 Cursor movement with the new model works _quickly_
  - 05:00:00 Insert mode works
  - 05:21:30 Motion by words works
  - 05:37:45 Repetition count works
  - 05:49:40 Inserting newlines works
  - 06:30:00 Scrolling works, up to a point
  - 07:17:30 Lots of commands work, start thinking about batching redraws
  - 07:34:45 Notice a hanging problem with scrolling over big lines
  - 07:39:30 Tea break, remove all the batching redraw stuff
  - 08:07:08 Finally figure out the horrible scrolling bug
  - 08:53:15 Saving documents works
  - 09:09:07 Simple colon commands work

Right now it's a shade under 10kB, allowing files of up to 49kB on a Kaypro
II; that's not bad. If I were to recompile it with sdcc it'd be both faster
and about half the size but sdcc's CP/M support is very poor and I keep
running into code generation bugs, so I'm sticking with the ACK for now.
(Maybe when z88dk reaches Debian...)

The main missing features are search and a yank buffer. Neither would be hard
to add, although I don't think this will ever get regular expressions, and
the yank buffer is most easily implemented by keeping the buffer on disk;
that'd be slow, though.

Since doing the video above I've done some polishing, bugfixing, and a few
more features. Terminal support is now factored out into a standalone
library, making it easy to support multiple terminal types, and for the
Kaypro II and NC200 it supports the erase-until-end-of-line control code,
making redraws a lot faster.

The code is BSD-2 clause licensed, meaning you can pretty much do with it
what you like!

https://github.com/davidgiven/cpmish/blob/master/cpmtools/qe.c