1 |
|
|
/* $OpenBSD: status.c,v 1.171 2017/11/02 21:29:17 nicm Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 |
|
|
* |
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. |
9 |
|
|
* |
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. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <sys/types.h> |
20 |
|
|
#include <sys/time.h> |
21 |
|
|
|
22 |
|
|
#include <errno.h> |
23 |
|
|
#include <limits.h> |
24 |
|
|
#include <stdarg.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
#include <string.h> |
27 |
|
|
#include <time.h> |
28 |
|
|
#include <unistd.h> |
29 |
|
|
|
30 |
|
|
#include "tmux.h" |
31 |
|
|
|
32 |
|
|
static char *status_redraw_get_left(struct client *, time_t, |
33 |
|
|
struct grid_cell *, size_t *); |
34 |
|
|
static char *status_redraw_get_right(struct client *, time_t, |
35 |
|
|
struct grid_cell *, size_t *); |
36 |
|
|
static char *status_print(struct client *, struct winlink *, time_t, |
37 |
|
|
struct grid_cell *); |
38 |
|
|
static char *status_replace(struct client *, struct winlink *, const char *, |
39 |
|
|
time_t); |
40 |
|
|
static void status_message_callback(int, short, void *); |
41 |
|
|
static void status_timer_callback(int, short, void *); |
42 |
|
|
|
43 |
|
|
static char *status_prompt_find_history_file(void); |
44 |
|
|
static const char *status_prompt_up_history(u_int *); |
45 |
|
|
static const char *status_prompt_down_history(u_int *); |
46 |
|
|
static void status_prompt_add_history(const char *); |
47 |
|
|
|
48 |
|
|
static const char **status_prompt_complete_list(u_int *, const char *); |
49 |
|
|
static char *status_prompt_complete_prefix(const char **, u_int); |
50 |
|
|
static char *status_prompt_complete(struct session *, const char *); |
51 |
|
|
|
52 |
|
|
/* Status prompt history. */ |
53 |
|
|
#define PROMPT_HISTORY 100 |
54 |
|
|
static char **status_prompt_hlist; |
55 |
|
|
static u_int status_prompt_hsize; |
56 |
|
|
|
57 |
|
|
/* Find the history file to load/save from/to. */ |
58 |
|
|
static char * |
59 |
|
|
status_prompt_find_history_file(void) |
60 |
|
|
{ |
61 |
|
|
const char *home, *history_file; |
62 |
|
|
char *path; |
63 |
|
|
|
64 |
|
|
history_file = options_get_string(global_options, "history-file"); |
65 |
|
|
if (*history_file == '\0') |
66 |
|
|
return (NULL); |
67 |
|
|
if (*history_file == '/') |
68 |
|
|
return (xstrdup(history_file)); |
69 |
|
|
|
70 |
|
|
if (history_file[0] != '~' || history_file[1] != '/') |
71 |
|
|
return (NULL); |
72 |
|
|
if ((home = find_home()) == NULL) |
73 |
|
|
return (NULL); |
74 |
|
|
xasprintf(&path, "%s%s", home, history_file + 1); |
75 |
|
|
return (path); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
/* Load status prompt history from file. */ |
79 |
|
|
void |
80 |
|
|
status_prompt_load_history(void) |
81 |
|
|
{ |
82 |
|
|
FILE *f; |
83 |
|
|
char *history_file, *line, *tmp; |
84 |
|
|
size_t length; |
85 |
|
|
|
86 |
|
|
if ((history_file = status_prompt_find_history_file()) == NULL) |
87 |
|
|
return; |
88 |
|
|
log_debug("loading history from %s", history_file); |
89 |
|
|
|
90 |
|
|
f = fopen(history_file, "r"); |
91 |
|
|
if (f == NULL) { |
92 |
|
|
log_debug("%s: %s", history_file, strerror(errno)); |
93 |
|
|
free(history_file); |
94 |
|
|
return; |
95 |
|
|
} |
96 |
|
|
free(history_file); |
97 |
|
|
|
98 |
|
|
for (;;) { |
99 |
|
|
if ((line = fgetln(f, &length)) == NULL) |
100 |
|
|
break; |
101 |
|
|
|
102 |
|
|
if (length > 0) { |
103 |
|
|
if (line[length - 1] == '\n') { |
104 |
|
|
line[length - 1] = '\0'; |
105 |
|
|
status_prompt_add_history(line); |
106 |
|
|
} else { |
107 |
|
|
tmp = xmalloc(length + 1); |
108 |
|
|
memcpy(tmp, line, length); |
109 |
|
|
tmp[length] = '\0'; |
110 |
|
|
status_prompt_add_history(tmp); |
111 |
|
|
free(tmp); |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
fclose(f); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
/* Save status prompt history to file. */ |
119 |
|
|
void |
120 |
|
|
status_prompt_save_history(void) |
121 |
|
|
{ |
122 |
|
|
FILE *f; |
123 |
|
|
u_int i; |
124 |
|
|
char *history_file; |
125 |
|
|
|
126 |
|
|
if ((history_file = status_prompt_find_history_file()) == NULL) |
127 |
|
|
return; |
128 |
|
|
log_debug("saving history to %s", history_file); |
129 |
|
|
|
130 |
|
|
f = fopen(history_file, "w"); |
131 |
|
|
if (f == NULL) { |
132 |
|
|
log_debug("%s: %s", history_file, strerror(errno)); |
133 |
|
|
free(history_file); |
134 |
|
|
return; |
135 |
|
|
} |
136 |
|
|
free(history_file); |
137 |
|
|
|
138 |
|
|
for (i = 0; i < status_prompt_hsize; i++) { |
139 |
|
|
fputs(status_prompt_hlist[i], f); |
140 |
|
|
fputc('\n', f); |
141 |
|
|
} |
142 |
|
|
fclose(f); |
143 |
|
|
|
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
/* Status timer callback. */ |
147 |
|
|
static void |
148 |
|
|
status_timer_callback(__unused int fd, __unused short events, void *arg) |
149 |
|
|
{ |
150 |
|
|
struct client *c = arg; |
151 |
|
|
struct session *s = c->session; |
152 |
|
|
struct timeval tv; |
153 |
|
|
|
154 |
|
|
evtimer_del(&c->status_timer); |
155 |
|
|
|
156 |
|
|
if (s == NULL) |
157 |
|
|
return; |
158 |
|
|
|
159 |
|
|
if (c->message_string == NULL && c->prompt_string == NULL) |
160 |
|
|
c->flags |= CLIENT_STATUS; |
161 |
|
|
|
162 |
|
|
timerclear(&tv); |
163 |
|
|
tv.tv_sec = options_get_number(s->options, "status-interval"); |
164 |
|
|
|
165 |
|
|
if (tv.tv_sec != 0) |
166 |
|
|
evtimer_add(&c->status_timer, &tv); |
167 |
|
|
log_debug("client %p, status interval %d", c, (int)tv.tv_sec); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
/* Start status timer for client. */ |
171 |
|
|
void |
172 |
|
|
status_timer_start(struct client *c) |
173 |
|
|
{ |
174 |
|
|
struct session *s = c->session; |
175 |
|
|
|
176 |
|
|
if (event_initialized(&c->status_timer)) |
177 |
|
|
evtimer_del(&c->status_timer); |
178 |
|
|
else |
179 |
|
|
evtimer_set(&c->status_timer, status_timer_callback, c); |
180 |
|
|
|
181 |
|
|
if (s != NULL && options_get_number(s->options, "status")) |
182 |
|
|
status_timer_callback(-1, 0, c); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
/* Start status timer for all clients. */ |
186 |
|
|
void |
187 |
|
|
status_timer_start_all(void) |
188 |
|
|
{ |
189 |
|
|
struct client *c; |
190 |
|
|
|
191 |
|
|
TAILQ_FOREACH(c, &clients, entry) |
192 |
|
|
status_timer_start(c); |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
/* Update status cache. */ |
196 |
|
|
void |
197 |
|
|
status_update_saved(struct session *s) |
198 |
|
|
{ |
199 |
|
|
if (!options_get_number(s->options, "status")) |
200 |
|
|
s->statusat = -1; |
201 |
|
|
else if (options_get_number(s->options, "status-position") == 0) |
202 |
|
|
s->statusat = 0; |
203 |
|
|
else |
204 |
|
|
s->statusat = 1; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
/* Get screen line of status line. -1 means off. */ |
208 |
|
|
int |
209 |
|
|
status_at_line(struct client *c) |
210 |
|
|
{ |
211 |
|
|
struct session *s = c->session; |
212 |
|
|
|
213 |
|
|
if (c->flags & CLIENT_STATUSOFF) |
214 |
|
|
return (-1); |
215 |
|
|
if (s->statusat != 1) |
216 |
|
|
return (s->statusat); |
217 |
|
|
return (c->tty.sy - status_line_size(s)); |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
/* |
221 |
|
|
* Get size of status line for session. 0 means off. Note that status line may |
222 |
|
|
* be forced off for an individual client if it is too small (the |
223 |
|
|
* CLIENT_STATUSOFF flag is set for this). |
224 |
|
|
*/ |
225 |
|
|
u_int |
226 |
|
|
status_line_size(struct session *s) |
227 |
|
|
{ |
228 |
|
|
if (s->statusat == -1) |
229 |
|
|
return (0); |
230 |
|
|
return (1); |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
/* Retrieve options for left string. */ |
234 |
|
|
static char * |
235 |
|
|
status_redraw_get_left(struct client *c, time_t t, struct grid_cell *gc, |
236 |
|
|
size_t *size) |
237 |
|
|
{ |
238 |
|
|
struct session *s = c->session; |
239 |
|
|
const char *template; |
240 |
|
|
char *left; |
241 |
|
|
size_t leftlen; |
242 |
|
|
|
243 |
|
|
style_apply_update(gc, s->options, "status-left-style"); |
244 |
|
|
|
245 |
|
|
template = options_get_string(s->options, "status-left"); |
246 |
|
|
left = status_replace(c, NULL, template, t); |
247 |
|
|
|
248 |
|
|
*size = options_get_number(s->options, "status-left-length"); |
249 |
|
|
leftlen = screen_write_cstrlen("%s", left); |
250 |
|
|
if (leftlen < *size) |
251 |
|
|
*size = leftlen; |
252 |
|
|
return (left); |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
/* Retrieve options for right string. */ |
256 |
|
|
static char * |
257 |
|
|
status_redraw_get_right(struct client *c, time_t t, struct grid_cell *gc, |
258 |
|
|
size_t *size) |
259 |
|
|
{ |
260 |
|
|
struct session *s = c->session; |
261 |
|
|
const char *template; |
262 |
|
|
char *right; |
263 |
|
|
size_t rightlen; |
264 |
|
|
|
265 |
|
|
style_apply_update(gc, s->options, "status-right-style"); |
266 |
|
|
|
267 |
|
|
template = options_get_string(s->options, "status-right"); |
268 |
|
|
right = status_replace(c, NULL, template, t); |
269 |
|
|
|
270 |
|
|
*size = options_get_number(s->options, "status-right-length"); |
271 |
|
|
rightlen = screen_write_cstrlen("%s", right); |
272 |
|
|
if (rightlen < *size) |
273 |
|
|
*size = rightlen; |
274 |
|
|
return (right); |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
/* Get window at window list position. */ |
278 |
|
|
struct window * |
279 |
|
|
status_get_window_at(struct client *c, u_int x) |
280 |
|
|
{ |
281 |
|
|
struct session *s = c->session; |
282 |
|
|
struct winlink *wl; |
283 |
|
|
struct options *oo; |
284 |
|
|
const char *sep; |
285 |
|
|
size_t seplen; |
286 |
|
|
|
287 |
|
|
x += c->wlmouse; |
288 |
|
|
RB_FOREACH(wl, winlinks, &s->windows) { |
289 |
|
|
oo = wl->window->options; |
290 |
|
|
|
291 |
|
|
sep = options_get_string(oo, "window-status-separator"); |
292 |
|
|
seplen = screen_write_cstrlen("%s", sep); |
293 |
|
|
|
294 |
|
|
if (x < wl->status_width) |
295 |
|
|
return (wl->window); |
296 |
|
|
x -= wl->status_width + seplen; |
297 |
|
|
} |
298 |
|
|
return (NULL); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
/* Draw status for client on the last lines of given context. */ |
302 |
|
|
int |
303 |
|
|
status_redraw(struct client *c) |
304 |
|
|
{ |
305 |
|
|
struct screen_write_ctx ctx; |
306 |
|
|
struct session *s = c->session; |
307 |
|
|
struct winlink *wl; |
308 |
|
|
struct screen old_status, window_list; |
309 |
|
|
struct grid_cell stdgc, lgc, rgc, gc; |
310 |
|
|
struct options *oo; |
311 |
|
|
time_t t; |
312 |
|
|
char *left, *right; |
313 |
|
|
const char *sep; |
314 |
|
|
u_int offset, needed, lines; |
315 |
|
|
u_int wlstart, wlwidth, wlavailable, wloffset, wlsize; |
316 |
|
|
size_t llen, rlen, seplen; |
317 |
|
|
int larrow, rarrow; |
318 |
|
|
|
319 |
|
|
/* Delete the saved status line, if any. */ |
320 |
|
|
if (c->old_status != NULL) { |
321 |
|
|
screen_free(c->old_status); |
322 |
|
|
free(c->old_status); |
323 |
|
|
c->old_status = NULL; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
/* No status line? */ |
327 |
|
|
lines = status_line_size(s); |
328 |
|
|
if (c->tty.sy == 0 || lines == 0) |
329 |
|
|
return (1); |
330 |
|
|
left = right = NULL; |
331 |
|
|
larrow = rarrow = 0; |
332 |
|
|
|
333 |
|
|
/* Store current time. */ |
334 |
|
|
t = time(NULL); |
335 |
|
|
|
336 |
|
|
/* Set up default colour. */ |
337 |
|
|
style_apply(&stdgc, s->options, "status-style"); |
338 |
|
|
|
339 |
|
|
/* Create the target screen. */ |
340 |
|
|
memcpy(&old_status, &c->status, sizeof old_status); |
341 |
|
|
screen_init(&c->status, c->tty.sx, lines, 0); |
342 |
|
|
screen_write_start(&ctx, NULL, &c->status); |
343 |
|
|
for (offset = 0; offset < lines * c->tty.sx; offset++) |
344 |
|
|
screen_write_putc(&ctx, &stdgc, ' '); |
345 |
|
|
screen_write_stop(&ctx); |
346 |
|
|
|
347 |
|
|
/* If the height is too small, blank status line. */ |
348 |
|
|
if (c->tty.sy < lines) |
349 |
|
|
goto out; |
350 |
|
|
|
351 |
|
|
/* Work out left and right strings. */ |
352 |
|
|
memcpy(&lgc, &stdgc, sizeof lgc); |
353 |
|
|
left = status_redraw_get_left(c, t, &lgc, &llen); |
354 |
|
|
memcpy(&rgc, &stdgc, sizeof rgc); |
355 |
|
|
right = status_redraw_get_right(c, t, &rgc, &rlen); |
356 |
|
|
|
357 |
|
|
/* |
358 |
|
|
* Figure out how much space we have for the window list. If there |
359 |
|
|
* isn't enough space, just show a blank status line. |
360 |
|
|
*/ |
361 |
|
|
needed = 0; |
362 |
|
|
if (llen != 0) |
363 |
|
|
needed += llen; |
364 |
|
|
if (rlen != 0) |
365 |
|
|
needed += rlen; |
366 |
|
|
if (c->tty.sx == 0 || c->tty.sx <= needed) |
367 |
|
|
goto out; |
368 |
|
|
wlavailable = c->tty.sx - needed; |
369 |
|
|
|
370 |
|
|
/* Calculate the total size needed for the window list. */ |
371 |
|
|
wlstart = wloffset = wlwidth = 0; |
372 |
|
|
RB_FOREACH(wl, winlinks, &s->windows) { |
373 |
|
|
free(wl->status_text); |
374 |
|
|
memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell); |
375 |
|
|
wl->status_text = status_print(c, wl, t, &wl->status_cell); |
376 |
|
|
wl->status_width = screen_write_cstrlen("%s", wl->status_text); |
377 |
|
|
|
378 |
|
|
if (wl == s->curw) |
379 |
|
|
wloffset = wlwidth; |
380 |
|
|
|
381 |
|
|
oo = wl->window->options; |
382 |
|
|
sep = options_get_string(oo, "window-status-separator"); |
383 |
|
|
seplen = screen_write_cstrlen("%s", sep); |
384 |
|
|
wlwidth += wl->status_width + seplen; |
385 |
|
|
} |
386 |
|
|
|
387 |
|
|
/* Create a new screen for the window list. */ |
388 |
|
|
screen_init(&window_list, wlwidth, 1, 0); |
389 |
|
|
|
390 |
|
|
/* And draw the window list into it. */ |
391 |
|
|
screen_write_start(&ctx, NULL, &window_list); |
392 |
|
|
RB_FOREACH(wl, winlinks, &s->windows) { |
393 |
|
|
screen_write_cnputs(&ctx, -1, &wl->status_cell, "%s", |
394 |
|
|
wl->status_text); |
395 |
|
|
|
396 |
|
|
oo = wl->window->options; |
397 |
|
|
sep = options_get_string(oo, "window-status-separator"); |
398 |
|
|
screen_write_cnputs(&ctx, -1, &stdgc, "%s", sep); |
399 |
|
|
} |
400 |
|
|
screen_write_stop(&ctx); |
401 |
|
|
|
402 |
|
|
/* If there is enough space for the total width, skip to draw now. */ |
403 |
|
|
if (wlwidth <= wlavailable) |
404 |
|
|
goto draw; |
405 |
|
|
|
406 |
|
|
/* Find size of current window text. */ |
407 |
|
|
wlsize = s->curw->status_width; |
408 |
|
|
|
409 |
|
|
/* |
410 |
|
|
* If the current window is already on screen, good to draw from the |
411 |
|
|
* start and just leave off the end. |
412 |
|
|
*/ |
413 |
|
|
if (wloffset + wlsize < wlavailable) { |
414 |
|
|
if (wlavailable > 0) { |
415 |
|
|
rarrow = 1; |
416 |
|
|
wlavailable--; |
417 |
|
|
} |
418 |
|
|
wlwidth = wlavailable; |
419 |
|
|
} else { |
420 |
|
|
/* |
421 |
|
|
* Work out how many characters we need to omit from the |
422 |
|
|
* start. There are wlavailable characters to fill, and |
423 |
|
|
* wloffset + wlsize must be the last. So, the start character |
424 |
|
|
* is wloffset + wlsize - wlavailable. |
425 |
|
|
*/ |
426 |
|
|
if (wlavailable > 0) { |
427 |
|
|
larrow = 1; |
428 |
|
|
wlavailable--; |
429 |
|
|
} |
430 |
|
|
|
431 |
|
|
wlstart = wloffset + wlsize - wlavailable; |
432 |
|
|
if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) { |
433 |
|
|
rarrow = 1; |
434 |
|
|
wlstart++; |
435 |
|
|
wlavailable--; |
436 |
|
|
} |
437 |
|
|
wlwidth = wlavailable; |
438 |
|
|
} |
439 |
|
|
|
440 |
|
|
/* Bail if anything is now too small too. */ |
441 |
|
|
if (wlwidth == 0 || wlavailable == 0) { |
442 |
|
|
screen_free(&window_list); |
443 |
|
|
goto out; |
444 |
|
|
} |
445 |
|
|
|
446 |
|
|
/* |
447 |
|
|
* Now the start position is known, work out the state of the left and |
448 |
|
|
* right arrows. |
449 |
|
|
*/ |
450 |
|
|
offset = 0; |
451 |
|
|
RB_FOREACH(wl, winlinks, &s->windows) { |
452 |
|
|
if (wl->flags & WINLINK_ALERTFLAGS && |
453 |
|
|
larrow == 1 && offset < wlstart) |
454 |
|
|
larrow = -1; |
455 |
|
|
|
456 |
|
|
offset += wl->status_width; |
457 |
|
|
|
458 |
|
|
if (wl->flags & WINLINK_ALERTFLAGS && |
459 |
|
|
rarrow == 1 && offset > wlstart + wlwidth) |
460 |
|
|
rarrow = -1; |
461 |
|
|
} |
462 |
|
|
|
463 |
|
|
draw: |
464 |
|
|
/* Begin drawing. */ |
465 |
|
|
screen_write_start(&ctx, NULL, &c->status); |
466 |
|
|
|
467 |
|
|
/* Draw the left string and arrow. */ |
468 |
|
|
screen_write_cursormove(&ctx, 0, 0); |
469 |
|
|
if (llen != 0) |
470 |
|
|
screen_write_cnputs(&ctx, llen, &lgc, "%s", left); |
471 |
|
|
if (larrow != 0) { |
472 |
|
|
memcpy(&gc, &stdgc, sizeof gc); |
473 |
|
|
if (larrow == -1) |
474 |
|
|
gc.attr ^= GRID_ATTR_REVERSE; |
475 |
|
|
screen_write_putc(&ctx, &gc, '<'); |
476 |
|
|
} |
477 |
|
|
|
478 |
|
|
/* Draw the right string and arrow. */ |
479 |
|
|
if (rarrow != 0) { |
480 |
|
|
screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0); |
481 |
|
|
memcpy(&gc, &stdgc, sizeof gc); |
482 |
|
|
if (rarrow == -1) |
483 |
|
|
gc.attr ^= GRID_ATTR_REVERSE; |
484 |
|
|
screen_write_putc(&ctx, &gc, '>'); |
485 |
|
|
} else |
486 |
|
|
screen_write_cursormove(&ctx, c->tty.sx - rlen, 0); |
487 |
|
|
if (rlen != 0) |
488 |
|
|
screen_write_cnputs(&ctx, rlen, &rgc, "%s", right); |
489 |
|
|
|
490 |
|
|
/* Figure out the offset for the window list. */ |
491 |
|
|
if (llen != 0) |
492 |
|
|
wloffset = llen; |
493 |
|
|
else |
494 |
|
|
wloffset = 0; |
495 |
|
|
if (wlwidth < wlavailable) { |
496 |
|
|
switch (options_get_number(s->options, "status-justify")) { |
497 |
|
|
case 1: /* centred */ |
498 |
|
|
wloffset += (wlavailable - wlwidth) / 2; |
499 |
|
|
break; |
500 |
|
|
case 2: /* right */ |
501 |
|
|
wloffset += (wlavailable - wlwidth); |
502 |
|
|
break; |
503 |
|
|
} |
504 |
|
|
} |
505 |
|
|
if (larrow != 0) |
506 |
|
|
wloffset++; |
507 |
|
|
|
508 |
|
|
/* Copy the window list. */ |
509 |
|
|
c->wlmouse = -wloffset + wlstart; |
510 |
|
|
screen_write_cursormove(&ctx, wloffset, 0); |
511 |
|
|
screen_write_fast_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1); |
512 |
|
|
screen_free(&window_list); |
513 |
|
|
|
514 |
|
|
screen_write_stop(&ctx); |
515 |
|
|
|
516 |
|
|
out: |
517 |
|
|
free(left); |
518 |
|
|
free(right); |
519 |
|
|
|
520 |
|
|
if (grid_compare(c->status.grid, old_status.grid) == 0) { |
521 |
|
|
screen_free(&old_status); |
522 |
|
|
return (0); |
523 |
|
|
} |
524 |
|
|
screen_free(&old_status); |
525 |
|
|
return (1); |
526 |
|
|
} |
527 |
|
|
|
528 |
|
|
/* Replace special sequences in fmt. */ |
529 |
|
|
static char * |
530 |
|
|
status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t) |
531 |
|
|
{ |
532 |
|
|
struct format_tree *ft; |
533 |
|
|
char *expanded; |
534 |
|
|
u_int tag; |
535 |
|
|
|
536 |
|
|
if (fmt == NULL) |
537 |
|
|
return (xstrdup("")); |
538 |
|
|
|
539 |
|
|
if (wl != NULL) |
540 |
|
|
tag = FORMAT_WINDOW|wl->window->id; |
541 |
|
|
else |
542 |
|
|
tag = FORMAT_NONE; |
543 |
|
|
if (c->flags & CLIENT_STATUSFORCE) |
544 |
|
|
ft = format_create(c, NULL, tag, FORMAT_STATUS|FORMAT_FORCE); |
545 |
|
|
else |
546 |
|
|
ft = format_create(c, NULL, tag, FORMAT_STATUS); |
547 |
|
|
format_defaults(ft, c, NULL, wl, NULL); |
548 |
|
|
|
549 |
|
|
expanded = format_expand_time(ft, fmt, t); |
550 |
|
|
|
551 |
|
|
format_free(ft); |
552 |
|
|
return (expanded); |
553 |
|
|
} |
554 |
|
|
|
555 |
|
|
/* Return winlink status line entry and adjust gc as necessary. */ |
556 |
|
|
static char * |
557 |
|
|
status_print(struct client *c, struct winlink *wl, time_t t, |
558 |
|
|
struct grid_cell *gc) |
559 |
|
|
{ |
560 |
|
|
struct options *oo = wl->window->options; |
561 |
|
|
struct session *s = c->session; |
562 |
|
|
const char *fmt; |
563 |
|
|
char *text; |
564 |
|
|
|
565 |
|
|
style_apply_update(gc, oo, "window-status-style"); |
566 |
|
|
fmt = options_get_string(oo, "window-status-format"); |
567 |
|
|
if (wl == s->curw) { |
568 |
|
|
style_apply_update(gc, oo, "window-status-current-style"); |
569 |
|
|
fmt = options_get_string(oo, "window-status-current-format"); |
570 |
|
|
} |
571 |
|
|
if (wl == TAILQ_FIRST(&s->lastw)) |
572 |
|
|
style_apply_update(gc, oo, "window-status-last-style"); |
573 |
|
|
|
574 |
|
|
if (wl->flags & WINLINK_BELL) |
575 |
|
|
style_apply_update(gc, oo, "window-status-bell-style"); |
576 |
|
|
else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE)) |
577 |
|
|
style_apply_update(gc, oo, "window-status-activity-style"); |
578 |
|
|
|
579 |
|
|
text = status_replace(c, wl, fmt, t); |
580 |
|
|
return (text); |
581 |
|
|
} |
582 |
|
|
|
583 |
|
|
/* Set a status line message. */ |
584 |
|
|
void |
585 |
|
|
status_message_set(struct client *c, const char *fmt, ...) |
586 |
|
|
{ |
587 |
|
|
struct timeval tv; |
588 |
|
|
va_list ap; |
589 |
|
|
int delay; |
590 |
|
|
|
591 |
|
|
status_message_clear(c); |
592 |
|
|
|
593 |
|
|
if (c->old_status == NULL) { |
594 |
|
|
c->old_status = xmalloc(sizeof *c->old_status); |
595 |
|
|
memcpy(c->old_status, &c->status, sizeof *c->old_status); |
596 |
|
|
screen_init(&c->status, c->tty.sx, 1, 0); |
597 |
|
|
} |
598 |
|
|
|
599 |
|
|
va_start(ap, fmt); |
600 |
|
|
xvasprintf(&c->message_string, fmt, ap); |
601 |
|
|
va_end(ap); |
602 |
|
|
|
603 |
|
|
server_client_add_message(c, "%s", c->message_string); |
604 |
|
|
|
605 |
|
|
delay = options_get_number(c->session->options, "display-time"); |
606 |
|
|
if (delay > 0) { |
607 |
|
|
tv.tv_sec = delay / 1000; |
608 |
|
|
tv.tv_usec = (delay % 1000) * 1000L; |
609 |
|
|
|
610 |
|
|
if (event_initialized(&c->message_timer)) |
611 |
|
|
evtimer_del(&c->message_timer); |
612 |
|
|
evtimer_set(&c->message_timer, status_message_callback, c); |
613 |
|
|
evtimer_add(&c->message_timer, &tv); |
614 |
|
|
} |
615 |
|
|
|
616 |
|
|
c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); |
617 |
|
|
c->flags |= CLIENT_STATUS; |
618 |
|
|
} |
619 |
|
|
|
620 |
|
|
/* Clear status line message. */ |
621 |
|
|
void |
622 |
|
|
status_message_clear(struct client *c) |
623 |
|
|
{ |
624 |
|
|
if (c->message_string == NULL) |
625 |
|
|
return; |
626 |
|
|
|
627 |
|
|
free(c->message_string); |
628 |
|
|
c->message_string = NULL; |
629 |
|
|
|
630 |
|
|
if (c->prompt_string == NULL) |
631 |
|
|
c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); |
632 |
|
|
c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ |
633 |
|
|
|
634 |
|
|
screen_reinit(&c->status); |
635 |
|
|
} |
636 |
|
|
|
637 |
|
|
/* Clear status line message after timer expires. */ |
638 |
|
|
static void |
639 |
|
|
status_message_callback(__unused int fd, __unused short event, void *data) |
640 |
|
|
{ |
641 |
|
|
struct client *c = data; |
642 |
|
|
|
643 |
|
|
status_message_clear(c); |
644 |
|
|
} |
645 |
|
|
|
646 |
|
|
/* Draw client message on status line of present else on last line. */ |
647 |
|
|
int |
648 |
|
|
status_message_redraw(struct client *c) |
649 |
|
|
{ |
650 |
|
|
struct screen_write_ctx ctx; |
651 |
|
|
struct session *s = c->session; |
652 |
|
|
struct screen old_status; |
653 |
|
|
size_t len; |
654 |
|
|
struct grid_cell gc; |
655 |
|
|
u_int lines, offset; |
656 |
|
|
|
657 |
|
|
if (c->tty.sx == 0 || c->tty.sy == 0) |
658 |
|
|
return (0); |
659 |
|
|
memcpy(&old_status, &c->status, sizeof old_status); |
660 |
|
|
|
661 |
|
|
lines = status_line_size(c->session); |
662 |
|
|
if (lines <= 1) |
663 |
|
|
screen_init(&c->status, c->tty.sx, 1, 0); |
664 |
|
|
else |
665 |
|
|
screen_init(&c->status, c->tty.sx, lines, 0); |
666 |
|
|
|
667 |
|
|
len = screen_write_strlen("%s", c->message_string); |
668 |
|
|
if (len > c->tty.sx) |
669 |
|
|
len = c->tty.sx; |
670 |
|
|
|
671 |
|
|
style_apply(&gc, s->options, "message-style"); |
672 |
|
|
|
673 |
|
|
screen_write_start(&ctx, NULL, &c->status); |
674 |
|
|
screen_write_cursormove(&ctx, 0, 0); |
675 |
|
|
for (offset = 0; offset < lines * c->tty.sx; offset++) |
676 |
|
|
screen_write_putc(&ctx, &gc, ' '); |
677 |
|
|
screen_write_cursormove(&ctx, 0, lines - 1); |
678 |
|
|
screen_write_nputs(&ctx, len, &gc, "%s", c->message_string); |
679 |
|
|
screen_write_stop(&ctx); |
680 |
|
|
|
681 |
|
|
if (grid_compare(c->status.grid, old_status.grid) == 0) { |
682 |
|
|
screen_free(&old_status); |
683 |
|
|
return (0); |
684 |
|
|
} |
685 |
|
|
screen_free(&old_status); |
686 |
|
|
return (1); |
687 |
|
|
} |
688 |
|
|
|
689 |
|
|
/* Enable status line prompt. */ |
690 |
|
|
void |
691 |
|
|
status_prompt_set(struct client *c, const char *msg, const char *input, |
692 |
|
|
prompt_input_cb inputcb, prompt_free_cb freecb, void *data, int flags) |
693 |
|
|
{ |
694 |
|
|
struct format_tree *ft; |
695 |
|
|
time_t t; |
696 |
|
|
char *tmp, *cp; |
697 |
|
|
|
698 |
|
|
ft = format_create(c, NULL, FORMAT_NONE, 0); |
699 |
|
|
format_defaults(ft, c, NULL, NULL, NULL); |
700 |
|
|
t = time(NULL); |
701 |
|
|
|
702 |
|
|
if (input == NULL) |
703 |
|
|
input = ""; |
704 |
|
|
if (flags & PROMPT_NOFORMAT) |
705 |
|
|
tmp = xstrdup(input); |
706 |
|
|
else |
707 |
|
|
tmp = format_expand_time(ft, input, t); |
708 |
|
|
|
709 |
|
|
status_message_clear(c); |
710 |
|
|
status_prompt_clear(c); |
711 |
|
|
|
712 |
|
|
if (c->old_status == NULL) { |
713 |
|
|
c->old_status = xmalloc(sizeof *c->old_status); |
714 |
|
|
memcpy(c->old_status, &c->status, sizeof *c->old_status); |
715 |
|
|
screen_init(&c->status, c->tty.sx, 1, 0); |
716 |
|
|
} |
717 |
|
|
|
718 |
|
|
c->prompt_string = format_expand_time(ft, msg, t); |
719 |
|
|
|
720 |
|
|
c->prompt_buffer = utf8_fromcstr(tmp); |
721 |
|
|
c->prompt_index = utf8_strlen(c->prompt_buffer); |
722 |
|
|
|
723 |
|
|
c->prompt_inputcb = inputcb; |
724 |
|
|
c->prompt_freecb = freecb; |
725 |
|
|
c->prompt_data = data; |
726 |
|
|
|
727 |
|
|
c->prompt_hindex = 0; |
728 |
|
|
|
729 |
|
|
c->prompt_flags = flags; |
730 |
|
|
c->prompt_mode = PROMPT_ENTRY; |
731 |
|
|
|
732 |
|
|
if (~flags & PROMPT_INCREMENTAL) |
733 |
|
|
c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); |
734 |
|
|
c->flags |= CLIENT_STATUS; |
735 |
|
|
|
736 |
|
|
if ((flags & PROMPT_INCREMENTAL) && *tmp != '\0') { |
737 |
|
|
xasprintf(&cp, "=%s", tmp); |
738 |
|
|
c->prompt_inputcb(c, c->prompt_data, cp, 0); |
739 |
|
|
free(cp); |
740 |
|
|
} |
741 |
|
|
|
742 |
|
|
free(tmp); |
743 |
|
|
format_free(ft); |
744 |
|
|
} |
745 |
|
|
|
746 |
|
|
/* Remove status line prompt. */ |
747 |
|
|
void |
748 |
|
|
status_prompt_clear(struct client *c) |
749 |
|
|
{ |
750 |
|
|
if (c->prompt_string == NULL) |
751 |
|
|
return; |
752 |
|
|
|
753 |
|
|
if (c->prompt_freecb != NULL && c->prompt_data != NULL) |
754 |
|
|
c->prompt_freecb(c->prompt_data); |
755 |
|
|
|
756 |
|
|
free(c->prompt_string); |
757 |
|
|
c->prompt_string = NULL; |
758 |
|
|
|
759 |
|
|
free(c->prompt_buffer); |
760 |
|
|
c->prompt_buffer = NULL; |
761 |
|
|
|
762 |
|
|
c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); |
763 |
|
|
c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ |
764 |
|
|
|
765 |
|
|
screen_reinit(&c->status); |
766 |
|
|
} |
767 |
|
|
|
768 |
|
|
/* Update status line prompt with a new prompt string. */ |
769 |
|
|
void |
770 |
|
|
status_prompt_update(struct client *c, const char *msg, const char *input) |
771 |
|
|
{ |
772 |
|
|
struct format_tree *ft; |
773 |
|
|
time_t t; |
774 |
|
|
char *tmp; |
775 |
|
|
|
776 |
|
|
ft = format_create(c, NULL, FORMAT_NONE, 0); |
777 |
|
|
format_defaults(ft, c, NULL, NULL, NULL); |
778 |
|
|
|
779 |
|
|
t = time(NULL); |
780 |
|
|
tmp = format_expand_time(ft, input, t); |
781 |
|
|
|
782 |
|
|
free(c->prompt_string); |
783 |
|
|
c->prompt_string = format_expand_time(ft, msg, t); |
784 |
|
|
|
785 |
|
|
free(c->prompt_buffer); |
786 |
|
|
c->prompt_buffer = utf8_fromcstr(tmp); |
787 |
|
|
c->prompt_index = utf8_strlen(c->prompt_buffer); |
788 |
|
|
|
789 |
|
|
c->prompt_hindex = 0; |
790 |
|
|
|
791 |
|
|
c->flags |= CLIENT_STATUS; |
792 |
|
|
|
793 |
|
|
free(tmp); |
794 |
|
|
format_free(ft); |
795 |
|
|
} |
796 |
|
|
|
797 |
|
|
/* Draw client prompt on status line of present else on last line. */ |
798 |
|
|
int |
799 |
|
|
status_prompt_redraw(struct client *c) |
800 |
|
|
{ |
801 |
|
|
struct screen_write_ctx ctx; |
802 |
|
|
struct session *s = c->session; |
803 |
|
|
struct screen old_status; |
804 |
|
|
u_int i, offset, left, start, pcursor, pwidth, width; |
805 |
|
|
u_int lines; |
806 |
|
|
size_t len, off; |
807 |
|
|
struct grid_cell gc, cursorgc; |
808 |
|
|
|
809 |
|
|
if (c->tty.sx == 0 || c->tty.sy == 0) |
810 |
|
|
return (0); |
811 |
|
|
memcpy(&old_status, &c->status, sizeof old_status); |
812 |
|
|
|
813 |
|
|
lines = status_line_size(c->session); |
814 |
|
|
if (lines <= 1) |
815 |
|
|
screen_init(&c->status, c->tty.sx, 1, 0); |
816 |
|
|
else |
817 |
|
|
screen_init(&c->status, c->tty.sx, lines, 0); |
818 |
|
|
|
819 |
|
|
len = screen_write_strlen("%s", c->prompt_string); |
820 |
|
|
if (len > c->tty.sx) |
821 |
|
|
len = c->tty.sx; |
822 |
|
|
off = 0; |
823 |
|
|
|
824 |
|
|
if (c->prompt_mode == PROMPT_COMMAND) |
825 |
|
|
style_apply(&gc, s->options, "message-command-style"); |
826 |
|
|
else |
827 |
|
|
style_apply(&gc, s->options, "message-style"); |
828 |
|
|
|
829 |
|
|
memcpy(&cursorgc, &gc, sizeof cursorgc); |
830 |
|
|
cursorgc.attr ^= GRID_ATTR_REVERSE; |
831 |
|
|
|
832 |
|
|
start = screen_write_strlen("%s", c->prompt_string); |
833 |
|
|
if (start > c->tty.sx) |
834 |
|
|
start = c->tty.sx; |
835 |
|
|
|
836 |
|
|
screen_write_start(&ctx, NULL, &c->status); |
837 |
|
|
screen_write_cursormove(&ctx, 0, 0); |
838 |
|
|
for (offset = 0; offset < lines * c->tty.sx; offset++) |
839 |
|
|
screen_write_putc(&ctx, &gc, ' '); |
840 |
|
|
screen_write_cursormove(&ctx, 0, 0); |
841 |
|
|
screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string); |
842 |
|
|
screen_write_cursormove(&ctx, start, 0); |
843 |
|
|
|
844 |
|
|
left = c->tty.sx - start; |
845 |
|
|
if (left == 0) |
846 |
|
|
goto finished; |
847 |
|
|
|
848 |
|
|
pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index); |
849 |
|
|
pwidth = utf8_strwidth(c->prompt_buffer, -1); |
850 |
|
|
if (pcursor >= left) { |
851 |
|
|
/* |
852 |
|
|
* The cursor would be outside the screen so start drawing |
853 |
|
|
* with it on the right. |
854 |
|
|
*/ |
855 |
|
|
offset = (pcursor - left) + 1; |
856 |
|
|
pwidth = left; |
857 |
|
|
} else |
858 |
|
|
offset = 0; |
859 |
|
|
if (pwidth > left) |
860 |
|
|
pwidth = left; |
861 |
|
|
|
862 |
|
|
width = 0; |
863 |
|
|
for (i = 0; c->prompt_buffer[i].size != 0; i++) { |
864 |
|
|
if (width < offset) { |
865 |
|
|
width += c->prompt_buffer[i].width; |
866 |
|
|
continue; |
867 |
|
|
} |
868 |
|
|
if (width >= offset + pwidth) |
869 |
|
|
break; |
870 |
|
|
width += c->prompt_buffer[i].width; |
871 |
|
|
if (width > offset + pwidth) |
872 |
|
|
break; |
873 |
|
|
|
874 |
|
|
if (i != c->prompt_index) { |
875 |
|
|
utf8_copy(&gc.data, &c->prompt_buffer[i]); |
876 |
|
|
screen_write_cell(&ctx, &gc); |
877 |
|
|
} else { |
878 |
|
|
utf8_copy(&cursorgc.data, &c->prompt_buffer[i]); |
879 |
|
|
screen_write_cell(&ctx, &cursorgc); |
880 |
|
|
} |
881 |
|
|
} |
882 |
|
|
if (c->status.cx < screen_size_x(&c->status) && c->prompt_index >= i) |
883 |
|
|
screen_write_putc(&ctx, &cursorgc, ' '); |
884 |
|
|
|
885 |
|
|
finished: |
886 |
|
|
screen_write_stop(&ctx); |
887 |
|
|
|
888 |
|
|
if (grid_compare(c->status.grid, old_status.grid) == 0) { |
889 |
|
|
screen_free(&old_status); |
890 |
|
|
return (0); |
891 |
|
|
} |
892 |
|
|
screen_free(&old_status); |
893 |
|
|
return (1); |
894 |
|
|
} |
895 |
|
|
|
896 |
|
|
/* Is this a separator? */ |
897 |
|
|
static int |
898 |
|
|
status_prompt_in_list(const char *ws, const struct utf8_data *ud) |
899 |
|
|
{ |
900 |
|
|
if (ud->size != 1 || ud->width != 1) |
901 |
|
|
return (0); |
902 |
|
|
return (strchr(ws, *ud->data) != NULL); |
903 |
|
|
} |
904 |
|
|
|
905 |
|
|
/* Is this a space? */ |
906 |
|
|
static int |
907 |
|
|
status_prompt_space(const struct utf8_data *ud) |
908 |
|
|
{ |
909 |
|
|
if (ud->size != 1 || ud->width != 1) |
910 |
|
|
return (0); |
911 |
|
|
return (*ud->data == ' '); |
912 |
|
|
} |
913 |
|
|
|
914 |
|
|
/* |
915 |
|
|
* Translate key from emacs to vi. Return 0 to drop key, 1 to process the key |
916 |
|
|
* as an emacs key; return 2 to append to the buffer. |
917 |
|
|
*/ |
918 |
|
|
static int |
919 |
|
|
status_prompt_translate_key(struct client *c, key_code key, key_code *new_key) |
920 |
|
|
{ |
921 |
|
|
if (c->prompt_mode == PROMPT_ENTRY) { |
922 |
|
|
switch (key) { |
923 |
|
|
case '\003': /* C-c */ |
924 |
|
|
case '\010': /* C-h */ |
925 |
|
|
case '\011': /* Tab */ |
926 |
|
|
case '\025': /* C-u */ |
927 |
|
|
case '\027': /* C-w */ |
928 |
|
|
case '\n': |
929 |
|
|
case '\r': |
930 |
|
|
case KEYC_BSPACE: |
931 |
|
|
case KEYC_DC: |
932 |
|
|
case KEYC_DOWN: |
933 |
|
|
case KEYC_END: |
934 |
|
|
case KEYC_HOME: |
935 |
|
|
case KEYC_LEFT: |
936 |
|
|
case KEYC_RIGHT: |
937 |
|
|
case KEYC_UP: |
938 |
|
|
*new_key = key; |
939 |
|
|
return (1); |
940 |
|
|
case '\033': /* Escape */ |
941 |
|
|
c->prompt_mode = PROMPT_COMMAND; |
942 |
|
|
c->flags |= CLIENT_STATUS; |
943 |
|
|
return (0); |
944 |
|
|
} |
945 |
|
|
*new_key = key; |
946 |
|
|
return (2); |
947 |
|
|
} |
948 |
|
|
|
949 |
|
|
switch (key) { |
950 |
|
|
case 'A': |
951 |
|
|
case 'I': |
952 |
|
|
case 'C': |
953 |
|
|
case 's': |
954 |
|
|
case 'a': |
955 |
|
|
c->prompt_mode = PROMPT_ENTRY; |
956 |
|
|
c->flags |= CLIENT_STATUS; |
957 |
|
|
break; /* switch mode and... */ |
958 |
|
|
case 'S': |
959 |
|
|
c->prompt_mode = PROMPT_ENTRY; |
960 |
|
|
c->flags |= CLIENT_STATUS; |
961 |
|
|
*new_key = '\025'; /* C-u */ |
962 |
|
|
return (1); |
963 |
|
|
case 'i': |
964 |
|
|
case '\033': /* Escape */ |
965 |
|
|
c->prompt_mode = PROMPT_ENTRY; |
966 |
|
|
c->flags |= CLIENT_STATUS; |
967 |
|
|
return (0); |
968 |
|
|
} |
969 |
|
|
|
970 |
|
|
switch (key) { |
971 |
|
|
case 'A': |
972 |
|
|
case '$': |
973 |
|
|
*new_key = KEYC_END; |
974 |
|
|
return (1); |
975 |
|
|
case 'I': |
976 |
|
|
case '0': |
977 |
|
|
case '^': |
978 |
|
|
*new_key = KEYC_HOME; |
979 |
|
|
return (1); |
980 |
|
|
case 'C': |
981 |
|
|
case 'D': |
982 |
|
|
*new_key = '\013'; /* C-k */ |
983 |
|
|
return (1); |
984 |
|
|
case KEYC_BSPACE: |
985 |
|
|
case 'X': |
986 |
|
|
*new_key = KEYC_BSPACE; |
987 |
|
|
return (1); |
988 |
|
|
case 'b': |
989 |
|
|
case 'B': |
990 |
|
|
*new_key = 'b'|KEYC_ESCAPE; |
991 |
|
|
return (1); |
992 |
|
|
case 'd': |
993 |
|
|
*new_key = '\025'; |
994 |
|
|
return (1); |
995 |
|
|
case 'e': |
996 |
|
|
case 'E': |
997 |
|
|
case 'w': |
998 |
|
|
case 'W': |
999 |
|
|
*new_key = 'f'|KEYC_ESCAPE; |
1000 |
|
|
return (1); |
1001 |
|
|
case 'p': |
1002 |
|
|
*new_key = '\031'; /* C-y */ |
1003 |
|
|
return (1); |
1004 |
|
|
case 's': |
1005 |
|
|
case KEYC_DC: |
1006 |
|
|
case 'x': |
1007 |
|
|
*new_key = KEYC_DC; |
1008 |
|
|
return (1); |
1009 |
|
|
case KEYC_DOWN: |
1010 |
|
|
case 'j': |
1011 |
|
|
*new_key = KEYC_DOWN; |
1012 |
|
|
return (1); |
1013 |
|
|
case KEYC_LEFT: |
1014 |
|
|
case 'h': |
1015 |
|
|
*new_key = KEYC_LEFT; |
1016 |
|
|
return (1); |
1017 |
|
|
case 'a': |
1018 |
|
|
case KEYC_RIGHT: |
1019 |
|
|
case 'l': |
1020 |
|
|
*new_key = KEYC_RIGHT; |
1021 |
|
|
return (1); |
1022 |
|
|
case KEYC_UP: |
1023 |
|
|
case 'k': |
1024 |
|
|
*new_key = KEYC_UP; |
1025 |
|
|
return (1); |
1026 |
|
|
case '\010' /* C-h */: |
1027 |
|
|
case '\003' /* C-c */: |
1028 |
|
|
case '\n': |
1029 |
|
|
case '\r': |
1030 |
|
|
return (1); |
1031 |
|
|
} |
1032 |
|
|
return (0); |
1033 |
|
|
} |
1034 |
|
|
|
1035 |
|
|
/* Handle keys in prompt. */ |
1036 |
|
|
int |
1037 |
|
|
status_prompt_key(struct client *c, key_code key) |
1038 |
|
|
{ |
1039 |
|
|
struct options *oo = c->session->options; |
1040 |
|
|
struct paste_buffer *pb; |
1041 |
|
|
char *s, *cp, word[64], prefix = '='; |
1042 |
|
|
const char *histstr, *bufdata, *ws = NULL; |
1043 |
|
|
u_char ch; |
1044 |
|
|
size_t size, n, off, idx, bufsize, used; |
1045 |
|
|
struct utf8_data tmp, *first, *last, *ud; |
1046 |
|
|
int keys; |
1047 |
|
|
|
1048 |
|
|
size = utf8_strlen(c->prompt_buffer); |
1049 |
|
|
|
1050 |
|
|
if (c->prompt_flags & PROMPT_NUMERIC) { |
1051 |
|
|
if (key >= '0' && key <= '9') |
1052 |
|
|
goto append_key; |
1053 |
|
|
s = utf8_tocstr(c->prompt_buffer); |
1054 |
|
|
c->prompt_inputcb(c, c->prompt_data, s, 1); |
1055 |
|
|
status_prompt_clear(c); |
1056 |
|
|
free(s); |
1057 |
|
|
return (1); |
1058 |
|
|
} |
1059 |
|
|
|
1060 |
|
|
keys = options_get_number(c->session->options, "status-keys"); |
1061 |
|
|
if (keys == MODEKEY_VI) { |
1062 |
|
|
switch (status_prompt_translate_key(c, key, &key)) { |
1063 |
|
|
case 1: |
1064 |
|
|
goto process_key; |
1065 |
|
|
case 2: |
1066 |
|
|
goto append_key; |
1067 |
|
|
default: |
1068 |
|
|
return (0); |
1069 |
|
|
} |
1070 |
|
|
} |
1071 |
|
|
|
1072 |
|
|
process_key: |
1073 |
|
|
switch (key) { |
1074 |
|
|
case KEYC_LEFT: |
1075 |
|
|
case '\002': /* C-b */ |
1076 |
|
|
if (c->prompt_index > 0) { |
1077 |
|
|
c->prompt_index--; |
1078 |
|
|
break; |
1079 |
|
|
} |
1080 |
|
|
break; |
1081 |
|
|
case KEYC_RIGHT: |
1082 |
|
|
case '\006': /* C-f */ |
1083 |
|
|
if (c->prompt_index < size) { |
1084 |
|
|
c->prompt_index++; |
1085 |
|
|
break; |
1086 |
|
|
} |
1087 |
|
|
break; |
1088 |
|
|
case KEYC_HOME: |
1089 |
|
|
case '\001': /* C-a */ |
1090 |
|
|
if (c->prompt_index != 0) { |
1091 |
|
|
c->prompt_index = 0; |
1092 |
|
|
break; |
1093 |
|
|
} |
1094 |
|
|
break; |
1095 |
|
|
case KEYC_END: |
1096 |
|
|
case '\005': /* C-e */ |
1097 |
|
|
if (c->prompt_index != size) { |
1098 |
|
|
c->prompt_index = size; |
1099 |
|
|
break; |
1100 |
|
|
} |
1101 |
|
|
break; |
1102 |
|
|
case '\011': /* Tab */ |
1103 |
|
|
if (c->prompt_buffer[0].size == 0) |
1104 |
|
|
break; |
1105 |
|
|
|
1106 |
|
|
idx = c->prompt_index; |
1107 |
|
|
if (idx != 0) |
1108 |
|
|
idx--; |
1109 |
|
|
|
1110 |
|
|
/* Find the word we are in. */ |
1111 |
|
|
first = &c->prompt_buffer[idx]; |
1112 |
|
|
while (first > c->prompt_buffer && !status_prompt_space(first)) |
1113 |
|
|
first--; |
1114 |
|
|
while (first->size != 0 && status_prompt_space(first)) |
1115 |
|
|
first++; |
1116 |
|
|
last = &c->prompt_buffer[idx]; |
1117 |
|
|
while (last->size != 0 && !status_prompt_space(last)) |
1118 |
|
|
last++; |
1119 |
|
|
while (last > c->prompt_buffer && status_prompt_space(last)) |
1120 |
|
|
last--; |
1121 |
|
|
if (last->size != 0) |
1122 |
|
|
last++; |
1123 |
|
|
if (last <= first) |
1124 |
|
|
break; |
1125 |
|
|
|
1126 |
|
|
used = 0; |
1127 |
|
|
for (ud = first; ud < last; ud++) { |
1128 |
|
|
if (used + ud->size >= sizeof word) |
1129 |
|
|
break; |
1130 |
|
|
memcpy(word + used, ud->data, ud->size); |
1131 |
|
|
used += ud->size; |
1132 |
|
|
} |
1133 |
|
|
if (ud != last) |
1134 |
|
|
break; |
1135 |
|
|
word[used] = '\0'; |
1136 |
|
|
|
1137 |
|
|
/* And try to complete it. */ |
1138 |
|
|
if ((s = status_prompt_complete(c->session, word)) == NULL) |
1139 |
|
|
break; |
1140 |
|
|
|
1141 |
|
|
/* Trim out word. */ |
1142 |
|
|
n = size - (last - c->prompt_buffer) + 1; /* with \0 */ |
1143 |
|
|
memmove(first, last, n * sizeof *c->prompt_buffer); |
1144 |
|
|
size -= last - first; |
1145 |
|
|
|
1146 |
|
|
/* Insert the new word. */ |
1147 |
|
|
size += strlen(s); |
1148 |
|
|
off = first - c->prompt_buffer; |
1149 |
|
|
c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1, |
1150 |
|
|
sizeof *c->prompt_buffer); |
1151 |
|
|
first = c->prompt_buffer + off; |
1152 |
|
|
memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer); |
1153 |
|
|
for (idx = 0; idx < strlen(s); idx++) |
1154 |
|
|
utf8_set(&first[idx], s[idx]); |
1155 |
|
|
|
1156 |
|
|
c->prompt_index = (first - c->prompt_buffer) + strlen(s); |
1157 |
|
|
free(s); |
1158 |
|
|
|
1159 |
|
|
goto changed; |
1160 |
|
|
case KEYC_BSPACE: |
1161 |
|
|
case '\010': /* C-h */ |
1162 |
|
|
if (c->prompt_index != 0) { |
1163 |
|
|
if (c->prompt_index == size) |
1164 |
|
|
c->prompt_buffer[--c->prompt_index].size = 0; |
1165 |
|
|
else { |
1166 |
|
|
memmove(c->prompt_buffer + c->prompt_index - 1, |
1167 |
|
|
c->prompt_buffer + c->prompt_index, |
1168 |
|
|
(size + 1 - c->prompt_index) * |
1169 |
|
|
sizeof *c->prompt_buffer); |
1170 |
|
|
c->prompt_index--; |
1171 |
|
|
} |
1172 |
|
|
goto changed; |
1173 |
|
|
} |
1174 |
|
|
break; |
1175 |
|
|
case KEYC_DC: |
1176 |
|
|
case '\004': /* C-d */ |
1177 |
|
|
if (c->prompt_index != size) { |
1178 |
|
|
memmove(c->prompt_buffer + c->prompt_index, |
1179 |
|
|
c->prompt_buffer + c->prompt_index + 1, |
1180 |
|
|
(size + 1 - c->prompt_index) * |
1181 |
|
|
sizeof *c->prompt_buffer); |
1182 |
|
|
goto changed; |
1183 |
|
|
} |
1184 |
|
|
break; |
1185 |
|
|
case '\025': /* C-u */ |
1186 |
|
|
c->prompt_buffer[0].size = 0; |
1187 |
|
|
c->prompt_index = 0; |
1188 |
|
|
goto changed; |
1189 |
|
|
case '\013': /* C-k */ |
1190 |
|
|
if (c->prompt_index < size) { |
1191 |
|
|
c->prompt_buffer[c->prompt_index].size = 0; |
1192 |
|
|
goto changed; |
1193 |
|
|
} |
1194 |
|
|
break; |
1195 |
|
|
case '\027': /* C-w */ |
1196 |
|
|
ws = options_get_string(oo, "word-separators"); |
1197 |
|
|
idx = c->prompt_index; |
1198 |
|
|
|
1199 |
|
|
/* Find a non-separator. */ |
1200 |
|
|
while (idx != 0) { |
1201 |
|
|
idx--; |
1202 |
|
|
if (!status_prompt_in_list(ws, &c->prompt_buffer[idx])) |
1203 |
|
|
break; |
1204 |
|
|
} |
1205 |
|
|
|
1206 |
|
|
/* Find the separator at the beginning of the word. */ |
1207 |
|
|
while (idx != 0) { |
1208 |
|
|
idx--; |
1209 |
|
|
if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) { |
1210 |
|
|
/* Go back to the word. */ |
1211 |
|
|
idx++; |
1212 |
|
|
break; |
1213 |
|
|
} |
1214 |
|
|
} |
1215 |
|
|
|
1216 |
|
|
memmove(c->prompt_buffer + idx, |
1217 |
|
|
c->prompt_buffer + c->prompt_index, |
1218 |
|
|
(size + 1 - c->prompt_index) * |
1219 |
|
|
sizeof *c->prompt_buffer); |
1220 |
|
|
memset(c->prompt_buffer + size - (c->prompt_index - idx), |
1221 |
|
|
'\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer); |
1222 |
|
|
c->prompt_index = idx; |
1223 |
|
|
|
1224 |
|
|
goto changed; |
1225 |
|
|
case 'f'|KEYC_ESCAPE: |
1226 |
|
|
ws = options_get_string(oo, "word-separators"); |
1227 |
|
|
|
1228 |
|
|
/* Find a word. */ |
1229 |
|
|
while (c->prompt_index != size) { |
1230 |
|
|
idx = ++c->prompt_index; |
1231 |
|
|
if (!status_prompt_in_list(ws, &c->prompt_buffer[idx])) |
1232 |
|
|
break; |
1233 |
|
|
} |
1234 |
|
|
|
1235 |
|
|
/* Find the separator at the end of the word. */ |
1236 |
|
|
while (c->prompt_index != size) { |
1237 |
|
|
idx = ++c->prompt_index; |
1238 |
|
|
if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) |
1239 |
|
|
break; |
1240 |
|
|
} |
1241 |
|
|
|
1242 |
|
|
/* Back up to the end-of-word like vi. */ |
1243 |
|
|
if (options_get_number(oo, "status-keys") == MODEKEY_VI && |
1244 |
|
|
c->prompt_index != 0) |
1245 |
|
|
c->prompt_index--; |
1246 |
|
|
|
1247 |
|
|
goto changed; |
1248 |
|
|
case 'b'|KEYC_ESCAPE: |
1249 |
|
|
ws = options_get_string(oo, "word-separators"); |
1250 |
|
|
|
1251 |
|
|
/* Find a non-separator. */ |
1252 |
|
|
while (c->prompt_index != 0) { |
1253 |
|
|
idx = --c->prompt_index; |
1254 |
|
|
if (!status_prompt_in_list(ws, &c->prompt_buffer[idx])) |
1255 |
|
|
break; |
1256 |
|
|
} |
1257 |
|
|
|
1258 |
|
|
/* Find the separator at the beginning of the word. */ |
1259 |
|
|
while (c->prompt_index != 0) { |
1260 |
|
|
idx = --c->prompt_index; |
1261 |
|
|
if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) { |
1262 |
|
|
/* Go back to the word. */ |
1263 |
|
|
c->prompt_index++; |
1264 |
|
|
break; |
1265 |
|
|
} |
1266 |
|
|
} |
1267 |
|
|
goto changed; |
1268 |
|
|
case KEYC_UP: |
1269 |
|
|
case '\020': /* C-p */ |
1270 |
|
|
histstr = status_prompt_up_history(&c->prompt_hindex); |
1271 |
|
|
if (histstr == NULL) |
1272 |
|
|
break; |
1273 |
|
|
free(c->prompt_buffer); |
1274 |
|
|
c->prompt_buffer = utf8_fromcstr(histstr); |
1275 |
|
|
c->prompt_index = utf8_strlen(c->prompt_buffer); |
1276 |
|
|
goto changed; |
1277 |
|
|
case KEYC_DOWN: |
1278 |
|
|
case '\016': /* C-n */ |
1279 |
|
|
histstr = status_prompt_down_history(&c->prompt_hindex); |
1280 |
|
|
if (histstr == NULL) |
1281 |
|
|
break; |
1282 |
|
|
free(c->prompt_buffer); |
1283 |
|
|
c->prompt_buffer = utf8_fromcstr(histstr); |
1284 |
|
|
c->prompt_index = utf8_strlen(c->prompt_buffer); |
1285 |
|
|
goto changed; |
1286 |
|
|
case '\031': /* C-y */ |
1287 |
|
|
if ((pb = paste_get_top(NULL)) == NULL) |
1288 |
|
|
break; |
1289 |
|
|
bufdata = paste_buffer_data(pb, &bufsize); |
1290 |
|
|
for (n = 0; n < bufsize; n++) { |
1291 |
|
|
ch = (u_char)bufdata[n]; |
1292 |
|
|
if (ch < 32 || ch >= 127) |
1293 |
|
|
break; |
1294 |
|
|
} |
1295 |
|
|
|
1296 |
|
|
c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1, |
1297 |
|
|
sizeof *c->prompt_buffer); |
1298 |
|
|
if (c->prompt_index == size) { |
1299 |
|
|
for (idx = 0; idx < n; idx++) { |
1300 |
|
|
ud = &c->prompt_buffer[c->prompt_index + idx]; |
1301 |
|
|
utf8_set(ud, bufdata[idx]); |
1302 |
|
|
} |
1303 |
|
|
c->prompt_index += n; |
1304 |
|
|
c->prompt_buffer[c->prompt_index].size = 0; |
1305 |
|
|
} else { |
1306 |
|
|
memmove(c->prompt_buffer + c->prompt_index + n, |
1307 |
|
|
c->prompt_buffer + c->prompt_index, |
1308 |
|
|
(size + 1 - c->prompt_index) * |
1309 |
|
|
sizeof *c->prompt_buffer); |
1310 |
|
|
for (idx = 0; idx < n; idx++) { |
1311 |
|
|
ud = &c->prompt_buffer[c->prompt_index + idx]; |
1312 |
|
|
utf8_set(ud, bufdata[idx]); |
1313 |
|
|
} |
1314 |
|
|
c->prompt_index += n; |
1315 |
|
|
} |
1316 |
|
|
goto changed; |
1317 |
|
|
case '\024': /* C-t */ |
1318 |
|
|
idx = c->prompt_index; |
1319 |
|
|
if (idx < size) |
1320 |
|
|
idx++; |
1321 |
|
|
if (idx >= 2) { |
1322 |
|
|
utf8_copy(&tmp, &c->prompt_buffer[idx - 2]); |
1323 |
|
|
utf8_copy(&c->prompt_buffer[idx - 2], |
1324 |
|
|
&c->prompt_buffer[idx - 1]); |
1325 |
|
|
utf8_copy(&c->prompt_buffer[idx - 1], &tmp); |
1326 |
|
|
c->prompt_index = idx; |
1327 |
|
|
goto changed; |
1328 |
|
|
} |
1329 |
|
|
break; |
1330 |
|
|
case '\r': |
1331 |
|
|
case '\n': |
1332 |
|
|
s = utf8_tocstr(c->prompt_buffer); |
1333 |
|
|
if (*s != '\0') |
1334 |
|
|
status_prompt_add_history(s); |
1335 |
|
|
if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0) |
1336 |
|
|
status_prompt_clear(c); |
1337 |
|
|
free(s); |
1338 |
|
|
break; |
1339 |
|
|
case '\033': /* Escape */ |
1340 |
|
|
case '\003': /* C-c */ |
1341 |
|
|
if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0) |
1342 |
|
|
status_prompt_clear(c); |
1343 |
|
|
break; |
1344 |
|
|
case '\022': /* C-r */ |
1345 |
|
|
if (c->prompt_flags & PROMPT_INCREMENTAL) { |
1346 |
|
|
prefix = '-'; |
1347 |
|
|
goto changed; |
1348 |
|
|
} |
1349 |
|
|
break; |
1350 |
|
|
case '\023': /* C-s */ |
1351 |
|
|
if (c->prompt_flags & PROMPT_INCREMENTAL) { |
1352 |
|
|
prefix = '+'; |
1353 |
|
|
goto changed; |
1354 |
|
|
} |
1355 |
|
|
break; |
1356 |
|
|
default: |
1357 |
|
|
goto append_key; |
1358 |
|
|
} |
1359 |
|
|
|
1360 |
|
|
c->flags |= CLIENT_STATUS; |
1361 |
|
|
return (0); |
1362 |
|
|
|
1363 |
|
|
append_key: |
1364 |
|
|
if (key <= 0x1f || key >= KEYC_BASE) |
1365 |
|
|
return (0); |
1366 |
|
|
if (utf8_split(key, &tmp) != UTF8_DONE) |
1367 |
|
|
return (0); |
1368 |
|
|
|
1369 |
|
|
c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2, |
1370 |
|
|
sizeof *c->prompt_buffer); |
1371 |
|
|
|
1372 |
|
|
if (c->prompt_index == size) { |
1373 |
|
|
utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp); |
1374 |
|
|
c->prompt_index++; |
1375 |
|
|
c->prompt_buffer[c->prompt_index].size = 0; |
1376 |
|
|
} else { |
1377 |
|
|
memmove(c->prompt_buffer + c->prompt_index + 1, |
1378 |
|
|
c->prompt_buffer + c->prompt_index, |
1379 |
|
|
(size + 1 - c->prompt_index) * |
1380 |
|
|
sizeof *c->prompt_buffer); |
1381 |
|
|
utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp); |
1382 |
|
|
c->prompt_index++; |
1383 |
|
|
} |
1384 |
|
|
|
1385 |
|
|
if (c->prompt_flags & PROMPT_SINGLE) { |
1386 |
|
|
s = utf8_tocstr(c->prompt_buffer); |
1387 |
|
|
if (strlen(s) != 1) |
1388 |
|
|
status_prompt_clear(c); |
1389 |
|
|
else if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0) |
1390 |
|
|
status_prompt_clear(c); |
1391 |
|
|
free(s); |
1392 |
|
|
} |
1393 |
|
|
|
1394 |
|
|
changed: |
1395 |
|
|
c->flags |= CLIENT_STATUS; |
1396 |
|
|
if (c->prompt_flags & PROMPT_INCREMENTAL) { |
1397 |
|
|
s = utf8_tocstr(c->prompt_buffer); |
1398 |
|
|
xasprintf(&cp, "%c%s", prefix, s); |
1399 |
|
|
c->prompt_inputcb(c, c->prompt_data, cp, 0); |
1400 |
|
|
free(cp); |
1401 |
|
|
free(s); |
1402 |
|
|
} |
1403 |
|
|
return (0); |
1404 |
|
|
} |
1405 |
|
|
|
1406 |
|
|
/* Get previous line from the history. */ |
1407 |
|
|
static const char * |
1408 |
|
|
status_prompt_up_history(u_int *idx) |
1409 |
|
|
{ |
1410 |
|
|
/* |
1411 |
|
|
* History runs from 0 to size - 1. Index is from 0 to size. Zero is |
1412 |
|
|
* empty. |
1413 |
|
|
*/ |
1414 |
|
|
|
1415 |
|
|
if (status_prompt_hsize == 0 || *idx == status_prompt_hsize) |
1416 |
|
|
return (NULL); |
1417 |
|
|
(*idx)++; |
1418 |
|
|
return (status_prompt_hlist[status_prompt_hsize - *idx]); |
1419 |
|
|
} |
1420 |
|
|
|
1421 |
|
|
/* Get next line from the history. */ |
1422 |
|
|
static const char * |
1423 |
|
|
status_prompt_down_history(u_int *idx) |
1424 |
|
|
{ |
1425 |
|
|
if (status_prompt_hsize == 0 || *idx == 0) |
1426 |
|
|
return (""); |
1427 |
|
|
(*idx)--; |
1428 |
|
|
if (*idx == 0) |
1429 |
|
|
return (""); |
1430 |
|
|
return (status_prompt_hlist[status_prompt_hsize - *idx]); |
1431 |
|
|
} |
1432 |
|
|
|
1433 |
|
|
/* Add line to the history. */ |
1434 |
|
|
static void |
1435 |
|
|
status_prompt_add_history(const char *line) |
1436 |
|
|
{ |
1437 |
|
|
size_t size; |
1438 |
|
|
|
1439 |
|
|
if (status_prompt_hsize > 0 && |
1440 |
|
|
strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0) |
1441 |
|
|
return; |
1442 |
|
|
|
1443 |
|
|
if (status_prompt_hsize == PROMPT_HISTORY) { |
1444 |
|
|
free(status_prompt_hlist[0]); |
1445 |
|
|
|
1446 |
|
|
size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist; |
1447 |
|
|
memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size); |
1448 |
|
|
|
1449 |
|
|
status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line); |
1450 |
|
|
return; |
1451 |
|
|
} |
1452 |
|
|
|
1453 |
|
|
status_prompt_hlist = xreallocarray(status_prompt_hlist, |
1454 |
|
|
status_prompt_hsize + 1, sizeof *status_prompt_hlist); |
1455 |
|
|
status_prompt_hlist[status_prompt_hsize++] = xstrdup(line); |
1456 |
|
|
} |
1457 |
|
|
|
1458 |
|
|
/* Build completion list. */ |
1459 |
|
|
static const char ** |
1460 |
|
|
status_prompt_complete_list(u_int *size, const char *s) |
1461 |
|
|
{ |
1462 |
|
|
const char **list = NULL, **layout; |
1463 |
|
|
const struct cmd_entry **cmdent; |
1464 |
|
|
const struct options_table_entry *oe; |
1465 |
|
|
const char *layouts[] = { |
1466 |
|
|
"even-horizontal", "even-vertical", "main-horizontal", |
1467 |
|
|
"main-vertical", "tiled", NULL |
1468 |
|
|
}; |
1469 |
|
|
|
1470 |
|
|
*size = 0; |
1471 |
|
|
for (cmdent = cmd_table; *cmdent != NULL; cmdent++) { |
1472 |
|
|
if (strncmp((*cmdent)->name, s, strlen(s)) == 0) { |
1473 |
|
|
list = xreallocarray(list, (*size) + 1, sizeof *list); |
1474 |
|
|
list[(*size)++] = (*cmdent)->name; |
1475 |
|
|
} |
1476 |
|
|
} |
1477 |
|
|
for (oe = options_table; oe->name != NULL; oe++) { |
1478 |
|
|
if (strncmp(oe->name, s, strlen(s)) == 0) { |
1479 |
|
|
list = xreallocarray(list, (*size) + 1, sizeof *list); |
1480 |
|
|
list[(*size)++] = oe->name; |
1481 |
|
|
} |
1482 |
|
|
} |
1483 |
|
|
for (layout = layouts; *layout != NULL; layout++) { |
1484 |
|
|
if (strncmp(*layout, s, strlen(s)) == 0) { |
1485 |
|
|
list = xreallocarray(list, (*size) + 1, sizeof *list); |
1486 |
|
|
list[(*size)++] = *layout; |
1487 |
|
|
} |
1488 |
|
|
} |
1489 |
|
|
return (list); |
1490 |
|
|
} |
1491 |
|
|
|
1492 |
|
|
/* Find longest prefix. */ |
1493 |
|
|
static char * |
1494 |
|
|
status_prompt_complete_prefix(const char **list, u_int size) |
1495 |
|
|
{ |
1496 |
|
|
char *out; |
1497 |
|
|
u_int i; |
1498 |
|
|
size_t j; |
1499 |
|
|
|
1500 |
|
|
out = xstrdup(list[0]); |
1501 |
|
|
for (i = 1; i < size; i++) { |
1502 |
|
|
j = strlen(list[i]); |
1503 |
|
|
if (j > strlen(out)) |
1504 |
|
|
j = strlen(out); |
1505 |
|
|
for (; j > 0; j--) { |
1506 |
|
|
if (out[j - 1] != list[i][j - 1]) |
1507 |
|
|
out[j - 1] = '\0'; |
1508 |
|
|
} |
1509 |
|
|
} |
1510 |
|
|
return (out); |
1511 |
|
|
} |
1512 |
|
|
|
1513 |
|
|
/* Complete word. */ |
1514 |
|
|
static char * |
1515 |
|
|
status_prompt_complete(struct session *session, const char *s) |
1516 |
|
|
{ |
1517 |
|
|
const char **list = NULL, *colon; |
1518 |
|
|
u_int size = 0, i; |
1519 |
|
|
struct session *s_loop; |
1520 |
|
|
struct winlink *wl; |
1521 |
|
|
struct window *w; |
1522 |
|
|
char *copy, *out, *tmp; |
1523 |
|
|
|
1524 |
|
|
if (*s == '\0') |
1525 |
|
|
return (NULL); |
1526 |
|
|
out = NULL; |
1527 |
|
|
|
1528 |
|
|
if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) { |
1529 |
|
|
list = status_prompt_complete_list(&size, s); |
1530 |
|
|
if (size == 0) |
1531 |
|
|
out = NULL; |
1532 |
|
|
else if (size == 1) |
1533 |
|
|
xasprintf(&out, "%s ", list[0]); |
1534 |
|
|
else |
1535 |
|
|
out = status_prompt_complete_prefix(list, size); |
1536 |
|
|
free(list); |
1537 |
|
|
return (out); |
1538 |
|
|
} |
1539 |
|
|
copy = xstrdup(s); |
1540 |
|
|
|
1541 |
|
|
colon = ":"; |
1542 |
|
|
if (copy[strlen(copy) - 1] == ':') |
1543 |
|
|
copy[strlen(copy) - 1] = '\0'; |
1544 |
|
|
else |
1545 |
|
|
colon = ""; |
1546 |
|
|
s = copy + 2; |
1547 |
|
|
|
1548 |
|
|
RB_FOREACH(s_loop, sessions, &sessions) { |
1549 |
|
|
if (strncmp(s_loop->name, s, strlen(s)) == 0) { |
1550 |
|
|
list = xreallocarray(list, size + 2, sizeof *list); |
1551 |
|
|
list[size++] = s_loop->name; |
1552 |
|
|
} |
1553 |
|
|
} |
1554 |
|
|
if (size == 1) { |
1555 |
|
|
out = xstrdup(list[0]); |
1556 |
|
|
if (session_find(list[0]) != NULL) |
1557 |
|
|
colon = ":"; |
1558 |
|
|
} else if (size != 0) |
1559 |
|
|
out = status_prompt_complete_prefix(list, size); |
1560 |
|
|
if (out != NULL) { |
1561 |
|
|
xasprintf(&tmp, "-%c%s%s", copy[1], out, colon); |
1562 |
|
|
free(out); |
1563 |
|
|
out = tmp; |
1564 |
|
|
goto found; |
1565 |
|
|
} |
1566 |
|
|
|
1567 |
|
|
colon = ""; |
1568 |
|
|
if (*s == ':') { |
1569 |
|
|
RB_FOREACH(wl, winlinks, &session->windows) { |
1570 |
|
|
xasprintf(&tmp, ":%s", wl->window->name); |
1571 |
|
|
if (strncmp(tmp, s, strlen(s)) == 0){ |
1572 |
|
|
list = xreallocarray(list, size + 1, |
1573 |
|
|
sizeof *list); |
1574 |
|
|
list[size++] = tmp; |
1575 |
|
|
continue; |
1576 |
|
|
} |
1577 |
|
|
free(tmp); |
1578 |
|
|
|
1579 |
|
|
xasprintf(&tmp, ":%d", wl->idx); |
1580 |
|
|
if (strncmp(tmp, s, strlen(s)) == 0) { |
1581 |
|
|
list = xreallocarray(list, size + 1, |
1582 |
|
|
sizeof *list); |
1583 |
|
|
list[size++] = tmp; |
1584 |
|
|
continue; |
1585 |
|
|
} |
1586 |
|
|
free(tmp); |
1587 |
|
|
} |
1588 |
|
|
} else { |
1589 |
|
|
RB_FOREACH(s_loop, sessions, &sessions) { |
1590 |
|
|
RB_FOREACH(wl, winlinks, &s_loop->windows) { |
1591 |
|
|
w = wl->window; |
1592 |
|
|
|
1593 |
|
|
xasprintf(&tmp, "%s:%s", s_loop->name, w->name); |
1594 |
|
|
if (strncmp(tmp, s, strlen(s)) == 0) { |
1595 |
|
|
list = xreallocarray(list, size + 1, |
1596 |
|
|
sizeof *list); |
1597 |
|
|
list[size++] = tmp; |
1598 |
|
|
continue; |
1599 |
|
|
} |
1600 |
|
|
free(tmp); |
1601 |
|
|
|
1602 |
|
|
xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx); |
1603 |
|
|
if (strncmp(tmp, s, strlen(s)) == 0) { |
1604 |
|
|
list = xreallocarray(list, size + 1, |
1605 |
|
|
sizeof *list); |
1606 |
|
|
list[size++] = tmp; |
1607 |
|
|
continue; |
1608 |
|
|
} |
1609 |
|
|
free(tmp); |
1610 |
|
|
} |
1611 |
|
|
} |
1612 |
|
|
} |
1613 |
|
|
if (size == 1) { |
1614 |
|
|
out = xstrdup(list[0]); |
1615 |
|
|
colon = " "; |
1616 |
|
|
} else if (size != 0) |
1617 |
|
|
out = status_prompt_complete_prefix(list, size); |
1618 |
|
|
if (out != NULL) { |
1619 |
|
|
xasprintf(&tmp, "-%c%s%s", copy[1], out, colon); |
1620 |
|
|
out = tmp; |
1621 |
|
|
} |
1622 |
|
|
|
1623 |
|
|
for (i = 0; i < size; i++) |
1624 |
|
|
free((void *)list[i]); |
1625 |
|
|
|
1626 |
|
|
found: |
1627 |
|
|
free(copy); |
1628 |
|
|
free(list); |
1629 |
|
|
return (out); |
1630 |
|
|
} |