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