Skip to content

Instantly share code, notes, and snippets.

@ThomasAdam
Created December 11, 2012 00:28
Show Gist options
  • Select an option

  • Save ThomasAdam/4254647 to your computer and use it in GitHub Desktop.

Select an option

Save ThomasAdam/4254647 to your computer and use it in GitHub Desktop.
diff --git a/format.c b/format.c
index 19f322a..a135597 100644
--- a/format.c
+++ b/format.c
@@ -392,6 +392,8 @@ format_window_pane(struct format_tree *ft, struct window_pane *wp)
if (wp->cwd != NULL)
format_add(ft, "pane_start_path", "%s", wp->cwd);
format_add(ft, "pane_current_path", "%s", osdep_get_cwd(wp->fd));
+ format_add(ft, "pane_current_program", "%s",
+ window_name_display(wp->window));
format_add(ft, "pane_pid", "%ld", (long) wp->pid);
format_add(ft, "pane_tty", "%s", wp->tty);
}
diff --git a/names.c b/names.c
index 72f1ad1..72c0dc9 100644
--- a/names.c
+++ b/names.c
@@ -47,8 +47,12 @@ queue_window_name(struct window *w)
void
window_name_callback(unused int fd, unused short events, void *data)
{
- struct window *w = data;
- char *name, *wname;
+ struct window *w = data;
+ struct winlink *wl;
+ struct format_tree *ft;
+ struct session *s, *s2;
+ char *name, *wname;
+ const char *name_template;
if (w->active == NULL)
return;
@@ -60,25 +64,30 @@ window_name_callback(unused int fd, unused short events, void *data)
}
queue_window_name(w);
+ ft = format_create();
+ RB_FOREACH(s2, sessions, &sessions) {
+ if ((wl = winlink_find_by_window(&s2->windows, w)) == NULL)
+ continue;
+ s = s2;
+ }
+ if (wl != NULL && s != NULL)
+ format_winlink(ft, s, wl);
+
+ format_window_pane(ft, w->active);
+
+ name_template = options_get_string(&w->options,
+ "automatic-rename-format");
+ if (name_template == NULL)
+ name_template = AUTOMATIC_RENAME_TEMPLATE;
+
if (w->active->screen != &w->active->base)
name = NULL;
else
- name = osdep_get_name(w->active->fd, w->active->tty);
+ name = format_expand(ft, name_template);
if (name == NULL)
wname = default_window_name(w);
- else {
- /*
- * If tmux is using the default command, it will be a login
- * shell and argv[0] may have a - prefix. Remove this if it is
- * present. Ick.
- */
- if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
- name != NULL && name[0] == '-' && name[1] != '\0')
- wname = parse_window_name(name + 1);
- else
- wname = parse_window_name(name);
- free(name);
- }
+ else
+ wname = window_name_display(w);
if (w->active->fd == -1) {
xasprintf(&name, "%s[dead]", wname);
@@ -91,6 +100,7 @@ window_name_callback(unused int fd, unused short events, void *data)
server_status_window(w);
}
free(wname);
+ format_free(ft);
}
char *
@@ -129,3 +139,27 @@ parse_window_name(const char *in)
free(copy);
return (name);
}
+
+char *
+window_name_display(struct window *w)
+{
+ struct window_pane *wp = w->active;
+ char *name, *wname;
+
+ name = osdep_get_name(wp->fd, wp->tty);
+
+ /*
+ * If tmux is using the default command, it will be a login
+ * shell and argv[0] may have a - prefix. Remove this if it is
+ * present. Ick.
+ */
+ if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
+ name != NULL && name[0] == '-' && name[1] != '\0')
+ wname = parse_window_name(name + 1);
+ else
+ wname = parse_window_name(name);
+
+ free(name);
+
+ return (wname);
+}
diff --git a/options-table.c b/options-table.c
index 4d1edbd..62a9025 100644
--- a/options-table.c
+++ b/options-table.c
@@ -469,6 +469,10 @@ const struct options_table_entry window_options_table[] = {
.default_num = 1
},
+ { .name = "automatic-rename-format",
+ .type = OPTIONS_TABLE_STRING,
+ .default_str = AUTOMATIC_RENAME_TEMPLATE
+ },
{ .name = "c0-change-trigger",
.type = OPTIONS_TABLE_NUMBER,
diff --git a/tmux.h b/tmux.h
index 960d130..8038629 100644
--- a/tmux.h
+++ b/tmux.h
@@ -158,6 +158,9 @@ extern char **environ;
#define NEW_WINDOW_TEMPLATE BREAK_PANE_TEMPLATE
#define SPLIT_WINDOW_TEMPLATE BREAK_PANE_TEMPLATE
+/* Default template for automatic-rename */
+#define AUTOMATIC_RENAME_TEMPLATE "#{window_name}"
+
/* Bell option values. */
#define BELL_NONE 0
#define BELL_ANY 1
@@ -2243,6 +2246,7 @@ struct window_choose_data *window_choose_add_item(struct window_pane *,
/* names.c */
void queue_window_name(struct window *);
char *default_window_name(struct window *);
+char *window_name_display(struct window *);
/* signal.c */
void set_signals(void(*)(int, short, void *));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment