aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2019-05-24 20:33:03 +0200
committerThomas White <taw@physics.org>2019-05-24 20:33:03 +0200
commite3ee9aa8ffa5f75707316627940c3dfe96c94ece (patch)
treebb7537efdb2c51a6380d9ecb064fa6f5fc4044fa
parentd321c825e10eaaec66f3584e2bdb8edda4e44e0e (diff)
Fix paragraph splitting logic
-rw-r--r--libstorycode/gtk/gtknarrativeview.c63
-rw-r--r--libstorycode/narrative.c11
2 files changed, 30 insertions, 44 deletions
diff --git a/libstorycode/gtk/gtknarrativeview.c b/libstorycode/gtk/gtknarrativeview.c
index 0876111..83d6d23 100644
--- a/libstorycode/gtk/gtknarrativeview.c
+++ b/libstorycode/gtk/gtknarrativeview.c
@@ -820,27 +820,6 @@ static void insert_text_in_paragraph(struct narrative_item *item, size_t offs,
}
-static void split_paragraph_at_cursor(Narrative *n, struct edit_pos *pos)
-{
- size_t off;
-
- if ( narrative_item_is_text(n, pos->para) ) {
- off = pos_trail_to_offset(&n->items[pos->para],
- pos->pos, pos->trail);
- } else {
- off = 0;
- }
-
- if ( (off > 0) && (off < strlen(n->items[pos->para].text)) ) {
- narrative_split_item(n, pos->para, off);
- } else if ( off == 0 ) {
- pos->para--;
- pos->pos = 0;
- pos->trail = 0;
- }
-}
-
-
static void insert_text(char *t, GtkNarrativeView *e)
{
struct narrative_item *item;
@@ -851,28 +830,22 @@ static void insert_text(char *t, GtkNarrativeView *e)
item = &e->n->items[e->cpos.para];
- if ( strcmp(t, "\n") == 0 ) {
- split_paragraph_at_cursor(e->n, &e->cpos);
- rewrap_range(e, e->cpos.para, e->cpos.para+1);
- update_size(e);
- cursor_moveh(e->n, &e->cpos, +1);
- check_cursor_visible(e);
- emit_change_sig(e);
- redraw(e);
- return;
- }
-
if ( narrative_item_is_text(e->n, e->cpos.para) ) {
- size_t off;
+ size_t off = pos_trail_to_offset(item, e->cpos.pos, e->cpos.trail);
+
+ if ( strcmp(t, "\n") == 0 ) {
+ narrative_split_item(e->n, e->cpos.para, off);
+ rewrap_range(e, e->cpos.para, e->cpos.para+1);
+ } else {
+ insert_text_in_paragraph(item, off, t);
+ rewrap_range(e, e->cpos.para, e->cpos.para);
+ }
- off = pos_trail_to_offset(item, e->cpos.pos, e->cpos.trail);
- insert_text_in_paragraph(item, off, t);
- rewrap_range(e, e->cpos.para, e->cpos.para);
update_size(e);
cursor_moveh(e->n, &e->cpos, +1);
- } /* else do nothing: pressing enter is OK, though */
+ } /* else do nothing */
emit_change_sig(e);
check_cursor_visible(e);
@@ -1227,8 +1200,20 @@ void gtk_narrative_view_add_slide_at_cursor(GtkNarrativeView *e)
s = slide_new();
if ( s == NULL ) return;
- split_paragraph_at_cursor(e->n, &e->cpos);
- narrative_insert_slide(e->n, s, e->cpos.para+1);
+ if ( narrative_item_is_text(e->n, e->cpos.para) ) {
+ size_t off = pos_trail_to_offset(&e->n->items[e->cpos.para],
+ e->cpos.pos, e->cpos.trail);
+ if ( (off > 0) && (off < strlen(e->n->items[e->cpos.para].text)) ) {
+ narrative_split_item(e->n, e->cpos.para, off);
+ narrative_insert_slide(e->n, s, e->cpos.para+1);
+ } else if ( off == 0 ) {
+ narrative_insert_slide(e->n, s, e->cpos.para);
+ } else {
+ narrative_insert_slide(e->n, s, e->cpos.para+1);
+ }
+ } else {
+ narrative_insert_slide(e->n, s, e->cpos.para+1);
+ }
rewrap_range(e, e->cpos.para, e->cpos.para+2);
e->cpos.para++;
diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c
index 38c7284..2a8f0f3 100644
--- a/libstorycode/narrative.c
+++ b/libstorycode/narrative.c
@@ -220,13 +220,14 @@ static struct narrative_item *add_item(Narrative *n)
}
+/* New item will have index 'pos' */
static struct narrative_item *insert_item(Narrative *n, int pos)
{
add_item(n);
memmove(&n->items[pos+1], &n->items[pos],
(n->n_items-pos-1)*sizeof(struct narrative_item));
- init_item(&n->items[pos+1]);
- return &n->items[pos+1];
+ init_item(&n->items[pos]);
+ return &n->items[pos];
}
@@ -298,7 +299,7 @@ void narrative_add_eop(Narrative *n)
void narrative_insert_slide(Narrative *n, Slide *slide, int pos)
{
- struct narrative_item *item = insert_item(n, pos-1);
+ struct narrative_item *item = insert_item(n, pos);
item->type = NARRATIVE_ITEM_SLIDE;
item->slide = slide;
item->slide_thumbnail = NULL;
@@ -389,9 +390,9 @@ void narrative_split_item(Narrative *n, int i1, size_t o1)
struct narrative_item *item2;
item1 = &n->items[i1];
- item2 = insert_item(n, i1);
+ item2 = insert_item(n, i1+1);
- if ( !narrative_item_is_text(n, i1) ) {
+ if ( narrative_item_is_text(n, i1) ) {
item2->text = strdup(&item1->text[o1]);
item1->text[o1] = '\0';
} else {