Website title

Computing: The Details
Lex's blog of curious computer-related shenanigans.

Blog post

I built a laptop for EMF Camp 2024

Published: 2024-05-14

This blog post is also available in video form! Watch it on YouTube

EMF camp is coming up soon. It's a camping festival with talks, workshops, and fascinating installations. Attended by tech folk, craft folk, artists, and engineers.

Last time I took a giant playable LED Rubik's cube. I'm not doing that again this time, sadly. The cube is a bit broken, and it was a lot of effort to do, so this time I'm just doing a personal thing rather than a big installation.

Because of the kind of event that EMF is, I need to take with me some kind of device that is:

  • portable
  • has a battery that will last for most of the event
  • is capable of displaying the timetable
  • is capable of displaying the camp map
  • can fetch updates to the timetable and camp map
  • and provides a convenient way to contact my friends at the camp

So that's why I built a ZX Spectrum based laptop with a dial up modem, telephone handset, and a cassette deck! With all that functionality, I might not even need my smartphone while I'm there!

The laptop, fully built, standing on a white table. The screen is lifted up, but isn't displaying anything. It has the rainbow shoulder strap attached.

The phone bit

There's going to be a copper telephone network, and a DECT network at EMF camp. DECT is the standard that your cordless home phone uses to connect to the base station. Hopefully there will be some interesting ViewData services running somewhere on the phone network. So I'm planning to take this ViewData modem to access those services. It needs to be connected to a ZX Spectrum, and needs a separate telephone for dialling. The modem is the black box underneath the ZX Spectrum in this photo.

A ZX Spectrum standing on top of a Prism VTX 5000.

ViewData is a telephone based information access system. You dial a number, wait for an answer, and then let the modem join the call, and then you can receive what is essentially a TeleText page that contains the information you want.

In the UK, there was a ViewData service offered by the Post Office Telecoms service, which later became British Telecom (BT). Their service was called Prestel, so you might hear these modems referred to as Prestel modems. Prestel ran from 1978 to 1994. It is now thoroughly obsolete. There is nothing you can do with ViewData that you can't do more easily with an internet connection. But, of course, I enjoy playing with esoteric old computer systems, and so do a lot of other people that attend EMF Camp. So we're still gonna use it just for fun.

The Prestel Logo. It is a grid of letters that spell the word Prestel in various directions, and with various colours on the letters.

I could just take this assortment of components and connect them all together, but I'm gonna be in a field, and I kinda want to be able to take it around the camp without much effort. So that's why I decided to turn them into a laptop. All I really had to do was put all these parts into a case, and add a battery and power circuitry.

All of the parts of the laptop, assembled and working on a bench, as a proof of concept.

For some reason I decided to massively overcomplicate the phone part though. I bought a cheap landline phone, disassembled it, replaced the dial pad, and built a custom housing for it.

A generic cheap BT telephone handset. The same handset, disassembled, with a custom keypad ready to connect to it in place of the original. The custom phone case, partly assembled, showing a 3D printed handset, and a laser cut cradle that also houses the keypad.

The phone line is kinda interesting. I was wondering about how to connect to the camp phone network while I'm wandering the camp. It's not like landline phones are portable, and the modem certainly doesn't support DECT. But then someone pointed out to me that BT makes a device called a Digital Voice adapter. You see, BT is trying to switch everyone over to VoIP. They want the whole phone network to be digital. If you register for an internet and phone service from BT today then they'll send you a "hub" which is a modem, wifi router, and a DECT base station all in the same package. But some customers have old style phones that they still want to use. In fact, you can still buy brand new BT branded analog telephones, which is what I did for this project. So they sell these devices for "upgrading" an old phone to be a DECT phone. I just need to embed one of those in my laptop.

A BT Digital Voice Adapter. The electronics from inside a BT Digital Voice Adapter.

With the DECT adapter, I can connect wirelessly to the phone network and have a completely mobile solution to accessing the timetable and camp map. Oh, and it will let me contact my friends while I'm there too, the phone still works as normal.

