sled

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

чување 130ffcfebc5b5aa924999ffb954a6c64dff0b211
родитељ 169bb48fdffd58734bb36efdf25464a1392cca18
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Sun, 10 Apr 2022 15:45:03 +0200

Introduce struct Coord to handle row + column

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

Diffstat:
MTODO | 2+-
Mdraw.c | 70++++++++++++++++++++++++++++++++++++++++------------------------------
Mdraw.h | 19+++++++++++--------
Msled.c | 401+++++++++++++++++++++++++++++++++++++++----------------------------------------
измењених датотека: 4, додавања: 251(+), брисања: 241(-)

diff --git a/TODO b/TODO @@ -7,7 +7,7 @@ [x] Cut - [ ] Code optimization (struct coord...) + [~] Code optimization (struct coord...) Legend diff --git a/draw.c b/draw.c @@ -80,24 +80,29 @@ init_bufferline(struct BufferLine* bl) } void +init_coord(struct Coord* c, const int col, const int row) +{ + c->col = col; + c->row = row; +} + +void init_drawstate(struct DrawState* state, char* error, char* action, char* filename, char* prompt, int skip_paste_and_mode_init) { - state->error = error; - state->action = action; - state->filename = filename; - state->input = NULL; - state->search = NULL; - state->buffer = NULL; - state->column = 0; - state->display_column = 0; - state->first_shown_column = 0; - state->first_shown_row = 0; + state->error = error; + state->action = action; + state->filename = filename; + state->input = NULL; + state->search = NULL; + state->buffer = NULL; + init_coord(&state->coord, 0, 0); + state->display_column = 0; + init_coord(&state->first_shown, 0, 0); state->input_first_shown_column = 0; state->input_display_column = 0; state->input_column = 0; state->input_title = NULL; - state->row = 0; state->rows_count = 0; state->saved_column = -1; state->help_width = calculate_help_width(state); @@ -122,10 +127,8 @@ init_drawstate(struct DrawState* state, char* error, char* action, 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; + init_coord(&state->sel_start, 0, 0); + init_coord(&state->sel_end, 0, 0); } void @@ -158,6 +161,12 @@ free_drawstate(struct DrawState* state, int skip_paste_free) } void +assign_coord(struct Coord* dest, const struct Coord src) +{ + init_coord(dest, src.col, src.row); +} + +void format_filename(char* result, size_t result_size, const char* format, const struct DrawState* state) { @@ -196,9 +205,10 @@ format_position(char* result, size_t result_size, const char* format, { if (!result) return; - snprintf(result, result_size, format, state->column + 1, state->row + 1, + snprintf(result, result_size, format, state->coord.col + 1, + state->coord.row + 1, state->rows_count > 1 - ? (state->row * 100) / (state->rows_count - 1) + ? (state->coord.row * 100) / (state->rows_count - 1) : 0); } @@ -637,7 +647,7 @@ draw_screen(const struct DrawState* state) const struct BufferLine* current = NULL; int max_screen_lines = state->maxy - 1; int cy = 0; - size_t i = state->first_shown_row; + size_t i = state->first_shown.row; /* Selection start/end index - passed to u32_draw_string */ size_t ssi = 0; size_t sei = 0; @@ -662,19 +672,19 @@ draw_screen(const struct DrawState* state) ssi = 0; sei = 0; - if (state->sel_start_row <= state->sel_end_row) + if (state->sel_start.row <= state->sel_end.row) { - ssr = state->sel_start_row; - ssc = state->sel_start_col; - ser = state->sel_end_row; - sec = state->sel_end_col; + ssr = state->sel_start.row; + ssc = state->sel_start.col; + ser = state->sel_end.row; + sec = state->sel_end.col; } else /* Swap */ { - ssr = state->sel_end_row; - ssc = state->sel_end_col; - ser = state->sel_start_row; - sec = state->sel_start_col; + ssr = state->sel_end.row; + ssc = state->sel_end.col; + ser = state->sel_start.row; + sec = state->sel_start.col; } if ((i > ssr) && (i < ser)) { @@ -698,14 +708,14 @@ draw_screen(const struct DrawState* state) if (current->text) u32_draw_string(0, cy, TEXT_FG, TEXT_BG, current->text, state->maxx, state->maxx, 0, 0, CENTER, - state->first_shown_column, 0, state->search, + state->first_shown.col, 0, state->search, SEARCH_FG, SEARCH_BG, ssi, sei, SEL_FG, SEL_BG); i++; cy++; } - tb_set_cursor(state->display_column - state->first_shown_column, - state->row - state->first_shown_row); + tb_set_cursor(state->display_column - state->first_shown.col, + state->coord.row - state->first_shown.row); return state; } diff --git a/draw.h b/draw.h @@ -23,6 +23,11 @@ typedef enum { LEFT, CENTER, RIGHT } Alignment; +struct Coord { + int col; + int row; +}; + struct BufferLine { uint32_t* text; size_t length; @@ -39,10 +44,8 @@ struct DrawState { uint32_t* search; struct BufferLine* buffer; size_t buffer_size; - int column; - int row; - int first_shown_column; - int first_shown_row; + struct Coord coord; + struct Coord first_shown; int input_first_shown_column; int input_display_column; int input_column; @@ -70,10 +73,8 @@ struct DrawState { int input_numerical; int maxx; int maxy; - int sel_start_row; - int sel_start_col; - int sel_end_row; - int sel_end_col; + struct Coord sel_start; + struct Coord sel_end; }; struct Key { @@ -90,10 +91,12 @@ struct StatusSegment { }; void init_bufferline(struct BufferLine* bl); +void init_coord(struct Coord* c, const int col, const int row); void init_drawstate(struct DrawState* state, char* error, char* action, char* filename, char* prompt, int skip_paste_and_mode_init); void free_bufferline(struct BufferLine* bl); void free_drawstate(struct DrawState* state, int skip_paste_free); +void assign_coord(struct Coord* dest, const struct Coord src); size_t display_length(const struct DrawState* state, uint32_t* buffer, const size_t max_index); size_t buffer_length(const struct DrawState* state, uint32_t* buffer, diff --git a/sled.c b/sled.c @@ -23,8 +23,8 @@ #include "config.h" #define SEL_NOT_EMPTY(state) \ - ((state->sel_start_row != state->sel_end_row) \ - || (state->sel_start_col != state->sel_end_col)) + ((state->sel_start.row != state->sel_end.row) \ + || (state->sel_start.col != state->sel_end.col)) enum { LOAD_ERR_NONE = 0, @@ -81,10 +81,8 @@ clear_selection(struct DrawState* state) if (!state) return 1; state->in_selection_mode = 0; - state->sel_start_row = 0; - state->sel_start_col = 0; - state->sel_end_row = 0; - state->sel_end_col = 0; + init_coord(&state->sel_start, 0, 0); + init_coord(&state->sel_end, 0, 0); return 0; } @@ -114,7 +112,7 @@ create_file(struct DrawState* state) state->buffer = newchunk; state->buffer_size = ALLOC_DELTA; state->rows_count = 1; - state->row = 0; + state->coord.row = 0; state->dirty = 1; return LOAD_ERR_NONE; @@ -137,19 +135,19 @@ erase_selection(struct DrawState* state) if (!buf) exit(print_error(ENOMEM, "Memory allocation failed")); - if (state->sel_start_row <= state->sel_end_row) + if (state->sel_start.row <= state->sel_end.row) { - ssr = state->sel_start_row; - ssc = state->sel_start_col; - ser = state->sel_end_row; - sec = state->sel_end_col; + ssr = state->sel_start.row; + ssc = state->sel_start.col; + ser = state->sel_end.row; + sec = state->sel_end.col; } else /* Swap */ { - ssr = state->sel_end_row; - ssc = state->sel_end_col; - ser = state->sel_start_row; - sec = state->sel_start_col; + ssr = state->sel_end.row; + ssc = state->sel_end.col; + ser = state->sel_start.row; + sec = state->sel_start.col; } u32_strcpy(buf, state->buffer[ser].text + sec); @@ -213,7 +211,7 @@ go_to_line_callback(struct DrawState* state) lineno = state->rows_count; else if (lineno < 1) lineno = 1; - position_to(state, lineno - 1, state->column); + position_to(state, lineno - 1, state->coord.col); *state->input = 0; } free(u8_input); @@ -387,7 +385,7 @@ insert_char(struct DrawState* state, uint32_t ch) int add_null; int len; - if (state->column >= MAXBUFLINE - 1) + if (state->coord.col >= MAXBUFLINE - 1) return 1; if (SEL_NOT_EMPTY(state)) @@ -397,46 +395,47 @@ insert_char(struct DrawState* state, uint32_t ch) clear_selection(state); } - buffer_line = &state->buffer[state->row]; - add_null = buffer_line->text[state->column] == 0; + buffer_line = &state->buffer[state->coord.row]; + add_null = buffer_line->text[state->coord.col] == 0; len = buffer_line->length; i = len + 1; if (!state->in_overwrite_mode) - while (i < MAXBUFLINE - 2 && i > state->column) + while (i < MAXBUFLINE - 2 && i > state->coord.col) { buffer_line->text[i] = buffer_line->text[i - 1]; i--; } - buffer_line->text[state->column] = ch; + buffer_line->text[state->coord.col] = ch; if (!state->in_overwrite_mode) buffer_line->length++; - position_to(state, state->row, state->column + 1); + position_to(state, state->coord.row, state->coord.col + 1); if (add_null) - buffer_line->text[state->column] = 0; + buffer_line->text[state->coord.col] = 0; #ifdef ENABLE_WRAP while (state->display_column >= WRAP_WIDTH) { - int saved_column = state->column; + int saved_column = state->coord.col; int distance_to_eol; - buffer_line = state->buffer + state->row; - distance_to_eol = buffer_line->length - saved_column; - state->column = WRAP_WIDTH + 1; - while (state->column > 0 - && !u32_is_word_boundary(buffer_line->text[state->column + buffer_line = state->buffer + state->coord.row; + distance_to_eol = buffer_line->length - saved_column; + state->coord.col = WRAP_WIDTH + 1; + while (state->coord.col > 0 + && !u32_is_word_boundary(buffer_line->text[state->coord.col - 1], 1)) - state->column--; - if (state->column == 0) + state->coord.col--; + if (state->coord.col == 0) { - state->column = saved_column; + state->coord.col = saved_column; break; } simple_insert_line(state); state->saved_column = -1; - position_to(state, state->row + 1, - state->buffer[state->row + 1].length - distance_to_eol); + position_to(state, state->coord.row + 1, + state->buffer[state->coord.row + 1].length + - distance_to_eol); } #endif /* ENABLE_WRAP */ @@ -454,31 +453,31 @@ join_lines(struct DrawState* state) /* Selection end row */ int ser = 0; int selection = 0; - int saved_row = state->row; + int saved_row = state->coord.row; struct BufferLine* current = NULL; if (SEL_NOT_EMPTY(state)) { - if (state->sel_start_row <= state->sel_end_row) + if (state->sel_start.row <= state->sel_end.row) { - ssr = state->sel_start_row; - ssc = state->sel_start_col; - ser = state->sel_end_row - 1; + ssr = state->sel_start.row; + ssc = state->sel_start.col; + ser = state->sel_end.row - 1; } else /* Swap */ { - ssr = state->sel_end_row; - ssc = state->sel_end_col; - ser = state->sel_start_row - 1; + ssr = state->sel_end.row; + ssc = state->sel_end.col; + ser = state->sel_start.row - 1; } selection = 1; clear_selection(state); } else { - ssr = state->row; - ssc = state->column; - ser = state->row + 1; + ssr = state->coord.row; + ssc = state->coord.col; + ser = state->coord.row + 1; } for (int row = ssr; row <= ser; row++) @@ -486,21 +485,21 @@ join_lines(struct DrawState* state) current = state->buffer + ssr; if (current->length + 2 < MAXBUFLINE) { - state->row = ssr; + state->coord.row = ssr; if (current->length > 0) { current->text[current->length] = ' '; current->text[current->length + 1] = 0; current->length++; } - state->column = current->length; + state->coord.col = current->length; state->display_column = display_length(state, - current->text, state->column); + current->text, state->coord.col); simple_join_lines(state); } } - state->row = saved_row; + state->coord.row = saved_row; if (selection) position_to(state, ssr, ssc); @@ -598,28 +597,31 @@ position_to(struct DrawState* state, const int row, const int column) { size_t dlen; - if (row != state->row) - { - 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 (row != state->coord.row) + { + state->coord.row = row; + if (state->coord.row > state->rows_count - 1) + state->coord.row = state->rows_count - 1; + if (state->first_shown.row > state->coord.row) + state->first_shown.row = state->coord.row; + if (state->first_shown.row + (state->maxy - 2) + < state->coord.row) + state->first_shown.row + = state->coord.row - (state->maxy - 2); if (state->saved_column == -1) { state->saved_column = state->display_column; - state->column = column; + state->coord.col = column; } else - state->column = buffer_length(state, - state->buffer[state->row].text, + state->coord.col = buffer_length(state, + state->buffer[state->coord.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 (state->coord.col > state->buffer[state->coord.row].length) + state->coord.col + = state->buffer[state->coord.row].length; + dlen = display_length(state, + state->buffer[state->coord.row].text, state->coord.col); if (dlen < state->saved_column) state->display_column = dlen; else @@ -627,24 +629,22 @@ position_to(struct DrawState* state, const int row, const int column) } else { - state->column = column; - if (state->column > state->buffer[state->row].length) - state->column = state->buffer[state->row].length; + state->coord.col = column; + if (state->coord.col > state->buffer[state->coord.row].length) + state->coord.col + = state->buffer[state->coord.row].length; state->display_column = display_length(state, - state->buffer[state->row].text, state->column); + state->buffer[state->coord.row].text, state->coord.col); state->saved_column = 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->column) - state->first_shown_column + if (state->first_shown.col > state->display_column) + state->first_shown.col = state->display_column; + if (state->first_shown.col + (state->maxx - 1) < state->coord.col) + state->first_shown.col = state->display_column - (state->maxx - 1); if (state->in_selection_mode) - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + assign_coord(&state->sel_end, state->coord); } int @@ -655,15 +655,15 @@ position_to_selection_start(struct DrawState* state) int ssr = 0; int ssc = 0; - if (state->sel_start_row <= state->sel_end_row) + if (state->sel_start.row <= state->sel_end.row) { - ssr = state->sel_start_row; - ssc = state->sel_start_col; + ssr = state->sel_start.row; + ssc = state->sel_start.col; } else /* Swap */ { - ssr = state->sel_end_row; - ssc = state->sel_end_col; + ssr = state->sel_end.row; + ssc = state->sel_end.col; } state->saved_column @@ -885,7 +885,7 @@ enter_insert_mode(struct DrawState* state) int erase_backwards(struct DrawState* state) { - struct BufferLine* buffer_line = &state->buffer[state->row]; + struct BufferLine* buffer_line = &state->buffer[state->coord.row]; if (SEL_NOT_EMPTY(state)) { @@ -895,9 +895,9 @@ erase_backwards(struct DrawState* state) return 0; } - if (state->column > 0) + if (state->coord.col > 0) { - size_t i = state->column; + size_t i = state->coord.col; do { buffer_line->text[i - 1] = buffer_line->text[i]; @@ -905,10 +905,10 @@ erase_backwards(struct DrawState* state) } while (buffer_line->text[i]); buffer_line->text[i - 1] = 0; buffer_line->length--; - position_to(state, state->row, state->column - 1); + position_to(state, state->coord.row, state->coord.col - 1); state->dirty = 1; } - else if (state->row > 0) + else if (state->coord.row > 0) { move_up(state); move_end(state); @@ -920,7 +920,7 @@ erase_backwards(struct DrawState* state) int erase_forward(struct DrawState* state) { - struct BufferLine* buffer_line = &state->buffer[state->row]; + struct BufferLine* buffer_line = &state->buffer[state->coord.row]; if (SEL_NOT_EMPTY(state)) { @@ -930,9 +930,9 @@ erase_forward(struct DrawState* state) return 0; } - if (buffer_line->text[state->column]) + if (buffer_line->text[state->coord.col]) { - size_t i = state->column; + size_t i = state->coord.col; while (i < MAXBUFLINE - 2 && buffer_line->text[i + 1]) { buffer_line->text[i] = buffer_line->text[i + 1]; @@ -942,12 +942,13 @@ erase_forward(struct DrawState* state) buffer_line->length--; state->dirty = 1; } - else if (state->row < state->rows_count - 1) + else if (state->coord.row < state->rows_count - 1) { - struct BufferLine* current = state->buffer + state->row; + struct BufferLine* current = state->buffer + state->coord.row; /* TODO: realloc? */ - if (state->row == state->rows_count - 1 - || current->length + state->buffer[state->row + 1].length + if (state->coord.row == state->rows_count - 1 + || current->length + + state->buffer[state->coord.row + 1].length >= MAXBUFLINE) goto skip_join_lines; simple_join_lines(state); @@ -966,7 +967,7 @@ erase_line(struct DrawState* state) state->buffer[0].length = 0; return 0; } - for (size_t row = state->row; row < state->rows_count - 1; row++) + for (size_t row = state->coord.row; row < state->rows_count - 1; row++) { current = state->buffer + row; current->length = u32_strcpy(current->text, @@ -974,9 +975,9 @@ erase_line(struct DrawState* state) } *state->buffer[state->rows_count - 1].text = 0; state->buffer[state->rows_count - 1].length = 0; - if (state->row == state->rows_count - 1) - state->row--; - position_to(state, state->row, 0); + if (state->coord.row == state->rows_count - 1) + state->coord.row--; + position_to(state, state->coord.row, 0); state->rows_count--; state->dirty = 1; return 0; @@ -985,11 +986,11 @@ erase_line(struct DrawState* state) int erase_prev_word(struct DrawState* state) { - int i = state->column; + int i = state->coord.col; struct BufferLine* current = NULL; int d; - current = state->buffer + state->row; + current = state->buffer + state->coord.row; if (i > 0 && u32_is_word_boundary(current->text[i - 1], 0)) i--; @@ -999,16 +1000,16 @@ erase_prev_word(struct DrawState* state) && !u32_is_word_boundary(current->text[i - 1], 0)) i--; - d = state->column - i; + d = state->coord.col - i; for (int j = i; j < current->length - d; j++) current->text[j] = current->text[j + d]; current->length -= d; current->text[current->length] = 0; - state->column = i; + state->coord.col = i; state->display_column - = display_length(state, current->text, state->column); + = display_length(state, current->text, state->coord.col); state->dirty = 1; return 0; } @@ -1016,7 +1017,7 @@ erase_prev_word(struct DrawState* state) int erase_to_end(struct DrawState* state) { - struct BufferLine* buffer_line = &state->buffer[state->row]; + struct BufferLine* buffer_line = &state->buffer[state->coord.row]; if (SEL_NOT_EMPTY(state)) { @@ -1026,8 +1027,8 @@ erase_to_end(struct DrawState* state) return 0; } - buffer_line->text[state->column] = 0; - buffer_line->length = state->column; + buffer_line->text[state->coord.col] = 0; + buffer_line->length = state->coord.col; move_end(state); state->dirty = 1; return 0; @@ -1036,8 +1037,8 @@ erase_to_end(struct DrawState* state) int erase_to_start(struct DrawState* state) { - size_t i = state->column; - struct BufferLine* buffer_line = &state->buffer[state->row]; + size_t i = state->coord.col; + struct BufferLine* buffer_line = &state->buffer[state->coord.row]; size_t len = buffer_line->length; if (SEL_NOT_EMPTY(state)) @@ -1050,11 +1051,11 @@ erase_to_start(struct DrawState* state) do { - buffer_line->text[i - state->column] = buffer_line->text[i]; + buffer_line->text[i - state->coord.col] = buffer_line->text[i]; i++; } while (buffer_line->text[i]); - buffer_line->text[len - state->column] = 0; - buffer_line->length = len - state->column; + buffer_line->text[len - state->coord.col] = 0; + buffer_line->length = len - state->coord.col; move_start(state); state->dirty = 1; return 0; @@ -1261,30 +1262,31 @@ insert_line(struct DrawState* state) #ifdef ENABLE_WRAP while (state->display_column >= WRAP_WIDTH) { - int saved_column = state->column; + int saved_column = state->coord.col; int distance_to_eol; - buffer_line = state->buffer + state->row; - distance_to_eol = buffer_line->length - saved_column; - state->column = WRAP_WIDTH + 1; - while (state->column > 0 - && !u32_is_word_boundary(buffer_line->text[state->column + buffer_line = state->buffer + state->coord.row; + distance_to_eol = buffer_line->length - saved_column; + state->coord.col = WRAP_WIDTH + 1; + while (state->coord.col > 0 + && !u32_is_word_boundary(buffer_line->text[state->coord.col - 1], 1)) - state->column--; - if (state->column == 0) + state->coord.col--; + if (state->coord.col == 0) { - state->column = saved_column; + state->coord.col = saved_column; break; } simple_insert_line(state); state->saved_column = -1; - position_to(state, state->row + 1, - state->buffer[state->row + 1].length - distance_to_eol); + position_to(state, state->coord.row + 1, + state->buffer[state->coord.row + 1].length + - distance_to_eol); } #endif /* ENABLE_WRAP */ simple_insert_line(state); state->saved_column = -1; - position_to(state, state->row + 1, 0); + position_to(state, state->coord.row + 1, 0); state->dirty = 1; return 0; } @@ -1305,51 +1307,53 @@ int move_bottom(struct DrawState* state) { position_to(state, state->rows_count > 1 ? state->rows_count - 1 : 0, - state->column); + state->coord.col); return 0; } int move_down(struct DrawState* state) { - position_to(state, state->row + 1, state->column); + position_to(state, state->coord.row + 1, state->coord.col); return 0; } int move_end(struct DrawState* state) { - position_to(state, state->row, state->buffer[state->row].length); + position_to(state, state->coord.row, + state->buffer[state->coord.row].length); return 0; } int move_left(struct DrawState* state) { - position_to(state, state->row, state->column - 1); + position_to(state, state->coord.row, state->coord.col - 1); return 0; } int move_next_word(struct DrawState* state) { - int i = state->column; - uint32_t* pline = state->buffer[state->row].text; - size_t len = state->buffer[state->row].length; + int i = state->coord.col; + uint32_t* pline = state->buffer[state->coord.row].text; + size_t len = state->buffer[state->coord.row].length; while (i < len && !u32_is_word_boundary(pline[i], 0)) i++; while (i < len && u32_is_word_boundary(pline[i], 0)) i++; - position_to(state, state->row, i); + position_to(state, state->coord.row, i); return 0; } int move_page_down(struct DrawState* state) { - if (state->row + state->maxy - 2 < state->rows_count) - position_to(state, state->row + state->maxy - 2, state->column); + if (state->coord.row + state->maxy - 2 < state->rows_count) + position_to(state, state->coord.row + state->maxy - 2, + state->coord.col); else move_bottom(state); return 0; @@ -1358,9 +1362,9 @@ move_page_down(struct DrawState* state) int move_page_up(struct DrawState* state) { - if (state->row > state->maxy - 2) - position_to(state, state->row - (state->maxy - 2), - state->column); + if (state->coord.row > state->maxy - 2) + position_to(state, state->coord.row - (state->maxy - 2), + state->coord.col); else move_top(state); return 0; @@ -1369,8 +1373,8 @@ move_page_up(struct DrawState* state) int move_prev_word(struct DrawState* state) { - int i = state->column; - uint32_t* pline = state->buffer[state->row].text; + int i = state->coord.col; + uint32_t* pline = state->buffer[state->coord.row].text; if (i > 0 && u32_is_word_boundary(pline[i - 1], 0)) i--; @@ -1379,14 +1383,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--; - position_to(state, state->row, i); + position_to(state, state->coord.row, i); return 0; } int move_right(struct DrawState* state) { - position_to(state, state->row, state->column + 1); + position_to(state, state->coord.row, state->coord.col + 1); return 0; } @@ -1394,13 +1398,13 @@ int move_screen_bottom(struct DrawState* state) { if (state->rows_count < state->maxy - 2) - position_to(state, state->rows_count - 1, state->column); + position_to(state, state->rows_count - 1, state->coord.col); else { - if (state->first_shown_row + state->maxy - 2 < state->rows_count) + if (state->first_shown.row + state->maxy - 2 < state->rows_count) position_to(state, - state->first_shown_row + state->maxy - 2, - state->column); + state->first_shown.row + state->maxy - 2, + state->coord.col); } return 0; } @@ -1409,13 +1413,13 @@ int move_screen_middle(struct DrawState* state) { if (state->rows_count < state->maxy - 2) - position_to(state, state->rows_count / 2, state->column); + position_to(state, state->rows_count / 2, state->coord.col); else { - int row = state->first_shown_row + (state->maxy - 2) / 2; + 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); + position_to(state, row, state->coord.col); } return 0; } @@ -1423,28 +1427,29 @@ move_screen_middle(struct DrawState* state) int move_screen_top(struct DrawState* state) { - position_to(state, state->first_shown_row, state->column); + position_to(state, state->first_shown.row, state->coord.col); return 0; } int move_start(struct DrawState* state) { - position_to(state, state->row, 0); + position_to(state, state->coord.row, 0); return 0; } int move_top(struct DrawState* state) { - position_to(state, 0, state->column); + position_to(state, 0, state->coord.col); return 0; } int move_up(struct DrawState* state) { - position_to(state, state->row > 1 ? state->row - 1 : 0, state->column); + position_to(state, state->coord.row > 1 ? state->coord.row - 1 : 0, + state->coord.col); return 0; } @@ -1459,12 +1464,12 @@ next_match(struct DrawState* state) return 0; *state->error = 0; - for (size_t row = state->row; row < state->rows_count; row++) + for (size_t row = state->coord.row; row < state->rows_count; row++) { current = state->buffer + row; found = current->text; - if (row == state->row) - found += state->column + 1; + if (row == state->coord.row) + found += state->coord.col + 1; do { found = u32_strstr(found, state->search); @@ -1522,7 +1527,7 @@ paste_from_paste(struct DrawState* state) } } - line = state->buffer + state->row; + line = state->buffer + state->coord.row; newtext = calloc(MAXBUFLINE, sizeof(uint32_t)); if (!newtext) { @@ -1530,14 +1535,14 @@ paste_from_paste(struct DrawState* state) strcpy(state->error, errors[ERR_CANT_ALLOC]); goto paste_from_paste_cleanup; } - u32_strncpy(newtext, line->text, state->column + 1); + u32_strncpy(newtext, line->text, state->coord.col + 1); u32_strcat(newtext, state->paste[0].text); if (state->paste_count == 1) { - u32_strcat(newtext, line->text + state->column); + u32_strcat(newtext, line->text + state->coord.col); line->length = u32_strcpy(line->text, newtext); - state->column += state->paste[state->paste_count - 1].length; + state->coord.col += state->paste[state->paste_count - 1].length; } else { @@ -1549,34 +1554,35 @@ paste_from_paste(struct DrawState* state) strcpy(state->error, errors[ERR_CANT_ALLOC]); goto paste_from_paste_cleanup; } - u32_strcpy(rest, line->text + state->column); + u32_strcpy(rest, line->text + state->coord.col); line->length = u32_strcpy(line->text, newtext); - for (size_t row = state->rows_count - state->row + for (size_t row = state->rows_count - state->coord.row - (state->paste_count - 2) - 1; row > 0; row--) { - line = state->buffer + state->row + state->paste_count - - 2 + row; + line = state->buffer + state->coord.row + + state->paste_count - 2 + row; line->length = u32_strcpy(line->text, - state->buffer[state->row + row - 1].text); + state->buffer[state->coord.row + row - 1].text); } - line = state->buffer + state->row; + line = state->buffer + state->coord.row; line->length = u32_strcpy(line->text, newtext); for (size_t row = 1; row < state->paste_count - 1; row++) { - line = state->buffer + state->row + row; + line = state->buffer + state->coord.row + row; line->length = u32_strcpy(line->text, state->paste[row].text); } - line = state->buffer + state->row + state->paste_count - 1; + line = state->buffer + state->coord.row + state->paste_count + - 1; u32_strcpy(newtext, state->paste[state->paste_count - 1].text); u32_strcat(newtext, rest); - line->length = u32_strcpy(line->text, newtext); - state->column = state->paste[state->paste_count - 1].length; - state->row += state->paste_count - 1; + line->length = u32_strcpy(line->text, newtext); + state->coord.col = state->paste[state->paste_count - 1].length; + state->coord.row += state->paste_count - 1; } - position_to(state, state->row, state->column); + position_to(state, state->coord.row, state->coord.col); state->dirty = 1; paste_from_paste_cleanup: @@ -1596,12 +1602,12 @@ prev_match(struct DrawState* state) return 0; *state->error = 0; - for (size_t row = state->row + 1; row > 0; row--) + for (size_t row = state->coord.row + 1; row > 0; row--) { current = state->buffer + row - 1; found = current->text; - if (row == state->row + 1) - found += state->column + 1; + if (row == state->coord.row + 1) + found += state->coord.col + 1; do { found = u32_strstr(found, state->search); @@ -1735,21 +1741,21 @@ simple_insert_line(struct DrawState* state) line = state->buffer + state->rows_count; } state->rows_count++; - for (size_t i = state->rows_count - 1; i > state->row + 1; i--) + for (size_t i = state->rows_count - 1; i > state->coord.row + 1; i--) state->buffer[i].length = u32_strcpy(state->buffer[i].text, state->buffer[i - 1].text); - state->buffer[state->row + 1].length - = u32_strcpy(state->buffer[state->row + 1].text, - state->buffer[state->row].text + state->column); - state->buffer[state->row].text[state->column] = 0; - state->buffer[state->row].length = state->column; - state->dirty = 1; + state->buffer[state->coord.row + 1].length + = u32_strcpy(state->buffer[state->coord.row + 1].text, + state->buffer[state->coord.row].text + state->coord.col); + state->buffer[state->coord.row].text[state->coord.col] = 0; + state->buffer[state->coord.row].length = state->coord.col; + state->dirty = 1; } void simple_join_lines(struct DrawState* state) { - size_t first_line = state->row; + size_t first_line = state->coord.row; if (state->rows_count == 1) return; @@ -1793,19 +1799,19 @@ simple_yank(struct DrawState* state) return; } - if (state->sel_start_row <= state->sel_end_row) + if (state->sel_start.row <= state->sel_end.row) { - ssr = state->sel_start_row; - ssc = state->sel_start_col; - ser = state->sel_end_row; - sec = state->sel_end_col; + ssr = state->sel_start.row; + ssc = state->sel_start.col; + ser = state->sel_end.row; + sec = state->sel_end.col; } else /* Swap */ { - ssr = state->sel_end_row; - ssc = state->sel_end_col; - ser = state->sel_start_row; - sec = state->sel_start_col; + ssr = state->sel_end.row; + ssc = state->sel_end.col; + ser = state->sel_start.row; + sec = state->sel_start.col; } state->paste_count = ser - ssr + 1; @@ -1871,17 +1877,8 @@ toggle_selection(struct DrawState* state) { state->in_selection_mode = !state->in_selection_mode; if (state->in_selection_mode) - { - state->sel_start_row = state->row; - state->sel_start_col = state->column; - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } - else - { - state->sel_end_row = state->row; - state->sel_end_col = state->column; - } + assign_coord(&state->sel_start, state->coord); + assign_coord(&state->sel_end, state->coord); return 0; }