factor/library/ui/text/interactor.factor

99 lines
3.4 KiB
Factor
Raw Normal View History

! Copyright (C) 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: gadgets-text
2006-07-31 16:49:26 -04:00
USING: gadgets gadgets-controls gadgets-panes generic hashtables
2006-08-19 15:33:55 -04:00
help io kernel namespaces prettyprint styles threads sequences
2006-08-24 00:45:58 -04:00
vectors jedit definitions parser words ;
2006-08-24 00:45:58 -04:00
TUPLE: interactor history output continuation queue busy? ;
C: interactor ( output -- gadget )
[ set-interactor-output ] keep
2006-07-27 19:46:21 -04:00
f <field> over set-gadget-delegate
2006-08-24 00:45:58 -04:00
V{ } clone over set-interactor-history
dup dup set-control-self ;
M: interactor graft*
2006-07-31 16:49:26 -04:00
f over set-interactor-busy? delegate graft* ;
2006-08-24 00:45:58 -04:00
: (interactor-eval) ( string interactor -- )
2006-07-31 16:49:26 -04:00
dup interactor-busy? [
2drop
] [
2006-07-31 16:50:40 -04:00
t over set-interactor-busy?
2006-08-19 15:33:55 -04:00
swap "\n" split <reversed> >vector
over set-interactor-queue
interactor-continuation schedule-thread
2006-07-31 16:49:26 -04:00
] if ;
2006-07-31 16:50:40 -04:00
SYMBOL: structured-input
: interactor-call ( quot gadget -- )
dup interactor-output [
"Command: " write over short.
] with-stream*
>r structured-input set-global
"\"structured-input\" \"gadgets-text\" lookup get-global call"
2006-08-24 00:45:58 -04:00
r> (interactor-eval) ;
2006-07-31 16:50:40 -04:00
2006-08-22 21:50:05 -04:00
: interactor-input. ( string interactor -- )
interactor-output [ dup print-input ] with-stream* ;
2006-08-24 00:45:58 -04:00
: interactor-eval ( string interactor -- )
dup control-model clear-doc
2dup interactor-history push-new
2dup interactor-input.
(interactor-eval) ;
2006-07-31 16:49:26 -04:00
: interactor-commit ( interactor -- )
dup interactor-busy? [
drop
] [
2006-08-24 00:45:58 -04:00
[ field-commit ] keep interactor-eval
] if ;
: quot-action ( interactor word -- )
over interactor-busy? [
2drop
] [
[ "[ " % over field-commit % " ] " % % ] "" make
swap interactor-eval
2006-07-31 16:49:26 -04:00
] if ;
2006-08-22 21:50:05 -04:00
: interactor-history. ( interactor -- )
dup interactor-output [
2006-08-24 00:45:58 -04:00
interactor-history [ dup print-input ] each
2006-08-22 21:50:05 -04:00
] with-stream* ;
2006-08-24 00:45:58 -04:00
: word-action ( interactor word -- )
over gadget-selection?
2006-08-24 22:44:42 -04:00
[ over T{ word-elt } select-elt ] unless
2006-08-24 00:45:58 -04:00
over gadget-selection add* swap interactor-call ;
: usable-words ( -- seq )
use get [ hash-values natural-sort ] map concat prune ;
: use-word ( str -- )
words-named [ word-vocabulary dup print use+ ] each ;
2006-08-24 19:15:50 -04:00
interactor {
{ f "Evaluate input" T{ key-down f f "RETURN" } [ interactor-commit ] }
{ f "Clear output" T{ key-down f { A+ } "c" } [ dup [ interactor-output pane-clear ] curry swap interactor-call ] }
{ f "History" T{ key-down f { C+ } "h" } [ dup [ interactor-history. ] curry swap interactor-call ] }
{ f "Send EOF" T{ key-down f { C+ } "d" } [ f swap interactor-eval ] }
{ f "Infer input" T{ key-down f { C+ } "i" } [ "infer ." quot-action ] }
{ f "Single step input" T{ key-down f { C+ } "w" } [ "walk" quot-action ] }
{ f "See at caret" T{ key-down f { A+ } "s" } [ [ search see ] word-action ] }
{ f "jEdit at caret" T{ key-down f { A+ } "j" } [ [ search jedit ] word-action ] }
{ f "Reload at caret" T{ key-down f { A+ } "r" } [ [ search reload ] word-action ] }
{ f "Apropos at caret (all)" T{ key-down f { A+ } "a" } [ [ apropos ] word-action ] }
{ f "Use word at caret" T{ key-down f { A+ } "u" } [ [ use-word ] word-action ] }
{ f "Apropos at caret (used)" T{ key-down f f "TAB" } [ [ usable-words (apropos) ] word-action ] }
} define-commands
M: interactor stream-readln
2006-08-19 15:33:55 -04:00
dup interactor-queue empty? [
f over set-interactor-busy?
[ over set-interactor-continuation stop ] callcc0
] when interactor-queue pop ;