Codebergdnkl

foot

master
ply5 git calls23 ms36 KiB3 cachedprotocol v2
1512 lines43 KiBRaw
1#include "search.h"
3#include <string.h>
5#include <wayland-client.h>
6#include <xkbcommon/xkbcommon-compose.h>
8#define LOG_MODULE "search"
9#define LOG_ENABLE_DBG 0
10#include "log.h"
11#include "char32.h"
12#include "commands.h"
13#include "config.h"
14#include "extract.h"
15#include "grid.h"
16#include "input.h"
17#include "key-binding.h"
18#include "misc.h"
19#include "quirks.h"
20#include "render.h"
21#include "selection.h"
22#include "shm.h"
23#include "unicode-mode.h"
24#include "util.h"
25#include "xmalloc.h"
27/*
28 * Ensures a "new" viewport doesn't contain any unallocated rows.
29 *
30 * This is done by first checking if the *first* row is NULL. If so,
31 * we move the viewport *forward*, until the first row is non-NULL. At
32 * this point, the entire viewport should be allocated rows only.
33 *
34 * If the first row already was non-NULL, we instead check the *last*
35 * row, and if it is NULL, we move the viewport *backward* until the
36 * last row is non-NULL.
37 */
38static int
39ensure_view_is_allocated(struct terminal *term, int new_view)
41 struct grid *grid = term->grid;
42 int view_end = (new_view + term->rows - 1) & (grid->num_rows - 1);
44 if (grid->rows[new_view] == NULL) {
45 while (grid->rows[new_view] == NULL)
46 new_view = (new_view + 1) & (grid->num_rows - 1);
47 }
49 else if (grid->rows[view_end] == NULL) {
50 while (grid->rows[view_end] == NULL) {
51 new_view--;
52 if (new_view < 0)
53 new_view += grid->num_rows;
54 view_end = (new_view + term->rows - 1) & (grid->num_rows - 1);
55 }
56 }
58#if defined(_DEBUG)
59 for (size_t r = 0; r < term->rows; r++)
60 xassert(grid->rows[(new_view + r) & (grid->num_rows - 1)] != NULL);
61#endif
63 return new_view;
66static bool
67search_ensure_size(struct terminal *term, size_t wanted_size)
69 while (wanted_size >= term->search.sz) {
70 size_t new_sz = term->search.sz == 0 ? 64 : term->search.sz * 2;
71 char32_t *new_buf = realloc(term->search.buf, new_sz * sizeof(term->search.buf[0]));
73 if (new_buf == NULL) {
74 LOG_ERRNO("failed to resize search buffer");
75 return false;
76 }
78 term->search.buf = new_buf;
79 term->search.sz = new_sz;
80 }
82 return true;
85static bool
86has_wrapped_around_left(const struct terminal *term, int abs_row_no)
88 int rebased_row = grid_row_abs_to_sb(term->grid, term->rows, abs_row_no);
89 return rebased_row == term->grid->num_rows - 1 || term->grid->rows[abs_row_no] == NULL;
92static bool
93has_wrapped_around_right(const struct terminal *term, int abs_row_no)
95 int rebased_row = grid_row_abs_to_sb(term->grid, term->rows, abs_row_no);
96 return rebased_row == 0;
99static void
100search_cancel_keep_selection(struct terminal *term)
102 struct wl_window *win = term->window;
103 wayl_win_subsurface_destroy(&win->search);
105 if (term->search.len > 0) {
106 free(term->search.last.buf);
107 term->search.last.buf = term->search.buf;
108 term->search.last.len = term->search.len;
109 } else
110 free(term->search.buf);
112 term->search.buf = NULL;
113 term->search.len = term->search.sz = 0;
115 term->search.cursor = 0;
116 term->search.match = (struct coord){-1, -1};
117 term->search.match_len = 0;
118 term->is_searching = false;
119 term->render.search_glyph_offset = 0;
121 /* Reset IME state */
122 if (term_ime_is_enabled(term)) {
123 term_ime_disable(term);
124 term_ime_enable(term);
125 }
127 term_xcursor_update(term);
128 render_refresh(term);
131void
132search_begin(struct terminal *term)
134 LOG_DBG("search: begin");
136 search_cancel_keep_selection(term);
137 selection_cancel(term);
139 /* Reset IME state */
140 if (term_ime_is_enabled(term)) {
141 term_ime_disable(term);
142 term_ime_enable(term);
143 }
145 /* On-demand instantiate wayland surface */
146 bool ret = wayl_win_subsurface_new(
147 term->window, &term->window->search, false);
148 xassert(ret);
150 const struct grid *grid = term->grid;
151 term->search.original_view = grid->view;
152 term->search.view_followed_offset = grid->view == grid->offset;
153 term->is_searching = true;
155 term->search.len = 0;
156 term->search.sz = 64;
157 term->search.buf = xmalloc(term->search.sz * sizeof(term->search.buf[0]));
158 term->search.buf[0] = U'\0';
160 term_xcursor_update(term);
161 render_refresh_search(term);
164void
165search_cancel(struct terminal *term)
167 if (!term->is_searching)
168 return;
170 search_cancel_keep_selection(term);
171 selection_cancel(term);
174void
175search_selection_cancelled(struct terminal *term)
177 term->search.match = (struct coord){-1, -1};
178 term->search.match_len = 0;
179 render_refresh_search(term);
182static void
183search_update_selection(struct terminal *term, const struct range *match)
185 struct grid *grid = term->grid;
186 int start_row = match->start.row;
187 int start_col = match->start.col;
188 int end_row = match->end.row;
189 int end_col = match->end.col;
191 xassert(start_row >= 0);
192 xassert(start_row < grid->num_rows);
194 bool move_viewport = true;
196 int view_end = (grid->view + term->rows - 1) & (grid->num_rows - 1);
197 if (view_end >= grid->view) {
198 /* Viewport does *not* wrap around */
199 if (start_row >= grid->view && end_row <= view_end)
200 move_viewport = false;
201 } else {
202 /* Viewport wraps */
203 if (start_row >= grid->view || end_row <= view_end)
204 move_viewport = false;
205 }
207 if (move_viewport) {
208 int rebased_new_view = grid_row_abs_to_sb(grid, term->rows, start_row);
210 rebased_new_view -= term->rows / 2;
211 rebased_new_view =
212 min(max(rebased_new_view, 0), grid->num_rows - term->rows);
214 const int old_view = grid->view;
215 int new_view = grid_row_sb_to_abs(grid, term->rows, rebased_new_view);
217 /* Scrollback may not be completely filled yet */
218 {
219 const int mask = grid->num_rows - 1;
220 while (grid->rows[new_view] == NULL)
221 new_view = (new_view + 1) & mask;
222 }
224#if defined(_DEBUG)
225 /* Verify all to-be-visible rows have been allocated */
226 for (int r = 0; r < term->rows; r++)
227 xassert(grid->rows[(new_view + r) & (grid->num_rows - 1)] != NULL);
228#endif
230#if defined(_DEBUG)
231 {
232 int rel_start_row = grid_row_abs_to_sb(grid, term->rows, start_row);
233 int rel_view = grid_row_abs_to_sb(grid, term->rows, new_view);
234 xassert(rel_view <= rel_start_row);
235 xassert(rel_start_row < rel_view + term->rows);
236 }
237#endif
239 /* Update view */
240 grid->view = new_view;
241 if (new_view != old_view)
242 term_damage_view(term);
243 }
245 if (start_row != term->search.match.row ||
246 start_col != term->search.match.col ||
248 /* Pointer leave events trigger selection_finalize() :/ */
249 !term->selection.ongoing)
250 {
251 int selection_row = start_row - grid->view + grid->num_rows;
252 selection_row &= grid->num_rows - 1;
254 selection_start(
255 term, start_col, selection_row, SELECTION_CHAR_WISE, false);
257 term->search.match.row = start_row;
258 term->search.match.col = start_col;
259 }
261 /* Update selection endpoint */
262 {
263 int selection_row = end_row - grid->view + grid->num_rows;
264 selection_row &= grid->num_rows - 1;
265 selection_update(term, end_col, selection_row);
266 }
269static ssize_t
270matches_cell(const struct terminal *term, const struct cell *cell, size_t search_ofs)
272 assert(search_ofs < term->search.len);
274 char32_t base = cell->wc;
275 const struct composed *composed = NULL;
277 if (base >= CELL_COMB_CHARS_LO && base <= CELL_COMB_CHARS_HI)
278 {
279 composed = composed_lookup(term->composed, base - CELL_COMB_CHARS_LO);
280 base = composed->chars[0];
281 }
283 if (composed == NULL && base == 0 && term->search.buf[search_ofs] == U' ')
284 return 1;
286 if (hasc32upper(term->search.buf)) {
287 if (c32ncmp(&base, &term->search.buf[search_ofs], 1) != 0)
288 return -1;
289 } else {
290 if (c32ncasecmp(&base, &term->search.buf[search_ofs], 1) != 0)
291 return -1;
292 }
294 if (composed != NULL) {
295 if (search_ofs + composed->count > term->search.len)
296 return -1;
298 for (size_t j = 1; j < composed->count; j++) {
299 if (composed->chars[j] != term->search.buf[search_ofs + j])
300 return -1;
301 }
302 }
304 return composed != NULL ? composed->count : 1;
307static bool
308find_next(struct terminal *term, enum search_direction direction,
309 struct coord abs_start, struct coord abs_end, struct range *match)
311#define ROW_DEC(_r) ((_r) = ((_r) - 1 + grid->num_rows) & (grid->num_rows - 1))
312#define ROW_INC(_r) ((_r) = ((_r) + 1) & (grid->num_rows - 1))
314 struct grid *grid = term->grid;
315 const bool backward = direction != SEARCH_FORWARD;
317 LOG_DBG("%s: start: %dx%d, end: %dx%d", backward ? "backward" : "forward",
318 abs_start.row, abs_start.col, abs_end.row, abs_end.col);
320 xassert(abs_start.row >= 0);
321 xassert(abs_start.row < grid->num_rows);
322 xassert(abs_start.col >= 0);
323 xassert(abs_start.col < term->cols);
325 xassert(abs_end.row >= 0);
326 xassert(abs_end.row < grid->num_rows);
327 xassert(abs_end.col >= 0);
328 xassert(abs_end.col < term->cols);
330 for (int match_start_row = abs_start.row, match_start_col = abs_start.col;
331 ;
332 backward ? ROW_DEC(match_start_row) : ROW_INC(match_start_row)) {
334 const struct row *row = grid->rows[match_start_row];
335 if (row == NULL) {
336 if (match_start_row == abs_end.row)
337 break;
338 continue;
339 }
341 for (;
342 backward ? match_start_col >= 0 : match_start_col < term->cols;
343 backward ? match_start_col-- : match_start_col++)
344 {
345 if (matches_cell(term, &row->cells[match_start_col], 0) < 0) {
346 if (match_start_row == abs_end.row &&
347 match_start_col == abs_end.col)
348 {
349 break;
350 }
351 continue;
352 }
354 /*
355 * Got a match on the first letter. Now we'll see if the
356 * rest of the search buffer matches.
357 */
359 LOG_DBG("search: initial match at row=%d, col=%d",
360 match_start_row, match_start_col);
362 int match_end_row = match_start_row;
363 int match_end_col = match_start_col;
364 const struct row *match_row = row;
365 size_t match_len = 0;
367 for (size_t i = 0; i < term->search.len;) {
368 if (match_end_col >= term->cols) {
369 ROW_INC(match_end_row);
370 match_end_col = 0;
372 match_row = grid->rows[match_end_row];
373 if (match_row == NULL)
374 break;
375 }
377 if (match_row->cells[match_end_col].wc >= CELL_SPACER) {
378 match_end_col++;
379 continue;
380 }
382 ssize_t additional_chars = matches_cell(
383 term, &match_row->cells[match_end_col], i);
384 if (additional_chars < 0)
385 break;
387 i += additional_chars;
388 match_len += additional_chars;
389 match_end_col++;
391 while (match_end_col < term->cols &&
392 match_row->cells[match_end_col].wc > CELL_SPACER)
393 {
394 match_end_col++;
395 }
396 }
398 if (match_len != term->search.len) {
399 /* Didn't match (completely) */
401 if (match_start_row == abs_end.row &&
402 match_start_col == abs_end.col)
403 {
404 break;
405 }
407 continue;
408 }
410 *match = (struct range){
411 .start = {match_start_col, match_start_row},
412 .end = {match_end_col - 1, match_end_row},
413 };
415 return true;
416 }
418 if (match_start_row == abs_end.row && match_start_col == abs_end.col)
419 break;
421 match_start_col = backward ? term->cols - 1 : 0;
422 }
424 return false;
427void
428search_find_next(struct terminal *term, enum search_direction direction)
430 struct grid *grid = term->grid;
432 if (term->search.len == 0) {
433 term->search.match = (struct coord){-1, -1};
434 term->search.match_len = 0;
435 selection_cancel(term);
436 return;
437 }
439 struct coord start = term->search.match;
440 size_t len = term->search.match_len;
442 xassert((len == 0 && start.row == -1 && start.col == -1) ||
443 (len > 0 && start.row >= 0 && start.col >= 0));
445 if (len == 0) {
446 /* No previous match, start from the top, or bottom, of the scrollback */
447 switch (direction) {
448 case SEARCH_FORWARD:
449 start.row = grid_row_absolute_in_view(grid, 0);
450 start.col = 0;
451 break;
453 case SEARCH_BACKWARD:
454 case SEARCH_BACKWARD_SAME_POSITION:
455 start.row = grid_row_absolute_in_view(grid, term->rows - 1);
456 start.col = term->cols - 1;
457 break;
458 }
459 } else {
460 /* Continue from last match */
461 xassert(start.row >= 0);
462 xassert(start.col >= 0);
464 switch (direction) {
465 case SEARCH_BACKWARD_SAME_POSITION:
466 break;
468 case SEARCH_BACKWARD:
469 if (--start.col < 0) {
470 start.col = term->cols - 1;
471 start.row += grid->num_rows - 1;
472 start.row &= grid->num_rows - 1;
473 }
474 break;
476 case SEARCH_FORWARD:
477 if (++start.col >= term->cols) {
478 start.col = 0;
479 start.row++;
480 start.row &= grid->num_rows - 1;
481 }
482 break;
483 }
485 xassert(start.row >= 0);
486 xassert(start.row < grid->num_rows);
487 xassert(start.col >= 0);
488 xassert(start.col < term->cols);
489 }
491 LOG_DBG(
492 "update: %s: starting at row=%d col=%d "
493 "(offset = %d, view = %d)",
494 direction != SEARCH_FORWARD ? "backward" : "forward",
495 start.row, start.col,
496 grid->offset, grid->view);
498 struct coord end = start;
499 switch (direction) {
500 case SEARCH_FORWARD:
501 /* Search forward, until we reach the cell *before* current start */
502 if (--end.col < 0) {
503 end.col = term->cols - 1;
504 end.row += grid->num_rows - 1;
505 end.row &= grid->num_rows - 1;
506 }
507 break;
509 case SEARCH_BACKWARD:
510 case SEARCH_BACKWARD_SAME_POSITION:
511 /* Search backwards, until we reach the cell *after* current start */
512 if (++end.col >= term->cols) {
513 end.col = 0;
514 end.row++;
515 end.row &= grid->num_rows - 1;
516 }
517 break;
518 }
520 struct range match;
521 bool found = find_next(term, direction, start, end, &match);
523 if (found) {
524 LOG_DBG("primary match found at %dx%d",
525 match.start.row, match.start.col);
526 search_update_selection(term, &match);
527 term->search.match = match.start;
528 term->search.match_len = term->search.len;
529 } else {
530 LOG_DBG("no match");
531 term->search.match = (struct coord){-1, -1};
532 term->search.match_len = 0;
533 selection_cancel(term);
534 }
535#undef ROW_DEC
538struct search_match_iterator
539search_matches_new_iter(struct terminal *term)
541 return (struct search_match_iterator){
542 .term = term,
543 .start = {0, 0},
544 };
547struct range
548search_matches_next(struct search_match_iterator *iter)
550 struct terminal *term = iter->term;
551 struct grid *grid = term->grid;
553 if (term->search.match_len == 0)
554 goto no_match;
556 if (iter->start.row >= term->rows)
557 goto no_match;
559 xassert(iter->start.row >= 0);
560 xassert(iter->start.row < term->rows);
561 xassert(iter->start.col >= 0);
562 xassert(iter->start.col < term->cols);
564 struct coord abs_start = iter->start;
565 abs_start.row = grid_row_absolute_in_view(grid, abs_start.row);
567 struct coord abs_end = {
568 term->cols - 1,
569 grid_row_absolute_in_view(grid, term->rows - 1)};
571 /* BUG: matches *starting* outside the view, but ending *inside*, aren't matched */
572 struct range match;
573 bool found = find_next(term, SEARCH_FORWARD, abs_start, abs_end, &match);
574 if (!found)
575 goto no_match;
577 LOG_DBG("match at (absolute coordinates) %dx%d-%dx%d",
578 match.start.row, match.start.col,
579 match.end.row, match.end.col);
581 /* Convert absolute row numbers back to view relative */
582 match.start.row = match.start.row - grid->view + grid->num_rows;
583 match.start.row &= grid->num_rows - 1;
584 match.end.row = match.end.row - grid->view + grid->num_rows;
585 match.end.row &= grid->num_rows - 1;
587 LOG_DBG("match at (view-local coordinates) %dx%d-%dx%d, view=%d",
588 match.start.row, match.start.col,
589 match.end.row, match.end.col, grid->view);
591 /* Assert match end comes *after* the match start */
592 xassert(match.end.row > match.start.row ||
593 (match.end.row == match.start.row &&
594 match.end.col >= match.start.col));
596 /* Assert the match starts at, or after, the iterator position */
597 xassert(match.start.row > iter->start.row ||
598 (match.start.row == iter->start.row &&
599 match.start.col >= iter->start.col));
601 /* Continue at next column, next time */
602 iter->start.row = match.start.row;
603 iter->start.col = match.start.col + 1;
605 if (iter->start.col >= term->cols) {
606 iter->start.col = 0;
607 iter->start.row++; /* Overflow is caught in next iteration */
608 }
610 xassert(iter->start.row >= 0);
611 xassert(iter->start.row <= term->rows);
612 xassert(iter->start.col >= 0);
613 xassert(iter->start.col < term->cols);
614 return match;
616no_match:
617 iter->start.row = -1;
618 iter->start.col = -1;
619 return (struct range){{-1, -1}, {-1, -1}};
622static void
623add_wchars(struct terminal *term, char32_t *src, size_t count)
625 /* Strip non-printable characters */
626 for (size_t i = 0, j = 0, orig_count = count; i < orig_count; i++) {
627 if (isc32print(src[i]))
628 src[j++] = src[i];
629 else
630 count--;
631 }
633 if (!search_ensure_size(term, term->search.len + count))
634 return;
636 xassert(term->search.len + count < term->search.sz);
638 memmove(&term->search.buf[term->search.cursor + count],
639 &term->search.buf[term->search.cursor],
640 (term->search.len - term->search.cursor) * sizeof(char32_t));
642 memcpy(&term->search.buf[term->search.cursor], src, count * sizeof(char32_t));
644 term->search.len += count;
645 term->search.cursor += count;
646 term->search.buf[term->search.len] = U'\0';
649void
650search_add_chars(struct terminal *term, const char *src, size_t count)
652 size_t chars = mbsntoc32(NULL, src, count, 0);
653 if (chars == (size_t)-1) {
654 LOG_ERRNO("failed to convert %.*s to Unicode", (int)count, src);
655 return;
656 }
658 char32_t c32s[chars + 1];
659 mbsntoc32(c32s, src, count, chars);
660 add_wchars(term, c32s, chars);
663enum extend_direction {SEARCH_EXTEND_LEFT, SEARCH_EXTEND_RIGHT};
665static bool
666coord_advance_left(const struct terminal *term, struct coord *pos,
667 const struct row **row)
669 const struct grid *grid = term->grid;
670 struct coord new_pos = *pos;
672 if (--new_pos.col < 0) {
673 new_pos.row = (new_pos.row - 1 + grid->num_rows) & (grid->num_rows - 1);
674 new_pos.col = term->cols - 1;
676 if (has_wrapped_around_left(term, new_pos.row))
677 return false;
679 if (row != NULL)
680 *row = grid->rows[new_pos.row];
681 }
683 *pos = new_pos;
684 return true;
687static bool
688coord_advance_right(const struct terminal *term, struct coord *pos,
689 const struct row **row)
691 const struct grid *grid = term->grid;
692 struct coord new_pos = *pos;
694 if (++new_pos.col >= term->cols) {
695 new_pos.row = (new_pos.row + 1) & (grid->num_rows - 1);
696 new_pos.col = 0;
698 if (has_wrapped_around_right(term, new_pos.row))
699 return false;
701 if (row != NULL)
702 *row = grid->rows[new_pos.row];
703 }
705 *pos = new_pos;
706 return true;
709static bool
710search_extend_find_char(const struct terminal *term, struct coord *target,
711 enum extend_direction direction)
713 if (term->search.match_len == 0)
714 return false;
716 struct coord pos = direction == SEARCH_EXTEND_LEFT
717 ? selection_get_start(term) : selection_get_end(term);
718 xassert(pos.row >= 0);
719 xassert(pos.row < term->grid->num_rows);
721 *target = pos;
723 const struct row *row = term->grid->rows[pos.row];
725 while (true) {
726 switch (direction) {
727 case SEARCH_EXTEND_LEFT:
728 if (!coord_advance_left(term, &pos, &row))
729 return false;
730 break;
732 case SEARCH_EXTEND_RIGHT:
733 if (!coord_advance_right(term, &pos, &row))
734 return false;
735 break;
736 }
738 const char32_t wc = row->cells[pos.col].wc;
740 if (wc >= CELL_SPACER || wc == U'\0')
741 continue;
743 *target = pos;
744 return true;
745 }
748static bool
749search_extend_find_char_left(const struct terminal *term, struct coord *target)
751 return search_extend_find_char(term, target, SEARCH_EXTEND_LEFT);
754static bool
755search_extend_find_char_right(const struct terminal *term, struct coord *target)
757 return search_extend_find_char(term, target, SEARCH_EXTEND_RIGHT);
760static bool
761search_extend_find_word(const struct terminal *term, bool spaces_only,
762 struct coord *target, enum extend_direction direction)
764 if (term->search.match_len == 0)
765 return false;
767 struct grid *grid = term->grid;
768 struct coord pos = direction == SEARCH_EXTEND_LEFT
769 ? selection_get_start(term)
770 : selection_get_end(term);
772 xassert(pos.row >= 0);
773 xassert(pos.row < grid->num_rows);
775 *target = pos;
777 /* First character to consider is the *next* character */
778 switch (direction) {
779 case SEARCH_EXTEND_LEFT:
780 if (!coord_advance_left(term, &pos, NULL))
781 return false;
782 break;
784 case SEARCH_EXTEND_RIGHT:
785 if (!coord_advance_right(term, &pos, NULL))
786 return false;
787 break;
788 }
790 xassert(pos.row >= 0);
791 xassert(pos.row < grid->num_rows);
792 xassert(grid->rows[pos.row] != NULL);
794 /* Find next word boundary */
795 switch (direction) {
796 case SEARCH_EXTEND_LEFT:
797 selection_find_word_boundary_left(term, &pos, spaces_only);
798 break;
800 case SEARCH_EXTEND_RIGHT:
801 selection_find_word_boundary_right(term, &pos, spaces_only, false);
802 break;
803 }
805 *target = pos;
806 return true;
809static bool
810search_extend_find_word_left(const struct terminal *term, bool spaces_only,
811 struct coord *target)
813 return search_extend_find_word(term, spaces_only, target, SEARCH_EXTEND_LEFT);
816static bool
817search_extend_find_word_right(const struct terminal *term, bool spaces_only,
818 struct coord *target)
820 return search_extend_find_word(term, spaces_only, target, SEARCH_EXTEND_RIGHT);
823static bool
824search_extend_find_line(const struct terminal *term, struct coord *target,
825 enum extend_direction direction)
827 if (term->search.match_len == 0)
828 return false;
830 struct coord pos = direction == SEARCH_EXTEND_LEFT
831 ? selection_get_start(term) : selection_get_end(term);
833 xassert(pos.row >= 0);
834 xassert(pos.row < term->grid->num_rows);
836 *target = pos;
838 const struct grid *grid = term->grid;
840 switch (direction) {
841 case SEARCH_EXTEND_LEFT:
842 pos.row = (pos.row - 1 + grid->num_rows) & (grid->num_rows - 1);
843 if (has_wrapped_around_left(term, pos.row))
844 return false;
845 break;
847 case SEARCH_EXTEND_RIGHT:
848 pos.row = (pos.row + 1) & (grid->num_rows - 1);
849 if (has_wrapped_around_right(term, pos.row))
850 return false;
851 break;
852 }
854 *target = pos;
855 return true;
858static bool
859search_extend_find_line_up(const struct terminal *term, struct coord *target)
861 return search_extend_find_line(term, target, SEARCH_EXTEND_LEFT);
864static bool
865search_extend_find_line_down(const struct terminal *term, struct coord *target)
867 return search_extend_find_line(term, target, SEARCH_EXTEND_RIGHT);
870static void
871search_extend_left(struct terminal *term, const struct coord *target)
873 if (term->search.match_len == 0)
874 return;
876 const struct coord last_coord = selection_get_start(term);
877 struct coord pos = *target;
878 const struct row *row = term->grid->rows[pos.row];
880 const bool move_cursor = term->search.cursor != 0;
882 struct extraction_context *ctx = extract_begin(SELECTION_NONE, false);
883 if (ctx == NULL)
884 return;
886 while (pos.col != last_coord.col || pos.row != last_coord.row) {
887 if (!extract_one(term, row, &row->cells[pos.col], pos.col, ctx))
888 break;
889 if (!coord_advance_right(term, &pos, &row))
890 break;
891 }
893 char32_t *new_text;
894 size_t new_len;
896 if (!extract_finish_wide(ctx, &new_text, &new_len))
897 return;
899 if (!search_ensure_size(term, term->search.len + new_len))
900 return;
902 memmove(&term->search.buf[new_len], &term->search.buf[0],
903 term->search.len * sizeof(term->search.buf[0]));
905 size_t actually_copied = 0;
906 for (size_t i = 0; i < new_len; i++) {
907 if (new_text[i] == U'\n') {
908 /* extract() adds newlines, which we never match against */
909 continue;
910 }
912 term->search.buf[actually_copied++] = new_text[i];
913 term->search.len++;
914 }
916 xassert(actually_copied <= new_len);
917 if (actually_copied < new_len) {
918 memmove(
919 &term->search.buf[actually_copied], &term->search.buf[new_len],
920 (term->search.len - actually_copied) * sizeof(term->search.buf[0]));
921 }
923 term->search.buf[term->search.len] = U'\0';
924 free(new_text);
926 if (move_cursor)
927 term->search.cursor += actually_copied;
929 struct range match = {.start = *target, .end = selection_get_end(term)};
930 search_update_selection(term, &match);
932 term->search.match_len = term->search.len;
935static void
936search_extend_right(struct terminal *term, const struct coord *target)
938 if (term->search.match_len == 0)
939 return;
941 struct coord pos = selection_get_end(term);
942 const struct row *row = term->grid->rows[pos.row];
944 const bool move_cursor = term->search.cursor == term->search.len;
946 struct extraction_context *ctx = extract_begin(SELECTION_NONE, false);
947 if (ctx == NULL)
948 return;
950 do {
951 if (!coord_advance_right(term, &pos, &row))
952 break;
953 if (!extract_one(term, row, &row->cells[pos.col], pos.col, ctx))
954 break;
955 } while (pos.col != target->col || pos.row != target->row);
957 char32_t *new_text;
958 size_t new_len;
960 if (!extract_finish_wide(ctx, &new_text, &new_len))
961 return;
963 if (!search_ensure_size(term, term->search.len + new_len))
964 return;
966 for (size_t i = 0; i < new_len; i++) {
967 if (new_text[i] == U'\n') {
968 /* extract() adds newlines, which we never match against */
969 continue;
970 }
972 term->search.buf[term->search.len++] = new_text[i];
973 }
975 term->search.buf[term->search.len] = U'\0';
976 free(new_text);
978 if (move_cursor)
979 term->search.cursor = term->search.len;
981 struct range match = {.start = term->search.match, .end = *target};
982 search_update_selection(term, &match);
983 term->search.match_len = term->search.len;
986static size_t
987distance_next_word(const struct terminal *term)
989 size_t cursor = term->search.cursor;
991 /* First eat non-whitespace. This is the word we're skipping past */
992 while (cursor < term->search.len) {
993 if (isc32space(term->search.buf[cursor++]))
994 break;
995 }
997 xassert(cursor == term->search.len || isc32space(term->search.buf[cursor - 1]));
999 /* Now skip past whitespace, so that we end up at the beginning of
1000 * the next word */
1001 while (cursor < term->search.len) {
1002 if (!isc32space(term->search.buf[cursor++]))
1003 break;
1006 xassert(cursor == term->search.len || !isc32space(term->search.buf[cursor - 1]));
1008 if (cursor < term->search.len && !isc32space(term->search.buf[cursor]))
1009 cursor--;
1011 return cursor - term->search.cursor;
1014static size_t
1015distance_prev_word(const struct terminal *term)
1017 int cursor = term->search.cursor;
1019 /* First, eat whitespace prefix */
1020 while (cursor > 0) {
1021 if (!isc32space(term->search.buf[--cursor]))
1022 break;
1025 xassert(cursor == 0 || !isc32space(term->search.buf[cursor]));
1027 /* Now eat non-whitespace. This is the word we're skipping past */
1028 while (cursor > 0) {
1029 if (isc32space(term->search.buf[--cursor]))
1030 break;
1033 xassert(cursor == 0 || isc32space(term->search.buf[cursor]));
1034 if (cursor > 0 && isc32space(term->search.buf[cursor]))
1035 cursor++;
1037 return term->search.cursor - cursor;
1040static void
1041from_clipboard_cb(char *text, size_t size, void *user)
1043 struct terminal *term = user;
1044 search_add_chars(term, text, size);
1047static void
1048from_clipboard_done(void *user)
1050 struct terminal *term = user;
1052 LOG_DBG("search: buffer: %ls", (const wchar_t *)term->search.buf);
1053 search_find_next(term, SEARCH_BACKWARD_SAME_POSITION);
1054 render_refresh_search(term);
1057static bool
1058execute_binding(struct seat *seat, struct terminal *term,
1059 const struct key_binding *binding, uint32_t serial,
1060 bool *update_search_result, enum search_direction *direction,
1061 bool *redraw)
1063 *update_search_result = *redraw = false;
1064 const enum bind_action_search action = binding->action;
1066 struct grid *grid = term->grid;
1068 switch (action) {
1069 case BIND_ACTION_SEARCH_NONE:
1070 return false;
1072 case BIND_ACTION_SEARCH_SCROLLBACK_UP_PAGE:
1073 if (term->grid == &term->normal) {
1074 cmd_scrollback_up(term, term->rows);
1075 return true;
1077 return false;
1079 case BIND_ACTION_SEARCH_SCROLLBACK_UP_HALF_PAGE:
1080 if (term->grid == &term->normal) {
1081 cmd_scrollback_up(term, max(term->rows / 2, 1));
1082 return true;
1084 break;
1086 case BIND_ACTION_SEARCH_SCROLLBACK_UP_LINE:
1087 if (term->grid == &term->normal) {
1088 cmd_scrollback_up(term, 1);
1089 return true;
1091 break;
1093 case BIND_ACTION_SEARCH_SCROLLBACK_DOWN_PAGE:
1094 if (term->grid == &term->normal) {
1095 cmd_scrollback_down(term, term->rows);
1096 return true;
1098 return false;
1100 case BIND_ACTION_SEARCH_SCROLLBACK_DOWN_HALF_PAGE:
1101 if (term->grid == &term->normal) {
1102 cmd_scrollback_down(term, max(term->rows / 2, 1));
1103 return true;
1105 break;
1107 case BIND_ACTION_SEARCH_SCROLLBACK_DOWN_LINE:
1108 if (term->grid == &term->normal) {
1109 cmd_scrollback_down(term, 1);
1110 return true;
1112 break;
1114 case BIND_ACTION_SEARCH_SCROLLBACK_HOME:
1115 if (term->grid == &term->normal) {
1116 cmd_scrollback_up(term, term->grid->num_rows);
1117 return true;
1119 break;
1121 case BIND_ACTION_SEARCH_SCROLLBACK_END:
1122 if (term->grid == &term->normal) {
1123 cmd_scrollback_down(term, term->grid->num_rows);
1124 return true;
1126 break;
1128 case BIND_ACTION_SEARCH_CANCEL:
1129 if (term->search.view_followed_offset)
1130 grid->view = grid->offset;
1131 else {
1132 grid->view = ensure_view_is_allocated(
1133 term, term->search.original_view);
1135 term_damage_view(term);
1136 search_cancel(term);
1137 return true;
1139 case BIND_ACTION_SEARCH_COMMIT:
1140 selection_finalize(seat, term, serial);
1141 search_cancel_keep_selection(term);
1142 return true;
1144 case BIND_ACTION_SEARCH_FIND_PREV:
1145 if (term->search.last.buf != NULL && term->search.len == 0) {
1146 add_wchars(term, term->search.last.buf, term->search.last.len);
1148 free(term->search.last.buf);
1149 term->search.last.buf = NULL;
1150 term->search.last.len = 0;
1153 *direction = SEARCH_BACKWARD;
1154 *update_search_result = *redraw = true;
1155 return true;
1157 case BIND_ACTION_SEARCH_FIND_NEXT:
1158 if (term->search.last.buf != NULL && term->search.len == 0) {
1159 add_wchars(term, term->search.last.buf, term->search.last.len);
1161 free(term->search.last.buf);
1162 term->search.last.buf = NULL;
1163 term->search.last.len = 0;
1166 *direction = SEARCH_FORWARD;
1167 *update_search_result = *redraw = true;
1168 return true;
1170 case BIND_ACTION_SEARCH_EDIT_LEFT:
1171 if (term->search.cursor > 0) {
1172 term->search.cursor--;
1173 *redraw = true;
1175 return true;
1177 case BIND_ACTION_SEARCH_EDIT_LEFT_WORD: {
1178 size_t diff = distance_prev_word(term);
1179 term->search.cursor -= diff;
1180 xassert(term->search.cursor <= term->search.len);
1182 if (diff > 0)
1183 *redraw = true;
1184 return true;
1187 case BIND_ACTION_SEARCH_EDIT_RIGHT:
1188 if (term->search.cursor < term->search.len) {
1189 term->search.cursor++;
1190 *redraw = true;
1192 return true;
1194 case BIND_ACTION_SEARCH_EDIT_RIGHT_WORD: {
1195 size_t diff = distance_next_word(term);
1196 term->search.cursor += diff;
1197 xassert(term->search.cursor <= term->search.len);
1199 if (diff > 0)
1200 *redraw = true;
1201 return true;
1204 case BIND_ACTION_SEARCH_EDIT_HOME:
1205 if (term->search.cursor != 0) {
1206 term->search.cursor = 0;
1207 *redraw = true;
1209 return true;
1211 case BIND_ACTION_SEARCH_EDIT_END:
1212 if (term->search.cursor != term->search.len) {
1213 term->search.cursor = term->search.len;
1214 *redraw = true;
1216 return true;
1218 case BIND_ACTION_SEARCH_DELETE_PREV:
1219 if (term->search.cursor > 0) {
1220 memmove(
1221 &term->search.buf[term->search.cursor - 1],
1222 &term->search.buf[term->search.cursor],
1223 (term->search.len - term->search.cursor) * sizeof(char32_t));
1224 term->search.cursor--;
1225 term->search.buf[--term->search.len] = U'\0';
1226 *update_search_result = *redraw = true;
1228 return true;
1230 case BIND_ACTION_SEARCH_DELETE_PREV_WORD: {
1231 size_t diff = distance_prev_word(term);
1232 size_t old_cursor = term->search.cursor;
1233 size_t new_cursor = old_cursor - diff;
1235 if (diff > 0) {
1236 memmove(&term->search.buf[new_cursor],
1237 &term->search.buf[old_cursor],
1238 (term->search.len - old_cursor) * sizeof(char32_t));
1240 term->search.len -= diff;
1241 term->search.cursor = new_cursor;
1242 *update_search_result = *redraw = true;
1244 return true;
1247 case BIND_ACTION_SEARCH_DELETE_NEXT:
1248 if (term->search.cursor < term->search.len) {
1249 memmove(
1250 &term->search.buf[term->search.cursor],
1251 &term->search.buf[term->search.cursor + 1],
1252 (term->search.len - term->search.cursor - 1) * sizeof(char32_t));
1253 term->search.buf[--term->search.len] = U'\0';
1254 *update_search_result = *redraw = true;
1256 return true;
1258 case BIND_ACTION_SEARCH_DELETE_NEXT_WORD: {
1259 size_t diff = distance_next_word(term);
1260 size_t cursor = term->search.cursor;
1262 if (diff > 0) {
1263 memmove(&term->search.buf[cursor],
1264 &term->search.buf[cursor + diff],
1265 (term->search.len - (cursor + diff)) * sizeof(char32_t));
1267 term->search.len -= diff;
1268 *update_search_result = *redraw = true;
1270 return true;
1273 case BIND_ACTION_SEARCH_DELETE_TO_START: {
1274 if (term->search.cursor > 0) {
1275 memmove(&term->search.buf[0],
1276 &term->search.buf[term->search.cursor],
1277 (term->search.len - term->search.cursor)
1278 * sizeof(char32_t));
1280 term->search.len -= term->search.cursor;
1281 term->search.cursor = 0;
1282 *update_search_result = *redraw = true;
1284 return true;
1287 case BIND_ACTION_SEARCH_DELETE_TO_END: {
1288 if (term->search.cursor < term->search.len) {
1289 term->search.buf[term->search.cursor] = '\0';
1290 term->search.len = term->search.cursor;
1291 *update_search_result = *redraw = true;
1293 return true;
1296 case BIND_ACTION_SEARCH_EXTEND_CHAR: {
1297 struct coord target;
1298 if (search_extend_find_char_right(term, &target)) {
1299 search_extend_right(term, &target);
1300 *update_search_result = false;
1301 *redraw = true;
1303 return true;
1306 case BIND_ACTION_SEARCH_EXTEND_WORD: {
1307 struct coord target;
1308 if (search_extend_find_word_right(term, false, &target)) {
1309 search_extend_right(term, &target);
1310 *update_search_result = false;
1311 *redraw = true;
1313 return true;
1316 case BIND_ACTION_SEARCH_EXTEND_WORD_WS: {
1317 struct coord target;
1318 if (search_extend_find_word_right(term, true, &target)) {
1319 search_extend_right(term, &target);
1320 *update_search_result = false;
1321 *redraw = true;
1323 return true;
1326 case BIND_ACTION_SEARCH_EXTEND_LINE_DOWN: {
1327 struct coord target;
1328 if (search_extend_find_line_down(term, &target)) {
1329 search_extend_right(term, &target);
1330 *update_search_result = false;
1331 *redraw = true;
1333 return true;
1336 case BIND_ACTION_SEARCH_EXTEND_BACKWARD_CHAR: {
1337 struct coord target;
1338 if (search_extend_find_char_left(term, &target)) {
1339 search_extend_left(term, &target);
1340 *update_search_result = false;
1341 *redraw = true;
1343 return true;
1346 case BIND_ACTION_SEARCH_EXTEND_BACKWARD_WORD: {
1347 struct coord target;
1348 if (search_extend_find_word_left(term, false, &target)) {
1349 search_extend_left(term, &target);
1350 *update_search_result = false;
1351 *redraw = true;
1353 return true;
1356 case BIND_ACTION_SEARCH_EXTEND_BACKWARD_WORD_WS: {
1357 struct coord target;
1358 if (search_extend_find_word_left(term, true, &target)) {
1359 search_extend_left(term, &target);
1360 *update_search_result = false;
1361 *redraw = true;
1363 return true;
1366 case BIND_ACTION_SEARCH_EXTEND_LINE_UP: {
1367 struct coord target;
1368 if (search_extend_find_line_up(term, &target)) {
1369 search_extend_left(term, &target);
1370 *update_search_result = false;
1371 *redraw = true;
1373 return true;
1376 case BIND_ACTION_SEARCH_CLIPBOARD_PASTE:
1377 text_from_clipboard(
1378 seat, term, false, &from_clipboard_cb, &from_clipboard_done, term);
1379 *update_search_result = *redraw = true;
1380 return true;
1382 case BIND_ACTION_SEARCH_PRIMARY_PASTE:
1383 text_from_primary(
1384 seat, term, false, &from_clipboard_cb, &from_clipboard_done, term);
1385 *update_search_result = *redraw = true;
1386 return true;
1388 case BIND_ACTION_SEARCH_UNICODE_INPUT:
1389 unicode_mode_activate(term);
1390 return true;
1392 case BIND_ACTION_SEARCH_COUNT:
1393 BUG("Invalid action type");
1394 return true;
1397 BUG("Unhandled action type");
1398 return false;
1401void
1402search_input(struct seat *seat, struct terminal *term,
1403 const struct key_binding_set *bindings, uint32_t key,
1404 xkb_keysym_t sym, xkb_mod_mask_t mods, xkb_mod_mask_t consumed,
1405 const xkb_keysym_t *raw_syms, size_t raw_count,
1406 uint32_t serial)
1408 LOG_DBG("search: input: sym=%d/0x%x, mods=0x%08x, consumed=0x%08x",
1409 sym, sym, mods, consumed);
1411 enum xkb_compose_status compose_status = seat->kbd.xkb_compose_state != NULL
1412 ? xkb_compose_state_get_status(seat->kbd.xkb_compose_state)
1413 : XKB_COMPOSE_NOTHING;
1415 enum search_direction search_direction = SEARCH_BACKWARD_SAME_POSITION;
1416 bool update_search_result = false;
1417 bool redraw = false;
1419 /*
1420 * Key bindings
1421 */
1423 /* Match untranslated symbols */
1424 tll_foreach(bindings->search, it) {
1425 const struct key_binding *bind = &it->item;
1427 if (bind->mods != mods || bind->mods == 0)
1428 continue;
1430 for (size_t i = 0; i < raw_count; i++) {
1431 if (bind->k.sym == raw_syms[i]) {
1432 if (execute_binding(seat, term, bind, serial,
1433 &update_search_result, &search_direction,
1434 &redraw))
1436 seat->kbd.last_shortcut_sym = sym;
1437 goto update_search;
1439 return;
1444 /* Match translated symbol */
1445 tll_foreach(bindings->search, it) {
1446 const struct key_binding *bind = &it->item;
1448 if (bind->k.sym == sym &&
1449 bind->mods == (mods & ~consumed)) {
1451 if (execute_binding(seat, term, bind, serial,
1452 &update_search_result, &search_direction,
1453 &redraw))
1455 seat->kbd.last_shortcut_sym = sym;
1456 goto update_search;
1458 return;
1462 /* Match raw key code */
1463 tll_foreach(bindings->search, it) {
1464 const struct key_binding *bind = &it->item;
1466 if (bind->mods != mods || bind->mods == 0)
1467 continue;
1469 tll_foreach(bind->k.key_codes, code) {
1470 if (code->item == key) {
1471 if (execute_binding(seat, term, bind, serial,
1472 &update_search_result, &search_direction,
1473 &redraw))
1475 seat->kbd.last_shortcut_sym = sym;
1476 goto update_search;
1478 return;
1483 uint8_t buf[64] = {0};
1484 int count = 0;
1486 if (compose_status == XKB_COMPOSE_COMPOSED) {
1487 count = xkb_compose_state_get_utf8(
1488 seat->kbd.xkb_compose_state, (char *)buf, sizeof(buf));
1489 xkb_compose_state_reset(seat->kbd.xkb_compose_state);
1490 } else if (compose_status == XKB_COMPOSE_CANCELLED ||
1491 compose_status == XKB_COMPOSE_COMPOSING) {
1492 count = 0;
1493 } else {
1494 count = xkb_state_key_get_utf8(
1495 seat->kbd.xkb_state, key, (char *)buf, sizeof(buf));
1498 update_search_result = redraw = count > 0;
1499 search_direction = SEARCH_BACKWARD_SAME_POSITION;
1501 if (count == 0)
1502 return;
1504 search_add_chars(term, (const char *)buf, count);
1506update_search:
1507 LOG_DBG("search: buffer: %ls", (const wchar_t *)term->search.buf);
1508 if (update_search_result)
1509 search_find_next(term, search_direction);
1510 if (redraw)
1511 render_refresh_search(term);
Diagnostics5 git calls · 23 ms
CallStatusSizeTime023 ms
discover200178 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=178
git.server.agent=git/2.56.1-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=178
git.pkt.payload.max=35
ls-refs20010 KiB2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=10322
git.response.cache=UPDATING
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=108
git.pkt.data=107
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=10322
git.pkt.payload.max=106
git.ls_refs.count=107
git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=8830
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=8830
git.pkt.payload.max=8730
git.sideband.band1.bytes=8730
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=14
git.pack.bytes=8730
git.fetch.wide.objects=14
git.fetch.wide.extra_bytes=9909
git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=9489
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=9489
git.pkt.payload.max=9389
git.sideband.band1.bytes=9389
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=9389
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a
git.pack.full=10
Open pack map
fetch2007.7 KiB · hit18 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=18
git.response.bytes=7884
git.response.cache=HIT
git.response.duration_ms=18
git.fetch.wants=9281559145e95ee5d7492849565f61aa2e080c91
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=7884
git.pkt.payload.max=7857
git.sideband.band1.bytes=7857
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://codeberg.org/dnkl/foot
Ref
refs/heads/master
Commit
c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
cf-ray
a5e15f5d6ccb725b
Request
f7d7dd7d…
Protocol
v2 · sha1
Agent
git/2.56.1-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format