status.c revision 1.143
1/* $OpenBSD: status.c,v 1.143 2015/11/22 18:28:01 tim Exp $ */ 2 3/* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 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 32char *status_redraw_get_left(struct client *, time_t, struct grid_cell *, 33 size_t *); 34char *status_redraw_get_right(struct client *, time_t, struct grid_cell *, 35 size_t *); 36char *status_print(struct client *, struct winlink *, time_t, 37 struct grid_cell *); 38char *status_replace(struct client *, struct winlink *, const char *, time_t); 39void status_message_callback(int, short, void *); 40void status_timer_callback(int, short, void *); 41 42const char *status_prompt_up_history(u_int *); 43const char *status_prompt_down_history(u_int *); 44void status_prompt_add_history(const char *); 45 46const char **status_prompt_complete_list(u_int *, const char *); 47char *status_prompt_complete_prefix(const char **, u_int); 48char *status_prompt_complete(struct session *, const char *); 49 50char *status_prompt_find_history_file(void); 51 52/* Status prompt history. */ 53#define PROMPT_HISTORY 100 54char **status_prompt_hlist; 55u_int status_prompt_hsize; 56 57/* Find the history file to load/save from/to. */ 58char * 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. */ 147void 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. */ 210char * 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. */ 232char * 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 size_t len; 261 262 x += c->wlmouse; 263 RB_FOREACH(wl, winlinks, &s->windows) { 264 oo = wl->window->options; 265 len = strlen(options_get_string(oo, "window-status-separator")); 266 267 if (x < wl->status_width) 268 return (wl->window); 269 x -= wl->status_width + len; 270 } 271 return (NULL); 272} 273 274/* Draw status for client on the last lines of given context. */ 275int 276status_redraw(struct client *c) 277{ 278 struct screen_write_ctx ctx; 279 struct session *s = c->session; 280 struct winlink *wl; 281 struct screen old_status, window_list; 282 struct grid_cell stdgc, lgc, rgc, gc; 283 struct options *oo; 284 time_t t; 285 char *left, *right, *sep; 286 u_int offset, needed; 287 u_int wlstart, wlwidth, wlavailable, wloffset, wlsize; 288 size_t llen, rlen, seplen; 289 int larrow, rarrow; 290 291 /* No status line? */ 292 if (c->tty.sy == 0 || !options_get_number(s->options, "status")) 293 return (1); 294 left = right = NULL; 295 larrow = rarrow = 0; 296 297 /* Store current time. */ 298 t = time(NULL); 299 300 /* Set up default colour. */ 301 style_apply(&stdgc, s->options, "status-style"); 302 303 /* Create the target screen. */ 304 memcpy(&old_status, &c->status, sizeof old_status); 305 screen_init(&c->status, c->tty.sx, 1, 0); 306 screen_write_start(&ctx, NULL, &c->status); 307 for (offset = 0; offset < c->tty.sx; offset++) 308 screen_write_putc(&ctx, &stdgc, ' '); 309 screen_write_stop(&ctx); 310 311 /* If the height is one line, blank status line. */ 312 if (c->tty.sy <= 1) 313 goto out; 314 315 /* Work out left and right strings. */ 316 memcpy(&lgc, &stdgc, sizeof lgc); 317 left = status_redraw_get_left(c, t, &lgc, &llen); 318 memcpy(&rgc, &stdgc, sizeof rgc); 319 right = status_redraw_get_right(c, t, &rgc, &rlen); 320 321 /* 322 * Figure out how much space we have for the window list. If there 323 * isn't enough space, just show a blank status line. 324 */ 325 needed = 0; 326 if (llen != 0) 327 needed += llen; 328 if (rlen != 0) 329 needed += rlen; 330 if (c->tty.sx == 0 || c->tty.sx <= needed) 331 goto out; 332 wlavailable = c->tty.sx - needed; 333 334 /* Calculate the total size needed for the window list. */ 335 wlstart = wloffset = wlwidth = 0; 336 RB_FOREACH(wl, winlinks, &s->windows) { 337 free(wl->status_text); 338 memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell); 339 wl->status_text = status_print(c, wl, t, &wl->status_cell); 340 wl->status_width = screen_write_cstrlen("%s", wl->status_text); 341 342 if (wl == s->curw) 343 wloffset = wlwidth; 344 345 oo = wl->window->options; 346 sep = options_get_string(oo, "window-status-separator"); 347 seplen = screen_write_strlen("%s", sep); 348 wlwidth += wl->status_width + seplen; 349 } 350 351 /* Create a new screen for the window list. */ 352 screen_init(&window_list, wlwidth, 1, 0); 353 354 /* And draw the window list into it. */ 355 screen_write_start(&ctx, NULL, &window_list); 356 RB_FOREACH(wl, winlinks, &s->windows) { 357 screen_write_cnputs(&ctx, -1, &wl->status_cell, "%s", 358 wl->status_text); 359 360 oo = wl->window->options; 361 sep = options_get_string(oo, "window-status-separator"); 362 screen_write_nputs(&ctx, -1, &stdgc, "%s", sep); 363 } 364 screen_write_stop(&ctx); 365 366 /* If there is enough space for the total width, skip to draw now. */ 367 if (wlwidth <= wlavailable) 368 goto draw; 369 370 /* Find size of current window text. */ 371 wlsize = s->curw->status_width; 372 373 /* 374 * If the current window is already on screen, good to draw from the 375 * start and just leave off the end. 376 */ 377 if (wloffset + wlsize < wlavailable) { 378 if (wlavailable > 0) { 379 rarrow = 1; 380 wlavailable--; 381 } 382 wlwidth = wlavailable; 383 } else { 384 /* 385 * Work out how many characters we need to omit from the 386 * start. There are wlavailable characters to fill, and 387 * wloffset + wlsize must be the last. So, the start character 388 * is wloffset + wlsize - wlavailable. 389 */ 390 if (wlavailable > 0) { 391 larrow = 1; 392 wlavailable--; 393 } 394 395 wlstart = wloffset + wlsize - wlavailable; 396 if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) { 397 rarrow = 1; 398 wlstart++; 399 wlavailable--; 400 } 401 wlwidth = wlavailable; 402 } 403 404 /* Bail if anything is now too small too. */ 405 if (wlwidth == 0 || wlavailable == 0) { 406 screen_free(&window_list); 407 goto out; 408 } 409 410 /* 411 * Now the start position is known, work out the state of the left and 412 * right arrows. 413 */ 414 offset = 0; 415 RB_FOREACH(wl, winlinks, &s->windows) { 416 if (wl->flags & WINLINK_ALERTFLAGS && 417 larrow == 1 && offset < wlstart) 418 larrow = -1; 419 420 offset += wl->status_width; 421 422 if (wl->flags & WINLINK_ALERTFLAGS && 423 rarrow == 1 && offset > wlstart + wlwidth) 424 rarrow = -1; 425 } 426 427draw: 428 /* Begin drawing. */ 429 screen_write_start(&ctx, NULL, &c->status); 430 431 /* Draw the left string and arrow. */ 432 screen_write_cursormove(&ctx, 0, 0); 433 if (llen != 0) 434 screen_write_cnputs(&ctx, llen, &lgc, "%s", left); 435 if (larrow != 0) { 436 memcpy(&gc, &stdgc, sizeof gc); 437 if (larrow == -1) 438 gc.attr ^= GRID_ATTR_REVERSE; 439 screen_write_putc(&ctx, &gc, '<'); 440 } 441 442 /* Draw the right string and arrow. */ 443 if (rarrow != 0) { 444 screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0); 445 memcpy(&gc, &stdgc, sizeof gc); 446 if (rarrow == -1) 447 gc.attr ^= GRID_ATTR_REVERSE; 448 screen_write_putc(&ctx, &gc, '>'); 449 } else 450 screen_write_cursormove(&ctx, c->tty.sx - rlen, 0); 451 if (rlen != 0) 452 screen_write_cnputs(&ctx, rlen, &rgc, "%s", right); 453 454 /* Figure out the offset for the window list. */ 455 if (llen != 0) 456 wloffset = llen; 457 else 458 wloffset = 0; 459 if (wlwidth < wlavailable) { 460 switch (options_get_number(s->options, "status-justify")) { 461 case 1: /* centred */ 462 wloffset += (wlavailable - wlwidth) / 2; 463 break; 464 case 2: /* right */ 465 wloffset += (wlavailable - wlwidth); 466 break; 467 } 468 } 469 if (larrow != 0) 470 wloffset++; 471 472 /* Copy the window list. */ 473 c->wlmouse = -wloffset + wlstart; 474 screen_write_cursormove(&ctx, wloffset, 0); 475 screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1); 476 screen_free(&window_list); 477 478 screen_write_stop(&ctx); 479 480out: 481 free(left); 482 free(right); 483 484 if (grid_compare(c->status.grid, old_status.grid) == 0) { 485 screen_free(&old_status); 486 return (0); 487 } 488 screen_free(&old_status); 489 return (1); 490} 491 492/* Replace special sequences in fmt. */ 493char * 494status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t) 495{ 496 struct format_tree *ft; 497 char *expanded; 498 499 if (fmt == NULL) 500 return (xstrdup("")); 501 502 if (c->flags & CLIENT_STATUSFORCE) 503 ft = format_create_flags(FORMAT_STATUS|FORMAT_FORCE); 504 else 505 ft = format_create_flags(FORMAT_STATUS); 506 format_defaults(ft, c, NULL, wl, NULL); 507 508 expanded = format_expand_time(ft, fmt, t); 509 510 format_free(ft); 511 return (expanded); 512} 513 514/* Return winlink status line entry and adjust gc as necessary. */ 515char * 516status_print(struct client *c, struct winlink *wl, time_t t, 517 struct grid_cell *gc) 518{ 519 struct options *oo = wl->window->options; 520 struct session *s = c->session; 521 const char *fmt; 522 char *text; 523 524 style_apply_update(gc, oo, "window-status-style"); 525 fmt = options_get_string(oo, "window-status-format"); 526 if (wl == s->curw) { 527 style_apply_update(gc, oo, "window-status-current-style"); 528 fmt = options_get_string(oo, "window-status-current-format"); 529 } 530 if (wl == TAILQ_FIRST(&s->lastw)) 531 style_apply_update(gc, oo, "window-status-last-style"); 532 533 if (wl->flags & WINLINK_BELL) 534 style_apply_update(gc, oo, "window-status-bell-style"); 535 else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE)) 536 style_apply_update(gc, oo, "window-status-activity-style"); 537 538 text = status_replace(c, wl, fmt, t); 539 return (text); 540} 541 542/* Set a status line message. */ 543void 544status_message_set(struct client *c, const char *fmt, ...) 545{ 546 struct timeval tv; 547 struct message_entry *msg, *msg1; 548 va_list ap; 549 int delay; 550 u_int first, limit; 551 552 limit = options_get_number(global_options, "message-limit"); 553 554 status_prompt_clear(c); 555 status_message_clear(c); 556 557 va_start(ap, fmt); 558 xvasprintf(&c->message_string, fmt, ap); 559 va_end(ap); 560 561 msg = xcalloc(1, sizeof *msg); 562 msg->msg_time = time(NULL); 563 msg->msg_num = c->message_next++; 564 msg->msg = xstrdup(c->message_string); 565 TAILQ_INSERT_TAIL(&c->message_log, msg, entry); 566 567 first = c->message_next - limit; 568 TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) { 569 if (msg->msg_num >= first) 570 continue; 571 free(msg->msg); 572 TAILQ_REMOVE(&c->message_log, msg, entry); 573 free(msg); 574 } 575 576 delay = options_get_number(c->session->options, "display-time"); 577 if (delay > 0) { 578 tv.tv_sec = delay / 1000; 579 tv.tv_usec = (delay % 1000) * 1000L; 580 581 if (event_initialized(&c->message_timer)) 582 evtimer_del(&c->message_timer); 583 evtimer_set(&c->message_timer, status_message_callback, c); 584 evtimer_add(&c->message_timer, &tv); 585 } 586 587 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); 588 c->flags |= CLIENT_STATUS; 589} 590 591/* Clear status line message. */ 592void 593status_message_clear(struct client *c) 594{ 595 if (c->message_string == NULL) 596 return; 597 598 free(c->message_string); 599 c->message_string = NULL; 600 601 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); 602 c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ 603 604 screen_reinit(&c->status); 605} 606 607/* Clear status line message after timer expires. */ 608void 609status_message_callback(__unused int fd, __unused short event, void *data) 610{ 611 struct client *c = data; 612 613 status_message_clear(c); 614} 615 616/* Draw client message on status line of present else on last line. */ 617int 618status_message_redraw(struct client *c) 619{ 620 struct screen_write_ctx ctx; 621 struct session *s = c->session; 622 struct screen old_status; 623 size_t len; 624 struct grid_cell gc; 625 626 if (c->tty.sx == 0 || c->tty.sy == 0) 627 return (0); 628 memcpy(&old_status, &c->status, sizeof old_status); 629 screen_init(&c->status, c->tty.sx, 1, 0); 630 631 len = screen_write_strlen("%s", c->message_string); 632 if (len > c->tty.sx) 633 len = c->tty.sx; 634 635 style_apply(&gc, s->options, "message-style"); 636 637 screen_write_start(&ctx, NULL, &c->status); 638 639 screen_write_cursormove(&ctx, 0, 0); 640 screen_write_nputs(&ctx, len, &gc, "%s", c->message_string); 641 for (; len < c->tty.sx; len++) 642 screen_write_putc(&ctx, &gc, ' '); 643 644 screen_write_stop(&ctx); 645 646 if (grid_compare(c->status.grid, old_status.grid) == 0) { 647 screen_free(&old_status); 648 return (0); 649 } 650 screen_free(&old_status); 651 return (1); 652} 653 654/* Enable status line prompt. */ 655void 656status_prompt_set(struct client *c, const char *msg, const char *input, 657 int (*callbackfn)(void *, const char *), void (*freefn)(void *), 658 void *data, int flags) 659{ 660 struct format_tree *ft; 661 int keys; 662 time_t t; 663 664 ft = format_create(); 665 format_defaults(ft, c, NULL, NULL, NULL); 666 t = time(NULL); 667 668 status_message_clear(c); 669 status_prompt_clear(c); 670 671 c->prompt_string = format_expand_time(ft, msg, t); 672 673 c->prompt_buffer = format_expand_time(ft, input, t); 674 c->prompt_index = strlen(c->prompt_buffer); 675 676 c->prompt_callbackfn = callbackfn; 677 c->prompt_freefn = freefn; 678 c->prompt_data = data; 679 680 c->prompt_hindex = 0; 681 682 c->prompt_flags = flags; 683 684 keys = options_get_number(c->session->options, "status-keys"); 685 if (keys == MODEKEY_EMACS) 686 mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit); 687 else 688 mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit); 689 690 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); 691 c->flags |= CLIENT_STATUS; 692 693 format_free(ft); 694} 695 696/* Remove status line prompt. */ 697void 698status_prompt_clear(struct client *c) 699{ 700 if (c->prompt_string == NULL) 701 return; 702 703 if (c->prompt_freefn != NULL && c->prompt_data != NULL) 704 c->prompt_freefn(c->prompt_data); 705 706 free(c->prompt_string); 707 c->prompt_string = NULL; 708 709 free(c->prompt_buffer); 710 c->prompt_buffer = NULL; 711 712 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); 713 c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ 714 715 screen_reinit(&c->status); 716} 717 718/* Update status line prompt with a new prompt string. */ 719void 720status_prompt_update(struct client *c, const char *msg, const char *input) 721{ 722 struct format_tree *ft; 723 time_t t; 724 725 ft = format_create(); 726 format_defaults(ft, c, NULL, NULL, NULL); 727 t = time(NULL); 728 729 free(c->prompt_string); 730 c->prompt_string = format_expand_time(ft, msg, t); 731 732 free(c->prompt_buffer); 733 c->prompt_buffer = format_expand_time(ft, input, t); 734 c->prompt_index = strlen(c->prompt_buffer); 735 736 c->prompt_hindex = 0; 737 738 c->flags |= CLIENT_STATUS; 739 740 format_free(ft); 741} 742 743/* Draw client prompt on status line of present else on last line. */ 744int 745status_prompt_redraw(struct client *c) 746{ 747 struct screen_write_ctx ctx; 748 struct session *s = c->session; 749 struct screen old_status; 750 size_t i, size, left, len, off; 751 struct grid_cell gc; 752 753 if (c->tty.sx == 0 || c->tty.sy == 0) 754 return (0); 755 memcpy(&old_status, &c->status, sizeof old_status); 756 screen_init(&c->status, c->tty.sx, 1, 0); 757 758 len = screen_write_strlen("%s", c->prompt_string); 759 if (len > c->tty.sx) 760 len = c->tty.sx; 761 off = 0; 762 763 /* Change colours for command mode. */ 764 if (c->prompt_mdata.mode == 1) 765 style_apply(&gc, s->options, "message-command-style"); 766 else 767 style_apply(&gc, s->options, "message-style"); 768 769 screen_write_start(&ctx, NULL, &c->status); 770 771 screen_write_cursormove(&ctx, 0, 0); 772 screen_write_nputs(&ctx, len, &gc, "%s", c->prompt_string); 773 774 left = c->tty.sx - len; 775 if (left != 0) { 776 size = screen_write_strlen("%s", c->prompt_buffer); 777 if (c->prompt_index >= left) { 778 off = c->prompt_index - left + 1; 779 if (c->prompt_index == size) 780 left--; 781 size = left; 782 } 783 screen_write_nputs(&ctx, left, &gc, "%s", c->prompt_buffer + 784 off); 785 786 for (i = len + size; i < c->tty.sx; i++) 787 screen_write_putc(&ctx, &gc, ' '); 788 } 789 790 screen_write_stop(&ctx); 791 792 /* Apply fake cursor. */ 793 off = len + c->prompt_index - off; 794 grid_view_get_cell(c->status.grid, off, 0, &gc); 795 gc.attr ^= GRID_ATTR_REVERSE; 796 grid_view_set_cell(c->status.grid, off, 0, &gc); 797 798 if (grid_compare(c->status.grid, old_status.grid) == 0) { 799 screen_free(&old_status); 800 return (0); 801 } 802 screen_free(&old_status); 803 return (1); 804} 805 806/* Handle keys in prompt. */ 807void 808status_prompt_key(struct client *c, key_code key) 809{ 810 struct session *sess = c->session; 811 struct options *oo = sess->options; 812 struct paste_buffer *pb; 813 char *s, *first, *last, word[64], swapc; 814 const char *histstr, *bufdata, *wsep = NULL; 815 u_char ch; 816 size_t size, n, off, idx, bufsize; 817 818 size = strlen(c->prompt_buffer); 819 switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) { 820 case MODEKEYEDIT_CURSORLEFT: 821 if (c->prompt_index > 0) { 822 c->prompt_index--; 823 c->flags |= CLIENT_STATUS; 824 } 825 break; 826 case MODEKEYEDIT_SWITCHMODE: 827 c->flags |= CLIENT_STATUS; 828 break; 829 case MODEKEYEDIT_SWITCHMODEAPPEND: 830 c->flags |= CLIENT_STATUS; 831 /* FALLTHROUGH */ 832 case MODEKEYEDIT_CURSORRIGHT: 833 if (c->prompt_index < size) { 834 c->prompt_index++; 835 c->flags |= CLIENT_STATUS; 836 } 837 break; 838 case MODEKEYEDIT_SWITCHMODEBEGINLINE: 839 c->flags |= CLIENT_STATUS; 840 /* FALLTHROUGH */ 841 case MODEKEYEDIT_STARTOFLINE: 842 if (c->prompt_index != 0) { 843 c->prompt_index = 0; 844 c->flags |= CLIENT_STATUS; 845 } 846 break; 847 case MODEKEYEDIT_SWITCHMODEAPPENDLINE: 848 c->flags |= CLIENT_STATUS; 849 /* FALLTHROUGH */ 850 case MODEKEYEDIT_ENDOFLINE: 851 if (c->prompt_index != size) { 852 c->prompt_index = size; 853 c->flags |= CLIENT_STATUS; 854 } 855 break; 856 case MODEKEYEDIT_COMPLETE: 857 if (*c->prompt_buffer == '\0') 858 break; 859 860 idx = c->prompt_index; 861 if (idx != 0) 862 idx--; 863 864 /* Find the word we are in. */ 865 first = c->prompt_buffer + idx; 866 while (first > c->prompt_buffer && *first != ' ') 867 first--; 868 while (*first == ' ') 869 first++; 870 last = c->prompt_buffer + idx; 871 while (*last != '\0' && *last != ' ') 872 last++; 873 while (*last == ' ') 874 last--; 875 if (*last != '\0') 876 last++; 877 if (last <= first || 878 ((size_t) (last - first)) > (sizeof word) - 1) 879 break; 880 memcpy(word, first, last - first); 881 word[last - first] = '\0'; 882 883 /* And try to complete it. */ 884 if ((s = status_prompt_complete(sess, word)) == NULL) 885 break; 886 887 /* Trim out word. */ 888 n = size - (last - c->prompt_buffer) + 1; /* with \0 */ 889 memmove(first, last, n); 890 size -= last - first; 891 892 /* Insert the new word. */ 893 size += strlen(s); 894 off = first - c->prompt_buffer; 895 c->prompt_buffer = xrealloc(c->prompt_buffer, size + 1); 896 first = c->prompt_buffer + off; 897 memmove(first + strlen(s), first, n); 898 memcpy(first, s, strlen(s)); 899 900 c->prompt_index = (first - c->prompt_buffer) + strlen(s); 901 free(s); 902 903 c->flags |= CLIENT_STATUS; 904 break; 905 case MODEKEYEDIT_BACKSPACE: 906 if (c->prompt_index != 0) { 907 if (c->prompt_index == size) 908 c->prompt_buffer[--c->prompt_index] = '\0'; 909 else { 910 memmove(c->prompt_buffer + c->prompt_index - 1, 911 c->prompt_buffer + c->prompt_index, 912 size + 1 - c->prompt_index); 913 c->prompt_index--; 914 } 915 c->flags |= CLIENT_STATUS; 916 } 917 break; 918 case MODEKEYEDIT_DELETE: 919 case MODEKEYEDIT_SWITCHMODESUBSTITUTE: 920 if (c->prompt_index != size) { 921 memmove(c->prompt_buffer + c->prompt_index, 922 c->prompt_buffer + c->prompt_index + 1, 923 size + 1 - c->prompt_index); 924 c->flags |= CLIENT_STATUS; 925 } 926 break; 927 case MODEKEYEDIT_DELETELINE: 928 case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE: 929 *c->prompt_buffer = '\0'; 930 c->prompt_index = 0; 931 c->flags |= CLIENT_STATUS; 932 break; 933 case MODEKEYEDIT_DELETETOENDOFLINE: 934 case MODEKEYEDIT_SWITCHMODECHANGELINE: 935 if (c->prompt_index < size) { 936 c->prompt_buffer[c->prompt_index] = '\0'; 937 c->flags |= CLIENT_STATUS; 938 } 939 break; 940 case MODEKEYEDIT_DELETEWORD: 941 wsep = options_get_string(oo, "word-separators"); 942 idx = c->prompt_index; 943 944 /* Find a non-separator. */ 945 while (idx != 0) { 946 idx--; 947 if (!strchr(wsep, c->prompt_buffer[idx])) 948 break; 949 } 950 951 /* Find the separator at the beginning of the word. */ 952 while (idx != 0) { 953 idx--; 954 if (strchr(wsep, c->prompt_buffer[idx])) { 955 /* Go back to the word. */ 956 idx++; 957 break; 958 } 959 } 960 961 memmove(c->prompt_buffer + idx, 962 c->prompt_buffer + c->prompt_index, 963 size + 1 - c->prompt_index); 964 memset(c->prompt_buffer + size - (c->prompt_index - idx), 965 '\0', c->prompt_index - idx); 966 c->prompt_index = idx; 967 c->flags |= CLIENT_STATUS; 968 break; 969 case MODEKEYEDIT_NEXTSPACE: 970 wsep = " "; 971 /* FALLTHROUGH */ 972 case MODEKEYEDIT_NEXTWORD: 973 if (wsep == NULL) 974 wsep = options_get_string(oo, "word-separators"); 975 976 /* Find a separator. */ 977 while (c->prompt_index != size) { 978 c->prompt_index++; 979 if (strchr(wsep, c->prompt_buffer[c->prompt_index])) 980 break; 981 } 982 983 /* Find the word right after the separation. */ 984 while (c->prompt_index != size) { 985 c->prompt_index++; 986 if (!strchr(wsep, c->prompt_buffer[c->prompt_index])) 987 break; 988 } 989 990 c->flags |= CLIENT_STATUS; 991 break; 992 case MODEKEYEDIT_NEXTSPACEEND: 993 wsep = " "; 994 /* FALLTHROUGH */ 995 case MODEKEYEDIT_NEXTWORDEND: 996 if (wsep == NULL) 997 wsep = options_get_string(oo, "word-separators"); 998 999 /* Find a word. */ 1000 while (c->prompt_index != size) { 1001 c->prompt_index++; 1002 if (!strchr(wsep, c->prompt_buffer[c->prompt_index])) 1003 break; 1004 } 1005 1006 /* Find the separator at the end of the word. */ 1007 while (c->prompt_index != size) { 1008 c->prompt_index++; 1009 if (strchr(wsep, c->prompt_buffer[c->prompt_index])) 1010 break; 1011 } 1012 1013 /* Back up to the end-of-word like vi. */ 1014 if (options_get_number(oo, "status-keys") == MODEKEY_VI && 1015 c->prompt_index != 0) 1016 c->prompt_index--; 1017 1018 c->flags |= CLIENT_STATUS; 1019 break; 1020 case MODEKEYEDIT_PREVIOUSSPACE: 1021 wsep = " "; 1022 /* FALLTHROUGH */ 1023 case MODEKEYEDIT_PREVIOUSWORD: 1024 if (wsep == NULL) 1025 wsep = options_get_string(oo, "word-separators"); 1026 1027 /* Find a non-separator. */ 1028 while (c->prompt_index != 0) { 1029 c->prompt_index--; 1030 if (!strchr(wsep, c->prompt_buffer[c->prompt_index])) 1031 break; 1032 } 1033 1034 /* Find the separator at the beginning of the word. */ 1035 while (c->prompt_index != 0) { 1036 c->prompt_index--; 1037 if (strchr(wsep, c->prompt_buffer[c->prompt_index])) { 1038 /* Go back to the word. */ 1039 c->prompt_index++; 1040 break; 1041 } 1042 } 1043 1044 c->flags |= CLIENT_STATUS; 1045 break; 1046 case MODEKEYEDIT_HISTORYUP: 1047 histstr = status_prompt_up_history(&c->prompt_hindex); 1048 if (histstr == NULL) 1049 break; 1050 free(c->prompt_buffer); 1051 c->prompt_buffer = xstrdup(histstr); 1052 c->prompt_index = strlen(c->prompt_buffer); 1053 c->flags |= CLIENT_STATUS; 1054 break; 1055 case MODEKEYEDIT_HISTORYDOWN: 1056 histstr = status_prompt_down_history(&c->prompt_hindex); 1057 if (histstr == NULL) 1058 break; 1059 free(c->prompt_buffer); 1060 c->prompt_buffer = xstrdup(histstr); 1061 c->prompt_index = strlen(c->prompt_buffer); 1062 c->flags |= CLIENT_STATUS; 1063 break; 1064 case MODEKEYEDIT_PASTE: 1065 if ((pb = paste_get_top(NULL)) == NULL) 1066 break; 1067 bufdata = paste_buffer_data(pb, &bufsize); 1068 for (n = 0; n < bufsize; n++) { 1069 ch = (u_char)bufdata[n]; 1070 if (ch < 32 || ch == 127) 1071 break; 1072 } 1073 1074 c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1); 1075 if (c->prompt_index == size) { 1076 memcpy(c->prompt_buffer + c->prompt_index, bufdata, n); 1077 c->prompt_index += n; 1078 c->prompt_buffer[c->prompt_index] = '\0'; 1079 } else { 1080 memmove(c->prompt_buffer + c->prompt_index + n, 1081 c->prompt_buffer + c->prompt_index, 1082 size + 1 - c->prompt_index); 1083 memcpy(c->prompt_buffer + c->prompt_index, bufdata, n); 1084 c->prompt_index += n; 1085 } 1086 1087 c->flags |= CLIENT_STATUS; 1088 break; 1089 case MODEKEYEDIT_TRANSPOSECHARS: 1090 idx = c->prompt_index; 1091 if (idx < size) 1092 idx++; 1093 if (idx >= 2) { 1094 swapc = c->prompt_buffer[idx - 2]; 1095 c->prompt_buffer[idx - 2] = c->prompt_buffer[idx - 1]; 1096 c->prompt_buffer[idx - 1] = swapc; 1097 c->prompt_index = idx; 1098 c->flags |= CLIENT_STATUS; 1099 } 1100 break; 1101 case MODEKEYEDIT_ENTER: 1102 if (*c->prompt_buffer != '\0') 1103 status_prompt_add_history(c->prompt_buffer); 1104 if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0) 1105 status_prompt_clear(c); 1106 break; 1107 case MODEKEYEDIT_CANCEL: 1108 if (c->prompt_callbackfn(c->prompt_data, NULL) == 0) 1109 status_prompt_clear(c); 1110 break; 1111 case MODEKEY_OTHER: 1112 if (key <= 0x1f || key >= 0x7f) 1113 break; 1114 c->prompt_buffer = xrealloc(c->prompt_buffer, size + 2); 1115 1116 if (c->prompt_index == size) { 1117 c->prompt_buffer[c->prompt_index++] = key; 1118 c->prompt_buffer[c->prompt_index] = '\0'; 1119 } else { 1120 memmove(c->prompt_buffer + c->prompt_index + 1, 1121 c->prompt_buffer + c->prompt_index, 1122 size + 1 - c->prompt_index); 1123 c->prompt_buffer[c->prompt_index++] = key; 1124 } 1125 1126 if (c->prompt_flags & PROMPT_SINGLE) { 1127 if (c->prompt_callbackfn( 1128 c->prompt_data, c->prompt_buffer) == 0) 1129 status_prompt_clear(c); 1130 } 1131 1132 c->flags |= CLIENT_STATUS; 1133 break; 1134 default: 1135 break; 1136 } 1137} 1138 1139/* Get previous line from the history. */ 1140const char * 1141status_prompt_up_history(u_int *idx) 1142{ 1143 /* 1144 * History runs from 0 to size - 1. Index is from 0 to size. Zero is 1145 * empty. 1146 */ 1147 1148 if (status_prompt_hsize == 0 || *idx == status_prompt_hsize) 1149 return (NULL); 1150 (*idx)++; 1151 return (status_prompt_hlist[status_prompt_hsize - *idx]); 1152} 1153 1154/* Get next line from the history. */ 1155const char * 1156status_prompt_down_history(u_int *idx) 1157{ 1158 if (status_prompt_hsize == 0 || *idx == 0) 1159 return (""); 1160 (*idx)--; 1161 if (*idx == 0) 1162 return (""); 1163 return (status_prompt_hlist[status_prompt_hsize - *idx]); 1164} 1165 1166/* Add line to the history. */ 1167void 1168status_prompt_add_history(const char *line) 1169{ 1170 size_t size; 1171 1172 if (status_prompt_hsize > 0 && 1173 strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0) 1174 return; 1175 1176 if (status_prompt_hsize == PROMPT_HISTORY) { 1177 free(status_prompt_hlist[0]); 1178 1179 size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist; 1180 memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size); 1181 1182 status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line); 1183 return; 1184 } 1185 1186 status_prompt_hlist = xreallocarray(status_prompt_hlist, 1187 status_prompt_hsize + 1, sizeof *status_prompt_hlist); 1188 status_prompt_hlist[status_prompt_hsize++] = xstrdup(line); 1189} 1190 1191/* Build completion list. */ 1192const char ** 1193status_prompt_complete_list(u_int *size, const char *s) 1194{ 1195 const char **list = NULL, **layout; 1196 const struct cmd_entry **cmdent; 1197 const struct options_table_entry *oe; 1198 const char *layouts[] = { 1199 "even-horizontal", "even-vertical", "main-horizontal", 1200 "main-vertical", "tiled", NULL 1201 }; 1202 1203 *size = 0; 1204 for (cmdent = cmd_table; *cmdent != NULL; cmdent++) { 1205 if (strncmp((*cmdent)->name, s, strlen(s)) == 0) { 1206 list = xreallocarray(list, (*size) + 1, sizeof *list); 1207 list[(*size)++] = (*cmdent)->name; 1208 } 1209 } 1210 for (oe = options_table; oe->name != NULL; oe++) { 1211 if (strncmp(oe->name, s, strlen(s)) == 0) { 1212 list = xreallocarray(list, (*size) + 1, sizeof *list); 1213 list[(*size)++] = oe->name; 1214 } 1215 } 1216 for (layout = layouts; *layout != NULL; layout++) { 1217 if (strncmp(*layout, s, strlen(s)) == 0) { 1218 list = xreallocarray(list, (*size) + 1, sizeof *list); 1219 list[(*size)++] = *layout; 1220 } 1221 } 1222 return (list); 1223} 1224 1225/* Find longest prefix. */ 1226char * 1227status_prompt_complete_prefix(const char **list, u_int size) 1228{ 1229 char *out; 1230 u_int i; 1231 size_t j; 1232 1233 out = xstrdup(list[0]); 1234 for (i = 1; i < size; i++) { 1235 j = strlen(list[i]); 1236 if (j > strlen(out)) 1237 j = strlen(out); 1238 for (; j > 0; j--) { 1239 if (out[j - 1] != list[i][j - 1]) 1240 out[j - 1] = '\0'; 1241 } 1242 } 1243 return (out); 1244} 1245 1246/* Complete word. */ 1247char * 1248status_prompt_complete(struct session *sess, const char *s) 1249{ 1250 const char **list = NULL, *colon; 1251 u_int size = 0, i; 1252 struct session *s_loop; 1253 struct winlink *wl; 1254 struct window *w; 1255 char *copy, *out, *tmp; 1256 1257 if (*s == '\0') 1258 return (NULL); 1259 out = NULL; 1260 1261 if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) { 1262 list = status_prompt_complete_list(&size, s); 1263 if (size == 0) 1264 out = NULL; 1265 else if (size == 1) 1266 xasprintf(&out, "%s ", list[0]); 1267 else 1268 out = status_prompt_complete_prefix(list, size); 1269 free(list); 1270 return (out); 1271 } 1272 copy = xstrdup(s); 1273 1274 colon = ":"; 1275 if (copy[strlen(copy) - 1] == ':') 1276 copy[strlen(copy) - 1] = '\0'; 1277 else 1278 colon = ""; 1279 s = copy + 2; 1280 1281 RB_FOREACH(s_loop, sessions, &sessions) { 1282 if (strncmp(s_loop->name, s, strlen(s)) == 0) { 1283 list = xreallocarray(list, size + 2, sizeof *list); 1284 list[size++] = s_loop->name; 1285 } 1286 } 1287 if (size == 1) { 1288 out = xstrdup(list[0]); 1289 if (session_find(list[0]) != NULL) 1290 colon = ":"; 1291 } else if (size != 0) 1292 out = status_prompt_complete_prefix(list, size); 1293 if (out != NULL) { 1294 xasprintf(&tmp, "-%c%s%s", copy[1], out, colon); 1295 out = tmp; 1296 goto found; 1297 } 1298 1299 colon = ""; 1300 if (*s == ':') { 1301 RB_FOREACH(wl, winlinks, &sess->windows) { 1302 xasprintf(&tmp, ":%s", wl->window->name); 1303 if (strncmp(tmp, s, strlen(s)) == 0){ 1304 list = xreallocarray(list, size + 1, 1305 sizeof *list); 1306 list[size++] = tmp; 1307 continue; 1308 } 1309 free(tmp); 1310 1311 xasprintf(&tmp, ":%d", wl->idx); 1312 if (strncmp(tmp, s, strlen(s)) == 0) { 1313 list = xreallocarray(list, size + 1, 1314 sizeof *list); 1315 list[size++] = tmp; 1316 continue; 1317 } 1318 free(tmp); 1319 } 1320 } else { 1321 RB_FOREACH(s_loop, sessions, &sessions) { 1322 RB_FOREACH(wl, winlinks, &s_loop->windows) { 1323 w = wl->window; 1324 1325 xasprintf(&tmp, "%s:%s", s_loop->name, w->name); 1326 if (strncmp(tmp, s, strlen(s)) == 0) { 1327 list = xreallocarray(list, size + 1, 1328 sizeof *list); 1329 list[size++] = tmp; 1330 continue; 1331 } 1332 free(tmp); 1333 1334 xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx); 1335 if (strncmp(tmp, s, strlen(s)) == 0) { 1336 list = xreallocarray(list, size + 1, 1337 sizeof *list); 1338 list[size++] = tmp; 1339 continue; 1340 } 1341 free(tmp); 1342 } 1343 } 1344 } 1345 if (size == 1) { 1346 out = xstrdup(list[0]); 1347 colon = " "; 1348 } else if (size != 0) 1349 out = status_prompt_complete_prefix(list, size); 1350 if (out != NULL) { 1351 xasprintf(&tmp, "-%c%s%s", copy[1], out, colon); 1352 out = tmp; 1353 } 1354 1355 for (i = 0; i < size; i++) 1356 free((void *)list[i]); 1357 1358found: 1359 free(copy); 1360 free(list); 1361 return (out); 1362} 1363