4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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>
28 static void name_time_callback(int, short, void *);
29 static int name_time_expired(struct window
*, struct timeval
*);
31 static char *format_window_name(struct window
*);
34 name_time_callback(__unused
int fd
, __unused
short events
, void *arg
)
36 struct window
*w
= arg
;
38 /* The event loop will call check_window_name for us on the way out. */
39 log_debug("@%u name timer expired", w
->id
);
43 name_time_expired(struct window
*w
, struct timeval
*tv
)
45 struct timeval offset
;
47 timersub(tv
, &w
->name_time
, &offset
);
48 if (offset
.tv_sec
!= 0 || offset
.tv_usec
> NAME_INTERVAL
)
50 return (NAME_INTERVAL
- offset
.tv_usec
);
54 check_window_name(struct window
*w
)
56 struct timeval tv
, next
;
60 if (w
->active
== NULL
)
63 if (!options_get_number(w
->options
, "automatic-rename"))
66 if (~w
->active
->flags
& PANE_CHANGED
) {
67 log_debug("@%u active pane not changed", w
->id
);
70 log_debug("@%u active pane changed", w
->id
);
72 gettimeofday(&tv
, NULL
);
73 left
= name_time_expired(w
, &tv
);
75 if (!event_initialized(&w
->name_event
))
76 evtimer_set(&w
->name_event
, name_time_callback
, w
);
77 if (!evtimer_pending(&w
->name_event
, NULL
)) {
78 log_debug("@%u name timer queued (%d left)", w
->id
,
82 event_add(&w
->name_event
, &next
);
84 log_debug("@%u name timer already queued (%d left)",
89 memcpy(&w
->name_time
, &tv
, sizeof w
->name_time
);
90 if (event_initialized(&w
->name_event
))
91 evtimer_del(&w
->name_event
);
93 w
->active
->flags
&= ~PANE_CHANGED
;
95 name
= format_window_name(w
);
96 if (strcmp(name
, w
->name
) != 0) {
97 log_debug("@%u new name %s (was %s)", w
->id
, name
, w
->name
);
98 window_set_name(w
, name
);
99 server_redraw_window_borders(w
);
100 server_status_window(w
);
102 log_debug("@%u name not changed (still %s)", w
->id
, w
->name
);
108 default_window_name(struct window
*w
)
112 if (w
->active
== NULL
)
113 return (xstrdup(""));
114 cmd
= cmd_stringify_argv(w
->active
->argc
, w
->active
->argv
);
115 if (cmd
!= NULL
&& *cmd
!= '\0')
116 s
= parse_window_name(cmd
);
118 s
= parse_window_name(w
->active
->shell
);
124 format_window_name(struct window
*w
)
126 struct format_tree
*ft
;
130 ft
= format_create(NULL
, NULL
, FORMAT_WINDOW
|w
->id
, 0);
131 format_defaults_window(ft
, w
);
132 format_defaults_pane(ft
, w
->active
);
134 fmt
= options_get_string(w
->options
, "automatic-rename-format");
135 name
= format_expand(ft
, fmt
);
142 parse_window_name(const char *in
)
144 char *copy
, *name
, *ptr
;
146 name
= copy
= xstrdup(in
);
149 name
[strcspn(name
, "\"")] = '\0';
151 if (strncmp(name
, "exec ", (sizeof "exec ") - 1) == 0)
152 name
= name
+ (sizeof "exec ") - 1;
154 while (*name
== ' ' || *name
== '-')
156 if ((ptr
= strchr(name
, ' ')) != NULL
)
160 ptr
= name
+ strlen(name
) - 1;
162 !isalnum((u_char
)*ptr
) &&
163 !ispunct((u_char
)*ptr
))
168 name
= basename(name
);
169 name
= xstrdup(name
);