Simplifying and organizing line commands

This commit is contained in:
Allen Webster 2019-06-14 15:01:50 -07:00
parent 0a9bfc8b84
commit 797fb42749
3 changed files with 56 additions and 79 deletions

View File

@ -1602,16 +1602,29 @@ CUSTOM_DOC("Queries the user for a name and creates a new directory with the giv
////////////////////////////////
internal void
current_view_move_line(Application_Links *app, Scan_Direction direction){
View_ID view = 0;
get_active_view(app, AccessOpen, &view);
Buffer_ID buffer = 0;
view_get_buffer(app, view, AccessOpen, &buffer);
i32 pos = 0;
view_get_cursor_pos(app, view, &pos);
i32 line_number = get_line_number_from_pos(app, buffer, pos);
pos = move_line(app, buffer, line_number, direction);
view_set_cursor(app, view, seek_pos(pos), true);
}
CUSTOM_COMMAND_SIG(move_line_up)
CUSTOM_DOC("Swaps the line under the cursor with the line above it, and moves the cursor up with it.")
{
move_line_current_view(app, Scan_Backward);
current_view_move_line(app, Scan_Backward);
}
CUSTOM_COMMAND_SIG(move_line_down)
CUSTOM_DOC("Swaps the line under the cursor with the line below it, and moves the cursor down with it.")
{
move_line_current_view(app, Scan_Forward);
current_view_move_line(app, Scan_Forward);
}
CUSTOM_COMMAND_SIG(duplicate_line)
@ -1619,17 +1632,17 @@ CUSTOM_DOC("Create a copy of the line on which the cursor sits.")
{
View_ID view = 0;
get_active_view(app, AccessOpen, &view);
Buffer_ID buffer_id = 0;
view_get_buffer(app, view, AccessOpen, &buffer_id);
i32 cursor_pos = 0;
view_get_cursor_pos(app, view, &cursor_pos);
Full_Cursor cursor = {};
view_compute_cursor(app, view, seek_pos(cursor_pos), &cursor);
Buffer_ID buffer = 0;
view_get_buffer(app, view, AccessOpen, &buffer);
i32 pos = 0;
view_get_cursor_pos(app, view, &pos);
i32 line = get_line_number_from_pos(app, buffer, pos);
Scratch_Block scratch(app);
String_Const_u8 line_string = push_buffer_line(app, scratch, buffer_id, cursor.line);
String_Const_u8 insertion = string_u8_pushf(scratch, "\n%.*s", string_expand(line_string));
i32 pos = get_line_end_pos(app, buffer_id, cursor.line);
buffer_replace_range(app, buffer_id, make_range(pos), insertion);
String_Const_u8 s = push_buffer_line(app, scratch, buffer, line);
s = string_u8_pushf(scratch, "\n%.*s", string_expand(s));
pos = get_line_side_pos(app, buffer, line, Side_Max);
buffer_replace_range(app, buffer, make_range(pos), s);
view_set_cursor(app, view, seek_pos(pos + 1), true);
}
CUSTOM_COMMAND_SIG(delete_line)
@ -1639,22 +1652,19 @@ CUSTOM_DOC("Delete the line the on which the cursor sits.")
get_active_view(app, AccessOpen, &view);
Buffer_ID buffer = 0;
view_get_buffer(app, view, AccessOpen, &buffer);
i32 cursor_pos = 0;
view_get_cursor_pos(app, view, &cursor_pos);
Full_Cursor cursor = {};
view_compute_cursor(app, view, seek_pos(cursor_pos), &cursor);
Range range = get_line_pos_range(app, buffer, cursor.line);
range.one_past_last += 1;
i32 buffer_size = 0;
buffer_get_size(app, buffer, &buffer_size);
range.one_past_last = clamp_top(range.one_past_last, buffer_size);
if (range_size(range) == 0 || buffer_get_char(app, buffer, range.end - 1) != '\n'){
i32 pos = 0;
view_get_cursor_pos(app, view, &pos);
i32 line = get_line_number_from_pos(app, buffer, pos);
Range range = get_line_pos_range(app, buffer, line);
range.end += 1;
i32 size = 0;
buffer_get_size(app, buffer, &size);
range.end = clamp_top(range.end, size);
if (range_size(range) == 0 ||
buffer_get_char(app, buffer, range.end - 1) != '\n'){
range.start -= 1;
range.first = clamp_bot(0, range.first);
}
buffer_replace_range(app, buffer, range, string_u8_litexpr(""));
}
@ -1699,26 +1709,6 @@ get_cpp_matching_file(Application_Links *app, Buffer_ID buffer, Buffer_ID *buffe
}
end_temp(temp);
}
#if 0
char *space = push_array(scratch, char, file_name.size + 16);
String file_name_old = make_string_cap(space, 0, (i32)file_name.size + 16);
append(&file_name_old, string_old_from_new(file_name));
remove_extension(&file_name_old);
i32 base_pos = file_name_old.size;
for (i32 i = 0; i < new_extensions_count; ++i){
String ext = string_old_from_new(new_extensions[i]);
file_name.size = base_pos;
append(&file_name_old, ext);
if (open_file(app, buffer_out, file_name_old.str, file_name_old.size, false, true)){
result = true;
break;
}
}
#endif
}
return(result);

