tex: yank and put commands - neatvi - [fork] simple vi-type editor with UTF-8 s… | |
git clone git://src.adamsgaard.dk/neatvi | |
Log | |
Files | |
Refs | |
README | |
--- | |
commit 1ed75144175e7ea9b167a76c9b096b4c144ddfad | |
parent 01c3c8a62f1daee16d343488699f804c72dbca91 | |
Author: Ali Gholami Rudi <[email protected]> | |
Date: Tue, 5 May 2015 18:00:31 +0430 | |
ex: yank and put commands | |
Diffstat: | |
M ex.c | 50 ++++++++++++++++++++++++++++-… | |
M reg.c | 22 ++++++++++++---------- | |
2 files changed, 57 insertions(+), 15 deletions(-) | |
--- | |
diff --git a/ex.c b/ex.c | |
t@@ -155,13 +155,13 @@ static int ex_region(char *loc, int *beg, int *end) | |
return 0; | |
} | |
while (*loc) { | |
- char *r = loc; | |
- while (*loc && *loc != ';' && *loc != ',') | |
- loc++; | |
- *beg = *end; | |
- *end = ex_lineno(r) + 1; | |
+ int end0 = *end; | |
+ *end = ex_lineno(loc) + 1; | |
+ *beg = naddr++ ? end0 - 1 : *end - 1; | |
if (!naddr++) | |
*beg = *end - 1; | |
+ while (*loc && *loc != ';' && *loc != ',') | |
+ loc++; | |
if (!*loc) | |
break; | |
if (*loc == ';') | |
t@@ -284,17 +284,55 @@ static void ec_print(char *ec) | |
} | |
} | |
+static void ex_yank(int reg, int beg, int end) | |
+{ | |
+ char *buf = lbuf_cp(xb, beg, end); | |
+ reg_put(reg, buf, 1); | |
+ free(buf); | |
+} | |
+ | |
static void ec_delete(char *ec) | |
{ | |
char loc[EXLEN]; | |
+ char arg[EXLEN]; | |
int beg, end; | |
ex_loc(ec, loc); | |
+ ex_arg(ec, arg); | |
if (!ex_region(loc, &beg, &end) && lbuf_len(xb)) { | |
+ ex_yank(arg[0], beg, end); | |
lbuf_rm(xb, beg, end); | |
xrow = beg; | |
} | |
} | |
+static void ec_yank(char *ec) | |
+{ | |
+ char loc[EXLEN]; | |
+ char arg[EXLEN]; | |
+ int beg, end; | |
+ ex_loc(ec, loc); | |
+ ex_arg(ec, arg); | |
+ if (!ex_region(loc, &beg, &end) && lbuf_len(xb)) | |
+ ex_yank(arg[0], beg, end); | |
+} | |
+ | |
+static void ec_put(char *ec) | |
+{ | |
+ char loc[EXLEN]; | |
+ char arg[EXLEN]; | |
+ int beg, end; | |
+ int lnmode; | |
+ char *buf; | |
+ int n = lbuf_len(xb); | |
+ ex_loc(ec, loc); | |
+ ex_arg(ec, arg); | |
+ buf = reg_get(arg[0], &lnmode); | |
+ if (buf && !ex_region(loc, &beg, &end)) { | |
+ lbuf_put(xb, end, buf); | |
+ xrow = MIN(lbuf_len(xb) - 1, end + lbuf_len(xb) - n - 1); | |
+ } | |
+} | |
+ | |
static void ec_lnum(char *ec) | |
{ | |
char loc[EXLEN]; | |
t@@ -397,12 +435,14 @@ static struct excmd { | |
{"e", "edit", ec_edit}, | |
{"=", "=", ec_lnum}, | |
{"k", "mark", ec_mark}, | |
+ {"pu", "put", ec_put}, | |
{"q", "quit", ec_quit}, | |
{"r", "read", ec_read}, | |
{"w", "write", ec_write}, | |
{"u", "undo", ec_undo}, | |
{"r", "redo", ec_redo}, | |
{"s", "substitute", ec_substitute}, | |
+ {"ya", "yank", ec_yank}, | |
{"", "", ec_print}, | |
}; | |
diff --git a/reg.c b/reg.c | |
t@@ -2,25 +2,27 @@ | |
#include <string.h> | |
#include "vi.h" | |
-static char *reg; | |
-static int lnmode; | |
+static char *bufs[256]; | |
+static int lnmode[256]; | |
char *reg_get(int c, int *ln) | |
{ | |
- *ln = lnmode; | |
- return reg; | |
+ *ln = lnmode[c]; | |
+ return bufs[c]; | |
} | |
void reg_put(int c, char *s, int ln) | |
{ | |
- char *nreg = malloc(strlen(s) + 1); | |
- strcpy(nreg, s); | |
- free(reg); | |
- reg = nreg; | |
- lnmode = ln; | |
+ char *buf = malloc(strlen(s) + 1); | |
+ strcpy(buf, s); | |
+ free(bufs[c]); | |
+ bufs[c] = buf; | |
+ lnmode[c] = ln; | |
} | |
void reg_done(void) | |
{ | |
- free(reg); | |
+ int i; | |
+ for (i = 0; i < LEN(bufs); i++) | |
+ free(bufs[i]); | |
} |