status.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <sys/time.h>
  19. #include <errno.h>
  20. #include <limits.h>
  21. #include <stdarg.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include <unistd.h>
  26. #include "tmux.h"
  27. #include "tmate.h"
  28. char *status_redraw_get_left(struct client *, time_t, struct grid_cell *,
  29. size_t *);
  30. char *status_redraw_get_right(struct client *, time_t, struct grid_cell *,
  31. size_t *);
  32. char *status_print(struct client *, struct winlink *, time_t,
  33. struct grid_cell *);
  34. char *status_replace(struct client *, struct winlink *, const char *, time_t);
  35. void status_message_callback(int, short, void *);
  36. void status_timer_callback(int, short, void *);
  37. const char *status_prompt_up_history(u_int *);
  38. const char *status_prompt_down_history(u_int *);
  39. void status_prompt_add_history(const char *);
  40. const char **status_prompt_complete_list(u_int *, const char *);
  41. char *status_prompt_complete_prefix(const char **, u_int);
  42. char *status_prompt_complete(struct session *, const char *);
  43. char *status_prompt_find_history_file(void);
  44. /* Status prompt history. */
  45. #define PROMPT_HISTORY 100
  46. char **status_prompt_hlist;
  47. u_int status_prompt_hsize;
  48. /* Find the history file to load/save from/to. */
  49. char *
  50. status_prompt_find_history_file(void)
  51. {
  52. const char *home, *history_file;
  53. char *path;
  54. history_file = options_get_string(global_options, "history-file");
  55. if (*history_file == '\0')
  56. return (NULL);
  57. if (*history_file == '/')
  58. return (xstrdup(history_file));
  59. if (history_file[0] != '~' || history_file[1] != '/')
  60. return (NULL);
  61. if ((home = find_home()) == NULL)
  62. return (NULL);
  63. xasprintf(&path, "%s%s", home, history_file + 1);
  64. return (path);
  65. }
  66. /* Load status prompt history from file. */
  67. void
  68. status_prompt_load_history(void)
  69. {
  70. FILE *f;
  71. char *history_file, *line, *tmp;
  72. size_t length;
  73. if ((history_file = status_prompt_find_history_file()) == NULL)
  74. return;
  75. log_debug("loading history from %s", history_file);
  76. f = fopen(history_file, "r");
  77. if (f == NULL) {
  78. log_debug("%s: %s", history_file, strerror(errno));
  79. free(history_file);
  80. return;
  81. }
  82. free(history_file);
  83. for (;;) {
  84. if ((line = fgetln(f, &length)) == NULL)
  85. break;
  86. if (length > 0) {
  87. if (line[length - 1] == '\n') {
  88. line[length - 1] = '\0';
  89. status_prompt_add_history(line);
  90. } else {
  91. tmp = xmalloc(length + 1);
  92. memcpy(tmp, line, length);
  93. tmp[length] = '\0';
  94. status_prompt_add_history(tmp);
  95. free(tmp);
  96. }
  97. }
  98. }
  99. fclose(f);
  100. }
  101. /* Save status prompt history to file. */
  102. void
  103. status_prompt_save_history(void)
  104. {
  105. FILE *f;
  106. u_int i;
  107. char *history_file;
  108. if ((history_file = status_prompt_find_history_file()) == NULL)
  109. return;
  110. log_debug("saving history to %s", history_file);
  111. f = fopen(history_file, "w");
  112. if (f == NULL) {
  113. log_debug("%s: %s", history_file, strerror(errno));
  114. free(history_file);
  115. return;
  116. }
  117. free(history_file);
  118. for (i = 0; i < status_prompt_hsize; i++) {
  119. fputs(status_prompt_hlist[i], f);
  120. fputc('\n', f);
  121. }
  122. fclose(f);
  123. }
  124. /* Status timer callback. */
  125. void
  126. status_timer_callback(__unused int fd, __unused short events, void *arg)
  127. {
  128. struct client *c = arg;
  129. struct session *s = c->session;
  130. struct timeval tv;
  131. evtimer_del(&c->status_timer);
  132. if (s == NULL)
  133. return;
  134. if (c->message_string == NULL && c->prompt_string == NULL)
  135. c->flags |= CLIENT_STATUS;
  136. timerclear(&tv);
  137. tv.tv_sec = options_get_number(s->options, "status-interval");
  138. if (tv.tv_sec != 0)
  139. evtimer_add(&c->status_timer, &tv);
  140. log_debug("client %p, status interval %d", c, (int)tv.tv_sec);
  141. }
  142. /* Start status timer for client. */
  143. void
  144. status_timer_start(struct client *c)
  145. {
  146. struct session *s = c->session;
  147. if (event_initialized(&c->status_timer))
  148. evtimer_del(&c->status_timer);
  149. else
  150. evtimer_set(&c->status_timer, status_timer_callback, c);
  151. if (s != NULL && options_get_number(s->options, "status"))
  152. status_timer_callback(-1, 0, c);
  153. }
  154. /* Start status timer for all clients. */
  155. void
  156. status_timer_start_all(void)
  157. {
  158. struct client *c;
  159. TAILQ_FOREACH(c, &clients, entry)
  160. status_timer_start(c);
  161. }
  162. /* Get screen line of status line. -1 means off. */
  163. int
  164. status_at_line(struct client *c)
  165. {
  166. struct session *s = c->session;
  167. if (!options_get_number(s->options, "status"))
  168. return (-1);
  169. if (options_get_number(s->options, "status-position") == 0)
  170. return (0);
  171. return (c->tty.sy - 1);
  172. }
  173. /* Retrieve options for left string. */
  174. char *
  175. status_redraw_get_left(struct client *c, time_t t, struct grid_cell *gc,
  176. size_t *size)
  177. {
  178. struct session *s = c->session;
  179. const char *template;
  180. char *left;
  181. size_t leftlen;
  182. style_apply_update(gc, s->options, "status-left-style");
  183. template = options_get_string(s->options, "status-left");
  184. left = status_replace(c, NULL, template, t);
  185. *size = options_get_number(s->options, "status-left-length");
  186. leftlen = screen_write_cstrlen("%s", left);
  187. if (leftlen < *size)
  188. *size = leftlen;
  189. return (left);
  190. }
  191. /* Retrieve options for right string. */
  192. char *
  193. status_redraw_get_right(struct client *c, time_t t, struct grid_cell *gc,
  194. size_t *size)
  195. {
  196. struct session *s = c->session;
  197. const char *template;
  198. char *right;
  199. size_t rightlen;
  200. style_apply_update(gc, s->options, "status-right-style");
  201. template = options_get_string(s->options, "status-right");
  202. right = status_replace(c, NULL, template, t);
  203. *size = options_get_number(s->options, "status-right-length");
  204. rightlen = screen_write_cstrlen("%s", right);
  205. if (rightlen < *size)
  206. *size = rightlen;
  207. return (right);
  208. }
  209. /* Get window at window list position. */
  210. struct window *
  211. status_get_window_at(struct client *c, u_int x)
  212. {
  213. struct session *s = c->session;
  214. struct winlink *wl;
  215. struct options *oo;
  216. size_t len;
  217. x += c->wlmouse;
  218. RB_FOREACH(wl, winlinks, &s->windows) {
  219. oo = wl->window->options;
  220. len = strlen(options_get_string(oo, "window-status-separator"));
  221. if (x < wl->status_width)
  222. return (wl->window);
  223. x -= wl->status_width + len;
  224. }
  225. return (NULL);
  226. }
  227. /* Draw status for client on the last lines of given context. */
  228. int
  229. status_redraw(struct client *c)
  230. {
  231. struct screen_write_ctx ctx;
  232. struct session *s = c->session;
  233. struct winlink *wl;
  234. struct screen old_status, window_list;
  235. struct grid_cell stdgc, lgc, rgc, gc;
  236. struct options *oo;
  237. time_t t;
  238. char *left, *right, *sep;
  239. u_int offset, needed;
  240. u_int wlstart, wlwidth, wlavailable, wloffset, wlsize;
  241. size_t llen, rlen, seplen;
  242. int larrow, rarrow;
  243. /* No status line? */
  244. if (c->tty.sy == 0 || !options_get_number(s->options, "status"))
  245. #ifdef TMATE
  246. if (c->tty.sy == 0 || !(c->flags & CLIENT_FORCE_STATUS))
  247. #endif
  248. return (1);
  249. left = right = NULL;
  250. larrow = rarrow = 0;
  251. /* Store current time. */
  252. t = time(NULL);
  253. /* Set up default colour. */
  254. style_apply(&stdgc, s->options, "status-style");
  255. /* Create the target screen. */
  256. memcpy(&old_status, &c->status, sizeof old_status);
  257. screen_init(&c->status, c->tty.sx, 1, 0);
  258. screen_write_start(&ctx, NULL, &c->status);
  259. for (offset = 0; offset < c->tty.sx; offset++)
  260. screen_write_putc(&ctx, &stdgc, ' ');
  261. screen_write_stop(&ctx);
  262. /* If the height is one line, blank status line. */
  263. if (c->tty.sy <= 1)
  264. goto out;
  265. /* Work out left and right strings. */
  266. memcpy(&lgc, &stdgc, sizeof lgc);
  267. left = status_redraw_get_left(c, t, &lgc, &llen);
  268. memcpy(&rgc, &stdgc, sizeof rgc);
  269. right = status_redraw_get_right(c, t, &rgc, &rlen);
  270. #ifdef TMATE
  271. tmate_status(left, right);
  272. #endif
  273. /*
  274. * Figure out how much space we have for the window list. If there
  275. * isn't enough space, just show a blank status line.
  276. */
  277. needed = 0;
  278. if (llen != 0)
  279. needed += llen;
  280. if (rlen != 0)
  281. needed += rlen;
  282. if (c->tty.sx == 0 || c->tty.sx <= needed)
  283. goto out;
  284. wlavailable = c->tty.sx - needed;
  285. /* Calculate the total size needed for the window list. */
  286. wlstart = wloffset = wlwidth = 0;
  287. RB_FOREACH(wl, winlinks, &s->windows) {
  288. free(wl->status_text);
  289. memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell);
  290. wl->status_text = status_print(c, wl, t, &wl->status_cell);
  291. wl->status_width = screen_write_cstrlen("%s", wl->status_text);
  292. if (wl == s->curw)
  293. wloffset = wlwidth;
  294. oo = wl->window->options;
  295. sep = options_get_string(oo, "window-status-separator");
  296. seplen = screen_write_strlen("%s", sep);
  297. wlwidth += wl->status_width + seplen;
  298. }
  299. /* Create a new screen for the window list. */
  300. screen_init(&window_list, wlwidth, 1, 0);
  301. /* And draw the window list into it. */
  302. screen_write_start(&ctx, NULL, &window_list);
  303. RB_FOREACH(wl, winlinks, &s->windows) {
  304. screen_write_cnputs(&ctx, -1, &wl->status_cell, "%s",
  305. wl->status_text);
  306. oo = wl->window->options;
  307. sep = options_get_string(oo, "window-status-separator");
  308. screen_write_nputs(&ctx, -1, &stdgc, "%s", sep);
  309. }
  310. screen_write_stop(&ctx);
  311. /* If there is enough space for the total width, skip to draw now. */
  312. if (wlwidth <= wlavailable)
  313. goto draw;
  314. /* Find size of current window text. */
  315. wlsize = s->curw->status_width;
  316. /*
  317. * If the current window is already on screen, good to draw from the
  318. * start and just leave off the end.
  319. */
  320. if (wloffset + wlsize < wlavailable) {
  321. if (wlavailable > 0) {
  322. rarrow = 1;
  323. wlavailable--;
  324. }
  325. wlwidth = wlavailable;
  326. } else {
  327. /*
  328. * Work out how many characters we need to omit from the
  329. * start. There are wlavailable characters to fill, and
  330. * wloffset + wlsize must be the last. So, the start character
  331. * is wloffset + wlsize - wlavailable.
  332. */
  333. if (wlavailable > 0) {
  334. larrow = 1;
  335. wlavailable--;
  336. }
  337. wlstart = wloffset + wlsize - wlavailable;
  338. if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) {
  339. rarrow = 1;
  340. wlstart++;
  341. wlavailable--;
  342. }
  343. wlwidth = wlavailable;
  344. }
  345. /* Bail if anything is now too small too. */
  346. if (wlwidth == 0 || wlavailable == 0) {
  347. screen_free(&window_list);
  348. goto out;
  349. }
  350. /*
  351. * Now the start position is known, work out the state of the left and
  352. * right arrows.
  353. */
  354. offset = 0;
  355. RB_FOREACH(wl, winlinks, &s->windows) {
  356. if (wl->flags & WINLINK_ALERTFLAGS &&
  357. larrow == 1 && offset < wlstart)
  358. larrow = -1;
  359. offset += wl->status_width;
  360. if (wl->flags & WINLINK_ALERTFLAGS &&
  361. rarrow == 1 && offset > wlstart + wlwidth)
  362. rarrow = -1;
  363. }
  364. draw:
  365. /* Begin drawing. */
  366. screen_write_start(&ctx, NULL, &c->status);
  367. /* Draw the left string and arrow. */
  368. screen_write_cursormove(&ctx, 0, 0);
  369. if (llen != 0)
  370. screen_write_cnputs(&ctx, llen, &lgc, "%s", left);
  371. if (larrow != 0) {
  372. memcpy(&gc, &stdgc, sizeof gc);
  373. if (larrow == -1)
  374. gc.attr ^= GRID_ATTR_REVERSE;
  375. screen_write_putc(&ctx, &gc, '<');
  376. }
  377. /* Draw the right string and arrow. */
  378. if (rarrow != 0) {
  379. screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0);
  380. memcpy(&gc, &stdgc, sizeof gc);
  381. if (rarrow == -1)
  382. gc.attr ^= GRID_ATTR_REVERSE;
  383. screen_write_putc(&ctx, &gc, '>');
  384. } else
  385. screen_write_cursormove(&ctx, c->tty.sx - rlen, 0);
  386. if (rlen != 0)
  387. screen_write_cnputs(&ctx, rlen, &rgc, "%s", right);
  388. /* Figure out the offset for the window list. */
  389. if (llen != 0)
  390. wloffset = llen;
  391. else
  392. wloffset = 0;
  393. if (wlwidth < wlavailable) {
  394. switch (options_get_number(s->options, "status-justify")) {
  395. case 1: /* centred */
  396. wloffset += (wlavailable - wlwidth) / 2;
  397. break;
  398. case 2: /* right */
  399. wloffset += (wlavailable - wlwidth);
  400. break;
  401. }
  402. }
  403. if (larrow != 0)
  404. wloffset++;
  405. /* Copy the window list. */
  406. c->wlmouse = -wloffset + wlstart;
  407. screen_write_cursormove(&ctx, wloffset, 0);
  408. screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1);
  409. screen_free(&window_list);
  410. screen_write_stop(&ctx);
  411. out:
  412. free(left);
  413. free(right);
  414. if (grid_compare(c->status.grid, old_status.grid) == 0) {
  415. screen_free(&old_status);
  416. return (0);
  417. }
  418. screen_free(&old_status);
  419. return (1);
  420. }
  421. /* Replace special sequences in fmt. */
  422. char *
  423. status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t)
  424. {
  425. struct format_tree *ft;
  426. char *expanded;
  427. if (fmt == NULL)
  428. return (xstrdup(""));
  429. if (c->flags & CLIENT_STATUSFORCE)
  430. ft = format_create(NULL, FORMAT_STATUS|FORMAT_FORCE);
  431. else
  432. ft = format_create(NULL, FORMAT_STATUS);
  433. format_defaults(ft, c, NULL, wl, NULL);
  434. expanded = format_expand_time(ft, fmt, t);
  435. format_free(ft);
  436. return (expanded);
  437. }
  438. /* Return winlink status line entry and adjust gc as necessary. */
  439. char *
  440. status_print(struct client *c, struct winlink *wl, time_t t,
  441. struct grid_cell *gc)
  442. {
  443. struct options *oo = wl->window->options;
  444. struct session *s = c->session;
  445. const char *fmt;
  446. char *text;
  447. style_apply_update(gc, oo, "window-status-style");
  448. fmt = options_get_string(oo, "window-status-format");
  449. if (wl == s->curw) {
  450. style_apply_update(gc, oo, "window-status-current-style");
  451. fmt = options_get_string(oo, "window-status-current-format");
  452. }
  453. if (wl == TAILQ_FIRST(&s->lastw))
  454. style_apply_update(gc, oo, "window-status-last-style");
  455. if (wl->flags & WINLINK_BELL)
  456. style_apply_update(gc, oo, "window-status-bell-style");
  457. else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE))
  458. style_apply_update(gc, oo, "window-status-activity-style");
  459. text = status_replace(c, wl, fmt, t);
  460. return (text);
  461. }
  462. /* Set a status line message. */
  463. void
  464. status_message_set(struct client *c, const char *fmt, ...)
  465. {
  466. struct timeval tv;
  467. struct message_entry *msg, *msg1;
  468. va_list ap;
  469. int delay;
  470. u_int limit;
  471. limit = options_get_number(global_options, "message-limit");
  472. status_prompt_clear(c);
  473. status_message_clear(c);
  474. va_start(ap, fmt);
  475. xvasprintf(&c->message_string, fmt, ap);
  476. va_end(ap);
  477. msg = xcalloc(1, sizeof *msg);
  478. msg->msg_time = time(NULL);
  479. msg->msg_num = c->message_next++;
  480. msg->msg = xstrdup(c->message_string);
  481. TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
  482. TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
  483. if (msg->msg_num + limit >= c->message_next)
  484. break;
  485. free(msg->msg);
  486. TAILQ_REMOVE(&c->message_log, msg, entry);
  487. free(msg);
  488. }
  489. delay = options_get_number(c->session->options, "display-time");
  490. if (delay > 0) {
  491. tv.tv_sec = delay / 1000;
  492. tv.tv_usec = (delay % 1000) * 1000L;
  493. if (event_initialized(&c->message_timer))
  494. evtimer_del(&c->message_timer);
  495. evtimer_set(&c->message_timer, status_message_callback, c);
  496. evtimer_add(&c->message_timer, &tv);
  497. }
  498. c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
  499. c->flags |= CLIENT_STATUS;
  500. }
  501. /* Clear status line message. */
  502. void
  503. status_message_clear(struct client *c)
  504. {
  505. if (c->message_string == NULL)
  506. return;
  507. free(c->message_string);
  508. c->message_string = NULL;
  509. c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
  510. #ifdef TMATE
  511. if (c->flags & CLIENT_FORCE_STATUS) {
  512. c->flags &= ~CLIENT_FORCE_STATUS;
  513. recalculate_sizes();
  514. }
  515. #endif
  516. c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
  517. screen_reinit(&c->status);
  518. }
  519. /* Clear status line message after timer expires. */
  520. void
  521. status_message_callback(__unused int fd, __unused short event, void *data)
  522. {
  523. struct client *c = data;
  524. status_message_clear(c);
  525. }
  526. /* Draw client message on status line of present else on last line. */
  527. int
  528. status_message_redraw(struct client *c)
  529. {
  530. struct screen_write_ctx ctx;
  531. struct session *s = c->session;
  532. struct screen old_status;
  533. size_t len;
  534. struct grid_cell gc;
  535. if (c->tty.sx == 0 || c->tty.sy == 0)
  536. return (0);
  537. memcpy(&old_status, &c->status, sizeof old_status);
  538. screen_init(&c->status, c->tty.sx, 1, 0);
  539. len = screen_write_strlen("%s", c->message_string);
  540. if (len > c->tty.sx)
  541. len = c->tty.sx;
  542. style_apply(&gc, s->options, "message-style");
  543. screen_write_start(&ctx, NULL, &c->status);
  544. screen_write_cursormove(&ctx, 0, 0);
  545. screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
  546. for (; len < c->tty.sx; len++)
  547. screen_write_putc(&ctx, &gc, ' ');
  548. screen_write_stop(&ctx);
  549. if (grid_compare(c->status.grid, old_status.grid) == 0) {
  550. screen_free(&old_status);
  551. return (0);
  552. }
  553. screen_free(&old_status);
  554. return (1);
  555. }
  556. /* Enable status line prompt. */
  557. void
  558. status_prompt_set(struct client *c, const char *msg, const char *input,
  559. int (*callbackfn)(void *, const char *), void (*freefn)(void *),
  560. void *data, int flags)
  561. {
  562. struct format_tree *ft;
  563. int keys;
  564. time_t t;
  565. ft = format_create(NULL, 0);
  566. format_defaults(ft, c, NULL, NULL, NULL);
  567. t = time(NULL);
  568. status_message_clear(c);
  569. status_prompt_clear(c);
  570. c->prompt_string = format_expand_time(ft, msg, t);
  571. c->prompt_buffer = format_expand_time(ft, input, t);
  572. c->prompt_index = strlen(c->prompt_buffer);
  573. c->prompt_callbackfn = callbackfn;
  574. c->prompt_freefn = freefn;
  575. c->prompt_data = data;
  576. c->prompt_hindex = 0;
  577. c->prompt_flags = flags;
  578. keys = options_get_number(c->session->options, "status-keys");
  579. if (keys == MODEKEY_EMACS)
  580. mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit);
  581. else
  582. mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit);
  583. c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
  584. c->flags |= CLIENT_STATUS;
  585. format_free(ft);
  586. }
  587. /* Remove status line prompt. */
  588. void
  589. status_prompt_clear(struct client *c)
  590. {
  591. if (c->prompt_string == NULL)
  592. return;
  593. if (c->prompt_freefn != NULL && c->prompt_data != NULL)
  594. c->prompt_freefn(c->prompt_data);
  595. free(c->prompt_string);
  596. c->prompt_string = NULL;
  597. free(c->prompt_buffer);
  598. c->prompt_buffer = NULL;
  599. c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
  600. c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
  601. screen_reinit(&c->status);
  602. }
  603. /* Update status line prompt with a new prompt string. */
  604. void
  605. status_prompt_update(struct client *c, const char *msg, const char *input)
  606. {
  607. struct format_tree *ft;
  608. time_t t;
  609. ft = format_create(NULL, 0);
  610. format_defaults(ft, c, NULL, NULL, NULL);
  611. t = time(NULL);
  612. free(c->prompt_string);
  613. c->prompt_string = format_expand_time(ft, msg, t);
  614. free(c->prompt_buffer);
  615. c->prompt_buffer = format_expand_time(ft, input, t);
  616. c->prompt_index = strlen(c->prompt_buffer);
  617. c->prompt_hindex = 0;
  618. c->flags |= CLIENT_STATUS;
  619. format_free(ft);
  620. }
  621. /* Draw client prompt on status line of present else on last line. */
  622. int
  623. status_prompt_redraw(struct client *c)
  624. {
  625. struct screen_write_ctx ctx;
  626. struct session *s = c->session;
  627. struct screen old_status;
  628. size_t i, size, left, len, off;
  629. struct grid_cell gc;
  630. if (c->tty.sx == 0 || c->tty.sy == 0)
  631. return (0);
  632. memcpy(&old_status, &c->status, sizeof old_status);
  633. screen_init(&c->status, c->tty.sx, 1, 0);
  634. len = screen_write_strlen("%s", c->prompt_string);
  635. if (len > c->tty.sx)
  636. len = c->tty.sx;
  637. off = 0;
  638. /* Change colours for command mode. */
  639. if (c->prompt_mdata.mode == 1)
  640. style_apply(&gc, s->options, "message-command-style");
  641. else
  642. style_apply(&gc, s->options, "message-style");
  643. screen_write_start(&ctx, NULL, &c->status);
  644. screen_write_cursormove(&ctx, 0, 0);
  645. screen_write_nputs(&ctx, len, &gc, "%s", c->prompt_string);
  646. left = c->tty.sx - len;
  647. if (left != 0) {
  648. size = screen_write_strlen("%s", c->prompt_buffer);
  649. if (c->prompt_index >= left) {
  650. off = c->prompt_index - left + 1;
  651. if (c->prompt_index == size)
  652. left--;
  653. size = left;
  654. }
  655. screen_write_nputs(&ctx, left, &gc, "%s", c->prompt_buffer +
  656. off);
  657. for (i = len + size; i < c->tty.sx; i++)
  658. screen_write_putc(&ctx, &gc, ' ');
  659. }
  660. screen_write_stop(&ctx);
  661. /* Apply fake cursor. */
  662. off = len + c->prompt_index - off;
  663. grid_view_get_cell(c->status.grid, off, 0, &gc);
  664. gc.attr ^= GRID_ATTR_REVERSE;
  665. grid_view_set_cell(c->status.grid, off, 0, &gc);
  666. if (grid_compare(c->status.grid, old_status.grid) == 0) {
  667. screen_free(&old_status);
  668. return (0);
  669. }
  670. screen_free(&old_status);
  671. return (1);
  672. }
  673. /* Handle keys in prompt. */
  674. void
  675. status_prompt_key(struct client *c, key_code key)
  676. {
  677. struct session *sess = c->session;
  678. struct options *oo = sess->options;
  679. struct paste_buffer *pb;
  680. char *s, *first, *last, word[64], swapc;
  681. const char *histstr, *bufdata, *wsep = NULL;
  682. u_char ch;
  683. size_t size, n, off, idx, bufsize;
  684. size = strlen(c->prompt_buffer);
  685. switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) {
  686. case MODEKEYEDIT_CURSORLEFT:
  687. if (c->prompt_index > 0) {
  688. c->prompt_index--;
  689. c->flags |= CLIENT_STATUS;
  690. }
  691. break;
  692. case MODEKEYEDIT_SWITCHMODE:
  693. c->flags |= CLIENT_STATUS;
  694. break;
  695. case MODEKEYEDIT_SWITCHMODEAPPEND:
  696. c->flags |= CLIENT_STATUS;
  697. /* FALLTHROUGH */
  698. case MODEKEYEDIT_CURSORRIGHT:
  699. if (c->prompt_index < size) {
  700. c->prompt_index++;
  701. c->flags |= CLIENT_STATUS;
  702. }
  703. break;
  704. case MODEKEYEDIT_SWITCHMODEBEGINLINE:
  705. c->flags |= CLIENT_STATUS;
  706. /* FALLTHROUGH */
  707. case MODEKEYEDIT_STARTOFLINE:
  708. if (c->prompt_index != 0) {
  709. c->prompt_index = 0;
  710. c->flags |= CLIENT_STATUS;
  711. }
  712. break;
  713. case MODEKEYEDIT_SWITCHMODEAPPENDLINE:
  714. c->flags |= CLIENT_STATUS;
  715. /* FALLTHROUGH */
  716. case MODEKEYEDIT_ENDOFLINE:
  717. if (c->prompt_index != size) {
  718. c->prompt_index = size;
  719. c->flags |= CLIENT_STATUS;
  720. }
  721. break;
  722. case MODEKEYEDIT_COMPLETE:
  723. if (*c->prompt_buffer == '\0')
  724. break;
  725. idx = c->prompt_index;
  726. if (idx != 0)
  727. idx--;
  728. /* Find the word we are in. */
  729. first = c->prompt_buffer + idx;
  730. while (first > c->prompt_buffer && *first != ' ')
  731. first--;
  732. while (*first == ' ')
  733. first++;
  734. last = c->prompt_buffer + idx;
  735. while (*last != '\0' && *last != ' ')
  736. last++;
  737. while (*last == ' ')
  738. last--;
  739. if (*last != '\0')
  740. last++;
  741. if (last <= first ||
  742. ((size_t) (last - first)) > (sizeof word) - 1)
  743. break;
  744. memcpy(word, first, last - first);
  745. word[last - first] = '\0';
  746. /* And try to complete it. */
  747. if ((s = status_prompt_complete(sess, word)) == NULL)
  748. break;
  749. /* Trim out word. */
  750. n = size - (last - c->prompt_buffer) + 1; /* with \0 */
  751. memmove(first, last, n);
  752. size -= last - first;
  753. /* Insert the new word. */
  754. size += strlen(s);
  755. off = first - c->prompt_buffer;
  756. c->prompt_buffer = xrealloc(c->prompt_buffer, size + 1);
  757. first = c->prompt_buffer + off;
  758. memmove(first + strlen(s), first, n);
  759. memcpy(first, s, strlen(s));
  760. c->prompt_index = (first - c->prompt_buffer) + strlen(s);
  761. free(s);
  762. c->flags |= CLIENT_STATUS;
  763. break;
  764. case MODEKEYEDIT_BACKSPACE:
  765. if (c->prompt_index != 0) {
  766. if (c->prompt_index == size)
  767. c->prompt_buffer[--c->prompt_index] = '\0';
  768. else {
  769. memmove(c->prompt_buffer + c->prompt_index - 1,
  770. c->prompt_buffer + c->prompt_index,
  771. size + 1 - c->prompt_index);
  772. c->prompt_index--;
  773. }
  774. c->flags |= CLIENT_STATUS;
  775. }
  776. break;
  777. case MODEKEYEDIT_DELETE:
  778. case MODEKEYEDIT_SWITCHMODESUBSTITUTE:
  779. if (c->prompt_index != size) {
  780. memmove(c->prompt_buffer + c->prompt_index,
  781. c->prompt_buffer + c->prompt_index + 1,
  782. size + 1 - c->prompt_index);
  783. c->flags |= CLIENT_STATUS;
  784. }
  785. break;
  786. case MODEKEYEDIT_DELETELINE:
  787. case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE:
  788. *c->prompt_buffer = '\0';
  789. c->prompt_index = 0;
  790. c->flags |= CLIENT_STATUS;
  791. break;
  792. case MODEKEYEDIT_DELETETOENDOFLINE:
  793. case MODEKEYEDIT_SWITCHMODECHANGELINE:
  794. if (c->prompt_index < size) {
  795. c->prompt_buffer[c->prompt_index] = '\0';
  796. c->flags |= CLIENT_STATUS;
  797. }
  798. break;
  799. case MODEKEYEDIT_DELETEWORD:
  800. wsep = options_get_string(oo, "word-separators");
  801. idx = c->prompt_index;
  802. /* Find a non-separator. */
  803. while (idx != 0) {
  804. idx--;
  805. if (!strchr(wsep, c->prompt_buffer[idx]))
  806. break;
  807. }
  808. /* Find the separator at the beginning of the word. */
  809. while (idx != 0) {
  810. idx--;
  811. if (strchr(wsep, c->prompt_buffer[idx])) {
  812. /* Go back to the word. */
  813. idx++;
  814. break;
  815. }
  816. }
  817. memmove(c->prompt_buffer + idx,
  818. c->prompt_buffer + c->prompt_index,
  819. size + 1 - c->prompt_index);
  820. memset(c->prompt_buffer + size - (c->prompt_index - idx),
  821. '\0', c->prompt_index - idx);
  822. c->prompt_index = idx;
  823. c->flags |= CLIENT_STATUS;
  824. break;
  825. case MODEKEYEDIT_NEXTSPACE:
  826. wsep = " ";
  827. /* FALLTHROUGH */
  828. case MODEKEYEDIT_NEXTWORD:
  829. if (wsep == NULL)
  830. wsep = options_get_string(oo, "word-separators");
  831. /* Find a separator. */
  832. while (c->prompt_index != size) {
  833. c->prompt_index++;
  834. if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
  835. break;
  836. }
  837. /* Find the word right after the separation. */
  838. while (c->prompt_index != size) {
  839. c->prompt_index++;
  840. if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
  841. break;
  842. }
  843. c->flags |= CLIENT_STATUS;
  844. break;
  845. case MODEKEYEDIT_NEXTSPACEEND:
  846. wsep = " ";
  847. /* FALLTHROUGH */
  848. case MODEKEYEDIT_NEXTWORDEND:
  849. if (wsep == NULL)
  850. wsep = options_get_string(oo, "word-separators");
  851. /* Find a word. */
  852. while (c->prompt_index != size) {
  853. c->prompt_index++;
  854. if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
  855. break;
  856. }
  857. /* Find the separator at the end of the word. */
  858. while (c->prompt_index != size) {
  859. c->prompt_index++;
  860. if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
  861. break;
  862. }
  863. /* Back up to the end-of-word like vi. */
  864. if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
  865. c->prompt_index != 0)
  866. c->prompt_index--;
  867. c->flags |= CLIENT_STATUS;
  868. break;
  869. case MODEKEYEDIT_PREVIOUSSPACE:
  870. wsep = " ";
  871. /* FALLTHROUGH */
  872. case MODEKEYEDIT_PREVIOUSWORD:
  873. if (wsep == NULL)
  874. wsep = options_get_string(oo, "word-separators");
  875. /* Find a non-separator. */
  876. while (c->prompt_index != 0) {
  877. c->prompt_index--;
  878. if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
  879. break;
  880. }
  881. /* Find the separator at the beginning of the word. */
  882. while (c->prompt_index != 0) {
  883. c->prompt_index--;
  884. if (strchr(wsep, c->prompt_buffer[c->prompt_index])) {
  885. /* Go back to the word. */
  886. c->prompt_index++;
  887. break;
  888. }
  889. }
  890. c->flags |= CLIENT_STATUS;
  891. break;
  892. case MODEKEYEDIT_HISTORYUP:
  893. histstr = status_prompt_up_history(&c->prompt_hindex);
  894. if (histstr == NULL)
  895. break;
  896. free(c->prompt_buffer);
  897. c->prompt_buffer = xstrdup(histstr);
  898. c->prompt_index = strlen(c->prompt_buffer);
  899. c->flags |= CLIENT_STATUS;
  900. break;
  901. case MODEKEYEDIT_HISTORYDOWN:
  902. histstr = status_prompt_down_history(&c->prompt_hindex);
  903. if (histstr == NULL)
  904. break;
  905. free(c->prompt_buffer);
  906. c->prompt_buffer = xstrdup(histstr);
  907. c->prompt_index = strlen(c->prompt_buffer);
  908. c->flags |= CLIENT_STATUS;
  909. break;
  910. case MODEKEYEDIT_PASTE:
  911. if ((pb = paste_get_top(NULL)) == NULL)
  912. break;
  913. bufdata = paste_buffer_data(pb, &bufsize);
  914. for (n = 0; n < bufsize; n++) {
  915. ch = (u_char)bufdata[n];
  916. if (ch < 32 || ch == 127)
  917. break;
  918. }
  919. c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1);
  920. if (c->prompt_index == size) {
  921. memcpy(c->prompt_buffer + c->prompt_index, bufdata, n);
  922. c->prompt_index += n;
  923. c->prompt_buffer[c->prompt_index] = '\0';
  924. } else {
  925. memmove(c->prompt_buffer + c->prompt_index + n,
  926. c->prompt_buffer + c->prompt_index,
  927. size + 1 - c->prompt_index);
  928. memcpy(c->prompt_buffer + c->prompt_index, bufdata, n);
  929. c->prompt_index += n;
  930. }
  931. c->flags |= CLIENT_STATUS;
  932. break;
  933. case MODEKEYEDIT_TRANSPOSECHARS:
  934. idx = c->prompt_index;
  935. if (idx < size)
  936. idx++;
  937. if (idx >= 2) {
  938. swapc = c->prompt_buffer[idx - 2];
  939. c->prompt_buffer[idx - 2] = c->prompt_buffer[idx - 1];
  940. c->prompt_buffer[idx - 1] = swapc;
  941. c->prompt_index = idx;
  942. c->flags |= CLIENT_STATUS;
  943. }
  944. break;
  945. case MODEKEYEDIT_ENTER:
  946. if (*c->prompt_buffer != '\0')
  947. status_prompt_add_history(c->prompt_buffer);
  948. if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0)
  949. status_prompt_clear(c);
  950. break;
  951. case MODEKEYEDIT_CANCEL:
  952. if (c->prompt_callbackfn(c->prompt_data, NULL) == 0)
  953. status_prompt_clear(c);
  954. break;
  955. case MODEKEY_OTHER:
  956. if (key <= 0x1f || key >= 0x7f)
  957. break;
  958. c->prompt_buffer = xrealloc(c->prompt_buffer, size + 2);
  959. if (c->prompt_index == size) {
  960. c->prompt_buffer[c->prompt_index++] = key;
  961. c->prompt_buffer[c->prompt_index] = '\0';
  962. } else {
  963. memmove(c->prompt_buffer + c->prompt_index + 1,
  964. c->prompt_buffer + c->prompt_index,
  965. size + 1 - c->prompt_index);
  966. c->prompt_buffer[c->prompt_index++] = key;
  967. }
  968. if (c->prompt_flags & PROMPT_SINGLE) {
  969. if (c->prompt_callbackfn(c->prompt_data,
  970. c->prompt_buffer) == 0)
  971. status_prompt_clear(c);
  972. }
  973. c->flags |= CLIENT_STATUS;
  974. break;
  975. default:
  976. break;
  977. }
  978. }
  979. /* Get previous line from the history. */
  980. const char *
  981. status_prompt_up_history(u_int *idx)
  982. {
  983. /*
  984. * History runs from 0 to size - 1. Index is from 0 to size. Zero is
  985. * empty.
  986. */
  987. if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
  988. return (NULL);
  989. (*idx)++;
  990. return (status_prompt_hlist[status_prompt_hsize - *idx]);
  991. }
  992. /* Get next line from the history. */
  993. const char *
  994. status_prompt_down_history(u_int *idx)
  995. {
  996. if (status_prompt_hsize == 0 || *idx == 0)
  997. return ("");
  998. (*idx)--;
  999. if (*idx == 0)
  1000. return ("");
  1001. return (status_prompt_hlist[status_prompt_hsize - *idx]);
  1002. }
  1003. /* Add line to the history. */
  1004. void
  1005. status_prompt_add_history(const char *line)
  1006. {
  1007. size_t size;
  1008. if (status_prompt_hsize > 0 &&
  1009. strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
  1010. return;
  1011. if (status_prompt_hsize == PROMPT_HISTORY) {
  1012. free(status_prompt_hlist[0]);
  1013. size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
  1014. memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
  1015. status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
  1016. return;
  1017. }
  1018. status_prompt_hlist = xreallocarray(status_prompt_hlist,
  1019. status_prompt_hsize + 1, sizeof *status_prompt_hlist);
  1020. status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
  1021. }
  1022. /* Build completion list. */
  1023. const char **
  1024. status_prompt_complete_list(u_int *size, const char *s)
  1025. {
  1026. const char **list = NULL, **layout;
  1027. const struct cmd_entry **cmdent;
  1028. const struct options_table_entry *oe;
  1029. const char *layouts[] = {
  1030. "even-horizontal", "even-vertical", "main-horizontal",
  1031. "main-vertical", "tiled", NULL
  1032. };
  1033. *size = 0;
  1034. for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
  1035. if (strncmp((*cmdent)->name, s, strlen(s)) == 0) {
  1036. list = xreallocarray(list, (*size) + 1, sizeof *list);
  1037. list[(*size)++] = (*cmdent)->name;
  1038. }
  1039. }
  1040. for (oe = options_table; oe->name != NULL; oe++) {
  1041. if (strncmp(oe->name, s, strlen(s)) == 0) {
  1042. list = xreallocarray(list, (*size) + 1, sizeof *list);
  1043. list[(*size)++] = oe->name;
  1044. }
  1045. }
  1046. for (layout = layouts; *layout != NULL; layout++) {
  1047. if (strncmp(*layout, s, strlen(s)) == 0) {
  1048. list = xreallocarray(list, (*size) + 1, sizeof *list);
  1049. list[(*size)++] = *layout;
  1050. }
  1051. }
  1052. return (list);
  1053. }
  1054. /* Find longest prefix. */
  1055. char *
  1056. status_prompt_complete_prefix(const char **list, u_int size)
  1057. {
  1058. char *out;
  1059. u_int i;
  1060. size_t j;
  1061. out = xstrdup(list[0]);
  1062. for (i = 1; i < size; i++) {
  1063. j = strlen(list[i]);
  1064. if (j > strlen(out))
  1065. j = strlen(out);
  1066. for (; j > 0; j--) {
  1067. if (out[j - 1] != list[i][j - 1])
  1068. out[j - 1] = '\0';
  1069. }
  1070. }
  1071. return (out);
  1072. }
  1073. /* Complete word. */
  1074. char *
  1075. status_prompt_complete(struct session *sess, const char *s)
  1076. {
  1077. const char **list = NULL, *colon;
  1078. u_int size = 0, i;
  1079. struct session *s_loop;
  1080. struct winlink *wl;
  1081. struct window *w;
  1082. char *copy, *out, *tmp;
  1083. if (*s == '\0')
  1084. return (NULL);
  1085. out = NULL;
  1086. if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) {
  1087. list = status_prompt_complete_list(&size, s);
  1088. if (size == 0)
  1089. out = NULL;
  1090. else if (size == 1)
  1091. xasprintf(&out, "%s ", list[0]);
  1092. else
  1093. out = status_prompt_complete_prefix(list, size);
  1094. free(list);
  1095. return (out);
  1096. }
  1097. copy = xstrdup(s);
  1098. colon = ":";
  1099. if (copy[strlen(copy) - 1] == ':')
  1100. copy[strlen(copy) - 1] = '\0';
  1101. else
  1102. colon = "";
  1103. s = copy + 2;
  1104. RB_FOREACH(s_loop, sessions, &sessions) {
  1105. if (strncmp(s_loop->name, s, strlen(s)) == 0) {
  1106. list = xreallocarray(list, size + 2, sizeof *list);
  1107. list[size++] = s_loop->name;
  1108. }
  1109. }
  1110. if (size == 1) {
  1111. out = xstrdup(list[0]);
  1112. if (session_find(list[0]) != NULL)
  1113. colon = ":";
  1114. } else if (size != 0)
  1115. out = status_prompt_complete_prefix(list, size);
  1116. if (out != NULL) {
  1117. xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
  1118. out = tmp;
  1119. goto found;
  1120. }
  1121. colon = "";
  1122. if (*s == ':') {
  1123. RB_FOREACH(wl, winlinks, &sess->windows) {
  1124. xasprintf(&tmp, ":%s", wl->window->name);
  1125. if (strncmp(tmp, s, strlen(s)) == 0){
  1126. list = xreallocarray(list, size + 1,
  1127. sizeof *list);
  1128. list[size++] = tmp;
  1129. continue;
  1130. }
  1131. free(tmp);
  1132. xasprintf(&tmp, ":%d", wl->idx);
  1133. if (strncmp(tmp, s, strlen(s)) == 0) {
  1134. list = xreallocarray(list, size + 1,
  1135. sizeof *list);
  1136. list[size++] = tmp;
  1137. continue;
  1138. }
  1139. free(tmp);
  1140. }
  1141. } else {
  1142. RB_FOREACH(s_loop, sessions, &sessions) {
  1143. RB_FOREACH(wl, winlinks, &s_loop->windows) {
  1144. w = wl->window;
  1145. xasprintf(&tmp, "%s:%s", s_loop->name, w->name);
  1146. if (strncmp(tmp, s, strlen(s)) == 0) {
  1147. list = xreallocarray(list, size + 1,
  1148. sizeof *list);
  1149. list[size++] = tmp;
  1150. continue;
  1151. }
  1152. free(tmp);
  1153. xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx);
  1154. if (strncmp(tmp, s, strlen(s)) == 0) {
  1155. list = xreallocarray(list, size + 1,
  1156. sizeof *list);
  1157. list[size++] = tmp;
  1158. continue;
  1159. }
  1160. free(tmp);
  1161. }
  1162. }
  1163. }
  1164. if (size == 1) {
  1165. out = xstrdup(list[0]);
  1166. colon = " ";
  1167. } else if (size != 0)
  1168. out = status_prompt_complete_prefix(list, size);
  1169. if (out != NULL) {
  1170. xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
  1171. out = tmp;
  1172. }
  1173. for (i = 0; i < size; i++)
  1174. free((void *)list[i]);
  1175. found:
  1176. free(copy);
  1177. free(list);
  1178. return (out);
  1179. }