From e3115be103d9350596c0aec628c121e045b356e0 Mon Sep 17 00:00:00 2001 From: QC Date: Wed, 30 Sep 2015 20:10:52 +0200 Subject: [PATCH 1/7] Fix test. remove debug output --- source/textbox.c | 1 - 1 file changed, 1 deletion(-) diff --git a/source/textbox.c b/source/textbox.c index 8a66d87a..ff2c3edc 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -666,7 +666,6 @@ void textbox_setup ( Display *display ) void textbox_cleanup ( void ) { - printf ( "cleanup\n" ); if ( p_context ) { g_object_unref ( p_context ); p_context = NULL; From 5de38cd7558711ab5ca90452e85bed6f8a08f8e8 Mon Sep 17 00:00:00 2001 From: QC Date: Fri, 2 Oct 2015 20:39:57 +0200 Subject: [PATCH 2/7] Add glob version for testing. --- config/config.def.c | 3 ++- include/rofi.h | 1 + source/dialogs/dmenu.c | 8 +++++--- source/helper.c | 19 ++++++++++++++++++- source/xrmoptions.c | 1 + 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/config/config.def.c b/config/config.def.c index 1afcd483..d25be080 100644 --- a/config/config.def.c +++ b/config/config.def.c @@ -129,7 +129,8 @@ Settings config = { /** Modi to combine into one view. */ .combi_modi = "window,run", /** Fuzzy matching. */ - .fuzzy = FALSE, + .fuzzy = FALSE, + .glob = FALSE, /** Monitor */ .monitor = -1, /** set line margin */ diff --git a/include/rofi.h b/include/rofi.h index bf7ee333..dc28bf99 100644 --- a/include/rofi.h +++ b/include/rofi.h @@ -230,6 +230,7 @@ typedef struct _Settings char *combi_modi; /** Fuzzy match */ unsigned int fuzzy; + unsigned int glob; /** Monitors */ int monitor; /** Line margin */ diff --git a/source/dialogs/dmenu.c b/source/dialogs/dmenu.c index 1ef5eb2d..3be7a97d 100644 --- a/source/dialogs/dmenu.c +++ b/source/dialogs/dmenu.c @@ -59,8 +59,9 @@ typedef struct _DmenuModePrivateData static char **get_dmenu ( unsigned int *length ) { - char buffer[1024]; - char **retv = NULL; + GTimer *t = g_timer_new (); + char buffer[1024]; + char **retv = NULL; *length = 0; @@ -80,7 +81,8 @@ static char **get_dmenu ( unsigned int *length ) return retv; } } - + g_timer_stop ( t ); + printf ( "%.4f\n", g_timer_elapsed ( t, NULL ) ); return retv; } diff --git a/source/helper.c b/source/helper.c index 94b90a28..18270e9c 100644 --- a/source/helper.c +++ b/source/helper.c @@ -345,11 +345,28 @@ static int normal_token_match ( char **tokens, const char *input, int case_sensi g_free ( compk ); return match; } +static int glob_token_match ( char **tokens, const char *input, int case_sensitive ) +{ + int match = 1; + char *compk = token_collate_key ( input, case_sensitive ); + + // Do a tokenized match. + if ( tokens ) { + for ( int j = 0; match && tokens[j]; j++ ) { + match = g_pattern_match_simple ( tokens[j], compk ); + } + } + g_free ( compk ); + return match; +} int token_match ( char **tokens, const char *input, int case_sensitive, __attribute__( ( unused ) ) unsigned int index, __attribute__( ( unused ) ) Switcher *data ) { - if ( config.fuzzy ) { + if ( config.glob ) { + return glob_token_match ( tokens, input, case_sensitive ); + } + else if ( config.fuzzy ) { return fuzzy_token_match ( tokens, input, case_sensitive ); } return normal_token_match ( tokens, input, case_sensitive ); diff --git a/source/xrmoptions.c b/source/xrmoptions.c index ffd05115..9422c861 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -122,6 +122,7 @@ static XrmOption xrmOptions[] = { { xrm_Boolean, "parse-hosts", { .num = &config.parse_hosts }, NULL }, { xrm_String, "combi-modi", { .str = &config.combi_modi }, NULL }, { xrm_Boolean, "fuzzy", { .num = &config.fuzzy }, NULL }, + { xrm_Boolean, "glob", { .num = &config.glob }, NULL }, { xrm_Number, "monitor", { .snum = &config.monitor }, NULL }, /* Alias for dmenu compatibility. */ { xrm_SNumber, "m", { .snum = &config.monitor }, NULL }, From 1369c162fefbde596b7e3592d4b1f14c9a20ce49 Mon Sep 17 00:00:00 2001 From: QC Date: Fri, 2 Oct 2015 22:24:49 +0200 Subject: [PATCH 3/7] Remove debug --- source/dialogs/dmenu.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/source/dialogs/dmenu.c b/source/dialogs/dmenu.c index 3be7a97d..a44dbcf4 100644 --- a/source/dialogs/dmenu.c +++ b/source/dialogs/dmenu.c @@ -59,7 +59,6 @@ typedef struct _DmenuModePrivateData static char **get_dmenu ( unsigned int *length ) { - GTimer *t = g_timer_new (); char buffer[1024]; char **retv = NULL; @@ -81,8 +80,6 @@ static char **get_dmenu ( unsigned int *length ) return retv; } } - g_timer_stop ( t ); - printf ( "%.4f\n", g_timer_elapsed ( t, NULL ) ); return retv; } From 6b039ac524e4569533b153d7a9276a82583e0ffe Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Sun, 4 Oct 2015 16:37:07 +0200 Subject: [PATCH 4/7] Add tokenize option and append * to glob. --- config/config.def.c | 5 +++-- include/rofi.h | 1 + source/dialogs/dmenu.c | 4 ++-- source/helper.c | 26 +++++++++++++++++++++++--- source/xrmoptions.c | 1 + 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/config/config.def.c b/config/config.def.c index d25be080..d7d1edaa 100644 --- a/config/config.def.c +++ b/config/config.def.c @@ -129,8 +129,9 @@ Settings config = { /** Modi to combine into one view. */ .combi_modi = "window,run", /** Fuzzy matching. */ - .fuzzy = FALSE, - .glob = FALSE, + .fuzzy = FALSE, + .glob = FALSE, + .tokenize = FALSE, /** Monitor */ .monitor = -1, /** set line margin */ diff --git a/include/rofi.h b/include/rofi.h index dc28bf99..da6c5cf7 100644 --- a/include/rofi.h +++ b/include/rofi.h @@ -231,6 +231,7 @@ typedef struct _Settings /** Fuzzy match */ unsigned int fuzzy; unsigned int glob; + unsigned int tokenize; /** Monitors */ int monitor; /** Line margin */ diff --git a/source/dialogs/dmenu.c b/source/dialogs/dmenu.c index a44dbcf4..82e8dc62 100644 --- a/source/dialogs/dmenu.c +++ b/source/dialogs/dmenu.c @@ -59,8 +59,8 @@ typedef struct _DmenuModePrivateData static char **get_dmenu ( unsigned int *length ) { - char buffer[1024]; - char **retv = NULL; + char buffer[1024]; + char **retv = NULL; *length = 0; diff --git a/source/helper.c b/source/helper.c index 18270e9c..76055c12 100644 --- a/source/helper.c +++ b/source/helper.c @@ -176,8 +176,21 @@ char **tokenize ( const char *input, int case_sensitive ) char *saveptr = NULL, *token; char **retv = NULL; + if ( !config.tokenize ) { + retv = g_malloc0 ( sizeof ( char* ) * 2 ); + if ( config.glob ) { + token = g_strconcat ( input, "*", NULL ); + retv[0] = token_collate_key ( token, case_sensitive ); + g_free ( token ); token = NULL; + } + else{ + token = token_collate_key ( input, case_sensitive ); + } + return retv; + } + // First entry is always full (modified) stringtext. - int num_tokens = 0; + int num_tokens = 0; // Copy the string, 'strtok_r' modifies it. char *str = g_strdup ( input ); @@ -185,8 +198,15 @@ char **tokenize ( const char *input, int case_sensitive ) // Iterate over tokens. // strtok should still be valid for utf8. for ( token = strtok_r ( str, " ", &saveptr ); token != NULL; token = strtok_r ( NULL, " ", &saveptr ) ) { - retv = g_realloc ( retv, sizeof ( char* ) * ( num_tokens + 2 ) ); - retv[num_tokens] = token_collate_key ( token, case_sensitive ); + retv = g_realloc ( retv, sizeof ( char* ) * ( num_tokens + 2 ) ); + if ( config.glob ) { + char *t = token_collate_key ( token, case_sensitive ); + retv[num_tokens] = g_strconcat ( t, "*", NULL ); + g_free ( t ); + } + else { + retv[num_tokens] = token_collate_key ( token, case_sensitive ); + } retv[num_tokens + 1] = NULL; num_tokens++; } diff --git a/source/xrmoptions.c b/source/xrmoptions.c index 9422c861..1ea219ba 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -123,6 +123,7 @@ static XrmOption xrmOptions[] = { { xrm_String, "combi-modi", { .str = &config.combi_modi }, NULL }, { xrm_Boolean, "fuzzy", { .num = &config.fuzzy }, NULL }, { xrm_Boolean, "glob", { .num = &config.glob }, NULL }, + { xrm_Boolean, "tokenize", { .num = &config.tokenize }, NULL }, { xrm_Number, "monitor", { .snum = &config.monitor }, NULL }, /* Alias for dmenu compatibility. */ { xrm_SNumber, "m", { .snum = &config.monitor }, NULL }, From 9a39ebb3e97d2dae335015e32083477de69a1026 Mon Sep 17 00:00:00 2001 From: QC Date: Sun, 4 Oct 2015 17:02:54 +0200 Subject: [PATCH 5/7] Fix typo --- source/helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/helper.c b/source/helper.c index 76055c12..9e66da4b 100644 --- a/source/helper.c +++ b/source/helper.c @@ -184,7 +184,7 @@ char **tokenize ( const char *input, int case_sensitive ) g_free ( token ); token = NULL; } else{ - token = token_collate_key ( input, case_sensitive ); + retv[0] = token_collate_key ( input, case_sensitive ); } return retv; } From 8feb3c906772439954ecda483877030502123091 Mon Sep 17 00:00:00 2001 From: QC Date: Sun, 4 Oct 2015 17:03:45 +0200 Subject: [PATCH 6/7] Tokenize by default --- config/config.def.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.def.c b/config/config.def.c index d7d1edaa..e971737d 100644 --- a/config/config.def.c +++ b/config/config.def.c @@ -131,7 +131,7 @@ Settings config = { /** Fuzzy matching. */ .fuzzy = FALSE, .glob = FALSE, - .tokenize = FALSE, + .tokenize = TRUE, /** Monitor */ .monitor = -1, /** set line margin */ From 77cdc8d7c4aab103d4ac1252cfe1653e14c914a4 Mon Sep 17 00:00:00 2001 From: QC Date: Sun, 4 Oct 2015 20:14:08 +0200 Subject: [PATCH 7/7] Remove levenshtein --- config/config.def.c | 2 -- include/rofi.h | 2 -- source/rofi.c | 66 --------------------------------------------- source/xrmoptions.c | 1 - 4 files changed, 71 deletions(-) diff --git a/config/config.def.c b/config/config.def.c index e971737d..1bc122c0 100644 --- a/config/config.def.c +++ b/config/config.def.c @@ -110,8 +110,6 @@ Settings config = { .fixed_num_lines = FALSE, /** Do not use history */ .disable_history = FALSE, - /** Use levenshtein sorting when matching */ - .levenshtein_sort = FALSE, /** Case sensitivity of the search */ .case_sensitive = FALSE, /** Separator to use for dmenu mode */ diff --git a/include/rofi.h b/include/rofi.h index da6c5cf7..e6612a84 100644 --- a/include/rofi.h +++ b/include/rofi.h @@ -210,8 +210,6 @@ typedef struct _Settings unsigned int fixed_num_lines; /** Do not use history */ unsigned int disable_history; - /** Use levenshtein sorting when matching */ - unsigned int levenshtein_sort; /** Search case sensitivity */ unsigned int case_sensitive; /** Separator to use for dmenu mode */ diff --git a/source/rofi.c b/source/rofi.c index d22eb54c..b470e5c5 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -141,59 +141,6 @@ static inline MainLoopEvent wait_for_xevent_or_timeout ( Display *display, int x * Levenshtein Sorting. */ -static int lev_sort ( const void *p1, const void *p2, void *arg ) -{ - const int *a = p1; - const int *b = p2; - int *distances = arg; - - return distances[*a] - distances[*b]; -} - -static int dist ( const char *s, const char *t, int *d, int ls, int lt, int i, int j ) -{ - if ( d[i * ( lt + 1 ) + j] >= 0 ) { - return d[i * ( lt + 1 ) + j]; - } - - int x; - if ( i == ls ) { - x = lt - j; - } - else if ( j == lt ) { - x = ls - i; - } - else if ( s[i] == t[j] ) { - x = dist ( s, t, d, ls, lt, i + 1, j + 1 ); - } - else { - x = dist ( s, t, d, ls, lt, i + 1, j + 1 ); - - int y; - if ( ( y = dist ( s, t, d, ls, lt, i, j + 1 ) ) < x ) { - x = y; - } - if ( ( y = dist ( s, t, d, ls, lt, i + 1, j ) ) < x ) { - x = y; - } - x++; - } - return d[i * ( lt + 1 ) + j] = x; -} -static int levenshtein ( const char *s, const char *t ) -{ - int ls = strlen ( s ), lt = strlen ( t ); - size_t array_length = ( ls + 1 ) * ( lt + 1 ); - - // For some reason Coverity does not get that I initialize the - // array in for loop. - int d[array_length]; - for ( size_t i = 0; i < array_length; i++ ) { - d[i] = -1; - } - - return dist ( s, t, d, ls, lt, 0, 0 ); -} // State of the menu. @@ -224,7 +171,6 @@ typedef struct MenuState textbox *case_indicator; textbox **boxes; scrollbar *scrollbar; - int *distance; unsigned int *line_map; unsigned int num_lines; @@ -306,7 +252,6 @@ static void menu_free_state ( MenuState *state ) g_free ( state->boxes ); g_free ( state->line_map ); - g_free ( state->distance ); } /** @@ -729,15 +674,9 @@ static void menu_refilter ( MenuState *state ) // If each token was matched, add it to list. if ( match ) { state->line_map[j] = i; - if ( config.levenshtein_sort ) { - state->distance[i] = levenshtein ( state->text->text, state->lines[i] ); - } j++; } } - if ( config.levenshtein_sort ) { - g_qsort_with_data ( state->line_map, j, sizeof ( int ), lev_sort, state->distance ); - } // Cleanup + bookkeeping. state->filtered_lines = j; @@ -999,7 +938,6 @@ MenuReturn menu ( Switcher *sw, char **input, char *prompt, unsigned int *select .prev_key = 0, .last_button_press = 0, .last_offset = 0, - .distance = NULL, .quit = FALSE, .skip_absorb = FALSE, .filtered_lines = 0, @@ -1111,9 +1049,6 @@ MenuReturn menu ( Switcher *sw, char **input, char *prompt, unsigned int *select scrollbar_set_max_value ( state.scrollbar, state.num_lines ); // filtered list state.line_map = g_malloc0_n ( state.num_lines, sizeof ( unsigned int ) ); - if ( config.levenshtein_sort ) { - state.distance = (int *) g_malloc0_n ( state.num_lines, sizeof ( int ) ); - } // resize window vertically to suit // Subtract the margin of the last row. @@ -1406,7 +1341,6 @@ void error_dialog ( const char *msg, int markup ) .last_button_press = 0, .last_offset = 0, .num_lines = 0, - .distance = NULL, .quit = FALSE, .skip_absorb = FALSE, .filtered_lines = 0, diff --git a/source/xrmoptions.c b/source/xrmoptions.c index 1ea219ba..7e144de0 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -113,7 +113,6 @@ static XrmOption xrmOptions[] = { { xrm_String, "run-shell-command", { .str = &config.run_shell_command }, NULL }, { xrm_Boolean, "disable-history", { .num = &config.disable_history }, NULL }, - { xrm_Boolean, "levenshtein-sort", { .num = &config.levenshtein_sort }, NULL }, { xrm_Boolean, "case-sensitive", { .num = &config.case_sensitive }, NULL }, { xrm_Boolean, "sidebar-mode", { .num = &config.sidebar_mode }, NULL }, { xrm_Number, "lazy-filter-limit", { .num = &config.lazy_filter_limit }, NULL },