Sure, I’d be happy to help you with that. Here’s your corrected write-up:

Introduction

I really enjoy using org mode. It’s incredibly easy to write in a simple text editor like Emacs or Vim, yet it’s capable of expressing most complex linear text formatting. However, the issue is that it’s primarily handled within Emacs Lisp, which I find to be a rather unpleasant language. It might have many cool features, but it’s unpleasant to look at, uncomfortable to write, and has virtually no support outside of Emacs.

To make my life a bit easier, I started writing a parser for org-mode in Rust. It’s definitely far from feature-complete and currently can only handle one simple task. However, I hope I’ve written it in a way that will make it easy for me to expand and modify it to suit my needs.

How does it work

The program is set up into four stages. If you want to add features, I hope that you would only have to focus on one of these steps at a time.

Reading in and parsing

As org is still just a text file, the content gets stored in simple text files. When parsing, you have to go through the file line by line, read it, and process it. This is saved into a Struct named Context. The first parsing to identify the type of line also occurs during this stage. In the end, there should be a Vector containing every line and information about what type it is, such as a heading, text block, empty line, etc. These are then passed to the tree creation function.

Creating tree

Every type now needs to be parsed, and most importantly, assigned a value. This is necessary to parse them into a tree. The values indicate which block is underneath any other block. Empty lines and text blocks have the lowest priority, with no other ObjectType beneath them. Conversely, a level one heading has one of the highest priorities, so almost everything else that follows will be underneath this heading.

This program goes through the list and recursively splits the blocks, ordering them underneath other blocks. In the end, there exists a tree of structs that represent the org file.

Actions

This is where one could write custom functions to handle specific actions on this tree. Rust becomes a compiled org scripting language at this point.

I have written two examples:

  • Updating all Todo items that have the LOOP tag to the next specified timestamp or just the next day.
  • Separating the DONE todo items from the rest of the org tree.

Building and writing

The Rust struct cannot be written to a file directly. Therefore, there needs to be a build function that recursively goes through the tree and builds a String representation of the parsed org mode, then writes it to a file.

Why Rust

To be honest, I don’t think it was the ideal language for the job. Besides having nice structs and enum support, I guess it worked, so I’m satisfied.