Porting the kilo text editor as a micropython module (work in progress)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
JohnLittle
Posts: 11
Joined: Mon Oct 08, 2018 7:10 pm

Porting the kilo text editor as a micropython module (work in progress)

Post by JohnLittle » Sun Apr 07, 2019 12:58 pm

I was looking for a small text editor that I could integrate with micropython as a C module and found kilo http://antirez.com/news/108

Someone in the comments even talks about running it on a microcontroller and gives pointers to reducing its memory footprint.

For now, I've only tested this on the unix port, but here's what I did. The idea is that you'll be able to:

Code: Select all

import kilo
kilo.edit("somefile.py")
Edit the file, then saving (ctrl-s) and/or quitting (ctrl-q) will return you to the repl.

First, my mangled kilo.c is here: https://github.com/j0hnlittle/kilo/blob ... hon/kilo.c

It includes some basic highlighting for Python keywords (better python support in that fork that could be lifted later: https://github.com/practicalswift/opene ... penemacs.c) and handles resizing the terminal window. Variable sized tabs would be nice but that's also for later.

Following https://micropython-dev-docs.readthedoc ... odule.html, you need to add kilo.c to your Makefile along the other source files:

Code: Select all

# source files
SRC_C = \
        kilo.c \
        main.c \
And modify mpconfigport.h by adding both this line with the other "extern const":

Code: Select all

extern const struct _mp_obj_module_t mp_module_kilo;
... and the last line in this block:

Code: Select all

#define MICROPY_PORT_BUILTIN_MODULES \
    MICROPY_PY_FFI_DEF \
    MICROPY_PY_JNI_DEF \
    MICROPY_PY_UTIME_DEF \
    MICROPY_PY_SOCKET_DEF \
    { MP_ROM_QSTR(MP_QSTR_umachine), MP_ROM_PTR(&mp_module_machine) }, \
    MICROPY_PY_UOS_DEF \
    MICROPY_PY_USELECT_DEF \
    MICROPY_PY_TERMIOS_DEF \
    { MP_OBJ_NEW_QSTR(MP_QSTR_kilo), (mp_obj_t)&mp_module_kilo }, \
Not sure how that would work on an ESP8266 but anyway, I thought I'd share if anyone wanted to try something similar :-)

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Porting the kilo text editor as a micropython module (work in progress)

Post by Roberthh » Sun Apr 07, 2019 2:34 pm

A few years ago i started a text editor purely written in Python for Pyboard, ESP8266, Wipy1, ...
You'll find it here: https://github.com/robert-hh/Micropython-Editor
It works best when integrated into frozen bytecode. On ESP8266 it's the only way to use it. Meanwhile It got a little bit too large for WiPy1, but on the other devices it works well. The more memory the better. Th eongoing effort is to keep it small and to resist the attempt to add features.

User avatar
JohnLittle
Posts: 11
Joined: Mon Oct 08, 2018 7:10 pm

Re: Porting the kilo text editor as a micropython module (work in progress)

Post by JohnLittle » Sun Apr 07, 2019 3:13 pm

Thanks Robert,

it was definitely on my TODO list to try and test your editor.

I am completely new to micropython development, so one of the reason I went with the kilo editor was to learn about developing modules in C. A single file (kilo.c) made this relatively painless.

It'll be interesting to compare memory usage with your editor. You mentioned the ESP8266 and that's where I'm headed next. Well, if I manage to cross-compile kilo to it.

Cheers for now!

Post Reply