]> gitweb.factorcode.org Git - factor.git/commitdiff
Add vocab completion for readline when line begins with USING:
authorErik Charlebois <erikcharlebois@gmail.com>
Tue, 24 May 2011 08:02:10 +0000 (04:02 -0400)
committerErik Charlebois <erikcharlebois@gmail.com>
Tue, 24 May 2011 08:02:10 +0000 (04:02 -0400)
extra/readline-listener/readline-listener.factor
extra/readline/readline.factor

index 4a7ff24fc0183836d5a9a261e3204fbc0c4879f6..2317631b6d8af1d64f52347aa86bb6f9f9f97ace 100644 (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* ;
 
index dd10bdac915343ee9dcfd52c502b161e0d6823da..688934cba439eb0c21f83856bbe53af4c2f75a91 100644 (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* ]