This introduces a choose-tree command, which amalgamates the functionality
of choose-session and choose-window to be displayed as a tree.
---
 trunk/Makefile.am       |    1 +
 trunk/cmd-choose-tree.c |  141 +++++++++++++++++++++++++++++++++++++++++++++++
 trunk/cmd.c             |    1 +
 trunk/tmux.1            |   45 +++++++++++++++
 trunk/tmux.h            |    1 +
 5 files changed, 189 insertions(+)
 create mode 100644 trunk/cmd-choose-tree.c

diff --git a/trunk/Makefile.am b/trunk/Makefile.am
index 5e26c57..0fbbb37 100644
--- a/trunk/Makefile.am
+++ b/trunk/Makefile.am
@@ -67,6 +67,7 @@ dist_tmux_SOURCES = \
        cmd-choose-buffer.c \
        cmd-choose-client.c \
        cmd-choose-session.c \
+       cmd-choose-tree.c \
        cmd-choose-window.c \
        cmd-clear-history.c \
        cmd-clock-mode.c \
diff --git a/trunk/cmd-choose-tree.c b/trunk/cmd-choose-tree.c
new file mode 100644
index 0000000..d14ddf6
--- /dev/null
+++ b/trunk/cmd-choose-tree.c
@@ -0,0 +1,141 @@
+/* $Id$ */
+
+/*
+ * Copyright (c) 2012 Thomas Adam <tho...@xteddy.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+
+#include <ctype.h>
+
+#include <string.h>
+
+#include "tmux.h"
+
+/*
+ * Enter choice mode to choose a session and/or window.
+ */
+
+int    cmd_choose_tree_exec(struct cmd *, struct cmd_ctx *);
+
+void   cmd_choose_tree_callback(struct window_choose_data *);
+void   cmd_choose_tree_free(struct window_choose_data *);
+
+const struct cmd_entry cmd_choose_tree_entry = {
+       "choose-tree", NULL,
+       "S:W:b:c:t:", 0, 1,
+       "[-S format] [-W format] [-b session template] [-c window template] " \
+               CMD_TARGET_WINDOW_USAGE,
+       0,
+       NULL,
+       NULL,
+       cmd_choose_tree_exec
+};
+
+int
+cmd_choose_tree_exec(struct cmd *self, struct cmd_ctx *ctx)
+{
+       struct args                     *args = self->args;
+       struct winlink                  *wl, *wm;
+       struct session                  *s, *s2;
+       struct window_choose_data       *wcd;
+       const char                      *ses_template, *win_template;
+       char                            *final_win_action, *final_win_template;
+       const char                      *ses_action, *win_action;
+       u_int                            cur_win, idx_ses;
+
+       if (ctx->curclient == NULL) {
+               ctx->error(ctx, "must be run interactively");
+               return (-1);
+       }
+       s = ctx->curclient->session;
+
+       if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
+               return (-1);
+
+       if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
+               return (0);
+
+       if ((ses_action = args_get(args, 'b')) == NULL)
+               ses_action = "switch-client -t '%%'";
+
+       if ((win_action = args_get(args, 'c')) == NULL)
+               win_action = "select-window -t '%%'";
+
+       if ((ses_template = args_get(args, 'S')) == NULL)
+               ses_template = DEFAULT_SESSION_TEMPLATE;
+
+       if ((win_template = args_get(args, 'W')) == NULL)
+               win_template = DEFAULT_WINDOW_TEMPLATE " \"#{pane_title}\"";
+
+       /* Pad the win template so it looks like part of a tree. */
+       /* TODO:  Consider ACS line drawing? */
+       xasprintf(&final_win_template, "%s%s", "        ", win_template);
+
+       idx_ses = 0;
+       RB_FOREACH(s2, sessions, &sessions) {
+               idx_ses++;
+
+               wcd = window_choose_add_session(wl->window->active,
+                       ctx, s2, ses_template, (char *)ses_action, idx_ses);
+
+               RB_FOREACH(wm, winlinks, &s2->windows) {
+                       if (wm == s2->curw && s == s2)
+                               cur_win = idx_ses;
+                       idx_ses++;
+
+                       xasprintf(&final_win_action, "%s ; %s", win_action,
+                               wcd->command);
+
+                       window_choose_add_window(wl->window->active,
+                               ctx, s2, wm, final_win_template,
+                               final_win_action, idx_ses);
+
+                       xfree(final_win_action);
+               }
+       }
+       xfree(final_win_template);
+
+       window_choose_ready(wl->window->active, cur_win,
+               cmd_choose_tree_callback, cmd_choose_tree_free);
+
+       return (0);
+}
+
+void
+cmd_choose_tree_callback(struct window_choose_data *cdata)
+{
+       if (cdata == NULL)
+               return;
+
+       if (cdata->client->flags & CLIENT_DEAD)
+               return;
+
+       window_choose_ctx(cdata);
+}
+
+void
+cmd_choose_tree_free(struct window_choose_data *cdata)
+{
+       cdata->session->references--;
+       cdata->client->references--;
+
+       xfree(cdata->ft_template);
+       xfree(cdata->command);
+       format_free(cdata->ft);
+       xfree(cdata);
+
+}
+
diff --git a/trunk/cmd.c b/trunk/cmd.c
index 5941692..aa6ea28 100644
--- a/trunk/cmd.c
+++ b/trunk/cmd.c
@@ -35,6 +35,7 @@ const struct cmd_entry *cmd_table[] = {
        &cmd_choose_buffer_entry,
        &cmd_choose_client_entry,
        &cmd_choose_session_entry,
+       &cmd_choose_tree_entry,
        &cmd_choose_window_entry,
        &cmd_clear_history_entry,
        &cmd_clock_mode_entry,
diff --git a/trunk/tmux.1 b/trunk/tmux.1
index 34d0232..809da8f 100644
--- a/trunk/tmux.1
+++ b/trunk/tmux.1
@@ -1084,6 +1084,51 @@ section.
 This command works only from inside
 .Nm .
 .It Xo
+.Ic choose-tree
+.Op Fl S Ar format
+.Op Fl W Ar format
+.Op Fl b Ar session-template
+.Op Fl c Ar window-template
+.Op Fl t Ar target-window
+.Xc
+Put a window into tre choice mode, where either a session or window may be
+selected interactively from a list.
+By default, windows belonging to a session are indented to show their
+relationship to a session.
+If
+.Fl b
+is given, will override the default session command.
+Note that
+.Ql %%
+can be used, and will be replaced with the session name.
+The default option if not specified is "switch-client -t '%%'".
+If
+.Fl c
+is given, will override the default window command.
+Note that
+.Ql %%
+can be used, and will be replaced with the session name and window index.
+This command will run
+.Ar session-template
+before it.
+If
+.Fl S
+is given will display the specified format instead of the default session
+format.
+If
+.Fl W
+is given will display the specified format instead of the default window
+format.
+For the meaning of the 
+.Fl S
+and
+.Fl W
+options, see the
+.Sx FORMATS
+section.
+This command only works from inside
+.Nm .
+.It Xo
 .Ic choose-window
 .Op Fl F Ar format
 .Op Fl t Ar target-window
diff --git a/trunk/tmux.h b/trunk/tmux.h
index 7fd355f..85e96d0 100644
--- a/trunk/tmux.h
+++ b/trunk/tmux.h
@@ -1657,6 +1657,7 @@ extern const struct cmd_entry cmd_capture_pane_entry;
 extern const struct cmd_entry cmd_choose_buffer_entry;
 extern const struct cmd_entry cmd_choose_client_entry;
 extern const struct cmd_entry cmd_choose_session_entry;
+extern const struct cmd_entry cmd_choose_tree_entry;
 extern const struct cmd_entry cmd_choose_window_entry;
 extern const struct cmd_entry cmd_clear_history_entry;
 extern const struct cmd_entry cmd_clock_mode_entry;
-- 
1.7.10


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to