]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/editors/editors.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[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 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 grouping ;
12 EXCLUDE: fonts => selection ;
13 IN: ui.gadgets.editors
14
15 TUPLE: editor < line-gadget
16 caret-color
17 caret mark
18 focused? blink blink-alarm ;
19
20 : <loc> ( -- loc ) { 0 0 } <model> ;
21
22 : init-editor-locs ( editor -- editor )
23     <loc> >>caret
24     <loc> >>mark ; inline
25
26 : editor-theme ( editor -- editor )
27     COLOR: red >>caret-color
28     monospace-font >>font ; inline
29
30 : new-editor ( class -- editor )
31     new-line-gadget
32         <document> >>model
33         init-editor-locs
34         editor-theme ; inline
35
36 : <editor> ( -- editor )
37     editor new-editor ;
38
39 : activate-editor-model ( editor model -- )
40     2dup add-connection
41     dup activate-model
42     swap model>> add-loc ;
43
44 : deactivate-editor-model ( editor model -- )
45     2dup remove-connection
46     dup deactivate-model
47     swap model>> remove-loc ;
48
49 : blink-caret ( editor -- )
50     [ not ] change-blink relayout-1 ;
51
52 SYMBOL: blink-interval
53
54 750 milliseconds blink-interval set-global
55
56 : stop-blinking ( editor -- )
57     [ [ cancel-alarm ] when* f ] change-blink-alarm drop ;
58
59 : start-blinking ( editor -- )
60     [ stop-blinking ] [
61         t >>blink
62         dup '[ _ blink-caret ] blink-interval get every
63         >>blink-alarm drop
64     ] bi ;
65
66 : restart-blinking ( editor -- )
67     dup focused?>> [
68         [ start-blinking ]
69         [ relayout-1 ]
70         bi
71     ] [ drop ] if ;
72
73 M: editor graft*
74     dup
75     dup caret>> activate-editor-model
76     dup mark>> activate-editor-model ;
77
78 M: editor ungraft*
79     dup
80     dup stop-blinking
81     dup caret>> deactivate-editor-model
82     dup mark>> deactivate-editor-model ;
83
84 : editor-caret ( editor -- loc ) caret>> value>> ;
85
86 : editor-mark ( editor -- loc ) mark>> value>> ;
87
88 : set-caret ( loc editor -- )
89     [ model>> validate-loc ] keep
90     caret>> set-model ;
91
92 : change-caret ( editor quot -- )
93     [ [ [ editor-caret ] [ model>> ] bi ] dip call ] [ drop ] 2bi
94     set-caret ; inline
95
96 : mark>caret ( editor -- )
97     [ editor-caret ] [ mark>> ] bi set-model ;
98
99 : change-caret&mark ( editor quot -- )
100     [ change-caret ] [ drop mark>caret ] 2bi ; inline
101
102 : editor-line ( n editor -- str ) control-value nth ;
103
104 :: point>loc ( point editor -- loc )
105     point second editor y>line {
106         { [ dup 0 < ] [ drop { 0 0 } ] }
107         { [ dup editor model>> last-line# > ] [ drop editor model>> doc-end ] }
108         [| n |
109             n
110             point first
111             editor font>>
112             n editor editor-line
113             x>offset 2array
114         ]
115     } cond ;
116
117 : clicked-loc ( editor -- loc )
118     [ hand-rel ] keep point>loc ;
119
120 : click-loc ( editor model -- )
121     [ clicked-loc ] dip set-model ;
122
123 : focus-editor ( editor -- )
124     [ start-blinking ] [ t >>focused? relayout-1 ] bi ;
125
126 : unfocus-editor ( editor -- )
127     [ stop-blinking ] [ f >>focused? relayout-1 ] bi ;
128
129 : loc>x ( loc editor -- x )
130     [ first2 swap ] dip [ editor-line ] [ font>> ] bi swap offset>x round ;
131
132 : loc>point ( loc editor -- loc )
133     [ loc>x ] [ [ first ] dip line>y ceiling ] 2bi 2array ;
134
135 : caret-loc ( editor -- loc )
136     [ editor-caret ] keep loc>point ;
137
138 : caret-dim ( editor -- dim )
139     line-height 0 swap 2array ;
140
141 : scroll>caret ( editor -- )
142     dup graft-state>> second [
143         [
144             [ caret-loc ] [ caret-dim { 2 1 } v+ ] bi <rect>
145         ] keep scroll>rect
146     ] [ drop ] if ;
147
148 : draw-caret? ( editor -- ? )
149     [ focused?>> ] [ blink>> ] bi and ;
150
151 : draw-caret ( editor -- )
152     dup draw-caret? [
153         [ caret-color>> gl-color ]
154         [
155             [ caret-loc ] [ caret-dim ] bi
156             over v+ gl-line
157         ] bi
158     ] [ drop ] if ;
159
160 : selection-start/end ( editor -- start end )
161     [ editor-mark ] [ editor-caret ] bi sort-pair ;
162
163 SYMBOL: selected-lines
164
165 TUPLE: selected-line start end first? last? ;
166
167 : compute-selection ( editor -- assoc )
168     dup gadget-selection? [
169         [ selection-start/end [ [ first ] bi@ [a,b] ] 2keep ] keep model>>
170         '[ [ _ _ ] keep _ start/end-on-line 2array ] H{ } map>assoc
171     ] [ drop f ] if ;
172
173 :: draw-selection ( line pair editor -- )
174     pair [ editor font>> line offset>x ] map :> pair
175     editor selection-color>> gl-color
176     pair first 0 2array
177     pair second pair first - round 1 max editor line-height 2array
178     gl-fill-rect ;
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{ button-up f { S+ } 1 } com-copy-selection }
456     { T{ drag } drag-selection }
457     { gain-focus focus-editor }
458     { lose-focus unfocus-editor }
459     { delete-action remove-selection }
460     { select-all-action select-all }
461     { T{ key-down f { C+ } "l" } select-line }
462     { T{ key-down f { S+ } "LEFT" } select-previous-character }
463     { T{ key-down f { S+ } "RIGHT" } select-next-character }
464     { T{ key-down f { S+ C+ } "LEFT" } select-previous-word }
465     { T{ key-down f { S+ C+ } "RIGHT" } select-next-word }
466     { T{ key-down f { S+ } "HOME" } select-start-of-line }
467     { T{ key-down f { S+ } "END" } select-end-of-line }
468     { T{ key-down f { S+ C+ } "HOME" } select-start-of-document }
469     { T{ key-down f { S+ C+ } "END" } select-end-of-document }
470 } define-command-map
471
472 : editor-menu ( editor -- )
473     {
474         com-undo
475         com-redo
476         ----
477         com-cut
478         com-copy
479         com-paste
480     } show-commands-menu ;
481
482 editor "misc" f {
483     { T{ button-down f f 3 } editor-menu }
484 } define-command-map
485
486 ! Multi-line editors
487 TUPLE: multiline-editor < editor ;
488
489 : <multiline-editor> ( -- editor )
490     multiline-editor new-editor ;
491
492 : previous-line ( editor -- ) line-elt editor-prev ;
493
494 : next-line ( editor -- ) line-elt editor-next ;
495
496 <PRIVATE
497
498 : page-elt ( editor -- editor element ) dup visible-lines 1 - <page-elt> ;
499
500 PRIVATE>
501
502 : previous-page ( editor -- ) page-elt editor-prev ;
503
504 : next-page ( editor -- ) page-elt editor-next ;
505
506 : select-previous-line ( editor -- ) line-elt editor-select-prev ;
507
508 : select-next-line ( editor -- ) line-elt editor-select-next ;
509
510 : select-previous-page ( editor -- ) page-elt editor-select-prev ;
511
512 : select-next-page ( editor -- ) page-elt editor-select-next ;
513
514 : insert-newline ( editor -- )
515     "\n" swap user-input* drop ;
516
517 : change-selection ( editor quot -- )
518     '[ gadget-selection @ ] keep user-input* drop ; inline
519
520 : join-lines ( string -- string' )
521     "\n" split
522     [ rest-slice [ [ blank? ] trim-head-slice ] change-each ]
523     [ but-last-slice [ [ blank? ] trim-tail-slice ] change-each ]
524     [ " " join ]
525     tri ;
526
527 : this-line-and-next ( document line -- start end )
528     [ nip 0 swap 2array ]
529     [ [ nip 1 + ] [ 1 + swap doc-line length ] 2bi 2array ]
530     2bi ;
531
532 : last-line? ( document line -- ? )
533     [ last-line# ] dip = ;
534
535 : com-join-lines ( editor -- )
536     dup gadget-selection?
537     [ [ join-lines ] change-selection ] [
538         [ model>> ] [ editor-caret first ] bi
539         2dup last-line? [ 2drop ] [
540             [ this-line-and-next ] [ drop ] 2bi
541             [ join-lines ] change-doc-range
542         ] if
543     ] if ;
544
545 multiline-editor "multiline" f {
546     { T{ key-down f f "UP" } previous-line }
547     { T{ key-down f f "DOWN" } next-line }
548     { T{ key-down f { S+ } "UP" } select-previous-line }
549     { T{ key-down f { S+ } "DOWN" } select-next-line }
550     { T{ key-down f f "PAGE_UP" } previous-page }
551     { T{ key-down f f "PAGE_DOWN" } next-page }
552     { T{ key-down f { S+ } "PAGE_UP" } select-previous-page }
553     { T{ key-down f { S+ } "PAGE_DOWN" } select-next-page }
554     { T{ key-down f f "RET" } insert-newline }
555     { T{ key-down f { S+ } "RET" } insert-newline }
556     { T{ key-down f f "ENTER" } insert-newline }
557     { T{ key-down f { C+ } "j" } com-join-lines }
558 } define-command-map
559
560 TUPLE: source-editor < multiline-editor ;
561
562 : <source-editor> ( -- editor )
563     source-editor new-editor ;
564
565 ! A useful model
566 : <element-model> ( editor element -- model )
567     [ [ caret>> ] [ model>> ] bi ] dip
568     '[ _ _ elt-string ] <arrow> ;
569
570 ! Fields wrap an editor
571 TUPLE: field < border editor min-cols max-cols ;
572
573 : field-theme ( gadget -- gadget )
574     { 2 2 } >>size
575     { 1 0 } >>fill
576     COLOR: gray <solid> >>boundary ; inline
577
578 : <field-border> ( gadget -- border )
579     { 2 2 } <border>
580         { 1 0 } >>fill
581         field-theme ;
582
583 : new-field ( class -- gadget )
584     [ <editor> ] dip new-border
585         dup gadget-child >>editor
586         field-theme ; inline
587
588 ! For line-gadget-width
589 M: field font>> editor>> font>> ;
590
591 M: field pref-dim*
592     dup
593     [ editor>> pref-dim ] keep
594     [ line-gadget-width ] [ drop second ] 2bi 2array
595     border-pref-dim ;
596
597 TUPLE: model-field < field field-model ;
598
599 : <model-field> ( model -- gadget )
600     model-field new-field swap >>field-model ;
601
602 M: model-field graft*
603     [ [ field-model>> value>> ] [ editor>> ] bi set-editor-string ]
604     [ dup editor>> model>> add-connection ]
605     bi ;
606
607 M: model-field ungraft*
608     dup editor>> model>> remove-connection ;
609
610 M: model-field model-changed
611     nip [ editor>> editor-string ] [ field-model>> ] bi set-model ;
612
613 TUPLE: action-field < field quot ;
614
615 : <action-field> ( quot -- gadget )
616     action-field new-field swap >>quot ;
617
618 : invoke-action-field ( field -- )
619     [ editor>> editor-string ]
620     [ editor>> clear-editor ]
621     [ quot>> ]
622     tri call( string -- ) ;
623
624 action-field H{
625     { T{ key-down f f "RET" } [ invoke-action-field ] }
626 } set-gestures