Return to repo list

suckless-gf-dmenu

Gentoo-friendly patches for Suckless's dmenu.
Return to HMagellan.com

highpriority.patch (4905B)


      1 diff --git a/dmenu.c b/dmenu.c
      2 index 6b8f51b..7bf4099 100644
      3 --- a/dmenu.c
      4 +++ b/dmenu.c
      5 @@ -26,14 +26,16 @@
      6  #define TEXTW(X)              (drw_fontset_getwidth(drw, (X)) + lrpad)
      7  
      8  /* enums */
      9 -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
     10 +enum { SchemeNorm, SchemeSel, SchemeHp, SchemeOut, SchemeLast }; /* color schemes */
     11  
     12  struct item {
     13  	char *text;
     14  	struct item *left, *right;
     15 -	int out;
     16 +	int out, hp;
     17  };
     18  
     19 +static char **hpitems = NULL;
     20 +static int hplength = 0;
     21  static char text[BUFSIZ] = "";
     22  static char *embed;
     23  static int bh, mw, mh;
     24 @@ -58,6 +60,37 @@ static Clr *scheme[SchemeLast];
     25  static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
     26  static char *(*fstrstr)(const char *, const char *) = strstr;
     27  
     28 +static char**
     29 +tokenize(char *source, const char *delim, int *llen) {
     30 +	int listlength = 0;
     31 +	char **list = malloc(1 * sizeof(char*));
     32 +	char *token = strtok(source, delim);
     33 +
     34 +	while (token) {
     35 +		if (!(list = realloc(list, sizeof(char*) * (listlength + 1))))
     36 +			die("Unable to realloc %d bytes\n", sizeof(char*) * (listlength + 1));
     37 +		if (!(list[listlength] = strdup(token)))
     38 +			die("Unable to strdup %d bytes\n", strlen(token) + 1);
     39 +		token = strtok(NULL, delim);
     40 +		listlength++;
     41 +	}
     42 +
     43 +	*llen = listlength;
     44 +	return list;
     45 +}
     46 +
     47 +static int
     48 +arrayhas(char **list, int length, char *item) {
     49 +   int i = 0;
     50 +	for (i; i < length; i++) {
     51 +		int len1 = strlen(list[i]);
     52 +		int len2 = strlen(item);
     53 +		if (fstrncmp(list[i], item, len1 > len2 ? len2 : len1) == 0)
     54 +			return 1;
     55 +	}
     56 +	return 0;
     57 +}
     58 +
     59  static void
     60  appenditem(struct item *item, struct item **list, struct item **last)
     61  {
     62 @@ -118,6 +150,8 @@ drawitem(struct item *item, int x, int y, int w)
     63  {
     64  	if (item == sel)
     65  		drw_setscheme(drw, scheme[SchemeSel]);
     66 +	else if (item->hp)
     67 +		drw_setscheme(drw, scheme[SchemeHp]);
     68  	else if (item->out)
     69  		drw_setscheme(drw, scheme[SchemeOut]);
     70  	else
     71 @@ -219,7 +253,7 @@ match(void)
     72  	char buf[sizeof text], *s;
     73  	int i, tokc = 0;
     74  	size_t len, textsize;
     75 -	struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
     76 +	struct item *item, *lhpprefix, *lprefix, *lsubstr, *hpprefixend, *prefixend, *substrend;
     77  
     78  	strcpy(buf, text);
     79  	/* separate input text into tokens to be matched individually */
     80 @@ -228,7 +262,7 @@ match(void)
     81  			die("cannot realloc %u bytes:", tokn * sizeof *tokv);
     82  	len = tokc ? strlen(tokv[0]) : 0;
     83  
     84 -	matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
     85 +	matches = lhpprefix = lprefix = lsubstr = matchend = hpprefixend = prefixend = substrend = NULL;
     86  	textsize = strlen(text) + 1;
     87  	for (item = items; item && item->text; item++) {
     88  		for (i = 0; i < tokc; i++)
     89 @@ -236,14 +270,24 @@ match(void)
     90  				break;
     91  		if (i != tokc) /* not all tokens match */
     92  			continue;
     93 -		/* exact matches go first, then prefixes, then substrings */
     94 +		/* exact matches go first, then prefixes with high priority, then prefixes, then substrings */
     95  		if (!tokc || !fstrncmp(text, item->text, textsize))
     96  			appenditem(item, &matches, &matchend);
     97 +		else if (item->hp && !fstrncmp(tokv[0], item->text, len))
     98 +			appenditem(item, &lhpprefix, &hpprefixend);
     99  		else if (!fstrncmp(tokv[0], item->text, len))
    100  			appenditem(item, &lprefix, &prefixend);
    101  		else
    102  			appenditem(item, &lsubstr, &substrend);
    103  	}
    104 +	if (lhpprefix) {
    105 +		if (matches) {
    106 +			matchend->right = lhpprefix;
    107 +			lhpprefix->left = matchend;
    108 +		} else
    109 +			matches = lhpprefix;
    110 +		matchend = hpprefixend;
    111 +	}
    112  	if (lprefix) {
    113  		if (matches) {
    114  			matchend->right = lprefix;
    115 @@ -535,6 +579,7 @@ readstdin(void)
    116  		if (!(items[i].text = strdup(buf)))
    117  			die("cannot strdup %u bytes:", strlen(buf) + 1);
    118  		items[i].out = 0;
    119 +		items[i].hp = arrayhas(hpitems, hplength, items[i].text);
    120  		drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
    121  		if (tmpmax > inputw) {
    122  			inputw = tmpmax;
    123 @@ -683,7 +728,8 @@ static void
    124  usage(void)
    125  {
    126  	fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
    127 -	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
    128 +	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n"
    129 +		  "             [-hb color] [-hf color] [-hp items]\n", stderr);
    130  	exit(1);
    131  }
    132  
    133 @@ -724,8 +770,14 @@ main(int argc, char *argv[])
    134  			colors[SchemeSel][ColBg] = argv[++i];
    135  		else if (!strcmp(argv[i], "-sf"))  /* selected foreground color */
    136  			colors[SchemeSel][ColFg] = argv[++i];
    137 +		else if (!strcmp(argv[i], "-hb"))  /* high priority background color */
    138 +			colors[SchemeHp][ColBg] = argv[++i];
    139 +		else if (!strcmp(argv[i], "-hf")) /* low priority background color */
    140 +			colors[SchemeHp][ColFg] = argv[++i];
    141  		else if (!strcmp(argv[i], "-w"))   /* embedding window id */
    142  			embed = argv[++i];
    143 +		else if (!strcmp(argv[i], "-hp"))
    144 +			hpitems = tokenize(argv[++i], ",", &hplength);
    145  		else
    146  			usage();
    147  
    148 -- 
    149 2.26.2
    150