From 19481cb33012f590bc97171356fb9e6a682bfa8f Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Tue, 10 Feb 2026 19:20:57 +0100 Subject: [PATCH] delete list elements when pressing enter in an empty list line --- application/gtk-text.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/application/gtk-text.c b/application/gtk-text.c index 08abca0..b184bd4 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -322,23 +322,24 @@ 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; if(nlines > 1) { // Inserted text contains at least one linebreak // Check if the current line contains a list - GtkTextIter prev_line = begin; + 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 - while(gtk_text_iter_get_offset(&prev_line) < end_offset) { - if(!gtk_text_iter_forward_to_tag_toggle(&prev_line, NULL)) { - prev_line = begin; - } - + 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"); @@ -347,12 +348,25 @@ static void edit_insert_text_cb( 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; } } } @@ -399,6 +413,14 @@ static void edit_insert_text_cb( editor_insert_list_element(editor, &iter, style_name, list_num+i+1); } } + + if(remove_list_off > 0) { + GtkTextIter iter; + gtk_text_buffer_get_iter_at_offset(buffer, &iter, remove_list_off-1); + GtkTextIter iter2 = iter; + gtk_text_iter_forward_char(&iter2); + gtk_text_buffer_delete(buffer, &iter, &iter2); + } } static gboolean path_is_image_file(cxstring path) { -- 2.47.3