]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/editors/editors.factor
f42fdf46167cef0dcb7f6e96aa4c6ae689173010
[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     [ [ cancel-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 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 { S+ } "DELETE" } delete-next-character }
369     { T{ key-down f f "BACKSPACE" } delete-previous-character }
370     { T{ key-down f { S+ } "BACKSPACE" } delete-previous-character }
371     { T{ key-down f { C+ } "DELETE" } delete-previous-word }
372     { T{ key-down f { C+ } "BACKSPACE" } delete-next-word }
373     { T{ key-down f { A+ } "DELETE" } delete-to-start-of-line }
374     { T{ key-down f { A+ } "BACKSPACE" } delete-to-end-of-line }
375 } define-command-map
376
377 : com-paste ( editor -- ) clipboard get paste-clipboard ;
378
379 : paste-selection ( editor -- ) selection get paste-clipboard ;
380
381 : com-cut ( editor -- ) clipboard get editor-cut ;
382
383 editor "clipboard" f {
384     { cut-action com-cut }
385     { copy-action com-copy }
386     { paste-action com-paste }
387     { T{ button-up } com-copy-selection }
388     { T{ button-up f f 2 } paste-selection }
389 } define-command-map
390
391 : previous-character ( editor -- )
392     dup gadget-selection? [
393         dup selection-start/end drop
394         over set-caret mark>caret
395     ] [
396         char-elt editor-prev
397     ] if ;
398
399 : next-character ( editor -- )
400     dup gadget-selection? [
401         dup selection-start/end nip
402         over set-caret mark>caret
403     ] [
404         char-elt editor-next
405     ] if ;
406
407 : previous-word ( editor -- ) word-elt editor-prev ;
408
409 : next-word ( editor -- ) word-elt editor-next ;
410
411 : start-of-line ( editor -- ) one-line-elt editor-prev ;
412
413 : end-of-line ( editor -- ) one-line-elt editor-next ;
414
415 editor "caret-motion" f {
416     { T{ button-down } position-caret }
417     { T{ key-down f f "LEFT" } previous-character }
418     { T{ key-down f f "RIGHT" } next-character }
419     { T{ key-down f { C+ } "LEFT" } previous-word }
420     { T{ key-down f { C+ } "RIGHT" } next-word }
421     { T{ key-down f f "HOME" } start-of-line }
422     { T{ key-down f f "END" } end-of-line }
423     { T{ key-down f { C+ } "HOME" } start-of-document }
424     { T{ key-down f { C+ } "END" } end-of-document }
425 } define-command-map
426
427 : clear-editor ( editor -- )
428     model>> clear-doc ;
429
430 : select-all ( editor -- ) doc-elt select-elt ;
431
432 : select-line ( editor -- ) one-line-elt select-elt ;
433
434 : select-word ( editor -- ) one-word-elt select-elt ;
435
436 : selected-token ( editor -- string )
437     dup gadget-selection?
438     [ dup select-word ] unless
439     gadget-selection ;
440
441 : select-previous-character ( editor -- ) 
442     char-elt editor-select-prev ;
443
444 : select-next-character ( editor -- ) 
445     char-elt editor-select-next ;
446
447 : select-previous-word ( editor -- ) 
448     word-elt editor-select-prev ;
449
450 : select-next-word ( editor -- ) 
451     word-elt editor-select-next ;
452
453 : select-start-of-line ( editor -- ) 
454     one-line-elt editor-select-prev ;
455
456 : select-end-of-line ( editor -- ) 
457     one-line-elt editor-select-next ;
458
459 : select-start-of-document ( editor -- ) 
460     doc-elt editor-select-prev ;
461
462 : select-end-of-document ( editor -- ) 
463     doc-elt editor-select-next ;
464
465 editor "selection" f {
466     { T{ button-down f { S+ } 1 } extend-selection }
467     { T{ button-up f { S+ } 1 } com-copy-selection }
468     { T{ drag } drag-selection }
469     { gain-focus focus-editor }
470     { lose-focus unfocus-editor }
471     { delete-action remove-selection }
472     { select-all-action select-all }
473     { T{ key-down f { C+ } "l" } select-line }
474     { T{ key-down f { S+ } "LEFT" } select-previous-character }
475     { T{ key-down f { S+ } "RIGHT" } select-next-character }
476     { T{ key-down f { S+ C+ } "LEFT" } select-previous-word }
477     { T{ key-down f { S+ C+ } "RIGHT" } select-next-word }
478     { T{ key-down f { S+ } "HOME" } select-start-of-line }
479     { T{ key-down f { S+ } "END" } select-end-of-line }
480     { T{ key-down f { S+ C+ } "HOME" } select-start-of-document }
481     { T{ key-down f { S+ C+ } "END" } select-end-of-document }
482 } define-command-map
483
484 : editor-menu ( editor -- )
485     {
486         com-undo
487         com-redo
488         ----
489         com-cut
490         com-copy
491         com-paste
492     } show-commands-menu ;
493
494 editor "misc" f {
495     { T{ button-down f f 3 } editor-menu }
496 } define-command-map
497
498 ! Multi-line editors
499 TUPLE: multiline-editor < editor ;
500
501 : <multiline-editor> ( -- editor )
502     multiline-editor new-editor ;
503
504 : previous-line ( editor -- ) line-elt editor-prev ;
505
506 : next-line ( editor -- ) line-elt editor-next ;
507
508 <PRIVATE
509
510 : page-elt ( editor -- editor element ) dup visible-lines 1 - <page-elt> ;
511
512 PRIVATE>
513
514 : previous-page ( editor -- ) page-elt editor-prev ;
515
516 : next-page ( editor -- ) page-elt editor-next ;
517
518 : select-previous-line ( editor -- ) line-elt editor-select-prev ;
519
520 : select-next-line ( editor -- ) line-elt editor-select-next ;
521
522 : select-previous-page ( editor -- ) page-elt editor-select-prev ;
523
524 : select-next-page ( editor -- ) page-elt editor-select-next ;
525
526 : insert-newline ( editor -- )
527     "\n" swap user-input* drop ;
528
529 : change-selection ( editor quot -- )
530     '[ gadget-selection @ ] [ user-input* drop ] bi ; inline
531
532 <PRIVATE
533
534 : join-lines ( string -- string' )
535     "\n" split
536     [ rest-slice [ [ blank? ] trim-head-slice ] map! drop ]
537     [ but-last-slice [ [ blank? ] trim-tail-slice ] map! drop ]
538     [ " " join ]
539     tri ;
540
541 : last-line? ( document line -- ? )
542     [ last-line# ] dip = ;
543
544 : prev-line-and-this ( document line -- start end )
545     swap
546     [ drop 1 - 0 2array ]
547     [ [ drop ] [ doc-line length ] 2bi 2array ]
548     2bi ;
549
550 : join-with-prev ( document line -- )
551     [ prev-line-and-this ] [ drop ] 2bi
552     [ join-lines ] change-doc-range ;
553
554 : this-line-and-next ( document line -- start end )
555     swap
556     [ drop 0 2array ]
557     [ [ 1 + ] dip [ drop ] [ doc-line length ] 2bi 2array ]
558     2bi ;
559
560 : join-with-next ( document line -- )
561     [ this-line-and-next ] [ drop ] 2bi
562     [ join-lines ] change-doc-range ;
563
564 PRIVATE>
565
566 : com-join-lines ( editor -- )
567     dup gadget-selection?
568     [ [ join-lines ] change-selection ] [
569         [ model>> ] [ editor-caret first ] bi {
570             { [ over last-line# 0 = ] [ 2drop ] }
571             { [ 2dup last-line? ] [ join-with-prev ] }
572             [ join-with-next ]
573         } cond
574     ] if ;
575
576 multiline-editor "multiline" f {
577     { T{ key-down f f "UP" } previous-line }
578     { T{ key-down f f "DOWN" } next-line }
579     { T{ key-down f { S+ } "UP" } select-previous-line }
580     { T{ key-down f { S+ } "DOWN" } select-next-line }
581     { T{ key-down f f "PAGE_UP" } previous-page }
582     { T{ key-down f f "PAGE_DOWN" } next-page }
583     { T{ key-down f { S+ } "PAGE_UP" } select-previous-page }
584     { T{ key-down f { S+ } "PAGE_DOWN" } select-next-page }
585     { T{ key-down f f "RET" } insert-newline }
586     { T{ key-down f { S+ } "RET" } insert-newline }
587     { T{ key-down f f "ENTER" } insert-newline }
588     { T{ key-down f { C+ } "j" } com-join-lines }
589 } define-command-map
590
591 TUPLE: source-editor < multiline-editor ;
592
593 : <source-editor> ( -- editor )
594     source-editor new-editor ;
595
596 ! A useful model
597 : <element-model> ( editor element -- model )
598     [ [ caret>> ] [ model>> ] bi ] dip
599     '[ _ _ elt-string ] <arrow> ;
600
601 ! Fields wrap an editor
602 TUPLE: field < border editor min-cols max-cols ;
603
604 <PRIVATE
605
606 : field-theme ( gadget -- gadget )
607     { 2 2 } >>size
608     { 1 0 } >>fill
609     COLOR: gray <solid> >>boundary ; inline
610
611 : <field-border> ( gadget -- border )
612     { 2 2 } <border>
613         { 1 0 } >>fill
614         field-theme ;
615
616 PRIVATE>
617
618 : new-field ( class -- gadget )
619     [ <editor> ] dip new-border
620         dup gadget-child >>editor
621         field-theme ; inline
622
623 ! For line-gadget-width
624 M: field font>> editor>> font>> ;
625
626 M: field pref-dim*
627     [ ]
628     [ editor>> pref-dim ]
629     [ [ line-gadget-width ] [ drop second ] 2bi 2array ]
630     tri border-pref-dim ;
631
632 TUPLE: model-field < field field-model ;
633
634 : <model-field> ( model -- gadget )
635     model-field new-field
636         swap >>field-model ;
637
638 M: model-field graft*
639     [ [ field-model>> value>> ] [ editor>> ] bi set-editor-string ]
640     [ dup editor>> model>> add-connection ]
641     bi ;
642
643 M: model-field ungraft*
644     dup editor>> model>> remove-connection ;
645
646 M: model-field model-changed
647     nip [ editor>> editor-string ] [ field-model>> ] bi set-model ;
648
649 TUPLE: action-field < field quot ;
650
651 : <action-field> ( quot -- gadget )
652     action-field new-field
653         swap >>quot ;
654
655 : invoke-action-field ( field -- )
656     [ editor>> editor-string ]
657     [ editor>> clear-editor ]
658     [ quot>> ]
659     tri call( string -- ) ;
660
661 action-field H{
662     { T{ key-down f f "RET" } [ invoke-action-field ] }
663 } set-gestures