summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/buffer.c16
-rw-r--r--src/reactor-epoll.c (renamed from src/reactor-linux.c)0
-rw-r--r--src/text.c36
3 files changed, 28 insertions, 24 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 71d486e..8a86e69 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -16,7 +16,9 @@
#include <unistd.h>
#include <wchar.h>
-static struct modeline { uint8_t *buffer; } g_modeline = {0};
+struct modeline {
+ uint8_t *buffer;
+};
#define KILL_RING_SZ 64
static struct kill_ring {
@@ -88,8 +90,9 @@ struct buffer buffer_create(char *name, bool modeline) {
sizeof(bindings) / sizeof(bindings[0]));
if (modeline) {
- g_modeline.buffer = malloc(1024);
- buffer_add_update_hook(&b, buffer_modeline_hook, &g_modeline);
+ struct modeline *modeline = calloc(1, sizeof(struct modeline));
+ modeline->buffer = malloc(1024);
+ buffer_add_update_hook(&b, buffer_modeline_hook, modeline);
}
if (modeline) {
@@ -108,10 +111,6 @@ void buffer_destroy(struct buffer *buffer) {
keymap_destroy(&buffer->keymaps[keymapi]);
}
free(buffer->keymaps);
-
- if (g_modeline.buffer != NULL) {
- free(g_modeline.buffer);
- }
}
void buffer_clear(struct buffer *buffer) {
@@ -194,7 +193,7 @@ struct region to_region(struct buffer_location dot,
bool region_has_size(struct buffer *buffer) {
return buffer->mark_set &&
- (labs((int64_t)buffer->mark.line - (int64_t)buffer->dot.line + 1) *
+ (labs((int64_t)buffer->mark.line - (int64_t)buffer->dot.line) +
labs((int64_t)buffer->mark.col - (int64_t)buffer->dot.col)) > 0;
}
@@ -234,6 +233,7 @@ void buffer_cut(struct buffer *buffer) {
text_delete(buffer->text, reg.begin.line, reg.begin.col, reg.end.line,
reg.end.col);
buffer_clear_mark(buffer);
+ buffer->dot = reg.begin;
}
}
diff --git a/src/reactor-linux.c b/src/reactor-epoll.c
index e488fef..e488fef 100644
--- a/src/reactor-linux.c
+++ b/src/reactor-epoll.c
diff --git a/src/text.c b/src/text.c
index 6e343ae..ece4ad4 100644
--- a/src/text.c
+++ b/src/text.c
@@ -336,7 +336,8 @@ struct copy_cmd {
struct text_chunk text_get_region(struct text *text, uint32_t start_line,
uint32_t start_col, uint32_t end_line,
uint32_t end_col) {
- struct copy_cmd *copy_cmds = malloc(end_line - start_line + 1);
+ uint32_t nlines = end_line - start_line + 1;
+ struct copy_cmd *copy_cmds = calloc(nlines, sizeof(struct copy_cmd));
uint32_t total_chars = 0, total_bytes = 0;
for (uint32_t line = start_line; line <= end_line; ++line) {
@@ -348,12 +349,10 @@ struct text_chunk text_get_region(struct text *text, uint32_t start_line,
cmd->line = line;
cmd->byteindex = 0;
cmd->nbytes = l->nbytes;
-
- ++line;
}
// correct first line
- struct copy_cmd *cmd_first = &copy_cmds[start_line];
+ struct copy_cmd *cmd_first = &copy_cmds[0];
struct line *first_line = &text->lines[start_line];
uint32_t byteoff =
utf8_nbytes(first_line->data, first_line->nbytes, start_col);
@@ -363,33 +362,38 @@ struct text_chunk text_get_region(struct text *text, uint32_t start_line,
total_chars -= start_col;
// correct last line
- struct copy_cmd *cmd_last = &copy_cmds[end_line];
+ struct copy_cmd *cmd_last = &copy_cmds[nlines - 1];
struct line *last_line = &text->lines[end_line];
uint32_t byteindex = utf8_nbytes(last_line->data, last_line->nbytes, end_col);
cmd_last->nbytes -= (last_line->nchars - end_col);
total_bytes -= (last_line->nbytes - byteindex);
total_chars -= (last_line->nchars - end_col);
- struct text_chunk txt = {
- .text = (uint8_t *)malloc(total_bytes + end_line - start_line),
- .line = 0,
- .nbytes = total_bytes,
- .nchars = total_chars,
- };
+ uint8_t *data = (uint8_t *)malloc(total_bytes + end_line - start_line);
// copy data
- for (uint32_t cmdi = 0, curr = 0; cmdi <= end_line - start_line; ++cmdi) {
+ for (uint32_t cmdi = 0, curr = 0; cmdi < nlines; ++cmdi) {
struct copy_cmd *c = &copy_cmds[cmdi];
struct line *l = &text->lines[c->line];
- memcpy(txt.text + curr, l->data + c->byteindex, c->nbytes);
+ memcpy(data + curr, l->data + c->byteindex, c->nbytes);
curr += c->nbytes;
- if (cmdi != end_line - start_line) {
- txt.text[++curr] = '\n';
+ if (cmdi != (nlines - 1)) {
+ data[curr] = '\n';
+ ++curr;
+ ++total_bytes;
+ ++total_chars;
}
}
- return txt;
+ free(copy_cmds);
+ return (struct text_chunk){
+ .text = data,
+ .line = 0,
+ .nbytes = total_bytes,
+ .nchars = total_chars,
+ };
+ ;
}
bool text_line_contains_unicode(struct text *text, uint32_t line) {