Merge branch 'fuzzy-ng'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Daniel Eklöf · committed by Daniel Eklöf · parents fa972e3, 5580a9b · Browse files · 4 files·+188 added−85 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+188 added−85 removed
- other:+188 added−85 removed
Provenance
- Commit
e661458- Tree
4f65a57- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent 1
fa972e3- Parent 2
5580a9b
4 files changed+188 added−85 removed
modified:CHANGELOG.md+2 added
29 unmodified lines30 * `Ctrl+y` binding to execute selected entry.31 * `Ctrl+j`/`Ctrl+k` binding to move to the next/previous item32 ([#120][120]).33 * Escape sequences in `Exec` arguments are now supported.34+* Multiple space-separated search words can now be entered at the prompt.35 36+37 [3]: https://codeberg.org/dnkl/fuzzel/issues/338 [107]: https://codeberg.org/dnkl/fuzzel/issues/10739 [108]: https://codeberg.org/dnkl/fuzzel/issues/10840 [120]: https://codeberg.org/dnkl/fuzzel/issues/120332 unmodified linesmodified:match.c+164 added-81 removed
62 unmodified lines63 }64 65 static const char32_t *66 match_levenshtein(struct matches *matches,67- const char32_t *src, const char32_t *pat)67+ const char32_t *src, const char32_t *pat, size_t *_match_len)68 {69 if (!matches->fuzzy)70 return NULL;71 63 unmodified lines135 .distance = m[pat_len][end].distance,136 };137 #endif138 const size_t match_ofs = c;139- const size_t match_len = end - c;139 const size_t match_distance = m[pat_len][end].distance;140+ const size_t match_len = end - c;141 142 LOG_DBG("%s vs. %s: sub-string: %.*ls, (distance=%zu, row=%zu, col=%zu)",143 src, pat, (int)match_len, &src[match_ofs], match_distance,144 r, c);8 unmodified lines153 154 if (len_diff <= matches->fuzzy_max_length_discrepancy &&155 match_distance <= matches->fuzzy_max_distance)156 {157+ if (_match_len != NULL)158+ *_match_len = match_len;159 return &src[match_ofs];160 }161 162 return NULL;25 unmodified lines188 {189 if (matches == NULL)190 return;191 192+ for (size_t i = 0; i < matches->applications->count; i++)193+ free(matches->matches[i].pos);194+195 free(matches->matches);196 free(matches);197 }198 4 unmodified lines203 assert(matches->applications == NULL);204 assert(matches->matches == NULL);205 206 matches->applications = applications;202- matches->matches = malloc(203- applications->count * sizeof(matches->matches[0]));207+ matches->matches = calloc(208+ applications->count, sizeof(matches->matches[0]));209 }210 211 size_t212 matches_max_matches_per_page(const struct matches *matches)208 unmodified lines421 for (size_t i = 0; i < matches->applications->count; i++) {422 if (!matches->applications->v[i].visible)423 continue;424 425+ free(matches->matches[matches->match_count].pos);426+427 matches->matches[matches->match_count++] = (struct match){428 .matched_type = MATCHED_NONE,429 .application = &matches->applications->v[i],423- .start_title = -1,430+ .pos = NULL,431+ .pos_count = 0,432 .index = i,433 };434 }435 33 unmodified lines469 matches->match_count = 0;470 471 LOG_DBG("match update begin");472 473+ char32_t *copy = c32dup(ptext);474+ size_t tok_count = 1;475+ const char32_t **tokens = malloc(tok_count * sizeof(tokens[0]));476+ tokens[0] = copy;477+478+ for (char32_t *p = copy; *p != U'\0'; p++) {479+ if (*p != U' ')480+ continue;481+482+ *p = U'\0';483+ if ((ptrdiff_t)(p - tokens[tok_count - 1]) == 0) {484+ /* Collapse multiple spaces */485+ tokens[tok_count - 1] = p + 1;486+ } else {487+ tok_count++;488+ tokens = realloc(tokens, tok_count * sizeof(tokens[0]));489+ tokens[tok_count - 1] = p + 1;490+ }491+ }492+493+ if (tokens[tok_count - 1][0] == U'\0') {494+ /* Don’t count trailing spaces as a token */495+ tok_count--;496+ }497+498 for (size_t i = 0; i < matches->applications->count; i++) {499 struct application *app = &matches->applications->v[i];467- enum matched_type matched_type = MATCHED_NONE;468- ssize_t start_title = -1;500 501 if (!app->visible)502 continue;503 473- if (matched_type == MATCHED_NONE && match_name) {474- const char32_t *m = c32casestr(app->title, ptext);475- if (m != NULL) {476- matched_type = MATCHED_EXACT;477- start_title = m - app->title;478- LOG_DBG("%ls: exact title", (const wchar_t *)app->title);479- } else if ((m = match_levenshtein(matches, app->title, ptext)) != NULL) {480- matched_type = MATCHED_FUZZY;481- start_title = m - app->title;482- LOG_DBG("%ls: fuzzy title", (const wchar_t *)app->title);504+ enum matched_type app_match_type = MATCHED_NONE;505+ size_t pos_count = 0;506+ struct match *match = &matches->matches[matches->match_count];507+ struct match_substring *pos = match->pos;508+509+ for (size_t j = 0; j < tok_count; j++) {510+ const char32_t *token = tokens[j];511+ enum matched_type token_match_type = MATCHED_NONE;512+513+ if (token_match_type == MATCHED_NONE && match_name) {514+ const char32_t *m = c32casestr(app->title, token);515+ size_t match_len = 0;516+517+ if (m != NULL) {518+ token_match_type = MATCHED_EXACT;519+ match_len = c32len(token);520+ LOG_DBG("%ls: exact title", (const wchar_t *)app->title);521+ } else if ((m = match_levenshtein(matches, app->title, token, &match_len)) != NULL) {522+ token_match_type = MATCHED_FUZZY;523+ LOG_DBG("%ls: fuzzy title", (const wchar_t *)app->title);524+ }525+526+ if (match_len > 0) {527+ size_t new_pos_count = pos_count + 1;528+ struct match_substring *new_pos = realloc(529+ pos, new_pos_count * sizeof(new_pos[0]));530+531+ if (new_pos != NULL) {532+ pos = new_pos;533+ pos_count = new_pos_count;534+535+ pos[pos_count - 1].start = m - app->title;536+ pos[pos_count - 1].len = match_len;537+538+ match->pos = pos;539+ }540+ }541 }484- }542 486- if (matched_type == MATCHED_NONE && match_filename && app->basename != NULL) {487- if (c32casestr(app->basename, ptext) != NULL) {488- matched_type = MATCHED_EXACT;489- LOG_DBG("%ls: exact filename", (const wchar_t *)app->title);543+ if (token_match_type == MATCHED_NONE && match_filename && app->basename != NULL) {544+ if (c32casestr(app->basename, token) != NULL) {545+ token_match_type = MATCHED_EXACT;546+ LOG_DBG("%ls: exact filename", (const wchar_t *)app->title);547+ }548+ else if (match_levenshtein(matches, app->basename, token, NULL) != NULL) {549+ token_match_type = MATCHED_FUZZY;550+ LOG_DBG("%ls: fuzzy filename", (const wchar_t *)app->title);551+ }552 }491- else if (match_levenshtein(matches, app->basename, ptext) != NULL) {492- matched_type = MATCHED_FUZZY;493- LOG_DBG("%ls: fuzzy filename", (const wchar_t *)app->title);494- }495- }553 497- if (matched_type == MATCHED_NONE && match_generic && app->generic_name != NULL) {498- if (c32casestr(app->generic_name, ptext) != NULL) {499- matched_type = MATCHED_EXACT;500- LOG_DBG("%ls: exact generic", (const wchar_t *)app->title);554+ if (token_match_type == MATCHED_NONE && match_generic && app->generic_name != NULL) {555+ if (c32casestr(app->generic_name, token) != NULL) {556+ token_match_type = MATCHED_EXACT;557+ LOG_DBG("%ls: exact generic", (const wchar_t *)app->title);558+ }559+ else if (match_levenshtein(matches, app->generic_name, token, NULL) != NULL) {560+ token_match_type = MATCHED_FUZZY;561+ LOG_DBG("%ls: fuzzy generic", (const wchar_t *)app->title);562+ }563 }502- else if (match_levenshtein(matches, app->generic_name, ptext) != NULL) {503- matched_type = MATCHED_FUZZY;504- LOG_DBG("%ls: fuzzy generic", (const wchar_t *)app->title);505- }506- }564 508- if (matched_type == MATCHED_NONE && match_exec && app->wexec != NULL) {509- if (c32casestr(app->wexec, ptext) != NULL) {510- matched_type = MATCHED_EXACT;511- LOG_DBG("%ls: exact exec", (const wchar_t *)app->title);512- } else if (match_levenshtein(matches, app->wexec, ptext) != NULL) {513- matched_type = MATCHED_FUZZY;514- LOG_DBG("%ls: fuzzy exec", (const wchar_t *)app->title);565+ if (token_match_type == MATCHED_NONE && match_exec && app->wexec != NULL) {566+ if (c32casestr(app->wexec, token) != NULL) {567+ token_match_type = MATCHED_EXACT;568+ LOG_DBG("%ls: exact exec", (const wchar_t *)app->title);569+ } else if (match_levenshtein(matches, app->wexec, token, NULL) != NULL) {570+ token_match_type = MATCHED_FUZZY;571+ LOG_DBG("%ls: fuzzy exec", (const wchar_t *)app->title);572+ }573 }516- }574 518- if (matched_type == MATCHED_NONE && match_comment && app->comment != NULL) {519- if (c32casestr(app->comment, ptext) != NULL) {520- matched_type = MATCHED_EXACT;521- LOG_DBG("%ls: exact comment", (const wchar_t *)app->title);522- } else if (match_levenshtein(matches, app->comment, ptext) != NULL) {523- matched_type = MATCHED_FUZZY;524- LOG_DBG("%ls: fuzzy comment", (const wchar_t *)app->title);575+ if (token_match_type == MATCHED_NONE && match_comment && app->comment != NULL) {576+ if (c32casestr(app->comment, token) != NULL) {577+ token_match_type = MATCHED_EXACT;578+ LOG_DBG("%ls: exact comment", (const wchar_t *)app->title);579+ } else if (match_levenshtein(matches, app->comment, token, NULL) != NULL) {580+ token_match_type = MATCHED_FUZZY;581+ LOG_DBG("%ls: fuzzy comment", (const wchar_t *)app->title);582+ }583 }526- }584 528- if (matched_type == MATCHED_NONE && match_keywords) {529- tll_foreach(app->keywords, it) {530- if (c32casestr(it->item, ptext) != NULL) {531- matched_type = MATCHED_EXACT;532- LOG_DBG("%ls: exact keyword (%ls)",533- (const wchar_t *)app->title, (const wchar_t *)it->item);534- break;535- } else if (match_levenshtein(matches, it->item, ptext) != NULL) {536- matched_type = MATCHED_FUZZY;537- LOG_DBG("%ls: fuzzy keyword (%ls)",538- (const wchar_t *)app->title, (const wchar_t *)it->item);539- break;585+ if (token_match_type == MATCHED_NONE && match_keywords) {586+ tll_foreach(app->keywords, it) {587+ if (c32casestr(it->item, token) != NULL) {588+ token_match_type = MATCHED_EXACT;589+ LOG_DBG("%ls: exact keyword (%ls)",590+ (const wchar_t *)app->title, (const wchar_t *)it->item);591+ break;592+ } else if (match_levenshtein(matches, it->item, token, NULL) != NULL) {593+ token_match_type = MATCHED_FUZZY;594+ LOG_DBG("%ls: fuzzy keyword (%ls)",595+ (const wchar_t *)app->title, (const wchar_t *)it->item);596+ break;597+ }598 }599 }542- }600 544- if (matched_type == MATCHED_NONE && match_categories) {545- tll_foreach(app->categories, it) {546- if (c32casestr(it->item, ptext) != NULL) {547- matched_type = MATCHED_EXACT;548- LOG_DBG("%ls: exact category (%ls)",549- (const wchar_t *)app->title, (const wchar_t *)it->item);550- break;551- } else if (match_levenshtein(matches, it->item, ptext) != NULL) {552- matched_type = MATCHED_FUZZY;553- LOG_DBG("%ls: fuzzy category (%ls)",554- (const wchar_t *)app->title, (const wchar_t *)it->item);555- break;601+ if (token_match_type == MATCHED_NONE && match_categories) {602+ tll_foreach(app->categories, it) {603+ if (c32casestr(it->item, token) != NULL) {604+ token_match_type = MATCHED_EXACT;605+ LOG_DBG("%ls: exact category (%ls)",606+ (const wchar_t *)app->title, (const wchar_t *)it->item);607+ break;608+ } else if (match_levenshtein(matches, it->item, token, NULL) != NULL) {609+ token_match_type = MATCHED_FUZZY;610+ LOG_DBG("%ls: fuzzy category (%ls)",611+ (const wchar_t *)app->title, (const wchar_t *)it->item);612+ break;613+ }614 }615 }616+617+ /*618+ * When propagating a token match type to the “top-level”619+ * application match type, we use the following rules:620+ *621+ * - all tokens must match: that is MATCHED_NONE -> MATCHED_NONE622+ * - the application is an exact match *iff* there’s a623+ * single token, *and* that token is an exact match.624+ */625+626+ if (token_match_type == MATCHED_NONE) {627+ app_match_type = MATCHED_NONE;628+ break;629+ }630+631+ else {632+ if (app_match_type != MATCHED_FUZZY)633+ app_match_type = token_match_type;634+ }635 }636 560- if (matched_type == MATCHED_NONE)637+ if (app_match_type == MATCHED_NONE)638 continue;639 563- matches->matches[matches->match_count++] = (struct match){564- .matched_type = matched_type,640+ *match = (struct match){641+ .matched_type = app_match_type,642 .application = app,566- .start_title = start_title,643+ .pos = pos,644+ .pos_count = pos_count,645 .index = i,646 };647+648+ matches->match_count++;649 }650 651+ free(copy);652+ free(tokens);653+654 LOG_DBG("match update done");655 656 /* Sort */657 qsort(matches->matches, matches->match_count, sizeof(matches->matches[0]), &match_compar);9 unmodified linesmodified:match.h+7 added-1 removed
11 unmodified lines12 MATCHED_EXACT,13 MATCHED_FUZZY,14 };15 16+struct match_substring {17+ ssize_t start;18+ size_t len;19+};20+21 struct match {22 enum matched_type matched_type;23 struct application *application;19- ssize_t start_title;24+ struct match_substring *pos;25+ size_t pos_count;26 size_t index;27 };28 29 struct matches;28 unmodified linesmodified:render.c+15 added-3 removed
237 unmodified lines238 }239 240 static void241 render_match_text(struct buffer *buf, double *_x, double _y, double max_x,242- const char32_t *text, ssize_t start, size_t length,242+ const char32_t *text, size_t match_count,243+ const struct match_substring matches[static match_count],244 struct fcft_font *font, enum fcft_subpixel subpixel,245 int letter_spacing,246 pixman_color_t regular_color, pixman_color_t match_color,247 struct fcft_text_run **run)41 unmodified lines289 }290 }291 292 for (size_t i = 0; i < count; i++) {292- bool is_match = start >= 0 && clusters[i] >= start && clusters[i] < start + length;293+ bool is_match = false;294+ for (size_t j = 0; j < match_count; j++) {295+ const struct match_substring *match = &matches[j];296+ assert(match->start >= 0);297 298+ if (clusters[i] >= match->start &&299+ clusters[i] < match->start + match->len)300+ {301+ is_match = true;302+ break;303+ }304+ }305+306 if (x + (kern != NULL ? kern[i] : 0) + glyphs[i]->advance.x >= max_x) {307 const struct fcft_glyph *ellipses =308 fcft_rasterize_char_utf32(font, U'…', subpixel);309 351 unmodified lines661 662 /* Application title */663 render_match_text(664 buf, &cur_x, y, max_x - (ellipses != NULL ? ellipses->width : 0),653- match->application->title, match->start_title, c32len(prompt_text(prompt)),665+ match->application->title, match->pos_count, match->pos,666 font, subpixel,667 pt_or_px_as_pixels(render, &render->conf->letter_spacing),668 (i == selected669 ? render->pix_selection_text_color114 unmodified lines