X-Git-Url: https://git.xinqibao.xyz/dmenu.git/blobdiff_plain/0ba3bae98196ec8a0887f494cb8a2dd5b645f763..504b797be8ed4535a038974355a301fbf82e2509:/dmenu.c

diff --git a/dmenu.c b/dmenu.c
index fbbfac0..2966954 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -19,6 +19,7 @@
 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
 #define MIN(a, b)               ((a) < (b) ? (a) : (b))
 #define MAX(a, b)               ((a) > (b) ? (a) : (b))
+#define IS_UTF8_1ST_CHAR(c)     ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
 
 /* enums */
 enum { ColFG, ColBG, ColLast };
@@ -42,8 +43,8 @@ typedef struct {
 typedef struct Item Item;
 struct Item {
 	char *text;
-	Item *next;		/* traverses all items */
-	Item *left, *right;	/* traverses items matching current search pattern */
+	Item *next;         /* traverses all items */
+	Item *left, *right; /* traverses items matching current search pattern */
 };
 
 /* forward declarations */
@@ -85,17 +86,16 @@ static unsigned int numlockmask = 0;
 static Bool running = True;
 static Display *dpy;
 static DC dc;
-static Item *allitems = NULL;	/* first of all items */
-static Item *item = NULL;	/* first of pattern matching items */
+static Item *allitems = NULL;  /* first of all items */
+static Item *item = NULL;      /* first of pattern matching items */
 static Item *sel = NULL;
 static Item *next = NULL;
 static Item *prev = NULL;
 static Item *curr = NULL;
-static Window root, win;
+static Window parent, win;
 static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
 static char *(*fstrstr)(const char *, const char *) = strstr;
-static Bool vlist = False;
-static unsigned int lines = 5;
+static unsigned int lines = 0;
 static void (*calcoffsets)(void) = calcoffsetsh;
 
 void
@@ -159,12 +159,12 @@ cistrstr(const char *s, const char *sub) {
 
 	if(!sub)
 		return (char *)s;
-	if((c = *sub++) != 0) {
+	if((c = *sub++) != '\0') {
 		c = tolower(c);
 		len = strlen(sub);
 		do {
 			do {
-				if((csub = *s++) == 0)
+				if((csub = *s++) == '\0')
 					return NULL;
 			}
 			while(tolower(csub) != c);
@@ -213,20 +213,19 @@ drawmenu(void) {
 	dc.h = mh;
 	drawtext(NULL, dc.norm);
 	/* print prompt? */
-	if(promptw) {
+	if(prompt) {
 		dc.w = promptw;
 		drawtext(prompt, dc.sel);
+		dc.x += dc.w;
 	}
-	dc.x += promptw;
-	dc.w = mw - promptw;
+	dc.w = mw - dc.x;
 	/* print command */
-	if(cmdw && item)
+	if(cmdw && item && lines == 0)
 		dc.w = cmdw;
 	drawtext(text[0] ? text : NULL, dc.norm);
 	drawcursor();
-	dc.x += cmdw;
 	if(curr) {
-		if(vlist)
+		if(lines > 0)
 			drawmenuv();
 		else
 			drawmenuh();
@@ -239,17 +238,17 @@ void
 drawmenuh(void) {
 	Item *i;
 
+	dc.x += cmdw;
 	dc.w = spaceitem;
-	drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
+	drawtext(curr->left ? "<" : NULL, dc.norm);
 	dc.x += dc.w;
-	/* determine maximum items */
 	for(i = curr; i != next; i=i->right) {
 		dc.w = MIN(textw(i->text), mw / 3);
 		drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
 		dc.x += dc.w;
 	}
-	dc.x = mw - spaceitem;
 	dc.w = spaceitem;
+	dc.x = mw - dc.w;
 	drawtext(next ? ">" : NULL, dc.norm);
 }
 
@@ -257,10 +256,8 @@ void
 drawmenuv(void) {
 	Item *i;
 
-	dc.x = 0;
-	dc.w = mw;
+	dc.w = mw - dc.x;
 	dc.y += dc.font.height + 2;
-	/* determine maximum items */
 	for(i = curr; i != next; i=i->right) {
 		drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
 		dc.y += dc.font.height + 2;
@@ -321,7 +318,7 @@ grabkeyboard(void) {
 	unsigned int len;
 
 	for(len = 1000; len; len--) {
-		if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
+		if(XGrabKeyboard(dpy, parent, True, GrabModeAsync, GrabModeAsync, CurrentTime)
 		== GrabSuccess)
 			break;
 		usleep(1000);
@@ -341,13 +338,11 @@ initfont(const char *fontstr) {
 	if(missing)
 		XFreeStringList(missing);
 	if(dc.font.set) {
-		XFontSetExtents *font_extents;
 		XFontStruct **xfonts;
 		char **font_names;
 		dc.font.ascent = dc.font.descent = 0;
-		font_extents = XExtentsOfFontSet(dc.font.set);
 		n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
-		for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
+		for(i = 0; i < n; i++) {
 			dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
 			dc.font.descent = MAX(dc.font.descent, (*xfonts)->descent);
 			xfonts++;
@@ -366,12 +361,12 @@ initfont(const char *fontstr) {
 void
 kpress(XKeyEvent * e) {
 	char buf[sizeof text];
-	int i, num;
+	int i, num, off;
 	unsigned int len;
 	KeySym ksym;
 
 	len = strlen(text);
-	buf[0] = 0;
+	buf[0] = '\0';
 	num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
 	if(IsKeypadKey(ksym)) {
 		if(ksym == XK_KP_Enter)
@@ -414,7 +409,8 @@ kpress(XKeyEvent * e) {
 			break;
 		case XK_u:
 		case XK_U:
-			text[0] = 0;
+			memmove(text, text + cursor, sizeof text - cursor + 1);
+			cursor = 0;
 			match(text);
 			break;
 		case XK_w:
@@ -480,13 +476,19 @@ kpress(XKeyEvent * e) {
 		break;
 	case XK_BackSpace:
 		if(cursor > 0) {
-			memmove(text + cursor - 1, text + cursor, sizeof text - cursor + 1);
-			cursor--;
+			off = 1;
+			while(cursor > off && !IS_UTF8_1ST_CHAR(text[cursor - off]))
+				off++;
+			memmove(text + cursor - off, text + cursor, sizeof text - cursor + off);
+			cursor -= off;
 			match(text);
 		}
 		break;
 	case XK_Delete:
-		memmove(text + cursor, text + cursor + 1, sizeof text - cursor);
+		off = 1;
+		while(cursor + off < sizeof text - 1 && !IS_UTF8_1ST_CHAR(text[cursor + off]))
+			off++;
+		memmove(text + cursor, text + cursor + off, sizeof text - cursor);
 		match(text);
 		break;
 	case XK_End:
@@ -522,9 +524,11 @@ kpress(XKeyEvent * e) {
 				calcoffsets();
 			}
 		}
-		else if(cursor > 0)
-			cursor--;
-		else
+		else if(cursor > 0) {
+			do {
+				cursor--;
+			} while(cursor > 0 && !IS_UTF8_1ST_CHAR(text[cursor]));
+		} else
 			return;
 		break;
 	case XK_Next:
@@ -549,9 +553,11 @@ kpress(XKeyEvent * e) {
 		break;
 	case XK_Right:
 	case XK_Down:
-		if(cursor < len)
-			cursor++;
-		else if(sel && sel->right) {
+		if(cursor < len) {
+			do {
+				cursor++;
+			} while(cursor < len && !IS_UTF8_1ST_CHAR(text[cursor]));
+		} else if(sel && sel->right) {
 			sel=sel->right;
 			if(sel == next) {
 				curr = next;
@@ -626,7 +632,7 @@ readstdin(void) {
 			buf[--len] = '\0';
 		if(!(p = strdup(buf)))
 			eprint("fatal: could not strdup() %u bytes\n", len);
-		if(max < len) {
+		if(max < len || !maxname) {
 			maxname = p;
 			max = len;
 		}
@@ -674,6 +680,7 @@ setup(Bool topbar) {
 #endif
 	XModifierKeymap *modmap;
 	XSetWindowAttributes wa;
+	XWindowAttributes pwa;
 
 	/* init modifier map */
 	modmap = XGetModifierMapping(dpy);
@@ -698,16 +705,15 @@ setup(Bool topbar) {
 	wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask | VisibilityChangeMask;
 
 	/* menu window geometry */
-	mh = dc.font.height + 2;
-	mh = vlist ? mh * (lines+1) : mh;
+	mh = (dc.font.height + 2) * (lines + 1);
 #if XINERAMA
-	if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
+	if(parent == RootWindow(dpy, screen) && XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
 		i = 0;
 		if(n > 1) {
 			int di;
 			unsigned int dui;
 			Window dummy;
-			if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
+			if(XQueryPointer(dpy, parent, &dummy, &dummy, &x, &y, &di, &di, &dui))
 				for(i = 0; i < n; i++)
 					if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
 						break;
@@ -720,19 +726,20 @@ setup(Bool topbar) {
 	else
 #endif
 	{
+		XGetWindowAttributes(dpy, parent, &pwa);
 		x = 0;
-		y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
-		mw = DisplayWidth(dpy, screen);
+		y = topbar ? 0 : pwa.height - mh;
+		mw = pwa.width;
 	}
 
-	win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
+	win = XCreateWindow(dpy, parent, x, y, mw, mh, 0,
 			DefaultDepth(dpy, screen), CopyFromParent,
 			DefaultVisual(dpy, screen),
 			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
 
 	/* pixmap */
-	dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
-	dc.gc = XCreateGC(dpy, root, 0, NULL);
+	dc.drawable = XCreatePixmap(dpy, parent, mw, mh, DefaultDepth(dpy, screen));
+	dc.gc = XCreateGC(dpy, parent, 0, NULL);
 	XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
 	if(!dc.font.set)
 		XSetFont(dpy, dc.gc, dc.font.xfont->fid);
@@ -740,7 +747,7 @@ setup(Bool topbar) {
 		cmdw = MIN(textw(maxname), mw / 3);
 	if(prompt)
 		promptw = MIN(textw(prompt), mw / 5);
-	text[0] = 0;
+	text[0] = '\0';
 	match(text);
 	XMapRaised(dpy, win);
 }
@@ -775,10 +782,9 @@ main(int argc, char *argv[]) {
 		else if(!strcmp(argv[i], "-b"))
 			topbar = False;
 		else if(!strcmp(argv[i], "-e")) {
-			if(++i < argc) root = atoi(argv[i]);
+			if(++i < argc) parent = atoi(argv[i]);
 		}
 		else if(!strcmp(argv[i], "-l")) {
-			vlist = True;
 			calcoffsets = calcoffsetsv;
 			if(++i < argc) lines = atoi(argv[i]);
 		}
@@ -810,8 +816,8 @@ main(int argc, char *argv[]) {
 	if(!(dpy = XOpenDisplay(NULL)))
 		eprint("dmenu: cannot open display\n");
 	screen = DefaultScreen(dpy);
-	if(!root)
-		root = RootWindow(dpy, screen);
+	if(!parent)
+		parent = RootWindow(dpy, screen);
 
 	readstdin();
 	running = grabkeyboard();