sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dmenu-highpriority-e35976f.diff (5541B)


      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 571bc35..d5265e9 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,9 @@ readstdin(void)
    128  	char buf[sizeof text], *p;
    129  	size_t i, size = 0;
    130  
    131 +	if (hpitems && hplength > 0)
    132 +		qsort(hpitems, hplength, sizeof *hpitems, str_compar);
    133 +
    134  	/* read each line from stdin and add it to the item list */
    135  	for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
    136  		if (i + 1 >= size / sizeof *items)
    137 @@ -562,6 +603,11 @@ readstdin(void)
    138  		if (!(items[i].text = strdup(buf)))
    139  			die("cannot strdup %zu bytes:", strlen(buf) + 1);
    140  		items[i].out = 0;
    141 +		p = hpitems == NULL ? NULL : bsearch(
    142 +			&items[i].text, hpitems, hplength, sizeof *hpitems,
    143 +			str_compar
    144 +		);
    145 +		items[i].hp = p != NULL;
    146  	}
    147  	if (items)
    148  		items[i].text = NULL;
    149 @@ -711,7 +757,8 @@ static void
    150  usage(void)
    151  {
    152  	fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
    153 -	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
    154 +	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n"
    155 +	      "             [-hb color] [-hf color] [-hp items]\n", stderr);
    156  	exit(1);
    157  }
    158  
    159 @@ -752,8 +799,14 @@ main(int argc, char *argv[])
    160  			colors[SchemeSel][ColBg] = argv[++i];
    161  		else if (!strcmp(argv[i], "-sf"))  /* selected foreground color */
    162  			colors[SchemeSel][ColFg] = argv[++i];
    163 +		else if (!strcmp(argv[i], "-hb"))  /* high priority background color */
    164 +			colors[SchemeHp][ColBg] = argv[++i];
    165 +		else if (!strcmp(argv[i], "-hf")) /* low priority background color */
    166 +			colors[SchemeHp][ColFg] = argv[++i];
    167  		else if (!strcmp(argv[i], "-w"))   /* embedding window id */
    168  			embed = argv[++i];
    169 +		else if (!strcmp(argv[i], "-hp"))
    170 +			parse_hpitems(argv[++i]);
    171  		else
    172  			usage();
    173