Another EMF-goer, called Ben, has put together a viewdata service for accessing the schedule information. I've left a link in the description to a browser based viewdata terminal emulator that he's set up to demo his service. I'm planning to make use of it while I'm there. But, of course, just in case that doesn't work for some reason, I have a backup plan.

You can find a demo of this on his website... On last inspection, the site appears to no longer work, but the link is preserved here anyway

https://viewdata.microwavepizza.co.uk/

Offline schedule access

Screenshot of the main menu for the schedule browser software Screenshot of one of the pages of events for the Friday Screenshot of the event details page for the opening ceremony

It's possible that I won't be able to connect to the DECT network, or that my modem might develop a fault, or something else I've not expected. So my backup plan for if I can't access the schedule on the phone network is to just have a local copy on cassette tape, and load it directly.

I put quite a bit of effort into the software for this. It can show the timetable, and even filter it based on event type. You can view just a specific day's events, or even search in the title or speaker name fields.

The full 2024 timetable is not released yet, so I've used the 2022 timetable as a placeholder. I'm hoping that the 2024 timetable is distributed in the same JSON format that the 2022 timetable was. I'm also hoping that there's not significantly more data in the 2024 timetable, since it's such a struggle to fit it all in the spectrum's memory.

In fact, I already can't fit it all in. There's way too much text. My friend, Lissa, came up with a really clever text encoding scheme that gets the text down below 60% of its original size. She analysed a whole bunch of encoding and compression schemes before settling on this design. So, thank you Lissa! you made this a whole lot easier!

Instead of using the file as a stream of bytes, this encoding considers them to be a stream of bits. The encoding process analyses the text to create a list of characters ordered by frequency of use, and then assigns shorter encodings to the more frequently used characters. The encodings are chosen so that no short encoding is a prefix to a larger one. This is essential for the decoder to be able to identify when it has parsed a complete character.

The shortest encodings are only 3 bits long, but they get longer and longer as you have more unique characters. The encodings can get really long, and some characters are actually much larger in this encoding, but those are only the really infrequent characters, so overall you get a good space saving.

However, even with that I can't load all the text at once. Each event on the timetable has a detailed description attached to it. This is great, and I want to make sure that my program can display it. The only problem is that even just the description fields alone is too much to load all of it at once. The encoded description text is about 94k. The Spectrum only has 48k and after loading the program, map data, and other event information, there's only about 10k left over anyway.

The solution to this problem is to simply not load it all at once. If it's currently saturday, then I probably don't care too much about what happened on friday, or what's going to happen on sunday. So I don't need all of the descriptions for all of the events to be loaded at once. I've split up the descriptions into modules that can be loaded and unloaded as long as there is space in memory for them. In practice this means that you can only have one description module loaded at a time.

The Map

I decided it was worthwhile allocating a little bit of RAM for the map feature. This can stay loaded all the time. It does take up about 6k of RAM, but I think it will be worth it for the convenience of being able to navigate the camp in a hurry when a talk is about to start. It's not particularly detailed. There's only two zoom levels. The first one is just an overview of the camp, with only the biggest landmarks visible. You can zoom in on the north or south of the camp to see a little more detail. I mainly just want to be able to find all the venues listed on the timetable.

Screenshot of the map of the camp Screenshot of the map of the camp, zoomed in to the North side Screenshot of the map of the camp, zoomed in to the South side

Have a go!

If you want to, you can play around with the software. Download the tap file for use in your favourite ZX Spectrum Emulator. You can also find all the code and releases on GitHub

I've tried to design it so that it can easily be ported to some other systems too, so that if you're going to EMF camp and bringing your own retro laptop that isn't ZX Spectrum based, then you can use the same software with a little bit of porting effort. I'd love to add support for MS DOS and Amiga myself, but I doubt I'll find the time before the event. It's only a couple of weeks away. Update: There is now an MS DOS version and also a Linux version

