Skip to content
Myr

dmenu: add support for --match-nth

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Same format as --accept-nth and --with-nth. --match-nth defines what we match against in dmenu mode (i.e. which "columns"). The default is to match against the displayed column, either the entire input (--with-nth not used), or against the column(s) specified in --with-nth. Example: printf "123\t456\t789" | \ fuzzel --dmenu --with-nth=1 --accept-nth=2 --match-nth=3 * Displays "123" * Matches against "789" * Prints "456" on stdout when "executed" (selected) Closes #589

authored by Daniel Eklöf · committed by Daniel Eklöf · parent 945f85d · Browse files · 8 files+116 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+116 added−14 removed

  • other:+116 added−14 removed

8 files+116 added−14 removed

modified:application.c+1 added
344 unmodified lines
345 tll_free_and_free(app->keywords, free);346 tll_free_and_free(app->categories, free);347348 free(app->dmenu_input);349 free(app->dmenu_match_nth);350351 switch (app->icon.type) {352 case ICON_NONE:353 break;
49 unmodified lines
modified:application.h+2 added
83 unmodified lines
84 char32_list_t keywords; /* Lower cased! */85 char32_list_t categories; /* Lower cased! */8687 char32_t *dmenu_input; /* Full dmenu input, may contain multiple columns */88 char32_t *dmenu_match_nth; /* What to match against, with --match-nth= */8990 size_t title_len;91 size_t basename_len;92 size_t wexec_len;93 size_t generic_name_len;94 size_t comment_len;95 size_t dmenu_match_nth_len;96 /* keywords and categories lengths not cached */9798 struct icon icon;99 bool visible;
20 unmodified lines
modified:config.c+1 added
1780 unmodified lines
1781 free(conf->icon_theme);1782 free(conf->cache_path);1783 free(conf->dmenu.with_nth_format);1784 free(conf->dmenu.accept_nth_format);1785 free(conf->dmenu.match_nth_format);1786 free_key_binding_list(&conf->key_bindings);1787}17881789struct rgba
9 unmodified lines
modified:config.h+2 added-2 removed
41 unmodified lines
42 MATCH_EXEC = 0x08,43 MATCH_COMMENT = 0x10,44 MATCH_KEYWORDS = 0x20,45 MATCH_CATEGORIES = 0x40,4647 MATCH_END = MATCH_COMMENT + 1,46 MATCH_NTH = 0x80,47};4849enum anchors {50 ANCHOR_TOP_LEFT = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT,
116 unmodified lines
167 char delim;168 char nth_delim;169 char *with_nth_format;170 char *accept_nth_format;171 char *match_nth_format;172 } dmenu;173174 enum anchors anchor;175
63 unmodified lines
modified:dmenu.c+13 added-2 removed
23 unmodified lines
24#include "xmalloc.h"2526void27dmenu_load_entries(struct application_list *applications, char delim,28 const char *with_nth_format, char nth_delim,29 int event_fd, int abort_fd)28 const char *with_nth_format, const char *match_nth_format,29 char nth_delim, int event_fd, int abort_fd)30{31 tll(struct application *) entries = tll_init();3233 size_t size = 0;
141 unmodified lines
175 char32_t *lowercase = xc32dup(title);176 for (size_t i = 0; i < c32len(lowercase); i++)177 lowercase[i] = toc32lower(lowercase[i]);178179 char32_t *match_nth = match_nth_format != NULL180 ? nth_column(wline, nth_delim, match_nth_format)181 : NULL;182183 if (match_nth != NULL) {184 for (size_t i = 0; i < c32len(match_nth); i++)185 match_nth[i] = toc32lower(match_nth[i]);186 }187188 struct application *app = xmalloc(sizeof(*app));189 *app = (struct application){190 .index = app_idx++,191 .dmenu_input = wline,192 .dmenu_match_nth = match_nth,193 .dmenu_match_nth_len = match_nth != NULL ? c32len(match_nth) : 0,194 .title = title,195 .title_lowercase = lowercase,196 .title_len = c32len(lowercase),197 .icon = {.name = icon_name},
136 unmodified lines
modified:dmenu.h+2 added-2 removed
7 unmodified lines
8#include "prompt.h"910void dmenu_load_entries(11 struct application_list *applications, char delim,12 const char *with_nth_format, char nth_delim,13 int event_fd, int abort_fd);12 const char *with_nth_format, const char *match_nth_format,13 char nth_delim, int event_fd, int abort_fd);1415bool dmenu_execute(16 const struct application *app, ssize_t index,17 const struct prompt *prompt, enum dmenu_mode format,
5 unmodified lines
modified:main.c+34 added-3 removed
439 unmodified lines
440 " --with-nth=N|FMT display the N:th column (tab separated by\n"441 " default) of each input line (dmenu only)\n"442 " --accept-nth=N|FMT output the N:th column (tab separated by\n"443 " default) of each input line (dmenu only)\n"444 " --match-nth=N|FMT match against the N:th column (tab separated by\n"445 " default) of each input line, instead of what is\n"446 " being displayed (dmenu only)\n"447 " --nth-delimiter=CHARACTER field (column) character, for --with-nth and\n"448 " --accept-nth\n"449 " -R,--no-run-if-empty exit immediately without showing UI if stdin\n"450 " is empty (dmenu mode only)\n"
120 unmodified lines
571 bool icons_enabled = conf->icons_enabled;572 char dmenu_delim = conf->dmenu.delim;573 char dmenu_nth_delim = conf->dmenu.nth_delim;574 const char *dmenu_with_nth_format = conf->dmenu.with_nth_format;575 const char *dmenu_match_nth_format = conf->dmenu.match_nth_format;576 bool filter_desktop = conf->filter_desktop;577 bool list_exec_in_path = conf->list_executables_in_path;578 char_list_t desktops = tll_init();579 char *saveptr = NULL;
16 unmodified lines
596597 if (dmenu_enabled) {598 if (!conf->prompt_only) {599 dmenu_load_entries(596 apps, dmenu_delim, dmenu_with_nth_format, dmenu_nth_delim,597 ctx->event_fd, ctx->dmenu_abort_fd);600 apps, dmenu_delim, dmenu_with_nth_format, dmenu_match_nth_format,601 dmenu_nth_delim, ctx->event_fd, ctx->dmenu_abort_fd);602 read_cache(cache_path, apps, true);603 }604 } else {605 xdg_find_programs(terminal, actions_enabled, filter_desktop, &desktops, apps);
249 unmodified lines
855 #define OPT_AUTO_SELECT 305856 #define OPT_SELECTION_RADIUS 306857 #define OPT_NO_MOUSE 307858 #define OPT_DMENU_NTH_DELIM 308859 #define OPT_DMENU_MATCH_NTH 309860861 static const struct option longopts[] = {862 {"config", required_argument, 0, OPT_CONFIG},863 {"check-config", no_argument, 0, OPT_CHECK_CONFIG},
71 unmodified lines
935 {"index", no_argument, 0, OPT_DMENU_INDEX},936 {"nth-delimiter", required_argument, 0, OPT_DMENU_NTH_DELIM},937 {"with-nth", required_argument, 0, OPT_DMENU_WITH_NTH},938 {"accept-nth", required_argument, 0, OPT_DMENU_ACCEPT_NTH},939 {"match-nth", required_argument, 0, OPT_DMENU_MATCH_NTH},940941 /* Misc */942 {"log-level", required_argument, 0, OPT_LOG_LEVEL},943 {"log-colorize", optional_argument, 0, OPT_LOG_COLORIZE},
58 unmodified lines
1002 bool dmenu_delim_set:1;1003 bool dmenu_nth_delim_set:1;1004 bool dmenu_with_nth_set:1;1005 bool dmenu_accept_nth_set:1;1006 bool dmenu_match_nth_set:1;1007 bool layer_set:1;1008 bool keyboard_focus_set:1;1009 bool no_exit_on_keyboard_focus_loss_set:1;1010 bool render_workers_set:1;
763 unmodified lines
1774 cmdline_overrides.dmenu_accept_nth_set = true;1775 break;1776 }17771778 case OPT_DMENU_MATCH_NTH: {1779 free(cmdline_overrides.conf.dmenu.match_nth_format);1780 cmdline_overrides.conf.dmenu.match_nth_format = NULL;17811782 unsigned int match_nth_idx;1783 if (sscanf(optarg, "%u", &match_nth_idx) == 1) {1784 if (match_nth_idx == 0) {1785 /* Do nothing more - i.e. leave it unset */1786 } else {1787 cmdline_overrides.conf.dmenu.match_nth_format =1788 xasprintf("{%u}", match_nth_idx);1789 }1790 } else if (optarg[0] != '\0')1791 cmdline_overrides.conf.dmenu.match_nth_format = xstrdup(optarg);17921793 cmdline_overrides.dmenu_match_nth_set = true;1794 break;1795 }17961797 case OPT_LOG_LEVEL: {1798 int lvl = log_level_from_string(optarg);1799 if (lvl < 0) {1800 fprintf(
313 unmodified lines
2114 if (cmdline_overrides.dmenu_accept_nth_set) {2115 free(conf.dmenu.accept_nth_format);2116 conf.dmenu.accept_nth_format = cmdline_overrides.conf.dmenu.accept_nth_format;2117 }2118 if (cmdline_overrides.dmenu_match_nth_set) {2119 free(conf.dmenu.match_nth_format);2120 conf.dmenu.match_nth_format = cmdline_overrides.conf.dmenu.match_nth_format;2121 }2122 if (cmdline_overrides.conf.list_executables_in_path)2123 conf.list_executables_in_path = cmdline_overrides.conf.list_executables_in_path;2124 if (cmdline_overrides.render_workers_set)2125 conf.render_worker_count = cmdline_overrides.conf.render_worker_count;
21 unmodified lines
2147 conf.enable_mouse = cmdline_overrides.conf.enable_mouse;21482149 if (conf.dmenu.enabled) {2150 /* We don't have any meta data in dmenu mode */2121 conf.match_fields = MATCH_NAME;2151 conf.match_fields = conf.dmenu.match_nth_format != NULL2152 ? MATCH_NTH : MATCH_NAME;21532154 if (conf.prompt_only) {2155 conf.lines = 0;2156 conf.match_counter = false;
224 unmodified lines
modified:match.c+61 added-5 removed
843 unmodified lines
844 bool match_generic,845 bool match_exec,846 bool match_comment,847 bool match_keywords,848 bool match_categories)848 bool match_categories,849 bool match_nth)850{851 size_t pos_count = 0;852 struct match_substring *pos = NULL;853
1 unmodified line
855 enum matched_type match_type_filename = MATCHED_NONE;856 enum matched_type match_type_generic = MATCHED_NONE;857 enum matched_type match_type_exec = MATCHED_NONE;858 enum matched_type match_type_comment = MATCHED_NONE;859 enum matched_type match_type_nth = MATCHED_NONE;860 enum matched_type match_type_keywords[tll_length(app->keywords)];861 enum matched_type match_type_categories[tll_length(app->categories)];862863 if (match_keywords) {
66 unmodified lines
930 else if (match_type_name == MATCHED_EXACT)931 match_type_name = match_type;932 }933934 if (match_nth && app->dmenu_match_nth != NULL &&935 (t == 0 || match_type_nth != MATCHED_NONE))936 {937 const char32_t *m = NULL;938 size_t match_len = 0;939 enum matched_type match_type = MATCHED_NONE;940941 switch (matches->mode) {942 case MATCH_MODE_EXACT:943 m = match_exact(944 app->dmenu_match_nth, app->dmenu_match_nth_len, tok, tok_len);945946 if (m != NULL) {947 match_type = MATCHED_EXACT;948 match_len = tok_len;949 }950 break;951952 case MATCH_MODE_FZF:953 match_fzf(app->dmenu_match_nth, app->dmenu_match_nth_len,954 tok, tok_len, NULL, NULL, &match_type);955 break;956957 case MATCH_MODE_FUZZY:958 m = match_exact(959 app->dmenu_match_nth, app->dmenu_match_nth_len, tok, tok_len);960961 if (m != NULL) {962 match_type = MATCHED_EXACT;963 match_len = tok_len;964 } else {965 m = match_levenshtein(966 matches, app->dmenu_match_nth, app->dmenu_match_nth_len,967 tok, tok_len, &match_len);968 if (m != NULL)969 match_type = MATCHED_FUZZY;970 }971 break;972 }973974 if (t == 0)975 match_type_nth = match_type;976 else if (match_type == MATCHED_NONE)977 match_type_nth = MATCHED_NONE;978 else if (match_type_nth == MATCHED_EXACT)979 match_type_nth = match_type;980 }981982 if (match_filename && app->basename != NULL &&983 (t == 0 || match_type_filename != MATCHED_NONE))984 {985 const char32_t *m = NULL;
333 unmodified lines
1319 else if (match_type_keywords_final != MATCHED_NONE)1320 app_match_type = match_type_keywords_final;1321 else if (match_type_categories_final != MATCHED_NONE)1322 app_match_type = match_type_categories_final;1323 else if (match_type_nth != MATCHED_NONE)1324 app_match_type = match_type_nth;13251326 if (app_match_type == MATCHED_NONE) {1327 free(pos);1328 return;
43 unmodified lines
1372 const bool match_exec = fields & MATCH_EXEC;1373 const bool match_comment = fields & MATCH_COMMENT;1374 const bool match_keywords = fields & MATCH_KEYWORDS;1375 const bool match_categories = fields & MATCH_CATEGORIES;1376 const bool match_nth = fields & MATCH_NTH;13771378 sem_t *start = &matches->workers.start;1379 sem_t *done = &matches->workers.done;1380 mtx_t *lock = &matches->workers.lock;
48 unmodified lines
1429 }14301431 match_app(matches, app, tok_count, tokens, tok_lengths,1432 match_name, match_filename, match_generic, match_exec,1380 match_comment, match_keywords, match_categories);1433 match_comment, match_keywords, match_categories,1434 match_nth);1435 }1436 }1437 }1438 }
52 unmodified lines
1491 const bool match_exec = fields & MATCH_EXEC;1492 const bool match_comment = fields & MATCH_COMMENT;1493 const bool match_keywords = fields & MATCH_KEYWORDS;1494 const bool match_categories = fields & MATCH_CATEGORIES;1495 const bool match_nth = fields & MATCH_NTH;14961497 LOG_DBG(1498 "matching: filename=%s, name=%s, generic=%s, exec=%s, categories=%s, "1444 "keywords=%s, comment=%s",1499 "keywords=%s, comment=%s, nth=%d",1500 match_filename ? "yes" : "no",1501 match_name ? "yes" : "no",1502 match_generic ? "yes" : "no",1503 match_exec ? "yes" : "no",1504 match_categories ? "yes" : "no",1505 match_keywords ? "yes" : "no",1451 match_comment ? "yes" : "no");1506 match_comment ? "yes" : "no",1507 match_nth ? "yes" : "no");15081509 LOG_DBG("match update begin");15101511 char32_t *copy = xc32dup(ptext);
94 unmodified lines
16061607 match_app(matches, app,1608 tok_count, (const char32_t *const *)tokens, tok_lengths,1609 match_name, match_filename, match_generic, match_exec,1554 match_comment, match_keywords, match_categories);1610 match_comment, match_keywords, match_categories, match_nth);1611 }1612 }1613 }1614
107 unmodified lines