mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-03-09 05:33:58 -04:00
search: utility functions distance_{next,prev}_word()
These functions calculate the distance from the current cursor to the next/previous word.
This commit is contained in:
parent
fcb0e05009
commit
61318d068e
1 changed files with 56 additions and 0 deletions
56
search.c
56
search.c
|
|
@ -1,6 +1,7 @@
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
|
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
|
#include <wctype.h>
|
||||||
|
|
||||||
#include <wayland-client.h>
|
#include <wayland-client.h>
|
||||||
#include <xkbcommon/xkbcommon-compose.h>
|
#include <xkbcommon/xkbcommon-compose.h>
|
||||||
|
|
@ -217,6 +218,61 @@ search_update(struct terminal *term)
|
||||||
#undef ROW_DEC
|
#undef ROW_DEC
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
distance_next_word(const struct terminal *term)
|
||||||
|
{
|
||||||
|
size_t cursor = term->search.cursor;
|
||||||
|
|
||||||
|
/* First eat non-whitespace. This is the word we're skipping past */
|
||||||
|
while (cursor < term->search.len) {
|
||||||
|
if (iswspace(term->search.buf[cursor++]))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(cursor == term->search.len || iswspace(term->search.buf[cursor - 1]));
|
||||||
|
|
||||||
|
/* Now skip past whitespace, so that we end up at the beginning of
|
||||||
|
* the next word */
|
||||||
|
while (cursor < term->search.len) {
|
||||||
|
if (!iswspace(term->search.buf[cursor++]))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INFO("cursor = %zu, iswspace() = %d", cursor, iswspace(term->search.buf[cursor - 1]));
|
||||||
|
assert(cursor == term->search.len || !iswspace(term->search.buf[cursor - 1]));
|
||||||
|
|
||||||
|
if (cursor < term->search.len && !iswspace(term->search.buf[cursor]))
|
||||||
|
cursor--;
|
||||||
|
|
||||||
|
return cursor - term->search.cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
distance_prev_word(const struct terminal *term)
|
||||||
|
{
|
||||||
|
int cursor = term->search.cursor;
|
||||||
|
|
||||||
|
/* First, eat whitespace prefix */
|
||||||
|
while (cursor > 0) {
|
||||||
|
if (!iswspace(term->search.buf[--cursor]))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(cursor == 0 || !iswspace(term->search.buf[cursor]));
|
||||||
|
|
||||||
|
/* Now eat non-whitespace. This is the word we're skipping past */
|
||||||
|
while (cursor > 0) {
|
||||||
|
if (iswspace(term->search.buf[--cursor]))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(cursor == 0 || iswspace(term->search.buf[cursor]));
|
||||||
|
if (iswspace(term->search.buf[cursor]))
|
||||||
|
cursor++;
|
||||||
|
|
||||||
|
return term->search.cursor - cursor;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask_t mods)
|
search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask_t mods)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue