Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 14 · From Scratch · Systems from scratch · about 17 min to read

Play it as a journeyTutor comes with a plan

A text editor of your own

Capstone Keypress to Pixel
Capstone Keypress to Pixel · Open board

: the road from a key you press to a letter on the screen. You will write the program in the middle of that road, the one that reads the key, changes the text and draws the screen again.

Salvatore Sanfilippo, who goes by antirez and who also created Redis, wrote a text editor and called it Kilo. The name comes from its size, because the whole editor is under 1024 lines of C. It uses no library for the screen, not even curses. It still has search and colours for code. The copyright line in its source says 2016, and he published it as a small program that anyone can read from top to bottom.

That is the surprise. An editor feels like a huge thing, but the core is small. Your terminal sends each key to the program, and the program must do everything else itself: keep the text, change it, undo the change, find words and paint the screen.

By the end you will be able to write a working terminal editor of 125 lines in Python. It will type, move, save, search, undo and redraw. You will also know why it stores its text the way it does.

Before you start. A terminal sends a signal when you press Ctrl-C, and a program decides what to do with it (The tap on the shoulder). A program reads the keyboard through a numbered door called a file descriptor, number 0 (Everything is a file descriptor). In the shell lesson the terminal chose which program got Ctrl-C and Ctrl-Z (A shell of your own). Here you take the keyboard for yourself and switch those helpers off. Growing a block of memory when it is full is the same problem that an allocator solves (A malloc of your own).

Lesson 110 of 117 in From Scratch

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • Raw mode is a terminal that sends every key straight to your program
  • An escape sequence is a short run of bytes that starts with byte 27
  • A gap buffer is the text with an empty hole at the cursor
  • An undo log is a list of changes, each one reversible
  • Search moves the cursor as you type each letter
  • The test run
  • Edge cases and limits of this editor
  • Where the real ones stand

Terms it explains: cooked mode, raw mode, escape sequence, gap buffer, rope.

Review From Scratch