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));
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);
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);
}
if(enable_regex) {
regfree(®ex);
}
+
+ return ret;
}
void note_search_next(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++;
+ }
+}
+
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
}
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);
}
}
}
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);
+ }
+}
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);