]> git.mdlowis.com Git - projs/tide.git/commitdiff
added Line tag to get line number range covered by selection
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 8 Jan 2019 04:19:28 +0000 (23:19 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 8 Jan 2019 04:19:28 +0000 (23:19 -0500)
TODO.md
inc/edit.h
src/lib/buf.c
src/tide.c

diff --git a/TODO.md b/TODO.md
index ec6aa9890145c9f392bcd52d73ffc93690f16bd2..39559c97c0e4ce71e8874d6421edc2bf73adc0ac 100644 (file)
--- a/TODO.md
+++ b/TODO.md
@@ -9,7 +9,6 @@
 * registrar: should remove invalid windows from registry when detected
 * tide: Ctrl+D should not pass tag name as arg when executing tag commands
 * tide: should re-register with the registrar when a new registrar is launched
-* tide: Line - Get the current line number(s) containing the selection
 * tide: gap buffer does not handle UTF-8 currently
 
 ## BACKLOG
index e6d15780d508bf54604b4f72a122adefb3338e3a..b9e2881fb8b137f53c3910ecb31c2c96b50dedd0 100644 (file)
@@ -72,6 +72,7 @@ size_t buf_byline(Buf* buf, size_t pos, int count);
 bool buf_findstr(Buf* buf, int dir, char* str);
 
 void buf_setln(Buf* buf, size_t line);
+void buf_getln(Buf* buf, size_t* begln, size_t* endln);
 void buf_getcol(Buf* buf);
 void buf_setcol(Buf* buf);
 
index 23a91c9100bb12c8bb22a11d905cd28fb877aab4..ba14c65fe6c18f2b245f746560503847b8f2d64c 100644 (file)
@@ -624,6 +624,23 @@ void buf_setln(Buf* buf, size_t line) {
     buf->selection.beg = buf->selection.end = curr;
 }
 
+void buf_getln(Buf* buf, size_t* begln, size_t* endln) {
+    size_t line = 1, curr = 0, end = buf_end(buf);
+    size_t sbeg = buf_selbeg(buf), send = buf_selend(buf);
+    while (curr < end) {
+        size_t next = buf_byline(buf, curr, DOWN);
+        if (curr <= sbeg && sbeg < next) {
+            *begln = line, *endln = line;
+        }
+        if (curr <= send && send < next) {
+            *endln = line;
+            break;
+        }
+        if (curr == next) break;
+        line++, curr = next;
+    }
+}
+
 void buf_getcol(Buf* buf) {
     Sel sel = buf->selection; //getsel(buf, NULL);
     size_t pos = sel.end, curr = buf_bol(buf, pos);
index 6c3a212813484e9002986f756521359dd62adcb0..3ae0589e311c48ebd8d08bf18f61564fce6df3cb 100644 (file)
@@ -26,7 +26,7 @@ typedef struct {
 } Tag;
 
 char* ARGV0;
-static Tag Builtins[17];
+static Tag Builtins[18];
 static Time Now;
 static struct XConf X;
 static int KeyBtnState;
@@ -787,9 +787,21 @@ static void tag_kill(char* cmd) {
         job_kill(job);
 }
 
+static void tag_line(char* cmd) {
+    (void)cmd;
+    char buf[256] = {0};
+    size_t lnbeg = 1, lnend = 1;
+    buf_getln(win_buf(EDIT), &lnbeg, &lnend);
+    if (lnbeg == lnend)
+        snprintf(buf, sizeof(buf)-1, "%lu", lnbeg);
+    else
+        snprintf(buf, sizeof(buf)-1, "%lu:%lu", lnbeg, lnend);
+    view_paste(win_view(TAGS), buf);
+}
+
 /* Main Routine
  ******************************************************************************/
-static Tag Builtins[17] = {
+static Tag Builtins[18] = {
     { .tag = "Cut",    .action = cut       },
     { .tag = "Copy",   .action = copy      },
     { .tag = "Del",    .action = quit      },
@@ -806,6 +818,7 @@ static Tag Builtins[17] = {
     { .tag = "Undo",   .action = tag_undo  },
     { .tag = "Font",   .action = win_font  },
     { .tag = "Kill",   .action = tag_kill  },
+    { .tag = "Line",   .action = tag_line  },
     { .tag = NULL,     .action = NULL      }
 };