]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/tools/listener/listener.factor
Merge branch 'master' into irc
[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 IN: ui.tools.listener
20
21 ! If waiting is t, we're waiting for user input, and invoking
22 ! evaluate-input resumes the thread.
23 TUPLE: interactor < source-editor
24 output history flag mailbox thread waiting token-model word-model popup ;
25
26 : register-self ( interactor -- )
27     <mailbox> >>mailbox
28     self >>thread
29     drop ;
30
31 : interactor-continuation ( interactor -- continuation )
32     thread>> continuation>> value>> ;
33
34 : interactor-busy? ( interactor -- ? )
35     #! We're busy if there's no thread to resume.
36     {
37         [ waiting>> ]
38         [ thread>> dup [ thread-registered? ] when ]
39     } 1&& not ;
40
41 SLOT: vocabs
42
43 M: interactor vocabs>>
44     dup interactor-busy? [ drop f ] [
45         use swap
46         interactor-continuation name>>
47         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     vocabs>> assoc-stack ;
60
61 M: char-completion (word-at-caret)
62     2drop f ;
63
64 : word-at-caret ( token interactor -- obj )
65     completion-mode (word-at-caret) ;
66
67 : <word-model> ( interactor -- model )
68     [ token-model>> 1/3 seconds <delay> ]
69     [ '[ _ word-at-caret ] ] bi
70     <arrow> ;
71
72 : <interactor> ( -- gadget )
73     interactor new-editor
74         <flag> >>flag
75         dup one-word-elt <element-model> >>token-model
76         dup <word-model> >>word-model
77         dup model>> <history> >>history ;
78
79 M: interactor graft*
80     [ call-next-method ] [ dup word-model>> add-connection ] bi ;
81
82 M: interactor ungraft*
83     [ dup word-model>> remove-connection ] [ call-next-method ] bi ;
84
85 M: interactor model-changed
86     2dup word-model>> eq? [
87         dup popup>>
88         [ 2drop ] [ [ value>> ] dip show-summary ] if
89     ] [ call-next-method ] if ;
90
91 M: interactor stream-element-type drop +character+ ;
92
93 GENERIC: (print-input) ( object -- )
94
95 M: input (print-input)
96     dup presented associate
97     [ string>> H{ { font-style bold } } format ] with-nesting nl ;
98
99 M: word (print-input)
100     "Command: "
101     [
102         "sans-serif" font-name set
103         bold font-style set
104     ] H{ } make-assoc format . ;
105
106 : print-input ( object interactor -- )
107     output>> [ (print-input) ] with-output-stream* ;
108
109 : interactor-continue ( obj interactor -- )
110     mailbox>> mailbox-put ;
111
112 : interactor-finish ( interactor -- )
113     [ history>> history-add ] keep
114     [ print-input ]
115     [ clear-editor drop ]
116     [ model>> clear-undo drop ] 2tri ;
117
118 : interactor-eof ( interactor -- )
119     dup interactor-busy? [
120         f over interactor-continue
121     ] unless drop ;
122
123 : evaluate-input ( interactor -- )
124     dup interactor-busy? [ drop ] [
125         [ control-value ] keep interactor-continue
126     ] if ;
127
128 : interactor-yield ( interactor -- obj )
129     dup thread>> self eq? [
130         {
131             [ t >>waiting drop ]
132             [ flag>> raise-flag ]
133             [ mailbox>> mailbox-get ]
134             [ f >>waiting drop ]
135         } cleave
136     ] [ drop f ] if ;
137
138 : interactor-read ( interactor -- lines )
139     [ interactor-yield ] [ interactor-finish ] bi ;
140
141 M: interactor stream-readln
142     interactor-read dup [ first ] when ;
143
144 : (call-listener) ( quot command listener -- )
145     input>> dup interactor-busy? [ 3drop ] [
146         [ print-input drop ]
147         [ nip interactor-continue ]
148         3bi
149     ] if ;
150
151 M: interactor stream-read
152     swap dup zero? [
153         2drop ""
154     ] [
155         [ interactor-read dup [ "\n" join ] when ] dip short head
156     ] if ;
157
158 M: interactor stream-read-partial
159     stream-read ;
160
161 M: interactor stream-read1
162     dup interactor-read {
163         { [ dup not ] [ 2drop f ] }
164         { [ dup empty? ] [ drop stream-read1 ] }
165         { [ dup first empty? ] [ 2drop CHAR: \n ] }
166         [ nip first first ]
167     } cond ;
168
169 M: interactor dispose drop ;
170
171 : go-to-error ( interactor error -- )
172     [ line>> 1- ] [ column>> ] bi 2array
173     over set-caret
174     mark>caret ;
175
176 TUPLE: listener-gadget < tool error-summary output scroller input ;
177
178 { 600 700 } listener-gadget set-tool-dim
179
180 : find-listener ( gadget -- listener )
181     [ listener-gadget? ] find-parent ;
182
183 : listener-streams ( listener -- input output )
184     [ input>> ] [ output>> <pane-stream> ] bi ;
185
186 : init-input/output ( listener -- listener )
187     <interactor>
188     [ >>input ] [ pane new-pane t >>scrolls? >>output ] bi
189     dup listener-streams >>output drop ;
190
191 : error-summary. ( -- )
192     error-counts keys [
193         H{ { table-gap { 3 3 } } } [
194             [ [ [ icon>> write-image ] with-cell ] each ] with-row
195         ] tabular-output
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 seq -- )
304     2dup [ vocabulary>> ] dip and [
305         2dup [ assoc-stack ] keep = [ 2drop ] [
306             [ vocabulary>> vocab-words ] dip push
307         ] if
308     ] [ 2drop ] if ;
309
310 M: word accept-completion-hook
311     interactor>> vocabs>> use-if-necessary ;
312
313 M: object accept-completion-hook 2drop ;
314
315 : quot-action ( interactor -- lines )
316     [ history>> history-add drop ] [ control-value ] [ select-all ] tri
317     [ parse-lines ] with-compilation-unit ;
318
319 : <debugger-popup> ( error continuation -- popup )
320     over compute-restarts [ hide-glass ] <debugger> "Error" <labeled-gadget> ;
321
322 : debugger-popup ( interactor error continuation -- )
323     [ one-line-elt ] 2dip <debugger-popup> show-listener-popup ;
324
325 : handle-parse-error ( interactor error -- )
326     dup lexer-error? [ 2dup go-to-error error>> ] when
327     error-continuation get
328     debugger-popup ;
329
330 : try-parse ( lines interactor -- quot/error/f )
331     [ drop parse-lines-interactive ] [
332         2nip
333         dup lexer-error? [
334             dup error>> unexpected-eof? [ drop f ] when
335         ] when
336     ] recover ;
337
338 : handle-interactive ( lines interactor -- quot/f ? )
339     [ nip ] [ try-parse ] 2bi {
340         { [ dup quotation? ] [ nip t ] }
341         { [ dup not ] [ drop "\n" swap user-input* drop f f ] }
342         [ handle-parse-error f f ]
343     } cond ;
344
345 M: interactor stream-read-quot
346     [ interactor-yield ] keep {
347         { [ over not ] [ drop ] }
348         { [ over callable? ] [ drop ] }
349         [
350             [ handle-interactive ] keep swap
351             [ interactor-finish ] [ nip stream-read-quot ] if
352         ]
353     } cond ;
354
355 : interactor-operation ( gesture interactor -- ? )
356     [ token-model>> value>> ] keep word-at-caret
357     [ nip ] [ gesture>operation ] 2bi
358     dup [ invoke-command f ] [ 2drop t ] if ;
359
360 M: interactor handle-gesture
361     {
362         { [ over key-gesture? not ] [ call-next-method ] }
363         { [ dup popup>> ] [ { [ pass-to-popup ] [ call-next-method ] } 2&& ] }
364         { [ dup token-model>> value>> ] [ { [ interactor-operation ] [ call-next-method ] } 2&& ] }
365         [ call-next-method ]
366     } cond ;
367
368 interactor "interactor" f {
369     { T{ key-down f f "RET" } evaluate-input }
370     { T{ key-down f { C+ } "k" } clear-editor }
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 : listener-thread ( listener -- )
381     dup listener-streams [
382         [ com-browse ] help-hook set
383         '[ [ _ input>> ] 2dip debugger-popup ] error-hook set
384         error-summary? off
385         tip-of-the-day. nl
386         listener
387     ] with-streams* ;
388
389 : start-listener-thread ( listener -- )
390     '[
391         _
392         [ input>> register-self ]
393         [ listener-thread ]
394         bi
395     ] "Listener" spawn drop ;
396
397 : restart-listener ( listener -- )
398     #! Returns when listener is ready to receive input.
399     {
400         [ com-end ]
401         [ clear-output ]
402         [ input>> clear-editor ]
403         [ start-listener-thread ]
404         [ wait-for-listener ]
405     } cleave ;
406
407 : listener-help ( -- ) "help.home" com-browse ;
408
409 \ listener-help H{ { +nullary+ t } } define-command
410
411 : com-auto-use ( -- )
412     auto-use? [ not ] change ;
413
414 \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command
415
416 listener-gadget "misc" "Miscellaneous commands" {
417     { T{ key-down f f "F1" } listener-help }
418 } define-command-map
419
420 listener-gadget "toolbar" f {
421     { f restart-listener }
422     { T{ key-down f { A+ } "u" } com-auto-use }
423     { T{ key-down f { A+ } "k" } clear-output }
424     { T{ key-down f { A+ } "K" } clear-stack }
425     { T{ key-down f { C+ } "d" } com-end }
426 } define-command-map
427
428 listener-gadget "scrolling"
429 "The listener's scroller can be scrolled from the keyboard."
430 {
431     { T{ key-down f { A+ } "UP" } com-scroll-up }
432     { T{ key-down f { A+ } "DOWN" } com-scroll-down }
433     { T{ key-down f { A+ } "PAGE_UP" } com-page-up }
434     { T{ key-down f { A+ } "PAGE_DOWN" } com-page-down }
435 } define-command-map
436
437 listener-gadget "multi-touch" f {
438     { up-action refresh-all }
439 } define-command-map
440
441 M: listener-gadget graft*
442     [ call-next-method ] [ restart-listener ] bi ;
443
444 M: listener-gadget ungraft*
445     [ com-end ] [ call-next-method ] bi ;