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
= get_proc_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
= get_proc_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
;
1133 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1136 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1139 if (!TAILQ_EMPTY(&wp
->modes
)) {
1140 if (TAILQ_FIRST(&wp
->modes
)->mode
== &window_copy_mode
||
1141 TAILQ_FIRST(&wp
->modes
)->mode
== &window_view_mode
)
1142 return (s
= window_copy_get_word(wp
, x
, y
));
1146 return (format_grid_word(gd
, x
, gd
->hsize
+ y
));
1149 /* Callback for mouse_hyperlink. */
1151 format_cb_mouse_hyperlink(struct format_tree
*ft
)
1153 struct window_pane
*wp
;
1159 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1162 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1165 return (format_grid_hyperlink(gd
, x
, gd
->hsize
+ y
, wp
->screen
));
1168 /* Callback for mouse_line. */
1170 format_cb_mouse_line(struct format_tree
*ft
)
1172 struct window_pane
*wp
;
1178 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1181 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1184 if (!TAILQ_EMPTY(&wp
->modes
)) {
1185 if (TAILQ_FIRST(&wp
->modes
)->mode
== &window_copy_mode
||
1186 TAILQ_FIRST(&wp
->modes
)->mode
== &window_view_mode
)
1187 return (window_copy_get_line(wp
, y
));
1191 return (format_grid_line(gd
, gd
->hsize
+ y
));
1194 /* Callback for alternate_on. */
1196 format_cb_alternate_on(struct format_tree
*ft
)
1198 if (ft
->wp
!= NULL
) {
1199 if (ft
->wp
->base
.saved_grid
!= NULL
)
1200 return (xstrdup("1"));
1201 return (xstrdup("0"));
1206 /* Callback for alternate_saved_x. */
1208 format_cb_alternate_saved_x(struct format_tree
*ft
)
1211 return (format_printf("%u", ft
->wp
->base
.saved_cx
));
1215 /* Callback for alternate_saved_y. */
1217 format_cb_alternate_saved_y(struct format_tree
*ft
)
1220 return (format_printf("%u", ft
->wp
->base
.saved_cy
));
1224 /* Callback for buffer_name. */
1226 format_cb_buffer_name(struct format_tree
*ft
)
1229 return (xstrdup(paste_buffer_name(ft
->pb
)));
1233 /* Callback for buffer_sample. */
1235 format_cb_buffer_sample(struct format_tree
*ft
)
1238 return (paste_make_sample(ft
->pb
));
1242 /* Callback for buffer_size. */
1244 format_cb_buffer_size(struct format_tree
*ft
)
1248 if (ft
->pb
!= NULL
) {
1249 paste_buffer_data(ft
->pb
, &size
);
1250 return (format_printf("%zu", size
));
1255 /* Callback for client_cell_height. */
1257 format_cb_client_cell_height(struct format_tree
*ft
)
1259 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1260 return (format_printf("%u", ft
->c
->tty
.ypixel
));
1264 /* Callback for client_cell_width. */
1266 format_cb_client_cell_width(struct format_tree
*ft
)
1268 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1269 return (format_printf("%u", ft
->c
->tty
.xpixel
));
1273 /* Callback for client_control_mode. */
1275 format_cb_client_control_mode(struct format_tree
*ft
)
1277 if (ft
->c
!= NULL
) {
1278 if (ft
->c
->flags
& CLIENT_CONTROL
)
1279 return (xstrdup("1"));
1280 return (xstrdup("0"));
1285 /* Callback for client_discarded. */
1287 format_cb_client_discarded(struct format_tree
*ft
)
1290 return (format_printf("%zu", ft
->c
->discarded
));
1294 /* Callback for client_flags. */
1296 format_cb_client_flags(struct format_tree
*ft
)
1299 return (xstrdup(server_client_get_flags(ft
->c
)));
1303 /* Callback for client_height. */
1305 format_cb_client_height(struct format_tree
*ft
)
1307 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1308 return (format_printf("%u", ft
->c
->tty
.sy
));
1312 /* Callback for client_key_table. */
1314 format_cb_client_key_table(struct format_tree
*ft
)
1317 return (xstrdup(ft
->c
->keytable
->name
));
1321 /* Callback for client_last_session. */
1323 format_cb_client_last_session(struct format_tree
*ft
)
1325 if (ft
->c
!= NULL
&&
1326 ft
->c
->last_session
!= NULL
&&
1327 session_alive(ft
->c
->last_session
))
1328 return (xstrdup(ft
->c
->last_session
->name
));
1332 /* Callback for client_name. */
1334 format_cb_client_name(struct format_tree
*ft
)
1337 return (xstrdup(ft
->c
->name
));
1341 /* Callback for client_pid. */
1343 format_cb_client_pid(struct format_tree
*ft
)
1346 return (format_printf("%ld", (long)ft
->c
->pid
));
1350 /* Callback for client_prefix. */
1352 format_cb_client_prefix(struct format_tree
*ft
)
1356 if (ft
->c
!= NULL
) {
1357 name
= server_client_get_key_table(ft
->c
);
1358 if (strcmp(ft
->c
->keytable
->name
, name
) == 0)
1359 return (xstrdup("0"));
1360 return (xstrdup("1"));
1365 /* Callback for client_readonly. */
1367 format_cb_client_readonly(struct format_tree
*ft
)
1369 if (ft
->c
!= NULL
) {
1370 if (ft
->c
->flags
& CLIENT_READONLY
)
1371 return (xstrdup("1"));
1372 return (xstrdup("0"));
1377 /* Callback for client_session. */
1379 format_cb_client_session(struct format_tree
*ft
)
1381 if (ft
->c
!= NULL
&& ft
->c
->session
!= NULL
)
1382 return (xstrdup(ft
->c
->session
->name
));
1386 /* Callback for client_termfeatures. */
1388 format_cb_client_termfeatures(struct format_tree
*ft
)
1391 return (xstrdup(tty_get_features(ft
->c
->term_features
)));
1395 /* Callback for client_termname. */
1397 format_cb_client_termname(struct format_tree
*ft
)
1400 return (xstrdup(ft
->c
->term_name
));
1404 /* Callback for client_termtype. */
1406 format_cb_client_termtype(struct format_tree
*ft
)
1408 if (ft
->c
!= NULL
) {
1409 if (ft
->c
->term_type
== NULL
)
1410 return (xstrdup(""));
1411 return (xstrdup(ft
->c
->term_type
));
1416 /* Callback for client_tty. */
1418 format_cb_client_tty(struct format_tree
*ft
)
1421 return (xstrdup(ft
->c
->ttyname
));
1425 /* Callback for client_uid. */
1427 format_cb_client_uid(struct format_tree
*ft
)
1431 if (ft
->c
!= NULL
) {
1432 uid
= proc_get_peer_uid(ft
->c
->peer
);
1433 if (uid
!= (uid_t
)-1)
1434 return (format_printf("%ld", (long)uid
));
1439 /* Callback for client_user. */
1441 format_cb_client_user(struct format_tree
*ft
)
1446 if (ft
->c
!= NULL
) {
1447 uid
= proc_get_peer_uid(ft
->c
->peer
);
1448 if (uid
!= (uid_t
)-1 && (pw
= getpwuid(uid
)) != NULL
)
1449 return (xstrdup(pw
->pw_name
));
1454 /* Callback for client_utf8. */
1456 format_cb_client_utf8(struct format_tree
*ft
)
1458 if (ft
->c
!= NULL
) {
1459 if (ft
->c
->flags
& CLIENT_UTF8
)
1460 return (xstrdup("1"));
1461 return (xstrdup("0"));
1466 /* Callback for client_width. */
1468 format_cb_client_width(struct format_tree
*ft
)
1471 return (format_printf("%u", ft
->c
->tty
.sx
));
1475 /* Callback for client_written. */
1477 format_cb_client_written(struct format_tree
*ft
)
1480 return (format_printf("%zu", ft
->c
->written
));
1484 /* Callback for config_files. */
1486 format_cb_config_files(__unused
struct format_tree
*ft
)
1493 for (i
= 0; i
< cfg_nfiles
; i
++) {
1494 n
= strlen(cfg_files
[i
]) + 1;
1495 s
= xrealloc(s
, slen
+ n
+ 1);
1496 slen
+= xsnprintf(s
+ slen
, n
+ 1, "%s,", cfg_files
[i
]);
1499 return (xstrdup(""));
1504 /* Callback for cursor_flag. */
1506 format_cb_cursor_flag(struct format_tree
*ft
)
1508 if (ft
->wp
!= NULL
) {
1509 if (ft
->wp
->base
.mode
& MODE_CURSOR
)
1510 return (xstrdup("1"));
1511 return (xstrdup("0"));
1516 /* Callback for cursor_x. */
1518 format_cb_cursor_x(struct format_tree
*ft
)
1521 return (format_printf("%u", ft
->wp
->base
.cx
));
1525 /* Callback for cursor_y. */
1527 format_cb_cursor_y(struct format_tree
*ft
)
1530 return (format_printf("%u", ft
->wp
->base
.cy
));
1534 /* Callback for history_limit. */
1536 format_cb_history_limit(struct format_tree
*ft
)
1539 return (format_printf("%u", ft
->wp
->base
.grid
->hlimit
));
1543 /* Callback for history_size. */
1545 format_cb_history_size(struct format_tree
*ft
)
1548 return (format_printf("%u", ft
->wp
->base
.grid
->hsize
));
1552 /* Callback for insert_flag. */
1554 format_cb_insert_flag(struct format_tree
*ft
)
1556 if (ft
->wp
!= NULL
) {
1557 if (ft
->wp
->base
.mode
& MODE_INSERT
)
1558 return (xstrdup("1"));
1559 return (xstrdup("0"));
1564 /* Callback for keypad_cursor_flag. */
1566 format_cb_keypad_cursor_flag(struct format_tree
*ft
)
1568 if (ft
->wp
!= NULL
) {
1569 if (ft
->wp
->base
.mode
& MODE_KCURSOR
)
1570 return (xstrdup("1"));
1571 return (xstrdup("0"));
1576 /* Callback for keypad_flag. */
1578 format_cb_keypad_flag(struct format_tree
*ft
)
1580 if (ft
->wp
!= NULL
) {
1581 if (ft
->wp
->base
.mode
& MODE_KKEYPAD
)
1582 return (xstrdup("1"));
1583 return (xstrdup("0"));
1588 /* Callback for mouse_all_flag. */
1590 format_cb_mouse_all_flag(struct format_tree
*ft
)
1592 if (ft
->wp
!= NULL
) {
1593 if (ft
->wp
->base
.mode
& MODE_MOUSE_ALL
)
1594 return (xstrdup("1"));
1595 return (xstrdup("0"));
1600 /* Callback for mouse_any_flag. */
1602 format_cb_mouse_any_flag(struct format_tree
*ft
)
1604 if (ft
->wp
!= NULL
) {
1605 if (ft
->wp
->base
.mode
& ALL_MOUSE_MODES
)
1606 return (xstrdup("1"));
1607 return (xstrdup("0"));
1612 /* Callback for mouse_button_flag. */
1614 format_cb_mouse_button_flag(struct format_tree
*ft
)
1616 if (ft
->wp
!= NULL
) {
1617 if (ft
->wp
->base
.mode
& MODE_MOUSE_BUTTON
)
1618 return (xstrdup("1"));
1619 return (xstrdup("0"));
1624 /* Callback for mouse_pane. */
1626 format_cb_mouse_pane(struct format_tree
*ft
)
1628 struct window_pane
*wp
;
1631 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1633 return (format_printf("%%%u", wp
->id
));
1639 /* Callback for mouse_sgr_flag. */
1641 format_cb_mouse_sgr_flag(struct format_tree
*ft
)
1643 if (ft
->wp
!= NULL
) {
1644 if (ft
->wp
->base
.mode
& MODE_MOUSE_SGR
)
1645 return (xstrdup("1"));
1646 return (xstrdup("0"));
1651 /* Callback for mouse_standard_flag. */
1653 format_cb_mouse_standard_flag(struct format_tree
*ft
)
1655 if (ft
->wp
!= NULL
) {
1656 if (ft
->wp
->base
.mode
& MODE_MOUSE_STANDARD
)
1657 return (xstrdup("1"));
1658 return (xstrdup("0"));
1663 /* Callback for mouse_utf8_flag. */
1665 format_cb_mouse_utf8_flag(struct format_tree
*ft
)
1667 if (ft
->wp
!= NULL
) {
1668 if (ft
->wp
->base
.mode
& MODE_MOUSE_UTF8
)
1669 return (xstrdup("1"));
1670 return (xstrdup("0"));
1675 /* Callback for mouse_x. */
1677 format_cb_mouse_x(struct format_tree
*ft
)
1679 struct window_pane
*wp
;
1684 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1685 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1686 return (format_printf("%u", x
));
1687 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1688 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1689 return (format_printf("%u", ft
->m
.x
));
1690 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1691 return (format_printf("%u", ft
->m
.x
));
1696 /* Callback for mouse_y. */
1698 format_cb_mouse_y(struct format_tree
*ft
)
1700 struct window_pane
*wp
;
1705 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1706 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1707 return (format_printf("%u", y
));
1708 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1709 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1710 return (format_printf("%u", ft
->m
.y
));
1711 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1712 return (format_printf("%u", ft
->m
.y
- ft
->m
.statusat
));
1717 /* Callback for next_session_id. */
1719 format_cb_next_session_id(__unused
struct format_tree
*ft
)
1721 return (format_printf("$%u", next_session_id
));
1724 /* Callback for origin_flag. */
1726 format_cb_origin_flag(struct format_tree
*ft
)
1728 if (ft
->wp
!= NULL
) {
1729 if (ft
->wp
->base
.mode
& MODE_ORIGIN
)
1730 return (xstrdup("1"));
1731 return (xstrdup("0"));
1736 /* Callback for pane_active. */
1738 format_cb_pane_active(struct format_tree
*ft
)
1740 if (ft
->wp
!= NULL
) {
1741 if (ft
->wp
== ft
->wp
->window
->active
)
1742 return (xstrdup("1"));
1743 return (xstrdup("0"));
1748 /* Callback for pane_at_left. */
1750 format_cb_pane_at_left(struct format_tree
*ft
)
1752 if (ft
->wp
!= NULL
) {
1753 if (ft
->wp
->xoff
== 0)
1754 return (xstrdup("1"));
1755 return (xstrdup("0"));
1760 /* Callback for pane_at_right. */
1762 format_cb_pane_at_right(struct format_tree
*ft
)
1764 if (ft
->wp
!= NULL
) {
1765 if (ft
->wp
->xoff
+ ft
->wp
->sx
== ft
->wp
->window
->sx
)
1766 return (xstrdup("1"));
1767 return (xstrdup("0"));
1772 /* Callback for pane_bottom. */
1774 format_cb_pane_bottom(struct format_tree
*ft
)
1777 return (format_printf("%u", ft
->wp
->yoff
+ ft
->wp
->sy
- 1));
1781 /* Callback for pane_dead. */
1783 format_cb_pane_dead(struct format_tree
*ft
)
1785 if (ft
->wp
!= NULL
) {
1786 if (ft
->wp
->fd
== -1)
1787 return (xstrdup("1"));
1788 return (xstrdup("0"));
1793 /* Callback for pane_dead_signal. */
1795 format_cb_pane_dead_signal(struct format_tree
*ft
)
1797 struct window_pane
*wp
= ft
->wp
;
1801 if ((wp
->flags
& PANE_STATUSREADY
) && WIFSIGNALED(wp
->status
)) {
1802 name
= sig2name(WTERMSIG(wp
->status
));
1803 return (format_printf("%s", name
));
1810 /* Callback for pane_dead_status. */
1812 format_cb_pane_dead_status(struct format_tree
*ft
)
1814 struct window_pane
*wp
= ft
->wp
;
1817 if ((wp
->flags
& PANE_STATUSREADY
) && WIFEXITED(wp
->status
))
1818 return (format_printf("%d", WEXITSTATUS(wp
->status
)));
1824 /* Callback for pane_dead_time. */
1826 format_cb_pane_dead_time(struct format_tree
*ft
)
1828 struct window_pane
*wp
= ft
->wp
;
1831 if (wp
->flags
& PANE_STATUSDRAWN
)
1832 return (&wp
->dead_time
);
1838 /* Callback for pane_format. */
1840 format_cb_pane_format(struct format_tree
*ft
)
1842 if (ft
->type
== FORMAT_TYPE_PANE
)
1843 return (xstrdup("1"));
1844 return (xstrdup("0"));
1847 /* Callback for pane_height. */
1849 format_cb_pane_height(struct format_tree
*ft
)
1852 return (format_printf("%u", ft
->wp
->sy
));
1856 /* Callback for pane_id. */
1858 format_cb_pane_id(struct format_tree
*ft
)
1861 return (format_printf("%%%u", ft
->wp
->id
));
1865 /* Callback for pane_index. */
1867 format_cb_pane_index(struct format_tree
*ft
)
1871 if (ft
->wp
!= NULL
&& window_pane_index(ft
->wp
, &idx
) == 0)
1872 return (format_printf("%u", idx
));
1876 /* Callback for pane_input_off. */
1878 format_cb_pane_input_off(struct format_tree
*ft
)
1880 if (ft
->wp
!= NULL
) {
1881 if (ft
->wp
->flags
& PANE_INPUTOFF
)
1882 return (xstrdup("1"));
1883 return (xstrdup("0"));
1888 /* Callback for pane_unseen_changes. */
1890 format_cb_pane_unseen_changes(struct format_tree
*ft
)
1892 if (ft
->wp
!= NULL
) {
1893 if (ft
->wp
->flags
& PANE_UNSEENCHANGES
)
1894 return (xstrdup("1"));
1895 return (xstrdup("0"));
1900 /* Callback for pane_last. */
1902 format_cb_pane_last(struct format_tree
*ft
)
1904 if (ft
->wp
!= NULL
) {
1905 if (ft
->wp
== TAILQ_FIRST(&ft
->wp
->window
->last_panes
))
1906 return (xstrdup("1"));
1907 return (xstrdup("0"));
1912 /* Callback for pane_left. */
1914 format_cb_pane_left(struct format_tree
*ft
)
1917 return (format_printf("%u", ft
->wp
->xoff
));
1921 /* Callback for pane_marked. */
1923 format_cb_pane_marked(struct format_tree
*ft
)
1925 if (ft
->wp
!= NULL
) {
1926 if (server_check_marked() && marked_pane
.wp
== ft
->wp
)
1927 return (xstrdup("1"));
1928 return (xstrdup("0"));
1933 /* Callback for pane_marked_set. */
1935 format_cb_pane_marked_set(struct format_tree
*ft
)
1937 if (ft
->wp
!= NULL
) {
1938 if (server_check_marked())
1939 return (xstrdup("1"));
1940 return (xstrdup("0"));
1945 /* Callback for pane_mode. */
1947 format_cb_pane_mode(struct format_tree
*ft
)
1949 struct window_mode_entry
*wme
;
1951 if (ft
->wp
!= NULL
) {
1952 wme
= TAILQ_FIRST(&ft
->wp
->modes
);
1954 return (xstrdup(wme
->mode
->name
));
1960 /* Callback for pane_path. */
1962 format_cb_pane_path(struct format_tree
*ft
)
1964 if (ft
->wp
!= NULL
) {
1965 if (ft
->wp
->base
.path
== NULL
)
1966 return (xstrdup(""));
1967 return (xstrdup(ft
->wp
->base
.path
));
1972 /* Callback for pane_pid. */
1974 format_cb_pane_pid(struct format_tree
*ft
)
1977 return (format_printf("%ld", (long)ft
->wp
->pid
));
1981 /* Callback for pane_pipe. */
1983 format_cb_pane_pipe(struct format_tree
*ft
)
1985 if (ft
->wp
!= NULL
) {
1986 if (ft
->wp
->pipe_fd
!= -1)
1987 return (xstrdup("1"));
1988 return (xstrdup("0"));
1993 /* Callback for pane_right. */
1995 format_cb_pane_right(struct format_tree
*ft
)
1998 return (format_printf("%u", ft
->wp
->xoff
+ ft
->wp
->sx
- 1));
2002 /* Callback for pane_search_string. */
2004 format_cb_pane_search_string(struct format_tree
*ft
)
2006 if (ft
->wp
!= NULL
) {
2007 if (ft
->wp
->searchstr
== NULL
)
2008 return (xstrdup(""));
2009 return (xstrdup(ft
->wp
->searchstr
));
2014 /* Callback for pane_synchronized. */
2016 format_cb_pane_synchronized(struct format_tree
*ft
)
2018 if (ft
->wp
!= NULL
) {
2019 if (options_get_number(ft
->wp
->options
, "synchronize-panes"))
2020 return (xstrdup("1"));
2021 return (xstrdup("0"));
2026 /* Callback for pane_title. */
2028 format_cb_pane_title(struct format_tree
*ft
)
2031 return (xstrdup(ft
->wp
->base
.title
));
2035 /* Callback for pane_top. */
2037 format_cb_pane_top(struct format_tree
*ft
)
2040 return (format_printf("%u", ft
->wp
->yoff
));
2044 /* Callback for pane_tty. */
2046 format_cb_pane_tty(struct format_tree
*ft
)
2049 return (xstrdup(ft
->wp
->tty
));
2053 /* Callback for pane_width. */
2055 format_cb_pane_width(struct format_tree
*ft
)
2058 return (format_printf("%u", ft
->wp
->sx
));
2062 /* Callback for scroll_region_lower. */
2064 format_cb_scroll_region_lower(struct format_tree
*ft
)
2067 return (format_printf("%u", ft
->wp
->base
.rlower
));
2071 /* Callback for scroll_region_upper. */
2073 format_cb_scroll_region_upper(struct format_tree
*ft
)
2076 return (format_printf("%u", ft
->wp
->base
.rupper
));
2080 /* Callback for server_sessions. */
2082 format_cb_server_sessions(__unused
struct format_tree
*ft
)
2087 RB_FOREACH(s
, sessions
, &sessions
)
2089 return (format_printf("%u", n
));
2092 /* Callback for session_attached. */
2094 format_cb_session_attached(struct format_tree
*ft
)
2097 return (format_printf("%u", ft
->s
->attached
));
2101 /* Callback for session_format. */
2103 format_cb_session_format(struct format_tree
*ft
)
2105 if (ft
->type
== FORMAT_TYPE_SESSION
)
2106 return (xstrdup("1"));
2107 return (xstrdup("0"));
2110 /* Callback for session_group. */
2112 format_cb_session_group(struct format_tree
*ft
)
2114 struct session_group
*sg
;
2116 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2117 return (xstrdup(sg
->name
));
2121 /* Callback for session_group_attached. */
2123 format_cb_session_group_attached(struct format_tree
*ft
)
2125 struct session_group
*sg
;
2127 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2128 return (format_printf("%u", session_group_attached_count (sg
)));
2132 /* Callback for session_group_many_attached. */
2134 format_cb_session_group_many_attached(struct format_tree
*ft
)
2136 struct session_group
*sg
;
2138 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
) {
2139 if (session_group_attached_count (sg
) > 1)
2140 return (xstrdup("1"));
2141 return (xstrdup("0"));
2146 /* Callback for session_group_size. */
2148 format_cb_session_group_size(struct format_tree
*ft
)
2150 struct session_group
*sg
;
2152 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2153 return (format_printf("%u", session_group_count (sg
)));
2157 /* Callback for session_grouped. */
2159 format_cb_session_grouped(struct format_tree
*ft
)
2161 if (ft
->s
!= NULL
) {
2162 if (session_group_contains(ft
->s
) != NULL
)
2163 return (xstrdup("1"));
2164 return (xstrdup("0"));
2169 /* Callback for session_id. */
2171 format_cb_session_id(struct format_tree
*ft
)
2174 return (format_printf("$%u", ft
->s
->id
));
2178 /* Callback for session_many_attached. */
2180 format_cb_session_many_attached(struct format_tree
*ft
)
2182 if (ft
->s
!= NULL
) {
2183 if (ft
->s
->attached
> 1)
2184 return (xstrdup("1"));
2185 return (xstrdup("0"));
2190 /* Callback for session_marked. */
2192 format_cb_session_marked(struct format_tree
*ft
)
2194 if (ft
->s
!= NULL
) {
2195 if (server_check_marked() && marked_pane
.s
== ft
->s
)
2196 return (xstrdup("1"));
2197 return (xstrdup("0"));
2202 /* Callback for session_name. */
2204 format_cb_session_name(struct format_tree
*ft
)
2207 return (xstrdup(ft
->s
->name
));
2211 /* Callback for session_path. */
2213 format_cb_session_path(struct format_tree
*ft
)
2216 return (xstrdup(ft
->s
->cwd
));
2220 /* Callback for session_windows. */
2222 format_cb_session_windows(struct format_tree
*ft
)
2225 return (format_printf("%u", winlink_count(&ft
->s
->windows
)));
2229 /* Callback for socket_path. */
2231 format_cb_socket_path(__unused
struct format_tree
*ft
)
2233 return (xstrdup(socket_path
));
2236 /* Callback for version. */
2238 format_cb_version(__unused
struct format_tree
*ft
)
2240 return (xstrdup(getversion()));
2243 /* Callback for active_window_index. */
2245 format_cb_active_window_index(struct format_tree
*ft
)
2248 return (format_printf("%u", ft
->s
->curw
->idx
));
2252 /* Callback for last_window_index. */
2254 format_cb_last_window_index(struct format_tree
*ft
)
2258 if (ft
->s
!= NULL
) {
2259 wl
= RB_MAX(winlinks
, &ft
->s
->windows
);
2260 return (format_printf("%u", wl
->idx
));
2265 /* Callback for window_active. */
2267 format_cb_window_active(struct format_tree
*ft
)
2269 if (ft
->wl
!= NULL
) {
2270 if (ft
->wl
== ft
->wl
->session
->curw
)
2271 return (xstrdup("1"));
2272 return (xstrdup("0"));
2277 /* Callback for window_activity_flag. */
2279 format_cb_window_activity_flag(struct format_tree
*ft
)
2281 if (ft
->wl
!= NULL
) {
2282 if (ft
->wl
->flags
& WINLINK_ACTIVITY
)
2283 return (xstrdup("1"));
2284 return (xstrdup("0"));
2289 /* Callback for window_bell_flag. */
2291 format_cb_window_bell_flag(struct format_tree
*ft
)
2293 if (ft
->wl
!= NULL
) {
2294 if (ft
->wl
->flags
& WINLINK_BELL
)
2295 return (xstrdup("1"));
2296 return (xstrdup("0"));
2301 /* Callback for window_bigger. */
2303 format_cb_window_bigger(struct format_tree
*ft
)
2305 u_int ox
, oy
, sx
, sy
;
2307 if (ft
->c
!= NULL
) {
2308 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2309 return (xstrdup("1"));
2310 return (xstrdup("0"));
2315 /* Callback for window_cell_height. */
2317 format_cb_window_cell_height(struct format_tree
*ft
)
2320 return (format_printf("%u", ft
->w
->ypixel
));
2324 /* Callback for window_cell_width. */
2326 format_cb_window_cell_width(struct format_tree
*ft
)
2329 return (format_printf("%u", ft
->w
->xpixel
));
2333 /* Callback for window_end_flag. */
2335 format_cb_window_end_flag(struct format_tree
*ft
)
2337 if (ft
->wl
!= NULL
) {
2338 if (ft
->wl
== RB_MAX(winlinks
, &ft
->wl
->session
->windows
))
2339 return (xstrdup("1"));
2340 return (xstrdup("0"));
2345 /* Callback for window_flags. */
2347 format_cb_window_flags(struct format_tree
*ft
)
2350 return (xstrdup(window_printable_flags(ft
->wl
, 1)));
2354 /* Callback for window_format. */
2356 format_cb_window_format(struct format_tree
*ft
)
2358 if (ft
->type
== FORMAT_TYPE_WINDOW
)
2359 return (xstrdup("1"));
2360 return (xstrdup("0"));
2363 /* Callback for window_height. */
2365 format_cb_window_height(struct format_tree
*ft
)
2368 return (format_printf("%u", ft
->w
->sy
));
2372 /* Callback for window_id. */
2374 format_cb_window_id(struct format_tree
*ft
)
2377 return (format_printf("@%u", ft
->w
->id
));
2381 /* Callback for window_index. */
2383 format_cb_window_index(struct format_tree
*ft
)
2386 return (format_printf("%d", ft
->wl
->idx
));
2390 /* Callback for window_last_flag. */
2392 format_cb_window_last_flag(struct format_tree
*ft
)
2394 if (ft
->wl
!= NULL
) {
2395 if (ft
->wl
== TAILQ_FIRST(&ft
->wl
->session
->lastw
))
2396 return (xstrdup("1"));
2397 return (xstrdup("0"));
2402 /* Callback for window_linked. */
2404 format_cb_window_linked(struct format_tree
*ft
)
2406 if (ft
->wl
!= NULL
) {
2407 if (session_is_linked(ft
->wl
->session
, ft
->wl
->window
))
2408 return (xstrdup("1"));
2409 return (xstrdup("0"));
2414 /* Callback for window_linked_sessions. */
2416 format_cb_window_linked_sessions(struct format_tree
*ft
)
2419 return (format_printf("%u", ft
->wl
->window
->references
));
2423 /* Callback for window_marked_flag. */
2425 format_cb_window_marked_flag(struct format_tree
*ft
)
2427 if (ft
->wl
!= NULL
) {
2428 if (server_check_marked() && marked_pane
.wl
== ft
->wl
)
2429 return (xstrdup("1"));
2430 return (xstrdup("0"));
2435 /* Callback for window_name. */
2437 format_cb_window_name(struct format_tree
*ft
)
2440 return (format_printf("%s", ft
->w
->name
));
2444 /* Callback for window_offset_x. */
2446 format_cb_window_offset_x(struct format_tree
*ft
)
2448 u_int ox
, oy
, sx
, sy
;
2450 if (ft
->c
!= NULL
) {
2451 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2452 return (format_printf("%u", ox
));
2458 /* Callback for window_offset_y. */
2460 format_cb_window_offset_y(struct format_tree
*ft
)
2462 u_int ox
, oy
, sx
, sy
;
2464 if (ft
->c
!= NULL
) {
2465 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2466 return (format_printf("%u", oy
));
2472 /* Callback for window_panes. */
2474 format_cb_window_panes(struct format_tree
*ft
)
2477 return (format_printf("%u", window_count_panes(ft
->w
)));
2481 /* Callback for window_raw_flags. */
2483 format_cb_window_raw_flags(struct format_tree
*ft
)
2486 return (xstrdup(window_printable_flags(ft
->wl
, 0)));
2490 /* Callback for window_silence_flag. */
2492 format_cb_window_silence_flag(struct format_tree
*ft
)
2494 if (ft
->wl
!= NULL
) {
2495 if (ft
->wl
->flags
& WINLINK_SILENCE
)
2496 return (xstrdup("1"));
2497 return (xstrdup("0"));
2502 /* Callback for window_start_flag. */
2504 format_cb_window_start_flag(struct format_tree
*ft
)
2506 if (ft
->wl
!= NULL
) {
2507 if (ft
->wl
== RB_MIN(winlinks
, &ft
->wl
->session
->windows
))
2508 return (xstrdup("1"));
2509 return (xstrdup("0"));
2514 /* Callback for window_width. */
2516 format_cb_window_width(struct format_tree
*ft
)
2519 return (format_printf("%u", ft
->w
->sx
));
2523 /* Callback for window_zoomed_flag. */
2525 format_cb_window_zoomed_flag(struct format_tree
*ft
)
2527 if (ft
->w
!= NULL
) {
2528 if (ft
->w
->flags
& WINDOW_ZOOMED
)
2529 return (xstrdup("1"));
2530 return (xstrdup("0"));
2535 /* Callback for wrap_flag. */
2537 format_cb_wrap_flag(struct format_tree
*ft
)
2539 if (ft
->wp
!= NULL
) {
2540 if (ft
->wp
->base
.mode
& MODE_WRAP
)
2541 return (xstrdup("1"));
2542 return (xstrdup("0"));
2547 /* Callback for buffer_created. */
2549 format_cb_buffer_created(struct format_tree
*ft
)
2551 static struct timeval tv
;
2553 if (ft
->pb
!= NULL
) {
2555 tv
.tv_sec
= paste_buffer_created(ft
->pb
);
2561 /* Callback for client_activity. */
2563 format_cb_client_activity(struct format_tree
*ft
)
2566 return (&ft
->c
->activity_time
);
2570 /* Callback for client_created. */
2572 format_cb_client_created(struct format_tree
*ft
)
2575 return (&ft
->c
->creation_time
);
2579 /* Callback for session_activity. */
2581 format_cb_session_activity(struct format_tree
*ft
)
2584 return (&ft
->s
->activity_time
);
2588 /* Callback for session_created. */
2590 format_cb_session_created(struct format_tree
*ft
)
2593 return (&ft
->s
->creation_time
);
2597 /* Callback for session_last_attached. */
2599 format_cb_session_last_attached(struct format_tree
*ft
)
2602 return (&ft
->s
->last_attached_time
);
2606 /* Callback for start_time. */
2608 format_cb_start_time(__unused
struct format_tree
*ft
)
2610 return (&start_time
);
2613 /* Callback for window_activity. */
2615 format_cb_window_activity(struct format_tree
*ft
)
2618 return (&ft
->w
->activity_time
);
2622 /* Callback for buffer_mode_format, */
2624 format_cb_buffer_mode_format(__unused
struct format_tree
*ft
)
2626 return (xstrdup(window_buffer_mode
.default_format
));
2629 /* Callback for client_mode_format, */
2631 format_cb_client_mode_format(__unused
struct format_tree
*ft
)
2633 return (xstrdup(window_client_mode
.default_format
));
2636 /* Callback for tree_mode_format, */
2638 format_cb_tree_mode_format(__unused
struct format_tree
*ft
)
2640 return (xstrdup(window_tree_mode
.default_format
));
2643 /* Callback for uid. */
2645 format_cb_uid(__unused
struct format_tree
*ft
)
2647 return (format_printf("%ld", (long)getuid()));
2650 /* Callback for user. */
2652 format_cb_user(__unused
struct format_tree
*ft
)
2656 if ((pw
= getpwuid(getuid())) != NULL
)
2657 return (xstrdup(pw
->pw_name
));
2661 /* Format table type. */
2662 enum format_table_type
{
2663 FORMAT_TABLE_STRING
,
2667 /* Format table entry. */
2668 struct format_table_entry
{
2670 enum format_table_type type
;
2675 * Format table. Default format variables (that are almost always in the tree
2676 * and where the value is expanded by a callback in this file) are listed here.
2677 * Only variables which are added by the caller go into the tree.
2679 static const struct format_table_entry format_table
[] = {
2680 { "active_window_index", FORMAT_TABLE_STRING
,
2681 format_cb_active_window_index
2683 { "alternate_on", FORMAT_TABLE_STRING
,
2684 format_cb_alternate_on
2686 { "alternate_saved_x", FORMAT_TABLE_STRING
,
2687 format_cb_alternate_saved_x
2689 { "alternate_saved_y", FORMAT_TABLE_STRING
,
2690 format_cb_alternate_saved_y
2692 { "buffer_created", FORMAT_TABLE_TIME
,
2693 format_cb_buffer_created
2695 { "buffer_mode_format", FORMAT_TABLE_STRING
,
2696 format_cb_buffer_mode_format
2698 { "buffer_name", FORMAT_TABLE_STRING
,
2699 format_cb_buffer_name
2701 { "buffer_sample", FORMAT_TABLE_STRING
,
2702 format_cb_buffer_sample
2704 { "buffer_size", FORMAT_TABLE_STRING
,
2705 format_cb_buffer_size
2707 { "client_activity", FORMAT_TABLE_TIME
,
2708 format_cb_client_activity
2710 { "client_cell_height", FORMAT_TABLE_STRING
,
2711 format_cb_client_cell_height
2713 { "client_cell_width", FORMAT_TABLE_STRING
,
2714 format_cb_client_cell_width
2716 { "client_control_mode", FORMAT_TABLE_STRING
,
2717 format_cb_client_control_mode
2719 { "client_created", FORMAT_TABLE_TIME
,
2720 format_cb_client_created
2722 { "client_discarded", FORMAT_TABLE_STRING
,
2723 format_cb_client_discarded
2725 { "client_flags", FORMAT_TABLE_STRING
,
2726 format_cb_client_flags
2728 { "client_height", FORMAT_TABLE_STRING
,
2729 format_cb_client_height
2731 { "client_key_table", FORMAT_TABLE_STRING
,
2732 format_cb_client_key_table
2734 { "client_last_session", FORMAT_TABLE_STRING
,
2735 format_cb_client_last_session
2737 { "client_mode_format", FORMAT_TABLE_STRING
,
2738 format_cb_client_mode_format
2740 { "client_name", FORMAT_TABLE_STRING
,
2741 format_cb_client_name
2743 { "client_pid", FORMAT_TABLE_STRING
,
2744 format_cb_client_pid
2746 { "client_prefix", FORMAT_TABLE_STRING
,
2747 format_cb_client_prefix
2749 { "client_readonly", FORMAT_TABLE_STRING
,
2750 format_cb_client_readonly
2752 { "client_session", FORMAT_TABLE_STRING
,
2753 format_cb_client_session
2755 { "client_termfeatures", FORMAT_TABLE_STRING
,
2756 format_cb_client_termfeatures
2758 { "client_termname", FORMAT_TABLE_STRING
,
2759 format_cb_client_termname
2761 { "client_termtype", FORMAT_TABLE_STRING
,
2762 format_cb_client_termtype
2764 { "client_tty", FORMAT_TABLE_STRING
,
2765 format_cb_client_tty
2767 { "client_uid", FORMAT_TABLE_STRING
,
2768 format_cb_client_uid
2770 { "client_user", FORMAT_TABLE_STRING
,
2771 format_cb_client_user
2773 { "client_utf8", FORMAT_TABLE_STRING
,
2774 format_cb_client_utf8
2776 { "client_width", FORMAT_TABLE_STRING
,
2777 format_cb_client_width
2779 { "client_written", FORMAT_TABLE_STRING
,
2780 format_cb_client_written
2782 { "config_files", FORMAT_TABLE_STRING
,
2783 format_cb_config_files
2785 { "cursor_character", FORMAT_TABLE_STRING
,
2786 format_cb_cursor_character
2788 { "cursor_flag", FORMAT_TABLE_STRING
,
2789 format_cb_cursor_flag
2791 { "cursor_x", FORMAT_TABLE_STRING
,
2794 { "cursor_y", FORMAT_TABLE_STRING
,
2797 { "history_all_bytes", FORMAT_TABLE_STRING
,
2798 format_cb_history_all_bytes
2800 { "history_bytes", FORMAT_TABLE_STRING
,
2801 format_cb_history_bytes
2803 { "history_limit", FORMAT_TABLE_STRING
,
2804 format_cb_history_limit
2806 { "history_size", FORMAT_TABLE_STRING
,
2807 format_cb_history_size
2809 { "host", FORMAT_TABLE_STRING
,
2812 { "host_short", FORMAT_TABLE_STRING
,
2813 format_cb_host_short
2815 { "insert_flag", FORMAT_TABLE_STRING
,
2816 format_cb_insert_flag
2818 { "keypad_cursor_flag", FORMAT_TABLE_STRING
,
2819 format_cb_keypad_cursor_flag
2821 { "keypad_flag", FORMAT_TABLE_STRING
,
2822 format_cb_keypad_flag
2824 { "last_window_index", FORMAT_TABLE_STRING
,
2825 format_cb_last_window_index
2827 { "mouse_all_flag", FORMAT_TABLE_STRING
,
2828 format_cb_mouse_all_flag
2830 { "mouse_any_flag", FORMAT_TABLE_STRING
,
2831 format_cb_mouse_any_flag
2833 { "mouse_button_flag", FORMAT_TABLE_STRING
,
2834 format_cb_mouse_button_flag
2836 { "mouse_hyperlink", FORMAT_TABLE_STRING
,
2837 format_cb_mouse_hyperlink
2839 { "mouse_line", FORMAT_TABLE_STRING
,
2840 format_cb_mouse_line
2842 { "mouse_pane", FORMAT_TABLE_STRING
,
2843 format_cb_mouse_pane
2845 { "mouse_sgr_flag", FORMAT_TABLE_STRING
,
2846 format_cb_mouse_sgr_flag
2848 { "mouse_standard_flag", FORMAT_TABLE_STRING
,
2849 format_cb_mouse_standard_flag
2851 { "mouse_utf8_flag", FORMAT_TABLE_STRING
,
2852 format_cb_mouse_utf8_flag
2854 { "mouse_word", FORMAT_TABLE_STRING
,
2855 format_cb_mouse_word
2857 { "mouse_x", FORMAT_TABLE_STRING
,
2860 { "mouse_y", FORMAT_TABLE_STRING
,
2863 { "next_session_id", FORMAT_TABLE_STRING
,
2864 format_cb_next_session_id
2866 { "origin_flag", FORMAT_TABLE_STRING
,
2867 format_cb_origin_flag
2869 { "pane_active", FORMAT_TABLE_STRING
,
2870 format_cb_pane_active
2872 { "pane_at_bottom", FORMAT_TABLE_STRING
,
2873 format_cb_pane_at_bottom
2875 { "pane_at_left", FORMAT_TABLE_STRING
,
2876 format_cb_pane_at_left
2878 { "pane_at_right", FORMAT_TABLE_STRING
,
2879 format_cb_pane_at_right
2881 { "pane_at_top", FORMAT_TABLE_STRING
,
2882 format_cb_pane_at_top
2884 { "pane_bg", FORMAT_TABLE_STRING
,
2887 { "pane_bottom", FORMAT_TABLE_STRING
,
2888 format_cb_pane_bottom
2890 { "pane_current_command", FORMAT_TABLE_STRING
,
2891 format_cb_current_command
2893 { "pane_current_path", FORMAT_TABLE_STRING
,
2894 format_cb_current_path
2896 { "pane_dead", FORMAT_TABLE_STRING
,
2899 { "pane_dead_signal", FORMAT_TABLE_STRING
,
2900 format_cb_pane_dead_signal
2902 { "pane_dead_status", FORMAT_TABLE_STRING
,
2903 format_cb_pane_dead_status
2905 { "pane_dead_time", FORMAT_TABLE_TIME
,
2906 format_cb_pane_dead_time
2908 { "pane_fg", FORMAT_TABLE_STRING
,
2911 { "pane_format", FORMAT_TABLE_STRING
,
2912 format_cb_pane_format
2914 { "pane_height", FORMAT_TABLE_STRING
,
2915 format_cb_pane_height
2917 { "pane_id", FORMAT_TABLE_STRING
,
2920 { "pane_in_mode", FORMAT_TABLE_STRING
,
2921 format_cb_pane_in_mode
2923 { "pane_index", FORMAT_TABLE_STRING
,
2924 format_cb_pane_index
2926 { "pane_input_off", FORMAT_TABLE_STRING
,
2927 format_cb_pane_input_off
2929 { "pane_last", FORMAT_TABLE_STRING
,
2932 { "pane_left", FORMAT_TABLE_STRING
,
2935 { "pane_marked", FORMAT_TABLE_STRING
,
2936 format_cb_pane_marked
2938 { "pane_marked_set", FORMAT_TABLE_STRING
,
2939 format_cb_pane_marked_set
2941 { "pane_mode", FORMAT_TABLE_STRING
,
2944 { "pane_path", FORMAT_TABLE_STRING
,
2947 { "pane_pid", FORMAT_TABLE_STRING
,
2950 { "pane_pipe", FORMAT_TABLE_STRING
,
2953 { "pane_right", FORMAT_TABLE_STRING
,
2954 format_cb_pane_right
2956 { "pane_search_string", FORMAT_TABLE_STRING
,
2957 format_cb_pane_search_string
2959 { "pane_start_command", FORMAT_TABLE_STRING
,
2960 format_cb_start_command
2962 { "pane_start_path", FORMAT_TABLE_STRING
,
2963 format_cb_start_path
2965 { "pane_synchronized", FORMAT_TABLE_STRING
,
2966 format_cb_pane_synchronized
2968 { "pane_tabs", FORMAT_TABLE_STRING
,
2971 { "pane_title", FORMAT_TABLE_STRING
,
2972 format_cb_pane_title
2974 { "pane_top", FORMAT_TABLE_STRING
,
2977 { "pane_tty", FORMAT_TABLE_STRING
,
2980 { "pane_unseen_changes", FORMAT_TABLE_STRING
,
2981 format_cb_pane_unseen_changes
2983 { "pane_width", FORMAT_TABLE_STRING
,
2984 format_cb_pane_width
2986 { "pid", FORMAT_TABLE_STRING
,
2989 { "scroll_region_lower", FORMAT_TABLE_STRING
,
2990 format_cb_scroll_region_lower
2992 { "scroll_region_upper", FORMAT_TABLE_STRING
,
2993 format_cb_scroll_region_upper
2995 { "server_sessions", FORMAT_TABLE_STRING
,
2996 format_cb_server_sessions
2998 { "session_activity", FORMAT_TABLE_TIME
,
2999 format_cb_session_activity
3001 { "session_alerts", FORMAT_TABLE_STRING
,
3002 format_cb_session_alerts
3004 { "session_attached", FORMAT_TABLE_STRING
,
3005 format_cb_session_attached
3007 { "session_attached_list", FORMAT_TABLE_STRING
,
3008 format_cb_session_attached_list
3010 { "session_created", FORMAT_TABLE_TIME
,
3011 format_cb_session_created
3013 { "session_format", FORMAT_TABLE_STRING
,
3014 format_cb_session_format
3016 { "session_group", FORMAT_TABLE_STRING
,
3017 format_cb_session_group
3019 { "session_group_attached", FORMAT_TABLE_STRING
,
3020 format_cb_session_group_attached
3022 { "session_group_attached_list", FORMAT_TABLE_STRING
,
3023 format_cb_session_group_attached_list
3025 { "session_group_list", FORMAT_TABLE_STRING
,
3026 format_cb_session_group_list
3028 { "session_group_many_attached", FORMAT_TABLE_STRING
,
3029 format_cb_session_group_many_attached
3031 { "session_group_size", FORMAT_TABLE_STRING
,
3032 format_cb_session_group_size
3034 { "session_grouped", FORMAT_TABLE_STRING
,
3035 format_cb_session_grouped
3037 { "session_id", FORMAT_TABLE_STRING
,
3038 format_cb_session_id
3040 { "session_last_attached", FORMAT_TABLE_TIME
,
3041 format_cb_session_last_attached
3043 { "session_many_attached", FORMAT_TABLE_STRING
,
3044 format_cb_session_many_attached
3046 { "session_marked", FORMAT_TABLE_STRING
,
3047 format_cb_session_marked
,
3049 { "session_name", FORMAT_TABLE_STRING
,
3050 format_cb_session_name
3052 { "session_path", FORMAT_TABLE_STRING
,
3053 format_cb_session_path
3055 { "session_stack", FORMAT_TABLE_STRING
,
3056 format_cb_session_stack
3058 { "session_windows", FORMAT_TABLE_STRING
,
3059 format_cb_session_windows
3061 { "socket_path", FORMAT_TABLE_STRING
,
3062 format_cb_socket_path
3064 { "start_time", FORMAT_TABLE_TIME
,
3065 format_cb_start_time
3067 { "tree_mode_format", FORMAT_TABLE_STRING
,
3068 format_cb_tree_mode_format
3070 { "uid", FORMAT_TABLE_STRING
,
3073 { "user", FORMAT_TABLE_STRING
,
3076 { "version", FORMAT_TABLE_STRING
,
3079 { "window_active", FORMAT_TABLE_STRING
,
3080 format_cb_window_active
3082 { "window_active_clients", FORMAT_TABLE_STRING
,
3083 format_cb_window_active_clients
3085 { "window_active_clients_list", FORMAT_TABLE_STRING
,
3086 format_cb_window_active_clients_list
3088 { "window_active_sessions", FORMAT_TABLE_STRING
,
3089 format_cb_window_active_sessions
3091 { "window_active_sessions_list", FORMAT_TABLE_STRING
,
3092 format_cb_window_active_sessions_list
3094 { "window_activity", FORMAT_TABLE_TIME
,
3095 format_cb_window_activity
3097 { "window_activity_flag", FORMAT_TABLE_STRING
,
3098 format_cb_window_activity_flag
3100 { "window_bell_flag", FORMAT_TABLE_STRING
,
3101 format_cb_window_bell_flag
3103 { "window_bigger", FORMAT_TABLE_STRING
,
3104 format_cb_window_bigger
3106 { "window_cell_height", FORMAT_TABLE_STRING
,
3107 format_cb_window_cell_height
3109 { "window_cell_width", FORMAT_TABLE_STRING
,
3110 format_cb_window_cell_width
3112 { "window_end_flag", FORMAT_TABLE_STRING
,
3113 format_cb_window_end_flag
3115 { "window_flags", FORMAT_TABLE_STRING
,
3116 format_cb_window_flags
3118 { "window_format", FORMAT_TABLE_STRING
,
3119 format_cb_window_format
3121 { "window_height", FORMAT_TABLE_STRING
,
3122 format_cb_window_height
3124 { "window_id", FORMAT_TABLE_STRING
,
3127 { "window_index", FORMAT_TABLE_STRING
,
3128 format_cb_window_index
3130 { "window_last_flag", FORMAT_TABLE_STRING
,
3131 format_cb_window_last_flag
3133 { "window_layout", FORMAT_TABLE_STRING
,
3134 format_cb_window_layout
3136 { "window_linked", FORMAT_TABLE_STRING
,
3137 format_cb_window_linked
3139 { "window_linked_sessions", FORMAT_TABLE_STRING
,
3140 format_cb_window_linked_sessions
3142 { "window_linked_sessions_list", FORMAT_TABLE_STRING
,
3143 format_cb_window_linked_sessions_list
3145 { "window_marked_flag", FORMAT_TABLE_STRING
,
3146 format_cb_window_marked_flag
3148 { "window_name", FORMAT_TABLE_STRING
,
3149 format_cb_window_name
3151 { "window_offset_x", FORMAT_TABLE_STRING
,
3152 format_cb_window_offset_x
3154 { "window_offset_y", FORMAT_TABLE_STRING
,
3155 format_cb_window_offset_y
3157 { "window_panes", FORMAT_TABLE_STRING
,
3158 format_cb_window_panes
3160 { "window_raw_flags", FORMAT_TABLE_STRING
,
3161 format_cb_window_raw_flags
3163 { "window_silence_flag", FORMAT_TABLE_STRING
,
3164 format_cb_window_silence_flag
3166 { "window_stack_index", FORMAT_TABLE_STRING
,
3167 format_cb_window_stack_index
3169 { "window_start_flag", FORMAT_TABLE_STRING
,
3170 format_cb_window_start_flag
3172 { "window_visible_layout", FORMAT_TABLE_STRING
,
3173 format_cb_window_visible_layout
3175 { "window_width", FORMAT_TABLE_STRING
,
3176 format_cb_window_width
3178 { "window_zoomed_flag", FORMAT_TABLE_STRING
,
3179 format_cb_window_zoomed_flag
3181 { "wrap_flag", FORMAT_TABLE_STRING
,
3186 /* Compare format table entries. */
3188 format_table_compare(const void *key0
, const void *entry0
)
3190 const char *key
= key0
;
3191 const struct format_table_entry
*entry
= entry0
;
3193 return (strcmp(key
, entry
->key
));
3196 /* Get a format callback. */
3197 static struct format_table_entry
*
3198 format_table_get(const char *key
)
3200 return (bsearch(key
, format_table
, nitems(format_table
),
3201 sizeof *format_table
, format_table_compare
));
3204 /* Merge one format tree into another. */
3206 format_merge(struct format_tree
*ft
, struct format_tree
*from
)
3208 struct format_entry
*fe
;
3210 RB_FOREACH(fe
, format_entry_tree
, &from
->tree
) {
3211 if (fe
->value
!= NULL
)
3212 format_add(ft
, fe
->key
, "%s", fe
->value
);
3216 /* Get format pane. */
3217 struct window_pane
*
3218 format_get_pane(struct format_tree
*ft
)
3223 /* Add item bits to tree. */
3225 format_create_add_item(struct format_tree
*ft
, struct cmdq_item
*item
)
3227 struct key_event
*event
= cmdq_get_event(item
);
3228 struct mouse_event
*m
= &event
->m
;
3230 cmdq_merge_formats(item
, ft
);
3231 memcpy(&ft
->m
, m
, sizeof ft
->m
);
3234 /* Create a new tree. */
3235 struct format_tree
*
3236 format_create(struct client
*c
, struct cmdq_item
*item
, int tag
, int flags
)
3238 struct format_tree
*ft
;
3240 ft
= xcalloc(1, sizeof *ft
);
3245 ft
->client
->references
++;
3253 format_create_add_item(ft
, item
);
3260 format_free(struct format_tree
*ft
)
3262 struct format_entry
*fe
, *fe1
;
3264 RB_FOREACH_SAFE(fe
, format_entry_tree
, &ft
->tree
, fe1
) {
3265 RB_REMOVE(format_entry_tree
, &ft
->tree
, fe
);
3271 if (ft
->client
!= NULL
)
3272 server_client_unref(ft
->client
);
3276 /* Log each format. */
3278 format_log_debug_cb(const char *key
, const char *value
, void *arg
)
3280 const char *prefix
= arg
;
3282 log_debug("%s: %s=%s", prefix
, key
, value
);
3285 /* Log a format tree. */
3287 format_log_debug(struct format_tree
*ft
, const char *prefix
)
3289 format_each(ft
, format_log_debug_cb
, (void *)prefix
);
3292 /* Walk each format. */
3294 format_each(struct format_tree
*ft
, void (*cb
)(const char *, const char *,
3297 const struct format_table_entry
*fte
;
3298 struct format_entry
*fe
;
3304 for (i
= 0; i
< nitems(format_table
); i
++) {
3305 fte
= &format_table
[i
];
3307 value
= fte
->cb(ft
);
3310 if (fte
->type
== FORMAT_TABLE_TIME
) {
3312 xsnprintf(s
, sizeof s
, "%lld", (long long)tv
->tv_sec
);
3313 cb(fte
->key
, s
, arg
);
3315 cb(fte
->key
, value
, arg
);
3319 RB_FOREACH(fe
, format_entry_tree
, &ft
->tree
) {
3320 if (fe
->time
!= 0) {
3321 xsnprintf(s
, sizeof s
, "%lld", (long long)fe
->time
);
3322 cb(fe
->key
, s
, arg
);
3324 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3325 fe
->value
= fe
->cb(ft
);
3326 if (fe
->value
== NULL
)
3327 fe
->value
= xstrdup("");
3329 cb(fe
->key
, fe
->value
, arg
);
3334 /* Add a key-value pair. */
3336 format_add(struct format_tree
*ft
, const char *key
, const char *fmt
, ...)
3338 struct format_entry
*fe
;
3339 struct format_entry
*fe_now
;
3342 fe
= xmalloc(sizeof *fe
);
3343 fe
->key
= xstrdup(key
);
3345 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3346 if (fe_now
!= NULL
) {
3349 free(fe_now
->value
);
3357 xvasprintf(&fe
->value
, fmt
, ap
);
3361 /* Add a key and time. */
3363 format_add_tv(struct format_tree
*ft
, const char *key
, struct timeval
*tv
)
3365 struct format_entry
*fe
, *fe_now
;
3367 fe
= xmalloc(sizeof *fe
);
3368 fe
->key
= xstrdup(key
);
3370 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3371 if (fe_now
!= NULL
) {
3374 free(fe_now
->value
);
3379 fe
->time
= tv
->tv_sec
;
3384 /* Add a key and function. */
3386 format_add_cb(struct format_tree
*ft
, const char *key
, format_cb cb
)
3388 struct format_entry
*fe
;
3389 struct format_entry
*fe_now
;
3391 fe
= xmalloc(sizeof *fe
);
3392 fe
->key
= xstrdup(key
);
3394 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3395 if (fe_now
!= NULL
) {
3398 free(fe_now
->value
);
3408 /* Quote shell special characters in string. */
3410 format_quote_shell(const char *s
)
3415 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3416 for (cp
= s
; *cp
!= '\0'; cp
++) {
3417 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp
) != NULL
)
3425 /* Quote #s in string. */
3427 format_quote_style(const char *s
)
3432 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3433 for (cp
= s
; *cp
!= '\0'; cp
++) {
3442 /* Make a prettier time. */
3444 format_pretty_time(time_t t
, int seconds
)
3446 struct tm now_tm
, tm
;
3455 localtime_r(&now
, &now_tm
);
3456 localtime_r(&t
, &tm
);
3458 /* Last 24 hours. */
3459 if (age
< 24 * 3600) {
3461 strftime(s
, sizeof s
, "%H:%M:%S", &tm
);
3463 strftime(s
, sizeof s
, "%H:%M", &tm
);
3464 return (xstrdup(s
));
3467 /* This month or last 28 days. */
3468 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
== now_tm
.tm_mon
) ||
3469 age
< 28 * 24 * 3600) {
3470 strftime(s
, sizeof s
, "%a%d", &tm
);
3471 return (xstrdup(s
));
3474 /* Last 12 months. */
3475 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
< now_tm
.tm_mon
) ||
3476 (tm
.tm_year
== now_tm
.tm_year
- 1 && tm
.tm_mon
> now_tm
.tm_mon
)) {
3477 strftime(s
, sizeof s
, "%d%b", &tm
);
3478 return (xstrdup(s
));
3481 /* Older than that. */
3482 strftime(s
, sizeof s
, "%h%y", &tm
);
3483 return (xstrdup(s
));
3486 /* Find a format entry. */
3488 format_find(struct format_tree
*ft
, const char *key
, int modifiers
,
3489 const char *time_format
)
3491 struct format_table_entry
*fte
;
3493 struct format_entry
*fe
, fe_find
;
3494 struct environ_entry
*envent
;
3495 struct options_entry
*o
;
3497 char *found
= NULL
, *saved
, s
[512];
3502 o
= options_parse_get(global_options
, key
, &idx
, 0);
3503 if (o
== NULL
&& ft
->wp
!= NULL
)
3504 o
= options_parse_get(ft
->wp
->options
, key
, &idx
, 0);
3505 if (o
== NULL
&& ft
->w
!= NULL
)
3506 o
= options_parse_get(ft
->w
->options
, key
, &idx
, 0);
3508 o
= options_parse_get(global_w_options
, key
, &idx
, 0);
3509 if (o
== NULL
&& ft
->s
!= NULL
)
3510 o
= options_parse_get(ft
->s
->options
, key
, &idx
, 0);
3512 o
= options_parse_get(global_s_options
, key
, &idx
, 0);
3514 found
= options_to_string(o
, idx
, 1);
3518 fte
= format_table_get(key
);
3520 value
= fte
->cb(ft
);
3521 if (fte
->type
== FORMAT_TABLE_TIME
&& value
!= NULL
)
3522 t
= ((struct timeval
*)value
)->tv_sec
;
3527 fe_find
.key
= (char *)key
;
3528 fe
= RB_FIND(format_entry_tree
, &ft
->tree
, &fe_find
);
3530 if (fe
->time
!= 0) {
3534 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3535 fe
->value
= fe
->cb(ft
);
3536 if (fe
->value
== NULL
)
3537 fe
->value
= xstrdup("");
3539 found
= xstrdup(fe
->value
);
3543 if (~modifiers
& FORMAT_TIMESTRING
) {
3546 envent
= environ_find(ft
->s
->environ
, key
);
3548 envent
= environ_find(global_environ
, key
);
3549 if (envent
!= NULL
&& envent
->value
!= NULL
) {
3550 found
= xstrdup(envent
->value
);
3558 if (modifiers
& FORMAT_TIMESTRING
) {
3559 if (t
== 0 && found
!= NULL
) {
3560 t
= strtonum(found
, 0, INT64_MAX
, &errstr
);
3567 if (modifiers
& FORMAT_PRETTY
)
3568 found
= format_pretty_time(t
, 0);
3570 if (time_format
!= NULL
) {
3571 localtime_r(&t
, &tm
);
3572 strftime(s
, sizeof s
, time_format
, &tm
);
3575 s
[strcspn(s
, "\n")] = '\0';
3583 xasprintf(&found
, "%lld", (long long)t
);
3584 else if (found
== NULL
)
3586 if (modifiers
& FORMAT_BASENAME
) {
3588 found
= xstrdup(basename(saved
));
3591 if (modifiers
& FORMAT_DIRNAME
) {
3593 found
= xstrdup(dirname(saved
));
3596 if (modifiers
& FORMAT_QUOTE_SHELL
) {
3598 found
= format_quote_shell(saved
);
3601 if (modifiers
& FORMAT_QUOTE_STYLE
) {
3603 found
= format_quote_style(saved
);
3609 /* Unescape escaped characters. */
3611 format_unescape(const char *s
)
3616 cp
= out
= xmalloc(strlen(s
) + 1);
3617 for (; *s
!= '\0'; s
++) {
3618 if (*s
== '#' && s
[1] == '{')
3620 if (brackets
== 0 &&
3622 strchr(",#{}:", s
[1]) != NULL
) {
3634 /* Remove escaped characters. */
3636 format_strip(const char *s
)
3641 cp
= out
= xmalloc(strlen(s
) + 1);
3642 for (; *s
!= '\0'; s
++) {
3643 if (*s
== '#' && s
[1] == '{')
3645 if (*s
== '#' && strchr(",#{}:", s
[1]) != NULL
) {
3658 /* Skip until end. */
3660 format_skip(const char *s
, const char *end
)
3664 for (; *s
!= '\0'; s
++) {
3665 if (*s
== '#' && s
[1] == '{')
3669 strchr(",#{}:", s
[1]) != NULL
) {
3675 if (strchr(end
, *s
) != NULL
&& brackets
== 0)
3683 /* Return left and right alternatives separated by commas. */
3685 format_choose(struct format_expand_state
*es
, const char *s
, char **left
,
3686 char **right
, int expand
)
3689 char *left0
, *right0
;
3691 cp
= format_skip(s
, ",");
3694 left0
= xstrndup(s
, cp
- s
);
3695 right0
= xstrdup(cp
+ 1);
3698 *left
= format_expand1(es
, left0
);
3700 *right
= format_expand1(es
, right0
);
3711 format_true(const char *s
)
3713 if (s
!= NULL
&& *s
!= '\0' && (s
[0] != '0' || s
[1] != '\0'))
3718 /* Check if modifier end. */
3720 format_is_end(char c
)
3722 return (c
== ';' || c
== ':');
3725 /* Add to modifier list. */
3727 format_add_modifier(struct format_modifier
**list
, u_int
*count
,
3728 const char *c
, size_t n
, char **argv
, int argc
)
3730 struct format_modifier
*fm
;
3732 *list
= xreallocarray(*list
, (*count
) + 1, sizeof **list
);
3733 fm
= &(*list
)[(*count
)++];
3735 memcpy(fm
->modifier
, c
, n
);
3736 fm
->modifier
[n
] = '\0';
3743 /* Free modifier list. */
3745 format_free_modifiers(struct format_modifier
*list
, u_int count
)
3749 for (i
= 0; i
< count
; i
++)
3750 cmd_free_argv(list
[i
].argc
, list
[i
].argv
);
3754 /* Build modifier list. */
3755 static struct format_modifier
*
3756 format_build_modifiers(struct format_expand_state
*es
, const char **s
,
3759 const char *cp
= *s
, *end
;
3760 struct format_modifier
*list
= NULL
;
3761 char c
, last
[] = "X;:", **argv
, *value
;
3765 * Modifiers are a ; separated list of the forms:
3766 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3777 while (*cp
!= '\0' && *cp
!= ':') {
3778 /* Skip any separator character. */
3782 /* Check single character modifiers with no arguments. */
3783 if (strchr("labcdnwETSWPL<>", cp
[0]) != NULL
&&
3784 format_is_end(cp
[1])) {
3785 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3790 /* Then try double character with no arguments. */
3791 if ((memcmp("||", cp
, 2) == 0 ||
3792 memcmp("&&", cp
, 2) == 0 ||
3793 memcmp("!=", cp
, 2) == 0 ||
3794 memcmp("==", cp
, 2) == 0 ||
3795 memcmp("<=", cp
, 2) == 0 ||
3796 memcmp(">=", cp
, 2) == 0) &&
3797 format_is_end(cp
[2])) {
3798 format_add_modifier(&list
, count
, cp
, 2, NULL
, 0);
3803 /* Now try single character with arguments. */
3804 if (strchr("mCNst=peq", cp
[0]) == NULL
)
3808 /* No arguments provided. */
3809 if (format_is_end(cp
[1])) {
3810 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3817 /* Single argument with no wrapper character. */
3818 if (!ispunct((u_char
)cp
[1]) || cp
[1] == '-') {
3819 end
= format_skip(cp
+ 1, ":;");
3823 argv
= xcalloc(1, sizeof *argv
);
3824 value
= xstrndup(cp
+ 1, end
- (cp
+ 1));
3825 argv
[0] = format_expand1(es
, value
);
3829 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3834 /* Multiple arguments with a wrapper character. */
3838 if (cp
[0] == last
[0] && format_is_end(cp
[1])) {
3842 end
= format_skip(cp
+ 1, last
);
3847 argv
= xreallocarray(argv
, argc
+ 1, sizeof *argv
);
3848 value
= xstrndup(cp
, end
- cp
);
3849 argv
[argc
++] = format_expand1(es
, value
);
3853 } while (!format_is_end(cp
[0]));
3854 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3857 format_free_modifiers(list
, *count
);
3865 /* Match against an fnmatch(3) pattern or regular expression. */
3867 format_match(struct format_modifier
*fm
, const char *pattern
, const char *text
)
3875 if (strchr(s
, 'r') == NULL
) {
3876 if (strchr(s
, 'i') != NULL
)
3877 flags
|= FNM_CASEFOLD
;
3878 if (fnmatch(pattern
, text
, flags
) != 0)
3879 return (xstrdup("0"));
3881 flags
= REG_EXTENDED
|REG_NOSUB
;
3882 if (strchr(s
, 'i') != NULL
)
3884 if (regcomp(&r
, pattern
, flags
) != 0)
3885 return (xstrdup("0"));
3886 if (regexec(&r
, text
, 0, NULL
, 0) != 0) {
3888 return (xstrdup("0"));
3892 return (xstrdup("1"));
3895 /* Perform substitution in string. */
3897 format_sub(struct format_modifier
*fm
, const char *text
, const char *pattern
,
3901 int flags
= REG_EXTENDED
;
3903 if (fm
->argc
>= 3 && strchr(fm
->argv
[2], 'i') != NULL
)
3905 value
= regsub(pattern
, with
, text
, flags
);
3907 return (xstrdup(text
));
3911 /* Search inside pane. */
3913 format_search(struct format_modifier
*fm
, struct window_pane
*wp
, const char *s
)
3915 int ignore
= 0, regex
= 0;
3918 if (fm
->argc
>= 1) {
3919 if (strchr(fm
->argv
[0], 'i') != NULL
)
3921 if (strchr(fm
->argv
[0], 'r') != NULL
)
3924 xasprintf(&value
, "%u", window_pane_search(wp
, s
, regex
, ignore
));
3928 /* Does session name exist? */
3930 format_session_name(struct format_expand_state
*es
, const char *fmt
)
3935 name
= format_expand1(es
, fmt
);
3936 RB_FOREACH(s
, sessions
, &sessions
) {
3937 if (strcmp(s
->name
, name
) == 0) {
3939 return (xstrdup("1"));
3943 return (xstrdup("0"));
3946 /* Loop over sessions. */
3948 format_loop_sessions(struct format_expand_state
*es
, const char *fmt
)
3950 struct format_tree
*ft
= es
->ft
;
3951 struct client
*c
= ft
->client
;
3952 struct cmdq_item
*item
= ft
->item
;
3953 struct format_tree
*nft
;
3954 struct format_expand_state next
;
3955 char *expanded
, *value
;
3959 value
= xcalloc(1, 1);
3962 RB_FOREACH(s
, sessions
, &sessions
) {
3963 format_log(es
, "session loop: $%u", s
->id
);
3964 nft
= format_create(c
, item
, FORMAT_NONE
, ft
->flags
);
3965 format_defaults(nft
, ft
->c
, s
, NULL
, NULL
);
3966 format_copy_state(&next
, es
, 0);
3968 expanded
= format_expand1(&next
, fmt
);
3969 format_free(next
.ft
);
3971 valuelen
+= strlen(expanded
);
3972 value
= xrealloc(value
, valuelen
);
3974 strlcat(value
, expanded
, valuelen
);
3981 /* Does window name exist? */
3983 format_window_name(struct format_expand_state
*es
, const char *fmt
)
3985 struct format_tree
*ft
= es
->ft
;
3989 if (ft
->s
== NULL
) {
3990 format_log(es
, "window name but no session");
3994 name
= format_expand1(es
, fmt
);
3995 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
3996 if (strcmp(wl
->window
->name
, name
) == 0) {
3998 return (xstrdup("1"));
4002 return (xstrdup("0"));
4005 /* Loop over windows. */
4007 format_loop_windows(struct format_expand_state
*es
, const char *fmt
)
4009 struct format_tree
*ft
= es
->ft
;
4010 struct client
*c
= ft
->client
;
4011 struct cmdq_item
*item
= ft
->item
;
4012 struct format_tree
*nft
;
4013 struct format_expand_state next
;
4014 char *all
, *active
, *use
, *expanded
, *value
;
4019 if (ft
->s
== NULL
) {
4020 format_log(es
, "window loop but no session");
4024 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4029 value
= xcalloc(1, 1);
4032 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
4034 format_log(es
, "window loop: %u @%u", wl
->idx
, w
->id
);
4035 if (active
!= NULL
&& wl
== ft
->s
->curw
)
4039 nft
= format_create(c
, item
, FORMAT_WINDOW
|w
->id
, ft
->flags
);
4040 format_defaults(nft
, ft
->c
, ft
->s
, wl
, NULL
);
4041 format_copy_state(&next
, es
, 0);
4043 expanded
= format_expand1(&next
, use
);
4046 valuelen
+= strlen(expanded
);
4047 value
= xrealloc(value
, valuelen
);
4049 strlcat(value
, expanded
, valuelen
);
4059 /* Loop over panes. */
4061 format_loop_panes(struct format_expand_state
*es
, const char *fmt
)
4063 struct format_tree
*ft
= es
->ft
;
4064 struct client
*c
= ft
->client
;
4065 struct cmdq_item
*item
= ft
->item
;
4066 struct format_tree
*nft
;
4067 struct format_expand_state next
;
4068 char *all
, *active
, *use
, *expanded
, *value
;
4070 struct window_pane
*wp
;
4072 if (ft
->w
== NULL
) {
4073 format_log(es
, "pane loop but no window");
4077 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4082 value
= xcalloc(1, 1);
4085 TAILQ_FOREACH(wp
, &ft
->w
->panes
, entry
) {
4086 format_log(es
, "pane loop: %%%u", wp
->id
);
4087 if (active
!= NULL
&& wp
== ft
->w
->active
)
4091 nft
= format_create(c
, item
, FORMAT_PANE
|wp
->id
, ft
->flags
);
4092 format_defaults(nft
, ft
->c
, ft
->s
, ft
->wl
, wp
);
4093 format_copy_state(&next
, es
, 0);
4095 expanded
= format_expand1(&next
, use
);
4098 valuelen
+= strlen(expanded
);
4099 value
= xrealloc(value
, valuelen
);
4101 strlcat(value
, expanded
, valuelen
);
4111 /* Loop over clients. */
4113 format_loop_clients(struct format_expand_state
*es
, const char *fmt
)
4115 struct format_tree
*ft
= es
->ft
;
4116 struct client
*c
= ft
->client
;
4117 struct cmdq_item
*item
= ft
->item
;
4118 struct format_tree
*nft
;
4119 struct format_expand_state next
;
4120 char *expanded
, *value
;
4123 value
= xcalloc(1, 1);
4126 TAILQ_FOREACH(c
, &clients
, entry
) {
4127 format_log(es
, "client loop: %s", c
->name
);
4128 nft
= format_create(c
, item
, 0, ft
->flags
);
4129 format_defaults(nft
, c
, ft
->s
, ft
->wl
, ft
->wp
);
4130 format_copy_state(&next
, es
, 0);
4132 expanded
= format_expand1(&next
, fmt
);
4135 valuelen
+= strlen(expanded
);
4136 value
= xrealloc(value
, valuelen
);
4138 strlcat(value
, expanded
, valuelen
);
4146 format_replace_expression(struct format_modifier
*mexp
,
4147 struct format_expand_state
*es
, const char *copy
)
4149 int argc
= mexp
->argc
;
4151 char *endch
, *value
, *left
= NULL
, *right
= NULL
;
4154 double mleft
, mright
, result
;
4165 LESS_THAN_EQUAL
} operator;
4167 if (strcmp(mexp
->argv
[0], "+") == 0)
4169 else if (strcmp(mexp
->argv
[0], "-") == 0)
4170 operator = SUBTRACT
;
4171 else if (strcmp(mexp
->argv
[0], "*") == 0)
4172 operator = MULTIPLY
;
4173 else if (strcmp(mexp
->argv
[0], "/") == 0)
4175 else if (strcmp(mexp
->argv
[0], "%") == 0 ||
4176 strcmp(mexp
->argv
[0], "m") == 0)
4178 else if (strcmp(mexp
->argv
[0], "==") == 0)
4180 else if (strcmp(mexp
->argv
[0], "!=") == 0)
4181 operator = NOT_EQUAL
;
4182 else if (strcmp(mexp
->argv
[0], ">") == 0)
4183 operator = GREATER_THAN
;
4184 else if (strcmp(mexp
->argv
[0], "<") == 0)
4185 operator = LESS_THAN
;
4186 else if (strcmp(mexp
->argv
[0], ">=") == 0)
4187 operator = GREATER_THAN_EQUAL
;
4188 else if (strcmp(mexp
->argv
[0], "<=") == 0)
4189 operator = LESS_THAN_EQUAL
;
4191 format_log(es
, "expression has no valid operator: '%s'",
4196 /* The second argument may be flags. */
4197 if (argc
>= 2 && strchr(mexp
->argv
[1], 'f') != NULL
) {
4202 /* The third argument may be precision. */
4204 prec
= strtonum(mexp
->argv
[2], INT_MIN
, INT_MAX
, &errstr
);
4205 if (errstr
!= NULL
) {
4206 format_log(es
, "expression precision %s: %s", errstr
,
4212 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4213 format_log(es
, "expression syntax error");
4217 mleft
= strtod(left
, &endch
);
4218 if (*endch
!= '\0') {
4219 format_log(es
, "expression left side is invalid: %s", left
);
4223 mright
= strtod(right
, &endch
);
4224 if (*endch
!= '\0') {
4225 format_log(es
, "expression right side is invalid: %s", right
);
4230 mleft
= (long long)mleft
;
4231 mright
= (long long)mright
;
4233 format_log(es
, "expression left side is: %.*f", prec
, mleft
);
4234 format_log(es
, "expression right side is: %.*f", prec
, mright
);
4238 result
= mleft
+ mright
;
4241 result
= mleft
- mright
;
4244 result
= mleft
* mright
;
4247 result
= mleft
/ mright
;
4250 result
= fmod(mleft
, mright
);
4253 result
= fabs(mleft
- mright
) < 1e-9;
4256 result
= fabs(mleft
- mright
) > 1e-9;
4259 result
= (mleft
> mright
);
4261 case GREATER_THAN_EQUAL
:
4262 result
= (mleft
>= mright
);
4265 result
= (mleft
< mright
);
4267 case LESS_THAN_EQUAL
:
4268 result
= (mleft
<= mright
);
4272 xasprintf(&value
, "%.*f", prec
, result
);
4274 xasprintf(&value
, "%.*f", prec
, (double)(long long)result
);
4275 format_log(es
, "expression result is %s", value
);
4287 /* Replace a key. */
4289 format_replace(struct format_expand_state
*es
, const char *key
, size_t keylen
,
4290 char **buf
, size_t *len
, size_t *off
)
4292 struct format_tree
*ft
= es
->ft
;
4293 struct window_pane
*wp
= ft
->wp
;
4294 const char *errstr
, *copy
, *cp
, *marker
= NULL
;
4295 const char *time_format
= NULL
;
4296 char *copy0
, *condition
, *found
, *new;
4297 char *value
, *left
, *right
;
4299 int modifiers
= 0, limit
= 0, width
= 0;
4301 struct format_modifier
*list
, *cmp
= NULL
, *search
= NULL
;
4302 struct format_modifier
**sub
= NULL
, *mexp
= NULL
, *fm
;
4303 u_int i
, count
, nsub
= 0;
4304 struct format_expand_state next
;
4306 /* Make a copy of the key. */
4307 copy
= copy0
= xstrndup(key
, keylen
);
4309 /* Process modifier list. */
4310 list
= format_build_modifiers(es
, ©
, &count
);
4311 for (i
= 0; i
< count
; i
++) {
4313 if (format_logging(ft
)) {
4314 format_log(es
, "modifier %u is %s", i
, fm
->modifier
);
4315 for (j
= 0; j
< fm
->argc
; j
++) {
4316 format_log(es
, "modifier %u argument %d: %s", i
,
4320 if (fm
->size
== 1) {
4321 switch (fm
->modifier
[0]) {
4333 sub
= xreallocarray(sub
, nsub
+ 1, sizeof *sub
);
4339 limit
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4343 if (fm
->argc
>= 2 && fm
->argv
[1] != NULL
)
4344 marker
= fm
->argv
[1];
4349 width
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4355 modifiers
|= FORMAT_WIDTH
;
4358 if (fm
->argc
< 1 || fm
->argc
> 3)
4363 modifiers
|= FORMAT_LITERAL
;
4366 modifiers
|= FORMAT_CHARACTER
;
4369 modifiers
|= FORMAT_BASENAME
;
4372 modifiers
|= FORMAT_COLOUR
;
4375 modifiers
|= FORMAT_DIRNAME
;
4378 modifiers
|= FORMAT_LENGTH
;
4381 modifiers
|= FORMAT_TIMESTRING
;
4384 if (strchr(fm
->argv
[0], 'p') != NULL
)
4385 modifiers
|= FORMAT_PRETTY
;
4386 else if (fm
->argc
>= 2 &&
4387 strchr(fm
->argv
[0], 'f') != NULL
)
4388 time_format
= format_strip(fm
->argv
[1]);
4392 modifiers
|= FORMAT_QUOTE_SHELL
;
4393 else if (strchr(fm
->argv
[0], 'e') != NULL
||
4394 strchr(fm
->argv
[0], 'h') != NULL
)
4395 modifiers
|= FORMAT_QUOTE_STYLE
;
4398 modifiers
|= FORMAT_EXPAND
;
4401 modifiers
|= FORMAT_EXPANDTIME
;
4405 strchr(fm
->argv
[0], 'w') != NULL
)
4406 modifiers
|= FORMAT_WINDOW_NAME
;
4407 else if (strchr(fm
->argv
[0], 's') != NULL
)
4408 modifiers
|= FORMAT_SESSION_NAME
;
4411 modifiers
|= FORMAT_SESSIONS
;
4414 modifiers
|= FORMAT_WINDOWS
;
4417 modifiers
|= FORMAT_PANES
;
4420 modifiers
|= FORMAT_CLIENTS
;
4423 } else if (fm
->size
== 2) {
4424 if (strcmp(fm
->modifier
, "||") == 0 ||
4425 strcmp(fm
->modifier
, "&&") == 0 ||
4426 strcmp(fm
->modifier
, "==") == 0 ||
4427 strcmp(fm
->modifier
, "!=") == 0 ||
4428 strcmp(fm
->modifier
, ">=") == 0 ||
4429 strcmp(fm
->modifier
, "<=") == 0)
4434 /* Is this a literal string? */
4435 if (modifiers
& FORMAT_LITERAL
) {
4436 format_log(es
, "literal string is '%s'", copy
);
4437 value
= format_unescape(copy
);
4441 /* Is this a character? */
4442 if (modifiers
& FORMAT_CHARACTER
) {
4443 new = format_expand1(es
, copy
);
4444 c
= strtonum(new, 32, 126, &errstr
);
4446 value
= xstrdup("");
4448 xasprintf(&value
, "%c", c
);
4453 /* Is this a colour? */
4454 if (modifiers
& FORMAT_COLOUR
) {
4455 new = format_expand1(es
, copy
);
4456 c
= colour_fromstring(new);
4457 if (c
== -1 || (c
= colour_force_rgb(c
)) == -1)
4458 value
= xstrdup("");
4460 xasprintf(&value
, "%06x", c
& 0xffffff);
4465 /* Is this a loop, comparison or condition? */
4466 if (modifiers
& FORMAT_SESSIONS
) {
4467 value
= format_loop_sessions(es
, copy
);
4470 } else if (modifiers
& FORMAT_WINDOWS
) {
4471 value
= format_loop_windows(es
, copy
);
4474 } else if (modifiers
& FORMAT_PANES
) {
4475 value
= format_loop_panes(es
, copy
);
4478 } else if (modifiers
& FORMAT_CLIENTS
) {
4479 value
= format_loop_clients(es
, copy
);
4482 } else if (modifiers
& FORMAT_WINDOW_NAME
) {
4483 value
= format_window_name(es
, copy
);
4486 } else if (modifiers
& FORMAT_SESSION_NAME
) {
4487 value
= format_session_name(es
, copy
);
4490 } else if (search
!= NULL
) {
4491 /* Search in pane. */
4492 new = format_expand1(es
, copy
);
4494 format_log(es
, "search '%s' but no pane", new);
4495 value
= xstrdup("0");
4497 format_log(es
, "search '%s' pane %%%u", new, wp
->id
);
4498 value
= format_search(search
, wp
, new);
4501 } else if (cmp
!= NULL
) {
4502 /* Comparison of left and right. */
4503 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4504 format_log(es
, "compare %s syntax error: %s",
4505 cmp
->modifier
, copy
);
4508 format_log(es
, "compare %s left is: %s", cmp
->modifier
, left
);
4509 format_log(es
, "compare %s right is: %s", cmp
->modifier
, right
);
4511 if (strcmp(cmp
->modifier
, "||") == 0) {
4512 if (format_true(left
) || format_true(right
))
4513 value
= xstrdup("1");
4515 value
= xstrdup("0");
4516 } else if (strcmp(cmp
->modifier
, "&&") == 0) {
4517 if (format_true(left
) && format_true(right
))
4518 value
= xstrdup("1");
4520 value
= xstrdup("0");
4521 } else if (strcmp(cmp
->modifier
, "==") == 0) {
4522 if (strcmp(left
, right
) == 0)
4523 value
= xstrdup("1");
4525 value
= xstrdup("0");
4526 } else if (strcmp(cmp
->modifier
, "!=") == 0) {
4527 if (strcmp(left
, right
) != 0)
4528 value
= xstrdup("1");
4530 value
= xstrdup("0");
4531 } else if (strcmp(cmp
->modifier
, "<") == 0) {
4532 if (strcmp(left
, right
) < 0)
4533 value
= xstrdup("1");
4535 value
= xstrdup("0");
4536 } else if (strcmp(cmp
->modifier
, ">") == 0) {
4537 if (strcmp(left
, right
) > 0)
4538 value
= xstrdup("1");
4540 value
= xstrdup("0");
4541 } else if (strcmp(cmp
->modifier
, "<=") == 0) {
4542 if (strcmp(left
, right
) <= 0)
4543 value
= xstrdup("1");
4545 value
= xstrdup("0");
4546 } else if (strcmp(cmp
->modifier
, ">=") == 0) {
4547 if (strcmp(left
, right
) >= 0)
4548 value
= xstrdup("1");
4550 value
= xstrdup("0");
4551 } else if (strcmp(cmp
->modifier
, "m") == 0)
4552 value
= format_match(cmp
, left
, right
);
4556 } else if (*copy
== '?') {
4557 /* Conditional: check first and choose second or third. */
4558 cp
= format_skip(copy
+ 1, ",");
4560 format_log(es
, "condition syntax error: %s", copy
+ 1);
4563 condition
= xstrndup(copy
+ 1, cp
- (copy
+ 1));
4564 format_log(es
, "condition is: %s", condition
);
4566 found
= format_find(ft
, condition
, modifiers
, time_format
);
4567 if (found
== NULL
) {
4569 * If the condition not found, try to expand it. If
4570 * the expansion doesn't have any effect, then assume
4573 found
= format_expand1(es
, condition
);
4574 if (strcmp(found
, condition
) == 0) {
4576 found
= xstrdup("");
4578 "condition '%s' not found; assuming false",
4582 format_log(es
, "condition '%s' found: %s", condition
,
4586 if (format_choose(es
, cp
+ 1, &left
, &right
, 0) != 0) {
4587 format_log(es
, "condition '%s' syntax error: %s",
4592 if (format_true(found
)) {
4593 format_log(es
, "condition '%s' is true", condition
);
4594 value
= format_expand1(es
, left
);
4596 format_log(es
, "condition '%s' is false", condition
);
4597 value
= format_expand1(es
, right
);
4604 } else if (mexp
!= NULL
) {
4605 value
= format_replace_expression(mexp
, es
, copy
);
4607 value
= xstrdup("");
4609 if (strstr(copy
, "#{") != 0) {
4610 format_log(es
, "expanding inner format '%s'", copy
);
4611 value
= format_expand1(es
, copy
);
4613 value
= format_find(ft
, copy
, modifiers
, time_format
);
4614 if (value
== NULL
) {
4615 format_log(es
, "format '%s' not found", copy
);
4616 value
= xstrdup("");
4618 format_log(es
, "format '%s' found: %s", copy
,
4625 /* Expand again if required. */
4626 if (modifiers
& FORMAT_EXPAND
) {
4627 new = format_expand1(es
, value
);
4630 } else if (modifiers
& FORMAT_EXPANDTIME
) {
4631 format_copy_state(&next
, es
, FORMAT_EXPAND_TIME
);
4632 new = format_expand1(&next
, value
);
4637 /* Perform substitution if any. */
4638 for (i
= 0; i
< nsub
; i
++) {
4639 left
= format_expand1(es
, sub
[i
]->argv
[0]);
4640 right
= format_expand1(es
, sub
[i
]->argv
[1]);
4641 new = format_sub(sub
[i
], value
, left
, right
);
4642 format_log(es
, "substitute '%s' to '%s': %s", left
, right
, new);
4649 /* Truncate the value if needed. */
4651 new = format_trim_left(value
, limit
);
4652 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4654 xasprintf(&value
, "%s%s", new, marker
);
4659 format_log(es
, "applied length limit %d: %s", limit
, value
);
4660 } else if (limit
< 0) {
4661 new = format_trim_right(value
, -limit
);
4662 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4664 xasprintf(&value
, "%s%s", marker
, new);
4669 format_log(es
, "applied length limit %d: %s", limit
, value
);
4672 /* Pad the value if needed. */
4674 new = utf8_padcstr(value
, width
);
4677 format_log(es
, "applied padding width %d: %s", width
, value
);
4678 } else if (width
< 0) {
4679 new = utf8_rpadcstr(value
, -width
);
4682 format_log(es
, "applied padding width %d: %s", width
, value
);
4685 /* Replace with the length or width if needed. */
4686 if (modifiers
& FORMAT_LENGTH
) {
4687 xasprintf(&new, "%zu", strlen(value
));
4690 format_log(es
, "replacing with length: %s", new);
4692 if (modifiers
& FORMAT_WIDTH
) {
4693 xasprintf(&new, "%u", format_width(value
));
4696 format_log(es
, "replacing with width: %s", new);
4699 /* Expand the buffer and copy in the value. */
4700 valuelen
= strlen(value
);
4701 while (*len
- *off
< valuelen
+ 1) {
4702 *buf
= xreallocarray(*buf
, 2, *len
);
4705 memcpy(*buf
+ *off
, value
, valuelen
);
4708 format_log(es
, "replaced '%s' with '%s'", copy0
, value
);
4712 format_free_modifiers(list
, count
);
4717 format_log(es
, "failed %s", copy0
);
4720 format_free_modifiers(list
, count
);
4725 /* Expand keys in a template. */
4727 format_expand1(struct format_expand_state
*es
, const char *fmt
)
4729 struct format_tree
*ft
= es
->ft
;
4730 char *buf
, *out
, *name
;
4731 const char *ptr
, *s
, *style_end
= NULL
;
4732 size_t off
, len
, n
, outlen
;
4734 char expanded
[8192];
4736 if (fmt
== NULL
|| *fmt
== '\0')
4737 return (xstrdup(""));
4739 if (es
->loop
== FORMAT_LOOP_LIMIT
) {
4740 format_log(es
, "reached loop limit (%u)", FORMAT_LOOP_LIMIT
);
4741 return (xstrdup(""));
4745 format_log(es
, "expanding format: %s", fmt
);
4747 if ((es
->flags
& FORMAT_EXPAND_TIME
) && strchr(fmt
, '%') != NULL
) {
4748 if (es
->time
== 0) {
4749 es
->time
= time(NULL
);
4750 localtime_r(&es
->time
, &es
->tm
);
4752 if (strftime(expanded
, sizeof expanded
, fmt
, &es
->tm
) == 0) {
4753 format_log(es
, "format is too long");
4754 return (xstrdup(""));
4756 if (format_logging(ft
) && strcmp(expanded
, fmt
) != 0)
4757 format_log(es
, "after time expanded: %s", expanded
);
4765 while (*fmt
!= '\0') {
4767 while (len
- off
< 2) {
4768 buf
= xreallocarray(buf
, 2, len
);
4771 buf
[off
++] = *fmt
++;
4776 ch
= (u_char
)*fmt
++;
4780 for (ptr
= fmt
; *ptr
!= '\0'; ptr
++) {
4783 if (*ptr
== ')' && --brackets
== 0)
4786 if (*ptr
!= ')' || brackets
!= 0)
4790 name
= xstrndup(fmt
, n
);
4791 format_log(es
, "found #(): %s", name
);
4793 if ((ft
->flags
& FORMAT_NOJOBS
) ||
4794 (es
->flags
& FORMAT_EXPAND_NOJOBS
)) {
4796 format_log(es
, "#() is disabled");
4798 out
= format_job_get(es
, name
);
4799 format_log(es
, "#() result: %s", out
);
4803 outlen
= strlen(out
);
4804 while (len
- off
< outlen
+ 1) {
4805 buf
= xreallocarray(buf
, 2, len
);
4808 memcpy(buf
+ off
, out
, outlen
);
4816 ptr
= format_skip((char *)fmt
- 2, "}");
4821 format_log(es
, "found #{}: %.*s", (int)n
, fmt
);
4822 if (format_replace(es
, fmt
, n
, &buf
, &len
, &off
) != 0)
4829 * If ##[ (with two or more #s), then it is a style and
4830 * can be left for format_draw to handle.
4832 ptr
= fmt
- (ch
== '[');
4833 n
= 2 - (ch
== '[');
4834 while (*ptr
== '#') {
4839 style_end
= format_skip(fmt
- 2, "]");
4840 format_log(es
, "found #*%zu[", n
);
4841 while (len
- off
< n
+ 2) {
4842 buf
= xreallocarray(buf
, 2, len
);
4845 memcpy(buf
+ off
, fmt
- 2, n
+ 1);
4853 format_log(es
, "found #%c", ch
);
4854 while (len
- off
< 2) {
4855 buf
= xreallocarray(buf
, 2, len
);
4862 if (fmt
> style_end
) { /* skip inside #[] */
4863 if (ch
>= 'A' && ch
<= 'Z')
4864 s
= format_upper
[ch
- 'A'];
4865 else if (ch
>= 'a' && ch
<= 'z')
4866 s
= format_lower
[ch
- 'a'];
4869 while (len
- off
< 3) {
4870 buf
= xreallocarray(buf
, 2, len
);
4878 format_log(es
, "found #%c: %s", ch
, s
);
4879 if (format_replace(es
, s
, n
, &buf
, &len
, &off
) != 0)
4888 format_log(es
, "result is: %s", buf
);
4894 /* Expand keys in a template, passing through strftime first. */
4896 format_expand_time(struct format_tree
*ft
, const char *fmt
)
4898 struct format_expand_state es
;
4900 memset(&es
, 0, sizeof es
);
4902 es
.flags
= FORMAT_EXPAND_TIME
;
4903 return (format_expand1(&es
, fmt
));
4906 /* Expand keys in a template. */
4908 format_expand(struct format_tree
*ft
, const char *fmt
)
4910 struct format_expand_state es
;
4912 memset(&es
, 0, sizeof es
);
4915 return (format_expand1(&es
, fmt
));
4918 /* Expand a single string. */
4920 format_single(struct cmdq_item
*item
, const char *fmt
, struct client
*c
,
4921 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
4923 struct format_tree
*ft
;
4926 ft
= format_create_defaults(item
, c
, s
, wl
, wp
);
4927 expanded
= format_expand(ft
, fmt
);
4932 /* Expand a single string using state. */
4934 format_single_from_state(struct cmdq_item
*item
, const char *fmt
,
4935 struct client
*c
, struct cmd_find_state
*fs
)
4937 return (format_single(item
, fmt
, c
, fs
->s
, fs
->wl
, fs
->wp
));
4940 /* Expand a single string using target. */
4942 format_single_from_target(struct cmdq_item
*item
, const char *fmt
)
4944 struct client
*tc
= cmdq_get_target_client(item
);
4946 return (format_single_from_state(item
, fmt
, tc
, cmdq_get_target(item
)));
4949 /* Create and add defaults. */
4950 struct format_tree
*
4951 format_create_defaults(struct cmdq_item
*item
, struct client
*c
,
4952 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
4954 struct format_tree
*ft
;
4957 ft
= format_create(cmdq_get_client(item
), item
, FORMAT_NONE
, 0);
4959 ft
= format_create(NULL
, item
, FORMAT_NONE
, 0);
4960 format_defaults(ft
, c
, s
, wl
, wp
);
4964 /* Create and add defaults using state. */
4965 struct format_tree
*
4966 format_create_from_state(struct cmdq_item
*item
, struct client
*c
,
4967 struct cmd_find_state
*fs
)
4969 return (format_create_defaults(item
, c
, fs
->s
, fs
->wl
, fs
->wp
));
4972 /* Create and add defaults using target. */
4973 struct format_tree
*
4974 format_create_from_target(struct cmdq_item
*item
)
4976 struct client
*tc
= cmdq_get_target_client(item
);
4978 return (format_create_from_state(item
, tc
, cmdq_get_target(item
)));
4981 /* Set defaults for any of arguments that are not NULL. */
4983 format_defaults(struct format_tree
*ft
, struct client
*c
, struct session
*s
,
4984 struct winlink
*wl
, struct window_pane
*wp
)
4986 struct paste_buffer
*pb
;
4988 if (c
!= NULL
&& c
->name
!= NULL
)
4989 log_debug("%s: c=%s", __func__
, c
->name
);
4991 log_debug("%s: c=none", __func__
);
4993 log_debug("%s: s=$%u", __func__
, s
->id
);
4995 log_debug("%s: s=none", __func__
);
4997 log_debug("%s: wl=%u", __func__
, wl
->idx
);
4999 log_debug("%s: wl=none", __func__
);
5001 log_debug("%s: wp=%%%u", __func__
, wp
->id
);
5003 log_debug("%s: wp=none", __func__
);
5005 if (c
!= NULL
&& s
!= NULL
&& c
->session
!= s
)
5006 log_debug("%s: session does not match", __func__
);
5009 ft
->type
= FORMAT_TYPE_PANE
;
5010 else if (wl
!= NULL
)
5011 ft
->type
= FORMAT_TYPE_WINDOW
;
5013 ft
->type
= FORMAT_TYPE_SESSION
;
5015 ft
->type
= FORMAT_TYPE_UNKNOWN
;
5017 if (s
== NULL
&& c
!= NULL
)
5019 if (wl
== NULL
&& s
!= NULL
)
5021 if (wp
== NULL
&& wl
!= NULL
)
5022 wp
= wl
->window
->active
;
5025 format_defaults_client(ft
, c
);
5027 format_defaults_session(ft
, s
);
5029 format_defaults_winlink(ft
, wl
);
5031 format_defaults_pane(ft
, wp
);
5033 pb
= paste_get_top(NULL
);
5035 format_defaults_paste_buffer(ft
, pb
);
5038 /* Set default format keys for a session. */
5040 format_defaults_session(struct format_tree
*ft
, struct session
*s
)
5045 /* Set default format keys for a client. */
5047 format_defaults_client(struct format_tree
*ft
, struct client
*c
)
5054 /* Set default format keys for a window. */
5056 format_defaults_window(struct format_tree
*ft
, struct window
*w
)
5061 /* Set default format keys for a winlink. */
5063 format_defaults_winlink(struct format_tree
*ft
, struct winlink
*wl
)
5066 format_defaults_window(ft
, wl
->window
);
5070 /* Set default format keys for a window pane. */
5072 format_defaults_pane(struct format_tree
*ft
, struct window_pane
*wp
)
5074 struct window_mode_entry
*wme
;
5077 format_defaults_window(ft
, wp
->window
);
5080 wme
= TAILQ_FIRST(&wp
->modes
);
5081 if (wme
!= NULL
&& wme
->mode
->formats
!= NULL
)
5082 wme
->mode
->formats(wme
, ft
);
5085 /* Set default format keys for paste buffer. */
5087 format_defaults_paste_buffer(struct format_tree
*ft
, struct paste_buffer
*pb
)
5092 /* Return word at given coordinates. Caller frees. */
5094 format_grid_word(struct grid
*gd
, u_int x
, u_int y
)
5096 const struct grid_line
*gl
;
5097 struct grid_cell gc
;
5099 struct utf8_data
*ud
= NULL
;
5105 ws
= options_get_string(global_s_options
, "word-separators");
5108 grid_get_cell(gd
, x
, y
, &gc
);
5109 if (gc
.flags
& GRID_FLAG_PADDING
)
5111 if (utf8_cstrhas(ws
, &gc
.data
) ||
5112 (gc
.data
.size
== 1 && *gc
.data
.data
== ' ')) {
5120 gl
= grid_peek_line(gd
, y
- 1);
5121 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5124 x
= grid_line_length(gd
, y
);
5132 end
= grid_line_length(gd
, y
);
5133 if (end
== 0 || x
== end
- 1) {
5134 if (y
== gd
->hsize
+ gd
->sy
- 1)
5136 gl
= grid_peek_line(gd
, y
);
5137 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5146 grid_get_cell(gd
, x
, y
, &gc
);
5147 if (gc
.flags
& GRID_FLAG_PADDING
)
5149 if (utf8_cstrhas(ws
, &gc
.data
) ||
5150 (gc
.data
.size
== 1 && *gc
.data
.data
== ' '))
5153 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5154 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5158 s
= utf8_tocstr(ud
);
5164 /* Return line at given coordinates. Caller frees. */
5166 format_grid_line(struct grid
*gd
, u_int y
)
5168 struct grid_cell gc
;
5169 struct utf8_data
*ud
= NULL
;
5174 for (x
= 0; x
< grid_line_length(gd
, y
); x
++) {
5175 grid_get_cell(gd
, x
, y
, &gc
);
5176 if (gc
.flags
& GRID_FLAG_PADDING
)
5179 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5180 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5184 s
= utf8_tocstr(ud
);
5190 /* Return hyperlink at given coordinates. Caller frees. */
5192 format_grid_hyperlink(struct grid
*gd
, u_int x
, u_int y
, struct screen
* s
)
5195 struct grid_cell gc
;
5197 grid_get_cell(gd
, x
, y
, &gc
);
5198 if (gc
.flags
& GRID_FLAG_PADDING
)
5200 if (s
->hyperlinks
== NULL
|| gc
.link
== 0)
5202 if (!hyperlinks_get(s
->hyperlinks
, gc
.link
, &uri
, NULL
, NULL
))
5204 return (xstrdup(uri
));