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