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 ;
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 : clear-input ( interactor -- ) model>> clear-doc ;
86 : interactor-finish ( interactor -- )
87 #! The spawn is a kludge to make it infer. Stupid.
88 [ editor-string ] keep
89 [ interactor-input. ] 2keep
90 [ add-interactor-history ] keep
91 [ clear-input ] curry "Clearing input" spawn drop ;
93 : interactor-eof ( interactor -- )
94 dup interactor-busy? [
95 f over interactor-continue
98 : evaluate-input ( interactor -- )
99 dup interactor-busy? [
100 dup control-value over interactor-continue
103 : interactor-yield ( interactor -- obj )
104 dup thread>> self eq? [
107 [ flag>> raise-flag ]
108 [ mailbox>> mailbox-get ]
113 : interactor-read ( interactor -- lines )
114 [ interactor-yield ] [ interactor-finish ] bi ;
116 M: interactor stream-readln
117 interactor-read dup [ first ] when ;
119 : interactor-call ( quot interactor -- )
120 dup interactor-busy? [
121 2dup interactor-input.
122 2dup interactor-continue
125 M: interactor stream-read
129 >r interactor-read dup [ "\n" join ] when r> short head
132 M: interactor stream-read-partial
135 M: interactor stream-read1
136 dup interactor-read {
137 { [ dup not ] [ 2drop f ] }
138 { [ dup empty? ] [ drop stream-read1 ] }
139 { [ dup first empty? ] [ 2drop CHAR: \n ] }
143 M: interactor dispose drop ;
145 : go-to-error ( interactor error -- )
146 [ line>> 1- ] [ column>> ] bi 2array
150 : handle-parse-error ( interactor error -- )
151 dup lexer-error? [ 2dup go-to-error error>> ] when
152 swap find-workspace debugger-popup ;
154 : try-parse ( lines interactor -- quot/error/f )
156 drop parse-lines-interactive
160 dup error>> unexpected-eof? [ drop f ] when
164 : handle-interactive ( lines interactor -- quot/f ? )
166 { [ dup quotation? ] [ nip t ] }
167 { [ dup not ] [ drop "\n" swap user-input f f ] }
168 [ handle-parse-error f f ]
171 M: interactor stream-read-quot
172 [ interactor-yield ] keep {
173 { [ over not ] [ drop ] }
174 { [ over callable? ] [ drop ] }
176 [ handle-interactive ] keep swap
177 [ interactor-finish ] [ nip stream-read-quot ] if
181 M: interactor pref-dim*
182 [ line-height 4 * 0 swap 2array ] [ call-next-method ] bi
185 interactor "interactor" f {
186 { T{ key-down f f "RET" } evaluate-input }
187 { T{ key-down f { C+ } "k" } clear-input }