View File

@ -355,22 +355,22 @@ static Command_Metadata fcoder_metacmd_table[234] = {
{ PROC_LINKS(save_to_query, 0), "save_to_query", 13, "Queries the user for a file name and saves the contents of the current buffer, altering the buffer's name too.", 110, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1501 },
{ PROC_LINKS(rename_file_query, 0), "rename_file_query", 17, "Queries the user for a new name and renames the file of the current buffer, altering the buffer's name too.", 107, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1539 },
{ PROC_LINKS(make_directory_query, 0), "make_directory_query", 20, "Queries the user for a name and creates a new directory with the given name.", 76, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1582 },
{ PROC_LINKS(move_line_up, 0), "move_line_up", 12, "Swaps the line under the cursor with the line above it, and moves the cursor up with it.", 88, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1605 },
{ PROC_LINKS(move_line_down, 0), "move_line_down", 14, "Swaps the line under the cursor with the line below it, and moves the cursor down with it.", 90, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1611 },
{ PROC_LINKS(duplicate_line, 0), "duplicate_line", 14, "Create a copy of the line on which the cursor sits.", 51, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1617 },
{ PROC_LINKS(delete_line, 0), "delete_line", 11, "Delete the line the on which the cursor sits.", 45, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1635 },
{ PROC_LINKS(open_file_in_quotes, 0), "open_file_in_quotes", 19, "Reads a filename from surrounding '\"' characters and attempts to open the corresponding file.", 94, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1727 },
{ PROC_LINKS(open_matching_file_cpp, 0), "open_matching_file_cpp", 22, "If the current file is a *.cpp or *.h, attempts to open the corresponding *.h or *.cpp file in the other view.", 110, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1765 },
{ PROC_LINKS(view_buffer_other_panel, 0), "view_buffer_other_panel", 23, "Set the other non-active panel to view the buffer that the active panel views, and switch to that panel.", 104, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1780 },
{ PROC_LINKS(swap_buffers_between_panels, 0), "swap_buffers_between_panels", 27, "Set the other non-active panel to view the buffer that the active panel views, and switch to that panel.", 104, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1795 },
{ PROC_LINKS(kill_buffer, 0), "kill_buffer", 11, "Kills the current buffer.", 25, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1840 },
{ PROC_LINKS(save, 0), "save", 4, "Saves the current buffer.", 25, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1850 },
{ PROC_LINKS(reopen, 0), "reopen", 6, "Reopen the current buffer from the hard drive.", 46, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1864 },
{ PROC_LINKS(undo, 0), "undo", 4, "Advances backwards through the undo history of the current buffer.", 66, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1928 },
{ PROC_LINKS(redo, 0), "redo", 4, "Advances forwards through the undo history of the current buffer.", 65, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1944 },
{ PROC_LINKS(undo_all_buffers, 0), "undo_all_buffers", 16, "Advances backward through the undo history in the buffer containing the most recent regular edit.", 97, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1962 },
{ PROC_LINKS(redo_all_buffers, 0), "redo_all_buffers", 16, "Advances forward through the undo history in the buffer containing the most recent regular edit.", 96, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 2041 },
{ PROC_LINKS(open_in_other, 0), "open_in_other", 13, "Interactively opens a file in the other panel.", 46, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 2151 },
{ PROC_LINKS(move_line_up, 0), "move_line_up", 12, "Swaps the line under the cursor with the line above it, and moves the cursor up with it.", 88, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1618 },
{ PROC_LINKS(move_line_down, 0), "move_line_down", 14, "Swaps the line under the cursor with the line below it, and moves the cursor down with it.", 90, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1624 },
{ PROC_LINKS(duplicate_line, 0), "duplicate_line", 14, "Create a copy of the line on which the cursor sits.", 51, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1630 },
{ PROC_LINKS(delete_line, 0), "delete_line", 11, "Delete the line the on which the cursor sits.", 45, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1648 },
{ PROC_LINKS(open_file_in_quotes, 0), "open_file_in_quotes", 19, "Reads a filename from surrounding '\"' characters and attempts to open the corresponding file.", 94, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1717 },
{ PROC_LINKS(open_matching_file_cpp, 0), "open_matching_file_cpp", 22, "If the current file is a *.cpp or *.h, attempts to open the corresponding *.h or *.cpp file in the other view.", 110, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1755 },
{ PROC_LINKS(view_buffer_other_panel, 0), "view_buffer_other_panel", 23, "Set the other non-active panel to view the buffer that the active panel views, and switch to that panel.", 104, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1770 },
{ PROC_LINKS(swap_buffers_between_panels, 0), "swap_buffers_between_panels", 27, "Set the other non-active panel to view the buffer that the active panel views, and switch to that panel.", 104, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1785 },
{ PROC_LINKS(kill_buffer, 0), "kill_buffer", 11, "Kills the current buffer.", 25, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1830 },
{ PROC_LINKS(save, 0), "save", 4, "Saves the current buffer.", 25, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1840 },
{ PROC_LINKS(reopen, 0), "reopen", 6, "Reopen the current buffer from the hard drive.", 46, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1854 },
{ PROC_LINKS(undo, 0), "undo", 4, "Advances backwards through the undo history of the current buffer.", 66, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1918 },
{ PROC_LINKS(redo, 0), "redo", 4, "Advances forwards through the undo history of the current buffer.", 65, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1934 },
{ PROC_LINKS(undo_all_buffers, 0), "undo_all_buffers", 16, "Advances backward through the undo history in the buffer containing the most recent regular edit.", 97, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 1952 },
{ PROC_LINKS(redo_all_buffers, 0), "redo_all_buffers", 16, "Advances forward through the undo history in the buffer containing the most recent regular edit.", 96, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 2031 },
{ PROC_LINKS(open_in_other, 0), "open_in_other", 13, "Interactively opens a file in the other panel.", 46, "w:\\4ed\\code\\4coder_base_commands.cpp", 36, 2141 },
{ PROC_LINKS(lister__quit, 0), "lister__quit", 12, "A lister mode command that quits the list without executing any actions.", 72, "w:\\4ed\\code\\4coder_lists.cpp", 28, 8 },
{ PROC_LINKS(lister__activate, 0), "lister__activate", 16, "A lister mode command that activates the list's action on the highlighted item.", 79, "w:\\4ed\\code\\4coder_lists.cpp", 28, 16 },
{ PROC_LINKS(lister__write_character, 0), "lister__write_character", 23, "A lister mode command that dispatches to the lister's write character handler.", 78, "w:\\4ed\\code\\4coder_lists.cpp", 28, 32 },

View File

@ -1366,19 +1366,6 @@ move_line(Application_Links *app, Buffer_ID buffer, i32 line_number, Scan_Direct
return(result);
}
internal void
move_line_current_view(Application_Links *app, Scan_Direction direction){
View_ID view = 0;
get_active_view(app, AccessOpen, &view);
Buffer_ID buffer = 0;
view_get_buffer(app, view, AccessOpen, &buffer);
i32 pos = 0;
view_get_cursor_pos(app, view, &pos);
i32 line_number = get_line_number_from_pos(app, buffer, pos);
pos = move_line(app, buffer, line_number, direction);
view_set_cursor(app, view, seek_pos(pos), true);
}
////////////////////////////////
static void