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.
36 [ thread>> dup [ thread-registered? ] when ]
41 M: interactor vocabs>>
42 dup interactor-busy? [ drop f ] [
44 interactor-continuation name>>
48 : vocab-exists? ( name -- ? )
49 '[ _ { [ 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)
57 vocabs>> assoc-stack ;
59 M: char-completion (word-at-caret)
62 : word-at-caret ( token interactor -- obj )
63 completion-mode (word-at-caret) ;
65 : <word-model> ( interactor -- model )
66 [ token-model>> 1/3 seconds <delay> ]
67 [ '[ _ word-at-caret ] ] bi
70 : <interactor> ( -- gadget )
73 dup one-word-elt <element-model> >>token-model
74 dup <word-model> >>word-model
75 dup model>> <history> >>history ;
78 [ call-next-method ] [ dup word-model>> add-connection ] bi ;
80 M: interactor ungraft*
81 [ dup word-model>> remove-connection ] [ call-next-method ] bi ;
83 M: interactor model-changed
84 2dup word-model>> eq? [
86 [ 2drop ] [ [ value>> ] dip show-summary ] if
87 ] [ call-next-method ] if ;
89 M: interactor stream-element-type drop +character+ ;
91 GENERIC: (print-input) ( object -- )
93 M: input (print-input)
94 dup presented associate
95 [ string>> H{ { font-style bold } } format ] with-nesting nl ;
100 "sans-serif" font-name set
102 ] H{ } make-assoc format . ;
104 : print-input ( object interactor -- )
105 output>> [ (print-input) ] with-output-stream* ;
107 : interactor-continue ( obj interactor -- )
108 mailbox>> mailbox-put ;
110 : interactor-finish ( interactor -- )
111 [ history>> history-add ] keep
113 [ clear-editor drop ]
114 [ model>> clear-undo drop ] 2tri ;
116 : interactor-eof ( interactor -- )
117 dup interactor-busy? [
118 f over interactor-continue
121 : evaluate-input ( interactor -- )
122 dup interactor-busy? [ drop ] [
123 [ control-value ] keep interactor-continue
126 : interactor-yield ( interactor -- obj )
127 dup thread>> self eq? [
130 [ flag>> raise-flag ]
131 [ mailbox>> mailbox-get ]
136 : interactor-read ( interactor -- lines )
137 [ interactor-yield ] [ interactor-finish ] bi ;
139 M: interactor stream-readln
140 interactor-read dup [ first ] when ;
142 : (call-listener) ( quot command listener -- )
143 input>> dup interactor-busy? [ 3drop ] [
145 [ nip interactor-continue ]
149 M: interactor stream-read
153 [ interactor-read dup [ "\n" join ] when ] dip short head
156 M: interactor stream-read-partial
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 dispose drop ;
169 : go-to-error ( interactor error -- )
170 [ line>> 1- ] [ column>> ] bi 2array
174 TUPLE: listener-gadget < tool input output scroller ;
176 { 600 700 } listener-gadget set-tool-dim
178 : find-listener ( gadget -- listener )
179 [ listener-gadget? ] find-parent ;
181 : listener-streams ( listener -- input output )
182 [ input>> ] [ output>> <pane-stream> ] bi ;
184 : init-listener ( listener -- listener )
186 [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
187 dup listener-streams >>output drop ;
189 : <listener-gadget> ( -- gadget )
190 vertical listener-gadget new-track
193 dup output>> <scroller> >>scroller
194 dup scroller>> 1 track-add ;
196 M: listener-gadget focusable-child*
197 input>> dup popup>> or ;
199 : wait-for-listener ( listener -- )
200 #! Wait for the listener to start.
201 input>> flag>> wait-for-flag ;
203 : listener-busy? ( listener -- ? )
204 input>> interactor-busy? ;
206 : listener-window* ( -- listener )
208 dup "Listener" open-status-window ;
210 : listener-window ( -- )
211 [ listener-window* drop ] with-ui ;
213 \ listener-window H{ { +nullary+ t } } define-command
215 : (get-listener) ( quot -- listener )
221 [ input>> scroll>caret ]
222 [ input>> request-focus ] tri
224 ] [ listener-window* ] if* ; inline
226 : get-listener ( -- listener )
227 [ listener-gadget? ] (get-listener) ;
229 : show-listener ( -- )
232 \ show-listener H{ { +nullary+ t } } define-command
234 : get-ready-listener ( -- listener )
238 [ listener-busy? not ]
242 GENERIC: listener-input ( obj -- )
244 M: input listener-input string>> listener-input ;
246 M: string listener-input
248 [ set-editor-string ] [ request-focus ] bi ;
250 : call-listener ( quot command -- )
252 '[ _ _ _ dup wait-for-listener (call-listener) ]
253 "Listener call" spawn drop ;
255 M: listener-command invoke-command ( target command -- )
256 [ command-quot ] [ nip ] 2bi call-listener ;
258 M: listener-operation invoke-command ( target command -- )
259 [ operation-quot ] [ nip command>> ] 2bi call-listener ;
261 : eval-listener ( string -- )
262 get-listener input>> [ set-editor-string ] keep
265 : listener-run-files ( seq -- )
267 '[ _ [ run-file ] each ]
272 : com-end ( listener -- )
273 input>> interactor-eof ;
275 : clear-output ( listener -- )
276 output>> pane-clear ;
278 \ clear-output H{ { +listener+ t } } define-command
280 : clear-stack ( listener -- )
281 [ [ clear ] \ clear ] dip (call-listener) ;
283 : use-if-necessary ( word seq -- )
284 2dup [ vocabulary>> ] dip and [
285 2dup [ assoc-stack ] keep = [ 2drop ] [
286 [ vocabulary>> vocab-words ] dip push
290 M: word accept-completion-hook
291 interactor>> vocabs>> use-if-necessary ;
293 M: object accept-completion-hook 2drop ;
295 : quot-action ( interactor -- lines )
296 [ history>> history-add drop ] [ control-value ] [ select-all ] tri
297 [ parse-lines ] with-compilation-unit ;
299 : <debugger-popup> ( error continuation -- popup )
300 over compute-restarts [ hide-glass ] <debugger> "Error" <labeled-gadget> ;
302 : debugger-popup ( interactor error continuation -- )
303 [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
305 : handle-parse-error ( interactor error -- )
306 dup lexer-error? [ 2dup go-to-error error>> ] when
307 error-continuation get
310 : try-parse ( lines interactor -- quot/error/f )
311 [ drop parse-lines-interactive ] [
314 dup error>> unexpected-eof? [ drop f ] when
318 : handle-interactive ( lines interactor -- quot/f ? )
319 [ nip ] [ try-parse ] 2bi {
320 { [ dup quotation? ] [ nip t ] }
321 { [ dup not ] [ drop "\n" swap user-input* drop f f ] }
322 [ handle-parse-error f f ]
325 M: interactor stream-read-quot
326 [ interactor-yield ] keep {
327 { [ over not ] [ drop ] }
328 { [ over callable? ] [ drop ] }
330 [ handle-interactive ] keep swap
331 [ interactor-finish ] [ nip stream-read-quot ] if
335 : interactor-operation ( gesture interactor -- ? )
336 [ token-model>> value>> ] keep word-at-caret
337 [ nip ] [ gesture>operation ] 2bi
338 dup [ invoke-command f ] [ 2drop t ] if ;
340 M: interactor handle-gesture
342 { [ over key-gesture? not ] [ call-next-method ] }
343 { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
344 { [ dup token-model>> value>> ] [ { [ interactor-operation ] [ call-next-method ] } 2&& ] }
348 interactor "interactor" f {
349 { T{ key-down f f "RET" } evaluate-input }
350 { T{ key-down f { C+ } "k" } clear-editor }
353 interactor "completion" f {
354 { T{ key-down f f "TAB" } code-completion-popup }
355 { T{ key-down f { C+ } "p" } recall-previous }
356 { T{ key-down f { C+ } "n" } recall-next }
357 { T{ key-down f { C+ } "r" } history-completion-popup }
360 : ui-error-summary ( -- )
362 [ error-type ] map prune
363 [ error-icon-path 1array \ $image prefix " " 2array ] { } map-as
364 { "Press " { $command tool "common" show-error-list } " to view errors." }
365 append print-element nl
368 : listener-thread ( listener -- )
369 dup listener-streams [
370 [ com-browse ] help-hook set
371 '[ [ _ input>> ] 2dip debugger-popup ] error-hook set
372 [ ui-error-summary ] error-summary-hook set
377 : start-listener-thread ( listener -- )
380 [ input>> register-self ]
383 ] "Listener" spawn drop ;
385 : restart-listener ( listener -- )
386 #! Returns when listener is ready to receive input.
390 [ input>> clear-editor ]
391 [ start-listener-thread ]
392 [ wait-for-listener ]
395 : listener-help ( -- ) "help.home" com-browse ;
397 \ listener-help H{ { +nullary+ t } } define-command
399 : com-auto-use ( -- )
400 auto-use? [ not ] change ;
402 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
404 listener-gadget "misc" "Miscellaneous commands" {
405 { T{ key-down f f "F1" } listener-help }
408 listener-gadget "toolbar" f {
409 { f restart-listener }
410 { T{ key-down f { A+ } "u" } com-auto-use }
411 { T{ key-down f { A+ } "k" } clear-output }
412 { T{ key-down f { A+ } "K" } clear-stack }
413 { T{ key-down f { C+ } "d" } com-end }
416 listener-gadget "scrolling"
417 "The listener's scroller can be scrolled from the keyboard."
419 { T{ key-down f { A+ } "UP" } com-scroll-up }
420 { T{ key-down f { A+ } "DOWN" } com-scroll-down }
421 { T{ key-down f { A+ } "PAGE_UP" } com-page-up }
422 { T{ key-down f { A+ } "PAGE_DOWN" } com-page-down }
425 listener-gadget "multi-touch" f {
426 { up-action refresh-all }
429 M: listener-gadget graft*
430 [ call-next-method ] [ restart-listener ] bi ;
432 M: listener-gadget ungraft*
433 [ com-end ] [ call-next-method ] bi ;