# Factor and the jEdit editor

## Slava Pestov — 2004-06-30 23:50:56

    Hi everybody,

    I have come up with a bare-bones but useful interface between Factor and

    jEdit.

    On the Factor side is a way of running the interpreter over a TCP/IP

    socket. Security is an issue here, make sure you only run this on

    machines not directly facing the internet.

    ====<8====

    First you need a little logging vocabulary:

    IN: logging

    USE: namespaces

    USE: combinators

    USE: stack

    USE: streams

    USE: strings

    : log ( msg -- )

         "log" get dup [ tuck fprint fflush ] [ 2drop ] ifte ;

    : log-error ( error -- )

         "Error: " swap cat2 log ;

    : log-client ( -- )

         "client" get [

             "Accepted connection from " swap

             [ "socket" get ] bind cat2 log

         ] when* ;

    : with-logging ( quot -- )

         <namespace> [ "stdio" get "log" set call ] bind ;

    ====<8====

    Now, the 'telnet' server itself. Note that it only supports 1

    simultaneous connection. Also it has some problems with the debugger.

    IN: telnetd

    USE: combinators

    USE: interpreter

    USE: logging

    USE: logic

    USE: namespaces

    USE: stack

    USE: stdio

    USE: streams

    : telnet-client ( socket -- )

         <namespace> [

             dup

             "client" set

             set-stdio

             log-client

             "remote " interpreter-loop

         ] bind ;

    : quit-flag ( -- ? )

         "telnetd-quit-flag" get ;

    : clear-quit-flag ( --  )

         f "telnetd-quit-flag" set ;

    : telnetd-loop ( server -- )

         [

             quit-flag not

         ] [

             dup accept dup telnet-client fclose

         ] while ;

    : telnetd ( port -- )

         [

             <server> dup telnetd-loop fclose clear-quit-flag

         ] with-logging ;

    ====<8====

    Now, if you key in:

    USE: telnetd

    9999 telnetd

    You should be able to connect to localhost:9999 with a telnet client and

    get a prompt. Exit by entering 'exit'.

    ====<8====

    The jEdit side of the equation consists of a macro:

    socket = new Socket("localhost",9999);

    out = new OutputStreamWriter(socket.getOutputStream());

    out.write("\"" + MiscUtilities.charsToEscapes(buffer.getPath()) + "\"");

    out.write(" run-file exit\n");

    out.close();

    ====<8====

    This macro can be bound to a keyboard shortcut in jEdit. Now, while

    working on the Factor source file, you can press this shortcut to load

    it in.

    Slava