]> uap-core.de Git - note.git/commitdiff
delete list elements when pressing enter in an empty list line
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 10 Feb 2026 18:20:57 +0000 (19:20 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 10 Feb 2026 18:20:57 +0000 (19:20 +0100)
application/gtk-text.c

index 08abca0962581b255c1c1500dca73781a227f2f1..b184bd494fab6b13073c5274e1bdcf03fb9debd7 100644 (file)
@@ -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) {