]> uap-core.de Git - note.git/commitdiff
implement text replace main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 3 Feb 2026 18:32:44 +0000 (19:32 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 3 Feb 2026 18:32:44 +0000 (19:32 +0100)
application/note.c
ui/cocoa/text.h
ui/cocoa/text.m
ui/common/types.c
ui/gtk/text.c
ui/gtk/text.h
ui/motif/text.c
ui/motif/text.h
ui/qt/text.cpp
ui/qt/text.h
ui/ui/toolkit.h

index acf5c83b8424b0f1286c0efaaf7b841940cba913..dfaec7c693e21e42e25bc6d2e79d2efe2fde576c 100644 (file)
@@ -33,6 +33,8 @@
 #include "notebook.h"
 #include "attachment.h"
 
+#include "../utils/strreplace.h"
+
 #include <cx/array_list.h>
 
 static TextNoteParagraphStyles paragraph_styles[] = {
@@ -422,7 +424,26 @@ static int get_search_start_pos(NoteModel *note, bool backwards) {
     return pos;
 }
 
-static void note_search(NoteModel *note, bool backwards) {
+typedef struct {
+    cxstring text;
+    regmatch_t *match;
+    size_t nmatch;
+} TplStringBuildData;
+
+static cxmutstr get_group(const CxAllocator *a, StringTemplateSegment *seg, void *userdata, bool *free_str) {
+    TplStringBuildData *data = userdata;
+    if(seg->num >= data->nmatch) {
+        return CX_NULLSTR;
+    }
+    regmatch_t match = data->match[seg->num];
+    if(match.rm_so < 0 || match.rm_eo < 0) {
+        return CX_NULLSTR;
+    }
+    cxstring s = cx_strsubsl(data->text, match.rm_so, match.rm_eo-match.rm_so);
+    return cx_mutstrn((char*)s.ptr, s.length);
+}
+
+static void note_search(NoteModel *note, bool backwards, bool replace) {
     cxstring searchstr = cx_str(ui_get(note->search));
     if(searchstr.length == 0) {
         return;
@@ -446,10 +467,24 @@ static void note_search(NoteModel *note, bool backwards) {
     }
     
     int begin, end;
-    if(text_search(text, searchstr, pos, backwards, cs, enable_regex ? &regex : NULL, &begin, &end)) {
-        note->text->setposition(note->text, end);
-        note->text->setselection(note->text, begin, end);
-        note->text->showposition(note->text, begin);
+    regmatch_t match[32];
+    if(text_search_match(text, searchstr, pos, backwards, cs, enable_regex ? &regex : NULL, &begin, &end, match, 32)) {
+        if(!replace) {
+            note->text->setposition(note->text, end);
+            note->text->setselection(note->text, begin, end);
+            note->text->showposition(note->text, begin);
+        } else {
+            cxstring tpl_str = cx_str(ui_get(note->replace));
+            StringTemplate *tpl = string_template_compile(cxDefaultAllocator, tpl_str);
+            TplStringBuildData data;
+            data.match = match;
+            data.nmatch = 32;
+            data.text = text;
+            cxmutstr replacement = string_template_build_string(tpl, cxDefaultAllocator, get_group, &data);
+            note->text->replace(note->text, begin, end, replacement.ptr);
+            free(replacement.ptr);
+            string_template_free(tpl);
+        }
     }
     
     if(enable_regex) {
@@ -458,13 +493,13 @@ static void note_search(NoteModel *note, bool backwards) {
 }
 
 void note_search_next(NoteModel *note) {
-    note_search(note, FALSE);
+    note_search(note, FALSE, FALSE);
 }
 
 void note_search_prev(NoteModel *note) {
-    note_search(note, TRUE);
+    note_search(note, TRUE, FALSE);
 }
 
 void note_replace(NoteModel *note) {
-    
+    note_search(note, FALSE, TRUE);
 }
index 23bc4389d1029e8eb96433ad08ac79c6307fe6eb..415717c58babe86b57cfd3e1713a0660423afd2e 100644 (file)
@@ -37,7 +37,7 @@ void  ui_textarea_restore(UiText *text);
 void  ui_textarea_set(UiText *text, const char *str);
 char* ui_textarea_get(UiText *text);
 char* ui_textarea_getsubstr(UiText *text, int begin, int end);
-void  ui_textarea_insert(UiText *text, int pos, char *str);
+void  ui_textarea_insert(UiText *text, int pos, const char *str);
 void  ui_textarea_setposition(UiText *text, int pos);
 int   ui_textarea_position(UiText *text);
 void  ui_textarea_setselection(UiText *text, int begin, int end);
index 324d6e7560478747b32a79e4c5259f1a278021e4..679968f78260a9144c833797e93a4ac7693a866d 100644 (file)
@@ -120,7 +120,7 @@ char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
     return strdup(sub.UTF8String);
 }
 
-void ui_textarea_insert(UiText *text, int pos, char *str) {
+void ui_textarea_insert(UiText *text, int pos, const char *str) {
     NSTextView *textview = (__bridge NSTextView*)text->obj;
     NSString *s = [[NSString alloc] initWithUTF8String:str];
     NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:s];
index 38e6b6c1a04939c56c8ff131cbd94805bda3c7cb..e0ecf515f16d06daa3c292c583c0042b248f0324 100644 (file)
@@ -676,6 +676,7 @@ void uic_text_copy(UiText *from, UiText *to) {
     to->set = from->set;
     to->getsubstr = from->getsubstr;
     to->insert = from->insert;
+    to->replace = from->replace;
     to->setposition = from->setposition;
     to->position = from->position;
     to->showposition = from->showposition;
@@ -768,9 +769,11 @@ void uic_text_unbind(UiText *t) {
     t->get = NULL;
     t->getsubstr = NULL;
     t->insert = NULL;
+    t->replace = NULL;
     t->setposition = NULL;
     t->position = NULL;
     t->selection = NULL;
+    t->setselection = NULL;
     t->length = NULL;
     t->remove = NULL;
     t->obj = NULL;
index fd355ee06c7e19dfb1dc0badadf4797fb5edaf4d..e9dc703e529318c422f9fdec6cad82ce916e3130 100644 (file)
@@ -186,6 +186,7 @@ UIWIDGET ui_textarea_create(UiObject *obj, UiTextAreaArgs *args) {
         value->set = ui_textarea_set;
         value->getsubstr = ui_textarea_getsubstr;
         value->insert = ui_textarea_insert;
+        value->replace = ui_textarea_replace;
         value->setposition = ui_textarea_setposition;
         value->position = ui_textarea_position;
         value->showposition = ui_textarea_showposition;
@@ -287,7 +288,7 @@ char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
     return str;
 }
 
-void ui_textarea_insert(UiText *text, int pos, char *str) {
+void ui_textarea_insert(UiText *text, int pos, const char *str) {
     GtkTextIter offset;
     gtk_text_buffer_get_iter_at_offset(text->data1, &offset, pos);
     gtk_text_buffer_insert(text->data1, &offset, str, -1);
@@ -298,6 +299,20 @@ void ui_textarea_insert(UiText *text, int pos, char *str) {
     text->value.free = NULL;
 }
 
+void ui_textarea_replace(UiText *text, int begin, int end, const char *replacement) {
+    GtkTextBuffer *buffer = text->data1;
+    GtkTextIter begin_offset;
+    GtkTextIter end_offset;
+    gtk_text_buffer_get_iter_at_offset(buffer, &begin_offset, begin);
+    gtk_text_buffer_get_iter_at_offset(buffer, &end_offset, end);
+    gtk_text_buffer_begin_user_action(buffer);
+    gtk_text_buffer_delete(buffer, &begin_offset, &end_offset);
+    if(replacement) {
+        gtk_text_buffer_insert(buffer, &begin_offset, replacement, -1);
+    }
+    gtk_text_buffer_end_user_action(buffer);
+}
+
 void ui_textarea_setposition(UiText *text, int pos) {
     GtkTextIter iter;
     gtk_text_buffer_get_iter_at_offset(text->data1, &iter, pos);
index a976cbc32d6bf46c56e68057c9ca197b66df0d48..4d306aa5026cadf9ffb9c5d3675a5974f9742e61 100644 (file)
@@ -117,7 +117,8 @@ void ui_textarea_text_destroy(UiText *text);
 char* ui_textarea_get(UiText *text);
 void ui_textarea_set(UiText *text, const char *str);
 char* ui_textarea_getsubstr(UiText *text, int begin, int end);
-void ui_textarea_insert(UiText *text, int pos, char *str);
+void ui_textarea_insert(UiText *text, int pos, const char *str);
+void ui_textarea_replace(UiText *text, int begin, int end, const char *replacement);
 void ui_textarea_setposition(UiText *text, int pos);
 int ui_textarea_position(UiText *text);
 void ui_textarea_showposition(UiText *text, int pos);
index 877659f0f9027ee86387ff103ab6e8e47fb34bab..f0dd46c0379fa82103c1d205aad5211f2710e296 100644 (file)
@@ -150,9 +150,9 @@ char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
     return str;
 }
 
-void ui_textarea_insert(UiText *text, int pos, char *str) {
+void ui_textarea_insert(UiText *text, int pos, const char *str) {
     text->value.ptr = NULL;
-    XmTextInsert(text->obj, pos, str);
+    XmTextInsert(text->obj, pos, (char*)str);
     if(text->value.ptr) {
         text->value.free(text->value.ptr);
     }
index 9f2932e3486c701b339c756491f65d0eef5765f7..3c59627aca9be360f105197ab97fc228c691a386 100644 (file)
@@ -70,7 +70,7 @@ void ui_textarea_text_destroy(UiText *text);
 char* ui_textarea_get(UiText *text);
 void ui_textarea_set(UiText *text, const char *str);
 char* ui_textarea_getsubstr(UiText *text, int begin, int end);
-void ui_textarea_insert(UiText *text, int pos, char *str);
+void ui_textarea_insert(UiText *text, int pos, const char *str);
 void ui_textarea_setposition(UiText *text, int pos);
 int ui_textarea_position(UiText *text);
 void ui_textarea_selection(UiText *text, int *begin, int *end);
index 95824789f2639c9ae4dc722bb46a1472f408fb5f..2aeab479607c5d977f46945a8f7e8c182ade04a5 100644 (file)
@@ -154,7 +154,7 @@ char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
     return cstr ? strdup(cstr) : NULL;
 }
 
-void ui_textarea_insert(UiText *text, int pos, char *str) {
+void ui_textarea_insert(UiText *text, int pos, const char *str) {
     QTextDocument *doc = (QTextDocument*)text->data1;
     QTextCursor cursor(doc);
     cursor.setPosition(pos);
index fcfe6129ef58ddf8f810ddc353f6c3dcd4f24a96..5ecee2aa29971e5aad44d2101321090cf8382047 100644 (file)
@@ -43,7 +43,7 @@ void ui_textarea_text_destroy(UiText *text);
 char* ui_textarea_get(UiText *text);
 void ui_textarea_set(UiText *text, const char *str);
 char* ui_textarea_getsubstr(UiText *text, int begin, int end);
-void ui_textarea_insert(UiText *text, int pos, char *str);
+void ui_textarea_insert(UiText *text, int pos, const char *str);
 void ui_textarea_setposition(UiText *text, int pos);
 int ui_textarea_position(UiText *text);
 void ui_textarea_setselection(UiText *text, int begin, int end);
index 9fbc6586f5dc1ab6aad42009973a4571ec13ab7c..8833f90f3eadd0f741aae379937445a988b691e4 100644 (file)
@@ -392,7 +392,8 @@ struct UiText {
     void  (*set)(UiText*, const char*);
     char* (*get)(UiText*);
     char* (*getsubstr)(UiText*, int, int); /* text, begin, end */
-    void  (*insert)(UiText*, int, char*);
+    void  (*insert)(UiText*, int, const char*);
+    void  (*replace)(UiText*, int, int, const char*);
     void  (*setposition)(UiText*,int);
     int   (*position)(UiText*);
     void  (*showposition)(UiText*, int);