Recently Antirez made a post documenting a simple editor in 1k of pure C, the post was interesting in itself, and the editor is a cute toy because it doesn't use curses - instead using escape sequences.
The github project became very popular and much interesting discussion took place on hacker news.
My interest was piqued because I've obviously spent a few months working on my own console based program, and so I had to read the code, see what I could learn, and generally have some fun.
As expected Salvatore's code is refreshingly simple, neat in some areas, terse in others, but always a pleasure to read.
Also, as expected, a number of forks appeared adding various features. I figured I could do the same, so I did the obvious thing in adding Lua scripting support to the project. In my fork the core of the editor is mostly left alone, instead code was moved out of it into an external lua script.
The highlight of my lua code is this magic:
--
-- Keymap of bound keys
--
local keymap = {}
--
-- Default bindings
--
keymap['^A'] = sol
keymap['^D'] = function() insert( os.date() ) end
keymap['^E'] = eol
keymap['^H'] = delete
keymap['^L'] = eval
keymap['^M'] = function() insert("\n") end
I wrote a function invoked on every key-press, and use that to lookup key-bindings. By adding a bunch of primitives to export/manipulate the core of the editor from Lua I simplified the editor's core logic, and allowed interesting facilities:
- Interactive evaluation of lua.
- The ability to remap keys on the fly.
- The ability to insert command output into the buffer.
- The implementation of copy/past entirely in Lua_.
All in all I had fun, and I continue to think a Lua-scripted editor would be a neat project - I'm just not sure there's a "market" for another editor.
View my fork here, and see the sample kilo.lua config file.