summaryrefslogtreecommitdiffstats
path: root/cmd.c
diff options
context:
space:
mode:
authorTiago Cunha <tcunha@gmx.com>2009-08-25 13:53:39 +0000
committerTiago Cunha <tcunha@gmx.com>2009-08-25 13:53:39 +0000
commit8fd77cbb5b9e81a14d52f9ddcf765d3ce557ed86 (patch)
treef15f24cd859da56f60f2fcac20b2b3dd565e0ae3 /cmd.c
parentc1653ff65452c01f0c2c41dbcb3c6d0eb241348b (diff)
Sync OpenBSD patchset 294:
Add a choose-client command and extend choose-{session,window} to accept a template. After a choice is made, %% (or %1) in the template is replaced by the name of the session, window or client suitable for -t and the result executed as a command. So, for example, "choose-window "killw -t '%%'"" will kill the selected window. The defaults if no template is given are (as now) select-window for choose-window, switch-client for choose-session, and detach-client for choose-client (now bound to D).
Diffstat (limited to 'cmd.c')
-rw-r--r--cmd.c44
1 files changed, 43 insertions, 1 deletions
diff --git a/cmd.c b/cmd.c
index 4ff05eb4..0a95a289 100644
--- a/cmd.c
+++ b/cmd.c
@@ -1,4 +1,4 @@
-/* $Id: cmd.c,v 1.113 2009-08-24 16:24:18 tcunha Exp $ */
+/* $Id: cmd.c,v 1.114 2009-08-25 13:53:39 tcunha Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -30,6 +30,7 @@ const struct cmd_entry *cmd_table[] = {
&cmd_attach_session_entry,
&cmd_bind_key_entry,
&cmd_break_pane_entry,
+ &cmd_choose_client_entry,
&cmd_choose_session_entry,
&cmd_choose_window_entry,
&cmd_clear_history_entry,
@@ -857,3 +858,44 @@ error:
xfree(winptr);
return (NULL);
}
+
+/* Replace the first %% or %idx in template by s. */
+char *
+cmd_template_replace(char *template, const char *s, int idx)
+{
+ char ch;
+ char *buf, *ptr;
+ int replaced;
+ size_t len;
+
+ if (strstr(template, "%") == NULL)
+ return (xstrdup(template));
+
+ buf = xmalloc(1);
+ *buf = '\0';
+ len = 0;
+ replaced = 0;
+
+ ptr = template;
+ while (*ptr != '\0') {
+ switch (ch = *ptr++) {
+ case '%':
+ if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
+ if (*ptr != '%' || replaced)
+ break;
+ replaced = 1;
+ }
+ ptr++;
+
+ len += strlen(s);
+ buf = xrealloc(buf, 1, len + 1);
+ strlcat(buf, s, len + 1);
+ continue;
+ }
+ buf = xrealloc(buf, 1, len + 2);
+ buf[len++] = ch;
+ buf[len] = '\0';
+ }
+
+ return (buf);
+}