From 07b52f78680858683acb4e40b158f8926285cae4 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Tue, 19 Nov 2024 14:33:30 +0300 Subject: implemented search mode flag (^X): allow case-sensitive searches (closes #17) * Usually you will only want -^X for enabling case sensitive searches and 0^X for case-insensitive searches (which is also the default). * An open question is what happens if the user sets -^X and then calls a macro. The search mode flag should probably be stacked away along with the search-string. This means we'd need a ^X special Q-Reg as well, so you can write [^X[_ 0^X S...$ ]_]^X. Alternatively, the search mode flag should be a property of the macro frame, along with the radix. --- src/search.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src/search.c') diff --git a/src/search.c b/src/search.c index 07a5154..ec62d02 100644 --- a/src/search.c +++ b/src/search.c @@ -52,6 +52,34 @@ TECO_DEFINE_UNDO_OBJECT_OWN(parameters, teco_search_parameters_t, /* don't delet */ static teco_search_parameters_t teco_search_parameters; +static teco_bool_t teco_search_mode = TECO_FAILURE; /* case-insensitive */ + +/*$ ^X search-mode + * mode^X -- Set or get search mode flag + * -^X + * ^X -> mode + * + * The search mode is interpreted as a TECO boolean. + * A true value (smaller than zero) configures case-sensitive searches, + * while a false value (larger than or equal to zero) configures case-insensitive + * searches. + * "-^X" is equivalent to "-1^X" and also enables case-sensitive searches. + * Searches are case-insensitive by default. + */ +void +teco_state_control_search_mode(teco_machine_main_t *ctx, GError **error) +{ + if (!teco_expressions_eval(FALSE, error)) + return; + if (!teco_expressions_args() && teco_num_sign > 0) { + teco_expressions_push(teco_search_mode); + } else { + teco_undo_int(teco_search_mode); + if (!teco_expressions_pop_num_calc(&teco_search_mode, teco_num_sign, error)) + return; + } +} + static gboolean teco_state_search_initial(teco_machine_main_t *ctx, GError **error) { @@ -599,7 +627,10 @@ static gboolean teco_state_search_process(teco_machine_main_t *ctx, const teco_string_t *str, gsize new_chars, GError **error) { /* FIXME: Should G_REGEX_OPTIMIZE be added under certain circumstances? */ - GRegexCompileFlags flags = G_REGEX_CASELESS | G_REGEX_MULTILINE | G_REGEX_DOTALL; + GRegexCompileFlags flags = G_REGEX_MULTILINE | G_REGEX_DOTALL; + + if (teco_is_failure(teco_search_mode)) + flags |= G_REGEX_CASELESS; /* this is set in teco_state_search_initial() */ if (ctx->expectstring.machine.codepage != SC_CP_UTF8) { -- cgit v1.2.3