From: Michael D. Lowis Date: Mon, 31 Dec 2018 21:30:24 +0000 (-0500) Subject: added a force flag to edit command to force launch tide without checking registrar X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=052ef66e46f6e6d78addc05ad3eb3fa83a52e211;p=projs%2Ftide.git added a force flag to edit command to force launch tide without checking registrar --- diff --git a/TODO.md b/TODO.md index cee6d02..43cc9b0 100644 --- a/TODO.md +++ b/TODO.md @@ -15,7 +15,7 @@ * tide: should re-register with the registrar when a new registrar is launched * tide: Line - Get the current line number(s) containing the selection * tide: gap buffer does not handle UTF-8 currently -* edit: should have a force option to launch tide without registrar notification +* edit: hangs after launching an empty tide instance then trying to open already open file ## BACKLOG diff --git a/src/edit.c b/src/edit.c index 0d8a04e..4c92598 100644 --- a/src/edit.c +++ b/src/edit.c @@ -5,6 +5,7 @@ #include "config.h" +char* ARGV0; Atom XA_REGISTRAR, XA_OPEN, XA_DONE; int spawn(char* cmd) { @@ -30,7 +31,7 @@ void prop_set(XConf* x, Window win, char* prop, char* value) { (const unsigned char *)value, strlen(value)+1); } -void edit_file(XConf* x, Window registrar, char* path, char* addr) { +void edit_file(XConf* x, Window registrar, char* path, char* addr, int force) { char* rpath = realpath(path, NULL); prop_set(x, x->self, "FILE", (rpath ? rpath : path)); prop_set(x, x->self, "ADDR", addr); @@ -38,41 +39,47 @@ void edit_file(XConf* x, Window registrar, char* path, char* addr) { XChangeProperty( x->display, registrar, XA_OPEN, XA_WINDOW, 32, PropModeAppend, (const unsigned char *)&(x->self), 1); - /* wait for the "done" message */ - for (XEvent e;;) { - XNextEvent(x->display, &e); - if (e.type == ClientMessage) { - if (e.xclient.message_type == XA_DONE) { - break; - } else if (e.xclient.message_type == XA_OPEN) { - if (!fork()) { - EditCmd[2] = addr, EditCmd[3] = path; - exit(execvp(EditCmd[0], EditCmd)); + EditCmd[2] = addr, EditCmd[3] = path; + if (force) { + if (!fork()) + exit(execvp(EditCmd[0], EditCmd)); + } else { + /* wait for the "done" message */ + for (XEvent e;;) { + XNextEvent(x->display, &e); + if (e.type == ClientMessage) { + if (e.xclient.message_type == XA_DONE) { + break; + } else if (e.xclient.message_type == XA_OPEN) { + if (!fork()) + exit(execvp(EditCmd[0], EditCmd)); + break; } - break; } } } } int main(int argc, char** argv) { - XConf x = {0}; - x11_init(&x); - x11_mkwin(&x, 1, 1, 0); - XA_REGISTRAR = XInternAtom(x.display, "TIDE_REGISTRAR", PropertyChangeMask); - XA_OPEN = XInternAtom(x.display, "OPEN", 0); - XA_DONE = XInternAtom(x.display, "DONE", 0); - Window registrar = start_registrar(&x); - if (argc == 1) { + int force = 0; + OPTBEGIN { case 'f': force = 1; break; } OPTEND; + if (argc == 0) { spawn("tide"); } else { + XConf x = {0}; + x11_init(&x); + x11_mkwin(&x, 1, 1, 0); + XA_REGISTRAR = XInternAtom(x.display, "TIDE_REGISTRAR", PropertyChangeMask); + XA_OPEN = XInternAtom(x.display, "OPEN", 0); + XA_DONE = XInternAtom(x.display, "DONE", 0); + Window registrar = start_registrar(&x); /* Loop over files and send an OPEN message for each one. */ - for (int i = 1; i < argc; i++) { + for (int i = 0; i < argc; i++) { char* addr = strrchr(argv[i], ':'); if (addr) *addr = '\0', addr++; - edit_file(&x, registrar, argv[i], (addr ? addr : "0")); + edit_file(&x, registrar, argv[i], (addr ? addr : "0"), force); } + XSync(x.display, False); } - XSync(x.display, False); return 0; } diff --git a/src/registrar.c b/src/registrar.c index fc0c4ed..9b69ee7 100644 --- a/src/registrar.c +++ b/src/registrar.c @@ -109,8 +109,7 @@ void propnotify(XConf* x, XEvent* e) { } } -int daemonize(void) -{ +int daemonize(void) { int status; /* fork into the background first */ if ((status = fork()) < 0)