aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2014-09-13 22:25:06 +0200
committerMarc André Tanner <mat@brain-dump.org>2014-09-13 22:29:36 +0200
commitc17ca10e11db30f4b4924b69dbf3b1693025d4dc (patch)
treedb8f9ed1b95d895e111f68181ab8433b6349639b
parentc94579526c082e75b9ee8ec2fc189540884ac039 (diff)
downloadvis-c17ca10e11db30f4b4924b69dbf3b1693025d4dc.tar.gz
vis-c17ca10e11db30f4b4924b69dbf3b1693025d4dc.tar.xz
Introduce and use EPOS instead of (size_t)-1
-rw-r--r--editor.c2
-rw-r--r--text.c8
-rw-r--r--text.h4
-rw-r--r--vis.c12
-rw-r--r--window.c14
5 files changed, 21 insertions, 19 deletions
diff --git a/editor.c b/editor.c
index c228f0e..7ca69f1 100644
--- a/editor.c
+++ b/editor.c
@@ -493,7 +493,7 @@ static void editor_prompt_update(Prompt *prompt) {
static void editor_prompt_clear(Prompt *prompt) {
Text *text = prompt->win->text;
- while (text_undo(text) != (size_t)-1);
+ while (text_undo(text) != EPOS);
window_cursor_to(prompt->win->win, 0);
}
diff --git a/text.c b/text.c
index a9685e4..4f1f28e 100644
--- a/text.c
+++ b/text.c
@@ -1087,8 +1087,8 @@ int text_search_range_forward(Text *txt, size_t pos, size_t len, Regex *r, size_
int ret = regexec(&r->regex, buf, nmatch, match, eflags);
if (!ret) {
for (size_t i = 0; i < nmatch; i++) {
- pmatch[i].start = match[i].rm_so == -1 ? (size_t)-1 : pos + match[i].rm_so;
- pmatch[i].end = match[i].rm_eo == -1 ? (size_t)-1 : pos + match[i].rm_eo;
+ pmatch[i].start = match[i].rm_so == -1 ? EPOS : pos + match[i].rm_so;
+ pmatch[i].end = match[i].rm_eo == -1 ? EPOS : pos + match[i].rm_eo;
}
}
free(buf);
@@ -1107,8 +1107,8 @@ int text_search_range_backward(Text *txt, size_t pos, size_t len, Regex *r, size
while (!regexec(&r->regex, cur, nmatch, match, eflags)) {
ret = 0;
for (size_t i = 0; i < nmatch; i++) {
- pmatch[i].start = match[i].rm_so == -1 ? (size_t)-1 : pos + (size_t)(cur - buf) + match[i].rm_so;
- pmatch[i].end = match[i].rm_eo == -1 ? (size_t)-1 : pos + (size_t)(cur - buf) + match[i].rm_eo;
+ pmatch[i].start = match[i].rm_so == -1 ? EPOS : pos + (size_t)(cur - buf) + match[i].rm_so;
+ pmatch[i].end = match[i].rm_eo == -1 ? EPOS : pos + (size_t)(cur - buf) + match[i].rm_eo;
}
cur += match[0].rm_eo;
}
diff --git a/text.h b/text.h
index 0dadbb6..f8b66ee 100644
--- a/text.h
+++ b/text.h
@@ -4,6 +4,8 @@
#include <stdbool.h>
#include <stddef.h>
+#define EPOS ((size_t)-1) /* invalid position */
+
typedef size_t Filepos;
typedef struct {
@@ -36,7 +38,7 @@ bool text_insert_raw(Text*, size_t pos, const char *data, size_t len);
bool text_delete(Text*, size_t pos, size_t len);
void text_snapshot(Text*);
/* undo/redos to the last snapshoted state. returns the position where
- * the change occured or (size_t)-1 if nothing could be undo/redo. */
+ * the change occured or EPOS if nothing could be undo/redo. */
size_t text_undo(Text*);
size_t text_redo(Text*);
diff --git a/vis.c b/vis.c
index 4be43e9..dbdee71 100644
--- a/vis.c
+++ b/vis.c
@@ -621,7 +621,7 @@ static void mark_line(const Arg *arg) {
static void undo(const Arg *arg) {
size_t pos = text_undo(vis->win->text);
- if (pos != (size_t)-1) {
+ if (pos != EPOS) {
window_cursor_to(vis->win->win, pos);
/* redraw all windows in case some display the same file */
editor_draw(vis);
@@ -630,7 +630,7 @@ static void undo(const Arg *arg) {
static void redo(const Arg *arg) {
size_t pos = text_redo(vis->win->text);
- if (pos != (size_t)-1) {
+ if (pos != EPOS) {
window_cursor_to(vis->win->win, pos);
/* redraw all windows in case some display the same file */
editor_draw(vis);
@@ -804,11 +804,11 @@ static void action_do(Action *a) {
pos = a->movement->win(win);
else
pos = a->movement->cmd(&a->arg);
- if (pos == (size_t)-1)
+ if (pos == EPOS)
break;
}
- if (pos == (size_t)-1) {
+ if (pos == EPOS) {
c.range.start = start;
c.range.end = start;
} else {
@@ -832,7 +832,7 @@ static void action_do(Action *a) {
for (int i = 0; i < a->count; i++) {
r = a->textobj->range(txt, pos);
// TODO range_valid?
- if (r.start == (size_t)-1 || r.end == (size_t)-1)
+ if (r.start == EPOS || r.end == EPOS)
break;
if (a->textobj->type == OUTER) {
r.start--;
@@ -851,7 +851,7 @@ static void action_do(Action *a) {
}
} else if (mode == &vis_modes[VIS_MODE_VISUAL]) {
c.range = window_selection_get(win);
- if (c.range.start == (size_t)-1 || c.range.end == (size_t)-1)
+ if (c.range.start == EPOS || c.range.end == EPOS)
c.range.start = c.range.end = pos;
}
diff --git a/window.c b/window.c
index dc1cb6d..3b579d6 100644
--- a/window.c
+++ b/window.c
@@ -84,7 +84,7 @@ static bool window_scroll_lines_down(Win *win, int n);
static bool window_scroll_lines_up(Win *win, int n);
void window_selection_clear(Win *win) {
- win->sel.start = win->sel.end = (size_t)-1;
+ win->sel.start = win->sel.end = EPOS;
window_draw(win);
window_cursor_update(win);
curs_set(1);
@@ -113,11 +113,11 @@ static void window_clear(Win *win) {
Filerange window_selection_get(Win *win) {
Filerange sel = win->sel;
- if (sel.start == (size_t)-1) {
- sel.end = (size_t)-1;
- } else if (sel.end == (size_t)-1) {
- sel.start = (size_t)-1;
- } else if (sel.start > sel.end) {
+ if (sel.start == EPOS || sel.end == EPOS) {
+ sel.start = sel.end = EPOS;
+ return sel;
+ }
+ if (sel.start > sel.end) {
size_t tmp = sel.start;
sel.start = sel.end;
sel.end = tmp;
@@ -254,7 +254,7 @@ void window_cursor_getxy(Win *win, size_t *lineno, size_t *col) {
* its changes. */
static size_t window_cursor_update(Win *win) {
Cursor *cursor = &win->cursor;
- if (win->sel.start != (size_t)-1) {
+ if (win->sel.start != EPOS) {
win->sel.end = cursor->pos;
window_draw(win);
}