~ubuntu-branches/ubuntu/precise/fte/precise

« back to all changes in this revision

Viewing changes to src/e_line.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Neil Williams
  • Date: 2011-08-14 10:28:46 UTC
  • mfrom: (1.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20110814102846-kate2jfkduwpnika
Tags: 0.50.2b6-1
* QA upload.
* Synchronise with current upstream sources.
  (Closes: #195945)
* Include NMUs by Nobuhiro Iwamatsu and Hideki Yamane,
  thanks to both.
* Move to 3.0 quilt

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
    , StateE(0)
18
18
#endif
19
19
{
20
 
    Allocate(Count); 
 
20
    Allocate(Count);
21
21
    if (AChars)
22
22
        memcpy(Chars, AChars, Count);
23
23
    else
38
38
}
39
39
 
40
40
int ELine::Allocate(size_t Bytes) {
41
 
    return ((Chars = (char*)realloc(Chars, Bytes | CHAR_TRESHOLD)) != NULL) ? 1 : 0;
 
41
    Chars = (char*)realloc(Chars, Bytes | CHAR_TRESHOLD);
 
42
    return (Chars != NULL);
42
43
}
43
44
 
44
45
int EBuffer::ScreenPos(ELine *L, int Offset) {
45
46
    int ExpandTabs = BFI(this, BFI_ExpandTabs);
46
47
    int TabSize = BFI(this, BFI_TabSize);
47
48
 
48
 
    if (!ExpandTabs) {
 
49
    if (!ExpandTabs)
49
50
        return Offset;
50
 
    } else {
51
 
        char *p = L->Chars;
52
 
        int Len = L->Count;
53
 
        int Pos = 0;
54
 
        int Ofs = Offset;
55
 
 
56
 
        if (Ofs > Len) {
57
 
            while (Len > 0) {
58
 
                if (*p++ != '\t')
59
 
                    Pos++;
60
 
                else
61
 
                    Pos = NextTab(Pos, TabSize);
62
 
                Len--;
63
 
            }
64
 
            Pos += Ofs - L->Count;
65
 
        } else {
66
 
            while (Ofs > 0) {
67
 
                if (*p++ != '\t')
68
 
                    Pos++;
69
 
                else
70
 
                    Pos = NextTab(Pos, TabSize);
71
 
                Ofs--;
72
 
            }
73
 
        }
74
 
        return Pos;
75
 
    }
 
51
 
 
52
    char *p = L->Chars;
 
53
    int Len = L->Count;
 
54
    int Pos = 0;
 
55
    int Ofs = Offset;
 
56
 
 
57
    if (Ofs > Len) {
 
58
        while (Len > 0) {
 
59
            if (*p++ != '\t')
 
60
                Pos++;
 
61
            else
 
62
                Pos = NextTab(Pos, TabSize);
 
63
            Len--;
 
64
        }
 
65
        Pos += Ofs - L->Count;
 
66
    } else
 
67
        while (Ofs > 0) {
 
68
            if (*p++ != '\t')
 
69
                Pos++;
 
70
            else
 
71
                Pos = NextTab(Pos, TabSize);
 
72
            Ofs--;
 
73
        }
 
74
 
 
75
    return Pos;
76
76
}
77
77
 
78
78
int EBuffer::CharOffset(ELine *L, int ScreenPos) {
79
79
    int ExpandTabs = BFI(this, BFI_ExpandTabs);
80
80
    int TabSize = BFI(this, BFI_TabSize);
81
81
 
82
 
    if (!ExpandTabs) {
 
82
    if (!ExpandTabs)
83
83
        return ScreenPos;
84
 
    } else {
85
 
        int Pos = 0;
86
 
        int Ofs = 0;
87
 
        char *p = L->Chars;
88
 
        int Len = L->Count;
89
 
 
90
 
        while (Len > 0) {
91
 
            if (*p++ != '\t')
92
 
                Pos++;
93
 
            else
94
 
                Pos = NextTab(Pos, TabSize);
95
 
            if (Pos > ScreenPos)
96
 
                return Ofs;
97
 
            Ofs++;
98
 
            Len--;
99
 
        }
100
 
        return Ofs + ScreenPos - Pos;
 
84
 
 
85
    int Pos = 0;
 
86
    int Ofs = 0;
 
87
    char *p = L->Chars;
 
88
    int Len = L->Count;
 
89
 
 
90
    while (Len > 0) {
 
91
        if (*p++ != '\t')
 
92
            Pos++;
 
93
        else
 
94
            Pos = NextTab(Pos, TabSize);
 
95
        if (Pos > ScreenPos)
 
96
            return Ofs;
 
97
        Ofs++;
 
98
        Len--;
101
99
    }
 
100
 
 
101
    return Ofs + ScreenPos - Pos;
102
102
}
103
103
 
104
104
int EBuffer::Allocate(int ACount) {
105
105
    PELine *L;
106
106
 
107
 
    if (ACount > 1024 * 1024) // FIXME: - let's crash, before OOOM kills us
108
 
        return 0;
 
107
    if (ACount > 10 * 1024 * 1024) // FIXME: - let's crash, before OOOM kills us
 
108
        return 0;
109
109
 
110
110
    L = (PELine *) realloc(LL, sizeof(PELine) * (ACount + 1));
111
 
    if (L == 0 && ACount != 0)
 
111
    if (!L && ACount != 0)
112
112
        return 0;
113
113
    RAllocated = ACount;
114
114
    LL = L;
 
115
 
115
116
    return 1;
116
117
}
117
118
 
118
119
int EBuffer::MoveRGap(int RPos) {
119
120
    int GapSize = RAllocated - RCount;
120
 
    
121
 
    if (RGap == RPos) return 1;
122
 
    if (RPos < 0 || RPos > RCount) return 0;
123
 
    
 
121
 
 
122
    if (RGap == RPos)
 
123
        return 1;
 
124
    if (RPos < 0 || RPos > RCount)
 
125
        return 0;
 
126
 
124
127
    if (RGap < RPos) {
125
 
        if (RPos - RGap == 1) {
 
128
        if (RPos - RGap == 1)
126
129
            LL[RGap] = LL[RGap + GapSize];
127
 
        } else {
 
130
        else
128
131
            memmove(LL + RGap,
129
132
                LL + RGap + GapSize,
130
133
                sizeof(PELine) * (RPos - RGap));
131
 
        }
132
134
    } else {
133
 
        if (RGap - RPos == 1) {
 
135
        if (RGap - RPos == 1)
134
136
            LL[RPos + GapSize] = LL[RPos];
135
 
        } else {
 
137
        else
136
138
            memmove(LL + RPos + GapSize,
137
139
                    LL + RPos,
138
140
                    sizeof(PELine) * (RGap - RPos));
139
 
        }
140
141
    }
141
142
    RGap = RPos;
 
143
 
142
144
    return 1;
143
145
}
144
146
 
145
147
int EBuffer::AllocVis(int ACount) {
146
 
    int *V;
147
 
    
148
 
    V = (int *) realloc(VV, sizeof(int) * (ACount + 1));
149
 
    if (V == 0 && ACount != 0) return 0;
 
148
    int *V = (int *) realloc(VV, sizeof(int) * (ACount + 1));
 
149
    if (!V && ACount != 0)
 
150
        return 0;
150
151
    VAllocated = ACount;
151
152
    VV = V;
 
153
 
152
154
    return 1;
153
155
}
154
156
 
155
157
int EBuffer::MoveVGap(int VPos) {
156
158
    int GapSize = VAllocated - VCount;
157
 
    
 
159
 
158
160
    if (VGap == VPos) return 1;
159
161
    if (VPos < 0 || VPos > VCount) return 0;
160
 
    
 
162
 
161
163
    if (VGap < VPos) {
162
 
        if (VPos - VGap == 1) {
 
164
        if (VPos - VGap == 1)
163
165
            VV[VGap] = VV[VGap + GapSize];
164
 
        } else {
 
166
        else
165
167
            memmove(VV + VGap,
166
168
                    VV + VGap + GapSize,
167
169
                    sizeof(VV[0]) * (VPos - VGap));
168
 
        }
169
170
    } else {
170
171
        if (VGap - VPos == 1) {
171
172
            VV[VPos + GapSize] = VV[VPos];
181
182
 
182
183
int EBuffer::RToV(int No) {
183
184
    int L = 0, R = VCount, M, V;
184
 
    
 
185
 
185
186
    if (No > Vis(VCount - 1) + VCount - 1)   // beyond end
186
187
        return -1;
187
188
    if (No < VCount) // no folds before (direct match)
192
193
        V = Vis(M) + M;
193
194
        if (V == No)
194
195
            return M;
195
 
        else if (V > No)
 
196
        if (V > No)
196
197
            R = M;
197
198
        else
198
199
            L = M + 1;
202
203
 
203
204
int EBuffer::RToVN(int No) {
204
205
    int L = 0, R = VCount, M, V;
205
 
    
 
206
 
206
207
    if (No == RCount)
207
208
        return VCount;
208
 
    if (No > Vis(VCount - 1) + VCount - 1) 
 
209
    if (No > Vis(VCount - 1) + VCount - 1)
209
210
        return VCount - 1;
210
211
    if (No < VCount)
211
212
        if (Vis(No) == 0) return No;
212
 
    
 
213
 
213
214
    while (L < R) {
214
215
        M = (L + R) / 2;
215
216
        V = Vis(M) + M;