summaryrefslogtreecommitdiff
path: root/src/input.c
blob: c7e77b051458ee0fbfe3d9e6a2b81ace66aa93d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
 * vimb - a webkit based vim like browser.
 *
 * Copyright (C) 2012-2017 Daniel Carl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

#include <glib.h>
#include <glib/gstdio.h>

#include "ascii.h"
#include "config.h"
#include "input.h"
#include "main.h"
#include "normal.h"
#include "util.h"
#include "ext-proxy.h"

typedef struct {
    Client *c;
    char   *file;
} EditorData;

static void resume_editor(GPid pid, int status, EditorData *data);

/**
 * Function called when vimb enters the input mode.
 */
void input_enter(Client *c)
{
    /* switch focus first to make sure we can write to the inputbox without
     * disturbing the user */
    gtk_widget_grab_focus(GTK_WIDGET(c->webview));
    vb_modelabel_update(c, "-- INPUT --");
    ext_proxy_eval_script(c, "var vimb_input_mode_element = document.activeElement;", NULL);
}

/**
 * Called when the input mode is left.
 */
void input_leave(Client *c)
{
    ext_proxy_eval_script(c, "vimb_input_mode_element.blur();", NULL);
    vb_modelabel_update(c, "");
}

/**
 * Handles the keypress events from webview and inputbox.
 */
VbResult input_keypress(Client *c, int key)
{
    static gboolean ctrlo = FALSE;

    if (ctrlo) {
        /* if we are in ctrl-O mode perform the next keys as normal mode
         * commands until the command is complete or error */
        VbResult res = normal_keypress(c, key);
        if (res != RESULT_MORE) {
            ctrlo = FALSE;
            /* Don't overwrite the mode label in case we landed in another
             * mode. This might occurre by CTRL-0 CTRL-Z or after running ex
             * command, where we mainly end up in normal mode. */
            if (c->mode->id == 'i') {
                /* reenter the input mode */
                input_enter(c);
            }
        }
        return res;
    }

    switch (key) {
        case CTRL('['): /* esc */
            vb_enter(c, 'n');
            return RESULT_COMPLETE;

        case CTRL('O'):
            /* enter CTRL-0 mode to execute next command in normal mode */
            ctrlo           = TRUE;
            c->mode->flags |= FLAG_NOMAP;
            vb_modelabel_update(c, "-- (input) --");
            return RESULT_MORE;

        case CTRL('T'):
            return input_open_editor(c);

        case CTRL('Z'):
            vb_enter(c, 'p');
            return RESULT_COMPLETE;
    }

    c->state.processed_key = FALSE;
    return RESULT_ERROR;
}

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;
}

static void resume_editor(GPid pid, int status, EditorData *data)
{
    char *text, *tmp;
    char *jscode;

    g_assert(pid);
    g_assert(data);
    g_assert(data->c);
    g_assert(data->file);

    if (status == 0) {
        /* get the text the editor stored */
        text = util_get_file_contents(data->file, NULL);

        if (text) {
            /* escape the text to form a valid JS string */
            /* TODO: could go into util.c maybe */
            struct search_replace {
                const char* search;
                const char* replace;
            } escapes[] = {
                {"\x01", ""},
                {"\x02", ""},
                {"\x03", ""},
                {"\x04", ""},
                {"\x05", ""},
                {"\x06", ""},
                {"\a", ""},
                {"\b", ""},
                {"\t", "\\t"},
                {"\n", "\\n"},
                {"\v", ""},
                {"\f", ""},
                {"\r", ""},
                {"\x0E", ""},
                {"\x0F", ""},
                {"\x10", ""},
                {"\x11", ""},
                {"\x12", ""},
                {"\x13", ""},
                {"\x14", ""},
                {"\x15", ""},
                {"\x16", ""},
                {"\x17", ""},
                {"\x18", ""},
                {"\x19", ""},
                {"\x1A", ""},
                {"\x1B", ""},
                {"\x1C", ""},
                {"\x1D", ""},
                {"\x1E", ""},
                {"\x1F", ""},
                {"\"", "\\\""},
                {"\x7F", ""},
                {NULL, NULL},
            };

            for(struct search_replace *i = escapes; i->search; i++) {
                tmp = util_str_replace(i->search, i->replace, text);
                g_free(text);
                text = tmp;
            }

            /* put the text back into the element */
            jscode = g_strdup_printf("vimb_input_mode_element.value=\"%s\"", text);
            ext_proxy_eval_script(data->c, jscode, NULL);

            g_free(jscode);
            g_free(text);
        }
    }

    ext_proxy_eval_script(data->c,
            "vimb_input_mode_element.disabled=false;"
            "vimb_input_mode_element.focus()", NULL);

    g_unlink(data->file);
    g_free(data->file);
    g_slice_free(EditorData, data);
    g_spawn_close_pid(pid);
}