]> git.mdlowis.com Git - projs/tide.git/commitdiff
check for external changes to file when focus is lost or gained
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 23 May 2017 12:54:16 +0000 (08:54 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 23 May 2017 12:54:16 +0000 (08:54 -0400)
TODO.md
inc/win.h
inc/x11.h
lib/win.c
lib/x11.c
term.c
xedit.c
xpick.c

diff --git a/TODO.md b/TODO.md
index e54929b57937a62f6ba8a7fb135555c9b75661bf..d639ff9b0c9031b26e750683832bfd82663f2fda 100644 (file)
--- 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
index a29a5b4fb1adc80f5bb93ed94be7de3482e96677..a2a8bbac46841b32a9fdee2d0e357bec0270d78c 100644 (file)
--- 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);
index 94986809fc7dfcbb211b5c34cbedd910a6e14dcf..4b60c18336637aa2822e90f8e4f11e2d53d5bbd7 100644 (file)
--- 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;
 
index baeebdfc44653bbb6e9b98e540e1afa70675f0d4..9982cccf8cec3e57b6969f055dc49a57cbd9593e 100644 (file)
--- 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
 };
 
index dbef0b8ba9bccba539c25bcad18412d391e97f21..2292c14d9be0d188c3015d25a7ea336cc22703c6 100644 (file)
--- 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 6ea26a9fcbf279246f721b7e2a4a7d82f54f8f3d..33d589b113d78549ac5d941aaec8a31cb34dc067 100644 (file)
--- 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 51e3ae7a39e5b339e3998c717b4918e405e089ec..2d92983a09d2d5fb1d1ecc6440cd3d6cba4fd137 100644 (file)
--- 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 1454e8e9a255ddd91bcf0ecb5469e9771bbb6f3b..cf117ae59452a9ad59180750b2543930b65cecee 100644 (file)
--- 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, "");