1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs combinators continuations documents
4 hashtables io io.styles kernel math math.order math.vectors
5 models models.delay namespaces parser lexer prettyprint
6 quotations sequences strings threads listener classes.tuple
7 ui.commands ui.gadgets ui.gadgets.editors ui.gadgets.status-bar
8 ui.gadgets.presentations ui.gadgets.worlds ui.gestures
9 definitions calendar concurrency.flags concurrency.mailboxes
10 ui.tools.workspace accessors sets destructors fry vocabs.parser ;
11 IN: ui.tools.interactor
13 ! If waiting is t, we're waiting for user input, and invoking
14 ! evaluate-input resumes the thread.
15 TUPLE: interactor < source-editor
16 output history flag mailbox thread waiting help ;
18 : register-self ( interactor -- )
23 : interactor-continuation ( interactor -- continuation )
24 thread>> continuation>> value>> ;
26 : interactor-busy? ( interactor -- ? )
27 #! We're busy if there's no thread to resume.
29 [ thread>> dup [ thread-registered? ] when ]
32 : interactor-use ( interactor -- seq )
33 dup interactor-busy? [ drop f ] [
35 interactor-continuation name>>
39 : <help-model> ( interactor -- model ) caret>> 1/3 seconds <delay> ;
41 : <interactor> ( output -- gadget )
45 dup <help-model> >>help
49 [ call-next-method ] [ dup help>> add-connection ] bi ;
51 M: interactor ungraft*
52 [ dup help>> remove-connection ] [ call-next-method ] bi ;
54 : word-at-loc ( loc interactor -- word )
56 [ model>> T{ one-word-elt } elt-string ] keep
57 interactor-use assoc-stack
62 M: interactor model-changed
64 swap value>> over word-at-loc swap show-summary
69 : write-input ( string input -- )
70 <input> presented associate
71 [ H{ { font-style bold } } format ] with-nesting ;
73 : interactor-input. ( string interactor -- )
75 dup string? [ dup write-input nl ] [ short. ] if
76 ] with-output-stream* ;
78 : add-interactor-history ( str interactor -- )
79 over empty? [ 2drop ] [ history>> adjoin ] if ;
81 : interactor-continue ( obj interactor -- )
82 mailbox>> mailbox-put ;
84 : interactor-finish ( interactor -- )
85 [ editor-string ] keep
86 [ interactor-input. ] 2keep
87 [ add-interactor-history ] keep
90 : interactor-eof ( interactor -- )
91 dup interactor-busy? [
92 f over interactor-continue
95 : evaluate-input ( interactor -- )
96 dup interactor-busy? [
97 dup control-value over interactor-continue
100 : interactor-yield ( interactor -- obj )
101 dup thread>> self eq? [
104 [ flag>> raise-flag ]
105 [ mailbox>> mailbox-get ]
110 : interactor-read ( interactor -- lines )
111 [ interactor-yield ] [ interactor-finish ] bi ;
113 M: interactor stream-readln
114 interactor-read dup [ first ] when ;
116 : interactor-call ( quot interactor -- )
117 dup interactor-busy? [
118 2dup interactor-input.
119 2dup interactor-continue
122 M: interactor stream-read
126 [ interactor-read dup [ "\n" join ] when ] dip short head
129 M: interactor stream-read-partial
132 M: interactor stream-read1
133 dup interactor-read {
134 { [ dup not ] [ 2drop f ] }
135 { [ dup empty? ] [ drop stream-read1 ] }
136 { [ dup first empty? ] [ 2drop CHAR: \n ] }
140 M: interactor dispose drop ;
142 : go-to-error ( interactor error -- )
143 [ line>> 1- ] [ column>> ] bi 2array
147 : handle-parse-error ( interactor error -- )
148 dup lexer-error? [ 2dup go-to-error error>> ] when
149 swap find-workspace debugger-popup ;
151 : try-parse ( lines interactor -- quot/error/f )
153 drop parse-lines-interactive
157 dup error>> unexpected-eof? [ drop f ] when
161 : handle-interactive ( lines interactor -- quot/f ? )
163 { [ dup quotation? ] [ nip t ] }
164 { [ dup not ] [ drop "\n" swap user-input* drop f f ] }
165 [ handle-parse-error f f ]
168 M: interactor stream-read-quot
169 [ interactor-yield ] keep {
170 { [ over not ] [ drop ] }
171 { [ over callable? ] [ drop ] }
173 [ handle-interactive ] keep swap
174 [ interactor-finish ] [ nip stream-read-quot ] if
178 interactor "interactor" f {
179 { T{ key-down f f "RET" } evaluate-input }
180 { T{ key-down f { C+ } "k" } clear-editor }