-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrow_shrink_integer.plugin.sl
102 lines (87 loc) · 3.12 KB
/
grow_shrink_integer.plugin.sl
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
% variable a; variable b = @a;
define grow_shrink_int__get_current_or_next_number() {
variable pos = -1, bol_offset, eol_offset, start_digit_offset,
t = "", found, start_digit_seen, c1, c2;
bol_offset = mc->cure_get_bol ();
eol_offset = mc->cure_get_eol ();
% Find the start of the number (a word, actually)
for (pos = mc->cure_cursor_offset(); pos >= bol_offset; pos--)
{
c1 = mc->cure_get_byte (pos);
c2 = mc->cure_get_byte (pos - 1);
if (not isdigit(c1))
break;
if (not isspace (c1) && isspace (c2))
break;
}
% Find the end of the number (a word, actually)
found=0;
start_digit_offset=0;
start_digit_seen=0;
for (; pos <= eol_offset; pos++)
{
c1 = mc->cure_get_byte (pos);
c2 = mc->cure_get_byte (pos + 1);
% Append the byte to the string
if (isdigit (c1)) {
if (not start_digit_seen) {
start_digit_seen=1;
start_digit_offset=pos;
}
found = 1; t += char(c1);
}
if (isdigit (c1) && not isdigit (c2)) {
found += 1;
break;
}
}
% Any number found?
% If not, return an empty string and minus one position
if (found == 2) {
% Include any minus sign
c1 = mc->cure_get_byte (start_digit_offset-1);
if (c1 == '-')
t = char(c1) + t;
}
return t, pos;
}
% A backend function for the two next public functions
define grow_shrink_int__increment(direction) {
variable pos, cpos, number = 0;
variable number_str, new_number_buf;
variable number_len = 0, new_number_len = 0, idx;
% Get the number
(number_str, pos) = grow_shrink_int__get_current_or_next_number();
% Has been a number found?
if (strlen(number_str) > 0) {
number_len = strlen(number_str);
% Convert the string into integer to increment it
number = atoll(number_str);
number += (direction > 0) ? 1 : -1;
new_number_buf = string(number);
new_number_len = strlen(new_number_buf);
% Move the cursor to the found number
cpos = mc->cure_cursor_offset();
mc->cure_cursor_move (pos-cpos);
% Delete the existing number
mc->cure_delete();
for (idx = 0; idx < number_len-1; idx ++)
mc->cure_backspace();
% Insert updated number
for (idx = 0; idx < new_number_len; idx ++)
mc->cure_insert_ahead(new_number_buf[new_number_len-idx-1]);
}
% Return the updated number. Just for fun :) Maybe it'll find some use one day
return number;
}
% The end user function for incrementing an integer
define grow_shrink_int__grow_int() {
return grow_shrink_int__increment(1);
}
% The end user function for taking 1 from an integer
define grow_shrink_int__shrink_int() {
return grow_shrink_int__increment(-1);
}
% Register the default key bindings – Alt-a for grow, Alt-x for shrink.
mc->editor_map_key_to_func("GrowInteger", "alt-a", "grow_shrink_int__grow_int");
mc->editor_map_key_to_func("ShrinkInteger", "alt-x", "grow_shrink_int__shrink_int");