Share a cut buffer between all edit buffers

weechat-hashes
June McEnroe 2022-02-20 16:20:33 -05:00
parent c8b6e331de
commit da1b943fcc
3 changed files with 16 additions and 23 deletions

26
edit.c
View File

@ -38,13 +38,6 @@ static bool isword(wchar_t ch) {
return !iswspace(ch) && !iswpunct(ch); return !iswspace(ch) && !iswpunct(ch);
} }
void editFree(struct Edit *e) {
free(e->buf);
free(e->cut.buf);
e->pos = e->len = e->cap = 0;
e->cut.len = 0;
}
char *editString(const struct Edit *e, char **buf, size_t *cap, size_t *pos) { char *editString(const struct Edit *e, char **buf, size_t *cap, size_t *pos) {
size_t req = e->len * MB_CUR_MAX + 1; size_t req = e->len * MB_CUR_MAX + 1;
if (req > *cap) { if (req > *cap) {
@ -93,11 +86,10 @@ int editCopy(struct Edit *e, size_t index, size_t count) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
wchar_t *buf = realloc(e->cut.buf, sizeof(*buf) * count); if (!e->cut) return 0;
if (!buf) return -1; e->cut->len = 0;
e->cut.buf = buf; if (editReserve(e->cut, 0, count) < 0) return -1;
wmemcpy(e->cut.buf, &e->buf[index], count); wmemcpy(e->cut->buf, &e->buf[index], count);
e->cut.len = count;
return 0; return 0;
} }
@ -159,10 +151,11 @@ int editFn(struct Edit *e, enum EditFn fn) {
} }
break; case EditPaste: { break; case EditPaste: {
ret = editReserve(e, e->pos, e->cut.len); if (!e->cut) break;
ret = editReserve(e, e->pos, e->cut->len);
if (ret == 0) { if (ret == 0) {
wmemcpy(&e->buf[e->pos], e->cut.buf, e->cut.len); wmemcpy(&e->buf[e->pos], e->cut->buf, e->cut->len);
e->pos += e->cut.len; e->pos += e->cut->len;
} }
} }
break; case EditTranspose: { break; case EditTranspose: {
@ -226,7 +219,8 @@ static bool eq(struct Edit *e, const char *str1) {
#define editFn(...) assert(0 == editFn(__VA_ARGS__)) #define editFn(...) assert(0 == editFn(__VA_ARGS__))
int main(void) { int main(void) {
struct Edit e = { .mode = EditEmacs }; struct Edit cut = {0};
struct Edit e = { .cut = &cut };
fix(&e, "foo bar"); fix(&e, "foo bar");
editFn(&e, EditHead); editFn(&e, EditHead);

8
edit.h
View File

@ -38,10 +38,7 @@ struct Edit {
size_t pos; size_t pos;
size_t len; size_t len;
size_t cap; size_t cap;
struct { struct Edit *cut;
wchar_t *buf;
size_t len;
} cut;
}; };
enum EditFn { enum EditFn {
@ -75,9 +72,6 @@ int editInsert(struct Edit *e, wchar_t ch);
// Convert the buffer to a multi-byte string. // Convert the buffer to a multi-byte string.
char *editString(const struct Edit *e, char **buf, size_t *cap, size_t *pos); char *editString(const struct Edit *e, char **buf, size_t *cap, size_t *pos);
// Free all buffers.
void editFree(struct Edit *e);
// Reserve a range in the buffer. // Reserve a range in the buffer.
int editReserve(struct Edit *e, size_t index, size_t count); int editReserve(struct Edit *e, size_t index, size_t count);

View File

@ -89,9 +89,14 @@ enum {
#undef X #undef X
}; };
static struct Edit cut;
static struct Edit edits[IDCap]; static struct Edit edits[IDCap];
void inputInit(void) { void inputInit(void) {
for (size_t i = 0; i < ARRAY_LEN(edits); ++i) {
edits[i].cut = &cut;
}
struct termios term; struct termios term;
int error = tcgetattr(STDOUT_FILENO, &term); int error = tcgetattr(STDOUT_FILENO, &term);
if (error) err(EX_OSERR, "tcgetattr"); if (error) err(EX_OSERR, "tcgetattr");