From 70c63fc26a5ba8b50eb4c968f66e1b2a2bc99094 Mon Sep 17 00:00:00 2001 From: pgen Date: Sun, 11 Apr 2021 23:17:01 +0200 Subject: Add an early substitution option -ES similar to -S Early substitutions are applied immediately after obtaining a word from the input. Operations such as numbering, coloring or excluding will be performed after the substitution and not before as in the classic -S/-E/-I options. --- smenu.1 | 21 +++++++++++++++++++++ smenu.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- usage.c | 16 +++++++++++----- 3 files changed, 91 insertions(+), 9 deletions(-) diff --git a/smenu.1 b/smenu.1 index 4fee109..4701834 100644 --- a/smenu.1 +++ b/smenu.1 @@ -56,6 +56,7 @@ and outputs the selection to stdout. [\fB-S\fP|\fB-subst\fP... \fI/regex/repl/opts\fP] [\fB-I\fP|\fB-si\fP|\fB-subst_included\fP... \fI/regex/repl/opts\fP] [\fB-E\fP|\fB-se\fP|\fB-subst_excluded\fP... \fI/regex/repl/opts\fP] + [\fB-ES\fP|\fB-early_subst\fP... \fI/regex/repl/opts\fP] [\fB-/\fP|\fB-search_method\fP \fIprefix\fP|\fIsubstring\fP|\fIfuzzy\fP] [\fB-s\fP|\fB-sp\fP|\fB-start\fP|\fB-start_pattern\fP \fIpattern\fP] [\fB-x\fP|\fB-tmout\fP|\fB-timeout\fP \fItype\fP [\fIword\fP] \fIdelay\fP] @@ -1462,6 +1463,26 @@ utility. .IP "otherwise:" The word is simply removed. .RE +.IP "\fB-ES\fP|\fB-subst\fP... \ +/\fIregex\fP/\fIrepl\fP/[\fIg\fP][\fIv\fP][\fIs\fP]" +(Allowed in all contexts.) + +Pre-processes words by applying a substitution based on a regular expression. +The argument must be formatted as in the \fBsed\fP editor. + +The substitutions are made, as the name of the option indicates, before +any other selection or coloring actions are made. + +This option can be used more than once. +Each substitution will be applied in sequence on each word. +This sequence can be stopped if a \fBstop\fP flag is encountered. + +In summary, this option is similar to the \fB-S\fP|\fB-subst\fP option +previously described, except that the substitutions are made earlier and +certain flags like \fBvisual\fP are ignored. + +Note that this option can be used in conjunction with the other +substitution options mentioned above. .IP "\fB-/\fP|\fB-search_method\fP \fIsearch_method\fP" (Allowed in all contexts.) diff --git a/smenu.c b/smenu.c index 9366386..1b0afe5 100644 --- a/smenu.c +++ b/smenu.c @@ -6336,6 +6336,8 @@ main(int argc, char * argv[]) regex_t include_re; /* variable to store the compiled include (-i) REs. */ regex_t exclude_re; /* variable to store the compiled exclude (-e) REs. */ + ll_t * early_sed_list = NULL; /* List of sed like string representation * + * of regex given after (-ES). */ ll_t * sed_list = NULL; /* List of sed like string representation * * of regex given after (-S). */ ll_t * include_sed_list = NULL; /* idem for -I. */ @@ -6675,6 +6677,7 @@ main(int argc, char * argv[]) "[keep_spaces] " "[word_separators #bytes] " "[no_scroll_bar] " + "[early_subst_all... #/regex/repl/opts] " "[post_subst_all... #/regex/repl/opts] " "[post_subst_included... #/regex/repl/opts] " "[post_subst_excluded... #/regex/repl/opts] " @@ -6823,6 +6826,7 @@ main(int argc, char * argv[]) ctxopt_add_opt_settings(parameters, "zapped_glyphs", "-z -zap -zap-glyphs"); ctxopt_add_opt_settings(parameters, "no_scroll_bar", "-q -no_bar -no-scroll_bar"); + ctxopt_add_opt_settings(parameters, "early_subst_all", "-ES -early_subst"); ctxopt_add_opt_settings(parameters, "post_subst_all", "-S -subst"); ctxopt_add_opt_settings(parameters, "post_subst_included", "-I -si -subst_included"); @@ -6922,6 +6926,8 @@ main(int argc, char * argv[]) &toggles, (char *)0); ctxopt_add_opt_settings(actions, "wide_mode", wide_mode_action, &win, (char *)0); + ctxopt_add_opt_settings(actions, "early_subst_all", post_subst_action, + &early_sed_list, &langinfo, &misc, (char *)0); ctxopt_add_opt_settings(actions, "post_subst_all", post_subst_action, &sed_list, &langinfo, &misc, (char *)0); ctxopt_add_opt_settings(actions, "post_subst_included", post_subst_action, @@ -7692,6 +7698,24 @@ main(int argc, char * argv[]) /* Parse the post-processing patterns and extract its values. */ /* """""""""""""""""""""""""""""""""""""""""""""""""""""""""" */ + if (early_sed_list != NULL) + { + ll_node_t * node = early_sed_list->head; + + while (node != NULL) + { + if (!parse_sed_like_string((sed_t *)(node->data))) + { + fprintf(stderr, "Bad -ES argument. Must be something like: " + "/regex/repl_string/[g][v][s][i].\n"); + + exit(EXIT_FAILURE); + } + + node = node->next; + } + } + if (sed_list != NULL) { ll_node_t * node = sed_list->head; @@ -7928,10 +7952,41 @@ main(int argc, char * argv[]) &langinfo, &win, &limits, &misc)) != NULL) { - int selectable; - int is_first = 0; - int special_level; - int row_inc_matched = 0; + int selectable; + int is_first = 0; + int special_level; + int row_inc_matched = 0; + ll_node_t * node; + + if (*word == '\0') + continue; + + /* Early substitution. */ + /* """"""""""""""""""" */ + if (early_sed_list != NULL) + { + char * tmp; + + node = early_sed_list->head; + + while (node != NULL) + { + tmp = xstrdup(word); + if (replace(word, (sed_t *)(node->data))) + { + + free(word); + word = xstrdup(word_buffer); + + if (((sed_t *)(node->data))->stop) + break; + } + + *word_buffer = '\0'; + node = node->next; + free(tmp); + } + } if (*word == '\0') continue; diff --git a/usage.c b/usage.c index d4cf07d..f87d1ba 100644 --- a/usage.c +++ b/usage.c @@ -63,13 +63,19 @@ common_help(void) printf("-q|-no_bar|-no-scroll_bar\n"); printf(" prevents the display of the scroll bar.\n"); printf("-S|-subst\n"); - printf(" sets the post-processing action to apply to all words.\n"); + printf(" sets the post substitution treatment action to be applied to all " + "words.\n"); printf("-I|-si|-subst_included\n"); - printf(" sets the post-processing action to apply to selectable " - "words only.\n"); + printf(" sets the post substitution treatment action to be applied to " + "selectable\n"); + printf(" words only.\n"); printf("-E|-se|-subst_excluded\n"); - printf(" sets the post-processing action to apply to non-selectable " - "words only.\n"); + printf(" sets the post substitution treatment action to be applied to " + "non-selectable\n"); + printf("-ES|-early_subst\n"); + printf(" sets the early substitution treatment action to be applied to " + "all words.\n"); + printf(" words only.\n"); printf("-/|-search_method\n"); printf(" changes the affectation of the / key (default fuzzy search).\n"); printf("-s|-sp|-start|-start_pattern\n"); -- cgit v1.2.3