1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs calendar combinators locals
4 source-files.errors colors.constants combinators.short-circuit
5 compiler.units help.tips concurrency.flags concurrency.mailboxes
6 continuations destructors documents documents.elements fry hashtables
7 help help.markup io io.styles kernel lexer listener math models sets
8 models.delay models.arrow namespaces parser prettyprint quotations
9 sequences strings threads vocabs vocabs.refresh vocabs.loader
10 vocabs.parser words debugger ui ui.commands ui.pens.solid ui.gadgets
11 ui.gadgets.glass ui.gadgets.buttons ui.gadgets.editors
12 ui.gadgets.labeled ui.gadgets.panes ui.gadgets.scrollers
13 ui.gadgets.status-bar ui.gadgets.tracks ui.gadgets.borders ui.gestures
14 ui.operations ui.tools.browser ui.tools.common ui.tools.debugger
15 ui.tools.listener.completion ui.tools.listener.popups
16 ui.tools.listener.history ui.images ui.tools.error-list
18 FROM: source-files.errors => all-errors ;
21 ! If waiting is t, we're waiting for user input, and invoking
22 ! evaluate-input resumes the thread.
23 TUPLE: interactor < source-editor
24 output history flag mailbox thread waiting token-model word-model popup ;
26 : register-self ( interactor -- )
31 : interactor-continuation ( interactor -- continuation )
32 thread>> continuation>> value>> ;
34 : interactor-busy? ( interactor -- ? )
35 #! We're busy if there's no thread to resume.
38 [ thread>> dup [ thread-registered? ] when ]
43 M: interactor vocabs>>
44 dup interactor-busy? [ drop f ] [
46 interactor-continuation name>>
50 : vocab-exists? ( name -- ? )
51 '[ _ { [ vocab ] [ find-vocab-root ] } 1|| ] [ drop f ] recover ;
53 GENERIC: (word-at-caret) ( token completion-mode -- obj )
55 M: vocab-completion (word-at-caret)
56 drop dup vocab-exists? [ >vocab-link ] [ drop f ] if ;
58 M: word-completion (word-at-caret)
59 vocabs>> assoc-stack ;
61 M: char-completion (word-at-caret)
64 : word-at-caret ( token interactor -- obj )
65 completion-mode (word-at-caret) ;
67 : <word-model> ( interactor -- model )
68 [ token-model>> 1/3 seconds <delay> ]
69 [ '[ _ word-at-caret ] ] bi
72 : <interactor> ( -- gadget )
75 dup one-word-elt <element-model> >>token-model
76 dup <word-model> >>word-model
77 dup model>> <history> >>history ;
80 [ call-next-method ] [ dup word-model>> add-connection ] bi ;
82 M: interactor ungraft*
83 [ dup word-model>> remove-connection ] [ call-next-method ] bi ;
85 M: interactor model-changed
86 2dup word-model>> eq? [
88 [ 2drop ] [ [ value>> ] dip show-summary ] if
89 ] [ call-next-method ] if ;
91 M: interactor stream-element-type drop +character+ ;
93 GENERIC: (print-input) ( object -- )
95 M: input (print-input)
96 dup presented associate
97 [ string>> H{ { font-style bold } } format ] with-nesting nl ;
102 "sans-serif" font-name set
104 ] H{ } make-assoc format . ;
106 : print-input ( object interactor -- )
107 output>> [ (print-input) ] with-output-stream* ;
109 : interactor-continue ( obj interactor -- )
110 mailbox>> mailbox-put ;
112 : interactor-finish ( interactor -- )
113 [ history>> history-add ] keep
115 [ clear-editor drop ]
116 [ model>> clear-undo drop ] 2tri ;
118 : interactor-eof ( interactor -- )
119 dup interactor-busy? [
120 f over interactor-continue
123 : evaluate-input ( interactor -- )
124 dup interactor-busy? [ drop ] [
125 [ control-value ] keep interactor-continue
128 : interactor-yield ( interactor -- obj )
129 dup thread>> self eq? [
132 [ flag>> raise-flag ]
133 [ mailbox>> mailbox-get ]
138 : interactor-read ( interactor -- lines )
139 [ interactor-yield ] [ interactor-finish ] bi ;
141 M: interactor stream-readln
142 interactor-read dup [ first ] when ;
144 : (call-listener) ( quot command listener -- )
145 input>> dup interactor-busy? [ 3drop ] [
147 [ nip interactor-continue ]
151 M: interactor stream-read
155 [ interactor-read dup [ "\n" join ] when ] dip short head
158 M: interactor stream-read-partial
161 M: interactor stream-read1
162 dup interactor-read {
163 { [ dup not ] [ 2drop f ] }
164 { [ dup empty? ] [ drop stream-read1 ] }
165 { [ dup first empty? ] [ 2drop CHAR: \n ] }
169 M: interactor dispose drop ;
171 : go-to-error ( interactor error -- )
172 [ line>> 1- ] [ column>> ] bi 2array
176 TUPLE: listener-gadget < tool error-summary output scroller input ;
178 { 600 700 } listener-gadget set-tool-dim
180 : find-listener ( gadget -- listener )
181 [ listener-gadget? ] find-parent ;
183 : listener-streams ( listener -- input output )
184 [ input>> ] [ output>> <pane-stream> ] bi ;
186 : init-input/output ( listener -- listener )
188 [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
189 dup listener-streams >>output drop ;
191 : error-summary. ( -- )
193 H{ { table-gap { 3 3 } } } [
194 [ [ [ icon>> write-image ] with-cell ] each ] with-row
196 { "Press " { $command tool "common" show-error-list } " to view errors." }
200 : <error-summary> ( -- gadget )
201 error-list-model get [ drop error-summary. ] <pane-control>
202 COLOR: light-yellow <solid> >>interior ;
204 : init-error-summary ( listener -- listener )
205 <error-summary> >>error-summary
206 dup error-summary>> f track-add ;
208 : <listener-gadget> ( -- listener )
209 vertical listener-gadget new-track
212 dup output>> <scroller> >>scroller
213 dup scroller>> 1 track-add
216 M: listener-gadget focusable-child*
217 input>> dup popup>> or ;
219 : wait-for-listener ( listener -- )
220 #! Wait for the listener to start.
221 input>> flag>> wait-for-flag ;
223 : listener-busy? ( listener -- ? )
224 input>> interactor-busy? ;
226 : listener-window* ( -- listener )
228 dup "Listener" open-status-window ;
230 : listener-window ( -- )
231 [ listener-window* drop ] with-ui ;
233 \ listener-window H{ { +nullary+ t } } define-command
235 : (get-listener) ( quot -- listener )
241 [ input>> scroll>caret ]
242 [ input>> request-focus ] tri
244 ] [ listener-window* ] if* ; inline
246 : get-listener ( -- listener )
247 [ listener-gadget? ] (get-listener) ;
249 : show-listener ( -- )
252 \ show-listener H{ { +nullary+ t } } define-command
254 : get-ready-listener ( -- listener )
258 [ listener-busy? not ]
262 GENERIC: listener-input ( obj -- )
264 M: input listener-input string>> listener-input ;
266 M: string listener-input
268 [ set-editor-string ] [ request-focus ] bi ;
270 : call-listener ( quot command -- )
272 '[ _ _ _ dup wait-for-listener (call-listener) ]
273 "Listener call" spawn drop ;
275 M: listener-command invoke-command ( target command -- )
276 [ command-quot ] [ nip ] 2bi call-listener ;
278 M: listener-operation invoke-command ( target command -- )
279 [ operation-quot ] [ nip command>> ] 2bi call-listener ;
281 : eval-listener ( string -- )
282 get-listener input>> [ set-editor-string ] keep
285 : listener-run-files ( seq -- )
287 '[ _ [ run-file ] each ]
292 : com-end ( listener -- )
293 input>> interactor-eof ;
295 : clear-output ( listener -- )
296 output>> pane-clear ;
298 \ clear-output H{ { +listener+ t } } define-command
300 : clear-stack ( listener -- )
301 [ [ clear ] \ clear ] dip (call-listener) ;
303 : use-if-necessary ( word seq -- )
304 2dup [ vocabulary>> ] dip and [
305 2dup [ assoc-stack ] keep = [ 2drop ] [
306 [ vocabulary>> vocab-words ] dip push
310 M: word accept-completion-hook
311 interactor>> vocabs>> use-if-necessary ;
313 M: object accept-completion-hook 2drop ;
315 : quot-action ( interactor -- lines )
316 [ history>> history-add drop ] [ control-value ] [ select-all ] tri
317 [ parse-lines ] with-compilation-unit ;
319 : <debugger-popup> ( error continuation -- popup )
320 over compute-restarts [ hide-glass ] <debugger> "Error" <labeled-gadget> ;
322 : debugger-popup ( interactor error continuation -- )
323 [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
325 : handle-parse-error ( interactor error -- )
326 dup lexer-error? [ 2dup go-to-error error>> ] when
327 error-continuation get
330 : try-parse ( lines interactor -- quot/error/f )
331 [ drop parse-lines-interactive ] [
334 dup error>> unexpected-eof? [ drop f ] when
338 : handle-interactive ( lines interactor -- quot/f ? )
339 [ nip ] [ try-parse ] 2bi {
340 { [ dup quotation? ] [ nip t ] }
341 { [ dup not ] [ drop "\n" swap user-input* drop f f ] }
342 [ handle-parse-error f f ]
345 M: interactor stream-read-quot
346 [ interactor-yield ] keep {
347 { [ over not ] [ drop ] }
348 { [ over callable? ] [ drop ] }
350 [ handle-interactive ] keep swap
351 [ interactor-finish ] [ nip stream-read-quot ] if
355 : interactor-operation ( gesture interactor -- ? )
356 [ token-model>> value>> ] keep word-at-caret
357 [ nip ] [ gesture>operation ] 2bi
358 dup [ invoke-command f ] [ 2drop t ] if ;
360 M: interactor handle-gesture
362 { [ over key-gesture? not ] [ call-next-method ] }
363 { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
364 { [ dup token-model>> value>> ] [ { [ interactor-operation ] [ call-next-method ] } 2&& ] }
368 interactor "interactor" f {
369 { T{ key-down f f "RET" } evaluate-input }
370 { T{ key-down f { C+ } "k" } clear-editor }
373 interactor "completion" f {
374 { T{ key-down f f "TAB" } code-completion-popup }
375 { T{ key-down f { C+ } "p" } recall-previous }
376 { T{ key-down f { C+ } "n" } recall-next }
377 { T{ key-down f { C+ } "r" } history-completion-popup }
380 : listener-thread ( listener -- )
381 dup listener-streams [
382 [ com-browse ] help-hook set
383 '[ [ _ input>> ] 2dip debugger-popup ] error-hook set
389 : start-listener-thread ( listener -- )
392 [ input>> register-self ]
395 ] "Listener" spawn drop ;
397 : restart-listener ( listener -- )
398 #! Returns when listener is ready to receive input.
402 [ input>> clear-editor ]
403 [ start-listener-thread ]
404 [ wait-for-listener ]
407 : listener-help ( -- ) "help.home" com-browse ;
409 \ listener-help H{ { +nullary+ t } } define-command
411 : com-auto-use ( -- )
412 auto-use? [ not ] change ;
414 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
416 listener-gadget "misc" "Miscellaneous commands" {
417 { T{ key-down f f "F1" } listener-help }
420 listener-gadget "toolbar" f {
421 { f restart-listener }
422 { T{ key-down f { A+ } "u" } com-auto-use }
423 { T{ key-down f { A+ } "k" } clear-output }
424 { T{ key-down f { A+ } "K" } clear-stack }
425 { T{ key-down f { C+ } "d" } com-end }
428 listener-gadget "scrolling"
429 "The listener's scroller can be scrolled from the keyboard."
431 { T{ key-down f { A+ } "UP" } com-scroll-up }
432 { T{ key-down f { A+ } "DOWN" } com-scroll-down }
433 { T{ key-down f { A+ } "PAGE_UP" } com-page-up }
434 { T{ key-down f { A+ } "PAGE_DOWN" } com-page-down }
437 listener-gadget "multi-touch" f {
438 { up-action refresh-all }
441 M: listener-gadget graft*
442 [ call-next-method ] [ restart-listener ] bi ;
444 M: listener-gadget ungraft*
445 [ com-end ] [ call-next-method ] bi ;