Add vocab completion for readline when line begins with USING:

db4
Erik Charlebois 2011-05-24 04:02:10 -04:00
parent b9e18184c6
commit c492ff62a7
2 changed files with 36 additions and 15 deletions

View File

@ -2,24 +2,14 @@
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.data fry io io.encodings.utf8 kernel
listener namespaces readline sequences threads vocabs
command-line ;
command-line vocabs.hierarchy sequences.deep locals
splitting math ;
QUALIFIED: readline.ffi
IN: readline-listener
<PRIVATE
SYMBOL: completions
: prefixed-words ( prefix -- words )
'[ name>> _ head? ] all-words swap filter [ name>> ] map ;
: clear-completions ( -- )
f completions tset ;
: get-completions ( prefix -- completions )
completions tget dup [ nip ] [ drop
prefixed-words dup completions tset
] if ;
TUPLE: readline-reader { prompt initial: f } ;
M: readline-reader stream-readln
flush [ prompt>> dup [ " " append ] [ ] if readline ]
@ -27,12 +17,37 @@ M: readline-reader stream-readln
M: readline-reader prompt.
>>prompt drop ;
: word-names ( -- strs )
all-words [ name>> ] map ;
: vocab-names ( -- strs )
all-vocabs-recursive no-roots no-prefixes [ name>> ] map ;
: prefixed-words ( prefix -- words )
'[ _ head? ] word-names swap filter ;
: prefixed-vocabs ( prefix -- words )
'[ _ head? ] vocab-names swap filter ;
: clear-completions ( -- )
f completions tset ;
: get-completions ( prefix -- completions )
completions tget dup [ nip ] [
drop current-line " " split first
"USING:" = [
prefixed-vocabs
] [
prefixed-words
] if dup completions tset
] if ;
PRIVATE>
: readline-listener ( -- )
[
swap get-completions ?nth
[ clear-completions f ] unless*
swap get-completions ?nth
[ clear-completions f ] unless*
] set-completion
readline-reader new [ listener ] with-input-stream* ;

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license.
USING: alien.strings destructors io.encodings.utf8 kernel libc
sequences macros quotations words compiler.units fry
alien.data ;
alien.data alien.libraries ;
QUALIFIED: readline.ffi
IN: readline
@ -15,6 +15,12 @@ IN: readline
] [ f ] if*
] with-destructors ;
: current-line ( -- str )
readline.ffi:rl_line_buffer ;
: has-readline ( -- ? )
"readline" dup load-library dlsym-raw >boolean ;
MACRO: set-completion ( quot -- )
[
'[ @ [ utf8 malloc-string ] [ f ] if* ]