summaryrefslogtreecommitdiff
path: root/src/dged/location.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/location.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/location.c')
-rw-r--r--src/dged/location.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/dged/location.c b/src/dged/location.c
new file mode 100644
index 0000000..cac0333
--- /dev/null
+++ b/src/dged/location.c
@@ -0,0 +1,68 @@
+#include "location.h"
+
+bool location_is_between(struct location location, struct location start,
+ struct location end) {
+ if (location.line >= start.line && location.line <= end.line) {
+ if (location.line == end.line && location.col <= end.col &&
+ location.line == start.line && location.col >= start.col) {
+ // only one line
+ return true;
+ } else if (location.line == start.line && location.line != end.line &&
+ location.col >= start.col) {
+ // we are on the first line
+ return true;
+ } else if (location.line == end.line && location.line != start.line &&
+ location.col <= end.col) {
+ // we are on the last line
+ return true;
+ } else if (location.line != end.line && location.line != start.line) {
+ // we are on lines in between
+ return true;
+ }
+ }
+ return false;
+}
+
+int location_compare(struct location l1, struct location l2) {
+ if (l1.line < l2.line) {
+ return -1;
+ } else if (l1.line > l2.line) {
+ return 1;
+ } else {
+ if (l1.col < l2.col) {
+ return -1;
+ } else if (l1.col > l2.col) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+}
+
+struct region region_new(struct location begin, struct location end) {
+ struct region reg = {.begin = begin, .end = end};
+
+ if (end.line < begin.line ||
+ (end.line == begin.line && end.col < begin.col)) {
+ reg.begin = end;
+ reg.end = begin;
+ }
+
+ return reg;
+}
+
+bool region_has_size(struct region region) {
+ return region.end.line != region.begin.line ||
+ (region.end.line == region.begin.line &&
+ region.begin.col != region.end.col);
+}
+
+bool region_is_inside(struct region region, struct location location) {
+ return location_is_between(location, region.begin, region.end);
+}
+
+bool region_is_inside_rect(struct region region, struct location location) {
+ return location.line >= region.begin.line &&
+ location.line <= region.end.line && location.col >= region.begin.col &&
+ location.col <= region.end.col;
+}