Debian Codegraph @

src/actions.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /* Copyright (C) 2000, 2001, 2002, 2003, 2004 Shawn Betts <[email protected]>
2 *
3 * This file is part of ratpoison.
4 *
5 * ratpoison is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * ratpoison is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
19 */
20
21 #include <unistd.h>
22 #include <ctype.h>
23 #include <sys/wait.h>
24 #include <X11/keysym.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <limits.h>
31 #include <X11/Xproto.h>
32
33 #include "ratpoison.h"
34
35 #ifdef HAVE_LIBXTST
36 # include <X11/extensions/XTest.h>
37 #endif
38
39 #ifdef HAVE_SYS_IOCTL_H
40 # include <sys/ioctl.h>
41 #endif
42
43
44
45
46 /* arg_REST and arg_SHELLCMD eat the rest of the input. */
47 enum argtype {
48 arg_REST,
49 arg_NUMBER,
50 arg_STRING,
51 arg_FRAME,
52 arg_WINDOW,
53 arg_COMMAND,
54 arg_SHELLCMD,
55 arg_KEYMAP,
56 arg_KEY,
57 arg_GRAVITY,
58 arg_GROUP,
59 arg_HOOK,
60 arg_VARIABLE,
61 arg_RAW,
62 };
63
64 union arg_union {
65 rp_frame *frame;
66 int number;
67 float fnumber;
68 rp_window *win;
69 rp_keymap *keymap;
70 rp_group *group;
71 struct list_head *hook;
72 struct set_var *variable;
73 struct rp_key *key;
74 int gravity;
75 };
76
77 struct cmdarg
78 {
79 int type;
80 char *string;
81 union arg_union arg;
82 struct list_head node;
83 };
84 #define ARG_STRING(elt) args[elt]->string
85 #define ARG(elt, type) args[elt]->arg.type
86
87 struct argspec
88 {
89 int type;
90 char *prompt;
91 };
92
93 struct set_var
94 {
95 char *var;
96 cmdret *(*set_fn)(struct cmdarg **);
97 int nargs;
98 struct argspec *args;
99 struct list_head node;
100 };
101
102 struct user_command
103 {
104 char *name;
105 cmdret * (*func)(int, struct cmdarg **);
106 struct argspec *args;
107 int num_args;
108 /* The number of required arguments. Any arguments after that are
109 optional and won't be filled in when called
110 interactively. ni_required_args is used when called non-interactively,
111 i_required_args when called interactively. */
112 int ni_required_args, i_required_args;
113
114 struct list_head node;
115 };
116
117 typedef struct
118 {
119 char *name;
120 char *alias;
121 } alias_t;
122
123 typedef struct rp_frame_undo
124 {
125 char *frames;
126 rp_screen *screen;
127 struct list_head node;
128 } rp_frame_undo;
129
130
131
132 static LIST_HEAD (user_commands);
133 static LIST_HEAD (rp_keymaps);
134 static LIST_HEAD (set_vars);
135 static LIST_HEAD (rp_frame_undos);
136 static LIST_HEAD (rp_frame_redos);
137
138 static alias_t *alias_list;
139 static int alias_list_size;
140 static int alias_list_last;
141
142 static const char invalid_negative_arg[] = "invalid negative argument";
143
144
145
146 static cmdret* frestore (char *data, rp_screen *s);
147 static char* fdump (rp_screen *screen);
148 static int spawn(char *data, int raw, rp_frame *frame);
149
150 /* setter function prototypes */
151 static cmdret * set_resizeunit (struct cmdarg **args);
152 static cmdret * set_wingravity (struct cmdarg **args);
153 static cmdret * set_transgravity (struct cmdarg **args);
154 static cmdret * set_maxsizegravity (struct cmdarg **args);
155 static cmdret * set_bargravity (struct cmdarg **args);
156 static cmdret * set_font (struct cmdarg **args);
157 static cmdret * set_padding (struct cmdarg **args);
158 static cmdret * set_border (struct cmdarg **args);
159 static cmdret * set_barborder (struct cmdarg **args);
160 static cmdret * set_barinpadding (struct cmdarg **args);
161 static cmdret * set_inputwidth (struct cmdarg **args);
162 static cmdret * set_waitcursor (struct cmdarg **args);
163 static cmdret * set_winfmt (struct cmdarg **args);
164 static cmdret * set_winname (struct cmdarg **args);
165 static cmdret * set_framefmt (struct cmdarg **args);
166 static cmdret * set_fgcolor (struct cmdarg **args);
167 static cmdret * set_bgcolor (struct cmdarg **args);
168 static cmdret * set_fwcolor (struct cmdarg **args);
169 static cmdret * set_bwcolor (struct cmdarg **args);
170 static cmdret * set_barpadding (struct cmdarg **args);
171 static cmdret * set_winliststyle (struct cmdarg **args);
172 static cmdret * set_framesels (struct cmdarg **args);
173 static cmdret * set_maxundos (struct cmdarg **args);
174 static cmdret * set_infofmt (struct cmdarg **args);
175 static cmdret * set_topkmap (struct cmdarg **args);
176 static cmdret * set_historysize (struct cmdarg **args);
177 static cmdret * set_historycompaction (struct cmdarg **args);
178 static cmdret * set_historyexpansion (struct cmdarg **args);
179 static cmdret * set_msgwait(struct cmdarg **args);
180 static cmdret * set_framemsgwait(struct cmdarg **args);
181 static cmdret * set_startupmessage(struct cmdarg **args);
182 static cmdret * set_warp(struct cmdarg **args);
183 static cmdret * set_rudeness(struct cmdarg **args);
184
185 /* command function prototypes. */
186 static cmdret *cmd_abort (int interactive, struct cmdarg **args);
187 static cmdret *cmd_addhook (int interactive, struct cmdarg **args);
188 static cmdret *cmd_alias (int interactive, struct cmdarg **args);
189 static cmdret *cmd_banish (int interactive, struct cmdarg **args);
190 static cmdret *cmd_banishrel (int interactive, struct cmdarg **args);
191 static cmdret *cmd_chdir (int interactive, struct cmdarg **args);
192 static cmdret *cmd_clrunmanaged (int interactive, struct cmdarg **args);
193 static cmdret *cmd_colon (int interactive, struct cmdarg **args);
194 static cmdret *cmd_commands (int interactive, struct cmdarg **args);
195 static cmdret *cmd_curframe (int interactive, struct cmdarg **args);
196 static cmdret *cmd_delete (int interactive, struct cmdarg **args);
197 static cmdret *cmd_echo (int interactive, struct cmdarg **args);
198 static cmdret *cmd_escape (int interactive, struct cmdarg **args);
199 static cmdret *cmd_exec (int interactive, struct cmdarg **args);
200 static cmdret *cmd_execa (int interactive, struct cmdarg **args);
201 static cmdret *cmd_execf (int interactive, struct cmdarg **args);
202 static cmdret *cmd_fdump (int interactive, struct cmdarg **args);
203 static cmdret *cmd_focusdown (int interactive, struct cmdarg **args);
204 static cmdret *cmd_focuslast (int interactive, struct cmdarg **args);
205 static cmdret *cmd_focusleft (int interactive, struct cmdarg **args);
206 static cmdret *cmd_focusright (int interactive, struct cmdarg **args);
207 static cmdret *cmd_focusup (int interactive, struct cmdarg **args);
208 static cmdret *cmd_exchangeup (int interactive, struct cmdarg **args);
209 static cmdret *cmd_exchangedown (int interactive, struct cmdarg **args);
210 static cmdret *cmd_exchangeleft (int interactive, struct cmdarg **args);
211 static cmdret *cmd_exchangeright (int interactive, struct cmdarg **args);
212 static cmdret *cmd_swap (int interactive, struct cmdarg **args);
213 static cmdret *cmd_frestore (int interactive, struct cmdarg **args);
214 static cmdret *cmd_fselect (int interactive, struct cmdarg **args);
215 static cmdret *cmd_gdelete (int interactive, struct cmdarg **args);
216 static cmdret *cmd_getenv (int interactive, struct cmdarg **args);
217 static cmdret *cmd_gmerge (int interactive, struct cmdarg **args);
218 static cmdret *cmd_gmove (int interactive, struct cmdarg **args);
219 static cmdret *cmd_gnew (int interactive, struct cmdarg **args);
220 static cmdret *cmd_gnewbg (int interactive, struct cmdarg **args);
221 static cmdret *cmd_gnext (int interactive, struct cmdarg **args);
222 static cmdret *cmd_gnumber (int interactive, struct cmdarg **args);
223 static cmdret *cmd_gprev (int interactive, struct cmdarg **args);
224 static cmdret *cmd_gother (int interactive, struct cmdarg **args);
225 static cmdret *cmd_gravity (int interactive, struct cmdarg **args);
226 static cmdret *cmd_grename (int interactive, struct cmdarg **args);
227 static cmdret *cmd_groups (int interactive, struct cmdarg **args);
228 static cmdret *cmd_gselect (int interactive, struct cmdarg **args);
229 static cmdret *cmd_h_split (int interactive, struct cmdarg **args);
230 static cmdret *cmd_help (int interactive, struct cmdarg **args);
231 static cmdret *cmd_info (int interactive, struct cmdarg **args);
232 static cmdret *cmd_kill (int interactive, struct cmdarg **args);
233 static cmdret *cmd_lastmsg (int interactive, struct cmdarg **args);
234 static cmdret *cmd_license (int interactive, struct cmdarg **args);
235 static cmdret *cmd_link (int interactive, struct cmdarg **args);
236 static cmdret *cmd_listhook (int interactive, struct cmdarg **args);
237 static cmdret *cmd_meta (int interactive, struct cmdarg **args);
238 static cmdret *cmd_msgwait (int interactive, struct cmdarg **args);
239 static cmdret *cmd_newwm (int interactive, struct cmdarg **args);
240 static cmdret *cmd_next (int interactive, struct cmdarg **args);
241 static cmdret *cmd_next_frame (int interactive, struct cmdarg **args);
242 static cmdret *cmd_nextscreen (int interactive, struct cmdarg **args);
243 static cmdret *cmd_number (int interactive, struct cmdarg **args);
244 static cmdret *cmd_only (int interactive, struct cmdarg **args);
245 static cmdret *cmd_other (int interactive, struct cmdarg **args);
246 static cmdret *cmd_prev (int interactive, struct cmdarg **args);
247 static cmdret *cmd_prev_frame (int interactive, struct cmdarg **args);
248 static cmdret *cmd_prevscreen (int interactive, struct cmdarg **args);
249 static cmdret *cmd_quit (int interactive, struct cmdarg **args);
250 static cmdret *cmd_redisplay (int interactive, struct cmdarg **args);
251 static cmdret *cmd_remhook (int interactive, struct cmdarg **args);
252 static cmdret *cmd_remove (int interactive, struct cmdarg **args);
253 static cmdret *cmd_rename (int interactive, struct cmdarg **args);
254 static cmdret *cmd_resize (int interactive, struct cmdarg **args);
255 static cmdret *cmd_restart (int interactive, struct cmdarg **args);
256 static cmdret *cmd_rudeness (int interactive, struct cmdarg **args);
257 static cmdret *cmd_select (int interactive, struct cmdarg **args);
258 static cmdret *cmd_setenv (int interactive, struct cmdarg **args);
259 static cmdret *cmd_shrink (int interactive, struct cmdarg **args);
260 static cmdret *cmd_source (int interactive, struct cmdarg **args);
261 static cmdret *cmd_startup_message (int interactive, struct cmdarg **args);
262 static cmdret *cmd_time (int interactive, struct cmdarg **args);
263 static cmdret *cmd_tmpwm (int interactive, struct cmdarg **args);
264 static cmdret *cmd_unalias (int interactive, struct cmdarg **args);
265 static cmdret *cmd_unmanage (int interactive, struct cmdarg **args);
266 static cmdret *cmd_unsetenv (int interactive, struct cmdarg **args);
267 static cmdret *cmd_v_split (int interactive, struct cmdarg **args);
268 static cmdret *cmd_verbexec (int interactive, struct cmdarg **args);
269 static cmdret *cmd_version (int interactive, struct cmdarg **args);
270 static cmdret *cmd_warp (int interactive, struct cmdarg **args);
271 static cmdret *cmd_windows (int interactive, struct cmdarg **args);
272 static cmdret *cmd_readkey (int interactive, struct cmdarg **args);
273 static cmdret *cmd_newkmap (int interactive, struct cmdarg **args);
274 static cmdret *cmd_delkmap (int interactive, struct cmdarg **args);
275 static cmdret *cmd_definekey (int interactive, struct cmdarg **args);
276 static cmdret *cmd_undefinekey (int interactive, struct cmdarg **args);
277 static cmdret *cmd_set (int interactive, struct cmdarg **args);
278 static cmdret *cmd_sselect (int interactive, struct cmdarg **args);
279 static cmdret *cmd_ratwarp (int interactive, struct cmdarg **args);
280 static cmdret *cmd_ratinfo (int interactive, struct cmdarg **args);
281 static cmdret *cmd_ratrelinfo (int interactive, struct cmdarg **args);
282 static cmdret *cmd_ratclick (int interactive, struct cmdarg **args);
283 static cmdret *cmd_ratrelwarp (int interactive, struct cmdarg **args);
284 static cmdret *cmd_rathold (int interactive, struct cmdarg **args);
285 static cmdret *cmd_cnext (int interactive, struct cmdarg **args);
286 static cmdret *cmd_cother (int interactive, struct cmdarg **args);
287 static cmdret *cmd_cprev (int interactive, struct cmdarg **args);
288 static cmdret *cmd_dedicate (int interactive, struct cmdarg **args);
289 static cmdret *cmd_describekey (int interactive, struct cmdarg **args);
290 static cmdret *cmd_inext (int interactive, struct cmdarg **args);
291 static cmdret *cmd_iother (int interactive, struct cmdarg **args);
292 static cmdret *cmd_iprev (int interactive, struct cmdarg **args);
293 static cmdret *cmd_prompt (int interactive, struct cmdarg **args);
294 static cmdret *cmd_sdump (int interactive, struct cmdarg **args);
295 static cmdret *cmd_sfdump (int interactive, struct cmdarg **args);
296 static cmdret *cmd_sfrestore (int interactive, struct cmdarg **args);
297 static cmdret *cmd_undo (int interactive, struct cmdarg **args);
298 static cmdret *cmd_redo (int interactive, struct cmdarg **args);
299 static cmdret *cmd_putsel (int interactive, struct cmdarg **args);
300 static cmdret *cmd_getsel (int interactive, struct cmdarg **args);
301
302
303
304 static void
305 add_set_var (char *name, cmdret * (*fn)(struct cmdarg **), int nargs, ...)
306 {
307 int i = 0;
308 struct set_var *var;
309 va_list va;
310
311 var = xmalloc (sizeof (struct set_var));
312 var->var = name;
313 var->set_fn = fn;
314 var->nargs = nargs;
315 var->args = xmalloc(sizeof(struct argspec) * nargs);
316
317 /* Fill var->args */
318 va_start(va, nargs);
319 for (i=0; i<nargs; i++)
320 {
321 var->args[i].prompt = va_arg(va, char*);
322 var->args[i].type = va_arg(va, int);
323 }
324 va_end(va);
325
326 list_add_tail (&var->node, &set_vars);
327 }
328
329 static void
330 set_var_free (struct set_var *var)
331 {
332 if (var == NULL)
333 return;
334 free(var->args);
335 free(var);
336 }
337
338 static void
339 init_set_vars (void)
340 {
341 /* Keep this sorted alphabetically. */
342 add_set_var ("barborder", set_barborder, 1, "", arg_NUMBER);
343 add_set_var ("bargravity", set_bargravity, 1, "", arg_GRAVITY);
344 add_set_var ("barinpadding", set_barinpadding, 1, "", arg_NUMBER);
345 add_set_var ("barpadding", set_barpadding, 2, "", arg_NUMBER, "", arg_NUMBER);
346 add_set_var ("bgcolor", set_bgcolor, 1, "", arg_STRING);
347 add_set_var ("border", set_border, 1, "", arg_NUMBER);
348 add_set_var ("bwcolor", set_bwcolor, 1, "", arg_STRING);
349 add_set_var ("fgcolor", set_fgcolor, 1, "", arg_STRING);
350 add_set_var ("font", set_font, 1, "", arg_STRING);
351 add_set_var ("framefmt", set_framefmt, 1, "", arg_REST);
352 add_set_var ("framemsgwait", set_framemsgwait, 1, "", arg_NUMBER);
353 add_set_var ("framesels", set_framesels, 1, "", arg_STRING);
354 add_set_var ("fwcolor", set_fwcolor, 1, "", arg_STRING);
355 add_set_var ("historycompaction", set_historycompaction, 1, "", arg_NUMBER);
356 add_set_var ("historyexpansion", set_historyexpansion, 1, "", arg_NUMBER);
357 add_set_var ("historysize", set_historysize, 1, "", arg_NUMBER);
358 add_set_var ("infofmt", set_infofmt, 1, "", arg_REST);
359 add_set_var ("inputwidth", set_inputwidth, 1, "", arg_NUMBER);
360 add_set_var ("maxsizegravity", set_maxsizegravity, 1, "", arg_GRAVITY);
361 add_set_var ("maxundos", set_maxundos, 1, "", arg_NUMBER);
362 add_set_var ("msgwait", set_msgwait, 1, "", arg_NUMBER);
363 add_set_var ("padding", set_padding, 4, "", arg_NUMBER, "", arg_NUMBER, "",
364 arg_NUMBER, "", arg_NUMBER);
365 add_set_var ("resizeunit", set_resizeunit, 1, "", arg_NUMBER);
366 add_set_var ("rudeness", set_rudeness, 1, "", arg_NUMBER);
367 add_set_var ("startupmessage", set_startupmessage, 1, "", arg_NUMBER);
368 add_set_var ("topkmap", set_topkmap, 1, "", arg_STRING);
369 add_set_var ("transgravity", set_transgravity, 1, "", arg_GRAVITY);
370 add_set_var ("waitcursor", set_waitcursor, 1, "", arg_NUMBER);
371 add_set_var ("warp", set_warp, 1, "", arg_NUMBER);
372 add_set_var ("winfmt", set_winfmt, 1, "", arg_REST);
373 add_set_var ("wingravity", set_wingravity, 1, "", arg_GRAVITY);
374 add_set_var ("winliststyle", set_winliststyle, 1, "", arg_STRING);
375 add_set_var ("winname", set_winname, 1, "", arg_STRING);
376 }
377
378 /* i_nrequired is the number required when called
379 interactively. ni_nrequired is when called non-interactively. */
380 static void
381 add_command (char *name, cmdret * (*fn)(int, struct cmdarg **), int nargs, int i_nrequired, int ni_nrequired, ...)
382 {
383 int i = 0;
384 struct user_command *cmd;
385 va_list va;
386
387 cmd = xmalloc (sizeof (struct user_command));
388 cmd->name = name;
389 cmd->func = fn;
390 cmd->num_args = nargs;
391 cmd->ni_required_args = ni_nrequired;
392 cmd->i_required_args = i_nrequired;
393 cmd->args = nargs ? xmalloc (nargs * sizeof (struct argspec)) : NULL;
394
395 /* Fill cmd->args */
396 va_start(va, ni_nrequired);
397 for (i=0; i<nargs; i++)
398 {
399 cmd->args[i].prompt = va_arg(va, char*);
400 cmd->args[i].type = va_arg(va, int);
401 }
402 va_end(va);
403
404 list_add (&cmd->node, &user_commands);
405 }
406
407 static void
408 user_command_free(struct user_command *cmd)
409 {
410 if (cmd == NULL )
411 return;
412
413 free(cmd->args);
414 free(cmd);
415 }
416
417 void
418 init_user_commands(void)
419 {
420 /*@begin (tag required for genrpbindings) */
421 add_command ("abort", cmd_abort, 0, 0, 0);
422 add_command ("addhook", cmd_addhook, 2, 2, 2,
423 "Hook: ", arg_HOOK,
424 "Command: ", arg_COMMAND);
425 add_command ("alias", cmd_alias, 2, 2, 2,
426 "Alias: ", arg_STRING,
427 "Command: ", arg_COMMAND);
428 add_command ("banish", cmd_banish, 0, 0, 0);
429 add_command ("chdir", cmd_chdir, 1, 0, 0,
430 "Dir: ", arg_REST);
431 add_command ("clrunmanaged", cmd_clrunmanaged, 0, 0, 0);
432 add_command ("colon", cmd_colon, 1, 0, 0,
433 "", arg_REST);
434 add_command ("curframe", cmd_curframe, 0, 0, 0);
435 add_command ("definekey", cmd_definekey, 3, 3, 3,
436 "Keymap: ", arg_KEYMAP,
437 "Key: ", arg_KEY,
438 "Command: ", arg_COMMAND);
439 add_command ("undefinekey", cmd_undefinekey, 2, 2, 2,
440 "Keymap: ", arg_KEYMAP,
441 "Key: ", arg_KEY);
442 add_command ("delete", cmd_delete, 0, 0, 0);
443 add_command ("delkmap", cmd_delkmap, 1, 1, 1,
444 "Keymap: ", arg_KEYMAP);
445 add_command ("echo", cmd_echo, 1, 1, 1,
446 "Echo: ", arg_RAW);
447 add_command ("escape", cmd_escape, 1, 1, 1,
448 "Key: ", arg_KEY);
449 add_command ("exec", cmd_exec, 1, 1, 1,
450 "/bin/sh -c ", arg_SHELLCMD);
451 add_command ("execa", cmd_execa, 1, 1, 1,
452 "/bin/sh -c ", arg_SHELLCMD);
453 add_command ("execf", cmd_execf, 2, 2, 2,
454 "frame to execute in:", arg_FRAME,
455 "/bin/sh -c ", arg_SHELLCMD);
456 add_command ("fdump", cmd_fdump, 1, 0, 0,
457 "", arg_NUMBER);
458 add_command ("focus", cmd_next_frame, 0, 0, 0);
459 add_command ("focusprev", cmd_prev_frame, 0, 0, 0);
460 add_command ("focusdown", cmd_focusdown, 0, 0, 0);
461 add_command ("exchangeup", cmd_exchangeup, 0, 0, 0);
462 add_command ("exchangedown", cmd_exchangedown, 0, 0, 0);
463 add_command ("exchangeleft", cmd_exchangeleft, 0, 0, 0);
464 add_command ("exchangeright", cmd_exchangeright, 0, 0, 0);
465 add_command ("swap", cmd_swap, 2, 1, 1,
466 "destination frame: ", arg_FRAME,
467 "source frame: ", arg_FRAME);
468 add_command ("focuslast", cmd_focuslast, 0, 0, 0);
469 add_command ("focusleft", cmd_focusleft, 0, 0, 0);
470 add_command ("focusright", cmd_focusright, 0, 0, 0);
471 add_command ("focusup", cmd_focusup, 0, 0, 0);
472 add_command ("frestore", cmd_frestore, 1, 1, 1,
473 "Frames: ", arg_REST);
474 add_command ("fselect", cmd_fselect, 1, 1, 1,
475 "", arg_FRAME);
476 add_command ("gdelete", cmd_gdelete, 1, 0, 0,
477 "Group:", arg_GROUP);
478 add_command ("getenv", cmd_getenv, 1, 1, 1,
479 "Variable: ", arg_STRING);
480 add_command ("gmerge", cmd_gmerge, 1, 1, 1,
481 "Group: ", arg_GROUP);
482 add_command ("gmove", cmd_gmove, 1, 1, 1,
483 "Group: ", arg_GROUP);
484 add_command ("gnew", cmd_gnew, 1, 1, 1,
485 "Name: ", arg_STRING);
486 add_command ("gnewbg", cmd_gnewbg, 1, 1, 1,
487 "Name: ", arg_STRING);
488 add_command ("gnumber", cmd_gnumber, 2, 1, 1,
489 "Number: ", arg_NUMBER,
490 "Number: ", arg_NUMBER);
491 add_command ("grename", cmd_grename, 1, 1, 1,
492 "Change group name to: ", arg_REST);
493 add_command ("gnext", cmd_gnext, 0, 0, 0);
494 add_command ("gprev", cmd_gprev, 0, 0, 0);
495 add_command ("gother", cmd_gother, 0, 0, 0);
496 add_command ("gravity", cmd_gravity, 1, 0, 0,
497 "Gravity: ", arg_GRAVITY);
498 add_command ("groups", cmd_groups, 0, 0, 0);
499 add_command ("gselect", cmd_gselect, 1, 1, 1,
500 "Group: ", arg_GROUP);
501 add_command ("help", cmd_help, 1, 0, 0,
502 "Keymap: ", arg_KEYMAP);
503 add_command ("hsplit", cmd_h_split, 1, 0, 0,
504 "Split: ", arg_STRING);
505 add_command ("info", cmd_info, 1, 0, 0,
506 "Format: ", arg_REST);
507 add_command ("kill", cmd_kill, 0, 0, 0);
508 add_command ("lastmsg", cmd_lastmsg, 0, 0, 0);
509 add_command ("license", cmd_license, 0, 0, 0);
510 add_command ("link", cmd_link, 2, 1, 1,
511 "Key: ", arg_STRING,
512 "Keymap: ", arg_KEYMAP);
513 add_command ("listhook", cmd_listhook, 1, 1, 1,
514 "Hook: ", arg_HOOK);
515 add_command ("meta", cmd_meta, 1, 0, 0,
516 "key: ", arg_KEY);
517 add_command ("msgwait", cmd_msgwait, 1, 0, 0,
518 "", arg_NUMBER);
519 add_command ("newkmap", cmd_newkmap, 1, 1, 1,
520 "Keymap: ", arg_STRING);
521 add_command ("newwm", cmd_newwm, 1, 1, 1,
522 "Switch to wm: ", arg_REST);
523 add_command ("next", cmd_next, 0, 0, 0);
524 add_command ("nextscreen", cmd_nextscreen, 0, 0, 0);
525 add_command ("number", cmd_number, 2, 1, 1,
526 "Number: ", arg_NUMBER,
527 "Number: ", arg_NUMBER);
528 add_command ("only", cmd_only, 0, 0, 0);
529 add_command ("other", cmd_other, 0, 0, 0);
530 add_command ("prev", cmd_prev, 0, 0, 0);
531 add_command ("prevscreen", cmd_prevscreen, 0, 0, 0);
532 add_command ("quit", cmd_quit, 0, 0, 0);
533 add_command ("ratinfo", cmd_ratinfo, 0, 0, 0);
534 add_command ("ratrelinfo", cmd_ratrelinfo, 0, 0, 0);
535 add_command ("banishrel", cmd_banishrel, 0, 0, 0);
536 add_command ("ratwarp", cmd_ratwarp, 2, 2, 2,
537 "X: ", arg_NUMBER,
538 "Y: ", arg_NUMBER);
539 add_command ("ratrelwarp", cmd_ratrelwarp, 2, 2, 2,
540 "X: ", arg_NUMBER,
541 "Y: ", arg_NUMBER);
542 add_command ("ratclick", cmd_ratclick, 1, 0, 0,
543 "Button: ", arg_NUMBER);
544 add_command ("rathold", cmd_rathold, 2, 1, 1,
545 "State: ", arg_STRING,
546 "Button: ", arg_NUMBER);
547 add_command ("readkey", cmd_readkey, 1, 1, 1,
548 "Keymap: ", arg_KEYMAP);
549 add_command ("redisplay", cmd_redisplay, 0, 0, 0);
550 add_command ("remhook", cmd_remhook, 2, 2, 2,
551 "Hook: ", arg_HOOK,
552 "Command: ", arg_COMMAND);
553 add_command ("remove", cmd_remove, 0, 0, 0);
554 add_command ("resize", cmd_resize, 2, 0, 2,
555 "", arg_NUMBER,
556 "", arg_NUMBER);
557 add_command ("restart", cmd_restart, 0, 0, 0);
558 add_command ("rudeness", cmd_rudeness, 1, 0, 0,
559 "Rudeness: ", arg_NUMBER);
560 add_command ("select", cmd_select, 1, 0, 1,
561 "Select window: ", arg_REST);
562 add_command ("set", cmd_set, 2, 0, 0,
563 "", arg_VARIABLE,
564 "", arg_REST);
565 add_command ("setenv", cmd_setenv, 2, 2, 2,
566 "Variable: ", arg_STRING,
567 "Value: ", arg_REST);
568 add_command ("shrink", cmd_shrink, 0, 0, 0);
569 add_command ("sfrestore", cmd_sfrestore, 1, 1, 1,
570 "Frames: ", arg_REST);
571 add_command ("source", cmd_source, 1, 1, 1,
572 "File: ", arg_REST);
573 add_command ("sselect", cmd_sselect, 1, 1, 1,
574 "Screen: ", arg_NUMBER);
575 add_command ("startup_message", cmd_startup_message, 1, 1, 1,
576 "Startup message: ", arg_STRING);
577 add_command ("time", cmd_time, 0, 0, 0);
578 add_command ("title", cmd_rename, 1, 1, 1,
579 "Set window's title to: ", arg_REST);
580 add_command ("tmpwm", cmd_tmpwm, 1, 1, 1,
581 "Tmp wm: ", arg_REST);
582 add_command ("unalias", cmd_unalias, 1, 1, 1,
583 "Alias: ", arg_STRING);
584 add_command ("unmanage", cmd_unmanage, 1, 1, 0,
585 "Unmanage: ", arg_REST);
586 add_command ("unsetenv", cmd_unsetenv, 1, 1, 1,
587 "Variable: ", arg_STRING);
588 add_command ("verbexec", cmd_verbexec, 1, 1, 1,
589 "/bin/sh -c ", arg_SHELLCMD);
590 add_command ("version", cmd_version, 0, 0, 0);
591 add_command ("vsplit", cmd_v_split, 1, 0, 0,
592 "Split: ", arg_STRING);
593 add_command ("warp", cmd_warp, 1, 1, 1,
594 "Warp State: ", arg_STRING);
595 add_command ("windows", cmd_windows, 1, 0, 0,
596 "", arg_REST);
597 add_command ("cnext", cmd_cnext, 0, 0, 0);
598 add_command ("cother", cmd_cother, 0, 0, 0);
599 add_command ("cprev", cmd_cprev, 0, 0, 0);
600 add_command ("dedicate", cmd_dedicate, 1, 0, 0,
601 "", arg_NUMBER);
602 add_command ("describekey", cmd_describekey, 1, 1, 1,
603 "Keymap: ", arg_KEYMAP);
604 add_command ("inext", cmd_inext, 0, 0, 0);
605 add_command ("iother", cmd_iother, 0, 0, 0);
606 add_command ("iprev", cmd_iprev, 0, 0, 0);
607 add_command ("prompt", cmd_prompt, 1, 0, 0,
608 "", arg_REST);
609 add_command ("sdump", cmd_sdump, 0, 0, 0);
610 add_command ("sfdump", cmd_sfdump, 0, 0, 0);
611 add_command ("undo", cmd_undo, 0, 0, 0);
612 add_command ("redo", cmd_redo, 0, 0, 0);
613 add_command ("putsel", cmd_putsel, 1, 1, 1,
614 "Text: ", arg_RAW);
615 add_command ("getsel", cmd_getsel, 0, 0, 0);
616 add_command ("commands", cmd_commands, 0, 0, 0);
617 /*@end (tag required for genrpbindings) */
618
619 /*
620 The following screen commands may or may not be able to be
621 implemented. See the screen documentation for what should be
622 emulated with these commands
623 - msgminwait
624 - nethack
625 - sleep
626 - stuff
627 */
628
629 init_set_vars();
630 }
631
632 /* Delete all entries in the redo list. */
633 static void
634 clear_frame_redos (void)
635 {
636 rp_frame_undo *cur;
637 struct list_head *tmp, *iter;
638
639 list_for_each_safe_entry (cur, iter, tmp, &rp_frame_redos, node)
640 {
641 free (cur->frames);
642 list_del (&(cur->node));
643 }
644 }
645
646 static void
647 del_frame_undo (rp_frame_undo *u)
648 {
649 if (!u) return;
650 free (u->frames);
651 list_del (&(u->node));
652 free (u);
653 }
654
655 void
656 clear_frame_undos (void)
657 {
658 while (list_size (&rp_frame_undos) > 0)
659 {
660 /* Delete the oldest node */
661 rp_frame_undo *cur;
662 list_last (cur, &rp_frame_undos, node);
663 del_frame_undo (cur);
664 }
665 }
666
667 static void
668 push_frame_undo(rp_screen *screen)
669 {
670 rp_frame_undo *cur;
671 if (list_size (&rp_frame_undos) > defaults.maxundos)
672 {
673 /* Delete the oldest node */
674 list_last (cur, &rp_frame_undos, node);
675 del_frame_undo (cur);
676 }
677 cur = xmalloc (sizeof(rp_frame_undo));
678 cur->frames = fdump (screen);
679 cur->screen = screen;
680 list_add (&cur->node, &rp_frame_undos);
681 /* Since we're creating new frames the redo list is now invalid, so
682 clear it. */
683 clear_frame_redos();
684 }
685
686 static rp_frame_undo *
687 pop_frame_list (struct list_head *undo_list, struct list_head *redo_list)
688 {
689 rp_screen *screen = rp_current_screen;
690 rp_frame_undo *first, *new;
691
692 /* Is there something to restore? */
693 list_first (first, undo_list, node);
694 if (!first)
695 return NULL;
696
697 /* First save the current layout into undo */
698 new = xmalloc (sizeof(rp_frame_undo));
699 new->frames = fdump (screen);
700 new->screen = screen;
701 list_add (&new->node, redo_list);
702
703 list_del (&(first->node));
704 return first;
705 }
706
707 /* Pop the head of the frame undo list off and put it in the redo list. */
708 static rp_frame_undo *
709 pop_frame_undo (void)
710 {
711 return pop_frame_list (&rp_frame_undos, &rp_frame_redos);
712 }
713
714 /* Pop the head of the frame redo list off and put it in the undo list. */
715 static rp_frame_undo *
716 pop_frame_redo (void)
717 {
718 return pop_frame_list (&rp_frame_redos, &rp_frame_undos);
719 }
720
721 rp_action*
722 find_keybinding_by_action (char *action, rp_keymap *map)
723 {
724 int i;
725
726 for (i=0; i<map->actions_last; i++)
727 {
728 if (!strcmp (map->actions[i].data, action))
729 {
730 return &map->actions[i];
731 }
732 }
733
734 return NULL;
735 }
736
737 rp_action*
738 find_keybinding (KeySym keysym, unsigned int state, rp_keymap *map)
739 {
740 int i;
741 for (i = 0; i < map->actions_last; i++)
742 {
743 if (map->actions[i].key == keysym
744 && map->actions[i].state == state)
745 return &map->actions[i];
746 }
747 return NULL;
748 }
749
750 static char *
751 find_command_by_keydesc (char *desc, rp_keymap *map)
752 {
753 int i = 0;
754 char *keysym_name;
755
756 while (i < map->actions_last)
757 {
758 keysym_name = keysym_to_string (map->actions[i].key, map->actions[i].state);
759 if (!strcmp (keysym_name, desc))
760 {
761 free (keysym_name);
762 return map->actions[i].data;
763 }
764 free (keysym_name);
765 i++;
766 }
767
768 return NULL;
769 }
770
771 static char *
772 resolve_command_from_keydesc (char *desc, int depth, rp_keymap *map)
773 {
774 char *cmd, *c;
775
776 c = find_command_by_keydesc (desc, map);
777 if (!c)
778 return NULL;
779
780 /* is it a link? */
781 if (strncmp (c, "link", 4) || depth > MAX_LINK_DEPTH)
782 /* it is not */
783 return c;
784
785 cmd = resolve_command_from_keydesc (&c[5], depth + 1, map);
786 return (cmd != NULL) ? cmd : c;
787 }
788
789
790 static void
791 add_keybinding (KeySym keysym, int state, char *cmd, rp_keymap *map)
792 {
793 if (map->actions_last >= map->actions_size)
794 {
795 /* double the key table size */
796 map->actions_size *= 2;
797 map->actions = xrealloc (map->actions, sizeof (rp_action) * map->actions_size);
798 PRINT_DEBUG (("realloc()ed key_table %d\n", map->actions_size));
799 }
800
801 map->actions[map->actions_last].key = keysym;
802 map->actions[map->actions_last].state = state;
803 /* free this on shutdown, or re/unbinding */
804 map->actions[map->actions_last].data = xstrdup (cmd);
805
806 map->actions_last++;
807 }
808
809 static void
810 replace_keybinding (rp_action *key_action, char *newcmd)
811 {
812 free (key_action->data);
813 key_action->data = xstrdup (newcmd);
814 }
815
816 static int
817 remove_keybinding (KeySym keysym, unsigned int state, rp_keymap *map)
818 {
819 int i;
820 int found = -1;
821
822 for (i=0; i<map->actions_last; i++)
823 {
824 if (map->actions[i].key == keysym && map->actions[i].state == state)
825 {
826 found = i;
827 break;
828 }
829 }
830
831 if (found >= 0)
832 {
833 free (map->actions[found].data);
834
835 memmove (&map->actions[found], &map->actions[found+1],
836 sizeof (rp_action) * (map->actions_last - found - 1));
837 map->actions_last--;
838
839 return 1;
840 }
841
842 return 0;
843 }
844
845 static rp_keymap *
846 keymap_new (char *name)
847 {
848 rp_keymap *map;
849
850 /* All keymaps must have a name. */
851 if (name == NULL)
852 return NULL;
853
854 map = xmalloc (sizeof (rp_keymap));
855 map->name = xstrdup (name);
856 map->actions_size = 1;
857 map->actions = xmalloc (sizeof (rp_action) * map->actions_size);
858 map->actions_last = 0;
859
860 return map;
861 }
862
863 rp_keymap *
864 find_keymap (char *name)
865 {
866 rp_keymap *cur;
867
868 list_for_each_entry (cur, &rp_keymaps, node)
869 {
870 if (!strcmp (name, cur->name))
871 {
872 return cur;
873 }
874 }
875
876 return NULL;
877 }
878
879 /* Search the alias table for a match. If a match is found, return its
880 index into the table. Otherwise return -1. */
881 static int
882 find_alias_index (char *name)
883 {
884 int i;
885
886 for (i=0; i<alias_list_last; i++)
887 if (!strcmp (name, alias_list[i].name))
888 return i;
889
890 return -1;
891 }
892
893 static void
894 add_alias (char *name, char *alias)
895 {
896 int i;
897
898 /* Are we updating an existing alias, or creating a new one? */
899 i = find_alias_index (name);
900 if (i >= 0)
901 {
902 free (alias_list[i].alias);
903 alias_list[i].alias = xstrdup (alias);
904 }
905 else
906 {
907 if (alias_list_last >= alias_list_size)
908 {
909 alias_list_size *= 2;
910 alias_list = xrealloc (alias_list, sizeof (alias_t) * alias_list_size);
911 }
912
913 alias_list[alias_list_last].name = xstrdup (name);
914 alias_list[alias_list_last].alias = xstrdup (alias);
915 alias_list_last++;
916 }
917 }
918
919 void
920 initialize_default_keybindings (void)
921 {
922 rp_keymap *map, *top;
923
924 map = keymap_new (ROOT_KEYMAP);
925 list_add (&map->node, &rp_keymaps);
926
927 top = keymap_new (defaults.top_kmap);
928 list_add (&top->node, &rp_keymaps);
929
930 /* Initialize the alias list. */
931 alias_list_size = 5;
932 alias_list_last = 0;
933 alias_list = xmalloc (sizeof (alias_t) * alias_list_size);
934
935 prefix_key.sym = KEY_PREFIX;
936 prefix_key.state = MODIFIER_PREFIX;
937
938 /* Add the prefix key to the top-level map. */
939 add_keybinding (prefix_key.sym, prefix_key.state, "readkey " ROOT_KEYMAP, top);
940
941 add_keybinding (prefix_key.sym, prefix_key.state, "other", map);
942 add_keybinding (prefix_key.sym, 0, "meta", map);
943 add_keybinding (XK_g, RP_CONTROL_MASK, "abort", map);
944 add_keybinding (XK_0, 0, "select 0", map);
945 add_keybinding (XK_1, 0, "select 1", map);
946 add_keybinding (XK_2, 0, "select 2", map);
947 add_keybinding (XK_3, 0, "select 3", map);
948 add_keybinding (XK_4, 0, "select 4", map);
949 add_keybinding (XK_5, 0, "select 5", map);
950 add_keybinding (XK_6, 0, "select 6", map);
951 add_keybinding (XK_7, 0, "select 7", map);
952 add_keybinding (XK_8, 0, "select 8", map);
953 add_keybinding (XK_9, 0, "select 9", map);
954 add_keybinding (XK_minus, 0, "select -", map);
955 add_keybinding (XK_A, 0, "title", map);
956 add_keybinding (XK_A, RP_CONTROL_MASK, "title", map);
957 add_keybinding (XK_K, 0, "kill", map);
958 add_keybinding (XK_K, RP_CONTROL_MASK, "kill", map);
959 add_keybinding (XK_Return, 0, "next", map);
960 add_keybinding (XK_Return, RP_CONTROL_MASK, "next", map);
961 add_keybinding (XK_a, 0, "time", map);
962 add_keybinding (XK_a, RP_CONTROL_MASK, "time", map);
963 add_keybinding (XK_b, 0, "banish", map);
964 add_keybinding (XK_b, RP_CONTROL_MASK, "banish", map);
965 add_keybinding (XK_c, 0, "exec " TERM_PROG, map);
966 add_keybinding (XK_c, RP_CONTROL_MASK, "exec " TERM_PROG, map);
967 add_keybinding (XK_colon, 0, "colon", map);
968 add_keybinding (XK_exclam, 0, "exec", map);
969 add_keybinding (XK_exclam, RP_CONTROL_MASK, "colon exec " TERM_PROG " -e ", map);
970 add_keybinding (XK_i, 0, "info", map);
971 add_keybinding (XK_i, RP_CONTROL_MASK, "info", map);
972 add_keybinding (XK_k, 0, "delete", map);
973 add_keybinding (XK_k, RP_CONTROL_MASK, "delete", map);
974 add_keybinding (XK_l, 0, "redisplay", map);
975 add_keybinding (XK_l, RP_CONTROL_MASK, "redisplay", map);
976 add_keybinding (XK_m, 0, "lastmsg", map);
977 add_keybinding (XK_m, RP_CONTROL_MASK, "lastmsg", map);
978 add_keybinding (XK_n, 0, "next", map);
979 add_keybinding (XK_n, RP_CONTROL_MASK, "next", map);
980 add_keybinding (XK_p, 0, "prev", map);
981 add_keybinding (XK_p, RP_CONTROL_MASK, "prev", map);
982 add_keybinding (XK_quoteright, 0, "select", map);
983 add_keybinding (XK_quoteright, RP_CONTROL_MASK, "select", map);
984 add_keybinding (XK_space, 0, "next", map);
985 add_keybinding (XK_space, RP_CONTROL_MASK, "next", map);
986 add_keybinding (XK_v, 0, "version", map);
987 add_keybinding (XK_v, RP_CONTROL_MASK, "version", map);
988 add_keybinding (XK_V, 0, "license", map);
989 add_keybinding (XK_V, RP_CONTROL_MASK, "license", map);
990 add_keybinding (XK_w, 0, "windows", map);
991 add_keybinding (XK_w, RP_CONTROL_MASK, "windows", map);
992 add_keybinding (XK_s, 0, "split", map);
993 add_keybinding (XK_s, RP_CONTROL_MASK, "split", map);
994 add_keybinding (XK_S, 0, "hsplit", map);
995 add_keybinding (XK_S, RP_CONTROL_MASK, "hsplit", map);
996 add_keybinding (XK_Tab, 0, "focus", map);
997 add_keybinding (XK_Tab, RP_META_MASK, "focuslast", map);
998 add_keybinding (XK_Left, RP_CONTROL_MASK, "exchangeleft", map);
999 add_keybinding (XK_Right, RP_CONTROL_MASK, "exchangeright", map);
1000 add_keybinding (XK_Up, RP_CONTROL_MASK, "exchangeup", map);
1001 add_keybinding (XK_Down, RP_CONTROL_MASK, "exchangedown", map);
1002 add_keybinding (XK_Left, 0, "focusleft", map);
1003 add_keybinding (XK_Right, 0, "focusright", map);
1004 add_keybinding (XK_Up, 0, "focusup", map);
1005 add_keybinding (XK_Down, 0, "focusdown", map);
1006 add_keybinding (XK_Q, 0, "only", map);
1007 add_keybinding (XK_R, 0, "remove", map);
1008 add_keybinding (XK_f, 0, "fselect", map);
1009 add_keybinding (XK_f, RP_CONTROL_MASK, "fselect", map);
1010 add_keybinding (XK_F, 0, "curframe", map);
1011 add_keybinding (XK_r, 0, "resize", map);
1012 add_keybinding (XK_r, RP_CONTROL_MASK, "resize", map);
1013 add_keybinding (XK_question, 0, "help " ROOT_KEYMAP, map);
1014 add_keybinding (XK_underscore, RP_CONTROL_MASK, "undo", map);
1015 add_keybinding (XK_u, 0, "undo", map);
1016 add_keybinding (XK_u, RP_CONTROL_MASK, "undo", map);
1017 add_keybinding (XK_U, 0, "redo", map);
1018 add_keybinding (XK_x, 0, "swap", map);
1019 add_keybinding (XK_x, RP_CONTROL_MASK, "swap", map);
1020 add_keybinding (XK_N, 0, "nextscreen", map);
1021 add_keybinding (XK_P, 0, "prevscreen", map);
1022
1023 add_alias ("unbind", "undefinekey " ROOT_KEYMAP);
1024 add_alias ("bind", "definekey " ROOT_KEYMAP);
1025 add_alias ("split", "vsplit");
1026 }
1027
1028 cmdret *
1029 cmdret_new (int success, char *fmt, ...)
1030 {
1031 cmdret *ret;
1032 va_list ap;
1033
1034 ret = xmalloc (sizeof (cmdret));
1035 ret->success = success;
1036
1037 if (fmt)
1038 {
1039 va_start (ap, fmt);
1040 ret->output = xvsprintf (fmt, ap);
1041 va_end (ap);
1042 }
1043 else
1044 ret->output = NULL;
1045
1046 return ret;
1047 }
1048
1049 void
1050 cmdret_free (cmdret *ret)
1051 {
1052 free (ret->output);
1053 free (ret);
1054 }
1055
1056 void
1057 keymap_free (rp_keymap *map)
1058 {
1059 int i;
1060
1061 /* Free the data in the actions. */
1062 for (i=0; i<map->actions_last; i++)
1063 {
1064 free (map->actions[i].data);
1065 }
1066
1067 /* Free the map data. */
1068 free (map->actions);
1069 free (map->name);
1070
1071 /* ...and the map itself. */
1072 free (map);
1073 }
1074
1075 void
1076 free_keymaps (void)
1077 {
1078 rp_keymap *cur;
1079 struct list_head *tmp, *iter;
1080
1081 list_for_each_safe_entry (cur, iter, tmp, &rp_keymaps, node)
1082 {
1083 list_del (&cur->node);
1084 keymap_free (cur);
1085 }
1086 }
1087
1088 void
1089 free_aliases (void)
1090 {
1091 int i;
1092
1093 /* Free the alias data. */
1094 for (i=0; i<alias_list_last; i++)
1095 {
1096 free (alias_list[i].name);
1097 free (alias_list[i].alias);
1098 }
1099
1100 /* Free the alias list. */
1101 free (alias_list);
1102 }
1103
1104 void
1105 free_user_commands (void)
1106 {
1107 struct user_command *cur;
1108 struct set_var *var;
1109 struct list_head *tmp, *iter;
1110
1111 list_for_each_safe_entry (cur, iter, tmp, &user_commands, node)
1112 {
1113 list_del (&cur->node);
1114 user_command_free (cur);
1115 }
1116 list_for_each_safe_entry (var, iter, tmp, &set_vars, node)
1117 {
1118 list_del (&var->node);
1119 set_var_free (var);
1120 }
1121 }
1122
1123 /* return a KeySym from a string that contains either a hex value or
1124 an X keysym description */
1125 static int string_to_keysym (char *str)
1126 {
1127 int retval;
1128 int keysym;
1129
1130 retval = sscanf (str, "0x%x", &keysym);
1131
1132 if (!retval || retval == EOF)
1133 keysym = XStringToKeysym (str);
1134
1135 return keysym;
1136 }
1137
1138 static cmdret *
1139 parse_keydesc (char *keydesc, struct rp_key *key)
1140 {
1141 char *token, *next_token;
1142
1143 if (keydesc == NULL)
1144 return NULL;
1145
1146 key->state = 0;
1147 key->sym = 0;
1148
1149 if (!strchr (keydesc, '-'))
1150 {
1151 /* Its got no hyphens in it, so just grab the keysym */
1152 key->sym = string_to_keysym (keydesc);
1153
1154 if (key->sym == NoSymbol)
1155 return cmdret_new (RET_FAILURE, "parse_keydesc: Unknown key '%s'",
1156 keydesc);
1157 }
1158 else if (keydesc[strlen (keydesc) - 1] == '-')
1159 {
1160 /* A key description can't end in a -. */
1161 return cmdret_new (RET_FAILURE, "parse_keydesc: Can't parse key '%s'",
1162 keydesc);
1163 }
1164 else
1165 {
1166 /* Its got hyphens, so parse out the modifiers and keysym */
1167 char *copy;
1168
1169 copy = xstrdup (keydesc);
1170
1171 token = strtok (copy, "-");
1172 if (token == NULL)
1173 {
1174 /* It was nothing but hyphens */
1175 free (copy);
1176 return cmdret_new (RET_FAILURE,
1177 "parse_keydesc: Can't parse key '%s'", keydesc);
1178 }
1179
1180 do
1181 {
1182 next_token = strtok (NULL, "-");
1183
1184 if (next_token == NULL)
1185 {
1186 /* There is nothing more to parse and token contains the
1187 keysym name. */
1188 key->sym = string_to_keysym (token);
1189
1190 if (key->sym == NoSymbol)
1191 {
1192 cmdret *ret = cmdret_new (RET_FAILURE,
1193 "parse_keydesc: Unknown key '%s'",
1194 token);
1195 free (copy);
1196 return ret;
1197 }
1198 }
1199 else
1200 {
1201 /* Which modifier is it? Only accept modifiers that are
1202 present. ie don't accept a hyper modifier if the keymap
1203 has no hyper key. */
1204 if (!strcmp (token, "C"))
1205 {
1206 key->state |= RP_CONTROL_MASK;
1207 }
1208 else if (!strcmp (token, "M"))
1209 {
1210 key->state |= RP_META_MASK;
1211 }
1212 else if (!strcmp (token, "A"))
1213 {
1214 key->state |= RP_ALT_MASK;
1215 }
1216 else if (!strcmp (token, "S"))
1217 {
1218 key->state |= RP_SHIFT_MASK;
1219 }
1220 else if (!strcmp (token, "s"))
1221 {
1222 key->state |= RP_SUPER_MASK;
1223 }
1224 else if (!strcmp (token, "H"))
1225 {
1226 key->state |= RP_HYPER_MASK;
1227 }
1228 else
1229 {
1230 free (copy);
1231 return cmdret_new (RET_FAILURE,
1232 "parse_keydesc: Unknown modifier '%s'",
1233 token);
1234 }
1235 }
1236
1237 token = next_token;
1238 } while (next_token != NULL);
1239
1240 free (copy);
1241 }
1242
1243 /* Successfully parsed the key. */
1244 return NULL;
1245 }
1246
1247 static void
1248 grab_rat (void)
1249 {
1250 XGrabPointer (dpy, rp_current_screen->root, True, 0,
1251 GrabModeAsync, GrabModeAsync,
1252 None, rp_current_screen->rat, CurrentTime);
1253 }
1254
1255 static void
1256 ungrab_rat (void)
1257 {
1258 XUngrabPointer (dpy, CurrentTime);
1259 }
1260
1261 /* Unmanage window */
1262 cmdret *
1263 cmd_unmanage (int interactive, struct cmdarg **args)
1264 {
1265 if (args[0] == NULL && !interactive)
1266 {
1267 cmdret *ret;
1268 char *s = list_unmanaged_windows();
1269
1270 if (s)
1271 ret = cmdret_new (RET_SUCCESS, "%s", s);
1272 else
1273 ret = cmdret_new (RET_SUCCESS, NULL);
1274
1275 free (s);
1276 return ret;
1277 }
1278
1279 if (args[0])
1280 add_unmanaged_window(ARG_STRING(0));
1281 else
1282 return cmdret_new (RET_FAILURE, "unmanage: at least one argument required");
1283
1284 return cmdret_new (RET_SUCCESS, NULL);
1285 }
1286
1287 /* Clear the unmanaged window list */
1288 cmdret *
1289 cmd_clrunmanaged (int interactive UNUSED, struct cmdarg **args UNUSED)
1290 {
1291 clear_unmanaged_list();
1292 return cmdret_new (RET_SUCCESS, NULL);
1293 }
1294
1295 cmdret *
1296 cmd_undefinekey (int interactive UNUSED, struct cmdarg **args)
1297 {
1298 cmdret *ret = NULL;
1299 rp_keymap *map;
1300 struct rp_key *key;
1301
1302 map = ARG (0, keymap);
1303 key = ARG (1, key);
1304
1305 /* If we're updating the top level map, we'll need to update the
1306 keys grabbed. */
1307 if (map == find_keymap (defaults.top_kmap))
1308 ungrab_keys_all_wins ();
1309
1310 /* If no comand is specified, then unbind the key. */
1311 if (!remove_keybinding (key->sym, key->state, map))
1312 ret = cmdret_new (RET_FAILURE, "undefinekey: key '%s' is not bound", ARG_STRING(1));
1313
1314 /* Update the grabbed keys. */
1315 if (map == find_keymap (defaults.top_kmap))
1316 grab_keys_all_wins ();
1317 XSync (dpy, False);
1318
1319 if (ret)
1320 return ret;
1321 else
1322 return cmdret_new (RET_SUCCESS, NULL);
1323 }
1324
1325 cmdret *
1326 cmd_definekey (int interactive UNUSED, struct cmdarg **args)
1327 {
1328 cmdret *ret = NULL;
1329 rp_keymap *map;
1330 struct rp_key *key;
1331 char *cmd;
1332 rp_action *key_action;
1333
1334 map = ARG(0,keymap);
1335 key = ARG(1,key);
1336 cmd = ARG_STRING(2);
1337
1338 /* If we're updating the top level map, we'll need to update the
1339 keys grabbed. */
1340 if (map == find_keymap (defaults.top_kmap))
1341 ungrab_keys_all_wins ();
1342
1343 if ((key_action = find_keybinding (key->sym, key->state, map)))
1344 replace_keybinding (key_action, cmd);
1345 else
1346 add_keybinding (key->sym, key->state, cmd, map);
1347
1348 /* Update the grabbed keys. */
1349 if (map == find_keymap (defaults.top_kmap))
1350 grab_keys_all_wins ();
1351 XSync (dpy, False);
1352
1353 if (ret)
1354 return ret;
1355 else
1356 return cmdret_new (RET_SUCCESS, NULL);
1357 }
1358
1359 cmdret *
1360 cmd_source (int interactive UNUSED, struct cmdarg **args)
1361 {
1362 FILE *fileptr;
1363
1364 if ((fileptr = fopen (ARG_STRING(0), "r")) == NULL)
1365 return cmdret_new (RET_FAILURE, "source: %s : %s", ARG_STRING(0), strerror(errno));
1366 else
1367 {
1368 set_close_on_exec (fileno (fileptr));
1369 read_rc_file (fileptr);
1370 fclose (fileptr);
1371 }
1372
1373 return cmdret_new (RET_SUCCESS, NULL);
1374 }
1375
1376 cmdret *
1377 cmd_meta (int interactive UNUSED, struct cmdarg **args)
1378 {
1379 XEvent ev;
1380
1381 memset (&ev, 0, sizeof (ev));
1382 /* Redundant with the line above, but points out that passing some
1383 garbage time value trips up some clients */
1384 ev.xkey.time = CurrentTime;
1385
1386 if (current_window() == NULL)
1387 return cmdret_new (RET_FAILURE, NULL);
1388
1389 ev.xkey.type = KeyPress;
1390 ev.xkey.display = dpy;
1391 ev.xkey.window = current_window()->w;
1392
1393 if (args[0])
1394 {
1395 struct rp_key key;
1396 cmdret *ret;
1397
1398 ret = parse_keydesc (ARG_STRING(0), &key);
1399 if (ret != NULL)
1400 return ret;
1401
1402 ev.xkey.state = rp_mask_to_x11_mask (key.state);
1403 ev.xkey.keycode = XKeysymToKeycode (dpy, key.sym);
1404 if (ev.xkey.keycode == NoSymbol)
1405 return cmdret_new (RET_FAILURE,
1406 "meta: Couldn't convert keysym to keycode");
1407 }
1408 else
1409 {
1410 ev.xkey.state = rp_mask_to_x11_mask (prefix_key.state);
1411 ev.xkey.keycode = XKeysymToKeycode (dpy, prefix_key.sym);
1412 }
1413
1414 XSendEvent (dpy, current_window()->w, False, KeyPressMask, &ev);
1415 XSync (dpy, False);
1416
1417 return cmdret_new (RET_SUCCESS, NULL);
1418 }
1419
1420 cmdret *
1421 cmd_prev (int interactive UNUSED, struct cmdarg **args UNUSED)
1422 {
1423 rp_window *cur, *win;
1424 cur = current_window();
1425 win = group_prev_window (rp_current_group, cur);
1426
1427 if (win)
1428 set_active_window (win);
1429 else if (cur)
1430 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_WINDOW);
1431 else
1432 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_MANAGED_WINDOWS);
1433
1434 return cmdret_new (RET_SUCCESS, NULL);
1435 }
1436
1437 cmdret *
1438 cmd_prev_frame (int interactive UNUSED, struct cmdarg **args UNUSED)
1439 {
1440 rp_frame *frame;
1441
1442 frame = find_frame_prev (current_frame());
1443 if (!frame)
1444 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_FRAME);
1445 else
1446 set_active_frame (frame, 0);
1447
1448 return cmdret_new (RET_SUCCESS, NULL);
1449 }
1450
1451 cmdret *
1452 cmd_next (int interactive UNUSED, struct cmdarg **args UNUSED)
1453 {
1454 rp_window *cur, *win;
1455 cur = current_window();
1456 win = group_next_window (rp_current_group, cur);
1457
1458 if (win)
1459 set_active_window (win);
1460 else if (cur)
1461 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_WINDOW);
1462 else
1463 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_MANAGED_WINDOWS);
1464
1465 return cmdret_new (RET_SUCCESS, NULL);
1466 }
1467
1468 cmdret *
1469 cmd_next_frame (int interactive UNUSED, struct cmdarg **args UNUSED)
1470 {
1471 rp_frame *frame;
1472
1473 frame = find_frame_next (current_frame());
1474 if (!frame)
1475 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_FRAME);
1476 else
1477 set_active_frame (frame, 0);
1478
1479 return cmdret_new (RET_SUCCESS, NULL);
1480 }
1481
1482 cmdret *
1483 cmd_other (int interactive UNUSED, struct cmdarg **args UNUSED)
1484 {
1485 rp_window *w;
1486
1487 /* w = find_window_other (); */
1488 w = group_last_window (rp_current_group, rp_current_screen);
1489
1490 if (!w)
1491 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_WINDOW);
1492 else
1493 set_active_window_force (w);
1494
1495 return cmdret_new (RET_SUCCESS, NULL);
1496 }
1497
1498 /* Parse a positive or null number, returns -1 on failure. */
1499 static int
1500 string_to_positive_int (char *str)
1501 {
1502 char *ep;
1503 long lval;
1504
1505 errno = 0;
1506 lval = strtol (str, &ep, 10);
1507 if (str[0] == '\0' || *ep != '\0')
1508 return -1;
1509 if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
1510 (lval > INT_MAX || lval < 0))
1511 return -1;
1512
1513 return (int)lval;
1514 }
1515
1516 static struct list_head *
1517 trivial_completions (char* str UNUSED)
1518 {
1519 struct list_head *list;
1520
1521 /* Initialize our list. */
1522 list = xmalloc (sizeof (struct list_head));
1523 INIT_LIST_HEAD (list);
1524
1525 return list;
1526 }
1527
1528 static struct list_head *
1529 keymap_completions (char* str UNUSED)
1530 {
1531 rp_keymap *cur;
1532 struct list_head *list;
1533
1534 /* Initialize our list. */
1535 list = xmalloc (sizeof (struct list_head));
1536 INIT_LIST_HEAD (list);
1537
1538 list_for_each_entry (cur, &rp_keymaps, node)
1539 {
1540 struct sbuf *name;
1541
1542 name = sbuf_new (0);
1543 sbuf_copy (name, cur->name);
1544 list_add_tail (&name->node, list);
1545 }
1546
1547 return list;
1548 }
1549
1550 static struct list_head *
1551 window_completions (char* str UNUSED)
1552 {
1553 rp_window_elem *cur;
1554 struct list_head *list;
1555
1556 /* Initialize our list. */
1557 list = xmalloc (sizeof (struct list_head));
1558 INIT_LIST_HEAD (list);
1559
1560 /* Gather the names of all the windows. */
1561 list_for_each_entry (cur, &rp_current_group->mapped_windows, node)
1562 {
1563 struct sbuf *name;
1564
1565 name = sbuf_new (0);
1566 sbuf_copy (name, window_name (cur->win));
1567 list_add_tail (&name->node, list);
1568 }
1569
1570 return list;
1571 }
1572
1573 /* switch to window number or name */
1574 cmdret *
1575 cmd_select (int interactive, struct cmdarg **args)
1576 {
1577 cmdret *ret = NULL;
1578 char *str;
1579 int n;
1580
1581 /* FIXME: This is manually done because of the kinds of things
1582 select accepts. */
1583 if (args[0] == NULL)
1584 str = get_more_input (MESSAGE_PROMPT_SWITCH_TO_WINDOW, "", hist_SELECT,
1585 SUBSTRING, window_completions);
1586 else
1587 str = xstrdup (ARG_STRING(0));
1588
1589 /* User aborted. */
1590 if (str == NULL)
1591 return cmdret_new (RET_FAILURE, NULL);
1592
1593 /* Only search if the string contains something to search for. */
1594 if (strlen (str) > 0)
1595 {
1596 if (strlen (str) == 1 && str[0] == '-')
1597 {
1598 blank_frame (current_frame());
1599 ret = cmdret_new (RET_SUCCESS, NULL);
1600 }
1601 /* try by number */
1602 else if ((n = string_to_positive_int (str)) >= 0)
1603 {
1604 rp_window_elem *elem = group_find_window_by_number (rp_current_group, n);
1605
1606 if (elem)
1607 {
1608 goto_window (elem->win);
1609 ret = cmdret_new (RET_SUCCESS, NULL);
1610 }
1611 else
1612 {
1613 if (interactive)
1614 {
1615 /* show the window list as feedback */
1616 show_bar (rp_current_screen, defaults.window_fmt);
1617 ret = cmdret_new (RET_SUCCESS, NULL);
1618 }
1619 else
1620 {
1621 ret = cmdret_new (RET_FAILURE,
1622 "select: unknown window number '%d'", n);
1623 }
1624 }
1625 }
1626 else
1627 /* try by name */
1628 {
1629 rp_window *win = find_window_name (str, 1);
1630
1631 if (!win)
1632 win = find_window_name (str, 0);
1633
1634 if (win)
1635 {
1636 goto_window (win);
1637 ret = cmdret_new (RET_SUCCESS, NULL);
1638 }
1639 else
1640 ret = cmdret_new (RET_FAILURE, "select: unknown window '%s'", str);
1641 }
1642 }
1643 else
1644 /* Silently fail, since the user didn't provide a window spec */
1645 ret = cmdret_new (RET_SUCCESS, NULL);
1646
1647 free (str);
1648
1649 return ret;
1650 }
1651
1652 cmdret *
1653 cmd_rename (int interactive UNUSED, struct cmdarg **args)
1654 {
1655 if (current_window() == NULL)
1656 return cmdret_new (RET_FAILURE, NULL);
1657
1658 free (current_window()->user_name);
1659 current_window()->user_name = xstrdup (ARG_STRING(0));
1660 current_window()->named = 1;
1661 hook_run (&rp_title_changed_hook);
1662
1663 /* Update the program bar. */
1664 update_window_names (rp_current_screen, defaults.window_fmt);
1665
1666 return cmdret_new (RET_SUCCESS, NULL);
1667 }
1668
1669 cmdret *
1670 cmd_delete (int interactive UNUSED, struct cmdarg **args UNUSED)
1671 {
1672 XEvent ev;
1673 int status;
1674
1675 if (current_window() == NULL)
1676 return cmdret_new (RET_FAILURE, NULL);
1677
1678 ev.xclient.type = ClientMessage;
1679 ev.xclient.window = current_window()->w;
1680 ev.xclient.message_type = wm_protocols;
1681 ev.xclient.format = 32;
1682 ev.xclient.data.l[0] = wm_delete;
1683 ev.xclient.data.l[1] = CurrentTime;
1684
1685 status = XSendEvent(dpy, current_window()->w, False, 0, &ev);
1686 if (status == 0)
1687 PRINT_DEBUG (("Delete window failed\n"));
1688
1689 return cmdret_new (RET_SUCCESS, NULL);
1690 }
1691
1692 cmdret *
1693 cmd_kill (int interactive UNUSED, struct cmdarg **args UNUSED)
1694 {
1695 if (current_window() == NULL)
1696 return cmdret_new (RET_FAILURE, NULL);
1697
1698 if (XKillClient(dpy, current_window()->w) == BadValue)
1699 {
1700 return cmdret_new (RET_FAILURE,
1701 "kill failed (got BadValue, this may be a bug)");
1702 }
1703 else
1704 return cmdret_new (RET_SUCCESS, NULL);
1705 }
1706
1707 cmdret *
1708 cmd_version (int interactive UNUSED, struct cmdarg **args UNUSED)
1709 {
1710 return cmdret_new (RET_SUCCESS, "%s", PACKAGE " " VERSION);
1711 }
1712
1713 static char *
1714 frame_selector (unsigned int n)
1715 {
1716 if (n < strlen (defaults.frame_selectors))
1717 {
1718 return xsprintf (" %c ", defaults.frame_selectors[n]);
1719 }
1720 else
1721 {
1722 return xsprintf (" %d ", n);
1723 }
1724 }
1725
1726 /* Return true if ch is nth frame selector. */
1727 static int
1728 frame_selector_match (char ch)
1729 {
1730 size_t i;
1731
1732 /* Is it in the frame selector string? */
1733 for (i=0; i<strlen (defaults.frame_selectors); i++)
1734 {
1735 if (ch == defaults.frame_selectors[i])
1736 return i;
1737 }
1738
1739 /* Maybe it's a number less than 9 and the frame selector doesn't
1740 define that many selectors. */
1741 if (ch >= '0' && ch <= '9'
1742 && (size_t)(ch - '0') >= strlen (defaults.frame_selectors))
1743 {
1744 return ch - '0';
1745 }
1746
1747 return -1;
1748 }
1749
1750 static cmdret *
1751 read_string (struct argspec *spec, struct sbuf *s, int history_id, completion_fn fn, struct cmdarg **arg)
1752 {
1753 char *input;
1754
1755 if (s)
1756 input = xstrdup (sbuf_get(s));
1757 else
1758 input = get_input (spec->prompt, history_id, fn);
1759
1760 if (input)
1761 {
1762 *arg = xmalloc (sizeof(struct cmdarg));
1763 (*arg)->type = spec->type;
1764 (*arg)->string = input;
1765 return NULL;
1766 }
1767
1768 *arg = NULL;
1769 return cmdret_new (RET_SUCCESS, NULL);
1770 }
1771
1772 static cmdret *
1773 read_keymap (struct argspec *spec, struct sbuf *s, struct cmdarg **arg)
1774 {
1775 char *input;
1776 if (s)
1777 input = xstrdup (sbuf_get (s));
1778 else
1779 input = get_input (spec->prompt, hist_KEYMAP, keymap_completions);
1780
1781 if (input)
1782 {
1783 rp_keymap *map;
1784 map = find_keymap (input);
1785 if (map == NULL)
1786 {
1787 cmdret *ret = cmdret_new (RET_FAILURE, "unknown keymap '%s'", input);
1788 free (input);
1789 return ret;
1790 }
1791 *arg = xmalloc (sizeof(struct cmdarg));
1792 (*arg)->type = spec->type;
1793 (*arg)->arg.keymap = map;
1794 (*arg)->string = input;
1795 return NULL;
1796 }
1797
1798 *arg = NULL;
1799 return cmdret_new (RET_SUCCESS, NULL);
1800 }
1801
1802 static cmdret *
1803 read_keydesc (struct argspec *spec, struct sbuf *s, struct cmdarg **arg)
1804 {
1805 char *input;
1806 if (s)
1807 input = xstrdup (sbuf_get (s));
1808 else
1809 input = get_input (spec->prompt, hist_KEY, trivial_completions);
1810
1811 if (input)
1812 {
1813 cmdret *ret;
1814 struct rp_key *key = xmalloc (sizeof(struct rp_key));
1815 ret = parse_keydesc (input, key);
1816 if (ret) {
1817 free (key);
1818 return ret;
1819 }
1820 *arg = xmalloc (sizeof(struct cmdarg));
1821 (*arg)->type = spec->type;
1822 (*arg)->arg.key = key;
1823 (*arg)->string = input;
1824 return NULL;
1825 }
1826
1827 *arg = NULL;
1828 return cmdret_new (RET_SUCCESS, NULL);
1829 }
1830
1831 static struct list_head *
1832 group_completions (char *str UNUSED)
1833 {
1834 struct list_head *list;
1835 rp_group *cur;
1836
1837 /* Initialize our list. */
1838 list = xmalloc (sizeof (struct list_head));
1839 INIT_LIST_HEAD (list);
1840
1841 /* Grab all the group names. */
1842 list_for_each_entry (cur, &rp_groups, node)
1843 {
1844 struct sbuf *s;
1845
1846 s = sbuf_new (0);
1847 /* A group may not have a name, so if it doesn't, use it's
1848 number. */
1849 if (cur->name)
1850 {
1851 sbuf_copy (s, cur->name);
1852 }
1853 else
1854 {
1855 sbuf_printf (s, "%d", cur->number);
1856 }
1857
1858 list_add_tail (&s->node, list);
1859 }
1860
1861 return list;
1862 }
1863
1864 static struct list_head *
1865 colon_completions (char* str UNUSED)
1866 {
1867 int i;
1868 struct user_command *uc;
1869 struct sbuf *s;
1870 struct list_head *list;
1871
1872 /* Initialize our list. */
1873 list = xmalloc (sizeof (struct list_head));
1874 INIT_LIST_HEAD (list);
1875
1876 /* Put all the aliases in our list. */
1877 for(i=0; i<alias_list_last; ++i)
1878 {
1879 s = sbuf_new (0);
1880 sbuf_copy (s, alias_list[i].name);
1881 /* The space is so when the user completes a space is
1882 conveniently inserted after the command. */
1883 sbuf_concat (s, " ");
1884 list_add_tail (&s->node, list);
1885 }
1886
1887 /* Put all the commands in our list. */
1888 list_for_each_entry (uc, &user_commands, node)
1889 {
1890 s = sbuf_new (0);
1891 sbuf_copy (s, uc->name);
1892 /* The space is so when the user completes a space is
1893 conveniently inserted after the command. */
1894 sbuf_concat (s, " ");
1895 list_add_tail (&s->node, list);
1896 }
1897
1898 return list;
1899 }
1900
1901 static cmdret *
1902 read_command (struct argspec *spec, struct sbuf *s, struct cmdarg **arg)
1903 {
1904 return read_string (spec, s, hist_COMMAND, colon_completions, arg);
1905 }
1906
1907 static struct list_head *
1908 exec_completions (char *str)
1909 {
1910 size_t n = 256;
1911 char *partial;
1912 struct sbuf *line;
1913 FILE *file;
1914 struct list_head *head;
1915 char *completion_string;
1916
1917 /* Initialize our list. */
1918 head = xmalloc (sizeof (struct list_head));
1919 INIT_LIST_HEAD (head);
1920
1921 /* FIXME: A Bash dependancy?? */
1922 completion_string = xsprintf("bash -c \"compgen -ac %s|sort\"", str);
1923 file = popen (completion_string, "r");
1924 free (completion_string);
1925 if (!file)
1926 {
1927 PRINT_ERROR (("popen failed\n"));
1928 return head;
1929 }
1930
1931 partial = xmalloc (n);
1932
1933 /* Read data from the file, split it into lines and store it in a
1934 list. */
1935 line = sbuf_new (0);
1936 while (fgets (partial, n, file) != NULL)
1937 {
1938 /* Read a chunk from the file into our line accumulator. */
1939 sbuf_concat (line, partial);
1940
1941 if (feof(file) || (*(sbuf_get (line) + strlen(sbuf_get (line)) - 1) == '\n'))
1942 {
1943 char *s;
1944 struct sbuf *elem;
1945
1946 s = sbuf_get (line);
1947
1948 /* Frob the newline into */
1949 if (*(s + strlen(s) - 1) == '\n')
1950 *(s + strlen(s) - 1) = '\0';
1951
1952 /* Add our line to the list. */
1953 elem = sbuf_new (0);
1954 sbuf_copy (elem, s);
1955 list_add_tail (&elem->node, head);
1956
1957 sbuf_clear (line);
1958 }
1959 }
1960
1961 sbuf_free (line);
1962
1963 free (partial);
1964 pclose (file);
1965
1966 return head;
1967 }
1968
1969 static cmdret *
1970 read_shellcmd (struct argspec *spec, struct sbuf *s, struct cmdarg **arg, const char *command_name)
1971 {
1972 cmdret *ret;
1973
1974 ret = read_string (spec, s, hist_SHELLCMD, exec_completions, arg);
1975 if (command_name && !s && !ret && (*arg)->string) {
1976 /* store for command history */
1977 struct sbuf *buf;
1978
1979 buf = sbuf_new (0);
1980 sbuf_printf (buf, "%s %s", command_name, (*arg)->string);
1981 history_add (hist_COMMAND, sbuf_get (buf));
1982 sbuf_free (buf);
1983 }
1984 return ret;
1985 }
1986
1987 static cmdret *
1988 read_frame (struct sbuf *s, struct cmdarg **arg)
1989 {
1990 rp_frame *frame;
1991 int fnum = -1;
1992 KeySym c;
1993 char keysym_buf[513];
1994 int keysym_bufsize = sizeof (keysym_buf);
1995 unsigned int mod;
1996 Window *wins;
1997 int i;
1998 rp_frame *cur_frame;
1999 rp_screen *cur_screen;
2000 int frames;
2001
2002 if (s == NULL)
2003 {
2004 frames = 0;
2005
2006 list_for_each_entry (cur_screen, &rp_screens, node)
2007 {
2008 frames += num_frames (cur_screen);
2009 }
2010
2011 wins = xmalloc (sizeof (Window) * frames);
2012
2013 /* Loop through each frame and display its number in it's top
2014 left corner. */
2015 i = 0;
2016 list_for_each_entry (cur_screen, &rp_screens, node)
2017 {
2018 XSetWindowAttributes attr;
2019
2020 /* Set up the window attributes to be used in the loop. */
2021 attr.border_pixel = rp_glob_screen.fg_color;
2022 attr.background_pixel = rp_glob_screen.bg_color;
2023 attr.override_redirect = True;
2024
2025 list_for_each_entry (cur_frame, &cur_screen->frames, node)
2026 {
2027 int width, height;
2028 char *num;
2029
2030 /* Create the string to be displayed in the window and
2031 determine the height and width of the window. */
2032 /* num = xsprintf (" %d ", cur_frame->number); */
2033 num = frame_selector (cur_frame->number);
2034 width = defaults.bar_x_padding * 2 + rp_text_width (cur_screen, num, -1);
2035 height = (FONT_HEIGHT (cur_screen) + defaults.bar_y_padding * 2);
2036
2037 /* Create and map the window. */
2038 wins[i] = XCreateWindow (dpy, cur_screen->root, cur_screen->left + cur_frame->x, cur_screen->top + cur_frame->y, width, height, 1,
2039 CopyFromParent, CopyFromParent, CopyFromParent,
2040 CWOverrideRedirect | CWBorderPixel | CWBackPixel,
2041 &attr);
2042 XMapWindow (dpy, wins[i]);
2043 XClearWindow (dpy, wins[i]);
2044
2045 /* Display the frame's number inside the window. */
2046 rp_draw_string (cur_screen, wins[i], STYLE_NORMAL,
2047 defaults.bar_x_padding,
2048 defaults.bar_y_padding + FONT_ASCENT(cur_screen),
2049 num, -1);
2050
2051 free (num);
2052 i++;
2053 }
2054 }
2055 XSync (dpy, False);
2056
2057 /* Read a key. */
2058 read_single_key (&c, &mod, keysym_buf, keysym_bufsize);
2059
2060 /* Destroy our number windows and free the array. */
2061 for (i=0; i<frames; i++)
2062 XDestroyWindow (dpy, wins[i]);
2063
2064 free (wins);
2065
2066 /* FIXME: We only handle one character long keysym names. */
2067 if (strlen (keysym_buf) == 1)
2068 {
2069 fnum = frame_selector_match (keysym_buf[0]);
2070 if (fnum == -1)
2071 return cmdret_new (RET_FAILURE, "unknown frame selector `%s'",
2072 keysym_buf);
2073 }
2074 else
2075 {
2076 return cmdret_new (RET_FAILURE, "frame selector too long `%s'",
2077 keysym_buf);
2078 }
2079 }
2080 else
2081 {
2082 fnum = string_to_positive_int (sbuf_get (s));
2083 if (fnum == -1)
2084 return cmdret_new (RET_FAILURE, "invalid frame selector `%s',"
2085 " negative or too big", sbuf_get (s));
2086 }
2087 /* Now that we have a frame number to go to, let's try to jump to
2088 it. */
2089 frame = find_frame_number (fnum);
2090 if (frame)
2091 {
2092 /* We have to return a string, because commands get lists of
2093 strings. Sucky, yes. The command is simply going to parse it
2094 back into an rp_frame. */
2095 *arg = xmalloc (sizeof(struct cmdarg));
2096 (*arg)->type = arg_FRAME;
2097 (*arg)->string = NULL;
2098 (*arg)->arg.frame = frame;
2099 return NULL;
2100 }
2101 else
2102 return cmdret_new (RET_FAILURE, "frame not found");
2103 }
2104
2105 static cmdret *
2106 read_window (struct argspec *spec, struct sbuf *s, struct cmdarg **arg)
2107 {
2108 rp_window *win = NULL;
2109 char *name;
2110 int n;
2111
2112 if (s)
2113 name = xstrdup (sbuf_get (s));
2114 else
2115 name = get_input (spec->prompt, hist_WINDOW, window_completions);
2116
2117 if (name)
2118 {
2119 /* try by number */
2120 if ((n = string_to_positive_int (name)) >= 0)
2121 {
2122 rp_window_elem *elem = group_find_window_by_number (rp_current_group, n);
2123 if (elem)
2124 win = elem->win;
2125 }
2126 else
2127 /* try by name */
2128 {
2129 win = find_window_name (name, 1);
2130 if (win == NULL)
2131 win = find_window_name (name, 0);
2132 }
2133
2134 if (win)
2135 {
2136 *arg = xmalloc (sizeof(struct cmdarg));
2137 (*arg)->type = arg_WINDOW;
2138 (*arg)->arg.win = win;
2139 (*arg)->string = name;
2140 return NULL;
2141 }
2142 else
2143 {
2144 free (name);
2145 *arg = NULL;
2146 return cmdret_new (RET_SUCCESS, NULL);
2147 }
2148 }
2149
2150 /* user abort. */
2151 *arg = NULL;
2152 return cmdret_new (RET_SUCCESS, NULL);
2153 }
2154
2155 static int
2156 parse_wingravity (char *data)
2157 {
2158 int ret = -1;
2159
2160 if (!strcasecmp (data, "northwest") || !strcasecmp (data, "nw") || !strcmp (data, "7"))
2161 ret = NorthWestGravity;
2162 if (!strcasecmp (data, "north") || !strcasecmp (data, "n") || !strcmp (data, "8"))
2163 ret = NorthGravity;
2164 if (!strcasecmp (data, "northeast") || !strcasecmp (data, "ne") || !strcmp (data, "9"))
2165 ret = NorthEastGravity;
2166 if (!strcasecmp (data, "west") || !strcasecmp (data, "w") || !strcmp (data, "4"))
2167 ret = WestGravity;
2168 if (!strcasecmp (data, "center") || !strcasecmp (data, "c") || !strcmp (data, "5"))
2169 ret = CenterGravity;
2170 if (!strcasecmp (data, "east") || !strcasecmp (data, "e") || !strcmp (data, "6"))
2171 ret = EastGravity;
2172 if (!strcasecmp (data, "southwest") || !strcasecmp (data, "sw") || !strcmp (data, "1"))
2173 ret = SouthWestGravity;
2174 if (!strcasecmp (data, "south") || !strcasecmp (data, "s") || !strcmp (data, "2"))
2175 ret = SouthGravity;
2176 if (!strcasecmp (data, "southeast") || !strcasecmp (data, "se") || !strcmp (data, "3"))
2177 ret = SouthEastGravity;
2178
2179 return ret;
2180 }
2181
2182 static cmdret *
2183 read_gravity (struct argspec *spec, struct sbuf *s, struct cmdarg **arg)
2184 {
2185 char *input;
2186
2187 if (s)
2188 input = xstrdup (sbuf_get(s));
2189 else
2190 input = get_input (spec->prompt, hist_GRAVITY, trivial_completions);
2191
2192 if (input)
2193 {
2194 int g = parse_wingravity (input);
2195 if (g == -1)
2196 {
2197 cmdret *ret = cmdret_new (RET_FAILURE, "bad gravity '%s'", input);
2198 free (input);
2199 return ret;
2200 }
2201 *arg = xmalloc (sizeof(struct cmdarg));
2202 (*arg)->type = arg_GRAVITY;
2203 (*arg)->arg.gravity = g;
2204 (*arg)->string = input;
2205 return NULL;
2206 }
2207
2208 *arg = NULL;
2209 return cmdret_new (RET_SUCCESS, NULL);
2210 }
2211
2212 /* Given a string, find a matching group. First check if the string exactly
2213 matches a group name, then check if it is a number & lastly check if it
2214 partially matches the name of a group. */
2215 static rp_group *
2216 find_group (char *str)
2217 {
2218 rp_group *group;
2219 int n;
2220
2221 /* Check if the user typed a group number. */
2222 n = string_to_positive_int (str);
2223 if (n >= 0)
2224 {
2225 group = groups_find_group_by_number (n);
2226 if (group)
2227 return group;
2228 }
2229
2230 /* Exact matches are special cases. */
2231 if ((group = groups_find_group_by_name (str, 1)))
2232 return group;
2233
2234 group = groups_find_group_by_name (str, 0);
2235 return group;
2236 }
2237
2238 static cmdret *
2239 read_group (struct argspec *spec, struct sbuf *s, struct cmdarg **arg)
2240 {
2241 char *input;
2242
2243 if (s)
2244 input = xstrdup (sbuf_get(s));
2245 else
2246 input = get_input (spec->prompt, hist_GROUP, group_completions);
2247
2248 if (input)
2249 {
2250 rp_group *g = find_group (input);
2251
2252 if (g)
2253 {
2254 *arg = xmalloc (sizeof(struct cmdarg));
2255 (*arg)->type = arg_GROUP;
2256 (*arg)->arg.group = g;
2257 (*arg)->string = input;
2258 return NULL;
2259 }
2260 else
2261 {
2262 cmdret *ret = cmdret_new (RET_FAILURE, "unknown group '%s'", input);
2263 free (input);
2264 return ret;
2265 }
2266 }
2267
2268 *arg = NULL;
2269 return cmdret_new (RET_SUCCESS, NULL);
2270 }
2271
2272 static struct list_head *
2273 hook_completions (char* str UNUSED)
2274 {
2275 struct list_head *list;
2276 struct rp_hook_db_entry *entry;
2277
2278 /* Initialize our list. */
2279 list = xmalloc (sizeof (struct list_head));
2280 INIT_LIST_HEAD (list);
2281
2282 for (entry = rp_hook_db; entry->name; entry++)
2283 {
2284 struct sbuf *hookname;
2285
2286 hookname = sbuf_new(0);
2287 sbuf_copy (hookname, entry->name);
2288 list_add_tail (&hookname->node, list);
2289 }
2290
2291 return list;
2292 }
2293
2294 static cmdret *
2295 read_hook (struct argspec *spec, struct sbuf *s, struct cmdarg **arg)
2296 {
2297 char *input;
2298
2299 if (s)
2300 input = xstrdup (sbuf_get(s));
2301 else
2302 input = get_input (spec->prompt, hist_HOOK, hook_completions);
2303
2304 if (input)
2305 {
2306 struct list_head *hook = hook_lookup (input);
2307
2308 if (hook)
2309 {
2310 *arg = xmalloc (sizeof(struct cmdarg));
2311 (*arg)->type = arg_HOOK;
2312 (*arg)->arg.hook = hook;
2313 (*arg)->string = input;
2314 return NULL;
2315 }
2316 else
2317 {
2318 cmdret *ret = cmdret_new (RET_FAILURE, "unknown hook '%s'", input);
2319 free (input);
2320 return ret;
2321 }
2322 }
2323
2324 *arg = NULL;
2325 return cmdret_new (RET_SUCCESS, NULL);
2326 }
2327
2328 static struct set_var *
2329 find_variable (char *str)
2330 {
2331 struct set_var *cur;
2332 list_for_each_entry (cur, &set_vars, node)
2333 {
2334 if (!strcmp (str, cur->var))
2335 return cur;
2336 }
2337 return NULL;
2338 }
2339
2340 static struct list_head *
2341 var_completions (char *str UNUSED)
2342 {
2343 struct list_head *list;
2344 struct set_var *cur;
2345
2346 /* Initialize our list. */
2347 list = xmalloc (sizeof (struct list_head));
2348 INIT_LIST_HEAD (list);
2349
2350 /* Grab all the group names. */
2351 list_for_each_entry (cur, &set_vars, node)
2352 {
2353 struct sbuf *s;
2354 s = sbuf_new (0);
2355 sbuf_copy (s, cur->var);
2356 list_add_tail (&s->node, list);
2357 }
2358
2359 return list;
2360 }
2361
2362 static cmdret *
2363 read_variable (struct argspec *spec, struct sbuf *s, struct cmdarg **arg)
2364 {
2365 char *input;
2366
2367 if (s)
2368 input = xstrdup (sbuf_get(s));
2369 else
2370 input = get_input (spec->prompt, hist_VARIABLE, var_completions);
2371
2372 if (input)
2373 {
2374 struct set_var *var = find_variable (input);
2375 if (var == NULL)
2376 {
2377 cmdret *ret = cmdret_new (RET_FAILURE, "unknown variable '%s'", input);
2378 free (input);
2379 return ret;
2380 }
2381
2382 *arg = xmalloc (sizeof(struct cmdarg));
2383 (*arg)->type = arg_VARIABLE;
2384 (*arg)->arg.variable = var;
2385 (*arg)->string = input;
2386 return NULL;
2387 }
2388
2389 *arg = NULL;
2390 return cmdret_new (RET_SUCCESS, NULL);
2391 }
2392
2393 static cmdret *
2394 read_number (struct argspec *spec, struct sbuf *s, struct cmdarg **arg)
2395 {
2396 char *input;
2397
2398 if (s)
2399 input = xstrdup (sbuf_get(s));
2400 else
2401 /* numbers should perhaps be more fine grained, or hist_NONE */
2402 input = get_input (spec->prompt, hist_OTHER, trivial_completions);
2403
2404 if (input)
2405 {
2406 char *ep;
2407 long lval;
2408
2409 errno = 0;
2410 lval = strtol (input, &ep, 10);
2411 if (input[0] == '\0' || *ep != '\0')
2412 return cmdret_new (RET_FAILURE, "malformed number `%s'", input);
2413 if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
2414 (lval > INT_MAX || lval < INT_MIN))
2415 return cmdret_new (RET_FAILURE, "out of range number `%s'", input);
2416 *arg = xmalloc (sizeof(struct cmdarg));
2417 (*arg)->type = arg_NUMBER;
2418 (*arg)->arg.number = lval;
2419 (*arg)->string = input;
2420 return NULL;
2421 }
2422
2423 *arg = NULL;
2424 return cmdret_new (RET_SUCCESS, NULL);
2425 }
2426
2427 static cmdret *
2428 read_arg (struct argspec *spec, struct sbuf *s, struct cmdarg **arg, const char *command_name)
2429 {
2430 cmdret *ret = NULL;
2431
2432 switch (spec->type)
2433 {
2434 case arg_STRING:
2435 case arg_REST:
2436 case arg_RAW:
2437 ret = read_string (spec, s, hist_OTHER, trivial_completions, arg);
2438 break;
2439 case arg_KEYMAP:
2440 ret = read_keymap (spec, s, arg);
2441 break;
2442 case arg_KEY:
2443 ret = read_keydesc (spec, s, arg);
2444 break;
2445 case arg_NUMBER:
2446 ret = read_number (spec, s, arg);
2447 break;
2448 case arg_GRAVITY:
2449 ret = read_gravity (spec, s, arg);
2450 break;
2451 case arg_COMMAND:
2452 ret = read_command (spec, s, arg);
2453 break;
2454 case arg_SHELLCMD:
2455 ret = read_shellcmd (spec, s, arg, command_name);
2456 break;
2457 case arg_WINDOW:
2458 ret = read_window (spec, s, arg);
2459 break;
2460 case arg_FRAME:
2461 ret = read_frame (s, arg);
2462 break;
2463 case arg_GROUP:
2464 ret = read_group (spec, s, arg);
2465 break;
2466 case arg_HOOK:
2467 ret = read_hook (spec, s, arg);
2468 break;
2469 case arg_VARIABLE:
2470 ret = read_variable (spec, s, arg);
2471 break;
2472 }
2473
2474 return ret;
2475 }
2476
2477 /* Return -1 on failure. Return the number of args on success. */
2478 static cmdret *
2479 parsed_input_to_args (int num_args, struct argspec *argspec, struct list_head *list,
2480 struct list_head *args, int *parsed_args, const char *command_name)
2481 {
2482 struct sbuf *s;
2483 struct cmdarg *arg;
2484 cmdret *ret;
2485
2486 PRINT_DEBUG (("list len: %d\n", list_size (list)));
2487
2488 *parsed_args = 0;
2489
2490 /* Convert the existing entries to cmdarg's. */
2491 list_for_each_entry (s, list, node)
2492 {
2493 if (*parsed_args >= num_args) break;
2494 ret = read_arg (&argspec[*parsed_args], s, &arg, command_name);
2495 /* If there was an error, then abort. */
2496 if (ret)
2497 return ret;
2498
2499 list_add_tail (&arg->node, args);
2500 (*parsed_args)++;
2501 }
2502
2503 return NULL;
2504 }
2505
2506 /* Prompt the user for missing arguments. Returns non-zero on
2507 failure. 0 on success. */
2508 static cmdret *
2509 fill_in_missing_args (struct user_command *cmd, struct list_head *list, struct list_head *args, const char *command_name)
2510 {
2511 cmdret *ret;
2512 struct cmdarg *arg;
2513 int i = 0;
2514
2515 ret = parsed_input_to_args (cmd->num_args, cmd->args, list, args, &i, command_name);
2516 if (ret)
2517 return ret;
2518
2519 /* Fill in the rest of the required arguments. */
2520 for(; i < cmd->i_required_args; i++)
2521 {
2522 ret = read_arg (&cmd->args[i], NULL, &arg, command_name);
2523 if (ret)
2524 return ret;
2525 list_add_tail (&arg->node, args);
2526 }
2527
2528 return NULL;
2529 }
2530
2531 /* Stick a list of sbuf's in list. if nargs >= 0 then only parse nargs
2532 arguments and and the rest of the string to the list. Return 0 on
2533 success. non-zero on failure. When raw is true, then when we hit
2534 nargs, we should keep any whitespace at the beginning. When false,
2535 gobble the whitespace. */
2536 static cmdret *
2537 parse_args (char *str, struct list_head *list, int nargs, int raw)
2538 {
2539 cmdret *ret = NULL;
2540 char *i;
2541 char *tmp;
2542 int len = 0;
2543 int str_escape = 0;
2544 int in_str = 0;
2545 int gobble = 1;
2546 int parsed_args = 0;
2547
2548 if (str == NULL)
2549 return NULL;
2550
2551 tmp = xmalloc (strlen(str) + 1);
2552
2553 for (i=str; *i; i++)
2554 {
2555 /* Have we hit the arg limit? */
2556 if (raw && parsed_args >= nargs)
2557 {
2558 struct sbuf *s = sbuf_new(0);
2559 if (!raw)
2560 {
2561 while (*i && isspace ((unsigned char)*i))
2562 i++;
2563 }
2564 if (*i)
2565 {
2566 sbuf_concat(s, i);
2567 list_add_tail (&s->node, list);
2568 }
2569 len = 0;
2570 break;
2571 }
2572
2573 /* Should we eat the whitespace? */
2574 if (gobble)
2575 {
2576 while (*i && isspace ((unsigned char)*i))
2577 i++;
2578 gobble = 0;
2579 }
2580
2581 /* Escaped characters always get added. */
2582 if (str_escape)
2583 {
2584 tmp[len] = *i;
2585 len++;
2586 str_escape = 0;
2587 }
2588 else if (*i == '\\')
2589 {
2590 str_escape = 1;
2591 }
2592 else if (*i == '"')
2593 {
2594 if (in_str)
2595 {
2596 /* End the arg. */
2597 struct sbuf *s = sbuf_new(0);
2598 sbuf_nconcat(s, tmp, len);
2599 list_add_tail (&s->node, list);
2600 len = 0;
2601 gobble = 1;
2602 in_str = 0;
2603 parsed_args++;
2604 }
2605 else if (len == 0)
2606 {
2607 /* A string open can only start at the beginning of an
2608 argument. */
2609 in_str = 1;
2610 }
2611 else
2612 {
2613 ret = cmdret_new (RET_FAILURE, "parse error in '%s'", str);
2614 break;
2615 }
2616 }
2617 else if (isspace ((unsigned char)*i) && !in_str)
2618 {
2619 /* End the current arg, and start a new one. */
2620 struct sbuf *s = sbuf_new(0);
2621 sbuf_nconcat(s, tmp, len);
2622 list_add_tail (&s->node, list);
2623 len = 0;
2624 gobble = 1;
2625 parsed_args++;
2626 }
2627 else
2628 {
2629 /* Add the character to the argument. */
2630 tmp[len] = *i;
2631 len++;
2632 }
2633 }
2634 /* Add the remaining text in tmp. */
2635 if (ret == NULL && len)
2636 {
2637 struct sbuf *s = sbuf_new(0);
2638 sbuf_nconcat(s, tmp, len);
2639 list_add_tail (&s->node, list);
2640 }
2641
2642 free (tmp);
2643 return ret;
2644 }
2645
2646 /* Convert the list to an array, for easier access in commands. */
2647 static struct cmdarg **
2648 arg_array (struct list_head *head)
2649 {
2650 int i = 0;
2651 struct cmdarg **args, *cur;
2652
2653 args = xmalloc (sizeof (struct cmdarg *) * (list_size (head) + 1));
2654 list_for_each_entry (cur, head, node)
2655 {
2656 args[i] = cur;
2657 i++;
2658 }
2659
2660 /* NULL terminate the array. */
2661 args[list_size (head)] = NULL;
2662 return args;
2663 }
2664
2665 static void
2666 arg_free (struct cmdarg *arg)
2667 {
2668 if (arg)
2669 {
2670 /* read_frame doesn't fill in string. */
2671 free (arg->string);
2672 switch (arg->type)
2673 {
2674 case arg_KEY:
2675 free (arg->arg.key);
2676 break;
2677 case arg_REST:
2678 case arg_STRING:
2679 case arg_NUMBER:
2680 case arg_WINDOW:
2681 case arg_FRAME:
2682 case arg_COMMAND:
2683 case arg_SHELLCMD:
2684 case arg_KEYMAP:
2685 case arg_GRAVITY:
2686 case arg_GROUP:
2687 case arg_HOOK:
2688 case arg_VARIABLE:
2689 case arg_RAW:
2690 /* Do nothing */
2691 break;
2692 default:
2693 PRINT_ERROR (("Missed an arg type.\n"));
2694 break;
2695 }
2696 free (arg);
2697 }
2698 }
2699
2700 cmdret *
2701 command (int interactive, char *data)
2702 {
2703 /* This static counter is used to exit from recursive alias calls. */
2704 static int alias_recursive_depth = 0;
2705 cmdret *result = NULL;
2706 char *cmd, *rest;
2707 char *input;
2708 struct user_command *uc;
2709 int i;
2710
2711 if (data == NULL)
2712 return cmdret_new (RET_FAILURE, NULL);
2713
2714 /* get a writable copy for strtok() */
2715 input = xstrdup (data);
2716
2717 cmd = input;
2718 /* skip beginning whitespace. */
2719 while (*cmd && isspace ((unsigned char)*cmd))
2720 cmd++;
2721 rest = cmd;
2722 /* skip til we get to whitespace */
2723 while (*rest && !isspace ((unsigned char)*rest))
2724 rest++;
2725 /* mark that spot as the end of the command and make rest point to
2726 the rest of the string. */
2727 if (*rest)
2728 {
2729 *rest = 0;
2730 rest++;
2731 }
2732
2733 PRINT_DEBUG (("cmd==%s rest==%s\n", cmd, rest?rest:"NULL"));
2734
2735 /* Look for it in the aliases, first. */
2736 for (i=0; i<alias_list_last; i++)
2737 {
2738 if (!strcmp (cmd, alias_list[i].name))
2739 {
2740 struct sbuf *s;
2741
2742 /* Append any arguments onto the end of the alias' command. */
2743 s = sbuf_new (0);
2744 sbuf_concat (s, alias_list[i].alias);
2745 if (rest != NULL && *rest)
2746 sbuf_printf_concat (s, " %s", rest);
2747
2748 alias_recursive_depth++;
2749 if (alias_recursive_depth >= MAX_ALIAS_RECURSIVE_DEPTH)
2750 result = cmdret_new (RET_FAILURE, "command: alias recursion has exceeded maximum depth");
2751 else
2752 result = command (interactive, sbuf_get (s));
2753 alias_recursive_depth--;
2754
2755 sbuf_free (s);
2756 goto done;
2757 }
2758 }
2759
2760 /* If it wasn't an alias, maybe its a command. */
2761 list_for_each_entry (uc, &user_commands, node)
2762 {
2763 if (!strcmp (cmd, uc->name))
2764 {
2765 struct sbuf *scur;
2766 struct cmdarg *acur;
2767 struct list_head *iter, *tmp;
2768 struct list_head head, args;
2769 int nargs = 0, raw = 0;
2770
2771 INIT_LIST_HEAD (&args);
2772 INIT_LIST_HEAD (&head);
2773
2774 /* We need to tell parse_args about arg_REST and arg_SHELLCMD. */
2775 for (i=0; i<uc->num_args; i++)
2776 if (uc->args[i].type == arg_REST
2777 || uc->args[i].type == arg_COMMAND
2778 || uc->args[i].type == arg_SHELLCMD
2779 || uc->args[i].type == arg_RAW)
2780 {
2781 raw = 1;
2782 nargs = i;
2783 break;
2784 }
2785
2786 /* Parse the arguments and call the function. */
2787 result = parse_args (rest, &head, nargs, raw);
2788 if (result)
2789 goto free_lists;
2790
2791 /* Interactive commands prompt the user for missing args. */
2792 if (interactive)
2793 result = fill_in_missing_args (uc, &head, &args, uc->name);
2794 else
2795 {
2796 int parsed_args;
2797 result = parsed_input_to_args (uc->num_args, uc->args, &head, &args, &parsed_args, uc->name);
2798 }
2799
2800 if (result == NULL)
2801 {
2802 if ((interactive && list_size (&args) < uc->i_required_args)
2803 || (!interactive && list_size (&args) < uc->ni_required_args))
2804 {
2805 result = cmdret_new (RET_FAILURE, "not enough arguments.");
2806 goto free_lists;
2807 }
2808 else if (list_size (&head) > uc->num_args)
2809 {
2810 result = cmdret_new (RET_FAILURE, "too many arguments.");
2811 goto free_lists;
2812 }
2813 else
2814 {
2815 struct cmdarg **cmdargs = arg_array (&args);
2816 result = uc->func (interactive, cmdargs);
2817 free (cmdargs);
2818 }
2819 }
2820
2821 free_lists:
2822 /* Free the parsed strings */
2823 list_for_each_safe_entry (scur, iter, tmp, &head, node)
2824 sbuf_free(scur);
2825 /* Free the args */
2826 list_for_each_safe_entry (acur, iter, tmp, &args, node)
2827 arg_free (acur);
2828
2829 goto done;
2830 }
2831 }
2832
2833 PRINT_WARNING (("command \"%s\" unknown, ignored\n", cmd));
2834 result = cmdret_new (RET_FAILURE, MESSAGE_UNKNOWN_COMMAND, cmd);
2835
2836 done:
2837 free (input);
2838 return result;
2839 }
2840
2841 cmdret *
2842 cmd_colon (int interactive UNUSED, struct cmdarg **args)
2843 {
2844 cmdret *result;
2845 char *input;
2846
2847 if (args[0] == NULL)
2848 input = get_input (MESSAGE_PROMPT_COMMAND, hist_COMMAND, colon_completions);
2849 else
2850 input = get_more_input (MESSAGE_PROMPT_COMMAND, ARG_STRING(0), hist_COMMAND,
2851 BASIC, colon_completions);
2852
2853 /* User aborted. */
2854 if (input == NULL)
2855 return cmdret_new (RET_FAILURE, NULL);
2856
2857 result = command (1, input);
2858 free (input);
2859 return result;
2860 }
2861
2862 cmdret *
2863 cmd_exec (int interactive UNUSED, struct cmdarg **args)
2864 {
2865 spawn (ARG_STRING(0), 0, current_frame());
2866 return cmdret_new (RET_SUCCESS, NULL);
2867 }
2868
2869 cmdret *
2870 cmd_execa (int interactive UNUSED, struct cmdarg **args)
2871 {
2872 spawn (ARG_STRING(0), 0, NULL);
2873 return cmdret_new (RET_SUCCESS, NULL);
2874 }
2875
2876 cmdret *
2877 cmd_execf (int interactive UNUSED, struct cmdarg **args)
2878 {
2879 spawn (ARG_STRING(1), 0, ARG(0,frame));
2880 return cmdret_new (RET_SUCCESS, NULL);
2881 }
2882
2883 int
2884 spawn(char *cmd, int raw, rp_frame *frame)
2885 {
2886 rp_child_info *child;
2887 int pid;
2888
2889 pid = fork();
2890 if (pid == 0)
2891 {
2892 /* Some process setup to make sure the spawned process runs
2893 in its own session. */
2894 putenv(rp_current_screen->display_string);
2895 #ifdef HAVE_SETSID
2896 if (setsid() == -1)
2897 #endif
2898 {
2899 #if defined (HAVE_SYS_IOCTL_H) && defined (TIOCNOTTY)
2900 int ctty;
2901
2902 ctty = open ("/dev/tty", O_RDONLY);
2903 if (ctty != -1)
2904 {
2905 ioctl (ctty, TIOCNOTTY);
2906 close (ctty);
2907 }
2908 #endif
2909
2910 #if defined (HAVE_SETPGID)
2911 setpgid (0, 0);
2912 #elif defined (HAVE_SETPGRP)
2913 /* Assume BSD-style setpgrp */
2914 setpgrp (0, 0);
2915 #endif
2916 }
2917
2918 /* raw means don't run it through sh. */
2919 if (raw)
2920 execl (cmd, cmd, (char *)NULL);
2921 execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
2922 _exit(EXIT_FAILURE);
2923 }
2924
2925 /* wait((int *) 0); */
2926 PRINT_DEBUG (("spawned %s\n", cmd));
2927
2928 /* Add this child process to our list. */
2929 child = xmalloc (sizeof (rp_child_info));
2930 child->cmd = xstrdup (cmd);
2931 child->pid = pid;
2932 child->terminated = 0;
2933 child->frame = frame;
2934 child->group = rp_current_group;
2935 child->screen = rp_current_screen;
2936 child->window_mapped = 0;
2937
2938 list_add (&child->node, &rp_children);
2939
2940 return pid;
2941 }
2942
2943 /* Switch to a different Window Manager. Thanks to
2944 "Chr. v. Stuckrad" <[email protected]> for the patch. */
2945 cmdret *
2946 cmd_newwm(int interactive UNUSED, struct cmdarg **args)
2947 {
2948 /* in the event loop, this will switch WMs. */
2949 rp_exec_newwm = xstrdup (ARG_STRING(0));
2950
2951 return cmdret_new (RET_SUCCESS, NULL);
2952 }
2953
2954 cmdret *
2955 cmd_quit(int interactive UNUSED, struct cmdarg **args UNUSED)
2956 {
2957 kill_signalled = 1;
2958 return cmdret_new (RET_SUCCESS, NULL);
2959 }
2960
2961 /* Show the current time on the bar. Thanks to Martin Samuelsson
2962 <[email protected]> for the patch. Thanks to Jonathan Walther
2963 <[email protected]> for making it pretty. */
2964 cmdret *
2965 cmd_time (int interactive UNUSED, struct cmdarg **args UNUSED)
2966 {
2967 char *msg, *tmp;
2968 time_t timep;
2969 cmdret *ret;
2970
2971 timep = time (NULL);
2972 tmp = ctime (&timep);
2973 msg = xstrdup (tmp);
2974 msg[strcspn (msg, "\n")] = '\0' ; /* Remove the newline */
2975
2976 ret = cmdret_new (RET_SUCCESS, "%s", msg);
2977 free (msg);
2978
2979 return ret;
2980 }
2981
2982 /* Assign a new number to a window ala screen's number command. */
2983 cmdret *
2984 cmd_number (int interactive UNUSED, struct cmdarg **args)
2985 {
2986 int old_number, new_number;
2987 rp_window_elem *other_win, *win;
2988
2989 /* Gather the args. */
2990 new_number = ARG(0,number);
2991 if (args[1])
2992 win = group_find_window_by_number (rp_current_group, ARG(1,number));
2993 else
2994 win = group_find_window (&rp_current_group->mapped_windows, current_window());
2995
2996 /* Make the switch. */
2997 if ( new_number >= 0 && win)
2998 {
2999 /* Find other window with same number and give it old number. */
3000 other_win = group_find_window_by_number (rp_current_group, new_number);
3001 if (other_win != NULL)
3002 {
3003 old_number = win->number;
3004 other_win->number = old_number;
3005
3006 /* Resort the window in the list */
3007 group_resort_window (rp_current_group, other_win);
3008 }
3009 else
3010 {
3011 numset_release (rp_current_group->numset, win->number);
3012 }
3013
3014 win->number = new_number;
3015 numset_add_num (rp_current_group->numset, new_number);
3016
3017 /* resort the the window in the list */
3018 group_resort_window (rp_current_group, win);
3019
3020 /* Update the window list. */
3021 update_window_names (win->win->scr, defaults.window_fmt);
3022 }
3023
3024 return cmdret_new (RET_SUCCESS, NULL);
3025 }
3026
3027 /* Toggle the display of the program bar */
3028 cmdret *
3029 cmd_windows (int interactive, struct cmdarg **args)
3030 {
3031 struct sbuf *window_list = NULL;
3032 int dummy;
3033 rp_screen *s;
3034 char *fmt;
3035
3036 if (args[0] == NULL)
3037 fmt = defaults.window_fmt;
3038 else
3039 fmt = ARG_STRING(0);
3040
3041 if (interactive)
3042 {
3043 s = rp_current_screen;
3044 /* This is a yukky hack. If the bar already hidden then show the
3045 bar. This handles the case when msgwait is 0 (the bar sticks)
3046 and the user uses this command to toggle the bar on and
3047 off. OR the timeout is >0 then show the bar. Which means,
3048 always show the bar if msgwait is >0 which fixes the case
3049 when a command in the prefix hook displays the bar. */
3050 if (!hide_bar (s) || defaults.bar_timeout > 0) show_bar (s, fmt);
3051
3052 return cmdret_new (RET_SUCCESS, NULL);
3053 }
3054 else
3055 {
3056 cmdret *ret;
3057
3058 window_list = sbuf_new (0);
3059 get_window_list (fmt, "\n", window_list, &dummy, &dummy);
3060 ret = cmdret_new (RET_SUCCESS, "%s", sbuf_get (window_list));
3061 sbuf_free (window_list);
3062 return ret;
3063 }
3064 }
3065
3066 cmdret *
3067 cmd_abort (int interactive UNUSED, struct cmdarg **args UNUSED)
3068 {
3069 return cmdret_new (RET_SUCCESS, NULL);
3070 }
3071
3072 /* Redisplay the current window by sending 2 resize events. */
3073 cmdret *
3074 cmd_redisplay (int interactive UNUSED, struct cmdarg **args UNUSED)
3075 {
3076 force_maximize (current_window());
3077 return cmdret_new (RET_SUCCESS, NULL);
3078 }
3079
3080 /* Reassign the prefix key. */
3081 cmdret *
3082 cmd_escape (int interactive UNUSED, struct cmdarg **args)
3083 {
3084 struct rp_key *key;
3085 rp_action *action;
3086 rp_keymap *map, *top;
3087
3088 top = find_keymap (defaults.top_kmap);
3089 map = find_keymap (ROOT_KEYMAP);
3090 key = ARG(0,key);
3091
3092 /* Update the "other" keybinding */
3093 action = find_keybinding(prefix_key.sym, prefix_key.state, map);
3094 if (action != NULL && !strcmp (action->data, "other"))
3095 {
3096 action->key = key->sym;
3097 action->state = key->state;
3098 }
3099
3100 /* Update the "meta" keybinding */
3101 action = find_keybinding(prefix_key.sym, 0, map);
3102 if (action != NULL && !strcmp (action->data, "meta"))
3103 {
3104 action->key = key->sym;
3105 if (key->state != 0)
3106 action->state = 0;
3107 else
3108 action->state = RP_CONTROL_MASK;
3109 }
3110
3111 /* Remove the grab on the current prefix key */
3112 ungrab_keys_all_wins();
3113
3114 action = find_keybinding(prefix_key.sym, prefix_key.state, top);
3115 if (action != NULL && !strcmp (action->data, "readkey " ROOT_KEYMAP))
3116 {
3117 action->key = key->sym;
3118 action->state = key->state;
3119 }
3120
3121 /* Add the grab for the new prefix key */
3122 grab_keys_all_wins();
3123
3124 /* Finally, keep track of the current prefix. */
3125 prefix_key.sym = key->sym;
3126 prefix_key.state = key->state;
3127
3128 return cmdret_new (RET_SUCCESS, NULL);
3129 }
3130
3131 /* User accessible call to display the passed in string. */
3132 cmdret *
3133 cmd_echo (int interactive UNUSED, struct cmdarg **args)
3134 {
3135 marked_message_printf (0, 0, "%s", ARG_STRING(0));
3136
3137 return cmdret_new (RET_SUCCESS, NULL);
3138 }
3139
3140
3141 static cmdret *
3142 read_split (char *str, int max, int *p)
3143 {
3144 int a, b;
3145
3146 if (sscanf(str, "%d/%d", &a, &b) == 2)
3147 {
3148 *p = (int)(max * (float)(a) / (float)(b));
3149 }
3150 else if (sscanf(str, "%d", p) == 1)
3151 {
3152 if (*p < 0)
3153 *p = max + *p;
3154 }
3155 else
3156 {
3157 /* Failed to read input. */
3158 return cmdret_new (RET_FAILURE, "bad split '%s'", str);
3159 }
3160
3161 return NULL;
3162 }
3163
3164 cmdret *
3165 cmd_v_split (int interactive UNUSED, struct cmdarg **args)
3166 {
3167 cmdret *ret;
3168 rp_frame *frame;
3169 int pixels;
3170
3171 push_frame_undo (rp_current_screen); /* fdump to stack */
3172 frame = current_frame();
3173
3174 /* Default to dividing the frame in half. */
3175 if (args[0] == NULL)
3176 pixels = frame->height / 2;
3177 else
3178 {
3179 ret = read_split (ARG_STRING(0), frame->height, &pixels);
3180 if (ret)
3181 return ret;
3182 }
3183
3184 if (pixels > 0)
3185 h_split_frame (frame, pixels);
3186 else
3187 return cmdret_new (RET_FAILURE, "vsplit: %s", invalid_negative_arg);
3188
3189 return cmdret_new (RET_SUCCESS, NULL);
3190 }
3191
3192 cmdret *
3193 cmd_h_split (int interactive UNUSED, struct cmdarg **args)
3194 {
3195 cmdret *ret;
3196 rp_frame *frame;
3197 int pixels;
3198
3199 push_frame_undo (rp_current_screen); /* fdump to stack */
3200 frame = current_frame();
3201
3202 /* Default to dividing the frame in half. */
3203 if (args[0] == NULL)
3204 pixels = frame->width / 2;
3205 else
3206 {
3207 ret = read_split (ARG_STRING(0), frame->width, &pixels);
3208 if (ret)
3209 return ret;
3210 }
3211
3212 if (pixels > 0)
3213 v_split_frame (frame, pixels);
3214 else
3215 return cmdret_new (RET_FAILURE, "hsplit: %s", invalid_negative_arg);
3216
3217 return cmdret_new (RET_SUCCESS, NULL);
3218 }
3219
3220 cmdret *
3221 cmd_only (int interactive UNUSED, struct cmdarg **args UNUSED)
3222 {
3223 push_frame_undo (rp_current_screen); /* fdump to stack */
3224 remove_all_splits();
3225 maximize (current_window());
3226
3227 return cmdret_new (RET_SUCCESS, NULL);
3228 }
3229
3230 cmdret *
3231 cmd_remove (int interactive UNUSED, struct cmdarg **args UNUSED)
3232 {
3233 rp_screen *s = rp_current_screen;
3234 rp_frame *frame;
3235
3236 push_frame_undo (rp_current_screen); /* fdump to stack */
3237
3238 if (num_frames(s) <= 1)
3239 {
3240 return cmdret_new (RET_FAILURE, "remove: cannot remove only frame");
3241 }
3242
3243 frame = find_frame_next (current_frame());
3244
3245 if (frame)
3246 {
3247 remove_frame (current_frame());
3248 set_active_frame (frame, 0);
3249 show_frame_indicator(0);
3250 }
3251
3252 return cmdret_new (RET_SUCCESS, NULL);
3253 }
3254
3255 cmdret *
3256 cmd_shrink (int interactive UNUSED, struct cmdarg **args UNUSED)
3257 {
3258 push_frame_undo (rp_current_screen); /* fdump to stack */
3259 resize_shrink_to_window (current_frame());
3260 return cmdret_new (RET_SUCCESS, NULL);
3261 }
3262
3263 typedef struct resize_binding resize_binding;
3264
3265 struct resize_binding
3266 {
3267 struct rp_key key;
3268 enum resize_action {RESIZE_UNKNOWN=0, RESIZE_VGROW, RESIZE_VSHRINK,
3269 RESIZE_HGROW, RESIZE_HSHRINK, RESIZE_TO_WINDOW,
3270 RESIZE_ABORT, RESIZE_END } action;
3271 };
3272
3273 static resize_binding resize_bindings[] =
3274 { {{INPUT_ABORT_KEY, INPUT_ABORT_MODIFIER}, RESIZE_ABORT},
3275 {{RESIZE_VGROW_KEY, RESIZE_VGROW_MODIFIER}, RESIZE_VGROW},
3276 {{RESIZE_VSHRINK_KEY, RESIZE_VSHRINK_MODIFIER}, RESIZE_VSHRINK},
3277 {{RESIZE_HGROW_KEY, RESIZE_HGROW_MODIFIER}, RESIZE_HGROW},
3278 {{RESIZE_HSHRINK_KEY, RESIZE_HSHRINK_MODIFIER}, RESIZE_HSHRINK},
3279 {{RESIZE_SHRINK_TO_WINDOW_KEY,RESIZE_SHRINK_TO_WINDOW_MODIFIER},RESIZE_TO_WINDOW},
3280 {{RESIZE_END_KEY, RESIZE_END_MODIFIER}, RESIZE_END},
3281 /* Some more default keys
3282 * (after the values from conf.h, so that they have lower priority):
3283 * first the arrow keys: */
3284 {{XK_Escape, 0}, RESIZE_ABORT},
3285 {{XK_Down, 0}, RESIZE_VGROW},
3286 {{XK_Up, 0}, RESIZE_VSHRINK},
3287 {{XK_Right, 0}, RESIZE_HGROW},
3288 {{XK_Left, 0}, RESIZE_HSHRINK},
3289 /* some vi-like bindings: */
3290 {{XK_j, 0}, RESIZE_VGROW},
3291 {{XK_k, 0}, RESIZE_VSHRINK},
3292 {{XK_l, 0}, RESIZE_HGROW},
3293 {{XK_h, 0}, RESIZE_HSHRINK},
3294 {{0, 0}, RESIZE_UNKNOWN} };
3295
3296
3297 cmdret *
3298 cmd_resize (int interactive, struct cmdarg **args)
3299 {
3300 rp_screen *s = rp_current_screen;
3301
3302 /* If the user calls resize with arguments, treat it like the
3303 non-interactive version. */
3304 if (interactive && args[0] == NULL)
3305 {
3306 char buffer[513];
3307 unsigned int mod;
3308 KeySym c;
3309 struct list_head *bk;
3310
3311 /* If we haven't got at least 2 frames, there isn't anything to
3312 scale. */
3313 if (num_frames (s) < 2)
3314 return cmdret_new (RET_FAILURE, NULL);
3315
3316 /* Save the frameset in case the user aborts. */
3317 bk = screen_copy_frameset (s);
3318
3319 /* Get ready to read keys. */
3320 grab_rat();
3321 XGrabKeyboard (dpy, s->key_window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
3322
3323 while (1)
3324 {
3325 struct resize_binding *binding;
3326
3327 show_frame_message ("Resize frame");
3328 read_key (&c, &mod, buffer, sizeof (buffer));
3329
3330 /* Convert the mask to be compatible with ratpoison. */
3331 mod = x11_mask_to_rp_mask (mod);
3332
3333 for (binding = resize_bindings; binding->action; binding++)
3334 {
3335 if (c == binding->key.sym && mod == binding->key.state)
3336 break;
3337 }
3338
3339 if (binding->action == RESIZE_VGROW)
3340 resize_frame_vertically (current_frame(), defaults.frame_resize_unit);
3341 else if (binding->action == RESIZE_VSHRINK)
3342 resize_frame_vertically (current_frame(), -defaults.frame_resize_unit);
3343 else if (binding->action == RESIZE_HGROW)
3344 resize_frame_horizontally (current_frame(), defaults.frame_resize_unit);
3345 else if (binding->action == RESIZE_HSHRINK)
3346 resize_frame_horizontally (current_frame(), -defaults.frame_resize_unit);
3347 else if (binding->action == RESIZE_TO_WINDOW)
3348 resize_shrink_to_window (current_frame());
3349 else if (binding->action == RESIZE_ABORT)
3350 {
3351 rp_frame *cur;
3352
3353 screen_restore_frameset (s, bk);
3354 list_for_each_entry (cur, &s->frames, node)
3355 {
3356 maximize_all_windows_in_frame (cur);
3357 }
3358 break;
3359 }
3360 else if (binding->action == RESIZE_END)
3361 {
3362 frameset_free (bk);
3363 break;
3364 }
3365 }
3366
3367 /* It is our responsibility to free this. */
3368 free (bk);
3369
3370 hide_frame_indicator ();
3371 ungrab_rat();
3372 XUngrabKeyboard (dpy, CurrentTime);
3373 }
3374 else
3375 {
3376 if (args[0] && args[1])
3377 {
3378 resize_frame_horizontally (current_frame(), ARG(0,number));
3379 resize_frame_vertically (current_frame(), ARG(1,number));
3380 }
3381 else
3382 return cmdret_new (RET_FAILURE, "resize: two numeric arguments required");
3383 }
3384
3385 return cmdret_new (RET_SUCCESS, NULL);
3386 }
3387
3388 static cmdret *
3389 set_resizeunit (struct cmdarg **args)
3390 {
3391 if (args[0] == NULL)
3392 return cmdret_new (RET_SUCCESS, "%d", defaults.frame_resize_unit);
3393
3394 if (ARG(0,number) >= 0)
3395 defaults.frame_resize_unit = ARG(0,number);
3396 else
3397 return cmdret_new (RET_FAILURE, "set resizeunit: %s", invalid_negative_arg);
3398
3399 return cmdret_new (RET_SUCCESS, NULL);
3400 }
3401
3402 /* banish the rat pointer */
3403 cmdret *
3404 cmd_banish (int interactive UNUSED, struct cmdarg **args UNUSED)
3405 {
3406 rp_screen *s;
3407
3408 s = rp_current_screen;
3409
3410 XWarpPointer (dpy, None, s->root, 0, 0, 0, 0, s->left + s->width - 2, s->top + s->height - 2);
3411 return cmdret_new (RET_SUCCESS, NULL);
3412 }
3413
3414 cmdret *
3415 cmd_banishrel (int interactive UNUSED, struct cmdarg **args UNUSED)
3416 {
3417 rp_screen *s = rp_current_screen;
3418 rp_window *w = current_window();
3419 rp_frame *f = current_frame();
3420
3421 if (w)
3422 XWarpPointer (dpy, None, w->w, 0, 0, 0, 0, w->x + w->width - 2, w->y + w->height - 2);
3423 else
3424 XWarpPointer (dpy, None, s->root, 0, 0, 0, 0, f->x + f->width, f->y + f->height);
3425
3426 return cmdret_new (RET_SUCCESS, NULL);
3427 }
3428
3429 cmdret *
3430 cmd_ratinfo (int interactive UNUSED, struct cmdarg **args UNUSED)
3431 {
3432 rp_screen *s;
3433 Window root_win, child_win;
3434 int mouse_x, mouse_y, root_x, root_y;
3435 unsigned int mask;
3436
3437 s = rp_current_screen;
3438 XQueryPointer (dpy, s->root, &root_win, &child_win, &mouse_x, &mouse_y, &root_x, &root_y, &mask);
3439
3440 return cmdret_new (RET_SUCCESS, "%d %d", mouse_x, mouse_y);
3441 }
3442
3443 cmdret *
3444 cmd_ratrelinfo (int interactive UNUSED, struct cmdarg **args UNUSED)
3445 {
3446 rp_screen *s;
3447 rp_window *rpw;
3448 rp_frame *f;
3449 Window root_win, child_win;
3450 int mouse_x, mouse_y, root_x, root_y;
3451 unsigned int mask;
3452
3453 s = rp_current_screen;
3454 rpw = current_window();
3455 f = current_frame();
3456
3457 if (rpw)
3458 XQueryPointer (dpy, rpw->w, &root_win, &child_win, &mouse_x, &mouse_y, &root_x, &root_y, &mask);
3459 else
3460 {
3461 XQueryPointer (dpy, s->root, &root_win, &child_win, &mouse_x, &mouse_y, &root_x, &root_y, &mask);
3462 root_x -= f->x;
3463 root_y -= f->y;
3464 }
3465
3466 return cmdret_new (RET_SUCCESS, "%d %d", root_x, root_y);
3467 }
3468
3469 cmdret *
3470 cmd_ratwarp (int interactive UNUSED, struct cmdarg **args)
3471 {
3472 rp_screen *s;
3473
3474 s = rp_current_screen;
3475 XWarpPointer (dpy, None, s->root, 0, 0, 0, 0, ARG(0,number), ARG(1,number));
3476 return cmdret_new (RET_SUCCESS, NULL);
3477 }
3478
3479 cmdret *
3480 cmd_ratrelwarp (int interactive UNUSED, struct cmdarg **args)
3481 {
3482 XWarpPointer (dpy, None, None, 0, 0, 0, 0, ARG(0,number), ARG(1,number));
3483 return cmdret_new (RET_SUCCESS, NULL);
3484 }
3485
3486 cmdret *
3487 cmd_ratclick (int interactive UNUSED, struct cmdarg **args)
3488 {
3489 int button = 1;
3490
3491 if (args[0])
3492 {
3493 button = ARG(0,number);
3494 if (button < 1 || button > 3)
3495 return cmdret_new (RET_SUCCESS, "ratclick: invalid argument");
3496 }
3497
3498 #ifdef HAVE_LIBXTST
3499 XTestFakeButtonEvent(dpy, button, True, CurrentTime);
3500 XTestFakeButtonEvent(dpy, button, False, CurrentTime);
3501 return cmdret_new (RET_SUCCESS, NULL);
3502 #else
3503 return cmdret_new (RET_FAILURE, "ratclick: Please compile with the Xtst extension");
3504 #endif
3505 }
3506
3507 cmdret *
3508 cmd_rathold (int interactive UNUSED, struct cmdarg **args)
3509 {
3510 int button = 1;
3511
3512 if (args[1])
3513 {
3514 button = ARG(1,number);
3515 if (button < 1 || button > 3)
3516 return cmdret_new (RET_SUCCESS, "ratclick: invalid argument");
3517 }
3518
3519 #ifdef HAVE_LIBXTST
3520 if (!strcmp(ARG_STRING(0), "down"))
3521 XTestFakeButtonEvent(dpy, button, True, CurrentTime);
3522 else if(!strcmp(ARG_STRING(0),"up"))
3523 XTestFakeButtonEvent(dpy, button, False, CurrentTime);
3524 else
3525 return cmdret_new (RET_FAILURE, "rathold: '%s' invalid argument", ARG_STRING(0));
3526
3527 return cmdret_new (RET_SUCCESS, NULL);
3528 #else
3529 return cmdret_new (RET_FAILURE, "rathold: Please compile with the Xtst extension");
3530 #endif
3531 }
3532
3533 cmdret *
3534 cmd_curframe (int interactive, struct cmdarg **args UNUSED)
3535 {
3536 if (interactive)
3537 {
3538 show_frame_indicator(1);
3539 return cmdret_new (RET_SUCCESS, NULL);
3540 }
3541 else
3542 return cmdret_new(RET_SUCCESS, "%d", current_frame()->number);
3543 }
3544
3545 /* Thanks to Martin Samuelsson <[email protected]> for the
3546 original patch. */
3547 cmdret *
3548 cmd_license (int interactive UNUSED, struct cmdarg **args UNUSED)
3549 {
3550 rp_screen *s = rp_current_screen;
3551 int x = 10;
3552 int y = 10;
3553 int i;
3554 int max_width = 0;
3555 char *license_text[] = { PACKAGE " " VERSION,
3556 "",
3557 "Copyright (C) 2000, 2001, 2002, 2003, 2004 Shawn Betts",
3558 "",
3559 "ratpoison is free software; you can redistribute it and/or modify ",
3560 "it under the terms of the GNU General Public License as published by ",
3561 "the Free Software Foundation; either version 2, or (at your option) ",
3562 "any later version.",
3563 "",
3564 "ratpoison is distributed in the hope that it will be useful, ",
3565 "but WITHOUT ANY WARRANTY; without even the implied warranty of ",
3566 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ",
3567 "GNU General Public License for more details.",
3568 "",
3569 "You should have received a copy of the GNU General Public License ",
3570 "along with this software; see the file COPYING. If not, write to ",
3571 "the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ",
3572 "Boston, MA 02111-1307 USA",
3573 "",
3574 "Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza ",
3575 "to [email protected] or visit ",
3576 "http://www.nongnu.org/ratpoison/",
3577 "",
3578 "[Press any key to end.] ",
3579 NULL};
3580
3581 /* Switch to the default colormap. */
3582 if (current_window())
3583 XUninstallColormap (dpy, current_window()->colormap);
3584 XInstallColormap (dpy, s->def_cmap);
3585
3586 XMapRaised (dpy, s->help_window);
3587
3588 /* Find the longest line. */
3589 for(i=0; license_text[i]; i++)
3590 {
3591 int tmp;
3592
3593 tmp = rp_text_width (s, license_text[i], -1);
3594 if (tmp > max_width)
3595 max_width = tmp;
3596 }
3597
3598 /* Offset the text so its in the center. */
3599 x = s->left + (s->width - max_width) / 2;
3600 y = s->top + (s->height - i * FONT_HEIGHT (s)) / 2;
3601 if (x < 0) x = 0;
3602 if (y < 0) y = 0;
3603
3604 /* Print the text. */
3605 for(i=0; license_text[i]; i++)
3606 {
3607 rp_draw_string (s, s->help_window, STYLE_NORMAL,
3608 x, y + FONT_ASCENT(s),
3609 license_text[i], -1);
3610
3611 y += FONT_HEIGHT (s);
3612 }
3613
3614 /* Wait for a key press. */
3615 read_any_key ();
3616 XUnmapWindow (dpy, s->help_window);
3617
3618 /* Possibly restore colormap. */
3619 if (current_window())
3620 {
3621 XUninstallColormap (dpy, s->def_cmap);
3622 XInstallColormap (dpy, current_window()->colormap);
3623 }
3624
3625 /* The help window overlaps the bar, so redraw it. */
3626 if (rp_current_screen->bar_is_raised)
3627 redraw_last_message();
3628
3629 return cmdret_new (RET_SUCCESS, NULL);
3630 }
3631
3632 cmdret *
3633 cmd_help (int interactive, struct cmdarg **args)
3634 {
3635 rp_keymap *map;
3636
3637 if (args[0])
3638 map = ARG(0,keymap);
3639 else
3640 map = find_keymap (ROOT_KEYMAP);
3641
3642 if (interactive)
3643 {
3644 rp_screen *s = rp_current_screen;
3645 int i, old_i;
3646 int x = 10;
3647 int y = 0;
3648 int header_offset;
3649 int width, max_width = 0;
3650 /* 1 if we are drawing keys, 0 if we are drawing commands */
3651 int drawing_keys = 1;
3652 char *keysym_name;
3653
3654 /* Switch to the default colormap. */
3655 if (current_window())
3656 XUninstallColormap (dpy, current_window()->colormap);
3657 XInstallColormap (dpy, s->def_cmap);
3658
3659 XMapRaised (dpy, s->help_window);
3660
3661 rp_draw_string (s, s->help_window, STYLE_NORMAL,
3662 10, y + FONT_ASCENT(s),
3663 "ratpoison key bindings", -1);
3664
3665 y += FONT_HEIGHT (s) * 2;
3666
3667 /* Only print the "Command key" for the root keymap */
3668 if (map == find_keymap (ROOT_KEYMAP))
3669 {
3670 rp_draw_string (s, s->help_window, STYLE_NORMAL,
3671 10, y + FONT_ASCENT(s),
3672 "Command key: ", -1);
3673
3674 keysym_name = keysym_to_string (prefix_key.sym, prefix_key.state);
3675 rp_draw_string (s, s->help_window, STYLE_NORMAL,
3676 10 + rp_text_width (s, "Command key: ", -1),
3677 y + FONT_ASCENT(s),
3678 keysym_name, -1);
3679 free (keysym_name);
3680
3681 y += FONT_HEIGHT (s) * 2;
3682 }
3683
3684 header_offset = y;
3685
3686 i = old_i = 0;
3687 while (i < map->actions_last && old_i < map->actions_last)
3688 {
3689 if (drawing_keys)
3690 {
3691 keysym_name =
3692 keysym_to_string (map->actions[i].key, map->actions[i].state);
3693
3694 rp_draw_string (s, s->help_window, STYLE_NORMAL,
3695 x, y + FONT_ASCENT(s),
3696 keysym_name, -1);
3697
3698 width = rp_text_width (s, keysym_name, -1);
3699 if (width > max_width)
3700 max_width = width;
3701
3702 free (keysym_name);
3703 }
3704 else
3705 {
3706 rp_draw_string (s, s->help_window, STYLE_NORMAL,
3707 x, y + FONT_ASCENT(s),
3708 map->actions[i].data, -1);
3709
3710 width = rp_text_width (s, map->actions[i].data, -1);
3711 if (width > max_width)
3712 max_width = width;
3713 }
3714
3715 y += FONT_HEIGHT (s);
3716 /* Make sure the next line fits entirely within the window. */
3717 if (y + FONT_HEIGHT (s) >= (s->top + s->height))
3718 {
3719 if (drawing_keys)
3720 {
3721 x += max_width + 10;
3722 drawing_keys = 0;
3723 i = old_i;
3724 }
3725 else
3726 {
3727 x += max_width + 20;
3728 drawing_keys = 1;
3729 i++;
3730 old_i = i;
3731 }
3732
3733 max_width = 0;
3734 y = header_offset;
3735 }
3736 else
3737 {
3738 i++;
3739 if (i == map->actions_last && drawing_keys)
3740 {
3741 x += max_width + 10;
3742 drawing_keys = 0;
3743 y = header_offset;
3744 i = old_i;
3745 max_width = 0;
3746 }
3747 }
3748 }
3749
3750 read_any_key();
3751 XUnmapWindow (dpy, s->help_window);
3752
3753 /* Possibly restore colormap. */
3754 if (current_window())
3755 {
3756 XUninstallColormap (dpy, s->def_cmap);
3757 XInstallColormap (dpy, current_window()->colormap);
3758 }
3759
3760 /* The help window overlaps the bar, so redraw it. */
3761 if (rp_current_screen->bar_is_raised)
3762 redraw_last_message();
3763
3764 return cmdret_new (RET_SUCCESS, NULL);
3765 }
3766 else
3767 {
3768 struct sbuf *help_list;
3769 char *keysym_name;
3770 int i;
3771 cmdret *ret;
3772
3773 help_list = sbuf_new (0);
3774
3775 for (i = 0; i < map->actions_last; i++)
3776 {
3777 keysym_name = keysym_to_string (map->actions[i].key, map->actions[i].state);
3778 sbuf_concat (help_list, keysym_name);
3779 free (keysym_name);
3780 sbuf_concat (help_list, " ");
3781 sbuf_concat (help_list, map->actions[i].data);
3782 if (i < map->actions_last - 1)
3783 sbuf_concat (help_list, "\n");
3784 }
3785
3786 ret = cmdret_new (RET_SUCCESS, "%s", sbuf_get (help_list));
3787 sbuf_free (help_list);
3788 return ret;
3789 }
3790
3791 return cmdret_new (RET_SUCCESS, NULL);
3792 }
3793
3794 cmdret *
3795 set_rudeness (struct cmdarg **args)
3796 {
3797 int num;
3798
3799 if (args[0] == NULL)
3800 return cmdret_new (RET_SUCCESS, "%d",
3801 rp_honour_transient_raise
3802 | (rp_honour_normal_raise << 1)
3803 | (rp_honour_transient_map << 2)
3804 | (rp_honour_normal_map << 3));
3805
3806 num = ARG(0,number);
3807 if (num < 0 || num > 15)
3808 return cmdret_new (RET_FAILURE, "rudeness: invalid level '%s'", ARG_STRING(0));
3809
3810 rp_honour_transient_raise = num & 1 ? 1 : 0;
3811 rp_honour_normal_raise = num & 2 ? 1 : 0;
3812 rp_honour_transient_map = num & 4 ? 1 : 0;
3813 rp_honour_normal_map = num & 8 ? 1 : 0;
3814
3815 return cmdret_new (RET_SUCCESS, NULL);
3816 }
3817
3818 /* compat */
3819 cmdret *
3820 cmd_rudeness (int interactive UNUSED, struct cmdarg **args)
3821 {
3822 PRINT_WARNING (("command \"rudeness\" is deprecated, "
3823 "use \"set rudeness\" instead\n"));
3824
3825 return set_rudeness (args);
3826 }
3827
3828 char *
3829 wingravity_to_string (int g)
3830 {
3831 switch (g)
3832 {
3833 case NorthWestGravity:
3834 return "nw";
3835 case WestGravity:
3836 return "w";
3837 case SouthWestGravity:
3838 return "sw";
3839 case NorthGravity:
3840 return "n";
3841 case CenterGravity:
3842 return "c";
3843 case SouthGravity:
3844 return "s";
3845 case NorthEastGravity:
3846 return "ne";
3847 case EastGravity:
3848 return "e";
3849 case SouthEastGravity:
3850 return "se";
3851 }
3852
3853 PRINT_DEBUG (("Unknown gravity!\n"));
3854 return "Unknown";
3855 }
3856
3857 cmdret *
3858 cmd_gravity (int interactive UNUSED, struct cmdarg **args)
3859 {
3860 int gravity;
3861 rp_window *win;
3862
3863 win = current_window();
3864 if (!win)
3865 return cmdret_new (RET_FAILURE, NULL);
3866
3867 if (args[0] == NULL)
3868 return cmdret_new (RET_SUCCESS, "%s", wingravity_to_string (win->gravity));
3869
3870 if ((gravity = parse_wingravity (ARG_STRING(0))) < 0)
3871 return cmdret_new (RET_FAILURE, "gravity: unknown gravity");
3872 else
3873 {
3874 win->gravity = gravity;
3875 maximize (win);
3876 }
3877
3878 return cmdret_new (RET_SUCCESS, NULL);
3879 }
3880
3881 static cmdret *
3882 set_wingravity (struct cmdarg **args)
3883 {
3884 if (args[0] == NULL)
3885 return cmdret_new (RET_SUCCESS, "%s", wingravity_to_string (defaults.win_gravity));
3886
3887 defaults.win_gravity = ARG(0,gravity);
3888
3889 return cmdret_new (RET_SUCCESS, NULL);
3890 }
3891
3892 static cmdret *
3893 set_transgravity (struct cmdarg **args)
3894 {
3895 if (args[0] == NULL)
3896 return cmdret_new (RET_SUCCESS, "%s", wingravity_to_string (defaults.trans_gravity));
3897
3898 defaults.trans_gravity = ARG(0,gravity);
3899
3900 return cmdret_new (RET_SUCCESS, NULL);
3901 }
3902
3903 static cmdret *
3904 set_maxsizegravity (struct cmdarg **args)
3905 {
3906 if (args[0] == NULL)
3907 return cmdret_new (RET_SUCCESS, "%s", wingravity_to_string (defaults.maxsize_gravity));
3908
3909 defaults.maxsize_gravity = ARG(0,gravity);
3910
3911 return cmdret_new (RET_SUCCESS, NULL);
3912 }
3913
3914 static cmdret *
3915 set_msgwait (struct cmdarg **args)
3916 {
3917 if (args[0] == NULL)
3918 return cmdret_new (RET_SUCCESS, "%d", defaults.bar_timeout);
3919
3920 if (ARG(0,number) < 0)
3921 return cmdret_new (RET_FAILURE, "msgwait: %s", invalid_negative_arg);
3922 else
3923 defaults.bar_timeout = ARG(0,number);
3924
3925 return cmdret_new (RET_SUCCESS, NULL);
3926 }
3927
3928 /* compat */
3929 cmdret *
3930 cmd_msgwait (int interactive UNUSED, struct cmdarg **args)
3931 {
3932 PRINT_WARNING (("command \"msgwait\" is deprecated, "
3933 "use \"set msgwait\" instead\n"));
3934
3935 return set_msgwait (args);
3936 }
3937
3938 static cmdret *
3939 set_framemsgwait (struct cmdarg **args)
3940 {
3941 if (args[0] == NULL)
3942 return cmdret_new (RET_SUCCESS, "%d", defaults.frame_indicator_timeout);
3943
3944 if (ARG(0,number) < -1)
3945 return cmdret_new (RET_FAILURE, "framemsgwait: %s", invalid_negative_arg);
3946 else
3947 defaults.frame_indicator_timeout = ARG(0,number);
3948
3949 return cmdret_new (RET_SUCCESS, NULL);
3950 }
3951
3952 static cmdret *
3953 set_bargravity (struct cmdarg **args)
3954 {
3955 if (args[0] == NULL)
3956 return cmdret_new (RET_SUCCESS, "%s", wingravity_to_string (defaults.bar_location));
3957
3958 defaults.bar_location = ARG(0,gravity);
3959
3960 return cmdret_new (RET_SUCCESS, NULL);
3961 }
3962
3963 static void
3964 update_gc (rp_screen *s)
3965 {
3966 XGCValues gcv;
3967
3968 gcv.foreground = rp_glob_screen.fg_color;
3969 gcv.background = rp_glob_screen.bg_color;
3970 gcv.function = GXcopy;
3971 gcv.line_width = 1;
3972 gcv.subwindow_mode = IncludeInferiors;
3973 XFreeGC (dpy, s->normal_gc);
3974 s->normal_gc = XCreateGC(dpy, s->root,
3975 GCForeground | GCBackground
3976 | GCFunction | GCLineWidth
3977 | GCSubwindowMode, &gcv);
3978 gcv.foreground = rp_glob_screen.bg_color;
3979 gcv.background = rp_glob_screen.fg_color;
3980 XFreeGC (dpy, s->inverse_gc);
3981 s->inverse_gc = XCreateGC(dpy, s->root,
3982 GCForeground | GCBackground
3983 | GCFunction | GCLineWidth
3984 | GCSubwindowMode, &gcv);
3985 }
3986
3987 #ifndef USE_XFT_FONT
3988 static void
3989 update_all_gcs (void)
3990 {
3991 rp_screen *cur;
3992
3993 list_for_each_entry (cur, &rp_screens, node)
3994 {
3995 update_gc (cur);
3996 }
3997 }
3998 #endif
3999
4000 static cmdret *
4001 set_historysize (struct cmdarg **args)
4002 {
4003 if (args[0] == NULL)
4004 return cmdret_new (RET_SUCCESS, "%d", defaults.history_size);
4005
4006 if (ARG(0, number) < 0)
4007 return cmdret_new (RET_FAILURE, "set historysize: %s",
4008 invalid_negative_arg);
4009
4010 defaults.history_size = ARG(0, number);
4011 return cmdret_new (RET_SUCCESS, NULL);
4012 }
4013
4014 static cmdret *
4015 set_historycompaction (struct cmdarg **args)
4016 {
4017 if (args[0] == NULL)
4018 return cmdret_new (RET_SUCCESS, "%d", defaults.history_compaction);
4019
4020 if (ARG(0, number) != 0 && ARG(0, number) != 1)
4021 return cmdret_new (RET_FAILURE, "set historycompaction: invalid argument");
4022
4023 defaults.history_compaction = ARG(0, number);
4024 return cmdret_new (RET_SUCCESS, NULL);
4025 }
4026
4027 static cmdret *
4028 set_historyexpansion (struct cmdarg **args)
4029 {
4030 if (args[0] == NULL)
4031 return cmdret_new (RET_SUCCESS, "%d", defaults.history_expansion);
4032 #ifndef HAVE_HISTORY
4033 if (ARG(0, number)) {
4034 return cmdret_new (RET_FAILURE, "Not compiled with libhistory");
4035 }
4036 #endif
4037
4038 if (ARG(0, number) != 0 && ARG(0, number) != 1)
4039 return cmdret_new (RET_SUCCESS, "set historyexpansion: invalid argument");
4040
4041 defaults.history_expansion = ARG(0, number);
4042 return cmdret_new (RET_SUCCESS, NULL);
4043 }
4044
4045 static cmdret *
4046 set_font (struct cmdarg **args)
4047 {
4048 #ifdef USE_XFT_FONT
4049 XftFont *font;
4050 rp_screen *s = rp_current_screen;
4051
4052 if (args[0] == NULL)
4053 return cmdret_new (RET_SUCCESS, "%s", defaults.font_string);
4054
4055 font = XftFontOpenName (dpy, s->screen_num, ARG_STRING (0));
4056
4057 if (font == NULL)
4058 return cmdret_new (RET_FAILURE, "set font: unknown font");
4059
4060 XftFontClose (dpy, s->xft_font);
4061 s->xft_font = font;
4062 #else
4063 XFontSet font;
4064
4065 if (args[0] == NULL)
4066 return cmdret_new (RET_SUCCESS, "%s", defaults.font_string);
4067
4068 font = load_query_font_set (dpy, ARG_STRING(0));
4069 if (font == NULL)
4070 return cmdret_new (RET_FAILURE, "set font: unknown font");
4071
4072 /* Save the font as the default. */
4073 XFreeFontSet (dpy, defaults.font);
4074 defaults.font = font;
4075 set_extents_of_fontset(font);
4076 update_all_gcs();
4077 #endif
4078
4079 free (defaults.font_string);
4080 defaults.font_string = xstrdup (ARG_STRING(0));
4081
4082 return cmdret_new (RET_SUCCESS, NULL);
4083 }
4084
4085 static cmdret *
4086 set_padding (struct cmdarg **args)
4087 {
4088 rp_frame *frame;
4089 int l, t, r, b;
4090
4091 if (args[0] == NULL)
4092 return cmdret_new (RET_SUCCESS, "%d %d %d %d",
4093 defaults.padding_left,
4094 defaults.padding_top,
4095 defaults.padding_right,
4096 defaults.padding_bottom);
4097
4098 l = ARG(0,number);
4099 t = ARG(1,number);
4100 r = ARG(2,number);
4101 b = ARG(3,number);
4102
4103 if (l < 0 || t < 0 || r < 0 || b < 0)
4104 return cmdret_new (RET_FAILURE, "set padding: %s", invalid_negative_arg);
4105
4106 /* Resize the frames to make sure they are not too big and not too
4107 small. */
4108 list_for_each_entry (frame,&(rp_current_screen->frames),node)
4109 {
4110 int bk_pos, bk_len;
4111
4112 /* Resize horizontally. */
4113 bk_pos = frame->x;
4114 bk_len = frame->width;
4115
4116 if (frame->x == defaults.padding_left)
4117 {
4118 frame->x = l;
4119 frame->width += bk_pos - l;
4120 }
4121
4122 if ((bk_pos + bk_len) == (rp_current_screen->left + rp_current_screen->width - defaults.padding_right))
4123 frame->width = rp_current_screen->left + rp_current_screen->width - r - frame->x;
4124
4125 /* Resize vertically. */
4126 bk_pos = frame->y;
4127 bk_len = frame->height;
4128
4129 if (frame->y == defaults.padding_top)
4130 {
4131 frame->y = t;
4132 frame->height += bk_pos - t;
4133 }
4134
4135 if ((bk_pos + bk_len) == (rp_current_screen->top + rp_current_screen->height - defaults.padding_bottom))
4136 frame->height = rp_current_screen->top + rp_current_screen->height - b - frame->y;
4137
4138 maximize_all_windows_in_frame (frame);
4139 }
4140
4141 defaults.padding_left = l;
4142 defaults.padding_right = r;
4143 defaults.padding_top = t;
4144 defaults.padding_bottom = b;
4145
4146 return cmdret_new (RET_SUCCESS, NULL);
4147 }
4148
4149 static cmdret *
4150 set_border (struct cmdarg **args)
4151 {
4152 rp_window *win;
4153
4154 if (args[0] == NULL)
4155 return cmdret_new (RET_SUCCESS, "%d", defaults.window_border_width);
4156
4157 if (ARG(0,number) < 0)
4158 return cmdret_new (RET_FAILURE, "set border: %s", invalid_negative_arg);
4159
4160 defaults.window_border_width = ARG(0,number);
4161
4162 /* Update all the visible windows. */
4163 list_for_each_entry (win,&rp_mapped_window,node)
4164 {
4165 if (win_get_frame (win))
4166 maximize (win);
4167 }
4168
4169 return cmdret_new (RET_SUCCESS, NULL);
4170 }
4171
4172 static cmdret *
4173 set_barborder (struct cmdarg **args)
4174 {
4175 rp_screen *cur;
4176
4177 if (args[0] == NULL)
4178 return cmdret_new (RET_SUCCESS, "%d", defaults.bar_border_width);
4179
4180 if (ARG(0,number) < 0)
4181 return cmdret_new (RET_FAILURE, "set barborder: %s", invalid_negative_arg);
4182
4183 defaults.bar_border_width = ARG(0,number);
4184
4185 /* Update the frame and bar windows. */
4186 list_for_each_entry (cur, &rp_screens, node)
4187 {
4188 XSetWindowBorderWidth (dpy, cur->bar_window, defaults.bar_border_width);
4189 XSetWindowBorderWidth (dpy, cur->frame_window, defaults.bar_border_width);
4190 XSetWindowBorderWidth (dpy, cur->input_window, defaults.bar_border_width);
4191 }
4192
4193 return cmdret_new (RET_SUCCESS, NULL);
4194 }
4195
4196 static cmdret *
4197 set_barinpadding (struct cmdarg **args)
4198 {
4199 int new_value;
4200
4201 if (args[0] == NULL)
4202 return cmdret_new (RET_SUCCESS, "%d", defaults.bar_in_padding);
4203
4204 new_value = ARG(0,number);
4205 if (new_value != 0 && new_value != 1)
4206 return cmdret_new (RET_FAILURE, "set barinpadding: invalid argument");
4207
4208 defaults.bar_in_padding = new_value;
4209
4210 return cmdret_new (RET_SUCCESS, NULL);
4211 }
4212
4213 static cmdret *
4214 set_inputwidth (struct cmdarg **args)
4215 {
4216 if (args[0] == NULL)
4217 return cmdret_new (RET_SUCCESS, "%d", defaults.input_window_size);
4218
4219 if (ARG(0,number) < 0)
4220 return cmdret_new (RET_FAILURE, "set inputwidth: %s", invalid_negative_arg);
4221
4222 defaults.input_window_size = ARG(0,number);
4223 return cmdret_new (RET_SUCCESS, NULL);
4224 }
4225
4226 static cmdret *
4227 set_waitcursor (struct cmdarg **args)
4228 {
4229 if (args[0] == NULL)
4230 return cmdret_new (RET_SUCCESS, "%d", defaults.wait_for_key_cursor);
4231
4232 if (ARG(0,number) != 0 && ARG(0,number) != 1)
4233 return cmdret_new (RET_FAILURE, "set waitcursor: invalid argument");
4234
4235 defaults.wait_for_key_cursor = ARG(0,number);
4236 return cmdret_new (RET_SUCCESS, NULL);
4237 }
4238
4239 static cmdret *
4240 set_infofmt (struct cmdarg **args)
4241 {
4242 if (args[0] == NULL)
4243 return cmdret_new (RET_SUCCESS, "%s", defaults.info_fmt);
4244
4245 free (defaults.info_fmt);
4246 defaults.info_fmt = xstrdup (ARG_STRING(0));
4247
4248 return cmdret_new (RET_SUCCESS, NULL);
4249 }
4250
4251 static cmdret *
4252 set_topkmap (struct cmdarg **args)
4253 {
4254 if (args[0] == NULL)
4255 return cmdret_new (RET_SUCCESS, "%s", defaults.top_kmap);
4256
4257 if (!find_keymap (ARG_STRING(0)))
4258 return cmdret_new(RET_FAILURE, "Unknown keymap %s", ARG_STRING(0));
4259
4260 ungrab_keys_all_wins();
4261
4262 free (defaults.top_kmap);
4263 defaults.top_kmap = xstrdup (ARG_STRING(0));
4264
4265 grab_keys_all_wins();
4266 XSync(dpy, False);
4267
4268 return cmdret_new (RET_SUCCESS, NULL);
4269 }
4270
4271 static cmdret *
4272 set_winfmt (struct cmdarg **args)
4273 {
4274 if (args[0] == NULL)
4275 return cmdret_new (RET_SUCCESS, "%s", defaults.window_fmt);
4276
4277 free (defaults.window_fmt);
4278 defaults.window_fmt = xstrdup (ARG_STRING(0));
4279
4280 return cmdret_new (RET_SUCCESS, NULL);
4281 }
4282
4283 static cmdret *
4284 set_winname (struct cmdarg **args)
4285 {
4286 char *name;
4287
4288 if (args[0] == NULL)
4289 switch (defaults.win_name)
4290 {
4291 case WIN_NAME_TITLE:
4292 return cmdret_new (RET_SUCCESS, "title");
4293 case WIN_NAME_RES_NAME:
4294 return cmdret_new (RET_SUCCESS, "name");
4295 case WIN_NAME_RES_CLASS:
4296 return cmdret_new (RET_SUCCESS, "class");
4297 default:
4298 PRINT_DEBUG (("Unknown win_name\n"));
4299 return cmdret_new (RET_FAILURE, "unknown");
4300 }
4301
4302 name = ARG_STRING(0);
4303
4304 if (!strncmp (name, "title", sizeof ("title")))
4305 defaults.win_name = WIN_NAME_TITLE;
4306 else if (!strncmp (name, "name", sizeof ("name")))
4307 defaults.win_name = WIN_NAME_RES_NAME;
4308 else if (!strncmp (name, "class", sizeof ("class")))
4309 defaults.win_name = WIN_NAME_RES_CLASS;
4310 else
4311 return cmdret_new (RET_FAILURE, "set winname: invalid argument `%s'", name);
4312
4313 return cmdret_new (RET_SUCCESS, NULL);
4314 }
4315
4316 static cmdret *
4317 set_framefmt (struct cmdarg **args)
4318 {
4319 if (args[0] == NULL)
4320 return cmdret_new (RET_SUCCESS, "%s", defaults.frame_fmt);
4321
4322 free (defaults.frame_fmt);
4323 defaults.frame_fmt = xstrdup (ARG_STRING(0));
4324
4325 return cmdret_new (RET_SUCCESS, NULL);
4326 }
4327
4328 static cmdret *
4329 set_fgcolor (struct cmdarg **args)
4330 {
4331 XColor color, junk;
4332 rp_screen *cur;
4333
4334 if (args[0] == NULL)
4335 return cmdret_new (RET_SUCCESS, "%s", defaults.fgcolor_string);
4336
4337 list_for_each_entry (cur, &rp_screens, node)
4338 {
4339 if (!XAllocNamedColor (dpy, cur->def_cmap, ARG_STRING(0), &color, &junk))
4340 return cmdret_new (RET_FAILURE, "set fgcolor: unknown color");
4341
4342 rp_glob_screen.fg_color = color.pixel;
4343 update_gc (cur);
4344 XSetWindowBorder (dpy, cur->bar_window, color.pixel);
4345 XSetWindowBorder (dpy, cur->input_window, color.pixel);
4346 XSetWindowBorder (dpy, cur->frame_window, color.pixel);
4347 XSetWindowBorder (dpy, cur->help_window, color.pixel);
4348
4349 #ifdef USE_XFT_FONT
4350 if (!XftColorAllocName (dpy, DefaultVisual (dpy, cur->screen_num),
4351 DefaultColormap (dpy, cur->screen_num),
4352 ARG_STRING(0), &cur->xft_fg_color))
4353 return cmdret_new (RET_FAILURE, "set fgcolor: unknown color");
4354 #endif
4355
4356 free (defaults.fgcolor_string);
4357 defaults.fgcolor_string = xstrdup (ARG_STRING(0));
4358 }
4359
4360 return cmdret_new (RET_SUCCESS, NULL);
4361 }
4362
4363 static cmdret *
4364 set_bgcolor (struct cmdarg **args)
4365 {
4366 XColor color, junk;
4367 rp_screen *cur;
4368
4369 if (args[0] == NULL)
4370 return cmdret_new (RET_SUCCESS, "%s", defaults.bgcolor_string);
4371
4372
4373 list_for_each_entry (cur, &rp_screens, node)
4374 {
4375 if (!XAllocNamedColor (dpy, cur->def_cmap, ARG_STRING(0), &color, &junk))
4376 return cmdret_new (RET_FAILURE, "set bgcolor: unknown color");
4377
4378 rp_glob_screen.bg_color = color.pixel;
4379 update_gc (cur);
4380 XSetWindowBackground (dpy, cur->bar_window, color.pixel);
4381 XSetWindowBackground (dpy, cur->input_window, color.pixel);
4382 XSetWindowBackground (dpy, cur->frame_window, color.pixel);
4383 XSetWindowBackground (dpy, cur->help_window, color.pixel);
4384
4385 #ifdef USE_XFT_FONT
4386 if (!XftColorAllocName (dpy, DefaultVisual (dpy, cur->screen_num),
4387 DefaultColormap (dpy, cur->screen_num),
4388 ARG_STRING(0), &cur->xft_bg_color))
4389 return cmdret_new (RET_FAILURE, "set fgcolor: unknown color");
4390 #endif
4391
4392 free (defaults.bgcolor_string);
4393 defaults.bgcolor_string = xstrdup (ARG_STRING(0));
4394 }
4395
4396 return cmdret_new (RET_SUCCESS, NULL);
4397 }
4398
4399 static cmdret *
4400 set_fwcolor (struct cmdarg **args)
4401 {
4402 XColor color, junk;
4403 rp_window *win = current_window();
4404 rp_screen *cur;
4405
4406 if (args[0] == NULL)
4407 return cmdret_new (RET_SUCCESS, "%s", defaults.fwcolor_string);
4408
4409 list_for_each_entry (cur, &rp_screens, node)
4410 {
4411 if (!XAllocNamedColor (dpy, cur->def_cmap, ARG_STRING(0), &color, &junk))
4412 return cmdret_new (RET_FAILURE, "set fwcolor: unknown color");
4413
4414 rp_glob_screen.fw_color = color.pixel;
4415 update_gc (cur);
4416
4417 free (defaults.fwcolor_string);
4418 defaults.fwcolor_string = xstrdup (ARG_STRING(0));
4419 }
4420
4421 /* Update current window. */
4422 if (win != NULL)
4423 XSetWindowBorder (dpy, win->w, rp_glob_screen.fw_color);
4424
4425 return cmdret_new (RET_SUCCESS, NULL);
4426 }
4427
4428 static cmdret *
4429 set_bwcolor (struct cmdarg **args)
4430 {
4431 XColor color, junk;
4432 rp_window *win, *cur_win = current_window();
4433 rp_screen *cur_screen;
4434
4435 if (args[0] == NULL)
4436 return cmdret_new (RET_SUCCESS, "%s", defaults.bwcolor_string);
4437
4438 list_for_each_entry (cur_screen, &rp_screens, node)
4439 {
4440 if (!XAllocNamedColor (dpy, cur_screen->def_cmap, ARG_STRING(0), &color, &junk))
4441 return cmdret_new (RET_FAILURE, "set bwcolor: unknown color");
4442
4443 rp_glob_screen.bw_color = color.pixel;
4444 update_gc (cur_screen);
4445
4446 free (defaults.bwcolor_string);
4447 defaults.bwcolor_string = xstrdup (ARG_STRING(0));
4448 }
4449
4450 /* Update all the visible windows. */
4451 list_for_each_entry (win,&rp_mapped_window,node)
4452 {
4453 if (win != cur_win)
4454 XSetWindowBorder (dpy, win->w, rp_glob_screen.bw_color);
4455 }
4456
4457
4458 return cmdret_new (RET_SUCCESS, NULL);
4459 }
4460
4461 cmdret *
4462 cmd_setenv (int interactive UNUSED, struct cmdarg **args)
4463 {
4464
4465 const char *var = ARG_STRING(0), *val = ARG_STRING(1);
4466 int ret;
4467
4468 #ifdef HAVE_SETENV
4469
4470 PRINT_DEBUG(("setenv (\"%s\", \"%s\", 1)\n", var, val));
4471 ret = setenv (var, val, 1);
4472
4473 #else /* not HAVE_SETENV */
4474
4475 struct sbuf *env;
4476
4477 /* Setup the environment string. */
4478 env = sbuf_new (strlen (var) + 1 + strlen (val) + 1);
4479
4480 sbuf_copy (env, var);
4481 sbuf_concat (env, "=");
4482 sbuf_concat (env, val);
4483
4484 /* Stick it in the environment. We must *not* free it. */
4485 PRINT_DEBUG(("putenv(\"%s\")\n", sbuf_get(env)));
4486 ret = putenv (sbuf_free_struct (env));
4487
4488 #endif /* not HAVE_SETENV */
4489
4490 if (ret == -1)
4491 return cmdret_new (RET_FAILURE, "cmd_setenv failed: %s", strerror(errno));
4492 else
4493 return cmdret_new (RET_SUCCESS, NULL);
4494 }
4495
4496 cmdret *
4497 cmd_getenv (int interactive UNUSED, struct cmdarg **args)
4498 {
4499 char *value;
4500
4501 value = getenv (ARG_STRING(0));
4502 if (value)
4503 return cmdret_new (RET_SUCCESS, "%s", value);
4504 else
4505 return cmdret_new (RET_FAILURE, NULL);
4506 }
4507
4508 /* Thanks to Gergely Nagy <[email protected]> for the original
4509 patch. */
4510 cmdret *
4511 cmd_chdir (int interactive UNUSED, struct cmdarg **args)
4512 {
4513 const char *dir;
4514
4515 if (args[0] == NULL)
4516 {
4517 dir = get_homedir ();
4518 if (dir == NULL)
4519 {
4520 return cmdret_new (RET_FAILURE, "chdir: unable to find your HOME directory");
4521 }
4522 }
4523 else
4524 dir = ARG_STRING(0);
4525
4526 if (chdir (dir) == -1)
4527 return cmdret_new (RET_FAILURE, "chdir: %s: %s", dir, strerror(errno));
4528
4529 return cmdret_new (RET_SUCCESS, NULL);
4530 }
4531
4532 /* Thanks to Gergely Nagy <[email protected]> for the original
4533 patch. */
4534 cmdret *
4535 cmd_unsetenv (int interactive UNUSED, struct cmdarg **args)
4536 {
4537 const char *var = ARG_STRING(0);
4538 int ret;
4539
4540 /* Use unsetenv() where possible since putenv("FOO") is not legit everywhere */
4541 #ifdef HAVE_UNSETENV
4542 ret = unsetenv(var);
4543 #else
4544 /* MinGW doesn't have unsetenv() and uses putenv("FOO=") */
4545 struct sbuf *s = sbuf_new (strlen (var) + 1 + 1);
4546 sbuf_copy (s, var);
4547 sbuf_concat (s, "=");
4548 /* let putenv() decide whether to call free() */
4549 ret = putenv (sbuf_free_struct (s));
4550 #endif
4551
4552 if (ret == -1)
4553 return cmdret_new (RET_FAILURE, "cmd_unsetenv failed: %s", strerror (errno));
4554 else
4555 return cmdret_new (RET_SUCCESS, NULL);
4556 }
4557
4558 /* Thanks to Gergely Nagy <[email protected]> for the original
4559 patch. */
4560 cmdret *
4561 cmd_info (int interactive UNUSED, struct cmdarg **args)
4562 {
4563 struct sbuf *buf;
4564 if (current_window() != NULL)
4565 {
4566 rp_window *win = current_window();
4567 rp_window_elem *win_elem;
4568 win_elem = group_find_window (&rp_current_group->mapped_windows, win);
4569 if (!win_elem)
4570 win_elem = group_find_window (&rp_current_group->unmapped_windows, win);
4571 if (!win_elem)
4572 {
4573 rp_group *g = groups_find_group_by_window(win);
4574 if (g != NULL)
4575 win_elem = group_find_window (&g->mapped_windows, win);
4576 if (!win_elem && g != NULL)
4577 win_elem = group_find_window (&g->unmapped_windows, win);
4578 }
4579 if (win_elem)
4580 {
4581 char *s;
4582 cmdret *ret;
4583
4584 if (args[0] == NULL)
4585 s = defaults.info_fmt;
4586 else
4587 s = ARG_STRING(0);
4588 buf = sbuf_new (0);
4589 format_string (s, win_elem, buf);
4590 ret = cmdret_new (RET_SUCCESS, "%s", sbuf_get (buf));
4591 sbuf_free (buf);
4592 return ret;
4593 }
4594 }
4595
4596 return cmdret_new (RET_SUCCESS, "No window.");
4597 }
4598
4599 /* Thanks to Gergely Nagy <[email protected]> for the original
4600 patch. */
4601 cmdret *
4602 cmd_lastmsg (int interactive UNUSED, struct cmdarg **args UNUSED)
4603 {
4604 show_last_message();
4605 return cmdret_new (RET_SUCCESS, NULL);
4606 }
4607
4608 cmdret *
4609 cmd_focusup (int interactive UNUSED, struct cmdarg **args UNUSED)
4610 {
4611 rp_frame *frame;
4612
4613 if ((frame = find_frame_up (current_frame())))
4614 set_active_frame (frame, 0);
4615 else
4616 show_frame_indicator(0);
4617
4618 return cmdret_new (RET_SUCCESS, NULL);
4619 }
4620
4621 cmdret *
4622 cmd_focusdown (int interactive UNUSED, struct cmdarg **args UNUSED)
4623 {
4624 rp_frame *frame;
4625
4626 if ((frame = find_frame_down (current_frame())))
4627 set_active_frame (frame, 0);
4628 else
4629 show_frame_indicator(0);
4630
4631 return cmdret_new (RET_SUCCESS, NULL);
4632 }
4633
4634 cmdret *
4635 cmd_focusleft (int interactive UNUSED, struct cmdarg **args UNUSED)
4636 {
4637 rp_frame *frame;
4638
4639 if ((frame = find_frame_left (current_frame())))
4640 set_active_frame (frame, 0);
4641 else
4642 show_frame_indicator(0);
4643
4644 return cmdret_new (RET_SUCCESS, NULL);
4645 }
4646
4647 cmdret *
4648 cmd_focusright (int interactive UNUSED, struct cmdarg **args UNUSED)
4649 {
4650 rp_frame *frame;
4651
4652 if ((frame = find_frame_right (current_frame())))
4653 set_active_frame (frame, 0);
4654 else
4655 show_frame_indicator(0);
4656
4657 return cmdret_new (RET_SUCCESS, NULL);
4658 }
4659
4660 cmdret *
4661 cmd_exchangeup (int interactive UNUSED, struct cmdarg **args UNUSED)
4662 {
4663 rp_frame *frame;
4664
4665 if ((frame = find_frame_up (current_frame())))
4666 exchange_with_frame (current_frame(), frame);
4667
4668 return cmdret_new (RET_SUCCESS, NULL);
4669 }
4670
4671 cmdret *
4672 cmd_exchangedown (int interactive UNUSED, struct cmdarg **args UNUSED)
4673 {
4674 rp_frame *frame;
4675
4676 if ((frame = find_frame_down (current_frame())))
4677 exchange_with_frame (current_frame(), frame);
4678
4679 return cmdret_new (RET_SUCCESS, NULL);
4680 }
4681
4682 cmdret *
4683 cmd_exchangeleft (int interactive UNUSED, struct cmdarg **args UNUSED)
4684 {
4685 rp_frame *frame;
4686
4687 if ((frame = find_frame_left (current_frame())))
4688 exchange_with_frame (current_frame(), frame);
4689
4690 return cmdret_new (RET_SUCCESS, NULL);
4691 }
4692
4693 cmdret *
4694 cmd_exchangeright (int interactive UNUSED, struct cmdarg **args UNUSED)
4695 {
4696 rp_frame *frame;
4697
4698 if ((frame = find_frame_right (current_frame())))
4699 exchange_with_frame (current_frame(), frame);
4700
4701 return cmdret_new (RET_SUCCESS, NULL);
4702 }
4703
4704 cmdret *
4705 cmd_swap (int interactive UNUSED, struct cmdarg **args)
4706 {
4707 rp_screen *s;
4708 rp_frame *dest_frame;
4709 rp_frame *src_frame;
4710
4711 dest_frame = ARG(0, frame);
4712 src_frame = args[1] ? ARG (1, frame) : current_frame();
4713
4714 if (!rp_have_xrandr)
4715 {
4716 s = frames_screen(src_frame);
4717 if (screen_find_frame_by_frame(s, dest_frame) == NULL)
4718 return cmdret_new (RET_FAILURE, "swap: frames on different screens");
4719 }
4720
4721 exchange_with_frame (src_frame, dest_frame);
4722
4723 return cmdret_new (RET_SUCCESS, NULL);
4724 }
4725
4726 cmdret *
4727 cmd_restart (int interactive UNUSED, struct cmdarg **args UNUSED)
4728 {
4729 hup_signalled = 1;
4730 return cmdret_new (RET_SUCCESS, NULL);
4731 }
4732
4733 static cmdret *
4734 set_startupmessage (struct cmdarg **args)
4735 {
4736 if (args[0] == NULL)
4737 return cmdret_new (RET_SUCCESS, "%d", defaults.startup_message);
4738
4739 if (ARG(0, number) != 0 && ARG(0, number) != 1)
4740 return cmdret_new (RET_FAILURE, "set startupmessage: invalid argument");
4741
4742 defaults.startup_message = ARG(0, number);
4743 return cmdret_new (RET_SUCCESS, NULL);
4744 }
4745
4746 /* compat */
4747 cmdret *
4748 cmd_startup_message (int interactive UNUSED, struct cmdarg **args)
4749 {
4750 PRINT_WARNING (("command \"startup_message\" is deprecated, "
4751 "use \"set startupmessage\" instead\n"));
4752 if (args[0] == NULL)
4753 return cmdret_new (RET_SUCCESS, "%s", defaults.startup_message ? "on":"off");
4754
4755 if (!strcasecmp (ARG_STRING(0), "on"))
4756 defaults.startup_message = 1;
4757 else if (!strcasecmp (ARG_STRING(0), "off"))
4758 defaults.startup_message = 0;
4759 else
4760 return cmdret_new (RET_FAILURE, "startup_message: invalid argument");
4761
4762 return cmdret_new (RET_SUCCESS, NULL);
4763 }
4764
4765 cmdret *
4766 cmd_focuslast (int interactive UNUSED, struct cmdarg **args UNUSED)
4767 {
4768 rp_frame *frame = find_last_frame();
4769
4770 if (frame)
4771 set_active_frame (frame, 0);
4772 else
4773 return cmdret_new (RET_FAILURE, "focuslast: no other frame");
4774
4775 return cmdret_new (RET_SUCCESS, NULL);
4776 }
4777
4778 cmdret *
4779 cmd_link (int interactive, struct cmdarg **args)
4780 {
4781 char *cmd = NULL;
4782 rp_keymap *map;
4783
4784 if (args[1])
4785 map = ARG(1,keymap);
4786 else
4787 map = find_keymap (ROOT_KEYMAP);
4788
4789 cmd = resolve_command_from_keydesc (args[0]->string, 0, map);
4790 if (cmd)
4791 return command (interactive, cmd);
4792
4793 return cmdret_new (RET_SUCCESS, NULL);
4794 }
4795
4796 /* Thanks to Doug Kearns <[email protected]> for the
4797 original patch. */
4798 static cmdret *
4799 set_barpadding (struct cmdarg **args)
4800 {
4801 int x, y;
4802
4803 if (args[0] == NULL)
4804 return cmdret_new (RET_SUCCESS, "%d %d", defaults.bar_x_padding, defaults.bar_y_padding);
4805
4806 x = ARG(0,number);
4807 y = ARG(1,number);
4808
4809 if (x < 0 || y < 0)
4810 return cmdret_new (RET_FAILURE, "set barpadding: %s", invalid_negative_arg);
4811
4812 defaults.bar_x_padding = x;
4813 defaults.bar_y_padding = y;
4814 return cmdret_new (RET_SUCCESS, NULL);
4815 }
4816
4817 cmdret *
4818 cmd_alias (int interactive UNUSED, struct cmdarg **args)
4819 {
4820 /* Add or update the alias. */
4821 add_alias (ARG_STRING(0), ARG_STRING(1));
4822 return cmdret_new (RET_SUCCESS, NULL);
4823 }
4824
4825 cmdret *
4826 cmd_unalias (int interactive UNUSED, struct cmdarg **args)
4827 {
4828 int i;
4829
4830 /* Are we updating an existing alias, or creating a new one? */
4831 i = find_alias_index (ARG_STRING(0));
4832 if (i >= 0)
4833 {
4834 char *tmp;
4835
4836 alias_list_last--;
4837
4838 /* Free the alias and put the last alias in the the space to
4839 keep alias_list from becoming sparse. This code must jump
4840 through some hoops to correctly handle the case when
4841 alias_list_last == i. */
4842 tmp = alias_list[i].alias;
4843 alias_list[i].alias = xstrdup (alias_list[alias_list_last].alias);
4844 free (tmp);
4845 free (alias_list[alias_list_last].alias);
4846
4847 /* Do the same for the name element. */
4848 tmp = alias_list[i].name;
4849 alias_list[i].name = xstrdup (alias_list[alias_list_last].name);
4850 free (tmp);
4851 free (alias_list[alias_list_last].name);
4852 }
4853 else
4854 return cmdret_new (RET_SUCCESS, "unalias: alias not found");
4855
4856 return cmdret_new (RET_SUCCESS, NULL);
4857 }
4858
4859 cmdret *
4860 cmd_nextscreen (int interactive UNUSED, struct cmdarg **args UNUSED)
4861 {
4862 rp_screen *new_screen;
4863 rp_frame *new_frame;
4864
4865 new_screen = screen_next ();
4866
4867 /* No need to go through the motions when we don't have to. */
4868 if (screen_count() <= 1 || new_screen == rp_current_screen)
4869 return cmdret_new (RET_FAILURE, "nextscreen: no other screen");
4870
4871 new_frame = screen_get_frame (new_screen, new_screen->current_frame);
4872
4873 set_active_frame (new_frame, 1);
4874
4875 return cmdret_new (RET_SUCCESS, NULL);
4876 }
4877
4878 cmdret *
4879 cmd_prevscreen (int interactive UNUSED, struct cmdarg **args UNUSED)
4880 {
4881 rp_screen *new_screen;
4882 rp_frame *new_frame;
4883
4884 new_screen = screen_prev ();
4885
4886 /* No need to go through the motions when we don't have to. */
4887 if (screen_count () <= 1 || new_screen == rp_current_screen)
4888 return cmdret_new (RET_SUCCESS, "prevscreen: no other screen");
4889
4890 new_frame = screen_get_frame (new_screen, new_screen->current_frame);
4891
4892 set_active_frame (new_frame, 1);
4893
4894 return cmdret_new (RET_SUCCESS, NULL);
4895 }
4896
4897 cmdret *
4898 cmd_sselect(int interactive UNUSED, struct cmdarg **args)
4899 {
4900 int new_screen;
4901 rp_frame *new_frame;
4902 rp_screen *screen;
4903
4904 new_screen = ARG(0,number);
4905 if (new_screen < 0)
4906 return cmdret_new (RET_FAILURE, "sselect: out of range");
4907
4908 screen = screen_number (new_screen);
4909 if (!screen)
4910 return cmdret_new (RET_FAILURE, "sselect: screen %d not found", new_screen);
4911
4912 new_frame = screen_get_frame (screen, screen->current_frame);
4913 set_active_frame (new_frame, 1);
4914
4915 return cmdret_new (RET_SUCCESS, NULL);
4916 }
4917
4918 static cmdret *
4919 set_warp (struct cmdarg **args)
4920 {
4921 if (args[0] == NULL)
4922 return cmdret_new (RET_SUCCESS, "%d", defaults.warp);
4923
4924 if (ARG(0, number) != 0 && ARG(0, number) != 1)
4925 return cmdret_new (RET_FAILURE, "set warp: invalid argument");
4926
4927 defaults.warp = ARG(0, number);
4928 return cmdret_new (RET_SUCCESS, NULL);
4929 }
4930
4931 /* compat */
4932 cmdret *
4933 cmd_warp (int interactive UNUSED, struct cmdarg **args)
4934 {
4935 PRINT_WARNING (("command \"warp\" is deprecated, "
4936 "use \"set warp\" instead\n"));
4937
4938 if (args[0] == NULL)
4939 return cmdret_new (RET_SUCCESS, "%s", defaults.warp ? "on":"off");
4940
4941 if (!strcasecmp (ARG_STRING(0), "on"))
4942 defaults.warp = 1;
4943 else if (!strcasecmp (ARG_STRING(0), "off"))
4944 defaults.warp = 0;
4945 else
4946 return cmdret_new (RET_FAILURE, "warp: invalid argument");
4947
4948 return cmdret_new (RET_SUCCESS, NULL);
4949 }
4950
4951 static void
4952 sync_wins (rp_screen *s)
4953 {
4954 rp_window *win, *wintmp;
4955 XWindowAttributes attr;
4956 unsigned int i, nwins;
4957 Window dw1, dw2, *wins;
4958 XQueryTree(dpy, s->root, &dw1, &dw2, &wins, &nwins);
4959
4960 /* Remove any windows in our cached lists that aren't in the query
4961 tree. These windows have been destroyed. */
4962 list_for_each_entry_safe (win, wintmp, &rp_mapped_window, node)
4963 {
4964 int found;
4965
4966 found = 0;
4967 for (i=0; i<nwins; i++)
4968 {
4969 if (win->w == wins[i])
4970 {
4971 found = 1;
4972 break;
4973 }
4974 }
4975 if (!found)
4976 {
4977 ignore_badwindow++;
4978
4979 /* If, somehow, the window is not withdrawn before it is destroyed,
4980 perform the necessary steps to withdraw the window before it is
4981 unmanaged. */
4982 if (win->state == IconicState)
4983 {
4984 PRINT_DEBUG (("Destroying Iconic Window (%s)\n", window_name (win)));
4985 withdraw_window (win);
4986 }
4987 else if (win->state == NormalState)
4988 {
4989 rp_frame *frame;
4990
4991 PRINT_DEBUG (("Destroying Normal Window (%s)\n", window_name (win)));
4992 frame = find_windows_frame (win);
4993 if (frame)
4994 {
4995 cleanup_frame (frame);
4996 if (frame->number == win->scr->current_frame)
4997 set_active_frame (frame, 0);
4998 }
4999 withdraw_window (win);
5000 }
5001
5002 /* Now that the window is guaranteed to be in the unmapped window
5003 list, we can safely stop managing it. */
5004 unmanage (win);
5005 ignore_badwindow--;
5006 }
5007 }
5008
5009 for (i=0; i<nwins; i++)
5010 {
5011 XGetWindowAttributes(dpy, wins[i], &attr);
5012 if (wins[i] == s->bar_window
5013 || wins[i] == s->key_window
5014 || wins[i] == s->input_window
5015 || wins[i] == s->frame_window
5016 || wins[i] == s->help_window
5017 || attr.override_redirect == True) continue;
5018
5019 /* Find the window in our mapped window list. */
5020 win = find_window_in_list (wins[i], &rp_mapped_window);
5021 if (win)
5022 {
5023 rp_frame *frame;
5024 /* If the window is viewable and it is in a frame, then
5025 maximize it and go to the next window. */
5026 if (attr.map_state == IsViewable)
5027 {
5028 frame = find_windows_frame (win);
5029 if (frame)
5030 {
5031 maximize (win);
5032 }
5033 else
5034 {
5035 hide_window (win);
5036 }
5037 }
5038 else if (attr.map_state == IsUnmapped
5039 && get_state (win) == IconicState)
5040 {
5041 frame = find_windows_frame (win);
5042 if (frame)
5043 {
5044 unhide_window (win);
5045 maximize (win);
5046 }
5047 }
5048 else
5049 {
5050 PRINT_DEBUG (("I don't know what to do...\n"));
5051 }
5052
5053 /* We've handled the window. */
5054 continue;
5055 }
5056
5057 /* Try the unmapped window list. */
5058 win = find_window_in_list (wins[i], &rp_unmapped_window);
5059 if (win)
5060 {
5061 /* If the window is viewable and it is in a frame, then
5062 maximize it and go to the next window. */
5063 if (attr.map_state == IsViewable)
5064 {
5065 /* We need to map it since it's visible now. */
5066 map_window (win);
5067 }
5068 else if (attr.map_state == IsUnmapped
5069 && get_state (win) == IconicState)
5070 {
5071 /* We need to map the window and then hide it. */
5072 map_window (win);
5073 hide_window (win);
5074 }
5075 else
5076 {
5077 PRINT_DEBUG (("I think it's all sync'd up...\n"));
5078 }
5079
5080 /* We've handled the window. */
5081 continue;
5082 }
5083
5084 /* The window isn't in the mapped or unmapped window list so add
5085 it. */
5086 win = add_to_window_list (s, wins[i]);
5087
5088 /* If it's visible or iconized. "Map" it. */
5089 if (attr.map_state == IsViewable
5090 || (attr.map_state == IsUnmapped
5091 && get_state (win) == IconicState))
5092 map_window (win);
5093 }
5094
5095 }
5096
5097 static int tmpwm_error_raised = 0;
5098
5099 static int
5100 tmpwm_error_handler (Display *d UNUSED, XErrorEvent *e)
5101 {
5102 if (e->request_code == X_ChangeWindowAttributes && e->error_code == BadAccess)
5103 {
5104 PRINT_DEBUG (("failed to grab root properties\n"));
5105 tmpwm_error_raised++;
5106 }
5107 return 0;
5108 }
5109
5110 /* Temporarily give control over to another window manager, reclaiming */
5111 /* control when that WM terminates. */
5112 cmdret *
5113 cmd_tmpwm (int interactive UNUSED, struct cmdarg **args)
5114 {
5115 struct list_head *tmp, *iter;
5116 rp_window *win = NULL;
5117 rp_screen *cur_screen;
5118 int child;
5119 int status;
5120 int pid;
5121 int (*old_handler)(Display *, XErrorEvent *);
5122
5123 push_frame_undo (rp_current_screen); /* fdump to stack */
5124
5125 /* Release event selection on the root windows, so the new WM can
5126 have it. */
5127
5128 list_for_each_entry (cur_screen, &rp_screens, node)
5129 {
5130 XSelectInput (dpy, RootWindow (dpy, cur_screen->screen_num), 0);
5131 deactivate_screen (cur_screen);
5132 }
5133
5134 /* Ungrab all our keys. */
5135 ungrab_keys_all_wins();
5136
5137 /* Don't listen for any events from any window. */
5138 list_for_each_safe_entry (win, iter, tmp, &rp_mapped_window, node)
5139 {
5140 unhide_window (win);
5141 maximize (win);
5142 XSelectInput (dpy, win->w, 0);
5143 }
5144
5145 list_for_each_safe_entry (win, iter, tmp, &rp_unmapped_window, node)
5146 XSelectInput (dpy, win->w, 0);
5147
5148 XSync (dpy, False);
5149
5150 /* Disable our SIGCHLD handler */
5151 set_sig_handler (SIGCHLD, SIG_DFL);
5152
5153 /* Launch the new WM and wait for it to terminate. */
5154 pid = spawn (ARG_STRING(0), 0, NULL);
5155 PRINT_DEBUG (("spawn pid: %d\n", pid));
5156 do
5157 {
5158 child = waitpid (pid, &status, 0);
5159 } while (child != -1 && child != pid);
5160
5161 /* Enable our SIGCHLD handler */
5162 set_sig_handler (SIGCHLD, chld_handler);
5163
5164 /* Some processes may have quit while our sigchld handler was
5165 disabled, so check for them. */
5166 check_child_procs();
5167
5168 /* Enable the event selection on the root window. We need to loop
5169 until we don't get an X error. This is due to a race between the
5170 X server cleaning up after the temporary wm and ratpoison
5171 grabbing events. */
5172 old_handler = XSetErrorHandler (tmpwm_error_handler);
5173 do {
5174 tmpwm_error_raised = 0;
5175
5176 list_for_each_entry (cur_screen, &rp_screens, node)
5177 {
5178 XSelectInput (dpy, RootWindow (dpy, cur_screen->screen_num),
5179 PropertyChangeMask | ColormapChangeMask
5180 | SubstructureRedirectMask | SubstructureNotifyMask
5181 | StructureNotifyMask);
5182 XSync (dpy, False);
5183 }
5184
5185 if (tmpwm_error_raised)
5186 sleep(1);
5187 } while (tmpwm_error_raised);
5188
5189 XSetErrorHandler (old_handler);
5190
5191 list_for_each_entry (cur_screen, &rp_screens, node)
5192 {
5193 activate_screen (cur_screen);
5194 }
5195
5196 /* Sort through all the windows in each group and pick out the ones
5197 that are unmapped or destroyed. */
5198 list_for_each_entry (cur_screen, &rp_screens, node)
5199 {
5200 sync_wins (cur_screen);
5201 }
5202
5203 /* At this point, new windows have the top level keys grabbed but
5204 existing windows don't. So grab them on all windows just to be
5205 sure. */
5206 grab_keys_all_wins();
5207
5208 /* If no window has focus, give the key_window focus. */
5209 if (current_window())
5210 set_active_window (current_window());
5211 else
5212 set_window_focus (rp_current_screen->key_window);
5213
5214 /* And we're back in ratpoison. */
5215 return cmdret_new (RET_SUCCESS, NULL);
5216 }
5217
5218 /* Return a new string with the frame selector or it as a string if no
5219 selector exists for the number. */
5220
5221 /* Select a frame by number. */
5222 cmdret *
5223 cmd_fselect (int interactive, struct cmdarg **args)
5224 {
5225 set_active_frame (ARG(0,frame), 1);
5226 if (interactive)
5227 return cmdret_new (RET_SUCCESS, NULL);
5228 else
5229 return cmdret_new (RET_SUCCESS, "%d", ARG(0,frame)->number);
5230 }
5231
5232 static char *
5233 fdump (rp_screen *screen)
5234 {
5235 struct sbuf *dump;
5236 rp_frame *cur;
5237
5238 dump = sbuf_new (0);
5239
5240 list_for_each_entry (cur, &(screen->frames), node)
5241 {
5242 char *frameset;
5243
5244 frameset = frame_dump (cur, screen);
5245 sbuf_concat (dump, frameset);
5246 sbuf_concat (dump, ",");
5247 free (frameset);
5248 }
5249 sbuf_chop (dump);
5250
5251 return sbuf_free_struct (dump);
5252 }
5253
5254 cmdret *
5255 cmd_fdump (int interactively UNUSED, struct cmdarg **args)
5256 {
5257 rp_screen *screen;
5258 cmdret *ret;
5259 char *dump;
5260
5261 if (args[0] == NULL)
5262 screen = rp_current_screen;
5263 else
5264 {
5265 int snum;
5266 snum = ARG(0,number);
5267
5268 if (snum < 0)
5269 return cmdret_new (RET_FAILURE, "fdump: invalid negative screen number");
5270 else
5271 {
5272 screen = screen_number (snum);
5273 if (!screen)
5274 return cmdret_new (RET_FAILURE, "fdump: screen %d not found", snum);
5275 }
5276 }
5277
5278 dump = fdump (screen);
5279 ret = cmdret_new (RET_SUCCESS, "%s", dump);
5280 free (dump);
5281
5282 return ret;
5283 }
5284
5285 static cmdret *
5286 frestore (char *data, rp_screen *s)
5287 {
5288 char *token;
5289 char *d;
5290 rp_frame *new, *cur;
5291 rp_window *win;
5292 struct list_head fset;
5293 int max = -1;
5294 char *nexttok = NULL;
5295
5296 INIT_LIST_HEAD (&fset);
5297
5298 d = xstrdup (data);
5299 token = strtok_r (d, ",", &nexttok);
5300 if (token == NULL)
5301 {
5302 free (d);
5303 return cmdret_new (RET_FAILURE, "frestore: invalid frame format");
5304 }
5305
5306 /* Build the new frame set. */
5307 while (token != NULL)
5308 {
5309 new = frame_read (token, s);
5310 if (new == NULL)
5311 {
5312 free (d);
5313 return cmdret_new (RET_FAILURE, "frestore: invalid frame format");
5314 }
5315 list_add_tail (&new->node, &fset);
5316 token = strtok_r (NULL, ",", &nexttok);
5317 }
5318
5319 free (d);
5320
5321 /* Clear all the frames. */
5322 list_for_each_entry (cur, &s->frames, node)
5323 {
5324 PRINT_DEBUG (("blank %d\n", cur->number));
5325 blank_frame (cur);
5326 }
5327
5328 /* Get rid of the frames' numbers */
5329 screen_free_nums (s);
5330
5331 /* Splice in our new frameset. */
5332 screen_restore_frameset (s, &fset);
5333 /* numset_clear (s->frames_numset); */
5334
5335 /* Process the frames a bit to make sure everything lines up. */
5336 list_for_each_entry (cur, &s->frames, node)
5337 {
5338 PRINT_DEBUG (("restore %d %d\n", cur->number, cur->win_number));
5339
5340 /* Grab the frame's number, but if it already exists request a
5341 new one. */
5342 if (!numset_add_num (s->frames_numset, cur->number)) {
5343 cur->number = numset_request (s->frames_numset);
5344 }
5345
5346 /* Find the current frame based on last_access. */
5347 if (cur->last_access > max)
5348 {
5349 s->current_frame = cur->number;
5350 max = cur->last_access;
5351 }
5352
5353 /* Update the window the frame points to. */
5354 if (cur->win_number != EMPTY)
5355 {
5356 set_frames_window (cur, find_window_number (cur->win_number));
5357 }
5358 }
5359
5360 /* Show the windows in the frames. */
5361 list_for_each_entry (win, &rp_mapped_window, node)
5362 {
5363 if (win->frame_number != EMPTY)
5364 {
5365 maximize (win);
5366 unhide_window (win);
5367 }
5368 }
5369
5370 set_active_frame (current_frame(), 0);
5371 update_bar (s);
5372 show_frame_indicator(0);
5373
5374 PRINT_DEBUG (("Done.\n"));
5375 return cmdret_new (RET_SUCCESS, NULL);
5376 }
5377
5378 cmdret *
5379 cmd_frestore (int interactively UNUSED, struct cmdarg **args)
5380 {
5381 push_frame_undo (rp_current_screen); /* fdump to stack */
5382 return frestore (ARG_STRING(0), rp_current_screen);
5383 }
5384
5385 cmdret *
5386 cmd_verbexec (int interactive UNUSED, struct cmdarg **args)
5387 {
5388 marked_message_printf(0, 0, "Running %s", ARG_STRING(0));
5389 spawn (ARG_STRING(0), 0, current_frame());
5390 return cmdret_new (RET_SUCCESS, NULL);
5391 }
5392
5393 static cmdret *
5394 set_winliststyle (struct cmdarg **args)
5395 {
5396 if (args[0] == NULL)
5397 return cmdret_new (RET_SUCCESS, "%s", defaults.window_list_style ? "column":"row");
5398
5399 if (!strcmp ("column", ARG_STRING(0)))
5400 defaults.window_list_style = STYLE_COLUMN;
5401 else if (!strcmp ("row", ARG_STRING(0)))
5402 defaults.window_list_style = STYLE_ROW;
5403 else
5404 return cmdret_new (RET_FAILURE, "set winliststyle: invalid argument");
5405
5406 return cmdret_new (RET_SUCCESS, NULL);
5407 }
5408
5409 cmdret *
5410 cmd_gnext (int interactive UNUSED, struct cmdarg **args UNUSED)
5411 {
5412 set_current_group (group_next_group ());
5413 return cmdret_new (RET_SUCCESS, NULL);
5414 }
5415
5416 cmdret *
5417 cmd_gprev (int interactive UNUSED, struct cmdarg **args UNUSED)
5418 {
5419 set_current_group (group_prev_group ());
5420 return cmdret_new (RET_SUCCESS, NULL);
5421 }
5422
5423 cmdret *
5424 cmd_gother (int interactive UNUSED, struct cmdarg **args UNUSED)
5425 {
5426 set_current_group (group_last_group ());
5427 return cmdret_new (RET_SUCCESS, NULL);
5428 }
5429
5430 cmdret *
5431 cmd_gnew (int interactive UNUSED, struct cmdarg **args)
5432 {
5433 if (groups_find_group_by_name (ARG_STRING (0), 1))
5434 return cmdret_new (RET_FAILURE, "gnew: group already exists");
5435 set_current_group (group_add_new_group (ARG_STRING(0)));
5436 return cmdret_new (RET_SUCCESS, NULL);
5437 }
5438
5439 cmdret *
5440 cmd_gnewbg (int interactive UNUSED, struct cmdarg **args)
5441 {
5442 if (groups_find_group_by_name (ARG_STRING (0), 1))
5443 return cmdret_new (RET_FAILURE, "gnewbg: group already exists");
5444 group_add_new_group (ARG_STRING(0));
5445 return cmdret_new (RET_SUCCESS, NULL);
5446 }
5447
5448 cmdret *
5449 cmd_gnumber (int interactive UNUSED, struct cmdarg **args)
5450 {
5451 int old_number, new_number;
5452 rp_group *other_g, *g;
5453
5454 struct numset *g_numset = group_get_numset();
5455
5456 /* Gather the args. */
5457 new_number = ARG(0,number);
5458 if (args[1])
5459 g = groups_find_group_by_number (ARG(1,number));
5460 else
5461 g = rp_current_group;
5462
5463 /* Make the switch. */
5464 if (new_number >= 0 && g)
5465 {
5466 /* Find other window with same number and give it old number. */
5467 other_g = groups_find_group_by_number (new_number);
5468 if (other_g != NULL)
5469 {
5470 old_number = g->number;
5471 other_g->number = old_number;
5472
5473 /* Resort the window in the list */
5474 group_resort_group (other_g);
5475 }
5476 else
5477 {
5478 numset_release (g_numset, g->number);
5479 }
5480
5481 g->number = new_number;
5482 numset_add_num (g_numset, new_number);
5483
5484 /* resort the the window in the list */
5485 group_resort_group (g);
5486
5487 /* Update the group list. */
5488 update_group_names (rp_current_screen);
5489 }
5490
5491 return cmdret_new (RET_SUCCESS, NULL);
5492 }
5493
5494 cmdret *
5495 cmd_grename (int interactive UNUSED, struct cmdarg **args)
5496 {
5497 if (groups_find_group_by_name (ARG_STRING (0), 1))
5498 return cmdret_new (RET_FAILURE, "grename: duplicate group name");
5499 group_rename (rp_current_group, ARG_STRING(0));
5500
5501 /* Update the group list. */
5502 update_group_names (rp_current_screen);
5503
5504 return cmdret_new (RET_SUCCESS, NULL);
5505 }
5506
5507 cmdret *
5508 cmd_gselect (int interactive, struct cmdarg **args)
5509 {
5510 rp_group *g;
5511
5512 g = find_group (ARG_STRING(0));
5513
5514 if (g)
5515 set_current_group (g);
5516 else
5517 return cmd_groups (interactive, NULL);
5518
5519 return cmdret_new (RET_SUCCESS, NULL);
5520 }
5521
5522 /* Show all the groups, with the current one highlighted. */
5523 cmdret *
5524 cmd_groups (int interactive, struct cmdarg **args UNUSED)
5525 {
5526 struct sbuf *group_list = NULL;
5527 int dummy;
5528 rp_screen *s;
5529
5530 if (interactive)
5531 {
5532 s = rp_current_screen;
5533 /* This is a yukky hack. If the bar already hidden then show the
5534 bar. This handles the case when msgwait is 0 (the bar sticks)
5535 and the user uses this command to toggle the bar on and
5536 off. OR the timeout is >0 then show the bar. Which means,
5537 always show the bar if msgwait is >0 which fixes the case
5538 when a command in the prefix hook displays the bar. */
5539 if (!hide_bar (s) || defaults.bar_timeout > 0) show_group_bar (s);
5540
5541 return cmdret_new (RET_SUCCESS, NULL);
5542 }
5543 else
5544 {
5545 cmdret *ret;
5546
5547 group_list = sbuf_new (0);
5548 get_group_list ("\n", group_list, &dummy, &dummy);
5549 ret = cmdret_new (RET_SUCCESS, "%s", sbuf_get (group_list));
5550 sbuf_free (group_list);
5551 return ret;
5552 }
5553 }
5554
5555 /* Move a window to a different group. */
5556 cmdret *
5557 cmd_gmove (int interactive UNUSED, struct cmdarg **args)
5558 {
5559 if (current_window() == NULL)
5560 return cmdret_new (RET_FAILURE, "gmove: no focused window");
5561
5562 group_move_window (ARG(0,group), current_window());
5563 return cmdret_new (RET_SUCCESS, NULL);
5564 }
5565
5566 cmdret *
5567 cmd_gmerge (int interactive UNUSED, struct cmdarg **args)
5568 {
5569 groups_merge (ARG(0,group), rp_current_group);
5570 return cmdret_new (RET_SUCCESS, NULL);
5571 }
5572
5573 cmdret *
5574 cmd_addhook (int interactive UNUSED, struct cmdarg **args)
5575 {
5576 struct list_head *hook;
5577 struct sbuf *cmd;
5578
5579 hook = hook_lookup (ARG_STRING(0));
5580 if (hook == NULL)
5581 return cmdret_new (RET_FAILURE, "addhook: unknown hook '%s'", ARG_STRING(0));
5582
5583 /* Add the command to the hook */
5584 cmd = sbuf_new (0);
5585 sbuf_copy (cmd, ARG_STRING(1));
5586 hook_add (hook, cmd);
5587
5588 return cmdret_new (RET_SUCCESS, NULL);
5589 }
5590
5591 cmdret *
5592 cmd_remhook (int interactive UNUSED, struct cmdarg **args)
5593 {
5594 struct sbuf *cmd;
5595
5596 /* Remove the command from the hook */
5597 cmd = sbuf_new (0);
5598 sbuf_copy (cmd, ARG_STRING(1));
5599 hook_remove (ARG(0,hook), cmd);
5600 sbuf_free (cmd);
5601
5602 return cmdret_new (RET_SUCCESS, NULL);
5603 }
5604
5605 cmdret *
5606 cmd_listhook (int interactive UNUSED, struct cmdarg **args)
5607 {
5608 cmdret *ret;
5609 struct sbuf *buffer;
5610 struct list_head *hook;
5611 struct sbuf *cur;
5612
5613 hook = hook_lookup (ARG_STRING(0));
5614 if (hook == NULL)
5615 return cmdret_new (RET_FAILURE, "listhook: unknown hook '%s'", ARG_STRING(0));
5616
5617 if (list_empty(hook))
5618 return cmdret_new (RET_FAILURE, " Nothing defined for %s ", ARG_STRING(0));
5619
5620 buffer = sbuf_new(0);
5621
5622 list_for_each_entry (cur, hook, node)
5623 {
5624 sbuf_printf_concat(buffer, "%s", sbuf_get (cur));
5625 if (cur->node.next != hook)
5626 sbuf_printf_concat(buffer, "\n");
5627 }
5628
5629 ret = cmdret_new (RET_SUCCESS, "%s", sbuf_get (buffer));
5630 sbuf_free (buffer);
5631 return ret;
5632 }
5633
5634 cmdret *
5635 cmd_gdelete (int interactive UNUSED, struct cmdarg **args)
5636 {
5637 rp_group *g;
5638
5639 if (args[0] == NULL)
5640 g = rp_current_group;
5641 else
5642 g = ARG(0,group);
5643
5644 switch (group_delete_group (g))
5645 {
5646 case GROUP_DELETE_GROUP_OK:
5647 break;
5648 case GROUP_DELETE_GROUP_NONEMPTY:
5649 return cmdret_new (RET_FAILURE, "gdelete: non-empty group");
5650 break;
5651 case GROUP_DELETE_LAST_GROUP:
5652 return cmdret_new (RET_FAILURE, "gdelete: cannot delete the sole group");
5653 break;
5654 default:
5655 return cmdret_new (RET_FAILURE, "gdelete: unknown return code (this shouldn't happen)");
5656 }
5657
5658 return cmdret_new (RET_SUCCESS, NULL);
5659 }
5660
5661 cmdret *
5662 cmd_readkey (int interactive UNUSED, struct cmdarg **args)
5663 {
5664 char *keysym_name;
5665 rp_action *key_action;
5666 KeySym keysym; /* Key pressed */
5667 unsigned int mod; /* Modifiers */
5668 int rat_grabbed = 0;
5669 rp_keymap *map;
5670
5671 map = ARG(0,keymap);
5672
5673 /* Change the mouse icon to indicate to the user we are waiting for
5674 more keystrokes */
5675 if (defaults.wait_for_key_cursor)
5676 {
5677 grab_rat();
5678 rat_grabbed = 1;
5679 }
5680
5681 read_single_key (&keysym, &mod, NULL, 0);
5682
5683 if (rat_grabbed)
5684 ungrab_rat();
5685
5686 if ((key_action = find_keybinding (keysym, x11_mask_to_rp_mask (mod), map)))
5687 {
5688 return command (1, key_action->data);
5689 }
5690 else
5691 {
5692 cmdret *ret;
5693 /* No key match, notify user. */
5694 keysym_name = keysym_to_string (keysym, x11_mask_to_rp_mask (mod));
5695 ret = cmdret_new (RET_FAILURE, "readkey: unbound key '%s'", keysym_name);
5696 free (keysym_name);
5697 return ret;
5698 }
5699 }
5700
5701 cmdret *
5702 cmd_newkmap (int interactive UNUSED, struct cmdarg **args)
5703 {
5704 rp_keymap *map;
5705
5706 map = find_keymap (ARG_STRING(0));
5707 if (map)
5708 return cmdret_new (RET_FAILURE, "newkmap: keymap '%s' already exists", ARG_STRING(0));
5709
5710 map = keymap_new (ARG_STRING(0));
5711 list_add_tail (&map->node, &rp_keymaps);
5712
5713 return cmdret_new (RET_SUCCESS, NULL);
5714 }
5715
5716 cmdret *
5717 cmd_delkmap (int interactive UNUSED, struct cmdarg **args)
5718 {
5719 rp_keymap *map, *top, *root;
5720
5721 top = find_keymap (defaults.top_kmap);
5722 root = find_keymap (ROOT_KEYMAP);
5723
5724 map = ARG(0,keymap);
5725 if (map == root || map == top)
5726 return cmdret_new (RET_FAILURE, "delkmap: cannot delete keymap '%s'", ARG_STRING(0));
5727
5728 list_del (&map->node);
5729
5730 return cmdret_new (RET_SUCCESS, NULL);
5731 }
5732
5733 static cmdret *
5734 set_framesels (struct cmdarg **args)
5735 {
5736 if (args[0] == NULL)
5737 return cmdret_new (RET_SUCCESS, "%s", defaults.frame_selectors);
5738
5739 free (defaults.frame_selectors);
5740 defaults.frame_selectors = xstrdup (ARG_STRING(0));
5741 return cmdret_new (RET_SUCCESS, NULL);
5742 }
5743
5744 cmdret *
5745 cmd_set (int interactive UNUSED, struct cmdarg **args)
5746 {
5747 if (args[0] == NULL)
5748 {
5749 /* List all the settings. */
5750 cmdret *ret;
5751 struct sbuf *s = sbuf_new(0);
5752 struct set_var *cur, *last;
5753
5754 list_last (last, &set_vars, node);
5755 list_for_each_entry (cur, &set_vars, node)
5756 {
5757 cmdret *r;
5758 r = cur->set_fn (args);
5759 sbuf_printf_concat (s, "%s: %s", cur->var, r->output);
5760 /* Skip a newline on the last line. */
5761 if (cur != last)
5762 sbuf_concat (s, "\n");
5763 cmdret_free (r);
5764 }
5765
5766 /* Return the accumulated string. */
5767 ret = cmdret_new (RET_SUCCESS, "%s", sbuf_get (s));
5768 sbuf_free (s);
5769 return ret;
5770 }
5771 else
5772 {
5773 struct sbuf *scur;
5774 struct cmdarg *acur;
5775 struct list_head *iter, *tmp;
5776 struct list_head head, arglist;
5777 int i, nargs = 0, raw = 0;
5778 int parsed_args;
5779 cmdret *result = NULL;
5780 struct cmdarg **cmdargs;
5781 char *input;
5782
5783 INIT_LIST_HEAD (&arglist);
5784 INIT_LIST_HEAD (&head);
5785
5786 /* We need to tell parse_args about arg_REST and arg_SHELLCMD. */
5787 for (i=0; i<ARG(0,variable)->nargs; i++)
5788 if (ARG(0,variable)->args[i].type == arg_REST
5789 || ARG(0,variable)->args[i].type == arg_COMMAND
5790 || ARG(0,variable)->args[i].type == arg_SHELLCMD
5791 || ARG(0,variable)->args[i].type == arg_RAW)
5792 {
5793 raw = 1;
5794 nargs = i;
5795 break;
5796 }
5797
5798 /* Parse the arguments and call the function. */
5799 if (args[1])
5800 input = xstrdup (args[1]->string);
5801 else
5802 input = xstrdup ("");
5803 result = parse_args (input, &head, nargs, raw);
5804 free (input);
5805
5806 if (result)
5807 goto failed;
5808 result = parsed_input_to_args (ARG(0,variable)->nargs, ARG(0,variable)->args,
5809 &head, &arglist, &parsed_args, NULL);
5810 if (result)
5811 goto failed;
5812 /* 0 or nargs is acceptable */
5813 if (list_size (&arglist) > 0 && list_size (&arglist) < ARG(0,variable)->nargs)
5814 {
5815 result = cmdret_new (RET_FAILURE, "not enough arguments.");
5816 goto failed;
5817 }
5818 else if (list_size (&head) > ARG(0,variable)->nargs)
5819 {
5820 result = cmdret_new (RET_FAILURE, "too many arguments.");
5821 goto failed;
5822 }
5823
5824 cmdargs = arg_array (&arglist);
5825 result = ARG(0,variable)->set_fn (cmdargs);
5826 free (cmdargs);
5827
5828 /* Free the lists. */
5829 failed:
5830 /* Free the parsed strings */
5831 list_for_each_safe_entry (scur, iter, tmp, &head, node)
5832 sbuf_free(scur);
5833 /* Free the args */
5834 list_for_each_safe_entry (acur, iter, tmp, &arglist, node)
5835 arg_free (acur);
5836
5837 return result;
5838 }
5839 }
5840
5841 cmdret *
5842 cmd_sfdump (int interactively UNUSED, struct cmdarg **args UNUSED)
5843 {
5844 char screen_suffix[16];
5845 cmdret *ret;
5846 struct sbuf *dump;
5847 rp_frame *cur_frame;
5848 rp_screen *cur_screen;
5849
5850 dump = sbuf_new (0);
5851
5852 list_for_each_entry (cur_screen, &rp_screens, node)
5853 {
5854 snprintf (screen_suffix, sizeof (screen_suffix), " %d,",
5855 cur_screen->number);
5856
5857 list_for_each_entry (cur_frame, &(cur_screen->frames), node)
5858 {
5859 char *frameset;
5860
5861 frameset = frame_dump (cur_frame, cur_screen);
5862 sbuf_concat (dump, frameset);
5863 sbuf_concat (dump, screen_suffix);
5864 free (frameset);
5865 }
5866 }
5867
5868 sbuf_chop (dump);
5869 ret = cmdret_new (RET_SUCCESS, "%s", sbuf_get (dump));
5870 sbuf_free (dump);
5871 return ret;
5872 }
5873
5874 cmdret *
5875 cmd_sfrestore (int interactively UNUSED, struct cmdarg **args)
5876 {
5877 char *copy, *ptr, *token;
5878 rp_screen *screen;
5879 int out_of_screen = 0, restored = 0;
5880
5881 list_for_each_entry (screen, &rp_screens, node)
5882 {
5883 sbuf_free (screen->scratch_buffer);
5884 screen->scratch_buffer = sbuf_new (0);
5885 }
5886
5887 copy = xstrdup (ARG_STRING (0));
5888
5889 token = strtok (copy, ",");
5890 if (token == NULL)
5891 {
5892 free (copy);
5893 return cmdret_new (RET_FAILURE, "sfrestore: invalid frame format");
5894 }
5895
5896 while (token != NULL)
5897 {
5898 int snum;
5899
5900 /* search for end of frameset */
5901 ptr = token;
5902 while (*ptr != ')')
5903 ptr++;
5904 ptr++;
5905
5906 snum = string_to_positive_int (ptr);
5907 screen = screen_number (snum);
5908 if (screen)
5909 {
5910 /* clobber screen number here, frestore() doesn't need it */
5911 *ptr = '\0';
5912 sbuf_concat (screen->scratch_buffer, token);
5913 sbuf_concat (screen->scratch_buffer, ",");
5914 }
5915 else
5916 out_of_screen++;
5917
5918 /* continue with next frameset */
5919 token = strtok (NULL, ",");
5920 }
5921
5922 free (copy);
5923
5924 /* now restore the frames for each screen */
5925 list_for_each_entry (screen, &rp_screens, node)
5926 {
5927 cmdret *ret;
5928
5929 if (strlen (sbuf_get (screen->scratch_buffer)) == 0)
5930 continue;
5931
5932 push_frame_undo (screen); /* fdump to stack */
5933
5934 /* XXX save the failure of each frestore and display it in case of error */
5935 ret = frestore (sbuf_get (screen->scratch_buffer), screen);
5936 if (ret->success)
5937 restored++;
5938 cmdret_free (ret);
5939
5940 sbuf_free (screen->scratch_buffer);
5941 screen->scratch_buffer = NULL;
5942 }
5943
5944 if (!out_of_screen)
5945 return cmdret_new (RET_SUCCESS, "screens restored: %d", restored);
5946 else
5947 return cmdret_new (RET_SUCCESS,
5948 "screens restored: %d, frames out of screen: %d",
5949 restored, out_of_screen);
5950 }
5951
5952 cmdret *
5953 cmd_sdump (int interactive UNUSED, struct cmdarg **args UNUSED)
5954 {
5955 cmdret *ret;
5956 struct sbuf *s;
5957 char *tmp;
5958 rp_screen *cur_screen;
5959
5960 s = sbuf_new (0);
5961 list_for_each_entry (cur_screen, &rp_screens, node)
5962 {
5963 tmp = screen_dump (cur_screen);
5964 sbuf_concat (s, tmp);
5965 sbuf_concat (s, ",");
5966 free (tmp);
5967 }
5968 sbuf_chop (s);
5969
5970 ret = cmdret_new (RET_SUCCESS, "%s", sbuf_get (s));
5971 sbuf_free (s);
5972 return ret;
5973 }
5974
5975 static cmdret *
5976 set_maxundos (struct cmdarg **args)
5977 {
5978 rp_frame_undo *cur;
5979
5980 if (args[0] == NULL)
5981 return cmdret_new (RET_SUCCESS, "%d", defaults.maxundos);
5982
5983 if (ARG(0,number) < 0)
5984 return cmdret_new (RET_FAILURE, "set maxundos: %s", invalid_negative_arg);
5985
5986 defaults.maxundos = ARG(0,number);
5987
5988 /* Delete any superfluous undos */
5989 while (list_size (&rp_frame_undos) > defaults.maxundos)
5990 {
5991 /* Delete the oldest node */
5992 list_last (cur, &rp_frame_undos, node);
5993 del_frame_undo (cur);
5994 }
5995
5996 return cmdret_new (RET_SUCCESS, NULL);
5997 }
5998
5999 cmdret *
6000 cmd_cnext (int interactive, struct cmdarg **args)
6001 {
6002 rp_window *cur, *last, *win;
6003
6004 cur = current_window();
6005 if (!cur || !cur->res_class) /* Can't be done. */
6006 return cmd_next (interactive, args);
6007
6008 /* CUR !in cycle list, so LAST marks last node. */
6009 last = group_prev_window (rp_current_group, cur);
6010
6011 if (last)
6012 for (win = group_next_window (rp_current_group, cur);
6013 win;
6014 win = group_next_window (rp_current_group, win))
6015 {
6016 if (win->res_class
6017 && strcmp (cur->res_class, win->res_class))
6018 {
6019 set_active_window_force (win);
6020 return cmdret_new (RET_SUCCESS, NULL);
6021 }
6022
6023 if (win == last) break;
6024 }
6025
6026 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_WINDOW);
6027 }
6028
6029 cmdret *
6030 cmd_cprev (int interactive, struct cmdarg **args)
6031 {
6032 rp_window *cur, *last, *win;
6033
6034 cur = current_window();
6035 if (!cur || !cur->res_class) /* Can't be done. */
6036 return cmd_next (interactive, args);
6037
6038 /* CUR !in cycle list, so LAST marks last node. */
6039 last = group_next_window (rp_current_group, cur);
6040
6041 if (last)
6042 for (win = group_prev_window (rp_current_group, cur);
6043 win;
6044 win = group_prev_window (rp_current_group, win))
6045 {
6046 if (win->res_class
6047 && strcmp (cur->res_class, win->res_class))
6048 {
6049 set_active_window_force (win);
6050 return cmdret_new (RET_SUCCESS, NULL);
6051 }
6052
6053 if (win == last) break;
6054 }
6055
6056 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_WINDOW);
6057 }
6058
6059 cmdret *
6060 cmd_inext (int interactive, struct cmdarg **args)
6061 {
6062 rp_window *cur, *last, *win;
6063
6064 cur = current_window();
6065 if (!cur || !cur->res_class) /* Can't be done. */
6066 return cmd_next (interactive, args);
6067
6068 /* CUR !in cycle list, so LAST marks last node. */
6069 last = group_prev_window (rp_current_group, cur);
6070
6071 if (last)
6072 for (win = group_next_window (rp_current_group, cur);
6073 win;
6074 win = group_next_window (rp_current_group, win))
6075 {
6076 if (win->res_class
6077 && !strcmp (cur->res_class, win->res_class))
6078 {
6079 set_active_window_force (win);
6080 return cmdret_new (RET_SUCCESS, NULL);
6081 }
6082
6083 if (win == last) break;
6084 }
6085
6086 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_WINDOW);
6087 }
6088
6089 cmdret *
6090 cmd_iprev (int interactive, struct cmdarg **args)
6091 {
6092 rp_window *cur, *last, *win;
6093
6094 cur = current_window();
6095 if (!cur || !cur->res_class) /* Can't be done. */
6096 return cmd_next (interactive, args);
6097
6098 /* CUR !in cycle list, so LAST marks last node. */
6099 last = group_next_window (rp_current_group, cur);
6100
6101 if (last)
6102 for (win = group_prev_window (rp_current_group, cur);
6103 win;
6104 win = group_prev_window (rp_current_group, win))
6105 {
6106 if (win->res_class
6107 && !strcmp (cur->res_class, win->res_class))
6108 {
6109 set_active_window_force (win);
6110 return cmdret_new (RET_SUCCESS, NULL);
6111 }
6112
6113 if (win == last) break;
6114 }
6115
6116 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_WINDOW);
6117 }
6118
6119 cmdret *
6120 cmd_cother (int interactive UNUSED, struct cmdarg **args UNUSED)
6121 {
6122 rp_window *cur, *w = NULL;
6123
6124 cur = current_window();
6125 if (cur)
6126 w = group_last_window_by_class (rp_current_group, cur->res_class);
6127
6128 if (!w)
6129 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_WINDOW);
6130 else
6131 set_active_window_force (w);
6132
6133 return cmdret_new (RET_SUCCESS, NULL);
6134 }
6135
6136 cmdret *
6137 cmd_iother (int interactive UNUSED, struct cmdarg **args UNUSED)
6138 {
6139 rp_window *cur, *w = NULL;
6140
6141 cur = current_window();
6142 if (cur)
6143 w = group_last_window_by_class_complement (rp_current_group, cur->res_class);
6144
6145 if (!w)
6146 return cmdret_new (RET_FAILURE, "%s", MESSAGE_NO_OTHER_WINDOW);
6147 else
6148 set_active_window_force (w);
6149
6150 return cmdret_new (RET_SUCCESS, NULL);
6151 }
6152
6153 cmdret *
6154 cmd_undo (int interactive UNUSED, struct cmdarg **args UNUSED)
6155 {
6156 rp_frame_undo *cur;
6157
6158 cur = pop_frame_undo ();
6159 if (!cur)
6160 return cmdret_new (RET_FAILURE, "No more undo information available");
6161 else
6162 {
6163 cmdret *ret;
6164
6165 ret = frestore (cur->frames, cur->screen);
6166 return ret;
6167 }
6168 }
6169
6170 cmdret *
6171 cmd_redo (int interactive UNUSED, struct cmdarg **args UNUSED)
6172 {
6173 rp_frame_undo *cur;
6174
6175 /* The current layout goes on the undo. */
6176
6177 cur = pop_frame_redo ();
6178 if (!cur)
6179 return cmdret_new (RET_FAILURE, "No more redo information available");
6180 else
6181 {
6182 cmdret *ret;
6183
6184 ret = frestore (cur->frames, cur->screen);
6185 return ret;
6186 }
6187 }
6188
6189 cmdret *
6190 cmd_prompt (int interactive UNUSED, struct cmdarg **args)
6191 {
6192 cmdret *ret;
6193 char *output;
6194
6195 if (args[0] == NULL)
6196 output = get_input (MESSAGE_PROMPT_COMMAND, hist_PROMPT,
6197 trivial_completions);
6198 else
6199 {
6200 char *arg_str, *prefix;
6201
6202 arg_str = ARG_STRING(0);
6203 prefix = strchr (arg_str, ':');
6204
6205 if (prefix)
6206 {
6207 struct sbuf *query;
6208
6209 prefix++; /* Don't return the colon. */
6210 query = sbuf_new (prefix - arg_str);
6211 sbuf_nconcat (query, arg_str, prefix - arg_str);
6212 output = get_more_input (sbuf_get (query), prefix, hist_PROMPT,
6213 BASIC, trivial_completions);
6214 sbuf_free (query);
6215 }
6216 else
6217 {
6218 output = get_input (arg_str, hist_PROMPT, trivial_completions);
6219 }
6220 }
6221
6222 if (output == NULL)
6223 return cmdret_new (RET_FAILURE, NULL); /* User aborted */
6224
6225 ret = cmdret_new (RET_SUCCESS, "%s", output);
6226 free (output);
6227
6228 return ret;
6229 }
6230
6231 cmdret *
6232 cmd_describekey (int interactive UNUSED, struct cmdarg **args)
6233 {
6234 char *keysym_name;
6235 rp_action *key_action;
6236 KeySym keysym; /* Key pressed */
6237 unsigned int mod; /* Modifiers */
6238 int rat_grabbed = 0;
6239 rp_keymap *map;
6240
6241 map = ARG(0,keymap);
6242
6243 /* Change the mouse icon to indicate to the user we are waiting for
6244 more keystrokes */
6245 if (defaults.wait_for_key_cursor)
6246 {
6247 grab_rat();
6248 rat_grabbed = 1;
6249 }
6250
6251 read_single_key (&keysym, &mod, NULL, 0);
6252
6253 if (rat_grabbed)
6254 ungrab_rat();
6255
6256 if ((key_action = find_keybinding (keysym, x11_mask_to_rp_mask (mod), map)))
6257 {
6258 cmdret *ret;
6259 keysym_name = keysym_to_string (keysym, x11_mask_to_rp_mask (mod));
6260 ret = cmdret_new (RET_SUCCESS, "%s bound to '%s'", keysym_name, key_action->data);
6261 free (keysym_name);
6262 return ret;
6263 }
6264 else
6265 {
6266 cmdret *ret;
6267 /* No key match, notify user. */
6268 keysym_name = keysym_to_string (keysym, x11_mask_to_rp_mask (mod));
6269 ret = cmdret_new (RET_SUCCESS, "describekey: unbound key '%s'", keysym_name);
6270 free (keysym_name);
6271 return ret;
6272 }
6273 }
6274
6275 cmdret *
6276 cmd_dedicate (int interactive UNUSED, struct cmdarg **args)
6277 {
6278 rp_frame *f;
6279
6280 f = current_frame ();
6281 if (f == NULL)
6282 return cmdret_new (RET_SUCCESS, NULL);
6283
6284 if (args[0] != NULL)
6285 {
6286 int dedicated;
6287
6288 dedicated = ARG (0, number);
6289 if (dedicated != 0 && dedicated != 1)
6290 return cmdret_new (RET_FAILURE,
6291 "Invalid \"dedicate\" value, use 0 or 1.");
6292 f->dedicated = dedicated;
6293 }
6294 else
6295 /* Just toggle it, rather than on or off. */
6296 f->dedicated = !(f->dedicated);
6297
6298 return cmdret_new (RET_SUCCESS, "Consider this frame %s.",
6299 f->dedicated ? "chaste":"promiscuous");
6300 }
6301
6302 cmdret *
6303 cmd_putsel (int interactive UNUSED, struct cmdarg **args)
6304 {
6305 set_selection(ARG_STRING(0));
6306 return cmdret_new (RET_SUCCESS, NULL);
6307 }
6308
6309 cmdret *
6310 cmd_getsel (int interactive UNUSED, struct cmdarg **args UNUSED)
6311 {
6312 char *sel;
6313 cmdret *ret;
6314 sel = get_selection();
6315 if (sel != NULL)
6316 {
6317 ret = cmdret_new (RET_SUCCESS, "%s", sel);
6318 free (sel);
6319 return ret;
6320 }
6321 else
6322 return cmdret_new (RET_FAILURE, "getsel: no X11 selection");
6323 }
6324
6325 cmdret *
6326 cmd_commands (int interactive UNUSED, struct cmdarg **args UNUSED)
6327 {
6328 struct sbuf *sb;
6329 struct user_command *cur;
6330 cmdret *ret;
6331
6332 sb = sbuf_new (0);
6333 list_for_each_entry (cur, &user_commands, node)
6334 {
6335 sbuf_printf_concat (sb, "%s\n", cur->name);
6336 }
6337 sbuf_chop (sb);
6338
6339 ret = cmdret_new (RET_SUCCESS, "%s", sbuf_get (sb));
6340 sbuf_free (sb);
6341 return ret;
6342 }