status.c revision 1.207
1/* $OpenBSD: status.c,v 1.207 2020/05/16 15:19:04 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 void status_message_callback(int, short, void *); 33static void status_timer_callback(int, short, void *); 34 35static char *status_prompt_find_history_file(void); 36static const char *status_prompt_up_history(u_int *); 37static const char *status_prompt_down_history(u_int *); 38static void status_prompt_add_history(const char *); 39 40static char *status_prompt_complete(struct client *, const char *, u_int); 41static char *status_prompt_complete_window_menu(struct client *, 42 struct session *, const char *, u_int, char); 43 44struct status_prompt_menu { 45 struct client *c; 46 u_int start; 47 u_int size; 48 char **list; 49 char flag; 50}; 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_REDRAWSTATUS; 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_cache(struct session *s) 198{ 199 s->statuslines = options_get_number(s->options, "status"); 200 if (s->statuslines == 0) 201 s->statusat = -1; 202 else if (options_get_number(s->options, "status-position") == 0) 203 s->statusat = 0; 204 else 205 s->statusat = 1; 206} 207 208/* Get screen line of status line. -1 means off. */ 209int 210status_at_line(struct client *c) 211{ 212 struct session *s = c->session; 213 214 if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL)) 215 return (-1); 216 if (s->statusat != 1) 217 return (s->statusat); 218 return (c->tty.sy - status_line_size(c)); 219} 220 221/* Get size of status line for client's session. 0 means off. */ 222u_int 223status_line_size(struct client *c) 224{ 225 struct session *s = c->session; 226 227 if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL)) 228 return (0); 229 return (s->statuslines); 230} 231 232/* Get window at window list position. */ 233struct style_range * 234status_get_range(struct client *c, u_int x, u_int y) 235{ 236 struct status_line *sl = &c->status; 237 struct style_range *sr; 238 239 if (y >= nitems(sl->entries)) 240 return (NULL); 241 TAILQ_FOREACH(sr, &sl->entries[y].ranges, entry) { 242 if (x >= sr->start && x < sr->end) 243 return (sr); 244 } 245 return (NULL); 246} 247 248/* Free all ranges. */ 249static void 250status_free_ranges(struct style_ranges *srs) 251{ 252 struct style_range *sr, *sr1; 253 254 TAILQ_FOREACH_SAFE(sr, srs, entry, sr1) { 255 TAILQ_REMOVE(srs, sr, entry); 256 free(sr); 257 } 258} 259 260/* Save old status line. */ 261static void 262status_push_screen(struct client *c) 263{ 264 struct status_line *sl = &c->status; 265 266 if (sl->active == &sl->screen) { 267 sl->active = xmalloc(sizeof *sl->active); 268 screen_init(sl->active, c->tty.sx, status_line_size(c), 0); 269 } 270 sl->references++; 271} 272 273/* Restore old status line. */ 274static void 275status_pop_screen(struct client *c) 276{ 277 struct status_line *sl = &c->status; 278 279 if (--sl->references == 0) { 280 screen_free(sl->active); 281 free(sl->active); 282 sl->active = &sl->screen; 283 } 284} 285 286/* Initialize status line. */ 287void 288status_init(struct client *c) 289{ 290 struct status_line *sl = &c->status; 291 u_int i; 292 293 for (i = 0; i < nitems(sl->entries); i++) 294 TAILQ_INIT(&sl->entries[i].ranges); 295 296 screen_init(&sl->screen, c->tty.sx, 1, 0); 297 sl->active = &sl->screen; 298} 299 300/* Free status line. */ 301void 302status_free(struct client *c) 303{ 304 struct status_line *sl = &c->status; 305 u_int i; 306 307 for (i = 0; i < nitems(sl->entries); i++) { 308 status_free_ranges(&sl->entries[i].ranges); 309 free((void *)sl->entries[i].expanded); 310 } 311 312 if (event_initialized(&sl->timer)) 313 evtimer_del(&sl->timer); 314 315 if (sl->active != &sl->screen) { 316 screen_free(sl->active); 317 free(sl->active); 318 } 319 screen_free(&sl->screen); 320} 321 322/* Draw status line for client. */ 323int 324status_redraw(struct client *c) 325{ 326 struct status_line *sl = &c->status; 327 struct status_line_entry *sle; 328 struct session *s = c->session; 329 struct screen_write_ctx ctx; 330 struct grid_cell gc; 331 u_int lines, i, n, width = c->tty.sx; 332 int flags, force = 0, changed = 0, fg, bg; 333 struct options_entry *o; 334 union options_value *ov; 335 struct format_tree *ft; 336 char *expanded; 337 338 log_debug("%s enter", __func__); 339 340 /* Shouldn't get here if not the active screen. */ 341 if (sl->active != &sl->screen) 342 fatalx("not the active screen"); 343 344 /* No status line? */ 345 lines = status_line_size(c); 346 if (c->tty.sy == 0 || lines == 0) 347 return (1); 348 349 /* Create format tree. */ 350 flags = FORMAT_STATUS; 351 if (c->flags & CLIENT_STATUSFORCE) 352 flags |= FORMAT_FORCE; 353 ft = format_create(c, NULL, FORMAT_NONE, flags); 354 format_defaults(ft, c, NULL, NULL, NULL); 355 356 /* Set up default colour. */ 357 style_apply(&gc, s->options, "status-style", ft); 358 fg = options_get_number(s->options, "status-fg"); 359 if (fg != 8) 360 gc.fg = fg; 361 bg = options_get_number(s->options, "status-bg"); 362 if (bg != 8) 363 gc.bg = bg; 364 if (!grid_cells_equal(&gc, &sl->style)) { 365 force = 1; 366 memcpy(&sl->style, &gc, sizeof sl->style); 367 } 368 369 /* Resize the target screen. */ 370 if (screen_size_x(&sl->screen) != width || 371 screen_size_y(&sl->screen) != lines) { 372 screen_resize(&sl->screen, width, lines, 0); 373 changed = force = 1; 374 } 375 screen_write_start(&ctx, NULL, &sl->screen); 376 377 /* Write the status lines. */ 378 o = options_get(s->options, "status-format"); 379 if (o == NULL) { 380 for (n = 0; n < width * lines; n++) 381 screen_write_putc(&ctx, &gc, ' '); 382 } else { 383 for (i = 0; i < lines; i++) { 384 screen_write_cursormove(&ctx, 0, i, 0); 385 386 ov = options_array_get(o, i); 387 if (ov == NULL) { 388 for (n = 0; n < width; n++) 389 screen_write_putc(&ctx, &gc, ' '); 390 continue; 391 } 392 sle = &sl->entries[i]; 393 394 expanded = format_expand_time(ft, ov->string); 395 if (!force && 396 sle->expanded != NULL && 397 strcmp(expanded, sle->expanded) == 0) { 398 free(expanded); 399 continue; 400 } 401 changed = 1; 402 403 for (n = 0; n < width; n++) 404 screen_write_putc(&ctx, &gc, ' '); 405 screen_write_cursormove(&ctx, 0, i, 0); 406 407 status_free_ranges(&sle->ranges); 408 format_draw(&ctx, &gc, width, expanded, &sle->ranges); 409 410 free(sle->expanded); 411 sle->expanded = expanded; 412 } 413 } 414 screen_write_stop(&ctx); 415 416 /* Free the format tree. */ 417 format_free(ft); 418 419 /* Return if the status line has changed. */ 420 log_debug("%s exit: force=%d, changed=%d", __func__, force, changed); 421 return (force || changed); 422} 423 424/* Set a status line message. */ 425void 426status_message_set(struct client *c, const char *fmt, ...) 427{ 428 struct timeval tv; 429 va_list ap; 430 int delay; 431 432 status_message_clear(c); 433 status_push_screen(c); 434 435 va_start(ap, fmt); 436 xvasprintf(&c->message_string, fmt, ap); 437 va_end(ap); 438 439 server_client_add_message(c, "%s", c->message_string); 440 441 delay = options_get_number(c->session->options, "display-time"); 442 if (delay > 0) { 443 tv.tv_sec = delay / 1000; 444 tv.tv_usec = (delay % 1000) * 1000L; 445 446 if (event_initialized(&c->message_timer)) 447 evtimer_del(&c->message_timer); 448 evtimer_set(&c->message_timer, status_message_callback, c); 449 evtimer_add(&c->message_timer, &tv); 450 } 451 452 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); 453 c->flags |= CLIENT_REDRAWSTATUS; 454} 455 456/* Clear status line message. */ 457void 458status_message_clear(struct client *c) 459{ 460 if (c->message_string == NULL) 461 return; 462 463 free(c->message_string); 464 c->message_string = NULL; 465 466 if (c->prompt_string == NULL) 467 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); 468 c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */ 469 470 status_pop_screen(c); 471} 472 473/* Clear status line message after timer expires. */ 474static void 475status_message_callback(__unused int fd, __unused short event, void *data) 476{ 477 struct client *c = data; 478 479 status_message_clear(c); 480} 481 482/* Draw client message on status line of present else on last line. */ 483int 484status_message_redraw(struct client *c) 485{ 486 struct status_line *sl = &c->status; 487 struct screen_write_ctx ctx; 488 struct session *s = c->session; 489 struct screen old_screen; 490 size_t len; 491 u_int lines, offset; 492 struct grid_cell gc; 493 struct format_tree *ft; 494 495 if (c->tty.sx == 0 || c->tty.sy == 0) 496 return (0); 497 memcpy(&old_screen, sl->active, sizeof old_screen); 498 499 lines = status_line_size(c); 500 if (lines <= 1) 501 lines = 1; 502 screen_init(sl->active, c->tty.sx, lines, 0); 503 504 len = screen_write_strlen("%s", c->message_string); 505 if (len > c->tty.sx) 506 len = c->tty.sx; 507 508 ft = format_create_defaults(NULL, c, NULL, NULL, NULL); 509 style_apply(&gc, s->options, "message-style", ft); 510 format_free(ft); 511 512 screen_write_start(&ctx, NULL, sl->active); 513 screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines - 1); 514 screen_write_cursormove(&ctx, 0, lines - 1, 0); 515 for (offset = 0; offset < c->tty.sx; offset++) 516 screen_write_putc(&ctx, &gc, ' '); 517 screen_write_cursormove(&ctx, 0, lines - 1, 0); 518 screen_write_nputs(&ctx, len, &gc, "%s", c->message_string); 519 screen_write_stop(&ctx); 520 521 if (grid_compare(sl->active->grid, old_screen.grid) == 0) { 522 screen_free(&old_screen); 523 return (0); 524 } 525 screen_free(&old_screen); 526 return (1); 527} 528 529/* Enable status line prompt. */ 530void 531status_prompt_set(struct client *c, const char *msg, const char *input, 532 prompt_input_cb inputcb, prompt_free_cb freecb, void *data, int flags) 533{ 534 struct format_tree *ft; 535 char *tmp, *cp; 536 537 ft = format_create(c, NULL, FORMAT_NONE, 0); 538 format_defaults(ft, c, NULL, NULL, NULL); 539 540 if (input == NULL) 541 input = ""; 542 if (flags & PROMPT_NOFORMAT) 543 tmp = xstrdup(input); 544 else 545 tmp = format_expand_time(ft, input); 546 547 status_message_clear(c); 548 status_prompt_clear(c); 549 status_push_screen(c); 550 551 c->prompt_string = format_expand_time(ft, msg); 552 553 c->prompt_buffer = utf8_fromcstr(tmp); 554 c->prompt_index = utf8_strlen(c->prompt_buffer); 555 556 c->prompt_inputcb = inputcb; 557 c->prompt_freecb = freecb; 558 c->prompt_data = data; 559 560 c->prompt_hindex = 0; 561 562 c->prompt_flags = flags; 563 c->prompt_mode = PROMPT_ENTRY; 564 565 if (~flags & PROMPT_INCREMENTAL) 566 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); 567 c->flags |= CLIENT_REDRAWSTATUS; 568 569 if ((flags & PROMPT_INCREMENTAL) && *tmp != '\0') { 570 xasprintf(&cp, "=%s", tmp); 571 c->prompt_inputcb(c, c->prompt_data, cp, 0); 572 free(cp); 573 } 574 575 free(tmp); 576 format_free(ft); 577} 578 579/* Remove status line prompt. */ 580void 581status_prompt_clear(struct client *c) 582{ 583 if (c->prompt_string == NULL) 584 return; 585 586 if (c->prompt_freecb != NULL && c->prompt_data != NULL) 587 c->prompt_freecb(c->prompt_data); 588 589 free(c->prompt_string); 590 c->prompt_string = NULL; 591 592 free(c->prompt_buffer); 593 c->prompt_buffer = NULL; 594 595 free(c->prompt_saved); 596 c->prompt_saved = NULL; 597 598 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); 599 c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */ 600 601 status_pop_screen(c); 602} 603 604/* Update status line prompt with a new prompt string. */ 605void 606status_prompt_update(struct client *c, const char *msg, const char *input) 607{ 608 struct format_tree *ft; 609 char *tmp; 610 611 ft = format_create(c, NULL, FORMAT_NONE, 0); 612 format_defaults(ft, c, NULL, NULL, NULL); 613 614 tmp = format_expand_time(ft, input); 615 616 free(c->prompt_string); 617 c->prompt_string = format_expand_time(ft, msg); 618 619 free(c->prompt_buffer); 620 c->prompt_buffer = utf8_fromcstr(tmp); 621 c->prompt_index = utf8_strlen(c->prompt_buffer); 622 623 c->prompt_hindex = 0; 624 625 c->flags |= CLIENT_REDRAWSTATUS; 626 627 free(tmp); 628 format_free(ft); 629} 630 631/* Draw client prompt on status line of present else on last line. */ 632int 633status_prompt_redraw(struct client *c) 634{ 635 struct status_line *sl = &c->status; 636 struct screen_write_ctx ctx; 637 struct session *s = c->session; 638 struct screen old_screen; 639 u_int i, lines, offset, left, start, width; 640 u_int pcursor, pwidth; 641 struct grid_cell gc, cursorgc; 642 struct format_tree *ft; 643 644 if (c->tty.sx == 0 || c->tty.sy == 0) 645 return (0); 646 memcpy(&old_screen, sl->active, sizeof old_screen); 647 648 lines = status_line_size(c); 649 if (lines <= 1) 650 lines = 1; 651 screen_init(sl->active, c->tty.sx, lines, 0); 652 653 ft = format_create_defaults(NULL, c, NULL, NULL, NULL); 654 if (c->prompt_mode == PROMPT_COMMAND) 655 style_apply(&gc, s->options, "message-command-style", ft); 656 else 657 style_apply(&gc, s->options, "message-style", ft); 658 format_free(ft); 659 660 memcpy(&cursorgc, &gc, sizeof cursorgc); 661 cursorgc.attr ^= GRID_ATTR_REVERSE; 662 663 start = screen_write_strlen("%s", c->prompt_string); 664 if (start > c->tty.sx) 665 start = c->tty.sx; 666 667 screen_write_start(&ctx, NULL, sl->active); 668 screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines - 1); 669 screen_write_cursormove(&ctx, 0, lines - 1, 0); 670 for (offset = 0; offset < c->tty.sx; offset++) 671 screen_write_putc(&ctx, &gc, ' '); 672 screen_write_cursormove(&ctx, 0, lines - 1, 0); 673 screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string); 674 screen_write_cursormove(&ctx, start, lines - 1, 0); 675 676 left = c->tty.sx - start; 677 if (left == 0) 678 goto finished; 679 680 pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index); 681 pwidth = utf8_strwidth(c->prompt_buffer, -1); 682 if (pcursor >= left) { 683 /* 684 * The cursor would be outside the screen so start drawing 685 * with it on the right. 686 */ 687 offset = (pcursor - left) + 1; 688 pwidth = left; 689 } else 690 offset = 0; 691 if (pwidth > left) 692 pwidth = left; 693 694 width = 0; 695 for (i = 0; c->prompt_buffer[i].size != 0; i++) { 696 if (width < offset) { 697 width += c->prompt_buffer[i].width; 698 continue; 699 } 700 if (width >= offset + pwidth) 701 break; 702 width += c->prompt_buffer[i].width; 703 if (width > offset + pwidth) 704 break; 705 706 if (i != c->prompt_index) { 707 utf8_copy(&gc.data, &c->prompt_buffer[i]); 708 screen_write_cell(&ctx, &gc); 709 } else { 710 utf8_copy(&cursorgc.data, &c->prompt_buffer[i]); 711 screen_write_cell(&ctx, &cursorgc); 712 } 713 } 714 if (sl->active->cx < screen_size_x(sl->active) && c->prompt_index >= i) 715 screen_write_putc(&ctx, &cursorgc, ' '); 716 717finished: 718 screen_write_stop(&ctx); 719 720 if (grid_compare(sl->active->grid, old_screen.grid) == 0) { 721 screen_free(&old_screen); 722 return (0); 723 } 724 screen_free(&old_screen); 725 return (1); 726} 727 728/* Is this a separator? */ 729static int 730status_prompt_in_list(const char *ws, const struct utf8_data *ud) 731{ 732 if (ud->size != 1 || ud->width != 1) 733 return (0); 734 return (strchr(ws, *ud->data) != NULL); 735} 736 737/* Is this a space? */ 738static int 739status_prompt_space(const struct utf8_data *ud) 740{ 741 if (ud->size != 1 || ud->width != 1) 742 return (0); 743 return (*ud->data == ' '); 744} 745 746/* 747 * Translate key from emacs to vi. Return 0 to drop key, 1 to process the key 748 * as an emacs key; return 2 to append to the buffer. 749 */ 750static int 751status_prompt_translate_key(struct client *c, key_code key, key_code *new_key) 752{ 753 if (c->prompt_mode == PROMPT_ENTRY) { 754 switch (key) { 755 case '\003': /* C-c */ 756 case '\007': /* C-g */ 757 case '\010': /* C-h */ 758 case '\011': /* Tab */ 759 case '\025': /* C-u */ 760 case '\027': /* C-w */ 761 case '\n': 762 case '\r': 763 case KEYC_BSPACE: 764 case KEYC_DC: 765 case KEYC_DOWN: 766 case KEYC_END: 767 case KEYC_HOME: 768 case KEYC_LEFT: 769 case KEYC_RIGHT: 770 case KEYC_UP: 771 *new_key = key; 772 return (1); 773 case '\033': /* Escape */ 774 c->prompt_mode = PROMPT_COMMAND; 775 c->flags |= CLIENT_REDRAWSTATUS; 776 return (0); 777 } 778 *new_key = key; 779 return (2); 780 } 781 782 switch (key) { 783 case 'A': 784 case 'I': 785 case 'C': 786 case 's': 787 case 'a': 788 c->prompt_mode = PROMPT_ENTRY; 789 c->flags |= CLIENT_REDRAWSTATUS; 790 break; /* switch mode and... */ 791 case 'S': 792 c->prompt_mode = PROMPT_ENTRY; 793 c->flags |= CLIENT_REDRAWSTATUS; 794 *new_key = '\025'; /* C-u */ 795 return (1); 796 case 'i': 797 case '\033': /* Escape */ 798 c->prompt_mode = PROMPT_ENTRY; 799 c->flags |= CLIENT_REDRAWSTATUS; 800 return (0); 801 } 802 803 switch (key) { 804 case 'A': 805 case '$': 806 *new_key = KEYC_END; 807 return (1); 808 case 'I': 809 case '0': 810 case '^': 811 *new_key = KEYC_HOME; 812 return (1); 813 case 'C': 814 case 'D': 815 *new_key = '\013'; /* C-k */ 816 return (1); 817 case KEYC_BSPACE: 818 case 'X': 819 *new_key = KEYC_BSPACE; 820 return (1); 821 case 'b': 822 case 'B': 823 *new_key = 'b'|KEYC_ESCAPE; 824 return (1); 825 case 'd': 826 *new_key = '\025'; 827 return (1); 828 case 'e': 829 case 'E': 830 case 'w': 831 case 'W': 832 *new_key = 'f'|KEYC_ESCAPE; 833 return (1); 834 case 'p': 835 *new_key = '\031'; /* C-y */ 836 return (1); 837 case 'q': 838 *new_key = '\003'; /* C-c */ 839 return (1); 840 case 's': 841 case KEYC_DC: 842 case 'x': 843 *new_key = KEYC_DC; 844 return (1); 845 case KEYC_DOWN: 846 case 'j': 847 *new_key = KEYC_DOWN; 848 return (1); 849 case KEYC_LEFT: 850 case 'h': 851 *new_key = KEYC_LEFT; 852 return (1); 853 case 'a': 854 case KEYC_RIGHT: 855 case 'l': 856 *new_key = KEYC_RIGHT; 857 return (1); 858 case KEYC_UP: 859 case 'k': 860 *new_key = KEYC_UP; 861 return (1); 862 case '\010' /* C-h */: 863 case '\003' /* C-c */: 864 case '\n': 865 case '\r': 866 return (1); 867 } 868 return (0); 869} 870 871/* Paste into prompt. */ 872static int 873status_prompt_paste(struct client *c) 874{ 875 struct paste_buffer *pb; 876 const char *bufdata; 877 size_t size, n, bufsize; 878 u_int i; 879 struct utf8_data *ud, *udp; 880 enum utf8_state more; 881 882 size = utf8_strlen(c->prompt_buffer); 883 if (c->prompt_saved != NULL) { 884 ud = c->prompt_saved; 885 n = utf8_strlen(c->prompt_saved); 886 } else { 887 if ((pb = paste_get_top(NULL)) == NULL) 888 return (0); 889 bufdata = paste_buffer_data(pb, &bufsize); 890 ud = xreallocarray(NULL, bufsize + 1, sizeof *ud); 891 udp = ud; 892 for (i = 0; i != bufsize; /* nothing */) { 893 more = utf8_open(udp, bufdata[i]); 894 if (more == UTF8_MORE) { 895 while (++i != bufsize && more == UTF8_MORE) 896 more = utf8_append(udp, bufdata[i]); 897 if (more == UTF8_DONE) { 898 udp++; 899 continue; 900 } 901 i -= udp->have; 902 } 903 if (bufdata[i] <= 31 || bufdata[i] >= 127) 904 break; 905 utf8_set(udp, bufdata[i]); 906 udp++; 907 i++; 908 } 909 udp->size = 0; 910 n = udp - ud; 911 } 912 if (n == 0) 913 return (0); 914 915 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1, 916 sizeof *c->prompt_buffer); 917 if (c->prompt_index == size) { 918 memcpy(c->prompt_buffer + c->prompt_index, ud, 919 n * sizeof *c->prompt_buffer); 920 c->prompt_index += n; 921 c->prompt_buffer[c->prompt_index].size = 0; 922 } else { 923 memmove(c->prompt_buffer + c->prompt_index + n, 924 c->prompt_buffer + c->prompt_index, 925 (size + 1 - c->prompt_index) * sizeof *c->prompt_buffer); 926 memcpy(c->prompt_buffer + c->prompt_index, ud, 927 n * sizeof *c->prompt_buffer); 928 c->prompt_index += n; 929 } 930 931 if (ud != c->prompt_saved) 932 free(ud); 933 return (1); 934} 935 936/* Finish completion. */ 937static int 938status_prompt_replace_complete(struct client *c, const char *s) 939{ 940 char word[64], *allocated = NULL; 941 size_t size, n, off, idx, used; 942 struct utf8_data *first, *last, *ud; 943 944 /* Work out where the cursor currently is. */ 945 idx = c->prompt_index; 946 if (idx != 0) 947 idx--; 948 size = utf8_strlen(c->prompt_buffer); 949 950 /* Find the word we are in. */ 951 first = &c->prompt_buffer[idx]; 952 while (first > c->prompt_buffer && !status_prompt_space(first)) 953 first--; 954 while (first->size != 0 && status_prompt_space(first)) 955 first++; 956 last = &c->prompt_buffer[idx]; 957 while (last->size != 0 && !status_prompt_space(last)) 958 last++; 959 while (last > c->prompt_buffer && status_prompt_space(last)) 960 last--; 961 if (last->size != 0) 962 last++; 963 if (last < first) 964 return (0); 965 if (s == NULL) { 966 used = 0; 967 for (ud = first; ud < last; ud++) { 968 if (used + ud->size >= sizeof word) 969 break; 970 memcpy(word + used, ud->data, ud->size); 971 used += ud->size; 972 } 973 if (ud != last) 974 return (0); 975 word[used] = '\0'; 976 } 977 978 /* Try to complete it. */ 979 if (s == NULL) { 980 allocated = status_prompt_complete(c, word, 981 first - c->prompt_buffer); 982 if (allocated == NULL) 983 return (0); 984 s = allocated; 985 } 986 987 /* Trim out word. */ 988 n = size - (last - c->prompt_buffer) + 1; /* with \0 */ 989 memmove(first, last, n * sizeof *c->prompt_buffer); 990 size -= last - first; 991 992 /* Insert the new word. */ 993 size += strlen(s); 994 off = first - c->prompt_buffer; 995 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1, 996 sizeof *c->prompt_buffer); 997 first = c->prompt_buffer + off; 998 memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer); 999 for (idx = 0; idx < strlen(s); idx++) 1000 utf8_set(&first[idx], s[idx]); 1001 c->prompt_index = (first - c->prompt_buffer) + strlen(s); 1002 1003 free(allocated); 1004 return (1); 1005} 1006 1007/* Handle keys in prompt. */ 1008int 1009status_prompt_key(struct client *c, key_code key) 1010{ 1011 struct options *oo = c->session->options; 1012 char *s, *cp, prefix = '='; 1013 const char *histstr, *ws = NULL, *keystring; 1014 size_t size, idx; 1015 struct utf8_data tmp; 1016 int keys; 1017 1018 if (c->prompt_flags & PROMPT_KEY) { 1019 keystring = key_string_lookup_key(key); 1020 c->prompt_inputcb(c, c->prompt_data, keystring, 1); 1021 status_prompt_clear(c); 1022 return (0); 1023 } 1024 size = utf8_strlen(c->prompt_buffer); 1025 1026 if (c->prompt_flags & PROMPT_NUMERIC) { 1027 if (key >= '0' && key <= '9') 1028 goto append_key; 1029 s = utf8_tocstr(c->prompt_buffer); 1030 c->prompt_inputcb(c, c->prompt_data, s, 1); 1031 status_prompt_clear(c); 1032 free(s); 1033 return (1); 1034 } 1035 key &= ~KEYC_XTERM; 1036 1037 keys = options_get_number(c->session->options, "status-keys"); 1038 if (keys == MODEKEY_VI) { 1039 switch (status_prompt_translate_key(c, key, &key)) { 1040 case 1: 1041 goto process_key; 1042 case 2: 1043 goto append_key; 1044 default: 1045 return (0); 1046 } 1047 } 1048 1049process_key: 1050 switch (key) { 1051 case KEYC_LEFT: 1052 case '\002': /* C-b */ 1053 if (c->prompt_index > 0) { 1054 c->prompt_index--; 1055 break; 1056 } 1057 break; 1058 case KEYC_RIGHT: 1059 case '\006': /* C-f */ 1060 if (c->prompt_index < size) { 1061 c->prompt_index++; 1062 break; 1063 } 1064 break; 1065 case KEYC_HOME: 1066 case '\001': /* C-a */ 1067 if (c->prompt_index != 0) { 1068 c->prompt_index = 0; 1069 break; 1070 } 1071 break; 1072 case KEYC_END: 1073 case '\005': /* C-e */ 1074 if (c->prompt_index != size) { 1075 c->prompt_index = size; 1076 break; 1077 } 1078 break; 1079 case '\011': /* Tab */ 1080 if (status_prompt_replace_complete(c, NULL)) 1081 goto changed; 1082 break; 1083 case KEYC_BSPACE: 1084 case '\010': /* C-h */ 1085 if (c->prompt_index != 0) { 1086 if (c->prompt_index == size) 1087 c->prompt_buffer[--c->prompt_index].size = 0; 1088 else { 1089 memmove(c->prompt_buffer + c->prompt_index - 1, 1090 c->prompt_buffer + c->prompt_index, 1091 (size + 1 - c->prompt_index) * 1092 sizeof *c->prompt_buffer); 1093 c->prompt_index--; 1094 } 1095 goto changed; 1096 } 1097 break; 1098 case KEYC_DC: 1099 case '\004': /* C-d */ 1100 if (c->prompt_index != size) { 1101 memmove(c->prompt_buffer + c->prompt_index, 1102 c->prompt_buffer + c->prompt_index + 1, 1103 (size + 1 - c->prompt_index) * 1104 sizeof *c->prompt_buffer); 1105 goto changed; 1106 } 1107 break; 1108 case '\025': /* C-u */ 1109 c->prompt_buffer[0].size = 0; 1110 c->prompt_index = 0; 1111 goto changed; 1112 case '\013': /* C-k */ 1113 if (c->prompt_index < size) { 1114 c->prompt_buffer[c->prompt_index].size = 0; 1115 goto changed; 1116 } 1117 break; 1118 case '\027': /* C-w */ 1119 ws = options_get_string(oo, "word-separators"); 1120 idx = c->prompt_index; 1121 1122 /* Find a non-separator. */ 1123 while (idx != 0) { 1124 idx--; 1125 if (!status_prompt_in_list(ws, &c->prompt_buffer[idx])) 1126 break; 1127 } 1128 1129 /* Find the separator at the beginning of the word. */ 1130 while (idx != 0) { 1131 idx--; 1132 if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) { 1133 /* Go back to the word. */ 1134 idx++; 1135 break; 1136 } 1137 } 1138 1139 free(c->prompt_saved); 1140 c->prompt_saved = xcalloc(sizeof *c->prompt_buffer, 1141 (c->prompt_index - idx) + 1); 1142 memcpy(c->prompt_saved, c->prompt_buffer + idx, 1143 (c->prompt_index - idx) * sizeof *c->prompt_buffer); 1144 1145 memmove(c->prompt_buffer + idx, 1146 c->prompt_buffer + c->prompt_index, 1147 (size + 1 - c->prompt_index) * 1148 sizeof *c->prompt_buffer); 1149 memset(c->prompt_buffer + size - (c->prompt_index - idx), 1150 '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer); 1151 c->prompt_index = idx; 1152 1153 goto changed; 1154 case 'f'|KEYC_ESCAPE: 1155 case KEYC_RIGHT|KEYC_CTRL: 1156 ws = options_get_string(oo, "word-separators"); 1157 1158 /* Find a word. */ 1159 while (c->prompt_index != size) { 1160 idx = ++c->prompt_index; 1161 if (!status_prompt_in_list(ws, &c->prompt_buffer[idx])) 1162 break; 1163 } 1164 1165 /* Find the separator at the end of the word. */ 1166 while (c->prompt_index != size) { 1167 idx = ++c->prompt_index; 1168 if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) 1169 break; 1170 } 1171 1172 /* Back up to the end-of-word like vi. */ 1173 if (options_get_number(oo, "status-keys") == MODEKEY_VI && 1174 c->prompt_index != 0) 1175 c->prompt_index--; 1176 1177 goto changed; 1178 case 'b'|KEYC_ESCAPE: 1179 case KEYC_LEFT|KEYC_CTRL: 1180 ws = options_get_string(oo, "word-separators"); 1181 1182 /* Find a non-separator. */ 1183 while (c->prompt_index != 0) { 1184 idx = --c->prompt_index; 1185 if (!status_prompt_in_list(ws, &c->prompt_buffer[idx])) 1186 break; 1187 } 1188 1189 /* Find the separator at the beginning of the word. */ 1190 while (c->prompt_index != 0) { 1191 idx = --c->prompt_index; 1192 if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) { 1193 /* Go back to the word. */ 1194 c->prompt_index++; 1195 break; 1196 } 1197 } 1198 goto changed; 1199 case KEYC_UP: 1200 case '\020': /* C-p */ 1201 histstr = status_prompt_up_history(&c->prompt_hindex); 1202 if (histstr == NULL) 1203 break; 1204 free(c->prompt_buffer); 1205 c->prompt_buffer = utf8_fromcstr(histstr); 1206 c->prompt_index = utf8_strlen(c->prompt_buffer); 1207 goto changed; 1208 case KEYC_DOWN: 1209 case '\016': /* C-n */ 1210 histstr = status_prompt_down_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 '\031': /* C-y */ 1218 if (status_prompt_paste(c)) 1219 goto changed; 1220 break; 1221 case '\024': /* C-t */ 1222 idx = c->prompt_index; 1223 if (idx < size) 1224 idx++; 1225 if (idx >= 2) { 1226 utf8_copy(&tmp, &c->prompt_buffer[idx - 2]); 1227 utf8_copy(&c->prompt_buffer[idx - 2], 1228 &c->prompt_buffer[idx - 1]); 1229 utf8_copy(&c->prompt_buffer[idx - 1], &tmp); 1230 c->prompt_index = idx; 1231 goto changed; 1232 } 1233 break; 1234 case '\r': 1235 case '\n': 1236 s = utf8_tocstr(c->prompt_buffer); 1237 if (*s != '\0') 1238 status_prompt_add_history(s); 1239 if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0) 1240 status_prompt_clear(c); 1241 free(s); 1242 break; 1243 case '\033': /* Escape */ 1244 case '\003': /* C-c */ 1245 case '\007': /* C-g */ 1246 if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0) 1247 status_prompt_clear(c); 1248 break; 1249 case '\022': /* C-r */ 1250 if (c->prompt_flags & PROMPT_INCREMENTAL) { 1251 prefix = '-'; 1252 goto changed; 1253 } 1254 break; 1255 case '\023': /* C-s */ 1256 if (c->prompt_flags & PROMPT_INCREMENTAL) { 1257 prefix = '+'; 1258 goto changed; 1259 } 1260 break; 1261 default: 1262 goto append_key; 1263 } 1264 1265 c->flags |= CLIENT_REDRAWSTATUS; 1266 return (0); 1267 1268append_key: 1269 if (key <= 0x1f || key >= KEYC_BASE) 1270 return (0); 1271 if (utf8_split(key, &tmp) != UTF8_DONE) 1272 return (0); 1273 1274 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2, 1275 sizeof *c->prompt_buffer); 1276 1277 if (c->prompt_index == size) { 1278 utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp); 1279 c->prompt_index++; 1280 c->prompt_buffer[c->prompt_index].size = 0; 1281 } else { 1282 memmove(c->prompt_buffer + c->prompt_index + 1, 1283 c->prompt_buffer + c->prompt_index, 1284 (size + 1 - c->prompt_index) * 1285 sizeof *c->prompt_buffer); 1286 utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp); 1287 c->prompt_index++; 1288 } 1289 1290 if (c->prompt_flags & PROMPT_SINGLE) { 1291 s = utf8_tocstr(c->prompt_buffer); 1292 if (strlen(s) != 1) 1293 status_prompt_clear(c); 1294 else if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0) 1295 status_prompt_clear(c); 1296 free(s); 1297 } 1298 1299changed: 1300 c->flags |= CLIENT_REDRAWSTATUS; 1301 if (c->prompt_flags & PROMPT_INCREMENTAL) { 1302 s = utf8_tocstr(c->prompt_buffer); 1303 xasprintf(&cp, "%c%s", prefix, s); 1304 c->prompt_inputcb(c, c->prompt_data, cp, 0); 1305 free(cp); 1306 free(s); 1307 } 1308 return (0); 1309} 1310 1311/* Get previous line from the history. */ 1312static const char * 1313status_prompt_up_history(u_int *idx) 1314{ 1315 /* 1316 * History runs from 0 to size - 1. Index is from 0 to size. Zero is 1317 * empty. 1318 */ 1319 1320 if (status_prompt_hsize == 0 || *idx == status_prompt_hsize) 1321 return (NULL); 1322 (*idx)++; 1323 return (status_prompt_hlist[status_prompt_hsize - *idx]); 1324} 1325 1326/* Get next line from the history. */ 1327static const char * 1328status_prompt_down_history(u_int *idx) 1329{ 1330 if (status_prompt_hsize == 0 || *idx == 0) 1331 return (""); 1332 (*idx)--; 1333 if (*idx == 0) 1334 return (""); 1335 return (status_prompt_hlist[status_prompt_hsize - *idx]); 1336} 1337 1338/* Add line to the history. */ 1339static void 1340status_prompt_add_history(const char *line) 1341{ 1342 size_t size; 1343 1344 if (status_prompt_hsize > 0 && 1345 strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0) 1346 return; 1347 1348 if (status_prompt_hsize == PROMPT_HISTORY) { 1349 free(status_prompt_hlist[0]); 1350 1351 size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist; 1352 memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size); 1353 1354 status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line); 1355 return; 1356 } 1357 1358 status_prompt_hlist = xreallocarray(status_prompt_hlist, 1359 status_prompt_hsize + 1, sizeof *status_prompt_hlist); 1360 status_prompt_hlist[status_prompt_hsize++] = xstrdup(line); 1361} 1362 1363/* Build completion list. */ 1364static char ** 1365status_prompt_complete_list(u_int *size, const char *s, int at_start) 1366{ 1367 char **list = NULL; 1368 const char **layout, *value, *cp; 1369 const struct cmd_entry **cmdent; 1370 const struct options_table_entry *oe; 1371 size_t slen = strlen(s), valuelen; 1372 struct options_entry *o; 1373 struct options_array_item *a; 1374 const char *layouts[] = { 1375 "even-horizontal", "even-vertical", "main-horizontal", 1376 "main-vertical", "tiled", NULL 1377 }; 1378 1379 *size = 0; 1380 for (cmdent = cmd_table; *cmdent != NULL; cmdent++) { 1381 if (strncmp((*cmdent)->name, s, slen) == 0) { 1382 list = xreallocarray(list, (*size) + 1, sizeof *list); 1383 list[(*size)++] = xstrdup((*cmdent)->name); 1384 } 1385 if ((*cmdent)->alias != NULL && 1386 strncmp((*cmdent)->alias, s, slen) == 0) { 1387 list = xreallocarray(list, (*size) + 1, sizeof *list); 1388 list[(*size)++] = xstrdup((*cmdent)->alias); 1389 } 1390 } 1391 o = options_get_only(global_options, "command-alias"); 1392 if (o != NULL) { 1393 a = options_array_first(o); 1394 while (a != NULL) { 1395 value = options_array_item_value(a)->string; 1396 if ((cp = strchr(value, '=')) == NULL) 1397 goto next; 1398 valuelen = cp - value; 1399 if (slen > valuelen || strncmp(value, s, slen) != 0) 1400 goto next; 1401 1402 list = xreallocarray(list, (*size) + 1, sizeof *list); 1403 list[(*size)++] = xstrndup(value, valuelen); 1404 1405 next: 1406 a = options_array_next(a); 1407 } 1408 } 1409 if (at_start) 1410 return (list); 1411 1412 for (oe = options_table; oe->name != NULL; oe++) { 1413 if (strncmp(oe->name, s, slen) == 0) { 1414 list = xreallocarray(list, (*size) + 1, sizeof *list); 1415 list[(*size)++] = xstrdup(oe->name); 1416 } 1417 } 1418 for (layout = layouts; *layout != NULL; layout++) { 1419 if (strncmp(*layout, s, slen) == 0) { 1420 list = xreallocarray(list, (*size) + 1, sizeof *list); 1421 list[(*size)++] = xstrdup(*layout); 1422 } 1423 } 1424 return (list); 1425} 1426 1427/* Find longest prefix. */ 1428static char * 1429status_prompt_complete_prefix(char **list, u_int size) 1430{ 1431 char *out; 1432 u_int i; 1433 size_t j; 1434 1435 out = xstrdup(list[0]); 1436 for (i = 1; i < size; i++) { 1437 j = strlen(list[i]); 1438 if (j > strlen(out)) 1439 j = strlen(out); 1440 for (; j > 0; j--) { 1441 if (out[j - 1] != list[i][j - 1]) 1442 out[j - 1] = '\0'; 1443 } 1444 } 1445 return (out); 1446} 1447 1448/* Complete word menu callback. */ 1449static void 1450status_prompt_menu_callback(__unused struct menu *menu, u_int idx, key_code key, 1451 void *data) 1452{ 1453 struct status_prompt_menu *spm = data; 1454 struct client *c = spm->c; 1455 u_int i; 1456 char *s; 1457 1458 if (key != KEYC_NONE) { 1459 idx += spm->start; 1460 if (spm->flag == '\0') 1461 s = xstrdup(spm->list[idx]); 1462 else 1463 xasprintf(&s, "-%c%s", spm->flag, spm->list[idx]); 1464 if (c->prompt_flags & PROMPT_WINDOW) { 1465 free(c->prompt_buffer); 1466 c->prompt_buffer = utf8_fromcstr(s); 1467 c->prompt_index = utf8_strlen(c->prompt_buffer); 1468 c->flags |= CLIENT_REDRAWSTATUS; 1469 } else if (status_prompt_replace_complete(c, s)) 1470 c->flags |= CLIENT_REDRAWSTATUS; 1471 free(s); 1472 } 1473 1474 for (i = 0; i < spm->size; i++) 1475 free(spm->list[i]); 1476 free(spm->list); 1477} 1478 1479/* Show complete word menu. */ 1480static int 1481status_prompt_complete_list_menu(struct client *c, char **list, u_int size, 1482 u_int offset, char flag) 1483{ 1484 struct menu *menu; 1485 struct menu_item item; 1486 struct status_prompt_menu *spm; 1487 u_int lines = status_line_size(c), height, i; 1488 u_int py; 1489 1490 if (size <= 1) 1491 return (0); 1492 if (c->tty.sy - lines < 3) 1493 return (0); 1494 1495 spm = xmalloc(sizeof *spm); 1496 spm->c = c; 1497 spm->size = size; 1498 spm->list = list; 1499 spm->flag = flag; 1500 1501 height = c->tty.sy - lines - 2; 1502 if (height > 10) 1503 height = 10; 1504 if (height > size) 1505 height = size; 1506 spm->start = size - height; 1507 1508 menu = menu_create(""); 1509 for (i = spm->start; i < size; i++) { 1510 item.name = list[i]; 1511 item.key = '0' + (i - spm->start); 1512 item.command = NULL; 1513 menu_add_item(menu, &item, NULL, NULL, NULL); 1514 } 1515 1516 if (options_get_number(c->session->options, "status-position") == 0) 1517 py = lines; 1518 else 1519 py = c->tty.sy - 3 - height; 1520 offset += utf8_cstrwidth(c->prompt_string); 1521 if (offset > 2) 1522 offset -= 2; 1523 else 1524 offset = 0; 1525 1526 if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, NULL, offset, 1527 py, c, NULL, status_prompt_menu_callback, spm) != 0) { 1528 menu_free(menu); 1529 free(spm); 1530 return (0); 1531 } 1532 return (1); 1533} 1534 1535/* Show complete word menu. */ 1536static char * 1537status_prompt_complete_window_menu(struct client *c, struct session *s, 1538 const char *word, u_int offset, char flag) 1539{ 1540 struct menu *menu; 1541 struct menu_item item; 1542 struct status_prompt_menu *spm; 1543 struct winlink *wl; 1544 char **list = NULL, *tmp; 1545 u_int lines = status_line_size(c), height; 1546 u_int py, size = 0; 1547 1548 if (c->tty.sy - lines < 3) 1549 return (NULL); 1550 1551 spm = xmalloc(sizeof *spm); 1552 spm->c = c; 1553 spm->flag = flag; 1554 1555 height = c->tty.sy - lines - 2; 1556 if (height > 10) 1557 height = 10; 1558 spm->start = 0; 1559 1560 menu = menu_create(""); 1561 RB_FOREACH(wl, winlinks, &s->windows) { 1562 if (word != NULL && *word != '\0') { 1563 xasprintf(&tmp, "%d", wl->idx); 1564 if (strncmp(tmp, word, strlen(word)) != 0) { 1565 free(tmp); 1566 continue; 1567 } 1568 free(tmp); 1569 } 1570 1571 list = xreallocarray(list, size + 1, sizeof *list); 1572 if (c->prompt_flags & PROMPT_WINDOW) { 1573 xasprintf(&tmp, "%d (%s)", wl->idx, wl->window->name); 1574 xasprintf(&list[size++], "%d", wl->idx); 1575 } else { 1576 xasprintf(&tmp, "%s:%d (%s)", s->name, wl->idx, 1577 wl->window->name); 1578 xasprintf(&list[size++], "%s:%d", s->name, wl->idx); 1579 } 1580 item.name = tmp; 1581 item.key = '0' + size - 1; 1582 item.command = NULL; 1583 menu_add_item(menu, &item, NULL, NULL, NULL); 1584 free(tmp); 1585 1586 if (size == height) 1587 break; 1588 } 1589 if (size == 0) { 1590 menu_free(menu); 1591 return (NULL); 1592 } 1593 if (size == 1) { 1594 menu_free(menu); 1595 if (flag != '\0') { 1596 xasprintf(&tmp, "-%c%s", flag, list[0]); 1597 free(list[0]); 1598 } else 1599 tmp = list[0]; 1600 free(list); 1601 return (tmp); 1602 } 1603 if (height > size) 1604 height = size; 1605 1606 spm->size = size; 1607 spm->list = list; 1608 1609 if (options_get_number(c->session->options, "status-position") == 0) 1610 py = lines; 1611 else 1612 py = c->tty.sy - 3 - height; 1613 offset += utf8_cstrwidth(c->prompt_string); 1614 if (offset > 2) 1615 offset -= 2; 1616 else 1617 offset = 0; 1618 1619 if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, NULL, offset, 1620 py, c, NULL, status_prompt_menu_callback, spm) != 0) { 1621 menu_free(menu); 1622 free(spm); 1623 return (NULL); 1624 } 1625 return (NULL); 1626} 1627 1628/* Sort complete list. */ 1629static int 1630status_prompt_complete_sort(const void *a, const void *b) 1631{ 1632 const char **aa = (const char **)a, **bb = (const char **)b; 1633 1634 return (strcmp(*aa, *bb)); 1635} 1636 1637/* Complete a session. */ 1638static char * 1639status_prompt_complete_session(char ***list, u_int *size, const char *s, 1640 char flag) 1641{ 1642 struct session *loop; 1643 char *out, *tmp; 1644 1645 RB_FOREACH(loop, sessions, &sessions) { 1646 if (*s != '\0' && strncmp(loop->name, s, strlen(s)) != 0) 1647 continue; 1648 *list = xreallocarray(*list, (*size) + 2, sizeof **list); 1649 xasprintf(&(*list)[(*size)++], "%s:", loop->name); 1650 } 1651 out = status_prompt_complete_prefix(*list, *size); 1652 if (out != NULL && flag != '\0') { 1653 xasprintf(&tmp, "-%c%s", flag, out); 1654 free(out); 1655 out = tmp; 1656 } 1657 return (out); 1658} 1659 1660/* Complete word. */ 1661static char * 1662status_prompt_complete(struct client *c, const char *word, u_int offset) 1663{ 1664 struct session *session; 1665 const char *s, *colon; 1666 char **list = NULL, *copy = NULL, *out = NULL; 1667 char flag = '\0'; 1668 u_int size = 0, i; 1669 1670 if (*word == '\0' && 1671 ((c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) == 0)) 1672 return (NULL); 1673 1674 if (((c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) == 0) && 1675 strncmp(word, "-t", 2) != 0 && 1676 strncmp(word, "-s", 2) != 0) { 1677 list = status_prompt_complete_list(&size, word, offset == 0); 1678 if (size == 0) 1679 out = NULL; 1680 else if (size == 1) 1681 xasprintf(&out, "%s ", list[0]); 1682 else 1683 out = status_prompt_complete_prefix(list, size); 1684 goto found; 1685 } 1686 1687 if (c->prompt_flags & (PROMPT_TARGET|PROMPT_WINDOW)) { 1688 s = word; 1689 flag = '\0'; 1690 } else { 1691 s = word + 2; 1692 flag = word[1]; 1693 offset += 2; 1694 } 1695 1696 /* If this is a window completion, open the window menu. */ 1697 if (c->prompt_flags & PROMPT_WINDOW) { 1698 out = status_prompt_complete_window_menu(c, c->session, s, 1699 offset, '\0'); 1700 goto found; 1701 } 1702 colon = strchr(s, ':'); 1703 1704 /* If there is no colon, complete as a session. */ 1705 if (colon == NULL) { 1706 out = status_prompt_complete_session(&list, &size, s, flag); 1707 goto found; 1708 } 1709 1710 /* If there is a colon but no period, find session and show a menu. */ 1711 if (strchr(colon + 1, '.') == NULL) { 1712 if (*s == ':') 1713 session = c->session; 1714 else { 1715 copy = xstrdup(s); 1716 *strchr(copy, ':') = '\0'; 1717 session = session_find(copy); 1718 free(copy); 1719 if (session == NULL) 1720 goto found; 1721 } 1722 out = status_prompt_complete_window_menu(c, session, colon + 1, 1723 offset, flag); 1724 if (out == NULL) 1725 return (NULL); 1726 } 1727 1728found: 1729 if (size != 0) { 1730 qsort(list, size, sizeof *list, status_prompt_complete_sort); 1731 for (i = 0; i < size; i++) 1732 log_debug("complete %u: %s", i, list[i]); 1733 } 1734 1735 if (out != NULL && strcmp(word, out) == 0) { 1736 free(out); 1737 out = NULL; 1738 } 1739 if (out != NULL || 1740 !status_prompt_complete_list_menu(c, list, size, offset, flag)) { 1741 for (i = 0; i < size; i++) 1742 free(list[i]); 1743 free(list); 1744 } 1745 return (out); 1746} 1747