Since I'm taking this around a field, I've tried to make it easy to carry. The rainbow guitar strap isn't just to make it look cool, and the screen hinge is also a sturdy carry handle. Fortunately, I even had the foresight to design this little spring-loaded telephone handset retention system, so that the phone stays in place while I have the laptop on my shoulder.

I'll probably be quite easy to spot in the field. If you do find me at EMF camp, and have a cassette recorder, you can take a copy of the software from my cassette tape if you like.

Or, even better: I've never actually used this user-to-user communication software that came with the Prism modem. I've only got one modem, and never known anyone else with one while I've also had a proper phone system to try it with. So if you have one at EMF camp, come find me! Let's try it out!

Blog post

A Very Silly CPU Monitor

Published: 2023-11-29

This blog post is also available in video form! Watch it on YouTube

We've all seen CPU usage indicators in Windows' Task manager, or htop, or whatever the MacOS thingy is called. But I thought it'd be cool if that wasn't just a number on the screen, but represented physically somehow.

And what better way to monitor a bunch of numbers, than to have a bunch of analog meters? I have fourteen of them, to be exact. My desktop PC reports 12 CPU load averages, so I have one for each of those, and I added in a couple of extras for memory usage and swap space usage.

The Monitor

I was originally planning to use a bunch of old AVO multimeters for this purpose. I have eight of them, which was enough for this project before I procrastinated it for so long that I had upgraded my computer. The problem is, they're huge, and not as easy to add coloured lighting to.

AVO meter

These little things are much more manageable. They're fairly cheap, only about £2.50 each. I got the 0-5mA displays, which I can drive directly from an Arduino using PWM through a 1k resistor. Of course, the 0-5mA scale that comes with the meters is not what I want, so I printed these custom scales for the various things I wanted. 12 different CPU% scales, all individually numbered, one for percentage of memory used, and another for percentage of swap space. It's fairly easy to add them in. The scales are printed onto little metal plates, so I just printed my replacement scales onto sticker paper and stuck them over the top.

Scale plate

If I was selling this as a complete product then I'd probably get the scales custom printed by the manufacturer, since that would look much neater. For a one-off though, I'm very happy with this.

I did consider making the memory and swap space meters use absolute scales, measured in gigabytes, but then it wouldn't be very future proof. I could easily upgrade the memory or swap space in my system and that would mean I'd need to replace the scale again.

As you can see, I also added LEDs for a little more pizzazz. By chance, I had an addressable LED strip lying around that happened to have exactly the right spacing to get two LEDs under each display with minimal spacing between the displays. I think the coloured lighting really adds a lot of charm.

The electronics for this is really simple. There's an arduino mega in the back, along with a little power supply for the LEDs, and a big mess of wires connecting all the displays up.

Wiring

Most of the frame is laser cut, and just slots together with little cut-outs and tabs. To make it really rigid, the main A-frame pieces are held together with some aluminium bars that I machined to length. It feels really strong, not like it's going to twist or bend at all.

I think it looks quite neat under my monitor, just quietly working away, letting me see at a glance how hard I'm pushing my computer, and providing an entertaining display whenever I start compiling some large software project for the first time.

Blog post

Printing Banners on the Sinclair ZX Spectrum (with a receipt printer)

Published: 2023-08-21

This blog post is also available in video form! Watch it on YouTube

I wanna show you this banner printer I made! It's not a very complicated system, except for one really specific part. Which of course means that's the part I want to talk about.

A pair of banners printed on till receipt paper in very large lettering. The top one says "DejaVu serif" and the bottom one says "The quick brown fox"

Before we get there though, let's have a look at the setup.

The Setup

There is:

  • a Sinclair ZX Spectrum, from 1982.
  • a cassette deck, of course.
  • a Kempston Centronics interface, for connecting to a printer, also 80s era.
  • And finally, a modern thermal printer.

A photo of the banner printer system set up. It contains all the components listed above, and also a more modern CRT television

Now, that last one is quite an anachronism, I know. So this isn't quite "how to print banners like it's 1982", but Sinclair's printer for this thing is incredibly slow, and it's hard to get the paper for it now. Also I don't have one anyway. So for now we get this.

