Allow multiple autocmd patterns (#100).
authorDaniel Carl <danielcarl@gmx.de>
Thu, 4 Sep 2014 20:04:58 +0000 (22:04 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Thu, 4 Sep 2014 20:04:58 +0000 (22:04 +0200)
src/autocmd.c
src/util.c

index 1d69a7e..c12b471 100644 (file)
@@ -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);
index 6a7d3b4..5c4a2ee 100644 (file)
@@ -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)
 {