]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/tools/listener/listener.factor
tweak error list sorting, listener now shows error list summary in a separate pane
[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 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 ;
18 IN: ui.tools.listener
19
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 ;
24
25 : register-self ( interactor -- )
26     <mailbox> >>mailbox
27     self >>thread
28     drop ;
29
30 : interactor-continuation ( interactor -- continuation )
31     thread>> continuation>> value>> ;
32
33 : interactor-busy? ( interactor -- ? )
34     #! We're busy if there's no thread to resume.
35     {
36         [ waiting>> ]
37         [ thread>> dup [ thread-registered? ] when ]
38     } 1&& not ;
39
40 SLOT: vocabs
41
42 M: interactor vocabs>>
43     dup interactor-busy? [ drop f ] [
44         use swap
45         interactor-continuation name>>
46         assoc-stack
47     ] if ;
48
49 : vocab-exists? ( name -- ? )
50     '[ _ { [ vocab ] [ find-vocab-root ] } 1|| ] [ drop f ] recover ;
51
52 GENERIC: (word-at-caret) ( token completion-mode -- obj )
53
54 M: vocab-completion (word-at-caret)
55     drop dup vocab-exists? [ >vocab-link ] [ drop f ] if ;
56
57 M: word-completion (word-at-caret)
58     vocabs>> assoc-stack ;
59
60 M: char-completion (word-at-caret)
61     2drop f ;
62
63 : word-at-caret ( token interactor -- obj )
64     completion-mode (word-at-caret) ;
65
66 : <word-model> ( interactor -- model )
67     [ token-model>> 1/3 seconds <delay> ]
68     [ '[ _ word-at-caret ] ] bi
69     <arrow> ;
70
71 : <interactor> ( -- gadget )
72     interactor new-editor
73         <flag> >>flag
74         dup one-word-elt <element-model> >>token-model
75         dup <word-model> >>word-model
76         dup model>> <history> >>history ;
77
78 M: interactor graft*
79     [ call-next-method ] [ dup word-model>> add-connection ] bi ;
80
81 M: interactor ungraft*
82     [ dup word-model>> remove-connection ] [ call-next-method ] bi ;
83
84 M: interactor model-changed
85     2dup word-model>> eq? [
86         dup popup>>
87         [ 2drop ] [ [ value>> ] dip show-summary ] if
88     ] [ call-next-method ] if ;
89
90 M: interactor stream-element-type drop +character+ ;
91
92 GENERIC: (print-input) ( object -- )
93
94 M: input (print-input)
95     dup presented associate
96     [ string>> H{ { font-style bold } } format ] with-nesting nl ;
97
98 M: word (print-input)
99     "Command: "
100     [
101         "sans-serif" font-name set
102         bold font-style set
103     ] H{ } make-assoc format . ;
104
105 : print-input ( object interactor -- )
106     output>> [ (print-input) ] with-output-stream* ;
107
108 : interactor-continue ( obj interactor -- )
109     mailbox>> mailbox-put ;
110
111 : interactor-finish ( interactor -- )
112     [ history>> history-add ] keep
113     [ print-input ]
114     [ clear-editor drop ]
115     [ model>> clear-undo drop ] 2tri ;
116
117 : interactor-eof ( interactor -- )
118     dup interactor-busy? [
119         f over interactor-continue
120     ] unless drop ;
121
122 : evaluate-input ( interactor -- )
123     dup interactor-busy? [ drop ] [
124         [ control-value ] keep interactor-continue
125     ] if ;
126
127 : interactor-yield ( interactor -- obj )
128     dup thread>> self eq? [
129         {
130             [ t >>waiting drop ]
131             [ flag>> raise-flag ]
132             [ mailbox>> mailbox-get ]
133             [ f >>waiting drop ]
134         } cleave
135     ] [ drop f ] if ;
136
137 : interactor-read ( interactor -- lines )
138     [ interactor-yield ] [ interactor-finish ] bi ;
139
140 M: interactor stream-readln
141     interactor-read dup [ first ] when ;
142
143 : (call-listener) ( quot command listener -- )
144     input>> dup interactor-busy? [ 3drop ] [
145         [ print-input drop ]
146         [ nip interactor-continue ]
147         3bi
148     ] if ;
149
150 M: interactor stream-read
151     swap dup zero? [
152         2drop ""
153     ] [
154         [ interactor-read dup [ "\n" join ] when ] dip short head
155     ] if ;
156
157 M: interactor stream-read-partial
158     stream-read ;
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 : init-error-summary ( listener -- listener )
191     <pane> >>error-summary
192     dup error-summary>> f track-add ;
193
194 : <listener-gadget> ( -- listener )
195     vertical listener-gadget new-track
196         add-toolbar
197         init-input/output
198         dup output>> <scroller> >>scroller
199         dup scroller>> 1 track-add
200         init-error-summary ;
201
202 M: listener-gadget focusable-child*
203     input>> dup popup>> or ;
204
205 : wait-for-listener ( listener -- )
206     #! Wait for the listener to start.
207     input>> flag>> wait-for-flag ;
208
209 : listener-busy? ( listener -- ? )
210     input>> interactor-busy? ;
211
212 : listener-window* ( -- listener )
213     <listener-gadget>
214     dup "Listener" open-status-window ;
215
216 : listener-window ( -- )
217     [ listener-window* drop ] with-ui ;
218
219 \ listener-window H{ { +nullary+ t } } define-command
220
221 : (get-listener) ( quot -- listener )
222     find-window [
223         [ raise-window ]
224         [
225             gadget-child
226             [ ]
227             [ input>> scroll>caret ]
228             [ input>> request-focus ] tri
229         ] bi
230     ] [ listener-window* ] if* ; inline
231
232 : get-listener ( -- listener )
233     [ listener-gadget? ] (get-listener) ;
234
235 : show-listener ( -- )
236     get-listener drop ;
237
238 \ show-listener H{ { +nullary+ t } } define-command
239
240 : get-ready-listener ( -- listener )
241     [
242         {
243             [ listener-gadget? ]
244             [ listener-busy? not ]
245         } 1&&
246     ] (get-listener) ;
247
248 GENERIC: listener-input ( obj -- )
249
250 M: input listener-input string>> listener-input ;
251
252 M: string listener-input
253     get-listener input>>
254     [ set-editor-string ] [ request-focus ] bi ;
255
256 : call-listener ( quot command -- )
257     get-ready-listener
258     '[ _ _ _ dup wait-for-listener (call-listener) ]
259     "Listener call" spawn drop ;
260
261 M: listener-command invoke-command ( target command -- )
262     [ command-quot ] [ nip ] 2bi call-listener ;
263
264 M: listener-operation invoke-command ( target command -- )
265     [ operation-quot ] [ nip command>> ] 2bi call-listener ;
266
267 : eval-listener ( string -- )
268     get-listener input>> [ set-editor-string ] keep
269     evaluate-input ;
270
271 : listener-run-files ( seq -- )
272     [
273         '[ _ [ run-file ] each ]
274         \ listener-run-files
275         call-listener
276     ] unless-empty ;
277
278 : com-end ( listener -- )
279     input>> interactor-eof ;
280
281 : clear-output ( listener -- )
282     output>> pane-clear ;
283
284 \ clear-output H{ { +listener+ t } } define-command
285
286 : clear-stack ( listener -- )
287     [ [ clear ] \ clear ] dip (call-listener) ;
288
289 : use-if-necessary ( word seq -- )
290     2dup [ vocabulary>> ] dip and [
291         2dup [ assoc-stack ] keep = [ 2drop ] [
292             [ vocabulary>> vocab-words ] dip push
293         ] if
294     ] [ 2drop ] if ;
295
296 M: word accept-completion-hook
297     interactor>> vocabs>> use-if-necessary ;
298
299 M: object accept-completion-hook 2drop ;
300
301 : quot-action ( interactor -- lines )
302     [ history>> history-add drop ] [ control-value ] [ select-all ] tri
303     [ parse-lines ] with-compilation-unit ;
304
305 : <debugger-popup> ( error continuation -- popup )
306     over compute-restarts [ hide-glass ] <debugger> "Error" <labeled-gadget> ;
307
308 : debugger-popup ( interactor error continuation -- )
309     [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
310
311 : handle-parse-error ( interactor error -- )
312     dup lexer-error? [ 2dup go-to-error error>> ] when
313     error-continuation get
314     debugger-popup ;
315
316 : try-parse ( lines interactor -- quot/error/f )
317     [ drop parse-lines-interactive ] [
318         2nip
319         dup lexer-error? [
320             dup error>> unexpected-eof? [ drop f ] when
321         ] when
322     ] recover ;
323
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 ]
329     } cond ;
330
331 M: interactor stream-read-quot
332     [ interactor-yield ] keep {
333         { [ over not ] [ drop ] }
334         { [ over callable? ] [ drop ] }
335         [
336             [ handle-interactive ] keep swap
337             [ interactor-finish ] [ nip stream-read-quot ] if
338         ]
339     } cond ;
340
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 ;
345
346 M: interactor handle-gesture
347     {
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&& ] }
351         [ call-next-method ]
352     } cond ;
353
354 interactor "interactor" f {
355     { T{ key-down f f "RET" } evaluate-input }
356     { T{ key-down f { C+ } "k" } clear-editor }
357 } define-command-map
358
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 }
364 } define-command-map
365
366 : ui-error-summary ( listener -- )
367     error-summary>> [
368         error-counts keys [
369             [ icon>> 1array \ $image prefix " " 2array ] { } map-as
370             { "Press " { $command tool "common" show-error-list } " to view errors." }
371             append print-element
372         ] unless-empty
373     ] with-pane ;
374
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
380         tip-of-the-day. nl
381         listener
382     ] with-streams* ;
383
384 : start-listener-thread ( listener -- )
385     '[
386         _
387         [ input>> register-self ]
388         [ listener-thread ]
389         bi
390     ] "Listener" spawn drop ;
391
392 : restart-listener ( listener -- )
393     #! Returns when listener is ready to receive input.
394     {
395         [ com-end ]
396         [ clear-output ]
397         [ input>> clear-editor ]
398         [ start-listener-thread ]
399         [ wait-for-listener ]
400     } cleave ;
401
402 : listener-help ( -- ) "help.home" com-browse ;
403
404 \ listener-help H{ { +nullary+ t } } define-command
405
406 : com-auto-use ( -- )
407     auto-use? [ not ] change ;
408
409 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
410
411 listener-gadget "misc" "Miscellaneous commands" {
412     { T{ key-down f f "F1" } listener-help }
413 } define-command-map
414
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 }
421 } define-command-map
422
423 listener-gadget "scrolling"
424 "The listener's scroller can be scrolled from the keyboard."
425 {
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 }
430 } define-command-map
431
432 listener-gadget "multi-touch" f {
433     { up-action refresh-all }
434 } define-command-map
435
436 M: listener-gadget graft*
437     [ call-next-method ] [ restart-listener ] bi ;
438
439 M: listener-gadget ungraft*
440     [ com-end ] [ call-next-method ] bi ;