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 manifest>>
44 dup interactor-busy? [ drop f ] [
45 interactor-continuation name>>
46 manifest swap assoc-stack
49 : vocab-exists? ( name -- ? )
50 '[ _ { [ vocab ] [ find-vocab-root ] } 1|| ] [ drop f ] recover ;
52 GENERIC: (word-at-caret) ( token completion-mode -- obj )
54 M: vocab-completion (word-at-caret)
55 drop dup vocab-exists? [ >vocab-link ] [ drop f ] if ;
57 M: word-completion (word-at-caret)
59 '[ _ _ search-manifest ] [ drop f ] recover
62 M: char-completion (word-at-caret)
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 set
105 ] H{ } make-assoc 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
156 [ interactor-read dup [ "\n" join ] when ] dip short head
159 M: interactor stream-read-partial
162 M: interactor stream-read1
163 dup interactor-read {
164 { [ dup not ] [ 2drop f ] }
165 { [ dup empty? ] [ drop stream-read1 ] }
166 { [ dup first empty? ] [ 2drop CHAR: \n ] }
170 M: interactor dispose drop ;
172 : go-to-error ( interactor error -- )
173 [ line>> 1 - ] [ column>> ] bi 2array
177 TUPLE: listener-gadget < tool error-summary output scroller input ;
179 { 600 700 } listener-gadget set-tool-dim
181 : find-listener ( gadget -- listener )
182 [ listener-gadget? ] find-parent ;
184 : listener-streams ( listener -- input output )
185 [ input>> ] [ output>> <pane-stream> ] bi ;
187 : init-input/output ( listener -- listener )
189 [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
190 dup listener-streams >>output drop ;
192 : error-summary. ( -- )
194 H{ { table-gap { 3 3 } } } [
195 [ [ [ icon>> write-image ] with-cell ] each ] with-row
197 { "Press " { $command tool "common" show-error-list } " to view errors." }
201 : <error-summary> ( -- gadget )
202 error-list-model get [ drop error-summary. ] <pane-control>
203 COLOR: light-yellow <solid> >>interior ;
205 : init-error-summary ( listener -- listener )
206 <error-summary> >>error-summary
207 dup error-summary>> f track-add ;
209 : <listener-gadget> ( -- listener )
210 vertical listener-gadget new-track
213 dup output>> <scroller> >>scroller
214 dup scroller>> 1 track-add
217 M: listener-gadget focusable-child*
218 input>> dup popup>> or ;
220 : wait-for-listener ( listener -- )
221 #! Wait for the listener to start.
222 input>> flag>> wait-for-flag ;
224 : listener-busy? ( listener -- ? )
225 input>> interactor-busy? ;
227 : listener-window* ( -- listener )
229 dup "Listener" open-status-window ;
231 : listener-window ( -- )
232 [ listener-window* drop ] with-ui ;
234 \ listener-window H{ { +nullary+ t } } define-command
236 : (get-listener) ( quot -- listener )
242 [ input>> scroll>caret ]
243 [ input>> request-focus ] tri
245 ] [ listener-window* ] if* ; inline
247 : get-listener ( -- listener )
248 [ listener-gadget? ] (get-listener) ;
250 : show-listener ( -- )
253 \ show-listener H{ { +nullary+ t } } define-command
255 : get-ready-listener ( -- listener )
259 [ listener-busy? not ]
263 GENERIC: listener-input ( obj -- )
265 M: input listener-input string>> listener-input ;
267 M: string listener-input
269 [ set-editor-string ] [ request-focus ] bi ;
271 : call-listener ( quot command -- )
273 '[ _ _ _ dup wait-for-listener (call-listener) ]
274 "Listener call" spawn drop ;
276 M: listener-command invoke-command ( target command -- )
277 [ command-quot ] [ nip ] 2bi call-listener ;
279 M: listener-operation invoke-command ( target command -- )
280 [ operation-quot ] [ nip command>> ] 2bi call-listener ;
282 : eval-listener ( string -- )
283 get-listener input>> [ set-editor-string ] keep
286 : listener-run-files ( seq -- )
288 '[ _ [ run-file ] each ]
293 : com-end ( listener -- )
294 input>> interactor-eof ;
296 : clear-output ( listener -- )
297 output>> pane-clear ;
299 \ clear-output H{ { +listener+ t } } define-command
301 : clear-stack ( listener -- )
302 [ [ clear ] \ clear ] dip (call-listener) ;
304 : use-if-necessary ( word manifest -- )
305 2dup [ vocabulary>> ] dip and [
307 [ vocabulary>> use-vocab ]
308 [ dup name>> associate use-words ] bi
312 M: word accept-completion-hook
313 interactor>> manifest>> use-if-necessary ;
315 M: object accept-completion-hook 2drop ;
317 : quot-action ( interactor -- lines )
318 [ history>> history-add drop ] [ control-value ] [ select-all ] tri
319 [ parse-lines ] with-compilation-unit ;
321 : <debugger-popup> ( error continuation -- popup )
322 over compute-restarts [ hide-glass ] <debugger> "Error" <labeled-gadget> ;
324 : debugger-popup ( interactor error continuation -- )
325 [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
327 : handle-parse-error ( interactor error -- )
328 dup lexer-error? [ 2dup go-to-error error>> ] when
329 error-continuation get
332 : try-parse ( lines interactor -- quot/error/f )
333 [ drop parse-lines-interactive ] [
336 dup error>> unexpected-eof? [ drop f ] when
340 : handle-interactive ( lines interactor -- quot/f ? )
341 [ nip ] [ try-parse ] 2bi {
342 { [ dup quotation? ] [ nip t ] }
343 { [ dup not ] [ drop "\n" swap user-input* drop f f ] }
344 [ handle-parse-error f f ]
347 M: interactor stream-read-quot
348 [ interactor-yield ] keep {
349 { [ over not ] [ drop ] }
350 { [ over callable? ] [ drop ] }
352 [ handle-interactive ] keep swap
353 [ interactor-finish ] [ nip stream-read-quot ] if
357 : interactor-operation ( gesture interactor -- ? )
358 [ token-model>> value>> ] keep word-at-caret
359 [ nip ] [ gesture>operation ] 2bi
360 dup [ invoke-command f ] [ 2drop t ] if ;
362 M: interactor handle-gesture
364 { [ over key-gesture? not ] [ call-next-method ] }
365 { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
366 { [ dup token-model>> value>> ] [ { [ interactor-operation ] [ call-next-method ] } 2&& ] }
370 interactor "interactor" f {
371 { T{ key-down f f "RET" } evaluate-input }
372 { T{ key-down f { C+ } "k" } clear-editor }
375 interactor "completion" f {
376 { T{ key-down f f "TAB" } code-completion-popup }
377 { T{ key-down f { C+ } "p" } recall-previous }
378 { T{ key-down f { C+ } "n" } recall-next }
379 { T{ key-down f { C+ } "r" } history-completion-popup }
382 : listener-thread ( listener -- )
383 dup listener-streams [
384 [ com-browse ] help-hook set
385 '[ [ _ input>> ] 2dip debugger-popup ] error-hook set
390 "The listener has exited. To start it again, click “Restart Listener”." print
393 : start-listener-thread ( listener -- )
396 [ input>> register-self ]
399 ] "Listener" spawn drop ;
401 : restart-listener ( listener -- )
402 #! Returns when listener is ready to receive input.
406 [ input>> clear-editor ]
407 [ start-listener-thread ]
408 [ wait-for-listener ]
411 : com-help ( -- ) "help.home" com-browse ;
413 \ com-help H{ { +nullary+ t } } define-command
415 : com-auto-use ( -- )
416 auto-use? [ not ] change ;
418 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
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 }
426 { T{ key-down f f "F1" } com-help }
429 listener-gadget "scrolling"
430 "The listener's scroller can be scrolled from the keyboard."
432 { T{ key-down f { A+ } "UP" } com-scroll-up }
433 { T{ key-down f { A+ } "DOWN" } com-scroll-down }
434 { T{ key-down f { A+ } "PAGE_UP" } com-page-up }
435 { T{ key-down f { A+ } "PAGE_DOWN" } com-page-down }
438 listener-gadget "multi-touch" f {
439 { up-action refresh-all }
442 M: listener-gadget graft*
443 [ call-next-method ] [ restart-listener ] bi ;
445 M: listener-gadget ungraft*
446 [ com-end ] [ call-next-method ] bi ;