чување 5b714fe1a1acb107e15fba2477c5f23956fe391d
родитељ dc80ec8d36e9feeeb06c3af7439e5ba69b45cccb
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Tue, 29 Oct 2024 18:14:51 +0100
Fix removing blank lines when wrapping selection
Diffstat:
| M | TODO | | | 6 | ------ |
| M | TODO.done | | | 6 | ++++++ |
| M | sled.c | | | 78 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------- |
измењених датотека: 3, додавања: 63(+), брисања: 27(-)
diff --git a/TODO b/TODO
@@ -1,12 +1,6 @@
TODO
====
-[x] Recognize binary files (having NUL bytes) and refuse to open them; possibly
- make it a config.h option?
-
-[ ] Fix removing blank lines when wrapping selected text containing multiple
- paragraphs
-
[ ] Reconsider state.error; it might be better to store only the errno code a la
errno
- Should tb_shutdown be called if malloc failed? It deallocates memory,
diff --git a/TODO.done b/TODO.done
@@ -1,6 +1,12 @@
Done or canceled todos
======================
+[x] Fix removing blank lines when wrapping selected text containing multiple
+ paragraphs
+
+[x] Recognize binary files (having NUL bytes) and refuse to open them; possibly
+ make it a config.h option?
+
[x] Investigate wrapping further
[x] Avoid blanks at the beginning of lines
[x] Last paragraph in README
diff --git a/sled.c b/sled.c
@@ -2669,9 +2669,15 @@ wrap_next_line:
next = &state->buffer[cur_row + 1];
state->coord.row = cur_row;
+ if (sel && (cur_row == state->sel_end.row)
+ && (cur_row != state->sel_start.row)
+ && (cur_row + 1 != state->rows_count))
+ goto wrap_finish;
+
+ /* If the line has less than wrap_width characters, join with next */
dlen = display_length(current->text, current->length);
while ((dlen < wrap_width) && (cur_row + 1 < state->rows_count)
- && u32_has_nonblanks(next->text))
+ && (current->length > 0) && u32_has_nonblanks(next->text))
{
/* Insert space to avoid joining ending and beginning words */
if (current->length > 0
@@ -2687,20 +2693,25 @@ wrap_next_line:
current->text[current->length] = 0;
}
- simple_join_lines(state);
- current = &state->buffer[cur_row];
- next = &state->buffer[cur_row + 1];
-
if (sel)
{
+ if (cur_row + 1 == state->sel_end.row)
+ state->sel_end.col
+ += state->buffer[cur_row].length;
+ if (cur_row + 1 == state->sel_start.row)
+ state->sel_end.col -= state->sel_start.col;
state->sel_end.row--;
- state->sel_end.col
- = state->buffer[state->sel_end.row].length;
}
+ simple_join_lines(state);
end_row--;
+
+ current = &state->buffer[cur_row];
+ next = &state->buffer[cur_row + 1];
+
dlen = display_length(current->text, current->length);
}
+ /* If the line has wrap_width or more characters, wrap it */
while (dlen >= wrap_width)
{
int tdlen;
@@ -2715,7 +2726,17 @@ wrap_next_line:
wrap_alloc_error, uint32_t);
state->coord.col = bcol - 1;
+ if (sel)
+ {
+ if (cur_row == state->sel_end.row)
+ state->sel_end.col -= bcol;
+ /* TODO: Add startcol parameter to buffer_length? */
+ /*if (cur_row == state->sel_start.row)
+ state->sel_end.col += state->sel_start.col;*/
+ state->sel_end.row++;
+ }
simple_insert_line(state);
+ end_row++;
cur_row++;
state->coord.row++;
@@ -2735,20 +2756,15 @@ wrap_next_line:
break;
}
- if (sel)
- {
- state->sel_end.row++;
- state->sel_end.col
- = state->buffer[state->sel_end.row].length;
- }
- end_row++;
if (cur_row + 1 == state->rows_count)
goto wrap_next_line;
/* If the rest of the line is still in block and is
* < wrap_width, join it with next (the new next) */
tdlen = display_length(current->text, current->length);
- if (u32_has_nonblanks(next->text) && tdlen < wrap_width)
+ if ((current->length > 0) && u32_has_nonblanks(next->text)
+ && (tdlen < wrap_width)
+ && !(sel && (cur_row == state->sel_end.row)))
{
/* Add space if needed to avoid joining words */
/* clang-format off */
@@ -2766,18 +2782,24 @@ wrap_next_line:
current->text[current->length] = 0;
}
- simple_join_lines(state);
-
if (sel)
{
+ if (cur_row + 1 == state->sel_end.row)
+ state->sel_end.col
+ += state->buffer[cur_row].length;
+ if (cur_row + 1 == state->sel_start.row)
+ state->sel_end.col
+ -= state->sel_start.col;
state->sel_end.row--;
- state->sel_end.col
- = state->buffer[state->sel_end.row]
- .length;
}
+ simple_join_lines(state);
end_row--;
- dlen = display_length(current->text, current->length);
+ dlen = display_length(current->text,
+ sel && (cur_row == state->sel_end.row)
+ ? state->sel_end.col
+ : current->length);
+ // dlen = display_length(current->text, current->length);
if (dlen < wrap_width)
goto wrap_next_line;
@@ -2796,6 +2818,17 @@ wrap_next_line:
wrap_alloc_error, uint32_t);
state->coord.col = bcol - 1;
+ if (sel)
+ {
+ if (cur_row == state->sel_end.row)
+ state->sel_end.col -= bcol;
+ /* TODO: Add startcol parameter to
+ * buffer_length? */
+ /*if (cur_row == state->sel_start.row)
+ state->sel_end.col +=
+ state->sel_start.col;*/
+ state->sel_end.row++;
+ }
simple_insert_line(state);
end_row++;
@@ -2811,6 +2844,9 @@ wrap_next_line:
}
}
+ if (cur_row + 1 == state->rows_count)
+ goto wrap_next_line;
+
if (cur_row == MIN((end_row + 1), state->rows_count))
break;