dmenu-highpriority-5.1.diff - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       dmenu-highpriority-5.1.diff (5665B)
       ---
            1 diff --git a/config.def.h b/config.def.h
            2 index 1edb647..47b616d 100644
            3 --- a/config.def.h
            4 +++ b/config.def.h
            5 @@ -12,6 +12,7 @@ static const char *colors[SchemeLast][2] = {
            6          [SchemeNorm] = { "#bbbbbb", "#222222" },
            7          [SchemeSel] = { "#eeeeee", "#005577" },
            8          [SchemeOut] = { "#000000", "#00ffff" },
            9 +        [SchemeHp]  = { "#bbbbbb", "#333333" }
           10  };
           11  /* -l option; if nonzero, dmenu uses vertical list with given number of lines */
           12  static unsigned int lines      = 0;
           13 diff --git a/dmenu.c b/dmenu.c
           14 index eca67ac..6ae896a 100644
           15 --- a/dmenu.c
           16 +++ b/dmenu.c
           17 @@ -26,14 +26,16 @@
           18  #define TEXTW(X)              (drw_fontset_getwidth(drw, (X)) + lrpad)
           19  
           20  /* enums */
           21 -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
           22 +enum { SchemeNorm, SchemeSel, SchemeHp, SchemeOut, SchemeLast }; /* color schemes */
           23  
           24  struct item {
           25          char *text;
           26          struct item *left, *right;
           27 -        int out;
           28 +        int out, hp;
           29  };
           30  
           31 +static char **hpitems = NULL;
           32 +static int hplength = 0;
           33  static char text[BUFSIZ] = "";
           34  static char *embed;
           35  static int bh, mw, mh;
           36 @@ -58,6 +60,42 @@ static Clr *scheme[SchemeLast];
           37  static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
           38  static char *(*fstrstr)(const char *, const char *) = strstr;
           39  
           40 +static char **
           41 +tokenize(char *source, const char *delim, int *llen)
           42 +{
           43 +        int listlength = 0, list_size = 0;
           44 +        char **list = NULL, *token;
           45 +
           46 +        token = strtok(source, delim);
           47 +        while (token) {
           48 +                if (listlength + 1 >= list_size) {
           49 +                        if (!(list = realloc(list, (list_size += 8) * sizeof(*list))))
           50 +                                die("Unable to realloc %zu bytes\n", list_size * sizeof(*list));
           51 +                }
           52 +                if (!(list[listlength] = strdup(token)))
           53 +                        die("Unable to strdup %zu bytes\n", strlen(token) + 1);
           54 +                token = strtok(NULL, delim);
           55 +                listlength++;
           56 +        }
           57 +
           58 +        *llen = listlength;
           59 +        return list;
           60 +}
           61 +
           62 +static int
           63 +arrayhas(char **list, int length, char *item)
           64 +{
           65 +        int i;
           66 +
           67 +        for (i = 0; i < length; i++) {
           68 +                size_t len1 = strlen(list[i]);
           69 +                size_t len2 = strlen(item);
           70 +                if (!fstrncmp(list[i], item, len1 > len2 ? len2 : len1))
           71 +                        return 1;
           72 +        }
           73 +        return 0;
           74 +}
           75 +
           76  static void
           77  appenditem(struct item *item, struct item **list, struct item **last)
           78  {
           79 @@ -97,6 +135,9 @@ cleanup(void)
           80          XUngrabKey(dpy, AnyKey, AnyModifier, root);
           81          for (i = 0; i < SchemeLast; i++)
           82                  free(scheme[i]);
           83 +        for (i = 0; i < hplength; ++i)
           84 +                free(hpitems[i]);
           85 +        free(hpitems);
           86          drw_free(drw);
           87          XSync(dpy, False);
           88          XCloseDisplay(dpy);
           89 @@ -125,6 +166,8 @@ drawitem(struct item *item, int x, int y, int w)
           90  {
           91          if (item == sel)
           92                  drw_setscheme(drw, scheme[SchemeSel]);
           93 +        else if (item->hp)
           94 +                drw_setscheme(drw, scheme[SchemeHp]);
           95          else if (item->out)
           96                  drw_setscheme(drw, scheme[SchemeOut]);
           97          else
           98 @@ -226,7 +269,7 @@ match(void)
           99          char buf[sizeof text], *s;
          100          int i, tokc = 0;
          101          size_t len, textsize;
          102 -        struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
          103 +        struct item *item, *lhpprefix, *lprefix, *lsubstr, *hpprefixend, *prefixend, *substrend;
          104  
          105          strcpy(buf, text);
          106          /* separate input text into tokens to be matched individually */
          107 @@ -235,7 +278,7 @@ match(void)
          108                          die("cannot realloc %u bytes:", tokn * sizeof *tokv);
          109          len = tokc ? strlen(tokv[0]) : 0;
          110  
          111 -        matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
          112 +        matches = lhpprefix = lprefix = lsubstr = matchend = hpprefixend = prefixend = substrend = NULL;
          113          textsize = strlen(text) + 1;
          114          for (item = items; item && item->text; item++) {
          115                  for (i = 0; i < tokc; i++)
          116 @@ -243,14 +286,24 @@ match(void)
          117                                  break;
          118                  if (i != tokc) /* not all tokens match */
          119                          continue;
          120 -                /* exact matches go first, then prefixes, then substrings */
          121 +                /* exact matches go first, then prefixes with high priority, then prefixes, then substrings */
          122                  if (!tokc || !fstrncmp(text, item->text, textsize))
          123                          appenditem(item, &matches, &matchend);
          124 +                else if (item->hp && !fstrncmp(tokv[0], item->text, len))
          125 +                        appenditem(item, &lhpprefix, &hpprefixend);
          126                  else if (!fstrncmp(tokv[0], item->text, len))
          127                          appenditem(item, &lprefix, &prefixend);
          128                  else
          129                          appenditem(item, &lsubstr, &substrend);
          130          }
          131 +        if (lhpprefix) {
          132 +                if (matches) {
          133 +                        matchend->right = lhpprefix;
          134 +                        lhpprefix->left = matchend;
          135 +                } else
          136 +                        matches = lhpprefix;
          137 +                matchend = hpprefixend;
          138 +        }
          139          if (lprefix) {
          140                  if (matches) {
          141                          matchend->right = lprefix;
          142 @@ -553,6 +606,7 @@ readstdin(void)
          143                  if (!(items[i].text = strdup(buf)))
          144                          die("cannot strdup %u bytes:", strlen(buf) + 1);
          145                  items[i].out = 0;
          146 +                items[i].hp = arrayhas(hpitems, hplength, items[i].text);
          147                  drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
          148                  if (tmpmax > inputw) {
          149                          inputw = tmpmax;
          150 @@ -708,7 +762,8 @@ static void
          151  usage(void)
          152  {
          153          fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
          154 -              "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
          155 +              "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n"
          156 +              "             [-hb color] [-hf color] [-hp items]\n", stderr);
          157          exit(1);
          158  }
          159  
          160 @@ -749,8 +804,14 @@ main(int argc, char *argv[])
          161                          colors[SchemeSel][ColBg] = argv[++i];
          162                  else if (!strcmp(argv[i], "-sf"))  /* selected foreground color */
          163                          colors[SchemeSel][ColFg] = argv[++i];
          164 +                else if (!strcmp(argv[i], "-hb"))  /* high priority background color */
          165 +                        colors[SchemeHp][ColBg] = argv[++i];
          166 +                else if (!strcmp(argv[i], "-hf")) /* low priority background color */
          167 +                        colors[SchemeHp][ColFg] = argv[++i];
          168                  else if (!strcmp(argv[i], "-w"))   /* embedding window id */
          169                          embed = argv[++i];
          170 +                else if (!strcmp(argv[i], "-hp"))
          171 +                        hpitems = tokenize(argv[++i], ",", &hplength);
          172                  else
          173                          usage();
          174