I would love to see this work on a Sinclair printer, and it would be possible to do it, but actually the interesting part of making this work was interfacing with this modern printer fast enough to print banners at the printer's maximum speed.

Okay, so why was this hard? Sounds like an easy project right? just print the right sequence for each letter. What is this mysterious complicated thing I mentioned? well, let me show you version one of this system.

A simple, big, slow solution

So I want to print the letter 'A'. maybe I'll do that with some LPRINT commands, and use spaces and hashes to make empty pixels and filled pixels.

I just have to have a subroutine that looks like this:

935 LPRINT "   #                 "
936 LPRINT "   ####              "
937 LPRINT "   #######           "
938 LPRINT "     ########        "
939 LPRINT "       #########     "
940 LPRINT "       ##   #####    "
941 LPRINT "       ##      ##    "
942 LPRINT "       ##   #####    "
943 LPRINT "       #########     "
944 LPRINT "     ########        "
945 LPRINT "   #######           "
946 LPRINT "   ####              "
947 LPRINT "   #                 "
948 RETURN

Now I need another one, for the letter B, and another one for each other letter I want to be able to print, and all the punctuation, etc.

I did start manually writing these, but quickly realised that was a silly idea, and generated all the code for this from a font I had on my laptop. This works fine, except, it took nearly three whole minutes to load the resulting program from the tape. It's a loooong program.

Making it shorter, better, faster, stronger

So, the program takes a long time to load. It's slow to print, and also the banners are not particularly readable from a distance. The contrast between the filled and unfilled pixels is not great. We can solve both of these problems at once.

There's a lot of repetition in this file. Lots of lines that start with LPRINT, lots of long runs of spaces and long runs of hashes. It's clearly very compressible. In particular, the only things we really care about here are the image data, and the boundaries between the images.

The current solution uses an entire byte for each pixel, but we only have two colours anyway, so we want to reduce this to just one bit per pixel. So in this section, for the letter 'A', we've used 369 bytes to represent the image, but at one bit per pixel, tightly packed, the bitmap would only be 35 bytes long.

Clearly this first approach is very wasteful and we should switch to a packed bitmap format. However, switching to this tightly packed format produces another problem. Previously, the image data was encoded exactly how the printer was expecting it. It was already a stream of characters that just get sent to the printer as-is. Now we need to do an extra step before we can send it. We need to convert the bitmap into a stream of printable characters.

We're gonna have to do some bit manipulation on the data, to extract each pixel bit by bit and generate the characters on the fly. Unfortunately, Sinclair BASIC doesn't have bitwise operations. They can be constructed from other operations that are present, but it's much faster to just use machine code. Most of the program is still BASIC though, just the expanding of the font data will be done with machine code.

Since I was going to this much effort, I also decided to add some compression.

Take a look at this letter L. It's three identical lines, and then seven identical lines.

1067 LPRINT "   ##############    "
1068 LPRINT "   ##############    "
1069 LPRINT "   ##############    "
1070 LPRINT "   ##                "
1071 LPRINT "   ##                "
1072 LPRINT "   ##                "
1073 LPRINT "   ##                "
1074 LPRINT "   ##                "
1075 LPRINT "   ##                "
1076 LPRINT "   ##                "
1077 RETURN

It would be nice to have a way to say "repeat this line this number of times" and save repeating the same few bytes over and over. Turns out there's a really neat way to do this.

The width of the printer output in the mode that I'm using to print these banners is 21 characters. That means that I need 21 pixels per line. A byte has 8 bits, and therefore 8 pixels, so I need three bytes before I have enough bits for an entire line. That's 24 bits. So I have three bits spare before the end of the third byte after I've encoded the first line of pixels.

I could start the next line in those three bits, if I wanted to, but actually I chose to keep all the lines aligned to 3 bytes, and to use those three spare bits for something else.

Two of these bits are going to encode a number. This number is in the range 1 to 4, and it's the number of times to print the line. Of course, a two bit binary number normally has the range 0 to 3, and so I add one during decompression to get a number in the range 1 to 4. The last bit tells us if this is the last line of the bitmap. This lets us know where to stop printing each character.

