Skip to content

Commit

Permalink
Improve undo/redo
Browse files Browse the repository at this point in the history
Currently a snapshot is taken whenever an operator is completed or
a certain idle time in either insert or replace mode is detected.
  • Loading branch information
martanne committed Sep 8, 2014
1 parent e7b6ac1 commit ee3ded9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
25 changes: 21 additions & 4 deletions config.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ static void split(const Arg *arg) {
void action_do(Action *a);
void action_reset(Action *a);

static void snapshot(void) {
text_snapshot(vis->win->text);
}

static void repeat(const Arg *arg) {
action = action_prev;
action_do(&action);
Expand Down Expand Up @@ -457,6 +461,7 @@ void action_do(Action *a) {
switchmode_to(mode_prev);
else if (mode == &vis_modes[VIS_MODE_VISUAL])
switchmode(&(const Arg){ .i = VIS_MODE_NORMAL });
snapshot();
}

if (a != &action_prev) {
Expand All @@ -466,6 +471,16 @@ void action_do(Action *a) {
}
}

static void undo(const Arg *arg) {
if (text_undo(vis->win->text))
vis_draw(vis);
}

static void redo(const Arg *arg) {
if (text_redo(vis->win->text))
vis_draw(vis);
}

static void zero(const Arg *arg) {
if (action.count == 0)
movement(&(const Arg){ .i = MOVE_LINE_BEGIN });
Expand Down Expand Up @@ -698,8 +713,8 @@ static KeyBinding vis_normal[] = {
{ { NONE('i') }, switchmode, { .i = VIS_MODE_INSERT } },
{ { NONE('v') }, switchmode, { .i = VIS_MODE_VISUAL } },
{ { NONE('R') }, switchmode, { .i = VIS_MODE_REPLACE} },
{ { NONE('u') }, call, { .f = vis_undo } },
{ { CONTROL('R') }, call, { .f = vis_redo } },
{ { NONE('u') }, undo, { NULL } },
{ { CONTROL('R') }, redo, { NULL } },
{ { CONTROL('L') }, call, { .f = vis_draw } },
{ { NONE(':') }, prompt, { .s = ":" } },
{ /* empty last element, array terminator */ },
Expand Down Expand Up @@ -862,12 +877,14 @@ static Mode vis_modes[] = {
.parent = &vis_modes[VIS_MODE_READLINE],
.bindings = vis_insert_mode,
.input = vis_insert_input,
.idle = snapshot,
},
[VIS_MODE_REPLACE] = {
.name = "REPLACE",
.parent = &vis_modes[VIS_MODE_INSERT],
.bindings = vis_replace,
.input = vis_replace_input,
.idle = snapshot,
},
};

Expand Down Expand Up @@ -939,8 +956,8 @@ static KeyBinding nano_keys[] = {
{ { META('|') }, movement, { .i = MOVE_FILE_BEGIN } },
{ { META('/') }, movement, { .i = MOVE_FILE_END } },
{ { META('?') }, movement, { .i = MOVE_FILE_END } },
{ { META('U') }, call, { .f = vis_undo } },
{ { META('E') }, call, { .f = vis_redo } },
{ { META('U') }, undo, { NULL } },
{ { META('E') }, redo, { NULL } },
#if 0
{ { CONTROL('I') }, insert, { .s = "\t" } },
/* TODO: handle this in vis to insert \n\r when appriopriate */
Expand Down
8 changes: 6 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct Mode {
void (*leave)(Mode *new);
bool (*unknown)(Key *key0, Key *key1); /* unknown key for this mode, return value determines whether parent modes will be checked */
bool (*input)(const char *str, size_t len); /* unknown key for this an all parent modes */
void (*idle)(void);
};

typedef struct {
Expand Down Expand Up @@ -263,7 +264,8 @@ int main(int argc, char *argv[]) {
}

if (!FD_ISSET(STDIN_FILENO, &fds)) {
vis_snapshot(vis);
if (mode->idle)
mode->idle();
timeout = NULL;
continue;
}
Expand Down Expand Up @@ -291,7 +293,9 @@ int main(int argc, char *argv[]) {
if (key.code)
continue;

if (mode->input && mode->input(key.str, strlen(key.str)))
if (mode->input)
mode->input(key.str, strlen(key.str));
if (mode->idle)
timeout = &idle;
}

Expand Down
18 changes: 0 additions & 18 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,6 @@ void vis_statusbar_set(Vis *vis, vis_statusbar_t statusbar) {
vis->statusbar = statusbar;
}

void vis_snapshot(Vis *vis) {
text_snapshot(vis->win->text);
}

void vis_undo(Vis *vis) {
VisWin *win = vis->win;
// TODO window invalidation
if (text_undo(win->text))
window_draw(win->win);
}

void vis_redo(Vis *vis) {
VisWin *win = vis->win;
// TODO window invalidation
if (text_redo(win->text))
window_draw(win->win);
}

static void vis_windows_arrange_horizontal(Vis *vis) {
int n = 0, x = 0, y = 0;
for (VisWin *win = vis->windows; win; win = win->next)
Expand Down
3 changes: 0 additions & 3 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ struct Syntax { /* a syntax definition */
Vis *vis_new(int width, int height);
void vis_free(Vis*);
void vis_resize(Vis*, int width, int height);
void vis_snapshot(Vis*);
void vis_undo(Vis*);
void vis_redo(Vis*);
void vis_draw(Vis*);
void vis_update(Vis*);
void vis_insert_key(Vis*, const char *c, size_t len);
Expand Down

0 comments on commit ee3ded9

Please sign in to comment.