diff options
author | Thomas White <taw@bitwiz.org.uk> | 2014-10-14 21:53:02 +0200 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2014-10-14 21:53:02 +0200 |
commit | ddca48bf3b71bd9795b00dc00c1543d7be1032f5 (patch) | |
tree | ce5e80892dd989880ff92e97a4bfc8c824e7f15c /src/sc_editor.c | |
parent | 17d143eb11e3db5f9afdd7873cc4e8e6554981b6 (diff) |
Safeguards against crashing during editing
Diffstat (limited to 'src/sc_editor.c')
-rw-r--r-- | src/sc_editor.c | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/sc_editor.c b/src/sc_editor.c index aeebf8a..69a9cf7 100644 --- a/src/sc_editor.c +++ b/src/sc_editor.c @@ -491,9 +491,39 @@ static gboolean draw_sig(GtkWidget *da, cairo_t *cr, static void fixup_cursor(SCEditor *e) { + struct frame *fr; + struct wrap_line *sline; struct wrap_box *sbox; - sbox = &e->cursor_frame->lines[e->cursor_line].boxes[e->cursor_box]; + fr = e->cursor_frame; + + if ( e->cursor_line >= fr->n_lines ) { + /* We find ourselves on a line which doesn't exist */ + e->cursor_line = fr->n_lines-1; + e->cursor_box = fr->lines[fr->n_lines-1].n_boxes-1; + } + + sline = &fr->lines[e->cursor_line]; + + if ( e->cursor_box >= sline->n_boxes ) { + + /* We find ourselves in a box which doesn't exist */ + + if ( e->cursor_line > fr->n_lines-1 ) { + /* This isn't the last line, so go to the first box of + * the next line */ + e->cursor_line++; + e->cursor_box = 0; + sline = &e->cursor_frame->lines[e->cursor_line]; + } else { + /* There are no more lines, so just go to the end */ + e->cursor_line = fr->n_lines-1; + sline = &e->cursor_frame->lines[e->cursor_line]; + e->cursor_box = sline->n_boxes-1; + } + } + + sbox = &sline->boxes[e->cursor_box]; if ( e->cursor_pos > sbox->len_chars ) { advance_cursor(e); @@ -577,6 +607,7 @@ static void do_backspace(struct frame *fr, SCEditor *e) // } while ( (scbl != fbox->scblock) && (scbl != NULL) ); rerender_slide(e); + fixup_cursor(e); redraw_editor(e); } |