From ef876ce4eaf3ef5f8e406205b2d797efccea293f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 13 Aug 2014 21:37:48 +0200 Subject: Add support for file marks --- editor.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'editor.c') diff --git a/editor.c b/editor.c index 2002264..d1c8d62 100644 --- a/editor.c +++ b/editor.c @@ -14,6 +14,7 @@ #define MAX(a, b) ((a) < (b) ? (b) : (a)) #define MIN(a, b) ((a) > (b) ? (b) : (a)) +#define LENGTH(x) ((int)(sizeof (x) / sizeof *(x))) #define BUFFER_SIZE (1 << 20) @@ -102,6 +103,7 @@ struct Editor { struct stat info; /* stat as proped on load time */ int fd; /* the file descriptor of the original mmap-ed data */ LineCache lines; /* mapping between absolute pos in bytes and logical line breaks */ + size_t marks[26]; /* a mark is a byte offset from the start of the document */ }; /* buffer management */ @@ -467,6 +469,10 @@ bool editor_insert_raw(Editor *ed, size_t pos, const char *data, size_t len) { return false; if (pos < ed->lines.pos) lineno_cache_invalidate(&ed->lines); + for (Mark mark = 0; mark < LENGTH(ed->marks); mark++) { + if (ed->marks[mark] > pos) + ed->marks[mark] += len; + } Location loc = piece_get(ed, pos); Piece *p = loc.piece; @@ -667,6 +673,18 @@ bool editor_delete(Editor *ed, size_t pos, size_t len) { return false; if (pos < ed->lines.pos) lineno_cache_invalidate(&ed->lines); + for (Mark mark = 0; mark < LENGTH(ed->marks); mark++) { + if (ed->marks[mark] > pos) { + if (ed->marks[mark] > pos + len) { + /* whole delete range before mark position */ + ed->marks[mark] -= len; + } else { + /* mark lies within delete range */ + editor_mark_clear(ed, mark); + } + } + } + Location loc = piece_get(ed, pos); Piece *p = loc.piece; size_t off = loc.off; -- cgit v1.2.3