summaryrefslogtreecommitdiff
path: root/src/input.c
blob: d8f37b9544b5db7f939939c410d4443f285c0ed1 (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
/**
 * vimb - a webkit based vim like browser.
 *
 * Copyright (C) 2012-2014 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 "config.h"
#include <glib/gstdio.h>
#include "mode.h"
#include "main.h"
#include "input.h"
#include "dom.h"
#include "util.h"

typedef struct {
    char    *file;
    Element *element;
} EditorData;

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

extern VbCore vb;

/**
 * Function called when vimb enters the input mode.
 */
void input_enter(void)
{
    /* switch focus first to make shure we can write to the inputbox without
     * disturbing the user */
    gtk_widget_grab_focus(GTK_WIDGET(vb.gui.webview));
    vb_echo(VB_MSG_NORMAL, false, "-- INPUT --");
}

/**
 * Called when the input mode is left.
 */
void input_leave(void)
{
    vb_set_input_text("");
}

/**
 * Handles the keypress events from webview and inputbox.
 */
VbResult input_keypress(int key)
{
    switch (key) {
        case CTRL('['): /* esc */
            mode_enter('n');
            return RESULT_COMPLETE;

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

        case CTRL('Z'):
            mode_enter('p');
            return RESULT_COMPLETE;
    }
    vb.state.processed_key = false;
    return RESULT_ERROR;
}

VbResult input_open_editor(void)
{
    char **argv, *file_path = NULL;
    const char *text;
    int argc;
    GPid pid;
    gboolean success;

    if (!vb.config.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(vb.config.editor_command, file_path);
    if (!g_shell_parse_argv(command, &argc, &argv, NULL)) {
        fprintf(stderr, "Could not parse editor-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);
        return RESULT_ERROR;
    }

    /* disable the active element */
    dom_editable_element_set_disable(active, true);

    EditorData *data = g_new0(EditorData, 1);
    data->file    = file_path;
    data->element = active;

    g_child_watch_add(pid, (GChildWatchFunc)resume_editor, data);

    return RESULT_COMPLETE;
}

static void resume_editor(GPid pid, int status, EditorData *data)
{
    char *text;
    if (status == 0) {
        text = util_get_file_contents(data->file, NULL);
        if (text) {
            dom_editable_element_set_value(data->element, text);
        }
        g_free(text);
    }
    dom_editable_element_set_disable(data->element, false);

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