summaryrefslogtreecommitdiff
path: root/src/change_vectors.adb
blob: 13be51f6428bf6a0f29685c0bc68e1686800836e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212


with

    FLTK.Text_Buffers,
    Ada.Strings.Unbounded;

use

    Ada.Strings.Unbounded;

use type

    FLTK.Text_Buffers.Modification,
    FLTK.Text_Buffers.Position;


package body Change_Vectors is


    function Continues_Insert
           (Orig, Cont : in Change)
        return Boolean is
    begin
        return
            Orig.Action = FLTK.Text_Buffers.Insert and then
            Integer (Orig.Place) + Orig.Length = Integer (Cont.Place) and then
            Cont.Length = 1 and then
            (Element (Orig.Text, Orig.Length) /= ' ' or else Element (Cont.Text, 1) = ' ');
    end Continues_Insert;




    function Continues_Delete
           (Orig, Cont : in Change)
        return Boolean is
    begin
        return
            Orig.Action = FLTK.Text_Buffers.Delete and then
            Orig.Place = Cont.Place and then
            Cont.Length = 1;
    end Continues_Delete;




    function Continues_Backspace
           (Orig, Cont : in Change)
        return Boolean is
    begin
        return
            Orig.Action = FLTK.Text_Buffers.Delete and then
            Orig.Place = Cont.Place + 1 and then
            Cont.Length = 1;
    end Continues_Backspace;




    procedure Push
           (This : in out Change_Vector;
            Item : in     Change)
    is

        procedure App (Ch : in out Change) is
        begin
            Ch.Length := Ch.Length + 1;
            Append (Ch.Text, Item.Text);
        end App;

        procedure Pre (Ch : in out Change) is
        begin
            Ch.Length := Ch.Length + 1;
            Ch.Text := Item.Text & Ch.Text;
            Ch.Place := Ch.Place - 1;
        end Pre;

    begin
        if Item.Action = FLTK.Text_Buffers.Insert then
            if  This.Near > 0 and then
                Continues_Insert (This.List.Element (This.Near), Item)
            then
                This.List.Update_Element (This.Near, App'Access);
            else
                This.Near := This.Near + 1;
                This.List.Insert (This.Near, Item);
            end if;
        elsif Item.Action = FLTK.Text_Buffers.Delete then
            if This.Near > 0 then
                if Continues_Delete (This.List.Element (This.Near), Item)
                then
                    This.List.Update_Element (This.Near, App'Access);
                elsif
                    Continues_Backspace (This.List.Element (This.Near), Item)
                then
                    This.List.Update_Element (This.Near, Pre'Access);
                else
                    This.Near := This.Near + 1;
                    This.List.Insert (This.Near, Item);
                end if;
            end if;
        end if;
        This.Far := This.Near;
        while Integer (This.List.Length) > This.Far loop
            This.List.Delete_Last;
        end loop;
    end Push;




    function Pop
           (This : in out Change_Vector)
        return Boolean is
    begin
        if This.Near > 0 then
            This.Near := This.Near - 1;
            return True;
        else
            return False;
        end if;
    end Pop;




    procedure Pop
           (This : in out Change_Vector) is
    begin
        if This.Near > 0 then
            This.Near := This.Near - 1;
        end if;
    end Pop;




    function Peek
           (This : in     Change_Vector;
            Item :    out Change)
        return Boolean is
    begin
        if This.Near > 0 then
            Item := This.List.Element (This.Near);
            return True;
        else
            return False;
        end if;
    end Peek;




    procedure Peek
           (This : in     Change_Vector;
            Item :    out Change) is
    begin
        if This.Near > 0 then
            Item := This.List.Element (This.Near);
        end if;
    end Peek;




    function Re_Push
           (This : in out Change_Vector)
        return Boolean is
    begin
        if This.Near < This.Far then
            This.Near := This.Near + 1;
            return True;
        else
            return False;
        end if;
    end Re_Push;




    procedure Re_Push
           (This : in out Change_Vector) is
    begin
        if This.Near < This.Far then
            This.Near := This.Near + 1;
        end if;
    end Re_Push;




    function At_Start
           (This : in Change_Vector)
        return Boolean is
    begin
        return This.Near = 0;
    end At_Start;




    function At_End
           (This : in Change_Vector)
        return Boolean is
    begin
        return This.Near >= This.Far;
    end At_End;


end Change_Vectors;