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
|
/*
4coder_combined_write_commands.cpp - Commands for writing text specialized for particular contexts.
*/
// TOP
function void
write_string(Application_Links *app, View_ID view, Buffer_ID buffer, String_Const_u8 string){
i64 pos = view_get_cursor_pos(app, view);
buffer_replace_range(app, buffer, Ii64(pos), string);
view_set_cursor_and_preferred_x(app, view, seek_pos(pos + string.size));
}
function void
write_string(Application_Links *app, String_Const_u8 string){
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
write_string(app, view, buffer, string);
}
function void
write_named_comment_string(Application_Links *app, char *type_string){
Scratch_Block scratch(app);
String_Const_u8 name = def_get_config_string(scratch, vars_save_string_lit("user_name"));
String_Const_u8 str = {};
if (name.size > 0){
str = push_u8_stringf(scratch, "// %s(%.*s): ", type_string, string_expand(name));
}
else{
str = push_u8_stringf(scratch, "// %s: ", type_string);
}
write_string(app, str);
}
function void
long_braces(Application_Links *app, char *text, i32 size){
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
i64 pos = view_get_cursor_pos(app, view);
buffer_replace_range(app, buffer, Ii64(pos), SCu8(text, size));
view_set_cursor_and_preferred_x(app, view, seek_pos(pos + 2));
auto_indent_buffer(app, buffer, Ii64_size(pos, size));
move_past_lead_whitespace(app, view, buffer);
}
CUSTOM_COMMAND_SIG(open_long_braces)
CUSTOM_DOC("At the cursor, insert a '{' and '}' separated by a blank line.")
{
char text[] = "{\n\n}";
i32 size = sizeof(text) - 1;
long_braces(app, text, size);
}
CUSTOM_COMMAND_SIG(open_long_braces_semicolon)
CUSTOM_DOC("At the cursor, insert a '{' and '};' separated by a blank line.")
{
char text[] = "{\n\n};";
i32 size = sizeof(text) - 1;
long_braces(app, text, size);
}
CUSTOM_COMMAND_SIG(open_long_braces_break)
CUSTOM_DOC("At the cursor, insert a '{' and '}break;' separated by a blank line.")
{
char text[] = "{\n\n}break;";
i32 size = sizeof(text) - 1;
long_braces(app, text, size);
}
CUSTOM_COMMAND_SIG(if0_off)
CUSTOM_DOC("Surround the range between the cursor and mark with an '#if 0' and an '#endif'")
{
place_begin_and_end_on_own_lines(app, "#if 0", "#endif");
}
CUSTOM_COMMAND_SIG(write_todo)
CUSTOM_DOC("At the cursor, insert a '// TODO' comment, includes user name if it was specified in config.4coder.")
{
write_named_comment_string(app, "TODO");
}
CUSTOM_COMMAND_SIG(write_hack)
CUSTOM_DOC("At the cursor, insert a '// HACK' comment, includes user name if it was specified in config.4coder.")
{
write_named_comment_string(app, "HACK");
}
CUSTOM_COMMAND_SIG(write_note)
CUSTOM_DOC("At the cursor, insert a '// NOTE' comment, includes user name if it was specified in config.4coder.")
{
write_named_comment_string(app, "NOTE");
}
CUSTOM_COMMAND_SIG(write_block)
CUSTOM_DOC("At the cursor, insert a block comment.")
{
place_begin_and_end_on_own_lines(app, "/* ", " */");
}
CUSTOM_COMMAND_SIG(write_zero_struct)
CUSTOM_DOC("At the cursor, insert a ' = {};'.")
{
write_string(app, string_u8_litexpr(" = {};"));
}
function i64
get_start_of_line_at_cursor(Application_Links *app, View_ID view, Buffer_ID buffer){
i64 pos = view_get_cursor_pos(app, view);
i64 line = get_line_number_from_pos(app, buffer, pos);
return(get_pos_past_lead_whitespace_from_line_number(app, buffer, line));
}
function b32
c_line_comment_starts_at_position(Application_Links *app, Buffer_ID buffer, i64 pos){
b32 alread_has_comment = false;
u8 check_buffer[2];
if (buffer_read_range(app, buffer, Ii64(pos, pos + 2), check_buffer)){
if (check_buffer[0] == '/' && check_buffer[1] == '/'){
alread_has_comment = true;
}
}
return(alread_has_comment);
}
CUSTOM_COMMAND_SIG(comment_line)
CUSTOM_DOC("Insert '//' at the beginning of the line after leading whitespace.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
i64 pos = get_start_of_line_at_cursor(app, view, buffer);
b32 alread_has_comment = c_line_comment_starts_at_position(app, buffer, pos);
if (!alread_has_comment){
buffer_replace_range(app, buffer, Ii64(pos), string_u8_litexpr("//"));
}
}
CUSTOM_COMMAND_SIG(uncomment_line)
CUSTOM_DOC("If present, delete '//' at the beginning of the line after leading whitespace.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
i64 pos = get_start_of_line_at_cursor(app, view, buffer);
b32 alread_has_comment = c_line_comment_starts_at_position(app, buffer, pos);
if (alread_has_comment){
buffer_replace_range(app, buffer, Ii64(pos, pos + 2), string_u8_empty);
}
}
CUSTOM_COMMAND_SIG(comment_line_toggle)
CUSTOM_DOC("Turns uncommented lines into commented lines and vice versa for comments starting with '//'.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
i64 pos = get_start_of_line_at_cursor(app, view, buffer);
b32 alread_has_comment = c_line_comment_starts_at_position(app, buffer, pos);
if (alread_has_comment){
buffer_replace_range(app, buffer, Ii64(pos, pos + 2), string_u8_empty);
}
else{
buffer_replace_range(app, buffer, Ii64(pos), string_u8_litexpr("//"));
}
}
////////////////////////////////
static Snippet default_snippets[] = {
// general (for Allen's style)
{"if", "if (){\n\n}\n", 4, 7},
{"ifelse", "if (){\n\n}\nelse{\n\n}", 4, 7},
{"forn", "for (node = ;\nnode != 0;\nnode = node->next){\n\n}\n", 5, 38},
{"fori", "for (i = 0; i < ; i += 1){\n\n}\n", 5, 16},
{"forj", "for (j = 0; j < ; j += 1){\n\n}\n", 5, 16},
{"fork", "for (k = 0; k < ; k += 1){\n\n}\n", 5, 16},
{"for", "for (;;){\n\n}\n", 5, 10},
{"///", "////////////////////////////////", 32, 32},
{"#guard", "#if !defined(Z)\n#define Z\n#endif\n", 0, 26},
{"op+", "Z\noperator+(Z a, Z b){\n,\n}\n", 0, 23},
{"op-", "Z\noperator-(Z a, Z b){\n,\n}\n", 0, 23},
{"op*", "Z\noperator*(Z a, Z b){\n,\n}\n", 0, 23},
{"op/", "Z\noperator/(Z a, Z b){\n,\n}\n", 0, 23},
{"op+=", "Z&\noperator+=(Z &a, Z b){\n,\n}\n", 0, 26},
{"op-=", "Z&\noperator-=(Z &a, Z b){\n,\n}\n", 0, 26},
{"op*=", "Z&\noperator*=(Z &a, Z b){\n,\n}\n", 0, 26},
{"op/=", "Z&\noperator/=(Z &a, Z b){\n,\n}\n", 0, 26},
// for 4coder development
{"4command", "CUSTOM_COMMAND_SIG()\nCUSTOM_DOC()\n{\n\n}\n", 19, 32},
{"4app", "Application_Links *app", 22, 22},
#if defined(SNIPPET_EXPANSION)
#include SNIPPET_EXPANSION
#endif
};
function void
write_snippet(Application_Links *app, View_ID view, Buffer_ID buffer,
i64 pos, Snippet *snippet){
if (snippet != 0){
String_Const_u8 snippet_text = SCu8(snippet->text);
buffer_replace_range(app, buffer, Ii64(pos), snippet_text);
i64 new_cursor = pos + snippet->cursor_offset;
view_set_cursor_and_preferred_x(app, view, seek_pos(new_cursor));
i64 new_mark = pos + snippet->mark_offset;
view_set_mark(app, view, seek_pos(new_mark));
auto_indent_buffer(app, buffer, Ii64_size(pos, snippet_text.size));
}
}
function Snippet*
get_snippet_from_user(Application_Links *app, Snippet *snippets, i32 snippet_count,
String_Const_u8 query){
Scratch_Block scratch(app);
Lister_Block lister(app, scratch);
lister_set_query(lister, query);
lister_set_default_handlers(lister);
Snippet *snippet = snippets;
for (i32 i = 0; i < snippet_count; i += 1, snippet += 1){
lister_add_item(lister, SCu8(snippet->name), SCu8(snippet->text), snippet, 0);
}
Lister_Result l_result = run_lister(app, lister);
Snippet *result = 0;
if (!l_result.canceled){
result = (Snippet*)l_result.user_data;
}
return(result);
}
function Snippet*
get_snippet_from_user(Application_Links *app, Snippet *snippets, i32 snippet_count,
char *query){
return(get_snippet_from_user(app, snippets, snippet_count, SCu8(query)));
}
CUSTOM_UI_COMMAND_SIG(snippet_lister)
CUSTOM_DOC("Opens a snippet lister for inserting whole pre-written snippets of text.")
{
View_ID view = get_this_ctx_view(app, Access_ReadWrite);
if (view != 0){
Snippet *snippet = get_snippet_from_user(app, default_snippets,
ArrayCount(default_snippets),
"Snippet:");
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
i64 pos = view_get_cursor_pos(app, view);
write_snippet(app, view, buffer, pos, snippet);
}
}
// BOTTOM
|