4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
29 void window_name_callback(unused
int, unused
short, void *);
30 char *parse_window_name(const char *);
33 queue_window_name(struct window
*w
)
38 tv
.tv_usec
= NAME_INTERVAL
* 1000L;
40 if (event_initialized(&w
->name_timer
))
41 evtimer_del(&w
->name_timer
);
42 evtimer_set(&w
->name_timer
, window_name_callback
, w
);
43 evtimer_add(&w
->name_timer
, &tv
);
47 window_name_callback(unused
int fd
, unused
short events
, void *data
)
49 struct window
*w
= data
;
52 if (w
->active
== NULL
)
55 if (!options_get_number(&w
->options
, "automatic-rename")) {
56 if (event_initialized(&w
->name_timer
))
57 event_del(&w
->name_timer
);
62 if (w
->active
->screen
!= &w
->active
->base
)
65 name
= get_proc_name(w
->active
->fd
, w
->active
->tty
);
67 wname
= default_window_name(w
);
70 * If tmux is using the default command, it will be a login
71 * shell and argv[0] may have a - prefix. Remove this if it is
74 if (w
->active
->cmd
!= NULL
&& *w
->active
->cmd
== '\0' &&
75 name
!= NULL
&& name
[0] == '-' && name
[1] != '\0')
76 wname
= parse_window_name(name
+ 1);
78 wname
= parse_window_name(name
);
82 if (w
->active
->fd
== -1) {
83 xasprintf(&name
, "%s[dead]", wname
);
88 if (strcmp(wname
, w
->name
)) {
89 window_set_name(w
, wname
);
90 server_status_window(w
);
96 default_window_name(struct window
*w
)
98 if (w
->active
->screen
!= &w
->active
->base
)
99 return (xstrdup("[tmux]"));
100 if (w
->active
->cmd
!= NULL
&& *w
->active
->cmd
!= '\0')
101 return (parse_window_name(w
->active
->cmd
));
102 return (parse_window_name(w
->active
->shell
));
106 parse_window_name(const char *in
)
108 char *copy
, *name
, *ptr
;
110 name
= copy
= xstrdup(in
);
111 if (strncmp(name
, "exec ", (sizeof "exec ") - 1) == 0)
112 name
= name
+ (sizeof "exec ") - 1;
116 if ((ptr
= strchr(name
, ' ')) != NULL
)
120 ptr
= name
+ strlen(name
) - 1;
121 while (ptr
> name
&& !isalnum((u_char
)*ptr
))
126 name
= basename(name
);
127 name
= xstrdup(name
);