чување 8c009b92325eba9a50253ab2003b058665bc4e7a
родитељ 5f74c433b7e60f9da6fb9d93b21cf19f3e076f75
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Tue, 15 Mar 2022 20:12:20 +0100
sled.c: Refactor wrapping logic and add it to insert_line
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | sled.c | | | 177 | +++++++++++++++++++++++++++++++++++++++++++++---------------------------------- |
измењених датотека: 1, додавања: 100(+), брисања: 77(-)
diff --git a/sled.c b/sled.c
@@ -71,7 +71,8 @@ void reread_file(struct DrawState* state);
void quit_callback(struct DrawState* state, struct tb_event* ev);
int save_file(struct DrawState* state);
void search_dialog_callback(struct DrawState* state);
-int simple_join_lines(struct DrawState* state);
+void simple_insert_line(struct DrawState* state);
+void simple_join_lines(struct DrawState* state);
int
clear_selection(struct DrawState* state)
@@ -414,34 +415,27 @@ insert_char(struct DrawState* state, uint32_t ch)
buffer_line->text[state->column] = 0;
#ifdef ENABLE_WRAP
-check_wrap:
- if (state->display_column >= WRAP_WIDTH)
+ while (state->display_column >= WRAP_WIDTH)
{
- int saved_column = state->column;
- int 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
- - 1],
- 1))
- state->column--;
- if (state->column == 0)
- goto skip_insert_line;
+ int saved_column = state->column;
+ 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
- 1],
1))
state->column--;
if (state->column == 0)
- goto skip_insert_line;
- insert_line(state);
- position_to(state, state->row,
- state->buffer[state->row].length - distance_to_eol);
- /* TODO: Line several times longer than WRAP_WIDTH should wrap
- * at word boundaries */
- goto check_wrap;
- skip_insert_line:
- state->column = saved_column;
+ {
+ state->column = 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);
}
#endif /* ENABLE_WRAP */
@@ -486,7 +480,7 @@ join_lines(struct DrawState* state)
ser = state->row + 1;
}
- for (int row = ssr; row < ser; row++)
+ for (int row = ssr; row <= ser; row++)
{
current = state->buffer + ssr;
if (current->length + 2 < MAXBUFLINE)
@@ -940,15 +934,12 @@ erase_forward(struct DrawState* state)
else if (state->row < state->rows_count - 1)
{
struct BufferLine* current = state->buffer + state->row;
+ /* TODO: realloc? */
if (state->row == state->rows_count - 1
|| current->length + state->buffer[state->row + 1].length
>= MAXBUFLINE)
goto skip_join_lines;
- if (simple_join_lines(state))
- {
- strcpy(state->error, errors[ERR_CANT_ALLOC]);
- state->running = 0;
- }
+ simple_join_lines(state);
skip_join_lines:;
}
return 0;
@@ -1247,55 +1238,42 @@ input_move_next_word(struct DrawState* state)
int
insert_line(struct DrawState* state)
{
- struct BufferLine* newchunk = NULL;
- struct BufferLine* line = NULL;
- uint32_t* newtext = NULL;
+ struct BufferLine* buffer_line = NULL;
- if (state->rows_count == state->buffer_size)
+ if (SEL_NOT_EMPTY(state))
{
- state->buffer_size += ALLOC_DELTA;
- newchunk = realloc(state->buffer,
- state->buffer_size * sizeof(struct BufferLine));
- if (!newchunk)
- {
- state->running = 0;
- strcpy(state->error, errors[ERR_CANT_ALLOC]);
- return 0;
- }
- state->buffer = newchunk;
- for (size_t row = state->buffer_size - ALLOC_DELTA;
- row < state->buffer_size; row++)
- {
- line = state->buffer + row;
- init_bufferline(line);
- newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
- if (!newtext)
- {
- state->running = 0;
- strcpy(state->error, errors[ERR_CANT_ALLOC]);
- return 0;
- }
- line->text = newtext;
- }
- line = state->buffer + state->rows_count;
+ erase_selection(state);
+ position_to_selection_start(state);
+ clear_selection(state);
}
- state->rows_count++;
- for (size_t i = state->rows_count - 1; i > state->row + 1; i--)
+
+#ifdef ENABLE_WRAP
+ while (state->display_column >= WRAP_WIDTH)
{
- u32_strcpy(state->buffer[i].text, state->buffer[i - 1].text);
- state->buffer[i].length = state->buffer[i - 1].length;
+ int saved_column = state->column;
+ 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
+ - 1],
+ 1))
+ state->column--;
+ if (state->column == 0)
+ {
+ state->column = 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);
}
- u32_strcpy(state->buffer[state->row + 1].text,
- state->buffer[state->row].text + state->column);
- state->buffer[state->row + 1].length
- = u32_strlen(state->buffer[state->row + 1].text);
- state->buffer[state->row].text[state->column] = 0;
- state->buffer[state->row].length
- = u32_strlen(state->buffer[state->row].text);
- state->row++;
- if (state->row > state->first_shown_row + (state->maxy - 2))
- state->first_shown_row = state->row - (state->maxy - 2);
- move_start(state);
+#endif /* ENABLE_WRAP */
+ simple_insert_line(state);
+ state->saved_column = -1;
+ position_to(state, state->row + 1, 0);
state->dirty = 1;
return 0;
}
@@ -1330,8 +1308,7 @@ move_down(struct DrawState* state)
int
move_end(struct DrawState* state)
{
- size_t len = state->buffer[state->row].length;
- position_to(state, state->row, len);
+ position_to(state, state->row, state->buffer[state->row].length);
return 0;
}
@@ -1711,13 +1688,60 @@ show_help(struct DrawState* state)
return 0;
}
-int
+void
+simple_insert_line(struct DrawState* state)
+{
+ struct BufferLine* newchunk = NULL;
+ struct BufferLine* line = NULL;
+ uint32_t* newtext = NULL;
+
+ if (state->rows_count == state->buffer_size)
+ {
+ state->buffer_size += ALLOC_DELTA;
+ newchunk = realloc(state->buffer,
+ state->buffer_size * sizeof(struct BufferLine));
+ if (!newchunk)
+ {
+ state->running = 0;
+ strcpy(state->error, errors[ERR_CANT_ALLOC]);
+ return;
+ }
+ state->buffer = newchunk;
+ for (size_t row = state->buffer_size - ALLOC_DELTA;
+ row < state->buffer_size; row++)
+ {
+ line = state->buffer + row;
+ init_bufferline(line);
+ newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
+ if (!newtext)
+ {
+ state->running = 0;
+ strcpy(state->error, errors[ERR_CANT_ALLOC]);
+ return;
+ }
+ line->text = newtext;
+ }
+ line = state->buffer + state->rows_count;
+ }
+ state->rows_count++;
+ for (size_t i = state->rows_count - 1; i > state->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;
+}
+
+void
simple_join_lines(struct DrawState* state)
{
size_t first_line = state->row;
if (state->rows_count == 1)
- return 0;
+ return;
u32_strcat(state->buffer[first_line].text,
state->buffer[first_line + 1].text);
@@ -1732,7 +1756,6 @@ simple_join_lines(struct DrawState* state)
state->buffer[state->rows_count - 1].length = 0;
state->rows_count--;
state->dirty = 1;
- return 0;
}
int