Annotation of src/usr.bin/tmux/format.c, Revision 1.64
1.64 ! nicm 1: /* $OpenBSD: format.c,v 1.63 2015/04/21 22:38:49 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2011 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>
1.55 nicm 20: #include <sys/wait.h>
1.1 nicm 21:
1.30 nicm 22: #include <ctype.h>
23: #include <errno.h>
1.1 nicm 24: #include <netdb.h>
25: #include <stdarg.h>
1.9 nicm 26: #include <stdlib.h>
1.1 nicm 27: #include <string.h>
28: #include <time.h>
29: #include <unistd.h>
30:
31: #include "tmux.h"
32:
33: /*
34: * Build a list of key-value pairs and use them to expand #{key} entries in a
35: * string.
36: */
37:
1.32 nicm 38: int format_replace(struct format_tree *, const char *, size_t, char **,
39: size_t *, size_t *);
1.60 nicm 40: char *format_time_string(time_t);
1.32 nicm 41: char *format_get_command(struct window_pane *);
1.57 nicm 42:
43: void format_defaults_pane_tabs(struct format_tree *, struct window_pane *);
44: void format_defaults_session(struct format_tree *, struct session *);
45: void format_defaults_client(struct format_tree *, struct client *);
46: void format_defaults_winlink(struct format_tree *, struct session *,
47: struct winlink *);
1.1 nicm 48:
1.54 nicm 49: /* Entry in format tree. */
50: struct format_entry {
51: char *key;
52: char *value;
53:
54: RB_ENTRY(format_entry) entry;
55: };
56:
57: /* Tree of format entries. */
58: struct format_tree {
59: struct window *w;
60: struct session *s;
61:
62: RB_HEAD(format_rb_tree, format_entry) tree;
63: };
64:
1.1 nicm 65: /* Format key-value replacement entry. */
1.54 nicm 66: int format_cmp(struct format_entry *, struct format_entry *);
67: RB_PROTOTYPE(format_rb_tree, format_entry, entry, format_cmp);
68: RB_GENERATE(format_rb_tree, format_entry, entry, format_cmp);
1.1 nicm 69:
70: /* Format tree comparison function. */
71: int
72: format_cmp(struct format_entry *fe1, struct format_entry *fe2)
73: {
74: return (strcmp(fe1->key, fe2->key));
75: }
76:
1.25 nicm 77: /* Single-character uppercase aliases. */
78: const char *format_upper[] = {
1.1 nicm 79: NULL, /* A */
80: NULL, /* B */
81: NULL, /* C */
82: "pane_id", /* D */
83: NULL, /* E */
84: "window_flags", /* F */
85: NULL, /* G */
86: "host", /* H */
87: "window_index", /* I */
88: NULL, /* J */
89: NULL, /* K */
90: NULL, /* L */
91: NULL, /* M */
92: NULL, /* N */
93: NULL, /* O */
94: "pane_index", /* P */
95: NULL, /* Q */
96: NULL, /* R */
97: "session_name", /* S */
98: "pane_title", /* T */
99: NULL, /* U */
100: NULL, /* V */
101: "window_name", /* W */
102: NULL, /* X */
103: NULL, /* Y */
104: NULL /* Z */
105: };
106:
1.25 nicm 107: /* Single-character lowercase aliases. */
108: const char *format_lower[] = {
109: NULL, /* a */
110: NULL, /* b */
111: NULL, /* c */
112: NULL, /* d */
113: NULL, /* e */
114: NULL, /* f */
115: NULL, /* g */
116: "host_short", /* h */
117: NULL, /* i */
118: NULL, /* j */
119: NULL, /* k */
120: NULL, /* l */
121: NULL, /* m */
122: NULL, /* n */
123: NULL, /* o */
124: NULL, /* p */
125: NULL, /* q */
126: NULL, /* r */
127: NULL, /* s */
128: NULL, /* t */
129: NULL, /* u */
130: NULL, /* v */
131: NULL, /* w */
132: NULL, /* x */
133: NULL, /* y */
134: NULL /* z */
135: };
136:
1.1 nicm 137: /* Create a new tree. */
138: struct format_tree *
139: format_create(void)
140: {
141: struct format_tree *ft;
1.56 deraadt 142: char host[HOST_NAME_MAX+1], *ptr;
1.1 nicm 143:
1.54 nicm 144: ft = xcalloc(1, sizeof *ft);
145: RB_INIT(&ft->tree);
1.1 nicm 146:
1.25 nicm 147: if (gethostname(host, sizeof host) == 0) {
1.1 nicm 148: format_add(ft, "host", "%s", host);
1.33 nicm 149: if ((ptr = strchr(host, '.')) != NULL)
1.25 nicm 150: *ptr = '\0';
151: format_add(ft, "host_short", "%s", host);
152: }
1.1 nicm 153:
154: return (ft);
155: }
156:
157: /* Free a tree. */
158: void
159: format_free(struct format_tree *ft)
160: {
1.54 nicm 161: struct format_entry *fe, *fe1;
1.1 nicm 162:
1.54 nicm 163: RB_FOREACH_SAFE(fe, format_rb_tree, &ft->tree, fe1) {
164: RB_REMOVE(format_rb_tree, &ft->tree, fe);
1.9 nicm 165: free(fe->value);
166: free(fe->key);
167: free(fe);
1.1 nicm 168: }
169:
1.25 nicm 170: free(ft);
1.1 nicm 171: }
172:
173: /* Add a key-value pair. */
174: void
175: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
176: {
177: struct format_entry *fe;
1.28 nicm 178: struct format_entry *fe_now;
1.1 nicm 179: va_list ap;
180:
181: fe = xmalloc(sizeof *fe);
182: fe->key = xstrdup(key);
183:
184: va_start(ap, fmt);
185: xvasprintf(&fe->value, fmt, ap);
186: va_end(ap);
187:
1.54 nicm 188: fe_now = RB_INSERT(format_rb_tree, &ft->tree, fe);
1.28 nicm 189: if (fe_now != NULL) {
190: free(fe_now->value);
191: fe_now->value = fe->value;
192: free(fe->key);
193: free(fe);
194: }
1.1 nicm 195: }
196:
197: /* Find a format entry. */
198: const char *
199: format_find(struct format_tree *ft, const char *key)
200: {
201: struct format_entry *fe, fe_find;
1.54 nicm 202: struct options_entry *o;
203: static char s[16];
204:
205: o = options_find(&global_options, key);
206: if (o == NULL && ft->w != NULL)
207: o = options_find(&ft->w->options, key);
208: if (o == NULL)
209: o = options_find(&global_w_options, key);
210: if (o == NULL && ft->s != NULL)
211: o = options_find(&ft->s->options, key);
212: if (o == NULL)
213: o = options_find(&global_s_options, key);
214: if (o != NULL) {
215: switch (o->type) {
216: case OPTIONS_STRING:
217: return (o->str);
218: case OPTIONS_NUMBER:
219: snprintf(s, sizeof s, "%lld", o->num);
220: return (s);
221: case OPTIONS_STYLE:
222: return (style_tostring(&o->style));
223: }
224: }
1.1 nicm 225:
226: fe_find.key = (char *) key;
1.54 nicm 227: fe = RB_FIND(format_rb_tree, &ft->tree, &fe_find);
1.1 nicm 228: if (fe == NULL)
229: return (NULL);
230: return (fe->value);
231: }
232:
233: /*
234: * Replace a key/value pair in buffer. #{blah} is expanded directly,
235: * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
236: */
237: int
1.30 nicm 238: format_replace(struct format_tree *ft, const char *key, size_t keylen,
239: char **buf, size_t *len, size_t *off)
1.1 nicm 240: {
1.44 nicm 241: char *copy, *copy0, *endptr, *ptr, *saved, *trimmed;
1.1 nicm 242: const char *value;
243: size_t valuelen;
1.44 nicm 244: u_long limit = 0;
1.1 nicm 245:
246: /* Make a copy of the key. */
1.30 nicm 247: copy0 = copy = xmalloc(keylen + 1);
1.1 nicm 248: memcpy(copy, key, keylen);
249: copy[keylen] = '\0';
250:
1.30 nicm 251: /* Is there a length limit or whatnot? */
1.54 nicm 252: if (!islower((u_char) *copy) && *copy != '@' && *copy != '?') {
1.30 nicm 253: while (*copy != ':' && *copy != '\0') {
254: switch (*copy) {
255: case '=':
256: errno = 0;
257: limit = strtoul(copy + 1, &endptr, 10);
258: if (errno == ERANGE && limit == ULONG_MAX)
259: goto fail;
260: copy = endptr;
261: break;
262: default:
263: copy++;
264: break;
265: }
266: }
267: if (*copy != ':')
268: goto fail;
269: copy++;
270: }
271:
1.1 nicm 272: /*
273: * Is this a conditional? If so, check it exists and extract either the
274: * first or second element. If not, look up the key directly.
275: */
276: if (*copy == '?') {
277: ptr = strchr(copy, ',');
278: if (ptr == NULL)
279: goto fail;
280: *ptr = '\0';
281:
282: value = format_find(ft, copy + 1);
1.52 nicm 283: if (value != NULL && *value != '\0' &&
284: (value[0] != '0' || value[1] != '\0')) {
1.1 nicm 285: value = ptr + 1;
286: ptr = strchr(value, ',');
287: if (ptr == NULL)
288: goto fail;
289: *ptr = '\0';
290: } else {
291: ptr = strchr(ptr + 1, ',');
292: if (ptr == NULL)
293: goto fail;
294: value = ptr + 1;
295: }
1.31 nicm 296: saved = format_expand(ft, value);
297: value = saved;
1.1 nicm 298: } else {
299: value = format_find(ft, copy);
300: if (value == NULL)
301: value = "";
1.31 nicm 302: saved = NULL;
1.1 nicm 303: }
304:
1.30 nicm 305: /* Truncate the value if needed. */
1.44 nicm 306: if (limit != 0) {
307: value = trimmed = utf8_trimcstr(value, limit);
308: free(saved);
309: saved = trimmed;
310: }
311: valuelen = strlen(value);
1.30 nicm 312:
1.1 nicm 313: /* Expand the buffer and copy in the value. */
314: while (*len - *off < valuelen + 1) {
1.50 nicm 315: *buf = xreallocarray(*buf, 2, *len);
1.1 nicm 316: *len *= 2;
317: }
318: memcpy(*buf + *off, value, valuelen);
319: *off += valuelen;
320:
1.31 nicm 321: free(saved);
1.30 nicm 322: free(copy0);
1.1 nicm 323: return (0);
324:
325: fail:
1.30 nicm 326: free(copy0);
1.1 nicm 327: return (-1);
328: }
329:
1.58 nicm 330: /* Expand keys in a template, passing through strftime first. */
331: char *
332: format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
333: {
334: char *tmp, *expanded;
335: size_t tmplen;
336: struct tm *tm;
337:
338: if (fmt == NULL)
339: return (xstrdup(""));
340:
341: tm = localtime(&t);
342:
343: tmp = NULL;
344: tmplen = strlen(fmt);
345:
346: do {
347: tmp = xreallocarray(tmp, 2, tmplen);
348: tmplen *= 2;
349: } while (strftime(tmp, tmplen, fmt, tm) == 0);
350:
351: expanded = format_expand(ft, tmp);
352: free(tmp);
353:
354: return (expanded);
355: }
356:
1.1 nicm 357: /* Expand keys in a template. */
358: char *
359: format_expand(struct format_tree *ft, const char *fmt)
360: {
1.31 nicm 361: char *buf;
362: const char *ptr, *s;
1.1 nicm 363: size_t off, len, n;
1.31 nicm 364: int ch, brackets;
1.58 nicm 365:
366: if (fmt == NULL)
367: return (xstrdup(""));
1.1 nicm 368:
369: len = 64;
370: buf = xmalloc(len);
371: off = 0;
372:
373: while (*fmt != '\0') {
374: if (*fmt != '#') {
375: while (len - off < 2) {
1.50 nicm 376: buf = xreallocarray(buf, 2, len);
1.1 nicm 377: len *= 2;
378: }
379: buf[off++] = *fmt++;
380: continue;
381: }
382: fmt++;
383:
384: ch = (u_char) *fmt++;
385: switch (ch) {
386: case '{':
1.31 nicm 387: brackets = 1;
388: for (ptr = fmt; *ptr != '\0'; ptr++) {
389: if (*ptr == '{')
390: brackets++;
391: if (*ptr == '}' && --brackets == 0)
392: break;
393: }
394: if (*ptr != '}' || brackets != 0)
1.1 nicm 395: break;
396: n = ptr - fmt;
397:
398: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
399: break;
400: fmt += n + 1;
1.40 nicm 401: continue;
402: case '#':
403: while (len - off < 2) {
1.50 nicm 404: buf = xreallocarray(buf, 2, len);
1.40 nicm 405: len *= 2;
406: }
407: buf[off++] = '#';
1.1 nicm 408: continue;
409: default:
1.25 nicm 410: s = NULL;
411: if (ch >= 'A' && ch <= 'Z')
412: s = format_upper[ch - 'A'];
413: else if (ch >= 'a' && ch <= 'z')
414: s = format_lower[ch - 'a'];
415: if (s == NULL) {
416: while (len - off < 3) {
1.50 nicm 417: buf = xreallocarray(buf, 2, len);
1.25 nicm 418: len *= 2;
1.1 nicm 419: }
1.25 nicm 420: buf[off++] = '#';
421: buf[off++] = ch;
422: continue;
1.1 nicm 423: }
1.25 nicm 424: n = strlen(s);
425: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
426: break;
1.1 nicm 427: continue;
428: }
429:
430: break;
431: }
432: buf[off] = '\0';
433:
434: return (buf);
435: }
436:
1.32 nicm 437: /* Get command name for format. */
438: char *
439: format_get_command(struct window_pane *wp)
440: {
1.37 nicm 441: char *cmd, *out;
1.32 nicm 442:
443: cmd = get_proc_name(wp->fd, wp->tty);
444: if (cmd == NULL || *cmd == '\0') {
1.39 nicm 445: free(cmd);
1.46 nicm 446: cmd = cmd_stringify_argv(wp->argc, wp->argv);
1.39 nicm 447: if (cmd == NULL || *cmd == '\0') {
448: free(cmd);
1.38 nicm 449: cmd = xstrdup(wp->shell);
1.39 nicm 450: }
1.32 nicm 451: }
1.37 nicm 452: out = parse_window_name(cmd);
453: free(cmd);
454: return (out);
1.32 nicm 455: }
456:
1.60 nicm 457: /* Get time as a string. */
458: char *
459: format_time_string(time_t t)
460: {
461: char *tim;
462:
463: tim = ctime(&t);
464: *strchr(tim, '\n') = '\0';
465:
466: return (tim);
467: }
468:
1.57 nicm 469: /* Set defaults for any of arguments that are not NULL. */
470: void
471: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
472: struct winlink *wl, struct window_pane *wp)
473: {
474: if (s == NULL && c != NULL)
475: s = c->session;
476: if (wl == NULL && s != NULL)
477: wl = s->curw;
478: if (wp == NULL && wl != NULL)
479: wp = wl->window->active;
480:
481: if (c != NULL)
482: format_defaults_client(ft, c);
483: if (s != NULL)
484: format_defaults_session(ft, s);
485: if (s != NULL && wl != NULL)
486: format_defaults_winlink(ft, s, wl);
487: if (wp != NULL)
488: format_defaults_pane(ft, wp);
489: }
490:
1.1 nicm 491: /* Set default format keys for a session. */
492: void
1.57 nicm 493: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 494: {
495: struct session_group *sg;
496: time_t t;
497:
1.54 nicm 498: ft->s = s;
499:
1.1 nicm 500: format_add(ft, "session_name", "%s", s->name);
501: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
502: format_add(ft, "session_width", "%u", s->sx);
503: format_add(ft, "session_height", "%u", s->sy);
1.23 nicm 504: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 505:
506: sg = session_group_find(s);
507: format_add(ft, "session_grouped", "%d", sg != NULL);
508: if (sg != NULL)
509: format_add(ft, "session_group", "%u", session_group_index(sg));
510:
511: t = s->creation_time.tv_sec;
1.24 deraadt 512: format_add(ft, "session_created", "%lld", (long long) t);
1.60 nicm 513: format_add(ft, "session_created_string", "%s", format_time_string(t));
514:
515: t = s->activity_time.tv_sec;
516: format_add(ft, "session_activity", "%lld", (long long) t);
517: format_add(ft, "session_activity_string", "%s", format_time_string(t));
1.1 nicm 518:
1.41 nicm 519: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 520: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.3 nicm 521: }
522:
523: /* Set default format keys for a client. */
524: void
1.57 nicm 525: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 526: {
1.60 nicm 527: struct session *s;
1.15 nicm 528: time_t t;
1.3 nicm 529:
1.54 nicm 530: if (ft->s == NULL)
531: ft->s = c->session;
532:
1.6 nicm 533: format_add(ft, "client_height", "%u", c->tty.sy);
534: format_add(ft, "client_width", "%u", c->tty.sx);
1.27 nicm 535: if (c->tty.path != NULL)
536: format_add(ft, "client_tty", "%s", c->tty.path);
537: if (c->tty.termname != NULL)
538: format_add(ft, "client_termname", "%s", c->tty.termname);
1.3 nicm 539:
540: t = c->creation_time.tv_sec;
1.24 deraadt 541: format_add(ft, "client_created", "%lld", (long long) t);
1.60 nicm 542: format_add(ft, "client_created_string", "%s", format_time_string(t));
1.3 nicm 543:
544: t = c->activity_time.tv_sec;
1.24 deraadt 545: format_add(ft, "client_activity", "%lld", (long long) t);
1.60 nicm 546: format_add(ft, "client_activity_string", "%s", format_time_string(t));
1.14 nicm 547:
1.61 nicm 548: if (strcmp(c->keytable->name, "root") == 0)
549: format_add(ft, "client_prefix", "%d", 0);
550: else
551: format_add(ft, "client_prefix", "%d", 1);
552: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 553:
554: if (c->tty.flags & TTY_UTF8)
555: format_add(ft, "client_utf8", "%d", 1);
556: else
557: format_add(ft, "client_utf8", "%d", 0);
558:
559: if (c->flags & CLIENT_READONLY)
560: format_add(ft, "client_readonly", "%d", 1);
561: else
562: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 563:
564: s = c->session;
565: if (s != NULL)
566: format_add(ft, "client_session", "%s", s->name);
567: s = c->last_session;
568: if (s != NULL && session_alive(s))
569: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 570: }
571:
1.32 nicm 572: /* Set default format keys for a window. */
573: void
1.57 nicm 574: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 575: {
576: char *layout;
577:
1.54 nicm 578: ft->w = w;
579:
1.63 nicm 580: if (w->saved_layout_root != NULL)
581: layout = layout_dump(w->saved_layout_root);
582: else
583: layout = layout_dump(w->layout_root);
1.32 nicm 584:
585: format_add(ft, "window_id", "@%u", w->id);
586: format_add(ft, "window_name", "%s", w->name);
587: format_add(ft, "window_width", "%u", w->sx);
588: format_add(ft, "window_height", "%u", w->sy);
589: format_add(ft, "window_layout", "%s", layout);
590: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 591: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 592: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 593:
594: free(layout);
595: }
596:
1.1 nicm 597: /* Set default format keys for a winlink. */
598: void
1.57 nicm 599: format_defaults_winlink(struct format_tree *ft, struct session *s,
600: struct winlink *wl)
1.1 nicm 601: {
602: struct window *w = wl->window;
1.32 nicm 603: char *flags;
1.1 nicm 604:
1.54 nicm 605: if (ft->w == NULL)
606: ft->w = wl->window;
607:
1.1 nicm 608: flags = window_printable_flags(s, wl);
609:
1.57 nicm 610: format_defaults_window(ft, w);
1.32 nicm 611:
1.1 nicm 612: format_add(ft, "window_index", "%d", wl->idx);
613: format_add(ft, "window_flags", "%s", flags);
614: format_add(ft, "window_active", "%d", wl == s->curw);
1.29 nicm 615:
1.59 nicm 616: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 617: !!(wl->flags & WINLINK_BELL));
1.59 nicm 618: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 619: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 620: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 621: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 622: format_add(ft, "window_last_flag", "%d",
1.49 nicm 623: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 ! nicm 624: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.32 nicm 625:
1.9 nicm 626: free(flags);
1.1 nicm 627: }
628:
1.19 nicm 629: /* Add window pane tabs. */
630: void
1.57 nicm 631: format_defaults_pane_tabs(struct format_tree *ft, struct window_pane *wp)
1.19 nicm 632: {
633: struct evbuffer *buffer;
634: u_int i;
635:
636: buffer = evbuffer_new();
637: for (i = 0; i < wp->base.grid->sx; i++) {
638: if (!bit_test(wp->base.tabs, i))
639: continue;
640:
641: if (EVBUFFER_LENGTH(buffer) > 0)
642: evbuffer_add(buffer, ",", 1);
1.59 nicm 643: evbuffer_add_printf(buffer, "%u", i);
1.19 nicm 644: }
645:
646: format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
647: EVBUFFER_DATA(buffer));
648: evbuffer_free(buffer);
649: }
650:
1.1 nicm 651: /* Set default format keys for a window pane. */
652: void
1.57 nicm 653: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 654: {
655: struct grid *gd = wp->base.grid;
656: struct grid_line *gl;
657: unsigned long long size;
1.20 nicm 658: u_int i, idx;
1.21 nicm 659: char *cmd;
1.55 nicm 660: int status;
1.54 nicm 661:
662: if (ft->w == NULL)
663: ft->w = wp->window;
1.1 nicm 664:
665: size = 0;
666: for (i = 0; i < gd->hsize; i++) {
667: gl = &gd->linedata[i];
668: size += gl->cellsize * sizeof *gl->celldata;
669: }
670: size += gd->hsize * sizeof *gd->linedata;
1.16 nicm 671: format_add(ft, "history_size", "%u", gd->hsize);
672: format_add(ft, "history_limit", "%u", gd->hlimit);
673: format_add(ft, "history_bytes", "%llu", size);
1.1 nicm 674:
1.4 nicm 675: if (window_pane_index(wp, &idx) != 0)
676: fatalx("index not found");
1.16 nicm 677: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 678:
1.1 nicm 679: format_add(ft, "pane_width", "%u", wp->sx);
680: format_add(ft, "pane_height", "%u", wp->sy);
681: format_add(ft, "pane_title", "%s", wp->base.title);
682: format_add(ft, "pane_id", "%%%u", wp->id);
683: format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55 nicm 684: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
685:
686: status = wp->status;
687: if (wp->fd == -1 && WIFEXITED(status))
688: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1 nicm 689: format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47 nicm 690:
691: if (window_pane_visible(wp)) {
692: format_add(ft, "pane_left", "%u", wp->xoff);
693: format_add(ft, "pane_top", "%u", wp->yoff);
694: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
695: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
696: }
1.16 nicm 697:
698: format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.26 nicm 699: format_add(ft, "pane_synchronized", "%d",
700: !!options_get_number(&wp->window->options, "synchronize-panes"));
1.16 nicm 701:
702: if (wp->tty != NULL)
703: format_add(ft, "pane_tty", "%s", wp->tty);
704: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.46 nicm 705: if ((cmd = cmd_stringify_argv(wp->argc, wp->argv)) != NULL) {
706: format_add(ft, "pane_start_command", "%s", cmd);
707: free(cmd);
708: }
1.35 nicm 709: if ((cmd = format_get_command(wp)) != NULL) {
1.17 nicm 710: format_add(ft, "pane_current_command", "%s", cmd);
1.21 nicm 711: free(cmd);
712: }
1.16 nicm 713:
1.59 nicm 714: format_add(ft, "cursor_x", "%u", wp->base.cx);
715: format_add(ft, "cursor_y", "%u", wp->base.cy);
716: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
717: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
718: format_add(ft, "saved_cursor_x", "%u", wp->ictx.old_cx);
719: format_add(ft, "saved_cursor_y", "%u", wp->ictx.old_cy);
1.16 nicm 720:
721: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 722: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
723: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 724:
725: format_add(ft, "cursor_flag", "%d",
726: !!(wp->base.mode & MODE_CURSOR));
727: format_add(ft, "insert_flag", "%d",
728: !!(wp->base.mode & MODE_INSERT));
729: format_add(ft, "keypad_cursor_flag", "%d",
730: !!(wp->base.mode & MODE_KCURSOR));
731: format_add(ft, "keypad_flag", "%d",
732: !!(wp->base.mode & MODE_KKEYPAD));
733: format_add(ft, "wrap_flag", "%d",
734: !!(wp->base.mode & MODE_WRAP));
735:
1.62 nicm 736: format_add(ft, "mouse_any_flag", "%d",
737: !!(wp->base.mode & (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON)));
1.16 nicm 738: format_add(ft, "mouse_standard_flag", "%d",
739: !!(wp->base.mode & MODE_MOUSE_STANDARD));
740: format_add(ft, "mouse_button_flag", "%d",
741: !!(wp->base.mode & MODE_MOUSE_BUTTON));
742: format_add(ft, "mouse_utf8_flag", "%d",
743: !!(wp->base.mode & MODE_MOUSE_UTF8));
1.19 nicm 744:
1.57 nicm 745: format_defaults_pane_tabs(ft, wp);
1.8 nicm 746: }
747:
1.19 nicm 748: /* Set default format keys for paste buffer. */
1.8 nicm 749: void
1.57 nicm 750: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb,
1.42 nicm 751: int utf8flag)
1.8 nicm 752: {
1.42 nicm 753: char *s;
1.8 nicm 754:
755: format_add(ft, "buffer_size", "%zu", pb->size);
1.45 nicm 756: format_add(ft, "buffer_name", "%s", pb->name);
1.8 nicm 757:
1.42 nicm 758: s = paste_make_sample(pb, utf8flag);
759: format_add(ft, "buffer_sample", "%s", s);
760: free(s);
1.1 nicm 761: }