2018-05-11 20:46:26 +00:00
|
|
|
/*
|
2018-09-17 18:47:06 +00:00
|
|
|
4coder_combined_write_commands.cpp - Commands for writing text specialized for particular contexts.
|
2018-05-11 20:46:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
|
|
|
static void
|
2019-06-01 23:58:28 +00:00
|
|
|
write_string(Application_Links *app, View_ID view, Buffer_ID buffer, String_Const_u8 string){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = view_get_cursor_pos(app, view);
|
|
|
|
buffer_replace_range(app, buffer, Ii64(pos), string);
|
2019-09-02 21:32:52 +00:00
|
|
|
view_set_cursor_and_preferred_x(app, view, seek_pos(pos + string.size));
|
2018-05-11 20:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-06-01 23:58:28 +00:00
|
|
|
write_string(Application_Links *app, String_Const_u8 string){
|
2019-10-18 02:54:02 +00:00
|
|
|
View_ID view = get_active_view(app, Access_ReadWriteVisible);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
|
2019-04-06 21:13:49 +00:00
|
|
|
write_string(app, view, buffer, string);
|
2018-05-11 20:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
write_named_comment_string(Application_Links *app, char *type_string){
|
2019-06-01 23:58:28 +00:00
|
|
|
Scratch_Block scratch(app);
|
|
|
|
String_Const_u8 name = global_config.user_name;
|
|
|
|
String_Const_u8 str = {};
|
2018-05-12 00:53:02 +00:00
|
|
|
if (name.size > 0){
|
2019-06-18 22:56:09 +00:00
|
|
|
str = push_u8_stringf(scratch, "// %s(%.*s): ", type_string, string_expand(name));
|
2018-09-04 17:59:53 +00:00
|
|
|
}
|
2018-05-11 20:46:26 +00:00
|
|
|
else{
|
2019-06-18 22:56:09 +00:00
|
|
|
str = push_u8_stringf(scratch, "// %s: ", type_string);
|
2018-05-11 20:46:26 +00:00
|
|
|
}
|
|
|
|
write_string(app, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-26 23:17:53 +00:00
|
|
|
long_braces(Application_Links *app, char *text, i32 size){
|
2019-10-18 02:54:02 +00:00
|
|
|
View_ID view = get_active_view(app, Access_ReadWriteVisible);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = view_get_cursor_pos(app, view);
|
|
|
|
buffer_replace_range(app, buffer, Ii64(pos), SCu8(text, size));
|
2019-09-02 21:32:52 +00:00
|
|
|
view_set_cursor_and_preferred_x(app, view, seek_pos(pos + 2));
|
2019-10-03 20:54:13 +00:00
|
|
|
auto_indent_buffer(app, buffer, Ii64_size(pos, size));
|
2019-04-06 21:13:49 +00:00
|
|
|
move_past_lead_whitespace(app, view, buffer);
|
2018-05-11 20:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(open_long_braces)
|
|
|
|
CUSTOM_DOC("At the cursor, insert a '{' and '}' separated by a blank line.")
|
|
|
|
{
|
|
|
|
char text[] = "{\n\n}";
|
2019-02-26 23:17:53 +00:00
|
|
|
i32 size = sizeof(text) - 1;
|
2018-05-11 20:46:26 +00:00
|
|
|
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};";
|
2019-02-26 23:17:53 +00:00
|
|
|
i32 size = sizeof(text) - 1;
|
2018-05-11 20:46:26 +00:00
|
|
|
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;";
|
2019-02-26 23:17:53 +00:00
|
|
|
i32 size = sizeof(text) - 1;
|
2018-05-11 20:46:26 +00:00
|
|
|
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'")
|
|
|
|
{
|
2019-06-01 23:58:28 +00:00
|
|
|
place_begin_and_end_on_own_lines(app, "#if 0", "#endif");
|
2018-05-11 20:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.")
|
|
|
|
{
|
2019-06-01 23:58:28 +00:00
|
|
|
write_string(app, string_u8_litexpr("/* */"));
|
2018-05-11 20:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(write_zero_struct)
|
2018-10-09 23:51:45 +00:00
|
|
|
CUSTOM_DOC("At the cursor, insert a ' = {};'.")
|
2018-05-11 20:46:26 +00:00
|
|
|
{
|
2019-06-01 23:58:28 +00:00
|
|
|
write_string(app, string_u8_litexpr(" = {};"));
|
2018-05-11 20:46:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 04:22:06 +00:00
|
|
|
function i64
|
2019-04-06 21:13:49 +00:00
|
|
|
get_start_of_line_at_cursor(Application_Links *app, View_ID view, Buffer_ID buffer){
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = view_get_cursor_pos(app, view);
|
|
|
|
i64 line = get_line_number_from_pos(app, buffer, pos);
|
2019-07-27 02:31:01 +00:00
|
|
|
return(get_pos_past_lead_whitespace_from_line_number(app, buffer, line));
|
2018-12-18 05:14:56 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 04:22:06 +00:00
|
|
|
function b32
|
2019-06-20 23:43:27 +00:00
|
|
|
c_line_comment_starts_at_position(Application_Links *app, Buffer_ID buffer, i64 pos){
|
2019-02-26 23:08:42 +00:00
|
|
|
b32 alread_has_comment = false;
|
2019-10-17 04:22:06 +00:00
|
|
|
u8 check_buffer[2];
|
2019-06-20 23:43:27 +00:00
|
|
|
if (buffer_read_range(app, buffer, Ii64(pos, pos + 2), check_buffer)){
|
2018-12-18 05:14:56 +00:00
|
|
|
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.")
|
|
|
|
{
|
2019-10-18 02:54:02 +00:00
|
|
|
View_ID view = get_active_view(app, Access_ReadWriteVisible);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = get_start_of_line_at_cursor(app, view, buffer);
|
2019-04-04 08:25:16 +00:00
|
|
|
b32 alread_has_comment = c_line_comment_starts_at_position(app, buffer, pos);
|
2018-12-18 05:14:56 +00:00
|
|
|
if (!alread_has_comment){
|
2019-06-20 23:43:27 +00:00
|
|
|
buffer_replace_range(app, buffer, Ii64(pos), string_u8_litexpr("//"));
|
2018-12-18 05:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(uncomment_line)
|
|
|
|
CUSTOM_DOC("If present, delete '//' at the beginning of the line after leading whitespace.")
|
|
|
|
{
|
2019-10-18 02:54:02 +00:00
|
|
|
View_ID view = get_active_view(app, Access_ReadWriteVisible);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = get_start_of_line_at_cursor(app, view, buffer);
|
2019-04-04 08:25:16 +00:00
|
|
|
b32 alread_has_comment = c_line_comment_starts_at_position(app, buffer, pos);
|
2018-12-18 05:14:56 +00:00
|
|
|
if (alread_has_comment){
|
2019-06-20 23:43:27 +00:00
|
|
|
buffer_replace_range(app, buffer, Ii64(pos, pos + 2), string_u8_empty);
|
2018-12-18 05:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(comment_line_toggle)
|
|
|
|
CUSTOM_DOC("Turns uncommented lines into commented lines and vice versa for comments starting with '//'.")
|
|
|
|
{
|
2019-10-18 02:54:02 +00:00
|
|
|
View_ID view = get_active_view(app, Access_ReadWriteVisible);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = get_start_of_line_at_cursor(app, view, buffer);
|
2019-04-04 08:25:16 +00:00
|
|
|
b32 alread_has_comment = c_line_comment_starts_at_position(app, buffer, pos);
|
2018-12-18 05:14:56 +00:00
|
|
|
if (alread_has_comment){
|
2019-06-20 23:43:27 +00:00
|
|
|
buffer_replace_range(app, buffer, Ii64(pos, pos + 2), string_u8_empty);
|
2018-12-18 05:14:56 +00:00
|
|
|
}
|
|
|
|
else{
|
2019-06-20 23:43:27 +00:00
|
|
|
buffer_replace_range(app, buffer, Ii64(pos), string_u8_litexpr("//"));
|
2018-12-18 05:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 18:47:06 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2018-09-17 21:01:15 +00:00
|
|
|
static Snippet default_snippets[] = {
|
2018-09-17 18:47:06 +00:00
|
|
|
// general (for Allen's style)
|
|
|
|
{"if", "if (){\n\n}\n", 4, 7},
|
|
|
|
{"ifelse", "if (){\n\n}\nelse{\n\n}", 4, 7},
|
2019-07-31 20:43:27 +00:00
|
|
|
{"forn", "for (node = ;\nnode != 0;\nnode = node->next){\n\n}\n", 5, 38},
|
2018-09-17 18:47:06 +00:00
|
|
|
{"fori", "for (i = 0; i < ; i += 1){\n\n}\n", 5, 16},
|
2019-07-31 20:43:27 +00:00
|
|
|
{"forj", "for (j = 0; j < ; j += 1){\n\n}\n", 5, 16},
|
|
|
|
{"fork", "for (k = 0; k < ; k += 1){\n\n}\n", 5, 16},
|
2018-09-17 18:47:06 +00:00
|
|
|
{"for", "for (;;){\n\n}\n", 5, 10},
|
|
|
|
{"case", "case :\n{\n\n}break;\n", 5, 9},
|
|
|
|
{"///", "////////////////////////////////", 32, 32},
|
|
|
|
{"#guard", "#if !defined(Z)\n#define Z\n#endif\n", 0, 26},
|
|
|
|
{"space", "char space[256];", 0, 14},
|
|
|
|
|
|
|
|
{"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
|
|
|
|
};
|
|
|
|
|
2019-10-25 23:33:50 +00:00
|
|
|
function void
|
|
|
|
write_snippet(Application_Links *app, View_ID view, Buffer_ID buffer,
|
|
|
|
i64 pos, Snippet *snippet){
|
|
|
|
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, Scratch_Share);
|
|
|
|
Lister *lister = begin_lister(app, scratch);
|
|
|
|
lister_set_query(lister, query);
|
|
|
|
lister->handlers = lister_get_default_handlers();
|
|
|
|
|
|
|
|
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);
|
2018-09-17 18:47:06 +00:00
|
|
|
}
|
2019-10-25 23:33:50 +00:00
|
|
|
Lister_Result l_result = run_lister(app, lister);
|
|
|
|
Snippet *result = 0;
|
|
|
|
if (!l_result.canceled){
|
|
|
|
result = (Snippet*)l_result.user_data;
|
2018-09-17 18:47:06 +00:00
|
|
|
}
|
2019-10-25 23:33:50 +00:00
|
|
|
return(result);
|
2018-09-17 18:47:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 23:33:50 +00:00
|
|
|
|
|
|
|
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)));
|
2018-09-17 18:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(snippet_lister)
|
|
|
|
CUSTOM_DOC("Opens a snippet lister for inserting whole pre-written snippets of text.")
|
|
|
|
{
|
2019-10-25 23:33:50 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-09-17 18:47:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 20:46:26 +00:00
|
|
|
// BOTTOM
|
|
|
|
|