示例#1
0
文件: ex.c 项目: dreamsxin/vimb
/**
 * Handles the keypress events from webview and inputbox.
 */
VbResult ex_keypress(int key)
{
    GtkTextIter start, end;
    GtkTextBuffer *buffer = vb.gui.buffer;
    GtkTextMark *mark;

    /* delegate call to the submode */
    if (RESULT_COMPLETE == hints_keypress(key)) {
        return RESULT_COMPLETE;
    }

    switch (key) {
        case KEY_TAB:
            complete(1);
            break;

        case KEY_SHIFT_TAB:
            complete(-1);
            break;

        case CTRL('['):
        case CTRL('C'):
            mode_enter('n');
            vb_set_input_text("");
            break;

        case KEY_CR:
            input_activate();
            break;

        case KEY_UP:
            history(true);
            break;

        case KEY_DOWN:
            history(false);
            break;

        /* basic command line editing */
        case CTRL('H'):
            /* delete the last char before the cursor */
            mark = gtk_text_buffer_get_insert(buffer);
            gtk_text_buffer_get_iter_at_mark(buffer, &start, mark);
            gtk_text_buffer_backspace(buffer, &start, true, true);
            break;

        case CTRL('W'):
            /* delete word backward from cursor */
            mark = gtk_text_buffer_get_insert(buffer);
            gtk_text_buffer_get_iter_at_mark(buffer, &end, mark);

            /* copy the iter to build start and end point for deletion */
            start = end;

            /* move the iterator to the beginning of previous word */
            if (gtk_text_iter_backward_word_start(&start)) {
                gtk_text_buffer_delete(buffer, &start, &end);
            }
            break;

        case CTRL('B'):
            /* move the cursor direct behind the prompt */
            gtk_text_buffer_get_iter_at_offset(buffer, &start, strlen(vb.state.prompt));
            gtk_text_buffer_place_cursor(buffer, &start);
            break;

        case CTRL('E'):
            /* move the cursor to the end of line */
            gtk_text_buffer_get_end_iter(buffer, &start);
            gtk_text_buffer_place_cursor(buffer, &start);
            break;

        case CTRL('U'):
            /* remove everythings between cursor and prompt */
            mark = gtk_text_buffer_get_insert(buffer);
            gtk_text_buffer_get_iter_at_mark(buffer, &end, mark);
            gtk_text_buffer_get_iter_at_offset(buffer, &start, strlen(vb.state.prompt));
            gtk_text_buffer_delete(buffer, &start, &end);
            break;

        default:
            /* if is printable ascii char, than write it at the cursor
             * position into input box */
            if (key >= 0x20 && key <= 0x7e) {
                gtk_text_buffer_insert_at_cursor(buffer, (char[2]){key, 0}, 1);
            } else {
示例#2
0
文件: ex.c 项目: yzhou61/vimb
/**
 * Handles the keypress events from webview and inputbox.
 */
VbResult ex_keypress(int key)
{
    GtkTextIter start, end;
    gboolean check_empty  = false;
    GtkTextBuffer *buffer = vb.gui.buffer;
    GtkTextMark *mark;
    VbResult res;
    const char *text;

    /* delegate call to hint mode if this is active */
    if (vb.mode->flags & FLAG_HINTING
        && RESULT_COMPLETE == hints_keypress(key)) {

        return RESULT_COMPLETE;
    }

    /* process the register */
    if (info.phase == PHASE_REG) {
        info.reg   = (char)key;
        info.phase = PHASE_REG;

        /* insert the register text at cursor position */
        text = vb_register_get((char)key);
        if (text) {
            gtk_text_buffer_insert_at_cursor(buffer, text, strlen(text));
        }

        res = RESULT_COMPLETE;
    } else {
        res = RESULT_COMPLETE;
        switch (key) {
            case KEY_TAB:
                complete(1);
                break;

            case KEY_SHIFT_TAB:
                complete(-1);
                break;

            case CTRL('['):
            case CTRL('C'):
                vb_enter('n');
                vb_set_input_text("");
                break;

            case KEY_CR:
                input_activate();
                break;

            case KEY_UP:
                history(true);
                break;

            case KEY_DOWN:
                history(false);
                break;

            /* basic command line editing */
            case CTRL('H'):
                /* delete the last char before the cursor */
                mark = gtk_text_buffer_get_insert(buffer);
                gtk_text_buffer_get_iter_at_mark(buffer, &start, mark);
                gtk_text_buffer_backspace(buffer, &start, true, true);
                check_empty = true;
                break;

            case CTRL('W'):
                /* delete word backward from cursor */
                mark = gtk_text_buffer_get_insert(buffer);
                gtk_text_buffer_get_iter_at_mark(buffer, &end, mark);

                /* copy the iter to build start and end point for deletion */
                start = end;

                /* move the iterator to the beginning of previous word */
                if (gtk_text_iter_backward_word_start(&start)) {
                    gtk_text_buffer_delete(buffer, &start, &end);
                }
                check_empty = true;
                break;

            case CTRL('B'):
                /* move the cursor direct behind the prompt */
                gtk_text_buffer_get_iter_at_offset(buffer, &start, strlen(vb.state.prompt));
                gtk_text_buffer_place_cursor(buffer, &start);
                break;

            case CTRL('E'):
                /* move the cursor to the end of line */
                gtk_text_buffer_get_end_iter(buffer, &start);
                gtk_text_buffer_place_cursor(buffer, &start);
                break;

            case CTRL('U'):
                /* remove everything between cursor and prompt */
                mark = gtk_text_buffer_get_insert(buffer);
                gtk_text_buffer_get_iter_at_mark(buffer, &end, mark);
                gtk_text_buffer_get_iter_at_offset(buffer, &start, strlen(vb.state.prompt));
                gtk_text_buffer_delete(buffer, &start, &end);
                break;

            case CTRL('R'):
                info.phase      = PHASE_REG;
                vb.mode->flags |= FLAG_NOMAP;
                res             = RESULT_MORE;
                break;

            default:
                /* if is printable ascii char, than write it at the cursor
                 * position into input box */
                if (key >= 0x20 && key <= 0x7e) {
                    gtk_text_buffer_insert_at_cursor(buffer, (char[2]){key, 0}, 1);
                } else {