first byte
---------------------------
|          7..0           |
|          bitmap         |
---------------------------
second byte
---------------------------
|          7..0           |
|          bitmap         |
---------------------------
third byte
---------------------------
|    7..3      | 2 | 1..0 |
|    bitmap    |stp| reps |
---------------------------

So the decompression routine builds a string representing the current line, calculates the number of times to repeat it, then sends that string to the printer the right number of times. Finally it checks if it has reached the end of the bitmap. If it has then it is finished printing this character, if not, it does the same thing again on the next three bytes.

One huge advantage of this approach is that I can use any pair of characters I want for the light and dark pixels with no extra cost. Previously I could only use characters that I could type on the spectrum keyboard. This printer has support for a character that is just a filled rectangle, it's perfect for this purpose. I use that character, and the space character, and get a much better contrast than before.

A comparison of the contrast of eavh printing method. The top banner uses the hash symbol for filled pixels, but the lower banner uses the block character. The contrast is much higher on the lower banner.

This loads soooo much faster too. We're down from 160 seconds of load time, to just 40 seconds. And now, since the font data is just a bitmap somewhere in memory, rather than a bunch of subroutines in a BASIC program, I can easily load other data in its place. So, naturally, I converted all of the fonts on my laptop into this format, and I loaded them all on to one big cassette tape.

It's a 90 minute cassette, and I did my best to fill it up, but only ended up with about 70 minutes of font data. I bet there's not many other people in the world that have 70 minutes worth of font data stored on a compact cassette.

A comparison of several different fonts printed using this banner printer system. Each example uses the phrase "The quick brown fox". From top to bottom, the fonts are as follows: DejaVu Serif, DejaVu Sans, Audiowide

I should have filtered through the fonts I had on my laptop, because actually lots of them don't even have the characters I care about and just rendered placeholders instead. But I had space to spare on the tape anyway, so it's not really a problem.

I also added in a name badge printing thing, because why not? And now I have possibly the silliest banner printing system there has ever been.

Blog post

terminal.doc: a Word document that is a terminal emulator

Published: 2023-05-29

This blog post is also available as a video series! Watch it on YouTube: Part 1 | Part 2

I decided to make a Word document that is also a terminal emulator. It doesn't have any practical purpose, I just made it for fun.

The first video linked above (Part 1) is a demo of me using the terminal emulator.

If you want to try this for yourself, you will need to be running Microsoft Word on Windows 10 or 11. You'll then need to download the terminal emulator document, open it in Word, and allow the macros to run.

This blog post explains how that document works.

The short answer

Some of you have predicted this already. It uses VBA scripts in the document, which load some kernel API functions from some Windows DLLs, and uses them to launch and interact with programs.

The long answer includes some interesting challenges though, and that's what the rest of this blog post is about.

A screenshot of the macro editor in Word, showing the macros definitions for terminal.doc

What makes a terminal emulator?

There's three important parts to a terminal emulator. There's:

  • a parser for the incoming data stream, from the program that is connected to the terminal, which determines how to update the terminal's text buffer.
  • a renderer to turn that buffer into something that can be displayed on the screen.
  • And finally there's a keyboard encoder which takes keyboard input data and turns it into a data stream to send to the connected program.

A diagram of the interactions between the parts listed above

The keyboard encoder part is normally very simple. Any modern OS provides keyboard input through a convenient abstraction layer. For most key presses, the terminal emulator just passes the character from the keyboard event directly to the program. Sometimes it needs to be a bit more sophisticated, though. For example arrow keys get turned into special control sequences.

The renderer part is the next most complicated part, as it requires interacting with graphics hardware. Again, modern OSes provide good abstraction layers for graphics output, but generally this part is still more complicated than input.

Finally, the parser for incoming data is the most complicated part. It is, in my opinion, what defines what a terminal emulator is. If you don't do this step and just put all the text on the screen without parsing it, then you wouldn't have a terminal emulator, you'd just have a thing that displays a raw data stream.

