示例#1
0
文件: main.c 项目: SamuelGrace/vic
int main(int argc, char *argv[])
{
    cur_file = malloc(sizeof(v_file_text));
    switch (argc)
    {
        default:
        case 1:
            cur_file_name[0] = 0;
            v_new_file(cur_file);
            break;
        case 2:
            strcpy(cur_file_name, argv[1]);
            v_load_file(cur_file_name, cur_file);
            break;
    }

    init_global();

    redraw_ui();
    init_display_back();
    disable_display_back();
    main_loop();
    enable_display_back();

    return 0;
}
示例#2
0
文件: normal_loop.c 项目: poiiii/vic
int normal_mode_process(int key_down)
{
#ifdef __VIC_POSIX
    int second_key_down = 0;
#endif

    char t[100];
    sprintf(t, "%x", key_down);
    print_log(t);

    switch (key_down)
    {
        case '\x1b':  //Esc
#ifdef __VIC_POSIX
            second_key_down = getchar();
            switch (second_key_down)    //double stroke Esc to return to normal mode.
            {
                case '\x5b':
                    second_key_down = getchar();
                    switch (second_key_down)
                    {
                        case '\x41':     //up
                            cursor_up();
                            break;

                        case '\x42':     //down
                            cursor_down();
                            break;

                        case '\x43':     //right
                            cursor_right();
                            break;

                        case '\x44':     //left
                            cursor_left();
                            break;

                        default:
                            break;
                    }
                    break;

                default:
                    break;
            }
#endif

#ifdef __VIC_WIN

#endif
            break;

#ifdef __VIC_WIN

            /*
        case '\xe0':    //first(or last maybe?) ascii of arrow keys.
        case '\x00':
            switch (getch())
            {
                case '\x48':    //up
                    cursor_up();
                    break;

                case '\x50':    //down
                    cursor_down();
                    break;

                case '\x4b':    //left
                    cursor_left();
                    break;

                case '\x4d':    //right
                    cursor_right();
                    break;

                default:
                    break;
            }
            break;
             */

        case '\x48':    //up
            cursor_up();
            break;

        case '\x50':    //down
            cursor_down();
            break;

        case '\x4b':    //left
            cursor_left();
            break;

        case '\x4d':    //right
            cursor_right();
            break;
#endif

        case '!':   //quit directly.
            enable_display_back();
            set_cursor_pos(console_columns, console_lines);
            exit(0);
            break;

        case '1':   //New File.
        case 'n':
            if (changed_flag == UNCHANGED || changed_flag == UNSAVED)
            {
                enable_display_back();
                set_cursor_pos(console_columns, console_lines);
            }
            else
            {
                changed_flag = UNSAVED;
            }
            break;

        case 'q':   //quit, is saved should be checked.
            if (changed_flag == UNCHANGED || changed_flag == UNSAVED)
            {
                enable_display_back();
                set_cursor_pos(console_columns, console_lines);
                exit(0);
            }
            else
            {
                changed_flag = UNSAVED;
            }
            break;

            //belows hjkl for cursor moving.
            //aiming to disable moving cursor outside the file part.
        case 'h':
            cursor_left();
            break;

        case 'l':
            cursor_right();
            break;

        case 'j':
            cursor_down();
            break;

        case 'k':
            cursor_up();
            break;

        case 'w':   //word forward.
        {
            int position = cur_left + cur_column - 1;
            int counter = 0;
            char *cur = get_line(cur_file, cur_top + cur_line - 1)->text;
            cur += cur_left + cur_column - 2;   //to be checked.
            if (is_word_start(cur))
            {
                position++;
                counter++;
                cur++;
            }
            for (; position < get_length(get_line(cur_file, cur_line + cur_top - 1)); position++, counter++)
            {
                if (is_word_start(cur++))
                {
                    break;
                }
            }
            for (; counter > 0; counter--)
            {
                cursor_right();
            }
        }
            break;

        case 'b':   //word backward.
        {
            int position = cur_left + cur_column - 1;
            int counter = 0;
            char *cur = get_line(cur_file, cur_top + cur_line - 1)->text;
            cur += cur_left + cur_column - 2;   //to be checked.
            if (is_word_start(cur))
            {
                position--;
                counter++;
                cur--;
            }
            for (; position > 0; position--, counter++)
            {
                if (is_word_start(cur--))
                {
                    break;
                }
            }
            for (; counter > 0; counter--)
            {
                cursor_left();
            }
        }
            break;

        case 'e':   //word-end forward.
        {
            int position = cur_left + cur_column - 1;
            int counter = 0;
            char *cur = get_line(cur_file, cur_top + cur_line - 1)->text;
            cur += cur_left + cur_column - 2;   //to be checked.
            if (is_word_end(cur))
            {
                position++;
                counter++;
                cur++;
            }
            for (; position < get_length(get_line(cur_file, cur_line + cur_top - 1)); position++, counter++)
            {
                if (is_word_end(cur++))
                {
                    break;
                }
            }
            for (; counter > 0; counter--)
            {
                cursor_right();
            }
        }
            break;

        case 'i':   //insert.
            mode_flag = INSERT_MODE;
            break;

        case 'a':   //append.
            cursor_right();
            mode_flag = INSERT_MODE;
            break;

        case 'd':   //delete a word.
        {
            v_line *this_line = get_line(cur_file, cur_top + cur_line - 1);
            unsigned int word_len = 0;
            unsigned int start_index = cur_left + cur_column - 2;
            unsigned int end_index = judge_word(this_line, start_index + 1);
            word_len = end_index - start_index + 1;
            for (int i = 0; i < end_index - start_index + 1; i++)
            {
                del_char(this_line, cur_left + cur_column - 2);
            }
            if (word_len != 0)
            {
                changed_flag = CHANGED;
            }
        }
            break;

        case 'x':   //delete a single char.
            del_char(get_line(cur_file, cur_top + cur_line - 1), cur_left + cur_column - 2);
            changed_flag = CHANGED;
            break;

        case 'o':   //open new line.
            insert_empty_line(cur_file, cur_top + cur_line - 1);
            cursor_down();
            mode_flag = INSERT_MODE;
            break;

        case '$':
            goto_line_end();
            break;

        case '^':   //goto first char of line.
            goto_line_actual_start();
            break;

        case '0':   //goto fixed first column.
            goto_line_start();
            break;

        case '3':
        case ':':   //bottom line command mode.
            //Now I wanna use this as open file command.
            if (changed_flag == CHANGED)
            {
                changed_flag = UNSAVED;
                break;
            }
            mode_flag = BOTTOMLINE_MODE;
            bottomline_sub_mode = BOTTOM_LINE_FILENAME_OPEN;
            break;

        case '/':    //bottom line and search.
            break;

        case '2':
        case 's':
            if (strlen(cur_file_name) == 0)
            {
                mode_flag = BOTTOMLINE_MODE;
                bottomline_sub_mode = BOTTOM_LINE_FILENAME_SAVE;
            }
            else
            {
                v_save_file(cur_file_name, cur_file);
                changed_flag = UNCHANGED;
            }
            break;

#ifdef __VIC_WIN
        case '\x00':
        case '\xe0':
#endif
        default:
            break;
    }
    return 0;
}