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_key_mode. */
1967 format_cb_pane_key_mode(struct format_tree
*ft
)
1969 if (ft
->wp
!= NULL
&& ft
->wp
->screen
!= NULL
) {
1970 switch (ft
->wp
->screen
->mode
& EXTENDED_KEY_MODES
) {
1971 case MODE_KEYS_EXTENDED
:
1972 return (xstrdup("Ext 1"));
1973 case MODE_KEYS_EXTENDED_2
:
1974 return (xstrdup("Ext 2"));
1976 return (xstrdup("VT10x"));
1982 /* Callback for pane_last. */
1984 format_cb_pane_last(struct format_tree
*ft
)
1986 if (ft
->wp
!= NULL
) {
1987 if (ft
->wp
== TAILQ_FIRST(&ft
->wp
->window
->last_panes
))
1988 return (xstrdup("1"));
1989 return (xstrdup("0"));
1994 /* Callback for pane_left. */
1996 format_cb_pane_left(struct format_tree
*ft
)
1999 return (format_printf("%u", ft
->wp
->xoff
));
2003 /* Callback for pane_marked. */
2005 format_cb_pane_marked(struct format_tree
*ft
)
2007 if (ft
->wp
!= NULL
) {
2008 if (server_check_marked() && marked_pane
.wp
== ft
->wp
)
2009 return (xstrdup("1"));
2010 return (xstrdup("0"));
2015 /* Callback for pane_marked_set. */
2017 format_cb_pane_marked_set(struct format_tree
*ft
)
2019 if (ft
->wp
!= NULL
) {
2020 if (server_check_marked())
2021 return (xstrdup("1"));
2022 return (xstrdup("0"));
2027 /* Callback for pane_mode. */
2029 format_cb_pane_mode(struct format_tree
*ft
)
2031 struct window_mode_entry
*wme
;
2033 if (ft
->wp
!= NULL
) {
2034 wme
= TAILQ_FIRST(&ft
->wp
->modes
);
2036 return (xstrdup(wme
->mode
->name
));
2042 /* Callback for pane_path. */
2044 format_cb_pane_path(struct format_tree
*ft
)
2046 if (ft
->wp
!= NULL
) {
2047 if (ft
->wp
->base
.path
== NULL
)
2048 return (xstrdup(""));
2049 return (xstrdup(ft
->wp
->base
.path
));
2054 /* Callback for pane_pid. */
2056 format_cb_pane_pid(struct format_tree
*ft
)
2059 return (format_printf("%ld", (long)ft
->wp
->pid
));
2063 /* Callback for pane_pipe. */
2065 format_cb_pane_pipe(struct format_tree
*ft
)
2067 if (ft
->wp
!= NULL
) {
2068 if (ft
->wp
->pipe_fd
!= -1)
2069 return (xstrdup("1"));
2070 return (xstrdup("0"));
2075 /* Callback for pane_right. */
2077 format_cb_pane_right(struct format_tree
*ft
)
2080 return (format_printf("%u", ft
->wp
->xoff
+ ft
->wp
->sx
- 1));
2084 /* Callback for pane_search_string. */
2086 format_cb_pane_search_string(struct format_tree
*ft
)
2088 if (ft
->wp
!= NULL
) {
2089 if (ft
->wp
->searchstr
== NULL
)
2090 return (xstrdup(""));
2091 return (xstrdup(ft
->wp
->searchstr
));
2096 /* Callback for pane_synchronized. */
2098 format_cb_pane_synchronized(struct format_tree
*ft
)
2100 if (ft
->wp
!= NULL
) {
2101 if (options_get_number(ft
->wp
->options
, "synchronize-panes"))
2102 return (xstrdup("1"));
2103 return (xstrdup("0"));
2108 /* Callback for pane_title. */
2110 format_cb_pane_title(struct format_tree
*ft
)
2113 return (xstrdup(ft
->wp
->base
.title
));
2117 /* Callback for pane_top. */
2119 format_cb_pane_top(struct format_tree
*ft
)
2122 return (format_printf("%u", ft
->wp
->yoff
));
2126 /* Callback for pane_tty. */
2128 format_cb_pane_tty(struct format_tree
*ft
)
2131 return (xstrdup(ft
->wp
->tty
));
2135 /* Callback for pane_width. */
2137 format_cb_pane_width(struct format_tree
*ft
)
2140 return (format_printf("%u", ft
->wp
->sx
));
2144 /* Callback for scroll_region_lower. */
2146 format_cb_scroll_region_lower(struct format_tree
*ft
)
2149 return (format_printf("%u", ft
->wp
->base
.rlower
));
2153 /* Callback for scroll_region_upper. */
2155 format_cb_scroll_region_upper(struct format_tree
*ft
)
2158 return (format_printf("%u", ft
->wp
->base
.rupper
));
2162 /* Callback for server_sessions. */
2164 format_cb_server_sessions(__unused
struct format_tree
*ft
)
2169 RB_FOREACH(s
, sessions
, &sessions
)
2171 return (format_printf("%u", n
));
2174 /* Callback for session_attached. */
2176 format_cb_session_attached(struct format_tree
*ft
)
2179 return (format_printf("%u", ft
->s
->attached
));
2183 /* Callback for session_format. */
2185 format_cb_session_format(struct format_tree
*ft
)
2187 if (ft
->type
== FORMAT_TYPE_SESSION
)
2188 return (xstrdup("1"));
2189 return (xstrdup("0"));
2192 /* Callback for session_group. */
2194 format_cb_session_group(struct format_tree
*ft
)
2196 struct session_group
*sg
;
2198 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2199 return (xstrdup(sg
->name
));
2203 /* Callback for session_group_attached. */
2205 format_cb_session_group_attached(struct format_tree
*ft
)
2207 struct session_group
*sg
;
2209 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2210 return (format_printf("%u", session_group_attached_count (sg
)));
2214 /* Callback for session_group_many_attached. */
2216 format_cb_session_group_many_attached(struct format_tree
*ft
)
2218 struct session_group
*sg
;
2220 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
) {
2221 if (session_group_attached_count (sg
) > 1)
2222 return (xstrdup("1"));
2223 return (xstrdup("0"));
2228 /* Callback for session_group_size. */
2230 format_cb_session_group_size(struct format_tree
*ft
)
2232 struct session_group
*sg
;
2234 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2235 return (format_printf("%u", session_group_count (sg
)));
2239 /* Callback for session_grouped. */
2241 format_cb_session_grouped(struct format_tree
*ft
)
2243 if (ft
->s
!= NULL
) {
2244 if (session_group_contains(ft
->s
) != NULL
)
2245 return (xstrdup("1"));
2246 return (xstrdup("0"));
2251 /* Callback for session_id. */
2253 format_cb_session_id(struct format_tree
*ft
)
2256 return (format_printf("$%u", ft
->s
->id
));
2260 /* Callback for session_many_attached. */
2262 format_cb_session_many_attached(struct format_tree
*ft
)
2264 if (ft
->s
!= NULL
) {
2265 if (ft
->s
->attached
> 1)
2266 return (xstrdup("1"));
2267 return (xstrdup("0"));
2272 /* Callback for session_marked. */
2274 format_cb_session_marked(struct format_tree
*ft
)
2276 if (ft
->s
!= NULL
) {
2277 if (server_check_marked() && marked_pane
.s
== ft
->s
)
2278 return (xstrdup("1"));
2279 return (xstrdup("0"));
2284 /* Callback for session_name. */
2286 format_cb_session_name(struct format_tree
*ft
)
2289 return (xstrdup(ft
->s
->name
));
2293 /* Callback for session_path. */
2295 format_cb_session_path(struct format_tree
*ft
)
2298 return (xstrdup(ft
->s
->cwd
));
2302 /* Callback for session_windows. */
2304 format_cb_session_windows(struct format_tree
*ft
)
2307 return (format_printf("%u", winlink_count(&ft
->s
->windows
)));
2311 /* Callback for socket_path. */
2313 format_cb_socket_path(__unused
struct format_tree
*ft
)
2315 return (xstrdup(socket_path
));
2318 /* Callback for version. */
2320 format_cb_version(__unused
struct format_tree
*ft
)
2322 return (xstrdup(getversion()));
2325 /* Callback for active_window_index. */
2327 format_cb_active_window_index(struct format_tree
*ft
)
2330 return (format_printf("%u", ft
->s
->curw
->idx
));
2334 /* Callback for last_window_index. */
2336 format_cb_last_window_index(struct format_tree
*ft
)
2340 if (ft
->s
!= NULL
) {
2341 wl
= RB_MAX(winlinks
, &ft
->s
->windows
);
2342 return (format_printf("%u", wl
->idx
));
2347 /* Callback for window_active. */
2349 format_cb_window_active(struct format_tree
*ft
)
2351 if (ft
->wl
!= NULL
) {
2352 if (ft
->wl
== ft
->wl
->session
->curw
)
2353 return (xstrdup("1"));
2354 return (xstrdup("0"));
2359 /* Callback for window_activity_flag. */
2361 format_cb_window_activity_flag(struct format_tree
*ft
)
2363 if (ft
->wl
!= NULL
) {
2364 if (ft
->wl
->flags
& WINLINK_ACTIVITY
)
2365 return (xstrdup("1"));
2366 return (xstrdup("0"));
2371 /* Callback for window_bell_flag. */
2373 format_cb_window_bell_flag(struct format_tree
*ft
)
2375 if (ft
->wl
!= NULL
) {
2376 if (ft
->wl
->flags
& WINLINK_BELL
)
2377 return (xstrdup("1"));
2378 return (xstrdup("0"));
2383 /* Callback for window_bigger. */
2385 format_cb_window_bigger(struct format_tree
*ft
)
2387 u_int ox
, oy
, sx
, sy
;
2389 if (ft
->c
!= NULL
) {
2390 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2391 return (xstrdup("1"));
2392 return (xstrdup("0"));
2397 /* Callback for window_cell_height. */
2399 format_cb_window_cell_height(struct format_tree
*ft
)
2402 return (format_printf("%u", ft
->w
->ypixel
));
2406 /* Callback for window_cell_width. */
2408 format_cb_window_cell_width(struct format_tree
*ft
)
2411 return (format_printf("%u", ft
->w
->xpixel
));
2415 /* Callback for window_end_flag. */
2417 format_cb_window_end_flag(struct format_tree
*ft
)
2419 if (ft
->wl
!= NULL
) {
2420 if (ft
->wl
== RB_MAX(winlinks
, &ft
->wl
->session
->windows
))
2421 return (xstrdup("1"));
2422 return (xstrdup("0"));
2427 /* Callback for window_flags. */
2429 format_cb_window_flags(struct format_tree
*ft
)
2432 return (xstrdup(window_printable_flags(ft
->wl
, 1)));
2436 /* Callback for window_format. */
2438 format_cb_window_format(struct format_tree
*ft
)
2440 if (ft
->type
== FORMAT_TYPE_WINDOW
)
2441 return (xstrdup("1"));
2442 return (xstrdup("0"));
2445 /* Callback for window_height. */
2447 format_cb_window_height(struct format_tree
*ft
)
2450 return (format_printf("%u", ft
->w
->sy
));
2454 /* Callback for window_id. */
2456 format_cb_window_id(struct format_tree
*ft
)
2459 return (format_printf("@%u", ft
->w
->id
));
2463 /* Callback for window_index. */
2465 format_cb_window_index(struct format_tree
*ft
)
2468 return (format_printf("%d", ft
->wl
->idx
));
2472 /* Callback for window_last_flag. */
2474 format_cb_window_last_flag(struct format_tree
*ft
)
2476 if (ft
->wl
!= NULL
) {
2477 if (ft
->wl
== TAILQ_FIRST(&ft
->wl
->session
->lastw
))
2478 return (xstrdup("1"));
2479 return (xstrdup("0"));
2484 /* Callback for window_linked. */
2486 format_cb_window_linked(struct format_tree
*ft
)
2488 if (ft
->wl
!= NULL
) {
2489 if (session_is_linked(ft
->wl
->session
, ft
->wl
->window
))
2490 return (xstrdup("1"));
2491 return (xstrdup("0"));
2496 /* Callback for window_linked_sessions. */
2498 format_cb_window_linked_sessions(struct format_tree
*ft
)
2501 return (format_printf("%u", ft
->wl
->window
->references
));
2505 /* Callback for window_marked_flag. */
2507 format_cb_window_marked_flag(struct format_tree
*ft
)
2509 if (ft
->wl
!= NULL
) {
2510 if (server_check_marked() && marked_pane
.wl
== ft
->wl
)
2511 return (xstrdup("1"));
2512 return (xstrdup("0"));
2517 /* Callback for window_name. */
2519 format_cb_window_name(struct format_tree
*ft
)
2522 return (format_printf("%s", ft
->w
->name
));
2526 /* Callback for window_offset_x. */
2528 format_cb_window_offset_x(struct format_tree
*ft
)
2530 u_int ox
, oy
, sx
, sy
;
2532 if (ft
->c
!= NULL
) {
2533 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2534 return (format_printf("%u", ox
));
2540 /* Callback for window_offset_y. */
2542 format_cb_window_offset_y(struct format_tree
*ft
)
2544 u_int ox
, oy
, sx
, sy
;
2546 if (ft
->c
!= NULL
) {
2547 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2548 return (format_printf("%u", oy
));
2554 /* Callback for window_panes. */
2556 format_cb_window_panes(struct format_tree
*ft
)
2559 return (format_printf("%u", window_count_panes(ft
->w
)));
2563 /* Callback for window_raw_flags. */
2565 format_cb_window_raw_flags(struct format_tree
*ft
)
2568 return (xstrdup(window_printable_flags(ft
->wl
, 0)));
2572 /* Callback for window_silence_flag. */
2574 format_cb_window_silence_flag(struct format_tree
*ft
)
2576 if (ft
->wl
!= NULL
) {
2577 if (ft
->wl
->flags
& WINLINK_SILENCE
)
2578 return (xstrdup("1"));
2579 return (xstrdup("0"));
2584 /* Callback for window_start_flag. */
2586 format_cb_window_start_flag(struct format_tree
*ft
)
2588 if (ft
->wl
!= NULL
) {
2589 if (ft
->wl
== RB_MIN(winlinks
, &ft
->wl
->session
->windows
))
2590 return (xstrdup("1"));
2591 return (xstrdup("0"));
2596 /* Callback for window_width. */
2598 format_cb_window_width(struct format_tree
*ft
)
2601 return (format_printf("%u", ft
->w
->sx
));
2605 /* Callback for window_zoomed_flag. */
2607 format_cb_window_zoomed_flag(struct format_tree
*ft
)
2609 if (ft
->w
!= NULL
) {
2610 if (ft
->w
->flags
& WINDOW_ZOOMED
)
2611 return (xstrdup("1"));
2612 return (xstrdup("0"));
2617 /* Callback for wrap_flag. */
2619 format_cb_wrap_flag(struct format_tree
*ft
)
2621 if (ft
->wp
!= NULL
) {
2622 if (ft
->wp
->base
.mode
& MODE_WRAP
)
2623 return (xstrdup("1"));
2624 return (xstrdup("0"));
2629 /* Callback for buffer_created. */
2631 format_cb_buffer_created(struct format_tree
*ft
)
2633 static struct timeval tv
;
2635 if (ft
->pb
!= NULL
) {
2637 tv
.tv_sec
= paste_buffer_created(ft
->pb
);
2643 /* Callback for client_activity. */
2645 format_cb_client_activity(struct format_tree
*ft
)
2648 return (&ft
->c
->activity_time
);
2652 /* Callback for client_created. */
2654 format_cb_client_created(struct format_tree
*ft
)
2657 return (&ft
->c
->creation_time
);
2661 /* Callback for session_activity. */
2663 format_cb_session_activity(struct format_tree
*ft
)
2666 return (&ft
->s
->activity_time
);
2670 /* Callback for session_created. */
2672 format_cb_session_created(struct format_tree
*ft
)
2675 return (&ft
->s
->creation_time
);
2679 /* Callback for session_last_attached. */
2681 format_cb_session_last_attached(struct format_tree
*ft
)
2684 return (&ft
->s
->last_attached_time
);
2688 /* Callback for start_time. */
2690 format_cb_start_time(__unused
struct format_tree
*ft
)
2692 return (&start_time
);
2695 /* Callback for window_activity. */
2697 format_cb_window_activity(struct format_tree
*ft
)
2700 return (&ft
->w
->activity_time
);
2704 /* Callback for buffer_mode_format, */
2706 format_cb_buffer_mode_format(__unused
struct format_tree
*ft
)
2708 return (xstrdup(window_buffer_mode
.default_format
));
2711 /* Callback for client_mode_format, */
2713 format_cb_client_mode_format(__unused
struct format_tree
*ft
)
2715 return (xstrdup(window_client_mode
.default_format
));
2718 /* Callback for tree_mode_format, */
2720 format_cb_tree_mode_format(__unused
struct format_tree
*ft
)
2722 return (xstrdup(window_tree_mode
.default_format
));
2725 /* Callback for uid. */
2727 format_cb_uid(__unused
struct format_tree
*ft
)
2729 return (format_printf("%ld", (long)getuid()));
2732 /* Callback for user. */
2734 format_cb_user(__unused
struct format_tree
*ft
)
2738 if ((pw
= getpwuid(getuid())) != NULL
)
2739 return (xstrdup(pw
->pw_name
));
2743 /* Format table type. */
2744 enum format_table_type
{
2745 FORMAT_TABLE_STRING
,
2749 /* Format table entry. */
2750 struct format_table_entry
{
2752 enum format_table_type type
;
2757 * Format table. Default format variables (that are almost always in the tree
2758 * and where the value is expanded by a callback in this file) are listed here.
2759 * Only variables which are added by the caller go into the tree.
2761 static const struct format_table_entry format_table
[] = {
2762 { "active_window_index", FORMAT_TABLE_STRING
,
2763 format_cb_active_window_index
2765 { "alternate_on", FORMAT_TABLE_STRING
,
2766 format_cb_alternate_on
2768 { "alternate_saved_x", FORMAT_TABLE_STRING
,
2769 format_cb_alternate_saved_x
2771 { "alternate_saved_y", FORMAT_TABLE_STRING
,
2772 format_cb_alternate_saved_y
2774 { "buffer_created", FORMAT_TABLE_TIME
,
2775 format_cb_buffer_created
2777 { "buffer_mode_format", FORMAT_TABLE_STRING
,
2778 format_cb_buffer_mode_format
2780 { "buffer_name", FORMAT_TABLE_STRING
,
2781 format_cb_buffer_name
2783 { "buffer_sample", FORMAT_TABLE_STRING
,
2784 format_cb_buffer_sample
2786 { "buffer_size", FORMAT_TABLE_STRING
,
2787 format_cb_buffer_size
2789 { "client_activity", FORMAT_TABLE_TIME
,
2790 format_cb_client_activity
2792 { "client_cell_height", FORMAT_TABLE_STRING
,
2793 format_cb_client_cell_height
2795 { "client_cell_width", FORMAT_TABLE_STRING
,
2796 format_cb_client_cell_width
2798 { "client_control_mode", FORMAT_TABLE_STRING
,
2799 format_cb_client_control_mode
2801 { "client_created", FORMAT_TABLE_TIME
,
2802 format_cb_client_created
2804 { "client_discarded", FORMAT_TABLE_STRING
,
2805 format_cb_client_discarded
2807 { "client_flags", FORMAT_TABLE_STRING
,
2808 format_cb_client_flags
2810 { "client_height", FORMAT_TABLE_STRING
,
2811 format_cb_client_height
2813 { "client_key_table", FORMAT_TABLE_STRING
,
2814 format_cb_client_key_table
2816 { "client_last_session", FORMAT_TABLE_STRING
,
2817 format_cb_client_last_session
2819 { "client_mode_format", FORMAT_TABLE_STRING
,
2820 format_cb_client_mode_format
2822 { "client_name", FORMAT_TABLE_STRING
,
2823 format_cb_client_name
2825 { "client_pid", FORMAT_TABLE_STRING
,
2826 format_cb_client_pid
2828 { "client_prefix", FORMAT_TABLE_STRING
,
2829 format_cb_client_prefix
2831 { "client_readonly", FORMAT_TABLE_STRING
,
2832 format_cb_client_readonly
2834 { "client_session", FORMAT_TABLE_STRING
,
2835 format_cb_client_session
2837 { "client_termfeatures", FORMAT_TABLE_STRING
,
2838 format_cb_client_termfeatures
2840 { "client_termname", FORMAT_TABLE_STRING
,
2841 format_cb_client_termname
2843 { "client_termtype", FORMAT_TABLE_STRING
,
2844 format_cb_client_termtype
2846 { "client_tty", FORMAT_TABLE_STRING
,
2847 format_cb_client_tty
2849 { "client_uid", FORMAT_TABLE_STRING
,
2850 format_cb_client_uid
2852 { "client_user", FORMAT_TABLE_STRING
,
2853 format_cb_client_user
2855 { "client_utf8", FORMAT_TABLE_STRING
,
2856 format_cb_client_utf8
2858 { "client_width", FORMAT_TABLE_STRING
,
2859 format_cb_client_width
2861 { "client_written", FORMAT_TABLE_STRING
,
2862 format_cb_client_written
2864 { "config_files", FORMAT_TABLE_STRING
,
2865 format_cb_config_files
2867 { "cursor_character", FORMAT_TABLE_STRING
,
2868 format_cb_cursor_character
2870 { "cursor_flag", FORMAT_TABLE_STRING
,
2871 format_cb_cursor_flag
2873 { "cursor_x", FORMAT_TABLE_STRING
,
2876 { "cursor_y", FORMAT_TABLE_STRING
,
2879 { "history_all_bytes", FORMAT_TABLE_STRING
,
2880 format_cb_history_all_bytes
2882 { "history_bytes", FORMAT_TABLE_STRING
,
2883 format_cb_history_bytes
2885 { "history_limit", FORMAT_TABLE_STRING
,
2886 format_cb_history_limit
2888 { "history_size", FORMAT_TABLE_STRING
,
2889 format_cb_history_size
2891 { "host", FORMAT_TABLE_STRING
,
2894 { "host_short", FORMAT_TABLE_STRING
,
2895 format_cb_host_short
2897 { "insert_flag", FORMAT_TABLE_STRING
,
2898 format_cb_insert_flag
2900 { "keypad_cursor_flag", FORMAT_TABLE_STRING
,
2901 format_cb_keypad_cursor_flag
2903 { "keypad_flag", FORMAT_TABLE_STRING
,
2904 format_cb_keypad_flag
2906 { "last_window_index", FORMAT_TABLE_STRING
,
2907 format_cb_last_window_index
2909 { "mouse_all_flag", FORMAT_TABLE_STRING
,
2910 format_cb_mouse_all_flag
2912 { "mouse_any_flag", FORMAT_TABLE_STRING
,
2913 format_cb_mouse_any_flag
2915 { "mouse_button_flag", FORMAT_TABLE_STRING
,
2916 format_cb_mouse_button_flag
2918 { "mouse_hyperlink", FORMAT_TABLE_STRING
,
2919 format_cb_mouse_hyperlink
2921 { "mouse_line", FORMAT_TABLE_STRING
,
2922 format_cb_mouse_line
2924 { "mouse_pane", FORMAT_TABLE_STRING
,
2925 format_cb_mouse_pane
2927 { "mouse_sgr_flag", FORMAT_TABLE_STRING
,
2928 format_cb_mouse_sgr_flag
2930 { "mouse_standard_flag", FORMAT_TABLE_STRING
,
2931 format_cb_mouse_standard_flag
2933 { "mouse_status_line", FORMAT_TABLE_STRING
,
2934 format_cb_mouse_status_line
2936 { "mouse_status_range", FORMAT_TABLE_STRING
,
2937 format_cb_mouse_status_range
2939 { "mouse_utf8_flag", FORMAT_TABLE_STRING
,
2940 format_cb_mouse_utf8_flag
2942 { "mouse_word", FORMAT_TABLE_STRING
,
2943 format_cb_mouse_word
2945 { "mouse_x", FORMAT_TABLE_STRING
,
2948 { "mouse_y", FORMAT_TABLE_STRING
,
2951 { "next_session_id", FORMAT_TABLE_STRING
,
2952 format_cb_next_session_id
2954 { "origin_flag", FORMAT_TABLE_STRING
,
2955 format_cb_origin_flag
2957 { "pane_active", FORMAT_TABLE_STRING
,
2958 format_cb_pane_active
2960 { "pane_at_bottom", FORMAT_TABLE_STRING
,
2961 format_cb_pane_at_bottom
2963 { "pane_at_left", FORMAT_TABLE_STRING
,
2964 format_cb_pane_at_left
2966 { "pane_at_right", FORMAT_TABLE_STRING
,
2967 format_cb_pane_at_right
2969 { "pane_at_top", FORMAT_TABLE_STRING
,
2970 format_cb_pane_at_top
2972 { "pane_bg", FORMAT_TABLE_STRING
,
2975 { "pane_bottom", FORMAT_TABLE_STRING
,
2976 format_cb_pane_bottom
2978 { "pane_current_command", FORMAT_TABLE_STRING
,
2979 format_cb_current_command
2981 { "pane_current_path", FORMAT_TABLE_STRING
,
2982 format_cb_current_path
2984 { "pane_dead", FORMAT_TABLE_STRING
,
2987 { "pane_dead_signal", FORMAT_TABLE_STRING
,
2988 format_cb_pane_dead_signal
2990 { "pane_dead_status", FORMAT_TABLE_STRING
,
2991 format_cb_pane_dead_status
2993 { "pane_dead_time", FORMAT_TABLE_TIME
,
2994 format_cb_pane_dead_time
2996 { "pane_fg", FORMAT_TABLE_STRING
,
2999 { "pane_format", FORMAT_TABLE_STRING
,
3000 format_cb_pane_format
3002 { "pane_height", FORMAT_TABLE_STRING
,
3003 format_cb_pane_height
3005 { "pane_id", FORMAT_TABLE_STRING
,
3008 { "pane_in_mode", FORMAT_TABLE_STRING
,
3009 format_cb_pane_in_mode
3011 { "pane_index", FORMAT_TABLE_STRING
,
3012 format_cb_pane_index
3014 { "pane_input_off", FORMAT_TABLE_STRING
,
3015 format_cb_pane_input_off
3017 { "pane_key_mode", FORMAT_TABLE_STRING
,
3018 format_cb_pane_key_mode
3020 { "pane_last", FORMAT_TABLE_STRING
,
3023 { "pane_left", FORMAT_TABLE_STRING
,
3026 { "pane_marked", FORMAT_TABLE_STRING
,
3027 format_cb_pane_marked
3029 { "pane_marked_set", FORMAT_TABLE_STRING
,
3030 format_cb_pane_marked_set
3032 { "pane_mode", FORMAT_TABLE_STRING
,
3035 { "pane_path", FORMAT_TABLE_STRING
,
3038 { "pane_pid", FORMAT_TABLE_STRING
,
3041 { "pane_pipe", FORMAT_TABLE_STRING
,
3044 { "pane_right", FORMAT_TABLE_STRING
,
3045 format_cb_pane_right
3047 { "pane_search_string", FORMAT_TABLE_STRING
,
3048 format_cb_pane_search_string
3050 { "pane_start_command", FORMAT_TABLE_STRING
,
3051 format_cb_start_command
3053 { "pane_start_path", FORMAT_TABLE_STRING
,
3054 format_cb_start_path
3056 { "pane_synchronized", FORMAT_TABLE_STRING
,
3057 format_cb_pane_synchronized
3059 { "pane_tabs", FORMAT_TABLE_STRING
,
3062 { "pane_title", FORMAT_TABLE_STRING
,
3063 format_cb_pane_title
3065 { "pane_top", FORMAT_TABLE_STRING
,
3068 { "pane_tty", FORMAT_TABLE_STRING
,
3071 { "pane_unseen_changes", FORMAT_TABLE_STRING
,
3072 format_cb_pane_unseen_changes
3074 { "pane_width", FORMAT_TABLE_STRING
,
3075 format_cb_pane_width
3077 { "pid", FORMAT_TABLE_STRING
,
3080 { "scroll_region_lower", FORMAT_TABLE_STRING
,
3081 format_cb_scroll_region_lower
3083 { "scroll_region_upper", FORMAT_TABLE_STRING
,
3084 format_cb_scroll_region_upper
3086 { "server_sessions", FORMAT_TABLE_STRING
,
3087 format_cb_server_sessions
3089 { "session_activity", FORMAT_TABLE_TIME
,
3090 format_cb_session_activity
3092 { "session_alerts", FORMAT_TABLE_STRING
,
3093 format_cb_session_alerts
3095 { "session_attached", FORMAT_TABLE_STRING
,
3096 format_cb_session_attached
3098 { "session_attached_list", FORMAT_TABLE_STRING
,
3099 format_cb_session_attached_list
3101 { "session_created", FORMAT_TABLE_TIME
,
3102 format_cb_session_created
3104 { "session_format", FORMAT_TABLE_STRING
,
3105 format_cb_session_format
3107 { "session_group", FORMAT_TABLE_STRING
,
3108 format_cb_session_group
3110 { "session_group_attached", FORMAT_TABLE_STRING
,
3111 format_cb_session_group_attached
3113 { "session_group_attached_list", FORMAT_TABLE_STRING
,
3114 format_cb_session_group_attached_list
3116 { "session_group_list", FORMAT_TABLE_STRING
,
3117 format_cb_session_group_list
3119 { "session_group_many_attached", FORMAT_TABLE_STRING
,
3120 format_cb_session_group_many_attached
3122 { "session_group_size", FORMAT_TABLE_STRING
,
3123 format_cb_session_group_size
3125 { "session_grouped", FORMAT_TABLE_STRING
,
3126 format_cb_session_grouped
3128 { "session_id", FORMAT_TABLE_STRING
,
3129 format_cb_session_id
3131 { "session_last_attached", FORMAT_TABLE_TIME
,
3132 format_cb_session_last_attached
3134 { "session_many_attached", FORMAT_TABLE_STRING
,
3135 format_cb_session_many_attached
3137 { "session_marked", FORMAT_TABLE_STRING
,
3138 format_cb_session_marked
,
3140 { "session_name", FORMAT_TABLE_STRING
,
3141 format_cb_session_name
3143 { "session_path", FORMAT_TABLE_STRING
,
3144 format_cb_session_path
3146 { "session_stack", FORMAT_TABLE_STRING
,
3147 format_cb_session_stack
3149 { "session_windows", FORMAT_TABLE_STRING
,
3150 format_cb_session_windows
3152 { "socket_path", FORMAT_TABLE_STRING
,
3153 format_cb_socket_path
3155 { "start_time", FORMAT_TABLE_TIME
,
3156 format_cb_start_time
3158 { "tree_mode_format", FORMAT_TABLE_STRING
,
3159 format_cb_tree_mode_format
3161 { "uid", FORMAT_TABLE_STRING
,
3164 { "user", FORMAT_TABLE_STRING
,
3167 { "version", FORMAT_TABLE_STRING
,
3170 { "window_active", FORMAT_TABLE_STRING
,
3171 format_cb_window_active
3173 { "window_active_clients", FORMAT_TABLE_STRING
,
3174 format_cb_window_active_clients
3176 { "window_active_clients_list", FORMAT_TABLE_STRING
,
3177 format_cb_window_active_clients_list
3179 { "window_active_sessions", FORMAT_TABLE_STRING
,
3180 format_cb_window_active_sessions
3182 { "window_active_sessions_list", FORMAT_TABLE_STRING
,
3183 format_cb_window_active_sessions_list
3185 { "window_activity", FORMAT_TABLE_TIME
,
3186 format_cb_window_activity
3188 { "window_activity_flag", FORMAT_TABLE_STRING
,
3189 format_cb_window_activity_flag
3191 { "window_bell_flag", FORMAT_TABLE_STRING
,
3192 format_cb_window_bell_flag
3194 { "window_bigger", FORMAT_TABLE_STRING
,
3195 format_cb_window_bigger
3197 { "window_cell_height", FORMAT_TABLE_STRING
,
3198 format_cb_window_cell_height
3200 { "window_cell_width", FORMAT_TABLE_STRING
,
3201 format_cb_window_cell_width
3203 { "window_end_flag", FORMAT_TABLE_STRING
,
3204 format_cb_window_end_flag
3206 { "window_flags", FORMAT_TABLE_STRING
,
3207 format_cb_window_flags
3209 { "window_format", FORMAT_TABLE_STRING
,
3210 format_cb_window_format
3212 { "window_height", FORMAT_TABLE_STRING
,
3213 format_cb_window_height
3215 { "window_id", FORMAT_TABLE_STRING
,
3218 { "window_index", FORMAT_TABLE_STRING
,
3219 format_cb_window_index
3221 { "window_last_flag", FORMAT_TABLE_STRING
,
3222 format_cb_window_last_flag
3224 { "window_layout", FORMAT_TABLE_STRING
,
3225 format_cb_window_layout
3227 { "window_linked", FORMAT_TABLE_STRING
,
3228 format_cb_window_linked
3230 { "window_linked_sessions", FORMAT_TABLE_STRING
,
3231 format_cb_window_linked_sessions
3233 { "window_linked_sessions_list", FORMAT_TABLE_STRING
,
3234 format_cb_window_linked_sessions_list
3236 { "window_marked_flag", FORMAT_TABLE_STRING
,
3237 format_cb_window_marked_flag
3239 { "window_name", FORMAT_TABLE_STRING
,
3240 format_cb_window_name
3242 { "window_offset_x", FORMAT_TABLE_STRING
,
3243 format_cb_window_offset_x
3245 { "window_offset_y", FORMAT_TABLE_STRING
,
3246 format_cb_window_offset_y
3248 { "window_panes", FORMAT_TABLE_STRING
,
3249 format_cb_window_panes
3251 { "window_raw_flags", FORMAT_TABLE_STRING
,
3252 format_cb_window_raw_flags
3254 { "window_silence_flag", FORMAT_TABLE_STRING
,
3255 format_cb_window_silence_flag
3257 { "window_stack_index", FORMAT_TABLE_STRING
,
3258 format_cb_window_stack_index
3260 { "window_start_flag", FORMAT_TABLE_STRING
,
3261 format_cb_window_start_flag
3263 { "window_visible_layout", FORMAT_TABLE_STRING
,
3264 format_cb_window_visible_layout
3266 { "window_width", FORMAT_TABLE_STRING
,
3267 format_cb_window_width
3269 { "window_zoomed_flag", FORMAT_TABLE_STRING
,
3270 format_cb_window_zoomed_flag
3272 { "wrap_flag", FORMAT_TABLE_STRING
,
3277 /* Compare format table entries. */
3279 format_table_compare(const void *key0
, const void *entry0
)
3281 const char *key
= key0
;
3282 const struct format_table_entry
*entry
= entry0
;
3284 return (strcmp(key
, entry
->key
));
3287 /* Get a format callback. */
3288 static struct format_table_entry
*
3289 format_table_get(const char *key
)
3291 return (bsearch(key
, format_table
, nitems(format_table
),
3292 sizeof *format_table
, format_table_compare
));
3295 /* Merge one format tree into another. */
3297 format_merge(struct format_tree
*ft
, struct format_tree
*from
)
3299 struct format_entry
*fe
;
3301 RB_FOREACH(fe
, format_entry_tree
, &from
->tree
) {
3302 if (fe
->value
!= NULL
)
3303 format_add(ft
, fe
->key
, "%s", fe
->value
);
3307 /* Get format pane. */
3308 struct window_pane
*
3309 format_get_pane(struct format_tree
*ft
)
3314 /* Add item bits to tree. */
3316 format_create_add_item(struct format_tree
*ft
, struct cmdq_item
*item
)
3318 struct key_event
*event
= cmdq_get_event(item
);
3319 struct mouse_event
*m
= &event
->m
;
3321 cmdq_merge_formats(item
, ft
);
3322 memcpy(&ft
->m
, m
, sizeof ft
->m
);
3325 /* Create a new tree. */
3326 struct format_tree
*
3327 format_create(struct client
*c
, struct cmdq_item
*item
, int tag
, int flags
)
3329 struct format_tree
*ft
;
3331 ft
= xcalloc(1, sizeof *ft
);
3336 ft
->client
->references
++;
3344 format_create_add_item(ft
, item
);
3351 format_free(struct format_tree
*ft
)
3353 struct format_entry
*fe
, *fe1
;
3355 RB_FOREACH_SAFE(fe
, format_entry_tree
, &ft
->tree
, fe1
) {
3356 RB_REMOVE(format_entry_tree
, &ft
->tree
, fe
);
3362 if (ft
->client
!= NULL
)
3363 server_client_unref(ft
->client
);
3367 /* Log each format. */
3369 format_log_debug_cb(const char *key
, const char *value
, void *arg
)
3371 const char *prefix
= arg
;
3373 log_debug("%s: %s=%s", prefix
, key
, value
);
3376 /* Log a format tree. */
3378 format_log_debug(struct format_tree
*ft
, const char *prefix
)
3380 format_each(ft
, format_log_debug_cb
, (void *)prefix
);
3383 /* Walk each format. */
3385 format_each(struct format_tree
*ft
, void (*cb
)(const char *, const char *,
3388 const struct format_table_entry
*fte
;
3389 struct format_entry
*fe
;
3395 for (i
= 0; i
< nitems(format_table
); i
++) {
3396 fte
= &format_table
[i
];
3398 value
= fte
->cb(ft
);
3401 if (fte
->type
== FORMAT_TABLE_TIME
) {
3403 xsnprintf(s
, sizeof s
, "%lld", (long long)tv
->tv_sec
);
3404 cb(fte
->key
, s
, arg
);
3406 cb(fte
->key
, value
, arg
);
3410 RB_FOREACH(fe
, format_entry_tree
, &ft
->tree
) {
3411 if (fe
->time
!= 0) {
3412 xsnprintf(s
, sizeof s
, "%lld", (long long)fe
->time
);
3413 cb(fe
->key
, s
, arg
);
3415 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3416 fe
->value
= fe
->cb(ft
);
3417 if (fe
->value
== NULL
)
3418 fe
->value
= xstrdup("");
3420 cb(fe
->key
, fe
->value
, arg
);
3425 /* Add a key-value pair. */
3427 format_add(struct format_tree
*ft
, const char *key
, const char *fmt
, ...)
3429 struct format_entry
*fe
;
3430 struct format_entry
*fe_now
;
3433 fe
= xmalloc(sizeof *fe
);
3434 fe
->key
= xstrdup(key
);
3436 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3437 if (fe_now
!= NULL
) {
3440 free(fe_now
->value
);
3448 xvasprintf(&fe
->value
, fmt
, ap
);
3452 /* Add a key and time. */
3454 format_add_tv(struct format_tree
*ft
, const char *key
, struct timeval
*tv
)
3456 struct format_entry
*fe
, *fe_now
;
3458 fe
= xmalloc(sizeof *fe
);
3459 fe
->key
= xstrdup(key
);
3461 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3462 if (fe_now
!= NULL
) {
3465 free(fe_now
->value
);
3470 fe
->time
= tv
->tv_sec
;
3475 /* Add a key and function. */
3477 format_add_cb(struct format_tree
*ft
, const char *key
, format_cb cb
)
3479 struct format_entry
*fe
;
3480 struct format_entry
*fe_now
;
3482 fe
= xmalloc(sizeof *fe
);
3483 fe
->key
= xstrdup(key
);
3485 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3486 if (fe_now
!= NULL
) {
3489 free(fe_now
->value
);
3499 /* Quote shell special characters in string. */
3501 format_quote_shell(const char *s
)
3506 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3507 for (cp
= s
; *cp
!= '\0'; cp
++) {
3508 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp
) != NULL
)
3516 /* Quote #s in string. */
3518 format_quote_style(const char *s
)
3523 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3524 for (cp
= s
; *cp
!= '\0'; cp
++) {
3533 /* Make a prettier time. */
3535 format_pretty_time(time_t t
, int seconds
)
3537 struct tm now_tm
, tm
;
3546 localtime_r(&now
, &now_tm
);
3547 localtime_r(&t
, &tm
);
3549 /* Last 24 hours. */
3550 if (age
< 24 * 3600) {
3552 strftime(s
, sizeof s
, "%H:%M:%S", &tm
);
3554 strftime(s
, sizeof s
, "%H:%M", &tm
);
3555 return (xstrdup(s
));
3558 /* This month or last 28 days. */
3559 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
== now_tm
.tm_mon
) ||
3560 age
< 28 * 24 * 3600) {
3561 strftime(s
, sizeof s
, "%a%d", &tm
);
3562 return (xstrdup(s
));
3565 /* Last 12 months. */
3566 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
< now_tm
.tm_mon
) ||
3567 (tm
.tm_year
== now_tm
.tm_year
- 1 && tm
.tm_mon
> now_tm
.tm_mon
)) {
3568 strftime(s
, sizeof s
, "%d%b", &tm
);
3569 return (xstrdup(s
));
3572 /* Older than that. */
3573 strftime(s
, sizeof s
, "%h%y", &tm
);
3574 return (xstrdup(s
));
3577 /* Find a format entry. */
3579 format_find(struct format_tree
*ft
, const char *key
, int modifiers
,
3580 const char *time_format
)
3582 struct format_table_entry
*fte
;
3584 struct format_entry
*fe
, fe_find
;
3585 struct environ_entry
*envent
;
3586 struct options_entry
*o
;
3588 char *found
= NULL
, *saved
, s
[512];
3593 o
= options_parse_get(global_options
, key
, &idx
, 0);
3594 if (o
== NULL
&& ft
->wp
!= NULL
)
3595 o
= options_parse_get(ft
->wp
->options
, key
, &idx
, 0);
3596 if (o
== NULL
&& ft
->w
!= NULL
)
3597 o
= options_parse_get(ft
->w
->options
, key
, &idx
, 0);
3599 o
= options_parse_get(global_w_options
, key
, &idx
, 0);
3600 if (o
== NULL
&& ft
->s
!= NULL
)
3601 o
= options_parse_get(ft
->s
->options
, key
, &idx
, 0);
3603 o
= options_parse_get(global_s_options
, key
, &idx
, 0);
3605 found
= options_to_string(o
, idx
, 1);
3609 fte
= format_table_get(key
);
3611 value
= fte
->cb(ft
);
3612 if (fte
->type
== FORMAT_TABLE_TIME
&& value
!= NULL
)
3613 t
= ((struct timeval
*)value
)->tv_sec
;
3618 fe_find
.key
= (char *)key
;
3619 fe
= RB_FIND(format_entry_tree
, &ft
->tree
, &fe_find
);
3621 if (fe
->time
!= 0) {
3625 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3626 fe
->value
= fe
->cb(ft
);
3627 if (fe
->value
== NULL
)
3628 fe
->value
= xstrdup("");
3630 found
= xstrdup(fe
->value
);
3634 if (~modifiers
& FORMAT_TIMESTRING
) {
3637 envent
= environ_find(ft
->s
->environ
, key
);
3639 envent
= environ_find(global_environ
, key
);
3640 if (envent
!= NULL
&& envent
->value
!= NULL
) {
3641 found
= xstrdup(envent
->value
);
3649 if (modifiers
& FORMAT_TIMESTRING
) {
3650 if (t
== 0 && found
!= NULL
) {
3651 t
= strtonum(found
, 0, INT64_MAX
, &errstr
);
3658 if (modifiers
& FORMAT_PRETTY
)
3659 found
= format_pretty_time(t
, 0);
3661 if (time_format
!= NULL
) {
3662 localtime_r(&t
, &tm
);
3663 strftime(s
, sizeof s
, time_format
, &tm
);
3666 s
[strcspn(s
, "\n")] = '\0';
3674 xasprintf(&found
, "%lld", (long long)t
);
3675 else if (found
== NULL
)
3677 if (modifiers
& FORMAT_BASENAME
) {
3679 found
= xstrdup(basename(saved
));
3682 if (modifiers
& FORMAT_DIRNAME
) {
3684 found
= xstrdup(dirname(saved
));
3687 if (modifiers
& FORMAT_QUOTE_SHELL
) {
3689 found
= format_quote_shell(saved
);
3692 if (modifiers
& FORMAT_QUOTE_STYLE
) {
3694 found
= format_quote_style(saved
);
3700 /* Unescape escaped characters. */
3702 format_unescape(const char *s
)
3707 cp
= out
= xmalloc(strlen(s
) + 1);
3708 for (; *s
!= '\0'; s
++) {
3709 if (*s
== '#' && s
[1] == '{')
3711 if (brackets
== 0 &&
3713 strchr(",#{}:", s
[1]) != NULL
) {
3725 /* Remove escaped characters. */
3727 format_strip(const char *s
)
3732 cp
= out
= xmalloc(strlen(s
) + 1);
3733 for (; *s
!= '\0'; s
++) {
3734 if (*s
== '#' && s
[1] == '{')
3736 if (*s
== '#' && strchr(",#{}:", s
[1]) != NULL
) {
3749 /* Skip until end. */
3751 format_skip(const char *s
, const char *end
)
3755 for (; *s
!= '\0'; s
++) {
3756 if (*s
== '#' && s
[1] == '{')
3760 strchr(",#{}:", s
[1]) != NULL
) {
3766 if (strchr(end
, *s
) != NULL
&& brackets
== 0)
3774 /* Return left and right alternatives separated by commas. */
3776 format_choose(struct format_expand_state
*es
, const char *s
, char **left
,
3777 char **right
, int expand
)
3780 char *left0
, *right0
;
3782 cp
= format_skip(s
, ",");
3785 left0
= xstrndup(s
, cp
- s
);
3786 right0
= xstrdup(cp
+ 1);
3789 *left
= format_expand1(es
, left0
);
3791 *right
= format_expand1(es
, right0
);
3802 format_true(const char *s
)
3804 if (s
!= NULL
&& *s
!= '\0' && (s
[0] != '0' || s
[1] != '\0'))
3809 /* Check if modifier end. */
3811 format_is_end(char c
)
3813 return (c
== ';' || c
== ':');
3816 /* Add to modifier list. */
3818 format_add_modifier(struct format_modifier
**list
, u_int
*count
,
3819 const char *c
, size_t n
, char **argv
, int argc
)
3821 struct format_modifier
*fm
;
3823 *list
= xreallocarray(*list
, (*count
) + 1, sizeof **list
);
3824 fm
= &(*list
)[(*count
)++];
3826 memcpy(fm
->modifier
, c
, n
);
3827 fm
->modifier
[n
] = '\0';
3834 /* Free modifier list. */
3836 format_free_modifiers(struct format_modifier
*list
, u_int count
)
3840 for (i
= 0; i
< count
; i
++)
3841 cmd_free_argv(list
[i
].argc
, list
[i
].argv
);
3845 /* Build modifier list. */
3846 static struct format_modifier
*
3847 format_build_modifiers(struct format_expand_state
*es
, const char **s
,
3850 const char *cp
= *s
, *end
;
3851 struct format_modifier
*list
= NULL
;
3852 char c
, last
[] = "X;:", **argv
, *value
;
3856 * Modifiers are a ; separated list of the forms:
3857 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3868 while (*cp
!= '\0' && *cp
!= ':') {
3869 /* Skip any separator character. */
3873 /* Check single character modifiers with no arguments. */
3874 if (strchr("labcdnwETSWPL<>", cp
[0]) != NULL
&&
3875 format_is_end(cp
[1])) {
3876 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3881 /* Then try double character with no arguments. */
3882 if ((memcmp("||", cp
, 2) == 0 ||
3883 memcmp("&&", cp
, 2) == 0 ||
3884 memcmp("!=", cp
, 2) == 0 ||
3885 memcmp("==", cp
, 2) == 0 ||
3886 memcmp("<=", cp
, 2) == 0 ||
3887 memcmp(">=", cp
, 2) == 0) &&
3888 format_is_end(cp
[2])) {
3889 format_add_modifier(&list
, count
, cp
, 2, NULL
, 0);
3894 /* Now try single character with arguments. */
3895 if (strchr("mCNst=peq", cp
[0]) == NULL
)
3899 /* No arguments provided. */
3900 if (format_is_end(cp
[1])) {
3901 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3908 /* Single argument with no wrapper character. */
3909 if (!ispunct((u_char
)cp
[1]) || cp
[1] == '-') {
3910 end
= format_skip(cp
+ 1, ":;");
3914 argv
= xcalloc(1, sizeof *argv
);
3915 value
= xstrndup(cp
+ 1, end
- (cp
+ 1));
3916 argv
[0] = format_expand1(es
, value
);
3920 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3925 /* Multiple arguments with a wrapper character. */
3929 if (cp
[0] == last
[0] && format_is_end(cp
[1])) {
3933 end
= format_skip(cp
+ 1, last
);
3938 argv
= xreallocarray(argv
, argc
+ 1, sizeof *argv
);
3939 value
= xstrndup(cp
, end
- cp
);
3940 argv
[argc
++] = format_expand1(es
, value
);
3944 } while (!format_is_end(cp
[0]));
3945 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3948 format_free_modifiers(list
, *count
);
3956 /* Match against an fnmatch(3) pattern or regular expression. */
3958 format_match(struct format_modifier
*fm
, const char *pattern
, const char *text
)
3966 if (strchr(s
, 'r') == NULL
) {
3967 if (strchr(s
, 'i') != NULL
)
3968 flags
|= FNM_CASEFOLD
;
3969 if (fnmatch(pattern
, text
, flags
) != 0)
3970 return (xstrdup("0"));
3972 flags
= REG_EXTENDED
|REG_NOSUB
;
3973 if (strchr(s
, 'i') != NULL
)
3975 if (regcomp(&r
, pattern
, flags
) != 0)
3976 return (xstrdup("0"));
3977 if (regexec(&r
, text
, 0, NULL
, 0) != 0) {
3979 return (xstrdup("0"));
3983 return (xstrdup("1"));
3986 /* Perform substitution in string. */
3988 format_sub(struct format_modifier
*fm
, const char *text
, const char *pattern
,
3992 int flags
= REG_EXTENDED
;
3994 if (fm
->argc
>= 3 && strchr(fm
->argv
[2], 'i') != NULL
)
3996 value
= regsub(pattern
, with
, text
, flags
);
3998 return (xstrdup(text
));
4002 /* Search inside pane. */
4004 format_search(struct format_modifier
*fm
, struct window_pane
*wp
, const char *s
)
4006 int ignore
= 0, regex
= 0;
4009 if (fm
->argc
>= 1) {
4010 if (strchr(fm
->argv
[0], 'i') != NULL
)
4012 if (strchr(fm
->argv
[0], 'r') != NULL
)
4015 xasprintf(&value
, "%u", window_pane_search(wp
, s
, regex
, ignore
));
4019 /* Does session name exist? */
4021 format_session_name(struct format_expand_state
*es
, const char *fmt
)
4026 name
= format_expand1(es
, fmt
);
4027 RB_FOREACH(s
, sessions
, &sessions
) {
4028 if (strcmp(s
->name
, name
) == 0) {
4030 return (xstrdup("1"));
4034 return (xstrdup("0"));
4037 /* Loop over sessions. */
4039 format_loop_sessions(struct format_expand_state
*es
, const char *fmt
)
4041 struct format_tree
*ft
= es
->ft
;
4042 struct client
*c
= ft
->client
;
4043 struct cmdq_item
*item
= ft
->item
;
4044 struct format_tree
*nft
;
4045 struct format_expand_state next
;
4046 char *expanded
, *value
;
4050 value
= xcalloc(1, 1);
4053 RB_FOREACH(s
, sessions
, &sessions
) {
4054 format_log(es
, "session loop: $%u", s
->id
);
4055 nft
= format_create(c
, item
, FORMAT_NONE
, ft
->flags
);
4056 format_defaults(nft
, ft
->c
, s
, NULL
, NULL
);
4057 format_copy_state(&next
, es
, 0);
4059 expanded
= format_expand1(&next
, fmt
);
4060 format_free(next
.ft
);
4062 valuelen
+= strlen(expanded
);
4063 value
= xrealloc(value
, valuelen
);
4065 strlcat(value
, expanded
, valuelen
);
4072 /* Does window name exist? */
4074 format_window_name(struct format_expand_state
*es
, const char *fmt
)
4076 struct format_tree
*ft
= es
->ft
;
4080 if (ft
->s
== NULL
) {
4081 format_log(es
, "window name but no session");
4085 name
= format_expand1(es
, fmt
);
4086 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
4087 if (strcmp(wl
->window
->name
, name
) == 0) {
4089 return (xstrdup("1"));
4093 return (xstrdup("0"));
4096 /* Loop over windows. */
4098 format_loop_windows(struct format_expand_state
*es
, const char *fmt
)
4100 struct format_tree
*ft
= es
->ft
;
4101 struct client
*c
= ft
->client
;
4102 struct cmdq_item
*item
= ft
->item
;
4103 struct format_tree
*nft
;
4104 struct format_expand_state next
;
4105 char *all
, *active
, *use
, *expanded
, *value
;
4110 if (ft
->s
== NULL
) {
4111 format_log(es
, "window loop but no session");
4115 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4120 value
= xcalloc(1, 1);
4123 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
4125 format_log(es
, "window loop: %u @%u", wl
->idx
, w
->id
);
4126 if (active
!= NULL
&& wl
== ft
->s
->curw
)
4130 nft
= format_create(c
, item
, FORMAT_WINDOW
|w
->id
, ft
->flags
);
4131 format_defaults(nft
, ft
->c
, ft
->s
, wl
, NULL
);
4132 format_copy_state(&next
, es
, 0);
4134 expanded
= format_expand1(&next
, use
);
4137 valuelen
+= strlen(expanded
);
4138 value
= xrealloc(value
, valuelen
);
4140 strlcat(value
, expanded
, valuelen
);
4150 /* Loop over panes. */
4152 format_loop_panes(struct format_expand_state
*es
, const char *fmt
)
4154 struct format_tree
*ft
= es
->ft
;
4155 struct client
*c
= ft
->client
;
4156 struct cmdq_item
*item
= ft
->item
;
4157 struct format_tree
*nft
;
4158 struct format_expand_state next
;
4159 char *all
, *active
, *use
, *expanded
, *value
;
4161 struct window_pane
*wp
;
4163 if (ft
->w
== NULL
) {
4164 format_log(es
, "pane loop but no window");
4168 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4173 value
= xcalloc(1, 1);
4176 TAILQ_FOREACH(wp
, &ft
->w
->panes
, entry
) {
4177 format_log(es
, "pane loop: %%%u", wp
->id
);
4178 if (active
!= NULL
&& wp
== ft
->w
->active
)
4182 nft
= format_create(c
, item
, FORMAT_PANE
|wp
->id
, ft
->flags
);
4183 format_defaults(nft
, ft
->c
, ft
->s
, ft
->wl
, wp
);
4184 format_copy_state(&next
, es
, 0);
4186 expanded
= format_expand1(&next
, use
);
4189 valuelen
+= strlen(expanded
);
4190 value
= xrealloc(value
, valuelen
);
4192 strlcat(value
, expanded
, valuelen
);
4202 /* Loop over clients. */
4204 format_loop_clients(struct format_expand_state
*es
, const char *fmt
)
4206 struct format_tree
*ft
= es
->ft
;
4208 struct cmdq_item
*item
= ft
->item
;
4209 struct format_tree
*nft
;
4210 struct format_expand_state next
;
4211 char *expanded
, *value
;
4214 value
= xcalloc(1, 1);
4217 TAILQ_FOREACH(c
, &clients
, entry
) {
4218 format_log(es
, "client loop: %s", c
->name
);
4219 nft
= format_create(c
, item
, 0, ft
->flags
);
4220 format_defaults(nft
, c
, ft
->s
, ft
->wl
, ft
->wp
);
4221 format_copy_state(&next
, es
, 0);
4223 expanded
= format_expand1(&next
, fmt
);
4226 valuelen
+= strlen(expanded
);
4227 value
= xrealloc(value
, valuelen
);
4229 strlcat(value
, expanded
, valuelen
);
4237 format_replace_expression(struct format_modifier
*mexp
,
4238 struct format_expand_state
*es
, const char *copy
)
4240 int argc
= mexp
->argc
;
4242 char *endch
, *value
, *left
= NULL
, *right
= NULL
;
4245 double mleft
, mright
, result
;
4256 LESS_THAN_EQUAL
} operator;
4258 if (strcmp(mexp
->argv
[0], "+") == 0)
4260 else if (strcmp(mexp
->argv
[0], "-") == 0)
4261 operator = SUBTRACT
;
4262 else if (strcmp(mexp
->argv
[0], "*") == 0)
4263 operator = MULTIPLY
;
4264 else if (strcmp(mexp
->argv
[0], "/") == 0)
4266 else if (strcmp(mexp
->argv
[0], "%") == 0 ||
4267 strcmp(mexp
->argv
[0], "m") == 0)
4269 else if (strcmp(mexp
->argv
[0], "==") == 0)
4271 else if (strcmp(mexp
->argv
[0], "!=") == 0)
4272 operator = NOT_EQUAL
;
4273 else if (strcmp(mexp
->argv
[0], ">") == 0)
4274 operator = GREATER_THAN
;
4275 else if (strcmp(mexp
->argv
[0], "<") == 0)
4276 operator = LESS_THAN
;
4277 else if (strcmp(mexp
->argv
[0], ">=") == 0)
4278 operator = GREATER_THAN_EQUAL
;
4279 else if (strcmp(mexp
->argv
[0], "<=") == 0)
4280 operator = LESS_THAN_EQUAL
;
4282 format_log(es
, "expression has no valid operator: '%s'",
4287 /* The second argument may be flags. */
4288 if (argc
>= 2 && strchr(mexp
->argv
[1], 'f') != NULL
) {
4293 /* The third argument may be precision. */
4295 prec
= strtonum(mexp
->argv
[2], INT_MIN
, INT_MAX
, &errstr
);
4296 if (errstr
!= NULL
) {
4297 format_log(es
, "expression precision %s: %s", errstr
,
4303 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4304 format_log(es
, "expression syntax error");
4308 mleft
= strtod(left
, &endch
);
4309 if (*endch
!= '\0') {
4310 format_log(es
, "expression left side is invalid: %s", left
);
4314 mright
= strtod(right
, &endch
);
4315 if (*endch
!= '\0') {
4316 format_log(es
, "expression right side is invalid: %s", right
);
4321 mleft
= (long long)mleft
;
4322 mright
= (long long)mright
;
4324 format_log(es
, "expression left side is: %.*f", prec
, mleft
);
4325 format_log(es
, "expression right side is: %.*f", prec
, mright
);
4329 result
= mleft
+ mright
;
4332 result
= mleft
- mright
;
4335 result
= mleft
* mright
;
4338 result
= mleft
/ mright
;
4341 result
= fmod(mleft
, mright
);
4344 result
= fabs(mleft
- mright
) < 1e-9;
4347 result
= fabs(mleft
- mright
) > 1e-9;
4350 result
= (mleft
> mright
);
4352 case GREATER_THAN_EQUAL
:
4353 result
= (mleft
>= mright
);
4356 result
= (mleft
< mright
);
4358 case LESS_THAN_EQUAL
:
4359 result
= (mleft
<= mright
);
4363 xasprintf(&value
, "%.*f", prec
, result
);
4365 xasprintf(&value
, "%.*f", prec
, (double)(long long)result
);
4366 format_log(es
, "expression result is %s", value
);
4378 /* Replace a key. */
4380 format_replace(struct format_expand_state
*es
, const char *key
, size_t keylen
,
4381 char **buf
, size_t *len
, size_t *off
)
4383 struct format_tree
*ft
= es
->ft
;
4384 struct window_pane
*wp
= ft
->wp
;
4385 const char *errstr
, *copy
, *cp
, *marker
= NULL
;
4386 const char *time_format
= NULL
;
4387 char *copy0
, *condition
, *found
, *new;
4388 char *value
, *left
, *right
;
4390 int modifiers
= 0, limit
= 0, width
= 0;
4392 struct format_modifier
*list
, *cmp
= NULL
, *search
= NULL
;
4393 struct format_modifier
**sub
= NULL
, *mexp
= NULL
, *fm
;
4394 u_int i
, count
, nsub
= 0;
4395 struct format_expand_state next
;
4397 /* Make a copy of the key. */
4398 copy
= copy0
= xstrndup(key
, keylen
);
4400 /* Process modifier list. */
4401 list
= format_build_modifiers(es
, ©
, &count
);
4402 for (i
= 0; i
< count
; i
++) {
4404 if (format_logging(ft
)) {
4405 format_log(es
, "modifier %u is %s", i
, fm
->modifier
);
4406 for (j
= 0; j
< fm
->argc
; j
++) {
4407 format_log(es
, "modifier %u argument %d: %s", i
,
4411 if (fm
->size
== 1) {
4412 switch (fm
->modifier
[0]) {
4424 sub
= xreallocarray(sub
, nsub
+ 1, sizeof *sub
);
4430 limit
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4434 if (fm
->argc
>= 2 && fm
->argv
[1] != NULL
)
4435 marker
= fm
->argv
[1];
4440 width
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4446 modifiers
|= FORMAT_WIDTH
;
4449 if (fm
->argc
< 1 || fm
->argc
> 3)
4454 modifiers
|= FORMAT_LITERAL
;
4457 modifiers
|= FORMAT_CHARACTER
;
4460 modifiers
|= FORMAT_BASENAME
;
4463 modifiers
|= FORMAT_COLOUR
;
4466 modifiers
|= FORMAT_DIRNAME
;
4469 modifiers
|= FORMAT_LENGTH
;
4472 modifiers
|= FORMAT_TIMESTRING
;
4475 if (strchr(fm
->argv
[0], 'p') != NULL
)
4476 modifiers
|= FORMAT_PRETTY
;
4477 else if (fm
->argc
>= 2 &&
4478 strchr(fm
->argv
[0], 'f') != NULL
)
4479 time_format
= format_strip(fm
->argv
[1]);
4483 modifiers
|= FORMAT_QUOTE_SHELL
;
4484 else if (strchr(fm
->argv
[0], 'e') != NULL
||
4485 strchr(fm
->argv
[0], 'h') != NULL
)
4486 modifiers
|= FORMAT_QUOTE_STYLE
;
4489 modifiers
|= FORMAT_EXPAND
;
4492 modifiers
|= FORMAT_EXPANDTIME
;
4496 strchr(fm
->argv
[0], 'w') != NULL
)
4497 modifiers
|= FORMAT_WINDOW_NAME
;
4498 else if (strchr(fm
->argv
[0], 's') != NULL
)
4499 modifiers
|= FORMAT_SESSION_NAME
;
4502 modifiers
|= FORMAT_SESSIONS
;
4505 modifiers
|= FORMAT_WINDOWS
;
4508 modifiers
|= FORMAT_PANES
;
4511 modifiers
|= FORMAT_CLIENTS
;
4514 } else if (fm
->size
== 2) {
4515 if (strcmp(fm
->modifier
, "||") == 0 ||
4516 strcmp(fm
->modifier
, "&&") == 0 ||
4517 strcmp(fm
->modifier
, "==") == 0 ||
4518 strcmp(fm
->modifier
, "!=") == 0 ||
4519 strcmp(fm
->modifier
, ">=") == 0 ||
4520 strcmp(fm
->modifier
, "<=") == 0)
4525 /* Is this a literal string? */
4526 if (modifiers
& FORMAT_LITERAL
) {
4527 format_log(es
, "literal string is '%s'", copy
);
4528 value
= format_unescape(copy
);
4532 /* Is this a character? */
4533 if (modifiers
& FORMAT_CHARACTER
) {
4534 new = format_expand1(es
, copy
);
4535 c
= strtonum(new, 32, 126, &errstr
);
4537 value
= xstrdup("");
4539 xasprintf(&value
, "%c", c
);
4544 /* Is this a colour? */
4545 if (modifiers
& FORMAT_COLOUR
) {
4546 new = format_expand1(es
, copy
);
4547 c
= colour_fromstring(new);
4548 if (c
== -1 || (c
= colour_force_rgb(c
)) == -1)
4549 value
= xstrdup("");
4551 xasprintf(&value
, "%06x", c
& 0xffffff);
4556 /* Is this a loop, comparison or condition? */
4557 if (modifiers
& FORMAT_SESSIONS
) {
4558 value
= format_loop_sessions(es
, copy
);
4561 } else if (modifiers
& FORMAT_WINDOWS
) {
4562 value
= format_loop_windows(es
, copy
);
4565 } else if (modifiers
& FORMAT_PANES
) {
4566 value
= format_loop_panes(es
, copy
);
4569 } else if (modifiers
& FORMAT_CLIENTS
) {
4570 value
= format_loop_clients(es
, copy
);
4573 } else if (modifiers
& FORMAT_WINDOW_NAME
) {
4574 value
= format_window_name(es
, copy
);
4577 } else if (modifiers
& FORMAT_SESSION_NAME
) {
4578 value
= format_session_name(es
, copy
);
4581 } else if (search
!= NULL
) {
4582 /* Search in pane. */
4583 new = format_expand1(es
, copy
);
4585 format_log(es
, "search '%s' but no pane", new);
4586 value
= xstrdup("0");
4588 format_log(es
, "search '%s' pane %%%u", new, wp
->id
);
4589 value
= format_search(search
, wp
, new);
4592 } else if (cmp
!= NULL
) {
4593 /* Comparison of left and right. */
4594 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4595 format_log(es
, "compare %s syntax error: %s",
4596 cmp
->modifier
, copy
);
4599 format_log(es
, "compare %s left is: %s", cmp
->modifier
, left
);
4600 format_log(es
, "compare %s right is: %s", cmp
->modifier
, right
);
4602 if (strcmp(cmp
->modifier
, "||") == 0) {
4603 if (format_true(left
) || format_true(right
))
4604 value
= xstrdup("1");
4606 value
= xstrdup("0");
4607 } else if (strcmp(cmp
->modifier
, "&&") == 0) {
4608 if (format_true(left
) && format_true(right
))
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
, "<") == 0) {
4623 if (strcmp(left
, right
) < 0)
4624 value
= xstrdup("1");
4626 value
= xstrdup("0");
4627 } else if (strcmp(cmp
->modifier
, ">") == 0) {
4628 if (strcmp(left
, right
) > 0)
4629 value
= xstrdup("1");
4631 value
= xstrdup("0");
4632 } else if (strcmp(cmp
->modifier
, "<=") == 0) {
4633 if (strcmp(left
, right
) <= 0)
4634 value
= xstrdup("1");
4636 value
= xstrdup("0");
4637 } else if (strcmp(cmp
->modifier
, ">=") == 0) {
4638 if (strcmp(left
, right
) >= 0)
4639 value
= xstrdup("1");
4641 value
= xstrdup("0");
4642 } else if (strcmp(cmp
->modifier
, "m") == 0)
4643 value
= format_match(cmp
, left
, right
);
4647 } else if (*copy
== '?') {
4648 /* Conditional: check first and choose second or third. */
4649 cp
= format_skip(copy
+ 1, ",");
4651 format_log(es
, "condition syntax error: %s", copy
+ 1);
4654 condition
= xstrndup(copy
+ 1, cp
- (copy
+ 1));
4655 format_log(es
, "condition is: %s", condition
);
4657 found
= format_find(ft
, condition
, modifiers
, time_format
);
4658 if (found
== NULL
) {
4660 * If the condition not found, try to expand it. If
4661 * the expansion doesn't have any effect, then assume
4664 found
= format_expand1(es
, condition
);
4665 if (strcmp(found
, condition
) == 0) {
4667 found
= xstrdup("");
4669 "condition '%s' not found; assuming false",
4673 format_log(es
, "condition '%s' found: %s", condition
,
4677 if (format_choose(es
, cp
+ 1, &left
, &right
, 0) != 0) {
4678 format_log(es
, "condition '%s' syntax error: %s",
4683 if (format_true(found
)) {
4684 format_log(es
, "condition '%s' is true", condition
);
4685 value
= format_expand1(es
, left
);
4687 format_log(es
, "condition '%s' is false", condition
);
4688 value
= format_expand1(es
, right
);
4695 } else if (mexp
!= NULL
) {
4696 value
= format_replace_expression(mexp
, es
, copy
);
4698 value
= xstrdup("");
4700 if (strstr(copy
, "#{") != 0) {
4701 format_log(es
, "expanding inner format '%s'", copy
);
4702 value
= format_expand1(es
, copy
);
4704 value
= format_find(ft
, copy
, modifiers
, time_format
);
4705 if (value
== NULL
) {
4706 format_log(es
, "format '%s' not found", copy
);
4707 value
= xstrdup("");
4709 format_log(es
, "format '%s' found: %s", copy
,
4716 /* Expand again if required. */
4717 if (modifiers
& FORMAT_EXPAND
) {
4718 new = format_expand1(es
, value
);
4721 } else if (modifiers
& FORMAT_EXPANDTIME
) {
4722 format_copy_state(&next
, es
, FORMAT_EXPAND_TIME
);
4723 new = format_expand1(&next
, value
);
4728 /* Perform substitution if any. */
4729 for (i
= 0; i
< nsub
; i
++) {
4730 left
= format_expand1(es
, sub
[i
]->argv
[0]);
4731 right
= format_expand1(es
, sub
[i
]->argv
[1]);
4732 new = format_sub(sub
[i
], value
, left
, right
);
4733 format_log(es
, "substitute '%s' to '%s': %s", left
, right
, new);
4740 /* Truncate the value if needed. */
4742 new = format_trim_left(value
, limit
);
4743 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4745 xasprintf(&value
, "%s%s", new, marker
);
4750 format_log(es
, "applied length limit %d: %s", limit
, value
);
4751 } else if (limit
< 0) {
4752 new = format_trim_right(value
, -limit
);
4753 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4755 xasprintf(&value
, "%s%s", marker
, new);
4760 format_log(es
, "applied length limit %d: %s", limit
, value
);
4763 /* Pad the value if needed. */
4765 new = utf8_padcstr(value
, width
);
4768 format_log(es
, "applied padding width %d: %s", width
, value
);
4769 } else if (width
< 0) {
4770 new = utf8_rpadcstr(value
, -width
);
4773 format_log(es
, "applied padding width %d: %s", width
, value
);
4776 /* Replace with the length or width if needed. */
4777 if (modifiers
& FORMAT_LENGTH
) {
4778 xasprintf(&new, "%zu", strlen(value
));
4781 format_log(es
, "replacing with length: %s", new);
4783 if (modifiers
& FORMAT_WIDTH
) {
4784 xasprintf(&new, "%u", format_width(value
));
4787 format_log(es
, "replacing with width: %s", new);
4790 /* Expand the buffer and copy in the value. */
4791 valuelen
= strlen(value
);
4792 while (*len
- *off
< valuelen
+ 1) {
4793 *buf
= xreallocarray(*buf
, 2, *len
);
4796 memcpy(*buf
+ *off
, value
, valuelen
);
4799 format_log(es
, "replaced '%s' with '%s'", copy0
, value
);
4803 format_free_modifiers(list
, count
);
4808 format_log(es
, "failed %s", copy0
);
4811 format_free_modifiers(list
, count
);
4816 /* Expand keys in a template. */
4818 format_expand1(struct format_expand_state
*es
, const char *fmt
)
4820 struct format_tree
*ft
= es
->ft
;
4821 char *buf
, *out
, *name
;
4822 const char *ptr
, *s
, *style_end
= NULL
;
4823 size_t off
, len
, n
, outlen
;
4825 char expanded
[8192];
4827 if (fmt
== NULL
|| *fmt
== '\0')
4828 return (xstrdup(""));
4830 if (es
->loop
== FORMAT_LOOP_LIMIT
) {
4831 format_log(es
, "reached loop limit (%u)", FORMAT_LOOP_LIMIT
);
4832 return (xstrdup(""));
4836 format_log(es
, "expanding format: %s", fmt
);
4838 if ((es
->flags
& FORMAT_EXPAND_TIME
) && strchr(fmt
, '%') != NULL
) {
4839 if (es
->time
== 0) {
4840 es
->time
= time(NULL
);
4841 localtime_r(&es
->time
, &es
->tm
);
4843 if (strftime(expanded
, sizeof expanded
, fmt
, &es
->tm
) == 0) {
4844 format_log(es
, "format is too long");
4845 return (xstrdup(""));
4847 if (format_logging(ft
) && strcmp(expanded
, fmt
) != 0)
4848 format_log(es
, "after time expanded: %s", expanded
);
4856 while (*fmt
!= '\0') {
4858 while (len
- off
< 2) {
4859 buf
= xreallocarray(buf
, 2, len
);
4862 buf
[off
++] = *fmt
++;
4867 ch
= (u_char
)*fmt
++;
4871 for (ptr
= fmt
; *ptr
!= '\0'; ptr
++) {
4874 if (*ptr
== ')' && --brackets
== 0)
4877 if (*ptr
!= ')' || brackets
!= 0)
4881 name
= xstrndup(fmt
, n
);
4882 format_log(es
, "found #(): %s", name
);
4884 if ((ft
->flags
& FORMAT_NOJOBS
) ||
4885 (es
->flags
& FORMAT_EXPAND_NOJOBS
)) {
4887 format_log(es
, "#() is disabled");
4889 out
= format_job_get(es
, name
);
4890 format_log(es
, "#() result: %s", out
);
4894 outlen
= strlen(out
);
4895 while (len
- off
< outlen
+ 1) {
4896 buf
= xreallocarray(buf
, 2, len
);
4899 memcpy(buf
+ off
, out
, outlen
);
4907 ptr
= format_skip((char *)fmt
- 2, "}");
4912 format_log(es
, "found #{}: %.*s", (int)n
, fmt
);
4913 if (format_replace(es
, fmt
, n
, &buf
, &len
, &off
) != 0)
4920 * If ##[ (with two or more #s), then it is a style and
4921 * can be left for format_draw to handle.
4923 ptr
= fmt
- (ch
== '[');
4924 n
= 2 - (ch
== '[');
4925 while (*ptr
== '#') {
4930 style_end
= format_skip(fmt
- 2, "]");
4931 format_log(es
, "found #*%zu[", n
);
4932 while (len
- off
< n
+ 2) {
4933 buf
= xreallocarray(buf
, 2, len
);
4936 memcpy(buf
+ off
, fmt
- 2, n
+ 1);
4944 format_log(es
, "found #%c", ch
);
4945 while (len
- off
< 2) {
4946 buf
= xreallocarray(buf
, 2, len
);
4953 if (fmt
> style_end
) { /* skip inside #[] */
4954 if (ch
>= 'A' && ch
<= 'Z')
4955 s
= format_upper
[ch
- 'A'];
4956 else if (ch
>= 'a' && ch
<= 'z')
4957 s
= format_lower
[ch
- 'a'];
4960 while (len
- off
< 3) {
4961 buf
= xreallocarray(buf
, 2, len
);
4969 format_log(es
, "found #%c: %s", ch
, s
);
4970 if (format_replace(es
, s
, n
, &buf
, &len
, &off
) != 0)
4979 format_log(es
, "result is: %s", buf
);
4985 /* Expand keys in a template, passing through strftime first. */
4987 format_expand_time(struct format_tree
*ft
, const char *fmt
)
4989 struct format_expand_state es
;
4991 memset(&es
, 0, sizeof es
);
4993 es
.flags
= FORMAT_EXPAND_TIME
;
4994 return (format_expand1(&es
, fmt
));
4997 /* Expand keys in a template. */
4999 format_expand(struct format_tree
*ft
, const char *fmt
)
5001 struct format_expand_state es
;
5003 memset(&es
, 0, sizeof es
);
5006 return (format_expand1(&es
, fmt
));
5009 /* Expand a single string. */
5011 format_single(struct cmdq_item
*item
, const char *fmt
, struct client
*c
,
5012 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
5014 struct format_tree
*ft
;
5017 ft
= format_create_defaults(item
, c
, s
, wl
, wp
);
5018 expanded
= format_expand(ft
, fmt
);
5023 /* Expand a single string using state. */
5025 format_single_from_state(struct cmdq_item
*item
, const char *fmt
,
5026 struct client
*c
, struct cmd_find_state
*fs
)
5028 return (format_single(item
, fmt
, c
, fs
->s
, fs
->wl
, fs
->wp
));
5031 /* Expand a single string using target. */
5033 format_single_from_target(struct cmdq_item
*item
, const char *fmt
)
5035 struct client
*tc
= cmdq_get_target_client(item
);
5037 return (format_single_from_state(item
, fmt
, tc
, cmdq_get_target(item
)));
5040 /* Create and add defaults. */
5041 struct format_tree
*
5042 format_create_defaults(struct cmdq_item
*item
, struct client
*c
,
5043 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
5045 struct format_tree
*ft
;
5048 ft
= format_create(cmdq_get_client(item
), item
, FORMAT_NONE
, 0);
5050 ft
= format_create(NULL
, item
, FORMAT_NONE
, 0);
5051 format_defaults(ft
, c
, s
, wl
, wp
);
5055 /* Create and add defaults using state. */
5056 struct format_tree
*
5057 format_create_from_state(struct cmdq_item
*item
, struct client
*c
,
5058 struct cmd_find_state
*fs
)
5060 return (format_create_defaults(item
, c
, fs
->s
, fs
->wl
, fs
->wp
));
5063 /* Create and add defaults using target. */
5064 struct format_tree
*
5065 format_create_from_target(struct cmdq_item
*item
)
5067 struct client
*tc
= cmdq_get_target_client(item
);
5069 return (format_create_from_state(item
, tc
, cmdq_get_target(item
)));
5072 /* Set defaults for any of arguments that are not NULL. */
5074 format_defaults(struct format_tree
*ft
, struct client
*c
, struct session
*s
,
5075 struct winlink
*wl
, struct window_pane
*wp
)
5077 struct paste_buffer
*pb
;
5079 if (c
!= NULL
&& c
->name
!= NULL
)
5080 log_debug("%s: c=%s", __func__
, c
->name
);
5082 log_debug("%s: c=none", __func__
);
5084 log_debug("%s: s=$%u", __func__
, s
->id
);
5086 log_debug("%s: s=none", __func__
);
5088 log_debug("%s: wl=%u", __func__
, wl
->idx
);
5090 log_debug("%s: wl=none", __func__
);
5092 log_debug("%s: wp=%%%u", __func__
, wp
->id
);
5094 log_debug("%s: wp=none", __func__
);
5096 if (c
!= NULL
&& s
!= NULL
&& c
->session
!= s
)
5097 log_debug("%s: session does not match", __func__
);
5100 ft
->type
= FORMAT_TYPE_PANE
;
5101 else if (wl
!= NULL
)
5102 ft
->type
= FORMAT_TYPE_WINDOW
;
5104 ft
->type
= FORMAT_TYPE_SESSION
;
5106 ft
->type
= FORMAT_TYPE_UNKNOWN
;
5108 if (s
== NULL
&& c
!= NULL
)
5110 if (wl
== NULL
&& s
!= NULL
)
5112 if (wp
== NULL
&& wl
!= NULL
)
5113 wp
= wl
->window
->active
;
5116 format_defaults_client(ft
, c
);
5118 format_defaults_session(ft
, s
);
5120 format_defaults_winlink(ft
, wl
);
5122 format_defaults_pane(ft
, wp
);
5124 pb
= paste_get_top(NULL
);
5126 format_defaults_paste_buffer(ft
, pb
);
5129 /* Set default format keys for a session. */
5131 format_defaults_session(struct format_tree
*ft
, struct session
*s
)
5136 /* Set default format keys for a client. */
5138 format_defaults_client(struct format_tree
*ft
, struct client
*c
)
5145 /* Set default format keys for a window. */
5147 format_defaults_window(struct format_tree
*ft
, struct window
*w
)
5152 /* Set default format keys for a winlink. */
5154 format_defaults_winlink(struct format_tree
*ft
, struct winlink
*wl
)
5157 format_defaults_window(ft
, wl
->window
);
5161 /* Set default format keys for a window pane. */
5163 format_defaults_pane(struct format_tree
*ft
, struct window_pane
*wp
)
5165 struct window_mode_entry
*wme
;
5168 format_defaults_window(ft
, wp
->window
);
5171 wme
= TAILQ_FIRST(&wp
->modes
);
5172 if (wme
!= NULL
&& wme
->mode
->formats
!= NULL
)
5173 wme
->mode
->formats(wme
, ft
);
5176 /* Set default format keys for paste buffer. */
5178 format_defaults_paste_buffer(struct format_tree
*ft
, struct paste_buffer
*pb
)
5183 /* Return word at given coordinates. Caller frees. */
5185 format_grid_word(struct grid
*gd
, u_int x
, u_int y
)
5187 const struct grid_line
*gl
;
5188 struct grid_cell gc
;
5190 struct utf8_data
*ud
= NULL
;
5196 ws
= options_get_string(global_s_options
, "word-separators");
5199 grid_get_cell(gd
, x
, y
, &gc
);
5200 if (gc
.flags
& GRID_FLAG_PADDING
)
5202 if (utf8_cstrhas(ws
, &gc
.data
) ||
5203 (gc
.data
.size
== 1 && *gc
.data
.data
== ' ')) {
5211 gl
= grid_peek_line(gd
, y
- 1);
5212 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5215 x
= grid_line_length(gd
, y
);
5223 end
= grid_line_length(gd
, y
);
5224 if (end
== 0 || x
== end
- 1) {
5225 if (y
== gd
->hsize
+ gd
->sy
- 1)
5227 gl
= grid_peek_line(gd
, y
);
5228 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5237 grid_get_cell(gd
, x
, y
, &gc
);
5238 if (gc
.flags
& GRID_FLAG_PADDING
)
5240 if (utf8_cstrhas(ws
, &gc
.data
) ||
5241 (gc
.data
.size
== 1 && *gc
.data
.data
== ' '))
5244 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5245 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5249 s
= utf8_tocstr(ud
);
5255 /* Return line at given coordinates. Caller frees. */
5257 format_grid_line(struct grid
*gd
, u_int y
)
5259 struct grid_cell gc
;
5260 struct utf8_data
*ud
= NULL
;
5265 for (x
= 0; x
< grid_line_length(gd
, y
); x
++) {
5266 grid_get_cell(gd
, x
, y
, &gc
);
5267 if (gc
.flags
& GRID_FLAG_PADDING
)
5270 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5271 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5275 s
= utf8_tocstr(ud
);
5281 /* Return hyperlink at given coordinates. Caller frees. */
5283 format_grid_hyperlink(struct grid
*gd
, u_int x
, u_int y
, struct screen
* s
)
5286 struct grid_cell gc
;
5288 grid_get_cell(gd
, x
, y
, &gc
);
5289 if (gc
.flags
& GRID_FLAG_PADDING
)
5291 if (s
->hyperlinks
== NULL
|| gc
.link
== 0)
5293 if (!hyperlinks_get(s
->hyperlinks
, gc
.link
, &uri
, NULL
, NULL
))
5295 return (xstrdup(uri
));