From 71e62ce84f96d4078168fe9d02cee93d8b092120 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Tue, 10 Aug 2021 21:25:40 -0400 Subject: [PATCH 1/4] Added in a hash check to the system clipboard. --- data/core/commands/doc.lua | 33 +++++++++++++++++++++------------ data/core/doc/init.lua | 1 + src/api/system.c | 9 +++++++++ src/rencache.c | 13 +++++-------- src/rencache.h | 4 ++++ 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index 95919a70..0a751e77 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -53,6 +53,18 @@ local function save(filename) core.log("Saved \"%s\"", saved_filename) end + +local function split_cursor(direction) + local new_cursors = {} + for _, line1, col1 in doc():get_selections() do + if line1 > 1 and line1 < #doc().lines then + table.insert(new_cursors, { line1 + direction, col1 }) + end + end + for i,v in ipairs(new_cursors) do doc():add_selection(v[1], v[2]) end + core.blink_reset() +end + local function cut_or_copy(delete) local full_text = "" for idx, line1, col1, line2, col2 in doc():get_selections() do @@ -67,18 +79,10 @@ local function cut_or_copy(delete) doc().cursor_clipboard[idx] = "" end end - system.set_clipboard(full_text) -end - -local function split_cursor(direction) - local new_cursors = {} - for _, line1, col1 in doc():get_selections() do - if line1 > 1 and line1 < #doc().lines then - table.insert(new_cursors, { line1 + direction, col1 }) - end + if #doc().cursor_clipboard > 1 then + doc().cursor_clipboard["hash"] = system.hash(system.get_clipboard()) end - for i,v in ipairs(new_cursors) do doc():add_selection(v[1], v[2]) end - core.blink_reset() + system.set_clipboard(full_text) end local commands = { @@ -99,8 +103,13 @@ local commands = { end, ["doc:paste"] = function() + local clipboard = system.get_clipboard() + -- If the clipboard has changed since our last look, use that instead + if doc().cursor_clipboard["hash"] ~= system.hash(clipboard) then + doc().cursor_clipboard = {} + end for idx, line1, col1, line2, col2 in doc():get_selections() do - local value = doc().cursor_clipboard[idx] or system.get_clipboard() + local value = doc().cursor_clipboard[idx] or clipboard doc():text_input(value:gsub("\r", ""), idx) end end, diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 52f2113c..1baf7182 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -173,6 +173,7 @@ function Doc:merge_cursors(idx) if self.selections[i] == self.selections[j] and self.selections[i+1] == self.selections[j+1] then common.splice(self.selections, i, 4) + common.splice(self.cursor_clipboard, i, 1) break end end diff --git a/src/api/system.c b/src/api/system.c index 2f1bf763..85238e34 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -637,6 +637,14 @@ static int f_set_window_opacity(lua_State *L) { return 1; } +static int f_hash(lua_State* L) { + unsigned i = RENCACHE_HASH_INITIAL; + size_t strLen; + const char *str = luaL_checklstring(L, 1, &strLen); + rencache_hash(&i, str, strLen) ; + lua_pushnumber(L, i); + return 1; +} static const luaL_Reg lib[] = { { "poll_event", f_poll_event }, @@ -664,6 +672,7 @@ static const luaL_Reg lib[] = { { "exec", f_exec }, { "fuzzy_match", f_fuzzy_match }, { "set_window_opacity", f_set_window_opacity }, + { "hash", f_hash }, { NULL, NULL } }; diff --git a/src/rencache.c b/src/rencache.c index 31165e90..530615f4 100644 --- a/src/rencache.c +++ b/src/rencache.c @@ -83,10 +83,7 @@ static void font_refs_clear(lua_State *L) { font_refs_len = 0; } -/* 32bit fnv-1a hash */ -#define HASH_INITIAL 2166136261 - -static void hash(unsigned *h, const void *data, int size) { +void rencache_hash(unsigned *h, const void *data, int size) { const unsigned char *p = data; while (size--) { *h = (*h ^ *p++) * 16777619; @@ -227,7 +224,7 @@ static void update_overlapping_cells(RenRect r, unsigned h) { for (int y = y1; y <= y2; y++) { for (int x = x1; x <= x2; x++) { int idx = cell_idx(x, y); - hash(&cells[idx], &h, sizeof(h)); + rencache_hash(&cells[idx], &h, sizeof(h)); } } } @@ -255,8 +252,8 @@ void rencache_end_frame(lua_State *L) { if (cmd->type == SET_CLIP) { cr = cmd->rect; } RenRect r = intersect_rects(cmd->rect, cr); if (r.width == 0 || r.height == 0) { continue; } - unsigned h = HASH_INITIAL; - hash(&h, cmd, cmd->size); + unsigned h = RENCACHE_HASH_INITIAL; + rencache_hash(&h, cmd, cmd->size); update_overlapping_cells(r, h); } @@ -271,7 +268,7 @@ void rencache_end_frame(lua_State *L) { if (cells[idx] != cells_prev[idx]) { push_rect((RenRect) { x, y, 1, 1 }, &rect_count); } - cells_prev[idx] = HASH_INITIAL; + cells_prev[idx] = RENCACHE_HASH_INITIAL; } } diff --git a/src/rencache.h b/src/rencache.h index 1d0f45a6..95a088d6 100644 --- a/src/rencache.h +++ b/src/rencache.h @@ -5,8 +5,12 @@ #include #include "renderer.h" +/* 32bit fnv-1a hash */ +#define RENCACHE_HASH_INITIAL 2166136261 + void rencache_show_debug(bool enable); void rencache_set_clip_rect(RenRect rect); +void rencache_hash(unsigned *h, const void *data, int size); void rencache_draw_rect(RenRect rect, RenColor color); int rencache_draw_text(lua_State *L, FontDesc *font_desc, int font_index, const char *text, int x, int y, RenColor color, bool draw_subpixel, CPReplaceTable *replacements, RenColor replace_color); From 255c45b30b641deb0fffabd8db31675fb106e0f8 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Tue, 10 Aug 2021 21:29:33 -0400 Subject: [PATCH 2/4] Since we're modifying the clipboard, actually makes way more sense to use this hash. --- data/core/commands/doc.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index 0a751e77..a571b1bb 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -80,7 +80,7 @@ local function cut_or_copy(delete) end end if #doc().cursor_clipboard > 1 then - doc().cursor_clipboard["hash"] = system.hash(system.get_clipboard()) + doc().cursor_clipboard["hash"] = system.hash(full_text) end system.set_clipboard(full_text) end From aa4d91a03f54a3b9b1075ac608a5fb3e546c2ee8 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Wed, 11 Aug 2021 18:14:46 -0400 Subject: [PATCH 3/4] Reverted hash changes; simply copy entire clipboard. --- data/core/commands/doc.lua | 29 +++++++++++++---------------- src/api/system.c | 9 --------- src/rencache.c | 13 ++++++++----- src/rencache.h | 4 ---- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index a571b1bb..7787054c 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -53,18 +53,6 @@ local function save(filename) core.log("Saved \"%s\"", saved_filename) end - -local function split_cursor(direction) - local new_cursors = {} - for _, line1, col1 in doc():get_selections() do - if line1 > 1 and line1 < #doc().lines then - table.insert(new_cursors, { line1 + direction, col1 }) - end - end - for i,v in ipairs(new_cursors) do doc():add_selection(v[1], v[2]) end - core.blink_reset() -end - local function cut_or_copy(delete) local full_text = "" for idx, line1, col1, line2, col2 in doc():get_selections() do @@ -79,12 +67,21 @@ local function cut_or_copy(delete) doc().cursor_clipboard[idx] = "" end end - if #doc().cursor_clipboard > 1 then - doc().cursor_clipboard["hash"] = system.hash(full_text) - end + doc().cursor_clipboard["full"] = full_text system.set_clipboard(full_text) end +local function split_cursor(direction) + local new_cursors = {} + for _, line1, col1 in doc():get_selections() do + if line1 > 1 and line1 < #doc().lines then + table.insert(new_cursors, { line1 + direction, col1 }) + end + end + for i,v in ipairs(new_cursors) do doc():add_selection(v[1], v[2]) end + core.blink_reset() +end + local commands = { ["doc:undo"] = function() doc():undo() @@ -105,7 +102,7 @@ local commands = { ["doc:paste"] = function() local clipboard = system.get_clipboard() -- If the clipboard has changed since our last look, use that instead - if doc().cursor_clipboard["hash"] ~= system.hash(clipboard) then + if doc().cursor_clipboard["full"] ~= clipboard then doc().cursor_clipboard = {} end for idx, line1, col1, line2, col2 in doc():get_selections() do diff --git a/src/api/system.c b/src/api/system.c index 85238e34..2f1bf763 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -637,14 +637,6 @@ static int f_set_window_opacity(lua_State *L) { return 1; } -static int f_hash(lua_State* L) { - unsigned i = RENCACHE_HASH_INITIAL; - size_t strLen; - const char *str = luaL_checklstring(L, 1, &strLen); - rencache_hash(&i, str, strLen) ; - lua_pushnumber(L, i); - return 1; -} static const luaL_Reg lib[] = { { "poll_event", f_poll_event }, @@ -672,7 +664,6 @@ static const luaL_Reg lib[] = { { "exec", f_exec }, { "fuzzy_match", f_fuzzy_match }, { "set_window_opacity", f_set_window_opacity }, - { "hash", f_hash }, { NULL, NULL } }; diff --git a/src/rencache.c b/src/rencache.c index 530615f4..31165e90 100644 --- a/src/rencache.c +++ b/src/rencache.c @@ -83,7 +83,10 @@ static void font_refs_clear(lua_State *L) { font_refs_len = 0; } -void rencache_hash(unsigned *h, const void *data, int size) { +/* 32bit fnv-1a hash */ +#define HASH_INITIAL 2166136261 + +static void hash(unsigned *h, const void *data, int size) { const unsigned char *p = data; while (size--) { *h = (*h ^ *p++) * 16777619; @@ -224,7 +227,7 @@ static void update_overlapping_cells(RenRect r, unsigned h) { for (int y = y1; y <= y2; y++) { for (int x = x1; x <= x2; x++) { int idx = cell_idx(x, y); - rencache_hash(&cells[idx], &h, sizeof(h)); + hash(&cells[idx], &h, sizeof(h)); } } } @@ -252,8 +255,8 @@ void rencache_end_frame(lua_State *L) { if (cmd->type == SET_CLIP) { cr = cmd->rect; } RenRect r = intersect_rects(cmd->rect, cr); if (r.width == 0 || r.height == 0) { continue; } - unsigned h = RENCACHE_HASH_INITIAL; - rencache_hash(&h, cmd, cmd->size); + unsigned h = HASH_INITIAL; + hash(&h, cmd, cmd->size); update_overlapping_cells(r, h); } @@ -268,7 +271,7 @@ void rencache_end_frame(lua_State *L) { if (cells[idx] != cells_prev[idx]) { push_rect((RenRect) { x, y, 1, 1 }, &rect_count); } - cells_prev[idx] = RENCACHE_HASH_INITIAL; + cells_prev[idx] = HASH_INITIAL; } } diff --git a/src/rencache.h b/src/rencache.h index 95a088d6..1d0f45a6 100644 --- a/src/rencache.h +++ b/src/rencache.h @@ -5,12 +5,8 @@ #include #include "renderer.h" -/* 32bit fnv-1a hash */ -#define RENCACHE_HASH_INITIAL 2166136261 - void rencache_show_debug(bool enable); void rencache_set_clip_rect(RenRect rect); -void rencache_hash(unsigned *h, const void *data, int size); void rencache_draw_rect(RenRect rect, RenColor color); int rencache_draw_text(lua_State *L, FontDesc *font_desc, int font_index, const char *text, int x, int y, RenColor color, bool draw_subpixel, CPReplaceTable *replacements, RenColor replace_color); From acd122bc345674fa93e4440ee335dc6625444da6 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Wed, 11 Aug 2021 18:38:36 -0400 Subject: [PATCH 4/4] Small fix. --- data/core/doc/init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 1baf7182..d05d9d45 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -178,6 +178,7 @@ function Doc:merge_cursors(idx) end end end + if #self.selections <= 4 then self.cursor_clipboard = {} end end local function selection_iterator(invariant, idx)