4 ms·
Build Your Own Text Editor (2017)
- unsafecast 4y agoI _love_ this style of tutorial. It really does feel like a Lego book. Amazing.
- teddyh 4y agoWrite your own Emacs: https://www.finseth.com/craft/ https://www.finseth.com/craft/
- heroku 4y ago
- laerus 4y ago[flagged]
- t-3 4y agoEverybody? It's been the language for open source software since the beginning. "Who likes C?" is a different question, but using it is often just plain practical. Almost all mainstream languages are syntactically close to C, so it's easy to read. Documentation for using C interfaces ships with every *nix, making it easy to write. Memory safety and footguns don't matter at all for the vast majority of programs, especially not the short and simple "do-one-thing"-type programs that are most non-commercial code.
- bediger4000 4y agoThis is a lot of fun. I used this step by step tutorial to write the same editor in Go.
- timsneath 4y agoMe too. It started a whole chain of events for me: - I used the tutorial to write it in Dart (https://github.com/timsneath/dart_console/blob/main/example/kilo.dart https://github.com/timsneath/dart_console/blob/main/example/...) - I refactored the console pieces (raw mode, ANSI VT sequences) into a separate package (https://pub.dev/packages/dart_console https://pub.dev/packages/dart_console) - I wrote Windows interop code for the previously Unix-only parts of the editor - I created a separate package for the Dart/Win32 interop pieces (https://pub.dev/packages/win32 https://pub.dev/packages/win32) - I wound up growing and expanding this package to the full Win32 surface area - Another OSS contributor took and extended my work to support the WinRT Now there are tens of Dart packages that use the Windows interop work that all started with this little tutorial on a completely unrelated area. So thank you to the author!
- bediger4000 4y agoThank you for following up. I refactored my original Go version into somewhat more idiomatic Go. I'll compare with your Dart version to see if the packages I ended up with are "organic" or just determined by Go peculiarities.
- bmitc 4y agoIs your code available somewhere, like on GitHub?
- bediger4000 4y agoSure. I'm bediger4000 there too
- peteforde 4y agoAnyone reading this who might be on the fence... this project is 10/10 excellent. It's absolutely worth doing.
- sprkwd 4y agoI did it. And at the end of it I had a text editor. And it made me feel like a programmer.
- sys_64738 4y agoThe memmove stuff suggests not using the unlimited undo technique. I only did a very brief scan but didn't see reference to undo.
- BaculumMeumEst 4y agoI'm really grateful people like author exist. This is awesome and I'm so excited to go through it.
- mrlonglong 4y agoThere's also a Rust version of this somewhere on the web. I'd dig out the URL but it's long forgotten.
- elashri 4y agoHere we are https://github.com/rhysd/kiro-editor https://github.com/rhysd/kiro-editor
- pflenker 4y agoI wrote this a couple of years back - it’s a bit out of date though: http://www.philippflenker.com/hecto/ http://www.philippflenker.com/hecto/
- mrlonglong 4y agoAh, great, yes that's the one I was referring to. Thanks for posting.
- signaru 4y agoMakes me wonder what was used to program the first text editor...
- vaylian 4y agoI'm pretty sure it was punch cards
- bmitc 4y agoThis is really cool! I’m definitely gonna try this out in one of my favorite languages. I love these types of project-based learning. You learn so much by going through them.
- thomastjeffery 4y agoChapter 2 is an excellent dive into some of the more unfamiliar and confusing features emulated by terminal emulators. It would be neat to have an equivalent set-up using a GUI toolkit instead, but the terminal is good enough to work with. In a broader sense, it's pretty frustrating to know that we are still emulating features (like ctrl-[s,q]) that only really made sense in the context of a physical terminal. The amount of work and frustration we could save with a modern equivalent to terminal emulators (without historical baggage) would be really significant.
- rollcat 4y ago> The amount of work and frustration we could save with a modern equivalent to terminal emulators (without historical baggage) would be really significant. This has been occupying my mind for over a decade, but I can't come up with a design that I like. For "TUI" apps: something like a GUI toolkit, but should remain reasonably performant over a poor connection (think how mosh tries to hide input latency), and the wire protocol should be something you can bang out in bash. For shells: the shell should not be concerned with things like keyboard shortcuts, line wrapping, or positioning the cursor - this should be done on the client (terminal). You should be able to set a proportional font, if that's what you like. The shell should be able to trivially request displaying a picture, report on the status of a background task, or present an interactive progress bar. Maybe this is two different projects / efforts, maybe one can build on top of another.
- thomastjeffery 4y agoTL;DR: I'm about to go on a lengthy rant, because I too have spent over a decade obsessing over this subject. I feel like I've been starting to actually get somewhere, but there's still a lot up in the air. Also my meds are wearing off, so all eighdy HD's are present. Read at your own risk... ----- It's a game of compromises. The first problem is to decide what goals you value most. The current compromises are decided by the goal of backwards compatibility. In that sense, we are "steeped in tradition". It's a mess, but at least decisions have been made; and we are recognizing - right here, right now - just how significant that fact is on its own. > For "TUI" apps: something like a GUI toolkit, but should remain reasonably performant over a poor connection That sounds a lot like what web browsers are; which reminds me of the sheer magnitude of options we have available. It's easy to get stuck with decision fatigue. We aren't making a universe simulator here, so we need to break this project down to a smaller domain. The constraints of the problem domain are what define a project, so pinning them down is 90% of the work. I'm thinking back at what my motivation was in the first place: frustration with the status quo. Having a simple text UI interface is evidently useful. The shell is the first and last thing I use: it's my home, and that's why I care so much in the first place. Something with generally the same constraints of a terminal emulator & shell is what I want. My frustration lies with the complexity in both the terminal emulator itself, and the way a shell (and every other terminal-based program) interfaces with that. So what's the bare minimum that these two things need to function? First, the terminal needs input and output. But these needs are really just an extension of the text-based program's needs, right? So what does a shell or a text editor need to function? Well, it needs input and output. Hold on now, do we really need the terminal emulator? Why not have the shell do that? Well, there are some reasons: Shells can be run without any IO. Inside shells, we can pipe programs into each other. Those pipes are made of text. Is that the domain we want? Well, using text as the interface is one of my main frustrations with terminal emulators: even the ASCII table itself shoves key-presses into the domain of text, and that's an ugly hack I don't want to keep. There are functional needs for dropping that, too: "ctrl-m" is the same ASCII code as "enter", and keyboard layouts aren't always very compatible with ASCII in the first place. There are too many assumptions baked in here. And while we are on the topic of ASCII, escape characters are the ugliest hack in here. On the terminal, we use them for input - to extend the ASCII table to accommodate a few more keys - and for output - for decoration like text color, boldness, and even flashing background. It's like a markup language, but made out of non-drawable characters that were supposed to be typed with the escape key. There are so many ugly edge cases here, you could write a novel about them! But shells aren't always about plain text. You can pipe gzip to dd or a file, and it will totally work. But if you pipe it to your terminal output, weird things can happen. That's because some raw binary looks like escape characters, and we are already solving that problem by making user input a separate thing. Progress! So what do we do with keystrokes? Let the user decide! But what data will end up getting sent to the program? The status quo is to be a REPL by default. That's the foundation for a lot of the ugly assumptions we just dropped. It's the reason that the OP has two whole chapters just on disabling that behavior. But most programs we invoke from a shell are either printing, or running a REPL of their own. And a lot of them are bad at it, which is why GNU readline is a thing. There's even rlwrap, which wraps a program in readline! That's very helpful, but I would love to never need it again. Plus, readline makes some UI decisions of its own, like its emacs-like key bindings, and I would rather it not. So the big question here is about how UI should flow. That question gets me all kinds of excited, and all kinds of overwhelmed. This is where I start shifting my focus away from shells and back to text editors. --- Emacs has its own shell. But Emacs isn't a dumb terminal emulator. It's built out of lisp, which represents data as text in a much cleaner way than ASCII escape sequences could ever dream of. And the way its interface flows is definitely more interesting than a REPL. In fact, some of the crazy shit you can do in eshell is one of the main things that get me thinking about shells and terminals and the ways they hold me back. And if you really want a good example of how text-based UI can flow, look no farther than Vim. The modular flow is elegant and efficient in ways that the primitive REPL could only dream of. Yet both of these wonderful programs bring me back to the very same frustrations I have with terminal emulators and shells: they are steeped in tradition! Vim was my first. I was happy to sin against the Church of Emacs, the stubborn atheist I am, and vi is a seductive mistress. But while vi could do most anything I wanted, and it could bend and extend in many exciting ways, I still managed to find its limits. When I started typing with a non-standard keyboard layout (workman), I tried remapping Vim's keys to compensate. First you remap a key, then you remap the one it replaced, then the next, etc... and eventually you hit a circular dependency. It can't be done. So I decided to take the opportunity to learn me an Emacs. Emacs is incredibly customizable. If you can navigate your way into its elisp structures, you can mold it into anything you want. But what Emacs already is by default may be easy to decorate and add to, it's not so easily unmade. I had big ideas about making my own vi-style modular editing keybindings once, but I never could quite find my way. Every move I made, I found myself tripping over what was already there; what I had come to replace. The whole time, in the back of my mind, my conscience screamed, "We just need a clean slate! Emacs without the defaults. VoidMACS."
- z3t4 4y ago> We can no longer just feed the substring of render that we want to print right into abAppend(). We’ll have to do it character-by-character from now on. The more functionality you add to a program, the more complicated and slower it becomes. You can add optimizations and abstraction layers. But the best way to write a bug free and efficient program is to keep it small and simple. If you do need those functions though, you can make your life easier by writing tests before implementing a feature. For example before writing the code that colors numbers, write a test that checks if numbers has a color, and a test that measure the time it takes to render a line - and have it fail if it becomes significant slower. So as an exercise you could write a test for each step in the tutorial.
- jeadie 4y agoI did this a few months ago in Golang and it was so much fun. Can 100% recommend. Shameless plug -> https://github.com/Jeadie/gram https://github.com/Jeadie/gram
- notpushkin 4y agoFascinating read. See also: • From Scratch series by Gary Bernhardt, e. g. the Text Editor From Scratch: https://www.destroyallsoftware.com/screencasts/catalog/text-editor-from-scratch https://www.destroyallsoftware.com/screencasts/catalog/text-... • Rope Science, a series of posts about planning Xi editor internals: https://xi-editor.io/docs/rope_science_00.html https://xi-editor.io/docs/rope_science_00.html
- pkphilip 4y agoI wrote one in 8086 assembly in my early 20s. Was a fun experience.
- garfieldnate 4y agoOnce in a while I see something about a text editor using the GPU to accelerate rendering of fonts. I would love to see some tutorial material on this. Generally speaking using the GPU is a complete mystery to me, as is anything to do with rendering fonts.