factor/extra/ui/tools/interactor/interactor.factor

170 lines
4.6 KiB
Factor
Raw Normal View History

2008-01-05 15:09:55 -05:00
! Copyright (C) 2006, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs combinators continuations documents
hashtables io io.styles kernel math
2007-09-20 18:09:08 -04:00
math.vectors models namespaces parser prettyprint quotations
2008-04-05 08:57:51 -04:00
sequences strings threads listener
classes.tuple ui.commands ui.gadgets ui.gadgets.editors
2008-02-02 01:30:36 -05:00
ui.gadgets.presentations ui.gadgets.worlds ui.gestures
2008-04-04 01:33:06 -04:00
definitions boxes calendar concurrency.flags ui.tools.workspace
accessors ;
2007-09-20 18:09:08 -04:00
IN: ui.tools.interactor
TUPLE: interactor history output flag thread help ;
2007-09-20 18:09:08 -04:00
: interactor-continuation ( interactor -- continuation )
interactor-thread box-value
thread-continuation box-value ;
: interactor-busy? ( interactor -- ? )
interactor-thread box-full? not ;
2008-01-04 21:10:49 -05:00
: interactor-use ( interactor -- seq )
dup interactor-busy? [ drop f ] [
use swap
interactor-continuation continuation-name
assoc-stack
] if ;
2008-01-04 21:10:49 -05:00
2007-09-20 18:09:08 -04:00
: init-caret-help ( interactor -- )
2008-02-21 21:57:41 -05:00
dup editor-caret 1/3 seconds <delay>
swap set-interactor-help ;
2007-09-20 18:09:08 -04:00
: init-interactor-history ( interactor -- )
V{ } clone swap set-interactor-history ;
: init-interactor-state ( interactor -- )
<flag> over set-interactor-flag
<box> swap set-interactor-thread ;
2007-09-20 18:09:08 -04:00
: <interactor> ( output -- gadget )
<source-editor>
2007-12-14 01:16:47 -05:00
interactor construct-editor
tuck set-interactor-output
2007-09-20 18:09:08 -04:00
dup init-interactor-history
dup init-interactor-state
2007-09-20 18:09:08 -04:00
dup init-caret-help ;
M: interactor graft*
dup delegate graft*
dup interactor-help add-connection ;
2007-09-20 18:09:08 -04:00
2007-12-30 21:15:59 -05:00
: word-at-loc ( loc interactor -- word )
over [
[ gadget-model T{ one-word-elt } elt-string ] keep
interactor-use assoc-stack
] [
2drop f
] if ;
M: interactor model-changed
2dup interactor-help eq? [
swap model-value over word-at-loc swap show-summary
] [
delegate model-changed
] if ;
2007-09-20 18:09:08 -04:00
: write-input ( string input -- )
<input> presented associate
[ H{ { font-style bold } } format ] with-nesting ;
: interactor-input. ( string interactor -- )
interactor-output [
dup string? [ dup write-input nl ] [ short. ] if
] with-stream* ;
: add-interactor-history ( str interactor -- )
over empty? [ 2drop ] [ interactor-history push-new ] if ;
: interactor-continue ( obj interactor -- )
interactor-thread box> resume-with ;
2007-09-20 18:09:08 -04:00
2008-01-04 21:10:49 -05:00
: clear-input ( interactor -- ) gadget-model clear-doc ;
2007-12-30 21:15:59 -05:00
: interactor-finish ( interactor -- )
#! The spawn is a kludge to make it infer. Stupid.
2007-09-20 18:09:08 -04:00
[ editor-string ] keep
[ interactor-input. ] 2keep
[ add-interactor-history ] keep
[ clear-input ] curry "Clearing input" spawn drop ;
2007-09-20 18:09:08 -04:00
: interactor-eof ( interactor -- )
2007-12-30 21:15:59 -05:00
dup interactor-busy? [
f over interactor-continue
] unless drop ;
2007-09-20 18:09:08 -04:00
: evaluate-input ( interactor -- )
2007-12-30 21:15:59 -05:00
dup interactor-busy? [
2008-02-18 06:07:40 -05:00
dup control-value over interactor-continue
2007-12-30 21:15:59 -05:00
] unless drop ;
2007-09-20 18:09:08 -04:00
2007-12-30 21:15:59 -05:00
: interactor-yield ( interactor -- obj )
[
[ interactor-thread >box ] keep
interactor-flag raise-flag
] curry "input" suspend ;
2007-09-20 18:09:08 -04:00
M: interactor stream-readln
2008-04-05 08:57:51 -04:00
[ interactor-yield ] keep interactor-finish
dup [ first ] when ;
2007-09-20 18:09:08 -04:00
: interactor-call ( quot interactor -- )
2007-12-30 21:15:59 -05:00
dup interactor-busy? [
2dup interactor-input.
2dup interactor-continue
] unless 2drop ;
2007-09-20 18:09:08 -04:00
M: interactor stream-read
swap dup zero? [
2drop ""
] [
>r stream-readln dup length r> min head
] if ;
M: interactor stream-read-partial
stream-read ;
: go-to-error ( interactor error -- )
2008-04-04 01:33:06 -04:00
[ line>> 1- ] [ column>> ] bi 2array
2007-12-30 21:15:59 -05:00
over set-caret
2007-09-20 18:09:08 -04:00
mark>caret ;
: handle-parse-error ( interactor error -- )
2008-04-04 01:33:06 -04:00
dup parse-error? [ 2dup go-to-error error>> ] when
2007-09-20 18:09:08 -04:00
swap find-workspace debugger-popup ;
2007-12-30 21:15:59 -05:00
: try-parse ( lines interactor -- quot/error/f )
2007-09-20 18:09:08 -04:00
[
2008-01-04 21:10:49 -05:00
drop parse-lines-interactive
2007-12-30 21:15:59 -05:00
] [
2nip
2008-04-10 22:49:08 -04:00
dup parse-error? [
dup error>> unexpected-eof? [ drop f ] when
] when
2007-12-30 21:15:59 -05:00
] recover ;
: handle-interactive ( lines interactor -- quot/f ? )
2007-09-20 18:09:08 -04:00
tuck try-parse {
2007-12-30 21:15:59 -05:00
{ [ dup quotation? ] [ nip t ] }
{ [ dup not ] [ drop "\n" swap user-input f f ] }
2008-04-11 13:54:33 -04:00
[ handle-parse-error f f ]
2007-09-20 18:09:08 -04:00
} cond ;
2007-12-29 22:29:59 -05:00
M: interactor stream-read-quot
2008-01-04 21:10:49 -05:00
[ interactor-yield ] keep {
{ [ over not ] [ drop ] }
{ [ over callable? ] [ drop ] }
2008-04-11 13:54:33 -04:00
[
2008-01-04 21:10:49 -05:00
[ handle-interactive ] keep swap
[ interactor-finish ] [ nip stream-read-quot ] if
2008-04-11 13:54:33 -04:00
]
2008-01-04 21:10:49 -05:00
} cond ;
2007-09-20 18:09:08 -04:00
M: interactor pref-dim*
0 over line-height 4 * 2array swap delegate pref-dim* vmax ;
interactor "interactor" f {
{ T{ key-down f f "RET" } evaluate-input }
{ T{ key-down f { C+ } "k" } clear-input }
} define-command-map