summaryrefslogtreecommitdiff
path: root/src/mode.c
blob: bb1af1de240c5a3ebf8a97897b0254ecbb27e01a (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
/**
 * 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 "main.h"
#include "mode.h"
#include "normal.h"
#include "ascii.h"
#include <glib.h>

static GHashTable *modes = NULL;
extern VbCore vb;

static void free_mode(Mode *mode);


void mode_init(void)
{
    modes = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)free_mode);
}

void mode_cleanup(void)
{
    if (modes) {
        g_hash_table_destroy(modes);
        vb.mode = NULL;
    }
}

/**
 * Creates a new mode with given callback functions.
 */
void mode_add(char id, ModeTransitionFunc enter, ModeTransitionFunc leave,
    ModeKeyFunc keypress, ModeInputChangedFunc input_changed)
{
    Mode *new = g_slice_new(Mode);
    new->id            = id;
    new->enter         = enter;
    new->leave         = leave;
    new->keypress      = keypress;
    new->input_changed = input_changed;
    new->flags         = 0;

    g_hash_table_insert(modes, GINT_TO_POINTER(id), new);
}

/**
 * Enter into the new given mode and leave possible active current mode.
 */
void mode_enter(char id)
{
    Mode *new = g_hash_table_lookup(modes, GINT_TO_POINTER(id));

    g_return_if_fail(new != NULL);

    if (vb.mode) {
        /* don't do anything if the mode isn't a new one */
        if (vb.mode == new) {
            return;
        }

        /* if there is a active mode, leave this first */
        if (vb.mode->leave) {
            vb.mode->leave();
        }
    }

    /* reset the flags of the new entered mode */
    new->flags = 0;

    /* set the new mode so that it is available also in enter function */
    vb.mode = new;
    /* call enter only if the new mode isn't the current mode */
    if (new->enter) {
        new->enter();
    }

#ifndef TESTLIB
    vb_update_statusbar();
#endif
}

/**
 * Set the prompt chars and switch to new mode.
 *
 * @id:           Mode id.
 * @prompt:       Prompt string to set as current prompt.
 * @print_prompt: Indicates if the new set prompt should be put into inputbox
 *                after switching the mode.
 */
void mode_enter_prompt(char id, const char *prompt, gboolean print_prompt)
{
    /* set the prompt to be accessible in mode_enter */
    strncpy(vb.state.prompt, prompt, PROMPT_SIZE - 1);
    vb.state.prompt[PROMPT_SIZE - 1] = '\0';

    mode_enter(id);

    if (print_prompt) {
        /* set it after the mode was entered so that the modes input change
         * event listener could grep the new prompt */
        vb_echo_force(VB_MSG_NORMAL, false, vb.state.prompt);
    }
}

VbResult mode_handle_key(int key)
{
    VbResult res;
    static gboolean ctrl_v = false;

    if (ctrl_v) {
        vb.state.processed_key = false;
        ctrl_v = false;

        return RESULT_COMPLETE;
    }
    if (key == CTRL('V')) {
        vb.mode->flags |= FLAG_NOMAP;
        ctrl_v = true;
        normal_showcmd(key);

        return RESULT_MORE;
    }

    if (vb.mode && vb.mode->keypress) {
#ifdef DEBUG
        int flags = vb.mode->flags;
        int id    = vb.mode->id;
        res = vb.mode->keypress(key);
        if (vb.mode) {
            PRINT_DEBUG(
                "%c[%d]: %#.2x '%c' -> %c[%d]",
                id - ' ', flags, key, (key >= 0x20 && key <= 0x7e) ? key : ' ',
                vb.mode->id - ' ', vb.mode->flags
            );
        }
#else
        res = vb.mode->keypress(key);
#endif
        return res;
    }
    return RESULT_ERROR;
}

gboolean mode_input_focusin(GtkWidget *widget, GdkEventFocus *event, gpointer data)
{
    /* enter the command mode if the focus is on inputbox */
    mode_enter('c');

    return false;
}

/**
 * Process input changed event on current active mode.
 */
void mode_input_changed(GtkTextBuffer* buffer, gpointer data)
{
    char *text;
    GtkTextIter start, end;
    /* don't observe changes in completion mode */
    if (vb.mode->flags & FLAG_COMPLETION) {
        return;
    }
    /* don't process changes not typed by the user */
    if (gtk_widget_is_focus(vb.gui.input) && vb.mode && vb.mode->input_changed) {
        gtk_text_buffer_get_bounds(buffer, &start, &end);
        text = gtk_text_buffer_get_text(buffer, &start, &end, false);

        vb.mode->input_changed(text);

        g_free(text);
    }
}

static void free_mode(Mode *mode)
{
    g_slice_free(Mode, mode);
}