1 ! Copyright (C) 2005, 2010 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 make 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 ;
19 FROM: namespaces => set ;
22 ! If waiting is t, we're waiting for user input, and invoking
23 ! evaluate-input resumes the thread.
24 TUPLE: interactor < source-editor
25 output history flag mailbox thread waiting token-model word-model popup ;
27 INSTANCE: interactor input-stream
29 : register-self ( interactor -- )
34 : interactor-continuation ( interactor -- continuation )
35 thread>> thread-continuation ;
37 : interactor-busy? ( interactor -- ? )
38 #! We're busy if there's no thread to resume.
41 [ thread>> dup [ thread-registered? ] when ]
46 M: interactor manifest>>
47 dup interactor-busy? [ drop f ] [
48 interactor-continuation name>>
49 manifest swap assoc-stack
52 : vocab-exists? ( name -- ? )
53 '[ _ { [ lookup-vocab ] [ find-vocab-root ] } 1|| ] [ drop f ] recover ;
55 GENERIC: (word-at-caret) ( token completion-mode -- obj )
57 M: vocab-completion (word-at-caret)
58 drop dup vocab-exists? [ >vocab-link ] [ drop f ] if ;
60 M: word-completion (word-at-caret)
62 '[ _ _ search-manifest ] [ drop f ] recover
65 M: char-completion (word-at-caret) 2drop f ;
67 M: path-completion (word-at-caret) 2drop f ;
69 M: color-completion (word-at-caret) 2drop f ;
71 : word-at-caret ( token interactor -- obj )
72 completion-mode (word-at-caret) ;
74 : <word-model> ( interactor -- model )
75 [ token-model>> 1/3 seconds <delay> ]
76 [ '[ _ word-at-caret ] ] bi
79 : <interactor> ( -- gadget )
82 dup one-word-elt <element-model> >>token-model
83 dup <word-model> >>word-model
84 dup model>> <history> >>history ;
87 [ call-next-method ] [ dup word-model>> add-connection ] bi ;
89 M: interactor ungraft*
90 [ dup word-model>> remove-connection ] [ call-next-method ] bi ;
92 M: interactor model-changed
93 2dup word-model>> eq? [
95 [ 2drop ] [ [ value>> ] dip show-summary ] if
96 ] [ call-next-method ] if ;
98 M: interactor stream-element-type drop +character+ ;
100 GENERIC: (print-input) ( object -- )
102 M: input (print-input)
103 dup presented associate
104 [ string>> H{ { font-style bold } } format ] with-nesting nl ;
106 M: word (print-input)
109 "sans-serif" font-name ,,
111 ] H{ } make format . ;
113 : print-input ( object interactor -- )
114 output>> [ (print-input) ] with-output-stream* ;
116 : interactor-continue ( obj interactor -- )
117 mailbox>> mailbox-put ;
119 : interactor-finish ( interactor -- )
120 [ history>> history-add ] keep
122 [ clear-editor drop ]
123 [ model>> clear-undo drop ] 2tri ;
125 : interactor-eof ( interactor -- )
126 dup interactor-busy? [
127 f over interactor-continue
130 : evaluate-input ( interactor -- )
131 dup interactor-busy? [ drop ] [
132 [ control-value ] keep interactor-continue
135 : interactor-yield ( interactor -- obj )
136 dup thread>> self eq? [
139 [ flag>> raise-flag ]
140 [ mailbox>> mailbox-get ]
145 : interactor-read ( interactor -- lines )
146 [ interactor-yield ] [ interactor-finish ] bi ;
148 M: interactor stream-readln
149 interactor-read dup [ first ] when ;
151 : (call-listener) ( quot command listener -- )
152 input>> dup interactor-busy? [ 3drop ] [
154 [ nip interactor-continue ]
158 M:: interactor stream-read-unsafe ( n buf interactor -- count )
161 interactor interactor-read dup [ "\n" join ] when
162 n short [ head-slice 0 buf copy ] keep
165 M: interactor stream-read1
166 dup interactor-read {
167 { [ dup not ] [ 2drop f ] }
168 { [ dup empty? ] [ drop stream-read1 ] }
169 { [ dup first empty? ] [ 2drop CHAR: \n ] }
173 M: interactor stream-read-until ( seps stream -- seq sep/f )
176 "\n" join CHAR: \n suffix
177 [ _ member? ] dupd find
178 [ [ head ] when* ] dip dup not
180 ] [ drop ] produce swap [ concat "" prepend-as ] dip ;
182 M: interactor dispose drop ;
184 : go-to-error ( interactor error -- )
185 [ line>> 1 - ] [ column>> ] bi 2array
189 TUPLE: listener-gadget < tool error-summary output scroller input ;
191 { 600 700 } listener-gadget set-tool-dim
193 : find-listener ( gadget -- listener )
194 [ listener-gadget? ] find-parent ;
196 : listener-streams ( listener -- input output )
197 [ input>> ] [ output>> <pane-stream> ] bi ;
199 : init-input/output ( listener -- listener )
201 [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
202 dup listener-streams >>output drop ;
204 : error-summary. ( -- )
206 H{ { table-gap { 3 3 } } } [
207 [ [ [ icon>> write-image ] with-cell ] each ] with-row
210 { "Press " { $command tool "common" show-error-list } " to view errors." }
214 : <error-summary> ( -- gadget )
215 error-list-model get [ drop error-summary. ] <pane-control>
216 COLOR: light-yellow <solid> >>interior ;
218 : init-error-summary ( listener -- listener )
219 <error-summary> >>error-summary
220 dup error-summary>> f track-add ;
222 : <listener-gadget> ( -- listener )
223 vertical listener-gadget new-track
226 dup output>> <scroller> >>scroller
227 dup scroller>> 1 track-add
230 M: listener-gadget focusable-child*
231 input>> dup popup>> or ;
233 : wait-for-listener ( listener -- )
234 #! Wait for the listener to start.
235 input>> flag>> wait-for-flag ;
237 : listener-busy? ( listener -- ? )
238 input>> interactor-busy? ;
240 : listener-window* ( -- listener )
242 dup "Listener" open-status-window ;
244 : listener-window ( -- )
245 [ listener-window* drop ] with-ui ;
247 \ listener-window H{ { +nullary+ t } } define-command
249 : (get-listener) ( quot -- listener )
255 [ input>> scroll>caret ]
256 [ input>> request-focus ] tri
258 ] [ listener-window* ] if* ; inline
260 : get-listener ( -- listener )
261 [ listener-gadget? ] (get-listener) ;
263 : show-listener ( -- )
266 \ show-listener H{ { +nullary+ t } } define-command
268 : get-ready-listener ( -- listener )
272 [ listener-busy? not ]
276 GENERIC: listener-input ( obj -- )
278 M: input listener-input string>> listener-input ;
280 M: string listener-input
282 [ set-editor-string ] [ request-focus ] bi ;
284 : call-listener ( quot command -- )
286 '[ _ _ _ dup wait-for-listener (call-listener) ]
287 "Listener call" spawn drop ;
289 M: listener-command invoke-command ( target command -- )
290 [ command-quot ] [ nip ] 2bi call-listener ;
292 M: listener-operation invoke-command ( target command -- )
293 [ operation-quot ] [ nip command>> ] 2bi call-listener ;
295 : eval-listener ( string -- )
296 get-listener input>> [ set-editor-string ] keep
299 : listener-run-files ( seq -- )
301 '[ _ [ run-file ] each ]
306 : com-end ( listener -- )
307 input>> interactor-eof ;
309 : clear-output ( listener -- )
310 output>> pane-clear ;
312 \ clear-output H{ { +listener+ t } } define-command
314 : clear-stack ( listener -- )
315 [ [ clear ] \ clear ] dip (call-listener) ;
317 : use-if-necessary ( word manifest -- )
318 2dup [ vocabulary>> ] dip and [
320 [ vocabulary>> use-vocab ]
321 [ dup name>> associate use-words ] bi
325 M: word accept-completion-hook
326 interactor>> manifest>> use-if-necessary ;
328 M: object accept-completion-hook 2drop ;
330 : quot-action ( interactor -- lines )
331 [ history>> history-add drop ] [ control-value ] [ select-all ] tri
332 parse-lines-interactive ;
334 : <debugger-popup> ( error continuation -- popup )
335 over compute-restarts [ hide-glass ] <debugger> "Error" <labeled-gadget> ;
337 : debugger-popup ( interactor error continuation -- )
338 [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
340 : handle-parse-error ( interactor error -- )
341 dup lexer-error? [ 2dup go-to-error error>> ] when
342 error-continuation get
345 : try-parse ( lines interactor -- quot/error/f )
346 [ drop parse-lines-interactive ] [
349 dup error>> unexpected-eof? [ drop f ] when
353 : handle-interactive ( lines interactor -- quot/f ? )
354 [ nip ] [ try-parse ] 2bi {
355 { [ dup quotation? ] [ nip t ] }
356 { [ dup not ] [ drop insert-newline f f ] }
357 [ handle-parse-error f f ]
360 M: interactor stream-read-quot
361 [ interactor-yield ] keep {
362 { [ over not ] [ drop ] }
363 { [ over callable? ] [ drop ] }
365 [ handle-interactive ] keep swap
366 [ interactor-finish ] [ nip stream-read-quot ] if
370 : interactor-operation ( gesture interactor -- ? )
371 [ token-model>> value>> ] keep word-at-caret
372 [ nip ] [ gesture>operation ] 2bi
373 dup [ invoke-command f ] [ 2drop t ] if ;
375 M: interactor handle-gesture
377 { [ over key-gesture? not ] [ call-next-method ] }
378 { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
379 { [ dup token-model>> value>> ] [ { [ interactor-operation ] [ call-next-method ] } 2&& ] }
383 interactor "interactor" f {
384 { T{ key-down f f "RET" } evaluate-input }
385 { T{ key-down f { C+ } "k" } clear-editor }
388 interactor "completion" f {
389 { T{ key-down f f "TAB" } code-completion-popup }
390 { T{ key-down f { C+ } "p" } recall-previous }
391 { T{ key-down f { C+ } "n" } recall-next }
392 { T{ key-down f { C+ } "r" } history-completion-popup }
395 : introduction. ( -- )
397 { $strong "Press " { $snippet "F1" } " at any time for help." } 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 ;