]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/tools/listener/listener.factor
Moving new-sets to sets
[factor.git] / basis / ui / tools / listener / listener.factor
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
17 tools.errors.model ;
18 FROM: source-files.errors => all-errors ;
19 FROM: namespaces => set ;
20 IN: ui.tools.listener
21
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 ;
26
27 : register-self ( interactor -- )
28     <mailbox> >>mailbox
29     self >>thread
30     drop ;
31
32 : interactor-continuation ( interactor -- continuation )
33     thread>> continuation>> value>> ;
34
35 : interactor-busy? ( interactor -- ? )
36     #! We're busy if there's no thread to resume.
37     {
38         [ waiting>> ]
39         [ thread>> dup [ thread-registered? ] when ]
40     } 1&& not ;
41
42 SLOT: manifest
43
44 M: interactor manifest>>
45     dup interactor-busy? [ drop f ] [
46         interactor-continuation name>>
47         manifest swap assoc-stack
48     ] if ;
49
50 : vocab-exists? ( name -- ? )
51     '[ _ { [ vocab ] [ find-vocab-root ] } 1|| ] [ drop f ] recover ;
52
53 GENERIC: (word-at-caret) ( token completion-mode -- obj )
54
55 M: vocab-completion (word-at-caret)
56     drop dup vocab-exists? [ >vocab-link ] [ drop f ] if ;
57
58 M: word-completion (word-at-caret)
59     manifest>> dup [
60         '[ _ _ search-manifest ] [ drop f ] recover
61     ] [ 2drop f ] if ;
62
63 M: char-completion (word-at-caret)
64     2drop f ;
65
66 : word-at-caret ( token interactor -- obj )
67     completion-mode (word-at-caret) ;
68
69 : <word-model> ( interactor -- model )
70     [ token-model>> 1/3 seconds <delay> ]
71     [ '[ _ word-at-caret ] ] bi
72     <arrow> ;
73
74 : <interactor> ( -- gadget )
75     interactor new-editor
76         <flag> >>flag
77         dup one-word-elt <element-model> >>token-model
78         dup <word-model> >>word-model
79         dup model>> <history> >>history ;
80
81 M: interactor graft*
82     [ call-next-method ] [ dup word-model>> add-connection ] bi ;
83
84 M: interactor ungraft*
85     [ dup word-model>> remove-connection ] [ call-next-method ] bi ;
86
87 M: interactor model-changed
88     2dup word-model>> eq? [
89         dup popup>>
90         [ 2drop ] [ [ value>> ] dip show-summary ] if
91     ] [ call-next-method ] if ;
92
93 M: interactor stream-element-type drop +character+ ;
94
95 GENERIC: (print-input) ( object -- )
96
97 M: input (print-input)
98     dup presented associate
99     [ string>> H{ { font-style bold } } format ] with-nesting nl ;
100
101 M: word (print-input)
102     "Command: "
103     [
104         "sans-serif" font-name set
105         bold font-style set
106     ] H{ } make-assoc format . ;
107
108 : print-input ( object interactor -- )
109     output>> [ (print-input) ] with-output-stream* ;
110
111 : interactor-continue ( obj interactor -- )
112     mailbox>> mailbox-put ;
113
114 : interactor-finish ( interactor -- )
115     [ history>> history-add ] keep
116     [ print-input ]
117     [ clear-editor drop ]
118     [ model>> clear-undo drop ] 2tri ;
119
120 : interactor-eof ( interactor -- )
121     dup interactor-busy? [
122         f over interactor-continue
123     ] unless drop ;
124
125 : evaluate-input ( interactor -- )
126     dup interactor-busy? [ drop ] [
127         [ control-value ] keep interactor-continue
128     ] if ;
129
130 : interactor-yield ( interactor -- obj )
131     dup thread>> self eq? [
132         {
133             [ t >>waiting drop ]
134             [ flag>> raise-flag ]
135             [ mailbox>> mailbox-get ]
136             [ f >>waiting drop ]
137         } cleave
138     ] [ drop f ] if ;
139
140 : interactor-read ( interactor -- lines )
141     [ interactor-yield ] [ interactor-finish ] bi ;
142
143 M: interactor stream-readln
144     interactor-read dup [ first ] when ;
145
146 : (call-listener) ( quot command listener -- )
147     input>> dup interactor-busy? [ 3drop ] [
148         [ print-input drop ]
149         [ nip interactor-continue ]
150         3bi
151     ] if ;
152
153 M: interactor stream-read
154     swap [
155         drop ""
156     ] [
157         [ interactor-read dup [ "\n" join ] when ] dip short head
158     ] if-zero ;
159
160 M: interactor stream-read-partial
161     stream-read ;
162
163 M: interactor stream-read1
164     dup interactor-read {
165         { [ dup not ] [ 2drop f ] }
166         { [ dup empty? ] [ drop stream-read1 ] }
167         { [ dup first empty? ] [ 2drop CHAR: \n ] }
168         [ nip first first ]
169     } cond ;
170
171 M: interactor dispose drop ;
172
173 : go-to-error ( interactor error -- )
174     [ line>> 1 - ] [ column>> ] bi 2array
175     over set-caret
176     mark>caret ;
177
178 TUPLE: listener-gadget < tool error-summary output scroller input ;
179
180 { 600 700 } listener-gadget set-tool-dim
181
182 : find-listener ( gadget -- listener )
183     [ listener-gadget? ] find-parent ;
184
185 : listener-streams ( listener -- input output )
186     [ input>> ] [ output>> <pane-stream> ] bi ;
187
188 : init-input/output ( listener -- listener )
189     <interactor>
190     [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
191     dup listener-streams >>output drop ;
192
193 : error-summary. ( -- )
194     error-counts keys [
195         H{ { table-gap { 3 3 } } } [
196             [ [ [ icon>> write-image ] with-cell ] each ] with-row
197         ] tabular-output
198         { "Press " { $command tool "common" show-error-list } " to view errors." }
199         print-element
200     ] unless-empty ;
201
202 : <error-summary> ( -- gadget )
203     error-list-model get [ drop error-summary. ] <pane-control>
204         COLOR: light-yellow <solid> >>interior ;
205
206 : init-error-summary ( listener -- listener )
207     <error-summary> >>error-summary
208     dup error-summary>> f track-add ;
209
210 : <listener-gadget> ( -- listener )
211     vertical listener-gadget new-track
212         add-toolbar
213         init-input/output
214         dup output>> <scroller> >>scroller
215         dup scroller>> 1 track-add
216         init-error-summary ;
217
218 M: listener-gadget focusable-child*
219     input>> dup popup>> or ;
220
221 : wait-for-listener ( listener -- )
222     #! Wait for the listener to start.
223     input>> flag>> wait-for-flag ;
224
225 : listener-busy? ( listener -- ? )
226     input>> interactor-busy? ;
227
228 : listener-window* ( -- listener )
229     <listener-gadget>
230     dup "Listener" open-status-window ;
231
232 : listener-window ( -- )
233     [ listener-window* drop ] with-ui ;
234
235 \ listener-window H{ { +nullary+ t } } define-command
236
237 : (get-listener) ( quot -- listener )
238     find-window [
239         [ raise-window ]
240         [
241             gadget-child
242             [ ]
243             [ input>> scroll>caret ]
244             [ input>> request-focus ] tri
245         ] bi
246     ] [ listener-window* ] if* ; inline
247
248 : get-listener ( -- listener )
249     [ listener-gadget? ] (get-listener) ;
250
251 : show-listener ( -- )
252     get-listener drop ;
253
254 \ show-listener H{ { +nullary+ t } } define-command
255
256 : get-ready-listener ( -- listener )
257     [
258         {
259             [ listener-gadget? ]
260             [ listener-busy? not ]
261         } 1&&
262     ] (get-listener) ;
263
264 GENERIC: listener-input ( obj -- )
265
266 M: input listener-input string>> listener-input ;
267
268 M: string listener-input
269     get-listener input>>
270     [ set-editor-string ] [ request-focus ] bi ;
271
272 : call-listener ( quot command -- )
273     get-ready-listener
274     '[ _ _ _ dup wait-for-listener (call-listener) ]
275     "Listener call" spawn drop ;
276
277 M: listener-command invoke-command ( target command -- )
278     [ command-quot ] [ nip ] 2bi call-listener ;
279
280 M: listener-operation invoke-command ( target command -- )
281     [ operation-quot ] [ nip command>> ] 2bi call-listener ;
282
283 : eval-listener ( string -- )
284     get-listener input>> [ set-editor-string ] keep
285     evaluate-input ;
286
287 : listener-run-files ( seq -- )
288     [
289         '[ _ [ run-file ] each ]
290         \ listener-run-files
291         call-listener
292     ] unless-empty ;
293
294 : com-end ( listener -- )
295     input>> interactor-eof ;
296
297 : clear-output ( listener -- )
298     output>> pane-clear ;
299
300 \ clear-output H{ { +listener+ t } } define-command
301
302 : clear-stack ( listener -- )
303     [ [ clear ] \ clear ] dip (call-listener) ;
304
305 : use-if-necessary ( word manifest -- )
306     2dup [ vocabulary>> ] dip and [
307         manifest [
308             [ vocabulary>> use-vocab ]
309             [ dup name>> associate use-words ] bi
310         ] with-variable
311     ] [ 2drop ] if ;
312
313 M: word accept-completion-hook
314     interactor>> manifest>> use-if-necessary ;
315
316 M: object accept-completion-hook 2drop ;
317
318 : quot-action ( interactor -- lines )
319     [ history>> history-add drop ] [ control-value ] [ select-all ] tri
320     [ parse-lines ] with-compilation-unit ;
321
322 : <debugger-popup> ( error continuation -- popup )
323     over compute-restarts [ hide-glass ] <debugger> "Error" <labeled-gadget> ;
324
325 : debugger-popup ( interactor error continuation -- )
326     [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
327
328 : handle-parse-error ( interactor error -- )
329     dup lexer-error? [ 2dup go-to-error error>> ] when
330     error-continuation get
331     debugger-popup ;
332
333 : try-parse ( lines interactor -- quot/error/f )
334     [ drop parse-lines-interactive ] [
335         2nip
336         dup lexer-error? [
337             dup error>> unexpected-eof? [ drop f ] when
338         ] when
339     ] recover ;
340
341 : handle-interactive ( lines interactor -- quot/f ? )
342     [ nip ] [ try-parse ] 2bi {
343         { [ dup quotation? ] [ nip t ] }
344         { [ dup not ] [ drop "\n" swap user-input* drop f f ] }
345         [ handle-parse-error f f ]
346     } cond ;
347
348 M: interactor stream-read-quot
349     [ interactor-yield ] keep {
350         { [ over not ] [ drop ] }
351         { [ over callable? ] [ drop ] }
352         [
353             [ handle-interactive ] keep swap
354             [ interactor-finish ] [ nip stream-read-quot ] if
355         ]
356     } cond ;
357
358 : interactor-operation ( gesture interactor -- ? )
359     [ token-model>> value>> ] keep word-at-caret
360     [ nip ] [ gesture>operation ] 2bi
361     dup [ invoke-command f ] [ 2drop t ] if ;
362
363 M: interactor handle-gesture
364     {
365         { [ over key-gesture? not ] [ call-next-method ] }
366         { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
367         { [ dup token-model>> value>> ] [ { [ interactor-operation ] [ call-next-method ] } 2&& ] }
368         [ call-next-method ]
369     } cond ;
370
371 interactor "interactor" f {
372     { T{ key-down f f "RET" } evaluate-input }
373     { T{ key-down f { C+ } "k" } clear-editor }
374 } define-command-map
375
376 interactor "completion" f {
377     { T{ key-down f f "TAB" } code-completion-popup }
378     { T{ key-down f { C+ } "p" } recall-previous }
379     { T{ key-down f { C+ } "n" } recall-next }
380     { T{ key-down f { C+ } "r" } history-completion-popup }
381 } define-command-map
382
383 : introduction. ( -- )
384     tip-of-the-day. nl
385     { $strong "Press " { $snippet "F1" } " at any time for help." } print-content nl nl ;
386
387 : listener-thread ( listener -- )
388     dup listener-streams [
389         [ com-browse ] help-hook set
390         '[ [ _ input>> ] 2dip debugger-popup ] error-hook set
391         error-summary? off
392         introduction.
393         listener
394         nl
395         "The listener has exited. To start it again, click “Restart Listener”." print
396     ] with-streams* ;
397
398 : start-listener-thread ( listener -- )
399     '[
400         _
401         [ input>> register-self ]
402         [ listener-thread ]
403         bi
404     ] "Listener" spawn drop ;
405
406 : restart-listener ( listener -- )
407     #! Returns when listener is ready to receive input.
408     {
409         [ com-end ]
410         [ clear-output ]
411         [ input>> clear-editor ]
412         [ start-listener-thread ]
413         [ wait-for-listener ]
414     } cleave ;
415
416 : com-help ( -- ) "help.home" com-browse ;
417
418 \ com-help H{ { +nullary+ t } } define-command
419
420 : com-auto-use ( -- )
421     auto-use? [ not ] change ;
422
423 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
424
425 listener-gadget "toolbar" f {
426     { f restart-listener }
427     { T{ key-down f { A+ } "u" } com-auto-use }
428     { T{ key-down f { A+ } "k" } clear-output }
429     { T{ key-down f { A+ } "K" } clear-stack }
430     { T{ key-down f { C+ } "d" } com-end }
431     { T{ key-down f f "F1" } com-help }
432 } define-command-map
433
434 listener-gadget "scrolling"
435 "The listener's scroller can be scrolled from the keyboard."
436 {
437     { T{ key-down f { A+ } "UP" } com-scroll-up }
438     { T{ key-down f { A+ } "DOWN" } com-scroll-down }
439     { T{ key-down f { A+ } "PAGE_UP" } com-page-up }
440     { T{ key-down f { A+ } "PAGE_DOWN" } com-page-down }
441 } define-command-map
442
443 listener-gadget "multi-touch" f {
444     { up-action refresh-all }
445 } define-command-map
446
447 M: listener-gadget graft*
448     [ call-next-method ] [ restart-listener ] bi ;
449
450 M: listener-gadget ungraft*
451     [ com-end ] [ call-next-method ] bi ;