Shop OBEX P1 Docs P2 Docs Learn Events
FED: A Local Forth Source Code Editor for P2 Taqoz — Parallax Forums

FED: A Local Forth Source Code Editor for P2 Taqoz

Christof Eb.Christof Eb. Posts: 1,213
edited 2023-12-11 17:17 in Customer Projects

Hi,
this idea had been discussed before: https://forums.parallax.com/discussion/175605/p2-taqoz-v2-8-the-challenge-make-a-local-editor-so-advantageous-that-we-will-really-use-it#latest
I hope, that it now has some maturity and stability to be worth posting..... Perhaps it might be interesting or inspiring?
The idea was to make an Editor for Forth source code, that runs on the target machine and therefore can take advantage of an integration with the Taqoz system. Also the edit-compile-test cycle should be faster. While it is a step in the direction a self contained system, the idea is to use Teraterm as ANSI terminal on a PC, to have access to PDFs and the internet. And also to have independent backup possibilities.

FED is a text editor for named files on a SD card. To make the editor fast, there is no spill over from line to line. Each line is separate. If you want to split a line into the next line, use ^y. If you enter too many chars into the active line, then there will be loss at the line end. If a new line is inserted then a great chunk of text has to be moved in Ram and the screen will be refreshed, which takes perhaps 1/3rd of a second. Mainly to render the syntax highlighting.

So the first picture shows the text with syntax highlighting. Here we can use the Forth dictionary: Known words are printed in white, unknown words are yellow. The cursor sits in line 139 over the word fopen$. The key combination ^w has invoked the search in the Glossary for this word, which is printed on top of the sceen. Below the explanation we have in magenta the file name and the number of free lines. Also a number of reminders, what control-keys will do.

Now we have moved the cursor over the word prtFileLine in line 149 and pressed ^p, which prints the first line of this definition, which is part of this file. If we would press ^o then we would set a marker and jump to this line. With ^j we can jump to previous set markers in a round robin scheme.

If we just move the cursor to any word or any begun word, then we will get suggestions of words, that are present in the Forth vocabulary. In this case the cursor is over "or" in line 145 and we get some words with this start. The Forth dictionary can be searched quite fast, so this function can be normally on. If we want to use one of these, we can ^e and then select one of the suggestions.

Now we want to navigate fast to some definition of a word and have pressed ^g. A list of all definitions appears. Blue are data, white are colon definitions and red are definitions made with pub, which we use to have some headlines here. If we press C2, we will directly jump to the definition of "findWord".

A submenu ^b starts the block operations and also the possibility to display a help file fed.hlp .
The block operations work via a file fedbuf.txt. This makes it possible to export or import text blocks.
here we have pressed ^bh to display the help file.

Other possibilities include normal cursor movement but also to next and previous definition starts.
With ^u you save the file.
With ^q you get the option to leave directly, to save and leave or to save, leave and execute ( compile ) the file.
The working file name is in the global string variable fedFile$, so typing fed will reopen this file.
I have done backups in this way: I had the working file fed.fth and have done "fcopy fed.fth fedX.fth". Also you can "cat fed.fth" and then copy the printout in Teraterm with the mouse to a file in an editor on PC side.

Some internals:
The text is stored as Textfile with line endings CRLF. When it is loaded, then this is converted to a fixed line length format. I use 100 ( lWidth# ) columns. In this format about 2300 lines of text is possible. Display height can be adjusted setting fedHeight#. For my better monitor I use fedHeight# of 65 lines in window of 74 lines. I use font Consolas 11. For lower resolution monitor fedheight# of 54 lines fits better.
If the program crashes and you had not saved (^u), but can recover the Forth system, then you can try to save the text, which is probably left in RAM with the word "writeFile", which will overwrite the active fedFile$.
To edit a new text, this text file must already exist. You can copy a template file with fcopy.

The input buffer of the serial port is 768bytes, which is a few lines of text. Without handshake there will be overflow if you try to paste a longer text into the editor. To do copy and paste via mouse and the Terminal window, you can toggle the line numbers and have the pure text with ^r. The editor tries to avoid buffer overflow sending Xon/Xoff codes to the PC. To make this work, you have to activate the protocol and to reduce buffer size in the Device Manager of Windows in the Device Driver settings. A way to produce crashes is to keep pagedown pressed. The buffer will overflow at some time in mid of a control-sequence.

In the package there is also a little simple menue facility. This is a little program in men.fth, invoked typing men. The program will read the file men.blk, which is a text file with fixed line length 100 chars. Each line begins with a single char and a blank. If you press the key of this single first char, then the commands of the rest of that line will be executed. The file men.blk is produced after editing the file men.txt. When this text is still in the Ram buffer of fed, we can write out the file men.blk with the command wrMen. (Try: metEd fed wrMen) You can simply end the menue program with x. If you type X instead, you can compile the file named by fedFile$. Men can save you some typing and is certainly not too overengineered... It is perhaps a good way to fload men.fth after autoW.fth first. You can then start it and end it with X, which will load the editor next. The words fedEd, metEd, menEd, testEd are shortcuts to set fedFile$, see source code.

The code is written using some of my tools namely local named variables, which for me improve the readability of the source code. Their syntax is:

: newWord {: stackInit1 stackInit2 , nonInit1 nonInit2 -- comment }
   ...program text...
; forgetLocals

Up to 5 local variables are possible. The ones before the comma get their values from the stack.
To have these tools available fload autoW.fth first.
(Mandatory for this is the use of the version _BOOT_P2.BIX in Taqoz.zip in https://sourceforge.net/projects/tachyon-forth/files/TAQOZ/binaries/ . )

Last not least I want to thank the helpful members of the forum! In the moments of confusion the friendly hints are very important for me! Also I want to thank @bob_g4bby for his great Glossary, which now makes possible a powerful feature of this editor too! Of course everything is based on @"Peter Jakacki" 's Taqoz Forth, which is so fascinating for me, because it is so very well tailored to the P2 and because it is so complete. It is also a great source of examples. Thank you! Isn't it great, that there is a language that allows to write an editor with all these features in 1400 lines of source code and <7kB of compiled code?
Thanks also for P2, Parallax! Great product, lots of fascination!

Christof

Comments

  • code added.

  • An update. The patch of LEN$ led to crashes, when you tried to recompile fedT.fth, because then the patched JMP led into nirvana....
    Also improved -I think- is the list format of the definitions after^g. It should be more easy to have orientation in the list to refind things....

  • This looks great. Thank you!

    As my eyesight has deteiorated over the years I will most probably tweak the colors to slightly more contrasting ones (I have trouble seeing dark blue on black) which, form what I can see in the code, will not be difficult.
    Is it TeraTerm specific (windows only) ?

    Again, kudos to you for writing it and even more so for making it available.

  • @Maciek said:
    This looks great. Thank you!

    As my eyesight has deteiorated over the years I will most probably tweak the colors to slightly more contrasting ones (I have trouble seeing dark blue on black) which, form what I can see in the code, will not be difficult.
    Is it TeraTerm specific (windows only) ?

    Again, kudos to you for writing it and even more so for making it available.

    Hi, it needs ANSI control Codes. Try %red %pen .
    Good luck.

Sign In or Register to comment.