Re: [Vimprobable-users] [Ticket #9] Edit text areas using $EDITOR
Vimprobable is a lean web browser optimised for full keyboard control
Brought to you by:
hanness
From: Matthew C. <je...@gm...> - 2012-06-30 04:16:04
|
Hi all, Initial testing on Arch Linux (up to date). Code failed to compile, required changing the following on 1825: time_t id = time() to time_t id = time(NULL) Not sure if that is specific to my machine or what. Test results with 'vim' as $EDITOR: Calling vimprobable2 from the CLI causes it to properly pull up vim and take the text into the input after saving with :x Calling from a menu (dmenu) does nothing (no terminal is brought up with vim loaded). Test results with 'gvim' as $EDITOR: Editor pulls up fine in both cases, however text is never sent back to the input field. -Matt On Fri, Jun 29, 2012 at 07:32:21PM +0200, Hannes Schüller wrote: > Sorry, here is already another one: This escapes quotation marks > correctly (hopefully). It supersedes the previous patch. > > Hannes > > diff --git a/keymap.h b/keymap.h > index 7ba5d0b..86aea55 100644 > --- a/keymap.h > +++ b/keymap.h > @@ -114,6 +114,9 @@ Key keys[] = { > { 0, GDK_semicolon, GDK_T, input, {.s = ";T"} }, > { 0, GDK_semicolon, GDK_W, input, {.s = ";W"} }, > > + /* this needs to be a binding using CTRL for obvious reasons */ > + { GDK_CONTROL_MASK, 0, GDK_t, open_editor,{} }, > + > { 0, GDK_VoidSymbol, GDK_Escape, set, {ModeNormal} }, > { GDK_CONTROL_MASK, GDK_VoidSymbol, GDK_bracketleft,set, {ModeNormal} }, > { GDK_CONTROL_MASK, 0, GDK_z, set, {ModePassThrough} }, > diff --git a/main.c b/main.c > index 20a9925..810455c 100644 > --- a/main.c > +++ b/main.c > @@ -59,6 +59,7 @@ static gboolean complete(const Arg *arg); > static gboolean descend(const Arg *arg); > gboolean echo(const Arg *arg); > static gboolean focus_input(const Arg *arg); > +static gboolean open_editor(const Arg *arg); > static gboolean input(const Arg *arg); > static gboolean navigate(const Arg *arg); > static gboolean number(const Arg *arg); > @@ -419,6 +420,9 @@ webview_keypress_cb(WebKitWebView *webview, GdkEventKey *event) { > g_free(a.s); > a.i = ModeNormal; > return set(&a); > + } else if (CLEAN(event->state) & GDK_CONTROL_MASK) { > + /* keybindings of non-printable characters */ > + if (process_keypress(event) == TRUE) return TRUE; > } > case ModePassThrough: > if (IS_ESCAPE(event)) { > @@ -1809,6 +1813,112 @@ view_source(const Arg * arg) { > } > > static gboolean > +open_editor(const Arg *arg) { > + char *editor, *type, *text = NULL, *tempfile, *argv[2], s[255] = "", idstr[64] = ""; > + FILE *fp; > + gboolean success; > + GString *new_text = g_string_new(""); > + time_t id = time(); > + gchar *value = NULL, *message = NULL, *tag = NULL, *command = NULL; > + > + /* check if $EDITOR is set */ > + if (getenv("EDITOR")) > + editor = g_strdup_printf("%s", getenv("EDITOR")); > + else > + return FALSE; > + > + /* check if active element is suitable for text editing */ > + jsapi_evaluate_script("document.activeElement.tagName", &value, &message); > + if (value == NULL) > + return FALSE; > + tag = g_strdup(value); > + if (strcmp(tag, "INPUT") == 0) { > + /* extra check: type == text */ > + jsapi_evaluate_script("document.activeElement.type", &value, &message); > + if (strcmp(value, "text") != 0) { > + g_free(value); > + g_free(message); > + return FALSE; > + } > + } else if (strcmp(tag, "TEXTAREA") != 0) { > + g_free(value); > + g_free(message); > + return FALSE; > + } > + jsapi_evaluate_script("document.activeElement.value", &value, &message); > + text = g_strdup(value); > + if (text == NULL) { > + g_free(value); > + g_free(message); > + return FALSE; > + } > + > + /* write text into temporary file */ > + tempfile = g_strdup_printf(TEMPFILE); > + sprintf(idstr, "%d", id); > + tempfile = g_strconcat(tempfile, ".", idstr); > + fp = fopen(tempfile, "w"); > + if (fp == NULL) { > + g_free(value); > + g_free(message); > + g_free(text); > + return FALSE; > + } > + if (fputs(text, fp) < 0) { > + g_free(value); > + g_free(message); > + g_free(text); > + return FALSE; > + } > + fclose(fp); > + g_free(text); > + > + /* spawn editor */ > + command = g_strconcat(editor, " ", tempfile, NULL); > + success = g_spawn_command_line_sync(command, NULL, NULL, NULL, NULL); > + if (!success) { > + g_free(value); > + g_free(message); > + return FALSE; > + } > + > + /* re-read the new contents of the file and put it into the HTML element */ > + if (!access(tempfile, R_OK) == 0) { > + g_free(value); > + g_free(message); > + return FALSE; > + } > + fp = fopen(tempfile, "r"); > + if (fp == NULL) { > + g_free(value); > + g_free(message); > + return FALSE; > + } > + jsapi_evaluate_script("document.activeElement.value = '';", &value, &message); > + new_text = g_string_append(new_text, "\""); > + while (fgets(s, 254, fp)) { > + if (s[strlen(s)-1] == '\n') { > + /* encode line breaks into the string as Javascript does not like actual line breaks */ > + new_text = g_string_append_len(new_text, s, strlen(s) - 1); > + new_text = g_string_append(new_text, "\\n"); > + } else { > + new_text = g_string_append(new_text, s); > + } > + } > + new_text = g_string_append(new_text, "\""); > + fclose(fp); > + jsapi_evaluate_script(g_strconcat("document.activeElement.value = ", new_text->str, ";", NULL), &value, &message); > + > + /* done */ > + g_string_free(new_text, TRUE); > + g_free(value); > + g_free(message); > + g_free(tag); > + remove(tempfile); > + return TRUE; > +} > + > +static gboolean > focus_input(const Arg *arg) { > static Arg a; > > diff --git a/vimprobable.h b/vimprobable.h > index 5a6c2df..e2c647e 100644 > --- a/vimprobable.h > +++ b/vimprobable.h > @@ -181,6 +181,9 @@ enum ConfigFileError { > #define HISTORY_STORAGE_FILENAME "%s/vimprobable/history", config_base > #define CLOSED_URL_FILENAME "%s/vimprobable/closed", config_base > > +/* external editor - temporary file */ > +#define TEMPFILE "/tmp/vimprobable_edit" > + > /* Command size */ > #define COMMANDSIZE 1024 > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Vimprobable-users mailing list > Vim...@li... > https://lists.sourceforge.net/lists/listinfo/vimprobable-users -- Matthew Carter je...@gm... |