size_t text_customword_end_next(Text *txt, size_t pos, int (*isboundary)(int)) { char c; Iterator it = text_iterator_get(txt, pos); while (text_iterator_char_next(&it, &c) && space(c)); if (boundary(c)) do pos = it.pos; while (text_iterator_char_next(&it, &c) && boundary(c) && !space(c)); else do pos = it.pos; while (text_iterator_char_next(&it, &c) && !isboundary(c)); return pos; }
size_t text_customword_start_next(Text *txt, size_t pos, int (*isboundary)(int)) { char c; Iterator it = text_iterator_get(txt, pos); if (!text_iterator_byte_get(&it, &c)) return pos; if (boundary(c)) while (boundary(c) && !space(c) && text_iterator_char_next(&it, &c)); else while (!boundary(c) && text_iterator_char_next(&it, &c)); while (space(c) && text_iterator_char_next(&it, &c)); return it.pos; }
size_t text_line_char_set(Text *txt, size_t pos, int count) { char c; size_t bol = text_line_begin(txt, pos); Iterator it = text_iterator_get(txt, bol); while (count-- > 0 && text_iterator_byte_get(&it, &c) && c != '\r' && c != '\n') text_iterator_char_next(&it, NULL); return it.pos; }
size_t text_line_char_next(Text *txt, size_t pos) { char c; Iterator it = text_iterator_get(txt, pos); if (!text_iterator_byte_get(&it, &c) || c == '\r' || c == '\n') return pos; if (!text_iterator_char_next(&it, &c) || c == '\r' || c == '\n') return pos; return it.pos; }
void vis_replace(Vis *vis, size_t pos, const char *data, size_t len) { Text *txt = vis->win->file->text; Iterator it = text_iterator_get(txt, pos); int chars = text_char_count(data, len); for (char c; chars-- > 0 && text_iterator_byte_get(&it, &c) && c != '\r' && c != '\n'; ) text_iterator_char_next(&it, NULL); text_delete(txt, pos, it.pos - pos); vis_insert(vis, pos, data, len); }
static size_t op_replace(Vis *vis, Text *txt, OperatorContext *c) { size_t count = 0; Iterator it = text_iterator_get(txt, c->range.start); while (it. pos < c->range.end && text_iterator_char_next(&it, NULL)) count++; op_delete(vis, txt, c); size_t pos = c->range.start; for (size_t len = strlen(c->arg->s); count > 0; pos += len, count--) text_insert(txt, pos, c->arg->s, len); return pos; }
int text_line_char_get(Text *txt, size_t pos) { char c; int count = 0; size_t bol = text_line_begin(txt, pos); Iterator it = text_iterator_get(txt, bol); while (text_iterator_byte_get(&it, &c) && it.pos < pos && c != '\r' && c != '\n') { text_iterator_char_next(&it, NULL); count++; } return count; }
size_t text_char_next(Text *txt, size_t pos) { Iterator it = text_iterator_get(txt, pos); text_iterator_char_next(&it, NULL); return it.pos; }