From: Michael D. Lowis Date: Tue, 23 May 2017 12:54:16 +0000 (-0400) Subject: check for external changes to file when focus is lost or gained X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=ea19adcd6edb7eeeff230d8db5846e505737e5a5;p=projs%2Ftide.git check for external changes to file when focus is lost or gained --- diff --git a/TODO.md b/TODO.md index e54929b..d639ff9 100644 --- a/TODO.md +++ b/TODO.md @@ -4,12 +4,10 @@ Up Next: * Run commands in the background and don't block the main thread. * refactor selection handling to buf.c to prepare for multiple selections. -* right click to fetch file or line * Make Fn keys execute nth command in the tags buffers -* check for file changes when window regains focus * 100% coverage with unit and unit-integration tests -* refactor x11.c and win.c * Status line should omit characters from beginning of path to make file path fit +* right click to fetch file or line Straight-up Bugs: @@ -22,6 +20,20 @@ The Future: * add command env vars to set options (Tabs, Indent, etc..) * implement command diffing logic to optimize the undo/redo log +# Mouse Chords + +Mouse Sequence Description +-------------------------------------------------------------------------------- +Pl Rl Null Selection +Pl Rl Pl Rl Select by context +Pl Rl Pl Rl Pl Rl Select big word +Pl Pm Cut line or selection +Pl Pr Paste +Pm Rm Execute text +Pm Pr Cancel the execution that would occur +Pr Rr Search +Pr Pm Cancel the search that would occur + # Auxillary Programs * Visual diff tool diff --git a/inc/win.h b/inc/win.h index a29a5b4..a2a8bba 100644 --- a/inc/win.h +++ b/inc/win.h @@ -51,6 +51,7 @@ void win_setscroll(double offset, double visible); /* These functions must be implemented by any appliation that wishes to use this module */ void onshutdown(void); +void onfocus(bool focused); void onupdate(void); void onlayout(void); void onscroll(double percent); diff --git a/inc/x11.h b/inc/x11.h index 9498680..4b60c18 100644 --- a/inc/x11.h +++ b/inc/x11.h @@ -19,6 +19,7 @@ typedef struct { void (*handle_key)(int mods, uint32_t rune); void (*handle_mouse)(MouseAct act, MouseBtn btn, int x, int y); void (*shutdown)(void); + void (*set_focus)(bool focus); uint32_t palette[16]; } XConfig; diff --git a/lib/win.c b/lib/win.c index baeebdf..9982ccc 100644 --- a/lib/win.c +++ b/lib/win.c @@ -29,6 +29,7 @@ static XConfig Config = { .handle_key = oninput, .handle_mouse = onmouse, .shutdown = onshutdown, + .set_focus = onfocus, .palette = COLOR_PALETTE }; diff --git a/lib/x11.c b/lib/x11.c index dbef0b8..2292c14 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -136,6 +136,7 @@ void x11_window(char* name, int width, int height) { | ButtonReleaseMask | ButtonMotionMask | KeyPressMask + | FocusChangeMask ); /* set input methods */ @@ -306,16 +307,27 @@ static void handle_mouse(XEvent* e) { Config->handle_mouse(action, button, x, y); } +static void set_focus(bool focused) { + if (focused) { + if (X.xic) XSetICFocus(X.xic); + } else { + if (X.xic) XUnsetICFocus(X.xic); + } + Config->set_focus(focused); +} + void x11_handle_event(XEvent* e) { Atom wmDeleteMessage = XInternAtom(X.display, "WM_DELETE_WINDOW", False); switch (e->type) { - case KeyPress: handle_key(e); break; - case ButtonRelease: handle_mouse(e); break; - case ButtonPress: handle_mouse(e); break; - case MotionNotify: handle_mouse(e); break; - case SelectionClear: selclear(e); break; - case SelectionNotify: selnotify(e); break; - case SelectionRequest: selrequest(e); break; + case FocusIn: set_focus(true); break; + case FocusOut: set_focus(false); break; + case KeyPress: handle_key(e); break; + case ButtonRelease: handle_mouse(e); break; + case ButtonPress: handle_mouse(e); break; + case MotionNotify: handle_mouse(e); break; + case SelectionClear: selclear(e); break; + case SelectionNotify: selnotify(e); break; + case SelectionRequest: selrequest(e); break; case ClientMessage: if (e->xclient.data.l[0] == wmDeleteMessage) Config->shutdown(); diff --git a/term.c b/term.c index 6ea26a9..33d589b 100644 --- a/term.c +++ b/term.c @@ -21,6 +21,9 @@ void onscroll(double percent) { } +void onfocus(bool focused) { +} + void onupdate(void) { } diff --git a/xedit.c b/xedit.c index 51e3ae7..2d92983 100644 --- a/xedit.c +++ b/xedit.c @@ -149,15 +149,13 @@ static void quit(void) { before = now; } -static void save(void) { - Buf* buf = win_buf(EDIT); - if (buf->modtime != modtime(buf->path)) { +static bool changed_externally(Buf* buf) { + bool modified = (buf->modtime != modtime(buf->path)); + if (modified) { view_append(win_view(TAGS), - "File modified externally: Reload or Overwrite"); - } else { - trim_whitespace(); - buf_save(win_buf(EDIT)); + "File modified externally: Reload, Overwrite, or {SaveAs }"); } + return modified; } static void overwrite(void) { @@ -165,6 +163,11 @@ static void overwrite(void) { buf_save(win_buf(EDIT)); } +static void save(void) { + if (!changed_externally(win_buf(EDIT))) + overwrite(); +} + static void reload(void) { view_reload(win_view(EDIT)); } @@ -455,6 +458,11 @@ void onscroll(double percent) { view_scrollto(win_view(EDIT), (off >= bend ? bend : off)); } +void onfocus(bool focused) { + /* notify the user if the file has changed externally */ + (void)changed_externally(win_buf(EDIT)); +} + void onupdate(void) { static char status_bytes[256]; memset(status_bytes, 0, sizeof(status_bytes)); diff --git a/xpick.c b/xpick.c index 1454e8e..cf117ae 100644 --- a/xpick.c +++ b/xpick.c @@ -127,6 +127,9 @@ void onscroll(double percent) { ChoiceIdx = vec_size(&Choices)-1; } +void onfocus(bool focused) { +} + void onupdate(void) { win_setregion(TAGS); win_settext(EDIT, "");