diff options
author | pgen <p.gen.progs@gmail.com> | 2021-04-11 23:17:01 +0200 |
---|---|---|
committer | pgen <p.gen.progs@gmail.com> | 2021-04-11 23:17:01 +0200 |
commit | 70c63fc26a5ba8b50eb4c968f66e1b2a2bc99094 (patch) | |
tree | 044d7db9c9b5b08d7eea4e38029262aea6f6f1e2 /smenu.c | |
parent | 9a0889f1be9d9c946113e40ae6900ec4cd6c0819 (diff) |
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.
Diffstat (limited to 'smenu.c')
-rw-r--r-- | smenu.c | 63 |
1 files changed, 59 insertions, 4 deletions
@@ -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; |