X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=dmenu.c;h=a06ae1572672deba09e4a36537369a8e457e4601;hb=14f72a276334de4a46543cf648a4922bc54f1e9f;hp=4ea95f80984475647c476db67c55deb9666668ca;hpb=3402a8d53a95bd60b434bd26139d398392489b28;p=dmenu.git diff --git a/dmenu.c b/dmenu.c index 4ea95f8..a06ae15 100644 --- a/dmenu.c +++ b/dmenu.c @@ -22,6 +22,7 @@ typedef struct Item Item; struct Item { char *text; Item *left, *right; + Bool out; }; static void appenditem(Item *item, Item **list, Item **last); @@ -43,17 +44,10 @@ static char text[BUFSIZ] = ""; static int bh, mw, mh; static int inputw, promptw; static size_t cursor = 0; -static const char *font = NULL; -static const char *prompt = NULL; -static const char *normbgcolor = "#222222"; -static const char *normfgcolor = "#bbbbbb"; -static const char *selbgcolor = "#005577"; -static const char *selfgcolor = "#eeeeee"; -static unsigned int lines = 0; static unsigned long normcol[ColLast]; static unsigned long selcol[ColLast]; +static unsigned long outcol[ColLast]; static Atom clip, utf8; -static Bool topbar = True; static DC *dc; static Item *items = NULL; static Item *matches, *matchend; @@ -61,6 +55,8 @@ static Item *prev, *curr, *next, *sel; static Window win; static XIC xic; +#include "config.h" + static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; static char *(*fstrstr)(const char *, const char *) = strstr; @@ -169,7 +165,7 @@ drawmenu(void) { dc->h = bh; drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol)); - if(prompt) { + if(prompt && *prompt) { dc->w = promptw; drawtext(dc, prompt, selcol); dc->x = dc->w; @@ -185,7 +181,8 @@ drawmenu(void) { dc->w = mw - dc->x; for(item = curr; item != next; item = item->right) { dc->y += dc->h; - drawtext(dc, item->text, (item == sel) ? selcol : normcol); + drawtext(dc, item->text, (item == sel) ? selcol : + (item->out) ? outcol : normcol); } } else if(matches) { @@ -197,7 +194,8 @@ drawmenu(void) { for(item = curr; item != next; item = item->right) { dc->x += dc->w; dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">")); - drawtext(dc, item->text, (item == sel) ? selcol : normcol); + drawtext(dc, item->text, (item == sel) ? selcol : + (item->out) ? outcol : normcol); } dc->w = textw(dc, ">"); dc->x = mw - dc->w; @@ -251,10 +249,13 @@ keypress(XKeyEvent *ev) { case XK_d: ksym = XK_Delete; break; case XK_e: ksym = XK_End; break; case XK_f: ksym = XK_Right; break; + case XK_g: ksym = XK_Escape; break; case XK_h: ksym = XK_BackSpace; break; case XK_i: ksym = XK_Tab; break; - case XK_j: ksym = XK_Return; break; - case XK_m: ksym = XK_Return; break; + case XK_j: /* fallthrough */ + case XK_J: ksym = XK_Return; break; + case XK_m: /* fallthrough */ + case XK_M: ksym = XK_Return; break; case XK_n: ksym = XK_Down; break; case XK_p: ksym = XK_Up; break; @@ -275,6 +276,9 @@ keypress(XKeyEvent *ev) { XConvertSelection(dc->dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY, utf8, utf8, win, CurrentTime); return; + case XK_Return: + case XK_KP_Enter: + break; default: return; } @@ -359,7 +363,10 @@ keypress(XKeyEvent *ev) { case XK_Return: case XK_KP_Enter: puts((sel && !(ev->state & ShiftMask)) ? sel->text : text); - exit(EXIT_SUCCESS); + if(!(ev->state & ControlMask)) + exit(EXIT_SUCCESS); + sel->out = True; + break; case XK_Right: if(text[cursor] != '\0') { cursor = nextrune(+1); @@ -377,7 +384,8 @@ keypress(XKeyEvent *ev) { case XK_Tab: if(!sel) return; - strncpy(text, sel->text, sizeof text); + strncpy(text, sel->text, sizeof text - 1); + text[sizeof text - 1] = '\0'; cursor = strlen(text); match(); break; @@ -477,6 +485,7 @@ readstdin(void) { *p = '\0'; if(!(items[i].text = strdup(buf))) eprintf("cannot strdup %u bytes:", strlen(buf)+1); + items[i].out = False; if(strlen(items[i].text) > max) max = strlen(maxstr = items[i].text); } @@ -528,6 +537,8 @@ setup(void) { normcol[ColFG] = getcolor(dc, normfgcolor); selcol[ColBG] = getcolor(dc, selbgcolor); selcol[ColFG] = getcolor(dc, selfgcolor); + outcol[ColBG] = getcolor(dc, outbgcolor); + outcol[ColFG] = getcolor(dc, outfgcolor); clip = XInternAtom(dc->dpy, "CLIPBOARD", False); utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False); @@ -576,7 +587,7 @@ setup(void) { y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh; mw = DisplayWidth(dc->dpy, screen); } - promptw = prompt ? textw(dc, prompt) : 0; + promptw = (prompt && *prompt) ? textw(dc, prompt) : 0; inputw = MIN(inputw, mw/3); match();