From 797e05a3a24dae69ed54cb1e27b635e993a07f5b Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Sun, 26 Jan 2020 19:36:02 -0800 Subject: [PATCH] Fix paste crash when view is not editable --- custom/4coder_clipboard.cpp | 126 +++++++++++++++------------- custom/generated/command_metadata.h | 6 +- 2 files changed, 69 insertions(+), 63 deletions(-) diff --git a/custom/4coder_clipboard.cpp b/custom/4coder_clipboard.cpp index ba37ad61..8b562253 100644 --- a/custom/4coder_clipboard.cpp +++ b/custom/4coder_clipboard.cpp @@ -46,23 +46,25 @@ CUSTOM_DOC("At the cursor, insert the text at the top of the clipboard.") Managed_Scope scope = view_get_managed_scope(app, view); Rewrite_Type *next_rewrite = scope_attachment(app, scope, view_next_rewrite_loc, Rewrite_Type); - *next_rewrite = Rewrite_Paste; - i32 *paste_index = scope_attachment(app, scope, view_paste_index_loc, i32); - *paste_index = 0; - - Scratch_Block scratch(app); - - String_Const_u8 string = push_clipboard_index(app, scratch, 0, *paste_index); - if (string.size > 0){ - Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible); + if (next_rewrite != 0){ + *next_rewrite = Rewrite_Paste; + i32 *paste_index = scope_attachment(app, scope, view_paste_index_loc, i32); + *paste_index = 0; - i64 pos = view_get_cursor_pos(app, view); - buffer_replace_range(app, buffer, Ii64(pos), string); - view_set_mark(app, view, seek_pos(pos)); - view_set_cursor_and_preferred_x(app, view, seek_pos(pos + (i32)string.size)); + Scratch_Block scratch(app); - ARGB_Color argb = fcolor_resolve(fcolor_id(defcolor_paste)); - buffer_post_fade(app, buffer, 0.667f, Ii64_size(pos, string.size), argb); + String_Const_u8 string = push_clipboard_index(app, scratch, 0, *paste_index); + if (string.size > 0){ + 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), string); + view_set_mark(app, view, seek_pos(pos)); + view_set_cursor_and_preferred_x(app, view, seek_pos(pos + (i32)string.size)); + + ARGB_Color argb = fcolor_resolve(fcolor_id(defcolor_paste)); + buffer_post_fade(app, buffer, 0.667f, Ii64_size(pos, string.size), argb); + } } } } @@ -79,29 +81,31 @@ CUSTOM_DOC("If the previous command was paste or paste_next, replaces the paste no_mark_snap_to_cursor(app, scope); Rewrite_Type *rewrite = scope_attachment(app, scope, view_rewrite_loc, Rewrite_Type); - if (*rewrite == Rewrite_Paste){ - Rewrite_Type *next_rewrite = scope_attachment(app, scope, view_next_rewrite_loc, Rewrite_Type); - *next_rewrite = Rewrite_Paste; - - i32 *paste_index_ptr = scope_attachment(app, scope, view_paste_index_loc, i32); - i32 paste_index = (*paste_index_ptr) + 1; - *paste_index_ptr = paste_index; - - String_Const_u8 string = push_clipboard_index(app, scratch, 0, paste_index); - - Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible); - - Range_i64 range = get_view_range(app, view); - i64 pos = range.min; - - buffer_replace_range(app, buffer, range, string); - view_set_cursor_and_preferred_x(app, view, seek_pos(pos + string.size)); - - ARGB_Color argb = fcolor_resolve(fcolor_id(defcolor_paste)); - buffer_post_fade(app, buffer, 0.667f, Ii64_size(pos, string.size), argb); - } - else{ - paste(app); + if (rewrite != 0){ + if (*rewrite == Rewrite_Paste){ + Rewrite_Type *next_rewrite = scope_attachment(app, scope, view_next_rewrite_loc, Rewrite_Type); + *next_rewrite = Rewrite_Paste; + + i32 *paste_index_ptr = scope_attachment(app, scope, view_paste_index_loc, i32); + i32 paste_index = (*paste_index_ptr) + 1; + *paste_index_ptr = paste_index; + + String_Const_u8 string = push_clipboard_index(app, scratch, 0, paste_index); + + Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible); + + Range_i64 range = get_view_range(app, view); + i64 pos = range.min; + + buffer_replace_range(app, buffer, range, string); + view_set_cursor_and_preferred_x(app, view, seek_pos(pos + string.size)); + + ARGB_Color argb = fcolor_resolve(fcolor_id(defcolor_paste)); + buffer_post_fade(app, buffer, 0.667f, Ii64_size(pos, string.size), argb); + } + else{ + paste(app); + } } } } @@ -131,28 +135,30 @@ CUSTOM_COMMAND_SIG(multi_paste){ Managed_Scope scope = view_get_managed_scope(app, view); Rewrite_Type *rewrite = scope_attachment(app, scope, view_rewrite_loc, Rewrite_Type); - if (*rewrite == Rewrite_Paste){ - Rewrite_Type *next_rewrite = scope_attachment(app, scope, view_next_rewrite_loc, Rewrite_Type); - *next_rewrite = Rewrite_Paste; - i32 *paste_index_ptr = scope_attachment(app, scope, view_paste_index_loc, i32); - i32 paste_index = (*paste_index_ptr) + 1; - *paste_index_ptr = paste_index; - - String_Const_u8 string = push_clipboard_index(app, scratch, 0, paste_index); - - String_Const_u8 insert_string = push_u8_stringf(scratch, "\n%.*s", string_expand(string)); - - Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible); - Range_i64 range = get_view_range(app, view); - buffer_replace_range(app, buffer, Ii64(range.max), insert_string); - view_set_mark(app, view, seek_pos(range.max + 1)); - view_set_cursor_and_preferred_x(app, view, seek_pos(range.max + insert_string.size)); - - ARGB_Color argb = fcolor_resolve(fcolor_id(defcolor_paste)); - view_post_fade(app, buffer, 0.667f, Ii64(range.max + 1, range.max + insert_string.size), argb); - } - else{ - paste(app); + if (rewrite != 0){ + if (*rewrite == Rewrite_Paste){ + Rewrite_Type *next_rewrite = scope_attachment(app, scope, view_next_rewrite_loc, Rewrite_Type); + *next_rewrite = Rewrite_Paste; + i32 *paste_index_ptr = scope_attachment(app, scope, view_paste_index_loc, i32); + i32 paste_index = (*paste_index_ptr) + 1; + *paste_index_ptr = paste_index; + + String_Const_u8 string = push_clipboard_index(app, scratch, 0, paste_index); + + String_Const_u8 insert_string = push_u8_stringf(scratch, "\n%.*s", string_expand(string)); + + Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible); + Range_i64 range = get_view_range(app, view); + buffer_replace_range(app, buffer, Ii64(range.max), insert_string); + view_set_mark(app, view, seek_pos(range.max + 1)); + view_set_cursor_and_preferred_x(app, view, seek_pos(range.max + insert_string.size)); + + ARGB_Color argb = fcolor_resolve(fcolor_id(defcolor_paste)); + view_post_fade(app, buffer, 0.667f, Ii64(range.max + 1, range.max + insert_string.size), argb); + } + else{ + paste(app); + } } } } diff --git a/custom/generated/command_metadata.h b/custom/generated/command_metadata.h index acd7f03e..9f806e50 100644 --- a/custom/generated/command_metadata.h +++ b/custom/generated/command_metadata.h @@ -391,9 +391,9 @@ static Command_Metadata fcoder_metacmd_table[230] = { { PROC_LINKS(page_down, 0), false, "page_down", 9, "Scrolls the view down one view height and moves the cursor down one view height.", 80, "w:\\4ed\\code\\custom\\4coder_base_commands.cpp", 43, 383 }, { PROC_LINKS(page_up, 0), false, "page_up", 7, "Scrolls the view up one view height and moves the cursor up one view height.", 76, "w:\\4ed\\code\\custom\\4coder_base_commands.cpp", 43, 375 }, { PROC_LINKS(paste, 0), false, "paste", 5, "At the cursor, insert the text at the top of the clipboard.", 59, "w:\\4ed\\code\\custom\\4coder_clipboard.cpp", 39, 39 }, -{ PROC_LINKS(paste_and_indent, 0), false, "paste_and_indent", 16, "Paste from the top of clipboard and run auto-indent on the newly pasted text.", 77, "w:\\4ed\\code\\custom\\4coder_clipboard.cpp", 39, 109 }, -{ PROC_LINKS(paste_next, 0), false, "paste_next", 10, "If the previous command was paste or paste_next, replaces the paste range with the next text down on the clipboard, otherwise operates as the paste command.", 156, "w:\\4ed\\code\\custom\\4coder_clipboard.cpp", 39, 70 }, -{ PROC_LINKS(paste_next_and_indent, 0), false, "paste_next_and_indent", 21, "Paste the next item on the clipboard and run auto-indent on the newly pasted text.", 82, "w:\\4ed\\code\\custom\\4coder_clipboard.cpp", 39, 116 }, +{ PROC_LINKS(paste_and_indent, 0), false, "paste_and_indent", 16, "Paste from the top of clipboard and run auto-indent on the newly pasted text.", 77, "w:\\4ed\\code\\custom\\4coder_clipboard.cpp", 39, 113 }, +{ PROC_LINKS(paste_next, 0), false, "paste_next", 10, "If the previous command was paste or paste_next, replaces the paste range with the next text down on the clipboard, otherwise operates as the paste command.", 156, "w:\\4ed\\code\\custom\\4coder_clipboard.cpp", 39, 72 }, +{ PROC_LINKS(paste_next_and_indent, 0), false, "paste_next_and_indent", 21, "Paste the next item on the clipboard and run auto-indent on the newly pasted text.", 82, "w:\\4ed\\code\\custom\\4coder_clipboard.cpp", 39, 120 }, { PROC_LINKS(place_in_scope, 0), false, "place_in_scope", 14, "Wraps the code contained in the range between cursor and mark with a new curly brace scope.", 91, "w:\\4ed\\code\\custom\\4coder_scope_commands.cpp", 44, 106 }, { PROC_LINKS(profile_clear, 0), false, "profile_clear", 13, "Clear all profiling information from 4coder's self profiler.", 60, "w:\\4ed\\code\\custom\\4coder_profile.cpp", 37, 226 }, { PROC_LINKS(profile_disable, 0), false, "profile_disable", 15, "Prevent 4coder's self profiler from gathering new profiling information.", 72, "w:\\4ed\\code\\custom\\4coder_profile.cpp", 37, 219 },