]> git.mdlowis.com Git - projs/tide.git/commitdiff
context sensitive selection for mouse buttons
authorMichael D. Lowis <mike@mdlowis.com>
Thu, 12 Jan 2017 03:36:09 +0000 (22:36 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Thu, 12 Jan 2017 03:36:09 +0000 (22:36 -0500)
TODO.md
inc/edit.h
inc/utf.h
lib/buf.c
lib/utf8.c
lib/view.c
xedit.c

diff --git a/TODO.md b/TODO.md
index 69d2bc54a79a2dda77d5c20f24a4b2e6916d033a..347441ca4a325def5a7c71dad533ae4e5418e7e4 100644 (file)
--- a/TODO.md
+++ b/TODO.md
@@ -2,7 +2,6 @@
 
 Up Next:
 
-* context sensitive selection of words, commands, line numbers, or filenames.
 * Implement X Selection protocol for handling clipboard and primary selections
 * Tag line count should account for wrapped lines
 * ctrl+alt+f should find next occurence of previous search term
index 37bd478c5938d580e5f0d7bba4a6eebf11f39ada..f4aa893d3dda79f365bacc07a93bb9b9ec924c0d 100644 (file)
@@ -78,6 +78,8 @@ unsigned buf_bow(Buf* buf, unsigned pos);
 unsigned buf_eow(Buf* buf, unsigned pos);
 unsigned buf_lscan(Buf* buf, unsigned pos, Rune r);
 unsigned buf_rscan(Buf* buf, unsigned pos, Rune r);
+
+void buf_getword(Buf* buf, bool (*isword)(Rune), Sel* sel);
 void buf_getblock(Buf* buf, Rune beg, Rune end, Sel* sel);
 
 unsigned buf_byrune(Buf* buf, unsigned pos, int count);
@@ -149,7 +151,7 @@ void view_byrune(View* view, int move, bool extsel);
 void view_byword(View* view, int move, bool extsel);
 void view_byline(View* view, int move, bool extsel);
 
-char* view_fetch(View* view, size_t row, size_t col);
+char* view_fetchcmd(View* view, size_t row, size_t col);
 void view_find(View* view, int dir, size_t row, size_t col);
 void view_findstr(View* view, int dir, char* str);
 void view_insert(View* view, bool indent, Rune rune);
index 7b361a476c62472fe49cd0709790a34c74e62413..0aa87f98abb30c18df18cf75141c6be79262e3bf 100644 (file)
--- a/inc/utf.h
+++ b/inc/utf.h
@@ -22,3 +22,5 @@ bool rissigil(Rune r);
 bool risfile(Rune r);
 bool riscmd(Rune r);
 bool risblank(Rune r);
+bool risbigword(Rune r);
+
index 35d9929051df25ba597bd99cf63fd942f273917e..ab033c1fd16ce2544ffa9209d37f3effbfe3cd45 100644 (file)
--- a/lib/buf.c
+++ b/lib/buf.c
@@ -380,6 +380,12 @@ unsigned buf_rscan(Buf* buf, unsigned pos, Rune r) {
     return (buf_get(buf, off) == r ? off : pos);
 }
 
+void buf_getword(Buf* buf, bool (*isword)(Rune), Sel* sel) {
+    for (; isword(buf_get(buf, sel->beg-1)); sel->beg--);
+    for (; isword(buf_get(buf, sel->end));   sel->end++);
+    sel->end--;
+}
+
 void buf_getblock(Buf* buf, Rune first, Rune last, Sel* sel) {
     int balance = 0, dir;
     unsigned beg = sel->end, end = sel->end, off;
@@ -459,8 +465,6 @@ unsigned buf_byline(Buf* buf, unsigned pos, int count) {
 
 /*****************************************************************************/
 
-int dir = +1;
-
 void buf_find(Buf* buf, int dir, size_t* beg, size_t* end) {
     unsigned dbeg = *beg, dend = *end;
     unsigned mbeg = dbeg+dir, mend = dend+dir;
index 76c4b25dafebf60c20b03487ccc247a94689f900..c363bb4864fe99c8851b51ea6c34d1fd13455730 100644 (file)
@@ -123,3 +123,8 @@ bool riscmd(Rune r) {
 bool risblank(Rune r) {
     return (r == ' ' || r == '\t' || r == '\n' || r == '\r' || r == RUNE_CRLF);
 }
+
+bool risbigword(Rune r) {
+    return !risblank(r);
+}
+
index ac4aab7994750da4e71f92832411a48ac0d12e84..e5e486ee4e15769ece33724450ce0f22172a08a8 100644 (file)
@@ -136,10 +136,7 @@ static void sync_center(View* view, size_t csr) {
     int move = (scrln - midrow);
     unsigned count = (move < 0 ? -move : move);
     for (; count > 0; count--)
-        if (move < 0)
-            scroll_up(view);
-        else
-            scroll_dn(view);
+        (move < 0 ? scroll_up : scroll_dn)(view);
 }
 
 static void sync_view(View* view, size_t csr) {
@@ -316,8 +313,7 @@ static void selcontext(View* view, Sel* sel) {
         sel->beg = bol;
         sel->end = buf_eol(buf, sel->end);
     } else if (risword(r)) {
-        sel->beg = buf_bow(buf, sel->end);
-        sel->end = buf_eow(buf, sel->end++);
+        buf_getword(buf, risword, sel);
     } else if (r == '(' || r == ')') {
         buf_getblock(buf, '(', ')', sel);
     } else if (r == '[' || r == ']') {
@@ -325,7 +321,7 @@ static void selcontext(View* view, Sel* sel) {
     } else if (r == '{' || r == '}') {
         buf_getblock(buf, '{', '}', sel);
     } else {
-        selbigword(view, sel);
+        buf_getword(buf, risbigword, sel);
     }
 }
 
@@ -333,7 +329,7 @@ void view_selword(View* view, size_t row, size_t col) {
     buf_loglock(&(view->buffer));
     view_setcursor(view, row, col);
     Sel sel = view->selection;
-    selbigword(view, &sel);
+    buf_getword(&(view->buffer), risbigword, &(sel));
     sel.end++;
     view->selection = sel;
 }
@@ -362,15 +358,15 @@ size_t view_selsize(View* view) {
     return num_selected(view->selection);
 }
 
-char* view_fetch(View* view, size_t row, size_t col) {
-    char* str = NULL;
+char* view_fetchcmd(View* view, size_t row, size_t col) {
+   char* str = NULL;
     size_t off = getoffset(view, row, col);
     if (off != SIZE_MAX) {
         Sel sel = { .beg = off, .end = off };
         if (in_selection(view->selection, off)) {
             sel = view->selection;
         } else {
-            selcontext(view, &sel);
+            buf_getword(&(view->buffer), riscmd, &sel);
             sel.end++;
         }
         str = view_getstr(view, &sel);
@@ -402,7 +398,7 @@ void view_findstr(View* view, int dir, char* str) {
     buf_findstr(&(view->buffer), dir, str, &sel.beg, &sel.end);
     view->selection = sel;
     view->sync_needed = true;
-    view->sync_center   = true;
+    view->sync_center = true;
 }
 
 void view_insert(View* view, bool indent, Rune rune) {
@@ -492,6 +488,7 @@ void view_redo(View* view) {
 }
 
 void view_putstr(View* view, char* str) {
+    unsigned beg = view->selection.beg;
     buf_loglock(&(view->buffer));
     while (*str) {
         Rune rune = 0;
@@ -500,6 +497,7 @@ void view_putstr(View* view, char* str) {
         view_insert(view, false, rune);
     }
     buf_loglock(&(view->buffer));
+    view->selection.beg = beg;
 }
 
 void view_append(View* view, char* str) {
diff --git a/xedit.c b/xedit.c
index 056a1872697cd952fb7c0fd8b620c700ab6f3f03..c5fc34420a05eafbb11c6a171719535367bc9783 100644 (file)
--- a/xedit.c
+++ b/xedit.c
@@ -199,6 +199,7 @@ int main(int argc, char** argv) {
     /* load the buffer views */
     view_init(getview(TAGS), NULL);
     view_putstr(getview(TAGS), DEFAULT_TAGS);
+    view_selprev(getview(TAGS)); // clear the selection
     buf_logclear(getbuf(TAGS));
     view_init(getview(EDIT), (argc > 1 ? argv[1] : NULL));
     /* initialize the display engine */
@@ -728,7 +729,6 @@ static void cmd_exec(char* cmd) {
         view_append(getview(TAGS), chomp(error));
     if (output) {
         view_putstr(getview(dest), output);
-        view_selprev(getview(dest));
         Focused = dest;
     }
     /* cleanup */
@@ -770,15 +770,9 @@ static void mouse_middle(enum RegionId id, size_t count, size_t row, size_t col)
     if (MouseBtns[MOUSE_BTN_LEFT].pressed) {
         cut();
     } else {
-    #if 0
-        char* str = view_selcmd(getview(id), row, col);
+        char* str = view_fetchcmd(getview(id), row, col);
         if (str) exec(str);
         free(str);
-    #else
-        char* str = view_fetch(getview(id), row, col);
-        if (str) exec(str);
-        free(str);
-    #endif
     }
 }