aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2016-03-31 22:48:43 +0200
committerThomas White <taw@bitwiz.org.uk>2016-03-31 22:48:43 +0200
commit37468b97fc8acff0948498da0996bf0835d3365a (patch)
treec23f5c92c0a1157f1ac6b2f0af9eeb189e6ad645 /src
parent554a3d38045a2c563342b534084462771621e079 (diff)
Make backspace work (pretty minimally)
Diffstat (limited to 'src')
-rw-r--r--src/frame.c40
-rw-r--r--src/frame.h3
-rw-r--r--src/sc_editor.c26
3 files changed, 68 insertions, 1 deletions
diff --git a/src/frame.c b/src/frame.c
index c835869..7f61a89 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -750,3 +750,43 @@ void insert_text_in_paragraph(Paragraph *para, size_t offs, const char *t)
para->runs[i].para_offs_bytes += ins_len;
}
}
+
+
+void delete_text_in_paragraph(Paragraph *para, size_t offs1, size_t offs2)
+{
+ int nrun1, nrun2;
+ int i;
+ struct text_run *run1;
+ struct text_run *run2;
+ size_t scblock_offs1, scblock_offs2;
+
+ /* Find which run we are in */
+ nrun1 = which_run(para, offs1);
+ nrun2 = which_run(para, offs2);
+ if ( (nrun1 == para->n_runs) || (nrun2 == para->n_runs) ) {
+ fprintf(stderr, "Couldn't find run to delete from.\n");
+ return;
+ }
+ run1 = &para->runs[nrun1];
+ run2 = &para->runs[nrun2];
+
+ /* Translate paragraph offsets into SCBlock offsets */
+ scblock_offs1 = offs1 - run1->para_offs_bytes + run1->scblock_offs_bytes;
+ scblock_offs2 = offs2 - run2->para_offs_bytes + run2->scblock_offs_bytes;
+ sc_delete_text(run1->scblock, scblock_offs1,
+ run2->scblock, scblock_offs2);
+
+ if ( nrun1 == nrun2 ) {
+ size_t del_len = offs2 - offs1;
+ run1->len_bytes -= del_len;
+ for ( i=nrun1+1; i<para->n_runs; i++ ) {
+ if ( para->runs[i].scblock == run1->scblock ) {
+ para->runs[i].scblock_offs_bytes -= del_len;
+ }
+ para->runs[i].para_offs_bytes -= del_len;
+ }
+ } else {
+ /* FIXME: Implement this case */
+ printf("Multi-run delete!\n");
+ }
+}
diff --git a/src/frame.h b/src/frame.h
index 50fdaca..a6b316f 100644
--- a/src/frame.h
+++ b/src/frame.h
@@ -147,4 +147,7 @@ extern void check_callback_click(struct frame *fr, int para);
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);
+
#endif /* FRAME_H */
diff --git a/src/sc_editor.c b/src/sc_editor.c
index ca37853..c75c8ac 100644
--- a/src/sc_editor.c
+++ b/src/sc_editor.c
@@ -605,7 +605,31 @@ static void insert_text(char *t, SCEditor *e)
static void do_backspace(struct frame *fr, SCEditor *e)
{
- /* FIXME: Delete at the cursor */
+ int old_pos = e->cursor_pos;
+ int old_para = e->cursor_para;
+ int old_trail = e->cursor_trail;
+
+ int new_para = old_para;
+ int 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 ) {
+ /* FIXME: Implement this case */
+ printf("Merge paragraphs!\n");
+ return;
+ }
+
+ delete_text_in_paragraph(para, e->cursor_pos+e->cursor_trail,
+ old_pos+old_trail);
+
+ wrap_paragraph(para, NULL, e->cursor_frame->w - e->cursor_frame->pad_l
+ - e->cursor_frame->pad_r);
+
sc_editor_redraw(e);
}