X-Git-Url: https://git.xinqibao.xyz/dmenu.git/blobdiff_plain/f5bd1eed8d746414f5fbf9645124ccd54a095386..43540746077d4fd8ef963b5939934d32d59c9417:/dmenu.c diff --git a/dmenu.c b/dmenu.c index 1681a55..a8a6290 100644 --- a/dmenu.c +++ b/dmenu.c @@ -27,9 +27,9 @@ static void calcoffsets(void); static void drawmenu(void); static char *fstrstr(const char *s, const char *sub); static void grabkeyboard(void); -static void insert(const char *s, ssize_t n); +static void insert(const char *str, ssize_t n); static void keypress(XKeyEvent *ev); -static void match(void); +static void match(Bool sub); static size_t nextrune(int incr); static void paste(void); static void readstdin(void); @@ -38,7 +38,7 @@ static void setup(void); static char text[BUFSIZ] = ""; static int bh, mw, mh; -static int inputw = 0; +static int inputw; static int lines = 0; static int monitor = -1; static int promptw; @@ -57,7 +57,7 @@ static DC *dc; static Item *items = NULL; static Item *matches, *matchend; static Item *prev, *curr, *next, *sel; -static Window root, win; +static Window win; static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; @@ -78,7 +78,7 @@ main(int argc, char *argv[]) { fast = True; else if(!strcmp(argv[i], "-i")) fstrncmp = strncasecmp; - else if(i == argc-1) + else if(i+1 == argc) goto usage; /* double flags */ else if(!strcmp(argv[i], "-l")) @@ -104,14 +104,14 @@ main(int argc, char *argv[]) { initfont(dc, font); if(fast) { - setup(); + grabkeyboard(); readstdin(); } else { readstdin(); - setup(); + grabkeyboard(); } - match(); + setup(); run(); return EXIT_FAILURE; @@ -209,7 +209,8 @@ grabkeyboard(void) { int i; for(i = 0; i < 1000; i++) { - if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)) + if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True, + GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) return; usleep(1000); } @@ -217,14 +218,14 @@ grabkeyboard(void) { } void -insert(const char *s, ssize_t n) { +insert(const char *str, ssize_t n) { if(strlen(text) + n > sizeof text - 1) return; - memmove(text + cursor + n, text + cursor, sizeof text - cursor - MAX(n, 0)); + memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0)); if(n > 0) - memcpy(text + cursor, s, n); + memcpy(&text[cursor], str, n); cursor += n; - match(); + match(n > 0); } void @@ -268,7 +269,7 @@ keypress(XKeyEvent *ev) { break; case XK_k: /* delete right */ text[cursor] = '\0'; - match(); + match(False); break; case XK_n: ksym = XK_Down; @@ -374,30 +375,31 @@ keypress(XKeyEvent *ev) { return; strncpy(text, sel->text, sizeof text); cursor = strlen(text); - match(); + match(True); break; } drawmenu(); } void -match(void) { +match(Bool sub) { size_t len = strlen(text); - Item *item, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend; + Item *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend; + Item *item, *next = NULL; - matches = lexact = lprefix = lsubstr = matchend = exactend = prefixend = substrend = NULL; - for(item = items; item && item->text; item++) + lexact = lprefix = lsubstr = exactend = prefixend = substrend = NULL; + for(item = sub ? matches : items; item && item->text; item = next) { + next = sub ? item->right : item + 1; if(!fstrncmp(text, item->text, len + 1)) appenditem(item, &lexact, &exactend); else if(!fstrncmp(text, item->text, len)) appenditem(item, &lprefix, &prefixend); else if(fstrstr(item->text, text)) appenditem(item, &lsubstr, &substrend); - - if(lexact) { - matches = lexact; - matchend = exactend; } + matches = lexact; + matchend = exactend; + if(lprefix) { if(matchend) { matchend->right = lprefix; @@ -458,8 +460,7 @@ readstdin(void) { if(strlen(items[i].text) > max) max = strlen(maxstr = items[i].text); } - if(maxstr) - inputw = textw(dc, maxstr); + inputw = maxstr ? textw(dc, maxstr) : 0; } void @@ -488,21 +489,20 @@ run(void) { void setup(void) { - int x, y, screen; + int x, y, screen = DefaultScreen(dc->dpy); + Window root = RootWindow(dc->dpy, screen); XSetWindowAttributes wa; #ifdef XINERAMA int n; XineramaScreenInfo *info; #endif - screen = DefaultScreen(dc->dpy); - root = RootWindow(dc->dpy, screen); - utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False); - normcol[ColBG] = getcolor(dc, normbgcolor); normcol[ColFG] = getcolor(dc, normfgcolor); - selcol[ColBG] = getcolor(dc, selbgcolor); - selcol[ColFG] = getcolor(dc, selfgcolor); + selcol[ColBG] = getcolor(dc, selbgcolor); + selcol[ColFG] = getcolor(dc, selfgcolor); + + utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False); /* menu geometry */ bh = dc->font.height + 2; @@ -531,6 +531,10 @@ setup(void) { y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh; mw = DisplayWidth(dc->dpy, screen); } + inputw = MIN(inputw, mw/3); + promptw = prompt ? textw(dc, prompt) : 0; + match(False); + /* menu window */ wa.override_redirect = True; wa.background_pixmap = ParentRelative; @@ -540,9 +544,7 @@ setup(void) { DefaultVisual(dc->dpy, screen), CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); - grabkeyboard(); - resizedc(dc, mw, mh); - inputw = MIN(inputw, mw/3); - promptw = prompt ? textw(dc, prompt) : 0; XMapRaised(dc->dpy, win); + resizedc(dc, mw, mh); + drawmenu(); }