[PATCH] disallow duplicate group names

Ali Gholami Rudi <[email protected]>
Newsgroups gmane.comp.window-managers.ratpoison.devel
Message-ID <[email protected]>
---
 src/actions.c |    8 +++++++-
 src/group.c   |    5 +++--
 src/group.h   |    2 +-
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/actions.c b/src/actions.c
index 292ffc4..c977075 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -1916,7 +1916,7 @@ find_group (char *str)
         return group;
     }
 
-  group = groups_find_group_by_name (str);
+  group = groups_find_group_by_name (str, 0);
   return group;
 }
 
@@ -4845,6 +4845,8 @@ cmd_gother (int interactive, struct cmdarg **args)
 cmdret *
 cmd_gnew (int interactive, struct cmdarg **args)
 {
+  if (groups_find_group_by_name (ARG_STRING (0), 1))
+    return cmdret_new (RET_FAILURE, "gnew: group already exists");
   set_current_group (group_add_new_group (ARG_STRING(0)));
   return cmdret_new (RET_SUCCESS, NULL);
 }
@@ -4852,6 +4854,8 @@ cmd_gnew (int interactive, struct cmdarg **args)
 cmdret *
 cmd_gnewbg (int interactive, struct cmdarg **args)
 {
+  if (groups_find_group_by_name (ARG_STRING (0), 1))
+    return cmdret_new (RET_FAILURE, "gnewbg: group already exists");
   group_add_new_group (ARG_STRING(0));
   return cmdret_new (RET_SUCCESS, NULL);
 }
@@ -4859,6 +4863,8 @@ cmd_gnewbg (int interactive, struct cmdarg **args)
 cmdret *
 cmd_grename (int interactive, struct cmdarg **args)
 {
+  if (groups_find_group_by_name (ARG_STRING (0), 1))
+    return cmdret_new (RET_FAILURE, "grename: duplicate group name");
   group_rename (rp_current_group, ARG_STRING(0));
   return cmdret_new (RET_SUCCESS, NULL);
 }
diff --git a/src/group.c b/src/group.c
index 1c4329b..5246b33 100644
--- a/src/group.c
+++ b/src/group.c
@@ -139,7 +139,7 @@ group_last_group (void)
 }
 
 rp_group *
-groups_find_group_by_name (char *s)
+groups_find_group_by_name (char *s, int exact_match)
 {
   rp_group *cur;
 
@@ -147,7 +147,8 @@ groups_find_group_by_name (char *s)
     {
       if (cur->name)
         {
-          if (str_comp (s, cur->name, strlen (s)))
+          if ((!exact_match && str_comp (s, cur->name, strlen (s))) ||
+              (exact_match && !strcmp (cur->name, s)))
             return cur;
         }
     }
diff --git a/src/group.h b/src/group.h
index 176e4ee..364100f 100644
--- a/src/group.h
+++ b/src/group.h
@@ -41,7 +41,7 @@ void groups_unmap_window (rp_window *win);
 
 rp_window *group_prev_window (rp_group *g, rp_window *win);
 rp_window *group_next_window (rp_group *g, rp_window *win);
-rp_group *groups_find_group_by_name (char *s);
+rp_group *groups_find_group_by_name (char *s, int exact_match);
 rp_group *groups_find_group_by_number (int n);
 rp_group *groups_find_group_by_window (rp_window *win);
 rp_group *groups_find_group_by_group (rp_group *g);
-- 
1.6.0.2.307.gc427.dirty
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.