4 * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
38 * Build a list of key-value pairs and use them to expand #{key} entries in a
42 struct format_expand_state
;
44 static char *format_job_get(struct format_expand_state
*, const char *);
45 static char *format_expand1(struct format_expand_state
*, const char *);
46 static int format_replace(struct format_expand_state
*, const char *,
47 size_t, char **, size_t *, size_t *);
48 static void format_defaults_session(struct format_tree
*,
50 static void format_defaults_client(struct format_tree
*, struct client
*);
51 static void format_defaults_winlink(struct format_tree
*,
54 /* Entry in format job tree. */
56 struct client
*client
;
68 RB_ENTRY(format_job
) entry
;
71 /* Format job tree. */
72 static int format_job_cmp(struct format_job
*, struct format_job
*);
73 static RB_HEAD(format_job_tree
, format_job
) format_jobs
= RB_INITIALIZER();
74 RB_GENERATE_STATIC(format_job_tree
, format_job
, entry
, format_job_cmp
);
76 /* Format job tree comparison function. */
78 format_job_cmp(struct format_job
*fj1
, struct format_job
*fj2
)
80 if (fj1
->tag
< fj2
->tag
)
82 if (fj1
->tag
> fj2
->tag
)
84 return (strcmp(fj1
->cmd
, fj2
->cmd
));
87 /* Format modifiers. */
88 #define FORMAT_TIMESTRING 0x1
89 #define FORMAT_BASENAME 0x2
90 #define FORMAT_DIRNAME 0x4
91 #define FORMAT_QUOTE_SHELL 0x8
92 #define FORMAT_LITERAL 0x10
93 #define FORMAT_EXPAND 0x20
94 #define FORMAT_EXPANDTIME 0x40
95 #define FORMAT_SESSIONS 0x80
96 #define FORMAT_WINDOWS 0x100
97 #define FORMAT_PANES 0x200
98 #define FORMAT_PRETTY 0x400
99 #define FORMAT_LENGTH 0x800
100 #define FORMAT_WIDTH 0x1000
101 #define FORMAT_QUOTE_STYLE 0x2000
102 #define FORMAT_WINDOW_NAME 0x4000
103 #define FORMAT_SESSION_NAME 0x8000
104 #define FORMAT_CHARACTER 0x10000
105 #define FORMAT_COLOUR 0x20000
106 #define FORMAT_CLIENTS 0x40000
108 /* Limit on recursion. */
109 #define FORMAT_LOOP_LIMIT 100
111 /* Format expand flags. */
112 #define FORMAT_EXPAND_TIME 0x1
113 #define FORMAT_EXPAND_NOJOBS 0x2
115 /* Entry in format tree. */
116 struct format_entry
{
121 RB_ENTRY(format_entry
) entry
;
133 enum format_type type
;
139 struct window_pane
*wp
;
140 struct paste_buffer
*pb
;
142 struct cmdq_item
*item
;
143 struct client
*client
;
147 struct mouse_event m
;
149 RB_HEAD(format_entry_tree
, format_entry
) tree
;
151 static int format_entry_cmp(struct format_entry
*, struct format_entry
*);
152 RB_GENERATE_STATIC(format_entry_tree
, format_entry
, entry
, format_entry_cmp
);
154 /* Format expand state. */
155 struct format_expand_state
{
156 struct format_tree
*ft
;
163 /* Format modifier. */
164 struct format_modifier
{
172 /* Format entry tree comparison function. */
174 format_entry_cmp(struct format_entry
*fe1
, struct format_entry
*fe2
)
176 return (strcmp(fe1
->key
, fe2
->key
));
179 /* Single-character uppercase aliases. */
180 static const char *format_upper
[] = {
186 "window_flags", /* F */
189 "window_index", /* I */
196 "pane_index", /* P */
199 "session_name", /* S */
200 "pane_title", /* T */
203 "window_name", /* W */
209 /* Single-character lowercase aliases. */
210 static const char *format_lower
[] = {
218 "host_short", /* h */
239 /* Is logging enabled? */
241 format_logging(struct format_tree
*ft
)
243 return (log_get_level() != 0 || (ft
->flags
& FORMAT_VERBOSE
));
246 /* Log a message if verbose. */
247 static void printflike(3, 4)
248 format_log1(struct format_expand_state
*es
, const char *from
, const char *fmt
,
251 struct format_tree
*ft
= es
->ft
;
254 static const char spaces
[] = " ";
256 if (!format_logging(ft
))
260 xvasprintf(&s
, fmt
, ap
);
263 log_debug("%s: %s", from
, s
);
264 if (ft
->item
!= NULL
&& (ft
->flags
& FORMAT_VERBOSE
))
265 cmdq_print(ft
->item
, "#%.*s%s", es
->loop
, spaces
, s
);
269 #define format_log(es, fmt, ...) format_log1(es, __func__, fmt, ##__VA_ARGS__)
271 /* Copy expand state. */
273 format_copy_state(struct format_expand_state
*to
,
274 struct format_expand_state
*from
, int flags
)
277 to
->loop
= from
->loop
;
278 to
->time
= from
->time
;
279 memcpy(&to
->tm
, &from
->tm
, sizeof to
->tm
);
280 to
->flags
= from
->flags
|flags
;
283 /* Format job update callback. */
285 format_job_update(struct job
*job
)
287 struct format_job
*fj
= job_get_data(job
);
288 struct evbuffer
*evb
= job_get_event(job
)->input
;
289 char *line
= NULL
, *next
;
292 while ((next
= evbuffer_readline(evb
)) != NULL
) {
303 log_debug("%s: %p %s: %s", __func__
, fj
, fj
->cmd
, fj
->out
);
306 if (fj
->status
&& fj
->last
!= t
) {
307 if (fj
->client
!= NULL
)
308 server_status_client(fj
->client
);
313 /* Format job complete callback. */
315 format_job_complete(struct job
*job
)
317 struct format_job
*fj
= job_get_data(job
);
318 struct evbuffer
*evb
= job_get_event(job
)->input
;
325 if ((line
= evbuffer_readline(evb
)) == NULL
) {
326 len
= EVBUFFER_LENGTH(evb
);
327 buf
= xmalloc(len
+ 1);
329 memcpy(buf
, EVBUFFER_DATA(evb
), len
);
334 log_debug("%s: %p %s: %s", __func__
, fj
, fj
->cmd
, buf
);
336 if (*buf
!= '\0' || !fj
->updated
) {
343 if (fj
->client
!= NULL
)
344 server_status_client(fj
->client
);
351 format_job_get(struct format_expand_state
*es
, const char *cmd
)
353 struct format_tree
*ft
= es
->ft
;
354 struct format_job_tree
*jobs
;
355 struct format_job fj0
, *fj
;
359 struct format_expand_state next
;
361 if (ft
->client
== NULL
)
363 else if (ft
->client
->jobs
!= NULL
)
364 jobs
= ft
->client
->jobs
;
366 jobs
= ft
->client
->jobs
= xmalloc(sizeof *ft
->client
->jobs
);
372 if ((fj
= RB_FIND(format_job_tree
, jobs
, &fj0
)) == NULL
) {
373 fj
= xcalloc(1, sizeof *fj
);
374 fj
->client
= ft
->client
;
376 fj
->cmd
= xstrdup(cmd
);
378 RB_INSERT(format_job_tree
, jobs
, fj
);
381 format_copy_state(&next
, es
, FORMAT_EXPAND_NOJOBS
);
382 next
.flags
&= ~FORMAT_EXPAND_TIME
;
384 expanded
= format_expand1(&next
, cmd
);
385 if (fj
->expanded
== NULL
|| strcmp(expanded
, fj
->expanded
) != 0) {
386 free((void *)fj
->expanded
);
387 fj
->expanded
= xstrdup(expanded
);
390 force
= (ft
->flags
& FORMAT_FORCE
);
393 if (force
&& fj
->job
!= NULL
)
395 if (force
|| (fj
->job
== NULL
&& fj
->last
!= t
)) {
396 fj
->job
= job_run(expanded
, 0, NULL
, NULL
, NULL
,
397 server_client_get_cwd(ft
->client
, NULL
), format_job_update
,
398 format_job_complete
, NULL
, fj
, JOB_NOWAIT
, -1, -1);
399 if (fj
->job
== NULL
) {
401 xasprintf(&fj
->out
, "<'%s' didn't start>", fj
->cmd
);
405 } else if (fj
->job
!= NULL
&& (t
- fj
->last
) > 1 && fj
->out
== NULL
)
406 xasprintf(&fj
->out
, "<'%s' not ready>", fj
->cmd
);
409 if (ft
->flags
& FORMAT_STATUS
)
412 return (xstrdup(""));
413 return (format_expand1(&next
, fj
->out
));
416 /* Remove old jobs. */
418 format_job_tidy(struct format_job_tree
*jobs
, int force
)
420 struct format_job
*fj
, *fj1
;
424 RB_FOREACH_SAFE(fj
, format_job_tree
, jobs
, fj1
) {
425 if (!force
&& (fj
->last
> now
|| now
- fj
->last
< 3600))
427 RB_REMOVE(format_job_tree
, jobs
, fj
);
429 log_debug("%s: %s", __func__
, fj
->cmd
);
434 free((void *)fj
->expanded
);
435 free((void *)fj
->cmd
);
442 /* Tidy old jobs for all clients. */
444 format_tidy_jobs(void)
448 format_job_tidy(&format_jobs
, 0);
449 TAILQ_FOREACH(c
, &clients
, entry
) {
451 format_job_tidy(c
->jobs
, 0);
455 /* Remove old jobs for client. */
457 format_lost_client(struct client
*c
)
460 format_job_tidy(c
->jobs
, 1);
464 /* Wrapper for asprintf. */
465 static char * printflike(1, 2)
466 format_printf(const char *fmt
, ...)
472 xvasprintf(&s
, fmt
, ap
);
477 /* Callback for host. */
479 format_cb_host(__unused
struct format_tree
*ft
)
481 char host
[HOST_NAME_MAX
+ 1];
483 if (gethostname(host
, sizeof host
) != 0)
484 return (xstrdup(""));
485 return (xstrdup(host
));
488 /* Callback for host_short. */
490 format_cb_host_short(__unused
struct format_tree
*ft
)
492 char host
[HOST_NAME_MAX
+ 1], *cp
;
494 if (gethostname(host
, sizeof host
) != 0)
495 return (xstrdup(""));
496 if ((cp
= strchr(host
, '.')) != NULL
)
498 return (xstrdup(host
));
501 /* Callback for pid. */
503 format_cb_pid(__unused
struct format_tree
*ft
)
507 xasprintf(&value
, "%ld", (long)getpid());
511 /* Callback for session_attached_list. */
513 format_cb_session_attached_list(struct format_tree
*ft
)
515 struct session
*s
= ft
->s
;
517 struct evbuffer
*buffer
;
524 buffer
= evbuffer_new();
526 fatalx("out of memory");
528 TAILQ_FOREACH(loop
, &clients
, entry
) {
529 if (loop
->session
== s
) {
530 if (EVBUFFER_LENGTH(buffer
) > 0)
531 evbuffer_add(buffer
, ",", 1);
532 evbuffer_add_printf(buffer
, "%s", loop
->name
);
536 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
537 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
538 evbuffer_free(buffer
);
542 /* Callback for session_alerts. */
544 format_cb_session_alerts(struct format_tree
*ft
)
546 struct session
*s
= ft
->s
;
548 char alerts
[1024], tmp
[16];
554 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
555 if ((wl
->flags
& WINLINK_ALERTFLAGS
) == 0)
557 xsnprintf(tmp
, sizeof tmp
, "%u", wl
->idx
);
560 strlcat(alerts
, ",", sizeof alerts
);
561 strlcat(alerts
, tmp
, sizeof alerts
);
562 if (wl
->flags
& WINLINK_ACTIVITY
)
563 strlcat(alerts
, "#", sizeof alerts
);
564 if (wl
->flags
& WINLINK_BELL
)
565 strlcat(alerts
, "!", sizeof alerts
);
566 if (wl
->flags
& WINLINK_SILENCE
)
567 strlcat(alerts
, "~", sizeof alerts
);
569 return (xstrdup(alerts
));
572 /* Callback for session_stack. */
574 format_cb_session_stack(struct format_tree
*ft
)
576 struct session
*s
= ft
->s
;
578 char result
[1024], tmp
[16];
583 xsnprintf(result
, sizeof result
, "%u", s
->curw
->idx
);
584 TAILQ_FOREACH(wl
, &s
->lastw
, sentry
) {
585 xsnprintf(tmp
, sizeof tmp
, "%u", wl
->idx
);
588 strlcat(result
, ",", sizeof result
);
589 strlcat(result
, tmp
, sizeof result
);
591 return (xstrdup(result
));
594 /* Callback for window_stack_index. */
596 format_cb_window_stack_index(struct format_tree
*ft
)
608 TAILQ_FOREACH(wl
, &s
->lastw
, sentry
) {
614 return (xstrdup("0"));
615 xasprintf(&value
, "%u", idx
);
619 /* Callback for window_linked_sessions_list. */
621 format_cb_window_linked_sessions_list(struct format_tree
*ft
)
625 struct evbuffer
*buffer
;
633 buffer
= evbuffer_new();
635 fatalx("out of memory");
637 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
638 if (EVBUFFER_LENGTH(buffer
) > 0)
639 evbuffer_add(buffer
, ",", 1);
640 evbuffer_add_printf(buffer
, "%s", wl
->session
->name
);
643 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
644 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
645 evbuffer_free(buffer
);
649 /* Callback for window_active_sessions. */
651 format_cb_window_active_sessions(struct format_tree
*ft
)
662 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
663 if (wl
->session
->curw
== wl
)
667 xasprintf(&value
, "%u", n
);
671 /* Callback for window_active_sessions_list. */
673 format_cb_window_active_sessions_list(struct format_tree
*ft
)
677 struct evbuffer
*buffer
;
685 buffer
= evbuffer_new();
687 fatalx("out of memory");
689 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
690 if (wl
->session
->curw
== wl
) {
691 if (EVBUFFER_LENGTH(buffer
) > 0)
692 evbuffer_add(buffer
, ",", 1);
693 evbuffer_add_printf(buffer
, "%s", wl
->session
->name
);
697 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
698 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
699 evbuffer_free(buffer
);
703 /* Callback for window_active_clients. */
705 format_cb_window_active_clients(struct format_tree
*ft
)
709 struct session
*client_session
;
717 TAILQ_FOREACH(loop
, &clients
, entry
) {
718 client_session
= loop
->session
;
719 if (client_session
== NULL
)
722 if (w
== client_session
->curw
->window
)
726 xasprintf(&value
, "%u", n
);
730 /* Callback for window_active_clients_list. */
732 format_cb_window_active_clients_list(struct format_tree
*ft
)
736 struct session
*client_session
;
737 struct evbuffer
*buffer
;
745 buffer
= evbuffer_new();
747 fatalx("out of memory");
749 TAILQ_FOREACH(loop
, &clients
, entry
) {
750 client_session
= loop
->session
;
751 if (client_session
== NULL
)
754 if (w
== client_session
->curw
->window
) {
755 if (EVBUFFER_LENGTH(buffer
) > 0)
756 evbuffer_add(buffer
, ",", 1);
757 evbuffer_add_printf(buffer
, "%s", loop
->name
);
761 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
762 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
763 evbuffer_free(buffer
);
767 /* Callback for window_layout. */
769 format_cb_window_layout(struct format_tree
*ft
)
771 struct window
*w
= ft
->w
;
776 if (w
->saved_layout_root
!= NULL
)
777 return (layout_dump(w
->saved_layout_root
));
778 return (layout_dump(w
->layout_root
));
781 /* Callback for window_visible_layout. */
783 format_cb_window_visible_layout(struct format_tree
*ft
)
785 struct window
*w
= ft
->w
;
790 return (layout_dump(w
->layout_root
));
793 /* Callback for pane_start_command. */
795 format_cb_start_command(struct format_tree
*ft
)
797 struct window_pane
*wp
= ft
->wp
;
802 return (cmd_stringify_argv(wp
->argc
, wp
->argv
));
805 /* Callback for pane_start_path. */
807 format_cb_start_path(struct format_tree
*ft
)
809 struct window_pane
*wp
= ft
->wp
;
815 return (xstrdup(""));
816 return (xstrdup(wp
->cwd
));
819 /* Callback for pane_current_command. */
821 format_cb_current_command(struct format_tree
*ft
)
823 struct window_pane
*wp
= ft
->wp
;
826 if (wp
== NULL
|| wp
->shell
== NULL
)
829 cmd
= osdep_get_name(wp
->fd
, wp
->tty
);
830 if (cmd
== NULL
|| *cmd
== '\0') {
832 cmd
= cmd_stringify_argv(wp
->argc
, wp
->argv
);
833 if (cmd
== NULL
|| *cmd
== '\0') {
835 cmd
= xstrdup(wp
->shell
);
838 value
= parse_window_name(cmd
);
843 /* Callback for pane_current_path. */
845 format_cb_current_path(struct format_tree
*ft
)
847 struct window_pane
*wp
= ft
->wp
;
853 cwd
= osdep_get_cwd(wp
->fd
);
856 return (xstrdup(cwd
));
859 /* Callback for history_bytes. */
861 format_cb_history_bytes(struct format_tree
*ft
)
863 struct window_pane
*wp
= ft
->wp
;
865 struct grid_line
*gl
;
874 for (i
= 0; i
< gd
->hsize
+ gd
->sy
; i
++) {
875 gl
= grid_get_line(gd
, i
);
876 size
+= gl
->cellsize
* sizeof *gl
->celldata
;
877 size
+= gl
->extdsize
* sizeof *gl
->extddata
;
879 size
+= (gd
->hsize
+ gd
->sy
) * sizeof *gl
;
881 xasprintf(&value
, "%zu", size
);
885 /* Callback for history_all_bytes. */
887 format_cb_history_all_bytes(struct format_tree
*ft
)
889 struct window_pane
*wp
= ft
->wp
;
891 struct grid_line
*gl
;
892 u_int i
, lines
, cells
= 0, extended_cells
= 0;
899 lines
= gd
->hsize
+ gd
->sy
;
900 for (i
= 0; i
< lines
; i
++) {
901 gl
= grid_get_line(gd
, i
);
902 cells
+= gl
->cellsize
;
903 extended_cells
+= gl
->extdsize
;
906 xasprintf(&value
, "%u,%zu,%u,%zu,%u,%zu", lines
,
907 lines
* sizeof *gl
, cells
, cells
* sizeof *gl
->celldata
,
908 extended_cells
, extended_cells
* sizeof *gl
->extddata
);
912 /* Callback for pane_tabs. */
914 format_cb_pane_tabs(struct format_tree
*ft
)
916 struct window_pane
*wp
= ft
->wp
;
917 struct evbuffer
*buffer
;
925 buffer
= evbuffer_new();
927 fatalx("out of memory");
928 for (i
= 0; i
< wp
->base
.grid
->sx
; i
++) {
929 if (!bit_test(wp
->base
.tabs
, i
))
932 if (EVBUFFER_LENGTH(buffer
) > 0)
933 evbuffer_add(buffer
, ",", 1);
934 evbuffer_add_printf(buffer
, "%u", i
);
936 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
937 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
938 evbuffer_free(buffer
);
942 /* Callback for pane_fg. */
944 format_cb_pane_fg(struct format_tree
*ft
)
946 struct window_pane
*wp
= ft
->wp
;
952 tty_default_colours(&gc
, wp
);
953 return (xstrdup(colour_tostring(gc
.fg
)));
956 /* Callback for pane_bg. */
958 format_cb_pane_bg(struct format_tree
*ft
)
960 struct window_pane
*wp
= ft
->wp
;
966 tty_default_colours(&gc
, wp
);
967 return (xstrdup(colour_tostring(gc
.bg
)));
970 /* Callback for session_group_list. */
972 format_cb_session_group_list(struct format_tree
*ft
)
974 struct session
*s
= ft
->s
;
975 struct session_group
*sg
;
976 struct session
*loop
;
977 struct evbuffer
*buffer
;
983 sg
= session_group_contains(s
);
987 buffer
= evbuffer_new();
989 fatalx("out of memory");
991 TAILQ_FOREACH(loop
, &sg
->sessions
, gentry
) {
992 if (EVBUFFER_LENGTH(buffer
) > 0)
993 evbuffer_add(buffer
, ",", 1);
994 evbuffer_add_printf(buffer
, "%s", loop
->name
);
997 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
998 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
999 evbuffer_free(buffer
);
1003 /* Callback for session_group_attached_list. */
1005 format_cb_session_group_attached_list(struct format_tree
*ft
)
1007 struct session
*s
= ft
->s
, *client_session
, *session_loop
;
1008 struct session_group
*sg
;
1009 struct client
*loop
;
1010 struct evbuffer
*buffer
;
1016 sg
= session_group_contains(s
);
1020 buffer
= evbuffer_new();
1022 fatalx("out of memory");
1024 TAILQ_FOREACH(loop
, &clients
, entry
) {
1025 client_session
= loop
->session
;
1026 if (client_session
== NULL
)
1028 TAILQ_FOREACH(session_loop
, &sg
->sessions
, gentry
) {
1029 if (session_loop
== client_session
){
1030 if (EVBUFFER_LENGTH(buffer
) > 0)
1031 evbuffer_add(buffer
, ",", 1);
1032 evbuffer_add_printf(buffer
, "%s", loop
->name
);
1037 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
1038 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
1039 evbuffer_free(buffer
);
1043 /* Callback for pane_in_mode. */
1045 format_cb_pane_in_mode(struct format_tree
*ft
)
1047 struct window_pane
*wp
= ft
->wp
;
1049 struct window_mode_entry
*wme
;
1055 TAILQ_FOREACH(wme
, &wp
->modes
, entry
)
1057 xasprintf(&value
, "%u", n
);
1061 /* Callback for pane_at_top. */
1063 format_cb_pane_at_top(struct format_tree
*ft
)
1065 struct window_pane
*wp
= ft
->wp
;
1074 status
= options_get_number(w
->options
, "pane-border-status");
1075 if (status
== PANE_STATUS_TOP
)
1076 flag
= (wp
->yoff
== 1);
1078 flag
= (wp
->yoff
== 0);
1079 xasprintf(&value
, "%d", flag
);
1083 /* Callback for pane_at_bottom. */
1085 format_cb_pane_at_bottom(struct format_tree
*ft
)
1087 struct window_pane
*wp
= ft
->wp
;
1096 status
= options_get_number(w
->options
, "pane-border-status");
1097 if (status
== PANE_STATUS_BOTTOM
)
1098 flag
= (wp
->yoff
+ wp
->sy
== w
->sy
- 1);
1100 flag
= (wp
->yoff
+ wp
->sy
== w
->sy
);
1101 xasprintf(&value
, "%d", flag
);
1105 /* Callback for cursor_character. */
1107 format_cb_cursor_character(struct format_tree
*ft
)
1109 struct window_pane
*wp
= ft
->wp
;
1110 struct grid_cell gc
;
1116 grid_view_get_cell(wp
->base
.grid
, wp
->base
.cx
, wp
->base
.cy
, &gc
);
1117 if (~gc
.flags
& GRID_FLAG_PADDING
)
1118 xasprintf(&value
, "%.*s", (int)gc
.data
.size
, gc
.data
.data
);
1122 /* Callback for mouse_word. */
1124 format_cb_mouse_word(struct format_tree
*ft
)
1126 struct window_pane
*wp
;
1132 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1135 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1138 if (!TAILQ_EMPTY(&wp
->modes
)) {
1139 if (window_pane_mode(wp
) != WINDOW_PANE_NO_MODE
)
1140 return (window_copy_get_word(wp
, x
, y
));
1144 return (format_grid_word(gd
, x
, gd
->hsize
+ y
));
1147 /* Callback for mouse_hyperlink. */
1149 format_cb_mouse_hyperlink(struct format_tree
*ft
)
1151 struct window_pane
*wp
;
1157 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1160 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1163 return (format_grid_hyperlink(gd
, x
, gd
->hsize
+ y
, wp
->screen
));
1166 /* Callback for mouse_line. */
1168 format_cb_mouse_line(struct format_tree
*ft
)
1170 struct window_pane
*wp
;
1176 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1179 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1182 if (!TAILQ_EMPTY(&wp
->modes
)) {
1183 if (window_pane_mode(wp
) != WINDOW_PANE_NO_MODE
)
1184 return (window_copy_get_line(wp
, y
));
1188 return (format_grid_line(gd
, gd
->hsize
+ y
));
1191 /* Callback for mouse_status_line. */
1193 format_cb_mouse_status_line(struct format_tree
*ft
)
1200 if (ft
->c
== NULL
|| (~ft
->c
->tty
.flags
& TTY_STARTED
))
1203 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
) {
1205 } else if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
) {
1206 y
= ft
->m
.y
- ft
->m
.statusat
;
1209 xasprintf(&value
, "%u", y
);
1214 /* Callback for mouse_status_range. */
1216 format_cb_mouse_status_range(struct format_tree
*ft
)
1218 struct style_range
*sr
;
1223 if (ft
->c
== NULL
|| (~ft
->c
->tty
.flags
& TTY_STARTED
))
1226 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
) {
1229 } else if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
) {
1231 y
= ft
->m
.y
- ft
->m
.statusat
;
1235 sr
= status_get_range(ft
->c
, x
, y
);
1239 case STYLE_RANGE_NONE
:
1241 case STYLE_RANGE_LEFT
:
1242 return (xstrdup("left"));
1243 case STYLE_RANGE_RIGHT
:
1244 return (xstrdup("right"));
1245 case STYLE_RANGE_PANE
:
1246 return (xstrdup("pane"));
1247 case STYLE_RANGE_WINDOW
:
1248 return (xstrdup("window"));
1249 case STYLE_RANGE_SESSION
:
1250 return (xstrdup("session"));
1251 case STYLE_RANGE_USER
:
1252 return (xstrdup(sr
->string
));
1257 /* Callback for alternate_on. */
1259 format_cb_alternate_on(struct format_tree
*ft
)
1261 if (ft
->wp
!= NULL
) {
1262 if (ft
->wp
->base
.saved_grid
!= NULL
)
1263 return (xstrdup("1"));
1264 return (xstrdup("0"));
1269 /* Callback for alternate_saved_x. */
1271 format_cb_alternate_saved_x(struct format_tree
*ft
)
1274 return (format_printf("%u", ft
->wp
->base
.saved_cx
));
1278 /* Callback for alternate_saved_y. */
1280 format_cb_alternate_saved_y(struct format_tree
*ft
)
1283 return (format_printf("%u", ft
->wp
->base
.saved_cy
));
1287 /* Callback for buffer_name. */
1289 format_cb_buffer_name(struct format_tree
*ft
)
1292 return (xstrdup(paste_buffer_name(ft
->pb
)));
1296 /* Callback for buffer_sample. */
1298 format_cb_buffer_sample(struct format_tree
*ft
)
1301 return (paste_make_sample(ft
->pb
));
1305 /* Callback for buffer_size. */
1307 format_cb_buffer_size(struct format_tree
*ft
)
1311 if (ft
->pb
!= NULL
) {
1312 paste_buffer_data(ft
->pb
, &size
);
1313 return (format_printf("%zu", size
));
1318 /* Callback for client_cell_height. */
1320 format_cb_client_cell_height(struct format_tree
*ft
)
1322 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1323 return (format_printf("%u", ft
->c
->tty
.ypixel
));
1327 /* Callback for client_cell_width. */
1329 format_cb_client_cell_width(struct format_tree
*ft
)
1331 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1332 return (format_printf("%u", ft
->c
->tty
.xpixel
));
1336 /* Callback for client_control_mode. */
1338 format_cb_client_control_mode(struct format_tree
*ft
)
1340 if (ft
->c
!= NULL
) {
1341 if (ft
->c
->flags
& CLIENT_CONTROL
)
1342 return (xstrdup("1"));
1343 return (xstrdup("0"));
1348 /* Callback for client_discarded. */
1350 format_cb_client_discarded(struct format_tree
*ft
)
1353 return (format_printf("%zu", ft
->c
->discarded
));
1357 /* Callback for client_flags. */
1359 format_cb_client_flags(struct format_tree
*ft
)
1362 return (xstrdup(server_client_get_flags(ft
->c
)));
1366 /* Callback for client_height. */
1368 format_cb_client_height(struct format_tree
*ft
)
1370 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1371 return (format_printf("%u", ft
->c
->tty
.sy
));
1375 /* Callback for client_key_table. */
1377 format_cb_client_key_table(struct format_tree
*ft
)
1380 return (xstrdup(ft
->c
->keytable
->name
));
1384 /* Callback for client_last_session. */
1386 format_cb_client_last_session(struct format_tree
*ft
)
1388 if (ft
->c
!= NULL
&&
1389 ft
->c
->last_session
!= NULL
&&
1390 session_alive(ft
->c
->last_session
))
1391 return (xstrdup(ft
->c
->last_session
->name
));
1395 /* Callback for client_name. */
1397 format_cb_client_name(struct format_tree
*ft
)
1400 return (xstrdup(ft
->c
->name
));
1404 /* Callback for client_pid. */
1406 format_cb_client_pid(struct format_tree
*ft
)
1409 return (format_printf("%ld", (long)ft
->c
->pid
));
1413 /* Callback for client_prefix. */
1415 format_cb_client_prefix(struct format_tree
*ft
)
1419 if (ft
->c
!= NULL
) {
1420 name
= server_client_get_key_table(ft
->c
);
1421 if (strcmp(ft
->c
->keytable
->name
, name
) == 0)
1422 return (xstrdup("0"));
1423 return (xstrdup("1"));
1428 /* Callback for client_readonly. */
1430 format_cb_client_readonly(struct format_tree
*ft
)
1432 if (ft
->c
!= NULL
) {
1433 if (ft
->c
->flags
& CLIENT_READONLY
)
1434 return (xstrdup("1"));
1435 return (xstrdup("0"));
1440 /* Callback for client_session. */
1442 format_cb_client_session(struct format_tree
*ft
)
1444 if (ft
->c
!= NULL
&& ft
->c
->session
!= NULL
)
1445 return (xstrdup(ft
->c
->session
->name
));
1449 /* Callback for client_termfeatures. */
1451 format_cb_client_termfeatures(struct format_tree
*ft
)
1454 return (xstrdup(tty_get_features(ft
->c
->term_features
)));
1458 /* Callback for client_termname. */
1460 format_cb_client_termname(struct format_tree
*ft
)
1463 return (xstrdup(ft
->c
->term_name
));
1467 /* Callback for client_termtype. */
1469 format_cb_client_termtype(struct format_tree
*ft
)
1471 if (ft
->c
!= NULL
) {
1472 if (ft
->c
->term_type
== NULL
)
1473 return (xstrdup(""));
1474 return (xstrdup(ft
->c
->term_type
));
1479 /* Callback for client_tty. */
1481 format_cb_client_tty(struct format_tree
*ft
)
1484 return (xstrdup(ft
->c
->ttyname
));
1488 /* Callback for client_uid. */
1490 format_cb_client_uid(struct format_tree
*ft
)
1494 if (ft
->c
!= NULL
) {
1495 uid
= proc_get_peer_uid(ft
->c
->peer
);
1496 if (uid
!= (uid_t
)-1)
1497 return (format_printf("%ld", (long)uid
));
1502 /* Callback for client_user. */
1504 format_cb_client_user(struct format_tree
*ft
)
1509 if (ft
->c
!= NULL
) {
1510 uid
= proc_get_peer_uid(ft
->c
->peer
);
1511 if (uid
!= (uid_t
)-1 && (pw
= getpwuid(uid
)) != NULL
)
1512 return (xstrdup(pw
->pw_name
));
1517 /* Callback for client_utf8. */
1519 format_cb_client_utf8(struct format_tree
*ft
)
1521 if (ft
->c
!= NULL
) {
1522 if (ft
->c
->flags
& CLIENT_UTF8
)
1523 return (xstrdup("1"));
1524 return (xstrdup("0"));
1529 /* Callback for client_width. */
1531 format_cb_client_width(struct format_tree
*ft
)
1534 return (format_printf("%u", ft
->c
->tty
.sx
));
1538 /* Callback for client_written. */
1540 format_cb_client_written(struct format_tree
*ft
)
1543 return (format_printf("%zu", ft
->c
->written
));
1547 /* Callback for config_files. */
1549 format_cb_config_files(__unused
struct format_tree
*ft
)
1556 for (i
= 0; i
< cfg_nfiles
; i
++) {
1557 n
= strlen(cfg_files
[i
]) + 1;
1558 s
= xrealloc(s
, slen
+ n
+ 1);
1559 slen
+= xsnprintf(s
+ slen
, n
+ 1, "%s,", cfg_files
[i
]);
1562 return (xstrdup(""));
1567 /* Callback for cursor_flag. */
1569 format_cb_cursor_flag(struct format_tree
*ft
)
1571 if (ft
->wp
!= NULL
) {
1572 if (ft
->wp
->base
.mode
& MODE_CURSOR
)
1573 return (xstrdup("1"));
1574 return (xstrdup("0"));
1579 /* Callback for cursor_x. */
1581 format_cb_cursor_x(struct format_tree
*ft
)
1584 return (format_printf("%u", ft
->wp
->base
.cx
));
1588 /* Callback for cursor_y. */
1590 format_cb_cursor_y(struct format_tree
*ft
)
1593 return (format_printf("%u", ft
->wp
->base
.cy
));
1597 /* Callback for history_limit. */
1599 format_cb_history_limit(struct format_tree
*ft
)
1602 return (format_printf("%u", ft
->wp
->base
.grid
->hlimit
));
1606 /* Callback for history_size. */
1608 format_cb_history_size(struct format_tree
*ft
)
1611 return (format_printf("%u", ft
->wp
->base
.grid
->hsize
));
1615 /* Callback for insert_flag. */
1617 format_cb_insert_flag(struct format_tree
*ft
)
1619 if (ft
->wp
!= NULL
) {
1620 if (ft
->wp
->base
.mode
& MODE_INSERT
)
1621 return (xstrdup("1"));
1622 return (xstrdup("0"));
1627 /* Callback for keypad_cursor_flag. */
1629 format_cb_keypad_cursor_flag(struct format_tree
*ft
)
1631 if (ft
->wp
!= NULL
) {
1632 if (ft
->wp
->base
.mode
& MODE_KCURSOR
)
1633 return (xstrdup("1"));
1634 return (xstrdup("0"));
1639 /* Callback for keypad_flag. */
1641 format_cb_keypad_flag(struct format_tree
*ft
)
1643 if (ft
->wp
!= NULL
) {
1644 if (ft
->wp
->base
.mode
& MODE_KKEYPAD
)
1645 return (xstrdup("1"));
1646 return (xstrdup("0"));
1651 /* Callback for mouse_all_flag. */
1653 format_cb_mouse_all_flag(struct format_tree
*ft
)
1655 if (ft
->wp
!= NULL
) {
1656 if (ft
->wp
->base
.mode
& MODE_MOUSE_ALL
)
1657 return (xstrdup("1"));
1658 return (xstrdup("0"));
1663 /* Callback for mouse_any_flag. */
1665 format_cb_mouse_any_flag(struct format_tree
*ft
)
1667 if (ft
->wp
!= NULL
) {
1668 if (ft
->wp
->base
.mode
& ALL_MOUSE_MODES
)
1669 return (xstrdup("1"));
1670 return (xstrdup("0"));
1675 /* Callback for mouse_button_flag. */
1677 format_cb_mouse_button_flag(struct format_tree
*ft
)
1679 if (ft
->wp
!= NULL
) {
1680 if (ft
->wp
->base
.mode
& MODE_MOUSE_BUTTON
)
1681 return (xstrdup("1"));
1682 return (xstrdup("0"));
1687 /* Callback for mouse_pane. */
1689 format_cb_mouse_pane(struct format_tree
*ft
)
1691 struct window_pane
*wp
;
1694 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1696 return (format_printf("%%%u", wp
->id
));
1702 /* Callback for mouse_sgr_flag. */
1704 format_cb_mouse_sgr_flag(struct format_tree
*ft
)
1706 if (ft
->wp
!= NULL
) {
1707 if (ft
->wp
->base
.mode
& MODE_MOUSE_SGR
)
1708 return (xstrdup("1"));
1709 return (xstrdup("0"));
1714 /* Callback for mouse_standard_flag. */
1716 format_cb_mouse_standard_flag(struct format_tree
*ft
)
1718 if (ft
->wp
!= NULL
) {
1719 if (ft
->wp
->base
.mode
& MODE_MOUSE_STANDARD
)
1720 return (xstrdup("1"));
1721 return (xstrdup("0"));
1726 /* Callback for mouse_utf8_flag. */
1728 format_cb_mouse_utf8_flag(struct format_tree
*ft
)
1730 if (ft
->wp
!= NULL
) {
1731 if (ft
->wp
->base
.mode
& MODE_MOUSE_UTF8
)
1732 return (xstrdup("1"));
1733 return (xstrdup("0"));
1738 /* Callback for mouse_x. */
1740 format_cb_mouse_x(struct format_tree
*ft
)
1742 struct window_pane
*wp
;
1747 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1748 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1749 return (format_printf("%u", x
));
1750 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1751 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1752 return (format_printf("%u", ft
->m
.x
));
1753 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1754 return (format_printf("%u", ft
->m
.x
));
1759 /* Callback for mouse_y. */
1761 format_cb_mouse_y(struct format_tree
*ft
)
1763 struct window_pane
*wp
;
1768 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1769 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1770 return (format_printf("%u", y
));
1771 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1772 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1773 return (format_printf("%u", ft
->m
.y
));
1774 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1775 return (format_printf("%u", ft
->m
.y
- ft
->m
.statusat
));
1780 /* Callback for next_session_id. */
1782 format_cb_next_session_id(__unused
struct format_tree
*ft
)
1784 return (format_printf("$%u", next_session_id
));
1787 /* Callback for origin_flag. */
1789 format_cb_origin_flag(struct format_tree
*ft
)
1791 if (ft
->wp
!= NULL
) {
1792 if (ft
->wp
->base
.mode
& MODE_ORIGIN
)
1793 return (xstrdup("1"));
1794 return (xstrdup("0"));
1799 /* Callback for pane_active. */
1801 format_cb_pane_active(struct format_tree
*ft
)
1803 if (ft
->wp
!= NULL
) {
1804 if (ft
->wp
== ft
->wp
->window
->active
)
1805 return (xstrdup("1"));
1806 return (xstrdup("0"));
1811 /* Callback for pane_at_left. */
1813 format_cb_pane_at_left(struct format_tree
*ft
)
1815 if (ft
->wp
!= NULL
) {
1816 if (ft
->wp
->xoff
== 0)
1817 return (xstrdup("1"));
1818 return (xstrdup("0"));
1823 /* Callback for pane_at_right. */
1825 format_cb_pane_at_right(struct format_tree
*ft
)
1827 if (ft
->wp
!= NULL
) {
1828 if (ft
->wp
->xoff
+ ft
->wp
->sx
== ft
->wp
->window
->sx
)
1829 return (xstrdup("1"));
1830 return (xstrdup("0"));
1835 /* Callback for pane_bottom. */
1837 format_cb_pane_bottom(struct format_tree
*ft
)
1840 return (format_printf("%u", ft
->wp
->yoff
+ ft
->wp
->sy
- 1));
1844 /* Callback for pane_dead. */
1846 format_cb_pane_dead(struct format_tree
*ft
)
1848 if (ft
->wp
!= NULL
) {
1849 if (ft
->wp
->fd
== -1)
1850 return (xstrdup("1"));
1851 return (xstrdup("0"));
1856 /* Callback for pane_dead_signal. */
1858 format_cb_pane_dead_signal(struct format_tree
*ft
)
1860 struct window_pane
*wp
= ft
->wp
;
1864 if ((wp
->flags
& PANE_STATUSREADY
) && WIFSIGNALED(wp
->status
)) {
1865 name
= sig2name(WTERMSIG(wp
->status
));
1866 return (format_printf("%s", name
));
1873 /* Callback for pane_dead_status. */
1875 format_cb_pane_dead_status(struct format_tree
*ft
)
1877 struct window_pane
*wp
= ft
->wp
;
1880 if ((wp
->flags
& PANE_STATUSREADY
) && WIFEXITED(wp
->status
))
1881 return (format_printf("%d", WEXITSTATUS(wp
->status
)));
1887 /* Callback for pane_dead_time. */
1889 format_cb_pane_dead_time(struct format_tree
*ft
)
1891 struct window_pane
*wp
= ft
->wp
;
1894 if (wp
->flags
& PANE_STATUSDRAWN
)
1895 return (&wp
->dead_time
);
1901 /* Callback for pane_format. */
1903 format_cb_pane_format(struct format_tree
*ft
)
1905 if (ft
->type
== FORMAT_TYPE_PANE
)
1906 return (xstrdup("1"));
1907 return (xstrdup("0"));
1910 /* Callback for pane_height. */
1912 format_cb_pane_height(struct format_tree
*ft
)
1915 return (format_printf("%u", ft
->wp
->sy
));
1919 /* Callback for pane_id. */
1921 format_cb_pane_id(struct format_tree
*ft
)
1924 return (format_printf("%%%u", ft
->wp
->id
));
1928 /* Callback for pane_index. */
1930 format_cb_pane_index(struct format_tree
*ft
)
1934 if (ft
->wp
!= NULL
&& window_pane_index(ft
->wp
, &idx
) == 0)
1935 return (format_printf("%u", idx
));
1939 /* Callback for pane_input_off. */
1941 format_cb_pane_input_off(struct format_tree
*ft
)
1943 if (ft
->wp
!= NULL
) {
1944 if (ft
->wp
->flags
& PANE_INPUTOFF
)
1945 return (xstrdup("1"));
1946 return (xstrdup("0"));
1951 /* Callback for pane_unseen_changes. */
1953 format_cb_pane_unseen_changes(struct format_tree
*ft
)
1955 if (ft
->wp
!= NULL
) {
1956 if (ft
->wp
->flags
& PANE_UNSEENCHANGES
)
1957 return (xstrdup("1"));
1958 return (xstrdup("0"));
1963 /* Callback for pane_key_mode. */
1965 format_cb_pane_key_mode(struct format_tree
*ft
)
1967 if (ft
->wp
!= NULL
&& ft
->wp
->screen
!= NULL
) {
1968 switch (ft
->wp
->screen
->mode
& EXTENDED_KEY_MODES
) {
1969 case MODE_KEYS_EXTENDED
:
1970 return (xstrdup("Ext 1"));
1971 case MODE_KEYS_EXTENDED_2
:
1972 return (xstrdup("Ext 2"));
1974 return (xstrdup("VT10x"));
1980 /* Callback for pane_last. */
1982 format_cb_pane_last(struct format_tree
*ft
)
1984 if (ft
->wp
!= NULL
) {
1985 if (ft
->wp
== TAILQ_FIRST(&ft
->wp
->window
->last_panes
))
1986 return (xstrdup("1"));
1987 return (xstrdup("0"));
1992 /* Callback for pane_left. */
1994 format_cb_pane_left(struct format_tree
*ft
)
1997 return (format_printf("%u", ft
->wp
->xoff
));
2001 /* Callback for pane_marked. */
2003 format_cb_pane_marked(struct format_tree
*ft
)
2005 if (ft
->wp
!= NULL
) {
2006 if (server_check_marked() && marked_pane
.wp
== ft
->wp
)
2007 return (xstrdup("1"));
2008 return (xstrdup("0"));
2013 /* Callback for pane_marked_set. */
2015 format_cb_pane_marked_set(struct format_tree
*ft
)
2017 if (ft
->wp
!= NULL
) {
2018 if (server_check_marked())
2019 return (xstrdup("1"));
2020 return (xstrdup("0"));
2025 /* Callback for pane_mode. */
2027 format_cb_pane_mode(struct format_tree
*ft
)
2029 struct window_mode_entry
*wme
;
2031 if (ft
->wp
!= NULL
) {
2032 wme
= TAILQ_FIRST(&ft
->wp
->modes
);
2034 return (xstrdup(wme
->mode
->name
));
2040 /* Callback for pane_path. */
2042 format_cb_pane_path(struct format_tree
*ft
)
2044 if (ft
->wp
!= NULL
) {
2045 if (ft
->wp
->base
.path
== NULL
)
2046 return (xstrdup(""));
2047 return (xstrdup(ft
->wp
->base
.path
));
2052 /* Callback for pane_pid. */
2054 format_cb_pane_pid(struct format_tree
*ft
)
2057 return (format_printf("%ld", (long)ft
->wp
->pid
));
2061 /* Callback for pane_pipe. */
2063 format_cb_pane_pipe(struct format_tree
*ft
)
2065 if (ft
->wp
!= NULL
) {
2066 if (ft
->wp
->pipe_fd
!= -1)
2067 return (xstrdup("1"));
2068 return (xstrdup("0"));
2073 /* Callback for pane_right. */
2075 format_cb_pane_right(struct format_tree
*ft
)
2078 return (format_printf("%u", ft
->wp
->xoff
+ ft
->wp
->sx
- 1));
2082 /* Callback for pane_search_string. */
2084 format_cb_pane_search_string(struct format_tree
*ft
)
2086 if (ft
->wp
!= NULL
) {
2087 if (ft
->wp
->searchstr
== NULL
)
2088 return (xstrdup(""));
2089 return (xstrdup(ft
->wp
->searchstr
));
2094 /* Callback for pane_synchronized. */
2096 format_cb_pane_synchronized(struct format_tree
*ft
)
2098 if (ft
->wp
!= NULL
) {
2099 if (options_get_number(ft
->wp
->options
, "synchronize-panes"))
2100 return (xstrdup("1"));
2101 return (xstrdup("0"));
2106 /* Callback for pane_title. */
2108 format_cb_pane_title(struct format_tree
*ft
)
2111 return (xstrdup(ft
->wp
->base
.title
));
2115 /* Callback for pane_top. */
2117 format_cb_pane_top(struct format_tree
*ft
)
2120 return (format_printf("%u", ft
->wp
->yoff
));
2124 /* Callback for pane_tty. */
2126 format_cb_pane_tty(struct format_tree
*ft
)
2129 return (xstrdup(ft
->wp
->tty
));
2133 /* Callback for pane_width. */
2135 format_cb_pane_width(struct format_tree
*ft
)
2138 return (format_printf("%u", ft
->wp
->sx
));
2142 /* Callback for scroll_region_lower. */
2144 format_cb_scroll_region_lower(struct format_tree
*ft
)
2147 return (format_printf("%u", ft
->wp
->base
.rlower
));
2151 /* Callback for scroll_region_upper. */
2153 format_cb_scroll_region_upper(struct format_tree
*ft
)
2156 return (format_printf("%u", ft
->wp
->base
.rupper
));
2160 /* Callback for server_sessions. */
2162 format_cb_server_sessions(__unused
struct format_tree
*ft
)
2167 RB_FOREACH(s
, sessions
, &sessions
)
2169 return (format_printf("%u", n
));
2172 /* Callback for session_attached. */
2174 format_cb_session_attached(struct format_tree
*ft
)
2177 return (format_printf("%u", ft
->s
->attached
));
2181 /* Callback for session_format. */
2183 format_cb_session_format(struct format_tree
*ft
)
2185 if (ft
->type
== FORMAT_TYPE_SESSION
)
2186 return (xstrdup("1"));
2187 return (xstrdup("0"));
2190 /* Callback for session_group. */
2192 format_cb_session_group(struct format_tree
*ft
)
2194 struct session_group
*sg
;
2196 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2197 return (xstrdup(sg
->name
));
2201 /* Callback for session_group_attached. */
2203 format_cb_session_group_attached(struct format_tree
*ft
)
2205 struct session_group
*sg
;
2207 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2208 return (format_printf("%u", session_group_attached_count (sg
)));
2212 /* Callback for session_group_many_attached. */
2214 format_cb_session_group_many_attached(struct format_tree
*ft
)
2216 struct session_group
*sg
;
2218 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
) {
2219 if (session_group_attached_count (sg
) > 1)
2220 return (xstrdup("1"));
2221 return (xstrdup("0"));
2226 /* Callback for session_group_size. */
2228 format_cb_session_group_size(struct format_tree
*ft
)
2230 struct session_group
*sg
;
2232 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2233 return (format_printf("%u", session_group_count (sg
)));
2237 /* Callback for session_grouped. */
2239 format_cb_session_grouped(struct format_tree
*ft
)
2241 if (ft
->s
!= NULL
) {
2242 if (session_group_contains(ft
->s
) != NULL
)
2243 return (xstrdup("1"));
2244 return (xstrdup("0"));
2249 /* Callback for session_id. */
2251 format_cb_session_id(struct format_tree
*ft
)
2254 return (format_printf("$%u", ft
->s
->id
));
2258 /* Callback for session_many_attached. */
2260 format_cb_session_many_attached(struct format_tree
*ft
)
2262 if (ft
->s
!= NULL
) {
2263 if (ft
->s
->attached
> 1)
2264 return (xstrdup("1"));
2265 return (xstrdup("0"));
2270 /* Callback for session_marked. */
2272 format_cb_session_marked(struct format_tree
*ft
)
2274 if (ft
->s
!= NULL
) {
2275 if (server_check_marked() && marked_pane
.s
== ft
->s
)
2276 return (xstrdup("1"));
2277 return (xstrdup("0"));
2282 /* Callback for session_name. */
2284 format_cb_session_name(struct format_tree
*ft
)
2287 return (xstrdup(ft
->s
->name
));
2291 /* Callback for session_path. */
2293 format_cb_session_path(struct format_tree
*ft
)
2296 return (xstrdup(ft
->s
->cwd
));
2300 /* Callback for session_windows. */
2302 format_cb_session_windows(struct format_tree
*ft
)
2305 return (format_printf("%u", winlink_count(&ft
->s
->windows
)));
2309 /* Callback for socket_path. */
2311 format_cb_socket_path(__unused
struct format_tree
*ft
)
2313 return (xstrdup(socket_path
));
2316 /* Callback for version. */
2318 format_cb_version(__unused
struct format_tree
*ft
)
2320 return (xstrdup(getversion()));
2323 /* Callback for active_window_index. */
2325 format_cb_active_window_index(struct format_tree
*ft
)
2328 return (format_printf("%u", ft
->s
->curw
->idx
));
2332 /* Callback for last_window_index. */
2334 format_cb_last_window_index(struct format_tree
*ft
)
2338 if (ft
->s
!= NULL
) {
2339 wl
= RB_MAX(winlinks
, &ft
->s
->windows
);
2340 return (format_printf("%u", wl
->idx
));
2345 /* Callback for window_active. */
2347 format_cb_window_active(struct format_tree
*ft
)
2349 if (ft
->wl
!= NULL
) {
2350 if (ft
->wl
== ft
->wl
->session
->curw
)
2351 return (xstrdup("1"));
2352 return (xstrdup("0"));
2357 /* Callback for window_activity_flag. */
2359 format_cb_window_activity_flag(struct format_tree
*ft
)
2361 if (ft
->wl
!= NULL
) {
2362 if (ft
->wl
->flags
& WINLINK_ACTIVITY
)
2363 return (xstrdup("1"));
2364 return (xstrdup("0"));
2369 /* Callback for window_bell_flag. */
2371 format_cb_window_bell_flag(struct format_tree
*ft
)
2373 if (ft
->wl
!= NULL
) {
2374 if (ft
->wl
->flags
& WINLINK_BELL
)
2375 return (xstrdup("1"));
2376 return (xstrdup("0"));
2381 /* Callback for window_bigger. */
2383 format_cb_window_bigger(struct format_tree
*ft
)
2385 u_int ox
, oy
, sx
, sy
;
2387 if (ft
->c
!= NULL
) {
2388 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2389 return (xstrdup("1"));
2390 return (xstrdup("0"));
2395 /* Callback for window_cell_height. */
2397 format_cb_window_cell_height(struct format_tree
*ft
)
2400 return (format_printf("%u", ft
->w
->ypixel
));
2404 /* Callback for window_cell_width. */
2406 format_cb_window_cell_width(struct format_tree
*ft
)
2409 return (format_printf("%u", ft
->w
->xpixel
));
2413 /* Callback for window_end_flag. */
2415 format_cb_window_end_flag(struct format_tree
*ft
)
2417 if (ft
->wl
!= NULL
) {
2418 if (ft
->wl
== RB_MAX(winlinks
, &ft
->wl
->session
->windows
))
2419 return (xstrdup("1"));
2420 return (xstrdup("0"));
2425 /* Callback for window_flags. */
2427 format_cb_window_flags(struct format_tree
*ft
)
2430 return (xstrdup(window_printable_flags(ft
->wl
, 1)));
2434 /* Callback for window_format. */
2436 format_cb_window_format(struct format_tree
*ft
)
2438 if (ft
->type
== FORMAT_TYPE_WINDOW
)
2439 return (xstrdup("1"));
2440 return (xstrdup("0"));
2443 /* Callback for window_height. */
2445 format_cb_window_height(struct format_tree
*ft
)
2448 return (format_printf("%u", ft
->w
->sy
));
2452 /* Callback for window_id. */
2454 format_cb_window_id(struct format_tree
*ft
)
2457 return (format_printf("@%u", ft
->w
->id
));
2461 /* Callback for window_index. */
2463 format_cb_window_index(struct format_tree
*ft
)
2466 return (format_printf("%d", ft
->wl
->idx
));
2470 /* Callback for window_last_flag. */
2472 format_cb_window_last_flag(struct format_tree
*ft
)
2474 if (ft
->wl
!= NULL
) {
2475 if (ft
->wl
== TAILQ_FIRST(&ft
->wl
->session
->lastw
))
2476 return (xstrdup("1"));
2477 return (xstrdup("0"));
2482 /* Callback for window_linked. */
2484 format_cb_window_linked(struct format_tree
*ft
)
2486 if (ft
->wl
!= NULL
) {
2487 if (session_is_linked(ft
->wl
->session
, ft
->wl
->window
))
2488 return (xstrdup("1"));
2489 return (xstrdup("0"));
2494 /* Callback for window_linked_sessions. */
2496 format_cb_window_linked_sessions(struct format_tree
*ft
)
2499 return (format_printf("%u", ft
->wl
->window
->references
));
2503 /* Callback for window_marked_flag. */
2505 format_cb_window_marked_flag(struct format_tree
*ft
)
2507 if (ft
->wl
!= NULL
) {
2508 if (server_check_marked() && marked_pane
.wl
== ft
->wl
)
2509 return (xstrdup("1"));
2510 return (xstrdup("0"));
2515 /* Callback for window_name. */
2517 format_cb_window_name(struct format_tree
*ft
)
2520 return (format_printf("%s", ft
->w
->name
));
2524 /* Callback for window_offset_x. */
2526 format_cb_window_offset_x(struct format_tree
*ft
)
2528 u_int ox
, oy
, sx
, sy
;
2530 if (ft
->c
!= NULL
) {
2531 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2532 return (format_printf("%u", ox
));
2538 /* Callback for window_offset_y. */
2540 format_cb_window_offset_y(struct format_tree
*ft
)
2542 u_int ox
, oy
, sx
, sy
;
2544 if (ft
->c
!= NULL
) {
2545 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2546 return (format_printf("%u", oy
));
2552 /* Callback for window_panes. */
2554 format_cb_window_panes(struct format_tree
*ft
)
2557 return (format_printf("%u", window_count_panes(ft
->w
)));
2561 /* Callback for window_raw_flags. */
2563 format_cb_window_raw_flags(struct format_tree
*ft
)
2566 return (xstrdup(window_printable_flags(ft
->wl
, 0)));
2570 /* Callback for window_silence_flag. */
2572 format_cb_window_silence_flag(struct format_tree
*ft
)
2574 if (ft
->wl
!= NULL
) {
2575 if (ft
->wl
->flags
& WINLINK_SILENCE
)
2576 return (xstrdup("1"));
2577 return (xstrdup("0"));
2582 /* Callback for window_start_flag. */
2584 format_cb_window_start_flag(struct format_tree
*ft
)
2586 if (ft
->wl
!= NULL
) {
2587 if (ft
->wl
== RB_MIN(winlinks
, &ft
->wl
->session
->windows
))
2588 return (xstrdup("1"));
2589 return (xstrdup("0"));
2594 /* Callback for window_width. */
2596 format_cb_window_width(struct format_tree
*ft
)
2599 return (format_printf("%u", ft
->w
->sx
));
2603 /* Callback for window_zoomed_flag. */
2605 format_cb_window_zoomed_flag(struct format_tree
*ft
)
2607 if (ft
->w
!= NULL
) {
2608 if (ft
->w
->flags
& WINDOW_ZOOMED
)
2609 return (xstrdup("1"));
2610 return (xstrdup("0"));
2615 /* Callback for wrap_flag. */
2617 format_cb_wrap_flag(struct format_tree
*ft
)
2619 if (ft
->wp
!= NULL
) {
2620 if (ft
->wp
->base
.mode
& MODE_WRAP
)
2621 return (xstrdup("1"));
2622 return (xstrdup("0"));
2627 /* Callback for buffer_created. */
2629 format_cb_buffer_created(struct format_tree
*ft
)
2631 static struct timeval tv
;
2633 if (ft
->pb
!= NULL
) {
2635 tv
.tv_sec
= paste_buffer_created(ft
->pb
);
2641 /* Callback for client_activity. */
2643 format_cb_client_activity(struct format_tree
*ft
)
2646 return (&ft
->c
->activity_time
);
2650 /* Callback for client_created. */
2652 format_cb_client_created(struct format_tree
*ft
)
2655 return (&ft
->c
->creation_time
);
2659 /* Callback for session_activity. */
2661 format_cb_session_activity(struct format_tree
*ft
)
2664 return (&ft
->s
->activity_time
);
2668 /* Callback for session_created. */
2670 format_cb_session_created(struct format_tree
*ft
)
2673 return (&ft
->s
->creation_time
);
2677 /* Callback for session_last_attached. */
2679 format_cb_session_last_attached(struct format_tree
*ft
)
2682 return (&ft
->s
->last_attached_time
);
2686 /* Callback for start_time. */
2688 format_cb_start_time(__unused
struct format_tree
*ft
)
2690 return (&start_time
);
2693 /* Callback for window_activity. */
2695 format_cb_window_activity(struct format_tree
*ft
)
2698 return (&ft
->w
->activity_time
);
2702 /* Callback for buffer_mode_format, */
2704 format_cb_buffer_mode_format(__unused
struct format_tree
*ft
)
2706 return (xstrdup(window_buffer_mode
.default_format
));
2709 /* Callback for client_mode_format, */
2711 format_cb_client_mode_format(__unused
struct format_tree
*ft
)
2713 return (xstrdup(window_client_mode
.default_format
));
2716 /* Callback for tree_mode_format, */
2718 format_cb_tree_mode_format(__unused
struct format_tree
*ft
)
2720 return (xstrdup(window_tree_mode
.default_format
));
2723 /* Callback for uid. */
2725 format_cb_uid(__unused
struct format_tree
*ft
)
2727 return (format_printf("%ld", (long)getuid()));
2730 /* Callback for user. */
2732 format_cb_user(__unused
struct format_tree
*ft
)
2736 if ((pw
= getpwuid(getuid())) != NULL
)
2737 return (xstrdup(pw
->pw_name
));
2741 /* Format table type. */
2742 enum format_table_type
{
2743 FORMAT_TABLE_STRING
,
2747 /* Format table entry. */
2748 struct format_table_entry
{
2750 enum format_table_type type
;
2755 * Format table. Default format variables (that are almost always in the tree
2756 * and where the value is expanded by a callback in this file) are listed here.
2757 * Only variables which are added by the caller go into the tree.
2759 static const struct format_table_entry format_table
[] = {
2760 { "active_window_index", FORMAT_TABLE_STRING
,
2761 format_cb_active_window_index
2763 { "alternate_on", FORMAT_TABLE_STRING
,
2764 format_cb_alternate_on
2766 { "alternate_saved_x", FORMAT_TABLE_STRING
,
2767 format_cb_alternate_saved_x
2769 { "alternate_saved_y", FORMAT_TABLE_STRING
,
2770 format_cb_alternate_saved_y
2772 { "buffer_created", FORMAT_TABLE_TIME
,
2773 format_cb_buffer_created
2775 { "buffer_mode_format", FORMAT_TABLE_STRING
,
2776 format_cb_buffer_mode_format
2778 { "buffer_name", FORMAT_TABLE_STRING
,
2779 format_cb_buffer_name
2781 { "buffer_sample", FORMAT_TABLE_STRING
,
2782 format_cb_buffer_sample
2784 { "buffer_size", FORMAT_TABLE_STRING
,
2785 format_cb_buffer_size
2787 { "client_activity", FORMAT_TABLE_TIME
,
2788 format_cb_client_activity
2790 { "client_cell_height", FORMAT_TABLE_STRING
,
2791 format_cb_client_cell_height
2793 { "client_cell_width", FORMAT_TABLE_STRING
,
2794 format_cb_client_cell_width
2796 { "client_control_mode", FORMAT_TABLE_STRING
,
2797 format_cb_client_control_mode
2799 { "client_created", FORMAT_TABLE_TIME
,
2800 format_cb_client_created
2802 { "client_discarded", FORMAT_TABLE_STRING
,
2803 format_cb_client_discarded
2805 { "client_flags", FORMAT_TABLE_STRING
,
2806 format_cb_client_flags
2808 { "client_height", FORMAT_TABLE_STRING
,
2809 format_cb_client_height
2811 { "client_key_table", FORMAT_TABLE_STRING
,
2812 format_cb_client_key_table
2814 { "client_last_session", FORMAT_TABLE_STRING
,
2815 format_cb_client_last_session
2817 { "client_mode_format", FORMAT_TABLE_STRING
,
2818 format_cb_client_mode_format
2820 { "client_name", FORMAT_TABLE_STRING
,
2821 format_cb_client_name
2823 { "client_pid", FORMAT_TABLE_STRING
,
2824 format_cb_client_pid
2826 { "client_prefix", FORMAT_TABLE_STRING
,
2827 format_cb_client_prefix
2829 { "client_readonly", FORMAT_TABLE_STRING
,
2830 format_cb_client_readonly
2832 { "client_session", FORMAT_TABLE_STRING
,
2833 format_cb_client_session
2835 { "client_termfeatures", FORMAT_TABLE_STRING
,
2836 format_cb_client_termfeatures
2838 { "client_termname", FORMAT_TABLE_STRING
,
2839 format_cb_client_termname
2841 { "client_termtype", FORMAT_TABLE_STRING
,
2842 format_cb_client_termtype
2844 { "client_tty", FORMAT_TABLE_STRING
,
2845 format_cb_client_tty
2847 { "client_uid", FORMAT_TABLE_STRING
,
2848 format_cb_client_uid
2850 { "client_user", FORMAT_TABLE_STRING
,
2851 format_cb_client_user
2853 { "client_utf8", FORMAT_TABLE_STRING
,
2854 format_cb_client_utf8
2856 { "client_width", FORMAT_TABLE_STRING
,
2857 format_cb_client_width
2859 { "client_written", FORMAT_TABLE_STRING
,
2860 format_cb_client_written
2862 { "config_files", FORMAT_TABLE_STRING
,
2863 format_cb_config_files
2865 { "cursor_character", FORMAT_TABLE_STRING
,
2866 format_cb_cursor_character
2868 { "cursor_flag", FORMAT_TABLE_STRING
,
2869 format_cb_cursor_flag
2871 { "cursor_x", FORMAT_TABLE_STRING
,
2874 { "cursor_y", FORMAT_TABLE_STRING
,
2877 { "history_all_bytes", FORMAT_TABLE_STRING
,
2878 format_cb_history_all_bytes
2880 { "history_bytes", FORMAT_TABLE_STRING
,
2881 format_cb_history_bytes
2883 { "history_limit", FORMAT_TABLE_STRING
,
2884 format_cb_history_limit
2886 { "history_size", FORMAT_TABLE_STRING
,
2887 format_cb_history_size
2889 { "host", FORMAT_TABLE_STRING
,
2892 { "host_short", FORMAT_TABLE_STRING
,
2893 format_cb_host_short
2895 { "insert_flag", FORMAT_TABLE_STRING
,
2896 format_cb_insert_flag
2898 { "keypad_cursor_flag", FORMAT_TABLE_STRING
,
2899 format_cb_keypad_cursor_flag
2901 { "keypad_flag", FORMAT_TABLE_STRING
,
2902 format_cb_keypad_flag
2904 { "last_window_index", FORMAT_TABLE_STRING
,
2905 format_cb_last_window_index
2907 { "mouse_all_flag", FORMAT_TABLE_STRING
,
2908 format_cb_mouse_all_flag
2910 { "mouse_any_flag", FORMAT_TABLE_STRING
,
2911 format_cb_mouse_any_flag
2913 { "mouse_button_flag", FORMAT_TABLE_STRING
,
2914 format_cb_mouse_button_flag
2916 { "mouse_hyperlink", FORMAT_TABLE_STRING
,
2917 format_cb_mouse_hyperlink
2919 { "mouse_line", FORMAT_TABLE_STRING
,
2920 format_cb_mouse_line
2922 { "mouse_pane", FORMAT_TABLE_STRING
,
2923 format_cb_mouse_pane
2925 { "mouse_sgr_flag", FORMAT_TABLE_STRING
,
2926 format_cb_mouse_sgr_flag
2928 { "mouse_standard_flag", FORMAT_TABLE_STRING
,
2929 format_cb_mouse_standard_flag
2931 { "mouse_status_line", FORMAT_TABLE_STRING
,
2932 format_cb_mouse_status_line
2934 { "mouse_status_range", FORMAT_TABLE_STRING
,
2935 format_cb_mouse_status_range
2937 { "mouse_utf8_flag", FORMAT_TABLE_STRING
,
2938 format_cb_mouse_utf8_flag
2940 { "mouse_word", FORMAT_TABLE_STRING
,
2941 format_cb_mouse_word
2943 { "mouse_x", FORMAT_TABLE_STRING
,
2946 { "mouse_y", FORMAT_TABLE_STRING
,
2949 { "next_session_id", FORMAT_TABLE_STRING
,
2950 format_cb_next_session_id
2952 { "origin_flag", FORMAT_TABLE_STRING
,
2953 format_cb_origin_flag
2955 { "pane_active", FORMAT_TABLE_STRING
,
2956 format_cb_pane_active
2958 { "pane_at_bottom", FORMAT_TABLE_STRING
,
2959 format_cb_pane_at_bottom
2961 { "pane_at_left", FORMAT_TABLE_STRING
,
2962 format_cb_pane_at_left
2964 { "pane_at_right", FORMAT_TABLE_STRING
,
2965 format_cb_pane_at_right
2967 { "pane_at_top", FORMAT_TABLE_STRING
,
2968 format_cb_pane_at_top
2970 { "pane_bg", FORMAT_TABLE_STRING
,
2973 { "pane_bottom", FORMAT_TABLE_STRING
,
2974 format_cb_pane_bottom
2976 { "pane_current_command", FORMAT_TABLE_STRING
,
2977 format_cb_current_command
2979 { "pane_current_path", FORMAT_TABLE_STRING
,
2980 format_cb_current_path
2982 { "pane_dead", FORMAT_TABLE_STRING
,
2985 { "pane_dead_signal", FORMAT_TABLE_STRING
,
2986 format_cb_pane_dead_signal
2988 { "pane_dead_status", FORMAT_TABLE_STRING
,
2989 format_cb_pane_dead_status
2991 { "pane_dead_time", FORMAT_TABLE_TIME
,
2992 format_cb_pane_dead_time
2994 { "pane_fg", FORMAT_TABLE_STRING
,
2997 { "pane_format", FORMAT_TABLE_STRING
,
2998 format_cb_pane_format
3000 { "pane_height", FORMAT_TABLE_STRING
,
3001 format_cb_pane_height
3003 { "pane_id", FORMAT_TABLE_STRING
,
3006 { "pane_in_mode", FORMAT_TABLE_STRING
,
3007 format_cb_pane_in_mode
3009 { "pane_index", FORMAT_TABLE_STRING
,
3010 format_cb_pane_index
3012 { "pane_input_off", FORMAT_TABLE_STRING
,
3013 format_cb_pane_input_off
3015 { "pane_key_mode", FORMAT_TABLE_STRING
,
3016 format_cb_pane_key_mode
3018 { "pane_last", FORMAT_TABLE_STRING
,
3021 { "pane_left", FORMAT_TABLE_STRING
,
3024 { "pane_marked", FORMAT_TABLE_STRING
,
3025 format_cb_pane_marked
3027 { "pane_marked_set", FORMAT_TABLE_STRING
,
3028 format_cb_pane_marked_set
3030 { "pane_mode", FORMAT_TABLE_STRING
,
3033 { "pane_path", FORMAT_TABLE_STRING
,
3036 { "pane_pid", FORMAT_TABLE_STRING
,
3039 { "pane_pipe", FORMAT_TABLE_STRING
,
3042 { "pane_right", FORMAT_TABLE_STRING
,
3043 format_cb_pane_right
3045 { "pane_search_string", FORMAT_TABLE_STRING
,
3046 format_cb_pane_search_string
3048 { "pane_start_command", FORMAT_TABLE_STRING
,
3049 format_cb_start_command
3051 { "pane_start_path", FORMAT_TABLE_STRING
,
3052 format_cb_start_path
3054 { "pane_synchronized", FORMAT_TABLE_STRING
,
3055 format_cb_pane_synchronized
3057 { "pane_tabs", FORMAT_TABLE_STRING
,
3060 { "pane_title", FORMAT_TABLE_STRING
,
3061 format_cb_pane_title
3063 { "pane_top", FORMAT_TABLE_STRING
,
3066 { "pane_tty", FORMAT_TABLE_STRING
,
3069 { "pane_unseen_changes", FORMAT_TABLE_STRING
,
3070 format_cb_pane_unseen_changes
3072 { "pane_width", FORMAT_TABLE_STRING
,
3073 format_cb_pane_width
3075 { "pid", FORMAT_TABLE_STRING
,
3078 { "scroll_region_lower", FORMAT_TABLE_STRING
,
3079 format_cb_scroll_region_lower
3081 { "scroll_region_upper", FORMAT_TABLE_STRING
,
3082 format_cb_scroll_region_upper
3084 { "server_sessions", FORMAT_TABLE_STRING
,
3085 format_cb_server_sessions
3087 { "session_activity", FORMAT_TABLE_TIME
,
3088 format_cb_session_activity
3090 { "session_alerts", FORMAT_TABLE_STRING
,
3091 format_cb_session_alerts
3093 { "session_attached", FORMAT_TABLE_STRING
,
3094 format_cb_session_attached
3096 { "session_attached_list", FORMAT_TABLE_STRING
,
3097 format_cb_session_attached_list
3099 { "session_created", FORMAT_TABLE_TIME
,
3100 format_cb_session_created
3102 { "session_format", FORMAT_TABLE_STRING
,
3103 format_cb_session_format
3105 { "session_group", FORMAT_TABLE_STRING
,
3106 format_cb_session_group
3108 { "session_group_attached", FORMAT_TABLE_STRING
,
3109 format_cb_session_group_attached
3111 { "session_group_attached_list", FORMAT_TABLE_STRING
,
3112 format_cb_session_group_attached_list
3114 { "session_group_list", FORMAT_TABLE_STRING
,
3115 format_cb_session_group_list
3117 { "session_group_many_attached", FORMAT_TABLE_STRING
,
3118 format_cb_session_group_many_attached
3120 { "session_group_size", FORMAT_TABLE_STRING
,
3121 format_cb_session_group_size
3123 { "session_grouped", FORMAT_TABLE_STRING
,
3124 format_cb_session_grouped
3126 { "session_id", FORMAT_TABLE_STRING
,
3127 format_cb_session_id
3129 { "session_last_attached", FORMAT_TABLE_TIME
,
3130 format_cb_session_last_attached
3132 { "session_many_attached", FORMAT_TABLE_STRING
,
3133 format_cb_session_many_attached
3135 { "session_marked", FORMAT_TABLE_STRING
,
3136 format_cb_session_marked
,
3138 { "session_name", FORMAT_TABLE_STRING
,
3139 format_cb_session_name
3141 { "session_path", FORMAT_TABLE_STRING
,
3142 format_cb_session_path
3144 { "session_stack", FORMAT_TABLE_STRING
,
3145 format_cb_session_stack
3147 { "session_windows", FORMAT_TABLE_STRING
,
3148 format_cb_session_windows
3150 { "socket_path", FORMAT_TABLE_STRING
,
3151 format_cb_socket_path
3153 { "start_time", FORMAT_TABLE_TIME
,
3154 format_cb_start_time
3156 { "tree_mode_format", FORMAT_TABLE_STRING
,
3157 format_cb_tree_mode_format
3159 { "uid", FORMAT_TABLE_STRING
,
3162 { "user", FORMAT_TABLE_STRING
,
3165 { "version", FORMAT_TABLE_STRING
,
3168 { "window_active", FORMAT_TABLE_STRING
,
3169 format_cb_window_active
3171 { "window_active_clients", FORMAT_TABLE_STRING
,
3172 format_cb_window_active_clients
3174 { "window_active_clients_list", FORMAT_TABLE_STRING
,
3175 format_cb_window_active_clients_list
3177 { "window_active_sessions", FORMAT_TABLE_STRING
,
3178 format_cb_window_active_sessions
3180 { "window_active_sessions_list", FORMAT_TABLE_STRING
,
3181 format_cb_window_active_sessions_list
3183 { "window_activity", FORMAT_TABLE_TIME
,
3184 format_cb_window_activity
3186 { "window_activity_flag", FORMAT_TABLE_STRING
,
3187 format_cb_window_activity_flag
3189 { "window_bell_flag", FORMAT_TABLE_STRING
,
3190 format_cb_window_bell_flag
3192 { "window_bigger", FORMAT_TABLE_STRING
,
3193 format_cb_window_bigger
3195 { "window_cell_height", FORMAT_TABLE_STRING
,
3196 format_cb_window_cell_height
3198 { "window_cell_width", FORMAT_TABLE_STRING
,
3199 format_cb_window_cell_width
3201 { "window_end_flag", FORMAT_TABLE_STRING
,
3202 format_cb_window_end_flag
3204 { "window_flags", FORMAT_TABLE_STRING
,
3205 format_cb_window_flags
3207 { "window_format", FORMAT_TABLE_STRING
,
3208 format_cb_window_format
3210 { "window_height", FORMAT_TABLE_STRING
,
3211 format_cb_window_height
3213 { "window_id", FORMAT_TABLE_STRING
,
3216 { "window_index", FORMAT_TABLE_STRING
,
3217 format_cb_window_index
3219 { "window_last_flag", FORMAT_TABLE_STRING
,
3220 format_cb_window_last_flag
3222 { "window_layout", FORMAT_TABLE_STRING
,
3223 format_cb_window_layout
3225 { "window_linked", FORMAT_TABLE_STRING
,
3226 format_cb_window_linked
3228 { "window_linked_sessions", FORMAT_TABLE_STRING
,
3229 format_cb_window_linked_sessions
3231 { "window_linked_sessions_list", FORMAT_TABLE_STRING
,
3232 format_cb_window_linked_sessions_list
3234 { "window_marked_flag", FORMAT_TABLE_STRING
,
3235 format_cb_window_marked_flag
3237 { "window_name", FORMAT_TABLE_STRING
,
3238 format_cb_window_name
3240 { "window_offset_x", FORMAT_TABLE_STRING
,
3241 format_cb_window_offset_x
3243 { "window_offset_y", FORMAT_TABLE_STRING
,
3244 format_cb_window_offset_y
3246 { "window_panes", FORMAT_TABLE_STRING
,
3247 format_cb_window_panes
3249 { "window_raw_flags", FORMAT_TABLE_STRING
,
3250 format_cb_window_raw_flags
3252 { "window_silence_flag", FORMAT_TABLE_STRING
,
3253 format_cb_window_silence_flag
3255 { "window_stack_index", FORMAT_TABLE_STRING
,
3256 format_cb_window_stack_index
3258 { "window_start_flag", FORMAT_TABLE_STRING
,
3259 format_cb_window_start_flag
3261 { "window_visible_layout", FORMAT_TABLE_STRING
,
3262 format_cb_window_visible_layout
3264 { "window_width", FORMAT_TABLE_STRING
,
3265 format_cb_window_width
3267 { "window_zoomed_flag", FORMAT_TABLE_STRING
,
3268 format_cb_window_zoomed_flag
3270 { "wrap_flag", FORMAT_TABLE_STRING
,
3275 /* Compare format table entries. */
3277 format_table_compare(const void *key0
, const void *entry0
)
3279 const char *key
= key0
;
3280 const struct format_table_entry
*entry
= entry0
;
3282 return (strcmp(key
, entry
->key
));
3285 /* Get a format callback. */
3286 static struct format_table_entry
*
3287 format_table_get(const char *key
)
3289 return (bsearch(key
, format_table
, nitems(format_table
),
3290 sizeof *format_table
, format_table_compare
));
3293 /* Merge one format tree into another. */
3295 format_merge(struct format_tree
*ft
, struct format_tree
*from
)
3297 struct format_entry
*fe
;
3299 RB_FOREACH(fe
, format_entry_tree
, &from
->tree
) {
3300 if (fe
->value
!= NULL
)
3301 format_add(ft
, fe
->key
, "%s", fe
->value
);
3305 /* Get format pane. */
3306 struct window_pane
*
3307 format_get_pane(struct format_tree
*ft
)
3312 /* Add item bits to tree. */
3314 format_create_add_item(struct format_tree
*ft
, struct cmdq_item
*item
)
3316 struct key_event
*event
= cmdq_get_event(item
);
3317 struct mouse_event
*m
= &event
->m
;
3319 cmdq_merge_formats(item
, ft
);
3320 memcpy(&ft
->m
, m
, sizeof ft
->m
);
3323 /* Create a new tree. */
3324 struct format_tree
*
3325 format_create(struct client
*c
, struct cmdq_item
*item
, int tag
, int flags
)
3327 struct format_tree
*ft
;
3329 ft
= xcalloc(1, sizeof *ft
);
3334 ft
->client
->references
++;
3342 format_create_add_item(ft
, item
);
3349 format_free(struct format_tree
*ft
)
3351 struct format_entry
*fe
, *fe1
;
3353 RB_FOREACH_SAFE(fe
, format_entry_tree
, &ft
->tree
, fe1
) {
3354 RB_REMOVE(format_entry_tree
, &ft
->tree
, fe
);
3360 if (ft
->client
!= NULL
)
3361 server_client_unref(ft
->client
);
3365 /* Log each format. */
3367 format_log_debug_cb(const char *key
, const char *value
, void *arg
)
3369 const char *prefix
= arg
;
3371 log_debug("%s: %s=%s", prefix
, key
, value
);
3374 /* Log a format tree. */
3376 format_log_debug(struct format_tree
*ft
, const char *prefix
)
3378 format_each(ft
, format_log_debug_cb
, (void *)prefix
);
3381 /* Walk each format. */
3383 format_each(struct format_tree
*ft
, void (*cb
)(const char *, const char *,
3386 const struct format_table_entry
*fte
;
3387 struct format_entry
*fe
;
3393 for (i
= 0; i
< nitems(format_table
); i
++) {
3394 fte
= &format_table
[i
];
3396 value
= fte
->cb(ft
);
3399 if (fte
->type
== FORMAT_TABLE_TIME
) {
3401 xsnprintf(s
, sizeof s
, "%lld", (long long)tv
->tv_sec
);
3402 cb(fte
->key
, s
, arg
);
3404 cb(fte
->key
, value
, arg
);
3408 RB_FOREACH(fe
, format_entry_tree
, &ft
->tree
) {
3409 if (fe
->time
!= 0) {
3410 xsnprintf(s
, sizeof s
, "%lld", (long long)fe
->time
);
3411 cb(fe
->key
, s
, arg
);
3413 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3414 fe
->value
= fe
->cb(ft
);
3415 if (fe
->value
== NULL
)
3416 fe
->value
= xstrdup("");
3418 cb(fe
->key
, fe
->value
, arg
);
3423 /* Add a key-value pair. */
3425 format_add(struct format_tree
*ft
, const char *key
, const char *fmt
, ...)
3427 struct format_entry
*fe
;
3428 struct format_entry
*fe_now
;
3431 fe
= xmalloc(sizeof *fe
);
3432 fe
->key
= xstrdup(key
);
3434 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3435 if (fe_now
!= NULL
) {
3438 free(fe_now
->value
);
3446 xvasprintf(&fe
->value
, fmt
, ap
);
3450 /* Add a key and time. */
3452 format_add_tv(struct format_tree
*ft
, const char *key
, struct timeval
*tv
)
3454 struct format_entry
*fe
, *fe_now
;
3456 fe
= xmalloc(sizeof *fe
);
3457 fe
->key
= xstrdup(key
);
3459 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3460 if (fe_now
!= NULL
) {
3463 free(fe_now
->value
);
3468 fe
->time
= tv
->tv_sec
;
3473 /* Add a key and function. */
3475 format_add_cb(struct format_tree
*ft
, const char *key
, format_cb cb
)
3477 struct format_entry
*fe
;
3478 struct format_entry
*fe_now
;
3480 fe
= xmalloc(sizeof *fe
);
3481 fe
->key
= xstrdup(key
);
3483 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3484 if (fe_now
!= NULL
) {
3487 free(fe_now
->value
);
3497 /* Quote shell special characters in string. */
3499 format_quote_shell(const char *s
)
3504 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3505 for (cp
= s
; *cp
!= '\0'; cp
++) {
3506 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp
) != NULL
)
3514 /* Quote #s in string. */
3516 format_quote_style(const char *s
)
3521 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3522 for (cp
= s
; *cp
!= '\0'; cp
++) {
3531 /* Make a prettier time. */
3533 format_pretty_time(time_t t
, int seconds
)
3535 struct tm now_tm
, tm
;
3544 localtime_r(&now
, &now_tm
);
3545 localtime_r(&t
, &tm
);
3547 /* Last 24 hours. */
3548 if (age
< 24 * 3600) {
3550 strftime(s
, sizeof s
, "%H:%M:%S", &tm
);
3552 strftime(s
, sizeof s
, "%H:%M", &tm
);
3553 return (xstrdup(s
));
3556 /* This month or last 28 days. */
3557 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
== now_tm
.tm_mon
) ||
3558 age
< 28 * 24 * 3600) {
3559 strftime(s
, sizeof s
, "%a%d", &tm
);
3560 return (xstrdup(s
));
3563 /* Last 12 months. */
3564 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
< now_tm
.tm_mon
) ||
3565 (tm
.tm_year
== now_tm
.tm_year
- 1 && tm
.tm_mon
> now_tm
.tm_mon
)) {
3566 strftime(s
, sizeof s
, "%d%b", &tm
);
3567 return (xstrdup(s
));
3570 /* Older than that. */
3571 strftime(s
, sizeof s
, "%h%y", &tm
);
3572 return (xstrdup(s
));
3575 /* Find a format entry. */
3577 format_find(struct format_tree
*ft
, const char *key
, int modifiers
,
3578 const char *time_format
)
3580 struct format_table_entry
*fte
;
3582 struct format_entry
*fe
, fe_find
;
3583 struct environ_entry
*envent
;
3584 struct options_entry
*o
;
3586 char *found
= NULL
, *saved
, s
[512];
3591 o
= options_parse_get(global_options
, key
, &idx
, 0);
3592 if (o
== NULL
&& ft
->wp
!= NULL
)
3593 o
= options_parse_get(ft
->wp
->options
, key
, &idx
, 0);
3594 if (o
== NULL
&& ft
->w
!= NULL
)
3595 o
= options_parse_get(ft
->w
->options
, key
, &idx
, 0);
3597 o
= options_parse_get(global_w_options
, key
, &idx
, 0);
3598 if (o
== NULL
&& ft
->s
!= NULL
)
3599 o
= options_parse_get(ft
->s
->options
, key
, &idx
, 0);
3601 o
= options_parse_get(global_s_options
, key
, &idx
, 0);
3603 found
= options_to_string(o
, idx
, 1);
3607 fte
= format_table_get(key
);
3609 value
= fte
->cb(ft
);
3610 if (fte
->type
== FORMAT_TABLE_TIME
&& value
!= NULL
)
3611 t
= ((struct timeval
*)value
)->tv_sec
;
3616 fe_find
.key
= (char *)key
;
3617 fe
= RB_FIND(format_entry_tree
, &ft
->tree
, &fe_find
);
3619 if (fe
->time
!= 0) {
3623 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3624 fe
->value
= fe
->cb(ft
);
3625 if (fe
->value
== NULL
)
3626 fe
->value
= xstrdup("");
3628 found
= xstrdup(fe
->value
);
3632 if (~modifiers
& FORMAT_TIMESTRING
) {
3635 envent
= environ_find(ft
->s
->environ
, key
);
3637 envent
= environ_find(global_environ
, key
);
3638 if (envent
!= NULL
&& envent
->value
!= NULL
) {
3639 found
= xstrdup(envent
->value
);
3647 if (modifiers
& FORMAT_TIMESTRING
) {
3648 if (t
== 0 && found
!= NULL
) {
3649 t
= strtonum(found
, 0, INT64_MAX
, &errstr
);
3656 if (modifiers
& FORMAT_PRETTY
)
3657 found
= format_pretty_time(t
, 0);
3659 if (time_format
!= NULL
) {
3660 localtime_r(&t
, &tm
);
3661 strftime(s
, sizeof s
, time_format
, &tm
);
3664 s
[strcspn(s
, "\n")] = '\0';
3672 xasprintf(&found
, "%lld", (long long)t
);
3673 else if (found
== NULL
)
3675 if (modifiers
& FORMAT_BASENAME
) {
3677 found
= xstrdup(basename(saved
));
3680 if (modifiers
& FORMAT_DIRNAME
) {
3682 found
= xstrdup(dirname(saved
));
3685 if (modifiers
& FORMAT_QUOTE_SHELL
) {
3687 found
= format_quote_shell(saved
);
3690 if (modifiers
& FORMAT_QUOTE_STYLE
) {
3692 found
= format_quote_style(saved
);
3698 /* Unescape escaped characters. */
3700 format_unescape(const char *s
)
3705 cp
= out
= xmalloc(strlen(s
) + 1);
3706 for (; *s
!= '\0'; s
++) {
3707 if (*s
== '#' && s
[1] == '{')
3709 if (brackets
== 0 &&
3711 strchr(",#{}:", s
[1]) != NULL
) {
3723 /* Remove escaped characters. */
3725 format_strip(const char *s
)
3730 cp
= out
= xmalloc(strlen(s
) + 1);
3731 for (; *s
!= '\0'; s
++) {
3732 if (*s
== '#' && s
[1] == '{')
3734 if (*s
== '#' && strchr(",#{}:", s
[1]) != NULL
) {
3747 /* Skip until end. */
3749 format_skip(const char *s
, const char *end
)
3753 for (; *s
!= '\0'; s
++) {
3754 if (*s
== '#' && s
[1] == '{')
3758 strchr(",#{}:", s
[1]) != NULL
) {
3764 if (strchr(end
, *s
) != NULL
&& brackets
== 0)
3772 /* Return left and right alternatives separated by commas. */
3774 format_choose(struct format_expand_state
*es
, const char *s
, char **left
,
3775 char **right
, int expand
)
3778 char *left0
, *right0
;
3780 cp
= format_skip(s
, ",");
3783 left0
= xstrndup(s
, cp
- s
);
3784 right0
= xstrdup(cp
+ 1);
3787 *left
= format_expand1(es
, left0
);
3789 *right
= format_expand1(es
, right0
);
3800 format_true(const char *s
)
3802 if (s
!= NULL
&& *s
!= '\0' && (s
[0] != '0' || s
[1] != '\0'))
3807 /* Check if modifier end. */
3809 format_is_end(char c
)
3811 return (c
== ';' || c
== ':');
3814 /* Add to modifier list. */
3816 format_add_modifier(struct format_modifier
**list
, u_int
*count
,
3817 const char *c
, size_t n
, char **argv
, int argc
)
3819 struct format_modifier
*fm
;
3821 *list
= xreallocarray(*list
, (*count
) + 1, sizeof **list
);
3822 fm
= &(*list
)[(*count
)++];
3824 memcpy(fm
->modifier
, c
, n
);
3825 fm
->modifier
[n
] = '\0';
3832 /* Free modifier list. */
3834 format_free_modifiers(struct format_modifier
*list
, u_int count
)
3838 for (i
= 0; i
< count
; i
++)
3839 cmd_free_argv(list
[i
].argc
, list
[i
].argv
);
3843 /* Build modifier list. */
3844 static struct format_modifier
*
3845 format_build_modifiers(struct format_expand_state
*es
, const char **s
,
3848 const char *cp
= *s
, *end
;
3849 struct format_modifier
*list
= NULL
;
3850 char c
, last
[] = "X;:", **argv
, *value
;
3854 * Modifiers are a ; separated list of the forms:
3855 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3866 while (*cp
!= '\0' && *cp
!= ':') {
3867 /* Skip any separator character. */
3871 /* Check single character modifiers with no arguments. */
3872 if (strchr("labcdnwETSWPL<>", cp
[0]) != NULL
&&
3873 format_is_end(cp
[1])) {
3874 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3879 /* Then try double character with no arguments. */
3880 if ((memcmp("||", cp
, 2) == 0 ||
3881 memcmp("&&", cp
, 2) == 0 ||
3882 memcmp("!=", cp
, 2) == 0 ||
3883 memcmp("==", cp
, 2) == 0 ||
3884 memcmp("<=", cp
, 2) == 0 ||
3885 memcmp(">=", cp
, 2) == 0) &&
3886 format_is_end(cp
[2])) {
3887 format_add_modifier(&list
, count
, cp
, 2, NULL
, 0);
3892 /* Now try single character with arguments. */
3893 if (strchr("mCNst=peq", cp
[0]) == NULL
)
3897 /* No arguments provided. */
3898 if (format_is_end(cp
[1])) {
3899 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3906 /* Single argument with no wrapper character. */
3907 if (!ispunct((u_char
)cp
[1]) || cp
[1] == '-') {
3908 end
= format_skip(cp
+ 1, ":;");
3912 argv
= xcalloc(1, sizeof *argv
);
3913 value
= xstrndup(cp
+ 1, end
- (cp
+ 1));
3914 argv
[0] = format_expand1(es
, value
);
3918 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3923 /* Multiple arguments with a wrapper character. */
3927 if (cp
[0] == last
[0] && format_is_end(cp
[1])) {
3931 end
= format_skip(cp
+ 1, last
);
3936 argv
= xreallocarray(argv
, argc
+ 1, sizeof *argv
);
3937 value
= xstrndup(cp
, end
- cp
);
3938 argv
[argc
++] = format_expand1(es
, value
);
3942 } while (!format_is_end(cp
[0]));
3943 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3946 format_free_modifiers(list
, *count
);
3954 /* Match against an fnmatch(3) pattern or regular expression. */
3956 format_match(struct format_modifier
*fm
, const char *pattern
, const char *text
)
3964 if (strchr(s
, 'r') == NULL
) {
3965 if (strchr(s
, 'i') != NULL
)
3966 flags
|= FNM_CASEFOLD
;
3967 if (fnmatch(pattern
, text
, flags
) != 0)
3968 return (xstrdup("0"));
3970 flags
= REG_EXTENDED
|REG_NOSUB
;
3971 if (strchr(s
, 'i') != NULL
)
3973 if (regcomp(&r
, pattern
, flags
) != 0)
3974 return (xstrdup("0"));
3975 if (regexec(&r
, text
, 0, NULL
, 0) != 0) {
3977 return (xstrdup("0"));
3981 return (xstrdup("1"));
3984 /* Perform substitution in string. */
3986 format_sub(struct format_modifier
*fm
, const char *text
, const char *pattern
,
3990 int flags
= REG_EXTENDED
;
3992 if (fm
->argc
>= 3 && strchr(fm
->argv
[2], 'i') != NULL
)
3994 value
= regsub(pattern
, with
, text
, flags
);
3996 return (xstrdup(text
));
4000 /* Search inside pane. */
4002 format_search(struct format_modifier
*fm
, struct window_pane
*wp
, const char *s
)
4004 int ignore
= 0, regex
= 0;
4007 if (fm
->argc
>= 1) {
4008 if (strchr(fm
->argv
[0], 'i') != NULL
)
4010 if (strchr(fm
->argv
[0], 'r') != NULL
)
4013 xasprintf(&value
, "%u", window_pane_search(wp
, s
, regex
, ignore
));
4017 /* Does session name exist? */
4019 format_session_name(struct format_expand_state
*es
, const char *fmt
)
4024 name
= format_expand1(es
, fmt
);
4025 RB_FOREACH(s
, sessions
, &sessions
) {
4026 if (strcmp(s
->name
, name
) == 0) {
4028 return (xstrdup("1"));
4032 return (xstrdup("0"));
4035 /* Loop over sessions. */
4037 format_loop_sessions(struct format_expand_state
*es
, const char *fmt
)
4039 struct format_tree
*ft
= es
->ft
;
4040 struct client
*c
= ft
->client
;
4041 struct cmdq_item
*item
= ft
->item
;
4042 struct format_tree
*nft
;
4043 struct format_expand_state next
;
4044 char *expanded
, *value
;
4048 value
= xcalloc(1, 1);
4051 RB_FOREACH(s
, sessions
, &sessions
) {
4052 format_log(es
, "session loop: $%u", s
->id
);
4053 nft
= format_create(c
, item
, FORMAT_NONE
, ft
->flags
);
4054 format_defaults(nft
, ft
->c
, s
, NULL
, NULL
);
4055 format_copy_state(&next
, es
, 0);
4057 expanded
= format_expand1(&next
, fmt
);
4058 format_free(next
.ft
);
4060 valuelen
+= strlen(expanded
);
4061 value
= xrealloc(value
, valuelen
);
4063 strlcat(value
, expanded
, valuelen
);
4070 /* Does window name exist? */
4072 format_window_name(struct format_expand_state
*es
, const char *fmt
)
4074 struct format_tree
*ft
= es
->ft
;
4078 if (ft
->s
== NULL
) {
4079 format_log(es
, "window name but no session");
4083 name
= format_expand1(es
, fmt
);
4084 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
4085 if (strcmp(wl
->window
->name
, name
) == 0) {
4087 return (xstrdup("1"));
4091 return (xstrdup("0"));
4094 /* Loop over windows. */
4096 format_loop_windows(struct format_expand_state
*es
, const char *fmt
)
4098 struct format_tree
*ft
= es
->ft
;
4099 struct client
*c
= ft
->client
;
4100 struct cmdq_item
*item
= ft
->item
;
4101 struct format_tree
*nft
;
4102 struct format_expand_state next
;
4103 char *all
, *active
, *use
, *expanded
, *value
;
4108 if (ft
->s
== NULL
) {
4109 format_log(es
, "window loop but no session");
4113 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4118 value
= xcalloc(1, 1);
4121 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
4123 format_log(es
, "window loop: %u @%u", wl
->idx
, w
->id
);
4124 if (active
!= NULL
&& wl
== ft
->s
->curw
)
4128 nft
= format_create(c
, item
, FORMAT_WINDOW
|w
->id
, ft
->flags
);
4129 format_defaults(nft
, ft
->c
, ft
->s
, wl
, NULL
);
4130 format_copy_state(&next
, es
, 0);
4132 expanded
= format_expand1(&next
, use
);
4135 valuelen
+= strlen(expanded
);
4136 value
= xrealloc(value
, valuelen
);
4138 strlcat(value
, expanded
, valuelen
);
4148 /* Loop over panes. */
4150 format_loop_panes(struct format_expand_state
*es
, const char *fmt
)
4152 struct format_tree
*ft
= es
->ft
;
4153 struct client
*c
= ft
->client
;
4154 struct cmdq_item
*item
= ft
->item
;
4155 struct format_tree
*nft
;
4156 struct format_expand_state next
;
4157 char *all
, *active
, *use
, *expanded
, *value
;
4159 struct window_pane
*wp
;
4161 if (ft
->w
== NULL
) {
4162 format_log(es
, "pane loop but no window");
4166 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4171 value
= xcalloc(1, 1);
4174 TAILQ_FOREACH(wp
, &ft
->w
->panes
, entry
) {
4175 format_log(es
, "pane loop: %%%u", wp
->id
);
4176 if (active
!= NULL
&& wp
== ft
->w
->active
)
4180 nft
= format_create(c
, item
, FORMAT_PANE
|wp
->id
, ft
->flags
);
4181 format_defaults(nft
, ft
->c
, ft
->s
, ft
->wl
, wp
);
4182 format_copy_state(&next
, es
, 0);
4184 expanded
= format_expand1(&next
, use
);
4187 valuelen
+= strlen(expanded
);
4188 value
= xrealloc(value
, valuelen
);
4190 strlcat(value
, expanded
, valuelen
);
4200 /* Loop over clients. */
4202 format_loop_clients(struct format_expand_state
*es
, const char *fmt
)
4204 struct format_tree
*ft
= es
->ft
;
4206 struct cmdq_item
*item
= ft
->item
;
4207 struct format_tree
*nft
;
4208 struct format_expand_state next
;
4209 char *expanded
, *value
;
4212 value
= xcalloc(1, 1);
4215 TAILQ_FOREACH(c
, &clients
, entry
) {
4216 format_log(es
, "client loop: %s", c
->name
);
4217 nft
= format_create(c
, item
, 0, ft
->flags
);
4218 format_defaults(nft
, c
, ft
->s
, ft
->wl
, ft
->wp
);
4219 format_copy_state(&next
, es
, 0);
4221 expanded
= format_expand1(&next
, fmt
);
4224 valuelen
+= strlen(expanded
);
4225 value
= xrealloc(value
, valuelen
);
4227 strlcat(value
, expanded
, valuelen
);
4235 format_replace_expression(struct format_modifier
*mexp
,
4236 struct format_expand_state
*es
, const char *copy
)
4238 int argc
= mexp
->argc
;
4240 char *endch
, *value
, *left
= NULL
, *right
= NULL
;
4243 double mleft
, mright
, result
;
4254 LESS_THAN_EQUAL
} operator;
4256 if (strcmp(mexp
->argv
[0], "+") == 0)
4258 else if (strcmp(mexp
->argv
[0], "-") == 0)
4259 operator = SUBTRACT
;
4260 else if (strcmp(mexp
->argv
[0], "*") == 0)
4261 operator = MULTIPLY
;
4262 else if (strcmp(mexp
->argv
[0], "/") == 0)
4264 else if (strcmp(mexp
->argv
[0], "%") == 0 ||
4265 strcmp(mexp
->argv
[0], "m") == 0)
4267 else if (strcmp(mexp
->argv
[0], "==") == 0)
4269 else if (strcmp(mexp
->argv
[0], "!=") == 0)
4270 operator = NOT_EQUAL
;
4271 else if (strcmp(mexp
->argv
[0], ">") == 0)
4272 operator = GREATER_THAN
;
4273 else if (strcmp(mexp
->argv
[0], "<") == 0)
4274 operator = LESS_THAN
;
4275 else if (strcmp(mexp
->argv
[0], ">=") == 0)
4276 operator = GREATER_THAN_EQUAL
;
4277 else if (strcmp(mexp
->argv
[0], "<=") == 0)
4278 operator = LESS_THAN_EQUAL
;
4280 format_log(es
, "expression has no valid operator: '%s'",
4285 /* The second argument may be flags. */
4286 if (argc
>= 2 && strchr(mexp
->argv
[1], 'f') != NULL
) {
4291 /* The third argument may be precision. */
4293 prec
= strtonum(mexp
->argv
[2], INT_MIN
, INT_MAX
, &errstr
);
4294 if (errstr
!= NULL
) {
4295 format_log(es
, "expression precision %s: %s", errstr
,
4301 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4302 format_log(es
, "expression syntax error");
4306 mleft
= strtod(left
, &endch
);
4307 if (*endch
!= '\0') {
4308 format_log(es
, "expression left side is invalid: %s", left
);
4312 mright
= strtod(right
, &endch
);
4313 if (*endch
!= '\0') {
4314 format_log(es
, "expression right side is invalid: %s", right
);
4319 mleft
= (long long)mleft
;
4320 mright
= (long long)mright
;
4322 format_log(es
, "expression left side is: %.*f", prec
, mleft
);
4323 format_log(es
, "expression right side is: %.*f", prec
, mright
);
4327 result
= mleft
+ mright
;
4330 result
= mleft
- mright
;
4333 result
= mleft
* mright
;
4336 result
= mleft
/ mright
;
4339 result
= fmod(mleft
, mright
);
4342 result
= fabs(mleft
- mright
) < 1e-9;
4345 result
= fabs(mleft
- mright
) > 1e-9;
4348 result
= (mleft
> mright
);
4350 case GREATER_THAN_EQUAL
:
4351 result
= (mleft
>= mright
);
4354 result
= (mleft
< mright
);
4356 case LESS_THAN_EQUAL
:
4357 result
= (mleft
<= mright
);
4361 xasprintf(&value
, "%.*f", prec
, result
);
4363 xasprintf(&value
, "%.*f", prec
, (double)(long long)result
);
4364 format_log(es
, "expression result is %s", value
);
4376 /* Replace a key. */
4378 format_replace(struct format_expand_state
*es
, const char *key
, size_t keylen
,
4379 char **buf
, size_t *len
, size_t *off
)
4381 struct format_tree
*ft
= es
->ft
;
4382 struct window_pane
*wp
= ft
->wp
;
4383 const char *errstr
, *copy
, *cp
, *marker
= NULL
;
4384 const char *time_format
= NULL
;
4385 char *copy0
, *condition
, *found
, *new;
4386 char *value
, *left
, *right
;
4388 int modifiers
= 0, limit
= 0, width
= 0;
4390 struct format_modifier
*list
, *cmp
= NULL
, *search
= NULL
;
4391 struct format_modifier
**sub
= NULL
, *mexp
= NULL
, *fm
;
4392 u_int i
, count
, nsub
= 0;
4393 struct format_expand_state next
;
4395 /* Make a copy of the key. */
4396 copy
= copy0
= xstrndup(key
, keylen
);
4398 /* Process modifier list. */
4399 list
= format_build_modifiers(es
, ©
, &count
);
4400 for (i
= 0; i
< count
; i
++) {
4402 if (format_logging(ft
)) {
4403 format_log(es
, "modifier %u is %s", i
, fm
->modifier
);
4404 for (j
= 0; j
< fm
->argc
; j
++) {
4405 format_log(es
, "modifier %u argument %d: %s", i
,
4409 if (fm
->size
== 1) {
4410 switch (fm
->modifier
[0]) {
4422 sub
= xreallocarray(sub
, nsub
+ 1, sizeof *sub
);
4428 limit
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4432 if (fm
->argc
>= 2 && fm
->argv
[1] != NULL
)
4433 marker
= fm
->argv
[1];
4438 width
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4444 modifiers
|= FORMAT_WIDTH
;
4447 if (fm
->argc
< 1 || fm
->argc
> 3)
4452 modifiers
|= FORMAT_LITERAL
;
4455 modifiers
|= FORMAT_CHARACTER
;
4458 modifiers
|= FORMAT_BASENAME
;
4461 modifiers
|= FORMAT_COLOUR
;
4464 modifiers
|= FORMAT_DIRNAME
;
4467 modifiers
|= FORMAT_LENGTH
;
4470 modifiers
|= FORMAT_TIMESTRING
;
4473 if (strchr(fm
->argv
[0], 'p') != NULL
)
4474 modifiers
|= FORMAT_PRETTY
;
4475 else if (fm
->argc
>= 2 &&
4476 strchr(fm
->argv
[0], 'f') != NULL
)
4477 time_format
= format_strip(fm
->argv
[1]);
4481 modifiers
|= FORMAT_QUOTE_SHELL
;
4482 else if (strchr(fm
->argv
[0], 'e') != NULL
||
4483 strchr(fm
->argv
[0], 'h') != NULL
)
4484 modifiers
|= FORMAT_QUOTE_STYLE
;
4487 modifiers
|= FORMAT_EXPAND
;
4490 modifiers
|= FORMAT_EXPANDTIME
;
4494 strchr(fm
->argv
[0], 'w') != NULL
)
4495 modifiers
|= FORMAT_WINDOW_NAME
;
4496 else if (strchr(fm
->argv
[0], 's') != NULL
)
4497 modifiers
|= FORMAT_SESSION_NAME
;
4500 modifiers
|= FORMAT_SESSIONS
;
4503 modifiers
|= FORMAT_WINDOWS
;
4506 modifiers
|= FORMAT_PANES
;
4509 modifiers
|= FORMAT_CLIENTS
;
4512 } else if (fm
->size
== 2) {
4513 if (strcmp(fm
->modifier
, "||") == 0 ||
4514 strcmp(fm
->modifier
, "&&") == 0 ||
4515 strcmp(fm
->modifier
, "==") == 0 ||
4516 strcmp(fm
->modifier
, "!=") == 0 ||
4517 strcmp(fm
->modifier
, ">=") == 0 ||
4518 strcmp(fm
->modifier
, "<=") == 0)
4523 /* Is this a literal string? */
4524 if (modifiers
& FORMAT_LITERAL
) {
4525 format_log(es
, "literal string is '%s'", copy
);
4526 value
= format_unescape(copy
);
4530 /* Is this a character? */
4531 if (modifiers
& FORMAT_CHARACTER
) {
4532 new = format_expand1(es
, copy
);
4533 c
= strtonum(new, 32, 126, &errstr
);
4535 value
= xstrdup("");
4537 xasprintf(&value
, "%c", c
);
4542 /* Is this a colour? */
4543 if (modifiers
& FORMAT_COLOUR
) {
4544 new = format_expand1(es
, copy
);
4545 c
= colour_fromstring(new);
4546 if (c
== -1 || (c
= colour_force_rgb(c
)) == -1)
4547 value
= xstrdup("");
4549 xasprintf(&value
, "%06x", c
& 0xffffff);
4554 /* Is this a loop, comparison or condition? */
4555 if (modifiers
& FORMAT_SESSIONS
) {
4556 value
= format_loop_sessions(es
, copy
);
4559 } else if (modifiers
& FORMAT_WINDOWS
) {
4560 value
= format_loop_windows(es
, copy
);
4563 } else if (modifiers
& FORMAT_PANES
) {
4564 value
= format_loop_panes(es
, copy
);
4567 } else if (modifiers
& FORMAT_CLIENTS
) {
4568 value
= format_loop_clients(es
, copy
);
4571 } else if (modifiers
& FORMAT_WINDOW_NAME
) {
4572 value
= format_window_name(es
, copy
);
4575 } else if (modifiers
& FORMAT_SESSION_NAME
) {
4576 value
= format_session_name(es
, copy
);
4579 } else if (search
!= NULL
) {
4580 /* Search in pane. */
4581 new = format_expand1(es
, copy
);
4583 format_log(es
, "search '%s' but no pane", new);
4584 value
= xstrdup("0");
4586 format_log(es
, "search '%s' pane %%%u", new, wp
->id
);
4587 value
= format_search(search
, wp
, new);
4590 } else if (cmp
!= NULL
) {
4591 /* Comparison of left and right. */
4592 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4593 format_log(es
, "compare %s syntax error: %s",
4594 cmp
->modifier
, copy
);
4597 format_log(es
, "compare %s left is: %s", cmp
->modifier
, left
);
4598 format_log(es
, "compare %s right is: %s", cmp
->modifier
, right
);
4600 if (strcmp(cmp
->modifier
, "||") == 0) {
4601 if (format_true(left
) || format_true(right
))
4602 value
= xstrdup("1");
4604 value
= xstrdup("0");
4605 } else if (strcmp(cmp
->modifier
, "&&") == 0) {
4606 if (format_true(left
) && format_true(right
))
4607 value
= xstrdup("1");
4609 value
= xstrdup("0");
4610 } else if (strcmp(cmp
->modifier
, "==") == 0) {
4611 if (strcmp(left
, right
) == 0)
4612 value
= xstrdup("1");
4614 value
= xstrdup("0");
4615 } else if (strcmp(cmp
->modifier
, "!=") == 0) {
4616 if (strcmp(left
, right
) != 0)
4617 value
= xstrdup("1");
4619 value
= xstrdup("0");
4620 } else if (strcmp(cmp
->modifier
, "<") == 0) {
4621 if (strcmp(left
, right
) < 0)
4622 value
= xstrdup("1");
4624 value
= xstrdup("0");
4625 } else if (strcmp(cmp
->modifier
, ">") == 0) {
4626 if (strcmp(left
, right
) > 0)
4627 value
= xstrdup("1");
4629 value
= xstrdup("0");
4630 } else if (strcmp(cmp
->modifier
, "<=") == 0) {
4631 if (strcmp(left
, right
) <= 0)
4632 value
= xstrdup("1");
4634 value
= xstrdup("0");
4635 } else if (strcmp(cmp
->modifier
, ">=") == 0) {
4636 if (strcmp(left
, right
) >= 0)
4637 value
= xstrdup("1");
4639 value
= xstrdup("0");
4640 } else if (strcmp(cmp
->modifier
, "m") == 0)
4641 value
= format_match(cmp
, left
, right
);
4645 } else if (*copy
== '?') {
4646 /* Conditional: check first and choose second or third. */
4647 cp
= format_skip(copy
+ 1, ",");
4649 format_log(es
, "condition syntax error: %s", copy
+ 1);
4652 condition
= xstrndup(copy
+ 1, cp
- (copy
+ 1));
4653 format_log(es
, "condition is: %s", condition
);
4655 found
= format_find(ft
, condition
, modifiers
, time_format
);
4656 if (found
== NULL
) {
4658 * If the condition not found, try to expand it. If
4659 * the expansion doesn't have any effect, then assume
4662 found
= format_expand1(es
, condition
);
4663 if (strcmp(found
, condition
) == 0) {
4665 found
= xstrdup("");
4667 "condition '%s' not found; assuming false",
4671 format_log(es
, "condition '%s' found: %s", condition
,
4675 if (format_choose(es
, cp
+ 1, &left
, &right
, 0) != 0) {
4676 format_log(es
, "condition '%s' syntax error: %s",
4681 if (format_true(found
)) {
4682 format_log(es
, "condition '%s' is true", condition
);
4683 value
= format_expand1(es
, left
);
4685 format_log(es
, "condition '%s' is false", condition
);
4686 value
= format_expand1(es
, right
);
4693 } else if (mexp
!= NULL
) {
4694 value
= format_replace_expression(mexp
, es
, copy
);
4696 value
= xstrdup("");
4698 if (strstr(copy
, "#{") != 0) {
4699 format_log(es
, "expanding inner format '%s'", copy
);
4700 value
= format_expand1(es
, copy
);
4702 value
= format_find(ft
, copy
, modifiers
, time_format
);
4703 if (value
== NULL
) {
4704 format_log(es
, "format '%s' not found", copy
);
4705 value
= xstrdup("");
4707 format_log(es
, "format '%s' found: %s", copy
,
4714 /* Expand again if required. */
4715 if (modifiers
& FORMAT_EXPAND
) {
4716 new = format_expand1(es
, value
);
4719 } else if (modifiers
& FORMAT_EXPANDTIME
) {
4720 format_copy_state(&next
, es
, FORMAT_EXPAND_TIME
);
4721 new = format_expand1(&next
, value
);
4726 /* Perform substitution if any. */
4727 for (i
= 0; i
< nsub
; i
++) {
4728 left
= format_expand1(es
, sub
[i
]->argv
[0]);
4729 right
= format_expand1(es
, sub
[i
]->argv
[1]);
4730 new = format_sub(sub
[i
], value
, left
, right
);
4731 format_log(es
, "substitute '%s' to '%s': %s", left
, right
, new);
4738 /* Truncate the value if needed. */
4740 new = format_trim_left(value
, limit
);
4741 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4743 xasprintf(&value
, "%s%s", new, marker
);
4748 format_log(es
, "applied length limit %d: %s", limit
, value
);
4749 } else if (limit
< 0) {
4750 new = format_trim_right(value
, -limit
);
4751 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4753 xasprintf(&value
, "%s%s", marker
, new);
4758 format_log(es
, "applied length limit %d: %s", limit
, value
);
4761 /* Pad the value if needed. */
4763 new = utf8_padcstr(value
, width
);
4766 format_log(es
, "applied padding width %d: %s", width
, value
);
4767 } else if (width
< 0) {
4768 new = utf8_rpadcstr(value
, -width
);
4771 format_log(es
, "applied padding width %d: %s", width
, value
);
4774 /* Replace with the length or width if needed. */
4775 if (modifiers
& FORMAT_LENGTH
) {
4776 xasprintf(&new, "%zu", strlen(value
));
4779 format_log(es
, "replacing with length: %s", new);
4781 if (modifiers
& FORMAT_WIDTH
) {
4782 xasprintf(&new, "%u", format_width(value
));
4785 format_log(es
, "replacing with width: %s", new);
4788 /* Expand the buffer and copy in the value. */
4789 valuelen
= strlen(value
);
4790 while (*len
- *off
< valuelen
+ 1) {
4791 *buf
= xreallocarray(*buf
, 2, *len
);
4794 memcpy(*buf
+ *off
, value
, valuelen
);
4797 format_log(es
, "replaced '%s' with '%s'", copy0
, value
);
4801 format_free_modifiers(list
, count
);
4806 format_log(es
, "failed %s", copy0
);
4809 format_free_modifiers(list
, count
);
4814 /* Expand keys in a template. */
4816 format_expand1(struct format_expand_state
*es
, const char *fmt
)
4818 struct format_tree
*ft
= es
->ft
;
4819 char *buf
, *out
, *name
;
4820 const char *ptr
, *s
, *style_end
= NULL
;
4821 size_t off
, len
, n
, outlen
;
4823 char expanded
[8192];
4825 if (fmt
== NULL
|| *fmt
== '\0')
4826 return (xstrdup(""));
4828 if (es
->loop
== FORMAT_LOOP_LIMIT
) {
4829 format_log(es
, "reached loop limit (%u)", FORMAT_LOOP_LIMIT
);
4830 return (xstrdup(""));
4834 format_log(es
, "expanding format: %s", fmt
);
4836 if ((es
->flags
& FORMAT_EXPAND_TIME
) && strchr(fmt
, '%') != NULL
) {
4837 if (es
->time
== 0) {
4838 es
->time
= time(NULL
);
4839 localtime_r(&es
->time
, &es
->tm
);
4841 if (strftime(expanded
, sizeof expanded
, fmt
, &es
->tm
) == 0) {
4842 format_log(es
, "format is too long");
4843 return (xstrdup(""));
4845 if (format_logging(ft
) && strcmp(expanded
, fmt
) != 0)
4846 format_log(es
, "after time expanded: %s", expanded
);
4854 while (*fmt
!= '\0') {
4856 while (len
- off
< 2) {
4857 buf
= xreallocarray(buf
, 2, len
);
4860 buf
[off
++] = *fmt
++;
4865 ch
= (u_char
)*fmt
++;
4869 for (ptr
= fmt
; *ptr
!= '\0'; ptr
++) {
4872 if (*ptr
== ')' && --brackets
== 0)
4875 if (*ptr
!= ')' || brackets
!= 0)
4879 name
= xstrndup(fmt
, n
);
4880 format_log(es
, "found #(): %s", name
);
4882 if ((ft
->flags
& FORMAT_NOJOBS
) ||
4883 (es
->flags
& FORMAT_EXPAND_NOJOBS
)) {
4885 format_log(es
, "#() is disabled");
4887 out
= format_job_get(es
, name
);
4888 format_log(es
, "#() result: %s", out
);
4892 outlen
= strlen(out
);
4893 while (len
- off
< outlen
+ 1) {
4894 buf
= xreallocarray(buf
, 2, len
);
4897 memcpy(buf
+ off
, out
, outlen
);
4905 ptr
= format_skip((char *)fmt
- 2, "}");
4910 format_log(es
, "found #{}: %.*s", (int)n
, fmt
);
4911 if (format_replace(es
, fmt
, n
, &buf
, &len
, &off
) != 0)
4918 * If ##[ (with two or more #s), then it is a style and
4919 * can be left for format_draw to handle.
4921 ptr
= fmt
- (ch
== '[');
4922 n
= 2 - (ch
== '[');
4923 while (*ptr
== '#') {
4928 style_end
= format_skip(fmt
- 2, "]");
4929 format_log(es
, "found #*%zu[", n
);
4930 while (len
- off
< n
+ 2) {
4931 buf
= xreallocarray(buf
, 2, len
);
4934 memcpy(buf
+ off
, fmt
- 2, n
+ 1);
4942 format_log(es
, "found #%c", ch
);
4943 while (len
- off
< 2) {
4944 buf
= xreallocarray(buf
, 2, len
);
4951 if (fmt
> style_end
) { /* skip inside #[] */
4952 if (ch
>= 'A' && ch
<= 'Z')
4953 s
= format_upper
[ch
- 'A'];
4954 else if (ch
>= 'a' && ch
<= 'z')
4955 s
= format_lower
[ch
- 'a'];
4958 while (len
- off
< 3) {
4959 buf
= xreallocarray(buf
, 2, len
);
4967 format_log(es
, "found #%c: %s", ch
, s
);
4968 if (format_replace(es
, s
, n
, &buf
, &len
, &off
) != 0)
4977 format_log(es
, "result is: %s", buf
);
4983 /* Expand keys in a template, passing through strftime first. */
4985 format_expand_time(struct format_tree
*ft
, const char *fmt
)
4987 struct format_expand_state es
;
4989 memset(&es
, 0, sizeof es
);
4991 es
.flags
= FORMAT_EXPAND_TIME
;
4992 return (format_expand1(&es
, fmt
));
4995 /* Expand keys in a template. */
4997 format_expand(struct format_tree
*ft
, const char *fmt
)
4999 struct format_expand_state es
;
5001 memset(&es
, 0, sizeof es
);
5004 return (format_expand1(&es
, fmt
));
5007 /* Expand a single string. */
5009 format_single(struct cmdq_item
*item
, const char *fmt
, struct client
*c
,
5010 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
5012 struct format_tree
*ft
;
5015 ft
= format_create_defaults(item
, c
, s
, wl
, wp
);
5016 expanded
= format_expand(ft
, fmt
);
5021 /* Expand a single string using state. */
5023 format_single_from_state(struct cmdq_item
*item
, const char *fmt
,
5024 struct client
*c
, struct cmd_find_state
*fs
)
5026 return (format_single(item
, fmt
, c
, fs
->s
, fs
->wl
, fs
->wp
));
5029 /* Expand a single string using target. */
5031 format_single_from_target(struct cmdq_item
*item
, const char *fmt
)
5033 struct client
*tc
= cmdq_get_target_client(item
);
5035 return (format_single_from_state(item
, fmt
, tc
, cmdq_get_target(item
)));
5038 /* Create and add defaults. */
5039 struct format_tree
*
5040 format_create_defaults(struct cmdq_item
*item
, struct client
*c
,
5041 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
5043 struct format_tree
*ft
;
5046 ft
= format_create(cmdq_get_client(item
), item
, FORMAT_NONE
, 0);
5048 ft
= format_create(NULL
, item
, FORMAT_NONE
, 0);
5049 format_defaults(ft
, c
, s
, wl
, wp
);
5053 /* Create and add defaults using state. */
5054 struct format_tree
*
5055 format_create_from_state(struct cmdq_item
*item
, struct client
*c
,
5056 struct cmd_find_state
*fs
)
5058 return (format_create_defaults(item
, c
, fs
->s
, fs
->wl
, fs
->wp
));
5061 /* Create and add defaults using target. */
5062 struct format_tree
*
5063 format_create_from_target(struct cmdq_item
*item
)
5065 struct client
*tc
= cmdq_get_target_client(item
);
5067 return (format_create_from_state(item
, tc
, cmdq_get_target(item
)));
5070 /* Set defaults for any of arguments that are not NULL. */
5072 format_defaults(struct format_tree
*ft
, struct client
*c
, struct session
*s
,
5073 struct winlink
*wl
, struct window_pane
*wp
)
5075 struct paste_buffer
*pb
;
5077 if (c
!= NULL
&& c
->name
!= NULL
)
5078 log_debug("%s: c=%s", __func__
, c
->name
);
5080 log_debug("%s: c=none", __func__
);
5082 log_debug("%s: s=$%u", __func__
, s
->id
);
5084 log_debug("%s: s=none", __func__
);
5086 log_debug("%s: wl=%u", __func__
, wl
->idx
);
5088 log_debug("%s: wl=none", __func__
);
5090 log_debug("%s: wp=%%%u", __func__
, wp
->id
);
5092 log_debug("%s: wp=none", __func__
);
5094 if (c
!= NULL
&& s
!= NULL
&& c
->session
!= s
)
5095 log_debug("%s: session does not match", __func__
);
5098 ft
->type
= FORMAT_TYPE_PANE
;
5099 else if (wl
!= NULL
)
5100 ft
->type
= FORMAT_TYPE_WINDOW
;
5102 ft
->type
= FORMAT_TYPE_SESSION
;
5104 ft
->type
= FORMAT_TYPE_UNKNOWN
;
5106 if (s
== NULL
&& c
!= NULL
)
5108 if (wl
== NULL
&& s
!= NULL
)
5110 if (wp
== NULL
&& wl
!= NULL
)
5111 wp
= wl
->window
->active
;
5114 format_defaults_client(ft
, c
);
5116 format_defaults_session(ft
, s
);
5118 format_defaults_winlink(ft
, wl
);
5120 format_defaults_pane(ft
, wp
);
5122 pb
= paste_get_top(NULL
);
5124 format_defaults_paste_buffer(ft
, pb
);
5127 /* Set default format keys for a session. */
5129 format_defaults_session(struct format_tree
*ft
, struct session
*s
)
5134 /* Set default format keys for a client. */
5136 format_defaults_client(struct format_tree
*ft
, struct client
*c
)
5143 /* Set default format keys for a window. */
5145 format_defaults_window(struct format_tree
*ft
, struct window
*w
)
5150 /* Set default format keys for a winlink. */
5152 format_defaults_winlink(struct format_tree
*ft
, struct winlink
*wl
)
5155 format_defaults_window(ft
, wl
->window
);
5159 /* Set default format keys for a window pane. */
5161 format_defaults_pane(struct format_tree
*ft
, struct window_pane
*wp
)
5163 struct window_mode_entry
*wme
;
5166 format_defaults_window(ft
, wp
->window
);
5169 wme
= TAILQ_FIRST(&wp
->modes
);
5170 if (wme
!= NULL
&& wme
->mode
->formats
!= NULL
)
5171 wme
->mode
->formats(wme
, ft
);
5174 /* Set default format keys for paste buffer. */
5176 format_defaults_paste_buffer(struct format_tree
*ft
, struct paste_buffer
*pb
)
5181 /* Return word at given coordinates. Caller frees. */
5183 format_grid_word(struct grid
*gd
, u_int x
, u_int y
)
5185 const struct grid_line
*gl
;
5186 struct grid_cell gc
;
5188 struct utf8_data
*ud
= NULL
;
5194 ws
= options_get_string(global_s_options
, "word-separators");
5197 grid_get_cell(gd
, x
, y
, &gc
);
5198 if (gc
.flags
& GRID_FLAG_PADDING
)
5200 if (utf8_cstrhas(ws
, &gc
.data
) ||
5201 (gc
.data
.size
== 1 && *gc
.data
.data
== ' ')) {
5209 gl
= grid_peek_line(gd
, y
- 1);
5210 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5213 x
= grid_line_length(gd
, y
);
5221 end
= grid_line_length(gd
, y
);
5222 if (end
== 0 || x
== end
- 1) {
5223 if (y
== gd
->hsize
+ gd
->sy
- 1)
5225 gl
= grid_peek_line(gd
, y
);
5226 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5235 grid_get_cell(gd
, x
, y
, &gc
);
5236 if (gc
.flags
& GRID_FLAG_PADDING
)
5238 if (utf8_cstrhas(ws
, &gc
.data
) ||
5239 (gc
.data
.size
== 1 && *gc
.data
.data
== ' '))
5242 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5243 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5247 s
= utf8_tocstr(ud
);
5253 /* Return line at given coordinates. Caller frees. */
5255 format_grid_line(struct grid
*gd
, u_int y
)
5257 struct grid_cell gc
;
5258 struct utf8_data
*ud
= NULL
;
5263 for (x
= 0; x
< grid_line_length(gd
, y
); x
++) {
5264 grid_get_cell(gd
, x
, y
, &gc
);
5265 if (gc
.flags
& GRID_FLAG_PADDING
)
5268 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5269 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5273 s
= utf8_tocstr(ud
);
5279 /* Return hyperlink at given coordinates. Caller frees. */
5281 format_grid_hyperlink(struct grid
*gd
, u_int x
, u_int y
, struct screen
* s
)
5284 struct grid_cell gc
;
5286 grid_get_cell(gd
, x
, y
, &gc
);
5287 if (gc
.flags
& GRID_FLAG_PADDING
)
5289 if (s
->hyperlinks
== NULL
|| gc
.link
== 0)
5291 if (!hyperlinks_get(s
->hyperlinks
, gc
.link
, &uri
, NULL
, NULL
))
5293 return (xstrdup(uri
));