sled

Једноставни уређивач текста
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 7a652755a9d5c953d5fa9fc8a31a19459a1d1509
родитељ 1021a3f538bcb0dd6c1633e78bb64ea28d9f0f4c
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Sun, 27 Feb 2022 16:52:05 +0100

Move positioning logic to separate function

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
MTODO | 4+---
Mdraw.c | 43+++++++++++++++++++++++--------------------
Mdraw.h | 10+++++-----
Msled.c | 689+++++++++++++++++++------------------------------------------------------------
измењених датотека: 4, додавања: 191(+), брисања: 555(-)

diff --git a/TODO b/TODO @@ -1,12 +1,10 @@ TODO ==== - [ ] Move positioning logic to separate function + [x] Move positioning logic to separate function [x] Investigate DrawState.first_shown_column - [/] Add vi V - < > Search? < > Replace? diff --git a/draw.c b/draw.c @@ -93,7 +93,7 @@ free_bufferline(struct BufferLine* bl) void init_drawstate(struct DrawState* state, char* error, char* action, - char* filename, char* prompt) + char* filename, char* prompt, int skip_paste_and_mode_init) { state->error = error; state->action = action; @@ -113,25 +113,28 @@ init_drawstate(struct DrawState* state, char* error, char* action, state->saved_column = -1; state->help_width = calculate_help_width(state); state->input_dialog_width = calculate_input_dialog_width(state); - state->paste = NULL; - state->paste_count = 0; - state->paste_size = 0; - state->prompt = prompt; - state->prompt_callback = NULL; - state->input_callback = NULL; - state->running = 1; - state->dirty = 0; - state->in_prompt = 0; - state->in_command_mode = 0; - state->in_overwrite_mode = 0; - state->in_selection_mode = 0; - state->show_help = 0; - state->show_input_dialog = 0; - state->input_numerical = 0; - state->sel_start_row = 0; - state->sel_start_col = 0; - state->sel_end_row = 0; - state->sel_end_col = 0; + if (!skip_paste_and_mode_init) + { + state->paste = NULL; + state->paste_count = 0; + state->paste_size = 0; + state->in_overwrite_mode = 0; + state->in_command_mode = 0; + } + state->prompt = prompt; + state->prompt_callback = NULL; + state->input_callback = NULL; + state->running = 1; + state->dirty = 0; + state->in_prompt = 0; + state->in_selection_mode = 0; + state->show_help = 0; + state->show_input_dialog = 0; + state->input_numerical = 0; + state->sel_start_row = 0; + state->sel_start_col = 0; + state->sel_end_row = 0; + state->sel_end_col = 0; } void diff --git a/draw.h b/draw.h @@ -67,10 +67,10 @@ struct DrawState { int input_numerical; int maxx; int maxy; - size_t sel_start_row; - size_t sel_start_col; - size_t sel_end_row; - size_t sel_end_col; + int sel_start_row; + int sel_start_col; + int sel_end_row; + int sel_end_col; }; struct Key { @@ -89,7 +89,7 @@ struct StatusSegment { void init_bufferline(struct BufferLine* bl); void free_bufferline(struct BufferLine* bl); void init_drawstate(struct DrawState* state, char* error, char* action, - char* filename, char* prompt); + char* filename, char* prompt, int skip_paste_and_mode_init); void free_drawstate(struct DrawState* state); size_t display_length(const struct DrawState* state, uint32_t* buffer, const size_t max_index); diff --git a/sled.c b/sled.c @@ -56,10 +56,12 @@ int erase_selection(struct DrawState* state); void go_to_line_callback(struct DrawState* state); void handle_key_event(struct tb_event* ev, struct DrawState* state); int input_insert_char(struct DrawState* state, uint32_t ch); +void input_position_to(struct DrawState* state, const int column); int insert_char(struct DrawState* state, uint32_t ch); struct DrawState* join_lines(struct DrawState* state, size_t first_line, int forward); int load_file(struct DrawState* dstate, long* lineno, long* col); +void position_to(struct DrawState* state, const int row, const int column); int position_to_selection_start(struct DrawState* state); int print_error(const int code, const char* msg, ...); void reread_callback(struct DrawState* state, struct tb_event* ev); @@ -118,11 +120,11 @@ erase_selection(struct DrawState* state) if (SEL_NOT_EMPTY(state)) { /* Selection start row/col */ - size_t ssr = 0; - size_t ssc = 0; + int ssr = 0; + int ssc = 0; /* Selection end row/col */ - size_t ser = 0; - size_t sec = 0; + int ser = 0; + int sec = 0; uint32_t* buf = NULL; buf = calloc(MAXBUFLINE, sizeof(uint32_t)); @@ -200,7 +202,7 @@ handle_key_event(struct tb_event* ev, struct DrawState* state) if (ev->key != TB_KEY_ARROW_DOWN && ev->key != TB_KEY_ARROW_UP && ev->key != TB_KEY_PGDN && ev->key != TB_KEY_PGUP && ev->key != TB_KEY_CTRL_B && ev->key != TB_KEY_CTRL_F - && !(state->in_command_mode && strchr("jkgG", ev->ch))) + && !(state->in_command_mode && strchr("jkgGHLM", ev->ch))) state->saved_column = -1; if (state->show_input_dialog) @@ -301,26 +303,29 @@ input_insert_char(struct DrawState* state, uint32_t ch) state->input[i] = state->input[i - 1]; i--; } - state->input[state->input_column++] = ch; - if (state->input_column == MAXINPUT) - state->input_column = MAXINPUT - 1; - state->input_display_column = display_length(state, - state->input, state->input_column); - if (state->input_display_column - state->input_first_shown_column - > state->input_dialog_width - 4) - { - state->input_first_shown_column - = state->input_display_column - - state->input_dialog_width + 4; - state->input_display_column = display_length(state, - state->input, state->input_column); - } + state->input[state->input_column] = ch; + input_position_to(state, state->input_column + 1); if (add_null) state->input[state->input_column] = 0; } return 0; } +void +input_position_to(struct DrawState* state, const int column) +{ + state->input_column = column; + + if (state->input_column >= MAXINPUT) + state->input_column = MAXINPUT - 1; + state->input_display_column + = display_length(state, state->input, state->input_column); + if (state->input_display_column > state->input_first_shown_column + + state->input_dialog_width - 4) + state->input_first_shown_column = state->input_display_column + - (state->input_dialog_width - 4); +} + int insert_char(struct DrawState* state, uint32_t ch) { @@ -334,8 +339,8 @@ insert_char(struct DrawState* state, uint32_t ch) if (SEL_NOT_EMPTY(state)) { - position_to_selection_start(state); erase_selection(state); + position_to_selection_start(state); clear_selection(state); } @@ -350,20 +355,10 @@ insert_char(struct DrawState* state, uint32_t ch) buffer_line->text[i] = buffer_line->text[i - 1]; i--; } - buffer_line->text[state->column++] = ch; + buffer_line->text[state->column] = ch; if (!state->in_overwrite_mode) buffer_line->length++; - if (state->column == MAXBUFLINE) - state->column = MAXBUFLINE - 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->display_column - state->first_shown_column > state->maxx - 1) - { - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - } + position_to(state, state->row, state->column + 1); if (add_null) buffer_line->text[state->column] = 0; state->dirty = 1; @@ -489,28 +484,84 @@ load_file(struct DrawState* state, long* lineno, long* col) return LOAD_ERR_NONE; } -int -position_to_selection_start(struct DrawState* state) +void +position_to(struct DrawState* state, const int row, const int column) { - if (SEL_NOT_EMPTY(state)) + size_t dlen; + + if (row != state->row) { - state->row = state->sel_start_row; - state->column = state->sel_start_col; + state->row = row; + if (state->row > state->rows_count - 1) + state->row = state->rows_count - 1; + if (state->first_shown_row > state->row) + state->first_shown_row = state->row; + if (state->first_shown_row + (state->maxy - 2) < state->row) + state->first_shown_row = state->row - (state->maxy - 2); + if (state->saved_column == -1) + { + state->saved_column = state->display_column; + state->column = column; + } + else + state->column = buffer_length(state, + state->buffer[state->row].text, + state->saved_column); + if (state->column > state->buffer[state->row].length) + state->column = state->buffer[state->row].length; + dlen = display_length(state, state->buffer[state->row].text, + state->column); + if (dlen < state->saved_column) + state->display_column = dlen; + else + state->display_column = state->saved_column; + } + else + { + state->column = column; + if (state->column > state->buffer[state->row].length) + state->column = state->buffer[state->row].length; state->display_column = display_length(state, - state->buffer[state->row].text, state->column - 1); + state->buffer[state->row].text, state->column); state->saved_column = state->display_column; - + if (state->row > state->rows_count - 1) + state->row = state->rows_count - 1; if (state->first_shown_row > state->row) state->first_shown_row = state->row; - else if (state->first_shown_row + (state->maxy - 2) < state->row) + if (state->first_shown_row + (state->maxy - 2) < state->row) state->first_shown_row = state->row - (state->maxy - 2); + } - if (state->display_column < state->first_shown_column) - state->first_shown_column = state->display_column; - else if (state->display_column - state->first_shown_column - > state->maxx - 1) - state->first_shown_column - = state->display_column - state->maxx + 1; + if (state->in_selection_mode) + { + state->sel_end_row = state->row; + state->sel_end_col = state->column; + } +} + +int +position_to_selection_start(struct DrawState* state) +{ + if (SEL_NOT_EMPTY(state)) + { + int ssr = 0; + int ssc = 0; + + if (state->sel_start_row <= state->sel_end_row) + { + ssr = state->sel_start_row; + ssc = state->sel_start_col; + } + else /* Swap */ + { + ssr = state->sel_end_row; + ssc = state->sel_end_col; + } + + state->saved_column + = display_length(state, state->buffer[ssr].text, ssc); + position_to(state, ssr, ssc); + state->saved_column = state->display_column; } return 0; } @@ -549,11 +600,11 @@ reread_file(struct DrawState* state) for (row = 0; row < state->buffer_size; row++) free_bufferline(state->buffer + row); free(state->buffer); - for (row = 0; row < state->paste_size; row++) + /*for (row = 0; row < state->paste_size; row++) free_bufferline(state->paste + row); - free(state->paste); + free(state->paste);*/ init_drawstate(state, state->error, state->action, state->filename, - state->prompt); + state->prompt, 1); result = load_file(state, &lineno, &col); switch (result) @@ -692,8 +743,8 @@ erase_backwards(struct DrawState* state) if (SEL_NOT_EMPTY(state)) { - position_to_selection_start(state); erase_selection(state); + position_to_selection_start(state); clear_selection(state); return 0; } @@ -708,20 +759,7 @@ erase_backwards(struct DrawState* state) } while (buffer_line->text[i]); buffer_line->text[i - 1] = 0; buffer_line->length--; - state->column--; - if (state->first_shown_column > 0) - { - const struct BufferLine* current_line - = &state->buffer[state->row]; - if (display_length(state, current_line->text, - current_line->length) - >= state->maxx - 1) - state->first_shown_column--; - } - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->display_column < state->first_shown_column) - state->first_shown_column = state->display_column; + position_to(state, state->row, state->column - 1); state->dirty = 1; } else if (state->row > 0) @@ -740,8 +778,8 @@ erase_forward(struct DrawState* state) if (SEL_NOT_EMPTY(state)) { - position_to_selection_start(state); erase_selection(state); + position_to_selection_start(state); clear_selection(state); return 0; } @@ -789,25 +827,7 @@ erase_line(struct DrawState* state) state->buffer[state->rows_count - 1].length = 0; if (state->row == state->rows_count - 1) state->row--; - if (state->first_shown_row > state->row) - state->first_shown_row = state->row; - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, state->buffer[state->row].text, - state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); + position_to(state, state->row, 0); state->rows_count--; state->dirty = 1; return 0; @@ -851,8 +871,8 @@ erase_to_end(struct DrawState* state) if (SEL_NOT_EMPTY(state)) { - position_to_selection_start(state); erase_selection(state); + position_to_selection_start(state); clear_selection(state); return 0; } @@ -873,8 +893,8 @@ erase_to_start(struct DrawState* state) if (SEL_NOT_EMPTY(state)) { - position_to_selection_start(state); erase_selection(state); + position_to_selection_start(state); clear_selection(state); return 0; } @@ -945,20 +965,7 @@ input_erase_backwards(struct DrawState* state) i++; } while (state->input[i]); state->input[i - 1] = 0; - state->input_column--; - if (state->input_first_shown_column > 0) - { - if (display_length(state, state->input, - u32_strlen(state->input)) - >= state->maxx - 1) - state->input_first_shown_column--; - } - state->input_display_column = display_length(state, - state->input, state->input_column); - if (state->input_display_column - < state->input_first_shown_column) - state->input_first_shown_column - = state->input_display_column; + input_position_to(state, state->input_column - 1); state->dirty = 1; } return 0; @@ -1022,16 +1029,7 @@ input_insert_tab(struct DrawState* state) int input_move_end(struct DrawState* state) { - size_t len = u32_strlen(state->input); - size_t dlen; - - state->input_column = len; - dlen = display_length(state, state->input, state->input_column); - state->input_display_column = dlen; - if (dlen > state->maxx - 4) - state->input_first_shown_column = dlen - state->maxx + 4; - else - state->input_first_shown_column = 0; + input_position_to(state, u32_strlen(state->input)); return 0; } @@ -1040,46 +1038,21 @@ input_move_left(struct DrawState* state) { if (state->input_column == 0) return 0; - - if (state->input[state->input_column - 1] == '\t') - state->input_display_column = display_length(state, - state->input, state->input_column - 1); - else - state->input_display_column--; - state->input_column--; - - if (state->input_display_column < state->input_first_shown_column) - state->input_first_shown_column = state->input_display_column; + input_position_to(state, state->input_column - 1); return 0; } int input_move_right(struct DrawState* state) { - size_t len = u32_strlen(state->input); - if (state->input_column == len) - return 0; - - if (state->input[state->input_column] == '\t') - state->input_display_column = display_length(state, - state->input, state->input_column + 1); - else - state->input_display_column++; - state->input_column++; - - if (state->input_display_column - state->input_first_shown_column - > state->maxx - 1) - state->input_first_shown_column - = state->input_display_column - state->maxx + 1; + input_position_to(state, state->input_column + 1); return 0; } int input_move_start(struct DrawState* state) { - state->input_column = 0; - state->input_display_column = 0; - state->input_first_shown_column = 0; + input_position_to(state, 0); return 0; } @@ -1096,12 +1069,7 @@ input_move_prev_word(struct DrawState* state) while (i > 0 && !u32_is_word_boundary(pline[i], 0) && !u32_is_word_boundary(pline[i - 1], 0)) i--; - - state->input_column = i; - state->input_display_column - = display_length(state, state->input, state->input_column); - if (state->input_display_column < state->input_first_shown_column) - state->input_first_shown_column = state->input_display_column; + input_position_to(state, i); return 0; } @@ -1116,13 +1084,7 @@ input_move_next_word(struct DrawState* state) i++; while (i < len && u32_is_word_boundary(pline[i], 0)) i++; - state->input_column = i; - state->input_display_column - = display_length(state, state->input, state->input_column); - if (state->input_display_column - state->input_first_shown_column - > state->maxx - 1) - state->input_first_shown_column - = state->input_display_column - state->maxx + 1; + input_position_to(state, i); return 0; } @@ -1197,111 +1159,30 @@ insert_tab(struct DrawState* state) int move_bottom(struct DrawState* state) { - state->row = state->rows_count - 1; - state->first_shown_row - = state->row > state->maxy ? state->row - state->maxy + 2 : 0; - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, state->buffer[state->row].text, - state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + position_to(state, state->rows_count > 1 ? state->rows_count - 1 : 0, + state->column); return 0; } int move_down(struct DrawState* state) { - if (state->row == state->rows_count - 1) - return 0; - if (state->row < state->rows_count - 1) - state->row++; - if (state->first_shown_row + (state->maxy - 2) < state->row) - state->first_shown_row = state->row - (state->maxy - 2); - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, state->buffer[state->row].text, - state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + position_to(state, state->row + 1, state->column); return 0; } int move_end(struct DrawState* state) { - size_t len; - size_t dlen; - - len = state->buffer[state->row].length; - state->column = len; - dlen = display_length(state, state->buffer[state->row].text, - state->column); - state->display_column = dlen; - if (dlen > state->maxx - 4) - state->first_shown_column = dlen - state->maxx + 4; - else - state->first_shown_column = 0; - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + size_t len = state->buffer[state->row].length; + position_to(state, state->row, len); return 0; } int move_left(struct DrawState* state) { - if (state->column == 0) - return 0; - - if (state->buffer[state->row].text[state->column - 1] == '\t') - state->display_column = display_length(state, - state->buffer[state->row].text, state->column - 1); - else - state->display_column--; - state->column--; - - if (state->display_column < state->first_shown_column) - state->first_shown_column = state->display_column; - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + position_to(state, state->row, state->column - 1); return 0; } @@ -1316,17 +1197,7 @@ move_next_word(struct DrawState* state) i++; while (i < len && u32_is_word_boundary(pline[i], 0)) i++; - state->column = i; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->display_column - state->first_shown_column > state->maxx - 1) - state->first_shown_column - = state->display_column - state->maxx + 1; - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + position_to(state, state->row, i); return 0; } @@ -1334,37 +1205,9 @@ int move_page_down(struct DrawState* state) { if (state->row + state->maxy - 2 < state->rows_count) - { - state->row += state->maxy - 2; - if (state->row > state->first_shown_row + state->maxy - 2) - state->first_shown_row = state->row - (state->maxy - 2); - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen - = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, - state->buffer[state->row].text, state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 - < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - } + position_to(state, state->row + state->maxy - 2, state->column); else move_bottom(state); - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } return 0; } @@ -1372,37 +1215,10 @@ int move_page_up(struct DrawState* state) { if (state->row > state->maxy - 2) - { - state->row -= state->maxy - 2; - if (state->row < state->first_shown_row) - state->first_shown_row = state->row; - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen - = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, - state->buffer[state->row].text, state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 - < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - } + position_to(state, state->row - (state->maxy - 2), + state->column); else move_top(state); - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } return 0; } @@ -1419,42 +1235,14 @@ move_prev_word(struct DrawState* state) while (i > 0 && !u32_is_word_boundary(pline[i], 0) && !u32_is_word_boundary(pline[i - 1], 0)) i--; - - state->column = i; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->display_column < state->first_shown_column) - state->first_shown_column = state->display_column; - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + position_to(state, state->row, i); return 0; } int move_right(struct DrawState* state) { - size_t len = state->buffer[state->row].length; - if (state->column == len) - return 0; - - if (state->buffer[state->row].text[state->column] == '\t') - state->display_column = display_length(state, - state->buffer[state->row].text, state->column + 1); - else - state->display_column++; - state->column++; - - if (state->display_column - state->first_shown_column > state->maxx - 1) - state->first_shown_column - = state->display_column - state->maxx + 1; - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + position_to(state, state->row, state->column + 1); return 0; } @@ -1462,36 +1250,13 @@ int move_screen_bottom(struct DrawState* state) { if (state->rows_count < state->maxy - 2) - { - state->row = state->rows_count - 1; - state->first_shown_row = 0; - } + position_to(state, state->rows_count - 1, state->column); else { if (state->first_shown_row + state->maxy - 2 < state->rows_count) - state->row = state->first_shown_row + state->maxy - 2; - } - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, state->buffer[state->row].text, - state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; + position_to(state, + state->first_shown_row + state->maxy - 2, + state->column); } return 0; } @@ -1500,37 +1265,13 @@ int move_screen_middle(struct DrawState* state) { if (state->rows_count < state->maxy - 2) - { - state->row = state->rows_count / 2; - state->first_shown_row = 0; - } + position_to(state, state->rows_count / 2, state->column); else { - state->row = state->first_shown_row + (state->maxy - 2) / 2; - if (state->rows_count - state->row < (state->maxy - 2) / 2) - state->row = state->rows_count - (state->maxy - 2) / 2; - } - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, state->buffer[state->row].text, - state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; + int row = state->first_shown_row + (state->maxy - 2) / 2; + if (state->rows_count - row < (state->maxy - 2) / 2) + row = state->rows_count - (state->maxy - 2) / 2; + position_to(state, row, state->column); } return 0; } @@ -1538,108 +1279,28 @@ move_screen_middle(struct DrawState* state) int move_screen_top(struct DrawState* state) { - if (state->row == 0) - return 0; - state->row = state->first_shown_row; - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, state->buffer[state->row].text, - state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + position_to(state, state->first_shown_row, state->column); return 0; } int move_start(struct DrawState* state) { - state->column = 0; - state->display_column = 0; - state->first_shown_column = 0; - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + position_to(state, state->row, 0); return 0; } int move_top(struct DrawState* state) { - state->row = 0; - state->first_shown_row = 0; - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, state->buffer[state->row].text, - state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + position_to(state, 0, state->column); return 0; } int move_up(struct DrawState* state) { - if (state->row == 0) - return 0; - state->row--; - if (state->first_shown_row > state->row) - state->first_shown_row = state->row; - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, state->buffer[state->row].text, - state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + position_to(state, state->row > 1 ? state->row - 1 : 0, state->column); return 0; } @@ -1656,8 +1317,8 @@ paste_from_paste(struct DrawState* state) if (SEL_NOT_EMPTY(state)) { - position_to_selection_start(state); erase_selection(state); + position_to_selection_start(state); clear_selection(state); } @@ -1736,15 +1397,7 @@ paste_from_paste(struct DrawState* state) state->row += state->paste_count - 1; } - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - if (state->first_shown_row + (state->maxy - 2) < state->row) - state->first_shown_row = state->row - (state->maxy - 2); + position_to(state, state->row, state->column); state->dirty = 1; paste_from_paste_cleanup: @@ -1862,16 +1515,25 @@ int yank_to_paste(struct DrawState* state) { /* Selection start row/col */ - size_t ssr = 0; - size_t ssc = 0; + int ssr = 0; + int ssc = 0; /* Selection end row/col */ - size_t ser = 0; - size_t sec = 0; + int ser = 0; + int sec = 0; struct BufferLine* newbuf = NULL; size_t initialize_start; if (!SEL_NOT_EMPTY(state)) + { + if (state->paste_count > 0) + { + state->paste_count = 0; + *state->paste[0].text = 0; + state->paste[0].length = 0; + } + clear_selection(state); return 0; + } if (state->sel_start_row <= state->sel_end_row) { @@ -1966,43 +1628,16 @@ go_to_line_callback(struct DrawState* state) lineno = strtoul(u8_input, NULL, 10); if (errno) strcpy(state->error, errors[ERR_INVALID_NUM]); - - if (lineno > state->rows_count - 1) - lineno = state->rows_count; - else if (lineno < 1) - lineno = 1; - state->row = lineno - 1; - - if ((state->rows_count - 1 < (state->maxy - 2) / 2) - || (lineno < (state->maxy - 2) / 2)) - state->first_shown_row = 0; - else if (lineno + (state->maxy - 2) / 2 < state->rows_count - 1) - state->first_shown_row = lineno - (state->maxy - 2) / 2; - else - state->first_shown_row - = state->rows_count - (state->maxy - 2) - 1; - if (state->saved_column == -1) - state->saved_column = state->display_column; - size_t dlen - = display_length(state, state->buffer[state->row].text, - state->buffer[state->row].length); - if (dlen < state->saved_column) - state->display_column = dlen; - else - state->display_column = state->saved_column; - state->column = buffer_length(state, - state->buffer[state->row].text, state->display_column); - if (state->first_shown_column > state->display_column) - state->first_shown_column = state->display_column; - if (state->first_shown_column + state->maxx - 1 - < state->display_column) - state->first_shown_column - = state->display_column - state->maxx + 1; - state->display_column = display_length(state, - state->buffer[state->row].text, state->column); - free(u8_input); if (!*state->error) + { + if (lineno > state->rows_count - 1) + lineno = state->rows_count; + else if (lineno < 1) + lineno = 1; + position_to(state, lineno - 1, state->column); *state->input = 0; + } + free(u8_input); } if (!*state->error) state->input_callback = NULL; @@ -2054,7 +1689,7 @@ main(int argc, char** argv) return print_error(EINVAL, "No filename given"); } - init_drawstate(&state, error, action, filename, prompt); + init_drawstate(&state, error, action, filename, prompt, 0); strcpy(dirname, filename); if ((slash = strrchr(dirname, '/')))