4 * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
38 * Build a list of key-value pairs and use them to expand #{key} entries in a
42 struct format_expand_state
;
44 static char *format_job_get(struct format_expand_state
*, const char *);
45 static char *format_expand1(struct format_expand_state
*, const char *);
46 static int format_replace(struct format_expand_state
*, const char *,
47 size_t, char **, size_t *, size_t *);
48 static void format_defaults_session(struct format_tree
*,
50 static void format_defaults_client(struct format_tree
*, struct client
*);
51 static void format_defaults_winlink(struct format_tree
*,
54 /* Entry in format job tree. */
56 struct client
*client
;
68 RB_ENTRY(format_job
) entry
;
71 /* Format job tree. */
72 static int format_job_cmp(struct format_job
*, struct format_job
*);
73 static RB_HEAD(format_job_tree
, format_job
) format_jobs
= RB_INITIALIZER();
74 RB_GENERATE_STATIC(format_job_tree
, format_job
, entry
, format_job_cmp
);
76 /* Format job tree comparison function. */
78 format_job_cmp(struct format_job
*fj1
, struct format_job
*fj2
)
80 if (fj1
->tag
< fj2
->tag
)
82 if (fj1
->tag
> fj2
->tag
)
84 return (strcmp(fj1
->cmd
, fj2
->cmd
));
87 /* Format modifiers. */
88 #define FORMAT_TIMESTRING 0x1
89 #define FORMAT_BASENAME 0x2
90 #define FORMAT_DIRNAME 0x4
91 #define FORMAT_QUOTE_SHELL 0x8
92 #define FORMAT_LITERAL 0x10
93 #define FORMAT_EXPAND 0x20
94 #define FORMAT_EXPANDTIME 0x40
95 #define FORMAT_SESSIONS 0x80
96 #define FORMAT_WINDOWS 0x100
97 #define FORMAT_PANES 0x200
98 #define FORMAT_PRETTY 0x400
99 #define FORMAT_LENGTH 0x800
100 #define FORMAT_WIDTH 0x1000
101 #define FORMAT_QUOTE_STYLE 0x2000
102 #define FORMAT_WINDOW_NAME 0x4000
103 #define FORMAT_SESSION_NAME 0x8000
104 #define FORMAT_CHARACTER 0x10000
105 #define FORMAT_COLOUR 0x20000
106 #define FORMAT_CLIENTS 0x40000
108 /* Limit on recursion. */
109 #define FORMAT_LOOP_LIMIT 100
111 /* Format expand flags. */
112 #define FORMAT_EXPAND_TIME 0x1
113 #define FORMAT_EXPAND_NOJOBS 0x2
115 /* Entry in format tree. */
116 struct format_entry
{
121 RB_ENTRY(format_entry
) entry
;
133 enum format_type type
;
139 struct window_pane
*wp
;
140 struct paste_buffer
*pb
;
142 struct cmdq_item
*item
;
143 struct client
*client
;
147 struct mouse_event m
;
149 RB_HEAD(format_entry_tree
, format_entry
) tree
;
151 static int format_entry_cmp(struct format_entry
*, struct format_entry
*);
152 RB_GENERATE_STATIC(format_entry_tree
, format_entry
, entry
, format_entry_cmp
);
154 /* Format expand state. */
155 struct format_expand_state
{
156 struct format_tree
*ft
;
163 /* Format modifier. */
164 struct format_modifier
{
172 /* Format entry tree comparison function. */
174 format_entry_cmp(struct format_entry
*fe1
, struct format_entry
*fe2
)
176 return (strcmp(fe1
->key
, fe2
->key
));
179 /* Single-character uppercase aliases. */
180 static const char *format_upper
[] = {
186 "window_flags", /* F */
189 "window_index", /* I */
196 "pane_index", /* P */
199 "session_name", /* S */
200 "pane_title", /* T */
203 "window_name", /* W */
209 /* Single-character lowercase aliases. */
210 static const char *format_lower
[] = {
218 "host_short", /* h */
239 /* Is logging enabled? */
241 format_logging(struct format_tree
*ft
)
243 return (log_get_level() != 0 || (ft
->flags
& FORMAT_VERBOSE
));
246 /* Log a message if verbose. */
247 static void printflike(3, 4)
248 format_log1(struct format_expand_state
*es
, const char *from
, const char *fmt
,
251 struct format_tree
*ft
= es
->ft
;
254 static const char spaces
[] = " ";
256 if (!format_logging(ft
))
260 xvasprintf(&s
, fmt
, ap
);
263 log_debug("%s: %s", from
, s
);
264 if (ft
->item
!= NULL
&& (ft
->flags
& FORMAT_VERBOSE
))
265 cmdq_print(ft
->item
, "#%.*s%s", es
->loop
, spaces
, s
);
269 #define format_log(es, fmt, ...) format_log1(es, __func__, fmt, ##__VA_ARGS__)
271 /* Copy expand state. */
273 format_copy_state(struct format_expand_state
*to
,
274 struct format_expand_state
*from
, int flags
)
277 to
->loop
= from
->loop
;
278 to
->time
= from
->time
;
279 memcpy(&to
->tm
, &from
->tm
, sizeof to
->tm
);
280 to
->flags
= from
->flags
|flags
;
283 /* Format job update callback. */
285 format_job_update(struct job
*job
)
287 struct format_job
*fj
= job_get_data(job
);
288 struct evbuffer
*evb
= job_get_event(job
)->input
;
289 char *line
= NULL
, *next
;
292 while ((next
= evbuffer_readline(evb
)) != NULL
) {
303 log_debug("%s: %p %s: %s", __func__
, fj
, fj
->cmd
, fj
->out
);
306 if (fj
->status
&& fj
->last
!= t
) {
307 if (fj
->client
!= NULL
)
308 server_status_client(fj
->client
);
313 /* Format job complete callback. */
315 format_job_complete(struct job
*job
)
317 struct format_job
*fj
= job_get_data(job
);
318 struct evbuffer
*evb
= job_get_event(job
)->input
;
325 if ((line
= evbuffer_readline(evb
)) == NULL
) {
326 len
= EVBUFFER_LENGTH(evb
);
327 buf
= xmalloc(len
+ 1);
329 memcpy(buf
, EVBUFFER_DATA(evb
), len
);
334 log_debug("%s: %p %s: %s", __func__
, fj
, fj
->cmd
, buf
);
336 if (*buf
!= '\0' || !fj
->updated
) {
343 if (fj
->client
!= NULL
)
344 server_status_client(fj
->client
);
351 format_job_get(struct format_expand_state
*es
, const char *cmd
)
353 struct format_tree
*ft
= es
->ft
;
354 struct format_job_tree
*jobs
;
355 struct format_job fj0
, *fj
;
359 struct format_expand_state next
;
361 if (ft
->client
== NULL
)
363 else if (ft
->client
->jobs
!= NULL
)
364 jobs
= ft
->client
->jobs
;
366 jobs
= ft
->client
->jobs
= xmalloc(sizeof *ft
->client
->jobs
);
372 if ((fj
= RB_FIND(format_job_tree
, jobs
, &fj0
)) == NULL
) {
373 fj
= xcalloc(1, sizeof *fj
);
374 fj
->client
= ft
->client
;
376 fj
->cmd
= xstrdup(cmd
);
378 RB_INSERT(format_job_tree
, jobs
, fj
);
381 format_copy_state(&next
, es
, FORMAT_EXPAND_NOJOBS
);
382 next
.flags
&= ~FORMAT_EXPAND_TIME
;
384 expanded
= format_expand1(&next
, cmd
);
385 if (fj
->expanded
== NULL
|| strcmp(expanded
, fj
->expanded
) != 0) {
386 free((void *)fj
->expanded
);
387 fj
->expanded
= xstrdup(expanded
);
390 force
= (ft
->flags
& FORMAT_FORCE
);
393 if (force
&& fj
->job
!= NULL
)
395 if (force
|| (fj
->job
== NULL
&& fj
->last
!= t
)) {
396 fj
->job
= job_run(expanded
, 0, NULL
, NULL
, NULL
,
397 server_client_get_cwd(ft
->client
, NULL
), format_job_update
,
398 format_job_complete
, NULL
, fj
, JOB_NOWAIT
, -1, -1);
399 if (fj
->job
== NULL
) {
401 xasprintf(&fj
->out
, "<'%s' didn't start>", fj
->cmd
);
405 } else if (fj
->job
!= NULL
&& (t
- fj
->last
) > 1 && fj
->out
== NULL
)
406 xasprintf(&fj
->out
, "<'%s' not ready>", fj
->cmd
);
409 if (ft
->flags
& FORMAT_STATUS
)
412 return (xstrdup(""));
413 return (format_expand1(&next
, fj
->out
));
416 /* Remove old jobs. */
418 format_job_tidy(struct format_job_tree
*jobs
, int force
)
420 struct format_job
*fj
, *fj1
;
424 RB_FOREACH_SAFE(fj
, format_job_tree
, jobs
, fj1
) {
425 if (!force
&& (fj
->last
> now
|| now
- fj
->last
< 3600))
427 RB_REMOVE(format_job_tree
, jobs
, fj
);
429 log_debug("%s: %s", __func__
, fj
->cmd
);
434 free((void *)fj
->expanded
);
435 free((void *)fj
->cmd
);
442 /* Tidy old jobs for all clients. */
444 format_tidy_jobs(void)
448 format_job_tidy(&format_jobs
, 0);
449 TAILQ_FOREACH(c
, &clients
, entry
) {
451 format_job_tidy(c
->jobs
, 0);
455 /* Remove old jobs for client. */
457 format_lost_client(struct client
*c
)
460 format_job_tidy(c
->jobs
, 1);
464 /* Wrapper for asprintf. */
465 static char * printflike(1, 2)
466 format_printf(const char *fmt
, ...)
472 xvasprintf(&s
, fmt
, ap
);
477 /* Callback for host. */
479 format_cb_host(__unused
struct format_tree
*ft
)
481 char host
[HOST_NAME_MAX
+ 1];
483 if (gethostname(host
, sizeof host
) != 0)
484 return (xstrdup(""));
485 return (xstrdup(host
));
488 /* Callback for host_short. */
490 format_cb_host_short(__unused
struct format_tree
*ft
)
492 char host
[HOST_NAME_MAX
+ 1], *cp
;
494 if (gethostname(host
, sizeof host
) != 0)
495 return (xstrdup(""));
496 if ((cp
= strchr(host
, '.')) != NULL
)
498 return (xstrdup(host
));
501 /* Callback for pid. */
503 format_cb_pid(__unused
struct format_tree
*ft
)
507 xasprintf(&value
, "%ld", (long)getpid());
511 /* Callback for session_attached_list. */
513 format_cb_session_attached_list(struct format_tree
*ft
)
515 struct session
*s
= ft
->s
;
517 struct evbuffer
*buffer
;
524 buffer
= evbuffer_new();
526 fatalx("out of memory");
528 TAILQ_FOREACH(loop
, &clients
, entry
) {
529 if (loop
->session
== s
) {
530 if (EVBUFFER_LENGTH(buffer
) > 0)
531 evbuffer_add(buffer
, ",", 1);
532 evbuffer_add_printf(buffer
, "%s", loop
->name
);
536 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
537 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
538 evbuffer_free(buffer
);
542 /* Callback for session_alerts. */
544 format_cb_session_alerts(struct format_tree
*ft
)
546 struct session
*s
= ft
->s
;
548 char alerts
[1024], tmp
[16];
554 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
555 if ((wl
->flags
& WINLINK_ALERTFLAGS
) == 0)
557 xsnprintf(tmp
, sizeof tmp
, "%u", wl
->idx
);
560 strlcat(alerts
, ",", sizeof alerts
);
561 strlcat(alerts
, tmp
, sizeof alerts
);
562 if (wl
->flags
& WINLINK_ACTIVITY
)
563 strlcat(alerts
, "#", sizeof alerts
);
564 if (wl
->flags
& WINLINK_BELL
)
565 strlcat(alerts
, "!", sizeof alerts
);
566 if (wl
->flags
& WINLINK_SILENCE
)
567 strlcat(alerts
, "~", sizeof alerts
);
569 return (xstrdup(alerts
));
572 /* Callback for session_stack. */
574 format_cb_session_stack(struct format_tree
*ft
)
576 struct session
*s
= ft
->s
;
578 char result
[1024], tmp
[16];
583 xsnprintf(result
, sizeof result
, "%u", s
->curw
->idx
);
584 TAILQ_FOREACH(wl
, &s
->lastw
, sentry
) {
585 xsnprintf(tmp
, sizeof tmp
, "%u", wl
->idx
);
588 strlcat(result
, ",", sizeof result
);
589 strlcat(result
, tmp
, sizeof result
);
591 return (xstrdup(result
));
594 /* Callback for window_stack_index. */
596 format_cb_window_stack_index(struct format_tree
*ft
)
608 TAILQ_FOREACH(wl
, &s
->lastw
, sentry
) {
614 return (xstrdup("0"));
615 xasprintf(&value
, "%u", idx
);
619 /* Callback for window_linked_sessions_list. */
621 format_cb_window_linked_sessions_list(struct format_tree
*ft
)
625 struct evbuffer
*buffer
;
633 buffer
= evbuffer_new();
635 fatalx("out of memory");
637 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
638 if (EVBUFFER_LENGTH(buffer
) > 0)
639 evbuffer_add(buffer
, ",", 1);
640 evbuffer_add_printf(buffer
, "%s", wl
->session
->name
);
643 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
644 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
645 evbuffer_free(buffer
);
649 /* Callback for window_active_sessions. */
651 format_cb_window_active_sessions(struct format_tree
*ft
)
662 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
663 if (wl
->session
->curw
== wl
)
667 xasprintf(&value
, "%u", n
);
671 /* Callback for window_active_sessions_list. */
673 format_cb_window_active_sessions_list(struct format_tree
*ft
)
677 struct evbuffer
*buffer
;
685 buffer
= evbuffer_new();
687 fatalx("out of memory");
689 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
690 if (wl
->session
->curw
== wl
) {
691 if (EVBUFFER_LENGTH(buffer
) > 0)
692 evbuffer_add(buffer
, ",", 1);
693 evbuffer_add_printf(buffer
, "%s", wl
->session
->name
);
697 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
698 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
699 evbuffer_free(buffer
);
703 /* Callback for window_active_clients. */
705 format_cb_window_active_clients(struct format_tree
*ft
)
709 struct session
*client_session
;
717 TAILQ_FOREACH(loop
, &clients
, entry
) {
718 client_session
= loop
->session
;
719 if (client_session
== NULL
)
722 if (w
== client_session
->curw
->window
)
726 xasprintf(&value
, "%u", n
);
730 /* Callback for window_active_clients_list. */
732 format_cb_window_active_clients_list(struct format_tree
*ft
)
736 struct session
*client_session
;
737 struct evbuffer
*buffer
;
745 buffer
= evbuffer_new();
747 fatalx("out of memory");
749 TAILQ_FOREACH(loop
, &clients
, entry
) {
750 client_session
= loop
->session
;
751 if (client_session
== NULL
)
754 if (w
== client_session
->curw
->window
) {
755 if (EVBUFFER_LENGTH(buffer
) > 0)
756 evbuffer_add(buffer
, ",", 1);
757 evbuffer_add_printf(buffer
, "%s", loop
->name
);
761 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
762 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
763 evbuffer_free(buffer
);
767 /* Callback for window_layout. */
769 format_cb_window_layout(struct format_tree
*ft
)
771 struct window
*w
= ft
->w
;
776 if (w
->saved_layout_root
!= NULL
)
777 return (layout_dump(w
->saved_layout_root
));
778 return (layout_dump(w
->layout_root
));
781 /* Callback for window_visible_layout. */
783 format_cb_window_visible_layout(struct format_tree
*ft
)
785 struct window
*w
= ft
->w
;
790 return (layout_dump(w
->layout_root
));
793 /* Callback for pane_start_command. */
795 format_cb_start_command(struct format_tree
*ft
)
797 struct window_pane
*wp
= ft
->wp
;
802 return (cmd_stringify_argv(wp
->argc
, wp
->argv
));
805 /* Callback for pane_start_path. */
807 format_cb_start_path(struct format_tree
*ft
)
809 struct window_pane
*wp
= ft
->wp
;
815 return (xstrdup(""));
816 return (xstrdup(wp
->cwd
));
819 /* Callback for pane_current_command. */
821 format_cb_current_command(struct format_tree
*ft
)
823 struct window_pane
*wp
= ft
->wp
;
826 if (wp
== NULL
|| wp
->shell
== NULL
)
829 cmd
= osdep_get_name(wp
->fd
, wp
->tty
);
830 if (cmd
== NULL
|| *cmd
== '\0') {
832 cmd
= cmd_stringify_argv(wp
->argc
, wp
->argv
);
833 if (cmd
== NULL
|| *cmd
== '\0') {
835 cmd
= xstrdup(wp
->shell
);
838 value
= parse_window_name(cmd
);
843 /* Callback for pane_current_path. */
845 format_cb_current_path(struct format_tree
*ft
)
847 struct window_pane
*wp
= ft
->wp
;
853 cwd
= osdep_get_cwd(wp
->fd
);
856 return (xstrdup(cwd
));
859 /* Callback for history_bytes. */
861 format_cb_history_bytes(struct format_tree
*ft
)
863 struct window_pane
*wp
= ft
->wp
;
865 struct grid_line
*gl
;
874 for (i
= 0; i
< gd
->hsize
+ gd
->sy
; i
++) {
875 gl
= grid_get_line(gd
, i
);
876 size
+= gl
->cellsize
* sizeof *gl
->celldata
;
877 size
+= gl
->extdsize
* sizeof *gl
->extddata
;
879 size
+= (gd
->hsize
+ gd
->sy
) * sizeof *gl
;
881 xasprintf(&value
, "%zu", size
);
885 /* Callback for history_all_bytes. */
887 format_cb_history_all_bytes(struct format_tree
*ft
)
889 struct window_pane
*wp
= ft
->wp
;
891 struct grid_line
*gl
;
892 u_int i
, lines
, cells
= 0, extended_cells
= 0;
899 lines
= gd
->hsize
+ gd
->sy
;
900 for (i
= 0; i
< lines
; i
++) {
901 gl
= grid_get_line(gd
, i
);
902 cells
+= gl
->cellsize
;
903 extended_cells
+= gl
->extdsize
;
906 xasprintf(&value
, "%u,%zu,%u,%zu,%u,%zu", lines
,
907 lines
* sizeof *gl
, cells
, cells
* sizeof *gl
->celldata
,
908 extended_cells
, extended_cells
* sizeof *gl
->extddata
);
912 /* Callback for pane_tabs. */
914 format_cb_pane_tabs(struct format_tree
*ft
)
916 struct window_pane
*wp
= ft
->wp
;
917 struct evbuffer
*buffer
;
925 buffer
= evbuffer_new();
927 fatalx("out of memory");
928 for (i
= 0; i
< wp
->base
.grid
->sx
; i
++) {
929 if (!bit_test(wp
->base
.tabs
, i
))
932 if (EVBUFFER_LENGTH(buffer
) > 0)
933 evbuffer_add(buffer
, ",", 1);
934 evbuffer_add_printf(buffer
, "%u", i
);
936 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
937 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
938 evbuffer_free(buffer
);
942 /* Callback for pane_fg. */
944 format_cb_pane_fg(struct format_tree
*ft
)
946 struct window_pane
*wp
= ft
->wp
;
952 tty_default_colours(&gc
, wp
);
953 return (xstrdup(colour_tostring(gc
.fg
)));
956 /* Callback for pane_bg. */
958 format_cb_pane_bg(struct format_tree
*ft
)
960 struct window_pane
*wp
= ft
->wp
;
966 tty_default_colours(&gc
, wp
);
967 return (xstrdup(colour_tostring(gc
.bg
)));
970 /* Callback for session_group_list. */
972 format_cb_session_group_list(struct format_tree
*ft
)
974 struct session
*s
= ft
->s
;
975 struct session_group
*sg
;
976 struct session
*loop
;
977 struct evbuffer
*buffer
;
983 sg
= session_group_contains(s
);
987 buffer
= evbuffer_new();
989 fatalx("out of memory");
991 TAILQ_FOREACH(loop
, &sg
->sessions
, gentry
) {
992 if (EVBUFFER_LENGTH(buffer
) > 0)
993 evbuffer_add(buffer
, ",", 1);
994 evbuffer_add_printf(buffer
, "%s", loop
->name
);
997 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
998 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
999 evbuffer_free(buffer
);
1003 /* Callback for session_group_attached_list. */
1005 format_cb_session_group_attached_list(struct format_tree
*ft
)
1007 struct session
*s
= ft
->s
, *client_session
, *session_loop
;
1008 struct session_group
*sg
;
1009 struct client
*loop
;
1010 struct evbuffer
*buffer
;
1016 sg
= session_group_contains(s
);
1020 buffer
= evbuffer_new();
1022 fatalx("out of memory");
1024 TAILQ_FOREACH(loop
, &clients
, entry
) {
1025 client_session
= loop
->session
;
1026 if (client_session
== NULL
)
1028 TAILQ_FOREACH(session_loop
, &sg
->sessions
, gentry
) {
1029 if (session_loop
== client_session
){
1030 if (EVBUFFER_LENGTH(buffer
) > 0)
1031 evbuffer_add(buffer
, ",", 1);
1032 evbuffer_add_printf(buffer
, "%s", loop
->name
);
1037 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
1038 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
1039 evbuffer_free(buffer
);
1043 /* Callback for pane_in_mode. */
1045 format_cb_pane_in_mode(struct format_tree
*ft
)
1047 struct window_pane
*wp
= ft
->wp
;
1049 struct window_mode_entry
*wme
;
1055 TAILQ_FOREACH(wme
, &wp
->modes
, entry
)
1057 xasprintf(&value
, "%u", n
);
1061 /* Callback for pane_at_top. */
1063 format_cb_pane_at_top(struct format_tree
*ft
)
1065 struct window_pane
*wp
= ft
->wp
;
1074 status
= options_get_number(w
->options
, "pane-border-status");
1075 if (status
== PANE_STATUS_TOP
)
1076 flag
= (wp
->yoff
== 1);
1078 flag
= (wp
->yoff
== 0);
1079 xasprintf(&value
, "%d", flag
);
1083 /* Callback for pane_at_bottom. */
1085 format_cb_pane_at_bottom(struct format_tree
*ft
)
1087 struct window_pane
*wp
= ft
->wp
;
1096 status
= options_get_number(w
->options
, "pane-border-status");
1097 if (status
== PANE_STATUS_BOTTOM
)
1098 flag
= (wp
->yoff
+ wp
->sy
== w
->sy
- 1);
1100 flag
= (wp
->yoff
+ wp
->sy
== w
->sy
);
1101 xasprintf(&value
, "%d", flag
);
1105 /* Callback for cursor_character. */
1107 format_cb_cursor_character(struct format_tree
*ft
)
1109 struct window_pane
*wp
= ft
->wp
;
1110 struct grid_cell gc
;
1116 grid_view_get_cell(wp
->base
.grid
, wp
->base
.cx
, wp
->base
.cy
, &gc
);
1117 if (~gc
.flags
& GRID_FLAG_PADDING
)
1118 xasprintf(&value
, "%.*s", (int)gc
.data
.size
, gc
.data
.data
);
1122 /* Callback for mouse_word. */
1124 format_cb_mouse_word(struct format_tree
*ft
)
1126 struct window_pane
*wp
;
1132 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1135 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1138 if (!TAILQ_EMPTY(&wp
->modes
)) {
1139 if (TAILQ_FIRST(&wp
->modes
)->mode
== &window_copy_mode
||
1140 TAILQ_FIRST(&wp
->modes
)->mode
== &window_view_mode
)
1141 return (window_copy_get_word(wp
, x
, y
));
1145 return (format_grid_word(gd
, x
, gd
->hsize
+ y
));
1148 /* Callback for mouse_hyperlink. */
1150 format_cb_mouse_hyperlink(struct format_tree
*ft
)
1152 struct window_pane
*wp
;
1158 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1161 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1164 return (format_grid_hyperlink(gd
, x
, gd
->hsize
+ y
, wp
->screen
));
1167 /* Callback for mouse_line. */
1169 format_cb_mouse_line(struct format_tree
*ft
)
1171 struct window_pane
*wp
;
1177 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1180 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1183 if (!TAILQ_EMPTY(&wp
->modes
)) {
1184 if (TAILQ_FIRST(&wp
->modes
)->mode
== &window_copy_mode
||
1185 TAILQ_FIRST(&wp
->modes
)->mode
== &window_view_mode
)
1186 return (window_copy_get_line(wp
, y
));
1190 return (format_grid_line(gd
, gd
->hsize
+ y
));
1193 /* Callback for mouse_status_line. */
1195 format_cb_mouse_status_line(struct format_tree
*ft
)
1202 if (ft
->c
== NULL
|| (~ft
->c
->tty
.flags
& TTY_STARTED
))
1205 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
) {
1207 } else if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
) {
1208 y
= ft
->m
.y
- ft
->m
.statusat
;
1211 xasprintf(&value
, "%u", y
);
1216 /* Callback for mouse_status_range. */
1218 format_cb_mouse_status_range(struct format_tree
*ft
)
1220 struct style_range
*sr
;
1225 if (ft
->c
== NULL
|| (~ft
->c
->tty
.flags
& TTY_STARTED
))
1228 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
) {
1231 } else if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
) {
1233 y
= ft
->m
.y
- ft
->m
.statusat
;
1237 sr
= status_get_range(ft
->c
, x
, y
);
1241 case STYLE_RANGE_NONE
:
1243 case STYLE_RANGE_LEFT
:
1244 return (xstrdup("left"));
1245 case STYLE_RANGE_RIGHT
:
1246 return (xstrdup("right"));
1247 case STYLE_RANGE_PANE
:
1248 return (xstrdup("pane"));
1249 case STYLE_RANGE_WINDOW
:
1250 return (xstrdup("window"));
1251 case STYLE_RANGE_SESSION
:
1252 return (xstrdup("session"));
1253 case STYLE_RANGE_USER
:
1254 return (xstrdup(sr
->string
));
1259 /* Callback for alternate_on. */
1261 format_cb_alternate_on(struct format_tree
*ft
)
1263 if (ft
->wp
!= NULL
) {
1264 if (ft
->wp
->base
.saved_grid
!= NULL
)
1265 return (xstrdup("1"));
1266 return (xstrdup("0"));
1271 /* Callback for alternate_saved_x. */
1273 format_cb_alternate_saved_x(struct format_tree
*ft
)
1276 return (format_printf("%u", ft
->wp
->base
.saved_cx
));
1280 /* Callback for alternate_saved_y. */
1282 format_cb_alternate_saved_y(struct format_tree
*ft
)
1285 return (format_printf("%u", ft
->wp
->base
.saved_cy
));
1289 /* Callback for buffer_name. */
1291 format_cb_buffer_name(struct format_tree
*ft
)
1294 return (xstrdup(paste_buffer_name(ft
->pb
)));
1298 /* Callback for buffer_sample. */
1300 format_cb_buffer_sample(struct format_tree
*ft
)
1303 return (paste_make_sample(ft
->pb
));
1307 /* Callback for buffer_size. */
1309 format_cb_buffer_size(struct format_tree
*ft
)
1313 if (ft
->pb
!= NULL
) {
1314 paste_buffer_data(ft
->pb
, &size
);
1315 return (format_printf("%zu", size
));
1320 /* Callback for client_cell_height. */
1322 format_cb_client_cell_height(struct format_tree
*ft
)
1324 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1325 return (format_printf("%u", ft
->c
->tty
.ypixel
));
1329 /* Callback for client_cell_width. */
1331 format_cb_client_cell_width(struct format_tree
*ft
)
1333 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1334 return (format_printf("%u", ft
->c
->tty
.xpixel
));
1338 /* Callback for client_control_mode. */
1340 format_cb_client_control_mode(struct format_tree
*ft
)
1342 if (ft
->c
!= NULL
) {
1343 if (ft
->c
->flags
& CLIENT_CONTROL
)
1344 return (xstrdup("1"));
1345 return (xstrdup("0"));
1350 /* Callback for client_discarded. */
1352 format_cb_client_discarded(struct format_tree
*ft
)
1355 return (format_printf("%zu", ft
->c
->discarded
));
1359 /* Callback for client_flags. */
1361 format_cb_client_flags(struct format_tree
*ft
)
1364 return (xstrdup(server_client_get_flags(ft
->c
)));
1368 /* Callback for client_height. */
1370 format_cb_client_height(struct format_tree
*ft
)
1372 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1373 return (format_printf("%u", ft
->c
->tty
.sy
));
1377 /* Callback for client_key_table. */
1379 format_cb_client_key_table(struct format_tree
*ft
)
1382 return (xstrdup(ft
->c
->keytable
->name
));
1386 /* Callback for client_last_session. */
1388 format_cb_client_last_session(struct format_tree
*ft
)
1390 if (ft
->c
!= NULL
&&
1391 ft
->c
->last_session
!= NULL
&&
1392 session_alive(ft
->c
->last_session
))
1393 return (xstrdup(ft
->c
->last_session
->name
));
1397 /* Callback for client_name. */
1399 format_cb_client_name(struct format_tree
*ft
)
1402 return (xstrdup(ft
->c
->name
));
1406 /* Callback for client_pid. */
1408 format_cb_client_pid(struct format_tree
*ft
)
1411 return (format_printf("%ld", (long)ft
->c
->pid
));
1415 /* Callback for client_prefix. */
1417 format_cb_client_prefix(struct format_tree
*ft
)
1421 if (ft
->c
!= NULL
) {
1422 name
= server_client_get_key_table(ft
->c
);
1423 if (strcmp(ft
->c
->keytable
->name
, name
) == 0)
1424 return (xstrdup("0"));
1425 return (xstrdup("1"));
1430 /* Callback for client_readonly. */
1432 format_cb_client_readonly(struct format_tree
*ft
)
1434 if (ft
->c
!= NULL
) {
1435 if (ft
->c
->flags
& CLIENT_READONLY
)
1436 return (xstrdup("1"));
1437 return (xstrdup("0"));
1442 /* Callback for client_session. */
1444 format_cb_client_session(struct format_tree
*ft
)
1446 if (ft
->c
!= NULL
&& ft
->c
->session
!= NULL
)
1447 return (xstrdup(ft
->c
->session
->name
));
1451 /* Callback for client_termfeatures. */
1453 format_cb_client_termfeatures(struct format_tree
*ft
)
1456 return (xstrdup(tty_get_features(ft
->c
->term_features
)));
1460 /* Callback for client_termname. */
1462 format_cb_client_termname(struct format_tree
*ft
)
1465 return (xstrdup(ft
->c
->term_name
));
1469 /* Callback for client_termtype. */
1471 format_cb_client_termtype(struct format_tree
*ft
)
1473 if (ft
->c
!= NULL
) {
1474 if (ft
->c
->term_type
== NULL
)
1475 return (xstrdup(""));
1476 return (xstrdup(ft
->c
->term_type
));
1481 /* Callback for client_tty. */
1483 format_cb_client_tty(struct format_tree
*ft
)
1486 return (xstrdup(ft
->c
->ttyname
));
1490 /* Callback for client_uid. */
1492 format_cb_client_uid(struct format_tree
*ft
)
1496 if (ft
->c
!= NULL
) {
1497 uid
= proc_get_peer_uid(ft
->c
->peer
);
1498 if (uid
!= (uid_t
)-1)
1499 return (format_printf("%ld", (long)uid
));
1504 /* Callback for client_user. */
1506 format_cb_client_user(struct format_tree
*ft
)
1511 if (ft
->c
!= NULL
) {
1512 uid
= proc_get_peer_uid(ft
->c
->peer
);
1513 if (uid
!= (uid_t
)-1 && (pw
= getpwuid(uid
)) != NULL
)
1514 return (xstrdup(pw
->pw_name
));
1519 /* Callback for client_utf8. */
1521 format_cb_client_utf8(struct format_tree
*ft
)
1523 if (ft
->c
!= NULL
) {
1524 if (ft
->c
->flags
& CLIENT_UTF8
)
1525 return (xstrdup("1"));
1526 return (xstrdup("0"));
1531 /* Callback for client_width. */
1533 format_cb_client_width(struct format_tree
*ft
)
1536 return (format_printf("%u", ft
->c
->tty
.sx
));
1540 /* Callback for client_written. */
1542 format_cb_client_written(struct format_tree
*ft
)
1545 return (format_printf("%zu", ft
->c
->written
));
1549 /* Callback for config_files. */
1551 format_cb_config_files(__unused
struct format_tree
*ft
)
1558 for (i
= 0; i
< cfg_nfiles
; i
++) {
1559 n
= strlen(cfg_files
[i
]) + 1;
1560 s
= xrealloc(s
, slen
+ n
+ 1);
1561 slen
+= xsnprintf(s
+ slen
, n
+ 1, "%s,", cfg_files
[i
]);
1564 return (xstrdup(""));
1569 /* Callback for cursor_flag. */
1571 format_cb_cursor_flag(struct format_tree
*ft
)
1573 if (ft
->wp
!= NULL
) {
1574 if (ft
->wp
->base
.mode
& MODE_CURSOR
)
1575 return (xstrdup("1"));
1576 return (xstrdup("0"));
1581 /* Callback for cursor_x. */
1583 format_cb_cursor_x(struct format_tree
*ft
)
1586 return (format_printf("%u", ft
->wp
->base
.cx
));
1590 /* Callback for cursor_y. */
1592 format_cb_cursor_y(struct format_tree
*ft
)
1595 return (format_printf("%u", ft
->wp
->base
.cy
));
1599 /* Callback for history_limit. */
1601 format_cb_history_limit(struct format_tree
*ft
)
1604 return (format_printf("%u", ft
->wp
->base
.grid
->hlimit
));
1608 /* Callback for history_size. */
1610 format_cb_history_size(struct format_tree
*ft
)
1613 return (format_printf("%u", ft
->wp
->base
.grid
->hsize
));
1617 /* Callback for insert_flag. */
1619 format_cb_insert_flag(struct format_tree
*ft
)
1621 if (ft
->wp
!= NULL
) {
1622 if (ft
->wp
->base
.mode
& MODE_INSERT
)
1623 return (xstrdup("1"));
1624 return (xstrdup("0"));
1629 /* Callback for keypad_cursor_flag. */
1631 format_cb_keypad_cursor_flag(struct format_tree
*ft
)
1633 if (ft
->wp
!= NULL
) {
1634 if (ft
->wp
->base
.mode
& MODE_KCURSOR
)
1635 return (xstrdup("1"));
1636 return (xstrdup("0"));
1641 /* Callback for keypad_flag. */
1643 format_cb_keypad_flag(struct format_tree
*ft
)
1645 if (ft
->wp
!= NULL
) {
1646 if (ft
->wp
->base
.mode
& MODE_KKEYPAD
)
1647 return (xstrdup("1"));
1648 return (xstrdup("0"));
1653 /* Callback for mouse_all_flag. */
1655 format_cb_mouse_all_flag(struct format_tree
*ft
)
1657 if (ft
->wp
!= NULL
) {
1658 if (ft
->wp
->base
.mode
& MODE_MOUSE_ALL
)
1659 return (xstrdup("1"));
1660 return (xstrdup("0"));
1665 /* Callback for mouse_any_flag. */
1667 format_cb_mouse_any_flag(struct format_tree
*ft
)
1669 if (ft
->wp
!= NULL
) {
1670 if (ft
->wp
->base
.mode
& ALL_MOUSE_MODES
)
1671 return (xstrdup("1"));
1672 return (xstrdup("0"));
1677 /* Callback for mouse_button_flag. */
1679 format_cb_mouse_button_flag(struct format_tree
*ft
)
1681 if (ft
->wp
!= NULL
) {
1682 if (ft
->wp
->base
.mode
& MODE_MOUSE_BUTTON
)
1683 return (xstrdup("1"));
1684 return (xstrdup("0"));
1689 /* Callback for mouse_pane. */
1691 format_cb_mouse_pane(struct format_tree
*ft
)
1693 struct window_pane
*wp
;
1696 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1698 return (format_printf("%%%u", wp
->id
));
1704 /* Callback for mouse_sgr_flag. */
1706 format_cb_mouse_sgr_flag(struct format_tree
*ft
)
1708 if (ft
->wp
!= NULL
) {
1709 if (ft
->wp
->base
.mode
& MODE_MOUSE_SGR
)
1710 return (xstrdup("1"));
1711 return (xstrdup("0"));
1716 /* Callback for mouse_standard_flag. */
1718 format_cb_mouse_standard_flag(struct format_tree
*ft
)
1720 if (ft
->wp
!= NULL
) {
1721 if (ft
->wp
->base
.mode
& MODE_MOUSE_STANDARD
)
1722 return (xstrdup("1"));
1723 return (xstrdup("0"));
1728 /* Callback for mouse_utf8_flag. */
1730 format_cb_mouse_utf8_flag(struct format_tree
*ft
)
1732 if (ft
->wp
!= NULL
) {
1733 if (ft
->wp
->base
.mode
& MODE_MOUSE_UTF8
)
1734 return (xstrdup("1"));
1735 return (xstrdup("0"));
1740 /* Callback for mouse_x. */
1742 format_cb_mouse_x(struct format_tree
*ft
)
1744 struct window_pane
*wp
;
1749 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1750 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1751 return (format_printf("%u", x
));
1752 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1753 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1754 return (format_printf("%u", ft
->m
.x
));
1755 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1756 return (format_printf("%u", ft
->m
.x
));
1761 /* Callback for mouse_y. */
1763 format_cb_mouse_y(struct format_tree
*ft
)
1765 struct window_pane
*wp
;
1770 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1771 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1772 return (format_printf("%u", y
));
1773 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1774 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1775 return (format_printf("%u", ft
->m
.y
));
1776 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1777 return (format_printf("%u", ft
->m
.y
- ft
->m
.statusat
));
1782 /* Callback for next_session_id. */
1784 format_cb_next_session_id(__unused
struct format_tree
*ft
)
1786 return (format_printf("$%u", next_session_id
));
1789 /* Callback for origin_flag. */
1791 format_cb_origin_flag(struct format_tree
*ft
)
1793 if (ft
->wp
!= NULL
) {
1794 if (ft
->wp
->base
.mode
& MODE_ORIGIN
)
1795 return (xstrdup("1"));
1796 return (xstrdup("0"));
1801 /* Callback for pane_active. */
1803 format_cb_pane_active(struct format_tree
*ft
)
1805 if (ft
->wp
!= NULL
) {
1806 if (ft
->wp
== ft
->wp
->window
->active
)
1807 return (xstrdup("1"));
1808 return (xstrdup("0"));
1813 /* Callback for pane_at_left. */
1815 format_cb_pane_at_left(struct format_tree
*ft
)
1817 if (ft
->wp
!= NULL
) {
1818 if (ft
->wp
->xoff
== 0)
1819 return (xstrdup("1"));
1820 return (xstrdup("0"));
1825 /* Callback for pane_at_right. */
1827 format_cb_pane_at_right(struct format_tree
*ft
)
1829 if (ft
->wp
!= NULL
) {
1830 if (ft
->wp
->xoff
+ ft
->wp
->sx
== ft
->wp
->window
->sx
)
1831 return (xstrdup("1"));
1832 return (xstrdup("0"));
1837 /* Callback for pane_bottom. */
1839 format_cb_pane_bottom(struct format_tree
*ft
)
1842 return (format_printf("%u", ft
->wp
->yoff
+ ft
->wp
->sy
- 1));
1846 /* Callback for pane_dead. */
1848 format_cb_pane_dead(struct format_tree
*ft
)
1850 if (ft
->wp
!= NULL
) {
1851 if (ft
->wp
->fd
== -1)
1852 return (xstrdup("1"));
1853 return (xstrdup("0"));
1858 /* Callback for pane_dead_signal. */
1860 format_cb_pane_dead_signal(struct format_tree
*ft
)
1862 struct window_pane
*wp
= ft
->wp
;
1866 if ((wp
->flags
& PANE_STATUSREADY
) && WIFSIGNALED(wp
->status
)) {
1867 name
= sig2name(WTERMSIG(wp
->status
));
1868 return (format_printf("%s", name
));
1875 /* Callback for pane_dead_status. */
1877 format_cb_pane_dead_status(struct format_tree
*ft
)
1879 struct window_pane
*wp
= ft
->wp
;
1882 if ((wp
->flags
& PANE_STATUSREADY
) && WIFEXITED(wp
->status
))
1883 return (format_printf("%d", WEXITSTATUS(wp
->status
)));
1889 /* Callback for pane_dead_time. */
1891 format_cb_pane_dead_time(struct format_tree
*ft
)
1893 struct window_pane
*wp
= ft
->wp
;
1896 if (wp
->flags
& PANE_STATUSDRAWN
)
1897 return (&wp
->dead_time
);
1903 /* Callback for pane_format. */
1905 format_cb_pane_format(struct format_tree
*ft
)
1907 if (ft
->type
== FORMAT_TYPE_PANE
)
1908 return (xstrdup("1"));
1909 return (xstrdup("0"));
1912 /* Callback for pane_height. */
1914 format_cb_pane_height(struct format_tree
*ft
)
1917 return (format_printf("%u", ft
->wp
->sy
));
1921 /* Callback for pane_id. */
1923 format_cb_pane_id(struct format_tree
*ft
)
1926 return (format_printf("%%%u", ft
->wp
->id
));
1930 /* Callback for pane_index. */
1932 format_cb_pane_index(struct format_tree
*ft
)
1936 if (ft
->wp
!= NULL
&& window_pane_index(ft
->wp
, &idx
) == 0)
1937 return (format_printf("%u", idx
));
1941 /* Callback for pane_input_off. */
1943 format_cb_pane_input_off(struct format_tree
*ft
)
1945 if (ft
->wp
!= NULL
) {
1946 if (ft
->wp
->flags
& PANE_INPUTOFF
)
1947 return (xstrdup("1"));
1948 return (xstrdup("0"));
1953 /* Callback for pane_unseen_changes. */
1955 format_cb_pane_unseen_changes(struct format_tree
*ft
)
1957 if (ft
->wp
!= NULL
) {
1958 if (ft
->wp
->flags
& PANE_UNSEENCHANGES
)
1959 return (xstrdup("1"));
1960 return (xstrdup("0"));
1965 /* Callback for pane_last. */
1967 format_cb_pane_last(struct format_tree
*ft
)
1969 if (ft
->wp
!= NULL
) {
1970 if (ft
->wp
== TAILQ_FIRST(&ft
->wp
->window
->last_panes
))
1971 return (xstrdup("1"));
1972 return (xstrdup("0"));
1977 /* Callback for pane_left. */
1979 format_cb_pane_left(struct format_tree
*ft
)
1982 return (format_printf("%u", ft
->wp
->xoff
));
1986 /* Callback for pane_marked. */
1988 format_cb_pane_marked(struct format_tree
*ft
)
1990 if (ft
->wp
!= NULL
) {
1991 if (server_check_marked() && marked_pane
.wp
== ft
->wp
)
1992 return (xstrdup("1"));
1993 return (xstrdup("0"));
1998 /* Callback for pane_marked_set. */
2000 format_cb_pane_marked_set(struct format_tree
*ft
)
2002 if (ft
->wp
!= NULL
) {
2003 if (server_check_marked())
2004 return (xstrdup("1"));
2005 return (xstrdup("0"));
2010 /* Callback for pane_mode. */
2012 format_cb_pane_mode(struct format_tree
*ft
)
2014 struct window_mode_entry
*wme
;
2016 if (ft
->wp
!= NULL
) {
2017 wme
= TAILQ_FIRST(&ft
->wp
->modes
);
2019 return (xstrdup(wme
->mode
->name
));
2025 /* Callback for pane_path. */
2027 format_cb_pane_path(struct format_tree
*ft
)
2029 if (ft
->wp
!= NULL
) {
2030 if (ft
->wp
->base
.path
== NULL
)
2031 return (xstrdup(""));
2032 return (xstrdup(ft
->wp
->base
.path
));
2037 /* Callback for pane_pid. */
2039 format_cb_pane_pid(struct format_tree
*ft
)
2042 return (format_printf("%ld", (long)ft
->wp
->pid
));
2046 /* Callback for pane_pipe. */
2048 format_cb_pane_pipe(struct format_tree
*ft
)
2050 if (ft
->wp
!= NULL
) {
2051 if (ft
->wp
->pipe_fd
!= -1)
2052 return (xstrdup("1"));
2053 return (xstrdup("0"));
2058 /* Callback for pane_right. */
2060 format_cb_pane_right(struct format_tree
*ft
)
2063 return (format_printf("%u", ft
->wp
->xoff
+ ft
->wp
->sx
- 1));
2067 /* Callback for pane_search_string. */
2069 format_cb_pane_search_string(struct format_tree
*ft
)
2071 if (ft
->wp
!= NULL
) {
2072 if (ft
->wp
->searchstr
== NULL
)
2073 return (xstrdup(""));
2074 return (xstrdup(ft
->wp
->searchstr
));
2079 /* Callback for pane_synchronized. */
2081 format_cb_pane_synchronized(struct format_tree
*ft
)
2083 if (ft
->wp
!= NULL
) {
2084 if (options_get_number(ft
->wp
->options
, "synchronize-panes"))
2085 return (xstrdup("1"));
2086 return (xstrdup("0"));
2091 /* Callback for pane_title. */
2093 format_cb_pane_title(struct format_tree
*ft
)
2096 return (xstrdup(ft
->wp
->base
.title
));
2100 /* Callback for pane_top. */
2102 format_cb_pane_top(struct format_tree
*ft
)
2105 return (format_printf("%u", ft
->wp
->yoff
));
2109 /* Callback for pane_tty. */
2111 format_cb_pane_tty(struct format_tree
*ft
)
2114 return (xstrdup(ft
->wp
->tty
));
2118 /* Callback for pane_width. */
2120 format_cb_pane_width(struct format_tree
*ft
)
2123 return (format_printf("%u", ft
->wp
->sx
));
2127 /* Callback for scroll_region_lower. */
2129 format_cb_scroll_region_lower(struct format_tree
*ft
)
2132 return (format_printf("%u", ft
->wp
->base
.rlower
));
2136 /* Callback for scroll_region_upper. */
2138 format_cb_scroll_region_upper(struct format_tree
*ft
)
2141 return (format_printf("%u", ft
->wp
->base
.rupper
));
2145 /* Callback for server_sessions. */
2147 format_cb_server_sessions(__unused
struct format_tree
*ft
)
2152 RB_FOREACH(s
, sessions
, &sessions
)
2154 return (format_printf("%u", n
));
2157 /* Callback for session_attached. */
2159 format_cb_session_attached(struct format_tree
*ft
)
2162 return (format_printf("%u", ft
->s
->attached
));
2166 /* Callback for session_format. */
2168 format_cb_session_format(struct format_tree
*ft
)
2170 if (ft
->type
== FORMAT_TYPE_SESSION
)
2171 return (xstrdup("1"));
2172 return (xstrdup("0"));
2175 /* Callback for session_group. */
2177 format_cb_session_group(struct format_tree
*ft
)
2179 struct session_group
*sg
;
2181 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2182 return (xstrdup(sg
->name
));
2186 /* Callback for session_group_attached. */
2188 format_cb_session_group_attached(struct format_tree
*ft
)
2190 struct session_group
*sg
;
2192 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2193 return (format_printf("%u", session_group_attached_count (sg
)));
2197 /* Callback for session_group_many_attached. */
2199 format_cb_session_group_many_attached(struct format_tree
*ft
)
2201 struct session_group
*sg
;
2203 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
) {
2204 if (session_group_attached_count (sg
) > 1)
2205 return (xstrdup("1"));
2206 return (xstrdup("0"));
2211 /* Callback for session_group_size. */
2213 format_cb_session_group_size(struct format_tree
*ft
)
2215 struct session_group
*sg
;
2217 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2218 return (format_printf("%u", session_group_count (sg
)));
2222 /* Callback for session_grouped. */
2224 format_cb_session_grouped(struct format_tree
*ft
)
2226 if (ft
->s
!= NULL
) {
2227 if (session_group_contains(ft
->s
) != NULL
)
2228 return (xstrdup("1"));
2229 return (xstrdup("0"));
2234 /* Callback for session_id. */
2236 format_cb_session_id(struct format_tree
*ft
)
2239 return (format_printf("$%u", ft
->s
->id
));
2243 /* Callback for session_many_attached. */
2245 format_cb_session_many_attached(struct format_tree
*ft
)
2247 if (ft
->s
!= NULL
) {
2248 if (ft
->s
->attached
> 1)
2249 return (xstrdup("1"));
2250 return (xstrdup("0"));
2255 /* Callback for session_marked. */
2257 format_cb_session_marked(struct format_tree
*ft
)
2259 if (ft
->s
!= NULL
) {
2260 if (server_check_marked() && marked_pane
.s
== ft
->s
)
2261 return (xstrdup("1"));
2262 return (xstrdup("0"));
2267 /* Callback for session_name. */
2269 format_cb_session_name(struct format_tree
*ft
)
2272 return (xstrdup(ft
->s
->name
));
2276 /* Callback for session_path. */
2278 format_cb_session_path(struct format_tree
*ft
)
2281 return (xstrdup(ft
->s
->cwd
));
2285 /* Callback for session_windows. */
2287 format_cb_session_windows(struct format_tree
*ft
)
2290 return (format_printf("%u", winlink_count(&ft
->s
->windows
)));
2294 /* Callback for socket_path. */
2296 format_cb_socket_path(__unused
struct format_tree
*ft
)
2298 return (xstrdup(socket_path
));
2301 /* Callback for version. */
2303 format_cb_version(__unused
struct format_tree
*ft
)
2305 return (xstrdup(getversion()));
2308 /* Callback for active_window_index. */
2310 format_cb_active_window_index(struct format_tree
*ft
)
2313 return (format_printf("%u", ft
->s
->curw
->idx
));
2317 /* Callback for last_window_index. */
2319 format_cb_last_window_index(struct format_tree
*ft
)
2323 if (ft
->s
!= NULL
) {
2324 wl
= RB_MAX(winlinks
, &ft
->s
->windows
);
2325 return (format_printf("%u", wl
->idx
));
2330 /* Callback for window_active. */
2332 format_cb_window_active(struct format_tree
*ft
)
2334 if (ft
->wl
!= NULL
) {
2335 if (ft
->wl
== ft
->wl
->session
->curw
)
2336 return (xstrdup("1"));
2337 return (xstrdup("0"));
2342 /* Callback for window_activity_flag. */
2344 format_cb_window_activity_flag(struct format_tree
*ft
)
2346 if (ft
->wl
!= NULL
) {
2347 if (ft
->wl
->flags
& WINLINK_ACTIVITY
)
2348 return (xstrdup("1"));
2349 return (xstrdup("0"));
2354 /* Callback for window_bell_flag. */
2356 format_cb_window_bell_flag(struct format_tree
*ft
)
2358 if (ft
->wl
!= NULL
) {
2359 if (ft
->wl
->flags
& WINLINK_BELL
)
2360 return (xstrdup("1"));
2361 return (xstrdup("0"));
2366 /* Callback for window_bigger. */
2368 format_cb_window_bigger(struct format_tree
*ft
)
2370 u_int ox
, oy
, sx
, sy
;
2372 if (ft
->c
!= NULL
) {
2373 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2374 return (xstrdup("1"));
2375 return (xstrdup("0"));
2380 /* Callback for window_cell_height. */
2382 format_cb_window_cell_height(struct format_tree
*ft
)
2385 return (format_printf("%u", ft
->w
->ypixel
));
2389 /* Callback for window_cell_width. */
2391 format_cb_window_cell_width(struct format_tree
*ft
)
2394 return (format_printf("%u", ft
->w
->xpixel
));
2398 /* Callback for window_end_flag. */
2400 format_cb_window_end_flag(struct format_tree
*ft
)
2402 if (ft
->wl
!= NULL
) {
2403 if (ft
->wl
== RB_MAX(winlinks
, &ft
->wl
->session
->windows
))
2404 return (xstrdup("1"));
2405 return (xstrdup("0"));
2410 /* Callback for window_flags. */
2412 format_cb_window_flags(struct format_tree
*ft
)
2415 return (xstrdup(window_printable_flags(ft
->wl
, 1)));
2419 /* Callback for window_format. */
2421 format_cb_window_format(struct format_tree
*ft
)
2423 if (ft
->type
== FORMAT_TYPE_WINDOW
)
2424 return (xstrdup("1"));
2425 return (xstrdup("0"));
2428 /* Callback for window_height. */
2430 format_cb_window_height(struct format_tree
*ft
)
2433 return (format_printf("%u", ft
->w
->sy
));
2437 /* Callback for window_id. */
2439 format_cb_window_id(struct format_tree
*ft
)
2442 return (format_printf("@%u", ft
->w
->id
));
2446 /* Callback for window_index. */
2448 format_cb_window_index(struct format_tree
*ft
)
2451 return (format_printf("%d", ft
->wl
->idx
));
2455 /* Callback for window_last_flag. */
2457 format_cb_window_last_flag(struct format_tree
*ft
)
2459 if (ft
->wl
!= NULL
) {
2460 if (ft
->wl
== TAILQ_FIRST(&ft
->wl
->session
->lastw
))
2461 return (xstrdup("1"));
2462 return (xstrdup("0"));
2467 /* Callback for window_linked. */
2469 format_cb_window_linked(struct format_tree
*ft
)
2471 if (ft
->wl
!= NULL
) {
2472 if (session_is_linked(ft
->wl
->session
, ft
->wl
->window
))
2473 return (xstrdup("1"));
2474 return (xstrdup("0"));
2479 /* Callback for window_linked_sessions. */
2481 format_cb_window_linked_sessions(struct format_tree
*ft
)
2484 return (format_printf("%u", ft
->wl
->window
->references
));
2488 /* Callback for window_marked_flag. */
2490 format_cb_window_marked_flag(struct format_tree
*ft
)
2492 if (ft
->wl
!= NULL
) {
2493 if (server_check_marked() && marked_pane
.wl
== ft
->wl
)
2494 return (xstrdup("1"));
2495 return (xstrdup("0"));
2500 /* Callback for window_name. */
2502 format_cb_window_name(struct format_tree
*ft
)
2505 return (format_printf("%s", ft
->w
->name
));
2509 /* Callback for window_offset_x. */
2511 format_cb_window_offset_x(struct format_tree
*ft
)
2513 u_int ox
, oy
, sx
, sy
;
2515 if (ft
->c
!= NULL
) {
2516 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2517 return (format_printf("%u", ox
));
2523 /* Callback for window_offset_y. */
2525 format_cb_window_offset_y(struct format_tree
*ft
)
2527 u_int ox
, oy
, sx
, sy
;
2529 if (ft
->c
!= NULL
) {
2530 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2531 return (format_printf("%u", oy
));
2537 /* Callback for window_panes. */
2539 format_cb_window_panes(struct format_tree
*ft
)
2542 return (format_printf("%u", window_count_panes(ft
->w
)));
2546 /* Callback for window_raw_flags. */
2548 format_cb_window_raw_flags(struct format_tree
*ft
)
2551 return (xstrdup(window_printable_flags(ft
->wl
, 0)));
2555 /* Callback for window_silence_flag. */
2557 format_cb_window_silence_flag(struct format_tree
*ft
)
2559 if (ft
->wl
!= NULL
) {
2560 if (ft
->wl
->flags
& WINLINK_SILENCE
)
2561 return (xstrdup("1"));
2562 return (xstrdup("0"));
2567 /* Callback for window_start_flag. */
2569 format_cb_window_start_flag(struct format_tree
*ft
)
2571 if (ft
->wl
!= NULL
) {
2572 if (ft
->wl
== RB_MIN(winlinks
, &ft
->wl
->session
->windows
))
2573 return (xstrdup("1"));
2574 return (xstrdup("0"));
2579 /* Callback for window_width. */
2581 format_cb_window_width(struct format_tree
*ft
)
2584 return (format_printf("%u", ft
->w
->sx
));
2588 /* Callback for window_zoomed_flag. */
2590 format_cb_window_zoomed_flag(struct format_tree
*ft
)
2592 if (ft
->w
!= NULL
) {
2593 if (ft
->w
->flags
& WINDOW_ZOOMED
)
2594 return (xstrdup("1"));
2595 return (xstrdup("0"));
2600 /* Callback for wrap_flag. */
2602 format_cb_wrap_flag(struct format_tree
*ft
)
2604 if (ft
->wp
!= NULL
) {
2605 if (ft
->wp
->base
.mode
& MODE_WRAP
)
2606 return (xstrdup("1"));
2607 return (xstrdup("0"));
2612 /* Callback for buffer_created. */
2614 format_cb_buffer_created(struct format_tree
*ft
)
2616 static struct timeval tv
;
2618 if (ft
->pb
!= NULL
) {
2620 tv
.tv_sec
= paste_buffer_created(ft
->pb
);
2626 /* Callback for client_activity. */
2628 format_cb_client_activity(struct format_tree
*ft
)
2631 return (&ft
->c
->activity_time
);
2635 /* Callback for client_created. */
2637 format_cb_client_created(struct format_tree
*ft
)
2640 return (&ft
->c
->creation_time
);
2644 /* Callback for session_activity. */
2646 format_cb_session_activity(struct format_tree
*ft
)
2649 return (&ft
->s
->activity_time
);
2653 /* Callback for session_created. */
2655 format_cb_session_created(struct format_tree
*ft
)
2658 return (&ft
->s
->creation_time
);
2662 /* Callback for session_last_attached. */
2664 format_cb_session_last_attached(struct format_tree
*ft
)
2667 return (&ft
->s
->last_attached_time
);
2671 /* Callback for start_time. */
2673 format_cb_start_time(__unused
struct format_tree
*ft
)
2675 return (&start_time
);
2678 /* Callback for window_activity. */
2680 format_cb_window_activity(struct format_tree
*ft
)
2683 return (&ft
->w
->activity_time
);
2687 /* Callback for buffer_mode_format, */
2689 format_cb_buffer_mode_format(__unused
struct format_tree
*ft
)
2691 return (xstrdup(window_buffer_mode
.default_format
));
2694 /* Callback for client_mode_format, */
2696 format_cb_client_mode_format(__unused
struct format_tree
*ft
)
2698 return (xstrdup(window_client_mode
.default_format
));
2701 /* Callback for tree_mode_format, */
2703 format_cb_tree_mode_format(__unused
struct format_tree
*ft
)
2705 return (xstrdup(window_tree_mode
.default_format
));
2708 /* Callback for uid. */
2710 format_cb_uid(__unused
struct format_tree
*ft
)
2712 return (format_printf("%ld", (long)getuid()));
2715 /* Callback for user. */
2717 format_cb_user(__unused
struct format_tree
*ft
)
2721 if ((pw
= getpwuid(getuid())) != NULL
)
2722 return (xstrdup(pw
->pw_name
));
2726 /* Format table type. */
2727 enum format_table_type
{
2728 FORMAT_TABLE_STRING
,
2732 /* Format table entry. */
2733 struct format_table_entry
{
2735 enum format_table_type type
;
2740 * Format table. Default format variables (that are almost always in the tree
2741 * and where the value is expanded by a callback in this file) are listed here.
2742 * Only variables which are added by the caller go into the tree.
2744 static const struct format_table_entry format_table
[] = {
2745 { "active_window_index", FORMAT_TABLE_STRING
,
2746 format_cb_active_window_index
2748 { "alternate_on", FORMAT_TABLE_STRING
,
2749 format_cb_alternate_on
2751 { "alternate_saved_x", FORMAT_TABLE_STRING
,
2752 format_cb_alternate_saved_x
2754 { "alternate_saved_y", FORMAT_TABLE_STRING
,
2755 format_cb_alternate_saved_y
2757 { "buffer_created", FORMAT_TABLE_TIME
,
2758 format_cb_buffer_created
2760 { "buffer_mode_format", FORMAT_TABLE_STRING
,
2761 format_cb_buffer_mode_format
2763 { "buffer_name", FORMAT_TABLE_STRING
,
2764 format_cb_buffer_name
2766 { "buffer_sample", FORMAT_TABLE_STRING
,
2767 format_cb_buffer_sample
2769 { "buffer_size", FORMAT_TABLE_STRING
,
2770 format_cb_buffer_size
2772 { "client_activity", FORMAT_TABLE_TIME
,
2773 format_cb_client_activity
2775 { "client_cell_height", FORMAT_TABLE_STRING
,
2776 format_cb_client_cell_height
2778 { "client_cell_width", FORMAT_TABLE_STRING
,
2779 format_cb_client_cell_width
2781 { "client_control_mode", FORMAT_TABLE_STRING
,
2782 format_cb_client_control_mode
2784 { "client_created", FORMAT_TABLE_TIME
,
2785 format_cb_client_created
2787 { "client_discarded", FORMAT_TABLE_STRING
,
2788 format_cb_client_discarded
2790 { "client_flags", FORMAT_TABLE_STRING
,
2791 format_cb_client_flags
2793 { "client_height", FORMAT_TABLE_STRING
,
2794 format_cb_client_height
2796 { "client_key_table", FORMAT_TABLE_STRING
,
2797 format_cb_client_key_table
2799 { "client_last_session", FORMAT_TABLE_STRING
,
2800 format_cb_client_last_session
2802 { "client_mode_format", FORMAT_TABLE_STRING
,
2803 format_cb_client_mode_format
2805 { "client_name", FORMAT_TABLE_STRING
,
2806 format_cb_client_name
2808 { "client_pid", FORMAT_TABLE_STRING
,
2809 format_cb_client_pid
2811 { "client_prefix", FORMAT_TABLE_STRING
,
2812 format_cb_client_prefix
2814 { "client_readonly", FORMAT_TABLE_STRING
,
2815 format_cb_client_readonly
2817 { "client_session", FORMAT_TABLE_STRING
,
2818 format_cb_client_session
2820 { "client_termfeatures", FORMAT_TABLE_STRING
,
2821 format_cb_client_termfeatures
2823 { "client_termname", FORMAT_TABLE_STRING
,
2824 format_cb_client_termname
2826 { "client_termtype", FORMAT_TABLE_STRING
,
2827 format_cb_client_termtype
2829 { "client_tty", FORMAT_TABLE_STRING
,
2830 format_cb_client_tty
2832 { "client_uid", FORMAT_TABLE_STRING
,
2833 format_cb_client_uid
2835 { "client_user", FORMAT_TABLE_STRING
,
2836 format_cb_client_user
2838 { "client_utf8", FORMAT_TABLE_STRING
,
2839 format_cb_client_utf8
2841 { "client_width", FORMAT_TABLE_STRING
,
2842 format_cb_client_width
2844 { "client_written", FORMAT_TABLE_STRING
,
2845 format_cb_client_written
2847 { "config_files", FORMAT_TABLE_STRING
,
2848 format_cb_config_files
2850 { "cursor_character", FORMAT_TABLE_STRING
,
2851 format_cb_cursor_character
2853 { "cursor_flag", FORMAT_TABLE_STRING
,
2854 format_cb_cursor_flag
2856 { "cursor_x", FORMAT_TABLE_STRING
,
2859 { "cursor_y", FORMAT_TABLE_STRING
,
2862 { "history_all_bytes", FORMAT_TABLE_STRING
,
2863 format_cb_history_all_bytes
2865 { "history_bytes", FORMAT_TABLE_STRING
,
2866 format_cb_history_bytes
2868 { "history_limit", FORMAT_TABLE_STRING
,
2869 format_cb_history_limit
2871 { "history_size", FORMAT_TABLE_STRING
,
2872 format_cb_history_size
2874 { "host", FORMAT_TABLE_STRING
,
2877 { "host_short", FORMAT_TABLE_STRING
,
2878 format_cb_host_short
2880 { "insert_flag", FORMAT_TABLE_STRING
,
2881 format_cb_insert_flag
2883 { "keypad_cursor_flag", FORMAT_TABLE_STRING
,
2884 format_cb_keypad_cursor_flag
2886 { "keypad_flag", FORMAT_TABLE_STRING
,
2887 format_cb_keypad_flag
2889 { "last_window_index", FORMAT_TABLE_STRING
,
2890 format_cb_last_window_index
2892 { "mouse_all_flag", FORMAT_TABLE_STRING
,
2893 format_cb_mouse_all_flag
2895 { "mouse_any_flag", FORMAT_TABLE_STRING
,
2896 format_cb_mouse_any_flag
2898 { "mouse_button_flag", FORMAT_TABLE_STRING
,
2899 format_cb_mouse_button_flag
2901 { "mouse_hyperlink", FORMAT_TABLE_STRING
,
2902 format_cb_mouse_hyperlink
2904 { "mouse_line", FORMAT_TABLE_STRING
,
2905 format_cb_mouse_line
2907 { "mouse_pane", FORMAT_TABLE_STRING
,
2908 format_cb_mouse_pane
2910 { "mouse_sgr_flag", FORMAT_TABLE_STRING
,
2911 format_cb_mouse_sgr_flag
2913 { "mouse_standard_flag", FORMAT_TABLE_STRING
,
2914 format_cb_mouse_standard_flag
2916 { "mouse_status_line", FORMAT_TABLE_STRING
,
2917 format_cb_mouse_status_line
2919 { "mouse_status_range", FORMAT_TABLE_STRING
,
2920 format_cb_mouse_status_range
2922 { "mouse_utf8_flag", FORMAT_TABLE_STRING
,
2923 format_cb_mouse_utf8_flag
2925 { "mouse_word", FORMAT_TABLE_STRING
,
2926 format_cb_mouse_word
2928 { "mouse_x", FORMAT_TABLE_STRING
,
2931 { "mouse_y", FORMAT_TABLE_STRING
,
2934 { "next_session_id", FORMAT_TABLE_STRING
,
2935 format_cb_next_session_id
2937 { "origin_flag", FORMAT_TABLE_STRING
,
2938 format_cb_origin_flag
2940 { "pane_active", FORMAT_TABLE_STRING
,
2941 format_cb_pane_active
2943 { "pane_at_bottom", FORMAT_TABLE_STRING
,
2944 format_cb_pane_at_bottom
2946 { "pane_at_left", FORMAT_TABLE_STRING
,
2947 format_cb_pane_at_left
2949 { "pane_at_right", FORMAT_TABLE_STRING
,
2950 format_cb_pane_at_right
2952 { "pane_at_top", FORMAT_TABLE_STRING
,
2953 format_cb_pane_at_top
2955 { "pane_bg", FORMAT_TABLE_STRING
,
2958 { "pane_bottom", FORMAT_TABLE_STRING
,
2959 format_cb_pane_bottom
2961 { "pane_current_command", FORMAT_TABLE_STRING
,
2962 format_cb_current_command
2964 { "pane_current_path", FORMAT_TABLE_STRING
,
2965 format_cb_current_path
2967 { "pane_dead", FORMAT_TABLE_STRING
,
2970 { "pane_dead_signal", FORMAT_TABLE_STRING
,
2971 format_cb_pane_dead_signal
2973 { "pane_dead_status", FORMAT_TABLE_STRING
,
2974 format_cb_pane_dead_status
2976 { "pane_dead_time", FORMAT_TABLE_TIME
,
2977 format_cb_pane_dead_time
2979 { "pane_fg", FORMAT_TABLE_STRING
,
2982 { "pane_format", FORMAT_TABLE_STRING
,
2983 format_cb_pane_format
2985 { "pane_height", FORMAT_TABLE_STRING
,
2986 format_cb_pane_height
2988 { "pane_id", FORMAT_TABLE_STRING
,
2991 { "pane_in_mode", FORMAT_TABLE_STRING
,
2992 format_cb_pane_in_mode
2994 { "pane_index", FORMAT_TABLE_STRING
,
2995 format_cb_pane_index
2997 { "pane_input_off", FORMAT_TABLE_STRING
,
2998 format_cb_pane_input_off
3000 { "pane_last", FORMAT_TABLE_STRING
,
3003 { "pane_left", FORMAT_TABLE_STRING
,
3006 { "pane_marked", FORMAT_TABLE_STRING
,
3007 format_cb_pane_marked
3009 { "pane_marked_set", FORMAT_TABLE_STRING
,
3010 format_cb_pane_marked_set
3012 { "pane_mode", FORMAT_TABLE_STRING
,
3015 { "pane_path", FORMAT_TABLE_STRING
,
3018 { "pane_pid", FORMAT_TABLE_STRING
,
3021 { "pane_pipe", FORMAT_TABLE_STRING
,
3024 { "pane_right", FORMAT_TABLE_STRING
,
3025 format_cb_pane_right
3027 { "pane_search_string", FORMAT_TABLE_STRING
,
3028 format_cb_pane_search_string
3030 { "pane_start_command", FORMAT_TABLE_STRING
,
3031 format_cb_start_command
3033 { "pane_start_path", FORMAT_TABLE_STRING
,
3034 format_cb_start_path
3036 { "pane_synchronized", FORMAT_TABLE_STRING
,
3037 format_cb_pane_synchronized
3039 { "pane_tabs", FORMAT_TABLE_STRING
,
3042 { "pane_title", FORMAT_TABLE_STRING
,
3043 format_cb_pane_title
3045 { "pane_top", FORMAT_TABLE_STRING
,
3048 { "pane_tty", FORMAT_TABLE_STRING
,
3051 { "pane_unseen_changes", FORMAT_TABLE_STRING
,
3052 format_cb_pane_unseen_changes
3054 { "pane_width", FORMAT_TABLE_STRING
,
3055 format_cb_pane_width
3057 { "pid", FORMAT_TABLE_STRING
,
3060 { "scroll_region_lower", FORMAT_TABLE_STRING
,
3061 format_cb_scroll_region_lower
3063 { "scroll_region_upper", FORMAT_TABLE_STRING
,
3064 format_cb_scroll_region_upper
3066 { "server_sessions", FORMAT_TABLE_STRING
,
3067 format_cb_server_sessions
3069 { "session_activity", FORMAT_TABLE_TIME
,
3070 format_cb_session_activity
3072 { "session_alerts", FORMAT_TABLE_STRING
,
3073 format_cb_session_alerts
3075 { "session_attached", FORMAT_TABLE_STRING
,
3076 format_cb_session_attached
3078 { "session_attached_list", FORMAT_TABLE_STRING
,
3079 format_cb_session_attached_list
3081 { "session_created", FORMAT_TABLE_TIME
,
3082 format_cb_session_created
3084 { "session_format", FORMAT_TABLE_STRING
,
3085 format_cb_session_format
3087 { "session_group", FORMAT_TABLE_STRING
,
3088 format_cb_session_group
3090 { "session_group_attached", FORMAT_TABLE_STRING
,
3091 format_cb_session_group_attached
3093 { "session_group_attached_list", FORMAT_TABLE_STRING
,
3094 format_cb_session_group_attached_list
3096 { "session_group_list", FORMAT_TABLE_STRING
,
3097 format_cb_session_group_list
3099 { "session_group_many_attached", FORMAT_TABLE_STRING
,
3100 format_cb_session_group_many_attached
3102 { "session_group_size", FORMAT_TABLE_STRING
,
3103 format_cb_session_group_size
3105 { "session_grouped", FORMAT_TABLE_STRING
,
3106 format_cb_session_grouped
3108 { "session_id", FORMAT_TABLE_STRING
,
3109 format_cb_session_id
3111 { "session_last_attached", FORMAT_TABLE_TIME
,
3112 format_cb_session_last_attached
3114 { "session_many_attached", FORMAT_TABLE_STRING
,
3115 format_cb_session_many_attached
3117 { "session_marked", FORMAT_TABLE_STRING
,
3118 format_cb_session_marked
,
3120 { "session_name", FORMAT_TABLE_STRING
,
3121 format_cb_session_name
3123 { "session_path", FORMAT_TABLE_STRING
,
3124 format_cb_session_path
3126 { "session_stack", FORMAT_TABLE_STRING
,
3127 format_cb_session_stack
3129 { "session_windows", FORMAT_TABLE_STRING
,
3130 format_cb_session_windows
3132 { "socket_path", FORMAT_TABLE_STRING
,
3133 format_cb_socket_path
3135 { "start_time", FORMAT_TABLE_TIME
,
3136 format_cb_start_time
3138 { "tree_mode_format", FORMAT_TABLE_STRING
,
3139 format_cb_tree_mode_format
3141 { "uid", FORMAT_TABLE_STRING
,
3144 { "user", FORMAT_TABLE_STRING
,
3147 { "version", FORMAT_TABLE_STRING
,
3150 { "window_active", FORMAT_TABLE_STRING
,
3151 format_cb_window_active
3153 { "window_active_clients", FORMAT_TABLE_STRING
,
3154 format_cb_window_active_clients
3156 { "window_active_clients_list", FORMAT_TABLE_STRING
,
3157 format_cb_window_active_clients_list
3159 { "window_active_sessions", FORMAT_TABLE_STRING
,
3160 format_cb_window_active_sessions
3162 { "window_active_sessions_list", FORMAT_TABLE_STRING
,
3163 format_cb_window_active_sessions_list
3165 { "window_activity", FORMAT_TABLE_TIME
,
3166 format_cb_window_activity
3168 { "window_activity_flag", FORMAT_TABLE_STRING
,
3169 format_cb_window_activity_flag
3171 { "window_bell_flag", FORMAT_TABLE_STRING
,
3172 format_cb_window_bell_flag
3174 { "window_bigger", FORMAT_TABLE_STRING
,
3175 format_cb_window_bigger
3177 { "window_cell_height", FORMAT_TABLE_STRING
,
3178 format_cb_window_cell_height
3180 { "window_cell_width", FORMAT_TABLE_STRING
,
3181 format_cb_window_cell_width
3183 { "window_end_flag", FORMAT_TABLE_STRING
,
3184 format_cb_window_end_flag
3186 { "window_flags", FORMAT_TABLE_STRING
,
3187 format_cb_window_flags
3189 { "window_format", FORMAT_TABLE_STRING
,
3190 format_cb_window_format
3192 { "window_height", FORMAT_TABLE_STRING
,
3193 format_cb_window_height
3195 { "window_id", FORMAT_TABLE_STRING
,
3198 { "window_index", FORMAT_TABLE_STRING
,
3199 format_cb_window_index
3201 { "window_last_flag", FORMAT_TABLE_STRING
,
3202 format_cb_window_last_flag
3204 { "window_layout", FORMAT_TABLE_STRING
,
3205 format_cb_window_layout
3207 { "window_linked", FORMAT_TABLE_STRING
,
3208 format_cb_window_linked
3210 { "window_linked_sessions", FORMAT_TABLE_STRING
,
3211 format_cb_window_linked_sessions
3213 { "window_linked_sessions_list", FORMAT_TABLE_STRING
,
3214 format_cb_window_linked_sessions_list
3216 { "window_marked_flag", FORMAT_TABLE_STRING
,
3217 format_cb_window_marked_flag
3219 { "window_name", FORMAT_TABLE_STRING
,
3220 format_cb_window_name
3222 { "window_offset_x", FORMAT_TABLE_STRING
,
3223 format_cb_window_offset_x
3225 { "window_offset_y", FORMAT_TABLE_STRING
,
3226 format_cb_window_offset_y
3228 { "window_panes", FORMAT_TABLE_STRING
,
3229 format_cb_window_panes
3231 { "window_raw_flags", FORMAT_TABLE_STRING
,
3232 format_cb_window_raw_flags
3234 { "window_silence_flag", FORMAT_TABLE_STRING
,
3235 format_cb_window_silence_flag
3237 { "window_stack_index", FORMAT_TABLE_STRING
,
3238 format_cb_window_stack_index
3240 { "window_start_flag", FORMAT_TABLE_STRING
,
3241 format_cb_window_start_flag
3243 { "window_visible_layout", FORMAT_TABLE_STRING
,
3244 format_cb_window_visible_layout
3246 { "window_width", FORMAT_TABLE_STRING
,
3247 format_cb_window_width
3249 { "window_zoomed_flag", FORMAT_TABLE_STRING
,
3250 format_cb_window_zoomed_flag
3252 { "wrap_flag", FORMAT_TABLE_STRING
,
3257 /* Compare format table entries. */
3259 format_table_compare(const void *key0
, const void *entry0
)
3261 const char *key
= key0
;
3262 const struct format_table_entry
*entry
= entry0
;
3264 return (strcmp(key
, entry
->key
));
3267 /* Get a format callback. */
3268 static struct format_table_entry
*
3269 format_table_get(const char *key
)
3271 return (bsearch(key
, format_table
, nitems(format_table
),
3272 sizeof *format_table
, format_table_compare
));
3275 /* Merge one format tree into another. */
3277 format_merge(struct format_tree
*ft
, struct format_tree
*from
)
3279 struct format_entry
*fe
;
3281 RB_FOREACH(fe
, format_entry_tree
, &from
->tree
) {
3282 if (fe
->value
!= NULL
)
3283 format_add(ft
, fe
->key
, "%s", fe
->value
);
3287 /* Get format pane. */
3288 struct window_pane
*
3289 format_get_pane(struct format_tree
*ft
)
3294 /* Add item bits to tree. */
3296 format_create_add_item(struct format_tree
*ft
, struct cmdq_item
*item
)
3298 struct key_event
*event
= cmdq_get_event(item
);
3299 struct mouse_event
*m
= &event
->m
;
3301 cmdq_merge_formats(item
, ft
);
3302 memcpy(&ft
->m
, m
, sizeof ft
->m
);
3305 /* Create a new tree. */
3306 struct format_tree
*
3307 format_create(struct client
*c
, struct cmdq_item
*item
, int tag
, int flags
)
3309 struct format_tree
*ft
;
3311 ft
= xcalloc(1, sizeof *ft
);
3316 ft
->client
->references
++;
3324 format_create_add_item(ft
, item
);
3331 format_free(struct format_tree
*ft
)
3333 struct format_entry
*fe
, *fe1
;
3335 RB_FOREACH_SAFE(fe
, format_entry_tree
, &ft
->tree
, fe1
) {
3336 RB_REMOVE(format_entry_tree
, &ft
->tree
, fe
);
3342 if (ft
->client
!= NULL
)
3343 server_client_unref(ft
->client
);
3347 /* Log each format. */
3349 format_log_debug_cb(const char *key
, const char *value
, void *arg
)
3351 const char *prefix
= arg
;
3353 log_debug("%s: %s=%s", prefix
, key
, value
);
3356 /* Log a format tree. */
3358 format_log_debug(struct format_tree
*ft
, const char *prefix
)
3360 format_each(ft
, format_log_debug_cb
, (void *)prefix
);
3363 /* Walk each format. */
3365 format_each(struct format_tree
*ft
, void (*cb
)(const char *, const char *,
3368 const struct format_table_entry
*fte
;
3369 struct format_entry
*fe
;
3375 for (i
= 0; i
< nitems(format_table
); i
++) {
3376 fte
= &format_table
[i
];
3378 value
= fte
->cb(ft
);
3381 if (fte
->type
== FORMAT_TABLE_TIME
) {
3383 xsnprintf(s
, sizeof s
, "%lld", (long long)tv
->tv_sec
);
3384 cb(fte
->key
, s
, arg
);
3386 cb(fte
->key
, value
, arg
);
3390 RB_FOREACH(fe
, format_entry_tree
, &ft
->tree
) {
3391 if (fe
->time
!= 0) {
3392 xsnprintf(s
, sizeof s
, "%lld", (long long)fe
->time
);
3393 cb(fe
->key
, s
, arg
);
3395 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3396 fe
->value
= fe
->cb(ft
);
3397 if (fe
->value
== NULL
)
3398 fe
->value
= xstrdup("");
3400 cb(fe
->key
, fe
->value
, arg
);
3405 /* Add a key-value pair. */
3407 format_add(struct format_tree
*ft
, const char *key
, const char *fmt
, ...)
3409 struct format_entry
*fe
;
3410 struct format_entry
*fe_now
;
3413 fe
= xmalloc(sizeof *fe
);
3414 fe
->key
= xstrdup(key
);
3416 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3417 if (fe_now
!= NULL
) {
3420 free(fe_now
->value
);
3428 xvasprintf(&fe
->value
, fmt
, ap
);
3432 /* Add a key and time. */
3434 format_add_tv(struct format_tree
*ft
, const char *key
, struct timeval
*tv
)
3436 struct format_entry
*fe
, *fe_now
;
3438 fe
= xmalloc(sizeof *fe
);
3439 fe
->key
= xstrdup(key
);
3441 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3442 if (fe_now
!= NULL
) {
3445 free(fe_now
->value
);
3450 fe
->time
= tv
->tv_sec
;
3455 /* Add a key and function. */
3457 format_add_cb(struct format_tree
*ft
, const char *key
, format_cb cb
)
3459 struct format_entry
*fe
;
3460 struct format_entry
*fe_now
;
3462 fe
= xmalloc(sizeof *fe
);
3463 fe
->key
= xstrdup(key
);
3465 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3466 if (fe_now
!= NULL
) {
3469 free(fe_now
->value
);
3479 /* Quote shell special characters in string. */
3481 format_quote_shell(const char *s
)
3486 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3487 for (cp
= s
; *cp
!= '\0'; cp
++) {
3488 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp
) != NULL
)
3496 /* Quote #s in string. */
3498 format_quote_style(const char *s
)
3503 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3504 for (cp
= s
; *cp
!= '\0'; cp
++) {
3513 /* Make a prettier time. */
3515 format_pretty_time(time_t t
, int seconds
)
3517 struct tm now_tm
, tm
;
3526 localtime_r(&now
, &now_tm
);
3527 localtime_r(&t
, &tm
);
3529 /* Last 24 hours. */
3530 if (age
< 24 * 3600) {
3532 strftime(s
, sizeof s
, "%H:%M:%S", &tm
);
3534 strftime(s
, sizeof s
, "%H:%M", &tm
);
3535 return (xstrdup(s
));
3538 /* This month or last 28 days. */
3539 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
== now_tm
.tm_mon
) ||
3540 age
< 28 * 24 * 3600) {
3541 strftime(s
, sizeof s
, "%a%d", &tm
);
3542 return (xstrdup(s
));
3545 /* Last 12 months. */
3546 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
< now_tm
.tm_mon
) ||
3547 (tm
.tm_year
== now_tm
.tm_year
- 1 && tm
.tm_mon
> now_tm
.tm_mon
)) {
3548 strftime(s
, sizeof s
, "%d%b", &tm
);
3549 return (xstrdup(s
));
3552 /* Older than that. */
3553 strftime(s
, sizeof s
, "%h%y", &tm
);
3554 return (xstrdup(s
));
3557 /* Find a format entry. */
3559 format_find(struct format_tree
*ft
, const char *key
, int modifiers
,
3560 const char *time_format
)
3562 struct format_table_entry
*fte
;
3564 struct format_entry
*fe
, fe_find
;
3565 struct environ_entry
*envent
;
3566 struct options_entry
*o
;
3568 char *found
= NULL
, *saved
, s
[512];
3573 o
= options_parse_get(global_options
, key
, &idx
, 0);
3574 if (o
== NULL
&& ft
->wp
!= NULL
)
3575 o
= options_parse_get(ft
->wp
->options
, key
, &idx
, 0);
3576 if (o
== NULL
&& ft
->w
!= NULL
)
3577 o
= options_parse_get(ft
->w
->options
, key
, &idx
, 0);
3579 o
= options_parse_get(global_w_options
, key
, &idx
, 0);
3580 if (o
== NULL
&& ft
->s
!= NULL
)
3581 o
= options_parse_get(ft
->s
->options
, key
, &idx
, 0);
3583 o
= options_parse_get(global_s_options
, key
, &idx
, 0);
3585 found
= options_to_string(o
, idx
, 1);
3589 fte
= format_table_get(key
);
3591 value
= fte
->cb(ft
);
3592 if (fte
->type
== FORMAT_TABLE_TIME
&& value
!= NULL
)
3593 t
= ((struct timeval
*)value
)->tv_sec
;
3598 fe_find
.key
= (char *)key
;
3599 fe
= RB_FIND(format_entry_tree
, &ft
->tree
, &fe_find
);
3601 if (fe
->time
!= 0) {
3605 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3606 fe
->value
= fe
->cb(ft
);
3607 if (fe
->value
== NULL
)
3608 fe
->value
= xstrdup("");
3610 found
= xstrdup(fe
->value
);
3614 if (~modifiers
& FORMAT_TIMESTRING
) {
3617 envent
= environ_find(ft
->s
->environ
, key
);
3619 envent
= environ_find(global_environ
, key
);
3620 if (envent
!= NULL
&& envent
->value
!= NULL
) {
3621 found
= xstrdup(envent
->value
);
3629 if (modifiers
& FORMAT_TIMESTRING
) {
3630 if (t
== 0 && found
!= NULL
) {
3631 t
= strtonum(found
, 0, INT64_MAX
, &errstr
);
3638 if (modifiers
& FORMAT_PRETTY
)
3639 found
= format_pretty_time(t
, 0);
3641 if (time_format
!= NULL
) {
3642 localtime_r(&t
, &tm
);
3643 strftime(s
, sizeof s
, time_format
, &tm
);
3646 s
[strcspn(s
, "\n")] = '\0';
3654 xasprintf(&found
, "%lld", (long long)t
);
3655 else if (found
== NULL
)
3657 if (modifiers
& FORMAT_BASENAME
) {
3659 found
= xstrdup(basename(saved
));
3662 if (modifiers
& FORMAT_DIRNAME
) {
3664 found
= xstrdup(dirname(saved
));
3667 if (modifiers
& FORMAT_QUOTE_SHELL
) {
3669 found
= format_quote_shell(saved
);
3672 if (modifiers
& FORMAT_QUOTE_STYLE
) {
3674 found
= format_quote_style(saved
);
3680 /* Unescape escaped characters. */
3682 format_unescape(const char *s
)
3687 cp
= out
= xmalloc(strlen(s
) + 1);
3688 for (; *s
!= '\0'; s
++) {
3689 if (*s
== '#' && s
[1] == '{')
3691 if (brackets
== 0 &&
3693 strchr(",#{}:", s
[1]) != NULL
) {
3705 /* Remove escaped characters. */
3707 format_strip(const char *s
)
3712 cp
= out
= xmalloc(strlen(s
) + 1);
3713 for (; *s
!= '\0'; s
++) {
3714 if (*s
== '#' && s
[1] == '{')
3716 if (*s
== '#' && strchr(",#{}:", s
[1]) != NULL
) {
3729 /* Skip until end. */
3731 format_skip(const char *s
, const char *end
)
3735 for (; *s
!= '\0'; s
++) {
3736 if (*s
== '#' && s
[1] == '{')
3740 strchr(",#{}:", s
[1]) != NULL
) {
3746 if (strchr(end
, *s
) != NULL
&& brackets
== 0)
3754 /* Return left and right alternatives separated by commas. */
3756 format_choose(struct format_expand_state
*es
, const char *s
, char **left
,
3757 char **right
, int expand
)
3760 char *left0
, *right0
;
3762 cp
= format_skip(s
, ",");
3765 left0
= xstrndup(s
, cp
- s
);
3766 right0
= xstrdup(cp
+ 1);
3769 *left
= format_expand1(es
, left0
);
3771 *right
= format_expand1(es
, right0
);
3782 format_true(const char *s
)
3784 if (s
!= NULL
&& *s
!= '\0' && (s
[0] != '0' || s
[1] != '\0'))
3789 /* Check if modifier end. */
3791 format_is_end(char c
)
3793 return (c
== ';' || c
== ':');
3796 /* Add to modifier list. */
3798 format_add_modifier(struct format_modifier
**list
, u_int
*count
,
3799 const char *c
, size_t n
, char **argv
, int argc
)
3801 struct format_modifier
*fm
;
3803 *list
= xreallocarray(*list
, (*count
) + 1, sizeof **list
);
3804 fm
= &(*list
)[(*count
)++];
3806 memcpy(fm
->modifier
, c
, n
);
3807 fm
->modifier
[n
] = '\0';
3814 /* Free modifier list. */
3816 format_free_modifiers(struct format_modifier
*list
, u_int count
)
3820 for (i
= 0; i
< count
; i
++)
3821 cmd_free_argv(list
[i
].argc
, list
[i
].argv
);
3825 /* Build modifier list. */
3826 static struct format_modifier
*
3827 format_build_modifiers(struct format_expand_state
*es
, const char **s
,
3830 const char *cp
= *s
, *end
;
3831 struct format_modifier
*list
= NULL
;
3832 char c
, last
[] = "X;:", **argv
, *value
;
3836 * Modifiers are a ; separated list of the forms:
3837 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3848 while (*cp
!= '\0' && *cp
!= ':') {
3849 /* Skip any separator character. */
3853 /* Check single character modifiers with no arguments. */
3854 if (strchr("labcdnwETSWPL<>", cp
[0]) != NULL
&&
3855 format_is_end(cp
[1])) {
3856 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3861 /* Then try double character with no arguments. */
3862 if ((memcmp("||", cp
, 2) == 0 ||
3863 memcmp("&&", cp
, 2) == 0 ||
3864 memcmp("!=", cp
, 2) == 0 ||
3865 memcmp("==", cp
, 2) == 0 ||
3866 memcmp("<=", cp
, 2) == 0 ||
3867 memcmp(">=", cp
, 2) == 0) &&
3868 format_is_end(cp
[2])) {
3869 format_add_modifier(&list
, count
, cp
, 2, NULL
, 0);
3874 /* Now try single character with arguments. */
3875 if (strchr("mCNst=peq", cp
[0]) == NULL
)
3879 /* No arguments provided. */
3880 if (format_is_end(cp
[1])) {
3881 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3888 /* Single argument with no wrapper character. */
3889 if (!ispunct((u_char
)cp
[1]) || cp
[1] == '-') {
3890 end
= format_skip(cp
+ 1, ":;");
3894 argv
= xcalloc(1, sizeof *argv
);
3895 value
= xstrndup(cp
+ 1, end
- (cp
+ 1));
3896 argv
[0] = format_expand1(es
, value
);
3900 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3905 /* Multiple arguments with a wrapper character. */
3909 if (cp
[0] == last
[0] && format_is_end(cp
[1])) {
3913 end
= format_skip(cp
+ 1, last
);
3918 argv
= xreallocarray(argv
, argc
+ 1, sizeof *argv
);
3919 value
= xstrndup(cp
, end
- cp
);
3920 argv
[argc
++] = format_expand1(es
, value
);
3924 } while (!format_is_end(cp
[0]));
3925 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3928 format_free_modifiers(list
, *count
);
3936 /* Match against an fnmatch(3) pattern or regular expression. */
3938 format_match(struct format_modifier
*fm
, const char *pattern
, const char *text
)
3946 if (strchr(s
, 'r') == NULL
) {
3947 if (strchr(s
, 'i') != NULL
)
3948 flags
|= FNM_CASEFOLD
;
3949 if (fnmatch(pattern
, text
, flags
) != 0)
3950 return (xstrdup("0"));
3952 flags
= REG_EXTENDED
|REG_NOSUB
;
3953 if (strchr(s
, 'i') != NULL
)
3955 if (regcomp(&r
, pattern
, flags
) != 0)
3956 return (xstrdup("0"));
3957 if (regexec(&r
, text
, 0, NULL
, 0) != 0) {
3959 return (xstrdup("0"));
3963 return (xstrdup("1"));
3966 /* Perform substitution in string. */
3968 format_sub(struct format_modifier
*fm
, const char *text
, const char *pattern
,
3972 int flags
= REG_EXTENDED
;
3974 if (fm
->argc
>= 3 && strchr(fm
->argv
[2], 'i') != NULL
)
3976 value
= regsub(pattern
, with
, text
, flags
);
3978 return (xstrdup(text
));
3982 /* Search inside pane. */
3984 format_search(struct format_modifier
*fm
, struct window_pane
*wp
, const char *s
)
3986 int ignore
= 0, regex
= 0;
3989 if (fm
->argc
>= 1) {
3990 if (strchr(fm
->argv
[0], 'i') != NULL
)
3992 if (strchr(fm
->argv
[0], 'r') != NULL
)
3995 xasprintf(&value
, "%u", window_pane_search(wp
, s
, regex
, ignore
));
3999 /* Does session name exist? */
4001 format_session_name(struct format_expand_state
*es
, const char *fmt
)
4006 name
= format_expand1(es
, fmt
);
4007 RB_FOREACH(s
, sessions
, &sessions
) {
4008 if (strcmp(s
->name
, name
) == 0) {
4010 return (xstrdup("1"));
4014 return (xstrdup("0"));
4017 /* Loop over sessions. */
4019 format_loop_sessions(struct format_expand_state
*es
, const char *fmt
)
4021 struct format_tree
*ft
= es
->ft
;
4022 struct client
*c
= ft
->client
;
4023 struct cmdq_item
*item
= ft
->item
;
4024 struct format_tree
*nft
;
4025 struct format_expand_state next
;
4026 char *expanded
, *value
;
4030 value
= xcalloc(1, 1);
4033 RB_FOREACH(s
, sessions
, &sessions
) {
4034 format_log(es
, "session loop: $%u", s
->id
);
4035 nft
= format_create(c
, item
, FORMAT_NONE
, ft
->flags
);
4036 format_defaults(nft
, ft
->c
, s
, NULL
, NULL
);
4037 format_copy_state(&next
, es
, 0);
4039 expanded
= format_expand1(&next
, fmt
);
4040 format_free(next
.ft
);
4042 valuelen
+= strlen(expanded
);
4043 value
= xrealloc(value
, valuelen
);
4045 strlcat(value
, expanded
, valuelen
);
4052 /* Does window name exist? */
4054 format_window_name(struct format_expand_state
*es
, const char *fmt
)
4056 struct format_tree
*ft
= es
->ft
;
4060 if (ft
->s
== NULL
) {
4061 format_log(es
, "window name but no session");
4065 name
= format_expand1(es
, fmt
);
4066 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
4067 if (strcmp(wl
->window
->name
, name
) == 0) {
4069 return (xstrdup("1"));
4073 return (xstrdup("0"));
4076 /* Loop over windows. */
4078 format_loop_windows(struct format_expand_state
*es
, const char *fmt
)
4080 struct format_tree
*ft
= es
->ft
;
4081 struct client
*c
= ft
->client
;
4082 struct cmdq_item
*item
= ft
->item
;
4083 struct format_tree
*nft
;
4084 struct format_expand_state next
;
4085 char *all
, *active
, *use
, *expanded
, *value
;
4090 if (ft
->s
== NULL
) {
4091 format_log(es
, "window loop but no session");
4095 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4100 value
= xcalloc(1, 1);
4103 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
4105 format_log(es
, "window loop: %u @%u", wl
->idx
, w
->id
);
4106 if (active
!= NULL
&& wl
== ft
->s
->curw
)
4110 nft
= format_create(c
, item
, FORMAT_WINDOW
|w
->id
, ft
->flags
);
4111 format_defaults(nft
, ft
->c
, ft
->s
, wl
, NULL
);
4112 format_copy_state(&next
, es
, 0);
4114 expanded
= format_expand1(&next
, use
);
4117 valuelen
+= strlen(expanded
);
4118 value
= xrealloc(value
, valuelen
);
4120 strlcat(value
, expanded
, valuelen
);
4130 /* Loop over panes. */
4132 format_loop_panes(struct format_expand_state
*es
, const char *fmt
)
4134 struct format_tree
*ft
= es
->ft
;
4135 struct client
*c
= ft
->client
;
4136 struct cmdq_item
*item
= ft
->item
;
4137 struct format_tree
*nft
;
4138 struct format_expand_state next
;
4139 char *all
, *active
, *use
, *expanded
, *value
;
4141 struct window_pane
*wp
;
4143 if (ft
->w
== NULL
) {
4144 format_log(es
, "pane loop but no window");
4148 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4153 value
= xcalloc(1, 1);
4156 TAILQ_FOREACH(wp
, &ft
->w
->panes
, entry
) {
4157 format_log(es
, "pane loop: %%%u", wp
->id
);
4158 if (active
!= NULL
&& wp
== ft
->w
->active
)
4162 nft
= format_create(c
, item
, FORMAT_PANE
|wp
->id
, ft
->flags
);
4163 format_defaults(nft
, ft
->c
, ft
->s
, ft
->wl
, wp
);
4164 format_copy_state(&next
, es
, 0);
4166 expanded
= format_expand1(&next
, use
);
4169 valuelen
+= strlen(expanded
);
4170 value
= xrealloc(value
, valuelen
);
4172 strlcat(value
, expanded
, valuelen
);
4182 /* Loop over clients. */
4184 format_loop_clients(struct format_expand_state
*es
, const char *fmt
)
4186 struct format_tree
*ft
= es
->ft
;
4188 struct cmdq_item
*item
= ft
->item
;
4189 struct format_tree
*nft
;
4190 struct format_expand_state next
;
4191 char *expanded
, *value
;
4194 value
= xcalloc(1, 1);
4197 TAILQ_FOREACH(c
, &clients
, entry
) {
4198 format_log(es
, "client loop: %s", c
->name
);
4199 nft
= format_create(c
, item
, 0, ft
->flags
);
4200 format_defaults(nft
, c
, ft
->s
, ft
->wl
, ft
->wp
);
4201 format_copy_state(&next
, es
, 0);
4203 expanded
= format_expand1(&next
, fmt
);
4206 valuelen
+= strlen(expanded
);
4207 value
= xrealloc(value
, valuelen
);
4209 strlcat(value
, expanded
, valuelen
);
4217 format_replace_expression(struct format_modifier
*mexp
,
4218 struct format_expand_state
*es
, const char *copy
)
4220 int argc
= mexp
->argc
;
4222 char *endch
, *value
, *left
= NULL
, *right
= NULL
;
4225 double mleft
, mright
, result
;
4236 LESS_THAN_EQUAL
} operator;
4238 if (strcmp(mexp
->argv
[0], "+") == 0)
4240 else if (strcmp(mexp
->argv
[0], "-") == 0)
4241 operator = SUBTRACT
;
4242 else if (strcmp(mexp
->argv
[0], "*") == 0)
4243 operator = MULTIPLY
;
4244 else if (strcmp(mexp
->argv
[0], "/") == 0)
4246 else if (strcmp(mexp
->argv
[0], "%") == 0 ||
4247 strcmp(mexp
->argv
[0], "m") == 0)
4249 else if (strcmp(mexp
->argv
[0], "==") == 0)
4251 else if (strcmp(mexp
->argv
[0], "!=") == 0)
4252 operator = NOT_EQUAL
;
4253 else if (strcmp(mexp
->argv
[0], ">") == 0)
4254 operator = GREATER_THAN
;
4255 else if (strcmp(mexp
->argv
[0], "<") == 0)
4256 operator = LESS_THAN
;
4257 else if (strcmp(mexp
->argv
[0], ">=") == 0)
4258 operator = GREATER_THAN_EQUAL
;
4259 else if (strcmp(mexp
->argv
[0], "<=") == 0)
4260 operator = LESS_THAN_EQUAL
;
4262 format_log(es
, "expression has no valid operator: '%s'",
4267 /* The second argument may be flags. */
4268 if (argc
>= 2 && strchr(mexp
->argv
[1], 'f') != NULL
) {
4273 /* The third argument may be precision. */
4275 prec
= strtonum(mexp
->argv
[2], INT_MIN
, INT_MAX
, &errstr
);
4276 if (errstr
!= NULL
) {
4277 format_log(es
, "expression precision %s: %s", errstr
,
4283 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4284 format_log(es
, "expression syntax error");
4288 mleft
= strtod(left
, &endch
);
4289 if (*endch
!= '\0') {
4290 format_log(es
, "expression left side is invalid: %s", left
);
4294 mright
= strtod(right
, &endch
);
4295 if (*endch
!= '\0') {
4296 format_log(es
, "expression right side is invalid: %s", right
);
4301 mleft
= (long long)mleft
;
4302 mright
= (long long)mright
;
4304 format_log(es
, "expression left side is: %.*f", prec
, mleft
);
4305 format_log(es
, "expression right side is: %.*f", prec
, mright
);
4309 result
= mleft
+ mright
;
4312 result
= mleft
- mright
;
4315 result
= mleft
* mright
;
4318 result
= mleft
/ mright
;
4321 result
= fmod(mleft
, mright
);
4324 result
= fabs(mleft
- mright
) < 1e-9;
4327 result
= fabs(mleft
- mright
) > 1e-9;
4330 result
= (mleft
> mright
);
4332 case GREATER_THAN_EQUAL
:
4333 result
= (mleft
>= mright
);
4336 result
= (mleft
< mright
);
4338 case LESS_THAN_EQUAL
:
4339 result
= (mleft
<= mright
);
4343 xasprintf(&value
, "%.*f", prec
, result
);
4345 xasprintf(&value
, "%.*f", prec
, (double)(long long)result
);
4346 format_log(es
, "expression result is %s", value
);
4358 /* Replace a key. */
4360 format_replace(struct format_expand_state
*es
, const char *key
, size_t keylen
,
4361 char **buf
, size_t *len
, size_t *off
)
4363 struct format_tree
*ft
= es
->ft
;
4364 struct window_pane
*wp
= ft
->wp
;
4365 const char *errstr
, *copy
, *cp
, *marker
= NULL
;
4366 const char *time_format
= NULL
;
4367 char *copy0
, *condition
, *found
, *new;
4368 char *value
, *left
, *right
;
4370 int modifiers
= 0, limit
= 0, width
= 0;
4372 struct format_modifier
*list
, *cmp
= NULL
, *search
= NULL
;
4373 struct format_modifier
**sub
= NULL
, *mexp
= NULL
, *fm
;
4374 u_int i
, count
, nsub
= 0;
4375 struct format_expand_state next
;
4377 /* Make a copy of the key. */
4378 copy
= copy0
= xstrndup(key
, keylen
);
4380 /* Process modifier list. */
4381 list
= format_build_modifiers(es
, ©
, &count
);
4382 for (i
= 0; i
< count
; i
++) {
4384 if (format_logging(ft
)) {
4385 format_log(es
, "modifier %u is %s", i
, fm
->modifier
);
4386 for (j
= 0; j
< fm
->argc
; j
++) {
4387 format_log(es
, "modifier %u argument %d: %s", i
,
4391 if (fm
->size
== 1) {
4392 switch (fm
->modifier
[0]) {
4404 sub
= xreallocarray(sub
, nsub
+ 1, sizeof *sub
);
4410 limit
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4414 if (fm
->argc
>= 2 && fm
->argv
[1] != NULL
)
4415 marker
= fm
->argv
[1];
4420 width
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4426 modifiers
|= FORMAT_WIDTH
;
4429 if (fm
->argc
< 1 || fm
->argc
> 3)
4434 modifiers
|= FORMAT_LITERAL
;
4437 modifiers
|= FORMAT_CHARACTER
;
4440 modifiers
|= FORMAT_BASENAME
;
4443 modifiers
|= FORMAT_COLOUR
;
4446 modifiers
|= FORMAT_DIRNAME
;
4449 modifiers
|= FORMAT_LENGTH
;
4452 modifiers
|= FORMAT_TIMESTRING
;
4455 if (strchr(fm
->argv
[0], 'p') != NULL
)
4456 modifiers
|= FORMAT_PRETTY
;
4457 else if (fm
->argc
>= 2 &&
4458 strchr(fm
->argv
[0], 'f') != NULL
)
4459 time_format
= format_strip(fm
->argv
[1]);
4463 modifiers
|= FORMAT_QUOTE_SHELL
;
4464 else if (strchr(fm
->argv
[0], 'e') != NULL
||
4465 strchr(fm
->argv
[0], 'h') != NULL
)
4466 modifiers
|= FORMAT_QUOTE_STYLE
;
4469 modifiers
|= FORMAT_EXPAND
;
4472 modifiers
|= FORMAT_EXPANDTIME
;
4476 strchr(fm
->argv
[0], 'w') != NULL
)
4477 modifiers
|= FORMAT_WINDOW_NAME
;
4478 else if (strchr(fm
->argv
[0], 's') != NULL
)
4479 modifiers
|= FORMAT_SESSION_NAME
;
4482 modifiers
|= FORMAT_SESSIONS
;
4485 modifiers
|= FORMAT_WINDOWS
;
4488 modifiers
|= FORMAT_PANES
;
4491 modifiers
|= FORMAT_CLIENTS
;
4494 } else if (fm
->size
== 2) {
4495 if (strcmp(fm
->modifier
, "||") == 0 ||
4496 strcmp(fm
->modifier
, "&&") == 0 ||
4497 strcmp(fm
->modifier
, "==") == 0 ||
4498 strcmp(fm
->modifier
, "!=") == 0 ||
4499 strcmp(fm
->modifier
, ">=") == 0 ||
4500 strcmp(fm
->modifier
, "<=") == 0)
4505 /* Is this a literal string? */
4506 if (modifiers
& FORMAT_LITERAL
) {
4507 format_log(es
, "literal string is '%s'", copy
);
4508 value
= format_unescape(copy
);
4512 /* Is this a character? */
4513 if (modifiers
& FORMAT_CHARACTER
) {
4514 new = format_expand1(es
, copy
);
4515 c
= strtonum(new, 32, 126, &errstr
);
4517 value
= xstrdup("");
4519 xasprintf(&value
, "%c", c
);
4524 /* Is this a colour? */
4525 if (modifiers
& FORMAT_COLOUR
) {
4526 new = format_expand1(es
, copy
);
4527 c
= colour_fromstring(new);
4528 if (c
== -1 || (c
= colour_force_rgb(c
)) == -1)
4529 value
= xstrdup("");
4531 xasprintf(&value
, "%06x", c
& 0xffffff);
4536 /* Is this a loop, comparison or condition? */
4537 if (modifiers
& FORMAT_SESSIONS
) {
4538 value
= format_loop_sessions(es
, copy
);
4541 } else if (modifiers
& FORMAT_WINDOWS
) {
4542 value
= format_loop_windows(es
, copy
);
4545 } else if (modifiers
& FORMAT_PANES
) {
4546 value
= format_loop_panes(es
, copy
);
4549 } else if (modifiers
& FORMAT_CLIENTS
) {
4550 value
= format_loop_clients(es
, copy
);
4553 } else if (modifiers
& FORMAT_WINDOW_NAME
) {
4554 value
= format_window_name(es
, copy
);
4557 } else if (modifiers
& FORMAT_SESSION_NAME
) {
4558 value
= format_session_name(es
, copy
);
4561 } else if (search
!= NULL
) {
4562 /* Search in pane. */
4563 new = format_expand1(es
, copy
);
4565 format_log(es
, "search '%s' but no pane", new);
4566 value
= xstrdup("0");
4568 format_log(es
, "search '%s' pane %%%u", new, wp
->id
);
4569 value
= format_search(search
, wp
, new);
4572 } else if (cmp
!= NULL
) {
4573 /* Comparison of left and right. */
4574 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4575 format_log(es
, "compare %s syntax error: %s",
4576 cmp
->modifier
, copy
);
4579 format_log(es
, "compare %s left is: %s", cmp
->modifier
, left
);
4580 format_log(es
, "compare %s right is: %s", cmp
->modifier
, right
);
4582 if (strcmp(cmp
->modifier
, "||") == 0) {
4583 if (format_true(left
) || format_true(right
))
4584 value
= xstrdup("1");
4586 value
= xstrdup("0");
4587 } else if (strcmp(cmp
->modifier
, "&&") == 0) {
4588 if (format_true(left
) && format_true(right
))
4589 value
= xstrdup("1");
4591 value
= xstrdup("0");
4592 } else if (strcmp(cmp
->modifier
, "==") == 0) {
4593 if (strcmp(left
, right
) == 0)
4594 value
= xstrdup("1");
4596 value
= xstrdup("0");
4597 } else if (strcmp(cmp
->modifier
, "!=") == 0) {
4598 if (strcmp(left
, right
) != 0)
4599 value
= xstrdup("1");
4601 value
= xstrdup("0");
4602 } else if (strcmp(cmp
->modifier
, "<") == 0) {
4603 if (strcmp(left
, right
) < 0)
4604 value
= xstrdup("1");
4606 value
= xstrdup("0");
4607 } else if (strcmp(cmp
->modifier
, ">") == 0) {
4608 if (strcmp(left
, right
) > 0)
4609 value
= xstrdup("1");
4611 value
= xstrdup("0");
4612 } else if (strcmp(cmp
->modifier
, "<=") == 0) {
4613 if (strcmp(left
, right
) <= 0)
4614 value
= xstrdup("1");
4616 value
= xstrdup("0");
4617 } else if (strcmp(cmp
->modifier
, ">=") == 0) {
4618 if (strcmp(left
, right
) >= 0)
4619 value
= xstrdup("1");
4621 value
= xstrdup("0");
4622 } else if (strcmp(cmp
->modifier
, "m") == 0)
4623 value
= format_match(cmp
, left
, right
);
4627 } else if (*copy
== '?') {
4628 /* Conditional: check first and choose second or third. */
4629 cp
= format_skip(copy
+ 1, ",");
4631 format_log(es
, "condition syntax error: %s", copy
+ 1);
4634 condition
= xstrndup(copy
+ 1, cp
- (copy
+ 1));
4635 format_log(es
, "condition is: %s", condition
);
4637 found
= format_find(ft
, condition
, modifiers
, time_format
);
4638 if (found
== NULL
) {
4640 * If the condition not found, try to expand it. If
4641 * the expansion doesn't have any effect, then assume
4644 found
= format_expand1(es
, condition
);
4645 if (strcmp(found
, condition
) == 0) {
4647 found
= xstrdup("");
4649 "condition '%s' not found; assuming false",
4653 format_log(es
, "condition '%s' found: %s", condition
,
4657 if (format_choose(es
, cp
+ 1, &left
, &right
, 0) != 0) {
4658 format_log(es
, "condition '%s' syntax error: %s",
4663 if (format_true(found
)) {
4664 format_log(es
, "condition '%s' is true", condition
);
4665 value
= format_expand1(es
, left
);
4667 format_log(es
, "condition '%s' is false", condition
);
4668 value
= format_expand1(es
, right
);
4675 } else if (mexp
!= NULL
) {
4676 value
= format_replace_expression(mexp
, es
, copy
);
4678 value
= xstrdup("");
4680 if (strstr(copy
, "#{") != 0) {
4681 format_log(es
, "expanding inner format '%s'", copy
);
4682 value
= format_expand1(es
, copy
);
4684 value
= format_find(ft
, copy
, modifiers
, time_format
);
4685 if (value
== NULL
) {
4686 format_log(es
, "format '%s' not found", copy
);
4687 value
= xstrdup("");
4689 format_log(es
, "format '%s' found: %s", copy
,
4696 /* Expand again if required. */
4697 if (modifiers
& FORMAT_EXPAND
) {
4698 new = format_expand1(es
, value
);
4701 } else if (modifiers
& FORMAT_EXPANDTIME
) {
4702 format_copy_state(&next
, es
, FORMAT_EXPAND_TIME
);
4703 new = format_expand1(&next
, value
);
4708 /* Perform substitution if any. */
4709 for (i
= 0; i
< nsub
; i
++) {
4710 left
= format_expand1(es
, sub
[i
]->argv
[0]);
4711 right
= format_expand1(es
, sub
[i
]->argv
[1]);
4712 new = format_sub(sub
[i
], value
, left
, right
);
4713 format_log(es
, "substitute '%s' to '%s': %s", left
, right
, new);
4720 /* Truncate the value if needed. */
4722 new = format_trim_left(value
, limit
);
4723 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4725 xasprintf(&value
, "%s%s", new, marker
);
4730 format_log(es
, "applied length limit %d: %s", limit
, value
);
4731 } else if (limit
< 0) {
4732 new = format_trim_right(value
, -limit
);
4733 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4735 xasprintf(&value
, "%s%s", marker
, new);
4740 format_log(es
, "applied length limit %d: %s", limit
, value
);
4743 /* Pad the value if needed. */
4745 new = utf8_padcstr(value
, width
);
4748 format_log(es
, "applied padding width %d: %s", width
, value
);
4749 } else if (width
< 0) {
4750 new = utf8_rpadcstr(value
, -width
);
4753 format_log(es
, "applied padding width %d: %s", width
, value
);
4756 /* Replace with the length or width if needed. */
4757 if (modifiers
& FORMAT_LENGTH
) {
4758 xasprintf(&new, "%zu", strlen(value
));
4761 format_log(es
, "replacing with length: %s", new);
4763 if (modifiers
& FORMAT_WIDTH
) {
4764 xasprintf(&new, "%u", format_width(value
));
4767 format_log(es
, "replacing with width: %s", new);
4770 /* Expand the buffer and copy in the value. */
4771 valuelen
= strlen(value
);
4772 while (*len
- *off
< valuelen
+ 1) {
4773 *buf
= xreallocarray(*buf
, 2, *len
);
4776 memcpy(*buf
+ *off
, value
, valuelen
);
4779 format_log(es
, "replaced '%s' with '%s'", copy0
, value
);
4783 format_free_modifiers(list
, count
);
4788 format_log(es
, "failed %s", copy0
);
4791 format_free_modifiers(list
, count
);
4796 /* Expand keys in a template. */
4798 format_expand1(struct format_expand_state
*es
, const char *fmt
)
4800 struct format_tree
*ft
= es
->ft
;
4801 char *buf
, *out
, *name
;
4802 const char *ptr
, *s
, *style_end
= NULL
;
4803 size_t off
, len
, n
, outlen
;
4805 char expanded
[8192];
4807 if (fmt
== NULL
|| *fmt
== '\0')
4808 return (xstrdup(""));
4810 if (es
->loop
== FORMAT_LOOP_LIMIT
) {
4811 format_log(es
, "reached loop limit (%u)", FORMAT_LOOP_LIMIT
);
4812 return (xstrdup(""));
4816 format_log(es
, "expanding format: %s", fmt
);
4818 if ((es
->flags
& FORMAT_EXPAND_TIME
) && strchr(fmt
, '%') != NULL
) {
4819 if (es
->time
== 0) {
4820 es
->time
= time(NULL
);
4821 localtime_r(&es
->time
, &es
->tm
);
4823 if (strftime(expanded
, sizeof expanded
, fmt
, &es
->tm
) == 0) {
4824 format_log(es
, "format is too long");
4825 return (xstrdup(""));
4827 if (format_logging(ft
) && strcmp(expanded
, fmt
) != 0)
4828 format_log(es
, "after time expanded: %s", expanded
);
4836 while (*fmt
!= '\0') {
4838 while (len
- off
< 2) {
4839 buf
= xreallocarray(buf
, 2, len
);
4842 buf
[off
++] = *fmt
++;
4847 ch
= (u_char
)*fmt
++;
4851 for (ptr
= fmt
; *ptr
!= '\0'; ptr
++) {
4854 if (*ptr
== ')' && --brackets
== 0)
4857 if (*ptr
!= ')' || brackets
!= 0)
4861 name
= xstrndup(fmt
, n
);
4862 format_log(es
, "found #(): %s", name
);
4864 if ((ft
->flags
& FORMAT_NOJOBS
) ||
4865 (es
->flags
& FORMAT_EXPAND_NOJOBS
)) {
4867 format_log(es
, "#() is disabled");
4869 out
= format_job_get(es
, name
);
4870 format_log(es
, "#() result: %s", out
);
4874 outlen
= strlen(out
);
4875 while (len
- off
< outlen
+ 1) {
4876 buf
= xreallocarray(buf
, 2, len
);
4879 memcpy(buf
+ off
, out
, outlen
);
4887 ptr
= format_skip((char *)fmt
- 2, "}");
4892 format_log(es
, "found #{}: %.*s", (int)n
, fmt
);
4893 if (format_replace(es
, fmt
, n
, &buf
, &len
, &off
) != 0)
4900 * If ##[ (with two or more #s), then it is a style and
4901 * can be left for format_draw to handle.
4903 ptr
= fmt
- (ch
== '[');
4904 n
= 2 - (ch
== '[');
4905 while (*ptr
== '#') {
4910 style_end
= format_skip(fmt
- 2, "]");
4911 format_log(es
, "found #*%zu[", n
);
4912 while (len
- off
< n
+ 2) {
4913 buf
= xreallocarray(buf
, 2, len
);
4916 memcpy(buf
+ off
, fmt
- 2, n
+ 1);
4924 format_log(es
, "found #%c", ch
);
4925 while (len
- off
< 2) {
4926 buf
= xreallocarray(buf
, 2, len
);
4933 if (fmt
> style_end
) { /* skip inside #[] */
4934 if (ch
>= 'A' && ch
<= 'Z')
4935 s
= format_upper
[ch
- 'A'];
4936 else if (ch
>= 'a' && ch
<= 'z')
4937 s
= format_lower
[ch
- 'a'];
4940 while (len
- off
< 3) {
4941 buf
= xreallocarray(buf
, 2, len
);
4949 format_log(es
, "found #%c: %s", ch
, s
);
4950 if (format_replace(es
, s
, n
, &buf
, &len
, &off
) != 0)
4959 format_log(es
, "result is: %s", buf
);
4965 /* Expand keys in a template, passing through strftime first. */
4967 format_expand_time(struct format_tree
*ft
, const char *fmt
)
4969 struct format_expand_state es
;
4971 memset(&es
, 0, sizeof es
);
4973 es
.flags
= FORMAT_EXPAND_TIME
;
4974 return (format_expand1(&es
, fmt
));
4977 /* Expand keys in a template. */
4979 format_expand(struct format_tree
*ft
, const char *fmt
)
4981 struct format_expand_state es
;
4983 memset(&es
, 0, sizeof es
);
4986 return (format_expand1(&es
, fmt
));
4989 /* Expand a single string. */
4991 format_single(struct cmdq_item
*item
, const char *fmt
, struct client
*c
,
4992 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
4994 struct format_tree
*ft
;
4997 ft
= format_create_defaults(item
, c
, s
, wl
, wp
);
4998 expanded
= format_expand(ft
, fmt
);
5003 /* Expand a single string using state. */
5005 format_single_from_state(struct cmdq_item
*item
, const char *fmt
,
5006 struct client
*c
, struct cmd_find_state
*fs
)
5008 return (format_single(item
, fmt
, c
, fs
->s
, fs
->wl
, fs
->wp
));
5011 /* Expand a single string using target. */
5013 format_single_from_target(struct cmdq_item
*item
, const char *fmt
)
5015 struct client
*tc
= cmdq_get_target_client(item
);
5017 return (format_single_from_state(item
, fmt
, tc
, cmdq_get_target(item
)));
5020 /* Create and add defaults. */
5021 struct format_tree
*
5022 format_create_defaults(struct cmdq_item
*item
, struct client
*c
,
5023 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
5025 struct format_tree
*ft
;
5028 ft
= format_create(cmdq_get_client(item
), item
, FORMAT_NONE
, 0);
5030 ft
= format_create(NULL
, item
, FORMAT_NONE
, 0);
5031 format_defaults(ft
, c
, s
, wl
, wp
);
5035 /* Create and add defaults using state. */
5036 struct format_tree
*
5037 format_create_from_state(struct cmdq_item
*item
, struct client
*c
,
5038 struct cmd_find_state
*fs
)
5040 return (format_create_defaults(item
, c
, fs
->s
, fs
->wl
, fs
->wp
));
5043 /* Create and add defaults using target. */
5044 struct format_tree
*
5045 format_create_from_target(struct cmdq_item
*item
)
5047 struct client
*tc
= cmdq_get_target_client(item
);
5049 return (format_create_from_state(item
, tc
, cmdq_get_target(item
)));
5052 /* Set defaults for any of arguments that are not NULL. */
5054 format_defaults(struct format_tree
*ft
, struct client
*c
, struct session
*s
,
5055 struct winlink
*wl
, struct window_pane
*wp
)
5057 struct paste_buffer
*pb
;
5059 if (c
!= NULL
&& c
->name
!= NULL
)
5060 log_debug("%s: c=%s", __func__
, c
->name
);
5062 log_debug("%s: c=none", __func__
);
5064 log_debug("%s: s=$%u", __func__
, s
->id
);
5066 log_debug("%s: s=none", __func__
);
5068 log_debug("%s: wl=%u", __func__
, wl
->idx
);
5070 log_debug("%s: wl=none", __func__
);
5072 log_debug("%s: wp=%%%u", __func__
, wp
->id
);
5074 log_debug("%s: wp=none", __func__
);
5076 if (c
!= NULL
&& s
!= NULL
&& c
->session
!= s
)
5077 log_debug("%s: session does not match", __func__
);
5080 ft
->type
= FORMAT_TYPE_PANE
;
5081 else if (wl
!= NULL
)
5082 ft
->type
= FORMAT_TYPE_WINDOW
;
5084 ft
->type
= FORMAT_TYPE_SESSION
;
5086 ft
->type
= FORMAT_TYPE_UNKNOWN
;
5088 if (s
== NULL
&& c
!= NULL
)
5090 if (wl
== NULL
&& s
!= NULL
)
5092 if (wp
== NULL
&& wl
!= NULL
)
5093 wp
= wl
->window
->active
;
5096 format_defaults_client(ft
, c
);
5098 format_defaults_session(ft
, s
);
5100 format_defaults_winlink(ft
, wl
);
5102 format_defaults_pane(ft
, wp
);
5104 pb
= paste_get_top(NULL
);
5106 format_defaults_paste_buffer(ft
, pb
);
5109 /* Set default format keys for a session. */
5111 format_defaults_session(struct format_tree
*ft
, struct session
*s
)
5116 /* Set default format keys for a client. */
5118 format_defaults_client(struct format_tree
*ft
, struct client
*c
)
5125 /* Set default format keys for a window. */
5127 format_defaults_window(struct format_tree
*ft
, struct window
*w
)
5132 /* Set default format keys for a winlink. */
5134 format_defaults_winlink(struct format_tree
*ft
, struct winlink
*wl
)
5137 format_defaults_window(ft
, wl
->window
);
5141 /* Set default format keys for a window pane. */
5143 format_defaults_pane(struct format_tree
*ft
, struct window_pane
*wp
)
5145 struct window_mode_entry
*wme
;
5148 format_defaults_window(ft
, wp
->window
);
5151 wme
= TAILQ_FIRST(&wp
->modes
);
5152 if (wme
!= NULL
&& wme
->mode
->formats
!= NULL
)
5153 wme
->mode
->formats(wme
, ft
);
5156 /* Set default format keys for paste buffer. */
5158 format_defaults_paste_buffer(struct format_tree
*ft
, struct paste_buffer
*pb
)
5163 /* Return word at given coordinates. Caller frees. */
5165 format_grid_word(struct grid
*gd
, u_int x
, u_int y
)
5167 const struct grid_line
*gl
;
5168 struct grid_cell gc
;
5170 struct utf8_data
*ud
= NULL
;
5176 ws
= options_get_string(global_s_options
, "word-separators");
5179 grid_get_cell(gd
, x
, y
, &gc
);
5180 if (gc
.flags
& GRID_FLAG_PADDING
)
5182 if (utf8_cstrhas(ws
, &gc
.data
) ||
5183 (gc
.data
.size
== 1 && *gc
.data
.data
== ' ')) {
5191 gl
= grid_peek_line(gd
, y
- 1);
5192 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5195 x
= grid_line_length(gd
, y
);
5203 end
= grid_line_length(gd
, y
);
5204 if (end
== 0 || x
== end
- 1) {
5205 if (y
== gd
->hsize
+ gd
->sy
- 1)
5207 gl
= grid_peek_line(gd
, y
);
5208 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5217 grid_get_cell(gd
, x
, y
, &gc
);
5218 if (gc
.flags
& GRID_FLAG_PADDING
)
5220 if (utf8_cstrhas(ws
, &gc
.data
) ||
5221 (gc
.data
.size
== 1 && *gc
.data
.data
== ' '))
5224 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5225 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5229 s
= utf8_tocstr(ud
);
5235 /* Return line at given coordinates. Caller frees. */
5237 format_grid_line(struct grid
*gd
, u_int y
)
5239 struct grid_cell gc
;
5240 struct utf8_data
*ud
= NULL
;
5245 for (x
= 0; x
< grid_line_length(gd
, y
); x
++) {
5246 grid_get_cell(gd
, x
, y
, &gc
);
5247 if (gc
.flags
& GRID_FLAG_PADDING
)
5250 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5251 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5255 s
= utf8_tocstr(ud
);
5261 /* Return hyperlink at given coordinates. Caller frees. */
5263 format_grid_hyperlink(struct grid
*gd
, u_int x
, u_int y
, struct screen
* s
)
5266 struct grid_cell gc
;
5268 grid_get_cell(gd
, x
, y
, &gc
);
5269 if (gc
.flags
& GRID_FLAG_PADDING
)
5271 if (s
->hyperlinks
== NULL
|| gc
.link
== 0)
5273 if (!hyperlinks_get(s
->hyperlinks
, gc
.link
, &uri
, NULL
, NULL
))
5275 return (xstrdup(uri
));