2008-10-14 05:38:03 +04:00
|
|
|
#include "dat.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2008-10-16 07:59:17 +04:00
|
|
|
#include <unistd.h>
|
2008-10-14 05:38:03 +04:00
|
|
|
#include "fns.h"
|
|
|
|
|
|
|
|
static Window* barwin;
|
|
|
|
static Handlers handlers;
|
|
|
|
|
|
|
|
static int ltwidth;
|
|
|
|
static int numlock;
|
|
|
|
|
|
|
|
static void menu_draw(void);
|
|
|
|
|
|
|
|
enum {
|
2008-10-15 00:57:48 +04:00
|
|
|
ACCEPT = CARET_LAST,
|
2008-10-14 05:38:03 +04:00
|
|
|
REJECT,
|
2008-10-16 07:15:42 +04:00
|
|
|
HIST,
|
2008-10-15 00:57:48 +04:00
|
|
|
KILL,
|
2008-10-14 05:38:03 +04:00
|
|
|
CMPL_NEXT,
|
|
|
|
CMPL_PREV,
|
|
|
|
CMPL_FIRST,
|
|
|
|
CMPL_LAST,
|
|
|
|
CMPL_NEXT_PAGE,
|
|
|
|
CMPL_PREV_PAGE,
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
menu_init(void) {
|
|
|
|
WinAttr wa;
|
|
|
|
|
|
|
|
wa.override_redirect = 1;
|
|
|
|
wa.background_pixmap = ParentRelative;
|
|
|
|
wa.event_mask = ExposureMask | KeyPressMask;
|
2008-10-16 07:59:17 +04:00
|
|
|
barwin = createwindow(&scr.root, Rect(-1, -1, 1, 1), scr.depth, InputOutput,
|
2008-10-14 05:38:03 +04:00
|
|
|
&wa, CWOverrideRedirect
|
|
|
|
| CWBackPixmap
|
|
|
|
| CWEventMask);
|
|
|
|
sethandler(barwin, &handlers);
|
2008-10-16 07:59:17 +04:00
|
|
|
mapwin(barwin);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
while(!grabkeyboard(barwin)) {
|
|
|
|
if(i++ > 1000)
|
|
|
|
fatal("can't grab keyboard");
|
|
|
|
usleep(1000);
|
|
|
|
}
|
2008-10-14 05:38:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
menu_unmap(long id, void *p) {
|
|
|
|
|
|
|
|
USED(id, p);
|
|
|
|
unmapwin(barwin);
|
|
|
|
XFlush(display);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(int op, int motion) {
|
2008-10-16 07:15:42 +04:00
|
|
|
int n;
|
2008-10-14 05:38:03 +04:00
|
|
|
|
|
|
|
switch(op) {
|
2008-10-16 07:15:42 +04:00
|
|
|
case HIST:
|
|
|
|
n = input.pos - input.string;
|
|
|
|
caret_insert(history_search(motion, input.string, n), true);
|
|
|
|
input.pos = input.string + n;
|
2008-10-14 05:38:03 +04:00
|
|
|
break;
|
2008-10-15 00:57:48 +04:00
|
|
|
case KILL:
|
|
|
|
caret_delete(BACKWARD, motion);
|
2008-10-14 05:38:03 +04:00
|
|
|
break;
|
2008-10-14 08:02:30 +04:00
|
|
|
default:
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
update_filter();
|
|
|
|
next:
|
|
|
|
switch(op) {
|
|
|
|
case ACCEPT:
|
|
|
|
srv.running = false;
|
2008-10-15 03:00:58 +04:00
|
|
|
if(matchidx->retstring && !motion)
|
2008-10-14 08:02:30 +04:00
|
|
|
print("%s", matchidx->retstring);
|
|
|
|
else
|
2008-10-15 03:00:58 +04:00
|
|
|
print("%s", input.string);
|
|
|
|
|
2008-10-14 08:02:30 +04:00
|
|
|
break;
|
|
|
|
case REJECT:
|
|
|
|
srv.running = false;
|
|
|
|
result = 1;
|
|
|
|
break;
|
2008-10-15 00:57:48 +04:00
|
|
|
case BACKWARD:
|
|
|
|
case FORWARD:
|
|
|
|
caret_move(op, motion);
|
|
|
|
break;
|
2008-10-14 05:38:03 +04:00
|
|
|
case CMPL_NEXT:
|
|
|
|
matchidx = matchidx->next;
|
|
|
|
break;
|
|
|
|
case CMPL_PREV:
|
|
|
|
matchidx = matchidx->prev;
|
|
|
|
break;
|
|
|
|
case CMPL_FIRST:
|
2008-10-14 08:02:30 +04:00
|
|
|
matchstart = matchfirst;
|
|
|
|
matchidx = nil;
|
|
|
|
matchend = nil;
|
2008-10-14 05:38:03 +04:00
|
|
|
break;
|
|
|
|
case CMPL_LAST:
|
|
|
|
matchidx = matchfirst->prev;
|
|
|
|
break;
|
|
|
|
case CMPL_NEXT_PAGE:
|
2008-10-14 08:02:30 +04:00
|
|
|
matchidx = matchend->next;
|
2008-10-14 05:38:03 +04:00
|
|
|
break;
|
|
|
|
case CMPL_PREV_PAGE:
|
|
|
|
matchend = matchstart->prev;
|
2008-10-14 08:02:30 +04:00
|
|
|
matchidx = nil;
|
2008-10-14 05:38:03 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
menu_draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
menu_draw(void) {
|
|
|
|
Rectangle r, r2;
|
|
|
|
CTuple *c;
|
|
|
|
Item *i;
|
2008-10-14 08:02:30 +04:00
|
|
|
int inputw, itemoff, end, pad, n;
|
2008-10-14 05:38:03 +04:00
|
|
|
|
|
|
|
r = barwin->r;
|
|
|
|
r = rectsetorigin(r, ZP);
|
|
|
|
r2 = r;
|
|
|
|
|
2008-10-14 08:02:30 +04:00
|
|
|
pad = (font->height & ~1);
|
2008-10-17 00:52:42 +04:00
|
|
|
inputw = min(Dx(r) / 3, maxwidth);
|
|
|
|
inputw = max(inputw, textwidth(font, input.string)) + pad;
|
2008-10-14 05:38:03 +04:00
|
|
|
itemoff = inputw + 2 * ltwidth;
|
|
|
|
end = Dx(r) - ltwidth;
|
|
|
|
|
|
|
|
fill(ibuf, r, cnorm.bg);
|
|
|
|
|
2008-10-14 08:02:30 +04:00
|
|
|
if(matchend && matchidx == matchend->next)
|
|
|
|
matchstart = matchidx;
|
|
|
|
else if(matchidx == matchstart->prev)
|
|
|
|
matchend = matchidx;
|
|
|
|
|
|
|
|
if(matchend == matchstart->prev && matchstart != matchidx) {
|
|
|
|
n = itemoff;
|
|
|
|
matchstart = matchend;
|
|
|
|
for(i=matchend; ; i=i->prev) {
|
|
|
|
n += i->width + pad;
|
|
|
|
if(n > end)
|
|
|
|
break;
|
|
|
|
matchstart = i;
|
|
|
|
if(i == matchfirst)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(matchidx == nil)
|
|
|
|
matchidx = matchstart;
|
|
|
|
|
2008-10-14 05:38:03 +04:00
|
|
|
for(i=matchstart; i->string; i=i->next) {
|
|
|
|
r2.min.x = itemoff;
|
|
|
|
itemoff = itemoff + i->width + pad;
|
|
|
|
r2.max.x = min(itemoff, end);
|
|
|
|
if(i != matchstart && itemoff > end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
c = (i == matchidx) ? &csel : &cnorm;
|
|
|
|
fill(ibuf, r2, c->bg);
|
|
|
|
drawstring(ibuf, font, r2, Center, i->string, c->fg);
|
|
|
|
matchend = i;
|
|
|
|
if(i->next == matchfirst)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
r2 = r;
|
|
|
|
r2.min.x = inputw;
|
|
|
|
if(matchstart != matchfirst)
|
|
|
|
drawstring(ibuf, font, r2, West, "<", cnorm.fg);
|
|
|
|
if(matchend->next != matchfirst)
|
|
|
|
drawstring(ibuf, font, r2, East, ">", cnorm.fg);
|
|
|
|
r2 = r;
|
|
|
|
r2.max.x = inputw;
|
2008-10-15 00:57:48 +04:00
|
|
|
drawstring(ibuf, font, r2, West, input.string, cnorm.fg);
|
2008-10-14 05:38:03 +04:00
|
|
|
|
2008-10-16 07:15:42 +04:00
|
|
|
r2.min.x = textwidth_l(font, input.string, input.pos - input.string) + pad/2 - 1;
|
2008-10-14 05:38:03 +04:00
|
|
|
r2.max.x = r2.min.x + 2;
|
|
|
|
r2.min.y++;
|
|
|
|
r2.max.y--;
|
|
|
|
border(ibuf, r2, 1, cnorm.border);
|
|
|
|
|
|
|
|
border(ibuf, r, 1, cnorm.border);
|
|
|
|
copyimage(barwin, r, ibuf, ZP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
menu_show(void) {
|
|
|
|
Rectangle r;
|
|
|
|
int height, pad;
|
|
|
|
|
|
|
|
USED(menu_unmap);
|
|
|
|
|
2008-10-16 07:59:17 +04:00
|
|
|
ltwidth = textwidth(font, "<");
|
|
|
|
|
2008-10-14 05:38:03 +04:00
|
|
|
pad = (font->height & ~1)/2;
|
|
|
|
height = font->height + 2;
|
|
|
|
|
|
|
|
r = scr.rect;
|
|
|
|
if(ontop)
|
|
|
|
r.max.y = r.min.y + height;
|
|
|
|
else
|
|
|
|
r.min.y = r.max.y - height;
|
|
|
|
reshapewin(barwin, r);
|
|
|
|
|
|
|
|
freeimage(ibuf);
|
|
|
|
ibuf = allocimage(Dx(r), Dy(r), scr.depth);
|
|
|
|
|
|
|
|
mapwin(barwin);
|
|
|
|
raisewin(barwin);
|
|
|
|
menu_draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kdown_event(Window *w, XKeyEvent *e) {
|
|
|
|
char buf[32];
|
2008-10-15 00:57:48 +04:00
|
|
|
int num;
|
2008-10-14 05:38:03 +04:00
|
|
|
KeySym ksym;
|
|
|
|
|
|
|
|
buf[0] = 0;
|
|
|
|
num = XLookupString(e, buf, sizeof buf, &ksym, 0);
|
|
|
|
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)
|
|
|
|
|| IsMiscFunctionKey(ksym)
|
2008-10-16 00:08:56 +04:00
|
|
|
|| IsKeypadKey(ksym)
|
2008-10-16 05:16:14 +04:00
|
|
|
|| IsPrivateKeypadKey(ksym)
|
|
|
|
|| IsPFKey(ksym))
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(e->state & ControlMask) {
|
|
|
|
switch (ksym) {
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
case XK_bracketleft: /* Esc */
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(REJECT, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_j:
|
|
|
|
case XK_J:
|
|
|
|
case XK_m:
|
|
|
|
case XK_M:
|
2008-10-15 03:00:58 +04:00
|
|
|
menu_cmd(ACCEPT, e->state&ShiftMask);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
2008-10-20 02:46:02 +04:00
|
|
|
case XK_a:
|
|
|
|
case XK_A:
|
|
|
|
menu_cmd(BACKWARD, LINE);
|
|
|
|
return;
|
|
|
|
case XK_e:
|
|
|
|
case XK_E:
|
|
|
|
menu_cmd(FORWARD, LINE);
|
|
|
|
return;
|
2008-10-14 05:38:03 +04:00
|
|
|
case XK_n:
|
|
|
|
case XK_N:
|
2008-10-16 07:15:42 +04:00
|
|
|
menu_cmd(HIST, FORWARD);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_p:
|
|
|
|
case XK_P:
|
2008-10-16 07:15:42 +04:00
|
|
|
menu_cmd(HIST, BACKWARD);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_h:
|
|
|
|
case XK_H:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(KILL, CHAR);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
2008-10-15 00:57:48 +04:00
|
|
|
case XK_BackSpace:
|
2008-10-14 05:38:03 +04:00
|
|
|
case XK_w:
|
|
|
|
case XK_W:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(KILL, WORD);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_u:
|
|
|
|
case XK_U:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(KILL, LINE);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
2008-10-16 00:08:56 +04:00
|
|
|
case XK_i: /* Tab */
|
|
|
|
case XK_I:
|
|
|
|
if(e->state & ShiftMask)
|
|
|
|
menu_cmd(CMPL_PREV, 0);
|
|
|
|
else
|
|
|
|
menu_cmd(CMPL_NEXT, 0);
|
|
|
|
return;
|
2008-10-14 05:38:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Alt-<Key> - Vim */
|
|
|
|
if((e->state & ~(numlock | LockMask)) & Mod1Mask) {
|
|
|
|
switch(ksym) {
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
case XK_h:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(CMPL_PREV, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_l:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(CMPL_NEXT, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_k:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(CMPL_PREV_PAGE, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
2008-10-16 00:08:56 +04:00
|
|
|
case XK_j:
|
|
|
|
menu_cmd(CMPL_NEXT_PAGE, 0);
|
|
|
|
return;
|
2008-10-14 05:38:03 +04:00
|
|
|
case XK_g:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(CMPL_FIRST, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_G:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(CMPL_LAST, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch(ksym) {
|
|
|
|
default:
|
|
|
|
if(num && !iscntrl(buf[0])) {
|
2008-10-15 00:57:48 +04:00
|
|
|
caret_insert(buf, false);
|
2008-10-14 05:38:03 +04:00
|
|
|
update_filter();
|
|
|
|
menu_draw();
|
|
|
|
}
|
|
|
|
break;
|
2008-10-16 00:08:56 +04:00
|
|
|
case XK_Tab:
|
|
|
|
if(e->state & ShiftMask)
|
|
|
|
menu_cmd(CMPL_PREV, 0);
|
|
|
|
else
|
|
|
|
menu_cmd(CMPL_NEXT, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_Return:
|
2008-10-16 00:08:56 +04:00
|
|
|
menu_cmd(ACCEPT, e->state & ShiftMask);
|
|
|
|
return;
|
|
|
|
case XK_Escape:
|
|
|
|
menu_cmd(REJECT, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_BackSpace:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(KILL, CHAR);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
2008-10-16 00:08:56 +04:00
|
|
|
case XK_Left:
|
|
|
|
menu_cmd(BACKWARD, CHAR);
|
|
|
|
return;
|
|
|
|
case XK_Right:
|
|
|
|
menu_cmd(FORWARD, CHAR);
|
|
|
|
return;
|
2008-10-14 05:38:03 +04:00
|
|
|
case XK_Up:
|
2008-10-16 07:15:42 +04:00
|
|
|
menu_cmd(HIST, BACKWARD);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_Down:
|
2008-10-16 07:15:42 +04:00
|
|
|
menu_cmd(HIST, FORWARD);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_Home:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(CMPL_FIRST, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_End:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(CMPL_LAST, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
case XK_Prior:
|
2008-10-15 00:57:48 +04:00
|
|
|
menu_cmd(CMPL_PREV_PAGE, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
2008-10-16 00:08:56 +04:00
|
|
|
case XK_Next:
|
|
|
|
menu_cmd(CMPL_NEXT_PAGE, 0);
|
2008-10-14 05:38:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
expose_event(Window *w, XExposeEvent *e) {
|
|
|
|
|
|
|
|
USED(w);
|
|
|
|
menu_draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Handlers handlers = {
|
|
|
|
.expose = expose_event,
|
|
|
|
.kdown = kdown_event,
|
|
|
|
};
|
|
|
|
|