So these are the three things I needed to have in my terminal emulator. Now, over to Word. I'm sure the first question some of you had after seeing the demo was "how is this even possible inside a word document? Word documents are just static blobs of data, they don't contain code!"

Well, actually they can contain code. Microsoft's Visual Basic for Applications, AKA "VBA", is a lighter weight version of Visual Basic that Microsoft has embedded into many of the applications that they offer. It enables application functionality to be augmented by the user, or by other developers, without them needing to have the source code for the application itself. This is a really powerful tool for building useful things on top of existing code.

In the case of VBA in Word: the code that you wrote would be stored inside of a Word document or a template file, and would be available to anyone using that file. In Word, it is typically used to generate parts of documents. The classic example is mail merge, where you want to generate a bunch of letters and print them out and mail them to people given a database of names and addresses.

There is a big downside to this though. Allowing people to embed computer code into Word documents is risky. Suppose I write some VBA code that starts sending all the contents of your Documents folder over the internet to a web server that I own. Now I just need to email you a word document, and once you open it I start receiving confidential files.

By default, modern versions of Microsoft Word will not execute VBA code embedded in documents. There are modern alternatives to VBA that are much safer for the user, and so these are preferred instead. Of course, Microsoft doesn't want to break compatibility with older documents, so Word will ask you if you want to allow macros to run.

The reason why I'm using VBA for this instead of the modern MS Word extension API is because it's far more entertaining to have a Word document be a terminal emulator than it is to have a Word extension that's a terminal emulator. It's almost too easy if it's an extension. The VBA method also means I can share just a single Word file with everyone and it "just works" with no plugins to install.

The Technical Details

So how does this actually work?

1. Text Buffers and Rendering

Normally a terminal emulator would maintain buffers in memory that contain details about the text to put on the screen. Typically there is a buffer for text, and another buffer for formatting attributes. Then the rendering code takes the contents of these buffers and produces the final image you see on the screen.

In the case of my Word document terminal, I wanted to use the existing rendering features in Word. Since Word already has its own internal buffers for keeping text and formatting information around, I wanted to simply reuse these. This is one of the things that I did to make the task deliberately more difficult. It's more difficult because the user is able to directly interact with this buffer at the same time as my code does. If I was maintaining my own buffers then there would be much less opportunity for race conditions and mismatches in internal state.

You see, when my code needs to add to this buffer, or rather: add to the document, it needs to determine somehow where in the document to insert the new text, insert it there, set any attributes on it, and make sure that the cursor ends up in the right place.

It's hard to be sure if the cursor is already in the right place. The user can move the cursor in various ways. They can use arrow keys, click somewhere else in the document, or perhaps implicitly move the cursor by deleting or pasting in text. All of these actions are handled by Word directly, and not from my code. My code just has to deal with the consequences of them. In practice, the way that I deal with this problem is by ignoring it, mostly.

While my code will move the cursor if required by a specific control sequence from the program, it otherwise just allows the user to move it, and inserts new text wherever the cursor happens to be. The reality is that this is good enough for most use cases. (As if there are any real use cases for a terminal emulator in a Word document.) This is part of the reason why vim doesn't work very well in this terminal emulator. Vim explicitly moves the cursor around a lot, and expects it to remain wherever it puts it until it moves it again. If the user has moved the cursor in the meantime, then vim's rendering logic fails.

So, my terminal emulator is using the document as its buffer. This means that I get to entirely skip writing the rendering part of a terminal emulator, since Word is doing that for me.

2. Handling Keyboard Input

Word already does a bunch of things in response to keyboard input. One thing that it doesn't do, however, is provide VBA code with a way to listen to keyboard input events.

It is technically possible to receive keyboard events from a Form in VBA code, but that's not applicable here because I want this to work in the normal document pane. Since I can't receive the keyboard events from Word, I have to derive keyboard events from the effects that they had on the document.

I do this by using an infinite loop that repeatedly checks the document for changes and determines what was the most likely cause of the change.

