From 9ce586146825200c819662ea8dcb44cc03d2cd53 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Wed, 6 Aug 2014 09:26:46 +0200 Subject: Add pattern filter option This adds a new item filter option --filter-pattern and removes the whole -S parameter logic, while making -S an alias for --filter-pattern. Signed-off-by: Lukas Fleischer --- src/apoint.c | 2 ++ src/args.c | 64 ++++++++++++++++++++++++++++------------------------------ src/calcurse.h | 3 ++- src/day.c | 32 +++++++++-------------------- src/event.c | 2 ++ src/recur.c | 4 ++++ 6 files changed, 51 insertions(+), 56 deletions(-) diff --git a/src/apoint.c b/src/apoint.c index 9bdd575..3c32c04 100644 --- a/src/apoint.c +++ b/src/apoint.c @@ -198,6 +198,8 @@ struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end, if (filter) { if (!(filter->type_mask & TYPE_MASK_APPT)) return NULL; + if (filter->regex && regexec(filter->regex, buf, 0, 0, 0)) + return NULL; if (filter->start_from >= 0 && tstart < filter->start_from) return NULL; if (filter->start_to >= 0 && tstart > filter->start_to) diff --git a/src/args.c b/src/args.c index e9c9ac2..9e06416 100644 --- a/src/args.c +++ b/src/args.c @@ -47,6 +47,7 @@ /* Long options */ enum { OPT_FILTER_TYPE = 1000, + OPT_FILTER_PATTERN, OPT_FILTER_START_FROM, OPT_FILTER_START_TO, OPT_FILTER_START_AFTER, @@ -214,10 +215,8 @@ static void status_arg(void) * then only todo items that have this priority will be displayed. * If priority is < 0, all todos will be displayed. * If priority == 0, only completed tasks will be displayed. - * If regex is not null, only the matching todos are printed. */ -static void todo_arg(int priority, const char *format, regex_t * regex, - int *limit) +static void todo_arg(int priority, const char *format, int *limit) { llist_item_t *i; int title = 1; @@ -239,8 +238,6 @@ static void todo_arg(int priority, const char *format, regex_t * regex, if (*limit == 0) return; struct todo *todo = LLIST_TS_GET_DATA(i); - if (regex && regexec(regex, todo->mesg, 0, 0, 0) != 0) - continue; if (todo->id < 0) { /* completed task */ if (priority == 0) { @@ -304,19 +301,18 @@ static void arg_print_date(long date) * Print appointments for given day and exit. * If no day is given, the given date is used. * If there is also no date given, current date is considered. - * If regex is not null, only the matching appointments or events are printed. */ static int app_arg(int add_line, struct date *day, long date, const char *fmt_apt, const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev, - regex_t * regex, int *limit) + int *limit) { if (*limit == 0) return 0; if (date == 0) date = get_sec_date(*day); - day_store_items(date, regex, 0); + day_store_items(date, 0); int n = day_item_count(0); @@ -339,7 +335,7 @@ app_arg(int add_line, struct date *day, long date, const char *fmt_apt, static void display_app(struct tm *t, int numdays, int add_line, const char *fmt_apt, const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev, - regex_t * regex, int *limit) + int *limit) { int i, app_found; struct date day; @@ -350,7 +346,7 @@ display_app(struct tm *t, int numdays, int add_line, const char *fmt_apt, day.yyyy = t->tm_year + 1900; app_found = app_arg(add_line, &day, 0, fmt_apt, fmt_rapt, fmt_ev, - fmt_rev, regex, limit); + fmt_rev, limit); if (app_found) add_line = 1; t->tm_mday++; @@ -365,7 +361,7 @@ display_app(struct tm *t, int numdays, int add_line, const char *fmt_apt, static void date_arg(const char *ddate, int add_line, const char *fmt_apt, const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev, - regex_t * regex, int *limit) + int *limit) { struct date day; static struct tm t; @@ -384,13 +380,13 @@ date_arg(const char *ddate, int add_line, const char *fmt_apt, timer = time(NULL); localtime_r(&timer, &t); display_app(&t, atoi(ddate), add_line, fmt_apt, fmt_rapt, - fmt_ev, fmt_rev, regex, limit); + fmt_ev, fmt_rev, limit); } else { /* A date was entered. */ if (parse_date(ddate, conf.input_datefmt, (int *)&day.yyyy, (int *)&day.mm, (int *)&day.dd, NULL)) { app_arg(add_line, &day, 0, fmt_apt, fmt_rapt, - fmt_ev, fmt_rev, regex, limit); + fmt_ev, fmt_rev, limit); } else { fputs(_("Argument to the '-d' flag is not valid\n"), stderr); @@ -413,8 +409,7 @@ date_arg(const char *ddate, int add_line, const char *fmt_apt, static void date_arg_extended(const char *startday, const char *range, int add_line, const char *fmt_apt, const char *fmt_rapt, - const char *fmt_ev, const char *fmt_rev, regex_t * regex, - int *limit) + const char *fmt_ev, const char *fmt_rev, int *limit) { int numdays = 1, error = 0; static struct tm t; @@ -445,7 +440,7 @@ date_arg_extended(const char *startday, const char *range, int add_line, } if (!error) { display_app(&t, numdays, add_line, fmt_apt, fmt_rapt, - fmt_ev, fmt_rev, regex, limit); + fmt_ev, fmt_rev, limit); } else { fputs(_("Argument is not valid\n"), stderr); fprintf(stdout, @@ -525,7 +520,7 @@ int parse_args(int argc, char **argv) int vflag = 0; /* -v: print version number */ int xflag = 0; /* -x: export data */ /* Filters */ - struct item_filter filter = { TYPE_MASK_ALL, -1, -1, -1, -1 }; + struct item_filter filter = { TYPE_MASK_ALL, NULL, -1, -1, -1, -1 }; /* Format strings */ const char *fmt_apt = " - %S -> %E\n\t%m\n"; const char *fmt_rapt = " - %S -> %E\n\t%m\n"; @@ -539,7 +534,7 @@ int parse_args(int argc, char **argv) const char *ddate = "", *cfile = NULL, *range = NULL, *startday = NULL; const char *datadir = NULL, *ifile = NULL; - regex_t reg, *preg = NULL; + regex_t reg; /* Long options only */ int statusflag = 0; /* --status: get the status of running instances */ @@ -569,6 +564,7 @@ int parse_args(int argc, char **argv) {"export", optional_argument, NULL, 'x'}, {"filter-type", required_argument, NULL, OPT_FILTER_TYPE}, + {"filter-pattern", required_argument, NULL, OPT_FILTER_PATTERN}, {"filter-start-from", required_argument, NULL, OPT_FILTER_START_FROM}, {"filter-start-to", required_argument, NULL, OPT_FILTER_START_TO}, {"filter-start-after", required_argument, NULL, OPT_FILTER_START_AFTER}, @@ -643,14 +639,6 @@ int parse_args(int argc, char **argv) load_data++; startday = optarg; break; - case 'S': - EXIT_IF(Sflag > 0, - _("Can not handle more than one regular expression.")); - Sflag = 1; - if (regcomp(®, optarg, REG_EXTENDED)) - EXIT(_("Could not compile regular expression.")); - preg = ® - break; case 't': tflag = 1; multiple_flag++; @@ -696,6 +684,16 @@ int parse_args(int argc, char **argv) EXIT_IF(filter.type_mask == 0, _("invalid filter mask")); break; + case 'S': + Sflag = 1; + /* FALLTHROUGH */ + case OPT_FILTER_PATTERN: + EXIT_IF(filter.regex, + _("Can not handle more than one regular expression.")); + if (regcomp(®, optarg, REG_EXTENDED)) + EXIT(_("Could not compile regular expression.")); + filter.regex = ® + break; case OPT_FILTER_START_FROM: filter.start_from = parse_datearg(optarg); EXIT_IF(filter.start_from == -1, @@ -834,7 +832,7 @@ int parse_args(int argc, char **argv) if (tflag) { io_check_file(path_todo); io_load_todo(); - todo_arg(tnum, fmt_todo, preg, &limit); + todo_arg(tnum, fmt_todo, &limit); non_interactive = 1; } if (nflag) { @@ -851,14 +849,13 @@ int parse_args(int argc, char **argv) if (dflag) date_arg(ddate, add_line, fmt_apt, fmt_rapt, fmt_ev, fmt_rev, - preg, &limit); + &limit); if (rflag || sflag) date_arg_extended(startday, range, add_line, fmt_apt, fmt_rapt, fmt_ev, - fmt_rev, preg, - &limit); + fmt_rev, &limit); non_interactive = 1; } else if (aflag) { struct date day; @@ -870,7 +867,7 @@ int parse_args(int argc, char **argv) io_load_app(&filter); day.dd = day.mm = day.yyyy = 0; app_arg(add_line, &day, 0, fmt_apt, fmt_rapt, - fmt_ev, fmt_rev, preg, &limit); + fmt_ev, fmt_rev, &limit); non_interactive = 1; } } else { @@ -879,8 +876,9 @@ int parse_args(int argc, char **argv) } } - if (preg) - regfree(preg); + /* Free filter parameters. */ + if (filter.regex) + regfree(filter.regex); return non_interactive; } diff --git a/src/calcurse.h b/src/calcurse.h index 4ae6c66..0290520 100644 --- a/src/calcurse.h +++ b/src/calcurse.h @@ -394,6 +394,7 @@ enum day_item_type { /* Filter settings. */ struct item_filter { int type_mask; + regex_t *regex; long start_from; long start_to; long end_from; @@ -722,7 +723,7 @@ long day_item_get_duration(struct day_item *); int day_item_get_state(struct day_item *); void day_item_add_exc(struct day_item *, long); void day_item_fork(struct day_item *, struct day_item *); -void day_store_items(long, regex_t *, int); +void day_store_items(long, int); void day_process_storage(struct date *, unsigned); void day_display_item_date(struct day_item *, WINDOW *, int, long, int, int); void day_display_item(struct day_item *, WINDOW *, int, int, int, int); diff --git a/src/day.c b/src/day.c index bf97cec..2ff5050 100644 --- a/src/day.c +++ b/src/day.c @@ -218,7 +218,7 @@ void day_item_fork(struct day_item *day_in, struct day_item *day_out) * dedicated to the selected day. * Returns the number of events for the selected day. */ -static int day_store_events(long date, regex_t * regex) +static int day_store_events(long date) { llist_item_t *i; union aptev_ptr p; @@ -227,9 +227,6 @@ static int day_store_events(long date, regex_t * regex) LLIST_FIND_FOREACH_CONT(&eventlist, &date, event_inday, i) { struct event *ev = LLIST_TS_GET_DATA(i); - if (regex && regexec(regex, ev->mesg, 0, 0, 0) != 0) - continue; - p.ev = ev; day_add_item(EVNT, ev->day, p); e_nb++; @@ -245,7 +242,7 @@ static int day_store_events(long date, regex_t * regex) * dedicated to the selected day. * Returns the number of recurrent events for the selected day. */ -static int day_store_recur_events(long date, regex_t * regex) +static int day_store_recur_events(long date) { llist_item_t *i; union aptev_ptr p; @@ -254,9 +251,6 @@ static int day_store_recur_events(long date, regex_t * regex) LLIST_FIND_FOREACH(&recur_elist, &date, recur_event_inday, i) { struct recur_event *rev = LLIST_TS_GET_DATA(i); - if (regex && regexec(regex, rev->mesg, 0, 0, 0) != 0) - continue; - p.rev = rev; day_add_item(RECUR_EVNT, rev->day, p); e_nb++; @@ -272,7 +266,7 @@ static int day_store_recur_events(long date, regex_t * regex) * structure dedicated to the selected day. * Returns the number of appointments for the selected day. */ -static int day_store_apoints(long date, regex_t * regex) +static int day_store_apoints(long date) { llist_item_t *i; union aptev_ptr p; @@ -282,9 +276,6 @@ static int day_store_apoints(long date, regex_t * regex) LLIST_TS_FIND_FOREACH(&alist_p, &date, apoint_inday, i) { struct apoint *apt = LLIST_TS_GET_DATA(i); - if (regex && regexec(regex, apt->mesg, 0, 0, 0) != 0) - continue; - p.apt = apt; if (apt->start >= date + DAYINSEC) @@ -305,7 +296,7 @@ static int day_store_apoints(long date, regex_t * regex) * structure dedicated to the selected day. * Returns the number of recurrent appointments for the selected day. */ -static int day_store_recur_apoints(long date, regex_t * regex) +static int day_store_recur_apoints(long date) { llist_item_t *i; union aptev_ptr p; @@ -315,9 +306,6 @@ static int day_store_recur_apoints(long date, regex_t * regex) LLIST_TS_FIND_FOREACH(&recur_alist_p, &date, recur_apoint_inday, i) { struct recur_apoint *rapt = LLIST_TS_GET_DATA(i); - if (regex && regexec(regex, rapt->mesg, 0, 0, 0) != 0) - continue; - p.rapt = rapt; unsigned real_start; @@ -340,7 +328,7 @@ static int day_store_recur_apoints(long date, regex_t * regex) * The number of events and appointments in the current day are also updated. */ void -day_store_items(long date, regex_t * regex, int include_captions) +day_store_items(long date, int include_captions) { unsigned apts, events; union aptev_ptr p = { NULL }; @@ -351,10 +339,10 @@ day_store_items(long date, regex_t * regex, int include_captions) if (include_captions) day_add_item(DAY_HEADING, 0, p); - events = day_store_recur_events(date, regex); - events += day_store_events(date, regex); - apts = day_store_recur_apoints(date, regex); - apts += day_store_apoints(date, regex); + events = day_store_recur_events(date); + events += day_store_events(date); + apts = day_store_recur_apoints(date); + apts += day_store_apoints(date); if (include_captions && events > 0 && apts > 0) day_add_item(DAY_SEPARATOR, 0, p); @@ -385,7 +373,7 @@ void day_process_storage(struct date *slctd_date, unsigned day_changed) delwin(apad.ptrwin); /* Store the events and appointments (recursive and normal items). */ - day_store_items(date, NULL, 1); + day_store_items(date, 1); } /* diff --git a/src/event.c b/src/event.c index 96ac41d..1bf3b29 100644 --- a/src/event.c +++ b/src/event.c @@ -152,6 +152,8 @@ struct event *event_scan(FILE * f, struct tm start, int id, char *note, if (filter) { if (!(filter->type_mask & TYPE_MASK_EVNT)) return NULL; + if (filter->regex && regexec(filter->regex, buf, 0, 0, 0)) + return NULL; if (filter->start_from >= 0 && tstart < filter->start_from) return NULL; if (filter->start_to >= 0 && tstart > filter->start_to) diff --git a/src/recur.c b/src/recur.c index 76585d6..61fd155 100644 --- a/src/recur.c +++ b/src/recur.c @@ -380,6 +380,8 @@ struct recur_apoint *recur_apoint_scan(FILE * f, struct tm start, if (filter) { if (!(filter->type_mask & TYPE_MASK_RECUR_APPT)) return NULL; + if (filter->regex && regexec(filter->regex, buf, 0, 0, 0)) + return NULL; if (filter->start_from >= 0 && tstart < filter->start_from) return NULL; if (filter->start_to >= 0 && tstart > filter->start_to) @@ -437,6 +439,8 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id, if (filter) { if (!(filter->type_mask & TYPE_MASK_RECUR_EVNT)) return NULL; + if (filter->regex && regexec(filter->regex, buf, 0, 0, 0)) + return NULL; if (filter->start_from >= 0 && tstart < filter->start_from) return NULL; if (filter->start_to >= 0 && tstart > filter->start_to) -- cgit v1.2.3-70-g09d2