From 5ec54e4d3d765bd099d79cdd47af7d9c58a700f6 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Wed, 11 Feb 2026 16:05:10 +0100 Subject: [PATCH] move list depth detection to separate function --- application/gtk-text.c | 95 ++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/application/gtk-text.c b/application/gtk-text.c index f5753ca..e8dc5e4 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -301,6 +301,58 @@ static void editor_delete_range_cb( } +static int get_prev_list(GtkTextBuffer *buffer, const GtkTextIter *iter, int *depth, int *style, int *num, int *remove_offset) { + int list_depth = 0; + int list_style = 0; + int list_num = 0; + int remove_list_off = -1; + + GtkTextIter iter2 = *iter; + GtkTextChildAnchor *anchor = NULL; + GtkTextChildAnchor *prevAnchor = NULL; + int off = 0; + // iterate over chars in line and check for anchors + int chars_per_line = 0; + while(gtk_text_iter_backward_char(&iter2)) { + if(gtk_text_iter_get_char(&iter2) == '\n') { + break; + } + off = gtk_text_iter_get_offset(&iter2); + + anchor = gtk_text_iter_get_child_anchor(&iter2); + if(anchor && anchor != prevAnchor) { + EmbeddedWidget *em = g_object_get_data(G_OBJECT(anchor), "em"); + if(em) { + if(em->type == EMBEDDED_WIDGET_LIST) { + list_style = em->intdata1; + list_num = em->intdata2; + list_depth++; + } else if(em->type == EMBEDDED_WIDGET_LIST_INDENT) { + list_depth++; + } // else: other type of embedded widget (image, ...) + } + } + prevAnchor = anchor; + + chars_per_line++; + } + + if(chars_per_line == 1) { + // empty list element -> inserting a newline terminates the list + list_depth = 0; + *remove_offset = off + 1; + } + + if(list_depth > 0) { + *depth = list_depth; + *style = list_style; + *num = list_num; + *remove_offset = -1; + return 1; + } + return 0; +} + static void edit_insert_text_cb( GtkTextBuffer *buffer, const GtkTextIter *location, @@ -322,53 +374,14 @@ static void edit_insert_text_cb( cxstring lines[64]; size_t nlines = cx_strsplit(ins_text, "\n", 64, lines); - int remove_list_off = -1; - int list_depth = 0; int list_style = 0; int list_num = 0; + int remove_list_off = -1; if(nlines > 1) { // Inserted text contains at least one linebreak // Check if the current line contains a list - GtkTextIter prev_line = *location; - if(gtk_text_iter_backward_line(&prev_line)) { - GtkTextChildAnchor *anchor = NULL; - GtkTextChildAnchor *prevAnchor = NULL; - int anchor_off = -1; - int end_offset = gtk_text_iter_get_offset(&begin); - // iterate over chars in line and check for anchors - int chars_per_line = 0; - int off; - while((off = gtk_text_iter_get_offset(&prev_line)) < end_offset) { - anchor = gtk_text_iter_get_child_anchor(&prev_line); - if(anchor && anchor != prevAnchor) { - EmbeddedWidget *em = g_object_get_data(G_OBJECT(anchor), "em"); - if(em) { - if(em->type == EMBEDDED_WIDGET_LIST) { - list_style = em->intdata1; - list_num = em->intdata2; - list_depth++; - anchor_off = off; - } else if(em->type == EMBEDDED_WIDGET_LIST_INDENT) { - list_depth++; - anchor_off = off; - } // else: other type of embedded widget (image, ...) - } - } - prevAnchor = anchor; - - if(!gtk_text_iter_forward_char(&prev_line)) { - prev_line = begin; - } - chars_per_line++; - } - - if(chars_per_line == 1) { - // empty list element -> inserting a newline terminates the list - list_depth = 0; - remove_list_off = off; - } - } + (void)get_prev_list(buffer, &begin, &list_depth, &list_style, &list_num, &remove_list_off); } GtkTextIter prev = begin; -- 2.47.3