]> gitweb.factorcode.org Git - factor.git/commitdiff
readline-listener: adding better support for tab completion.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 25 Sep 2012 16:11:05 +0000 (09:11 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 25 Sep 2012 16:11:05 +0000 (09:11 -0700)
extra/readline-listener/readline-listener.factor

index c32badfaf2460de8b75dec21f77f7263e7e11b85..155a209907df3579a4cff903ed355906ac090243 100644 (file)
@@ -1,12 +1,11 @@
 ! Copyright (C) 2011 Erik Charlebois.
 ! 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 vocabs.hierarchy sequences.deep locals
-splitting math ;
+USING: accessors assocs combinators io kernel listener readline
+sequences splitting threads tools.completion ;
 IN: readline-listener
 
 <PRIVATE
+
 SYMBOL: completions
 
 TUPLE: readline-reader { prompt initial: f } ;
@@ -19,30 +18,19 @@ M: readline-reader stream-readln
 M: readline-reader prompt.
     >>prompt drop ;
 
-: word-names ( -- strs )
-    all-words [ name>> ] map! ;
-
-: vocab-names ( -- strs )
-    all-vocabs-recursive filter-vocabs [ 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 " " split1 drop
-        "USING:" = [
-            prefixed-vocabs
-        ] [
-            prefixed-words
-        ] if dup completions tset
-    ] if ;
+    completions tget [ nip ] [
+        current-line " \r\n" split {
+            { [ dup complete-vocab? ] [ drop vocabs-matching ] }
+            { [ dup complete-CHAR:? ] [ drop chars-matching ] }
+            { [ dup complete-COLOR:? ] [ drop colors-matching ] }
+            [ drop words-matching ]
+        } cond values dup completions tset
+    ] if* ;
+
 PRIVATE>
 
 : readline-listener ( -- )