Restore paragraph splitting
authorThomas White <taw@physics.org>
Sat, 5 Oct 2019 07:58:38 +0000 (09:58 +0200)
committerThomas White <taw@physics.org>
Sat, 5 Oct 2019 15:03:43 +0000 (17:03 +0200)
libstorycode/gtk/gtknarrativeview.c
libstorycode/narrative.c

index e44d25b..897bf97 100644 (file)
@@ -832,13 +832,16 @@ static void insert_text(char *t, GtkNarrativeView *e)
                if ( strcmp(t, "\n") == 0 ) {
                        narrative_split_item(e->n, e->cpos.para, off);
                        rewrap_range(e, e->cpos.para, e->cpos.para+1);
+                       e->cpos.para += 1;
+                       e->cpos.pos = 0;
+                       e->cpos.trail = 0;
                } else {
                        insert_text_in_paragraph(item, off, t);
                        rewrap_range(e, e->cpos.para, e->cpos.para);
+                       cursor_moveh(e->n, &e->cpos, +1);
                }
 
                update_size(e);
-               cursor_moveh(e->n, &e->cpos, +1);
 
        } /* else do nothing */
 
index 08c19d3..895f530 100644 (file)
@@ -422,26 +422,42 @@ int which_run(struct narrative_item *item, size_t item_offs, size_t *run_offs)
        return run;
 }
 
-
 void narrative_split_item(Narrative *n, int i1, size_t o1)
 {
-       /* FIXME! */
-#if 0
        struct narrative_item *item1;
        struct narrative_item *item2;
 
-       item1 = &n->items[i1];
        item2 = insert_item(n, i1+1);
+       item1 = &n->items[i1];  /* NB n->items was realloced by insert_item */
+       item2->type = NARRATIVE_ITEM_TEXT;
 
        if ( narrative_item_is_text(n, i1) ) {
-               item2->text = strdup(&item1->text[o1]);
-               item1->text[o1] = '\0';
+
+               size_t run_offs;
+               int run = which_run(item1, o1, &run_offs);
+               int j;
+
+               item2->n_runs = item1->n_runs - run;
+               item2->runs = malloc(item2->n_runs*sizeof(struct text_run));
+               for ( j=run; j<item1->n_runs; j++ ) {
+                       item2->runs[j-run] = item1->runs[j];
+               }
+
+               /* Now break the run */
+               item2->runs[0].text = strdup(item1->runs[run].text+run_offs);
+               item1->runs[run].text[run_offs] = '\0';
+               item1->n_runs = run + 1;
+
        } else {
-               item2->text = strdup("");
-       }
 
-       item2->type = NARRATIVE_ITEM_TEXT;
-#endif
+               /* Splitting a non-text run simply means creating a new
+                * plain text item after it */
+               item2->runs = malloc(sizeof(struct text_run));
+               item2->n_runs = 1;
+               item2->runs[0].text = strdup("");
+               item2->runs[0].type = TEXT_RUN_NORMAL;;
+
+       }
 }