]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/editors/editors.factor
Move call( and execute( to core
[factor.git] / basis / ui / gadgets / editors / editors.factor
1 ! Copyright (C) 2006, 2009 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays documents documents.elements kernel math
4 math.ranges models models.arrow namespaces locals fry make opengl
5 opengl.gl sequences strings math.vectors math.functions sorting colors
6 colors.constants combinators assocs math.order fry calendar alarms
7 continuations ui.clipboards ui.commands ui.gadgets ui.gadgets.borders
8 ui.gadgets.buttons ui.gadgets.labels ui.gadgets.scrollers
9 ui.gadgets.menus ui.gadgets.wrappers ui.render ui.pens.solid
10 ui.gadgets.line-support ui.text ui.gestures ui.baseline-alignment
11 math.rectangles splitting unicode.categories fonts grouping ;
12 IN: ui.gadgets.editors
13
14 TUPLE: editor < line-gadget
15 caret-color
16 caret mark
17 focused? blink blink-alarm ;
18
19 : <loc> ( -- loc ) { 0 0 } <model> ;
20
21 : init-editor-locs ( editor -- editor )
22     <loc> >>caret
23     <loc> >>mark ; inline
24
25 : editor-theme ( editor -- editor )
26     COLOR: red >>caret-color
27     monospace-font >>font ; inline
28
29 : new-editor ( class -- editor )
30     new-line-gadget
31         <document> >>model
32         init-editor-locs
33         editor-theme ; inline
34
35 : <editor> ( -- editor )
36     editor new-editor ;
37
38 : activate-editor-model ( editor model -- )
39     2dup add-connection
40     dup activate-model
41     swap model>> add-loc ;
42
43 : deactivate-editor-model ( editor model -- )
44     2dup remove-connection
45     dup deactivate-model
46     swap model>> remove-loc ;
47
48 : blink-caret ( editor -- )
49     [ not ] change-blink relayout-1 ;
50
51 SYMBOL: blink-interval
52
53 750 milliseconds blink-interval set-global
54
55 : stop-blinking ( editor -- )
56     [ [ cancel-alarm ] when* f ] change-blink-alarm drop ;
57
58 : start-blinking ( editor -- )
59     [ stop-blinking ] [
60         t >>blink
61         dup '[ _ blink-caret ] blink-interval get every
62         >>blink-alarm drop
63     ] bi ;
64
65 : restart-blinking ( editor -- )
66     dup focused?>> [
67         [ start-blinking ]
68         [ relayout-1 ]
69         bi
70     ] [ drop ] if ;
71
72 M: editor graft*
73     dup
74     dup caret>> activate-editor-model
75     dup mark>> activate-editor-model ;
76
77 M: editor ungraft*
78     dup
79     dup stop-blinking
80     dup caret>> deactivate-editor-model
81     dup mark>> deactivate-editor-model ;
82
83 : editor-caret ( editor -- loc ) caret>> value>> ;
84
85 : editor-mark ( editor -- loc ) mark>> value>> ;
86
87 : set-caret ( loc editor -- )
88     [ model>> validate-loc ] keep
89     caret>> set-model ;
90
91 : change-caret ( editor quot -- )
92     [ [ [ editor-caret ] [ model>> ] bi ] dip call ] [ drop ] 2bi
93     set-caret ; inline
94
95 : mark>caret ( editor -- )
96     [ editor-caret ] [ mark>> ] bi set-model ;
97
98 : change-caret&mark ( editor quot -- )
99     [ change-caret ] [ drop mark>caret ] 2bi ; inline
100
101 : editor-line ( n editor -- str ) control-value nth ;
102
103 :: point>loc ( point editor -- loc )
104     point second editor y>line {
105         { [ dup 0 < ] [ drop { 0 0 } ] }
106         { [ dup editor model>> last-line# > ] [ drop editor model>> doc-end ] }
107         [| n |
108             n
109             point first
110             editor font>>
111             n editor editor-line
112             x>offset 2array
113         ]
114     } cond ;
115
116 : clicked-loc ( editor -- loc )
117     [ hand-rel ] keep point>loc ;
118
119 : click-loc ( editor model -- )
120     [ clicked-loc ] dip set-model ;
121
122 : focus-editor ( editor -- )
123     [ start-blinking ] [ t >>focused? relayout-1 ] bi ;
124
125 : unfocus-editor ( editor -- )
126     [ stop-blinking ] [ f >>focused? relayout-1 ] bi ;
127
128 : loc>x ( loc editor -- x )
129     [ first2 swap ] dip [ editor-line ] [ font>> ] bi swap offset>x round ;
130
131 : loc>point ( loc editor -- loc )
132     [ loc>x ] [ [ first ] dip line>y ceiling ] 2bi 2array ;
133
134 : caret-loc ( editor -- loc )
135     [ editor-caret ] keep loc>point ;
136
137 : caret-dim ( editor -- dim )
138     line-height 0 swap 2array ;
139
140 : scroll>caret ( editor -- )
141     dup graft-state>> second [
142         [
143             [ caret-loc ] [ caret-dim { 1 0 } v+ ] bi <rect>
144         ] keep scroll>rect
145     ] [ drop ] if ;
146
147 : draw-caret? ( editor -- ? )
148     [ focused?>> ] [ blink>> ] bi and ;
149
150 : draw-caret ( editor -- )
151     dup draw-caret? [
152         [ caret-color>> gl-color ]
153         [
154             [ caret-loc ] [ caret-dim ] bi
155             over v+ gl-line
156         ] bi
157     ] [ drop ] if ;
158
159 : selection-start/end ( editor -- start end )
160     [ editor-mark ] [ editor-caret ] bi sort-pair ;
161
162 SYMBOL: selected-lines
163
164 TUPLE: selected-line start end first? last? ;
165
166 : compute-selection ( editor -- assoc )
167     dup gadget-selection? [
168         [ selection-start/end [ [ first ] bi@ [a,b] ] 2keep ] keep model>>
169         '[ [ _ _ ] keep _ start/end-on-line 2array ] H{ } map>assoc
170     ] [ drop f ] if ;
171
172 :: draw-selection ( line pair editor -- )
173     pair [ editor font>> line offset>x ] map :> pair
174     pair first 0 2array [
175         editor selection-color>> gl-color
176         pair second pair first - round 1 max
177         editor line-height 2array gl-fill-rect
178     ] with-translation ;
179
180 : draw-unselected-line ( line editor -- )
181     font>> swap draw-text ;
182
183 : draw-selected-line ( line pair editor -- )
184     over all-equal? [
185         [ nip draw-unselected-line ] [ draw-selection ] 3bi
186     ] [
187         [ draw-selection ]
188         [
189             [ [ first2 ] [ selection-color>> ] bi* <selection> ] keep
190             draw-unselected-line
191         ] 3bi
192     ] if ;
193
194 M: editor draw-line ( line index editor -- )
195     [ selected-lines get at ] dip over
196     [ draw-selected-line ] [ nip draw-unselected-line ] if ;
197
198 M: editor draw-gadget*
199     dup compute-selection selected-lines [
200         [ draw-lines ] [ draw-caret ] bi
201     ] with-variable ;
202
203 M: editor pref-dim*
204     ! Add some space for the caret.
205     [ font>> ] [ control-value ] bi text-dim { 1 0 } v+ ;
206
207 M: editor baseline font>> font-metrics ascent>> ;
208
209 M: editor cap-height font>> font-metrics cap-height>> ;
210
211 : contents-changed ( model editor -- )
212     swap
213     over caret>> [ over validate-loc ] (change-model)
214     over mark>> [ over validate-loc ] (change-model)
215     drop relayout ;
216
217 : caret/mark-changed ( model editor -- )
218     nip [ restart-blinking ] [ scroll>caret ] bi ;
219
220 M: editor model-changed
221     {
222         { [ 2dup model>> eq? ] [ contents-changed ] }
223         { [ 2dup caret>> eq? ] [ caret/mark-changed ] }
224         { [ 2dup mark>> eq? ] [ caret/mark-changed ] }
225     } cond ;
226
227 M: editor gadget-selection?
228     selection-start/end = not ;
229
230 M: editor gadget-selection
231     [ selection-start/end ] keep model>> doc-range ;
232
233 : remove-selection ( editor -- )
234     [ selection-start/end ] keep model>> remove-doc-range ;
235
236 M: editor user-input*
237     [ selection-start/end ] keep model>> set-doc-range t ;
238
239 : editor-string ( editor -- string )
240     model>> doc-string ;
241
242 : set-editor-string ( string editor -- )
243     model>> set-doc-string ;
244
245 M: editor gadget-text* editor-string % ;
246
247 : extend-selection ( editor -- )
248     dup request-focus
249     dup restart-blinking
250     dup caret>> click-loc ;
251
252 : mouse-elt ( -- element )
253     hand-click# get {
254         { 1 one-char-elt }
255         { 2 one-word-elt }
256     } at one-line-elt or ;
257
258 : drag-direction? ( loc editor -- ? )
259     editor-mark before? ;
260
261 : drag-selection-caret ( loc editor element -- loc )
262     [
263         [ drag-direction? ] 2keep model>>
264     ] dip prev/next-elt ? ;
265
266 : drag-selection-mark ( loc editor element -- loc )
267     [
268         [ drag-direction? not ] keep
269         [ editor-mark ] [ model>> ] bi
270     ] dip prev/next-elt ? ;
271
272 : drag-caret&mark ( editor -- caret mark )
273     dup clicked-loc swap mouse-elt
274     [ drag-selection-caret ] 3keep
275     drag-selection-mark ;
276
277 : drag-selection ( editor -- )
278     dup drag-caret&mark
279     pick mark>> set-model
280     swap caret>> set-model ;
281
282 : editor-cut ( editor clipboard -- )
283     [ gadget-copy ] [ drop remove-selection ] 2bi ;
284
285 : delete/backspace ( editor quot -- )
286     over gadget-selection? [
287         drop remove-selection
288     ] [
289         [ [ [ editor-caret ] [ model>> ] bi ] dip call ]
290         [ drop model>> ]
291         2bi remove-doc-range
292     ] if ; inline
293
294 : editor-delete ( editor elt -- )
295     '[ dupd _ next-elt ] delete/backspace ;
296
297 : editor-backspace ( editor elt -- )
298     '[ over [ _ prev-elt ] dip ] delete/backspace ;
299
300 : editor-select-prev ( editor elt -- )
301     '[ _ prev-elt ] change-caret ;
302
303 : editor-prev ( editor elt -- )
304     [ editor-select-prev ] [ drop mark>caret ] 2bi ;
305
306 : editor-select-next ( editor elt -- )
307     '[ _ next-elt ] change-caret ;
308
309 : editor-next ( editor elt -- )
310     dupd editor-select-next mark>caret ;
311
312 : editor-select ( from to editor -- )
313     [ mark>> set-model ] [ caret>> set-model ] bi-curry bi* ;
314
315 : select-elt ( editor elt -- )
316     [ [ [ editor-caret ] [ model>> ] bi ] dip prev/next-elt ] [ drop ] 2bi
317     editor-select ;
318
319 : start-of-document ( editor -- ) doc-elt editor-prev ;
320
321 : end-of-document ( editor -- ) doc-elt editor-next ;
322
323 : position-caret ( editor -- )
324     mouse-elt dup one-char-elt =
325     [ drop dup extend-selection dup mark>> click-loc ]
326     [ select-elt ] if ;
327
328 : delete-next-character ( editor -- ) 
329     char-elt editor-delete ;
330
331 : delete-previous-character ( editor -- ) 
332     char-elt editor-backspace ;
333
334 : delete-previous-word ( editor -- ) 
335     word-elt editor-delete ;
336
337 : delete-next-word ( editor -- ) 
338     word-elt editor-backspace ;
339
340 : delete-to-start-of-line ( editor -- ) 
341     one-line-elt editor-delete ;
342
343 : delete-to-end-of-line ( editor -- ) 
344     one-line-elt editor-backspace ;
345
346 : com-undo ( editor -- )
347     model>> undo ;
348
349 : com-redo ( editor -- )
350     model>> redo ;
351
352 editor "editing" f {
353     { undo-action com-undo }
354     { redo-action com-redo }
355     { T{ key-down f f "DELETE" } delete-next-character }
356     { T{ key-down f { S+ } "DELETE" } delete-next-character }
357     { T{ key-down f f "BACKSPACE" } delete-previous-character }
358     { T{ key-down f { S+ } "BACKSPACE" } delete-previous-character }
359     { T{ key-down f { C+ } "DELETE" } delete-previous-word }
360     { T{ key-down f { C+ } "BACKSPACE" } delete-next-word }
361     { T{ key-down f { A+ } "DELETE" } delete-to-start-of-line }
362     { T{ key-down f { A+ } "BACKSPACE" } delete-to-end-of-line }
363 } define-command-map
364
365 : com-paste ( editor -- ) clipboard get paste-clipboard ;
366
367 : paste-selection ( editor -- ) selection get paste-clipboard ;
368
369 : com-cut ( editor -- ) clipboard get editor-cut ;
370
371 editor "clipboard" f {
372     { cut-action com-cut }
373     { copy-action com-copy }
374     { paste-action com-paste }
375     { T{ button-up } com-copy-selection }
376     { T{ button-up f f 2 } paste-selection }
377 } define-command-map
378
379 : previous-character ( editor -- )
380     dup gadget-selection? [
381         dup selection-start/end drop
382         over set-caret mark>caret
383     ] [
384         char-elt editor-prev
385     ] if ;
386
387 : next-character ( editor -- )
388     dup gadget-selection? [
389         dup selection-start/end nip
390         over set-caret mark>caret
391     ] [
392         char-elt editor-next
393     ] if ;
394
395 : previous-word ( editor -- ) word-elt editor-prev ;
396
397 : next-word ( editor -- ) word-elt editor-next ;
398
399 : start-of-line ( editor -- ) one-line-elt editor-prev ;
400
401 : end-of-line ( editor -- ) one-line-elt editor-next ;
402
403 editor "caret-motion" f {
404     { T{ button-down } position-caret }
405     { T{ key-down f f "LEFT" } previous-character }
406     { T{ key-down f f "RIGHT" } next-character }
407     { T{ key-down f { C+ } "LEFT" } previous-word }
408     { T{ key-down f { C+ } "RIGHT" } next-word }
409     { T{ key-down f f "HOME" } start-of-line }
410     { T{ key-down f f "END" } end-of-line }
411     { T{ key-down f { C+ } "HOME" } start-of-document }
412     { T{ key-down f { C+ } "END" } end-of-document }
413 } define-command-map
414
415 : clear-editor ( editor -- )
416     model>> clear-doc ;
417
418 : select-all ( editor -- ) doc-elt select-elt ;
419
420 : select-line ( editor -- ) one-line-elt select-elt ;
421
422 : select-word ( editor -- ) one-word-elt select-elt ;
423
424 : selected-token ( editor -- string )
425     dup gadget-selection?
426     [ dup select-word ] unless
427     gadget-selection ;
428
429 : select-previous-character ( editor -- ) 
430     char-elt editor-select-prev ;
431
432 : select-next-character ( editor -- ) 
433     char-elt editor-select-next ;
434
435 : select-previous-word ( editor -- ) 
436     word-elt editor-select-prev ;
437
438 : select-next-word ( editor -- ) 
439     word-elt editor-select-next ;
440
441 : select-start-of-line ( editor -- ) 
442     one-line-elt editor-select-prev ;
443
444 : select-end-of-line ( editor -- ) 
445     one-line-elt editor-select-next ;
446
447 : select-start-of-document ( editor -- ) 
448     doc-elt editor-select-prev ;
449
450 : select-end-of-document ( editor -- ) 
451     doc-elt editor-select-next ;
452
453 editor "selection" f {
454     { T{ button-down f { S+ } 1 } extend-selection }
455     { T{ drag } drag-selection }
456     { gain-focus focus-editor }
457     { lose-focus unfocus-editor }
458     { delete-action remove-selection }
459     { select-all-action select-all }
460     { T{ key-down f { C+ } "l" } select-line }
461     { T{ key-down f { S+ } "LEFT" } select-previous-character }
462     { T{ key-down f { S+ } "RIGHT" } select-next-character }
463     { T{ key-down f { S+ C+ } "LEFT" } select-previous-word }
464     { T{ key-down f { S+ C+ } "RIGHT" } select-next-word }
465     { T{ key-down f { S+ } "HOME" } select-start-of-line }
466     { T{ key-down f { S+ } "END" } select-end-of-line }
467     { T{ key-down f { S+ C+ } "HOME" } select-start-of-document }
468     { T{ key-down f { S+ C+ } "END" } select-end-of-document }
469 } define-command-map
470
471 : editor-menu ( editor -- )
472     {
473         com-undo
474         com-redo
475         ----
476         com-cut
477         com-copy
478         com-paste
479     } show-commands-menu ;
480
481 editor "misc" f {
482     { T{ button-down f f 3 } editor-menu }
483 } define-command-map
484
485 ! Multi-line editors
486 TUPLE: multiline-editor < editor ;
487
488 : <multiline-editor> ( -- editor )
489     multiline-editor new-editor ;
490
491 : previous-line ( editor -- ) line-elt editor-prev ;
492
493 : next-line ( editor -- ) line-elt editor-next ;
494
495 <PRIVATE
496
497 : page-elt ( editor -- editor element ) dup visible-lines 1- <page-elt> ;
498
499 PRIVATE>
500
501 : previous-page ( editor -- ) page-elt editor-prev ;
502
503 : next-page ( editor -- ) page-elt editor-next ;
504
505 : select-previous-line ( editor -- ) line-elt editor-select-prev ;
506
507 : select-next-line ( editor -- ) line-elt editor-select-next ;
508
509 : select-previous-page ( editor -- ) page-elt editor-select-prev ;
510
511 : select-next-page ( editor -- ) page-elt editor-select-next ;
512
513 : insert-newline ( editor -- )
514     "\n" swap user-input* drop ;
515
516 : change-selection ( editor quot -- )
517     '[ gadget-selection @ ] keep user-input* drop ; inline
518
519 : join-lines ( string -- string' )
520     "\n" split
521     [ rest-slice [ [ blank? ] trim-head-slice ] change-each ]
522     [ but-last-slice [ [ blank? ] trim-tail-slice ] change-each ]
523     [ " " join ]
524     tri ;
525
526 : this-line-and-next ( document line -- start end )
527     [ nip 0 swap 2array ]
528     [ [ nip 1+ ] [ 1+ swap doc-line length ] 2bi 2array ]
529     2bi ;
530
531 : last-line? ( document line -- ? )
532     [ last-line# ] dip = ;
533
534 : com-join-lines ( editor -- )
535     dup gadget-selection?
536     [ [ join-lines ] change-selection ] [
537         [ model>> ] [ editor-caret first ] bi
538         2dup last-line? [ 2drop ] [
539             [ this-line-and-next ] [ drop ] 2bi
540             [ join-lines ] change-doc-range
541         ] if
542     ] if ;
543
544 multiline-editor "multiline" f {
545     { T{ key-down f f "UP" } previous-line }
546     { T{ key-down f f "DOWN" } next-line }
547     { T{ key-down f { S+ } "UP" } select-previous-line }
548     { T{ key-down f { S+ } "DOWN" } select-next-line }
549     { T{ key-down f f "PAGE_UP" } previous-page }
550     { T{ key-down f f "PAGE_DOWN" } next-page }
551     { T{ key-down f { S+ } "PAGE_UP" } select-previous-page }
552     { T{ key-down f { S+ } "PAGE_DOWN" } select-next-page }
553     { T{ key-down f f "RET" } insert-newline }
554     { T{ key-down f { S+ } "RET" } insert-newline }
555     { T{ key-down f f "ENTER" } insert-newline }
556     { T{ key-down f { C+ } "j" } com-join-lines }
557 } define-command-map
558
559 TUPLE: source-editor < multiline-editor ;
560
561 : <source-editor> ( -- editor )
562     source-editor new-editor ;
563
564 ! A useful model
565 : <element-model> ( editor element -- model )
566     [ [ caret>> ] [ model>> ] bi ] dip
567     '[ _ _ elt-string ] <arrow> ;
568
569 ! Fields wrap an editor
570 TUPLE: field < border editor min-cols max-cols ;
571
572 : field-theme ( gadget -- gadget )
573     { 2 2 } >>size
574     { 1 0 } >>fill
575     COLOR: gray <solid> >>boundary ; inline
576
577 : <field-border> ( gadget -- border )
578     { 2 2 } <border>
579         { 1 0 } >>fill
580         field-theme ;
581
582 : new-field ( class -- gadget )
583     [ <editor> ] dip new-border
584         dup gadget-child >>editor
585         field-theme ; inline
586
587 ! For line-gadget-width
588 M: field font>> editor>> font>> ;
589
590 M: field pref-dim*
591     dup
592     [ editor>> pref-dim ] keep
593     [ line-gadget-width ] [ drop second ] 2bi 2array
594     border-pref-dim ;
595
596 TUPLE: model-field < field field-model ;
597
598 : <model-field> ( model -- gadget )
599     model-field new-field swap >>field-model ;
600
601 M: model-field graft*
602     [ [ field-model>> value>> ] [ editor>> ] bi set-editor-string ]
603     [ dup editor>> model>> add-connection ]
604     bi ;
605
606 M: model-field ungraft*
607     dup editor>> model>> remove-connection ;
608
609 M: model-field model-changed
610     nip [ editor>> editor-string ] [ field-model>> ] bi set-model ;
611
612 TUPLE: action-field < field quot ;
613
614 : <action-field> ( quot -- gadget )
615     action-field new-field swap >>quot ;
616
617 : invoke-action-field ( field -- )
618     [ editor>> editor-string ]
619     [ editor>> clear-editor ]
620     [ quot>> ]
621     tri call( string -- ) ;
622
623 action-field H{
624     { T{ key-down f f "RET" } [ invoke-action-field ] }
625 } set-gestures