aboutsummaryrefslogtreecommitdiff
path: root/text.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2014-09-08 14:11:55 +0200
committerMarc André Tanner <mat@brain-dump.org>2014-09-08 14:15:05 +0200
commitfc57421057fcdcc80080dbc59fae633fc49e0ca5 (patch)
tree261d750338281d945a42112d42c0cca569d933d9 /text.c
parentc6b0e43f3fd8fbafc323ffe14a2f12312d0bc64a (diff)
downloadvis-fc57421057fcdcc80080dbc59fae633fc49e0ca5.tar.gz
vis-fc57421057fcdcc80080dbc59fae633fc49e0ca5.tar.xz
Unify variable naming
Only textual substitution no functional changes.
Diffstat (limited to 'text.c')
-rw-r--r--text.c390
1 files changed, 195 insertions, 195 deletions
diff --git a/text.c b/text.c
index 4a4cd80..8c775da 100644
--- a/text.c
+++ b/text.c
@@ -51,10 +51,10 @@ struct Buffer {
* All active pieces chained together form the whole content of the document.
* At the beginning there exists only one piece, spanning the whole document.
* Upon insertion/delition new pieces will be created to represent the changes.
- * Generally pieces are never destroyed, but kept around to peform undo/redo operations.
+ * Generally pieces are never destroytxt, but kept around to peform undo/redo operations.
*/
struct Piece {
- Text *editor; /* editor to which this piece belongs */
+ Text *text; /* text to which this piece belongs */
Piece *prev, *next; /* pointers to the logical predecessor/successor */
Piece *global_prev; /* double linked list in order of allocation, */
Piece *global_next; /* used to free individual pieces */
@@ -122,42 +122,42 @@ struct Text {
};
/* buffer management */
-static Buffer *buffer_alloc(Text *ed, size_t size);
+static Buffer *buffer_alloc(Text *txt, size_t size);
static void buffer_free(Buffer *buf);
static bool buffer_capacity(Buffer *buf, size_t len);
static const char *buffer_append(Buffer *buf, const char *data, size_t len);
static bool buffer_insert(Buffer *buf, size_t pos, const char *data, size_t len);
static bool buffer_delete(Buffer *buf, size_t pos, size_t len);
-static const char *buffer_store(Text *ed, const char *data, size_t len);
+static const char *buffer_store(Text *txt, const char *data, size_t len);
/* cache layer */
-static void cache_piece(Text *ed, Piece *p);
-static bool cache_contains(Text *ed, Piece *p);
-static bool cache_insert(Text *ed, Piece *p, size_t off, const char *data, size_t len);
-static bool cache_delete(Text *ed, Piece *p, size_t off, size_t len);
+static void cache_piece(Text *txt, Piece *p);
+static bool cache_contains(Text *txt, Piece *p);
+static bool cache_insert(Text *txt, Piece *p, size_t off, const char *data, size_t len);
+static bool cache_delete(Text *txt, Piece *p, size_t off, size_t len);
/* piece management */
-static Piece *piece_alloc(Text *ed);
+static Piece *piece_alloc(Text *txt);
static void piece_free(Piece *p);
static void piece_init(Piece *p, Piece *prev, Piece *next, const char *data, size_t len);
-static Location piece_get_intern(Text *ed, size_t pos);
-static Location piece_get_extern(Text *ed, size_t pos);
+static Location piece_get_intern(Text *txt, size_t pos);
+static Location piece_get_extern(Text *txt, size_t pos);
/* span management */
static void span_init(Span *span, Piece *start, Piece *end);
-static void span_swap(Text *ed, Span *old, Span *new);
+static void span_swap(Text *txt, Span *old, Span *new);
/* change management */
-static Change *change_alloc(Text *ed);
+static Change *change_alloc(Text *txt);
static void change_free(Change *c);
/* action management */
-static Action *action_alloc(Text *ed);
+static Action *action_alloc(Text *txt);
static void action_free(Action *a);
static void action_push(Action **stack, Action *action);
static Action *action_pop(Action **stack);
/* logical line counting cache */
static void lineno_cache_invalidate(LineCache *cache);
-static size_t lines_skip_forward(Text *ed, size_t pos, size_t lines);
-static size_t lines_count(Text *ed, size_t pos, size_t len);
+static size_t lines_skip_forward(Text *txt, size_t pos, size_t lines);
+static size_t lines_count(Text *txt, size_t pos, size_t len);
/* allocate a new buffer of MAX(size, BUFFER_SIZE) bytes */
-static Buffer *buffer_alloc(Text *ed, size_t size) {
+static Buffer *buffer_alloc(Text *txt, size_t size) {
Buffer *buf = calloc(1, sizeof(Buffer));
if (!buf)
return NULL;
@@ -168,8 +168,8 @@ static Buffer *buffer_alloc(Text *ed, size_t size) {
return NULL;
}
buf->size = size;
- buf->next = ed->buffers;
- ed->buffers = buf;
+ buf->next = txt->buffers;
+ txt->buffers = buf;
return buf;
}
@@ -194,9 +194,9 @@ static const char *buffer_append(Buffer *buf, const char *data, size_t len) {
/* stores the given data in a buffer, allocates a new one if necessary. returns
* a pointer to the storage location or NULL if allocation failed. */
-static const char *buffer_store(Text *ed, const char *data, size_t len) {
- Buffer *buf = ed->buffers;
- if ((!buf || !buffer_capacity(buf, len)) && !(buf = buffer_alloc(ed, len)))
+static const char *buffer_store(Text *txt, const char *data, size_t len) {
+ Buffer *buf = txt->buffers;
+ if ((!buf || !buffer_capacity(buf, len)) && !(buf = buffer_alloc(txt, len)))
return NULL;
return buffer_append(buf, data, len);
}
@@ -231,18 +231,18 @@ static bool buffer_delete(Buffer *buf, size_t pos, size_t len) {
}
/* cache the given piece if it is the most recently changed one */
-static void cache_piece(Text *ed, Piece *p) {
- Buffer *buf = ed->buffers;
+static void cache_piece(Text *txt, Piece *p) {
+ Buffer *buf = txt->buffers;
if (!buf || p->data < buf->data || p->data + p->len != buf->data + buf->len)
return;
- ed->cache = p;
+ txt->cache = p;
}
/* check whether the given piece was the most recently modified one */
-static bool cache_contains(Text *ed, Piece *p) {
- Buffer *buf = ed->buffers;
- Action *a = ed->current_action;
- if (!buf || !ed->cache || ed->cache != p || !a || !a->change)
+static bool cache_contains(Text *txt, Piece *p) {
+ Buffer *buf = txt->buffers;
+ Action *a = txt->current_action;
+ if (!buf || !txt->cache || txt->cache != p || !a || !a->change)
return false;
Piece *start = a->change->new.start;
@@ -261,16 +261,16 @@ static bool cache_contains(Text *ed, Piece *p) {
/* try to insert a junk of data at a given piece offset. the insertion is only
* performed if the piece is the most recenetly changed one. the legnth of the
* piece, the span containing it and the whole text is adjusted accordingly */
-static bool cache_insert(Text *ed, Piece *p, size_t off, const char *data, size_t len) {
- if (!cache_contains(ed, p))
+static bool cache_insert(Text *txt, Piece *p, size_t off, const char *data, size_t len) {
+ if (!cache_contains(txt, p))
return false;
- Buffer *buf = ed->buffers;
+ Buffer *buf = txt->buffers;
size_t bufpos = p->data + off - buf->data;
if (!buffer_insert(buf, bufpos, data, len))
return false;
p->len += len;
- ed->current_action->change->new.len += len;
- ed->size += len;
+ txt->current_action->change->new.len += len;
+ txt->size += len;
return true;
}
@@ -278,16 +278,16 @@ static bool cache_insert(Text *ed, Piece *p, size_t off, const char *data, size_
* performed if the piece is the most recenetly changed one and the whole
* affected range lies within it. the legnth of the piece, the span containing it
* and the whole text is adjusted accordingly */
-static bool cache_delete(Text *ed, Piece *p, size_t off, size_t len) {
- if (!cache_contains(ed, p))
+static bool cache_delete(Text *txt, Piece *p, size_t off, size_t len) {
+ if (!cache_contains(txt, p))
return false;
- Buffer *buf = ed->buffers;
+ Buffer *buf = txt->buffers;
size_t bufpos = p->data + off - buf->data;
if (off + len > p->len || !buffer_delete(buf, bufpos, len))
return false;
p->len -= len;
- ed->current_action->change->new.len -= len;
- ed->size -= len;
+ txt->current_action->change->new.len -= len;
+ txt->size -= len;
return true;
}
@@ -311,7 +311,7 @@ static void span_init(Span *span, Piece *start, Piece *end) {
*
* adjusts the document size accordingly.
*/
-static void span_swap(Text *ed, Span *old, Span *new) {
+static void span_swap(Text *txt, Span *old, Span *new) {
/* TODO use a balanced search tree to keep the pieces
instead of a doubly linked list.
*/
@@ -330,8 +330,8 @@ static void span_swap(Text *ed, Span *old, Span *new) {
old->start->prev->next = new->start;
old->end->next->prev = new->end;
}
- ed->size -= old->len;
- ed->size += new->len;
+ txt->size -= old->len;
+ txt->size += new->len;
}
static void action_push(Action **stack, Action *action) {
@@ -348,16 +348,16 @@ static Action *action_pop(Action **stack) {
/* allocate a new action, empty the redo stack and push the new action onto
* the undo stack. all further changes will be associated with this action. */
-static Action *action_alloc(Text *ed) {
+static Action *action_alloc(Text *txt) {
Action *old, *new = calloc(1, sizeof(Action));
if (!new)
return NULL;
new->time = time(NULL);
/* throw a away all old redo operations */
- while ((old = action_pop(&ed->redo)))
+ while ((old = action_pop(&txt->redo)))
action_free(old);
- ed->current_action = new;
- action_push(&ed->undo, new);
+ txt->current_action = new;
+ action_push(&txt->undo, new);
return new;
}
@@ -371,16 +371,16 @@ static void action_free(Action *a) {
free(a);
}
-static Piece *piece_alloc(Text *ed) {
+static Piece *piece_alloc(Text *txt) {
Piece *p = calloc(1, sizeof(Piece));
if (!p)
return NULL;
- p->editor = ed;
- p->index = ++ed->piece_count;
- p->global_next = ed->pieces;
- if (ed->pieces)
- ed->pieces->global_prev = p;
- ed->pieces = p;
+ p->text = txt;
+ p->index = ++txt->piece_count;
+ p->global_next = txt->pieces;
+ if (txt->pieces)
+ txt->pieces->global_prev = p;
+ txt->pieces = p;
return p;
}
@@ -391,10 +391,10 @@ static void piece_free(Piece *p) {
p->global_prev->global_next = p->global_next;
if (p->global_next)
p->global_next->global_prev = p->global_prev;
- if (p->editor->pieces == p)
- p->editor->pieces = p->global_next;
- if (p->editor->cache == p)
- p->editor->cache = NULL;
+ if (p->text->pieces == p)
+ p->text->pieces = p->global_next;
+ if (p->text->cache == p)
+ p->text->cache = NULL;
free(p);
}
@@ -413,9 +413,9 @@ static void piece_init(Piece *p, Piece *prev, Piece *next, const char *data, siz
*
* in particular if pos is zero, the begin sentinel piece is returned.
*/
-static Location piece_get_intern(Text *ed, size_t pos) {
+static Location piece_get_intern(Text *txt, size_t pos) {
size_t cur = 0;
- for (Piece *p = &ed->begin; p->next; p = p->next) {
+ for (Piece *p = &txt->begin; p->next; p = p->next) {
if (cur <= pos && pos <= cur + p->len)
return (Location){ .piece = p, .off = pos - cur };
cur += p->len;
@@ -426,9 +426,9 @@ static Location piece_get_intern(Text *ed, size_t pos) {
/* similiar to piece_get_intern but usable as a public API. returns the piece
* holding the text at byte offset pos. never returns a sentinel piece. */
-static Location piece_get_extern(Text *ed, size_t pos) {
+static Location piece_get_extern(Text *txt, size_t pos) {
size_t cur = 0;
- for (Piece *p = ed->begin.next; p->next; p = p->next) {
+ for (Piece *p = txt->begin.next; p->next; p = p->next) {
if (cur <= pos && pos < cur + p->len)
return (Location){ .piece = p, .off = pos - cur };
cur += p->len;
@@ -439,10 +439,10 @@ static Location piece_get_extern(Text *ed, size_t pos) {
/* allocate a new change, associate it with current action or a newly
* allocated one if none exists. */
-static Change *change_alloc(Text *ed) {
- Action *a = ed->current_action;
+static Change *change_alloc(Text *txt) {
+ Action *a = txt->current_action;
if (!a) {
- a = action_alloc(ed);
+ a = action_alloc(txt);
if (!a)
return NULL;
}
@@ -489,23 +489,23 @@ static void change_free(Change *c) {
* | | |short| | existing text | | |
* \-+ <-- +-----+ <-- +---------------+ <-- +-/
*/
-bool text_insert_raw(Text *ed, size_t pos, const char *data, size_t len) {
- if (pos > ed->size)
+bool text_insert_raw(Text *txt, size_t pos, const char *data, size_t len) {
+ if (pos > txt->size)
return false;
- if (pos < ed->lines.pos)
- lineno_cache_invalidate(&ed->lines);
+ if (pos < txt->lines.pos)
+ lineno_cache_invalidate(&txt->lines);
- Location loc = piece_get_intern(ed, pos);
+ Location loc = piece_get_intern(txt, pos);
Piece *p = loc.piece;
size_t off = loc.off;
- if (cache_insert(ed, p, off, data, len))
+ if (cache_insert(txt, p, off, data, len))
return true;
- Change *c = change_alloc(ed);
+ Change *c = change_alloc(txt);
if (!c)
return false;
- if (!(data = buffer_store(ed, data, len)))
+ if (!(data = buffer_store(txt, data, len)))
return false;
Piece *new = NULL;
@@ -513,7 +513,7 @@ bool text_insert_raw(Text *ed, size_t pos, const char *data, size_t len) {
if (off == p->len) {
/* insert between two existing pieces, hence there is nothing to
* remove, just add a new piece holding the extra text */
- if (!(new = piece_alloc(ed)))
+ if (!(new = piece_alloc(txt)))
return false;
piece_init(new, p, p->next, data, len);
span_init(&c->new, new, new);
@@ -524,9 +524,9 @@ bool text_insert_raw(Text *ed, size_t pos, const char *data, size_t len) {
* before the insertion point then one holding the newly inserted
* text and one holding the content after the insertion point.
*/
- Piece *before = piece_alloc(ed);
- new = piece_alloc(ed);
- Piece *after = piece_alloc(ed);
+ Piece *before = piece_alloc(txt);
+ new = piece_alloc(txt);
+ Piece *after = piece_alloc(txt);
if (!before || !new || !after)
return false;
piece_init(before, p->prev, new, p->data, off);
@@ -537,40 +537,40 @@ bool text_insert_raw(Text *ed, size_t pos, const char *data, size_t len) {
span_init(&c->old, p, p);
}
- cache_piece(ed, new);
- span_swap(ed, &c->old, &c->new);
+ cache_piece(txt, new);
+ span_swap(txt, &c->old, &c->new);
return true;
}
-bool text_insert(Text *ed, size_t pos, const char *data) {
- return text_insert_raw(ed, pos, data, strlen(data));
+bool text_insert(Text *txt, size_t pos, const char *data) {
+ return text_insert_raw(txt, pos, data, strlen(data));
}
/* undo all changes of the last action, return whether changes existed */
-bool text_undo(Text *ed) {
- Action *a = action_pop(&ed->undo);
+bool text_undo(Text *txt) {
+ Action *a = action_pop(&txt->undo);
if (!a)
return false;
for (Change *c = a->change; c; c = c->next) {
- span_swap(ed, &c->new, &c->old);
+ span_swap(txt, &c->new, &c->old);
}
- action_push(&ed->redo, a);
- lineno_cache_invalidate(&ed->lines);
+ action_push(&txt->redo, a);
+ lineno_cache_invalidate(&txt->lines);
return true;
}
/* redo all changes of the last action, return whether changes existed */
-bool text_redo(Text *ed) {
- Action *a = action_pop(&ed->redo);
+bool text_redo(Text *txt) {
+ Action *a = action_pop(&txt->redo);
if (!a)
return false;
for (Change *c = a->change; c; c = c->next) {
- span_swap(ed, &c->old, &c->new);
+ span_swap(txt, &c->old, &c->new);
}
- action_push(&ed->undo, a);
- lineno_cache_invalidate(&ed->lines);
+ action_push(&txt->undo, a);
+ lineno_cache_invalidate(&txt->lines);
return true;
}
@@ -578,7 +578,7 @@ bool text_redo(Text *ed) {
* a file called `.filename.tmp` and then atomically moved to its final
* (possibly alredy existing) destination using rename(2).
*/
-int text_save(Text *ed, const char *filename) {
+int text_save(Text *txt, const char *filename) {
size_t len = strlen(filename) + 10;
char tmpname[len];
snprintf(tmpname, len, ".%s.tmp", filename);
@@ -587,29 +587,29 @@ int text_save(Text *ed, const char *filename) {
int fd = open(tmpname, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd == -1)
return -1;
- if (ftruncate(fd, ed->size) == -1)
+ if (ftruncate(fd, txt->size) == -1)
goto err;
- if (ed->size > 0) {
- void *buf = mmap(NULL, ed->size, PROT_WRITE, MAP_SHARED, fd, 0);
+ if (txt->size > 0) {
+ void *buf = mmap(NULL, txt->size, PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED)
goto err;
char *cur = buf;
- text_iterate(ed, it, 0) {
+ text_iterate(txt, it, 0) {
size_t len = it.end - it.start;
memcpy(cur, it.start, len);
cur += len;
}
- if (munmap(buf, ed->size) == -1)
+ if (munmap(buf, txt->size) == -1)
goto err;
}
if (close(fd) == -1)
return -1;
if (rename(tmpname, filename) == -1)
return -1;
- ed->saved_action = ed->undo;
- text_snapshot(ed);
+ txt->saved_action = txt->undo;
+ text_snapshot(txt);
return 0;
err:
close(fd);
@@ -619,43 +619,43 @@ err:
/* load the given file as starting point for further editing operations.
* to start with an empty document, pass NULL as filename. */
Text *text_load(const char *filename) {
- Text *ed = calloc(1, sizeof(Text));
- if (!ed)
+ Text *txt = calloc(1, sizeof(Text));
+ if (!txt)
return NULL;
- ed->begin.index = 1;
- ed->end.index = 2;
- ed->piece_count = 2;
- piece_init(&ed->begin, NULL, &ed->end, NULL, 0);
- piece_init(&ed->end, &ed->begin, NULL, NULL, 0);
- lineno_cache_invalidate(&ed->lines);
+ txt->begin.index = 1;
+ txt->end.index = 2;
+ txt->piece_count = 2;
+ piece_init(&txt->begin, NULL, &txt->end, NULL, 0);
+ piece_init(&txt->end, &txt->begin, NULL, NULL, 0);
+ lineno_cache_invalidate(&txt->lines);
if (filename) {
- ed->filename = strdup(filename);
- ed->fd = open(filename, O_RDONLY);
- if (ed->fd == -1)
+ txt->filename = strdup(filename);
+ txt->fd = open(filename, O_RDONLY);
+ if (txt->fd == -1)
goto out;
- if (fstat(ed->fd, &ed->info) == -1)
+ if (fstat(txt->fd, &txt->info) == -1)
goto out;
- if (!S_ISREG(ed->info.st_mode))
+ if (!S_ISREG(txt->info.st_mode))
goto out;
// XXX: use lseek(fd, 0, SEEK_END); instead?
- ed->buf.size = ed->info.st_size;
- ed->buf.data = mmap(NULL, ed->info.st_size, PROT_READ, MAP_SHARED, ed->fd, 0);
- if (ed->buf.data == MAP_FAILED)
+ txt->buf.size = txt->info.st_size;
+ txt->buf.data = mmap(NULL, txt->info.st_size, PROT_READ, MAP_SHARED, txt->fd, 0);
+ if (txt->buf.data == MAP_FAILED)
goto out;
- Piece *p = piece_alloc(ed);
+ Piece *p = piece_alloc(txt);
if (!p)
goto out;
- piece_init(&ed->begin, NULL, p, NULL, 0);
- piece_init(p, &ed->begin, &ed->end, ed->buf.data, ed->buf.size);
- piece_init(&ed->end, p, NULL, NULL, 0);
- ed->size = ed->buf.size;
+ piece_init(&txt->begin, NULL, p, NULL, 0);
+ piece_init(p, &txt->begin, &txt->end, txt->buf.data, txt->buf.size);
+ piece_init(&txt->end, p, NULL, NULL, 0);
+ txt->size = txt->buf.size;
}
- return ed;
+ return txt;
out:
- if (ed->fd > 2)
- close(ed->fd);
- text_free(ed);
+ if (txt->fd > 2)
+ close(txt->fd);
+ text_free(txt);
return NULL;
}
@@ -669,8 +669,8 @@ static void print_piece(Piece *p) {
write(2, "\n", 1);
}
-void text_debug(Text *ed) {
- for (Piece *p = &ed->begin; p; p = p->next) {
+void text_debug(Text *txt) {
+ for (Piece *p = &txt->begin; p; p = p->next) {
print_piece(p);
}
}
@@ -689,22 +689,22 @@ void text_debug(Text *ed) {
* | | | exi| |t | | |
* \-+ <-- +----+ <-- +--+ <-- +-/
*/
-bool text_delete(Text *ed, size_t pos, size_t len) {
+bool text_delete(Text *txt, size_t pos, size_t len) {
if (len == 0)
return true;
- if (pos + len > ed->size)
+ if (pos + len > txt->size)
return false;
- if (pos < ed->lines.pos)
- lineno_cache_invalidate(&ed->lines);
+ if (pos < txt->lines.pos)
+ lineno_cache_invalidate(&txt->lines);
- Location loc = piece_get_intern(ed, pos);
+ Location loc = piece_get_intern(txt, pos);
Piece *p = loc.piece;
size_t off = loc.off;
- if (cache_delete(ed, p, off, len))
+ if (cache_delete(txt, p, off, len))
return true;
size_t cur; // how much has already been deleted
bool midway_start = false, midway_end = false;
- Change *c = change_alloc(ed);
+ Change *c = change_alloc(txt);
if (!c)
return false;
Piece *before, *after; // unmodified pieces before / after deletion point
@@ -719,7 +719,7 @@ bool text_delete(Text *ed, size_t pos, size_t len) {
midway_start = true;
cur = p->len - off;
start = p;
- before = piece_alloc(ed);
+ before = piece_alloc(txt);
}
/* skip all pieces which fall into deletion range */
while (cur < len) {
@@ -735,7 +735,7 @@ bool text_delete(Text *ed, size_t pos, size_t len) {
/* deletion stops midway through a piece */
midway_end = true;
end = p;
- after = piece_alloc(ed);
+ after = piece_alloc(txt);
piece_init(after, before, p->next, p->data + p->len - (cur - len), cur - len);
}
@@ -758,46 +758,46 @@ bool text_delete(Text *ed, size_t pos, size_t len) {
span_init(&c->new, new_start, new_end);
span_init(&c->old, start, end);
- span_swap(ed, &c->old, &c->new);
+ span_swap(txt, &c->old, &c->new);
return true;
}
/* preserve the current text content such that it can be restored by
* means of undo/redo operations */
-void text_snapshot(Text *ed) {
- ed->current_action = NULL;
- ed->cache = NULL;
+void text_snapshot(Text *txt) {
+ txt->current_action = NULL;
+ txt->cache = NULL;
}
-void text_free(Text *ed) {
- if (!ed)
+void text_free(Text *txt) {
+ if (!txt)
return;
Action *a;
- while ((a = action_pop(&ed->undo)))
+ while ((a = action_pop(&txt->undo)))
action_free(a);
- while ((a = action_pop(&ed->redo)))
+ while ((a = action_pop(&txt->redo)))
action_free(a);
- for (Piece *next, *p = ed->pieces; p; p = next) {
+ for (Piece *next, *p = txt->pieces; p; p = next) {
next = p->global_next;
piece_free(p);
}
- for (Buffer *next, *buf = ed->buffers; buf; buf = next) {
+ for (Buffer *next, *buf = txt->buffers; buf; buf = next) {
next = buf->next;
buffer_free(buf);
}
- if (ed->buf.data)
- munmap(ed->buf.data, ed->buf.size);
+ if (txt->buf.data)
+ munmap(txt->buf.data, txt->buf.size);
- free((char*)ed->filename);
- free(ed);
+ free((char*)txt->filename);
+ free(txt);
}
-bool text_modified(Text *ed) {
- return ed->saved_action != ed->undo;
+bool text_modified(Text *txt) {
+ return txt->saved_action != txt->undo;
}
static bool text_iterator_init(Iterator *it, size_t pos, Piece *p, size_t off) {
@@ -811,9 +811,9 @@ static bool text_iterator_init(Iterator *it, size_t pos, Piece *p, size_t off) {
return text_iterator_valid(it);
}
-Iterator text_iterator_get(Text *ed, size_t pos) {
+Iterator text_iterator_get(Text *txt, size_t pos) {
Iterator it;
- Location loc = piece_get_extern(ed, pos);
+ Location loc = piece_get_extern(txt, pos);
text_iterator_init(&it, pos, loc.piece, loc.off);
return it;
}
@@ -836,7 +836,7 @@ bool text_iterator_prev(Iterator *it) {
bool text_iterator_valid(const Iterator *it) {
/* filter out sentinel nodes */
- return it->piece && it->piece->editor;
+ return it->piece && it->piece->text;
}
bool text_iterator_byte_next(Iterator *it, char *b) {
@@ -891,16 +891,16 @@ bool text_iterator_char_prev(Iterator *it, char *c) {
return false;
}
-bool text_byte_get(Text *ed, size_t pos, char *buf) {
- return text_bytes_get(ed, pos, 1, buf);
+bool text_byte_get(Text *txt, size_t pos, char *buf) {
+ return text_bytes_get(txt, pos, 1, buf);
}
-size_t text_bytes_get(Text *ed, size_t pos, size_t len, char *buf) {
+size_t text_bytes_get(Text *txt, size_t pos, size_t len, char *buf) {
if (!buf)
return 0;
char *cur = buf;
size_t rem = len;
- text_iterate(ed, it, pos) {
+ text_iterate(txt, it, pos) {
if (rem == 0)
break;
size_t piece_len = it.end - it.text;
@@ -913,14 +913,14 @@ size_t text_bytes_get(Text *ed, size_t pos, size_t len, char *buf) {
return len - rem;
}
-size_t text_size(Text *ed) {
- return ed->size;
+size_t text_size(Text *txt) {
+ return txt->size;
}
/* count the number of new lines '\n' in range [pos, pos+len) */
-static size_t lines_count(Text *ed, size_t pos, size_t len) {
+static size_t lines_count(Text *txt, size_t pos, size_t len) {
size_t lines = 0;
- text_iterate(ed, it, pos) {
+ text_iterate(txt, it, pos) {
const char *start = it.text;
while (len > 0 && start < it.end) {
size_t n = MIN(len, (size_t)(it.end - start));
@@ -941,8 +941,8 @@ static size_t lines_count(Text *ed, size_t pos, size_t len) {
}
/* skip n lines forward and return position afterwards */
-static size_t lines_skip_forward(Text *ed, size_t pos, size_t lines) {
- text_iterate(ed, it, pos) {
+static size_t lines_skip_forward(Text *txt, size_t pos, size_t lines) {
+ text_iterate(txt, it, pos) {
const char *start = it.text;
while (lines > 0 && start < it.end) {
size_t n = it.end - start;
@@ -970,58 +970,58 @@ static void lineno_cache_invalidate(LineCache *cache) {
cache->lineno = 1;
}
-size_t text_pos_by_lineno(Text *ed, size_t lineno) {
- LineCache *cache = &ed->lines;
+size_t text_pos_by_lineno(Text *txt, size_t lineno) {
+ LineCache *cache = &txt->lines;
if (lineno <= 1)
return 0;
if (lineno > cache->lineno) {
- cache->pos = lines_skip_forward(ed, cache->pos, lineno - cache->lineno);
+ cache->pos = lines_skip_forward(txt, cache->pos, lineno - cache->lineno);
} else if (lineno < cache->lineno) {
#if 0
// TODO does it make sense to scan memory backwards here?
size_t diff = cache->lineno - lineno;
if (diff < lineno) {
- lines_skip_backward(ed, cache->pos, diff);
+ lines_skip_backward(txt, cache->pos, diff);
} else
#endif
- cache->pos = lines_skip_forward(ed, 0, lineno - 1);
+ cache->pos = lines_skip_forward(txt, 0, lineno - 1);
}
cache->lineno = lineno;
return cache->pos;
}
-size_t text_lineno_by_pos(Text *ed, size_t pos) {
- LineCache *cache = &ed->lines;
- if (pos > ed->size)
- pos = ed->size;
+size_t text_lineno_by_pos(Text *txt, size_t pos) {
+ LineCache *cache = &txt->lines;
+ if (pos > txt->size)
+ pos = txt->size;
if (pos < cache->pos) {
size_t diff = cache->pos - pos;
if (diff < pos)
- cache->lineno -= lines_count(ed, pos, diff);
+ cache->lineno -= lines_count(txt, pos, diff);
else
- cache->lineno = lines_count(ed, 0, pos) + 1;
+ cache->lineno = lines_count(txt, 0, pos) + 1;
} else if (pos > cache->pos) {
- cache->lineno += lines_count(ed, cache->pos, pos - cache->pos);
+ cache->lineno += lines_count(txt, cache->pos, pos - cache->pos);
}
cache->pos = pos;
return cache->lineno;
}
-void text_mark_set(Text *ed, Mark mark, size_t pos) {
- if (mark < 0 || mark >= LENGTH(ed->marks))
+void text_mark_set(Text *txt, Mark mark, size_t pos) {
+ if (mark < 0 || mark >= LENGTH(txt->marks))
return;
- Location loc = piece_get_extern(ed, pos);
+ Location loc = piece_get_extern(txt, pos);
if (!loc.piece)
return;
- ed->marks[mark] = loc.piece->data + loc.off;
+ txt->marks[mark] = loc.piece->data + loc.off;
}
-size_t text_mark_get(Text *ed, Mark mark) {
- if (mark < 0 || mark >= LENGTH(ed->marks))
+size_t text_mark_get(Text *txt, Mark mark) {
+ if (mark < 0 || mark >= LENGTH(txt->marks))
return -1;
- const char *pos = ed->marks[mark];
+ const char *pos = txt->marks[mark];
size_t cur = 0;
- for (Piece *p = ed->begin.next; p->next; p = p->next) {
+ for (Piece *p = txt->begin.next; p->next; p = p->next) {
if (p->data <= pos && pos < p->data + p->len)
return cur + (pos - p->data);
cur += p->len;
@@ -1030,19 +1030,19 @@ size_t text_mark_get(Text *ed, Mark mark) {
return -1;
}
-void text_mark_clear(Text *ed, Mark mark) {
- if (mark < 0 || mark >= LENGTH(ed->marks))
+void text_mark_clear(Text *txt, Mark mark) {
+ if (mark < 0 || mark >= LENGTH(txt->marks))
return;
- ed->marks[mark] = NULL;
+ txt->marks[mark] = NULL;
}
-void text_mark_clear_all(Text *ed) {
- for (Mark mark = 0; mark < LENGTH(ed->marks); mark++)
- text_mark_clear(ed, mark);
+void text_mark_clear_all(Text *txt) {
+ for (Mark mark = 0; mark < LENGTH(txt->marks); mark++)
+ text_mark_clear(txt, mark);
}
-const char *text_filename(Text *ed) {
- return ed->filename;
+const char *text_filename(Text *txt) {
+ return txt->filename;
}
Regex *text_regex_new(void) {
@@ -1058,11 +1058,11 @@ void text_regex_free(Regex *r) {
regfree(&r->regex);
}
-int text_search_forward(Text *ed, size_t pos, size_t len, Regex *r, size_t nmatch, RegexMatch pmatch[], int eflags) {
+int text_search_forward(Text *txt, size_t pos, size_t len, Regex *r, size_t nmatch, RegexMatch pmatch[], int eflags) {
char *buf = malloc(len + 1);
if (!buf)
return REG_NOMATCH;
- len = text_bytes_get(ed, pos, len, buf);
+ len = text_bytes_get(txt, pos, len, buf);
buf[len] = '\0';
regmatch_t match[nmatch];
int ret = regexec(&r->regex, buf, nmatch, match, eflags);
@@ -1076,11 +1076,11 @@ int text_search_forward(Text *ed, size_t pos, size_t len, Regex *r, size_t nmatc
return ret;
}
-int text_search_backward(Text *ed, size_t pos, size_t len, Regex *r, size_t nmatch, RegexMatch pmatch[], int eflags) {
+int text_search_backward(Text *txt, size_t pos, size_t len, Regex *r, size_t nmatch, RegexMatch pmatch[], int eflags) {
char *buf = malloc(len + 1);
if (!buf)
return REG_NOMATCH;
- len = text_bytes_get(ed, pos, len, buf);
+ len = text_bytes_get(txt, pos, len, buf);
buf[len] = '\0';
regmatch_t match[nmatch];
char *cur = buf;