diff options
author | Thomas White <taw@bitwiz.org.uk> | 2017-03-27 08:22:17 +0200 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2017-03-27 08:22:17 +0200 |
commit | 3413239509a9a00d89199f8aa661504f43dcf585 (patch) | |
tree | 648b236b56c28d35ad9f24d45efe71fd9a8f2e97 | |
parent | 4b0854d62b4b4e7ce0c721a6f9f43df595aacd72 (diff) |
Delete selected text
-rw-r--r-- | src/frame.c | 43 | ||||
-rw-r--r-- | src/frame.h | 6 | ||||
-rw-r--r-- | src/sc_editor.c | 63 |
3 files changed, 83 insertions, 29 deletions
diff --git a/src/frame.c b/src/frame.c index 866ae31..d69f0fb 100644 --- a/src/frame.c +++ b/src/frame.c @@ -886,7 +886,48 @@ void insert_text_in_paragraph(Paragraph *para, size_t offs, const char *t) } -void delete_text_in_paragraph(Paragraph *para, size_t offs1, size_t offs2) +static void delete_paragraph(struct frame *fr, int p) +{ +} + + +void delete_text_from_frame(struct frame *fr, struct edit_pos p1, struct edit_pos p2, + double wrapw) +{ + int i; + + for ( i=p1.para; i<=p2.para; i++ ) { + + size_t start; + ssize_t finis; + + Paragraph *para = fr->paras[i]; + + if ( i == p1.para ) { + start = pos_trail_to_offset(para, p1.pos, p1.trail); + } else { + start = 0; + } + + if ( i == p2.para ) { + finis = pos_trail_to_offset(para, p2.pos, p2.trail); + } else { + finis = -1; + } + + if ( (start == 0) && (finis == -1) ) { + delete_paragraph(fr, para); + } else { + delete_text_in_paragraph(para, start, finis); + wrap_paragraph(para, NULL, wrapw, 0, 0); + } + + } +} + + +/* offs2 negative means "to end" */ +void delete_text_in_paragraph(Paragraph *para, size_t offs1, ssize_t offs2) { int nrun1, nrun2, nrun; int i; diff --git a/src/frame.h b/src/frame.h index b6ae617..6302edc 100644 --- a/src/frame.h +++ b/src/frame.h @@ -178,8 +178,10 @@ extern size_t pos_trail_to_offset(Paragraph *para, size_t offs, int trail); extern void insert_text_in_paragraph(Paragraph *para, size_t offs, const char *t); -extern void delete_text_in_paragraph(Paragraph *para, - size_t offs1, size_t offs2); +extern void delete_text_from_frame(struct frame *fr, struct edit_pos p1, struct edit_pos p2, + double wrap_w); + +extern void delete_text_in_paragraph(Paragraph *para, size_t offs1, ssize_t offs2); extern SCBlock *split_paragraph(struct frame *fr, int pn, size_t pos, PangoContext *pc); diff --git a/src/sc_editor.c b/src/sc_editor.c index a3754a6..ffc895f 100644 --- a/src/sc_editor.c +++ b/src/sc_editor.c @@ -715,36 +715,47 @@ static void insert_text(char *t, SCEditor *e) static void do_backspace(struct frame *fr, SCEditor *e) { - 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; - - double wrapw = e->cursor_frame->w - e->cursor_frame->pad_l - - e->cursor_frame->pad_r; - - 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); + double wrapw = e->cursor_frame->w - e->cursor_frame->pad_l - e->cursor_frame->pad_r; + + if ( e->sel_active ) { + + /* Delete the selected block */ + printf("delete block\n"); + delete_text_from_frame(e->cursor_frame, e->sel_start, e->sel_end, wrapw); + } else { - size_t offs_new, offs_old; + /* 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 ) { - offs_new = pos_trail_to_offset(para, e->cursor_pos, - e->cursor_trail); - offs_old = pos_trail_to_offset(para, old_pos, old_trail); + merge_paragraphs(e->cursor_frame, e->cursor_para); + wrap_paragraph(e->cursor_frame->paras[new_para], NULL, wrapw, 0, 0); - delete_text_in_paragraph(para, offs_new, offs_old); - wrap_paragraph(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(para, offs_new, offs_old); + wrap_paragraph(para, NULL, wrapw, 0, 0); + + } } |