From: Erik Charlebois Date: Tue, 24 May 2011 08:02:10 +0000 (-0400) Subject: Add vocab completion for readline when line begins with USING: X-Git-Tag: 0.97~4323^2^2 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=c492ff62a74fc2b04f2827271f326d05a66f2cb0 Add vocab completion for readline when line begins with USING: --- diff --git a/extra/readline-listener/readline-listener.factor b/extra/readline-listener/readline-listener.factor index 4a7ff24fc0..2317631b6d 100644 --- a/extra/readline-listener/readline-listener.factor +++ b/extra/readline-listener/readline-listener.factor @@ -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 > _ 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* ; diff --git a/extra/readline/readline.factor b/extra/readline/readline.factor index dd10bdac91..688934cba4 100644 --- a/extra/readline/readline.factor +++ b/extra/readline/readline.factor @@ -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* ]