static VbResult normal_input_open(Client *c, const NormalCmdInfo *info) { if (strchr("ot", info->key)) { vb_echo(c, MSG_NORMAL, FALSE, ":%s ", info->key == 't' ? "tabopen" : "open"); } else { vb_echo(c, MSG_NORMAL, FALSE, ":%s %s", info->key == 'T' ? "tabopen" : "open", c->state.uri); } /* switch mode after setting the input text to not trigger the * commands modes input change handler */ vb_enter_prompt(c, 'c', ":", FALSE); return RESULT_COMPLETE; }
static void setting_print(Client *c, Setting *s) { switch (s->type) { case TYPE_BOOLEAN: vb_echo(c, MSG_NORMAL, FALSE, " %s=%s", s->name, s->value.b ? "true" : "false"); break; case TYPE_INTEGER: vb_echo(c, MSG_NORMAL, FALSE, " %s=%d", s->name, s->value.i); break; default: vb_echo(c, MSG_NORMAL, FALSE, " %s=%s", s->name, s->value.s); break; } }
gboolean command_yank(const Arg *arg, char buf) { static char *tmpl = "Yanked: %s"; if (arg->i == COMMAND_YANK_SELECTION) { char *text = NULL; /* copy current selection to clipboard */ webkit_web_view_copy_clipboard(vb.gui.webview); text = gtk_clipboard_wait_for_text(PRIMARY_CLIPBOARD()); if (!text) { text = gtk_clipboard_wait_for_text(SECONDARY_CLIPBOARD()); } if (text) { /* put the text into the yank buffer */ vb_register_add(buf, text); vb_echo(VB_MSG_NORMAL, false, tmpl, text); g_free(text); return true; } return false; } Arg a = {VB_CLIPBOARD_PRIMARY|VB_CLIPBOARD_SECONDARY}; if (arg->i == COMMAND_YANK_URI) { /* yank current uri */ a.s = vb.state.uri; } else { /* use current arg.s as new clipboard content */ a.s = arg->s; } if (a.s) { /* put the text into the yank buffer */ vb_set_clipboard(&a); vb_register_add(buf, a.s); vb_echo(VB_MSG_NORMAL, false, tmpl, a.s); return true; } return false; }
gboolean command_queue(const Arg *arg) { gboolean res = false; int count = 0; char *uri; switch (arg->i) { case COMMAND_QUEUE_POP: if ((uri = bookmark_queue_pop(&count))) { res = vb_load_uri(&(Arg){VB_TARGET_CURRENT, uri}); g_free(uri); } vb_echo(VB_MSG_NORMAL, false, "Queue length %d", count); break; case COMMAND_QUEUE_PUSH: res = bookmark_queue_push(arg->s ? arg->s : vb.state.uri); if (res) { vb_echo(VB_MSG_NORMAL, false, "Pushed to queue"); } break; case COMMAND_QUEUE_UNSHIFT: res = bookmark_queue_unshift(arg->s ? arg->s : vb.state.uri); if (res) { vb_echo(VB_MSG_NORMAL, false, "Pushed to queue"); } break; case COMMAND_QUEUE_CLEAR: if (bookmark_queue_clear()) { vb_echo(VB_MSG_NORMAL, false, "Queue cleared"); } break; } return res; }
static int hardware_acceleration_policy(Client *c, const char *name, DataType type, void *value, void *data) { WebKitSettings *settings = webkit_web_view_get_settings(c->webview); if (g_str_equal(value, "ondemand")) { webkit_settings_set_hardware_acceleration_policy(settings, WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND); } else if (g_str_equal(value, "always")) { webkit_settings_set_hardware_acceleration_policy(settings, WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS); } else if (g_str_equal(value, "never")) { webkit_settings_set_hardware_acceleration_policy(settings, WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER); } else { vb_echo(c, MSG_ERROR, TRUE, "%s must be in [ondemand, always, never]", name); return CMD_ERROR|CMD_KEEPINPUT; } return CMD_SUCCESS; }
static VbResult normal_view_inspector(Client *c, const NormalCmdInfo *info) { WebKitSettings *settings; settings = webkit_web_view_get_settings(c->webview); /* Try to get the inspected uri to identify if the inspector is shown at * the time or not. */ if (webkit_web_inspector_is_attached(c->inspector)) { webkit_web_inspector_close(c->inspector); } else if (webkit_settings_get_enable_developer_extras(settings)) { webkit_web_inspector_show(c->inspector); } else { /* Inform the user on attempt to enable webinspector when the * developer extra are not enabled. */ vb_echo(c, MSG_ERROR, TRUE, "webinspector is not enabled"); return RESULT_ERROR; } return RESULT_COMPLETE; }
static int cookie_accept(Client *c, const char *name, DataType type, void *value, void *data) { WebKitWebContext *ctx; WebKitCookieManager *cm; char *policy = (char*)value; ctx = webkit_web_view_get_context(c->webview); cm = webkit_web_context_get_cookie_manager(ctx); if (strcmp("always", policy) == 0) { webkit_cookie_manager_set_accept_policy(cm, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS); } else if (strcmp("origin", policy) == 0) { webkit_cookie_manager_set_accept_policy(cm, WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY); } else if (strcmp("never", policy) == 0) { webkit_cookie_manager_set_accept_policy(cm, WEBKIT_COOKIE_POLICY_ACCEPT_NEVER); } else { vb_echo(c, MSG_ERROR, TRUE, "%s must be in [always, origin, never]", name); return CMD_ERROR | CMD_KEEPINPUT; } return CMD_SUCCESS; }
static VbResult normal_clear_input(Client *c, const NormalCmdInfo *info) { /* If an element requested the fullscreen - the <esc> shoudl cause to * leave this fullscreen mode. */ if (c->state.is_fullscreen) { /* Unset the processed_key to indicate that the <esc> was not handled * by us and letting the event bubble up. So that webkit handles the * key for us to leave the fullscreen mode. */ c->state.processed_key = FALSE; return RESULT_COMPLETE; } gtk_widget_grab_focus(GTK_WIDGET(c->webview)); /* Clear the inputbox and change the style to normal to reset also the * possible colored error background. */ vb_echo(c, MSG_NORMAL, FALSE, ""); /* Unset search highlightning. */ command_search(c, &((Arg){0, NULL}), FALSE); return RESULT_COMPLETE; }
VbCmdResult setting_run(Client *c, char *name, const char *param) { SettingType type = SETTING_SET; char modifier; int res, len; /* determine the type to names last char and param */ len = strlen(name); modifier = name[len - 1]; if (modifier == '?') { name[len - 1] = '\0'; type = SETTING_GET; } else if (modifier == '+') { name[len - 1] = '\0'; type = SETTING_APPEND; } else if (modifier == '^') { name[len - 1] = '\0'; type = SETTING_PREPEND; } else if (modifier == '-') { name[len - 1] = '\0'; type = SETTING_REMOVE; } else if (modifier == '!') { name[len - 1] = '\0'; type = SETTING_TOGGLE; } else if (!param) { type = SETTING_GET; } /* lookup a matching setting */ Setting *s = g_hash_table_lookup(c->config.settings, name); if (!s) { vb_echo(c, MSG_ERROR, TRUE, "Config '%s' not found", name); return CMD_ERROR | CMD_KEEPINPUT; } if (type == SETTING_GET) { setting_print(c, s); return CMD_SUCCESS | CMD_KEEPINPUT; } if (type == SETTING_TOGGLE) { if (s->type != TYPE_BOOLEAN) { vb_echo(c, MSG_ERROR, TRUE, "Could not toggle none boolean %s", s->name); return CMD_ERROR | CMD_KEEPINPUT; } gboolean value = !s->value.b; res = setting_set_value(c, s, &value, SETTING_SET); setting_print(c, s); /* make sure the new value set by the toggle keep visible */ res |= CMD_KEEPINPUT; } else { if (!param) { vb_echo(c, MSG_ERROR, TRUE, "No valid value"); return CMD_ERROR | CMD_KEEPINPUT; } /* convert sting value into internal used data type */ gboolean boolvar; int intvar; switch (s->type) { case TYPE_BOOLEAN: boolvar = g_ascii_strncasecmp(param, "true", 4) == 0 || g_ascii_strncasecmp(param, "on", 2) == 0; res = setting_set_value(c, s, &boolvar, type); break; case TYPE_INTEGER: intvar = g_ascii_strtoull(param, (char**)NULL, 10); res = setting_set_value(c, s, &intvar, type); break; default: res = setting_set_value(c, s, (void*)param, type); break; } } if (res & (CMD_SUCCESS | CMD_KEEPINPUT)) { return res; } vb_echo(c, MSG_ERROR, TRUE, "Could not set %s", s->name); return CMD_ERROR | CMD_KEEPINPUT; }
VbResult input_open_editor(Client *c) { char **argv, *file_path = NULL; const char *text = NULL, *editor_command; int argc; GPid pid; gboolean success; GVariant *jsreturn; g_assert(c); /* get the editor command */ editor_command = GET_CHAR(c, "editor-command"); if (!editor_command || !*editor_command) { vb_echo(c, MSG_ERROR, TRUE, "No editor-command configured"); return RESULT_ERROR; } /* get the selected input element */ jsreturn = ext_proxy_eval_script_sync(c, "vimb_input_mode_element.value"); g_variant_get(jsreturn, "(bs)", &success, &text); if (!success || !text) { return RESULT_ERROR; } /* create a temp file to pass text to and from editor */ if (!util_create_tmp_file(text, &file_path)) { return RESULT_ERROR; } /* spawn editor */ char* command = g_strdup_printf(editor_command, file_path); if (!g_shell_parse_argv(command, &argc, &argv, NULL)) { g_critical("Could not parse editor-command '%s'", command); g_free(command); return RESULT_ERROR; } g_free(command); success = g_spawn_async( NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL ); g_strfreev(argv); if (!success) { unlink(file_path); g_free(file_path); g_warning("Could not spawn editor-command"); return RESULT_ERROR; } /* disable the active element */ ext_proxy_eval_script(c, "vimb_input_mode_element.disabled=true", NULL); /* watch the editor process */ EditorData *data = g_slice_new0(EditorData); data->file = file_path; data->c = c; g_child_watch_add(pid, (GChildWatchFunc)resume_editor, data); return RESULT_COMPLETE; }
VbResult input_open_editor(void) { char **argv, *file_path = NULL; const char *text, *editor_command; int argc; GPid pid; gboolean success; editor_command = GET_CHAR("editor-command"); if (!editor_command || !*editor_command) { vb_echo(VB_MSG_ERROR, true, "No editor-command configured"); return RESULT_ERROR; } Element* active = dom_get_active_element(vb.gui.webview); /* check if element is suitable for editing */ if (!active || !dom_is_editable(active)) { return RESULT_ERROR; } text = dom_editable_element_get_value(active); if (!text) { return RESULT_ERROR; } if (!util_create_tmp_file(text, &file_path)) { return RESULT_ERROR; } /* spawn editor */ char* command = g_strdup_printf(editor_command, file_path); if (!g_shell_parse_argv(command, &argc, &argv, NULL)) { g_critical("Could not parse editor-command '%s'", command); g_free(command); return RESULT_ERROR; } g_free(command); success = g_spawn_async( NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL ); g_strfreev(argv); if (!success) { unlink(file_path); g_free(file_path); g_warning("Could not spawn editor-command"); return RESULT_ERROR; } /* disable the active element */ dom_editable_element_set_disable(active, true); EditorData *data = g_slice_new0(EditorData); data->file = file_path; data->element = active; g_child_watch_add(pid, (GChildWatchFunc)resume_editor, data); return RESULT_COMPLETE; }