i3 - improved tiling WM


reduce some yajl boilerplate

Patch status: merged

Patch by Vivien Didelot

Long description:

This patch introduces a yerror() macro in src/commands.c and also
removes some unused yajl helper macros from src/config_directives.c.

Signed-off-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com>

To apply this patch, use:
curl http://cr.i3wm.org/patch/238/raw.patch | git am

b/src/commands.c

18
@@ -24,6 +24,14 @@
19
     y(bool, success); \
20
     y(map_close); \
21
 } while (0)
22
+#define yerror(message) do { \
23
+    y(map_open); \
24
+    ystr("success"); \
25
+    y(bool, false); \
26
+    ystr("error"); \
27
+    ystr(message); \
28
+    y(map_close); \
29
+} while (0)
30
 
31
 /** When the command did not include match criteria (!), we use the currently
32
  * focused container. Do not confuse this case with a command which included
33
@@ -441,12 +449,7 @@ void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
34
     ws = workspace_back_and_forth_get();
35
 
36
     if (ws == NULL) {
37
-        y(map_open);
38
-        ystr("success");
39
-        y(bool, false);
40
-        ystr("error");
41
-        ystr("No workspace was previously active.");
42
-        y(map_close);
43
+        yerror("No workspace was previously active.");
44
         return;
45
     }
46
 
47
@@ -535,13 +538,8 @@ void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
48
         parsed_num < 0 ||
49
         endptr == which) {
50
         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
51
-        y(map_open);
52
-        ystr("success");
53
-        y(bool, false);
54
-        ystr("error");
55
         // TODO: better error message
56
-        ystr("Could not parse number");
57
-        y(map_close);
58
+        yerror("Could not parse number");
59
         return;
60
     }
61
 
62
@@ -939,13 +937,8 @@ void cmd_workspace_number(I3_CMD, char *which) {
63
         parsed_num < 0 ||
64
         endptr == which) {
65
         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
66
-        y(map_open);
67
-        ystr("success");
68
-        y(bool, false);
69
-        ystr("error");
70
         // TODO: better error message
71
-        ystr("Could not parse number");
72
-        y(map_close);
73
+        yerror("Could not parse number");
74
 
75
         return;
76
     }
77
@@ -1439,12 +1432,7 @@ void cmd_focus(I3_CMD) {
78
         ELOG("You have to specify which window/container should be focused.\n");
79
         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
80
 
81
-        y(map_open);
82
-        ystr("success");
83
-        y(bool, false);
84
-        ystr("error");
85
-        ystr("You have to specify which window/container should be focused");
86
-        y(map_close);
87
+        yerror("You have to specify which window/container should be focused");
88
 
89
         return;
90
     }
91
@@ -1750,12 +1738,7 @@ void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
92
 
93
     if (!con_is_floating(focused)) {
94
         ELOG("Cannot change position. The window/container is not floating\n");
95
-        y(map_open);
96
-        ystr("success");
97
-        y(bool, false);
98
-        ystr("error");
99
-        ystr("Cannot change position. The window/container is not floating.");
100
-        y(map_close);
101
+        yerror("Cannot change position. The window/container is not floating.");
102
         return;
103
     }
104
 
105
@@ -1790,12 +1773,7 @@ void cmd_move_window_to_center(I3_CMD, char *method) {
106
 
107
     if (!con_is_floating(focused)) {
108
         ELOG("Cannot change position. The window/container is not floating\n");
109
-        y(map_open);
110
-        ystr("success");
111
-        y(bool, false);
112
-        ystr("error");
113
-        ystr("Cannot change position. The window/container is not floating.");
114
-        y(map_close);
115
+        yerror("Cannot change position. The window/container is not floating.");
116
         return;
117
     }
118
 
119
@@ -1890,13 +1868,8 @@ void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
120
     if (!workspace) {
121
         // TODO: we should include the old workspace name here and use yajl for
122
         // generating the reply.
123
-        y(map_open);
124
-        ystr("success");
125
-        y(bool, false);
126
-        ystr("error");
127
         // TODO: better error message
128
-        ystr("Old workspace not found");
129
-        y(map_close);
130
+        yerror("Old workspace not found");
131
         return;
132
     }
133
 
134
@@ -1908,13 +1881,8 @@ void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
135
     if (check_dest != NULL) {
136
         // TODO: we should include the new workspace name here and use yajl for
137
         // generating the reply.
138
-        y(map_open);
139
-        ystr("success");
140
-        y(bool, false);
141
-        ystr("error");
142
         // TODO: better error message
143
-        ystr("New workspace already exists");
144
-        y(map_close);
145
+        yerror("New workspace already exists");
146
         return;
147
     }
148
 

b/src/config_directives.c

153
@@ -14,16 +14,6 @@
154
 
155
 #include "all.h"
156
 
157
-// Macros to make the YAJL API a bit easier to use.
158
-#define y(x, ...) yajl_gen_ ## x (cmd_output->json_gen, ##__VA_ARGS__)
159
-#define ystr(str) yajl_gen_string(cmd_output->json_gen, (unsigned char*)str, strlen(str))
160
-#define ysuccess(success) do { \
161
-    y(map_open); \
162
-    ystr("success"); \
163
-    y(bool, success); \
164
-    y(map_close); \
165
-} while (0)
166
-
167
 /*******************************************************************************
168
  * Criteria functions.
169
  ******************************************************************************/