LOG("Renaming current workspace to \"%s\"\n", new_name);
}
- Con *output, *workspace = NULL;
+ Con *workspace;
if (old_name) {
- TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
- GREP_FIRST(workspace, output_get_content(output),
- !strcasecmp(child->name, old_name));
+ workspace = get_existing_workspace_by_name(old_name);
} else {
workspace = con_get_workspace(focused);
old_name = workspace->name;
return;
}
- Con *check_dest = NULL;
- TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
- GREP_FIRST(check_dest, output_get_content(output),
- !strcasecmp(child->name, new_name));
+ Con *check_dest = get_existing_workspace_by_name(new_name);
/* If check_dest == workspace, the user might be changing the case of the
* workspace, or it might just be a no-op. */
/* Prevent name clashes when appending a workspace, e.g. when the
* user tries to restore a workspace called “1” but already has a
* workspace called “1”. */
- Con *output;
- Con *workspace = NULL;
- TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
- GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
char *base = sstrdup(json_node->name);
int cnt = 1;
- while (workspace != NULL) {
+ while (get_existing_workspace_by_name(json_node->name) != NULL) {
FREE(json_node->name);
sasprintf(&(json_node->name), "%s_%d", base, cnt++);
- workspace = NULL;
- TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
- GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
}
free(base);
* keybindings. */
static char **binding_workspace_names = NULL;
+/*
+ * Returns the workspace with the given name or NULL if such a workspace does
+ * not exist.
+ *
+ */
+Con *get_existing_workspace_by_name(const char *name) {
+ Con *output, *workspace = NULL;
+ TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
+ GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, name));
+ }
+
+ return workspace;
+}
+
/*
* Sets ws->layout to splith/splitv if default_orientation was specified in the
* configfile. Otherwise, it uses splith/splitv depending on whether the output
*
*/
Con *workspace_get(const char *num, bool *created) {
- Con *output, *workspace = NULL;
-
- TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
- GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
+ Con *workspace = get_existing_workspace_by_name(num);
if (workspace == NULL) {
LOG("Creating new workspace \"%s\"\n", num);
/* unless an assignment is found, we will create this workspace on the current output */
- output = con_get_output(focused);
+ Con *output = con_get_output(focused);
/* look for assignments */
struct Workspace_Assignment *assignment;
*/
Con *create_workspace_on_output(Output *output, Con *content) {
/* add a workspace to this output */
- Con *out, *current;
char *name;
bool exists = true;
Con *ws = con_new(NULL, NULL);
if (assigned)
continue;
- current = NULL;
- TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
- GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, target_name));
- exists = (current != NULL);
+ exists = (get_existing_workspace_by_name(target_name) != NULL);
if (!exists) {
ws->name = sstrdup(target_name);
/* Set ->num to the number of the workspace, if the name actually
ws->num = c;
- current = NULL;
+ Con *out, *current = NULL;
TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
GREP_FIRST(current, output_get_content(out), child->num == ws->num);
exists = (current != NULL);
TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
if (assignment->output == NULL || strcmp(assignment->output, output_primary_name(current_output)) != 0)
continue;
-
/* check if this workspace is already attached to the tree */
- Con *workspace = NULL, *out;
- TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
- GREP_FIRST(workspace, output_get_content(out),
- !strcasecmp(child->name, assignment->name));
- if (workspace != NULL)
+ if (get_existing_workspace_by_name(assignment->name) != NULL) {
continue;
+ }
/* so create the workspace referenced to by this assignment */
LOG("Creating workspace from assignment %s.\n", assignment->name);