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 tools.vocabs vocabs 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.tools.error-list ;
17 FROM: source-files.errors => all-errors ;
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 : register-self ( interactor -- )
30 : interactor-continuation ( interactor -- continuation )
31 thread>> continuation>> value>> ;
33 : interactor-busy? ( interactor -- ? )
34 #! We're busy if there's no thread to resume.
37 [ thread>> dup [ thread-registered? ] when ]
42 M: interactor vocabs>>
43 dup interactor-busy? [ drop f ] [
45 interactor-continuation name>>
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)
58 vocabs>> assoc-stack ;
60 M: char-completion (word-at-caret)
63 : word-at-caret ( token interactor -- obj )
64 completion-mode (word-at-caret) ;
66 : <word-model> ( interactor -- model )
67 [ token-model>> 1/3 seconds <delay> ]
68 [ '[ _ word-at-caret ] ] bi
71 : <interactor> ( -- gadget )
74 dup one-word-elt <element-model> >>token-model
75 dup <word-model> >>word-model
76 dup model>> <history> >>history ;
79 [ call-next-method ] [ dup word-model>> add-connection ] bi ;
81 M: interactor ungraft*
82 [ dup word-model>> remove-connection ] [ call-next-method ] bi ;
84 M: interactor model-changed
85 2dup word-model>> eq? [
87 [ 2drop ] [ [ value>> ] dip show-summary ] if
88 ] [ call-next-method ] if ;
90 M: interactor stream-element-type drop +character+ ;
92 GENERIC: (print-input) ( object -- )
94 M: input (print-input)
95 dup presented associate
96 [ string>> H{ { font-style bold } } format ] with-nesting nl ;
101 "sans-serif" font-name set
103 ] H{ } make-assoc format . ;
105 : print-input ( object interactor -- )
106 output>> [ (print-input) ] with-output-stream* ;
108 : interactor-continue ( obj interactor -- )
109 mailbox>> mailbox-put ;
111 : interactor-finish ( interactor -- )
112 [ history>> history-add ] keep
114 [ clear-editor drop ]
115 [ model>> clear-undo drop ] 2tri ;
117 : interactor-eof ( interactor -- )
118 dup interactor-busy? [
119 f over interactor-continue
122 : evaluate-input ( interactor -- )
123 dup interactor-busy? [ drop ] [
124 [ control-value ] keep interactor-continue
127 : interactor-yield ( interactor -- obj )
128 dup thread>> self eq? [
131 [ flag>> raise-flag ]
132 [ mailbox>> mailbox-get ]
137 : interactor-read ( interactor -- lines )
138 [ interactor-yield ] [ interactor-finish ] bi ;
140 M: interactor stream-readln
141 interactor-read dup [ first ] when ;
143 : (call-listener) ( quot command listener -- )
144 input>> dup interactor-busy? [ 3drop ] [
146 [ nip interactor-continue ]
150 M: interactor stream-read
154 [ interactor-read dup [ "\n" join ] when ] dip short head
157 M: interactor stream-read-partial
160 M: interactor stream-read1
161 dup interactor-read {
162 { [ dup not ] [ 2drop f ] }
163 { [ dup empty? ] [ drop stream-read1 ] }
164 { [ dup first empty? ] [ 2drop CHAR: \n ] }
168 M: interactor dispose drop ;
170 : go-to-error ( interactor error -- )
171 [ line>> 1- ] [ column>> ] bi 2array
175 TUPLE: listener-gadget < tool error-summary output scroller input ;
177 { 600 700 } listener-gadget set-tool-dim
179 : find-listener ( gadget -- listener )
180 [ listener-gadget? ] find-parent ;
182 : listener-streams ( listener -- input output )
183 [ input>> ] [ output>> <pane-stream> ] bi ;
185 : init-input/output ( listener -- listener )
187 [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
188 dup listener-streams >>output drop ;
190 : init-error-summary ( listener -- listener )
191 <pane> >>error-summary
192 dup error-summary>> f track-add ;
194 : <listener-gadget> ( -- listener )
195 vertical listener-gadget new-track
198 dup output>> <scroller> >>scroller
199 dup scroller>> 1 track-add
202 M: listener-gadget focusable-child*
203 input>> dup popup>> or ;
205 : wait-for-listener ( listener -- )
206 #! Wait for the listener to start.
207 input>> flag>> wait-for-flag ;
209 : listener-busy? ( listener -- ? )
210 input>> interactor-busy? ;
212 : listener-window* ( -- listener )
214 dup "Listener" open-status-window ;
216 : listener-window ( -- )
217 [ listener-window* drop ] with-ui ;
219 \ listener-window H{ { +nullary+ t } } define-command
221 : (get-listener) ( quot -- listener )
227 [ input>> scroll>caret ]
228 [ input>> request-focus ] tri
230 ] [ listener-window* ] if* ; inline
232 : get-listener ( -- listener )
233 [ listener-gadget? ] (get-listener) ;
235 : show-listener ( -- )
238 \ show-listener H{ { +nullary+ t } } define-command
240 : get-ready-listener ( -- listener )
244 [ listener-busy? not ]
248 GENERIC: listener-input ( obj -- )
250 M: input listener-input string>> listener-input ;
252 M: string listener-input
254 [ set-editor-string ] [ request-focus ] bi ;
256 : call-listener ( quot command -- )
258 '[ _ _ _ dup wait-for-listener (call-listener) ]
259 "Listener call" spawn drop ;
261 M: listener-command invoke-command ( target command -- )
262 [ command-quot ] [ nip ] 2bi call-listener ;
264 M: listener-operation invoke-command ( target command -- )
265 [ operation-quot ] [ nip command>> ] 2bi call-listener ;
267 : eval-listener ( string -- )
268 get-listener input>> [ set-editor-string ] keep
271 : listener-run-files ( seq -- )
273 '[ _ [ run-file ] each ]
278 : com-end ( listener -- )
279 input>> interactor-eof ;
281 : clear-output ( listener -- )
282 output>> pane-clear ;
284 \ clear-output H{ { +listener+ t } } define-command
286 : clear-stack ( listener -- )
287 [ [ clear ] \ clear ] dip (call-listener) ;
289 : use-if-necessary ( word seq -- )
290 2dup [ vocabulary>> ] dip and [
291 2dup [ assoc-stack ] keep = [ 2drop ] [
292 [ vocabulary>> vocab-words ] dip push
296 M: word accept-completion-hook
297 interactor>> vocabs>> use-if-necessary ;
299 M: object accept-completion-hook 2drop ;
301 : quot-action ( interactor -- lines )
302 [ history>> history-add drop ] [ control-value ] [ select-all ] tri
303 [ parse-lines ] with-compilation-unit ;
305 : <debugger-popup> ( error continuation -- popup )
306 over compute-restarts [ hide-glass ] <debugger> "Error" <labeled-gadget> ;
308 : debugger-popup ( interactor error continuation -- )
309 [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
311 : handle-parse-error ( interactor error -- )
312 dup lexer-error? [ 2dup go-to-error error>> ] when
313 error-continuation get
316 : try-parse ( lines interactor -- quot/error/f )
317 [ drop parse-lines-interactive ] [
320 dup error>> unexpected-eof? [ drop f ] when
324 : handle-interactive ( lines interactor -- quot/f ? )
325 [ nip ] [ try-parse ] 2bi {
326 { [ dup quotation? ] [ nip t ] }
327 { [ dup not ] [ drop "\n" swap user-input* drop f f ] }
328 [ handle-parse-error f f ]
331 M: interactor stream-read-quot
332 [ interactor-yield ] keep {
333 { [ over not ] [ drop ] }
334 { [ over callable? ] [ drop ] }
336 [ handle-interactive ] keep swap
337 [ interactor-finish ] [ nip stream-read-quot ] if
341 : interactor-operation ( gesture interactor -- ? )
342 [ token-model>> value>> ] keep word-at-caret
343 [ nip ] [ gesture>operation ] 2bi
344 dup [ invoke-command f ] [ 2drop t ] if ;
346 M: interactor handle-gesture
348 { [ over key-gesture? not ] [ call-next-method ] }
349 { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
350 { [ dup token-model>> value>> ] [ { [ interactor-operation ] [ call-next-method ] } 2&& ] }
354 interactor "interactor" f {
355 { T{ key-down f f "RET" } evaluate-input }
356 { T{ key-down f { C+ } "k" } clear-editor }
359 interactor "completion" f {
360 { T{ key-down f f "TAB" } code-completion-popup }
361 { T{ key-down f { C+ } "p" } recall-previous }
362 { T{ key-down f { C+ } "n" } recall-next }
363 { T{ key-down f { C+ } "r" } history-completion-popup }
366 : ui-error-summary ( listener -- )
369 [ icon>> 1array \ $image prefix " " 2array ] { } map-as
370 { "Press " { $command tool "common" show-error-list } " to view errors." }
375 : listener-thread ( listener -- )
376 dup listener-streams [
377 [ com-browse ] help-hook set
378 [ '[ [ _ input>> ] 2dip debugger-popup ] error-hook set ]
379 [ '[ _ ui-error-summary ] error-summary-hook set ] bi
384 : start-listener-thread ( listener -- )
387 [ input>> register-self ]
390 ] "Listener" spawn drop ;
392 : restart-listener ( listener -- )
393 #! Returns when listener is ready to receive input.
397 [ input>> clear-editor ]
398 [ start-listener-thread ]
399 [ wait-for-listener ]
402 : listener-help ( -- ) "help.home" com-browse ;
404 \ listener-help H{ { +nullary+ t } } define-command
406 : com-auto-use ( -- )
407 auto-use? [ not ] change ;
409 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
411 listener-gadget "misc" "Miscellaneous commands" {
412 { T{ key-down f f "F1" } listener-help }
415 listener-gadget "toolbar" f {
416 { f restart-listener }
417 { T{ key-down f { A+ } "u" } com-auto-use }
418 { T{ key-down f { A+ } "k" } clear-output }
419 { T{ key-down f { A+ } "K" } clear-stack }
420 { T{ key-down f { C+ } "d" } com-end }
423 listener-gadget "scrolling"
424 "The listener's scroller can be scrolled from the keyboard."
426 { T{ key-down f { A+ } "UP" } com-scroll-up }
427 { T{ key-down f { A+ } "DOWN" } com-scroll-down }
428 { T{ key-down f { A+ } "PAGE_UP" } com-page-up }
429 { T{ key-down f { A+ } "PAGE_DOWN" } com-page-down }
432 listener-gadget "multi-touch" f {
433 { up-action refresh-all }
436 M: listener-gadget graft*
437 [ call-next-method ] [ restart-listener ] bi ;
439 M: listener-gadget ungraft*
440 [ com-end ] [ call-next-method ] bi ;