status.c revision 1.60
1/* $OpenBSD: status.c,v 1.60 2010/03/27 15:06:40 nicm 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( 33 struct client *, time_t, int, struct grid_cell *, size_t *); 34char *status_redraw_get_right( 35 struct client *, time_t, int, struct grid_cell *, size_t *); 36char *status_job(struct client *, char **); 37void status_job_callback(struct job *); 38char *status_print( 39 struct client *, struct winlink *, time_t, struct grid_cell *); 40void status_replace1(struct client *, 41 struct winlink *, char **, char **, char *, size_t, int); 42void status_message_callback(int, short, void *); 43 44void status_prompt_add_history(struct client *); 45char *status_prompt_complete(const char *); 46 47/* Retrieve options for left string. */ 48char * 49status_redraw_get_left(struct client *c, 50 time_t t, int utf8flag, struct grid_cell *gc, size_t *size) 51{ 52 struct session *s = c->session; 53 char *left; 54 u_char fg, bg, attr; 55 size_t leftlen; 56 57 fg = options_get_number(&s->options, "status-left-fg"); 58 if (fg != 8) 59 colour_set_fg(gc, fg); 60 bg = options_get_number(&s->options, "status-left-bg"); 61 if (bg != 8) 62 colour_set_bg(gc, bg); 63 attr = options_get_number(&s->options, "status-left-attr"); 64 if (attr != 0) 65 gc->attr = attr; 66 67 left = status_replace( 68 c, NULL, options_get_string(&s->options, "status-left"), t, 1); 69 70 *size = options_get_number(&s->options, "status-left-length"); 71 leftlen = screen_write_cstrlen(utf8flag, "%s", left); 72 if (leftlen < *size) 73 *size = leftlen; 74 return (left); 75} 76 77/* Retrieve options for right string. */ 78char * 79status_redraw_get_right(struct client *c, 80 time_t t, int utf8flag, struct grid_cell *gc, size_t *size) 81{ 82 struct session *s = c->session; 83 char *right; 84 u_char fg, bg, attr; 85 size_t rightlen; 86 87 fg = options_get_number(&s->options, "status-right-fg"); 88 if (fg != 8) 89 colour_set_fg(gc, fg); 90 bg = options_get_number(&s->options, "status-right-bg"); 91 if (bg != 8) 92 colour_set_bg(gc, bg); 93 attr = options_get_number(&s->options, "status-right-attr"); 94 if (attr != 0) 95 gc->attr = attr; 96 97 right = status_replace( 98 c, NULL, options_get_string(&s->options, "status-right"), t, 1); 99 100 *size = options_get_number(&s->options, "status-right-length"); 101 rightlen = screen_write_cstrlen(utf8flag, "%s", right); 102 if (rightlen < *size) 103 *size = rightlen; 104 return (right); 105} 106 107/* Draw status for client on the last lines of given context. */ 108int 109status_redraw(struct client *c) 110{ 111 struct screen_write_ctx ctx; 112 struct session *s = c->session; 113 struct winlink *wl; 114 struct screen old_status, window_list; 115 struct grid_cell stdgc, lgc, rgc, gc; 116 time_t t; 117 char *left, *right; 118 u_int offset, needed; 119 u_int wlstart, wlwidth, wlavailable, wloffset, wlsize; 120 size_t llen, rlen; 121 int larrow, rarrow, utf8flag; 122 123 /* No status line? */ 124 if (c->tty.sy == 0 || !options_get_number(&s->options, "status")) 125 return (1); 126 left = right = NULL; 127 larrow = rarrow = 0; 128 129 /* Update status timer. */ 130 if (gettimeofday(&c->status_timer, NULL) != 0) 131 fatal("gettimeofday failed"); 132 t = c->status_timer.tv_sec; 133 134 /* Set up default colour. */ 135 memcpy(&stdgc, &grid_default_cell, sizeof gc); 136 colour_set_fg(&stdgc, options_get_number(&s->options, "status-fg")); 137 colour_set_bg(&stdgc, options_get_number(&s->options, "status-bg")); 138 stdgc.attr |= options_get_number(&s->options, "status-attr"); 139 140 /* Create the target screen. */ 141 memcpy(&old_status, &c->status, sizeof old_status); 142 screen_init(&c->status, c->tty.sx, 1, 0); 143 screen_write_start(&ctx, NULL, &c->status); 144 for (offset = 0; offset < c->tty.sx; offset++) 145 screen_write_putc(&ctx, &stdgc, ' '); 146 screen_write_stop(&ctx); 147 148 /* If the height is one line, blank status line. */ 149 if (c->tty.sy <= 1) 150 goto out; 151 152 /* Get UTF-8 flag. */ 153 utf8flag = options_get_number(&s->options, "status-utf8"); 154 155 /* Work out left and right strings. */ 156 memcpy(&lgc, &stdgc, sizeof lgc); 157 left = status_redraw_get_left(c, t, utf8flag, &lgc, &llen); 158 memcpy(&rgc, &stdgc, sizeof rgc); 159 right = status_redraw_get_right(c, t, utf8flag, &rgc, &rlen); 160 161 /* 162 * Figure out how much space we have for the window list. If there 163 * isn't enough space, just show a blank status line. 164 */ 165 needed = 0; 166 if (llen != 0) 167 needed += llen + 1; 168 if (rlen != 0) 169 needed += rlen + 1; 170 if (c->tty.sx == 0 || c->tty.sx <= needed) 171 goto out; 172 wlavailable = c->tty.sx - needed; 173 174 /* Calculate the total size needed for the window list. */ 175 wlstart = wloffset = wlwidth = 0; 176 RB_FOREACH(wl, winlinks, &s->windows) { 177 if (wl->status_text != NULL) 178 xfree(wl->status_text); 179 memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell); 180 wl->status_text = status_print(c, wl, t, &wl->status_cell); 181 wl->status_width = 182 screen_write_cstrlen(utf8flag, "%s", wl->status_text); 183 184 if (wl == s->curw) 185 wloffset = wlwidth; 186 wlwidth += wl->status_width + 1; 187 } 188 189 /* Create a new screen for the window list. */ 190 screen_init(&window_list, wlwidth, 1, 0); 191 192 /* And draw the window list into it. */ 193 screen_write_start(&ctx, NULL, &window_list); 194 RB_FOREACH(wl, winlinks, &s->windows) { 195 screen_write_cnputs(&ctx, 196 -1, &wl->status_cell, utf8flag, "%s", wl->status_text); 197 screen_write_putc(&ctx, &stdgc, ' '); 198 } 199 screen_write_stop(&ctx); 200 201 /* If there is enough space for the total width, skip to draw now. */ 202 if (wlwidth <= wlavailable) 203 goto draw; 204 205 /* Find size of current window text. */ 206 wlsize = s->curw->status_width; 207 208 /* 209 * If the current window is already on screen, good to draw from the 210 * start and just leave off the end. 211 */ 212 if (wloffset + wlsize < wlavailable) { 213 if (wlavailable > 0) { 214 rarrow = 1; 215 wlavailable--; 216 } 217 wlwidth = wlavailable; 218 } else { 219 /* 220 * Work out how many characters we need to omit from the 221 * start. There are wlavailable characters to fill, and 222 * wloffset + wlsize must be the last. So, the start character 223 * is wloffset + wlsize - wlavailable. 224 */ 225 if (wlavailable > 0) { 226 larrow = 1; 227 wlavailable--; 228 } 229 230 wlstart = wloffset + wlsize - wlavailable; 231 if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) { 232 rarrow = 1; 233 wlstart++; 234 wlavailable--; 235 } 236 wlwidth = wlavailable; 237 } 238 239 /* Bail if anything is now too small too. */ 240 if (wlwidth == 0 || wlavailable == 0) { 241 screen_free(&window_list); 242 goto out; 243 } 244 245 /* 246 * Now the start position is known, work out the state of the left and 247 * right arrows. 248 */ 249 offset = 0; 250 RB_FOREACH(wl, winlinks, &s->windows) { 251 if (larrow == 1 && offset < wlstart) { 252 if (session_alert_has(s, wl, WINDOW_ACTIVITY)) 253 larrow = -1; 254 else if (session_alert_has(s, wl, WINDOW_BELL)) 255 larrow = -1; 256 else if (session_alert_has(s, wl, WINDOW_CONTENT)) 257 larrow = -1; 258 } 259 260 offset += wl->status_width; 261 262 if (rarrow == 1 && offset > wlstart + wlwidth) { 263 if (session_alert_has(s, wl, WINDOW_ACTIVITY)) 264 rarrow = -1; 265 else if (session_alert_has(s, wl, WINDOW_BELL)) 266 rarrow = -1; 267 else if (session_alert_has(s, wl, WINDOW_CONTENT)) 268 rarrow = -1; 269 } 270 } 271 272draw: 273 /* Begin drawing. */ 274 screen_write_start(&ctx, NULL, &c->status); 275 276 /* Draw the left string and arrow. */ 277 screen_write_cursormove(&ctx, 0, 0); 278 if (llen != 0) { 279 screen_write_cnputs(&ctx, llen, &lgc, utf8flag, "%s", left); 280 screen_write_putc(&ctx, &stdgc, ' '); 281 } 282 if (larrow != 0) { 283 memcpy(&gc, &stdgc, sizeof gc); 284 if (larrow == -1) 285 gc.attr ^= GRID_ATTR_REVERSE; 286 screen_write_putc(&ctx, &gc, '<'); 287 } 288 289 /* Draw the right string and arrow. */ 290 if (rarrow != 0) { 291 screen_write_cursormove(&ctx, c->tty.sx - rlen - 2, 0); 292 memcpy(&gc, &stdgc, sizeof gc); 293 if (rarrow == -1) 294 gc.attr ^= GRID_ATTR_REVERSE; 295 screen_write_putc(&ctx, &gc, '>'); 296 } else 297 screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0); 298 if (rlen != 0) { 299 screen_write_putc(&ctx, &stdgc, ' '); 300 screen_write_cnputs(&ctx, rlen, &rgc, utf8flag, "%s", right); 301 } 302 303 /* Figure out the offset for the window list. */ 304 if (llen != 0) 305 wloffset = llen + 1; 306 else 307 wloffset = 0; 308 if (wlwidth < wlavailable) { 309 switch (options_get_number(&s->options, "status-justify")) { 310 case 1: /* centered */ 311 wloffset += (wlavailable - wlwidth) / 2; 312 break; 313 case 2: /* right */ 314 wloffset += (wlavailable - wlwidth); 315 break; 316 } 317 } 318 if (larrow != 0) 319 wloffset++; 320 321 /* Copy the window list. */ 322 screen_write_cursormove(&ctx, wloffset, 0); 323 screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1); 324 screen_free(&window_list); 325 326 screen_write_stop(&ctx); 327 328out: 329 if (left != NULL) 330 xfree(left); 331 if (right != NULL) 332 xfree(right); 333 334 if (grid_compare(c->status.grid, old_status.grid) == 0) { 335 screen_free(&old_status); 336 return (0); 337 } 338 screen_free(&old_status); 339 return (1); 340} 341 342/* Replace a single special sequence (prefixed by #). */ 343void 344status_replace1(struct client *c,struct winlink *wl, 345 char **iptr, char **optr, char *out, size_t outsize, int jobsflag) 346{ 347 struct session *s = c->session; 348 char ch, tmp[256], *ptr, *endptr, *freeptr; 349 size_t ptrlen; 350 long limit; 351 352 if (wl == NULL) 353 wl = s->curw; 354 355 errno = 0; 356 limit = strtol(*iptr, &endptr, 10); 357 if ((limit == 0 && errno != EINVAL) || 358 (limit == LONG_MIN && errno != ERANGE) || 359 (limit == LONG_MAX && errno != ERANGE) || 360 limit != 0) 361 *iptr = endptr; 362 if (limit <= 0) 363 limit = LONG_MAX; 364 365 freeptr = NULL; 366 367 switch (*(*iptr)++) { 368 case '(': 369 if (!jobsflag) { 370 ch = ')'; 371 goto skip_to; 372 } 373 if ((ptr = status_job(c, iptr)) == NULL) 374 return; 375 freeptr = ptr; 376 goto do_replace; 377 case 'H': 378 if (gethostname(tmp, sizeof tmp) != 0) 379 fatal("gethostname failed"); 380 ptr = tmp; 381 goto do_replace; 382 case 'I': 383 xsnprintf(tmp, sizeof tmp, "%d", wl->idx); 384 ptr = tmp; 385 goto do_replace; 386 case 'P': 387 xsnprintf(tmp, sizeof tmp, "%u", 388 window_pane_index(wl->window, wl->window->active)); 389 ptr = tmp; 390 goto do_replace; 391 case 'S': 392 ptr = s->name; 393 goto do_replace; 394 case 'T': 395 ptr = wl->window->active->base.title; 396 goto do_replace; 397 case 'W': 398 ptr = wl->window->name; 399 goto do_replace; 400 case 'F': 401 tmp[0] = ' '; 402 if (session_alert_has(s, wl, WINDOW_CONTENT)) 403 tmp[0] = '+'; 404 else if (session_alert_has(s, wl, WINDOW_BELL)) 405 tmp[0] = '!'; 406 else if (session_alert_has(s, wl, WINDOW_ACTIVITY)) 407 tmp[0] = '#'; 408 else if (wl == s->curw) 409 tmp[0] = '*'; 410 else if (wl == TAILQ_FIRST(&s->lastw)) 411 tmp[0] = '-'; 412 tmp[1] = '\0'; 413 ptr = tmp; 414 goto do_replace; 415 case '[': 416 /* 417 * Embedded style, handled at display time. Leave present and 418 * skip input until ]. 419 */ 420 ch = ']'; 421 goto skip_to; 422 case '#': 423 *(*optr)++ = '#'; 424 break; 425 } 426 427 return; 428 429do_replace: 430 ptrlen = strlen(ptr); 431 if ((size_t) limit < ptrlen) 432 ptrlen = limit; 433 434 if (*optr + ptrlen >= out + outsize - 1) 435 return; 436 while (ptrlen > 0 && *ptr != '\0') { 437 *(*optr)++ = *ptr++; 438 ptrlen--; 439 } 440 441 if (freeptr != NULL) 442 xfree(freeptr); 443 return; 444 445skip_to: 446 *(*optr)++ = '#'; 447 448 (*iptr)--; /* include ch */ 449 while (**iptr != ch && **iptr != '\0') { 450 if (*optr >= out + outsize - 1) 451 break; 452 *(*optr)++ = *(*iptr)++; 453 } 454} 455 456/* Replace special sequences in fmt. */ 457char * 458status_replace(struct client *c, 459 struct winlink *wl, const char *fmt, time_t t, int jobsflag) 460{ 461 static char out[BUFSIZ]; 462 char in[BUFSIZ], ch, *iptr, *optr; 463 464 strftime(in, sizeof in, fmt, localtime(&t)); 465 in[(sizeof in) - 1] = '\0'; 466 467 iptr = in; 468 optr = out; 469 470 while (*iptr != '\0') { 471 if (optr >= out + (sizeof out) - 1) 472 break; 473 ch = *iptr++; 474 475 if (ch != '#') { 476 *optr++ = ch; 477 continue; 478 } 479 status_replace1(c, wl, &iptr, &optr, out, sizeof out, jobsflag); 480 } 481 *optr = '\0'; 482 483 return (xstrdup(out)); 484} 485 486/* Figure out job name and get its result, starting it off if necessary. */ 487char * 488status_job(struct client *c, char **iptr) 489{ 490 struct job *job; 491 char *cmd; 492 int lastesc; 493 size_t len; 494 495 if (**iptr == '\0') 496 return (NULL); 497 if (**iptr == ')') { /* no command given */ 498 (*iptr)++; 499 return (NULL); 500 } 501 502 cmd = xmalloc(strlen(*iptr) + 1); 503 len = 0; 504 505 lastesc = 0; 506 for (; **iptr != '\0'; (*iptr)++) { 507 if (!lastesc && **iptr == ')') 508 break; /* unescaped ) is the end */ 509 if (!lastesc && **iptr == '\\') { 510 lastesc = 1; 511 continue; /* skip \ if not escaped */ 512 } 513 lastesc = 0; 514 cmd[len++] = **iptr; 515 } 516 if (**iptr == '\0') /* no terminating ) */ { 517 xfree(cmd); 518 return (NULL); 519 } 520 (*iptr)++; /* skip final ) */ 521 cmd[len] = '\0'; 522 523 job = job_get(&c->status_jobs, cmd); 524 if (job == NULL) { 525 job = job_add(&c->status_jobs, 526 JOB_PERSIST, c, cmd, status_job_callback, xfree, NULL); 527 job_run(job); 528 } 529 xfree(cmd); 530 if (job->data == NULL) 531 return (xstrdup("")); 532 return (xstrdup(job->data)); 533} 534 535/* Job has finished: save its result. */ 536void 537status_job_callback(struct job *job) 538{ 539 char *line, *buf; 540 size_t len; 541 542 buf = NULL; 543 if ((line = evbuffer_readline(job->event->input)) == NULL) { 544 len = EVBUFFER_LENGTH(job->event->input); 545 buf = xmalloc(len + 1); 546 if (len != 0) 547 memcpy(buf, EVBUFFER_DATA(job->event->input), len); 548 buf[len] = '\0'; 549 } 550 551 if (job->data != NULL) 552 xfree(job->data); 553 else 554 server_redraw_client(job->client); 555 556 if (line == NULL) 557 job->data = buf; 558 else 559 job->data = xstrdup(line); 560} 561 562/* Return winlink status line entry and adjust gc as necessary. */ 563char * 564status_print( 565 struct client *c, struct winlink *wl, time_t t, struct grid_cell *gc) 566{ 567 struct options *oo = &wl->window->options; 568 struct session *s = c->session; 569 const char *fmt; 570 char *text; 571 u_char fg, bg, attr; 572 573 fg = options_get_number(oo, "window-status-fg"); 574 if (fg != 8) 575 colour_set_fg(gc, fg); 576 bg = options_get_number(oo, "window-status-bg"); 577 if (bg != 8) 578 colour_set_bg(gc, bg); 579 attr = options_get_number(oo, "window-status-attr"); 580 if (attr != 0) 581 gc->attr = attr; 582 fmt = options_get_string(oo, "window-status-format"); 583 if (wl == s->curw) { 584 fg = options_get_number(oo, "window-status-current-fg"); 585 if (fg != 8) 586 colour_set_fg(gc, fg); 587 bg = options_get_number(oo, "window-status-current-bg"); 588 if (bg != 8) 589 colour_set_bg(gc, bg); 590 attr = options_get_number(oo, "window-status-current-attr"); 591 if (attr != 0) 592 gc->attr = attr; 593 fmt = options_get_string(oo, "window-status-current-format"); 594 } 595 596 if (session_alert_has(s, wl, WINDOW_ACTIVITY) || 597 session_alert_has(s, wl, WINDOW_BELL) || 598 session_alert_has(s, wl, WINDOW_CONTENT)) 599 gc->attr ^= GRID_ATTR_REVERSE; 600 601 text = status_replace(c, wl, fmt, t, 1); 602 return (text); 603} 604 605/* Set a status line message. */ 606void printflike2 607status_message_set(struct client *c, const char *fmt, ...) 608{ 609 struct timeval tv; 610 struct session *s = c->session; 611 struct message_entry *msg; 612 va_list ap; 613 int delay; 614 u_int i, limit; 615 616 status_prompt_clear(c); 617 status_message_clear(c); 618 619 va_start(ap, fmt); 620 xvasprintf(&c->message_string, fmt, ap); 621 va_end(ap); 622 623 ARRAY_EXPAND(&c->message_log, 1); 624 msg = &ARRAY_LAST(&c->message_log); 625 msg->msg_time = time(NULL); 626 msg->msg = xstrdup(c->message_string); 627 628 if (s == NULL) 629 limit = 0; 630 else 631 limit = options_get_number(&s->options, "message-limit"); 632 if (ARRAY_LENGTH(&c->message_log) > limit) { 633 limit = ARRAY_LENGTH(&c->message_log) - limit; 634 for (i = 0; i < limit; i++) { 635 msg = &ARRAY_FIRST(&c->message_log); 636 xfree(msg->msg); 637 ARRAY_REMOVE(&c->message_log, 0); 638 } 639 } 640 641 delay = options_get_number(&c->session->options, "display-time"); 642 tv.tv_sec = delay / 1000; 643 tv.tv_usec = (delay % 1000) * 1000L; 644 645 evtimer_del(&c->message_timer); 646 evtimer_set(&c->message_timer, status_message_callback, c); 647 evtimer_add(&c->message_timer, &tv); 648 649 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); 650 c->flags |= CLIENT_STATUS; 651} 652 653/* Clear status line message. */ 654void 655status_message_clear(struct client *c) 656{ 657 if (c->message_string == NULL) 658 return; 659 660 xfree(c->message_string); 661 c->message_string = NULL; 662 663 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); 664 c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ 665 666 screen_reinit(&c->status); 667} 668 669/* Clear status line message after timer expires. */ 670/* ARGSUSED */ 671void 672status_message_callback(unused int fd, unused short event, void *data) 673{ 674 struct client *c = data; 675 676 status_message_clear(c); 677} 678 679/* Draw client message on status line of present else on last line. */ 680int 681status_message_redraw(struct client *c) 682{ 683 struct screen_write_ctx ctx; 684 struct session *s = c->session; 685 struct screen old_status; 686 size_t len; 687 struct grid_cell gc; 688 int utf8flag; 689 690 if (c->tty.sx == 0 || c->tty.sy == 0) 691 return (0); 692 memcpy(&old_status, &c->status, sizeof old_status); 693 screen_init(&c->status, c->tty.sx, 1, 0); 694 695 utf8flag = options_get_number(&s->options, "status-utf8"); 696 697 len = screen_write_strlen(utf8flag, "%s", c->message_string); 698 if (len > c->tty.sx) 699 len = c->tty.sx; 700 701 memcpy(&gc, &grid_default_cell, sizeof gc); 702 colour_set_fg(&gc, options_get_number(&s->options, "message-fg")); 703 colour_set_bg(&gc, options_get_number(&s->options, "message-bg")); 704 gc.attr |= options_get_number(&s->options, "message-attr"); 705 706 screen_write_start(&ctx, NULL, &c->status); 707 708 screen_write_cursormove(&ctx, 0, 0); 709 screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->message_string); 710 for (; len < c->tty.sx; len++) 711 screen_write_putc(&ctx, &gc, ' '); 712 713 screen_write_stop(&ctx); 714 715 if (grid_compare(c->status.grid, old_status.grid) == 0) { 716 screen_free(&old_status); 717 return (0); 718 } 719 screen_free(&old_status); 720 return (1); 721} 722 723/* Enable status line prompt. */ 724void 725status_prompt_set(struct client *c, const char *msg, 726 int (*callbackfn)(void *, const char *), void (*freefn)(void *), 727 void *data, int flags) 728{ 729 int keys; 730 731 status_message_clear(c); 732 status_prompt_clear(c); 733 734 c->prompt_string = xstrdup(msg); 735 736 c->prompt_buffer = xstrdup(""); 737 c->prompt_index = 0; 738 739 c->prompt_callbackfn = callbackfn; 740 c->prompt_freefn = freefn; 741 c->prompt_data = data; 742 743 c->prompt_hindex = 0; 744 745 c->prompt_flags = flags; 746 747 keys = options_get_number(&c->session->options, "status-keys"); 748 if (keys == MODEKEY_EMACS) 749 mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit); 750 else 751 mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit); 752 753 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); 754 c->flags |= CLIENT_STATUS; 755} 756 757/* Remove status line prompt. */ 758void 759status_prompt_clear(struct client *c) 760{ 761 if (c->prompt_string == NULL) 762 return; 763 764 if (c->prompt_freefn != NULL && c->prompt_data != NULL) 765 c->prompt_freefn(c->prompt_data); 766 767 xfree(c->prompt_string); 768 c->prompt_string = NULL; 769 770 xfree(c->prompt_buffer); 771 c->prompt_buffer = NULL; 772 773 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); 774 c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ 775 776 screen_reinit(&c->status); 777} 778 779/* Update status line prompt with a new prompt string. */ 780void 781status_prompt_update(struct client *c, const char *msg) 782{ 783 xfree(c->prompt_string); 784 c->prompt_string = xstrdup(msg); 785 786 *c->prompt_buffer = '\0'; 787 c->prompt_index = 0; 788 789 c->prompt_hindex = 0; 790 791 c->flags |= CLIENT_STATUS; 792} 793 794/* Draw client prompt on status line of present else on last line. */ 795int 796status_prompt_redraw(struct client *c) 797{ 798 struct screen_write_ctx ctx; 799 struct session *s = c->session; 800 struct screen old_status; 801 size_t i, size, left, len, off; 802 struct grid_cell gc, *gcp; 803 int utf8flag; 804 805 if (c->tty.sx == 0 || c->tty.sy == 0) 806 return (0); 807 memcpy(&old_status, &c->status, sizeof old_status); 808 screen_init(&c->status, c->tty.sx, 1, 0); 809 810 utf8flag = options_get_number(&s->options, "status-utf8"); 811 812 len = screen_write_strlen(utf8flag, "%s", c->prompt_string); 813 if (len > c->tty.sx) 814 len = c->tty.sx; 815 off = 0; 816 817 memcpy(&gc, &grid_default_cell, sizeof gc); 818 colour_set_fg(&gc, options_get_number(&s->options, "message-fg")); 819 colour_set_bg(&gc, options_get_number(&s->options, "message-bg")); 820 gc.attr |= options_get_number(&s->options, "message-attr"); 821 822 screen_write_start(&ctx, NULL, &c->status); 823 824 screen_write_cursormove(&ctx, 0, 0); 825 screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->prompt_string); 826 827 left = c->tty.sx - len; 828 if (left != 0) { 829 size = screen_write_strlen(utf8flag, "%s", c->prompt_buffer); 830 if (c->prompt_index >= left) { 831 off = c->prompt_index - left + 1; 832 if (c->prompt_index == size) 833 left--; 834 size = left; 835 } 836 screen_write_nputs( 837 &ctx, left, &gc, utf8flag, "%s", c->prompt_buffer + off); 838 839 for (i = len + size; i < c->tty.sx; i++) 840 screen_write_putc(&ctx, &gc, ' '); 841 } 842 843 screen_write_stop(&ctx); 844 845 /* Apply fake cursor. */ 846 off = len + c->prompt_index - off; 847 gcp = grid_view_get_cell(c->status.grid, off, 0); 848 gcp->attr ^= GRID_ATTR_REVERSE; 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/* Handle keys in prompt. */ 859void 860status_prompt_key(struct client *c, int key) 861{ 862 struct paste_buffer *pb; 863 char *s, *first, *last, word[64], swapc; 864 u_char ch; 865 size_t size, n, off, idx; 866 867 size = strlen(c->prompt_buffer); 868 switch (mode_key_lookup(&c->prompt_mdata, key)) { 869 case MODEKEYEDIT_CURSORLEFT: 870 if (c->prompt_index > 0) { 871 c->prompt_index--; 872 c->flags |= CLIENT_STATUS; 873 } 874 break; 875 case MODEKEYEDIT_SWITCHMODEAPPEND: 876 case MODEKEYEDIT_CURSORRIGHT: 877 if (c->prompt_index < size) { 878 c->prompt_index++; 879 c->flags |= CLIENT_STATUS; 880 } 881 break; 882 case MODEKEYEDIT_STARTOFLINE: 883 if (c->prompt_index != 0) { 884 c->prompt_index = 0; 885 c->flags |= CLIENT_STATUS; 886 } 887 break; 888 case MODEKEYEDIT_ENDOFLINE: 889 if (c->prompt_index != size) { 890 c->prompt_index = size; 891 c->flags |= CLIENT_STATUS; 892 } 893 break; 894 case MODEKEYEDIT_COMPLETE: 895 if (*c->prompt_buffer == '\0') 896 break; 897 898 idx = c->prompt_index; 899 if (idx != 0) 900 idx--; 901 902 /* Find the word we are in. */ 903 first = c->prompt_buffer + idx; 904 while (first > c->prompt_buffer && *first != ' ') 905 first--; 906 while (*first == ' ') 907 first++; 908 last = c->prompt_buffer + idx; 909 while (*last != '\0' && *last != ' ') 910 last++; 911 while (*last == ' ') 912 last--; 913 if (*last != '\0') 914 last++; 915 if (last <= first || 916 ((size_t) (last - first)) > (sizeof word) - 1) 917 break; 918 memcpy(word, first, last - first); 919 word[last - first] = '\0'; 920 921 /* And try to complete it. */ 922 if ((s = status_prompt_complete(word)) == NULL) 923 break; 924 925 /* Trim out word. */ 926 n = size - (last - c->prompt_buffer) + 1; /* with \0 */ 927 memmove(first, last, n); 928 size -= last - first; 929 930 /* Insert the new word. */ 931 size += strlen(s); 932 off = first - c->prompt_buffer; 933 c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 1); 934 first = c->prompt_buffer + off; 935 memmove(first + strlen(s), first, n); 936 memcpy(first, s, strlen(s)); 937 938 c->prompt_index = (first - c->prompt_buffer) + strlen(s); 939 xfree(s); 940 941 c->flags |= CLIENT_STATUS; 942 break; 943 case MODEKEYEDIT_BACKSPACE: 944 if (c->prompt_index != 0) { 945 if (c->prompt_index == size) 946 c->prompt_buffer[--c->prompt_index] = '\0'; 947 else { 948 memmove(c->prompt_buffer + c->prompt_index - 1, 949 c->prompt_buffer + c->prompt_index, 950 size + 1 - c->prompt_index); 951 c->prompt_index--; 952 } 953 c->flags |= CLIENT_STATUS; 954 } 955 break; 956 case MODEKEYEDIT_DELETE: 957 if (c->prompt_index != size) { 958 memmove(c->prompt_buffer + c->prompt_index, 959 c->prompt_buffer + c->prompt_index + 1, 960 size + 1 - c->prompt_index); 961 c->flags |= CLIENT_STATUS; 962 } 963 break; 964 case MODEKEYEDIT_DELETELINE: 965 *c->prompt_buffer = '\0'; 966 c->prompt_index = 0; 967 c->flags |= CLIENT_STATUS; 968 break; 969 case MODEKEYEDIT_DELETETOENDOFLINE: 970 if (c->prompt_index < size) { 971 c->prompt_buffer[c->prompt_index] = '\0'; 972 c->flags |= CLIENT_STATUS; 973 } 974 break; 975 case MODEKEYEDIT_HISTORYUP: 976 if (ARRAY_LENGTH(&c->prompt_hdata) == 0) 977 break; 978 xfree(c->prompt_buffer); 979 980 c->prompt_buffer = xstrdup(ARRAY_ITEM(&c->prompt_hdata, 981 ARRAY_LENGTH(&c->prompt_hdata) - 1 - c->prompt_hindex)); 982 if (c->prompt_hindex != ARRAY_LENGTH(&c->prompt_hdata) - 1) 983 c->prompt_hindex++; 984 985 c->prompt_index = strlen(c->prompt_buffer); 986 c->flags |= CLIENT_STATUS; 987 break; 988 case MODEKEYEDIT_HISTORYDOWN: 989 xfree(c->prompt_buffer); 990 991 if (c->prompt_hindex != 0) { 992 c->prompt_hindex--; 993 c->prompt_buffer = xstrdup(ARRAY_ITEM( 994 &c->prompt_hdata, ARRAY_LENGTH( 995 &c->prompt_hdata) - 1 - c->prompt_hindex)); 996 } else 997 c->prompt_buffer = xstrdup(""); 998 999 c->prompt_index = strlen(c->prompt_buffer); 1000 c->flags |= CLIENT_STATUS; 1001 break; 1002 case MODEKEYEDIT_PASTE: 1003 if ((pb = paste_get_top(&c->session->buffers)) == NULL) 1004 break; 1005 for (n = 0; n < pb->size; n++) { 1006 ch = (u_char) pb->data[n]; 1007 if (ch < 32 || ch == 127) 1008 break; 1009 } 1010 1011 c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + n + 1); 1012 if (c->prompt_index == size) { 1013 memcpy(c->prompt_buffer + c->prompt_index, pb->data, n); 1014 c->prompt_index += n; 1015 c->prompt_buffer[c->prompt_index] = '\0'; 1016 } else { 1017 memmove(c->prompt_buffer + c->prompt_index + n, 1018 c->prompt_buffer + c->prompt_index, 1019 size + 1 - c->prompt_index); 1020 memcpy(c->prompt_buffer + c->prompt_index, pb->data, n); 1021 c->prompt_index += n; 1022 } 1023 1024 c->flags |= CLIENT_STATUS; 1025 break; 1026 case MODEKEYEDIT_TRANSPOSECHARS: 1027 idx = c->prompt_index; 1028 if (idx < size) 1029 idx++; 1030 if (idx >= 2) { 1031 swapc = c->prompt_buffer[idx - 2]; 1032 c->prompt_buffer[idx - 2] = c->prompt_buffer[idx - 1]; 1033 c->prompt_buffer[idx - 1] = swapc; 1034 c->prompt_index = idx; 1035 c->flags |= CLIENT_STATUS; 1036 } 1037 break; 1038 case MODEKEYEDIT_ENTER: 1039 if (*c->prompt_buffer != '\0') 1040 status_prompt_add_history(c); 1041 if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0) 1042 status_prompt_clear(c); 1043 break; 1044 case MODEKEYEDIT_CANCEL: 1045 if (c->prompt_callbackfn(c->prompt_data, NULL) == 0) 1046 status_prompt_clear(c); 1047 break; 1048 case MODEKEY_OTHER: 1049 if (key < 32 || key == 127) 1050 break; 1051 c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 2); 1052 1053 if (c->prompt_index == size) { 1054 c->prompt_buffer[c->prompt_index++] = key; 1055 c->prompt_buffer[c->prompt_index] = '\0'; 1056 } else { 1057 memmove(c->prompt_buffer + c->prompt_index + 1, 1058 c->prompt_buffer + c->prompt_index, 1059 size + 1 - c->prompt_index); 1060 c->prompt_buffer[c->prompt_index++] = key; 1061 } 1062 1063 if (c->prompt_flags & PROMPT_SINGLE) { 1064 if (c->prompt_callbackfn( 1065 c->prompt_data, c->prompt_buffer) == 0) 1066 status_prompt_clear(c); 1067 } 1068 1069 c->flags |= CLIENT_STATUS; 1070 break; 1071 default: 1072 break; 1073 } 1074} 1075 1076/* Add line to the history. */ 1077void 1078status_prompt_add_history(struct client *c) 1079{ 1080 if (ARRAY_LENGTH(&c->prompt_hdata) > 0 && 1081 strcmp(ARRAY_LAST(&c->prompt_hdata), c->prompt_buffer) == 0) 1082 return; 1083 1084 if (ARRAY_LENGTH(&c->prompt_hdata) == PROMPT_HISTORY) { 1085 xfree(ARRAY_FIRST(&c->prompt_hdata)); 1086 ARRAY_REMOVE(&c->prompt_hdata, 0); 1087 } 1088 1089 ARRAY_ADD(&c->prompt_hdata, xstrdup(c->prompt_buffer)); 1090} 1091 1092/* Complete word. */ 1093char * 1094status_prompt_complete(const char *s) 1095{ 1096 const struct cmd_entry **cmdent; 1097 const struct set_option_entry *entry; 1098 ARRAY_DECL(, const char *) list; 1099 char *prefix, *s2; 1100 u_int i; 1101 size_t j; 1102 1103 if (*s == '\0') 1104 return (NULL); 1105 1106 /* First, build a list of all the possible matches. */ 1107 ARRAY_INIT(&list); 1108 for (cmdent = cmd_table; *cmdent != NULL; cmdent++) { 1109 if (strncmp((*cmdent)->name, s, strlen(s)) == 0) 1110 ARRAY_ADD(&list, (*cmdent)->name); 1111 } 1112 for (entry = set_option_table; entry->name != NULL; entry++) { 1113 if (strncmp(entry->name, s, strlen(s)) == 0) 1114 ARRAY_ADD(&list, entry->name); 1115 } 1116 for (entry = set_session_option_table; entry->name != NULL; entry++) { 1117 if (strncmp(entry->name, s, strlen(s)) == 0) 1118 ARRAY_ADD(&list, entry->name); 1119 } 1120 for (entry = set_window_option_table; entry->name != NULL; entry++) { 1121 if (strncmp(entry->name, s, strlen(s)) == 0) 1122 ARRAY_ADD(&list, entry->name); 1123 } 1124 1125 /* If none, bail now. */ 1126 if (ARRAY_LENGTH(&list) == 0) { 1127 ARRAY_FREE(&list); 1128 return (NULL); 1129 } 1130 1131 /* If an exact match, return it, with a trailing space. */ 1132 if (ARRAY_LENGTH(&list) == 1) { 1133 xasprintf(&s2, "%s ", ARRAY_FIRST(&list)); 1134 ARRAY_FREE(&list); 1135 return (s2); 1136 } 1137 1138 /* Now loop through the list and find the longest common prefix. */ 1139 prefix = xstrdup(ARRAY_FIRST(&list)); 1140 for (i = 1; i < ARRAY_LENGTH(&list); i++) { 1141 s = ARRAY_ITEM(&list, i); 1142 1143 j = strlen(s); 1144 if (j > strlen(prefix)) 1145 j = strlen(prefix); 1146 for (; j > 0; j--) { 1147 if (prefix[j - 1] != s[j - 1]) 1148 prefix[j - 1] = '\0'; 1149 } 1150 } 1151 1152 ARRAY_FREE(&list); 1153 return (prefix); 1154} 1155