Commit: 1eb870dd331ab9235aa1f1e79b8d947647a46b09
Parent: 74a07eb867da5ff89d82991b946540bc99610802
Author: Randy Palamar
Date: Thu, 31 Oct 2024 07:42:53 -0600
make sure backwards tab also works across boundaries
Diffstat:
2 files changed, 19 insertions(+), 11 deletions(-)
diff --git a/terminal.c b/terminal.c
@@ -341,18 +341,18 @@ next_tab_position(Term *t, b32 backwards)
u32 bit = col % ARRAY_COUNT(t->tabs);
u32 mask = safe_left_shift(1, bit - backwards) - 1;
- u32 result = 32 * idx, zeroes;
- if (backwards) zeroes = 32 - _lzcnt_u32(t->tabs[idx++] & mask);
- else zeroes = _tzcnt_u32(t->tabs[idx++] & ~mask);
- result += zeroes;
-
- /* NOTE we may need to advance across idx boundaries */
- while (idx < ARRAY_COUNT(t->tabs) && zeroes == 32) {
- if (backwards) zeroes = 32 - _lzcnt_u32(t->tabs[idx++]);
- else zeroes = _tzcnt_u32(t->tabs[idx++]);
- result += zeroes;
+ u32 result = 32 * idx;
+ if (backwards) {
+ u32 zeroes = _lzcnt_u32(t->tabs[idx--] & mask);
+ while (idx < ARRAY_COUNT(t->tabs) && zeroes == 32)
+ zeroes = _lzcnt_u32(t->tabs[idx--]);
+ result = 32 * (idx + 1) + 32 - zeroes;
+ } else {
+ u32 zeroes = _tzcnt_u32(t->tabs[idx++] & ~mask);
+ while (idx < ARRAY_COUNT(t->tabs) && zeroes == 32)
+ zeroes = _tzcnt_u32(t->tabs[idx++]);
+ result = 32 * (idx - 1) + zeroes + 1;
}
- result += !backwards;
ASSERT(result < t->size.w);
return result;
diff --git a/test.c b/test.c
@@ -328,6 +328,14 @@ static TEST_FN(cursor_tabs_across_boundary)
handle_input(term, arena, raw);
result.status &= term->cursor.pos.x == 33;
+ /* NOTE: ensure backwards tab works */
+ launder_static_string(term, CSI(1;34H));
+ launder_static_string(term, ESC(0g));
+ launder_static_string(term, CSI(1;66H));
+ raw = launder_static_string(term, CSI(1Z));
+ handle_input(term, arena, raw);
+ result.status &= term->cursor.pos.x == 33;
+
return result;
}