From af43549eb3a6add4c9ae5ac86f30fc2bd0325ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sat, 27 Jun 2015 15:19:10 +0200 Subject: Make :earlier and :later accept arguments similar to vim Currently the following arguments are accepted: {count} Go to older text state {count} times. {N}s Go to older text state about {N} seconds before. {N}m Go to older text state about {N} minutes before. {N}h Go to older text state about {N} hours before. {N}d Go to older text state about {N} days before --- vis.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 19 deletions(-) (limited to 'vis.c') diff --git a/vis.c b/vis.c index 2529f65..b85ae47 100644 --- a/vis.c +++ b/vis.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -419,10 +420,8 @@ static bool cmd_write(Filerange*, enum CmdOpt, const char *argv[]); static bool cmd_saveas(Filerange*, enum CmdOpt, const char *argv[]); /* filter range through external program argv[1] */ static bool cmd_filter(Filerange*, enum CmdOpt, const char *argv[]); -/* switch to the previous saved state of the text, chronologically */ -static bool cmd_earlier(Filerange*, enum CmdOpt, const char *argv[]); -/* switch to the next saved state of the text, chronologically */ -static bool cmd_later(Filerange*, enum CmdOpt, const char *argv[]); +/* switch to the previous/next saved state of the text, chronologically */ +static bool cmd_earlier_later(Filerange*, enum CmdOpt, const char *argv[]); static void action_reset(Action *a); static void switchmode_to(Mode *new_mode); @@ -955,7 +954,7 @@ static void redo(const Arg *arg) { } static void earlier(const Arg *arg) { - size_t pos = text_earlier(vis->win->file->text); + size_t pos = text_earlier(vis->win->file->text, MAX(vis->action.count, 1)); if (pos != EPOS) { view_cursor_to(vis->win->view, pos); /* redraw all windows in case some display the same file */ @@ -964,7 +963,7 @@ static void earlier(const Arg *arg) { } static void later(const Arg *arg) { - size_t pos = text_later(vis->win->file->text); + size_t pos = text_later(vis->win->file->text, MAX(vis->action.count, 1)); if (pos != EPOS) { view_cursor_to(vis->win->view, pos); /* redraw all windows in case some display the same file */ @@ -1879,20 +1878,52 @@ static bool cmd_filter(Filerange *range, enum CmdOpt opt, const char *argv[]) { return status == 0; } -static bool cmd_earlier(Filerange *range, enum CmdOpt opt, const char *argv[]) { - if (argv[1]) - return false; - //TODO eventually support time arguments - text_earlier(vis->win->file->text); - return true; -} +static bool cmd_earlier_later(Filerange *range, enum CmdOpt opt, const char *argv[]) { + Text *txt = vis->win->file->text; + char *unit = ""; + long count = 1; + size_t pos = EPOS; + if (argv[1]) { + errno = 0; + count = strtol(argv[1], &unit, 10); + if (errno || unit == argv[1] || count < 0) { + editor_info_show(vis, "Invalid number"); + return false; + } -static bool cmd_later(Filerange *range, enum CmdOpt opt, const char *argv[]) { - if (argv[1]) - return false; - //TODO eventually support time arguments - text_later(vis->win->file->text); - return true; + if (*unit) { + while (*unit && isspace((unsigned char)*unit)) + unit++; + switch (*unit) { + case 'd': count *= 24; + case 'h': count *= 60; + case 'm': count *= 60; + case 's': break; + default: + editor_info_show(vis, "Unknown time specifier (use: s,m,h or d)"); + return false; + } + + if (argv[0][0] == 'e') + count = -count; /* earlier, move back in time */ + + pos = text_restore(txt, text_state(txt) + count); + } + } + + if (!*unit) { + if (argv[0][0] == 'e') + pos = text_earlier(txt, count); + else + pos = text_later(txt, count); + } + + time_t state = text_state(txt); + char buf[32]; + strftime(buf, sizeof buf, "State from %H:%M", localtime(&state)); + editor_info_show(vis, "%s", buf); + + return pos != EPOS; } static Filepos parse_pos(char **cmd) { -- cgit v1.2.3