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
5 concurrency.mailboxes continuations destructors documents
6 documents.elements fonts fry hashtables help help.markup
7 help.tips io io.styles kernel lexer listener literals locals
8 math models models.arrow models.delay namespaces parser
9 prettyprint sequences source-files.errors strings system threads
10 tools.errors.model ui ui.commands ui.gadgets ui.gadgets.editors
11 ui.gadgets.glass ui.gadgets.labeled ui.gadgets.panes
12 ui.gadgets.scrollers ui.gadgets.status-bar ui.theme
14 ui.gadgets.toolbar ui.gadgets.tracks ui.gestures ui.operations
15 ui.pens.solid ui.tools.browser ui.tools.common ui.tools.debugger
16 ui.tools.error-list ui.tools.listener.completion
17 ui.tools.listener.history ui.tools.listener.popups vocabs
18 vocabs.loader vocabs.parser vocabs.refresh words ;
21 TUPLE: interactor < source-editor
22 output history flag mailbox thread waiting token-model word-model popup ;
24 INSTANCE: interactor input-stream
26 : register-self ( interactor -- )
31 : interactor-continuation ( interactor -- continuation )
32 thread>> thread-continuation ;
34 : interactor-busy? ( interactor -- ? )
37 [ thread>> dup [ thread-registered? ] when ]
42 M: interactor manifest>>
43 dup interactor-busy? [ drop f ] [
44 interactor-continuation name>>
45 manifest swap assoc-stack
48 : vocab-exists? ( name -- ? )
49 '[ _ { [ lookup-vocab ] [ find-vocab-root ] } 1|| ] [ drop f ] recover ;
51 GENERIC: (word-at-caret) ( token completion-mode -- obj )
53 M: vocab-completion (word-at-caret)
54 drop dup vocab-exists? [ >vocab-link ] [ drop f ] if ;
56 M: word-completion (word-at-caret)
58 '[ _ _ search-manifest ] [ drop f ] recover
61 M: char-completion (word-at-caret) 2drop f ;
63 M: path-completion (word-at-caret) 2drop f ;
65 M: color-completion (word-at-caret) 2drop f ;
67 : word-at-caret ( token interactor -- obj )
68 completion-mode (word-at-caret) ;
70 : <word-model> ( interactor -- model )
71 [ token-model>> 1/3 seconds <delay> ]
72 [ '[ _ word-at-caret ] ] bi
75 : <interactor> ( -- gadget )
79 dup one-word-elt <element-model> >>token-model
80 dup <word-model> >>word-model
81 dup model>> <history> >>history ;
84 [ call-next-method ] [ dup word-model>> add-connection ] bi ;
86 M: interactor ungraft*
87 [ dup word-model>> remove-connection ] [ call-next-method ] bi ;
89 M: interactor model-changed
90 2dup word-model>> eq? [
92 [ 2drop ] [ [ value>> ] dip show-summary ] if
93 ] [ call-next-method ] if ;
95 M: interactor stream-element-type drop +character+ ;
97 GENERIC: (print-input) ( object -- )
99 SYMBOL: listener-input-style
102 { foreground $ text-color }
103 } listener-input-style set-global
105 SYMBOL: listener-word-style
107 { font-name "sans-serif" }
109 { foreground $ text-color }
110 } listener-word-style set-global
112 M: input (print-input)
113 dup presented associate [
114 string>> listener-input-style get-global format
117 M: word (print-input)
118 "Command: " listener-word-style get-global format . ;
120 : print-input ( object interactor -- )
121 output>> [ (print-input) ] with-output-stream* ;
123 : interactor-continue ( obj interactor -- )
124 mailbox>> mailbox-put ;
126 : interactor-finish ( interactor -- )
127 [ history>> history-add ] keep
129 [ clear-editor drop ]
130 [ model>> clear-undo drop ] 2tri ;
132 : interactor-eof ( interactor -- )
133 dup interactor-busy? [
134 f over interactor-continue
137 : evaluate-input ( interactor -- )
138 dup interactor-busy? [ drop ] [
139 [ control-value ] keep interactor-continue
142 : interactor-yield ( interactor -- obj )
143 dup thread>> self eq? [
146 [ flag>> raise-flag ]
147 [ mailbox>> mailbox-get ]
152 : interactor-read ( interactor -- lines )
153 [ interactor-yield ] [ interactor-finish ] bi ;
155 M: interactor stream-readln
156 interactor-read dup [ first ] when ;
158 : (call-listener) ( quot command listener -- )
159 input>> dup interactor-busy? [ 3drop ] [
161 [ nip interactor-continue ]
165 M:: interactor stream-read-unsafe ( n buf interactor -- count )
168 interactor interactor-read dup [ "\n" join ] when
169 n short [ head-slice 0 buf copy ] keep
172 M: interactor stream-read1
173 dup interactor-read {
174 { [ dup not ] [ 2drop f ] }
175 { [ dup empty? ] [ drop stream-read1 ] }
176 { [ dup first empty? ] [ 2drop CHAR: \n ] }
180 M: interactor stream-read-until ( seps stream -- seq sep/f )
183 "\n" join CHAR: \n suffix
184 [ _ member? ] dupd find
185 [ [ head ] when* ] dip dup not
187 ] [ drop ] produce swap [ concat "" prepend-as ] dip ;
189 M: interactor dispose drop ;
191 : go-to-error ( interactor error -- )
192 [ line>> 1 - ] [ column>> ] bi 2array
196 TUPLE: listener-gadget < tool error-summary output scroller input ;
198 { 600 700 } listener-gadget set-tool-dim
200 : listener-streams ( listener -- input output )
201 [ input>> ] [ output>> <pane-stream> ] bi ;
203 : init-input/output ( listener -- listener )
205 [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
206 dup listener-streams >>output drop ;
208 : error-summary. ( -- )
210 H{ { table-gap { 3 3 } } } [
211 [ [ [ icon>> write-image ] with-cell ] each ] with-row
214 { "Press " { $command tool "common" show-error-list } " to view errors." }
218 : <error-summary> ( -- gadget )
219 error-list-model get [ drop error-summary. ] <pane-control>
220 error-summary-background <solid> >>interior ;
222 : init-error-summary ( listener -- listener )
223 <error-summary> >>error-summary
224 dup error-summary>> f track-add ;
226 : add-listener-area ( listener -- listener )
227 dup output>> margins <scroller> >>scroller
228 dup scroller>> white-interior 1 track-add ;
230 : <listener-gadget> ( -- listener )
231 vertical listener-gadget new-track with-lines
237 M: listener-gadget focusable-child*
238 input>> dup popup>> or ;
240 : wait-for-listener ( listener -- )
241 input>> flag>> 5 seconds wait-for-flag-timeout ;
243 : listener-busy? ( listener -- ? )
244 input>> interactor-busy? ;
246 : listener-window* ( -- listener )
248 dup "Listener" open-status-window ;
250 : listener-window ( -- )
251 [ listener-window* drop ] with-ui ;
253 \ listener-window H{ { +nullary+ t } } define-command
255 : (get-listener) ( quot -- listener )
261 [ input>> scroll>caret ]
262 [ input>> request-focus ] tri
264 ] [ listener-window* ] if* ; inline
266 : get-listener ( -- listener )
267 [ listener-gadget? ] (get-listener) ;
269 : show-listener ( -- )
272 \ show-listener H{ { +nullary+ t } } define-command
274 : get-ready-listener ( -- listener )
278 [ listener-busy? not ]
282 GENERIC: listener-input ( obj -- )
284 M: input listener-input string>> listener-input ;
286 M: string listener-input
288 [ set-editor-string ] [ request-focus ] bi ;
290 : call-listener ( quot command -- )
292 '[ _ _ _ dup wait-for-listener (call-listener) ]
293 "Listener call" spawn drop ;
295 M: listener-command invoke-command ( target command -- )
296 [ command-quot ] [ nip ] 2bi call-listener ;
298 M: listener-operation invoke-command ( target command -- )
299 [ operation-quot ] [ nip command>> ] 2bi call-listener ;
301 : eval-listener ( string -- )
302 get-listener input>> [ set-editor-string ] keep
305 : listener-run-files ( seq -- )
307 '[ _ [ run-file ] each ]
312 : com-end ( listener -- )
313 input>> interactor-eof ;
315 : clear-output ( listener -- )
316 output>> pane-clear ;
318 \ clear-output H{ { +listener+ t } } define-command
320 : clear-stack ( listener -- )
321 [ [ clear ] \ clear ] dip (call-listener) ;
323 : use-if-necessary ( word manifest -- )
324 2dup [ vocabulary>> ] dip and [
326 [ vocabulary>> use-vocab ]
327 [ dup name>> associate use-words ] bi
331 M: word accept-completion-hook
332 interactor>> manifest>> use-if-necessary ;
334 M: object accept-completion-hook 2drop ;
336 : quot-action ( interactor -- lines )
337 [ history>> history-add drop ] [ control-value ] [ select-all ] tri
338 parse-lines-interactive ;
340 : do-recall? ( table error -- ? )
341 [ selection>> value>> not ] [ lexer-error? ] bi* and ;
343 : recall-lexer-error ( interactor error -- )
344 over recall-previous go-to-error ;
346 : make-restart-hook-quot ( error interactor -- quot )
349 _ do-recall? [ _ _ recall-lexer-error ] when
352 : frame-debugger ( debugger -- labeled )
353 "Error" debugger-color <framed-labeled> ;
355 :: <debugger-popup> ( error continuation interactor -- popup )
358 error compute-restarts
359 error interactor make-restart-hook-quot
360 <debugger> frame-debugger ;
362 : debugger-popup ( interactor error continuation -- )
363 pick <debugger-popup> one-line-elt swap show-listener-popup ;
365 : try-parse ( lines -- quot/f )
366 [ parse-lines-interactive ] [ nip '[ _ rethrow ] ] recover ;
368 M: interactor stream-read-quot ( stream -- quot/f )
369 dup interactor-yield dup array? [
370 over interactor-finish try-parse
371 [ nip ] [ stream-read-quot ] if*
374 : interactor-operation ( gesture interactor -- ? )
375 [ token-model>> value>> ] keep word-at-caret
376 [ nip ] [ gesture>operation ] 2bi
377 [ invoke-command f ] [ drop t ] if* ;
379 M: interactor handle-gesture
381 { [ over key-gesture? not ] [ call-next-method ] }
382 { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
384 [ dup token-model>> value>> ]
385 [ { [ interactor-operation ] [ call-next-method ] } 2&& ]
390 interactor "interactor" f {
391 { T{ key-down f f "RET" } evaluate-input }
392 { T{ key-down f { C+ } "k" } clear-editor }
395 interactor "completion" f {
396 { T{ key-down f f "TAB" } code-completion-popup }
397 { T{ key-down f { C+ } "p" } recall-previous }
398 { T{ key-down f { C+ } "n" } recall-next }
399 { T{ key-down f { C+ } "r" } history-completion-popup }
402 : introduction. ( -- )
404 H{ { font-size $ default-font-size } } [
405 { $tip-of-the-day } print-element nl
406 { $strong "Press " { $snippet "F1" } " at any time for help." } print-element nl
407 version-info print-element
409 ] with-default-style nl nl ;
411 : listener-thread ( listener -- )
412 dup listener-streams [
413 [ com-browse ] help-hook set
414 '[ [ _ input>> ] 2dip debugger-popup ] error-hook set
419 "The listener has exited. To start it again, click “Restart Listener”." print
420 ] with-input-output+error-streams* ;
422 : start-listener-thread ( listener -- )
425 [ input>> register-self ]
428 ] "Listener" spawn drop ;
430 : restart-listener ( listener -- )
431 ! Returns when listener is ready to receive input.
435 [ input>> clear-editor ]
436 [ start-listener-thread ]
437 [ wait-for-listener ]
440 : com-help ( -- ) "help.home" com-browse ;
442 \ com-help H{ { +nullary+ t } } define-command
444 : com-auto-use ( -- )
447 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
449 listener-gadget "toolbar" f {
450 { f restart-listener }
451 { T{ key-down f { A+ } "u" } com-auto-use }
452 { T{ key-down f { A+ } "k" } clear-output }
453 { T{ key-down f { A+ } "K" } clear-stack }
454 { T{ key-down f { C+ } "d" } com-end }
455 { T{ key-down f f "F1" } com-help }
458 listener-gadget "scrolling"
459 "The listener's scroller can be scrolled from the keyboard."
461 { T{ key-down f { A+ } "UP" } com-scroll-up }
462 { T{ key-down f { A+ } "DOWN" } com-scroll-down }
463 { T{ key-down f { A+ } "PAGE_UP" } com-page-up }
464 { T{ key-down f { A+ } "PAGE_DOWN" } com-page-down }
467 listener-gadget "multi-touch" f {
468 { up-action refresh-all }
471 M: listener-gadget graft*
472 [ call-next-method ] [ restart-listener ] bi ;
474 M: listener-gadget ungraft*
475 [ com-end ] [ call-next-method ] bi ;
479 :: make-font-style ( family size -- assoc )
481 family font-name pick set-at
482 size font-size pick set-at ;
486 :: set-listener-font ( family size -- )
487 get-listener input>> :> inter
488 family size make-font-style
489 inter output>> make-span-stream :> ostream
490 ostream inter output<<
495 ostream output-stream set ;