Move jump commands to grid reader, make them UTF-8 aware, and tidy up,

from Anindya Mukherjee.
This commit is contained in:
nicm
2021-02-22 06:53:04 +00:00
parent b04f8acb70
commit 8986c8dfcd
4 changed files with 206 additions and 216 deletions

View File

@ -17,6 +17,7 @@
*/
#include "tmux.h"
#include <string.h>
/* Initialise virtual cursor. */
void
@ -301,3 +302,64 @@ grid_reader_cursor_previous_word(struct grid_reader *gr, const char *separators,
gr->cx = oldx;
gr->cy = oldy;
}
/* Jump forward to character. */
int
grid_reader_cursor_jump(struct grid_reader *gr, const struct utf8_data *jc)
{
struct grid_cell gc;
u_int px, py, xx, yy;
px = gr->cx;
yy = gr->gd->hsize + gr->gd->sy - 1;
for (py = gr->cy; py <= yy; py++) {
xx = grid_line_length(gr->gd, py);
while (px < xx) {
grid_get_cell(gr->gd, px, py, &gc);
if (!(gc.flags & GRID_FLAG_PADDING) &&
gc.data.size == jc->size &&
memcmp(gc.data.data, jc->data, gc.data.size) == 0) {
gr->cx = px;
gr->cy = py;
return 1;
}
px++;
}
if (py == yy ||
!(grid_get_line(gr->gd, py)->flags & GRID_LINE_WRAPPED))
return 0;
px = 0;
}
return 0;
}
/* Jump back to character. */
int
grid_reader_cursor_jump_back(struct grid_reader *gr, const struct utf8_data *jc)
{
struct grid_cell gc;
u_int px, py, xx;
xx = gr->cx + 1;
for (py = gr->cy + 1; py > 0; py--) {
for (px = xx; px > 0; px--) {
grid_get_cell(gr->gd, px - 1, py - 1, &gc);
if (!(gc.flags & GRID_FLAG_PADDING) &&
gc.data.size == jc->size &&
memcmp(gc.data.data, jc->data, gc.data.size) == 0) {
gr->cx = px - 1;
gr->cy = py - 1;
return 1;
}
}
if (py == 1 ||
!(grid_get_line(gr->gd, py - 2)->flags & GRID_LINE_WRAPPED))
return 0;
xx = grid_line_length(gr->gd, py - 2);
}
return 0;
}