factor/library/tools/jedit-wire.factor

83 lines
2.3 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-11-16 19:45:15 -05:00
IN: jedit
USING: generic kernel listener lists namespaces parser
prettyprint sequences stdio streams strings words ;
2004-11-16 19:45:15 -05:00
! Wire protocol for jEdit to evaluate Factor code.
! Packets are of the form:
!
! 4 bytes length
! <n> bytes data
!
! jEdit sends a packet with code to eval, it receives the output
! captured with with-string.
2004-11-16 19:45:15 -05:00
: write-packet ( string -- )
dup length write-big-endian-32 write flush ;
2004-11-16 19:45:15 -05:00
: read-packet ( -- string )
2005-02-14 22:15:02 -05:00
read-big-endian-32 read ;
2004-11-16 19:45:15 -05:00
: wire-server ( -- )
#! Repeatedly read jEdit requests and execute them. Return
#! on EOF.
read-packet [ eval>string write-packet wire-server ] when* ;
! Stream protocol for jEdit allows user to interact with a
! Factor listener.
!
! Packets have the following form:
!
! 1 byte -- type. CHAR: w: write, CHAR: r: read CHAR: f flush
! 4 bytes -- for write only -- length of write request
! remaining -- unparsed write request -- string then style
! After a read line request, the server reads a response from
! the client:
! 4 bytes -- length. -1 means EOF
! remaining -- input
: jedit-write-attr ( str style -- )
CHAR: w write
[ swap . . ] with-string
dup length write-big-endian-32
2004-11-16 19:45:15 -05:00
write ;
TUPLE: jedit-stream ;
2004-11-16 19:45:15 -05:00
2005-02-14 22:15:02 -05:00
M: jedit-stream stream-readln ( stream -- str )
2005-05-03 04:40:13 -04:00
[
CHAR: r write flush read-big-endian-32 read
] with-wrapper ;
2004-11-28 21:56:58 -05:00
2005-02-14 22:15:02 -05:00
M: jedit-stream stream-write-attr ( str style stream -- )
2005-05-03 04:40:13 -04:00
[ jedit-write-attr ] with-wrapper ;
2004-11-28 21:56:58 -05:00
2005-02-14 22:15:02 -05:00
M: jedit-stream stream-flush ( stream -- )
2005-05-03 04:40:13 -04:00
[ CHAR: f write flush ] with-wrapper ;
2004-11-28 21:56:58 -05:00
C: jedit-stream ( stream -- stream )
[ >r <wrapper-stream> r> set-delegate ] keep ;
2004-11-16 19:45:15 -05:00
: stream-server ( -- )
#! Execute this in the inferior Factor.
stdio [ <jedit-stream> ] change print-banner ;
2004-11-18 23:23:12 -05:00
2004-12-19 19:36:10 -05:00
: jedit-lookup ( word -- list )
2004-11-18 23:23:12 -05:00
#! A utility word called by the Factor plugin to get some
#! required word info.
2004-12-19 19:36:10 -05:00
dup [
2004-11-18 23:23:12 -05:00
[
"vocabulary"
"name"
"stack-effect"
] [
2005-03-23 22:49:40 -05:00
dupd word-prop
] map >r definer r> cons
2004-11-18 23:23:12 -05:00
] when ;
2004-12-19 19:36:10 -05:00
: completions ( str pred -- list | pred: str word -- ? )
2004-12-19 19:36:10 -05:00
#! Make a list of completions. Each element of the list is
#! a vocabulary/name/stack-effect triplet list.
word-subset-with [ jedit-lookup ] map ;