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