aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--editor.c6
-rw-r--r--text.c8
-rw-r--r--window.c18
3 files changed, 22 insertions, 10 deletions
diff --git a/editor.c b/editor.c
index 9d520d8..455ed5b 100644
--- a/editor.c
+++ b/editor.c
@@ -66,7 +66,7 @@ static void editor_windows_arrange_horizontal(Editor *ed) {
int n = 0, x = 0, y = 0;
for (EditorWin *win = ed->windows; win; win = win->next)
n++;
- int height = ed->height / n;
+ int height = ed->height / MAX(1, n);
for (EditorWin *win = ed->windows; win; win = win->next) {
editor_window_resize(win, ed->width, win->next ? height : ed->height - y);
editor_window_move(win, x, y);
@@ -78,7 +78,7 @@ static void editor_windows_arrange_vertical(Editor *ed) {
int n = 0, x = 0, y = 0;
for (EditorWin *win = ed->windows; win; win = win->next)
n++;
- int width = ed->width / n - 1;
+ int width = (ed->width / MAX(1, n)) - 1;
for (EditorWin *win = ed->windows; win; win = win->next) {
editor_window_resize(win, win->next ? width : ed->width - x, ed->height);
editor_window_move(win, x, y);
@@ -126,6 +126,8 @@ static void editor_window_split_internal(Editor *ed, const char *filename) {
}
if (sel) {
EditorWin *win = editor_window_new_text(ed, sel->text);
+ if (!win)
+ return;
win->text = sel->text;
window_syntax_set(win->win, window_syntax_get(sel->win));
window_cursor_to(win->win, window_cursor_get(sel->win));
diff --git a/text.c b/text.c
index 3b73a62..56332db 100644
--- a/text.c
+++ b/text.c
@@ -499,6 +499,8 @@ bool text_insert(Text *txt, size_t pos, const char *data, size_t len) {
Location loc = piece_get_intern(txt, pos);
Piece *p = loc.piece;
+ if (!p)
+ return false;
size_t off = loc.off;
if (cache_insert(txt, p, off, data, len))
return true;
@@ -703,6 +705,8 @@ bool text_delete(Text *txt, size_t pos, size_t len) {
Location loc = piece_get_intern(txt, pos);
Piece *p = loc.piece;
+ if (!p)
+ return false;
size_t off = loc.off;
if (cache_delete(txt, p, off, len))
return true;
@@ -724,6 +728,8 @@ bool text_delete(Text *txt, size_t pos, size_t len) {
cur = p->len - off;
start = p;
before = piece_alloc(txt);
+ if (!before)
+ return false;
}
/* skip all pieces which fall into deletion range */
while (cur < len) {
@@ -740,6 +746,8 @@ bool text_delete(Text *txt, size_t pos, size_t len) {
midway_end = true;
end = p;
after = piece_alloc(txt);
+ if (!after)
+ return false;
piece_init(after, before, p->next, p->data + p->len - (cur - len), cur - len);
}
diff --git a/window.c b/window.c
index 7329077..eb1c8e7 100644
--- a/window.c
+++ b/window.c
@@ -106,7 +106,7 @@ static void window_clear(Win *win) {
prev->next = line;
prev = line;
}
- win->bottomline = prev;
+ win->bottomline = prev ? prev : win->topline;
win->line = win->topline;
win->col = 0;
}
@@ -338,11 +338,13 @@ void window_draw(Win *win) {
Char c;
/* current selection */
Filerange sel = window_selection_get(win);
+ /* syntax definition to use */
+ Syntax *syntax = win->syntax;
/* matched tokens for each syntax rule */
- regmatch_t match[SYNTAX_RULES][1];
- if (win->syntax) {
- for (int i = 0; i < LENGTH(win->syntax->rules); i++) {
- SyntaxRule *rule = &win->syntax->rules[i];
+ regmatch_t match[syntax ? LENGTH(syntax->rules) : 1][1];
+ if (syntax) {
+ for (int i = 0; i < LENGTH(syntax->rules); i++) {
+ SyntaxRule *rule = &syntax->rules[i];
if (!rule->rule)
break;
if (regexec(&rule->regex, cur, 1, match[i], 0) ||
@@ -357,10 +359,10 @@ void window_draw(Win *win) {
int attrs = COLOR_PAIR(0) | A_NORMAL;
- if (win->syntax) {
+ if (syntax) {
size_t off = cur - text; /* number of already processed bytes */
- for (int i = 0; i < LENGTH(win->syntax->rules); i++) {
- SyntaxRule *rule = &win->syntax->rules[i];
+ for (int i = 0; i < LENGTH(syntax->rules); i++) {
+ SyntaxRule *rule = &syntax->rules[i];
if (!rule->rule)
break;
if (match[i][0].rm_so == -1)