-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathElasticTabstops.cpp
More file actions
326 lines (285 loc) · 8.1 KB
/
Copy pathElasticTabstops.cpp
File metadata and controls
326 lines (285 loc) · 8.1 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Port of elastic tabstops Gedit plugin to Scintilla
#define VC_EXTRALEAN
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#undef WINVER
#define WINVER 0x0501
#define NTDDI_VERSION 0x05010300
#include <afxwin.h>
#include <malloc.h>
#include <vector>
#include "Platform.h"
#include "Scintilla.h"
extern "C" sptr_t __stdcall Scintilla_DirectFunction(sptr_t, UINT, uptr_t, sptr_t);
LONG_PTR call_edit(sptr_t edit, UINT msg, DWORD wp = 0, LONG_PTR lp = 0)
{
return Scintilla_DirectFunction(edit, msg, wp, lp);
}
int get_line_start(sptr_t edit, int pos)
{
int line = call_edit(edit, SCI_LINEFROMPOSITION, pos);
return call_edit(edit, SCI_POSITIONFROMLINE, line);
}
int get_line_end(sptr_t edit, int pos)
{
int line = call_edit(edit, SCI_LINEFROMPOSITION, pos);
return call_edit(edit, SCI_GETLINEENDPOSITION, line);
}
bool is_line_end(sptr_t edit, int pos)
{
int line = call_edit(edit, SCI_LINEFROMPOSITION, pos);
int end_pos = call_edit(edit, SCI_GETLINEENDPOSITION, line);
return (pos == end_pos);
}
struct et_tabstop
{
int text_width_pix;
int *widest_width_pix;
bool ends_in_tab;
};
struct et_line
{
int num_tabs;
};
enum direction
{
BACKWARDS,
FORWARDS
};
et_tabstop* grid_buffer = NULL;
int grid_buffer_size = 0;
// by default, tabstops are at least 32 pixels plus 12 pixels of padding
#define MINIMUM_WIDTH_DEFAULT 32
#define PADDING_WIDTH_DEFAULT 12
int tab_width_minimum = MINIMUM_WIDTH_DEFAULT;
int tab_width_padding = PADDING_WIDTH_DEFAULT;
int get_text_width(sptr_t edit, int start, int end)
{
TextRange range;
range.chrg.cpMin = start;
range.chrg.cpMax = end;
range.lpstrText = (char*)_alloca(end-start + 1);
call_edit(edit, SCI_GETTEXTRANGE, 0, (sptr_t)&range);
LONG_PTR style = call_edit(edit, SCI_GETSTYLEAT, start);
return call_edit(edit, SCI_TEXTWIDTH, style, (LONG_PTR)range.lpstrText);
}
int calc_tab_width(int text_width_in_tab)
{
if (text_width_in_tab < tab_width_minimum)
{
text_width_in_tab = tab_width_minimum;
}
return text_width_in_tab + tab_width_padding;
}
bool change_line(sptr_t edit, int& location, direction which_dir)
{
int line = call_edit(edit, SCI_LINEFROMPOSITION, location);
if (which_dir == FORWARDS)
{
location = call_edit(edit, SCI_POSITIONFROMLINE, line + 1);
}
else
{
if (line <= 0)
{
return false;
}
location = call_edit(edit, SCI_POSITIONFROMLINE, line-1);
}
return (location >= 0);
}
int get_block_boundary(sptr_t edit, int& location, direction which_dir)
{
int current_pos;
int max_tabs = 0;
bool orig_line = true;
location = get_line_start(edit, location);
do
{
int tabs_on_line = 0;
current_pos = location;
unsigned char current_char = (unsigned char)call_edit(edit, SCI_GETCHARAT, current_pos);
bool current_char_ends_line = is_line_end(edit, current_pos);
while (current_char != '\0' && !current_char_ends_line)
{
if (current_char == '\t')
{
tabs_on_line++;
if (tabs_on_line > max_tabs)
{
max_tabs = tabs_on_line;
}
}
current_pos = call_edit(edit, SCI_POSITIONAFTER, current_pos);
current_char = (unsigned char)call_edit(edit, SCI_GETCHARAT, current_pos);
current_char_ends_line = is_line_end(edit, current_pos);
}
if (tabs_on_line == 0 && !orig_line)
{
return max_tabs;
}
orig_line = false;
}
while (change_line(edit, location, which_dir));
return max_tabs;
}
int get_nof_tabs_between(sptr_t edit, int start, int end)
{
int current_pos = get_line_start(edit, start);
int max_tabs = 0;
do
{
unsigned char current_char = (unsigned char)call_edit(edit, SCI_GETCHARAT, current_pos);
bool current_char_ends_line = is_line_end(edit, current_pos);
int tabs_on_line = 0;
while (current_char != '\0' && !current_char_ends_line)
{
if (current_char == '\t')
{
tabs_on_line++;
if (tabs_on_line > max_tabs)
{
max_tabs = tabs_on_line;
}
}
current_pos = call_edit(edit, SCI_POSITIONAFTER, current_pos);
current_char = (unsigned char)call_edit(edit, SCI_GETCHARAT, current_pos);
current_char_ends_line = is_line_end(edit, current_pos);
}
}
while (change_line(edit, current_pos, FORWARDS) && current_pos < end);
return max_tabs;
}
void stretch_tabstops(sptr_t edit, int block_start_linenum, int block_nof_lines, int max_tabs)
{
int l, t;
et_line* lines = (et_line*)_alloca(sizeof (et_line) * block_nof_lines);
memset(lines, 0, sizeof (et_line) * block_nof_lines);
int new_buffer_size = sizeof (et_tabstop) * __max(1, block_nof_lines * max_tabs);
if (new_buffer_size > grid_buffer_size)
{
et_tabstop* new_buffer = (et_tabstop*)realloc(grid_buffer, new_buffer_size);
if (new_buffer == NULL)
{
free(grid_buffer);
return;
}
grid_buffer = new_buffer;
grid_buffer_size = new_buffer_size;
}
memset(grid_buffer, 0, new_buffer_size);
et_tabstop** grid = (et_tabstop**)_alloca(sizeof (et_tabstop*) * block_nof_lines);
for (l = 0; l < block_nof_lines; l++)
{
grid[l] = grid_buffer + (l * max_tabs);
}
// get width of text in cells
for (l = 0; l < block_nof_lines; l++) // for each line
{
int text_width_in_tab = 0;
int current_line_num = block_start_linenum + l;
int current_tab_num = 0;
bool cell_empty = true;
int current_pos = call_edit(edit, SCI_POSITIONFROMLINE, current_line_num);
int cell_start = current_pos;
unsigned char current_char = (unsigned char)call_edit(edit, SCI_GETCHARAT, current_pos);
bool current_char_ends_line = is_line_end(edit, current_pos);
// maybe change this to search forwards for tabs/newlines
while (current_char != '\0')
{
if (current_char_ends_line)
{
grid[l][current_tab_num].ends_in_tab = false;
text_width_in_tab = 0;
break;
}
else if (current_char == '\t')
{
if (!cell_empty)
{
text_width_in_tab = get_text_width(edit, cell_start, current_pos);
}
grid[l][current_tab_num].ends_in_tab = true;
grid[l][current_tab_num].text_width_pix = calc_tab_width(text_width_in_tab);
current_tab_num++;
lines[l].num_tabs++;
text_width_in_tab = 0;
cell_empty = true;
}
else
{
if (cell_empty)
{
cell_start = current_pos;
cell_empty = false;
}
}
current_pos = call_edit(edit, SCI_POSITIONAFTER, current_pos);
current_char = (unsigned char)call_edit(edit, SCI_GETCHARAT, current_pos);
current_char_ends_line = is_line_end(edit, current_pos);
}
}
// find columns blocks and stretch to fit the widest cell
for (t = 0; t < max_tabs; t++) // for each column
{
bool starting_new_block = true;
int first_line_in_block = 0;
int max_width = 0;
for (l = 0; l < block_nof_lines; l++) // for each line
{
if (starting_new_block)
{
starting_new_block = false;
first_line_in_block = l;
max_width = 0;
}
if (grid[l][t].ends_in_tab)
{
grid[l][t].widest_width_pix = &(grid[first_line_in_block][t].text_width_pix); // point widestWidthPix at first
if (grid[l][t].text_width_pix > max_width)
{
max_width = grid[l][t].text_width_pix;
grid[first_line_in_block][t].text_width_pix = max_width;
}
}
else // end column block
{
starting_new_block = true;
}
}
}
// set tabstops
for (l = 0; l < block_nof_lines; l++) // for each line
{
int current_line_num = block_start_linenum + l;
int acc_tabstop = 0;
call_edit(edit, SCI_CLEARTABSTOPS, current_line_num);
for (t = 0; t < lines[l].num_tabs; t++)
{
if (grid[l][t].widest_width_pix != NULL)
{
acc_tabstop += *(grid[l][t].widest_width_pix);
call_edit(edit, SCI_ADDTABSTOP, current_line_num, acc_tabstop);
}
else
{
break;
}
}
}
}
void ElasticTabstops_OnModify(sptr_t edit, int start, int end)
{
int max_tabs_between = get_nof_tabs_between(edit, start, end);
int max_tabs_backwards = get_block_boundary(edit, start, BACKWARDS);
int max_tabs_forwards = get_block_boundary(edit, end, FORWARDS);
int max_tabs = __max(__max(max_tabs_between, max_tabs_backwards), max_tabs_forwards);
int block_start_linenum = call_edit(edit, SCI_LINEFROMPOSITION, start);
int block_end_linenum = call_edit(edit, SCI_LINEFROMPOSITION, end);
int block_nof_lines = (block_end_linenum - block_start_linenum) + 1;
stretch_tabstops(edit, block_start_linenum, block_nof_lines, max_tabs);
}
void exit_cleanly()
{
free(grid_buffer);
}