Пример #1
0
static void actfunc_suggest(
        vtparse_t *parser,
        vtparse_action_t action,
        unsigned char ch) {
    char buf[TEXTEDITOR_MAXLEN];
    if (SUGGEST_INDEX(parser) < 0) {
        if (text_editor_get_text(
                    GET_EDITOR(parser),
                    SUGGEST_SOURCE(parser),
                    sizeof(SUGGEST_SOURCE(parser))) > 0) {
            SUGGEST_INDEX(parser) = 0;
            if (text_history_find(
                        GET_HISTORY(parser),
                        SUGGEST_INDEX(parser),
                        SUGGEST_SOURCE(parser),
                        buf,
                        sizeof(buf)) == 0) {
                int n = ntlibc_strlen((const char *)buf);
                SERIAL_WRITE(parser, "\x1b[2K", 4);
                SERIAL_WRITE(parser, "\x1b[80D", 5);
                SERIAL_WRITE(parser, ">", 1);
                SERIAL_WRITE(parser, buf, n);
                text_editor_set_text(GET_EDITOR(parser), buf);
            } else {
                SUGGEST_INDEX(parser) = -1;
            }
        }
    } else {
        SUGGEST_INDEX(parser) = SUGGEST_INDEX(parser) + 1;
        if (text_history_find(
                    GET_HISTORY(parser),
                    SUGGEST_INDEX(parser),
                    SUGGEST_SOURCE(parser),
                    buf,
                    sizeof(buf)) == 0) {
            int n = ntlibc_strlen((const char *)buf);
            SERIAL_WRITE(parser, "\x1b[2K", 4);
            SERIAL_WRITE(parser, "\x1b[80D", 5);
            SERIAL_WRITE(parser, ">", 1);
            SERIAL_WRITE(parser, buf, n);
            text_editor_set_text(GET_EDITOR(parser), buf);
        } else {
            int n = ntlibc_strlen(SUGGEST_SOURCE(parser));
            SERIAL_WRITE(parser, "\x1b[2K", 4);
            SERIAL_WRITE(parser, "\x1b[80D", 5);
            SERIAL_WRITE(parser, ">", 1);
            SERIAL_WRITE(parser, SUGGEST_SOURCE(parser), n);
            text_editor_set_text(GET_EDITOR(parser), SUGGEST_SOURCE(parser));
            SUGGEST_INDEX(parser) = -1;
        }
    }
}
Пример #2
0
static void actfunc_cursor_tail(
        vtparse_t *parser,
        vtparse_action_t action,
        unsigned char ch) {
    char buf[TEXTEDITOR_MAXLEN];
    int len;
    text_editor_get_text(GET_EDITOR(parser), buf, sizeof(buf));
    len = ntlibc_strlen((const char *)buf);
    SERIAL_WRITE(parser, "\x1b[80D", 5);
    SERIAL_WRITE(parser, ">", 1);
    SERIAL_WRITE(parser, buf, len);
    text_editor_cursor_tail(GET_EDITOR(parser));
}
/**
 * @brief 与えられたテキストで始まる文字列を探す。
 * @details このインターフェースはテキスト入力補完のために作られた。
 *
 * @param p テキストヒストリオブジェクト。
 * @param index ヒストリ中で見つかる文字列のindex個目。
 * @param text 検索する文字列。
 * @param buf 格納先バッファ。
 * @param siz 格納先バッファサイズ。
 *
 * @retval 0 成功。
 * @retval 0以外 失敗。
 */
int text_history_find(text_history_t *p,
        const int index, const char *text,
        char *buf, const int siz)
{
    const int text_len = ntlibc_strlen((const char *)text);
    int found = 0;
    int i;
    for (i = 0; i < TEXTHISTORY_DEPTH; i++) {
        int target = (p->rp + i) % TEXTHISTORY_DEPTH;
        char *txtp = p->history + (TEXTHISTORY_MAXLEN * target);
        const int target_len = ntlibc_strlen((const char *)txtp);
        int comp_len = (target_len < text_len) ? target_len : text_len;
        if ((ntlibc_strncmp(
                    (const char *)txtp,
                    (const char *)text, comp_len) == 0) && (comp_len > 0)) {
            if (found == index) {
                ntlibc_strcpy((char *)buf, (char *)txtp);
                return 0;
            }
            found++;
        }
    }
    return -1;
}