2019-10-14 17:17:43 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
stty cbreak
|
|
|
|
/usr/bin/env retro $0 $1
|
|
|
|
stty -cbreak
|
|
|
|
exit
|
|
|
|
|
|
|
|
---------------------------------------------------------------
|
2019-05-14 13:19:49 +02:00
|
|
|
_ _
|
|
|
|
| | | |_ _ __ _
|
|
|
|
| |_| | | | |/ _` |
|
|
|
|
| _ | |_| | (_| |
|
|
|
|
|_| |_|\__,_|\__,_|
|
|
|
|
line oriented editor
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2017-11-12 20:50:52 +01:00
|
|
|
# Hua: a text editor written in RETRO
|
|
|
|
|
2019-05-14 13:19:49 +02:00
|
|
|
Hua is a small, functional text editor written in RETRO for
|
|
|
|
Unix systems. It is line oriented, visual, and easy to learn.
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
## Starting
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
## A Word of Warning
|
|
|
|
|
|
|
|
Hua saves changes as you edit the file. I advise using it along
|
2019-05-14 13:19:49 +02:00
|
|
|
with a version control system so you can revert changes when
|
|
|
|
needed.
|
2019-02-02 05:45:38 +01:00
|
|
|
|
|
|
|
## The Code
|
|
|
|
|
|
|
|
Since this runs as a standalone application I use a quick check
|
|
|
|
to exit if no arguments were passed.
|
2017-11-12 06:20:14 +01:00
|
|
|
|
|
|
|
~~~
|
2019-05-14 13:19:49 +02:00
|
|
|
sys:argc n:zero? [ 'No_file_specified! s:put nl #0 unix:exit ] if
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
If I get here, a filename was provided. So I start by creating
|
|
|
|
a few variables and constants.
|
|
|
|
|
2019-05-14 13:29:09 +02:00
|
|
|
Get the name of the file to edit.
|
|
|
|
|
|
|
|
~~~
|
2020-07-06 04:49:49 +02:00
|
|
|
#0 sys:argv s:keep 'SourceFile var-n
|
2019-05-14 13:29:09 +02:00
|
|
|
~~~
|
|
|
|
|
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
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.
|
2017-11-12 17:07:44 +01:00
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
2019-05-14 13:19:49 +02:00
|
|
|
#70 'COLS const
|
2017-11-15 02:26:50 +01:00
|
|
|
#16 'MAX-LINES const
|
2019-05-14 13:29:09 +02:00
|
|
|
'/tmp/hua.editor.scratch 'TEMP-FILE s:const
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
Next are the variables that I use to track various bits of
|
|
|
|
state.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
'CurrentLine var
|
|
|
|
'LineCount var
|
|
|
|
'ShowEOL var
|
|
|
|
'FID var
|
|
|
|
'CopiedLine d:create #1025 allot
|
|
|
|
~~~
|
|
|
|
|
|
|
|
To create a new file, Hua allows for the use of `new` followed
|
|
|
|
by the filename. I handle the file creation here.
|
|
|
|
|
|
|
|
~~~
|
2017-11-17 22:01:51 +01:00
|
|
|
@SourceFile 'new s:eq?
|
|
|
|
[ #1 sys:argv s:keep !SourceFile
|
|
|
|
@SourceFile file:A file:open file:close ] if
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
This is just a shortcut to make writing strings to the current file
|
|
|
|
easier.
|
|
|
|
|
|
|
|
~~~
|
2018-05-07 18:36:37 +02:00
|
|
|
:file:s:put (s-) [ @FID file:write ] s:for-each ASCII:LF @FID file:write ;
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
I now turn my attention to displaying the file. I am aiming for
|
|
|
|
an interface like:
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
<filename> : <line-count>
|
|
|
|
---------------------------------------------------------------
|
|
|
|
* 99:
|
|
|
|
100: :n:square dup * ;
|
|
|
|
101:
|
|
|
|
102: This is the current line
|
|
|
|
103:
|
|
|
|
---------------------------------------------------------------
|
|
|
|
j: down | k: up | ... other helpful text ...
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
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.
|
2017-11-12 05:28:24 +01:00
|
|
|
|
|
|
|
~~~
|
2017-11-12 06:20:14 +01:00
|
|
|
:count-lines (-)
|
2017-11-13 18:45:36 +01:00
|
|
|
#0 @SourceFile [ drop n:inc ] file:for-each-line dup !LineCount ;
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
:skip-to
|
2017-11-13 20:43:04 +01:00
|
|
|
@CurrentLine MAX-LINES #2 / - #0 n:max [ @FID file:read-line drop ] times ;
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
Now for words to format the output. This should all be pretty
|
|
|
|
clear in intent.
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
`clear-display` uses an ANSI/VT100 escape sequence. This might
|
|
|
|
need to be adjusted for your chosen terminal.
|
2017-11-12 17:07:44 +01:00
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
|
|
|
:clear-display (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
ASCII:ESC dup '%c[2J%c[0;0H s:format s:put nl ;
|
2017-11-12 17:07:44 +01:00
|
|
|
~~~
|
2017-11-12 06:20:14 +01:00
|
|
|
|
2017-11-12 17:07:44 +01:00
|
|
|
This just displays the separator bars.
|
|
|
|
|
|
|
|
~~~
|
2017-11-12 06:20:14 +01:00
|
|
|
:---- (-)
|
2019-02-03 23:45:12 +01:00
|
|
|
#7 [ $- c:put ] times
|
|
|
|
#0 COLS #10 / [ dup n:put #9 [ $- c:put ] times n:inc ] times n:put nl ;
|
2017-11-12 17:07:44 +01:00
|
|
|
~~~
|
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
Next, a word to display the header. Currently just the name of
|
|
|
|
the file being edited and the line count.
|
2017-11-12 06:20:14 +01:00
|
|
|
|
2017-11-12 17:07:44 +01:00
|
|
|
~~~
|
2017-11-12 06:20:14 +01:00
|
|
|
:header (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
count-lines @SourceFile '%s_:_%n_lines\n s:format s:put ;
|
2017-11-12 17:07:44 +01:00
|
|
|
~~~
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
The `pad` word is used to make sure line numbers are all the
|
|
|
|
same width.
|
2017-11-12 17:07:44 +01:00
|
|
|
|
|
|
|
~~~
|
2017-11-12 05:28:24 +01:00
|
|
|
:pad (n-n)
|
2019-02-02 05:45:38 +01:00
|
|
|
dup #0 #9 n:between? [ '____ s:put ] if
|
|
|
|
dup #10 #99 n:between? [ '___ s:put ] if
|
|
|
|
dup #100 #999 n:between? [ '__ s:put ] if
|
|
|
|
dup #1000 #9999 n:between? [ '_ s:put ] if ;
|
2017-11-12 17:07:44 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
A line has a form:
|
|
|
|
|
2017-11-14 03:15:34 +01:00
|
|
|
<indicator><number>: <text><eol>
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
The indicator is an asterisk, and visually marks the current
|
|
|
|
line.
|
2017-11-12 17:07:44 +01:00
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
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.
|
2017-11-14 03:15:34 +01:00
|
|
|
|
2017-11-12 17:07:44 +01:00
|
|
|
~~~
|
2017-11-12 06:20:14 +01:00
|
|
|
:mark-if-current (n-n)
|
2018-05-07 18:36:37 +02:00
|
|
|
dup @CurrentLine eq? [ $* c:put ] [ sp ] choose ;
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
:line# (n-)
|
2018-05-07 18:36:37 +02:00
|
|
|
n:put ':_ s:put ;
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2017-11-14 03:15:34 +01:00
|
|
|
:eol (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
@ShowEOL [ $~ c:put ] if nl ;
|
2017-11-14 03:15:34 +01:00
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
:display-line (n-n)
|
2017-11-15 02:38:42 +01:00
|
|
|
dup @LineCount lt?
|
2018-05-07 18:36:37 +02:00
|
|
|
[ dup mark-if-current pad line# n:inc @FID file:read-line s:put eol ] if ;
|
2017-11-12 06:20:14 +01:00
|
|
|
|
|
|
|
:display (-)
|
2017-11-12 05:28:24 +01:00
|
|
|
@SourceFile file:R file:open !FID
|
2017-11-12 06:20:14 +01:00
|
|
|
clear-display header ---- skip-to
|
2019-05-14 13:29:09 +02:00
|
|
|
@CurrentLine MAX-LINES #2 / - #0 n:max count-lines MAX-LINES n:min
|
|
|
|
[ display-line ] times drop
|
2017-11-15 02:26:50 +01:00
|
|
|
---- @FID file:close ;
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
With the code to display the file done, I can proceed to the
|
|
|
|
words for handling editing.
|
2017-11-12 06:20:14 +01:00
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
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.
|
2017-11-12 20:50:52 +01:00
|
|
|
|
2019-02-02 05:45:38 +01:00
|
|
|
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.
|
2017-11-14 03:15:34 +01:00
|
|
|
|
2017-11-12 20:50:52 +01:00
|
|
|
~~~
|
|
|
|
:process-lines (q-)
|
|
|
|
TEMP-FILE file:W file:open !FID
|
|
|
|
[ #0 @SourceFile ] dip file:for-each-line drop
|
|
|
|
@FID file:close
|
2017-11-15 20:45:49 +01:00
|
|
|
here TEMP-FILE file:slurp here @SourceFile file:spew ;
|
2017-11-12 20:50:52 +01:00
|
|
|
|
2019-02-04 13:11:28 +01:00
|
|
|
:current? (ns-nsf)
|
2017-11-12 06:20:14 +01:00
|
|
|
over @CurrentLine eq? ;
|
2017-11-14 03:15:34 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
So first up, a word to delete all text in the current line.
|
2017-11-12 06:20:14 +01:00
|
|
|
|
2017-11-14 03:15:34 +01:00
|
|
|
~~~
|
2017-11-12 06:20:14 +01:00
|
|
|
:delete-line (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
[ current? [ drop '_ ] if file:s:put n:inc ] process-lines ;
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
|
|
|
|
2017-11-14 03:15:34 +01:00
|
|
|
Then a word to discard the current line, removing it from the file.
|
|
|
|
|
2017-11-12 16:31:02 +01:00
|
|
|
~~~
|
|
|
|
:kill-line (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
[ current? [ drop ] [ file:s:put ] choose n:inc ] process-lines ;
|
2017-11-12 16:31:02 +01:00
|
|
|
~~~
|
|
|
|
|
2017-11-14 03:15:34 +01:00
|
|
|
And the inverse, a word to inject a new line into the file.
|
|
|
|
|
2017-11-12 16:31:02 +01:00
|
|
|
~~~
|
2019-02-03 23:45:12 +01:00
|
|
|
:add-line (-)
|
|
|
|
[ current? [ file:s:put ASCII:LF @FID file:write ]
|
|
|
|
[ file:s:put ] choose n:inc ] process-lines CurrentLine v:inc ;
|
2017-11-12 16:31:02 +01:00
|
|
|
~~~
|
|
|
|
|
2019-02-02 06:00:31 +01:00
|
|
|
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.
|
2017-11-12 05:40:20 +01:00
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
2017-11-12 21:06:21 +01:00
|
|
|
{{
|
|
|
|
:save (c-)
|
|
|
|
ASCII:BS [ buffer:get drop ] case
|
|
|
|
ASCII:DEL [ buffer:get drop ] case
|
|
|
|
buffer:add ;
|
|
|
|
---reveal---
|
2018-05-07 18:36:37 +02:00
|
|
|
:s:get (-s)
|
2017-11-12 21:06:21 +01:00
|
|
|
s:empty [ buffer:set
|
2018-05-07 18:36:37 +02:00
|
|
|
[ repeat c:get dup ASCII:LF -eq? 0; drop save again ] call drop ] sip ;
|
2017-11-12 21:06:21 +01:00
|
|
|
}}
|
2017-11-12 05:44:52 +01:00
|
|
|
|
2019-02-03 23:45:12 +01:00
|
|
|
:replace-line (-)
|
|
|
|
[ current? [ drop #7 [ ASCII:SPACE c:put ] times s:get ] if
|
|
|
|
file:s:put n:inc ] process-lines ;
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
|
|
|
|
2019-02-02 06:00:31 +01:00
|
|
|
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.
|
2017-11-14 03:15:34 +01:00
|
|
|
|
2017-11-14 02:58:06 +01:00
|
|
|
~~~
|
|
|
|
:indent-line (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
[ current? [ ASCII:SPACE dup @FID file:write @FID file:write ] if file:s:put n:inc ] process-lines ;
|
2017-11-14 03:15:34 +01:00
|
|
|
|
2017-11-14 02:58:06 +01:00
|
|
|
:dedent-line (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
[ current? [ n:inc n:inc ] if file:s:put n:inc ] process-lines ;
|
2017-11-14 02:58:06 +01:00
|
|
|
|
2017-11-14 02:51:02 +01:00
|
|
|
:trim-trailing (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
[ current? [ s:trim-right ] if file:s:put n:inc ] process-lines ;
|
2017-11-15 02:26:50 +01:00
|
|
|
|
|
|
|
:code-block (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
[ current? [ drop '~~~ ] if file:s:put n:inc ] process-lines ;
|
2017-11-14 02:51:02 +01:00
|
|
|
~~~
|
2017-11-12 21:06:21 +01:00
|
|
|
|
2019-02-02 06:00:31 +01:00
|
|
|
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.
|
2017-11-14 03:15:34 +01:00
|
|
|
|
2017-11-14 02:51:02 +01:00
|
|
|
~~~
|
2017-11-12 21:06:21 +01:00
|
|
|
:copy-line (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
[ current? [ dup &CopiedLine s:copy ] if file:s:put n:inc ] process-lines ;
|
2017-11-12 21:06:21 +01:00
|
|
|
|
|
|
|
:paste-line (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
[ current? [ drop &CopiedLine ] if file:s:put n:inc ] process-lines ;
|
2017-11-12 21:06:21 +01:00
|
|
|
~~~
|
|
|
|
|
2019-02-02 06:00:31 +01:00
|
|
|
One more set of commands: jump to a particular line in the
|
|
|
|
file, jump to the start or end of the file.
|
2017-11-14 03:15:34 +01:00
|
|
|
|
2017-11-12 16:31:02 +01:00
|
|
|
~~~
|
|
|
|
:goto (-)
|
2018-05-07 18:36:37 +02:00
|
|
|
s:get s:to-number !CurrentLine ;
|
2017-11-12 16:31:02 +01:00
|
|
|
|
2017-11-15 02:11:34 +01:00
|
|
|
:goto-start (-)
|
|
|
|
#0 !CurrentLine ;
|
|
|
|
|
|
|
|
:goto-end (-)
|
2017-11-15 02:38:42 +01:00
|
|
|
@LineCount n:dec !CurrentLine ;
|
2017-11-15 02:11:34 +01:00
|
|
|
~~~
|
|
|
|
|
2019-02-02 06:00:31 +01:00
|
|
|
And now tie everything together. There's a key handler and a
|
|
|
|
top level loop.
|
2017-11-12 05:44:52 +01:00
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
2017-11-12 16:31:02 +01:00
|
|
|
:describe (cs-)
|
2018-05-07 18:36:37 +02:00
|
|
|
swap c:put $: c:put s:put ;
|
|
|
|
:| describe '_|_ s:put ;
|
2017-11-12 16:31:02 +01:00
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
:help
|
2017-11-15 02:26:50 +01:00
|
|
|
$1 'replace_ |
|
|
|
|
$2 'insert__ |
|
|
|
|
$3 'trim____ |
|
|
|
|
$4 'erase___ |
|
|
|
|
$5 'delete__ |
|
|
|
|
$j 'down____ | nl
|
|
|
|
$k 'up______ |
|
|
|
|
$g 'goto____ |
|
|
|
|
$[ 'start___ |
|
|
|
|
$] 'end_____ |
|
|
|
|
$c 'copy____ |
|
|
|
|
$v 'paste___ | nl
|
|
|
|
$< 'dedent__ |
|
|
|
|
$> 'indent__ |
|
|
|
|
$~ 'mark_eol |
|
|
|
|
$| '~~~_____ |
|
2018-05-07 18:36:37 +02:00
|
|
|
'___________|_ s:put
|
2017-11-15 02:26:50 +01:00
|
|
|
$q 'quit____ | nl ;
|
2017-11-14 02:29:16 +01:00
|
|
|
~~~
|
2017-11-12 05:44:52 +01:00
|
|
|
|
2017-11-14 02:29:16 +01:00
|
|
|
~~~
|
2017-11-15 02:38:42 +01:00
|
|
|
:constrain (-) &CurrentLine #0 @LineCount n:dec v:limit ;
|
2017-11-12 05:28:24 +01:00
|
|
|
:handler
|
2018-05-07 18:36:37 +02:00
|
|
|
c:get
|
2019-05-14 13:29:09 +02:00
|
|
|
$1 [ replace-line ] case
|
|
|
|
$2 [ add-line ] case
|
|
|
|
$3 [ trim-trailing ] case
|
|
|
|
$4 [ delete-line ] case
|
|
|
|
$5 [ kill-line ] case
|
|
|
|
$~ [ @ShowEOL not !ShowEOL ] case
|
|
|
|
$c [ copy-line ] case
|
|
|
|
$v [ paste-line ] case
|
|
|
|
$< [ dedent-line ] case
|
|
|
|
$> [ indent-line ] case
|
|
|
|
$| [ code-block ] case
|
|
|
|
$[ [ goto-start ] case
|
|
|
|
$] [ goto-end ] case
|
|
|
|
$j [ &CurrentLine v:inc constrain ] case
|
|
|
|
$k [ &CurrentLine v:dec constrain ] case
|
|
|
|
$h [ &CurrentLine v:inc constrain ] case
|
|
|
|
$t [ &CurrentLine v:dec constrain ] case
|
|
|
|
$g [ goto constrain ] case
|
2019-10-14 17:17:43 +02:00
|
|
|
$q [ bye ] case
|
2017-11-12 05:28:24 +01:00
|
|
|
drop ;
|
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
:edit
|
2017-11-12 05:28:24 +01:00
|
|
|
repeat
|
2017-11-12 06:20:14 +01:00
|
|
|
display help handler
|
2017-11-12 05:28:24 +01:00
|
|
|
again ;
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
Run the editor.
|
2017-11-12 05:28:24 +01:00
|
|
|
|
2017-11-12 06:20:14 +01:00
|
|
|
~~~
|
|
|
|
edit
|
2017-11-12 05:28:24 +01:00
|
|
|
~~~
|