From 9985c9c1ae407c3643f1f5f6b5be0ada109ed9ea Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 10 Dec 2018 23:08:42 -0500 Subject: [PATCH] ignore warnings about initializers and move logic for opening files from registrar to the edit command. --- TODO.md | 5 ++--- config.h | 11 ++++------- config.mk | 1 + src/edit.c | 19 +++++++++++++------ src/pick.c | 2 +- src/registrar.c | 33 ++++++++++++--------------------- 6 files changed, 33 insertions(+), 38 deletions(-) diff --git a/TODO.md b/TODO.md index e26d92b..ea22584 100644 --- a/TODO.md +++ b/TODO.md @@ -2,11 +2,11 @@ ## STAGING +* registrar doesnt match open windows when new file created and is then opened for edit or line number +* fetch should stuff match strings in environment variables instead of evaling * registrar should remove invalid windows from registry when detected * tide should re-register with the registrar when a new registrar is launched * edit command should have a force option to launch tide without registrar notification -* edit command should launch tide instead of registrar -* fetch should stuff match strings in environment variables instead of evaling * Line - Get the current line number(s) containing the selection * gap buffer does not handle UTF-8 currently @@ -14,7 +14,6 @@ * refactor selection handling to avoid swapping manually (use buf_selbeg and buf_selend) * encode EOL setting in log entries? -* centering logic in view.c seems slightly broken ## BACKLOG diff --git a/config.h b/config.h index 3e71db8..65c73f2 100644 --- a/config.h +++ b/config.h @@ -90,11 +90,10 @@ Rule* BuiltinRules[] = { (Rule[]){ /* Match URLS and open them with the browser */ { ISSET, "BROWSER", NULL }, { MATCHES, "data", "^(https?|ftp)://.*" }, - { LAUNCH, "$BROWSER $0", NULL }, + { LAUNCH, "$BROWSER \"$0\"", NULL }, { COMPLETE, NULL, NULL } }, (Rule[]){ /* Open files with addresses in the editor */ - { ISSET, "EDITOR", NULL }, { MATCHES, "data", "^([^:]+):([0-9]+)" }, { ISFILE, "$1", NULL }, { LAUNCH, "edit \"$0\"", NULL }, @@ -103,23 +102,21 @@ Rule* BuiltinRules[] = { (Rule[]){ /* If it's an existing text file, open it with editor */ { ISSET, "EDITOR", NULL }, { ISFILE, "$data", NULL }, - { EXEC, "file --mime '$file' | grep -q 'text/'", NULL }, - { LAUNCH, "$EDITOR '$file'", NULL }, + { EXEC, "file --mime \"$file\" | grep -q 'text/'", NULL }, + { LAUNCH, "edit \"$file\"", NULL }, { COMPLETE, NULL, NULL } }, (Rule[]){ /* Look it up in ctags database */ - { ISSET, "EDITOR", NULL }, { ISFILE, "tags", NULL }, { EXEC, "grep -q '^$data\\s\\+' tags", NULL }, { LAUNCH, "picktag fetch tags '$data' | xargs -r edit", NULL }, { COMPLETE, NULL, NULL } }, (Rule[]){ /* Look up .c or .h files in Code/ */ - { ISSET, "EDITOR", NULL }, { MATCHES, "data", "\\.[ch]$" }, { ISDIR, "Code", NULL }, { EXEC, "[[ $(find Code -type f -name '*$data') ]]", NULL }, - { LAUNCH, "find Code -type f -name '*$data' | xargs -r $EDITOR", NULL }, + { LAUNCH, "find Code -type f -name '*$data' | xargs -r edit", NULL }, { COMPLETE, NULL, NULL } }, (Rule[]){ /* If it's an existing directory, open it with system default */ diff --git a/config.mk b/config.mk index 6c80145..f203e4f 100644 --- a/config.mk +++ b/config.mk @@ -22,6 +22,7 @@ CFLAGS = -g -MMD $(INCS) CFLAGS += --std=c99 -pedantic CFLAGS += -Wall -Wextra CFLAGS += -Werror +CFLAGS += -Wno-missing-field-initializers # Linker Setup LD = $(CC) diff --git a/src/edit.c b/src/edit.c index 3ea9c0b..0d8a04e 100644 --- a/src/edit.c +++ b/src/edit.c @@ -3,6 +3,8 @@ #include #include +#include "config.h" + Atom XA_REGISTRAR, XA_OPEN, XA_DONE; int spawn(char* cmd) { @@ -29,11 +31,7 @@ void prop_set(XConf* x, Window win, char* prop, char* value) { } void edit_file(XConf* x, Window registrar, char* path, char* addr) { - static char wdirbuf[32768] = {0}; - char* wdir = getcwd(wdirbuf, sizeof(wdirbuf)); - if (!wdir) return; char* rpath = realpath(path, NULL); - prop_set(x, x->self, "WDIR", wdir); prop_set(x, x->self, "FILE", (rpath ? rpath : path)); prop_set(x, x->self, "ADDR", addr); free(rpath); @@ -43,8 +41,17 @@ void edit_file(XConf* x, Window registrar, char* path, char* addr) { /* wait for the "done" message */ for (XEvent e;;) { XNextEvent(x->display, &e); - if (e.type == ClientMessage && e.xclient.message_type == XA_DONE) - break; + 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)); + } + break; + } + } } } diff --git a/src/pick.c b/src/pick.c index b0edf24..b932e43 100644 --- a/src/pick.c +++ b/src/pick.c @@ -172,4 +172,4 @@ int main(void) { if (vec_size(&Choices) && ChoiceIdx != SIZE_MAX) printf("%s\n", choice->string); return 0; -} \ No newline at end of file +} diff --git a/src/registrar.c b/src/registrar.c index 8b744b9..d0d6be5 100644 --- a/src/registrar.c +++ b/src/registrar.c @@ -8,16 +8,16 @@ typedef struct TWindow { char* path; } TWindow; -Atom XA_REGISTRAR, XA_ADD, XA_DEL, XA_OPEN, XA_DONE, XA_WDIR, XA_FILE, XA_ADDR; +Atom XA_REGISTRAR, XA_ADD, XA_DEL, XA_OPEN, XA_DONE; TWindow* Windows = NULL; -char* readprop(XConf* x, Window win, Atom prop, size_t* length) { - Atom type; +char* readprop(XConf* x, Window win, char* prop, size_t* length) { + Atom type, xa_prop = XInternAtom(x->display, prop, False); int format; unsigned long datalen, nleft; unsigned char* data = NULL; XGetWindowProperty( - x->display, win, prop, 0, -1, True, XA_STRING, + x->display, win, xa_prop, 0, -1, True, XA_STRING, &type, &format, &datalen, &nleft, &data); if (length) *length = datalen; return (char*)data; @@ -27,7 +27,7 @@ void win_add(XConf* x, Window id) { TWindow* win = calloc(1, sizeof(TWindow)); win->win = id; win->next = Windows; - win->path = readprop(x, id, XA_FILE, 0); + win->path = readprop(x, id, "FILE", 0); Windows = win; } @@ -60,7 +60,7 @@ void win_send(XConf* x, Window from, Window to, int mask, char* atom, size_t val XFlush(x->display); } -void win_open(XConf* x, char* wdir, char* path, char* addr) { +void win_open(XConf* x, Window winid, char* path, char* addr) { if (!path) return; /* search for an existing window */ for (TWindow* win = Windows; win; win = win->next) { @@ -68,16 +68,13 @@ void win_open(XConf* x, char* wdir, char* path, char* addr) { win_send(x, win->win, x->root, SubstructureRedirectMask|SubstructureNotifyMask, "_NET_ACTIVE_WINDOW", 0); XMapRaised(x->display, win->win); win_send(x, x->self, win->win, 0, "GOTO", strtoul(addr, NULL, 0)); + win_send(x, x->self, winid, 0, "DONE", 0); return; } } - /* if we don't find it, spawn a new one */ - if (!fork()) { - if (wdir) chdir(wdir); - EditCmd[2] = addr, EditCmd[3] = path; - exit(execvp(EditCmd[0], EditCmd)); - } + /* if we don't find it, tell sender to spawn a new one */ + win_send(x, x->self, winid, 0, "OPEN", 0); } void selclear(XConf* x, XEvent* e) { @@ -104,12 +101,9 @@ void propnotify(XConf* x, XEvent* e) { x->display, x->self, XA_OPEN, 0, -1, True, XA_WINDOW, &type, &format, &datalen, &nleft, &data); for (Window* win = (Window*)data; datalen && win && *win; win++, datalen--) { - char* wdir = readprop(x, *win, XA_WDIR, NULL); - char* file = readprop(x, *win, XA_FILE, NULL); - char* addr = readprop(x, *win, XA_ADDR, NULL); - win_open(x, wdir, file, (addr ? addr : "0")); - win_send(x, x->self, *win, 0, "DONE", 0); - if(wdir) XFree(wdir); + char* file = readprop(x, *win, "FILE", NULL); + char* addr = readprop(x, *win, "ADDR", NULL); + win_open(x, *win, file, (addr ? addr : "0")); if(file) XFree(file); if(addr) XFree(addr); } @@ -124,9 +118,6 @@ int main(void) { XA_DEL = XInternAtom(x.display, "DEL", 0); XA_OPEN = XInternAtom(x.display, "OPEN", 0); XA_DONE = XInternAtom(x.display, "DONE", 0); - XA_WDIR = XInternAtom(x.display, "WDIR", 0); - XA_FILE = XInternAtom(x.display, "FILE", 0); - XA_ADDR = XInternAtom(x.display, "ADDR", 0); x.eventfns[SelectionClear] = selclear; x.eventfns[ClientMessage] = clientmsg; x.eventfns[PropertyNotify] = propnotify; -- 2.51.0