From c3e70d4e1abd3d0b75a6c0dfeca56179b18b77cf Mon Sep 17 00:00:00 2001 From: unisgn Date: Sat, 29 May 2021 19:39:31 +0800 Subject: [PATCH] add prefix matching method feature (#1237) * add prefix matching method feature * Update helper.c * prefix matching regex memory leak fix * prefix matching regex memory leak fix Co-authored-by: francis --- doc/rofi.1 | 2 ++ doc/rofi.1.markdown | 3 ++- doc/test_xr.txt | 26 +++++++++++++------------- include/settings.h | 3 ++- source/helper.c | 18 +++++++++++++++++- source/xrmoptions.c | 2 +- 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/doc/rofi.1 b/doc/rofi.1 index f1747749..6e6b5675 100644 --- a/doc/rofi.1 +++ b/doc/rofi.1 @@ -410,6 +410,8 @@ Current the following methods are supported. \fBglob\fP: match a glob pattern .IP \(bu 2 \fBfuzzy\fP: do a fuzzy match +.IP \(bu 2 +\fBprefix\fP: match prefix .RE diff --git a/doc/rofi.1.markdown b/doc/rofi.1.markdown index 3a16360e..56aabb04 100644 --- a/doc/rofi.1.markdown +++ b/doc/rofi.1.markdown @@ -197,7 +197,7 @@ Select the scrolling method. 0: Per page, 1: continuous. Normalize the string before matching, so o will match ö, and é matches e. This is not a perfect implementation, but works. -For now it disabled highlighting of the matched part. +For now it disabled highlighting of the matched part. `-no-lazy-grab` @@ -244,6 +244,7 @@ Current the following methods are supported. * **regex**: match a regex input * **glob**: match a glob pattern * **fuzzy**: do a fuzzy match +* **prefix**: match prefix Default: *normal* diff --git a/doc/test_xr.txt b/doc/test_xr.txt index 7674f5f3..e11806e3 100644 --- a/doc/test_xr.txt +++ b/doc/test_xr.txt @@ -31,7 +31,7 @@ rofi.ssh-command: {terminal} -e {ssh-client} {host} ! "Run command to execute" Set from: File rofi.run-command: bash -c "{cmd}" ! "Command to get extra run targets" Set from: Default -! rofi.run-list-command: +! rofi.run-list-command: ! "Run command to execute that runs in shell" Set from: File rofi.run-shell-command: {terminal} -e {cmd} ! "Command executed on accep-entry-custom for window modus" Set from: File @@ -39,11 +39,11 @@ rofi.window-command: xkill -id {window} ! "Window fields to match in window mode" Set from: Default ! rofi.window-match-fields: all ! "Theme to use to look for icons" Set from: Default -! rofi.icon-theme: +! rofi.icon-theme: ! "Desktop entry fields to match in drun" Set from: Default ! rofi.drun-match-fields: name,generic,exec,categories,keywords ! "Only show Desktop entry from these categories" Set from: Default -! rofi.drun-categories: +! rofi.drun-categories: ! "Desktop entry show actions." Set from: Default ! rofi.drun-show-actions: false ! "DRUN format string. (Supports: generic,name,comment,exec,categories)" Set from: Default @@ -51,11 +51,11 @@ rofi.window-command: xkill -id {window} ! "Disable history in run/ssh" Set from: File rofi.disable-history: false ! "Programs ignored for history" Set from: Default -! rofi.ignored-prefixes: +! rofi.ignored-prefixes: ! "Use sorting" Set from: Default ! rofi.sort: false ! "Choose the strategy used for sorting: normal (levenshtein) or fzf." Set from: Default -! rofi.sorting-method: +! rofi.sorting-method: ! "Set case-sensitivity" Set from: File rofi.case-sensitive: false ! "Cycle through the results list" Set from: File @@ -72,7 +72,7 @@ rofi.parse-hosts: false rofi.parse-known-hosts: true ! "Set the modi to combine in combi mode" Set from: File rofi.combi-modi: window,drun,run,ssh -! "Set the matching algorithm. (normal, regex, glob, fuzzy)" Set from: Default +! "Set the matching algorithm. (normal, regex, glob, fuzzy, prefix)" Set from: Default ! rofi.matching: normal ! "Tokenize input string" Set from: File rofi.tokenize: true @@ -83,7 +83,7 @@ rofi.m: -1 ! "Padding within rows *DEPRECATED*" Set from: Default ! rofi.line-padding: 1 ! "Pre-set filter" Set from: Default -! rofi.filter: +! rofi.filter: ! "Separator style (none, dash, solid) *DEPRECATED*" Set from: Default ! rofi.separator-style: dash ! "Hide scroll-bar *DEPRECATED*" Set from: Default @@ -107,15 +107,15 @@ rofi.scroll-method: 0 ! "Click outside the window to exit" Set from: Default ! rofi.click-to-exit: true ! "New style theme file" Set from: Default -! rofi.theme: +! rofi.theme: ! "Color scheme for normal row" Set from: Default -! rofi.color-normal: +! rofi.color-normal: ! "Color scheme for urgent row" Set from: Default -! rofi.color-urgent: +! rofi.color-urgent: ! "Color scheme for active row" Set from: Default -! rofi.color-active: +! rofi.color-active: ! "Color scheme window" Set from: Default -! rofi.color-window: +! rofi.color-window: ! "Max history size (WARNING: can cause slowdowns when set to high)." Set from: Default ! rofi.max-history-size: 25 ! "Hide the prefix mode prefix on the combi view." Set from: Default @@ -123,7 +123,7 @@ rofi.scroll-method: 0 ! "Set the character used to negate the matching. ('\0' to disable)" Set from: Default ! rofi.matching-negate-char: - ! "Directory where history and temporary files are stored." Set from: Default -! rofi.cache-dir: +! rofi.cache-dir: ! "Show window thumbnail in window switcher if availalbe." Set from: Default ! rofi.window-thumbnail: false ! "Pidfile location" Set from: File diff --git a/include/settings.h b/include/settings.h index 4952eb02..ba13d445 100644 --- a/include/settings.h +++ b/include/settings.h @@ -40,7 +40,8 @@ typedef enum MM_NORMAL = 0, MM_REGEX = 1, MM_GLOB = 2, - MM_FUZZY = 3 + MM_FUZZY = 3, + MM_PREFIX = 4 } MatchingMethod; /** diff --git a/source/helper.c b/source/helper.c index 6061e064..874ceea2 100644 --- a/source/helper.c +++ b/source/helper.c @@ -179,6 +179,14 @@ static gchar *fuzzy_to_regex ( const char * input ) return retv; } +static gchar *prefix_regex (const char * input) +{ + gchar *r = g_regex_escape_string(input, -1); + char *retv = g_strconcat("\\b", r, NULL); + g_free(r); + return retv; +} + static char *utf8_helper_simplify_string ( const char *s ) { gunichar buf2[G_UNICHAR_MAX_DECOMPOSITION_LENGTH] = { 0, }; @@ -248,6 +256,11 @@ static rofi_int_matcher * create_regex ( const char *input, int case_sensitive ) retv = R ( r, case_sensitive ); g_free ( r ); break; + case MM_PREFIX: + r = prefix_regex(input); + retv = R(r, case_sensitive); + g_free(r); + break; default: r = g_regex_escape_string ( input, -1 ); retv = R ( r, case_sensitive ); @@ -632,8 +645,11 @@ int config_sanity_check ( void ) else if ( g_strcmp0 ( config.matching, "normal" ) == 0 ) { config.matching_method = MM_NORMAL;; } + else if (g_strcmp0 (config.matching, "prefix") == 0) { + config.matching_method = MM_PREFIX; + } else { - g_string_append_printf ( msg, "\tconfig.matching=%s is not a valid matching strategy.\nValid options are: glob, regex, fuzzy or normal.\n", + g_string_append_printf ( msg, "\tconfig.matching=%s is not a valid matching strategy.\nValid options are: glob, regex, fuzzy, prefix or normal.\n", config.matching ); found_error = 1; } diff --git a/source/xrmoptions.c b/source/xrmoptions.c index 344f3b89..b597decc 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -169,7 +169,7 @@ static XrmOption xrmOptions[] = { { xrm_String, "combi-modi", { .str = &config.combi_modi }, NULL, "Set the modi to combine in combi mode", CONFIG_DEFAULT }, { xrm_String, "matching", { .str = &config.matching }, NULL, - "Set the matching algorithm. (normal, regex, glob, fuzzy)", CONFIG_DEFAULT }, + "Set the matching algorithm. (normal, regex, glob, fuzzy, prefix)", CONFIG_DEFAULT }, { xrm_Boolean, "tokenize", { .num = &config.tokenize }, NULL, "Tokenize input string", CONFIG_DEFAULT }, { xrm_String, "monitor", { .str = &config.monitor }, NULL,