Python has a hidden small text editor available. With Emacs Keybindings.

While attempting to start a new project that I knew I would never finish, I found out a pretty interesting module in the curses library. I was planning on picking a project from a pretty cool aggregator of side project ideas: https://github.com/codecrafters-io/build-your-own-x . As you can see, there are a lot of different projects that go from very low level things to more abstract or complex systems. I decided to go with creating my own text editor. However, I noticed that the examples in the aggregator were not for terminal based editors, which was what I wanted to do. I remember that anthonywritescode had written his own terminal text editor in Python, and so I decided to have a look at the repo of his project. I noticed that it relied on the curses ( https://docs.python.org/3/library/curses.html ) built in library, which, to be honest, I had never worked with, so I decided to look at Python's curses documentation and found out that it provides an exmple of its use to make a very basic message/text editor: https://docs.python.org/3/howto/curses.html

So, if you want to try it out you can just run the following snippet from the documentation page:

  import curses
  from curses.textpad import Textbox, rectangle

  def main(stdscr):
      stdscr.addstr(0, 0, "Enter IM message: (hit Ctrl-G to send)")

      editwin = curses.newwin(5,30, 2,1)
      rectangle(stdscr, 1,0, 1+5+1, 1+30+1)
      stdscr.refresh()

      box = Textbox(editwin)

      # Let the user edit until Ctrl-G is struck.
      box.edit()

      # Get resulting contents
      message = box.gather()

The example it provides is very basic, but the funny and interesting thing to me was that this example editor comes with Emacs-like keybindings, a very pleasant surprise since, as maybe some of you know, I am quite an avid Emacs user and enthusiast. I decided then to build upon the foundation of this textpad textbox example (https://github.com/python/cpython/blob/main/Lib/curses/textpad.py) and build my own text editor as a new side project. The curses module provides cursor movement, character deletion, line cutting. However, that is it. So, I have already implemented copy/paste, region selection, a primitive mark feature (as in Emacs) and will keep on building the minimally necessary features to make this somewhat usable, just for the fun of it, trying to make it as Emacs-like as possible by including undoing undos and evaluating expressions to extend the editor's functionality. At least let's see how long this side project will last before ending up in the "unfinished" and forgotten bin. Moreover, working on a text editor means having to brush up patterns and data structures that may not be frequently used in your day job (as the command pattern for example to implement undo).

Finally, out of curiosity, I have dug into the origin of this module and found out it was written by Andrew Kuchling at least 23 years ago! So, in case he ever reads this, thank you very much for the nice textpad example and for providing the direction to start a fun little side project.