]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/tools/listener/listener.factor
ui.tools.listener: set error stream
[factor.git] / basis / ui / tools / listener / listener.factor
1 ! Copyright (C) 2005, 2010 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>> thread-continuation ;
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-unsafe ( n buf interactor -- count )
154     n [ 0 ] [
155         drop
156         interactor interactor-read dup [ "\n" join ] when
157         n short [ head-slice 0 buf copy ] keep
158     ] if-zero ;
159
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 ] }
165         [ nip first first ]
166     } cond ;
167
168 M: interactor dispose drop ;
169
170 : go-to-error ( interactor error -- )
171     [ line>> 1 - ] [ column>> ] bi 2array
172     over set-caret
173     mark>caret ;
174
175 TUPLE: listener-gadget < tool error-summary output scroller input ;
176
177 { 600 700 } listener-gadget set-tool-dim
178
179 : find-listener ( gadget -- listener )
180     [ listener-gadget? ] find-parent ;
181
182 : listener-streams ( listener -- input output )
183     [ input>> ] [ output>> <pane-stream> ] bi ;
184
185 : init-input/output ( listener -- listener )
186     <interactor>
187     [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
188     dup listener-streams >>output drop ;
189
190 : error-summary. ( -- )
191     error-counts keys [
192         H{ { table-gap { 3 3 } } } [
193             [ [ [ icon>> write-image ] with-cell ] each ] with-row
194         ] tabular-output
195         last-element off
196         { "Press " { $command tool "common" show-error-list } " to view errors." }
197         print-element
198     ] unless-empty ;
199
200 : <error-summary> ( -- gadget )
201     error-list-model get [ drop error-summary. ] <pane-control>
202         COLOR: light-yellow <solid> >>interior ;
203
204 : init-error-summary ( listener -- listener )
205     <error-summary> >>error-summary
206     dup error-summary>> f track-add ;
207
208 : <listener-gadget> ( -- listener )
209     vertical listener-gadget new-track
210         add-toolbar
211         init-input/output
212         dup output>> <scroller> >>scroller
213         dup scroller>> 1 track-add
214         init-error-summary ;
215
216 M: listener-gadget focusable-child*
217     input>> dup popup>> or ;
218
219 : wait-for-listener ( listener -- )
220     #! Wait for the listener to start.
221     input>> flag>> wait-for-flag ;
222
223 : listener-busy? ( listener -- ? )
224     input>> interactor-busy? ;
225
226 : listener-window* ( -- listener )
227     <listener-gadget>
228     dup "Listener" open-status-window ;
229
230 : listener-window ( -- )
231     [ listener-window* drop ] with-ui ;
232
233 \ listener-window H{ { +nullary+ t } } define-command
234
235 : (get-listener) ( quot -- listener )
236     find-window [
237         [ raise-window ]
238         [
239             gadget-child
240             [ ]
241             [ input>> scroll>caret ]
242             [ input>> request-focus ] tri
243         ] bi
244     ] [ listener-window* ] if* ; inline
245
246 : get-listener ( -- listener )
247     [ listener-gadget? ] (get-listener) ;
248
249 : show-listener ( -- )
250     get-listener drop ;
251
252 \ show-listener H{ { +nullary+ t } } define-command
253
254 : get-ready-listener ( -- listener )
255     [
256         {
257             [ listener-gadget? ]
258             [ listener-busy? not ]
259         } 1&&
260     ] (get-listener) ;
261
262 GENERIC: listener-input ( obj -- )
263
264 M: input listener-input string>> listener-input ;
265
266 M: string listener-input
267     get-listener input>>
268     [ set-editor-string ] [ request-focus ] bi ;
269
270 : call-listener ( quot command -- )
271     get-ready-listener
272     '[ _ _ _ dup wait-for-listener (call-listener) ]
273     "Listener call" spawn drop ;
274
275 M: listener-command invoke-command ( target command -- )
276     [ command-quot ] [ nip ] 2bi call-listener ;
277
278 M: listener-operation invoke-command ( target command -- )
279     [ operation-quot ] [ nip command>> ] 2bi call-listener ;
280
281 : eval-listener ( string -- )
282     get-listener input>> [ set-editor-string ] keep
283     evaluate-input ;
284
285 : listener-run-files ( seq -- )
286     [
287         '[ _ [ run-file ] each ]
288         \ listener-run-files
289         call-listener
290     ] unless-empty ;
291
292 : com-end ( listener -- )
293     input>> interactor-eof ;
294
295 : clear-output ( listener -- )
296     output>> pane-clear ;
297
298 \ clear-output H{ { +listener+ t } } define-command
299
300 : clear-stack ( listener -- )
301     [ [ clear ] \ clear ] dip (call-listener) ;
302
303 : use-if-necessary ( word manifest -- )
304     2dup [ vocabulary>> ] dip and [
305         manifest [
306             [ vocabulary>> use-vocab ]
307             [ dup name>> associate use-words ] bi
308         ] with-variable
309     ] [ 2drop ] if ;
310
311 M: word accept-completion-hook
312     interactor>> manifest>> use-if-necessary ;
313
314 M: object accept-completion-hook 2drop ;
315
316 : quot-action ( interactor -- lines )
317     [ history>> history-add drop ] [ control-value ] [ select-all ] tri
318     parse-lines-interactive ;
319
320 : <debugger-popup> ( error continuation -- popup )
321     over compute-restarts [ hide-glass ] <debugger> "Error" <labeled-gadget> ;
322
323 : debugger-popup ( interactor error continuation -- )
324     [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
325
326 : handle-parse-error ( interactor error -- )
327     dup lexer-error? [ 2dup go-to-error error>> ] when
328     error-continuation get
329     debugger-popup ;
330
331 : try-parse ( lines interactor -- quot/error/f )
332     [ drop parse-lines-interactive ] [
333         2nip
334         dup lexer-error? [
335             dup error>> unexpected-eof? [ drop f ] when
336         ] when
337     ] recover ;
338
339 : handle-interactive ( lines interactor -- quot/f ? )
340     [ nip ] [ try-parse ] 2bi {
341         { [ dup quotation? ] [ nip t ] }
342         { [ dup not ] [ drop insert-newline f f ] }
343         [ handle-parse-error f f ]
344     } cond ;
345
346 M: interactor stream-read-quot
347     [ interactor-yield ] keep {
348         { [ over not ] [ drop ] }
349         { [ over callable? ] [ drop ] }
350         [
351             [ handle-interactive ] keep swap
352             [ interactor-finish ] [ nip stream-read-quot ] if
353         ]
354     } cond ;
355
356 : interactor-operation ( gesture interactor -- ? )
357     [ token-model>> value>> ] keep word-at-caret
358     [ nip ] [ gesture>operation ] 2bi
359     dup [ invoke-command f ] [ 2drop t ] if ;
360
361 M: interactor handle-gesture
362     {
363         { [ over key-gesture? not ] [ call-next-method ] }
364         { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
365         { [ dup token-model>> value>> ] [ { [ interactor-operation ] [ call-next-method ] } 2&& ] }
366         [ call-next-method ]
367     } cond ;
368
369 interactor "interactor" f {
370     { T{ key-down f f "RET" } evaluate-input }
371 } define-command-map
372
373 interactor "completion" f {
374     { T{ key-down f f "TAB" } code-completion-popup }
375     { T{ key-down f { C+ } "p" } recall-previous }
376     { T{ key-down f { C+ } "n" } recall-next }
377     { T{ key-down f { C+ } "r" } history-completion-popup }
378 } define-command-map
379
380 : introduction. ( -- )
381     tip-of-the-day. nl
382     { $strong "Press " { $snippet "F1" } " at any time for help." } print-content nl nl ;
383
384 : listener-thread ( listener -- )
385     dup listener-streams [
386         [ com-browse ] help-hook set
387         '[ [ _ input>> ] 2dip debugger-popup ] error-hook set
388         error-summary? off
389         introduction.
390         listener
391         nl
392         "The listener has exited. To start it again, click “Restart Listener”." print
393     ] with-input-output+error-streams* ;
394
395 : start-listener-thread ( listener -- )
396     '[
397         _
398         [ input>> register-self ]
399         [ listener-thread ]
400         bi
401     ] "Listener" spawn drop ;
402
403 : restart-listener ( listener -- )
404     #! Returns when listener is ready to receive input.
405     {
406         [ com-end ]
407         [ clear-output ]
408         [ input>> clear-editor ]
409         [ start-listener-thread ]
410         [ wait-for-listener ]
411     } cleave ;
412
413 : com-help ( -- ) "help.home" com-browse ;
414
415 \ com-help H{ { +nullary+ t } } define-command
416
417 : com-auto-use ( -- )
418     auto-use? [ not ] change ;
419
420 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
421
422 listener-gadget "toolbar" f {
423     { f restart-listener }
424     { T{ key-down f { A+ } "u" } com-auto-use }
425     { T{ key-down f { A+ } "k" } clear-output }
426     { T{ key-down f { A+ } "K" } clear-stack }
427     { T{ key-down f { C+ } "d" } com-end }
428     { T{ key-down f f "F1" } com-help }
429 } define-command-map
430
431 listener-gadget "scrolling"
432 "The listener's scroller can be scrolled from the keyboard."
433 {
434     { T{ key-down f { A+ } "UP" } com-scroll-up }
435     { T{ key-down f { A+ } "DOWN" } com-scroll-down }
436     { T{ key-down f { A+ } "PAGE_UP" } com-page-up }
437     { T{ key-down f { A+ } "PAGE_DOWN" } com-page-down }
438 } define-command-map
439
440 listener-gadget "multi-touch" f {
441     { up-action refresh-all }
442 } define-command-map
443
444 M: listener-gadget graft*
445     [ call-next-method ] [ restart-listener ] bi ;
446
447 M: listener-gadget ungraft*
448     [ com-end ] [ call-next-method ] bi ;