For example: suppose in one iteration of my main loop I see that the previous state of the document was when it was 23 characters long in total, but now I see that in its current state it is 24 characters long. The document has one more character in it than it did before. I assume that this is because the user has typed a character. It's possible that they typed multiple characters between the times that I checked, and so perhaps it is 25 or 26 characters long. Regardless, I can determine how many new characters have appeared, and extract that number of characters from directly behind the current cursor position in the document.

Surprisingly this is actually quite effective. Similarly I can detect when the document gets shorter, and I can assume that this is the result of the user pressing backspace. I can then insert backspace characters into the input stream for the connected program.

I could also observe changes in the cursor position that don't correspond to changes in document length, and use those to deduce when the user has pressed arrow keys to move the cursor. In practice though, this is too complicated to be worthwhile. Naturally, the user can achieve the same effect in the document via multiple different means, so I cannot perfectly map document changes onto the keyboard events that produced them.

The theme of this project was deliberately making things hard for myself, and in this case I actually made things impossible for myself. Did the user just type "hello" by pressing H E L L O? or did they paste in the word hello with Ctrl-V? There is no way I can know. Similarly, if a keyboard action has no effect on the document, then I can't detect it. This is why I can't detect Ctrl-C and send an interrupt to the program.

This is acceptable, however. The benefit of writing a tool simply for entertainment purposes is that bugs that produce comically bad user experiences are actually features.

Okay, so I kinda have keyboard input. It's a bit janky, but it's good enough. Now all I need is a parser for terminal escape sequences.

3. Parsing Escape Sequences

This is the actual terminal emulator part. And actually this is the really boring part of the whole thing. Honestly I don't have much to say about it. It works as you would expect. The only strange thing it is doing is interacting with Word to maintain the resulting state, instead of maintaining its own buffers.

It moves the cursor around the word document instead of maintaining its own cursor position. It puts text directly into the document instead of buffering it itself. Again, there's a bit of jankiness here, but it works well as an entertainment piece.

The cursor doesn't move around exactly as expected. It struggles to delete the right sections of text when it needs to overwrite something that's already there. It takes a long time to complete certain actions. But it's fine! I could have spent more time on it and probably gotten Vim to be almost usable like this, but I decided to just leave it. It was entertaining already.

And then the other little annoying bits

Now onto some strange little things that came up while I was writing this.

First of all, you probably saw in the other video that Vim doesn't believe it's connected to a terminal. I joked about Vim being rude, but actually it's technically correct. You see, for historical reasons that I don't fully understand, some programs care about if they are connected to a terminal or not. Most modern OSes provide a mechanism for a program to query "hey am I connected to a terminal?", and since we have terminal emulators, the OSes also have to provide a way for a terminal emulator to tell the OS "hey, I'm a terminal emulator, if this program asks if it's connected to a terminal, say yes!".

If you want to read about how Linux handles this, have a read of the documentation for the ioctl tty syscalls (man ioctl_tty). You can also open a process on a pseudoterminal device as documented in man pty.

Windows handles this very differently, of course. There is a guide for creating pseudoconsoles which explains the normal way to do this on windows.

So why didn't I just do that so that this worked properly? Well, I tried, but I couldn't figure out how to do it. You see, this is my first ever VBA project, and perhaps a more experienced VBA dev would be able to make this work, but I couldn't figure it out.

I know exactly what sequence of system calls are required, and I know what values to give them. The problem is just that these system calls have a more complicated set of arguments and I don't really know VBA well enough to get all the right pointers to these arguments. In the end I gave up, and just went with the thing that kinda worked already.

The other effect this had was that I couldn't escape the default config that programs use when they don't think they are connected to a terminal. In particular: bash echos the characters it receives by default. The problem this causes for me is that Word is also adding the same characters to the document, so every letter typed appears twice. This is something that you can normally configure by setting a flag in the virtual terminal device. Since I had given up on getting that working, instead I had to workaround this. So when bash is running, my terminal emulator deletes each character that Word adds to the document, so that only bash's echoed character remains.