]> uap-core.de Git - note.git/commitdiff
add replace callbacks
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 5 Feb 2026 17:23:20 +0000 (18:23 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 5 Feb 2026 17:23:20 +0000 (18:23 +0100)
application/note.c
application/note.h
application/window.c
application/window.h

index dfaec7c693e21e42e25bc6d2e79d2efe2fde576c..7c88043b12999f3db6889885e6a3520a03572468 100644 (file)
@@ -443,10 +443,10 @@ static cxmutstr get_group(const CxAllocator *a, StringTemplateSegment *seg, void
     return cx_mutstrn((char*)s.ptr, s.length);
 }
 
-static void note_search(NoteModel *note, bool backwards, bool replace) {
+static int note_search(NoteModel *note, bool backwards, bool replace) {
     cxstring searchstr = cx_str(ui_get(note->search));
     if(searchstr.length == 0) {
-        return;
+        return 0;
     }
     
     cxstring text = cx_str(ui_get(note->text));
@@ -468,7 +468,9 @@ static void note_search(NoteModel *note, bool backwards, bool replace) {
     
     int begin, end;
     regmatch_t match[32];
+    int ret = 0;
     if(text_search_match(text, searchstr, pos, backwards, cs, enable_regex ? &regex : NULL, &begin, &end, match, 32)) {
+        ret = 1;
         if(!replace) {
             note->text->setposition(note->text, end);
             note->text->setselection(note->text, begin, end);
@@ -481,7 +483,10 @@ static void note_search(NoteModel *note, bool backwards, bool replace) {
             data.nmatch = 32;
             data.text = text;
             cxmutstr replacement = string_template_build_string(tpl, cxDefaultAllocator, get_group, &data);
+            int endpos = begin + replacement.length;
             note->text->replace(note->text, begin, end, replacement.ptr);
+            note->text->setposition(note->text, endpos);
+            note->text->showposition(note->text, endpos);
             free(replacement.ptr);
             string_template_free(tpl);
         }
@@ -490,6 +495,8 @@ static void note_search(NoteModel *note, bool backwards, bool replace) {
     if(enable_regex) {
         regfree(&regex);
     }
+    
+    return ret;
 }
 
 void note_search_next(NoteModel *note) {
@@ -503,3 +510,12 @@ void note_search_prev(NoteModel *note) {
 void note_replace(NoteModel *note) {
     note_search(note, FALSE, TRUE);
 }
+
+void note_replace_all(NoteModel *note) {
+    int i = 0;
+    int max = 32000;
+    while(i < max || note_search(note, FALSE, TRUE)) {
+        i++;
+    }
+}
+
index 58357ad5eb5343153b01f8297db18e4b1085763f..48645c821c305c80eb15a4b93334601cfda5fada 100644 (file)
@@ -100,6 +100,7 @@ int text_search_match(
 void note_search_next(NoteModel *note);
 void note_search_prev(NoteModel *note);
 void note_replace(NoteModel *note);
+void note_replace_all(NoteModel *note);
 
 #ifdef __cplusplus
 }
index 4811571e9a579a02684401b89eaff06508cfeb95..884d8ab16baa6c6b69f60b78b1a623bbdbd7fbda 100644 (file)
@@ -129,9 +129,9 @@ void window_create() {
                         
                         ui_newline(obj);
                         ui_rlabel(obj, .label = "Replace", .visibility_states = UI_GROUPS(APP_STATE_NOTE_REPLACE));
-                        ui_textfield(obj, .hexpand = TRUE, .varname = "replace", .visibility_states = UI_GROUPS(APP_STATE_NOTE_REPLACE));
-                        ui_button(obj, .label = "Replace", .visibility_states = UI_GROUPS(APP_STATE_NOTE_REPLACE));
-                        ui_button(obj, .label = "Replace All", .visibility_states = UI_GROUPS(APP_STATE_NOTE_REPLACE));
+                        ui_textfield(obj, .hexpand = TRUE, .varname = "replace", .visibility_states = UI_GROUPS(APP_STATE_NOTE_REPLACE), .onactivate = action_searchbar_replace);
+                        ui_button(obj, .label = "Replace", .visibility_states = UI_GROUPS(APP_STATE_NOTE_REPLACE), .onclick = action_searchbar_replace);
+                        ui_button(obj, .label = "Replace All", .visibility_states = UI_GROUPS(APP_STATE_NOTE_REPLACE), .onclick = action_searchbar_replace_all);
                     }
                 } 
             }
@@ -588,3 +588,17 @@ void action_searchbar_prev(UiEvent *event, void *userdata) {
         note_search_prev(note);
     }
 }
+
+void action_searchbar_replace(UiEvent *event, void *userdata) {
+    NoteModel *note = notemodel_current(event->obj);
+    if(note) {
+        note_replace(note);
+    }
+}
+
+void action_searchbar_replace_all(UiEvent *event, void *userdata) {
+    NoteModel *note = notemodel_current(event->obj);
+    if(note) {
+        note_replace_all(note);
+    }
+}
index fe1e1ee3cc586fcb41707358c3c7ec1b301889f9..60af481aa61cf9b0f100af8098bdcca20f77e1e8 100644 (file)
@@ -92,6 +92,8 @@ void action_textnote_insertimg(UiEvent *event, void *userdata);
 void action_searchbar_close(UiEvent *event, void *userdata);
 void action_searchbar_next(UiEvent *event, void *userdata);
 void action_searchbar_prev(UiEvent *event, void *userdata);
+void action_searchbar_replace(UiEvent *event, void *userdata);
+void action_searchbar_replace_all(UiEvent *event, void *userdata);