From cff9b33ea6ece591472ff16fff472fa6def9eb2b Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Tue, 23 Oct 2018 22:41:14 +0300 Subject: memview: u for basic undo --- src/memview.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/memview.c b/src/memview.c index 07c50b1..422789b 100644 --- a/src/memview.c +++ b/src/memview.c @@ -152,6 +152,13 @@ static struct { struct { unsigned int x; unsigned int y; } cur; } term; + // kind of hack, lets make real action history someday + // but for now, I want u to work at least for offsets + struct { + size_t data[32]; + unsigned char pointer; + } undo; + struct { char data[255]; unsigned char pointer; @@ -202,6 +209,17 @@ hex_for_byte(unsigned char byte) return hex[byte]; } +static void +store_offset(const size_t offset) +{ + if (ctx.undo.pointer > 0 && offset == ctx.undo.data[0]) + return; + + memmove(ctx.undo.data + 1, ctx.undo.data, ctx.undo.pointer * sizeof(ctx.undo.data[0])); + ctx.undo.data[0] = ctx.last_hexview.offset; + ctx.undo.pointer += (ctx.undo.pointer < ARRAY_SIZE(ctx.undo.data)); +} + static bool offset_is_in_named_region(const size_t offset, const struct named_region *named) { @@ -739,6 +757,8 @@ goto_offset(void *arg) return; } + store_offset(ctx.hexview.offset); + if (v[0] == '+') { ctx.hexview.offset += ret; } else if (v[0] == '-') { @@ -752,6 +772,7 @@ static void follow(void *arg) { (void)arg; + store_offset(ctx.hexview.offset); const union selection v = get_selection(); switch (ctx.native_bits) { case 64: ctx.hexview.offset = v.u64; break; @@ -761,6 +782,17 @@ follow(void *arg) } } +static void +undo(void *arg) +{ + (void)arg; + if (!ctx.undo.pointer) + return; + + ctx.hexview.offset = ctx.undo.data[0]; + memmove(ctx.undo.data, ctx.undo.data + 1, --ctx.undo.pointer * sizeof(ctx.undo.data[0])); +} + static void write_bytes(void *arg) { @@ -923,6 +955,7 @@ main(int argc, char *argv[]) { .seq = { 'q', 0 }, .fun = q_quit }, { .seq = { 'o', 0 }, .fun = goto_offset }, { .seq = { 'f', 0 }, .fun = follow }, + { .seq = { 'u', 0 }, .fun = undo }, { .seq = { 'w', 0 }, .fun = write_bytes }, }; -- cgit v1.2.3