From e16ba67cc800d2679f8be4fb60a3a84744af28f0 Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Thu, 4 Sep 2014 22:04:58 +0200 Subject: Allow multiple autocmd patterns (#100). --- src/autocmd.c | 35 +++++++++++++++++++++++++++++++++-- src/util.c | 2 -- 2 files changed, 33 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/autocmd.c b/src/autocmd.c index 1d69a7e..c12b471 100644 --- a/src/autocmd.c +++ b/src/autocmd.c @@ -55,6 +55,7 @@ static GSList *groups = NULL; static guint get_event_bits(const char *name); static AuGroup *get_group(const char *name); static char *get_next_word(char **line); +static gboolean wildmatch(char *patterns, const char *uri); static AuGroup *new_group(const char *name); static void free_group(AuGroup *group); static AutoCmd *new_autocmd(const char *excmd, const char *pattern); @@ -171,7 +172,7 @@ gboolean autocmd_add(char *name, gboolean delete) } /* skip if pattern does not match - we check the pattern against * another pattern */ - if (!util_wildmatch(pattern, cmd->pattern)) { + if (!wildmatch(pattern, cmd->pattern)) { continue; } /* remove the bits from the item */ @@ -226,7 +227,7 @@ gboolean autocmd_run(const char *group, AuEvent event, const char *uri) } /* check pattern only if uri was given */ /* skip if pattern does not match */ - if (uri && !util_wildmatch(cmd->pattern, uri)) { + if (uri && !wildmatch(cmd->pattern, uri)) { continue; } /* run the command */ @@ -331,6 +332,36 @@ static char *get_next_word(char **line) return word; } +/** + * Check if given uri matches one of the patterns given as comma separated + * string. + */ +static gboolean wildmatch(char *patterns, const char *uri) +{ + char *cur, *start; + gboolean matched; + + for (cur = start = patterns; *cur; cur++) { + /* iterate through the patterns until the next ',' */ + if (*cur == ',') { + *cur = '\0'; + matched = util_wildmatch(start, uri); + *cur = ','; + + /* return if the first match is found */ + if (matched) { + return true; + } + + /* set the start right after the ',' */ + start = cur + 1; + } + } + + /* still no match found - check the last pattern */ + return util_wildmatch(start, uri); +} + static AuGroup *new_group(const char *name) { AuGroup *new = g_slice_new(AuGroup); diff --git a/src/util.c b/src/util.c index 6a7d3b4..5c4a2ee 100644 --- a/src/util.c +++ b/src/util.c @@ -512,8 +512,6 @@ gboolean util_parse_expansion(const char **input, GString *str, int flags, * * matches any sequence of characters * ? matches any single character except of / * \? matches a ? - * . matches a . - * ~ matches a ~ */ gboolean util_wildmatch(const char *pattern, const char *string) { -- cgit v1.2.3