> I don't know about you guys, but:
> 
> 1) I can recall last octet of an IP address more often than the first,
> 2) I have a lot of machines in the same /24 subnet.

Why not use globbing?

I find search_match_text redundant since search_match_exec has support for
globbing. You can do "*x*" to get the same behaviour as search_match_text "x".
With search_match_exec you can also list all alternatives ("*").

Furthermore when it comes to searching in the menus in cwm, why not do case
sensitive matching? Every user is used to the fact that case matters everywhere
else. It also allows for better filtering.

I suggest the following diff.

Index: calmwm.h
===================================================================
RCS file: /cvs/openbsd/xenocara/app/cwm/calmwm.h,v
retrieving revision 1.152
diff -u -p -r1.152 calmwm.h
--- calmwm.h    13 Jul 2012 17:01:04 -0000      1.152
+++ calmwm.h    26 Aug 2012 11:58:30 -0000
@@ -356,8 +356,6 @@ void                         search_match_client(struct 
menu_
                             char *);
 void                    search_match_exec(struct menu_q *, struct menu_q *,
                             char *);
-void                    search_match_text(struct menu_q *, struct menu_q *,
-                            char *);
 void                    search_print_client(struct menu *, int);
 
 XineramaScreenInfo     *screen_find_xinerama(struct screen_ctx *, int, int);
Index: conf.c
===================================================================
RCS file: /cvs/openbsd/xenocara/app/cwm/conf.c,v
retrieving revision 1.99
diff -u -p -r1.99 conf.c
--- conf.c      13 May 2012 15:15:54 -0000      1.99
+++ conf.c      26 Aug 2012 10:18:01 -0000
@@ -297,7 +297,7 @@ conf_client(struct client_ctx *cc)
        int              ignore = 0;
 
        TAILQ_FOREACH(wm, &Conf.ignoreq, entry) {
-               if (strncasecmp(wm->title, wname, strlen(wm->title)) == 0) {
+               if (strncmp(wm->title, wname, strlen(wm->title)) == 0) {
                        ignore = 1;
                        break;
                }
Index: kbfunc.c
===================================================================
RCS file: /cvs/openbsd/xenocara/app/cwm/kbfunc.c,v
retrieving revision 1.62
diff -u -p -r1.62 kbfunc.c
--- kbfunc.c    13 Jul 2012 17:01:04 -0000      1.62
+++ kbfunc.c    26 Aug 2012 11:47:12 -0000
@@ -190,7 +190,7 @@ kbfunc_menu_search(struct client_ctx *cc
        }
 
        if ((mi = menu_filter(sc, &menuq, "application", NULL, 0,
-           search_match_text, NULL)) != NULL)
+           search_match_exec, NULL)) != NULL)
                u_spawn(((struct cmd *)mi->ctx)->image);
 
        while ((mi = TAILQ_FIRST(&menuq)) != NULL) {
@@ -257,7 +257,7 @@ kbfunc_exec(struct client_ctx *cc, union
                        break;
                default:
                        err(1, "kbfunc_exec: invalid cmd %d", cmd);
-                       /*NOTREACHED*/
+                       /* NOTREACHED */
        }
 
        TAILQ_INIT(&menuq);
@@ -311,7 +311,7 @@ kbfunc_exec(struct client_ctx *cc, union
                                break;
                        default:
                                err(1, "kb_func: egad, cmd changed value!");
-                               break;
+                               /* NOTREACHED */
                }
        }
 out:
