@@ -79,32 +79,17 @@ tokenize(struct bm_menu *menu, char ***out_tokv, uint32_t *out_tokc)
7979 return NULL ;
8080}
8181
82- struct fuzzy_match {
83- struct bm_item * item ;
84- int distance ;
85- };
86-
87- static int fuzzy_match_comparator (const void * a , const void * b ) {
88- const struct fuzzy_match * fa = (const struct fuzzy_match * )a ;
89- const struct fuzzy_match * fb = (const struct fuzzy_match * )b ;
90-
91- return fa -> distance - fb -> distance ;
92- }
93-
94-
9582/**
96- * Dmenu filterer that accepts substring function or fuzzy match .
83+ * Dmenu filterer that accepts substring function.
9784 *
9885 * @param menu bm_menu instance to filter.
9986 * @param addition This will be 1, if filter is same as previous filter with something appended.
10087 * @param fstrstr Substring function used to match items.
101- * @param fstrncmp String comparison function for exact matches.
10288 * @param out_nmemb uint32_t reference to filtered items count.
103- * @param fuzzy Boolean flag to toggle fuzzy matching.
10489 * @return Pointer to array of bm_item pointers.
10590 */
10691static struct bm_item * *
107- filter_dmenu_fun (struct bm_menu * menu , char addition , char * (* fstrstr )(const char * a , const char * b ), int (* fstrncmp )(const char * a , const char * b , size_t len ), uint32_t * out_nmemb , bool fuzzy )
92+ filter_dmenu_fun (struct bm_menu * menu , char addition , char * (* fstrstr )(const char * a , const char * b ), int (* fstrncmp )(const char * a , const char * b , size_t len ), uint32_t * out_nmemb )
10893{
10994 assert (menu && fstrstr && fstrncmp && out_nmemb );
11095 * out_nmemb = 0 ;
@@ -129,48 +114,13 @@ filter_dmenu_fun(struct bm_menu *menu, char addition, char* (*fstrstr)(const cha
129114 goto fail ;
130115
131116 const char * filter = menu -> filter ? menu -> filter : "" ;
132- if (strlen (filter ) == 0 ) {
133- goto fail ;
134- }
135117 size_t len = (tokc ? strlen (tokv [0 ]) : 0 );
136118 uint32_t i , f , e ;
137- f = e = 0 ;
138-
139- struct fuzzy_match * fuzzy_matches = NULL ;
140-
141- int fuzzy_match_count = 0 ;
142-
143- if (fuzzy && !(fuzzy_matches = calloc (count , sizeof (* fuzzy_matches ))))
144- goto fail ;
145-
146- for (i = 0 ; i < count ; ++ i ) {
119+ for (e = f = i = 0 ; i < count ; ++ i ) {
147120 struct bm_item * item = items [i ];
148121 if (!item -> text && tokc != 0 )
149122 continue ;
150123
151- if (fuzzy && tokc && item -> text ) {
152- const char * text = item -> text ;
153- int sidx = -1 , eidx = -1 , pidx = 0 , text_len = strlen (text ), distance = 0 ;
154- for (int j = 0 ; j < text_len && text [j ]; ++ j ) {
155- if (!fstrncmp (& text [j ], & filter [pidx ], 1 )) {
156- if (sidx == -1 )
157- sidx = j ;
158- pidx ++ ;
159- if (pidx == strlen (filter )) {
160- eidx = j ;
161- break ;
162- }
163- }
164- }
165- if (eidx != -1 ) {
166- distance = eidx - sidx + (text_len - eidx + sidx ) / 3 ;
167- fuzzy_matches [fuzzy_match_count ++ ] = (struct fuzzy_match ){ item , distance };
168- continue ;
169- }
170- }
171-
172- if (fuzzy ) continue ;
173-
174124 if (tokc && item -> text ) {
175125 uint32_t t ;
176126 for (t = 0 ; t < tokc && fstrstr (item -> text , tokv [t ]); ++ t );
@@ -192,23 +142,12 @@ filter_dmenu_fun(struct bm_menu *menu, char addition, char* (*fstrstr)(const cha
192142 f ++ ; /* where do all matches end */
193143 }
194144
195- if (fuzzy && fuzzy_match_count > 0 ) {
196- qsort (fuzzy_matches , fuzzy_match_count , sizeof (struct fuzzy_match ), fuzzy_match_comparator );
197-
198- for (int j = 0 ; j < fuzzy_match_count ; ++ j ) {
199- filtered [f ++ ] = fuzzy_matches [j ].item ;
200- }
201-
202- free (fuzzy_matches );
203- }
204-
205145 free (buffer );
206146 free (tokv );
207147 return shrink_list (& filtered , menu -> items .count , (* out_nmemb = f ));
208148
209149fail :
210150 free (filtered );
211- free (fuzzy_matches );
212151 free (buffer );
213152 return NULL ;
214153}
@@ -224,7 +163,7 @@ filter_dmenu_fun(struct bm_menu *menu, char addition, char* (*fstrstr)(const cha
224163struct bm_item * *
225164bm_filter_dmenu (struct bm_menu * menu , bool addition , uint32_t * out_nmemb )
226165{
227- return filter_dmenu_fun (menu , addition , strstr , strncmp , out_nmemb , menu -> fuzzy );
166+ return filter_dmenu_fun (menu , addition , strstr , strncmp , out_nmemb );
228167}
229168
230169/**
@@ -238,7 +177,7 @@ bm_filter_dmenu(struct bm_menu *menu, bool addition, uint32_t *out_nmemb)
238177struct bm_item * *
239178bm_filter_dmenu_case_insensitive (struct bm_menu * menu , bool addition , uint32_t * out_nmemb )
240179{
241- return filter_dmenu_fun (menu , addition , bm_strupstr , bm_strnupcmp , out_nmemb , menu -> fuzzy );
180+ return filter_dmenu_fun (menu , addition , bm_strupstr , bm_strnupcmp , out_nmemb );
242181}
243182
244183/* vim: set ts=8 sw=4 tw=0 :*/
0 commit comments