applied Connor's next nice patch, thanks a lot! eededaa7
Anselm R Garbe · 2010-03-31 22:37 1 file(s) · +29 −36
dmenu.c +29 −36
52 52
static void calcoffsetsv(void);
53 53
static char *cistrstr(const char *s, const char *sub);
54 54
static void cleanup(void);
55 +
static void drawcursor(void);
55 56
static void drawmenu(void);
56 57
static void drawmenuh(void);
57 58
static void drawmenuv(void);
247 248
	dc.x += dc.w;
248 249
	/* determine maximum items */
249 250
	for(i = curr; i != next; i=i->right) {
250 -
		dc.w = textw(i->text);
251 -
		if(dc.w > mw / 3)
252 -
			dc.w = mw / 3;
251 +
		dc.w = MIN(textw(i->text), mw / 3);
253 252
		drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
254 253
		dc.x += dc.w;
255 254
	}
395 394
		switch (ksym) {
396 395
		default:	/* ignore other control sequences */
397 396
			return;
398 -
		case XK_bracketleft:
397 +
		case XK_c:
398 +
		case XK_C:
399 399
			ksym = XK_Escape;
400 400
			break;
401 401
		case XK_h:
414 414
		case XK_U:
415 415
			text[0] = 0;
416 416
			match(text);
417 -
			drawmenu();
418 417
			break;
419 418
		case XK_w:
420 419
		case XK_W:
421 -
			if(len) {
422 -
				i = len - 1;
423 -
				while(i >= 0 && text[i] == ' ')
424 -
					text[i--] = 0;
425 -
				while(i >= 0 && text[i] != ' ')
426 -
					text[i--] = 0;
420 +
			if(cursor > 0) {
421 +
				i = cursor;
422 +
				while(i-- > 0 && text[i] == ' ');
423 +
				while(i-- > 0 && text[i] != ' ');
424 +
				memmove(text + i + 1, text + cursor, sizeof text - cursor);
425 +
				cursor = i + 1;
427 426
				match(text);
428 -
				drawmenu();
429 427
			}
430 428
			break;
431 429
		}
473 471
		num = MIN(num, sizeof text - cursor);
474 472
		if(num && !iscntrl((int) buf[0])) {
475 473
			memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
476 -
			memmove(text + cursor, buf, num);
474 +
			memcpy(text + cursor, buf, num);
477 475
			cursor+=num;
478 476
			match(text);
479 477
		}
480 478
		break;
481 479
	case XK_BackSpace:
482 480
		if(cursor > 0) {
483 -
			memmove(text + cursor + -1, text + cursor, sizeof text - cursor);
481 +
			memmove(text + cursor - 1, text + cursor, sizeof text - cursor + 1);
484 482
			cursor--;
485 483
			match(text);
486 484
		}
490 488
		match(text);
491 489
		break;
492 490
	case XK_End:
493 -
		if(!item)
494 -
			return;
491 +
		if(cursor < len) {
492 +
			cursor = len;
493 +
			break;
494 +
		}
495 495
		while(next) {
496 496
			sel = curr = next;
497 497
			calcoffsets();
504 504
		running = False;
505 505
		break;
506 506
	case XK_Home:
507 -
		if(!item)
508 -
			return;
507 +
		if(sel == item) {
508 +
			cursor = 0;
509 +
			break;
510 +
		}
509 511
		sel = curr = item;
510 512
		calcoffsets();
511 513
		break;
536 538
		calcoffsets();
537 539
		break;
538 540
	case XK_Return:
539 -
		if((e->state & ShiftMask) && *text)
541 +
		if((e->state & ShiftMask) || !sel)
540 542
			fprintf(stdout, "%s", text);
541 -
		else if(sel)
543 +
		else
542 544
			fprintf(stdout, "%s", sel->text);
543 -
		else if(*text)
544 -
			fprintf(stdout, "%s", text);
545 545
		fflush(stdout);
546 546
		running = False;
547 547
		break;
567 567
		match(text);
568 568
		break;
569 569
	}
570 -
	len = strlen(text);
571 -
	cursor = MIN(cursor, len);
572 -
	cursor = MAX(cursor, 0);
573 570
	drawmenu();
574 571
}
575 572
620 617
	unsigned int len = 0, max = 0;
621 618
	Item *i, *new;
622 619
623 -
	i = 0;
620 +
	i = NULL;
624 621
	while(fgets(buf, sizeof buf, stdin)) {
625 622
		len = strlen(buf);
626 -
		if (buf[len - 1] == '\n')
627 -
			buf[len - 1] = 0;
623 +
		if(buf[len-1] == '\n')
624 +
			buf[--len] = '\0';
628 625
		if(!(p = strdup(buf)))
629 -
			eprint("fatal: could not strdup() %u bytes\n", strlen(buf));
626 +
			eprint("fatal: could not strdup() %u bytes\n", len);
630 627
		if(max < len) {
631 628
			maxname = p;
632 629
			max = len;
734 731
	if(!dc.font.set)
735 732
		XSetFont(dpy, dc.gc, dc.font.xfont->fid);
736 733
	if(maxname)
737 -
		cmdw = textw(maxname);
738 -
	if(cmdw > mw / 3)
739 -
		cmdw = mw / 3;
734 +
		cmdw = MIN(textw(maxname), mw / 3);
740 735
	if(prompt)
741 -
		promptw = textw(prompt);
742 -
	if(promptw > mw / 5)
743 -
		promptw = mw / 5;
736 +
		promptw = MIN(textw(prompt), mw / 5);
744 737
	text[0] = 0;
745 738
	match(text);
746 739
	XMapRaised(dpy, win);
799 792
			if(++i < argc) selfgcolor = argv[i];
800 793
		}
801 794
		else if(!strcmp(argv[i], "-v"))
802 -
			eprint("dmenu-"VERSION", © 2006-2009 dmenu engineers, see LICENSE for details\n");
795 +
			eprint("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
803 796
		else
804 797
			eprint("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
805 798
			       "             [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");