From: Olaf Wintermann Date: Mon, 17 Mar 2025 11:43:22 +0000 (+0100) Subject: apply paragraph styles to the next line, when it is not empty X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=9b312d01d71a40cd43ab67395eda10dca492f3da;p=note.git apply paragraph styles to the next line, when it is not empty --- diff --git a/application/gtk-text.c b/application/gtk-text.c index fb55168..068c2c9 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -33,6 +33,9 @@ #include #include +#include +#include + // 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); + } }