Return to repo list

suckless-gf-dmenu

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

dynamicoptions-nodoc.patch (2737B)


      1 diff --git a/dmenu.c b/dmenu.c
      2 index 6b8f51b5..356d4cc9 100644
      3 --- a/dmenu.c
      4 +++ b/dmenu.c
      5 @@ -210,9 +210,33 @@ grabkeyboard(void)
      6  	die("cannot grab keyboard");
      7  }
      8  
      9 +static void readstdin(FILE* stream);
     10 +
     11 +static void
     12 +refreshoptions(){
     13 +	int dynlen = strlen(dynamic);
     14 +	char* cmd= malloc(dynlen + strlen(text)+2);
     15 +	if(cmd == NULL)
     16 +		die("malloc:");
     17 +	sprintf(cmd,"%s %s",dynamic, text);
     18 +	FILE *stream = popen(cmd, "r");
     19 +	if(!stream)
     20 +		die("popen(%s):",cmd);
     21 +	readstdin(stream);
     22 +	int pc = pclose(stream);
     23 +	if(pc == -1)
     24 +		die("pclose:");
     25 +	free(cmd);
     26 +	curr = sel = items;
     27 +}
     28 +
     29  static void
     30  match(void)
     31  {
     32 +	if(dynamic && *dynamic){
     33 +		refreshoptions();
     34 +	}
     35 +
     36  	static char **tokv = NULL;
     37  	static int tokn = 0;
     38  
     39 @@ -234,7 +258,7 @@ match(void)
     40  		for (i = 0; i < tokc; i++)
     41  			if (!fstrstr(item->text, tokv[i]))
     42  				break;
     43 -		if (i != tokc) /* not all tokens match */
     44 +		if (i != tokc && !(dynamic && *dynamic)) /* not all tokens match */
     45  			continue;
     46  		/* exact matches go first, then prefixes, then substrings */
     47  		if (!tokc || !fstrncmp(text, item->text, textsize))
     48 @@ -519,14 +543,14 @@ paste(void)
     49  }
     50  
     51  static void
     52 -readstdin(void)
     53 +readstdin(FILE* stream)
     54  {
     55  	char buf[sizeof text], *p;
     56  	size_t i, imax = 0, size = 0;
     57  	unsigned int tmpmax = 0;
     58  
     59  	/* read each line from stdin and add it to the item list */
     60 -	for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
     61 +	for (i = 0; fgets(buf, sizeof buf, stream); i++) {
     62  		if (i + 1 >= size / sizeof *items)
     63  			if (!(items = realloc(items, (size += BUFSIZ))))
     64  				die("cannot realloc %u bytes:", size);
     65 @@ -544,7 +568,8 @@ readstdin(void)
     66  	if (items)
     67  		items[i].text = NULL;
     68  	inputw = items ? TEXTW(items[imax].text) : 0;
     69 -	lines = MIN(lines, i);
     70 +	if (!dynamic || !*dynamic)
     71 +		lines = MIN(lines, i);
     72  }
     73  
     74  static void
     75 @@ -683,7 +708,7 @@ static void
     76  usage(void)
     77  {
     78  	fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
     79 -	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
     80 +	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n" "[-dy command]\n", stderr);
     81  	exit(1);
     82  }
     83  
     84 @@ -726,6 +751,8 @@ main(int argc, char *argv[])
     85  			colors[SchemeSel][ColFg] = argv[++i];
     86  		else if (!strcmp(argv[i], "-w"))   /* embedding window id */
     87  			embed = argv[++i];
     88 +		else if (!strcmp(argv[i], "-dy"))  /* dynamic command to run */
     89 +			dynamic = argv[++i];
     90  		else
     91  			usage();
     92  
     93 @@ -754,9 +781,11 @@ main(int argc, char *argv[])
     94  
     95  	if (fast && !isatty(0)) {
     96  		grabkeyboard();
     97 -		readstdin();
     98 +		if(!(dynamic && *dynamic))
     99 +			readstdin(stdin);
    100  	} else {
    101 -		readstdin();
    102 +		if(!(dynamic && *dynamic))
    103 +			readstdin(stdin);
    104  		grabkeyboard();
    105  	}
    106  	setup();
    107 -- 
    108 2.26.2
    109