1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs calendar colors.constants
4 combinators combinators.short-circuit concurrency.flags
5 concurrency.mailboxes continuations destructors documents
6 documents.elements fry hashtables help help.markup help.tips io
7 io.styles kernel lexer listener locals make math models
8 models.arrow models.delay namespaces parser prettyprint
9 quotations sequences source-files.errors strings system threads
10 tools.errors.model ui ui.commands ui.gadgets ui.gadgets.buttons
11 ui.gadgets.editors ui.gadgets.glass ui.gadgets.labeled
12 ui.gadgets.panes ui.gadgets.scrollers ui.gadgets.status-bar
13 ui.gadgets.tracks ui.gestures ui.operations ui.pens.solid
14 ui.tools.browser ui.tools.common ui.tools.debugger
15 ui.tools.error-list ui.tools.listener.completion
16 ui.tools.listener.history ui.tools.listener.popups vocabs
17 vocabs.loader vocabs.parser vocabs.refresh words ;
20 ! If waiting is t, we're waiting for user input, and invoking
21 ! evaluate-input resumes the thread.
22 TUPLE: interactor < source-editor
23 output history flag mailbox thread waiting token-model word-model popup ;
25 INSTANCE: interactor input-stream
27 : register-self ( interactor -- )
32 : interactor-continuation ( interactor -- continuation )
33 thread>> thread-continuation ;
35 : interactor-busy? ( interactor -- ? )
36 #! We're busy if there's no thread to resume.
39 [ thread>> dup [ thread-registered? ] when ]
44 M: interactor manifest>>
45 dup interactor-busy? [ drop f ] [
46 interactor-continuation name>>
47 manifest swap assoc-stack
50 : vocab-exists? ( name -- ? )
51 '[ _ { [ lookup-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)
60 '[ _ _ search-manifest ] [ drop f ] recover
63 M: char-completion (word-at-caret) 2drop f ;
65 M: path-completion (word-at-caret) 2drop f ;
67 M: color-completion (word-at-caret) 2drop f ;
69 : word-at-caret ( token interactor -- obj )
70 completion-mode (word-at-caret) ;
72 : <word-model> ( interactor -- model )
73 [ token-model>> 1/3 seconds <delay> ]
74 [ '[ _ word-at-caret ] ] bi
77 : <interactor> ( -- gadget )
80 dup one-word-elt <element-model> >>token-model
81 dup <word-model> >>word-model
82 dup model>> <history> >>history ;
85 [ call-next-method ] [ dup word-model>> add-connection ] bi ;
87 M: interactor ungraft*
88 [ dup word-model>> remove-connection ] [ call-next-method ] bi ;
90 M: interactor model-changed
91 2dup word-model>> eq? [
93 [ 2drop ] [ [ value>> ] dip show-summary ] if
94 ] [ call-next-method ] if ;
96 M: interactor stream-element-type drop +character+ ;
98 GENERIC: (print-input) ( object -- )
100 M: input (print-input)
101 dup presented associate
102 [ string>> H{ { font-style bold } } format ] with-nesting nl ;
104 M: word (print-input)
107 "sans-serif" font-name ,,
109 ] H{ } make format . ;
111 : print-input ( object interactor -- )
112 output>> [ (print-input) ] with-output-stream* ;
114 : interactor-continue ( obj interactor -- )
115 mailbox>> mailbox-put ;
117 : interactor-finish ( interactor -- )
118 [ history>> history-add ] keep
120 [ clear-editor drop ]
121 [ model>> clear-undo drop ] 2tri ;
123 : interactor-eof ( interactor -- )
124 dup interactor-busy? [
125 f over interactor-continue
128 : evaluate-input ( interactor -- )
129 dup interactor-busy? [ drop ] [
130 [ control-value ] keep interactor-continue
133 : interactor-yield ( interactor -- obj )
134 dup thread>> self eq? [
137 [ flag>> raise-flag ]
138 [ mailbox>> mailbox-get ]
143 : interactor-read ( interactor -- lines )
144 [ interactor-yield ] [ interactor-finish ] bi ;
146 M: interactor stream-readln
147 interactor-read dup [ first ] when ;
149 : (call-listener) ( quot command listener -- )
150 input>> dup interactor-busy? [ 3drop ] [
152 [ nip interactor-continue ]
156 M:: interactor stream-read-unsafe ( n buf interactor -- count )
159 interactor interactor-read dup [ "\n" join ] when
160 n short [ head-slice 0 buf copy ] keep
163 M: interactor stream-read1
164 dup interactor-read {
165 { [ dup not ] [ 2drop f ] }
166 { [ dup empty? ] [ drop stream-read1 ] }
167 { [ dup first empty? ] [ 2drop CHAR: \n ] }
171 M: interactor stream-read-until ( seps stream -- seq sep/f )
174 "\n" join CHAR: \n suffix
175 [ _ member? ] dupd find
176 [ [ head ] when* ] dip dup not
178 ] [ drop ] produce swap [ concat "" prepend-as ] dip ;
180 M: interactor dispose drop ;
182 : go-to-error ( interactor error -- )
183 [ line>> 1 - ] [ column>> ] bi 2array
187 TUPLE: listener-gadget < tool error-summary output scroller input ;
189 { 600 700 } listener-gadget set-tool-dim
191 : find-listener ( gadget -- listener )
192 [ listener-gadget? ] find-parent ;
194 : listener-streams ( listener -- input output )
195 [ input>> ] [ output>> <pane-stream> ] bi ;
197 : init-input/output ( listener -- listener )
199 [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
200 dup listener-streams >>output drop ;
202 : error-summary. ( -- )
204 H{ { table-gap { 3 3 } } } [
205 [ [ [ icon>> write-image ] with-cell ] each ] with-row
208 { "Press " { $command tool "common" show-error-list } " to view errors." }
212 : <error-summary> ( -- gadget )
213 error-list-model get [ drop error-summary. ] <pane-control>
214 COLOR: light-yellow <solid> >>interior ;
216 : init-error-summary ( listener -- listener )
217 <error-summary> >>error-summary
218 dup error-summary>> f track-add ;
220 : <listener-gadget> ( -- listener )
221 vertical listener-gadget new-track
224 dup output>> <scroller> >>scroller
225 dup scroller>> 1 track-add
228 M: listener-gadget focusable-child*
229 input>> dup popup>> or ;
231 : wait-for-listener ( listener -- )
232 #! Wait for the listener to start.
233 input>> flag>> wait-for-flag ;
235 : listener-busy? ( listener -- ? )
236 input>> interactor-busy? ;
238 : listener-window* ( -- listener )
240 dup "Listener" open-status-window ;
242 : listener-window ( -- )
243 [ listener-window* drop ] with-ui ;
245 \ listener-window H{ { +nullary+ t } } define-command
247 : (get-listener) ( quot -- listener )
253 [ input>> scroll>caret ]
254 [ input>> request-focus ] tri
256 ] [ listener-window* ] if* ; inline
258 : get-listener ( -- listener )
259 [ listener-gadget? ] (get-listener) ;
261 : show-listener ( -- )
264 \ show-listener H{ { +nullary+ t } } define-command
266 : get-ready-listener ( -- listener )
270 [ listener-busy? not ]
274 GENERIC: listener-input ( obj -- )
276 M: input listener-input string>> listener-input ;
278 M: string listener-input
280 [ set-editor-string ] [ request-focus ] bi ;
282 : call-listener ( quot command -- )
284 '[ _ _ _ dup wait-for-listener (call-listener) ]
285 "Listener call" spawn drop ;
287 M: listener-command invoke-command ( target command -- )
288 [ command-quot ] [ nip ] 2bi call-listener ;
290 M: listener-operation invoke-command ( target command -- )
291 [ operation-quot ] [ nip command>> ] 2bi call-listener ;
293 : eval-listener ( string -- )
294 get-listener input>> [ set-editor-string ] keep
297 : listener-run-files ( seq -- )
299 '[ _ [ run-file ] each ]
304 : com-end ( listener -- )
305 input>> interactor-eof ;
307 : clear-output ( listener -- )
308 output>> pane-clear ;
310 \ clear-output H{ { +listener+ t } } define-command
312 : clear-stack ( listener -- )
313 [ [ clear ] \ clear ] dip (call-listener) ;
315 : use-if-necessary ( word manifest -- )
316 2dup [ vocabulary>> ] dip and [
318 [ vocabulary>> use-vocab ]
319 [ dup name>> associate use-words ] bi
323 M: word accept-completion-hook
324 interactor>> manifest>> use-if-necessary ;
326 M: object accept-completion-hook 2drop ;
328 : quot-action ( interactor -- lines )
329 [ history>> history-add drop ] [ control-value ] [ select-all ] tri
330 parse-lines-interactive ;
332 : <debugger-popup> ( error continuation -- popup )
333 over compute-restarts [ hide-glass ] <debugger> "Error" <labeled-gadget> ;
335 : debugger-popup ( interactor error continuation -- )
336 [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
338 : handle-parse-error ( interactor error -- )
339 dup lexer-error? [ 2dup go-to-error error>> ] when
340 error-continuation get
343 : try-parse ( lines interactor -- quot/error/f )
344 [ drop parse-lines-interactive ] [
347 dup error>> unexpected-eof? [ drop f ] when
351 : handle-interactive ( lines interactor -- quot/f ? )
352 [ nip ] [ try-parse ] 2bi {
353 { [ dup quotation? ] [ nip t ] }
354 { [ dup not ] [ drop insert-newline f f ] }
355 [ handle-parse-error f f ]
358 M: interactor stream-read-quot
359 [ interactor-yield ] keep {
360 { [ over not ] [ drop ] }
361 { [ over callable? ] [ drop ] }
363 [ handle-interactive ] keep swap
364 [ interactor-finish ] [ nip stream-read-quot ] if
368 : interactor-operation ( gesture interactor -- ? )
369 [ token-model>> value>> ] keep word-at-caret
370 [ nip ] [ gesture>operation ] 2bi
371 dup [ invoke-command f ] [ 2drop t ] if ;
373 M: interactor handle-gesture
375 { [ over key-gesture? not ] [ call-next-method ] }
376 { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
377 { [ dup token-model>> value>> ] [ { [ interactor-operation ] [ call-next-method ] } 2&& ] }
381 interactor "interactor" f {
382 { T{ key-down f f "RET" } evaluate-input }
383 { T{ key-down f { C+ } "k" } clear-editor }
386 interactor "completion" f {
387 { T{ key-down f f "TAB" } code-completion-popup }
388 { T{ key-down f { C+ } "p" } recall-previous }
389 { T{ key-down f { C+ } "n" } recall-next }
390 { T{ key-down f { C+ } "r" } history-completion-popup }
393 : introduction. ( -- )
395 { $strong "Press " { $snippet "F1" } " at any time for help." } print-content nl
396 version-info print-content nl nl ;
398 : listener-thread ( listener -- )
399 dup listener-streams [
400 [ com-browse ] help-hook set
401 '[ [ _ input>> ] 2dip debugger-popup ] error-hook set
406 "The listener has exited. To start it again, click “Restart Listener”." print
407 ] with-input-output+error-streams* ;
409 : start-listener-thread ( listener -- )
412 [ input>> register-self ]
415 ] "Listener" spawn drop ;
417 : restart-listener ( listener -- )
418 #! Returns when listener is ready to receive input.
422 [ input>> clear-editor ]
423 [ start-listener-thread ]
424 [ wait-for-listener ]
427 : com-help ( -- ) "help.home" com-browse ;
429 \ com-help H{ { +nullary+ t } } define-command
431 : com-auto-use ( -- )
434 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
436 listener-gadget "toolbar" f {
437 { f restart-listener }
438 { T{ key-down f { A+ } "u" } com-auto-use }
439 { T{ key-down f { A+ } "k" } clear-output }
440 { T{ key-down f { A+ } "K" } clear-stack }
441 { T{ key-down f { C+ } "d" } com-end }
442 { T{ key-down f f "F1" } com-help }
445 listener-gadget "scrolling"
446 "The listener's scroller can be scrolled from the keyboard."
448 { T{ key-down f { A+ } "UP" } com-scroll-up }
449 { T{ key-down f { A+ } "DOWN" } com-scroll-down }
450 { T{ key-down f { A+ } "PAGE_UP" } com-page-up }
451 { T{ key-down f { A+ } "PAGE_DOWN" } com-page-down }
454 listener-gadget "multi-touch" f {
455 { up-action refresh-all }
458 M: listener-gadget graft*
459 [ call-next-method ] [ restart-listener ] bi ;
461 M: listener-gadget ungraft*
462 [ com-end ] [ call-next-method ] bi ;
466 :: make-font-style ( family size -- assoc )
468 family font-name pick set-at
469 size font-size pick set-at ;
473 :: set-listener-font ( family size -- )
474 get-listener input>> :> inter
475 family size make-font-style
476 inter output>> make-span-stream :> ostream
477 ostream inter output<<
482 ostream output-stream set ;