From: Olaf Wintermann Date: Thu, 5 Feb 2026 17:23:20 +0000 (+0100) Subject: add replace callbacks X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=f0e9ae8cca3a1ddef41d4328dcd82da3ac66d9fc;p=note.git add replace callbacks --- diff --git a/application/note.c b/application/note.c index dfaec7c..7c88043 100644 --- a/application/note.c +++ b/application/note.c @@ -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 ? ®ex : 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(®ex); } + + 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++; + } +} + diff --git a/application/note.h b/application/note.h index 58357ad..48645c8 100644 --- a/application/note.h +++ b/application/note.h @@ -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 } diff --git a/application/window.c b/application/window.c index 4811571..884d8ab 100644 --- a/application/window.c +++ b/application/window.c @@ -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); + } +} diff --git a/application/window.h b/application/window.h index fe1e1ee..60af481 100644 --- a/application/window.h +++ b/application/window.h @@ -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);