@@ -403,7 +403,7 @@ kbfunc_client_label(struct client_ctx *c
 
        /* dummy is set, so this will always return */
        mi = menu_filter(cc->sc, &menuq, "label", cc->label, 1,
-           search_match_text, NULL);
+           search_match_exec, NULL);
 
        if (!mi->abort) {
                if (cc->label != NULL)
Index: menu.c
===================================================================
RCS file: /cvs/openbsd/xenocara/app/cwm/menu.c,v
retrieving revision 1.36
diff -u -p -r1.36 menu.c
--- menu.c      7 Aug 2012 14:05:49 -0000       1.36
+++ menu.c      26 Aug 2012 10:56:59 -0000
@@ -304,7 +304,7 @@ menu_draw(struct screen_ctx *sc, struct 
        int                      n, dy, xsave, ysave;
 
        if (mc->list) {
-               if (TAILQ_EMPTY(resultq) && mc->list) {
+               if (TAILQ_EMPTY(resultq)) {
                        /* Copy them all over. */
                        TAILQ_FOREACH(mi, menuq, entry)
                                TAILQ_INSERT_TAIL(resultq, mi,
Index: search.c
===================================================================
RCS file: /cvs/openbsd/xenocara/app/cwm/search.c,v
retrieving revision 1.24
diff -u -p -r1.24 search.c
--- search.c    25 Jul 2011 15:10:24 -0000      1.24
+++ search.c    26 Aug 2012 11:43:54 -0000
@@ -32,8 +32,6 @@
 
 #include "calmwm.h"
 
-static int     strsubmatch(char *, char *, int);
-
 /*
  * Match: label, title, class.
  */
@@ -43,6 +41,11 @@ search_match_client(struct menu_q *menuq
 {
        struct winname  *wn;
        struct menu     *mi, *tierp[4], *before = NULL;
+       size_t          slen;
+
+       if (search == NULL)
+               return;
+       slen = strlen(search);
 
        TAILQ_INIT(resultq);
 
@@ -61,7 +64,8 @@ search_match_client(struct menu_q *menuq
                struct client_ctx *cc = mi->ctx;
 
                /* First, try to match on labels. */
-               if (cc->label != NULL && strsubmatch(search, cc->label, 0)) {
+               if (cc->label != NULL && (strncmp(search, cc->label, slen) == 0
+                   || fnmatch(search, mi->text, 0) == 0)) {
                        cc->matchname = cc->label;
                        tier = 0;
                }
@@ -69,7 +73,8 @@ search_match_client(struct menu_q *menuq
                /* Then, on window names. */
                if (tier < 0) {
                        TAILQ_FOREACH_REVERSE(wn, &cc->nameq, winname_q, entry)
-                               if (strsubmatch(search, wn->name, 0)) {
+                               if (strncmp(search, wn->name, slen) == 0 ||
+                                   fnmatch(search, wn->name, 0) == 0) {
                                        cc->matchname = wn->name;
                                        tier = 2;
                                        break;
@@ -77,7 +82,9 @@ search_match_client(struct menu_q *menuq
                }
 
                /* Then if there is a match on the window class name. */
-               if (tier < 0 && strsubmatch(search, cc->app_class, 0)) {
+               if (tier < 0 && cc->app_class != NULL && (strncmp(search,
+                   cc->app_class, slen) == 0 || fnmatch(search, cc->app_class,
+                   0) == 0)) {
                        cc->matchname = cc->app_class;
                        tier = 3;
                }
@@ -157,36 +164,28 @@ search_print_client(struct menu *mi, int
 
                (void)strlcpy(buf, mi->print, sizeof(buf));
                (void)snprintf(mi->print, sizeof(mi->print),
-                   "%s:%.*s%s", buf, diff, cc->name, marker);
+                   strlen(cc->matchname) > 0 ? "%s:%.*s%s" : "%s%.*s%s", buf,
+                   diff, cc->name, marker);
        }
 }
 
 void
-search_match_text(struct menu_q *menuq, struct menu_q *resultq, char *search)
-{
-       struct menu     *mi;
-
-       TAILQ_INIT(resultq);
-
-       TAILQ_FOREACH(mi, menuq, entry)
-               if (strsubmatch(search, mi->text, 0))
-                       TAILQ_INSERT_TAIL(resultq, mi, resultentry);
-}
-
-void
 search_match_exec(struct menu_q *menuq, struct menu_q *resultq, char *search)
 {
        struct menu     *mi, *mj;
 
+       if (search == NULL)
+               return;
+
        TAILQ_INIT(resultq);
 
        TAILQ_FOREACH(mi, menuq, entry) {
-               if (strsubmatch(search, mi->text, 1) == 0 &&
+               if (strncmp(search, mi->text, strlen(search)) != 0 &&
                    fnmatch(search, mi->text, 0) == FNM_NOMATCH)
                                continue;
                for (mj = TAILQ_FIRST(resultq); mj != NULL;
                     mj = TAILQ_NEXT(mj, resultentry)) {
-                       if (strcasecmp(mi->text, mj->text) < 0) {
+                       if (strcmp(mi->text, mj->text) < 0) {
                                TAILQ_INSERT_BEFORE(mj, mi, resultentry);
                                break;
                        }
@@ -194,31 +193,4 @@ search_match_exec(struct menu_q *menuq, 
                if (mj == NULL)
                        TAILQ_INSERT_TAIL(resultq, mi, resultentry);
        }
-}
-
-static int
-strsubmatch(char *sub, char *str, int zeroidx)
-{
-       size_t   len, sublen;
-       u_int    n, flen;
-
-       if (sub == NULL || str == NULL)
-               return (0);
-
-       len = strlen(str);
-       sublen = strlen(sub);
-
-       if (sublen > len)
-               return (0);
-
-       if (!zeroidx)
-               flen = len - sublen;
-       else
-               flen = 0;
-
-       for (n = 0; n <= flen; n++)
-               if (strncasecmp(sub, str + n, sublen) == 0)
-                       return (1);
-
-       return (0);
 }

Reply via email to