summaryrefslogtreecommitdiff
path: root/src/dged/text.c
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2023-07-12 16:20:50 +0200
committerAlbert Cervin <albert@acervin.com>2023-10-19 22:41:33 +0200
commit54c9b4b533210b77be998f458ff96bdc54272f64 (patch)
treeeb434343bb1083172af50b7372d1e2745af00f8f /src/dged/text.c
parent3a8ae83aa13636679c151027cace905fa87ebd8e (diff)
downloaddged-54c9b4b533210b77be998f458ff96bdc54272f64.tar.gz
dged-54c9b4b533210b77be998f458ff96bdc54272f64.tar.xz
dged-54c9b4b533210b77be998f458ff96bdc54272f64.zip
big buffer/buffer_view rework
A buffer is only the text and the corresponding operation. A buffer view holds information about scroll, dot and mark positions. One way to think about it is that a buffer is stateless whereas a buffer view is stateful.
Diffstat (limited to 'src/dged/text.c')
-rw-r--r--src/dged/text.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/dged/text.c b/src/dged/text.c
index f8ba72d..b3eb4ad 100644
--- a/src/dged/text.c
+++ b/src/dged/text.c
@@ -8,6 +8,7 @@
#include "display.h"
#include "signal.h"
#include "utf8.h"
+#include "vec.h"
enum flags {
LineChanged = 1 << 0,
@@ -20,11 +21,18 @@ struct line {
uint32_t nchars;
};
+struct text_property_entry {
+ struct location start;
+ struct location end;
+ struct text_property property;
+};
+
struct text {
// raw bytes without any null terminators
struct line *lines;
uint32_t nlines;
uint32_t capacity;
+ VEC(struct text_property_entry) properties;
};
struct text *text_create(uint32_t initial_capacity) {
@@ -33,6 +41,8 @@ struct text *text_create(uint32_t initial_capacity) {
txt->capacity = initial_capacity;
txt->nlines = 0;
+ VEC_INIT(&txt->properties, 32);
+
return txt;
}
@@ -60,6 +70,7 @@ void text_clear(struct text *text) {
}
text->nlines = 0;
+ text_clear_properties(text);
}
// given `char_idx` as a character index, return the byte index
@@ -494,3 +505,32 @@ struct text_chunk text_get_region(struct text *text, uint32_t start_line,
bool text_line_contains_unicode(struct text *text, uint32_t line) {
return text->lines[line].nbytes != text->lines[line].nchars;
}
+
+void text_add_property(struct text *text, struct location start,
+ struct location end, struct text_property property) {
+ struct text_property_entry entry = {
+ .start = start,
+ .end = end,
+ .property = property,
+ };
+ VEC_PUSH(&text->properties, entry);
+}
+
+void text_get_properties(struct text *text, struct location location,
+ struct text_property **properties,
+ uint32_t max_nproperties, uint32_t *nproperties) {
+ uint32_t nres = 0;
+ VEC_FOR_EACH(&text->properties, struct text_property_entry * prop) {
+ if (location_is_between(location, prop->start, prop->end)) {
+ properties[nres] = &prop->property;
+ ++nres;
+
+ if (nres == max_nproperties) {
+ break;
+ }
+ }
+ }
+ *nproperties = nres;
+}
+
+void text_clear_properties(struct text *text) { VEC_CLEAR(&text->properties); }