static int cmdw = 0;
static int promptw = 0;
static int ret = 0;
-static int cursor = 0;
static int screen;
static unsigned int mw, mh;
+static unsigned int cursor = 0;
static unsigned int numlockmask = 0;
static Bool running = True;
static Display *dpy;
static Item *prev = NULL;
static Item *curr = NULL;
static Window parent, win;
-static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
+static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
static char *(*fstrstr)(const char *, const char *) = strstr;
static unsigned int lines = 0;
static void (*calcoffsets)(void) = calcoffsetsh;
void
calcoffsetsh(void) {
- int tw;
unsigned int w;
if(!curr)
return;
w = promptw + cmdw + 2 * spaceitem;
- for(next = curr; next; next=next->right) {
- tw = MIN(textw(next->text), mw / 3);
- w += tw;
- if(w > mw)
- break;
- }
+ for(next = curr; next && w < mw; next=next->right)
+ w += MIN(textw(next->text), mw / 3);
w = promptw + cmdw + 2 * spaceitem;
- for(prev = curr; prev && prev->left; prev=prev->left) {
- tw = MIN(textw(prev->left->text), mw / 3);
- w += tw;
- if(w > mw)
- break;
- }
+ for(prev = curr; prev && prev->left && w < mw; prev=prev->left)
+ w += MIN(textw(prev->left->text), mw / 3);
}
void
calcoffsetsv(void) {
- static unsigned int h;
+ unsigned int h;
if(!curr)
return;
- h = (dc.font.height + 2) * (lines + 1);
- for(next = curr; next; next=next->right) {
+ h = (dc.font.height + 2) * lines;
+ for(next = curr; next && h > 0; next = next->right)
h -= dc.font.height + 2;
- if(h <= 0)
- break;
- }
- h = (dc.font.height + 2) * (lines + 1);
- for(prev = curr; prev && prev->left; prev=prev->left) {
+ h = (dc.font.height + 2) * lines;
+ for(prev = curr; prev && prev->left && h > 0; prev = prev->left)
h -= dc.font.height + 2;
- if(h <= 0)
- break;
- }
}
char *
if(!sub)
return (char *)s;
- if((c = *sub++) != '\0') {
- c = tolower(c);
+ if((c = tolower(*sub++)) != '\0') {
len = strlen(sub);
do {
do {
void
cleanup(void) {
- Item *itm;
-
- while(allitems) {
- itm = allitems->next;
- free(allitems->text);
- free(allitems);
- allitems = itm;
- }
if(dc.font.set)
XFreeFontSet(dpy, dc.font.set);
else
/* print command */
if(cmdw && item && lines == 0)
dc.w = cmdw;
- drawtext(text[0] ? text : NULL, dc.norm);
+ drawtext(*text ? text : NULL, dc.norm);
drawcursor();
if(curr) {
if(lines > 0)
Item *i;
dc.w = mw - dc.x;
- dc.y += dc.font.height + 2;
+ dc.h = dc.font.height + 2;
+ dc.y = dc.h;
for(i = curr; i != next; i=i->right) {
drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
- dc.y += dc.font.height + 2;
+ dc.y += dc.h;
}
+ dc.h = mh - dc.y;
drawtext(NULL, dc.norm);
}
XColor color;
if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
- eprint("error, cannot allocate color '%s'\n", colstr);
+ eprint("dmenu: cannot allocate color '%s'\n", colstr);
return color.pixel;
}
void
initfont(const char *fontstr) {
- char *def, **missing;
+ char *def, **missing = NULL;
int i, n;
if(!fontstr || fontstr[0] == '\0')
- eprint("error, cannot load font: '%s'\n", fontstr);
- missing = NULL;
+ eprint("dmenu: cannot load font: '%s'\n", fontstr);
dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
if(missing)
XFreeStringList(missing);
else {
if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
&& !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
- eprint("error, cannot load font: '%s'\n", fontstr);
+ eprint("dmenu: cannot load font: '%s'\n", fontstr);
dc.font.ascent = dc.font.xfont->ascent;
dc.font.descent = dc.font.xfont->descent;
}
void
kpress(XKeyEvent * e) {
char buf[sizeof text];
- int i, num, off;
- unsigned int len;
+ int num;
+ unsigned int i, len;
KeySym ksym;
len = strlen(text);
- buf[0] = '\0';
num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
- if(IsKeypadKey(ksym)) {
- if(ksym == XK_KP_Enter)
- ksym = XK_Return;
- else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
- ksym = (ksym - XK_KP_0) + XK_0;
- }
- if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
- || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
- || IsPrivateKeypadKey(ksym))
+ if(ksym == XK_KP_Enter)
+ ksym = XK_Return;
+ else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
+ ksym = (ksym - XK_KP_0) + XK_0;
+ else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
+ || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
+ || IsPrivateKeypadKey(ksym))
return;
/* first check if a control mask is omitted */
if(e->state & ControlMask) {
- switch (ksym) {
- default: /* ignore other control sequences */
+ switch(tolower(ksym)) {
+ default:
return;
case XK_a:
- case XK_A:
ksym = XK_Home;
break;
+ case XK_b:
+ ksym = XK_Left;
+ break;
case XK_c:
- case XK_C:
ksym = XK_Escape;
break;
case XK_e:
- case XK_E:
ksym = XK_End;
break;
+ case XK_f:
+ ksym = XK_Right;
+ break;
case XK_h:
- case XK_H:
ksym = XK_BackSpace;
break;
case XK_i:
- case XK_I:
ksym = XK_Tab;
break;
case XK_j:
- case XK_J:
ksym = XK_Return;
break;
+ case XK_k:
+ text[cursor] = '\0';
+ break;
+ case XK_n:
+ ksym = XK_Down;
+ break;
+ case XK_p:
+ ksym = XK_Up;
+ break;
case XK_u:
- case XK_U:
memmove(text, text + cursor, sizeof text - cursor + 1);
cursor = 0;
match(text);
break;
case XK_w:
- case XK_W:
if(cursor > 0) {
i = cursor;
while(i-- > 0 && text[i] == ' ');
match(text);
}
break;
- }
- }
- if(CLEANMASK(e->state) & Mod1Mask) {
- switch(ksym) {
- default: return;
- case XK_h:
- ksym = XK_Left;
- break;
- case XK_l:
- ksym = XK_Right;
- break;
- case XK_j:
- ksym = XK_Next;
- break;
- case XK_k:
- ksym = XK_Prior;
- break;
- case XK_g:
- ksym = XK_Home;
- break;
- case XK_G:
- ksym = XK_End;
- break;
- case XK_p:
+ case XK_y:
{
FILE *fp;
char *s;
- if(!(fp = (FILE*)popen("sselp", "r")))
- eprint("dmenu: Could not popen sselp\n");
+ if(!(fp = popen("sselp", "r")))
+ eprint("dmenu: cannot popen sselp\n");
s = fgets(buf, sizeof buf, fp);
pclose(fp);
if(s == NULL)
if(num && !iscntrl((int) buf[0])) {
memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
memcpy(text + cursor, buf, num);
- cursor+=num;
+ cursor += num;
match(text);
}
break;
case XK_BackSpace:
- if(cursor > 0) {
- 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);
- }
+ if(cursor == 0)
+ return;
+ for(i = 1; cursor - i > 0 && !IS_UTF8_1ST_CHAR(text[cursor - i]); i++);
+ memmove(text + cursor - i, text + cursor, sizeof text - cursor + i);
+ cursor -= i;
+ match(text);
break;
case XK_Delete:
- 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);
+ if(cursor == len)
+ return;
+ for(i = 1; cursor + i < len && !IS_UTF8_1ST_CHAR(text[cursor + i]); i++);
+ memmove(text + cursor, text + cursor + i, sizeof text - cursor);
match(text);
break;
case XK_End:
case XK_Escape:
ret = 1;
running = False;
- break;
+ return;
case XK_Home:
if(sel == item) {
cursor = 0;
calcoffsets();
break;
case XK_Left:
- case XK_Up:
- if(sel && sel->left){
- sel=sel->left;
- if(sel->right == curr) {
- curr = prev;
- calcoffsets();
- }
+ if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
+ while(cursor-- > 0 && !IS_UTF8_1ST_CHAR(text[cursor]));
+ break;
}
- else if(cursor > 0) {
- do {
- cursor--;
- } while(cursor > 0 && !IS_UTF8_1ST_CHAR(text[cursor]));
- } else
+ if(lines > 0)
+ return;
+ case XK_Up:
+ if(!sel || !sel->left)
return;
+ sel = sel->left;
+ if(sel->right == curr) {
+ curr = prev;
+ calcoffsets();
+ }
break;
case XK_Next:
if(!next)
fprintf(stdout, "%s", sel->text);
fflush(stdout);
running = False;
- break;
+ return;
case XK_Right:
- case XK_Down:
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;
- calcoffsets();
- }
+ while(cursor++ < len && !IS_UTF8_1ST_CHAR(text[cursor]));
+ break;
}
- else
+ if(lines > 0)
+ return;
+ case XK_Down:
+ if(!sel || !sel->right)
return;
+ sel = sel->right;
+ if(sel == next) {
+ curr = next;
+ calcoffsets();
+ }
break;
case XK_Tab:
if(!sel)
if(buf[len-1] == '\n')
buf[--len] = '\0';
if(!(p = strdup(buf)))
- eprint("fatal: could not strdup() %u bytes\n", len);
- if(max < len || !maxname) {
+ eprint("dmenu: cannot strdup %u bytes\n", len);
+ if((max = MAX(max, len)) == len)
maxname = p;
- max = len;
- }
- if(!(new = (Item *)malloc(sizeof(Item))))
- eprint("fatal: could not malloc() %u bytes\n", sizeof(Item));
+ if(!(new = malloc(sizeof *new)))
+ eprint("dmenu: cannot malloc %u bytes\n", sizeof *new);
new->next = new->left = new->right = NULL;
new->text = p;
if(!i)
/* main event loop */
while(running && !XNextEvent(dpy, &ev))
switch (ev.type) {
- default: /* ignore all crap */
- break;
case KeyPress:
kpress(&ev.xkey);
break;
if(++i < argc) parent = atoi(argv[i]);
}
else if(!strcmp(argv[i], "-l")) {
- calcoffsets = calcoffsetsv;
if(++i < argc) lines = atoi(argv[i]);
+ if(lines > 0)
+ calcoffsets = calcoffsetsv;
}
else if(!strcmp(argv[i], "-fn")) {
if(++i < argc) font = argv[i];
eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
" [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
- fprintf(stderr, "warning: no locale support\n");
+ fprintf(stderr, "dmenu: warning: no locale support\n");
if(!(dpy = XOpenDisplay(NULL)))
eprint("dmenu: cannot open display\n");
screen = DefaultScreen(dpy);