]> git.mdlowis.com Git - projs/tide.git/commitdiff
refactored cursor movement handlers to reduce number of functions and table entries
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 30 Nov 2016 00:11:01 +0000 (19:11 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 30 Nov 2016 00:11:01 +0000 (19:11 -0500)
inc/x11.h
libx/x11.c
xedit.c

index 3d3e7886a5b50c2c510a268c7fa9347614ce46d3..62453b8c3c66b3ad65cd708285e1cdb49ab812a2 100644 (file)
--- a/inc/x11.h
+++ b/inc/x11.h
@@ -122,6 +122,8 @@ enum {
 
 void x11_init(XConfig* cfg);
 void x11_deinit(void);
+int x11_keymods(void);
+bool x11_keymodsset(int mask);
 void x11_window(char* name, int width, int height);
 void x11_dialog(char* name, int height, int width);
 void x11_show(void);
index 3300a51df8afba52e0e1cb72d0be5e485e2c3a92..51a44d507e5d0c819ee028be3faa28e4aa2f2289 100644 (file)
@@ -46,6 +46,7 @@ static struct {
     GC gc;
 } X;
 static XConfig* Config;
+static int Mods;
 
 static void xftcolor(XftColor* xc, uint32_t c) {
     xc->color.alpha = 0xFF | ((c & 0xFF000000) >> 16);
@@ -77,6 +78,14 @@ void x11_init(XConfig* cfg) {
     X.depth    = DefaultDepth(X.display, X.screen);
 }
 
+int x11_keymods(void) {
+    return Mods;
+}
+
+bool x11_keymodsset(int mask) {
+    return ((Mods & mask) == mask);
+}
+
 void x11_window(char* name, int width, int height) {
     /* create the main window */
     X.width  = width ;
@@ -213,9 +222,9 @@ static uint32_t getkey(XEvent* e) {
 
 static void handle_key(XEvent* event) {
     uint32_t key = getkey(event);
-    int mods = event->xkey.state & ModAny;
+    Mods = event->xkey.state & ModAny;
     if (key == RUNE_ERR) return;
-    Config->handle_key(mods, key);
+    Config->handle_key(Mods, key);
 }
 
 static void handle_mouse(XEvent* e) {
@@ -228,6 +237,7 @@ static void handle_mouse(XEvent* e) {
         x      = e->xmotion.x;
         y      = e->xmotion.y;
     } else {
+        Mods = e->xbutton.state & ModAny;
         action = (e->type == ButtonPress ? MOUSE_ACT_DOWN : MOUSE_ACT_UP);
         /* set the button id */
         switch (e->xbutton.button) {
diff --git a/xedit.c b/xedit.c
index 73af9f294f54f139923e94e56d7dd1d315e4cc4f..b2acfe4e8be9aa9ea079d20e5f7f7eeacb6c3e77 100644 (file)
--- a/xedit.c
+++ b/xedit.c
@@ -26,12 +26,12 @@ static void redraw(int width, int height);
 
 // UI Callbacks
 static void delete(void);
+static void cursor_home(void);
+static void cursor_end(void);
 static void cursor_up(void);
 static void cursor_dn(void);
 static void cursor_left(void);
 static void cursor_right(void);
-static void cursor_bol(void);
-static void cursor_eol(void);
 static void page_up(void);
 static void page_dn(void);
 static void select_prev(void);
@@ -52,17 +52,11 @@ static void delword_right(void);
 static void delrune_left(void);
 static void delword_left(void);
 static void cursor_bof(void);
-static void select_bol(void);
 static void select_bof(void);
 static void cursor_eof(void);
-static void select_eol(void);
 static void select_eof(void);
-static void select_up(void);
-static void select_dn(void);
-static void select_left(void);
 static void word_left(void);
 static void selword_left(void);
-static void select_right(void);
 static void word_right(void);
 static void selword_right(void);
 
@@ -129,8 +123,8 @@ static KeyBinding Bindings[] = {
     //{ KEY_CTRL_U,    del_to_bol   },
     //{ KEY_CTRL_W,    del_to_bow   },
     //{ KEY_CTRL_H,    del_prev_char},
-    { ModCtrl, 'a', cursor_bol },
-    { ModCtrl, 'e', cursor_eol },
+    { ModCtrl, 'a', cursor_home },
+    { ModCtrl, 'e', cursor_end  },
 
     /* Standard Text Editing Shortcuts */
     { ModCtrl, 's', save  },
@@ -143,45 +137,26 @@ static KeyBinding Bindings[] = {
     /* Common Special Keys */
     { ModNone, KEY_PGUP,      page_up       },
     { ModNone, KEY_PGDN,      page_dn       },
-    { ModNone, KEY_DELETE,    delete        }, // DELETE
-    //{ ModNone, KEY_DELETE,    delrune_right },
-    //{ ModCtrl, KEY_DELETE,    delword_right },
-    //{ ModNone, KEY_BACKSPACE, delrune_left  },
-    //{ ModCtrl, KEY_BACKSPACE, delword_left  },
+    { ModAny,  KEY_DELETE,    delete        },
+    //{ ModAny,  KEY_BACKSPACE, backspace     },
 
     /* Cursor Movements */
-    { ModNone,          KEY_HOME,   cursor_bol    },
-    { ModCtrl,          KEY_HOME,   cursor_bof    },
-    { ModShift,         KEY_HOME,   select_bol    },
-    { ModCtrl|ModShift, KEY_HOME,   select_bof    },
-    { ModNone,          KEY_END,    cursor_eol    },
-    { ModCtrl,          KEY_END,    cursor_eof    },
-    { ModShift,         KEY_END,    select_eol    },
-    { ModCtrl|ModShift, KEY_END,    select_eof    },
-    { ModNone,          KEY_UP,     cursor_up     },
-    { ModShift,         KEY_UP,     select_up     },
-    { ModNone,          KEY_DOWN,   cursor_dn     },
-    { ModShift,         KEY_DOWN,   select_dn     },
-    { ModNone,          KEY_LEFT,   cursor_left   },
-    { ModShift,         KEY_LEFT,   select_left   },
-    { ModCtrl,          KEY_LEFT,   word_left     },
-    { ModCtrl|ModShift, KEY_LEFT,   selword_left  },
-    { ModNone,          KEY_RIGHT,  cursor_right  },
-    { ModShift,         KEY_RIGHT,  select_right  },
-    { ModCtrl,          KEY_RIGHT,  word_right    },
-    { ModCtrl|ModShift, KEY_RIGHT,  selword_right },
+    { ModAny, KEY_HOME,  cursor_home  },
+    { ModAny, KEY_END,   cursor_end   },
+    { ModAny, KEY_UP,    cursor_up    },
+    { ModAny, KEY_DOWN,  cursor_dn    },
+    { ModAny, KEY_LEFT,  cursor_left  },
+    { ModAny, KEY_RIGHT, cursor_right },
 
     /* Implementation Specific */
-    { ModAny,  KEY_ESCAPE, select_prev  },
+    { ModNone, KEY_ESCAPE, select_prev  },
     { ModCtrl, 't',        change_focus },
     { ModCtrl, 'q',        quit         },
     { ModCtrl, 'f',        search       },
     { ModCtrl, 'd',        execute      },
-
-    /* Picker Shortcuts */
-    { ModCtrl, 'o', open_file },
-    //{ KEY_CTRL_P,    find_ctag    },
-    //{ KEY_CTRL_G,    goto_ctag    },
+    { ModCtrl, 'o',        open_file    },
+    //{ ModCtrl, 'p',        find_ctag    },
+    //{ ModCtrl, 'g',        goto_ctag    },
 
     { 0, 0, NULL } // End the table
 };
@@ -392,30 +367,35 @@ static void delete(void) {
     view_delete(currview());
 }
 
+static void cursor_home(void) {
+    bool extsel = x11_keymodsset(ModShift);
+    view_bol(currview(), extsel);
+}
+
+static void cursor_end(void) {
+    bool extsel = x11_keymodsset(ModShift);
+    view_eol(currview(), extsel);
+}
+
 static void cursor_up(void) {
-    view_byline(currview(), -1, false);
+    bool extsel = x11_keymodsset(ModShift);
+    view_byline(currview(), -1, extsel);
 }
 
 static void cursor_dn(void) {
-    view_byline(currview(), +1, false);
+    bool extsel = x11_keymodsset(ModShift);
+    view_byline(currview(), +1, extsel);
 }
 
 static void cursor_left(void) {
-    view_byrune(currview(), -1, false);
+    bool extsel = x11_keymodsset(ModShift);
+    view_byrune(currview(), -1, extsel);
 }
 
 static void cursor_right(void) {
     view_byrune(currview(), +1, false);
 }
 
-static void cursor_bol(void) {
-    view_bol(currview(), false);
-}
-
-static void cursor_eol(void) {
-    view_eol(currview(), false);
-}
-
 static void page_up(void) {
     view_scrollpage(currview(), -1);
 }
@@ -515,39 +495,11 @@ static void delword_right(void){}
 static void delrune_left(void){}
 static void delword_left(void){}
 static void cursor_bof(void){}
-
-static void select_bol(void){
-    view_bol(currview(), true);
-}
-
 static void select_bof(void){}
 static void cursor_eof(void){}
-
-static void select_eol(void) {
-    view_eol(currview(), true);
-}
-
 static void select_eof(void){}
-
-static void select_up(void) {
-    view_byline(currview(), -1, true);
-}
-
-static void select_dn(void) {
-    view_byline(currview(), +1, true);
-}
-
-static void select_left(void) {
-    view_byrune(currview(), -1, true);
-}
-
 static void word_left(void){}
 static void selword_left(void){}
-
-static void select_right(void) {
-    view_byrune(currview(), +1, true);
-}
-
 static void word_right(void){}
 static void selword_right(void){}