diff options
author | Thomas White <taw@physics.org> | 2017-10-08 21:24:15 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2017-10-08 21:24:15 +0200 |
commit | 54adeb60b85641f6c1d968200eeb5e5d705c51ea (patch) | |
tree | 331b3371c2d690f86b59d78a7afa8da1afe32dc6 /src | |
parent | a86853b86674d1ecf917665fd8323b8e48c919d9 (diff) |
Delete text when inserting with block selected
Diffstat (limited to 'src')
-rw-r--r-- | src/sc_editor.c | 121 |
1 files changed, 62 insertions, 59 deletions
diff --git a/src/sc_editor.c b/src/sc_editor.c index 7fbf15e..a60112d 100644 --- a/src/sc_editor.c +++ b/src/sc_editor.c @@ -664,6 +664,65 @@ static void check_cursor_visible(SCEditor *e) } +static void do_backspace(struct frame *fr, SCEditor *e) +{ + double wrapw = e->cursor_frame->w - e->cursor_frame->pad_l - e->cursor_frame->pad_r; + + if ( e->sel_active ) { + + /* Delete the selected block */ + delete_text_from_frame(e->cursor_frame, e->sel_start, e->sel_end, wrapw); + + /* Cursor goes at start of deletion */ + sort_positions(&e->sel_start, &e->sel_end); + e->cursor_para = e->sel_start.para; + e->cursor_pos = e->sel_start.pos; + e->cursor_trail = e->sel_start.trail; + e->sel_active = 0; + + } else { + + /* Delete one character */ + size_t old_pos = e->cursor_pos; + int old_para = e->cursor_para; + int old_trail = e->cursor_trail; + + int new_para = old_para; + size_t new_pos = old_pos; + int new_trail = old_trail; + + Paragraph *para = e->cursor_frame->paras[old_para]; + + cursor_moveh(e->cursor_frame, &new_para, &new_pos, &new_trail, -1); + cursor_moveh(e->cursor_frame, &e->cursor_para, &e->cursor_pos, &e->cursor_trail, -1); + + if ( e->cursor_para != old_para ) { + + merge_paragraphs(e->cursor_frame, e->cursor_para); + wrap_paragraph(e->cursor_frame->paras[new_para], NULL, wrapw, 0, 0); + + } else { + + size_t offs_new, offs_old; + + offs_new = pos_trail_to_offset(para, e->cursor_pos, + e->cursor_trail); + offs_old = pos_trail_to_offset(para, old_pos, old_trail); + + delete_text_in_paragraph(e->cursor_frame, old_para, + offs_new, offs_old); + wrap_paragraph(para, NULL, wrapw, 0, 0); + + + } + + } + + emit_change_sig(e); + sc_editor_redraw(e); +} + + static void insert_text(char *t, SCEditor *e) { Paragraph *para; @@ -678,6 +737,9 @@ static void insert_text(char *t, SCEditor *e) return; } + if ( e->sel_active ) { + do_backspace(e->cursor_frame, e); + } if ( strcmp(t, "\n") == 0 ) { split_paragraph_at_cursor(e); @@ -740,65 +802,6 @@ static void insert_text(char *t, SCEditor *e) } -static void do_backspace(struct frame *fr, SCEditor *e) -{ - double wrapw = e->cursor_frame->w - e->cursor_frame->pad_l - e->cursor_frame->pad_r; - - if ( e->sel_active ) { - - /* Delete the selected block */ - delete_text_from_frame(e->cursor_frame, e->sel_start, e->sel_end, wrapw); - - /* Cursor goes at start of deletion */ - sort_positions(&e->sel_start, &e->sel_end); - e->cursor_para = e->sel_start.para; - e->cursor_pos = e->sel_start.pos; - e->cursor_trail = e->sel_start.trail; - e->sel_active = 0; - - } else { - - /* Delete one character */ - size_t old_pos = e->cursor_pos; - int old_para = e->cursor_para; - int old_trail = e->cursor_trail; - - int new_para = old_para; - size_t new_pos = old_pos; - int new_trail = old_trail; - - Paragraph *para = e->cursor_frame->paras[old_para]; - - cursor_moveh(e->cursor_frame, &new_para, &new_pos, &new_trail, -1); - cursor_moveh(e->cursor_frame, &e->cursor_para, &e->cursor_pos, &e->cursor_trail, -1); - - if ( e->cursor_para != old_para ) { - - merge_paragraphs(e->cursor_frame, e->cursor_para); - wrap_paragraph(e->cursor_frame->paras[new_para], NULL, wrapw, 0, 0); - - } else { - - size_t offs_new, offs_old; - - offs_new = pos_trail_to_offset(para, e->cursor_pos, - e->cursor_trail); - offs_old = pos_trail_to_offset(para, old_pos, old_trail); - - delete_text_in_paragraph(e->cursor_frame, old_para, - offs_new, offs_old); - wrap_paragraph(para, NULL, wrapw, 0, 0); - - - } - - } - - emit_change_sig(e); - sc_editor_redraw(e); -} - - static gboolean im_commit_sig(GtkIMContext *im, gchar *str, SCEditor *e) { |