1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs calendar combinators
4 combinators.short-circuit concurrency.flags concurrency.mailboxes
5 continuations destructors documents documents.elements fry hashtables
6 help help.markup help.tips io io.styles kernel lexer listener locals
7 make math models models.arrow models.delay namespaces parser
8 prettyprint quotations sequences source-files.errors strings system
9 threads tools.errors.model ui ui.commands ui.gadgets
10 ui.gadgets.editors ui.gadgets.glass ui.gadgets.labeled
11 ui.gadgets.panes ui.gadgets.scrollers ui.gadgets.status-bar
12 ui.gadgets.theme ui.gadgets.toolbar ui.gadgets.tracks ui.gestures
13 ui.operations ui.pens.solid ui.tools.browser ui.tools.common
14 ui.tools.debugger ui.tools.error-list ui.tools.listener.completion
15 ui.tools.listener.history ui.tools.listener.popups vocabs
16 vocabs.loader vocabs.parser vocabs.refresh words ;
19 TUPLE: interactor < source-editor
20 output history flag mailbox thread waiting token-model word-model popup ;
22 INSTANCE: interactor input-stream
24 : register-self ( interactor -- )
29 : interactor-continuation ( interactor -- continuation )
30 thread>> thread-continuation ;
32 : interactor-busy? ( interactor -- ? )
35 [ thread>> dup [ thread-registered? ] when ]
40 M: interactor manifest>>
41 dup interactor-busy? [ drop f ] [
42 interactor-continuation name>>
43 manifest swap assoc-stack
46 : vocab-exists? ( name -- ? )
47 '[ _ { [ lookup-vocab ] [ find-vocab-root ] } 1|| ] [ drop f ] recover ;
49 GENERIC: (word-at-caret) ( token completion-mode -- obj )
51 M: vocab-completion (word-at-caret)
52 drop dup vocab-exists? [ >vocab-link ] [ drop f ] if ;
54 M: word-completion (word-at-caret)
56 '[ _ _ search-manifest ] [ drop f ] recover
59 M: char-completion (word-at-caret) 2drop f ;
61 M: path-completion (word-at-caret) 2drop f ;
63 M: color-completion (word-at-caret) 2drop f ;
65 : word-at-caret ( token interactor -- obj )
66 completion-mode (word-at-caret) ;
68 : <word-model> ( interactor -- model )
69 [ token-model>> 1/3 seconds <delay> ]
70 [ '[ _ word-at-caret ] ] bi
73 : <interactor> ( -- gadget )
76 dup one-word-elt <element-model> >>token-model
77 dup <word-model> >>word-model
78 dup model>> <history> >>history ;
81 [ call-next-method ] [ dup word-model>> add-connection ] bi ;
83 M: interactor ungraft*
84 [ dup word-model>> remove-connection ] [ call-next-method ] bi ;
86 M: interactor model-changed
87 2dup word-model>> eq? [
89 [ 2drop ] [ [ value>> ] dip show-summary ] if
90 ] [ call-next-method ] if ;
92 M: interactor stream-element-type drop +character+ ;
94 GENERIC: (print-input) ( object -- )
96 M: input (print-input)
97 dup presented associate
98 [ string>> H{ { font-style bold } } format ] with-nesting nl ;
100 M: word (print-input)
103 "sans-serif" font-name ,,
105 ] H{ } make format . ;
107 : print-input ( object interactor -- )
108 output>> [ (print-input) ] with-output-stream* ;
110 : interactor-continue ( obj interactor -- )
111 mailbox>> mailbox-put ;
113 : interactor-finish ( interactor -- )
114 [ history>> history-add ] keep
116 [ clear-editor drop ]
117 [ model>> clear-undo drop ] 2tri ;
119 : interactor-eof ( interactor -- )
120 dup interactor-busy? [
121 f over interactor-continue
124 : evaluate-input ( interactor -- )
125 dup interactor-busy? [ drop ] [
126 [ control-value ] keep interactor-continue
129 : interactor-yield ( interactor -- obj )
130 dup thread>> self eq? [
133 [ flag>> raise-flag ]
134 [ mailbox>> mailbox-get ]
139 : interactor-read ( interactor -- lines )
140 [ interactor-yield ] [ interactor-finish ] bi ;
142 M: interactor stream-readln
143 interactor-read dup [ first ] when ;
145 : (call-listener) ( quot command listener -- )
146 input>> dup interactor-busy? [ 3drop ] [
148 [ nip interactor-continue ]
152 M:: interactor stream-read-unsafe ( n buf interactor -- count )
155 interactor interactor-read dup [ "\n" join ] when
156 n short [ head-slice 0 buf copy ] keep
159 M: interactor stream-read1
160 dup interactor-read {
161 { [ dup not ] [ 2drop f ] }
162 { [ dup empty? ] [ drop stream-read1 ] }
163 { [ dup first empty? ] [ 2drop CHAR: \n ] }
167 M: interactor stream-read-until ( seps stream -- seq sep/f )
170 "\n" join CHAR: \n suffix
171 [ _ member? ] dupd find
172 [ [ head ] when* ] dip dup not
174 ] [ drop ] produce swap [ concat "" prepend-as ] dip ;
176 M: interactor dispose drop ;
178 : go-to-error ( interactor error -- )
179 [ line>> 1 - ] [ column>> ] bi 2array
183 TUPLE: listener-gadget < tool error-summary output scroller input ;
185 { 600 700 } listener-gadget set-tool-dim
187 : listener-streams ( listener -- input output )
188 [ input>> ] [ output>> <pane-stream> ] bi ;
190 : init-input/output ( listener -- listener )
192 [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
193 dup listener-streams >>output drop ;
195 : error-summary. ( -- )
197 H{ { table-gap { 3 3 } } } [
198 [ [ [ icon>> write-image ] with-cell ] each ] with-row
201 { "Press " { $command tool "common" show-error-list } " to view errors." }
205 : <error-summary> ( -- gadget )
206 error-list-model get [ drop error-summary. ] <pane-control>
207 error-summary-background <solid> >>interior ;
209 : init-error-summary ( listener -- listener )
210 <error-summary> >>error-summary
211 dup error-summary>> f track-add ;
213 : add-listener-area ( listener -- listener )
214 dup output>> margins <scroller> >>scroller
215 dup scroller>> white-interior 1 track-add ;
217 : <listener-gadget> ( -- listener )
218 vertical listener-gadget new-track with-lines
224 M: listener-gadget focusable-child*
225 input>> dup popup>> or ;
227 : wait-for-listener ( listener -- )
228 input>> flag>> wait-for-flag ;
230 : listener-busy? ( listener -- ? )
231 input>> interactor-busy? ;
233 : listener-window* ( -- listener )
235 dup "Listener" open-status-window ;
237 : listener-window ( -- )
238 [ listener-window* drop ] with-ui ;
240 \ listener-window H{ { +nullary+ t } } define-command
242 : (get-listener) ( quot -- listener )
248 [ input>> scroll>caret ]
249 [ input>> request-focus ] tri
251 ] [ listener-window* ] if* ; inline
253 : get-listener ( -- listener )
254 [ listener-gadget? ] (get-listener) ;
256 : show-listener ( -- )
259 \ show-listener H{ { +nullary+ t } } define-command
261 : get-ready-listener ( -- listener )
265 [ listener-busy? not ]
269 GENERIC: listener-input ( obj -- )
271 M: input listener-input string>> listener-input ;
273 M: string listener-input
275 [ set-editor-string ] [ request-focus ] bi ;
277 : call-listener ( quot command -- )
279 '[ _ _ _ dup wait-for-listener (call-listener) ]
280 "Listener call" spawn drop ;
282 M: listener-command invoke-command ( target command -- )
283 [ command-quot ] [ nip ] 2bi call-listener ;
285 M: listener-operation invoke-command ( target command -- )
286 [ operation-quot ] [ nip command>> ] 2bi call-listener ;
288 : eval-listener ( string -- )
289 get-listener input>> [ set-editor-string ] keep
292 : listener-run-files ( seq -- )
294 '[ _ [ run-file ] each ]
299 : com-end ( listener -- )
300 input>> interactor-eof ;
302 : clear-output ( listener -- )
303 output>> pane-clear ;
305 \ clear-output H{ { +listener+ t } } define-command
307 : clear-stack ( listener -- )
308 [ [ clear ] \ clear ] dip (call-listener) ;
310 : use-if-necessary ( word manifest -- )
311 2dup [ vocabulary>> ] dip and [
313 [ vocabulary>> use-vocab ]
314 [ dup name>> associate use-words ] bi
318 M: word accept-completion-hook
319 interactor>> manifest>> use-if-necessary ;
321 M: object accept-completion-hook 2drop ;
323 : quot-action ( interactor -- lines )
324 [ history>> history-add drop ] [ control-value ] [ select-all ] tri
325 parse-lines-interactive ;
327 : <debugger-popup> ( error continuation -- popup )
328 over compute-restarts [ hide-glass ] <debugger>
329 "Error" debugger-color <framed-labeled> ;
331 : debugger-popup ( interactor error continuation -- )
332 [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
334 : handle-parse-error ( interactor error -- )
335 dup lexer-error? [ 2dup go-to-error error>> ] when
336 error-continuation get
339 : try-parse ( lines interactor -- quot/error/f )
340 [ drop parse-lines-interactive ] [
343 dup error>> unexpected-eof? [ drop f ] when
347 : handle-interactive ( lines interactor -- quot/f ? )
348 [ nip ] [ try-parse ] 2bi {
349 { [ dup quotation? ] [ nip t ] }
350 { [ dup not ] [ drop insert-newline f f ] }
351 [ handle-parse-error f f ]
354 M: interactor stream-read-quot
355 [ interactor-yield ] keep {
356 { [ over not ] [ drop ] }
357 { [ over callable? ] [ drop ] }
359 [ handle-interactive ] keep swap
360 [ interactor-finish ] [ nip stream-read-quot ] if
364 : interactor-operation ( gesture interactor -- ? )
365 [ token-model>> value>> ] keep word-at-caret
366 [ nip ] [ gesture>operation ] 2bi
367 [ invoke-command f ] [ drop t ] if* ;
369 M: interactor handle-gesture
371 { [ over key-gesture? not ] [ call-next-method ] }
372 { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
374 [ dup token-model>> value>> ]
375 [ { [ interactor-operation ] [ call-next-method ] } 2&& ]
380 interactor "interactor" f {
381 { T{ key-down f f "RET" } evaluate-input }
382 { T{ key-down f { C+ } "k" } clear-editor }
385 interactor "completion" f {
386 { T{ key-down f f "TAB" } code-completion-popup }
387 { T{ key-down f { C+ } "p" } recall-previous }
388 { T{ key-down f { C+ } "n" } recall-next }
389 { T{ key-down f f "UP" } recall-previous }
390 { T{ key-down f f "DOWN" } recall-next }
391 { T{ key-down f { C+ } "r" } history-completion-popup }
394 : introduction. ( -- )
396 { $strong "Press " { $snippet "F1" } " at any time for help." } print-content nl
397 version-info print-content nl nl ;
399 : listener-thread ( listener -- )
400 dup listener-streams [
401 [ com-browse ] help-hook set
402 '[ [ _ input>> ] 2dip debugger-popup ] error-hook set
407 "The listener has exited. To start it again, click “Restart Listener”." print
408 ] with-input-output+error-streams* ;
410 : start-listener-thread ( listener -- )
413 [ input>> register-self ]
416 ] "Listener" spawn drop ;
418 : restart-listener ( listener -- )
419 ! Returns when listener is ready to receive input.
423 [ input>> clear-editor ]
424 [ start-listener-thread ]
425 [ wait-for-listener ]
428 : com-help ( -- ) "help.home" com-browse ;
430 \ com-help H{ { +nullary+ t } } define-command
432 : com-auto-use ( -- )
435 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
437 listener-gadget "toolbar" f {
438 { f restart-listener }
439 { T{ key-down f { A+ } "u" } com-auto-use }
440 { T{ key-down f { A+ } "k" } clear-output }
441 { T{ key-down f { A+ } "K" } clear-stack }
442 { T{ key-down f { C+ } "d" } com-end }
443 { T{ key-down f f "F1" } com-help }
446 listener-gadget "scrolling"
447 "The listener's scroller can be scrolled from the keyboard."
449 { T{ key-down f { A+ } "UP" } com-scroll-up }
450 { T{ key-down f { A+ } "DOWN" } com-scroll-down }
451 { T{ key-down f { A+ } "PAGE_UP" } com-page-up }
452 { T{ key-down f { A+ } "PAGE_DOWN" } com-page-down }
455 listener-gadget "multi-touch" f {
456 { up-action refresh-all }
459 M: listener-gadget graft*
460 [ call-next-method ] [ restart-listener ] bi ;
462 M: listener-gadget ungraft*
463 [ com-end ] [ call-next-method ] bi ;
467 :: make-font-style ( family size -- assoc )
469 family font-name pick set-at
470 size font-size pick set-at ;
474 :: set-listener-font ( family size -- )
475 get-listener input>> :> inter
476 family size make-font-style
477 inter output>> make-span-stream :> ostream
478 ostream inter output<<
483 ostream output-stream set ;