Search code examples
forthgforth

Multiline command-line editing in Gforth console


I have just started learning the Forth programming language. I'm using Gforth on Ubuntu. In Gforth interactive console, I want to do indentation but it requires changing line. Enter key didn't work, it executed code. For comparison, for example, when one tests JavaScript code in web browser console, shift+enter change line without executing code. I want something like that. What key should I press? Is there a way other than using text editors like vim? Best.


Solution

  • Gforth doesn't support multiline editing (see the manual).

    A workaround is to edit a file in your favorite editor in another window and reload this file in Gforth console as:

    include /tmp/scratch.fs
    

    An external file can be also edited in Gforth console via a command like:

    "vim /tmp/scratch.fs" system
    

    So a one-liner for that is:

    "vim /tmp/scratch.fs" system  "/tmp/scratch.fs" included
    

    That can be wrapped into a definition as:

    : scratch "vim /tmp/scratch.fs" system  "/tmp/scratch.fs" included ;
    

    So the word scratch will open an editor and than load the edited file.

    NB: if you use a quite old build of Gforth, you have to use s" ccc" instead of "ccc" for string literals.

    To conditionally include/exclude some parts in a file the words [defined] and [if] can be used; to erase the previous instance of the loaded definitions the word marker can be used as:

    [defined] _clear [if] _clear [then]
    marker _clear
    
    \ some definitions
    \ ...
    
    

    Take into account that usual control-flow words can be used in definitions only.