Return to repo list

suckless-gf-dmenu

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

highlight.patch (2092B)


      1 diff --git a/dmenu.c b/dmenu.c
      2 index 6b8f51b..d5e1991 100644
      3 --- a/dmenu.c
      4 +++ b/dmenu.c
      5 @@ -26,7 +26,7 @@
      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, SchemeOut, SchemeNormHighlight, SchemeSelHighlight, SchemeLast }; /* color schemes */
     11  
     12  struct item {
     13  	char *text;
     14 @@ -113,6 +113,43 @@ cistrstr(const char *s, const char *sub)
     15  	return NULL;
     16  }
     17  
     18 +static void
     19 +drawhighlights(struct item *item, int x, int y, int maxw)
     20 +{
     21 +	char restorechar, tokens[sizeof text], *highlight,  *token;
     22 +	int indentx, highlightlen;
     23 +
     24 +	drw_setscheme(drw, scheme[item == sel ? SchemeSelHighlight : SchemeNormHighlight]);
     25 +	strcpy(tokens, text);
     26 +	for (token = strtok(tokens, " "); token; token = strtok(NULL, " ")) {
     27 +		highlight = fstrstr(item->text, token);
     28 +		while (highlight) {
     29 +			/* Move item str end, calc width for highlight indent, & restore */
     30 +			highlightlen = highlight - item->text;
     31 +			restorechar = *highlight;
     32 +			item->text[highlightlen] = '\0';
     33 +			indentx = TEXTW(item->text);
     34 +			item->text[highlightlen] = restorechar;
     35 +
     36 +			/* Move highlight str end, draw highlight, & restore */
     37 +			restorechar = highlight[strlen(token)];
     38 +			highlight[strlen(token)] = '\0';
     39 +			if (indentx - (lrpad / 2) - 1 < maxw)
     40 +				drw_text(
     41 +					drw,
     42 +					x + indentx - (lrpad / 2) - 1,
     43 +					y,
     44 +					MIN(maxw - indentx, TEXTW(highlight) - lrpad),
     45 +					bh, 0, highlight, 0
     46 +				);
     47 +			highlight[strlen(token)] = restorechar;
     48 +
     49 +			if (strlen(highlight) - strlen(token) < strlen(token)) break;
     50 +			highlight = fstrstr(highlight + strlen(token), token);
     51 +		}
     52 +	}
     53 +}
     54 +
     55  static int
     56  drawitem(struct item *item, int x, int y, int w)
     57  {
     58 @@ -123,7 +160,9 @@ drawitem(struct item *item, int x, int y, int w)
     59  	else
     60  		drw_setscheme(drw, scheme[SchemeNorm]);
     61  
     62 -	return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
     63 +	int r = drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
     64 +	drawhighlights(item, x, y, w);
     65 +	return r;
     66  }
     67  
     68  static void
     69 -- 
     70 2.23.1
     71