~ubuntu-branches/ubuntu/wily/geany/wily

« back to all changes in this revision

Viewing changes to scintilla/src/RunStyles.cxx

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2011-12-10 07:43:26 UTC
  • mfrom: (3.3.7 sid)
  • Revision ID: package-import@ubuntu.com-20111210074326-s8yqbew5i20h33tf
Tags: 0.21-1ubuntu1
* Merge from Debian Unstable, remaining changes:
  - debian/patches/20_use_evince_viewer.patch:
     + use evince as viewer for pdf and dvi files
  - debian/patches/20_use_x_terminal_emulator.patch:
     + use x-terminal-emulator as terminal
  - debian/control
     + Add breaks on geany-plugins-common << 0.20
* Also fixes bugs:
  - Filter for MATLAB/Octave files filters everythign (LP: 885505)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file RunStyles.cxx
 
2
 ** Data structure used to store sparse styles.
 
3
 **/
 
4
// Copyright 1998-2007 by Neil Hodgson <neilh@scintilla.org>
 
5
// The License.txt file describes the conditions under which this software may be distributed.
 
6
 
 
7
#include <stdio.h>
 
8
#include <string.h>
 
9
#include <stdlib.h>
 
10
#include <stdarg.h>
 
11
 
 
12
#include "Platform.h"
 
13
 
 
14
#include "Scintilla.h"
 
15
#include "SplitVector.h"
 
16
#include "Partitioning.h"
 
17
#include "RunStyles.h"
 
18
 
 
19
#ifdef SCI_NAMESPACE
 
20
using namespace Scintilla;
 
21
#endif
 
22
 
 
23
// Find the first run at a position
 
24
int RunStyles::RunFromPosition(int position) {
 
25
        int run = starts->PartitionFromPosition(position);
 
26
        // Go to first element with this position
 
27
        while ((run > 0) && (position == starts->PositionFromPartition(run-1))) {
 
28
                run--;
 
29
        }
 
30
        return run;
 
31
}
 
32
 
 
33
// If there is no run boundary at position, insert one continuing style.
 
34
int RunStyles::SplitRun(int position) {
 
35
        int run = RunFromPosition(position);
 
36
        int posRun = starts->PositionFromPartition(run);
 
37
        if (posRun < position) {
 
38
                int runStyle = ValueAt(position);
 
39
                run++;
 
40
                starts->InsertPartition(run, position);
 
41
                styles->InsertValue(run, 1, runStyle);
 
42
        }
 
43
        return run;
 
44
}
 
45
 
 
46
void RunStyles::RemoveRun(int run) {
 
47
        starts->RemovePartition(run);
 
48
        styles->DeleteRange(run, 1);
 
49
}
 
50
 
 
51
void RunStyles::RemoveRunIfEmpty(int run) {
 
52
        if ((run < starts->Partitions()) && (starts->Partitions() > 1)) {
 
53
                if (starts->PositionFromPartition(run) == starts->PositionFromPartition(run+1)) {
 
54
                        RemoveRun(run);
 
55
                }
 
56
        }
 
57
}
 
58
 
 
59
void RunStyles::RemoveRunIfSameAsPrevious(int run) {
 
60
        if ((run > 0) && (run < starts->Partitions())) {
 
61
                if (styles->ValueAt(run-1) == styles->ValueAt(run)) {
 
62
                        RemoveRun(run);
 
63
                }
 
64
        }
 
65
}
 
66
 
 
67
RunStyles::RunStyles() {
 
68
        starts = new Partitioning(8);
 
69
        styles = new SplitVector<int>();
 
70
        styles->InsertValue(0, 2, 0);
 
71
}
 
72
 
 
73
RunStyles::~RunStyles() {
 
74
        delete starts;
 
75
        starts = NULL;
 
76
        delete styles;
 
77
        styles = NULL;
 
78
}
 
79
 
 
80
int RunStyles::Length() const {
 
81
        return starts->PositionFromPartition(starts->Partitions());
 
82
}
 
83
 
 
84
int RunStyles::ValueAt(int position) const {
 
85
        return styles->ValueAt(starts->PartitionFromPosition(position));
 
86
}
 
87
 
 
88
int RunStyles::FindNextChange(int position, int end) {
 
89
        int run = starts->PartitionFromPosition(position);
 
90
        if (run < starts->Partitions()) {
 
91
                int runChange = starts->PositionFromPartition(run);
 
92
                if (runChange > position)
 
93
                        return runChange;
 
94
                int nextChange = starts->PositionFromPartition(run + 1);
 
95
                if (nextChange > position) {
 
96
                        return nextChange;
 
97
                } else if (position < end) {
 
98
                        return end;
 
99
                } else {
 
100
                        return end + 1;
 
101
                }
 
102
        } else {
 
103
                return end + 1;
 
104
        }
 
105
}
 
