match.c
This file
Path
match.c
1,746 lines · 52.1 KiB · C
Last changed by
🐛 Fix sort order regression from 1.14.0Mark Stosbergbefore adoptionf6772e4
History of this fileTake it
The walk stopped after 200 commits: lines older than 6a4c416 point at it.
changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1–102why, one step back1#include "match.h"23#include <assert.h>4#include <errno.h>5#include <pthread.h>6#include <semaphore.h>7#include <signal.h>8#include <stdlib.h>9#include <string.h>10#include <string.h>11#include <threads.h>1213#include <sys/epoll.h>14#include <sys/timerfd.h>1516#include "macros.h"17#if HAS_INCLUDE(<pthread_np.h>)18#include <pthread_np.h>19#define pthread_setname_np(thread, name) (pthread_set_name_np(thread, name), 0)20#elif defined(__NetBSD__)21#define pthread_setname_np(thread, name) pthread_setname_np(thread, "%s", (void *)name)22#endif2324#include <tllist.h>2526#define LOG_MODULE "match"27#define LOG_ENABLE_DBG 028#include "log.h"29#include "char32.h"30#include "wayland.h"31#include "xmalloc.h"32#include "xsnprintf.h"3334#define min(x, y) ((x < y) ? (x) : (y))35#define max(x, y) ((x > y) ? (x) : (y))3637enum delayed_update_type {38 DELAYED_NO_UPDATE,39 DELAYED_FULL_UPDATE,40 DELAYED_INCREMENTAL_UPDATE,41 DELAYED_UPDATE_IN_PROGRESS,42};4344struct matches {45 struct fdm *fdm;46 struct wayland *wayl;47 const struct prompt *prompt;48 struct application_list *applications;49 enum match_fields fields;50 bool all_apps_loaded;5152 enum match_mode mode;53 bool sort_result;5455 struct match *matches;56 _Atomic size_t match_count; /* Number of matched applications */57 size_t matches_size; /* Number of elements in 'matches' */5859 size_t page_count;60 size_t selected;61 size_t max_matches_per_page;62 size_t fuzzy_min_length;63 size_t fuzzy_max_length_discrepancy;64 size_t fuzzy_max_distance;65 bool have_icons;6667 size_t delay_ms;68 size_t delay_limit;69 int delay_fd;70 enum delayed_update_type delayed_update_type;7172 /* Thread synchronization */73 struct {74 uint16_t count;75 sem_t start;76 sem_t done;77 mtx_t lock;78 tll(uint64_t) queue;79 thrd_t *threads;8081 /* Set before feeding thread with sorting data */82 bool incremental;83 const char32_t *const *tokens;84 size_t *tok_lengths;85 size_t tok_count;8687 struct match *old_matches;88 } workers;89};9091struct thread_context {92 struct matches *matches;93 int my_id;94};9596struct levenshtein_matrix {97 size_t distance;98 enum choice { UNSET, FIRST, SECOND, THIRD } choice;99};100101static int match_thread(void *_ctx);102match: fzf: prioritize matches at word boundary, like fzfMark Stosbergbefore adoption6554dbe
lines 103–114why, one step back103static bool104is_word_boundary(const char32_t *str, size_t pos)105{106 /* Position 0 is always a word boundary */107 if (pos == 0)108 return true;109110 /* Check if character before is a space/separator */111 char32_t prev = str[pos - 1];112 return isc32space(prev);113}114changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 115–436why, one step back115static char32_t *116match_exact(const char32_t *haystack, size_t haystack_len,117 const char32_t *needle, size_t needle_len)118{119 return memmem(haystack, haystack_len * sizeof(char32_t),120 needle, needle_len * sizeof(char32_t));121}122123static void124match_fzf(const char32_t *haystack, size_t haystack_len,125 const char32_t *needle, size_t needle_len,126 struct match_substring **pos, size_t *pos_count,127 enum matched_type *match_type)128{129 const char32_t *const needle_end = &needle[needle_len];130 const char32_t *const haystack_end = &haystack[haystack_len];131132 const char32_t *haystack_search_start = haystack;133134 while (needle < needle_end) {135 if (haystack_search_start >= haystack_end) {136 if (pos != NULL) {137 free(*pos);138 *pos = NULL;139 *pos_count = 0;140 }141142 if (match_type != NULL)143 *match_type = MATCHED_NONE;144145 return;146 }147148 size_t longest_match_len = 0;149 size_t longest_match_ofs = 0;150151 for (const char32_t *start = haystack_search_start;152 start < haystack_end;153 start++)154 {155 const char32_t *n = needle;156 const char32_t *h = start;157158 size_t match_len = 0;159 while (n < needle_end &&160 h < haystack_end && *n == *h)161 {162 match_len++;163 n++;164 h++;165 }166167 if (match_len > longest_match_len) {168 longest_match_len = match_len;169 longest_match_ofs = start - haystack;170 }171172 if (n >= needle_end) {173 /* We've matched all of the search string; no need to174 look for any longer matches */175 break;176 }177 }178179 if (longest_match_len == 0) {180 if (match_type != NULL)181 *match_type = MATCHED_NONE;182 return;183 }184185 if (pos != NULL) {186 assert(pos_count != NULL);187 (*pos_count)++;188 *pos = xreallocarray(*pos, *pos_count, sizeof((*pos)[0]));189 (*pos)[*pos_count - 1].start = longest_match_ofs;190 (*pos)[*pos_count - 1].len = longest_match_len;191 }192193 if (match_type != NULL)194 *match_type = MATCHED_EXACT;195196 needle += longest_match_len;197 haystack_search_start = haystack + longest_match_ofs + longest_match_len;198 }199}200201static void202levenshtein_distance(const char32_t *a, size_t alen,203 const char32_t *b, size_t blen,204 struct levenshtein_matrix **m)205{206 for (size_t i = 1; i <= blen; i++) {207 m[i][0].distance = i;208 m[i][0].choice = FIRST;209 }210211 for (size_t i = 1; i <= blen; i++) {212 for (size_t j = 1; j <= alen; j++) {213 const size_t cost = a[j - 1] == b[i - 1] ? 0 : 1;214 const size_t first = m[i - 1][j].distance + 1;215 const size_t second = m[i][j - 1].distance + 1;216 const size_t third = m[i - 1][j - 1].distance + cost;217218 const size_t shortest = min(min(first, second), third);219 m[i][j].distance = shortest;220221 if (shortest == first)222 m[i][j].choice = FIRST;223 else if (shortest == second)224 m[i][j].choice = SECOND;225 else226 m[i][j].choice = THIRD;227 }228 }229}230231static const char32_t *232match_levenshtein(struct matches *matches,233 const char32_t *src, size_t src_len,234 const char32_t *pat, size_t pat_len, size_t *_match_len)235{236 if (matches->mode != MATCH_MODE_FUZZY)237 return NULL;238239 if (pat_len < matches->fuzzy_min_length)240 return NULL;241242 if (src_len < pat_len)243 return NULL;244245 struct levenshtein_matrix **m = xcalloc(pat_len + 1, sizeof(m[0]));246 for (size_t i = 0; i < pat_len + 1; i++)247 m[i] = xcalloc(src_len + 1, sizeof(m[0][0]));248249 levenshtein_distance(src, src_len, pat, pat_len, m);250251#if 0252 /* Dump levenshtein table */253 printf(" ");254 for (size_t j = 0; j < src_len; j++)255 printf("%lc ", src[j]);256 printf("\n");257 for (size_t i = 0; i < pat_len + 1; i++) {258 if (i > 0)259 printf("%lc ", pat[i - 1]);260 else261 printf(" ");262263 for (size_t j = 0; j < src_len + 1; j++)264 printf("%zu%c ", m[i][j].distance,265 m[i][j].choice == FIRST ? 'f' :266 m[i][j].choice == SECOND ? 's' :267 m[i][j].choice == THIRD ? 't' : 'u');268 printf("\n");269 }270#endif271272 size_t c = 0;273 size_t best = m[pat_len][0].distance;274275 for (ssize_t j = src_len; j >= 0; j--) {276 if (m[pat_len][j].distance < best) {277 best = m[pat_len][j].distance;278 c = j;279 }280 }281282 const size_t end = c;283 size_t r = pat_len;284 // printf("starting at r=%zu, c=%zu\n", r, c);285286 while (r > 0) {287 switch (m[r][c].choice) {288 case UNSET: assert(false); break;289 case FIRST: r--; break;290 case SECOND: c--; break;291 case THIRD: r--; c--; break;292 }293 }294295#if 0296 *match = (struct levenshtein_match){297 .start = c,298 .len = end - c,299 .distance = m[pat_len][end].distance,300 };301#endif302 const size_t match_ofs = c;303 const size_t match_distance = m[pat_len][end].distance;304 const size_t match_len = end - c;305306 LOG_DBG("%ls vs. %ls: sub-string: %.*ls, (distance=%zu, row=%zu, col=%zu)",307 (const wchar_t *)src, (const wchar_t *)pat,308 (int)match_len, (const wchar_t *)&src[match_ofs], match_distance, r, c);309310 for (size_t i = 0; i < pat_len + 1; i++)311 free(m[i]);312 free(m);313314 const size_t len_diff = match_len > pat_len315 ? match_len - pat_len316 : pat_len - match_len;317318 if (len_diff <= matches->fuzzy_max_length_discrepancy &&319 match_distance <= matches->fuzzy_max_distance)320 {321 if (_match_len != NULL)322 *_match_len = match_len;323 return &src[match_ofs];324 }325326 return NULL;327}328329static bool330fdm_delayed_timer(struct fdm *fdm, int fd, int events, void *data)331{332 if (events & EPOLLHUP)333 return false;334335 struct matches *matches = data;336337 uint64_t expiration_count;338 ssize_t ret = read(339 matches->delay_fd, &expiration_count, sizeof(expiration_count));340341 if (ret < 0) {342 if (errno == EAGAIN)343 return true;344345 LOG_ERRNO("failed to read blink timer");346 return false;347 }348349 const enum delayed_update_type update_type = matches->delayed_update_type;350 matches->delayed_update_type = DELAYED_UPDATE_IN_PROGRESS;351352 switch (update_type) {353 case DELAYED_NO_UPDATE:354 break;355356 case DELAYED_INCREMENTAL_UPDATE:357 matches_update_incremental(matches);358 break;359360 case DELAYED_FULL_UPDATE:361 matches_update(matches);362 break;363364 case DELAYED_UPDATE_IN_PROGRESS:365 assert(false);366 break;367 }368369 matches->delayed_update_type = DELAYED_NO_UPDATE;370 wayl_refresh(matches->wayl);371 return true;372}373374struct matches *375matches_init(struct fdm *fdm, const struct prompt *prompt,376 enum match_fields fields, enum match_mode mode, bool sort_result,377 size_t fuzzy_min_length, size_t fuzzy_max_length_discrepancy,378 size_t fuzzy_max_distance, uint16_t workers,379 size_t delay_ms, size_t delay_limit)380{381 struct matches *matches = xmalloc(sizeof(*matches));382 *matches = (struct matches) {383 .fdm = fdm,384 .prompt = prompt,385 .applications = NULL,386 .fields = fields,387 .mode = mode,388 .sort_result = sort_result,389 .matches = NULL,390 .matches_size = 0,391 .match_count = 0,392 .page_count = 0,393 .selected = 0,394 .max_matches_per_page = 0,395 .fuzzy_min_length = fuzzy_min_length,396 .fuzzy_max_length_discrepancy = fuzzy_max_length_discrepancy,397 .fuzzy_max_distance = fuzzy_max_distance,398 .delay_fd = -1,399 .delay_ms = delay_ms,400 .delay_limit = delay_limit,401 };402403 if (workers > 0) {404 if (sem_init(&matches->workers.start, 0, 0) < 0 ||405 sem_init(&matches->workers.done, 0, 0) < 0)406 {407 LOG_ERRNO("failed to instantiate match worker semaphores");408 goto err_free_matches;409 }410411 int err;412 if ((err = mtx_init(&matches->workers.lock, mtx_plain)) != thrd_success) {413 LOG_ERR("failed to instantiate match worker mutex: %d", err);414 goto err_free_semaphores;415 }416417 matches->workers.threads =418 xcalloc(workers, sizeof(matches->workers.threads[0]));419420 for (size_t i = 0; i < workers; i++) {421 struct thread_context *ctx = xmalloc(sizeof(*ctx));422 *ctx = (struct thread_context){423 .matches = matches,424 .my_id = 1 + i,425 };426427 int ret = thrd_create(428 &matches->workers.threads[i], &match_thread, ctx);429 if (ret != thrd_success) {430 LOG_ERR("failed to create match worker thread: %d", ret);431 matches->workers.threads[i] = 0;432 goto err_free_semaphores_and_lock;433 }434435 matches->workers.count++;436 }match+render: log number of worker threads (at info level)Daniel Eklöfbefore adoption7835e25
lines 437–438why, one step back437438 LOG_INFO("using %hu match worker threads", matches->workers.count);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 439–711why, one step back439 }440441 int timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);442 if (timer_fd < 0) {443 LOG_ERRNO("failed to create timerfd");444 goto err_free_semaphores_and_lock;445 }446447 if (!fdm_add(fdm, timer_fd, EPOLLIN, &fdm_delayed_timer, matches)) {448 close(timer_fd);449 goto err_free_semaphores_and_lock;450 }451452 matches->delay_fd = timer_fd;453 return matches;454455err_free_semaphores_and_lock:456 mtx_destroy(&matches->workers.lock);457err_free_semaphores:458 sem_destroy(&matches->workers.start);459 sem_destroy(&matches->workers.done);460err_free_matches:461 free(matches);462 return NULL;463}464465void466matches_destroy(struct matches *matches)467{468 if (matches == NULL)469 return;470471 fdm_del(matches->fdm, matches->delay_fd);472473 mtx_lock(&matches->workers.lock);474 {475 assert(tll_length(matches->workers.queue) == 0);476477 for (size_t i = 0; i < matches->workers.count; i++) {478 assert(matches->workers.threads[i] != 0);479 sem_post(&matches->workers.start);480 tll_push_back(matches->workers.queue, (uint64_t)-2);481 }482 }483 mtx_unlock(&matches->workers.lock);484485 for (size_t i = 0; i < matches->workers.count; i++)486 thrd_join(matches->workers.threads[i], NULL);487488 mtx_lock(&matches->applications->lock);489 if (matches->applications != NULL) {490 for (size_t i = 0; i < matches->matches_size; i++)491 free(matches->matches[i].pos);492 }493 mtx_unlock(&matches->applications->lock);494495 free(matches->workers.threads);496 free(matches->matches);497 free(matches->workers.old_matches);498 mtx_destroy(&matches->workers.lock);499 sem_destroy(&matches->workers.start);500 sem_destroy(&matches->workers.done);501 free(matches);502}503504void505matches_set_wayland(struct matches *matches, struct wayland *wayl)506{507 assert(matches->wayl == NULL);508 matches->wayl = wayl;509}510511void512matches_all_applications_loaded(struct matches *matches)513{514 matches->all_apps_loaded = true;515#if defined(_DEBUG)516 matches_lock(matches);517 assert(matches->matches_size == matches->applications->count);518 matches_unlock(matches);519#endif520}521522void523matches_set_applications(struct matches *matches,524 struct application_list *applications)525{526 mtx_lock(&applications->lock);527528 assert(matches->matches == NULL || matches->matches_size > 0);529 assert(applications->count >= matches->matches_size);530531 matches->applications = applications;532533 if (matches->matches == NULL && applications->count == 0) {534 mtx_unlock(&applications->lock);535 return;536 }537538 matches->matches = xreallocarray(539 matches->matches, applications->count, sizeof(matches->matches[0]));540541 const size_t old_size = matches->matches_size;542 const size_t diff = applications->count - old_size;543544 memset(&matches->matches[old_size], 0, diff * sizeof(matches->matches[0]));545546 matches->matches_size = applications->count;547 mtx_unlock(&applications->lock);548 matches_icons_loaded(matches);549}550551void552matches_icons_loaded(struct matches *matches)553{554 if (matches->have_icons)555 return;556557 mtx_lock(&matches->applications->lock);558 for (size_t i = 0; i < matches->applications->count; i++) {559 if (matches->applications->v[i]->icon.name != NULL) {560 matches->have_icons = true;561 break;562 }563 }564 mtx_unlock(&matches->applications->lock);565}566567bool568matches_have_icons(const struct matches *matches)569{570 return matches->have_icons;571}572573void574matches_lock(struct matches *matches)575{576 mtx_lock(&matches->applications->lock);577}578579void580matches_unlock(struct matches *matches)581{582 mtx_unlock(&matches->applications->lock);583}584585size_t586matches_max_matches_per_page(const struct matches *matches)587{588 return matches->max_matches_per_page;589}590591void592matches_max_matches_per_page_set(struct matches *matches, size_t max_matches)593{594 matches->max_matches_per_page = max_matches;595}596597size_t598matches_get_page_count(const struct matches *matches)599{600 return matches->max_matches_per_page != 0601 ? matches->match_count / matches->max_matches_per_page602 : matches->match_count;603}604605size_t606matches_get_page(const struct matches *matches)607{608 return matches->max_matches_per_page != 0609 ? matches->selected / matches->max_matches_per_page610 : matches->selected;611}612613size_t614match_get_idx(const struct matches *matches, size_t idx)615{616 const size_t page_no = matches_get_page(matches);617 const size_t items_on_page UNUSED = matches_get_count(matches);618619 LOG_DBG(620 "page-count: %zu, page-no: %zu, items-on-page: %zu, idx: %zu, max: %zu, "621 "match-count: %zu",622 matches->page_count, page_no, items_on_page, idx,623 matches->max_matches_per_page, matches->match_count);624625 assert(idx < items_on_page);626 idx += page_no * matches->max_matches_per_page;627628 assert(idx < matches->match_count);629 return idx;630}631632const struct match *633matches_get(const struct matches *matches, size_t idx)634{635 return &matches->matches[match_get_idx(matches, idx)];636}637638const struct match *639matches_get_match(const struct matches *matches)640{641 return matches->match_count > 0642 ? &matches->matches[matches->selected]643 : NULL;644}645646size_t647matches_get_application_visible_count(const struct matches *matches)648{649 return matches->applications->visible_count;650}651652size_t653matches_get_count(const struct matches *matches)654{655 const size_t total = matches->match_count;656 const size_t page_no = matches_get_page(matches);657658 if (matches->max_matches_per_page == 0)659 return 0;660661 if (total == 0)662 return 0;663 else if (page_no + 1 >= matches->page_count) {664 size_t remainder = total % matches->max_matches_per_page;665 return remainder == 0 ? matches->max_matches_per_page : remainder;666 } else667 return matches->max_matches_per_page;668}669670size_t671matches_get_total_count(const struct matches *matches)672{673 return matches->match_count;674}675676size_t677matches_get_match_index(const struct matches *matches)678{679 return matches->max_matches_per_page != 0680 ? matches->selected % matches->max_matches_per_page681 : 0;682}683684bool685matches_selected_select(struct matches *matches, const char *_string)686{687 if (_string == NULL)688 return false;689690 char32_t *string = ambstoc32(_string);691 if (string == NULL)692 return false;693694 for (size_t i = 0; i < matches->match_count; i++) {695 if (match_exact(matches->matches[i].application->title,696 matches->matches[i].application->title_len,697 string, c32len(string)) != NULL)698 {699 matches->selected = i;700 free(string);701 return true;702 }703 }704705 free(string);706 return false;707}708709bool710matches_idx_select(struct matches *matches, size_t idx)711{wayland: don't ignore the mouse's X coordinate when selectingDaniel Eklöfbefore adoption1f6e155
line 712why, one step back712 if (idx == (size_t)-1)changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 713–825why, one step back713 return false;714715 matches->selected = match_get_idx(matches, idx);716 return true;717}718719bool720matches_selected_first(struct matches *matches)721{722 if (matches->match_count <= 0 || matches->selected <= 0)723 return false;724725 matches->selected = 0;726 return true;727}728729bool730matches_selected_set(struct matches *matches, size_t idx)731{732 if (matches == NULL || matches->match_count <= 0 || idx >= matches->match_count)733 return false;734735 matches->selected = idx;736 return true;737}738739bool740matches_selected_last(struct matches *matches)741{742 if (matches->match_count <= 0 ||743 matches->selected >= matches->match_count - 1)744 {745 return false;746 }747748 matches->selected = matches->match_count - 1;749 return true;750}751752bool753matches_selected_prev(struct matches *matches, bool wrap)754{755 if (matches->selected > 0) {756 matches->selected--;757 return true;758 } else if (wrap && matches->match_count > 1) {759 matches->selected = matches->match_count - 1;760 return true;761 }762763 return false;764}765766bool767matches_selected_next(struct matches *matches, bool wrap)768{769 if (matches->selected + 1 < matches->match_count) {770 matches->selected++;771 return true;772 } else if (wrap && matches->match_count > 1) {773 matches->selected = 0;774 return true;775 }776777 return false;778}779780bool781matches_selected_prev_page(struct matches *matches, bool scrolling)782{783 const size_t page_no = matches_get_page(matches);784 if (page_no > 0) {785 assert(matches->selected >= matches->max_matches_per_page);786 matches->selected -= matches->max_matches_per_page;787 return true;788 } else if (!scrolling && matches->selected > 0) {789 matches->selected = 0;790 return true;791 }792793 return false;794}795796bool797matches_selected_next_page(struct matches *matches, bool scrolling)798{799 const size_t page_no = matches_get_page(matches);800 if (page_no + 1 < matches->page_count) {801 matches->selected = min(802 matches->selected + matches->max_matches_per_page,803 matches->match_count - 1);804 return true;805 } else if (!scrolling && matches->selected < matches->match_count - 1) {806 matches->selected = matches->match_count - 1;807 return true;808 }809810 return false;811}812813static int814match_compar(const void *_a, const void *_b)815{816 const struct match *a = _a;817 const struct match *b = _b;818819 if (a->matched_type != b->matched_type) {820 if (a->matched_type == MATCHED_EXACT)821 return -1;822 else823 return 1;824 }825🐛 Fix sort order regression from 1.14.0Mark Stosbergbefore adoptionf6772e4
line 826why, one step back826 if (a->score > b->score)changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 827–829why, one step back827 return -1;828 else if (a->score < b->score)829 return 1;🐛 Fix sort order regression from 1.14.0Mark Stosbergbefore adoptionf6772e4
lines 830–833why, one step back830 /* Prioritize matches at word boundaries (when scores are equal) */831 else if (a->word_boundary != b->word_boundary) {832 return a->word_boundary ? -1 : 1;833 }changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 834–863why, one step back834 else if (a->pos_count < b->pos_count)835 return -1;836 else if (a->pos_count > b->pos_count)837 return 1;838 else if (a->application->count > b->application->count)839 return -1;840 else if (a->application->count < b->application->count)841 return 1;842 else if (a->pos_count > 0 && b->pos_count > 0 && a->pos[0].start < b->pos[0].start)843 return -1;844 else if (a->pos_count > 0 && b->pos_count > 0 && a->pos[0].start > b->pos[0].start)845 return 1;846 else if (a->pos_count > 0 && b->pos_count > 0 &&847 a->application->title != NULL && b->application->title != NULL)848 {849 return a->application->title_len > b->application->title_len;850 } else851 return 0;852}853854static void855match_app(struct matches *matches, struct application *app,856 size_t tok_count, const char32_t *const tokens[static tok_count],857 const size_t tok_lengths[static tok_count],858 bool match_name,859 bool match_filename,860 bool match_generic,861 bool match_exec,862 bool match_comment,863 bool match_keywords,dmenu: add support for --match-nthDaniel Eklöfbefore adoptionc006428
lines 864–865why, one step back864 bool match_categories,865 bool match_nth)changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 866–874why, one step back866{867 size_t pos_count = 0;868 struct match_substring *pos = NULL;869870 enum matched_type match_type_name = MATCHED_NONE;871 enum matched_type match_type_filename = MATCHED_NONE;872 enum matched_type match_type_generic = MATCHED_NONE;873 enum matched_type match_type_exec = MATCHED_NONE;874 enum matched_type match_type_comment = MATCHED_NONE;dmenu: add support for --match-nthDaniel Eklöfbefore adoptionc006428
line 875why, one step back875 enum matched_type match_type_nth = MATCHED_NONE;changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 876–949why, one step back876 enum matched_type match_type_keywords[tll_length(app->keywords)];877 enum matched_type match_type_categories[tll_length(app->categories)];878879 if (match_keywords) {880 for (size_t k = 0; k < tll_length(app->keywords); k++)881 match_type_keywords[k] = MATCHED_NONE;882 }883 if (match_categories) {884 for (size_t k = 0; k < tll_length(app->categories); k++)885 match_type_categories[k] = MATCHED_NONE;886 }887888 for (size_t t = 0; t < tok_count; t++) {889 const char32_t *const tok = tokens[t];890 const size_t tok_len = tok_lengths[t];891892 if (match_name && (t == 0 || match_type_name != MATCHED_NONE)) {893 const char32_t *m = NULL;894 size_t match_len = 0;895 enum matched_type match_type = MATCHED_NONE;896897 switch (matches->mode) {898 case MATCH_MODE_EXACT:899 m = match_exact(app->title_lowercase, app->title_len, tok, tok_len);900 if (m != NULL) {901 match_type = MATCHED_EXACT;902 match_len = tok_len;903 }904 break;905906 case MATCH_MODE_FZF:907 match_fzf(app->title_lowercase, app->title_len,908 tok, tok_len, &pos, &pos_count, &match_type);909 break;910911 case MATCH_MODE_FUZZY:912 m = match_exact(app->title_lowercase, app->title_len, tok, tok_len);913 if (m != NULL) {914 match_type = MATCHED_EXACT;915 match_len = tok_len;916 } else {917 m = match_levenshtein(918 matches, app->title_lowercase, app->title_len,919 tok, tok_len, &match_len);920 if (m != NULL)921 match_type = MATCHED_FUZZY;922 }923 break;924 }925926 if (match_len > 0) {927 assert(matches->mode != MATCH_MODE_FZF);928929 if (pos_count > 0 && m == &app->title_lowercase[pos[pos_count - 1].start +930 pos[pos_count - 1].len]) {931 /* Extend last match position */932 pos[pos_count - 1].len += match_len;933 } else {934 pos_count += 1;935 pos = xreallocarray(pos, pos_count, sizeof(pos[0]));936937 pos[pos_count - 1].start = m - app->title_lowercase;938 pos[pos_count - 1].len = match_len;939 }940 }941942 if (t == 0)943 match_type_name = match_type;944 else if (match_type == MATCHED_NONE)945 match_type_name = MATCHED_NONE;946 else if (match_type_name == MATCHED_EXACT)947 match_type_name = match_type;948 }949dmenu: add support for --match-nthDaniel Eklöfbefore adoptionc006428
lines 950–997why, one step back950 if (match_nth && app->dmenu_match_nth != NULL &&951 (t == 0 || match_type_nth != MATCHED_NONE))952 {953 const char32_t *m = NULL;954 size_t match_len = 0;955 enum matched_type match_type = MATCHED_NONE;956957 switch (matches->mode) {958 case MATCH_MODE_EXACT:959 m = match_exact(960 app->dmenu_match_nth, app->dmenu_match_nth_len, tok, tok_len);961962 if (m != NULL) {963 match_type = MATCHED_EXACT;964 match_len = tok_len;965 }966 break;967968 case MATCH_MODE_FZF:969 match_fzf(app->dmenu_match_nth, app->dmenu_match_nth_len,970 tok, tok_len, NULL, NULL, &match_type);971 break;972973 case MATCH_MODE_FUZZY:974 m = match_exact(975 app->dmenu_match_nth, app->dmenu_match_nth_len, tok, tok_len);976977 if (m != NULL) {978 match_type = MATCHED_EXACT;979 match_len = tok_len;980 } else {981 m = match_levenshtein(982 matches, app->dmenu_match_nth, app->dmenu_match_nth_len,983 tok, tok_len, &match_len);984 if (m != NULL)985 match_type = MATCHED_FUZZY;986 }987 break;988 }989990 if (t == 0)991 match_type_nth = match_type;992 else if (match_type == MATCHED_NONE)993 match_type_nth = MATCHED_NONE;994 else if (match_type_nth == MATCHED_EXACT)995 match_type_nth = match_type;996 }997changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 998–1338why, one step back998 if (match_filename && app->basename != NULL &&999 (t == 0 || match_type_filename != MATCHED_NONE))1000 {1001 const char32_t *m = NULL;1002 size_t match_len = 0;1003 enum matched_type match_type = MATCHED_NONE;10041005 switch (matches->mode) {1006 case MATCH_MODE_EXACT:1007 m = match_exact(app->basename, app->basename_len, tok, tok_len);1008 if (m != NULL) {1009 match_type = MATCHED_EXACT;1010 match_len = tok_len;1011 }1012 break;10131014 case MATCH_MODE_FZF:1015 match_fzf(app->basename, app->basename_len, tok, tok_len,1016 NULL, NULL, &match_type);1017 break;10181019 case MATCH_MODE_FUZZY:1020 m = match_exact(app->basename, app->basename_len, tok, tok_len);1021 if (m != NULL) {1022 match_type = MATCHED_EXACT;1023 match_len = tok_len;1024 } else {1025 m = match_levenshtein(1026 matches, app->basename, app->basename_len,1027 tok, tok_len, &match_len);1028 if (m != NULL)1029 match_type = MATCHED_FUZZY;1030 }1031 break;1032 }10331034 if (t == 0)1035 match_type_filename = match_type;1036 else if (match_type == MATCHED_NONE)1037 match_type_filename = MATCHED_NONE;1038 else if (match_type_filename == MATCHED_EXACT)1039 match_type_filename = match_type;1040 }10411042 if (match_generic && app->generic_name != NULL &&1043 (t == 0 || match_type_generic != MATCHED_NONE))1044 {1045 const char32_t *m = NULL;1046 size_t match_len = 0;1047 enum matched_type match_type = MATCHED_NONE;10481049 switch (matches->mode) {1050 case MATCH_MODE_EXACT:1051 m = match_exact(app->generic_name, app->generic_name_len, tok, tok_len);1052 if (m != NULL) {1053 match_type = MATCHED_EXACT;1054 match_len = tok_len;1055 }1056 break;10571058 case MATCH_MODE_FZF:1059 match_fzf(app->generic_name, app->generic_name_len,1060 tok, tok_len, NULL, NULL, &match_type);1061 break;10621063 case MATCH_MODE_FUZZY:1064 m = match_exact(app->generic_name, app->generic_name_len, tok, tok_len);1065 if (m != NULL) {1066 match_type = MATCHED_EXACT;1067 match_len = tok_len;1068 } else {1069 m = match_levenshtein(1070 matches, app->generic_name, app->generic_name_len,1071 tok, tok_len, &match_len);1072 if (m != NULL)1073 match_type = MATCHED_FUZZY;1074 }1075 break;1076 }10771078 if (t == 0)1079 match_type_generic = match_type;1080 else if (match_type == MATCHED_NONE)1081 match_type_generic = MATCHED_NONE;1082 else if (match_type_generic == MATCHED_EXACT)1083 match_type_generic = match_type;1084 }10851086 if (match_exec && app->wexec != NULL &&1087 (t == 0 || match_type_exec != MATCHED_NONE))1088 {1089 const char32_t *m = NULL;1090 size_t match_len = 0;1091 enum matched_type match_type = MATCHED_NONE;10921093 switch (matches->mode) {1094 case MATCH_MODE_EXACT:1095 m = match_exact(app->wexec, app->wexec_len, tok, tok_len);1096 if (m != NULL) {1097 match_type = MATCHED_EXACT;1098 match_len = tok_len;1099 }1100 break;11011102 case MATCH_MODE_FZF:1103 match_fzf(app->wexec, app->wexec_len, tok, tok_len,1104 NULL, NULL, &match_type);1105 break;11061107 case MATCH_MODE_FUZZY:1108 m = match_exact(app->wexec, app->wexec_len, tok, tok_len);1109 if (m != NULL) {1110 match_type = MATCHED_EXACT;1111 match_len = tok_len;1112 } else {1113 m = match_levenshtein(1114 matches, app->wexec, app->wexec_len,1115 tok, tok_len, &match_len);1116 if (m != NULL)1117 match_type = MATCHED_FUZZY;1118 }1119 break;1120 }11211122 if (t == 0)1123 match_type_exec = match_type;1124 else if (match_type == MATCHED_NONE)1125 match_type_exec = MATCHED_NONE;1126 else if (match_type_exec == MATCHED_EXACT)1127 match_type_exec = match_type;1128 }11291130 if (match_comment && app->comment != NULL &&1131 (t == 0 || match_type_comment != MATCHED_NONE))1132 {1133 const char32_t *m = NULL;1134 size_t match_len = 0;1135 enum matched_type match_type = MATCHED_NONE;11361137 switch (matches->mode) {1138 case MATCH_MODE_EXACT:1139 m = match_exact(app->comment, app->comment_len, tok, tok_len);1140 if (m != NULL) {1141 match_type = MATCHED_EXACT;1142 match_len = tok_len;1143 }1144 break;11451146 case MATCH_MODE_FZF:1147 match_fzf(app->comment, app->comment_len, tok, tok_len,1148 NULL, NULL, &match_type);1149 break;11501151 case MATCH_MODE_FUZZY:1152 m = match_exact(app->comment, app->comment_len, tok, tok_len);1153 if (m != NULL) {1154 match_type = MATCHED_EXACT;1155 match_len = tok_len;1156 } else {1157 m = match_levenshtein(1158 matches, app->comment, app->comment_len,1159 tok, tok_len, &match_len);1160 if (m != NULL)1161 match_type = MATCHED_FUZZY;1162 }1163 break;1164 }11651166 if (t == 0)1167 match_type_comment = match_type;1168 else if (match_type == MATCHED_NONE)1169 match_type_comment = MATCHED_NONE;1170 else if (match_type_comment == MATCHED_EXACT)1171 match_type_comment = match_type;1172 }11731174 if (match_keywords) {1175 size_t k = 0;1176 tll_foreach(app->keywords, it) {1177 if (!(t == 0 || match_type_keywords[k] != MATCHED_NONE))1178 continue;11791180 const char32_t *m = NULL;1181 size_t match_len = 0;1182 enum matched_type match_type = MATCHED_NONE;11831184 switch (matches->mode) {1185 case MATCH_MODE_EXACT:1186 m = match_exact(it->item, c32len(it->item), tok, tok_len);1187 if (m != NULL) {1188 match_type = MATCHED_EXACT;1189 match_len = tok_len;1190 }1191 break;11921193 case MATCH_MODE_FZF:1194 match_fzf(it->item, c32len(it->item), tok, tok_len,1195 NULL, NULL, &match_type);1196 break;11971198 case MATCH_MODE_FUZZY:1199 m = match_exact(it->item, c32len(it->item), tok, tok_len);1200 if (m != NULL) {1201 match_type = MATCHED_EXACT;1202 match_len = tok_len;1203 } else {1204 m = match_levenshtein(1205 matches, it->item, c32len(it->item),1206 tok, tok_len, &match_len);1207 if (m != NULL)1208 match_type = MATCHED_FUZZY;1209 }1210 break;1211 }12121213 if (t == 0)1214 match_type_keywords[k] = match_type;1215 else if (match_type == MATCHED_NONE)1216 match_type_keywords[k] = MATCHED_NONE;1217 else if (match_type_keywords[k] == MATCHED_EXACT)1218 match_type_keywords[k] = match_type;12191220 k++;1221 }1222 }12231224 if (match_categories) {1225 size_t k = 0;1226 tll_foreach(app->categories, it) {1227 if (!(t == 0 || match_type_categories[k] != MATCHED_NONE))1228 continue;12291230 const char32_t *m = NULL;1231 size_t match_len = 0;1232 enum matched_type match_type = MATCHED_NONE;12331234 switch (matches->mode) {1235 case MATCH_MODE_EXACT:1236 m = match_exact(it->item, c32len(it->item), tok, tok_len);1237 if (m != NULL) {1238 match_type = MATCHED_EXACT;1239 match_len = tok_len;1240 }1241 break;12421243 case MATCH_MODE_FZF:1244 match_fzf(it->item, c32len(it->item), tok, tok_len,1245 NULL, NULL, &match_type);1246 break;12471248 case MATCH_MODE_FUZZY:1249 m = match_exact(it->item, c32len(it->item), tok, tok_len);1250 if (m != NULL) {1251 match_type = MATCHED_EXACT;1252 match_len = tok_len;1253 } else {1254 m = match_levenshtein(1255 matches, it->item, c32len(it->item),1256 tok, tok_len, &match_len);1257 if (m != NULL)1258 match_type = MATCHED_FUZZY;1259 }1260 break;1261 }12621263 if (t == 0)1264 match_type_categories[k] = match_type;1265 else if (match_type == MATCHED_NONE)1266 match_type_categories[k] = MATCHED_NONE;1267 else if (match_type_categories[k] == MATCHED_EXACT)1268 match_type_categories[k] = match_type;12691270 k++;1271 }1272 }1273 }12741275 enum matched_type match_type_keywords_final = MATCHED_NONE;12761277 if (match_keywords) {1278 for (size_t k = 0; k < tll_length(app->keywords); k++) {1279 if (match_type_keywords[k] != MATCHED_NONE) {1280 /* match_type_keywords_final represents the combined1281 result of all keywords; if a single keyword matched1282 exactly, treat the entire keywords field as an1283 exact match */1284 if (match_type_keywords_final != MATCHED_EXACT)1285 match_type_keywords_final = match_type_keywords[k];1286 }1287 }1288 }12891290 enum matched_type match_type_categories_final = MATCHED_NONE;12911292 if (match_categories) {1293 for (size_t k = 0; k < tll_length(app->categories); k++) {1294 if (match_type_categories[k] != MATCHED_NONE) {1295 /* match_type_categories_final represents the combined1296 result of all keywords; if a single keyword matched1297 exactly, treat the entire keywords field as an1298 exact match */1299 if (match_type_categories_final != MATCHED_EXACT)1300 match_type_categories_final = match_type_categories[k];1301 }1302 }1303 }13041305 enum matched_type app_match_type = MATCHED_NONE;13061307 /*1308 * For now, only track score for the application title. The longer1309 * consecutive match, the higher the score.1310 *1311 * This means searching for 'oo' will sort 'foot' before 'firefox1312 * browser', for example.1313 */1314 size_t score = 0;1315 if (match_name) {1316 size_t longest_match = 0;1317 for (size_t k = 0; k < pos_count; k++) {1318 if (pos[k].len > longest_match)1319 longest_match = pos[k].len;1320 }13211322 score = longest_match;1323 }13241325 if (match_type_name != MATCHED_NONE)1326 app_match_type = match_type_name;1327 else if (match_type_filename != MATCHED_NONE)1328 app_match_type = match_type_filename;1329 else if (match_type_generic != MATCHED_NONE)1330 app_match_type = match_type_generic;1331 else if (match_type_exec != MATCHED_NONE)1332 app_match_type = match_type_exec;1333 else if (match_type_comment != MATCHED_NONE)1334 app_match_type = match_type_comment;1335 else if (match_type_keywords_final != MATCHED_NONE)1336 app_match_type = match_type_keywords_final;1337 else if (match_type_categories_final != MATCHED_NONE)1338 app_match_type = match_type_categories_final;dmenu: add support for --match-nthDaniel Eklöfbefore adoptionc006428
lines 1339–1340why, one step back1339 else if (match_type_nth != MATCHED_NONE)1340 app_match_type = match_type_nth;changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1341–1346why, one step back13411342 if (app_match_type == MATCHED_NONE) {1343 free(pos);1344 return;1345 }1346match: fzf: prioritize matches at word boundary, like fzfMark Stosbergbefore adoption6554dbe
lines 1347–1353why, one step back1347 /* Check if match starts at word boundary */1348 bool word_boundary = false;1349 if (match_name && pos_count > 0) {1350 /* Check if first match position is at a word boundary in the title */1351 word_boundary = is_word_boundary(app->title_lowercase, pos[0].start);1352 }1353changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1354–1359why, one step back1354 struct match m = {1355 .matched_type = app_match_type,1356 .application = app,1357 .pos = pos,1358 .pos_count = pos_count,1359 .score = score,match: fzf: prioritize matches at word boundary, like fzfMark Stosbergbefore adoption6554dbe
line 1360why, one step back1360 .word_boundary = word_boundary,changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1361–1381why, one step back1361 };13621363 const size_t idx = matches->match_count++;13641365 free(matches->matches[idx].pos);1366 matches->matches[idx] = m;1367}13681369/* THREAD */1370static int1371match_thread(void *_ctx)1372{1373 struct thread_context *ctx = _ctx;1374 struct matches *matches = ctx->matches;1375 const int my_id = ctx->my_id;1376 free(ctx);13771378 sigset_t mask;1379 sigfillset(&mask);1380 pthread_sigmask(SIG_SETMASK, &mask, NULL);1381match: reduce length of thread title, to allow for core IDs > 99Daniel Eklöfbefore adoptionb4ed87b
lines 1382–1385why, one step back1382 /*1383 * Note: using 'mtch' instead of 'match', to allow for core IDs >1384 * 99.1385 */changelog: add missing refDaniel Eklöfbefore adoption6a4c416
line 1386why, one step back1386 char proc_title[16];match: reduce length of thread title, to allow for core IDs > 99Daniel Eklöfbefore adoptionb4ed87b
line 1387why, one step back1387 xsnprintf(proc_title, sizeof(proc_title), "fuzzel:mtch:%d", my_id);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1388–1399why, one step back13881389 if (pthread_setname_np(pthread_self(), proc_title) < 0)1390 LOG_ERRNO("render worker %d: failed to set process title", my_id);13911392 const enum match_fields fields = matches->fields;1393 const bool match_name = fields & MATCH_NAME;1394 const bool match_filename = fields & MATCH_FILENAME;1395 const bool match_generic = fields & MATCH_GENERIC;1396 const bool match_exec = fields & MATCH_EXEC;1397 const bool match_comment = fields & MATCH_COMMENT;1398 const bool match_keywords = fields & MATCH_KEYWORDS;1399 const bool match_categories = fields & MATCH_CATEGORIES;dmenu: add support for --match-nthDaniel Eklöfbefore adoptionc006428
line 1400why, one step back1400 const bool match_nth = fields & MATCH_NTH;changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1401–1456why, one step back14011402 sem_t *start = &matches->workers.start;1403 sem_t *done = &matches->workers.done;1404 mtx_t *lock = &matches->workers.lock;14051406 while (true) {1407 sem_wait(start);14081409 bool incremental = matches->workers.incremental;1410 const size_t tok_count = matches->workers.tok_count;1411 const char32_t *const *tokens = matches->workers.tokens;1412 const size_t *tok_lengths = matches->workers.tok_lengths;1413 struct match *prev_matches = matches->workers.old_matches;14141415 bool match_done = false;14161417 while (!match_done) {1418 mtx_lock(lock);1419 assert(tll_length(matches->workers.queue) > 0);14201421 const uint64_t slice_start_and_end =1422 tll_pop_front(matches->workers.queue);14231424 const uint32_t slice_start = slice_start_and_end;1425 const uint32_t slice_end = slice_start_and_end >> 32;14261427 mtx_unlock(lock);14281429 if (slice_start == (uint32_t)-1) {1430 match_done = true;1431 sem_post(done);1432 } else if (slice_start == (uint32_t)-2) {1433 /* Exit */1434 return 0;1435 } else {1436 struct application **apps = matches->applications->v;1437 struct application **a = &apps[slice_start];1438 struct match *m = &prev_matches[slice_start];14391440 for (size_t i = slice_start; i < slice_end; i++) {1441 struct application *app;14421443 if (incremental) {1444 app = m->application;1445 m++;1446 assert(app->visible);1447 } else {1448 app = *a;1449 a++;14501451 if (!app->visible)1452 continue;1453 }14541455 match_app(matches, app, tok_count, tokens, tok_lengths,1456 match_name, match_filename, match_generic, match_exec,dmenu: add support for --match-nthDaniel Eklöfbefore adoptionc006428
lines 1457–1458why, one step back1457 match_comment, match_keywords, match_categories,1458 match_nth);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1459–1466why, one step back1459 }1460 }1461 }1462 }14631464 return -1;1465}1466✨ Add --auto-select opt. When one entry is left, automatically select it.Mark Stosbergbefore adoption5751a65
line 1467why, one step back1467changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1468–1491why, one step back1468static void1469matches_update_internal(struct matches *matches, bool incremental)1470{1471 if (matches->applications == NULL)1472 return;14731474 matches_lock(matches);14751476 const char32_t *ptext = prompt_text(matches->prompt);14771478 /* Nothing entered; all programs found matches */1479 if (ptext[0] == '\0') {1480 matches->match_count = 0;1481 for (size_t i = 0; i < matches->matches_size; i++) {1482 if (!matches->applications->v[i]->visible)1483 continue;14841485 free(matches->matches[matches->match_count].pos);14861487 matches->matches[matches->match_count++] = (struct match){1488 .matched_type = MATCHED_NONE,1489 .application = matches->applications->v[i],1490 .pos = NULL,1491 .pos_count = 0,match: fzf: prioritize matches at word boundary, like fzfMark Stosbergbefore adoption6554dbe
line 1492why, one step back1492 .word_boundary = false,changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1493–1519why, one step back1493 };1494 }14951496 /* Sort */1497 if (matches->sort_result && matches->all_apps_loaded) {1498 qsort(matches->matches, matches->match_count,1499 sizeof(matches->matches[0]), &match_compar);1500 }15011502 if (matches->selected >= matches->match_count && matches->selected > 0)1503 matches->selected = matches->match_count - 1;15041505 matches->page_count = matches->max_matches_per_page > 01506 ? ((matches->match_count + (matches->max_matches_per_page - 1)) /1507 matches->max_matches_per_page)1508 : 1;1509 goto unlock_and_return;1510 }15111512 const enum match_fields fields = matches->fields;1513 const bool match_name = fields & MATCH_NAME;1514 const bool match_filename = fields & MATCH_FILENAME;1515 const bool match_generic = fields & MATCH_GENERIC;1516 const bool match_exec = fields & MATCH_EXEC;1517 const bool match_comment = fields & MATCH_COMMENT;1518 const bool match_keywords = fields & MATCH_KEYWORDS;1519 const bool match_categories = fields & MATCH_CATEGORIES;dmenu: add support for --match-nthDaniel Eklöfbefore adoptionc006428
line 1520why, one step back1520 const bool match_nth = fields & MATCH_NTH;changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1521–1523why, one step back15211522 LOG_DBG(1523 "matching: filename=%s, name=%s, generic=%s, exec=%s, categories=%s, "dmenu: add support for --match-nthDaniel Eklöfbefore adoptionc006428
line 1524why, one step back1524 "keywords=%s, comment=%s, nth=%d",changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1525–1530why, one step back1525 match_filename ? "yes" : "no",1526 match_name ? "yes" : "no",1527 match_generic ? "yes" : "no",1528 match_exec ? "yes" : "no",1529 match_categories ? "yes" : "no",1530 match_keywords ? "yes" : "no",dmenu: add support for --match-nthDaniel Eklöfbefore adoptionc006428
lines 1531–1532why, one step back1531 match_comment ? "yes" : "no",1532 match_nth ? "yes" : "no");changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1533–1634why, one step back15331534 LOG_DBG("match update begin");15351536 char32_t *copy = xc32dup(ptext);1537 char32_t **tokens = xmalloc(sizeof(tokens[0]));1538 size_t *tok_lengths = xmalloc(sizeof(tok_lengths[0]));1539 size_t tok_count = 1;1540 tokens[0] = copy;1541 tok_lengths[0] = 0;15421543 for (char32_t *p = copy; *p != U'\0'; p++) {1544 if (*p != U' ') {1545 *p = toc32lower(*p);1546 tok_lengths[tok_count - 1]++;1547 continue;1548 }15491550 *p = U'\0';1551 if ((ptrdiff_t)(p - tokens[tok_count - 1]) == 0) {1552 /* Collapse multiple spaces */1553 tokens[tok_count - 1] = p + 1;1554 } else {1555 tok_count++;15561557 tokens = xreallocarray(tokens, tok_count, sizeof(tokens[0]));15581559 tok_lengths = xreallocarray(1560 tok_lengths, tok_count, sizeof(tok_lengths[0]));15611562 tokens[tok_count - 1] = p + 1;1563 tok_lengths[tok_count - 1] = 0;1564 }1565 }15661567 if (tokens[tok_count - 1][0] == U'\0') {1568 /* Don’t count trailing spaces as a token */1569 tok_count--;1570 }15711572#if defined(_DEBUG)1573 for (size_t i = 0; i < tok_count; i++)1574 assert(c32len(tokens[i]) == tok_lengths[i]);1575#endif15761577 const size_t search_count =1578 incremental ? matches->match_count : matches->matches_size;15791580 const size_t slice_size = 4096;1581 const size_t slice_count = (search_count + slice_size - 1) / slice_size;1582 assert(search_count == 0 || slice_count >= 1);15831584 const bool use_threads = /*!incremental && */matches->workers.count > 0 && slice_count > 1;15851586 if (use_threads) {1587 mtx_lock(&matches->workers.lock);1588 matches->workers.incremental = incremental;1589 matches->workers.tok_count = tok_count;1590 matches->workers.tokens = (const char32_t *const *)tokens;1591 matches->workers.tok_lengths = tok_lengths;15921593 if (incremental) {1594 matches->workers.old_matches =1595 xreallocarray(matches->workers.old_matches,1596 matches->match_count,1597 sizeof(matches->workers.old_matches[0]));15981599 memcpy(matches->workers.old_matches,1600 matches->matches,1601 matches->match_count * sizeof(matches->matches[0]));1602 }16031604 for (size_t i = 0; i < matches->workers.count; i++)1605 sem_post(&matches->workers.start);1606 }16071608 matches->match_count = 0;16091610 for (size_t i = 0, slice_start_idx = 0; i < slice_count; i++, slice_start_idx += slice_size) {1611 const size_t count = min(search_count - slice_start_idx, slice_size);16121613 if (use_threads) {1614 tll_push_back(1615 matches->workers.queue,1616 (uint64_t)(slice_start_idx + count) << 32 | slice_start_idx);1617 } else {1618 for (size_t j = 0; j < count; j++) {1619 struct application *app = NULL;1620 const size_t idx = slice_start_idx + j;16211622 if (incremental) {1623 app = matches->matches[idx].application;1624 assert(app->visible);1625 } else {1626 app = matches->applications->v[idx];16271628 if (!app->visible)1629 continue;1630 }16311632 match_app(matches, app,1633 tok_count, (const char32_t *const *)tokens, tok_lengths,1634 match_name, match_filename, match_generic, match_exec,dmenu: add support for --match-nthDaniel Eklöfbefore adoptionc006428
line 1635why, one step back1635 match_comment, match_keywords, match_categories, match_nth);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1636–1746why, one step back1636 }1637 }1638 }16391640 if (use_threads) {1641 for (size_t i = 0; i < matches->workers.count; i++)1642 tll_push_back(matches->workers.queue, (uint64_t)-1);16431644 mtx_unlock(&matches->workers.lock);16451646 for (size_t i = 0; i < matches->workers.count; i++)1647 sem_wait(&matches->workers.done);16481649 matches->workers.tok_count = 0;1650 matches->workers.tokens = NULL;1651 matches->workers.tok_lengths = NULL;1652 }16531654 LOG_DBG("match update done");16551656 /* Sort */1657 if (matches->sort_result) {1658 qsort(matches->matches, matches->match_count,1659 sizeof(matches->matches[0]), &match_compar);1660 }16611662 matches->page_count = matches->max_matches_per_page1663 ? ((matches->match_count + (matches->max_matches_per_page - 1)) /1664 matches->max_matches_per_page)1665 : 1;16661667 if (matches->selected >= matches->match_count && matches->selected > 0)1668 matches->selected = matches->match_count - 1;16691670 free(tok_lengths);1671 free(tokens);1672 free(copy);16731674unlock_and_return:1675 matches_unlock(matches);1676}16771678static bool1679arm_delayed_timer(struct matches *matches)1680{1681 struct itimerspec timeout = {1682 .it_value = {1683 .tv_sec = matches->delay_ms / 1000,1684 .tv_nsec = (matches->delay_ms % 1000) * 1000000,1685 },1686 };16871688 if (timerfd_settime(matches->delay_fd, 0, &timeout, NULL) < 0) {1689 LOG_ERRNO("failed to arm delayed filter timer");1690 return false;1691 }16921693 return true;1694}16951696void1697matches_update(struct matches *matches)1698{1699 if (matches->delay_ms > 0 &&1700 matches->applications->count > matches->delay_limit &&1701 matches->delayed_update_type != DELAYED_UPDATE_IN_PROGRESS)1702 {1703 matches->delayed_update_type = DELAYED_FULL_UPDATE;1704 if (arm_delayed_timer(matches))1705 return;1706 }17071708 matches_update_internal(matches, false);1709}17101711void1712matches_update_no_delay(struct matches *matches)1713{1714 matches_update_internal(matches, false);1715}17161717void1718matches_update_incremental(struct matches *matches)1719{1720 const bool need_full_update = matches->mode == MATCH_MODE_FUZZY;17211722 if (matches->delay_ms > 0 &&1723 matches->match_count > matches->delay_limit &&1724 matches->delayed_update_type != DELAYED_UPDATE_IN_PROGRESS)1725 {1726 switch (matches->delayed_update_type) {1727 case DELAYED_NO_UPDATE:1728 case DELAYED_INCREMENTAL_UPDATE:1729 matches->delayed_update_type = need_full_update1730 ? DELAYED_FULL_UPDATE : DELAYED_INCREMENTAL_UPDATE;1731 break;17321733 case DELAYED_FULL_UPDATE:1734 break;17351736 case DELAYED_UPDATE_IN_PROGRESS:1737 assert(false);1738 break;1739 }17401741 if (arm_delayed_timer(matches))1742 return;1743 }17441745 matches_update_internal(matches, !need_full_update);1746}