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