From: Michael D. Lowis Date: Wed, 9 Jan 2019 21:20:24 +0000 (-0500) Subject: minor refactoring, added xerror handler, and fixed a bug in pick that allowed selecti... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=20872e25f1899c4a847e113da79e4abcc0a9d957;p=projs%2Ftide.git minor refactoring, added xerror handler, and fixed a bug in pick that allowed selecting an invalid index --- diff --git a/TODO.md b/TODO.md index 6c02eef..05821e2 100644 --- a/TODO.md +++ b/TODO.md @@ -8,7 +8,6 @@ * registrar: group by hostname or group env var in registrar * registrar: should remove invalid windows from registry when detected * tide: gap buffer does not handle UTF-8 currently -* pick can return null if mouse clicked on empty row ## BACKLOG diff --git a/inc/x11.h b/inc/x11.h index cd85b1b..a89a8b1 100644 --- a/inc/x11.h +++ b/inc/x11.h @@ -3,7 +3,7 @@ #include typedef struct XConf { - Bool running; + Bool running, error; int fd, screen, width, height; Window root; Display* display; @@ -27,6 +27,9 @@ enum { }; int x11_init(XConf* x); +void x11_clear_error(void); +XErrorEvent* x11_error_get(void); + void x11_mkwin(XConf* x, int width, int height, int evmask); void x11_mkdialog(XConf* x, int width, int height, int evmask); void x11_event_loop(XConf* x, void (*redraw)(XConf* x)); diff --git a/src/lib/x11.c b/src/lib/x11.c index 69807dc..cb36742 100644 --- a/src/lib/x11.c +++ b/src/lib/x11.c @@ -4,6 +4,15 @@ #include #include +static Bool Has_Error = False; +static XErrorEvent Error = {0}; + +static int onerror(Display* disp, XErrorEvent* ev) { + (void)disp; + Has_Error = True, Error = *ev; + return 0; +} + int x11_init(XConf* x) { signal(SIGPIPE, SIG_IGN); // Ignore the SIGPIPE signal setlocale(LC_CTYPE, ""); @@ -19,9 +28,19 @@ int x11_init(XConf* x) { x->screen = DefaultScreen(x->display); x->depth = DefaultDepth(x->display, x->screen); x->running = True; + XSetErrorHandler(onerror); return 0; } +void x11_clear_error(void) { + Has_Error = False; + memset(&Error, 0, sizeof(Error)); +} + +XErrorEvent* x11_error_get(void) { + return (Has_Error ? &Error : NULL); +} + void x11_mkwin(XConf* x, int width, int height, int evmask) { /* create the main window */ x->width = width, x->height = height; diff --git a/src/pick.c b/src/pick.c index 947f3e5..1ec7051 100644 --- a/src/pick.c +++ b/src/pick.c @@ -147,6 +147,8 @@ static void xbtnpress(XConf* x, XEvent* e) { int starty = x->font->height + 4; e->xbutton.y = (e->xbutton.y < starty ? starty : e->xbutton.y - starty); ChoiceIdx = Offset + (e->xbutton.y / x->font->height); + if (ChoiceIdx >= vec_size(&Choices)) + ChoiceIdx = vec_size(&Choices)-1; } else if (e->xbutton.button == Button4) { if (Offset > 0) Offset--; } else if (e->xbutton.button == Button5) { diff --git a/src/tide.c b/src/tide.c index 626a0f6..ce18744 100644 --- a/src/tide.c +++ b/src/tide.c @@ -16,7 +16,7 @@ #include "config.h" /* predeclare some things */ -void exec(char* cmd, char* arg); +static void exec(char* cmd, char* arg); void cut(char* arg); void paste(char* arg); @@ -435,13 +435,7 @@ static void cmd_exec(char* cmd) { job_start(execcmd, input, len, (op != '<' ? curr : edit)); } -static void cmd_execwitharg(char* cmd, char* arg) { - cmd = (arg ? strmcat(cmd, " '", arg, "'", 0) : strmcat(cmd)); - cmd_exec(cmd); - free(cmd); -} - -void exec(char* cmd, char* arg) { +static void exec(char* cmd, char* arg) { /* skip leading space */ for (; *cmd && isspace(*cmd); cmd++); if (!*cmd) return; @@ -706,7 +700,7 @@ static void open_file(char* arg) { } static void pick_symbol(char* symbol) { - cmd_execwitharg(CMD_GOTO_TAG, symbol); + exec(CMD_GOTO_TAG, symbol); } static void pick_ctag(char* arg) { @@ -718,14 +712,14 @@ static void complete(char* arg) { (void)arg; View* view = win_view(FOCUSED); view_selectobj(view, risword); - cmd_execwitharg(CMD_COMPLETE, view_getstr(view)); + exec(CMD_COMPLETE, view_getstr(view)); } static void fcomplete(char* arg) { (void)arg; View* view = win_view(FOCUSED); view_selectobj(view, risfile); - cmd_execwitharg(CMD_FCOMPLETE, view_getstr(view)); + exec(CMD_FCOMPLETE, view_getstr(view)); } static void jump_to(char* arg) {