From 5b9a7f3c7d6794d6642ac07bd2b00d21a88bb995 Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Mon, 7 Oct 2019 21:32:21 +0200 Subject: [PATCH] Fixed the 'y' option + added the --delete-regexp option. --- selector.1 | 3 +++ selector.c | 29 ++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/selector.1 b/selector.1 index e8eb8ff..a673a13 100644 --- a/selector.1 +++ b/selector.1 @@ -119,6 +119,9 @@ standard setting for bash history search, same as -b -i -d -v -w -l ${HISTSIZE} .TP +\fB--delete-regexp \fI\fR +deletes in every line the portion matching the regexp +.TP \fB-t \fI\fR, \fB--title \fI<title>\fR add a title in the modeline .TP diff --git a/selector.c b/selector.c index ac80d93..f573d79 100644 --- a/selector.c +++ b/selector.c @@ -65,6 +65,7 @@ int global_remove_duplicates = 0; int global_use_regexp = 0; int global_case_sensitive = 0; char *global_title = 0; +regex_t *global_prefix_regexp = 0; int global_error_flash = 0; int global_upper_caps_makes_case_sensitive = 0; int global_show_long_lines = 0; @@ -204,6 +205,8 @@ void usage(FILE *out) { fprintf(out, " make a flash instead of a beep on an edition error\n"); fprintf(out, " --bash\n"); fprintf(out, " setting for bash history search, same as -b -i -d -v -w -l ${HISTSIZE}\n"); + fprintf(out, " --delete-regexp <regexp>\n"); + fprintf(out, " deletes in every line the portion matching the regexp\n"); fprintf(out, " --\n"); fprintf(out, " all following arguments are filenames\n"); fprintf(out, " -t <title>, --title <title>\n"); @@ -877,9 +880,20 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, /*********************************************************************/ void store_line(struct hash_table_t *hash_table, - const char *new_line, + char *new_line, int *nb_lines, char **lines) { int dup; + char *c, *d; + regmatch_t matches; + + /* Remove some parts matching a regexp */ + + if(global_prefix_regexp && regexec(global_prefix_regexp, new_line, 1, &matches, 0) == 0) { + c = new_line + matches.rm_so; + d = new_line + matches.rm_eo; + while(*d) { *c++ = *d++; } + *c = 0; + } /* Remove the zsh history prefix */ @@ -961,7 +975,8 @@ void read_file(struct hash_table_t *hash_table, /* For long options that have no equivalent short option, use a non-character as a pseudo short option, starting with CHAR_MAX + 1. */ enum { - OPT_BASH_MODE = CHAR_MAX + 1 + OPT_BASH_MODE = CHAR_MAX + 1, + OPT_DELETE_REGEXP = CHAR_MAX + 2 }; static struct option long_options[] = { @@ -979,13 +994,14 @@ static struct option long_options[] = { { "regexp", no_argument, 0, 'e' }, { "case-sensitive", no_argument, 0, 'a' }, { "show-long-lines", no_argument, 0, 'j'}, - { "show-hits", no_argument, 0, 'j'}, + { "show-hits", no_argument, 0, 'y'}, { "upper-case-makes-case-sensitive", no_argument, 0, 'u' }, { "title", 1, 0, 't' }, { "pattern", 1, 0, 'r' }, { "number-of-lines", 1, 0, 'l' }, { "colors", 1, 0, 'c' }, { "bash", no_argument, 0, OPT_BASH_MODE }, + { "delete-regexp", 1, 0, OPT_DELETE_REGEXP }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 } }; @@ -1107,6 +1123,12 @@ int main(int argc, char **argv) { strcpy(global_title, optarg); break; + case OPT_DELETE_REGEXP: + free(global_prefix_regexp); + global_prefix_regexp = safe_malloc(sizeof(*global_prefix_regexp)); + regcomp(global_prefix_regexp, optarg, 0); + break; + case 'r': strcpy(pattern, optarg); break; @@ -1441,6 +1463,7 @@ int main(int argc, char **argv) { free(labels); free(lines); free(global_title); + free(global_prefix_regexp); exit(EXIT_SUCCESS); } -- 2.39.5