]> uap-core.de Git - note.git/commitdiff
apply paragraph styles to the next line, when it is not empty
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 17 Mar 2025 11:43:22 +0000 (12:43 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 17 Mar 2025 11:43:22 +0000 (12:43 +0100)
application/gtk-text.c

index fb55168d654503faf22bbfbbf23f18da92a6d1a9..068c2c95fcad0da034e1e2ff1f5f6cb5ef110f22 100644 (file)
@@ -33,6 +33,9 @@
 #include <cx/buffer.h>
 #include <cx/hash_map.h>
 
+#include <ctype.h>
+#include <string.h>
+
 // workaround for netbeans bug
 #define g_object_set g_object_set
 #define g_object_get_property g_object_get_property
@@ -64,6 +67,7 @@ void editor_global_init() {
     cxMapPut(markdown_tags, EDITOR_STYLE_HEADING4, &((MDTag){"#### ", NULL}));
     cxMapPut(markdown_tags, EDITOR_STYLE_HEADING5, &((MDTag){"##### ", NULL}));
     cxMapPut(markdown_tags, EDITOR_STYLE_HEADING6, &((MDTag){"###### ", NULL}));
+    cxMapPut(markdown_tags, EDITOR_STYLE_CODE_BLOCK, &((MDTag){"    ", NULL}));
     cxMapPut(markdown_tags, EDITOR_STYLE_STRONG, &((MDTag){"**", "**"}));
     cxMapPut(markdown_tags, EDITOR_STYLE_EMPHASIS, &((MDTag){"*", "*"}));
     cxMapPut(markdown_tags, EDITOR_STYLE_CODE, &((MDTag){"`", "`"}));
@@ -544,6 +548,8 @@ void editor_set_paragraph_style(UiText *text, const char *style) {
     GtkTextBuffer *buffer = text->data1;
     NoteEditor *editor = g_object_get_data(text->obj, "editor");
     
+    int begin_line, end_line;
+    
     // currently changing the selection's paragraph style is not supported
     if(gtk_text_buffer_get_has_selection(buffer)) {
         update_cursor_paragraph_style(editor, buffer);
@@ -557,11 +563,50 @@ void editor_set_paragraph_style(UiText *text, const char *style) {
     }
     GtkTextIter pos;
     gtk_text_buffer_get_iter_at_mark(buffer, &pos, cursor);
-    GtkTextIter begin = pos;
-    GtkTextIter end = pos;
-    gtk_text_iter_set_line_offset(&begin, 0);
-    gtk_text_iter_forward_to_line_end(&end);
     
-    remove_paragraph_styles(buffer, &begin, &end);
-    gtk_text_buffer_apply_tag_by_name(buffer, style, &begin, &end);
+    begin_line = gtk_text_iter_get_line(&pos);
+    end_line = begin_line;
+    
+    // check if the next line is still part of this paragraph
+    // paragraphs are separated by blank lines
+    GtkTextIter next_line = pos;
+    while(gtk_text_iter_forward_line(&next_line)) {
+        GtkTextIter begin = next_line;
+        GtkTextIter end = next_line;
+        gtk_text_iter_set_line_offset(&begin, 0);
+        gtk_text_iter_forward_to_line_end(&end);
+        
+        char *line_str = gtk_text_buffer_get_slice(buffer, &begin, &end, TRUE);
+        char *str = line_str;
+        int blank = TRUE;
+        while(*str) {
+            char c = *str;
+            if(!isspace(*str)) {
+                blank = FALSE;
+                break;
+            }
+            
+            str++;
+        }
+        g_free(line_str);
+        if(blank) {
+            break;
+        }
+        
+        end_line++;
+    }
+    
+    
+    
+    for(int i=begin_line;i<=end_line;i++) {
+        GtkTextIter begin = pos;
+        GtkTextIter end = pos;
+        gtk_text_iter_set_line_offset(&begin, 0);
+        gtk_text_iter_forward_to_line_end(&end);
+
+        remove_paragraph_styles(buffer, &begin, &end);
+        gtk_text_buffer_apply_tag_by_name(buffer, style, &begin, &end);
+        
+        gtk_text_iter_forward_line(&pos);
+    }
 }