From e025418382a7036d32f05aca65f652495ac7b1b6 Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Sat, 22 Nov 2014 23:32:53 +0100 Subject: [PATCH] Clear inputbox after running command successfully (#104). If a ex command was run successfully the inputbox is cleared if the command does not omit this. So the user have a fast response if the command failed or not. This might be useful for :open command where it could take some time until the user see if the requested page starts to load or not. --- src/ex.c | 173 ++++++++++++++++++++++++++++---------------------- src/main.h | 6 ++ src/setting.c | 70 ++++++++++---------- src/setting.h | 8 +-- 4 files changed, 140 insertions(+), 117 deletions(-) diff --git a/src/ex.c b/src/ex.c index 2f433a3..d67b573 100644 --- a/src/ex.c +++ b/src/ex.c @@ -98,7 +98,7 @@ typedef struct { int flags; /* flags for the already parsed command */ } ExArg; -typedef gboolean (*ExFunc)(const ExArg *arg); +typedef VbCmdResult (*ExFunc)(const ExArg *arg); typedef struct { const char *name; /* full name of the command even if called abbreviated */ @@ -127,29 +127,29 @@ static gboolean parse_lhs(const char **input, ExArg *arg); static gboolean parse_rhs(const char **input, ExArg *arg); static void skip_whitespace(const char **input); static void free_cmdarg(ExArg *arg); -static gboolean execute(const ExArg *arg); +static VbCmdResult execute(const ExArg *arg); #ifdef FEATURE_AUTOCMD -static gboolean ex_augroup(const ExArg *arg); -static gboolean ex_autocmd(const ExArg *arg); +static VbCmdResult ex_augroup(const ExArg *arg); +static VbCmdResult ex_autocmd(const ExArg *arg); #endif -static gboolean ex_bookmark(const ExArg *arg); -static gboolean ex_eval(const ExArg *arg); -static gboolean ex_hardcopy(const ExArg *arg); -static gboolean ex_map(const ExArg *arg); -static gboolean ex_unmap(const ExArg *arg); -static gboolean ex_normal(const ExArg *arg); -static gboolean ex_open(const ExArg *arg); +static VbCmdResult ex_bookmark(const ExArg *arg); +static VbCmdResult ex_eval(const ExArg *arg); +static VbCmdResult ex_hardcopy(const ExArg *arg); +static VbCmdResult ex_map(const ExArg *arg); +static VbCmdResult ex_unmap(const ExArg *arg); +static VbCmdResult ex_normal(const ExArg *arg); +static VbCmdResult ex_open(const ExArg *arg); #ifdef FEATURE_QUEUE -static gboolean ex_queue(const ExArg *arg); +static VbCmdResult ex_queue(const ExArg *arg); #endif -static gboolean ex_register(const ExArg *arg); -static gboolean ex_quit(const ExArg *arg); -static gboolean ex_save(const ExArg *arg); -static gboolean ex_set(const ExArg *arg); -static gboolean ex_shellcmd(const ExArg *arg); -static gboolean ex_shortcut(const ExArg *arg); -static gboolean ex_handlers(const ExArg *arg); +static VbCmdResult ex_register(const ExArg *arg); +static VbCmdResult ex_quit(const ExArg *arg); +static VbCmdResult ex_save(const ExArg *arg); +static VbCmdResult ex_set(const ExArg *arg); +static VbCmdResult ex_shellcmd(const ExArg *arg); +static VbCmdResult ex_shortcut(const ExArg *arg); +static VbCmdResult ex_handlers(const ExArg *arg); static gboolean complete(short direction); static void completion_select(char *match); @@ -468,6 +468,7 @@ static void input_activate(void) gboolean ex_run_string(const char *input) { + VbCmdResult res; ExArg *arg = g_slice_new0(ExArg); arg->lhs = g_string_new(""); arg->rhs = g_string_new(""); @@ -476,11 +477,16 @@ gboolean ex_run_string(const char *input) history_add(HISTORY_COMMAND, input, NULL); while (input && *input) { - if (!parse(&input, arg) || !execute(arg)) { + if (!parse(&input, arg) || !(res = execute(arg))) { free_cmdarg(arg); return false; } } + /* check the result of the last executed command */ + if (!(res & VB_CMD_KEEPINPUT)) { + /* clear input text on success if this is not explicit ommited */ + vb_set_input_text(""); + } free_cmdarg(arg); return true; @@ -696,7 +702,7 @@ static gboolean parse_rhs(const char **input, ExArg *arg) /** * Executes the command given by ExArg. */ -static gboolean execute(const ExArg *arg) +static VbCmdResult execute(const ExArg *arg) { return (commands[arg->idx].func)(arg); } @@ -720,38 +726,39 @@ static void free_cmdarg(ExArg *arg) } #ifdef FEATURE_AUTOCMD -static gboolean ex_augroup(const ExArg *arg) +static VbCmdResult ex_augroup(const ExArg *arg) { - return autocmd_augroup(arg->lhs->str, arg->bang); + return autocmd_augroup(arg->lhs->str, arg->bang) ? VB_CMD_SUCCESS : VB_CMD_ERROR; } -static gboolean ex_autocmd(const ExArg *arg) +static VbCmdResult ex_autocmd(const ExArg *arg) { - return autocmd_add(arg->rhs->str, arg->bang); + return autocmd_add(arg->rhs->str, arg->bang) ? VB_CMD_SUCCESS : VB_CMD_ERROR; } #endif -static gboolean ex_bookmark(const ExArg *arg) +static VbCmdResult ex_bookmark(const ExArg *arg) { if (arg->code == EX_BMR) { if (bookmark_remove(*arg->rhs->str ? arg->rhs->str : vb.state.uri)) { - vb_echo_force(VB_MSG_NORMAL, false, " Bookmark removed"); + vb_echo_force(VB_MSG_NORMAL, true, " Bookmark removed"); - return true; + return VB_CMD_SUCCESS | VB_CMD_KEEPINPUT; } } else if (bookmark_add(vb.state.uri, webkit_web_view_get_title(vb.gui.webview), arg->rhs->str)) { - vb_echo_force(VB_MSG_NORMAL, false, " Bookmark added"); + vb_echo_force(VB_MSG_NORMAL, true, " Bookmark added"); - return true; + return VB_CMD_SUCCESS | VB_CMD_KEEPINPUT; } - return false; + return VB_CMD_ERROR; } -static gboolean ex_eval(const ExArg *arg) +static VbCmdResult ex_eval(const ExArg *arg) { gboolean success; - char *value = NULL; + char *value = NULL; + VbCmdResult res = VB_CMD_SUCCESS; if (!arg->rhs->len) { return false; @@ -764,39 +771,41 @@ static gboolean ex_eval(const ExArg *arg) if (!arg->bang) { if (success) { vb_echo(VB_MSG_NORMAL, false, "%s", value); + res = VB_CMD_SUCCESS | VB_CMD_KEEPINPUT; } else { vb_echo(VB_MSG_ERROR, true, "%s", value); + res = VB_CMD_ERROR | VB_CMD_KEEPINPUT; } } g_free(value); - return success; + return res; } -static gboolean ex_hardcopy(const ExArg *arg) +static VbCmdResult ex_hardcopy(const ExArg *arg) { webkit_web_frame_print(webkit_web_view_get_main_frame(vb.gui.webview)); - return true; + return VB_CMD_SUCCESS; } -static gboolean ex_map(const ExArg *arg) +static VbCmdResult ex_map(const ExArg *arg) { if (!arg->lhs->len || !arg->rhs->len) { - return false; + return VB_CMD_ERROR; } /* instead of using the EX_XMAP constants we use the first char of the * command name as mode and the second to determine if noremap is used */ map_insert(arg->lhs->str, arg->rhs->str, arg->name[0], arg->name[1] != 'n'); - return true;; + return VB_CMD_SUCCESS; } -static gboolean ex_unmap(const ExArg *arg) +static VbCmdResult ex_unmap(const ExArg *arg) { char *lhs; if (!arg->lhs->len) { - return false; + return VB_CMD_ERROR; } lhs = arg->lhs->str; @@ -808,29 +817,29 @@ static gboolean ex_unmap(const ExArg *arg) } else { map_delete(lhs, 'i'); } - return true; + return VB_CMD_SUCCESS; } -static gboolean ex_normal(const ExArg *arg) +static VbCmdResult ex_normal(const ExArg *arg) { mode_enter('n'); /* if called with bang - don't apply mapping */ map_handle_string(arg->rhs->str, !arg->bang); - return true; + return VB_CMD_SUCCESS; } -static gboolean ex_open(const ExArg *arg) +static VbCmdResult ex_open(const ExArg *arg) { if (arg->code == EX_TABOPEN) { - return vb_load_uri(&((Arg){VB_TARGET_NEW, arg->rhs->str})); + return vb_load_uri(&((Arg){VB_TARGET_NEW, arg->rhs->str})) ? VB_CMD_SUCCESS : VB_CMD_ERROR; } - return vb_load_uri(&((Arg){VB_TARGET_CURRENT, arg->rhs->str})); + return vb_load_uri(&((Arg){VB_TARGET_CURRENT, arg->rhs->str})) ? VB_CMD_SUCCESS :VB_CMD_ERROR; } #ifdef FEATURE_QUEUE -static gboolean ex_queue(const ExArg *arg) +static VbCmdResult ex_queue(const ExArg *arg) { Arg a = {0}; @@ -852,7 +861,7 @@ static gboolean ex_queue(const ExArg *arg) break; default: - return false; + return VB_CMD_ERROR; } /* if no argument is found in rhs, keep the uri in arg null to force @@ -861,14 +870,16 @@ static gboolean ex_queue(const ExArg *arg) a.s = arg->rhs->str; } - return command_queue(&a); + return command_queue(&a) + ? VB_CMD_SUCCESS | VB_CMD_KEEPINPUT + : VB_CMD_ERROR | VB_CMD_KEEPINPUT; } #endif /** * Show the contents of the registers :reg. */ -static gboolean ex_register(const ExArg *arg) +static VbCmdResult ex_register(const ExArg *arg) { int idx; char *reg; @@ -888,21 +899,23 @@ static gboolean ex_register(const ExArg *arg) vb_echo(VB_MSG_NORMAL, false, "%s", str->str); g_string_free(str, true); - return true; + return VB_CMD_SUCCESS | VB_CMD_KEEPINPUT; } -static gboolean ex_quit(const ExArg *arg) +static VbCmdResult ex_quit(const ExArg *arg) { vb_quit(arg->bang); - return true; + return VB_CMD_SUCCESS; } -static gboolean ex_save(const ExArg *arg) +static VbCmdResult ex_save(const ExArg *arg) { - return command_save(&((Arg){COMMAND_SAVE_CURRENT, arg->rhs->str})); + return command_save(&((Arg){COMMAND_SAVE_CURRENT, arg->rhs->str})) + ? VB_CMD_SUCCESS | VB_CMD_KEEPINPUT + : VB_CMD_ERROR | VB_CMD_KEEPINPUT; } -static gboolean ex_set(const ExArg *arg) +static VbCmdResult ex_set(const ExArg *arg) { char *param = NULL; @@ -919,34 +932,34 @@ static gboolean ex_set(const ExArg *arg) return setting_run(arg->rhs->str, NULL); } -static gboolean ex_shellcmd(const ExArg *arg) +static VbCmdResult ex_shellcmd(const ExArg *arg) { int status; char *stdOut = NULL, *stdErr = NULL; - gboolean success; + VbCmdResult res; GError *error = NULL; if (!*arg->rhs->str) { - return false; + return VB_CMD_ERROR; } if (arg->bang) { if (!g_spawn_command_line_async(arg->rhs->str, &error)) { g_warning("Can't run '%s': %s", arg->rhs->str, error->message); g_clear_error(&error); - success = false; + res = VB_CMD_ERROR | VB_CMD_KEEPINPUT; } else { - success = true; + res = VB_CMD_SUCCESS; } } else { if (!g_spawn_command_line_sync(arg->rhs->str, &stdOut, &stdErr, &status, &error)) { g_warning("Can't run '%s': %s", arg->rhs->str, error->message); g_clear_error(&error); - success = false; + res = VB_CMD_ERROR | VB_CMD_KEEPINPUT; } else { /* the commands success depends not on the return code of the * called shell command, so we know the result already here */ - success = true; + res = VB_CMD_SUCCESS | VB_CMD_KEEPINPUT; } if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { @@ -956,32 +969,37 @@ static gboolean ex_shellcmd(const ExArg *arg) } } - return success; + return res; } -static gboolean ex_handlers(const ExArg *arg) +static VbCmdResult ex_handlers(const ExArg *arg) { char *p; + gboolean success = false; switch (arg->code) { case EX_HANDADD: if (arg->rhs->len && (p = strchr(arg->rhs->str, '='))) { *p++ = '\0'; - return handler_add(arg->rhs->str, p); + success = handler_add(arg->rhs->str, p); } - return false; + break; case EX_HANDREM: - return handler_remove(arg->rhs->str); + success = handler_remove(arg->rhs->str); + break; default: - return false; + break; } + + return success ? VB_CMD_SUCCESS : VB_CMD_ERROR; } -static gboolean ex_shortcut(const ExArg *arg) +static VbCmdResult ex_shortcut(const ExArg *arg) { char *p; + gboolean success = false; /* TODO allow to set shortcuts with set command like ':set * shortcut[name]=http://donain.tld/?q=$0' */ @@ -989,19 +1007,22 @@ static gboolean ex_shortcut(const ExArg *arg) case EX_SCA: if (arg->rhs->len && (p = strchr(arg->rhs->str, '='))) { *p++ = '\0'; - return shortcut_add(arg->rhs->str, p); + success = shortcut_add(arg->rhs->str, p); } - return false; + break; case EX_SCR: - return shortcut_remove(arg->rhs->str); + success = shortcut_remove(arg->rhs->str); + break; case EX_SCD: - return shortcut_set_default(arg->rhs->str); + success = shortcut_set_default(arg->rhs->str); + break; default: - return false; + break; } + return success ? VB_CMD_SUCCESS : VB_CMD_ERROR; } /** diff --git a/src/main.h b/src/main.h index 4c1e284..dcf6d5f 100644 --- a/src/main.h +++ b/src/main.h @@ -191,6 +191,12 @@ typedef enum { VB_STATUS_LAST } StatusType; +typedef enum { + VB_CMD_ERROR, /* command could not be parses or executed */ + VB_CMD_SUCCESS = 0x01, /* command runned successfully */ + VB_CMD_KEEPINPUT = 0x02, /* don't clear inputbox after command run */ +} VbCmdResult; + typedef enum { VB_COMP_NORMAL, VB_COMP_ACTIVE, diff --git a/src/setting.c b/src/setting.c index bacce09..2c4464b 100644 --- a/src/setting.c +++ b/src/setting.c @@ -235,7 +235,7 @@ void setting_init() handler_add("magnet", "xdg-open '%s'"); } -gboolean setting_run(char *name, const char *param) +VbCmdResult setting_run(char *name, const char *param) { SettingType type = SETTING_SET; char modifier; @@ -267,18 +267,19 @@ gboolean setting_run(char *name, const char *param) Setting *s = g_hash_table_lookup(vb.config.settings, name); if (!s) { vb_echo(VB_MSG_ERROR, true, "Config '%s' not found", name); - return false; + return VB_CMD_ERROR | VB_CMD_KEEPINPUT; } if (type == SETTING_GET) { setting_print(s); - return true; + return VB_CMD_SUCCESS | VB_CMD_KEEPINPUT; } if (type == SETTING_TOGGLE) { if (s->type != TYPE_BOOLEAN) { vb_echo(VB_MSG_ERROR, true, "Could not toggle none boolean %s", s->name); - return false; + + return VB_CMD_ERROR | VB_CMD_KEEPINPUT; } gboolean value = !s->value.b; res = setting_set_value(s, &value, SETTING_SET); @@ -287,7 +288,7 @@ gboolean setting_run(char *name, const char *param) if (!param) { vb_echo(VB_MSG_ERROR, true, "No valid value"); - return false; + return VB_CMD_ERROR | VB_CMD_KEEPINPUT; } /* convert sting value into internal used data type */ @@ -310,12 +311,13 @@ gboolean setting_run(char *name, const char *param) break; } } - if (res == SETTING_OK || res & SETTING_USER_NOTIFIED) { - return true; + + if (res & (VB_CMD_SUCCESS | VB_CMD_KEEPINPUT)) { + return res; } vb_echo(VB_MSG_ERROR, true, "Could not set %s", s->name); - return false; + return VB_CMD_ERROR | VB_CMD_KEEPINPUT; } gboolean setting_fill_completion(GtkListStore *store, const char *input) @@ -338,7 +340,7 @@ void setting_cleanup(void) static int setting_set_value(Setting *prop, void *value, SettingType type) { - int res = SETTING_OK; + int res = VB_CMD_SUCCESS; /* by default given value is also the new value */ void *newvalue = NULL; gboolean free_newvalue; @@ -351,7 +353,7 @@ static int setting_set_value(Setting *prop, void *value, SettingType type) if (prop->setter) { res = prop->setter(prop->name, prop->type, newvalue, prop->data); /* break here on error and don't change the setting */ - if (res & SETTING_ERROR) { + if (res & VB_CMD_ERROR) { goto free; } } @@ -544,7 +546,7 @@ static int webkit(const char *name, int type, void *value, void *data) g_object_set(G_OBJECT(web_setting), property, (char*)value, NULL); break; } - return SETTING_OK; + return VB_CMD_SUCCESS; } static int pagecache(const char *name, int type, void *value, void *data) @@ -555,7 +557,7 @@ static int pagecache(const char *name, int type, void *value, void *data) /* first set the setting on the web settings */ res = webkit(name, type, value, data); - if (res == SETTING_OK && on) { + if (res == VB_CMD_SUCCESS && on) { webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER); } else { /* reduce memory usage if caching is not used */ @@ -581,7 +583,7 @@ static int soup(const char *name, int type, void *value, void *data) g_object_set(G_OBJECT(vb.session), property, (char*)value, NULL); break; } - return SETTING_OK; + return VB_CMD_SUCCESS; } static int internal(const char *name, int type, void *value, void *data) @@ -601,7 +603,7 @@ static int internal(const char *name, int type, void *value, void *data) OVERWRITE_STRING(*str, (char*)value); break; } - return SETTING_OK; + return VB_CMD_SUCCESS; } static int input_autohide(const char *name, int type, void *value, void *data) @@ -622,7 +624,7 @@ static int input_autohide(const char *name, int type, void *value, void *data) gtk_widget_set_visible(GTK_WIDGET(vb.gui.input), true); } - return SETTING_OK; + return VB_CMD_SUCCESS; } static int input_color(const char *name, int type, void *value, void *data) @@ -630,14 +632,14 @@ static int input_color(const char *name, int type, void *value, void *data) VB_COLOR_PARSE((VbColor*)data, (char*)value); vb_update_input_style(); - return SETTING_OK; + return VB_CMD_SUCCESS; } static int statusbar(const char *name, int type, void *value, void *data) { gtk_widget_set_visible(GTK_WIDGET(vb.gui.statusbar.box), *(gboolean*)value); - return SETTING_OK; + return VB_CMD_SUCCESS; } static int status_color(const char *name, int type, void *value, void *data) @@ -645,7 +647,7 @@ static int status_color(const char *name, int type, void *value, void *data) VB_COLOR_PARSE((VbColor*)data, (char*)value); vb_update_status_style(); - return SETTING_OK; + return VB_CMD_SUCCESS; } static int input_font(const char *name, int type, void *value, void *data) @@ -658,7 +660,7 @@ static int input_font(const char *name, int type, void *value, void *data) *font = pango_font_description_from_string((char*)value); vb_update_input_style(); - return SETTING_OK; + return VB_CMD_SUCCESS; } static int status_font(const char *name, int type, void *value, void *data) @@ -671,7 +673,7 @@ static int status_font(const char *name, int type, void *value, void *data) *font = pango_font_description_from_string((char*)value); vb_update_status_style(); - return SETTING_OK; + return VB_CMD_SUCCESS; } #ifdef FEATURE_COOKIE @@ -695,12 +697,12 @@ static int cookie_accept(const char *name, int type, void *value, void *data) if (!strcmp(map[i].name, policy)) { g_object_set(jar, SOUP_COOKIE_JAR_ACCEPT_POLICY, map[i].policy, NULL); - return SETTING_OK; + return VB_CMD_SUCCESS; } } vb_echo(VB_MSG_ERROR, true, "%s must be in [always, origin, never]", name); - return SETTING_ERROR | SETTING_USER_NOTIFIED; + return VB_CMD_ERROR | VB_CMD_KEEPINPUT; } #endif @@ -716,14 +718,14 @@ static int ca_bundle(const char *name, int type, void *value, void *data) g_warning("Could not load ssl database '%s': %s", (char*)value, error->message); g_error_free(error); - return SETTING_ERROR; + return VB_CMD_ERROR; } /* there is no function to get the file back from tls file database so * it's saved as separate configuration */ g_object_set(vb.session, "tls-database", vb.config.tls_db, NULL); - return SETTING_OK; + return VB_CMD_SUCCESS; } @@ -777,7 +779,7 @@ static int proxy(const char *name, int type, void *value, void *data) #endif } - return SETTING_OK; + return VB_CMD_SUCCESS; } static int user_style(const char *name, int type, void *value, void *data) @@ -793,7 +795,7 @@ static int user_style(const char *name, int type, void *value, void *data) g_object_set(web_setting, "user-stylesheet-uri", NULL, NULL); } - return SETTING_OK; + return VB_CMD_SUCCESS; } @@ -816,7 +818,7 @@ static int headers(const char *name, int type, void *value, void *data) } vb.config.headers = soup_header_parse_param_list((char*)value); - return SETTING_OK; + return VB_CMD_SUCCESS; } #ifdef FEATURE_ARH @@ -833,11 +835,11 @@ static int autoresponseheader(const char *name, int type, void *value, void *dat /* add the new one */ vb.config.autoresponseheader = new; - return SETTING_OK; + return VB_CMD_SUCCESS; } else { vb_echo(VB_MSG_ERROR, true, "auto-response-header: %s", error); - return SETTING_ERROR | SETTING_USER_NOTIFIED; + return VB_CMD_ERROR | VB_CMD_KEEPINPUT; } } #endif @@ -850,10 +852,10 @@ static int prevnext(const char *name, int type, void *value, void *data) } else { OVERWRITE_STRING(vb.config.prevpattern, (char*)value); } - return SETTING_OK; + return VB_CMD_SUCCESS; } - return SETTING_ERROR | SETTING_USER_NOTIFIED; + return VB_CMD_ERROR | VB_CMD_KEEPINPUT; } static int fullscreen(const char *name, int type, void *value, void *data) @@ -864,7 +866,7 @@ static int fullscreen(const char *name, int type, void *value, void *data) gtk_window_unfullscreen(GTK_WINDOW(vb.gui.window)); } - return SETTING_OK; + return VB_CMD_SUCCESS; } #ifdef FEATURE_HSTS @@ -875,7 +877,7 @@ static int hsts(const char *name, int type, void *value, void *data) } else { soup_session_remove_feature(vb.session, SOUP_SESSION_FEATURE(vb.config.hsts_provider)); } - return SETTING_OK; + return VB_CMD_SUCCESS; } #endif @@ -891,7 +893,7 @@ static int soup_cache(const char *name, int type, void *value, void *data) if (!kilobytes) { soup_cache_clear(vb.config.soup_cache); } - return SETTING_OK; + return VB_CMD_SUCCESS; } #endif diff --git a/src/setting.h b/src/setting.h index d8e59d2..b130855 100644 --- a/src/setting.h +++ b/src/setting.h @@ -22,15 +22,9 @@ #include "main.h" -typedef enum { - SETTING_OK, - SETTING_ERROR = (1 << 1), - SETTING_USER_NOTIFIED = (1 << 2) -} SettingStatus; - void setting_init(void); void setting_cleanup(void); -gboolean setting_run(char* name, const char* param); +VbCmdResult setting_run(char* name, const char* param); gboolean setting_fill_completion(GtkListStore *store, const char *input); #endif /* end of include guard: _SETTING_H */ -- 2.20.1