stty cbreak /usr/bin/env retro $0 $1 stty -cbreak exit
Hua is a small, functional text editor written in RETRO for Unix systems. It is line oriented, visual, and easy to learn.
Hua is intended to run as a standalone tool. Use a line like:
edit.forth filename
To create a new file:
edit.forth new filename
Hua saves changes as you edit the file. I advise using it along with a version control system so you can revert changes when needed.
Since this runs as a standalone application I use a quick check to exit if no arguments were passed.
If I get here, a filename was provided. So I start by creating a few variables and constants.
Get the name of the file to edit.
The configuration here is for two items. The number of lines from the file to show on screen, and the name of the temporary file to use when editing.
Next are the variables that I use to track various bits of state.
To create a new file, Hua allows for the use of new followed by the filename. I handle the file creation here.
This is just a shortcut to make writing strings to the current file easier.
I now turn my attention to displaying the file. I am aiming for an interface like:
<filename> : <line-count> --------------------------------------------------------------- * 99: 100: :n:square dup * ; 101: 102: This is the current line 103: --------------------------------------------------------------- j: down | k: up | ... other helpful text ...
The denotes the currently selected line.
I start with words to count the number of lines in the file and
advance to the currently selected line.
Now for words to format the output. This should all be pretty
clear in intent.
clear-display uses an ANSI/VT100 escape sequence. This might
need to be adjusted for your chosen terminal.
This just displays the separator bars.
Next, a word to display the header. Currently just the name of
the file being edited and the line count.
The pad word is used to make sure line numbers are all the
same width.
A line has a form:
<indicator><number>: <text><eol>
The indicator is an asterisk, and visually marks the current
line.
EOL is optional. If ShowEOL is TRUE, it'll display a ~ at
the end of each line. This is useful when looking for trailing
whitespace. The indicator can be toggled via the ~ key.
With the code to display the file done, I can proceed to the
words for handling editing.
I add a custom combinator, process-lines to iterate over the
lines in the file. This takes a quote, and runs it once for
each line in the file. The quote gets passed two values: a
counter and a pointer to the current line in the file. The
quote should consume the pointer an increment the counter. This
also sets up FID as a pointer to the temporary file where
changes can be written. The combinator will replace the
original file after execution completes.
Additionally, I define a word named current? which returns
TRUE if the specified line is the current one. This is just
to aid in later readability.
So first up, a word to delete all text in the current line.
Then a word to discard the current line, removing it from the file.
And the inverse, a word to inject a new line into the file.
Replacing a line is next. Much like the delete-line, this
writes all but the current line to a dummy file. It uses an
s:get word to read in the text to write instead of the
original current line. When done, it replaces the original
file with the dummy one.
The next four are just things I find useful. They allow me to
indent, remove indention, trim trailing whitespace, and insert
a code block delimiter at a single keystroke.
And then a very limited form of copy/paste, which moves a copy
of the current line into a CopiedLine buffer and back again.
The line buffer is 1024 characters long, use of a longer line
will cause problems.
One more set of commands: jump to a particular line in the
file, jump to the start or end of the file.
And now tie everything together. There's a key handler and a
top level loop.
Run the editor.