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