dotfiles

My collection of dotfiles
git clone git://git.stellar-nexus.ru/dotfiles
Log | Files | Refs

01-dmenu-highpriority-5.2.diff (5479B)


      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 7cf253b..283a009 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 const char **hpitems = NULL;
     32 +static int hplength = 0;
     33  static char text[BUFSIZ] = "";
     34  static char *embed;
     35  static int bh, mw, mh;
     36 @@ -65,6 +67,29 @@ textw_clamp(const char *str, unsigned int n)
     37  	return MIN(w, n);
     38  }
     39  
     40 +static int
     41 +str_compar(const void *s0_in, const void *s1_in)
     42 +{
     43 +	const char *s0 = *(const char **)s0_in;
     44 +	const char *s1 = *(const char **)s1_in;
     45 +	return fstrncmp == strncasecmp ? strcasecmp(s0, s1) : strcmp(s0, s1);
     46 +}
     47 +
     48 +static void
     49 +parse_hpitems(char *src)
     50 +{
     51 +	int n = 0;
     52 +	char *t;
     53 +
     54 +	for (t = strtok(src, ","); t; t = strtok(NULL, ",")) {
     55 +		if (hplength + 1 >= n) {
     56 +			if (!(hpitems = realloc(hpitems, (n += 8) * sizeof *hpitems)))
     57 +				die("Unable to realloc %zu bytes\n", n * sizeof *hpitems);
     58 +		}
     59 +		hpitems[hplength++] = t;
     60 +	}
     61 +}
     62 +
     63  static void
     64  appenditem(struct item *item, struct item **list, struct item **last)
     65  {
     66 @@ -107,6 +132,7 @@ cleanup(void)
     67  	for (i = 0; items && items[i].text; ++i)
     68  		free(items[i].text);
     69  	free(items);
     70 +	free(hpitems);
     71  	drw_free(drw);
     72  	XSync(dpy, False);
     73  	XCloseDisplay(dpy);
     74 @@ -135,6 +161,8 @@ drawitem(struct item *item, int x, int y, int w)
     75  {
     76  	if (item == sel)
     77  		drw_setscheme(drw, scheme[SchemeSel]);
     78 +	else if (item->hp)
     79 +		drw_setscheme(drw, scheme[SchemeHp]);
     80  	else if (item->out)
     81  		drw_setscheme(drw, scheme[SchemeOut]);
     82  	else
     83 @@ -236,7 +264,7 @@ match(void)
     84  	char buf[sizeof text], *s;
     85  	int i, tokc = 0;
     86  	size_t len, textsize;
     87 -	struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
     88 +	struct item *item, *lhpprefix, *lprefix, *lsubstr, *hpprefixend, *prefixend, *substrend;
     89  
     90  	strcpy(buf, text);
     91  	/* separate input text into tokens to be matched individually */
     92 @@ -245,7 +273,7 @@ match(void)
     93  			die("cannot realloc %zu bytes:", tokn * sizeof *tokv);
     94  	len = tokc ? strlen(tokv[0]) : 0;
     95  
     96 -	matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
     97 +	matches = lhpprefix = lprefix = lsubstr = matchend = hpprefixend = prefixend = substrend = NULL;
     98  	textsize = strlen(text) + 1;
     99  	for (item = items; item && item->text; item++) {
    100  		for (i = 0; i < tokc; i++)
    101 @@ -253,14 +281,24 @@ match(void)
    102  				break;
    103  		if (i != tokc) /* not all tokens match */
    104  			continue;
    105 -		/* exact matches go first, then prefixes, then substrings */
    106 +		/* exact matches go first, then prefixes with high priority, then prefixes, then substrings */
    107  		if (!tokc || !fstrncmp(text, item->text, textsize))
    108  			appenditem(item, &matches, &matchend);
    109 +		else if (item->hp && !fstrncmp(tokv[0], item->text, len))
    110 +			appenditem(item, &lhpprefix, &hpprefixend);
    111  		else if (!fstrncmp(tokv[0], item->text, len))
    112  			appenditem(item, &lprefix, &prefixend);
    113  		else
    114  			appenditem(item, &lsubstr, &substrend);
    115  	}
    116 +	if (lhpprefix) {
    117 +		if (matches) {
    118 +			matchend->right = lhpprefix;
    119 +			lhpprefix->left = matchend;
    120 +		} else
    121 +			matches = lhpprefix;
    122 +		matchend = hpprefixend;
    123 +	}
    124  	if (lprefix) {
    125  		if (matches) {
    126  			matchend->right = lprefix;
    127 @@ -552,6 +590,10 @@ readstdin(void)
    128  	char *line = NULL;
    129  	size_t i, junk, size = 0;
    130  	ssize_t len;
    131 +	char **p;
    132 +
    133 +	if (hpitems && hplength > 0)
    134 +		qsort(hpitems, hplength, sizeof *hpitems, str_compar);
    135  
    136  	/* read each line from stdin and add it to the item list */
    137  	for (i = 0; (len = getline(&line, &junk, stdin)) != -1; i++, line = NULL) {
    138 @@ -562,6 +604,11 @@ readstdin(void)
    139  			line[len - 1] = '\0';
    140  		items[i].text = line;
    141  		items[i].out = 0;
    142 +		p = hpitems == NULL ? NULL : bsearch(
    143 +			&items[i].text, hpitems, hplength, sizeof *hpitems,
    144 +			str_compar
    145 +		);
    146 +		items[i].hp = p != NULL;
    147  	}
    148  	if (items)
    149  		items[i].text = NULL;
    150 @@ -711,7 +758,8 @@ static void
    151  usage(void)
    152  {
    153  	die("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
    154 -	    "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]");
    155 +	    "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n"
    156 +	    "             [-hb color] [-hf color] [-hp items]");
    157  }
    158  
    159  int
    160 @@ -751,8 +799,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 +			parse_hpitems(argv[++i]);
    172  		else
    173  			usage();
    174