Let's say I want to write a shell script that operates on the
currently focused pane, to be executed with 'run-shell'. The only way
to do that for now is to call 'tmux list-panes' inside the script and
parse the output to determine the active pane. This patches modifies
run-shell to expand tmux #{variables} in the command string, so data
about tmux can be passed to run-shell commands.

This is implemented in Thomas Adams hook-support branch, but as shown
above, it is useful without hooks support.

Since Thomas can't foresee when he will merge hook-support, and I
would like to see this feature in the next version, here it goes.

---
 cmd-run-shell.c | 19 +++++++++++++++----
 tmux.1          |  2 ++
 2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/cmd-run-shell.c b/cmd-run-shell.c
index 9b4c006..d25c5e6 100644
--- a/cmd-run-shell.c
+++ b/cmd-run-shell.c
@@ -75,20 +75,31 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
 {
 	struct args			*args = self->args;
 	struct cmd_run_shell_data	*cdata;
-	const char			*shellcmd = args->argv[0];
+	const char			*shellcmd;
+	struct session		*s;
+	struct winlink		*wl;
 	struct window_pane		*wp;
+	struct format_tree		*ft;
 
-	if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL)
+	wl = cmd_find_pane(ctx, args_get(args, 't'), &s, &wp);
+	if (wl == NULL)
 		return (CMD_RETURN_ERROR);
 
+	ft = format_create();
+	format_session(ft, s);
+	format_winlink(ft, s, wl);
+	format_window_pane(ft, wp);
+	shellcmd = format_expand(ft, args->argv[0]);
+
 	cdata = xmalloc(sizeof *cdata);
-	cdata->cmd = xstrdup(args->argv[0]);
+	cdata->cmd = xstrdup(shellcmd);
 	cdata->wp_id = wp->id;
-
 	cdata->ctx = ctx;
 	cmd_ref_ctx(ctx);
 
 	job_run(shellcmd, cmd_run_shell_callback, cmd_run_shell_free, cdata);
+	format_free(ft);
+	free(shellcmd);
 
 	return (CMD_RETURN_YIELD);	/* don't let client exit */
 }
diff --git a/tmux.1 b/tmux.1
index 882a71f..ada931a 100644
--- a/tmux.1
+++ b/tmux.1
@@ -3490,6 +3490,8 @@ option.
 Execute
 .Ar shell-command
 in the background without creating a window.
+The command string is expanded using the rules specified in the FORMATS
+section.
 After it finishes, any output to stdout is displayed in copy mode (in the pane
 specified by
 .Fl t
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_feb
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to