]> gitweb.factorcode.org Git - factor.git/blob - library/ui/text/commands.factor
Documentation updates, menus fix
[factor.git] / library / ui / text / commands.factor
1 ! Copyright (C) 2006 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: gadgets-text
4 USING: gadgets kernel models namespaces sequences ;
5
6 : editor-extend-selection ( editor -- )
7     dup request-focus
8     dup editor-caret click-loc ;
9
10 : editor-mouse-down ( editor -- )
11     dup editor-extend-selection
12     dup editor-mark click-loc ;
13
14 : editor-mouse-drag ( editor -- )
15     dup editor-caret click-loc ;
16
17 : editor-copy ( editor clipboard -- )
18     over gadget-selection? [
19         >r [ gadget-selection ] keep r> copy-clipboard
20     ] [
21         2drop
22     ] if ;
23
24 : editor-cut ( editor clipboard -- )
25     dupd editor-copy remove-editor-selection ;
26
27 : delete/backspace ( elt editor quot -- )
28     over gadget-selection? [
29         drop nip remove-editor-selection
30     ] [
31         over >r >r dup editor-caret* swap control-model
32         r> call r> control-model remove-doc-range
33     ] if ; inline
34
35 : editor-delete ( editor elt -- )
36     swap [ over >r rot next-elt r> swap ] delete/backspace ;
37
38 : editor-backspace ( editor elt -- )
39     swap [ over >r rot prev-elt r> ] delete/backspace ;
40
41 : editor-select-prev ( editor elt -- )
42     swap [ rot prev-elt ] change-caret ;
43
44 : editor-prev ( editor elt -- )
45     dupd editor-select-prev mark>caret ;
46
47 : editor-select-next ( editor elt -- )
48     swap [ rot next-elt ] change-caret ;
49
50 : editor-next ( editor elt -- )
51     dupd editor-select-next mark>caret ;
52
53 : editor-select ( from to editor -- )
54     tuck editor-caret set-model* editor-mark set-model* ;
55
56 : select-elt ( editor elt -- )
57     over >r
58     >r dup editor-caret* swap control-model r>
59     3dup next-elt >r prev-elt r>
60     r> editor-select ;
61
62 : select-all ( editor -- ) T{ doc-elt } select-elt ;
63
64 : editor-doc-start ( editor -- ) T{ doc-elt } editor-prev ;
65
66 : editor-doc-end ( editor -- ) T{ doc-elt } editor-next ;
67
68 : selected-word ( editor -- string )
69     dup gadget-selection?
70     [ dup T{ word-elt } select-elt ] unless
71     gadget-selection ;
72
73 editor "editing" {
74     { "Insert newline" T{ key-down f f "RETURN" } [ "\n" swap user-input ] }
75     { "Insert newline" T{ key-down f { S+ } "RETURN" } [ "\n" swap user-input ] }
76     { "Insert newline" T{ key-down f f "ENTER" } [ "\n" swap user-input ] }
77     { "Delete next character" T{ key-down f f "DELETE" } [ T{ char-elt } editor-delete ] }
78     { "Delete previous character" T{ key-down f f "BACKSPACE" } [ T{ char-elt } editor-backspace ] }
79     { "Delete previous word" T{ key-down f { C+ } "DELETE" } [ T{ word-elt } editor-delete ] }
80     { "Delete next word" T{ key-down f { C+ } "BACKSPACE" } [ T{ word-elt } editor-backspace ] }
81     { "Delete to start of line" T{ key-down f { A+ } "DELETE" } [ T{ one-line-elt } editor-delete ] }
82     { "Delete to end of line" T{ key-down f { A+ } "BACKSPACE" } [ T{ one-line-elt } editor-backspace ] }
83 } define-commands
84
85 editor "clipboard" {
86     { "Paste" T{ paste-action } [ clipboard get paste-clipboard ] }
87     { "Paste selection" T{ button-up f f 2 } [ selection get paste-clipboard ] }
88     { "Copy" T{ copy-action } [ clipboard get editor-copy ] }
89     { "Copy selection" T{ button-up } [ selection get editor-copy ] }
90     { "Cut" T{ cut-action } [ clipboard get editor-cut ] }
91 } define-commands
92
93 editor "caret" {
94     { "Position caret" T{ button-down } [ editor-mouse-down ] }
95     { "Previous character" T{ key-down f f "LEFT" } [ T{ char-elt } editor-prev ] }
96     { "Next character" T{ key-down f f "RIGHT" } [ T{ char-elt } editor-next ] }
97     { "Previous line" T{ key-down f f "UP" } [ T{ line-elt } editor-prev ] }
98     { "Next line" T{ key-down f f "DOWN" } [ T{ line-elt } editor-next ] }
99     { "Previous word" T{ key-down f { C+ } "LEFT" } [ T{ word-elt } editor-prev ] }
100     { "Next word" T{ key-down f { C+ } "RIGHT" } [ T{ word-elt } editor-next ] }
101     { "Start of line" T{ key-down f f "HOME" } [ T{ one-line-elt } editor-prev ] }
102     { "End of line" T{ key-down f f "END" } [ T{ one-line-elt } editor-next ] }
103     { "Start of document" T{ key-down f { C+ } "HOME" } [ editor-doc-start ] }
104     { "End of document" T{ key-down f { C+ } "END" } [ editor-doc-end ] }
105 } define-commands
106     
107 editor "selection" {
108     { "Extend selection" T{ button-down f { S+ } } [ editor-extend-selection ] }
109     { "Start selection" T{ drag } [ editor-mouse-drag ] }
110     { "Focus editor" T{ gain-focus } [ focus-editor ] }
111     { "Unfocus editor" T{ lose-focus } [ unfocus-editor ] }
112     { "Clear" T{ delete-action } [ remove-editor-selection ] }
113     { "Select all" T{ select-all-action } [ T{ doc-elt } select-elt ] }
114     { "Select line" T{ key-down f { C+ } "l" } [ T{ one-line-elt } select-elt ] }
115     { "Select word" T{ key-down f { C+ } "w" } [ T{ word-elt } select-elt ] }
116     { "Select previous character" T{ key-down f { S+ } "LEFT" } [ T{ char-elt } editor-select-prev ] }
117     { "Select next character" T{ key-down f { S+ } "RIGHT" } [ T{ char-elt } editor-select-next ] }
118     { "Select previous line" T{ key-down f { S+ } "UP" } [ T{ line-elt } editor-select-prev ] }
119     { "Select next line" T{ key-down f { S+ } "DOWN" } [ T{ line-elt } editor-select-next ] }
120     { "Select previous line" T{ key-down f { S+ C+ } "LEFT" } [ T{ word-elt } editor-select-prev ] }
121     { "Select next line" T{ key-down f { S+ C+ } "RIGHT" } [ T{ word-elt } editor-select-next ] }
122     { "Select to start of line" T{ key-down f { S+ } "HOME" } [ T{ one-line-elt } editor-select-prev ] }
123     { "Select to end of line" T{ key-down f { S+ } "END" } [ T{ one-line-elt } editor-select-next ] }
124     { "Select start of document" T{ key-down f { S+ C+ } "HOME" } [ T{ doc-elt } editor-select-prev ] }
125     { "Select end of document" T{ key-down f { S+ C+ } "END" } [ T{ doc-elt } editor-select-next ] }
126 } define-commands