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