94 lines
2.6 KiB
Factor
94 lines
2.6 KiB
Factor
! Copyright (C) 2004, 2006 Slava Pestov.
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
IN: jedit
|
|
USING: arrays definitions errors io kernel listener math
|
|
namespaces parser prettyprint sequences strings words shells ;
|
|
|
|
! Some words to send requests to a running jEdit instance to
|
|
! edit files and position the cursor on a specific line number.
|
|
|
|
: jedit-server-info ( -- port auth )
|
|
"~" get "/.jedit/server" path+ <file-reader> [
|
|
readln drop
|
|
readln string>number
|
|
readln string>number
|
|
] with-stream ;
|
|
|
|
: make-jedit-request ( files params -- code )
|
|
[
|
|
"EditServer.handleClient(false,false,false,null," write
|
|
"new String[] {" write
|
|
[ pprint "," write ] each
|
|
"null});\n" write
|
|
] string-out ;
|
|
|
|
: send-jedit-request ( request -- )
|
|
jedit-server-info swap "localhost" swap <client> [
|
|
4 >be write
|
|
dup length 2 >be write
|
|
write
|
|
] with-stream ;
|
|
|
|
: jedit-line/file ( file line -- )
|
|
number>string "+line:" swap append 2array
|
|
make-jedit-request send-jedit-request ;
|
|
|
|
: jedit-file ( file -- )
|
|
1array make-jedit-request send-jedit-request ;
|
|
|
|
: jedit ( spec -- )
|
|
#! Note that line numbers here start from 1
|
|
where first2 >r ?resource-path r> jedit-line/file ;
|
|
|
|
! 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 string-out.
|
|
|
|
: write-len ( seq -- ) length 4 >be write ;
|
|
|
|
: write-packet ( string -- ) dup write-len write flush ;
|
|
|
|
: read-packet ( -- string ) 4 read be> read ;
|
|
|
|
: wire-server ( -- )
|
|
#! Repeatedly read jEdit requests and execute them. Return
|
|
#! on EOF.
|
|
read-packet [ eval>string write-packet wire-server ] when* ;
|
|
|
|
: jedit-lookup ( word -- list )
|
|
#! A utility word called by the Factor plugin to get some
|
|
#! required word info.
|
|
dup [
|
|
[
|
|
dup definer ,
|
|
dup word-vocabulary ,
|
|
dup word-name ,
|
|
stack-effect ,
|
|
] [ ] make
|
|
] when ;
|
|
|
|
: completions ( str pred -- seq )
|
|
#! Make a list of completions. Each element of the list is
|
|
#! a vocabulary/name/stack-effect triplet list.
|
|
word-subset-with [ jedit-lookup ] map ;
|
|
|
|
! The telnet server is for the jEdit plugin.
|
|
: telnetd ( port -- )
|
|
\ telnetd [ tty ] with-server ;
|
|
|
|
: search ( name vocabs -- word )
|
|
dupd [ lookup ] find-with nip lookup ;
|
|
|
|
IN: shells
|
|
|
|
: telnet
|
|
"telnetd-port" get string>number telnetd ;
|
|
|
|
! This is a string since we string>number it above.
|
|
"9999" "telnetd-port" set-global
|