106
 
 
107
int RunStyles::StartRun(int position) {
 
108
        return starts->PositionFromPartition(starts->PartitionFromPosition(position));
 
109
}
 
110
 
 
111
int RunStyles::EndRun(int position) {
 
112
        return starts->PositionFromPartition(starts->PartitionFromPosition(position) + 1);
 
113
}
 
114
 
 
115
bool RunStyles::FillRange(int &position, int value, int &fillLength) {
 
116
        int end = position + fillLength;
 
117
        int runEnd = RunFromPosition(end);
 
118
        if (styles->ValueAt(runEnd) == value) {
 
119
                // End already has value so trim range.
 
120
                end = starts->PositionFromPartition(runEnd);
 
121
                if (position >= end) {
 
122
                        // Whole range is already same as value so no action
 
123
                        return false;
 
124
                }
 
125
                fillLength = end - position;
 
126
        } else {
 
127
                runEnd = SplitRun(end);
 
128
        }
 
129
        int runStart = RunFromPosition(position);
 
130
        if (styles->ValueAt(runStart) == value) {
 
131
                // Start is in expected value so trim range.
 
132
                runStart++;
 
133
                position = starts->PositionFromPartition(runStart);
 
134
                fillLength = end - position;
 
135
        } else {
 
136
                if (starts->PositionFromPartition(runStart) < position) {
 
137
                        runStart = SplitRun(position);
 
138
                        runEnd++;
 
139
                }
 
140
        }
 
141
        if (runStart < runEnd) {
 
142
                styles->SetValueAt(runStart, value);
 
143
                // Remove each old run over the range
 
144
                for (int run=runStart+1; run<runEnd; run++) {
 
145
                        RemoveRun(runStart+1);
 
146
                }
 
147
                runEnd = RunFromPosition(end);
 
148
                RemoveRunIfSameAsPrevious(runEnd);
 
149
                RemoveRunIfSameAsPrevious(runStart);
 
150
                return true;
 
151
        } else {
 
152
                return false;
 
153
        }
 
154
}
 
155
 
 
156
void RunStyles::SetValueAt(int position, int value) {
 
157
        int len = 1;
 
158
        FillRange(position, value, len);
 
159
}
 
160
 
 
161
void RunStyles::InsertSpace(int position, int insertLength) {
 
162
        int runStart = RunFromPosition(position);
 
163
        if (starts->PositionFromPartition(runStart) == position) {
 
164
                int runStyle = ValueAt(position);
 
165
                // Inserting at start of run so make previous longer
 
166
                if (runStart == 0) {
 
167
                        // Inserting at start of document so ensure 0
 
168
                        if (runStyle) {
 
169
                                styles->SetValueAt(0, 0);
 
170
                                starts->InsertPartition(1, 0);
 
171
                                styles->InsertValue(1, 1, runStyle);
 
172
                                starts->InsertText(0, insertLength);
 
173
                        } else {
 
174
                                starts->InsertText(runStart, insertLength);
 
175
                        }
 
176
                } else {
 
177
                        if (runStyle) {
 
178
                                starts->InsertText(runStart-1, insertLength);
 
179
                        } else {
 
180
                                // Insert at end of run so do not extend style
 
181
                                starts->InsertText(runStart, insertLength);
 
182
                        }
 
183
                }
 
184
        } else {
 
185
                starts->InsertText(runStart, insertLength);
 
186
        }
 
187
}
 
188
 
 
189
void RunStyles::DeleteAll() {
 
190
        delete starts;
 
191
        starts = NULL;
 
192
        delete styles;
 
193
        styles = NULL;
 
194
        starts = new Partitioning(8);
 
195
        styles = new SplitVector<int>();
 
196
        styles->InsertValue(0, 2, 0);
 
197
}
 
198
 
 
199
void RunStyles::DeleteRange(int position, int deleteLength) {
 
200
        int end = position + deleteLength;
 
201
        int runStart = RunFromPosition(position);
 
202
        int runEnd = RunFromPosition(end);
 
203
        if (runStart == runEnd) {
 
204
                // Deleting from inside one run
 
205
                starts->InsertText(runStart, -deleteLength);
 
206
        } else {
 
207
                runStart = SplitRun(position);
 
208
                runEnd = SplitRun(end);
 
209
                starts->InsertText(runStart, -deleteLength);
 
210
                // Remove each old run over the range
 
211
                for (int run=runStart; run<runEnd; run++) {
 
212
                        RemoveRun(runStart);
 
213
                }
 
214
                RemoveRunIfEmpty(runStart);
 
215
                RemoveRunIfSameAsPrevious(runStart);
 
216
        }
 
217
}
 
218