]> gitweb.factorcode.org Git - factor.git/commitdiff
Modify the listener vocabulary so that readline can hook in. Add the readline-listene...
authorErik Charlebois <erikcharlebois@gmail.com>
Sun, 22 May 2011 07:44:36 +0000 (03:44 -0400)
committerErik Charlebois <erikcharlebois@gmail.com>
Tue, 24 May 2011 03:36:14 +0000 (23:36 -0400)
basis/listener/listener.factor
extra/readline-listener/authors.txt [new file with mode: 0644]
extra/readline-listener/readline-listener-docs.factor [new file with mode: 0644]
extra/readline-listener/readline-listener.factor [new file with mode: 0644]
extra/readline-listener/summary.txt [new file with mode: 0644]
extra/readline-listener/tags.txt [new file with mode: 0644]

index 96db935f07cae6ada9e70c5d83049aaccb68bd13..f77c5262df83b68e2b96b4cc1122c558adb26f87 100644 (file)
@@ -8,6 +8,15 @@ sets vocabs.parser source-files.errors locals vocabs vocabs.loader ;
 IN: listener
 
 GENERIC: stream-read-quot ( stream -- quot/f )
+GENERIC# prompt. 1 ( stream prompt -- )
+
+: prompt ( -- str )
+    current-vocab name>> auto-use? get [ " - auto" append ] when
+    "( " " )" surround ;
+
+M: object prompt.
+    nip H{ { background T{ rgba f 1 0.7 0.7 1 } } } format bl
+    flush ;
 
 : parse-lines-interactive ( lines -- quot/f )
     [ parse-lines ] with-compilation-unit ;
@@ -82,7 +91,7 @@ t error-summary? set-global
             ] each
         ] tabular-output nl
     ] unless-empty ;
-    
+
 : trimmed-stack. ( seq -- )
     dup length max-stack-items get > [
         max-stack-items get cut*
@@ -97,15 +106,11 @@ t error-summary? set-global
         [ nl "--- Data stack:" title. trimmed-stack. ] unless-empty
     ] [ drop ] if ;
 
-: prompt. ( -- )
-    current-vocab name>> auto-use? get [ " - auto" append ] when "( " " )" surround
-    H{ { background T{ rgba f 1 0.7 0.7 1 } } } format bl flush ;
-
 :: (listener) ( datastack -- )
     error-summary? get [ error-summary ] when
     visible-vars.
     datastack datastack.
-    prompt.
+    input-stream get prompt prompt.
 
     [
         read-quot [
diff --git a/extra/readline-listener/authors.txt b/extra/readline-listener/authors.txt
new file mode 100644 (file)
index 0000000..6f03a12
--- /dev/null
@@ -0,0 +1 @@
+Erik Charlebois
diff --git a/extra/readline-listener/readline-listener-docs.factor b/extra/readline-listener/readline-listener-docs.factor
new file mode 100644 (file)
index 0000000..c678b05
--- /dev/null
@@ -0,0 +1,17 @@
+! Copyright (C) 2011 Erik Charlebois.
+! See http://factorcode.org/license.txt for BSD license.
+USING: help.markup help.syntax vocabs.loader ;
+IN: readline-listener
+
+HELP: readline-listener
+{ $description "Invokes a listener that uses libreadline for editing, history and word completion." } ;
+
+ARTICLE: "readline-listener" "Readline listener"
+{ $vocab-link "readline-listener" }
+$nl
+"By default, the terminal listener does not provide any command history or completion. This vocabulary uses libreadline to provide a listener with history, word completion and more convenient editing facilities."
+$nl
+{ $code "\"readline-listener\" run" }
+;
+
+ABOUT: "readline-listener"
diff --git a/extra/readline-listener/readline-listener.factor b/extra/readline-listener/readline-listener.factor
new file mode 100644 (file)
index 0000000..4a7ff24
--- /dev/null
@@ -0,0 +1,39 @@
+! 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 ;
+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 ]
+    keep f >>prompt drop ;
+
+M: readline-reader prompt.
+    >>prompt drop ;
+PRIVATE>
+
+: readline-listener ( -- )
+    [
+      swap get-completions ?nth
+      [ clear-completions f ] unless*
+    ] set-completion
+    readline-reader new [ listener ] with-input-stream* ;
+
+MAIN: readline-listener
diff --git a/extra/readline-listener/summary.txt b/extra/readline-listener/summary.txt
new file mode 100644 (file)
index 0000000..c582d23
--- /dev/null
@@ -0,0 +1 @@
+A listener that uses libreadline.
diff --git a/extra/readline-listener/tags.txt b/extra/readline-listener/tags.txt
new file mode 100644 (file)
index 0000000..e69de29