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
;
1133 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1136 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1139 if (!TAILQ_EMPTY(&wp
->modes
)) {
1140 if (TAILQ_FIRST(&wp
->modes
)->mode
== &window_copy_mode
||
1141 TAILQ_FIRST(&wp
->modes
)->mode
== &window_view_mode
)
1142 return (s
= window_copy_get_word(wp
, x
, y
));
1146 return (format_grid_word(gd
, x
, gd
->hsize
+ y
));
1149 /* Callback for mouse_hyperlink. */
1151 format_cb_mouse_hyperlink(struct format_tree
*ft
)
1153 struct window_pane
*wp
;
1159 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1162 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1165 return (format_grid_hyperlink(gd
, x
, gd
->hsize
+ y
, wp
->screen
));
1168 /* Callback for mouse_line. */
1170 format_cb_mouse_line(struct format_tree
*ft
)
1172 struct window_pane
*wp
;
1178 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1181 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1184 if (!TAILQ_EMPTY(&wp
->modes
)) {
1185 if (TAILQ_FIRST(&wp
->modes
)->mode
== &window_copy_mode
||
1186 TAILQ_FIRST(&wp
->modes
)->mode
== &window_view_mode
)
1187 return (window_copy_get_line(wp
, y
));
1191 return (format_grid_line(gd
, gd
->hsize
+ y
));
1194 /* Callback for mouse_status_line. */
1196 format_cb_mouse_status_line(struct format_tree
*ft
)
1203 if (ft
->c
== NULL
|| (~ft
->c
->tty
.flags
& TTY_STARTED
))
1206 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
) {
1208 } else if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
) {
1209 y
= ft
->m
.y
- ft
->m
.statusat
;
1212 xasprintf(&value
, "%u", y
);
1217 /* Callback for mouse_status_range. */
1219 format_cb_mouse_status_range(struct format_tree
*ft
)
1221 struct style_range
*sr
;
1226 if (ft
->c
== NULL
|| (~ft
->c
->tty
.flags
& TTY_STARTED
))
1229 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
) {
1232 } else if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
) {
1234 y
= ft
->m
.y
- ft
->m
.statusat
;
1238 sr
= status_get_range(ft
->c
, x
, y
);
1242 case STYLE_RANGE_NONE
:
1244 case STYLE_RANGE_LEFT
:
1245 return (xstrdup("left"));
1246 case STYLE_RANGE_RIGHT
:
1247 return (xstrdup("right"));
1248 case STYLE_RANGE_PANE
:
1249 return (xstrdup("pane"));
1250 case STYLE_RANGE_WINDOW
:
1251 return (xstrdup("window"));
1252 case STYLE_RANGE_SESSION
:
1253 return (xstrdup("session"));
1254 case STYLE_RANGE_USER
:
1255 return (xstrdup(sr
->string
));
1260 /* Callback for alternate_on. */
1262 format_cb_alternate_on(struct format_tree
*ft
)
1264 if (ft
->wp
!= NULL
) {
1265 if (ft
->wp
->base
.saved_grid
!= NULL
)
1266 return (xstrdup("1"));
1267 return (xstrdup("0"));
1272 /* Callback for alternate_saved_x. */
1274 format_cb_alternate_saved_x(struct format_tree
*ft
)
1277 return (format_printf("%u", ft
->wp
->base
.saved_cx
));
1281 /* Callback for alternate_saved_y. */
1283 format_cb_alternate_saved_y(struct format_tree
*ft
)
1286 return (format_printf("%u", ft
->wp
->base
.saved_cy
));
1290 /* Callback for buffer_name. */
1292 format_cb_buffer_name(struct format_tree
*ft
)
1295 return (xstrdup(paste_buffer_name(ft
->pb
)));
1299 /* Callback for buffer_sample. */
1301 format_cb_buffer_sample(struct format_tree
*ft
)
1304 return (paste_make_sample(ft
->pb
));
1308 /* Callback for buffer_size. */
1310 format_cb_buffer_size(struct format_tree
*ft
)
1314 if (ft
->pb
!= NULL
) {
1315 paste_buffer_data(ft
->pb
, &size
);
1316 return (format_printf("%zu", size
));
1321 /* Callback for client_cell_height. */
1323 format_cb_client_cell_height(struct format_tree
*ft
)
1325 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1326 return (format_printf("%u", ft
->c
->tty
.ypixel
));
1330 /* Callback for client_cell_width. */
1332 format_cb_client_cell_width(struct format_tree
*ft
)
1334 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1335 return (format_printf("%u", ft
->c
->tty
.xpixel
));
1339 /* Callback for client_control_mode. */
1341 format_cb_client_control_mode(struct format_tree
*ft
)
1343 if (ft
->c
!= NULL
) {
1344 if (ft
->c
->flags
& CLIENT_CONTROL
)
1345 return (xstrdup("1"));
1346 return (xstrdup("0"));
1351 /* Callback for client_discarded. */
1353 format_cb_client_discarded(struct format_tree
*ft
)
1356 return (format_printf("%zu", ft
->c
->discarded
));
1360 /* Callback for client_flags. */
1362 format_cb_client_flags(struct format_tree
*ft
)
1365 return (xstrdup(server_client_get_flags(ft
->c
)));
1369 /* Callback for client_height. */
1371 format_cb_client_height(struct format_tree
*ft
)
1373 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1374 return (format_printf("%u", ft
->c
->tty
.sy
));
1378 /* Callback for client_key_table. */
1380 format_cb_client_key_table(struct format_tree
*ft
)
1383 return (xstrdup(ft
->c
->keytable
->name
));
1387 /* Callback for client_last_session. */
1389 format_cb_client_last_session(struct format_tree
*ft
)
1391 if (ft
->c
!= NULL
&&
1392 ft
->c
->last_session
!= NULL
&&
1393 session_alive(ft
->c
->last_session
))
1394 return (xstrdup(ft
->c
->last_session
->name
));
1398 /* Callback for client_name. */
1400 format_cb_client_name(struct format_tree
*ft
)
1403 return (xstrdup(ft
->c
->name
));
1407 /* Callback for client_pid. */
1409 format_cb_client_pid(struct format_tree
*ft
)
1412 return (format_printf("%ld", (long)ft
->c
->pid
));
1416 /* Callback for client_prefix. */
1418 format_cb_client_prefix(struct format_tree
*ft
)
1422 if (ft
->c
!= NULL
) {
1423 name
= server_client_get_key_table(ft
->c
);
1424 if (strcmp(ft
->c
->keytable
->name
, name
) == 0)
1425 return (xstrdup("0"));
1426 return (xstrdup("1"));
1431 /* Callback for client_readonly. */
1433 format_cb_client_readonly(struct format_tree
*ft
)
1435 if (ft
->c
!= NULL
) {
1436 if (ft
->c
->flags
& CLIENT_READONLY
)
1437 return (xstrdup("1"));
1438 return (xstrdup("0"));
1443 /* Callback for client_session. */
1445 format_cb_client_session(struct format_tree
*ft
)
1447 if (ft
->c
!= NULL
&& ft
->c
->session
!= NULL
)
1448 return (xstrdup(ft
->c
->session
->name
));
1452 /* Callback for client_termfeatures. */
1454 format_cb_client_termfeatures(struct format_tree
*ft
)
1457 return (xstrdup(tty_get_features(ft
->c
->term_features
)));
1461 /* Callback for client_termname. */
1463 format_cb_client_termname(struct format_tree
*ft
)
1466 return (xstrdup(ft
->c
->term_name
));
1470 /* Callback for client_termtype. */
1472 format_cb_client_termtype(struct format_tree
*ft
)
1474 if (ft
->c
!= NULL
) {
1475 if (ft
->c
->term_type
== NULL
)
1476 return (xstrdup(""));
1477 return (xstrdup(ft
->c
->term_type
));
1482 /* Callback for client_tty. */
1484 format_cb_client_tty(struct format_tree
*ft
)
1487 return (xstrdup(ft
->c
->ttyname
));
1491 /* Callback for client_uid. */
1493 format_cb_client_uid(struct format_tree
*ft
)
1497 if (ft
->c
!= NULL
) {
1498 uid
= proc_get_peer_uid(ft
->c
->peer
);
1499 if (uid
!= (uid_t
)-1)
1500 return (format_printf("%ld", (long)uid
));
1505 /* Callback for client_user. */
1507 format_cb_client_user(struct format_tree
*ft
)
1512 if (ft
->c
!= NULL
) {
1513 uid
= proc_get_peer_uid(ft
->c
->peer
);
1514 if (uid
!= (uid_t
)-1 && (pw
= getpwuid(uid
)) != NULL
)
1515 return (xstrdup(pw
->pw_name
));
1520 /* Callback for client_utf8. */
1522 format_cb_client_utf8(struct format_tree
*ft
)
1524 if (ft
->c
!= NULL
) {
1525 if (ft
->c
->flags
& CLIENT_UTF8
)
1526 return (xstrdup("1"));
1527 return (xstrdup("0"));
1532 /* Callback for client_width. */
1534 format_cb_client_width(struct format_tree
*ft
)
1537 return (format_printf("%u", ft
->c
->tty
.sx
));
1541 /* Callback for client_written. */
1543 format_cb_client_written(struct format_tree
*ft
)
1546 return (format_printf("%zu", ft
->c
->written
));
1550 /* Callback for config_files. */
1552 format_cb_config_files(__unused
struct format_tree
*ft
)
1559 for (i
= 0; i
< cfg_nfiles
; i
++) {
1560 n
= strlen(cfg_files
[i
]) + 1;
1561 s
= xrealloc(s
, slen
+ n
+ 1);
1562 slen
+= xsnprintf(s
+ slen
, n
+ 1, "%s,", cfg_files
[i
]);
1565 return (xstrdup(""));
1570 /* Callback for cursor_flag. */
1572 format_cb_cursor_flag(struct format_tree
*ft
)
1574 if (ft
->wp
!= NULL
) {
1575 if (ft
->wp
->base
.mode
& MODE_CURSOR
)
1576 return (xstrdup("1"));
1577 return (xstrdup("0"));
1582 /* Callback for cursor_x. */
1584 format_cb_cursor_x(struct format_tree
*ft
)
1587 return (format_printf("%u", ft
->wp
->base
.cx
));
1591 /* Callback for cursor_y. */
1593 format_cb_cursor_y(struct format_tree
*ft
)
1596 return (format_printf("%u", ft
->wp
->base
.cy
));
1600 /* Callback for history_limit. */
1602 format_cb_history_limit(struct format_tree
*ft
)
1605 return (format_printf("%u", ft
->wp
->base
.grid
->hlimit
));
1609 /* Callback for history_size. */
1611 format_cb_history_size(struct format_tree
*ft
)
1614 return (format_printf("%u", ft
->wp
->base
.grid
->hsize
));
1618 /* Callback for insert_flag. */
1620 format_cb_insert_flag(struct format_tree
*ft
)
1622 if (ft
->wp
!= NULL
) {
1623 if (ft
->wp
->base
.mode
& MODE_INSERT
)
1624 return (xstrdup("1"));
1625 return (xstrdup("0"));
1630 /* Callback for keypad_cursor_flag. */
1632 format_cb_keypad_cursor_flag(struct format_tree
*ft
)
1634 if (ft
->wp
!= NULL
) {
1635 if (ft
->wp
->base
.mode
& MODE_KCURSOR
)
1636 return (xstrdup("1"));
1637 return (xstrdup("0"));
1642 /* Callback for keypad_flag. */
1644 format_cb_keypad_flag(struct format_tree
*ft
)
1646 if (ft
->wp
!= NULL
) {
1647 if (ft
->wp
->base
.mode
& MODE_KKEYPAD
)
1648 return (xstrdup("1"));
1649 return (xstrdup("0"));
1654 /* Callback for mouse_all_flag. */
1656 format_cb_mouse_all_flag(struct format_tree
*ft
)
1658 if (ft
->wp
!= NULL
) {
1659 if (ft
->wp
->base
.mode
& MODE_MOUSE_ALL
)
1660 return (xstrdup("1"));
1661 return (xstrdup("0"));
1666 /* Callback for mouse_any_flag. */
1668 format_cb_mouse_any_flag(struct format_tree
*ft
)
1670 if (ft
->wp
!= NULL
) {
1671 if (ft
->wp
->base
.mode
& ALL_MOUSE_MODES
)
1672 return (xstrdup("1"));
1673 return (xstrdup("0"));
1678 /* Callback for mouse_button_flag. */
1680 format_cb_mouse_button_flag(struct format_tree
*ft
)
1682 if (ft
->wp
!= NULL
) {
1683 if (ft
->wp
->base
.mode
& MODE_MOUSE_BUTTON
)
1684 return (xstrdup("1"));
1685 return (xstrdup("0"));
1690 /* Callback for mouse_pane. */
1692 format_cb_mouse_pane(struct format_tree
*ft
)
1694 struct window_pane
*wp
;
1697 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1699 return (format_printf("%%%u", wp
->id
));
1705 /* Callback for mouse_sgr_flag. */
1707 format_cb_mouse_sgr_flag(struct format_tree
*ft
)
1709 if (ft
->wp
!= NULL
) {
1710 if (ft
->wp
->base
.mode
& MODE_MOUSE_SGR
)
1711 return (xstrdup("1"));
1712 return (xstrdup("0"));
1717 /* Callback for mouse_standard_flag. */
1719 format_cb_mouse_standard_flag(struct format_tree
*ft
)
1721 if (ft
->wp
!= NULL
) {
1722 if (ft
->wp
->base
.mode
& MODE_MOUSE_STANDARD
)
1723 return (xstrdup("1"));
1724 return (xstrdup("0"));
1729 /* Callback for mouse_utf8_flag. */
1731 format_cb_mouse_utf8_flag(struct format_tree
*ft
)
1733 if (ft
->wp
!= NULL
) {
1734 if (ft
->wp
->base
.mode
& MODE_MOUSE_UTF8
)
1735 return (xstrdup("1"));
1736 return (xstrdup("0"));
1741 /* Callback for mouse_x. */
1743 format_cb_mouse_x(struct format_tree
*ft
)
1745 struct window_pane
*wp
;
1750 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1751 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1752 return (format_printf("%u", x
));
1753 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1754 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1755 return (format_printf("%u", ft
->m
.x
));
1756 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1757 return (format_printf("%u", ft
->m
.x
));
1762 /* Callback for mouse_y. */
1764 format_cb_mouse_y(struct format_tree
*ft
)
1766 struct window_pane
*wp
;
1771 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1772 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1773 return (format_printf("%u", y
));
1774 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1775 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1776 return (format_printf("%u", ft
->m
.y
));
1777 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1778 return (format_printf("%u", ft
->m
.y
- ft
->m
.statusat
));
1783 /* Callback for next_session_id. */
1785 format_cb_next_session_id(__unused
struct format_tree
*ft
)
1787 return (format_printf("$%u", next_session_id
));
1790 /* Callback for origin_flag. */
1792 format_cb_origin_flag(struct format_tree
*ft
)
1794 if (ft
->wp
!= NULL
) {
1795 if (ft
->wp
->base
.mode
& MODE_ORIGIN
)
1796 return (xstrdup("1"));
1797 return (xstrdup("0"));
1802 /* Callback for pane_active. */
1804 format_cb_pane_active(struct format_tree
*ft
)
1806 if (ft
->wp
!= NULL
) {
1807 if (ft
->wp
== ft
->wp
->window
->active
)
1808 return (xstrdup("1"));
1809 return (xstrdup("0"));
1814 /* Callback for pane_at_left. */
1816 format_cb_pane_at_left(struct format_tree
*ft
)
1818 if (ft
->wp
!= NULL
) {
1819 if (ft
->wp
->xoff
== 0)
1820 return (xstrdup("1"));
1821 return (xstrdup("0"));
1826 /* Callback for pane_at_right. */
1828 format_cb_pane_at_right(struct format_tree
*ft
)
1830 if (ft
->wp
!= NULL
) {
1831 if (ft
->wp
->xoff
+ ft
->wp
->sx
== ft
->wp
->window
->sx
)
1832 return (xstrdup("1"));
1833 return (xstrdup("0"));
1838 /* Callback for pane_bottom. */
1840 format_cb_pane_bottom(struct format_tree
*ft
)
1843 return (format_printf("%u", ft
->wp
->yoff
+ ft
->wp
->sy
- 1));
1847 /* Callback for pane_dead. */
1849 format_cb_pane_dead(struct format_tree
*ft
)
1851 if (ft
->wp
!= NULL
) {
1852 if (ft
->wp
->fd
== -1)
1853 return (xstrdup("1"));
1854 return (xstrdup("0"));
1859 /* Callback for pane_dead_signal. */
1861 format_cb_pane_dead_signal(struct format_tree
*ft
)
1863 struct window_pane
*wp
= ft
->wp
;
1867 if ((wp
->flags
& PANE_STATUSREADY
) && WIFSIGNALED(wp
->status
)) {
1868 name
= sig2name(WTERMSIG(wp
->status
));
1869 return (format_printf("%s", name
));
1876 /* Callback for pane_dead_status. */
1878 format_cb_pane_dead_status(struct format_tree
*ft
)
1880 struct window_pane
*wp
= ft
->wp
;
1883 if ((wp
->flags
& PANE_STATUSREADY
) && WIFEXITED(wp
->status
))
1884 return (format_printf("%d", WEXITSTATUS(wp
->status
)));
1890 /* Callback for pane_dead_time. */
1892 format_cb_pane_dead_time(struct format_tree
*ft
)
1894 struct window_pane
*wp
= ft
->wp
;
1897 if (wp
->flags
& PANE_STATUSDRAWN
)
1898 return (&wp
->dead_time
);
1904 /* Callback for pane_format. */
1906 format_cb_pane_format(struct format_tree
*ft
)
1908 if (ft
->type
== FORMAT_TYPE_PANE
)
1909 return (xstrdup("1"));
1910 return (xstrdup("0"));
1913 /* Callback for pane_height. */
1915 format_cb_pane_height(struct format_tree
*ft
)
1918 return (format_printf("%u", ft
->wp
->sy
));
1922 /* Callback for pane_id. */
1924 format_cb_pane_id(struct format_tree
*ft
)
1927 return (format_printf("%%%u", ft
->wp
->id
));
1931 /* Callback for pane_index. */
1933 format_cb_pane_index(struct format_tree
*ft
)
1937 if (ft
->wp
!= NULL
&& window_pane_index(ft
->wp
, &idx
) == 0)
1938 return (format_printf("%u", idx
));
1942 /* Callback for pane_input_off. */
1944 format_cb_pane_input_off(struct format_tree
*ft
)
1946 if (ft
->wp
!= NULL
) {
1947 if (ft
->wp
->flags
& PANE_INPUTOFF
)
1948 return (xstrdup("1"));
1949 return (xstrdup("0"));
1954 /* Callback for pane_unseen_changes. */
1956 format_cb_pane_unseen_changes(struct format_tree
*ft
)
1958 if (ft
->wp
!= NULL
) {
1959 if (ft
->wp
->flags
& PANE_UNSEENCHANGES
)
1960 return (xstrdup("1"));
1961 return (xstrdup("0"));
1966 /* Callback for pane_last. */
1968 format_cb_pane_last(struct format_tree
*ft
)
1970 if (ft
->wp
!= NULL
) {
1971 if (ft
->wp
== TAILQ_FIRST(&ft
->wp
->window
->last_panes
))
1972 return (xstrdup("1"));
1973 return (xstrdup("0"));
1978 /* Callback for pane_left. */
1980 format_cb_pane_left(struct format_tree
*ft
)
1983 return (format_printf("%u", ft
->wp
->xoff
));
1987 /* Callback for pane_marked. */
1989 format_cb_pane_marked(struct format_tree
*ft
)
1991 if (ft
->wp
!= NULL
) {
1992 if (server_check_marked() && marked_pane
.wp
== ft
->wp
)
1993 return (xstrdup("1"));
1994 return (xstrdup("0"));
1999 /* Callback for pane_marked_set. */
2001 format_cb_pane_marked_set(struct format_tree
*ft
)
2003 if (ft
->wp
!= NULL
) {
2004 if (server_check_marked())
2005 return (xstrdup("1"));
2006 return (xstrdup("0"));
2011 /* Callback for pane_mode. */
2013 format_cb_pane_mode(struct format_tree
*ft
)
2015 struct window_mode_entry
*wme
;
2017 if (ft
->wp
!= NULL
) {
2018 wme
= TAILQ_FIRST(&ft
->wp
->modes
);
2020 return (xstrdup(wme
->mode
->name
));
2026 /* Callback for pane_path. */
2028 format_cb_pane_path(struct format_tree
*ft
)
2030 if (ft
->wp
!= NULL
) {
2031 if (ft
->wp
->base
.path
== NULL
)
2032 return (xstrdup(""));
2033 return (xstrdup(ft
->wp
->base
.path
));
2038 /* Callback for pane_pid. */
2040 format_cb_pane_pid(struct format_tree
*ft
)
2043 return (format_printf("%ld", (long)ft
->wp
->pid
));
2047 /* Callback for pane_pipe. */
2049 format_cb_pane_pipe(struct format_tree
*ft
)
2051 if (ft
->wp
!= NULL
) {
2052 if (ft
->wp
->pipe_fd
!= -1)
2053 return (xstrdup("1"));
2054 return (xstrdup("0"));
2059 /* Callback for pane_right. */
2061 format_cb_pane_right(struct format_tree
*ft
)
2064 return (format_printf("%u", ft
->wp
->xoff
+ ft
->wp
->sx
- 1));
2068 /* Callback for pane_search_string. */
2070 format_cb_pane_search_string(struct format_tree
*ft
)
2072 if (ft
->wp
!= NULL
) {
2073 if (ft
->wp
->searchstr
== NULL
)
2074 return (xstrdup(""));
2075 return (xstrdup(ft
->wp
->searchstr
));
2080 /* Callback for pane_synchronized. */
2082 format_cb_pane_synchronized(struct format_tree
*ft
)
2084 if (ft
->wp
!= NULL
) {
2085 if (options_get_number(ft
->wp
->options
, "synchronize-panes"))
2086 return (xstrdup("1"));
2087 return (xstrdup("0"));
2092 /* Callback for pane_title. */
2094 format_cb_pane_title(struct format_tree
*ft
)
2097 return (xstrdup(ft
->wp
->base
.title
));
2101 /* Callback for pane_top. */
2103 format_cb_pane_top(struct format_tree
*ft
)
2106 return (format_printf("%u", ft
->wp
->yoff
));
2110 /* Callback for pane_tty. */
2112 format_cb_pane_tty(struct format_tree
*ft
)
2115 return (xstrdup(ft
->wp
->tty
));
2119 /* Callback for pane_width. */
2121 format_cb_pane_width(struct format_tree
*ft
)
2124 return (format_printf("%u", ft
->wp
->sx
));
2128 /* Callback for scroll_region_lower. */
2130 format_cb_scroll_region_lower(struct format_tree
*ft
)
2133 return (format_printf("%u", ft
->wp
->base
.rlower
));
2137 /* Callback for scroll_region_upper. */
2139 format_cb_scroll_region_upper(struct format_tree
*ft
)
2142 return (format_printf("%u", ft
->wp
->base
.rupper
));
2146 /* Callback for server_sessions. */
2148 format_cb_server_sessions(__unused
struct format_tree
*ft
)
2153 RB_FOREACH(s
, sessions
, &sessions
)
2155 return (format_printf("%u", n
));
2158 /* Callback for session_attached. */
2160 format_cb_session_attached(struct format_tree
*ft
)
2163 return (format_printf("%u", ft
->s
->attached
));
2167 /* Callback for session_format. */
2169 format_cb_session_format(struct format_tree
*ft
)
2171 if (ft
->type
== FORMAT_TYPE_SESSION
)
2172 return (xstrdup("1"));
2173 return (xstrdup("0"));
2176 /* Callback for session_group. */
2178 format_cb_session_group(struct format_tree
*ft
)
2180 struct session_group
*sg
;
2182 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2183 return (xstrdup(sg
->name
));
2187 /* Callback for session_group_attached. */
2189 format_cb_session_group_attached(struct format_tree
*ft
)
2191 struct session_group
*sg
;
2193 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2194 return (format_printf("%u", session_group_attached_count (sg
)));
2198 /* Callback for session_group_many_attached. */
2200 format_cb_session_group_many_attached(struct format_tree
*ft
)
2202 struct session_group
*sg
;
2204 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
) {
2205 if (session_group_attached_count (sg
) > 1)
2206 return (xstrdup("1"));
2207 return (xstrdup("0"));
2212 /* Callback for session_group_size. */
2214 format_cb_session_group_size(struct format_tree
*ft
)
2216 struct session_group
*sg
;
2218 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2219 return (format_printf("%u", session_group_count (sg
)));
2223 /* Callback for session_grouped. */
2225 format_cb_session_grouped(struct format_tree
*ft
)
2227 if (ft
->s
!= NULL
) {
2228 if (session_group_contains(ft
->s
) != NULL
)
2229 return (xstrdup("1"));
2230 return (xstrdup("0"));
2235 /* Callback for session_id. */
2237 format_cb_session_id(struct format_tree
*ft
)
2240 return (format_printf("$%u", ft
->s
->id
));
2244 /* Callback for session_many_attached. */
2246 format_cb_session_many_attached(struct format_tree
*ft
)
2248 if (ft
->s
!= NULL
) {
2249 if (ft
->s
->attached
> 1)
2250 return (xstrdup("1"));
2251 return (xstrdup("0"));
2256 /* Callback for session_marked. */
2258 format_cb_session_marked(struct format_tree
*ft
)
2260 if (ft
->s
!= NULL
) {
2261 if (server_check_marked() && marked_pane
.s
== ft
->s
)
2262 return (xstrdup("1"));
2263 return (xstrdup("0"));
2268 /* Callback for session_name. */
2270 format_cb_session_name(struct format_tree
*ft
)
2273 return (xstrdup(ft
->s
->name
));
2277 /* Callback for session_path. */
2279 format_cb_session_path(struct format_tree
*ft
)
2282 return (xstrdup(ft
->s
->cwd
));
2286 /* Callback for session_windows. */
2288 format_cb_session_windows(struct format_tree
*ft
)
2291 return (format_printf("%u", winlink_count(&ft
->s
->windows
)));
2295 /* Callback for socket_path. */
2297 format_cb_socket_path(__unused
struct format_tree
*ft
)
2299 return (xstrdup(socket_path
));
2302 /* Callback for version. */
2304 format_cb_version(__unused
struct format_tree
*ft
)
2306 return (xstrdup(getversion()));
2309 /* Callback for active_window_index. */
2311 format_cb_active_window_index(struct format_tree
*ft
)
2314 return (format_printf("%u", ft
->s
->curw
->idx
));
2318 /* Callback for last_window_index. */
2320 format_cb_last_window_index(struct format_tree
*ft
)
2324 if (ft
->s
!= NULL
) {
2325 wl
= RB_MAX(winlinks
, &ft
->s
->windows
);
2326 return (format_printf("%u", wl
->idx
));
2331 /* Callback for window_active. */
2333 format_cb_window_active(struct format_tree
*ft
)
2335 if (ft
->wl
!= NULL
) {
2336 if (ft
->wl
== ft
->wl
->session
->curw
)
2337 return (xstrdup("1"));
2338 return (xstrdup("0"));
2343 /* Callback for window_activity_flag. */
2345 format_cb_window_activity_flag(struct format_tree
*ft
)
2347 if (ft
->wl
!= NULL
) {
2348 if (ft
->wl
->flags
& WINLINK_ACTIVITY
)
2349 return (xstrdup("1"));
2350 return (xstrdup("0"));
2355 /* Callback for window_bell_flag. */
2357 format_cb_window_bell_flag(struct format_tree
*ft
)
2359 if (ft
->wl
!= NULL
) {
2360 if (ft
->wl
->flags
& WINLINK_BELL
)
2361 return (xstrdup("1"));
2362 return (xstrdup("0"));
2367 /* Callback for window_bigger. */
2369 format_cb_window_bigger(struct format_tree
*ft
)
2371 u_int ox
, oy
, sx
, sy
;
2373 if (ft
->c
!= NULL
) {
2374 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2375 return (xstrdup("1"));
2376 return (xstrdup("0"));
2381 /* Callback for window_cell_height. */
2383 format_cb_window_cell_height(struct format_tree
*ft
)
2386 return (format_printf("%u", ft
->w
->ypixel
));
2390 /* Callback for window_cell_width. */
2392 format_cb_window_cell_width(struct format_tree
*ft
)
2395 return (format_printf("%u", ft
->w
->xpixel
));
2399 /* Callback for window_end_flag. */
2401 format_cb_window_end_flag(struct format_tree
*ft
)
2403 if (ft
->wl
!= NULL
) {
2404 if (ft
->wl
== RB_MAX(winlinks
, &ft
->wl
->session
->windows
))
2405 return (xstrdup("1"));
2406 return (xstrdup("0"));
2411 /* Callback for window_flags. */
2413 format_cb_window_flags(struct format_tree
*ft
)
2416 return (xstrdup(window_printable_flags(ft
->wl
, 1)));
2420 /* Callback for window_format. */
2422 format_cb_window_format(struct format_tree
*ft
)
2424 if (ft
->type
== FORMAT_TYPE_WINDOW
)
2425 return (xstrdup("1"));
2426 return (xstrdup("0"));
2429 /* Callback for window_height. */
2431 format_cb_window_height(struct format_tree
*ft
)
2434 return (format_printf("%u", ft
->w
->sy
));
2438 /* Callback for window_id. */
2440 format_cb_window_id(struct format_tree
*ft
)
2443 return (format_printf("@%u", ft
->w
->id
));
2447 /* Callback for window_index. */
2449 format_cb_window_index(struct format_tree
*ft
)
2452 return (format_printf("%d", ft
->wl
->idx
));
2456 /* Callback for window_last_flag. */
2458 format_cb_window_last_flag(struct format_tree
*ft
)
2460 if (ft
->wl
!= NULL
) {
2461 if (ft
->wl
== TAILQ_FIRST(&ft
->wl
->session
->lastw
))
2462 return (xstrdup("1"));
2463 return (xstrdup("0"));
2468 /* Callback for window_linked. */
2470 format_cb_window_linked(struct format_tree
*ft
)
2472 if (ft
->wl
!= NULL
) {
2473 if (session_is_linked(ft
->wl
->session
, ft
->wl
->window
))
2474 return (xstrdup("1"));
2475 return (xstrdup("0"));
2480 /* Callback for window_linked_sessions. */
2482 format_cb_window_linked_sessions(struct format_tree
*ft
)
2485 return (format_printf("%u", ft
->wl
->window
->references
));
2489 /* Callback for window_marked_flag. */
2491 format_cb_window_marked_flag(struct format_tree
*ft
)
2493 if (ft
->wl
!= NULL
) {
2494 if (server_check_marked() && marked_pane
.wl
== ft
->wl
)
2495 return (xstrdup("1"));
2496 return (xstrdup("0"));
2501 /* Callback for window_name. */
2503 format_cb_window_name(struct format_tree
*ft
)
2506 return (format_printf("%s", ft
->w
->name
));
2510 /* Callback for window_offset_x. */
2512 format_cb_window_offset_x(struct format_tree
*ft
)
2514 u_int ox
, oy
, sx
, sy
;
2516 if (ft
->c
!= NULL
) {
2517 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2518 return (format_printf("%u", ox
));
2524 /* Callback for window_offset_y. */
2526 format_cb_window_offset_y(struct format_tree
*ft
)
2528 u_int ox
, oy
, sx
, sy
;
2530 if (ft
->c
!= NULL
) {
2531 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2532 return (format_printf("%u", oy
));
2538 /* Callback for window_panes. */
2540 format_cb_window_panes(struct format_tree
*ft
)
2543 return (format_printf("%u", window_count_panes(ft
->w
)));
2547 /* Callback for window_raw_flags. */
2549 format_cb_window_raw_flags(struct format_tree
*ft
)
2552 return (xstrdup(window_printable_flags(ft
->wl
, 0)));
2556 /* Callback for window_silence_flag. */
2558 format_cb_window_silence_flag(struct format_tree
*ft
)
2560 if (ft
->wl
!= NULL
) {
2561 if (ft
->wl
->flags
& WINLINK_SILENCE
)
2562 return (xstrdup("1"));
2563 return (xstrdup("0"));
2568 /* Callback for window_start_flag. */
2570 format_cb_window_start_flag(struct format_tree
*ft
)
2572 if (ft
->wl
!= NULL
) {
2573 if (ft
->wl
== RB_MIN(winlinks
, &ft
->wl
->session
->windows
))
2574 return (xstrdup("1"));
2575 return (xstrdup("0"));
2580 /* Callback for window_width. */
2582 format_cb_window_width(struct format_tree
*ft
)
2585 return (format_printf("%u", ft
->w
->sx
));
2589 /* Callback for window_zoomed_flag. */
2591 format_cb_window_zoomed_flag(struct format_tree
*ft
)
2593 if (ft
->w
!= NULL
) {
2594 if (ft
->w
->flags
& WINDOW_ZOOMED
)
2595 return (xstrdup("1"));
2596 return (xstrdup("0"));
2601 /* Callback for wrap_flag. */
2603 format_cb_wrap_flag(struct format_tree
*ft
)
2605 if (ft
->wp
!= NULL
) {
2606 if (ft
->wp
->base
.mode
& MODE_WRAP
)
2607 return (xstrdup("1"));
2608 return (xstrdup("0"));
2613 /* Callback for buffer_created. */
2615 format_cb_buffer_created(struct format_tree
*ft
)
2617 static struct timeval tv
;
2619 if (ft
->pb
!= NULL
) {
2621 tv
.tv_sec
= paste_buffer_created(ft
->pb
);
2627 /* Callback for client_activity. */
2629 format_cb_client_activity(struct format_tree
*ft
)
2632 return (&ft
->c
->activity_time
);
2636 /* Callback for client_created. */
2638 format_cb_client_created(struct format_tree
*ft
)
2641 return (&ft
->c
->creation_time
);
2645 /* Callback for session_activity. */
2647 format_cb_session_activity(struct format_tree
*ft
)
2650 return (&ft
->s
->activity_time
);
2654 /* Callback for session_created. */
2656 format_cb_session_created(struct format_tree
*ft
)
2659 return (&ft
->s
->creation_time
);
2663 /* Callback for session_last_attached. */
2665 format_cb_session_last_attached(struct format_tree
*ft
)
2668 return (&ft
->s
->last_attached_time
);
2672 /* Callback for start_time. */
2674 format_cb_start_time(__unused
struct format_tree
*ft
)
2676 return (&start_time
);
2679 /* Callback for window_activity. */
2681 format_cb_window_activity(struct format_tree
*ft
)
2684 return (&ft
->w
->activity_time
);
2688 /* Callback for buffer_mode_format, */
2690 format_cb_buffer_mode_format(__unused
struct format_tree
*ft
)
2692 return (xstrdup(window_buffer_mode
.default_format
));
2695 /* Callback for client_mode_format, */
2697 format_cb_client_mode_format(__unused
struct format_tree
*ft
)
2699 return (xstrdup(window_client_mode
.default_format
));
2702 /* Callback for tree_mode_format, */
2704 format_cb_tree_mode_format(__unused
struct format_tree
*ft
)
2706 return (xstrdup(window_tree_mode
.default_format
));
2709 /* Callback for uid. */
2711 format_cb_uid(__unused
struct format_tree
*ft
)
2713 return (format_printf("%ld", (long)getuid()));
2716 /* Callback for user. */
2718 format_cb_user(__unused
struct format_tree
*ft
)
2722 if ((pw
= getpwuid(getuid())) != NULL
)
2723 return (xstrdup(pw
->pw_name
));
2727 /* Format table type. */
2728 enum format_table_type
{
2729 FORMAT_TABLE_STRING
,
2733 /* Format table entry. */
2734 struct format_table_entry
{
2736 enum format_table_type type
;
2741 * Format table. Default format variables (that are almost always in the tree
2742 * and where the value is expanded by a callback in this file) are listed here.
2743 * Only variables which are added by the caller go into the tree.
2745 static const struct format_table_entry format_table
[] = {
2746 { "active_window_index", FORMAT_TABLE_STRING
,
2747 format_cb_active_window_index
2749 { "alternate_on", FORMAT_TABLE_STRING
,
2750 format_cb_alternate_on
2752 { "alternate_saved_x", FORMAT_TABLE_STRING
,
2753 format_cb_alternate_saved_x
2755 { "alternate_saved_y", FORMAT_TABLE_STRING
,
2756 format_cb_alternate_saved_y
2758 { "buffer_created", FORMAT_TABLE_TIME
,
2759 format_cb_buffer_created
2761 { "buffer_mode_format", FORMAT_TABLE_STRING
,
2762 format_cb_buffer_mode_format
2764 { "buffer_name", FORMAT_TABLE_STRING
,
2765 format_cb_buffer_name
2767 { "buffer_sample", FORMAT_TABLE_STRING
,
2768 format_cb_buffer_sample
2770 { "buffer_size", FORMAT_TABLE_STRING
,
2771 format_cb_buffer_size
2773 { "client_activity", FORMAT_TABLE_TIME
,
2774 format_cb_client_activity
2776 { "client_cell_height", FORMAT_TABLE_STRING
,
2777 format_cb_client_cell_height
2779 { "client_cell_width", FORMAT_TABLE_STRING
,
2780 format_cb_client_cell_width
2782 { "client_control_mode", FORMAT_TABLE_STRING
,
2783 format_cb_client_control_mode
2785 { "client_created", FORMAT_TABLE_TIME
,
2786 format_cb_client_created
2788 { "client_discarded", FORMAT_TABLE_STRING
,
2789 format_cb_client_discarded
2791 { "client_flags", FORMAT_TABLE_STRING
,
2792 format_cb_client_flags
2794 { "client_height", FORMAT_TABLE_STRING
,
2795 format_cb_client_height
2797 { "client_key_table", FORMAT_TABLE_STRING
,
2798 format_cb_client_key_table
2800 { "client_last_session", FORMAT_TABLE_STRING
,
2801 format_cb_client_last_session
2803 { "client_mode_format", FORMAT_TABLE_STRING
,
2804 format_cb_client_mode_format
2806 { "client_name", FORMAT_TABLE_STRING
,
2807 format_cb_client_name
2809 { "client_pid", FORMAT_TABLE_STRING
,
2810 format_cb_client_pid
2812 { "client_prefix", FORMAT_TABLE_STRING
,
2813 format_cb_client_prefix
2815 { "client_readonly", FORMAT_TABLE_STRING
,
2816 format_cb_client_readonly
2818 { "client_session", FORMAT_TABLE_STRING
,
2819 format_cb_client_session
2821 { "client_termfeatures", FORMAT_TABLE_STRING
,
2822 format_cb_client_termfeatures
2824 { "client_termname", FORMAT_TABLE_STRING
,
2825 format_cb_client_termname
2827 { "client_termtype", FORMAT_TABLE_STRING
,
2828 format_cb_client_termtype
2830 { "client_tty", FORMAT_TABLE_STRING
,
2831 format_cb_client_tty
2833 { "client_uid", FORMAT_TABLE_STRING
,
2834 format_cb_client_uid
2836 { "client_user", FORMAT_TABLE_STRING
,
2837 format_cb_client_user
2839 { "client_utf8", FORMAT_TABLE_STRING
,
2840 format_cb_client_utf8
2842 { "client_width", FORMAT_TABLE_STRING
,
2843 format_cb_client_width
2845 { "client_written", FORMAT_TABLE_STRING
,
2846 format_cb_client_written
2848 { "config_files", FORMAT_TABLE_STRING
,
2849 format_cb_config_files
2851 { "cursor_character", FORMAT_TABLE_STRING
,
2852 format_cb_cursor_character
2854 { "cursor_flag", FORMAT_TABLE_STRING
,
2855 format_cb_cursor_flag
2857 { "cursor_x", FORMAT_TABLE_STRING
,
2860 { "cursor_y", FORMAT_TABLE_STRING
,
2863 { "history_all_bytes", FORMAT_TABLE_STRING
,
2864 format_cb_history_all_bytes
2866 { "history_bytes", FORMAT_TABLE_STRING
,
2867 format_cb_history_bytes
2869 { "history_limit", FORMAT_TABLE_STRING
,
2870 format_cb_history_limit
2872 { "history_size", FORMAT_TABLE_STRING
,
2873 format_cb_history_size
2875 { "host", FORMAT_TABLE_STRING
,
2878 { "host_short", FORMAT_TABLE_STRING
,
2879 format_cb_host_short
2881 { "insert_flag", FORMAT_TABLE_STRING
,
2882 format_cb_insert_flag
2884 { "keypad_cursor_flag", FORMAT_TABLE_STRING
,
2885 format_cb_keypad_cursor_flag
2887 { "keypad_flag", FORMAT_TABLE_STRING
,
2888 format_cb_keypad_flag
2890 { "last_window_index", FORMAT_TABLE_STRING
,
2891 format_cb_last_window_index
2893 { "mouse_all_flag", FORMAT_TABLE_STRING
,
2894 format_cb_mouse_all_flag
2896 { "mouse_any_flag", FORMAT_TABLE_STRING
,
2897 format_cb_mouse_any_flag
2899 { "mouse_button_flag", FORMAT_TABLE_STRING
,
2900 format_cb_mouse_button_flag
2902 { "mouse_hyperlink", FORMAT_TABLE_STRING
,
2903 format_cb_mouse_hyperlink
2905 { "mouse_line", FORMAT_TABLE_STRING
,
2906 format_cb_mouse_line
2908 { "mouse_pane", FORMAT_TABLE_STRING
,
2909 format_cb_mouse_pane
2911 { "mouse_sgr_flag", FORMAT_TABLE_STRING
,
2912 format_cb_mouse_sgr_flag
2914 { "mouse_standard_flag", FORMAT_TABLE_STRING
,
2915 format_cb_mouse_standard_flag
2917 { "mouse_status_line", FORMAT_TABLE_STRING
,
2918 format_cb_mouse_status_line
2920 { "mouse_status_range", FORMAT_TABLE_STRING
,
2921 format_cb_mouse_status_range
2923 { "mouse_utf8_flag", FORMAT_TABLE_STRING
,
2924 format_cb_mouse_utf8_flag
2926 { "mouse_word", FORMAT_TABLE_STRING
,
2927 format_cb_mouse_word
2929 { "mouse_x", FORMAT_TABLE_STRING
,
2932 { "mouse_y", FORMAT_TABLE_STRING
,
2935 { "next_session_id", FORMAT_TABLE_STRING
,
2936 format_cb_next_session_id
2938 { "origin_flag", FORMAT_TABLE_STRING
,
2939 format_cb_origin_flag
2941 { "pane_active", FORMAT_TABLE_STRING
,
2942 format_cb_pane_active
2944 { "pane_at_bottom", FORMAT_TABLE_STRING
,
2945 format_cb_pane_at_bottom
2947 { "pane_at_left", FORMAT_TABLE_STRING
,
2948 format_cb_pane_at_left
2950 { "pane_at_right", FORMAT_TABLE_STRING
,
2951 format_cb_pane_at_right
2953 { "pane_at_top", FORMAT_TABLE_STRING
,
2954 format_cb_pane_at_top
2956 { "pane_bg", FORMAT_TABLE_STRING
,
2959 { "pane_bottom", FORMAT_TABLE_STRING
,
2960 format_cb_pane_bottom
2962 { "pane_current_command", FORMAT_TABLE_STRING
,
2963 format_cb_current_command
2965 { "pane_current_path", FORMAT_TABLE_STRING
,
2966 format_cb_current_path
2968 { "pane_dead", FORMAT_TABLE_STRING
,
2971 { "pane_dead_signal", FORMAT_TABLE_STRING
,
2972 format_cb_pane_dead_signal
2974 { "pane_dead_status", FORMAT_TABLE_STRING
,
2975 format_cb_pane_dead_status
2977 { "pane_dead_time", FORMAT_TABLE_TIME
,
2978 format_cb_pane_dead_time
2980 { "pane_fg", FORMAT_TABLE_STRING
,
2983 { "pane_format", FORMAT_TABLE_STRING
,
2984 format_cb_pane_format
2986 { "pane_height", FORMAT_TABLE_STRING
,
2987 format_cb_pane_height
2989 { "pane_id", FORMAT_TABLE_STRING
,
2992 { "pane_in_mode", FORMAT_TABLE_STRING
,
2993 format_cb_pane_in_mode
2995 { "pane_index", FORMAT_TABLE_STRING
,
2996 format_cb_pane_index
2998 { "pane_input_off", FORMAT_TABLE_STRING
,
2999 format_cb_pane_input_off
3001 { "pane_last", FORMAT_TABLE_STRING
,
3004 { "pane_left", FORMAT_TABLE_STRING
,
3007 { "pane_marked", FORMAT_TABLE_STRING
,
3008 format_cb_pane_marked
3010 { "pane_marked_set", FORMAT_TABLE_STRING
,
3011 format_cb_pane_marked_set
3013 { "pane_mode", FORMAT_TABLE_STRING
,
3016 { "pane_path", FORMAT_TABLE_STRING
,
3019 { "pane_pid", FORMAT_TABLE_STRING
,
3022 { "pane_pipe", FORMAT_TABLE_STRING
,
3025 { "pane_right", FORMAT_TABLE_STRING
,
3026 format_cb_pane_right
3028 { "pane_search_string", FORMAT_TABLE_STRING
,
3029 format_cb_pane_search_string
3031 { "pane_start_command", FORMAT_TABLE_STRING
,
3032 format_cb_start_command
3034 { "pane_start_path", FORMAT_TABLE_STRING
,
3035 format_cb_start_path
3037 { "pane_synchronized", FORMAT_TABLE_STRING
,
3038 format_cb_pane_synchronized
3040 { "pane_tabs", FORMAT_TABLE_STRING
,
3043 { "pane_title", FORMAT_TABLE_STRING
,
3044 format_cb_pane_title
3046 { "pane_top", FORMAT_TABLE_STRING
,
3049 { "pane_tty", FORMAT_TABLE_STRING
,
3052 { "pane_unseen_changes", FORMAT_TABLE_STRING
,
3053 format_cb_pane_unseen_changes
3055 { "pane_width", FORMAT_TABLE_STRING
,
3056 format_cb_pane_width
3058 { "pid", FORMAT_TABLE_STRING
,
3061 { "scroll_region_lower", FORMAT_TABLE_STRING
,
3062 format_cb_scroll_region_lower
3064 { "scroll_region_upper", FORMAT_TABLE_STRING
,
3065 format_cb_scroll_region_upper
3067 { "server_sessions", FORMAT_TABLE_STRING
,
3068 format_cb_server_sessions
3070 { "session_activity", FORMAT_TABLE_TIME
,
3071 format_cb_session_activity
3073 { "session_alerts", FORMAT_TABLE_STRING
,
3074 format_cb_session_alerts
3076 { "session_attached", FORMAT_TABLE_STRING
,
3077 format_cb_session_attached
3079 { "session_attached_list", FORMAT_TABLE_STRING
,
3080 format_cb_session_attached_list
3082 { "session_created", FORMAT_TABLE_TIME
,
3083 format_cb_session_created
3085 { "session_format", FORMAT_TABLE_STRING
,
3086 format_cb_session_format
3088 { "session_group", FORMAT_TABLE_STRING
,
3089 format_cb_session_group
3091 { "session_group_attached", FORMAT_TABLE_STRING
,
3092 format_cb_session_group_attached
3094 { "session_group_attached_list", FORMAT_TABLE_STRING
,
3095 format_cb_session_group_attached_list
3097 { "session_group_list", FORMAT_TABLE_STRING
,
3098 format_cb_session_group_list
3100 { "session_group_many_attached", FORMAT_TABLE_STRING
,
3101 format_cb_session_group_many_attached
3103 { "session_group_size", FORMAT_TABLE_STRING
,
3104 format_cb_session_group_size
3106 { "session_grouped", FORMAT_TABLE_STRING
,
3107 format_cb_session_grouped
3109 { "session_id", FORMAT_TABLE_STRING
,
3110 format_cb_session_id
3112 { "session_last_attached", FORMAT_TABLE_TIME
,
3113 format_cb_session_last_attached
3115 { "session_many_attached", FORMAT_TABLE_STRING
,
3116 format_cb_session_many_attached
3118 { "session_marked", FORMAT_TABLE_STRING
,
3119 format_cb_session_marked
,
3121 { "session_name", FORMAT_TABLE_STRING
,
3122 format_cb_session_name
3124 { "session_path", FORMAT_TABLE_STRING
,
3125 format_cb_session_path
3127 { "session_stack", FORMAT_TABLE_STRING
,
3128 format_cb_session_stack
3130 { "session_windows", FORMAT_TABLE_STRING
,
3131 format_cb_session_windows
3133 { "socket_path", FORMAT_TABLE_STRING
,
3134 format_cb_socket_path
3136 { "start_time", FORMAT_TABLE_TIME
,
3137 format_cb_start_time
3139 { "tree_mode_format", FORMAT_TABLE_STRING
,
3140 format_cb_tree_mode_format
3142 { "uid", FORMAT_TABLE_STRING
,
3145 { "user", FORMAT_TABLE_STRING
,
3148 { "version", FORMAT_TABLE_STRING
,
3151 { "window_active", FORMAT_TABLE_STRING
,
3152 format_cb_window_active
3154 { "window_active_clients", FORMAT_TABLE_STRING
,
3155 format_cb_window_active_clients
3157 { "window_active_clients_list", FORMAT_TABLE_STRING
,
3158 format_cb_window_active_clients_list
3160 { "window_active_sessions", FORMAT_TABLE_STRING
,
3161 format_cb_window_active_sessions
3163 { "window_active_sessions_list", FORMAT_TABLE_STRING
,
3164 format_cb_window_active_sessions_list
3166 { "window_activity", FORMAT_TABLE_TIME
,
3167 format_cb_window_activity
3169 { "window_activity_flag", FORMAT_TABLE_STRING
,
3170 format_cb_window_activity_flag
3172 { "window_bell_flag", FORMAT_TABLE_STRING
,
3173 format_cb_window_bell_flag
3175 { "window_bigger", FORMAT_TABLE_STRING
,
3176 format_cb_window_bigger
3178 { "window_cell_height", FORMAT_TABLE_STRING
,
3179 format_cb_window_cell_height
3181 { "window_cell_width", FORMAT_TABLE_STRING
,
3182 format_cb_window_cell_width
3184 { "window_end_flag", FORMAT_TABLE_STRING
,
3185 format_cb_window_end_flag
3187 { "window_flags", FORMAT_TABLE_STRING
,
3188 format_cb_window_flags
3190 { "window_format", FORMAT_TABLE_STRING
,
3191 format_cb_window_format
3193 { "window_height", FORMAT_TABLE_STRING
,
3194 format_cb_window_height
3196 { "window_id", FORMAT_TABLE_STRING
,
3199 { "window_index", FORMAT_TABLE_STRING
,
3200 format_cb_window_index
3202 { "window_last_flag", FORMAT_TABLE_STRING
,
3203 format_cb_window_last_flag
3205 { "window_layout", FORMAT_TABLE_STRING
,
3206 format_cb_window_layout
3208 { "window_linked", FORMAT_TABLE_STRING
,
3209 format_cb_window_linked
3211 { "window_linked_sessions", FORMAT_TABLE_STRING
,
3212 format_cb_window_linked_sessions
3214 { "window_linked_sessions_list", FORMAT_TABLE_STRING
,
3215 format_cb_window_linked_sessions_list
3217 { "window_marked_flag", FORMAT_TABLE_STRING
,
3218 format_cb_window_marked_flag
3220 { "window_name", FORMAT_TABLE_STRING
,
3221 format_cb_window_name
3223 { "window_offset_x", FORMAT_TABLE_STRING
,
3224 format_cb_window_offset_x
3226 { "window_offset_y", FORMAT_TABLE_STRING
,
3227 format_cb_window_offset_y
3229 { "window_panes", FORMAT_TABLE_STRING
,
3230 format_cb_window_panes
3232 { "window_raw_flags", FORMAT_TABLE_STRING
,
3233 format_cb_window_raw_flags
3235 { "window_silence_flag", FORMAT_TABLE_STRING
,
3236 format_cb_window_silence_flag
3238 { "window_stack_index", FORMAT_TABLE_STRING
,
3239 format_cb_window_stack_index
3241 { "window_start_flag", FORMAT_TABLE_STRING
,
3242 format_cb_window_start_flag
3244 { "window_visible_layout", FORMAT_TABLE_STRING
,
3245 format_cb_window_visible_layout
3247 { "window_width", FORMAT_TABLE_STRING
,
3248 format_cb_window_width
3250 { "window_zoomed_flag", FORMAT_TABLE_STRING
,
3251 format_cb_window_zoomed_flag
3253 { "wrap_flag", FORMAT_TABLE_STRING
,
3258 /* Compare format table entries. */
3260 format_table_compare(const void *key0
, const void *entry0
)
3262 const char *key
= key0
;
3263 const struct format_table_entry
*entry
= entry0
;
3265 return (strcmp(key
, entry
->key
));
3268 /* Get a format callback. */
3269 static struct format_table_entry
*
3270 format_table_get(const char *key
)
3272 return (bsearch(key
, format_table
, nitems(format_table
),
3273 sizeof *format_table
, format_table_compare
));
3276 /* Merge one format tree into another. */
3278 format_merge(struct format_tree
*ft
, struct format_tree
*from
)
3280 struct format_entry
*fe
;
3282 RB_FOREACH(fe
, format_entry_tree
, &from
->tree
) {
3283 if (fe
->value
!= NULL
)
3284 format_add(ft
, fe
->key
, "%s", fe
->value
);
3288 /* Get format pane. */
3289 struct window_pane
*
3290 format_get_pane(struct format_tree
*ft
)
3295 /* Add item bits to tree. */
3297 format_create_add_item(struct format_tree
*ft
, struct cmdq_item
*item
)
3299 struct key_event
*event
= cmdq_get_event(item
);
3300 struct mouse_event
*m
= &event
->m
;
3302 cmdq_merge_formats(item
, ft
);
3303 memcpy(&ft
->m
, m
, sizeof ft
->m
);
3306 /* Create a new tree. */
3307 struct format_tree
*
3308 format_create(struct client
*c
, struct cmdq_item
*item
, int tag
, int flags
)
3310 struct format_tree
*ft
;
3312 ft
= xcalloc(1, sizeof *ft
);
3317 ft
->client
->references
++;
3325 format_create_add_item(ft
, item
);
3332 format_free(struct format_tree
*ft
)
3334 struct format_entry
*fe
, *fe1
;
3336 RB_FOREACH_SAFE(fe
, format_entry_tree
, &ft
->tree
, fe1
) {
3337 RB_REMOVE(format_entry_tree
, &ft
->tree
, fe
);
3343 if (ft
->client
!= NULL
)
3344 server_client_unref(ft
->client
);
3348 /* Log each format. */
3350 format_log_debug_cb(const char *key
, const char *value
, void *arg
)
3352 const char *prefix
= arg
;
3354 log_debug("%s: %s=%s", prefix
, key
, value
);
3357 /* Log a format tree. */
3359 format_log_debug(struct format_tree
*ft
, const char *prefix
)
3361 format_each(ft
, format_log_debug_cb
, (void *)prefix
);
3364 /* Walk each format. */
3366 format_each(struct format_tree
*ft
, void (*cb
)(const char *, const char *,
3369 const struct format_table_entry
*fte
;
3370 struct format_entry
*fe
;
3376 for (i
= 0; i
< nitems(format_table
); i
++) {
3377 fte
= &format_table
[i
];
3379 value
= fte
->cb(ft
);
3382 if (fte
->type
== FORMAT_TABLE_TIME
) {
3384 xsnprintf(s
, sizeof s
, "%lld", (long long)tv
->tv_sec
);
3385 cb(fte
->key
, s
, arg
);
3387 cb(fte
->key
, value
, arg
);
3391 RB_FOREACH(fe
, format_entry_tree
, &ft
->tree
) {
3392 if (fe
->time
!= 0) {
3393 xsnprintf(s
, sizeof s
, "%lld", (long long)fe
->time
);
3394 cb(fe
->key
, s
, arg
);
3396 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3397 fe
->value
= fe
->cb(ft
);
3398 if (fe
->value
== NULL
)
3399 fe
->value
= xstrdup("");
3401 cb(fe
->key
, fe
->value
, arg
);
3406 /* Add a key-value pair. */
3408 format_add(struct format_tree
*ft
, const char *key
, const char *fmt
, ...)
3410 struct format_entry
*fe
;
3411 struct format_entry
*fe_now
;
3414 fe
= xmalloc(sizeof *fe
);
3415 fe
->key
= xstrdup(key
);
3417 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3418 if (fe_now
!= NULL
) {
3421 free(fe_now
->value
);
3429 xvasprintf(&fe
->value
, fmt
, ap
);
3433 /* Add a key and time. */
3435 format_add_tv(struct format_tree
*ft
, const char *key
, struct timeval
*tv
)
3437 struct format_entry
*fe
, *fe_now
;
3439 fe
= xmalloc(sizeof *fe
);
3440 fe
->key
= xstrdup(key
);
3442 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3443 if (fe_now
!= NULL
) {
3446 free(fe_now
->value
);
3451 fe
->time
= tv
->tv_sec
;
3456 /* Add a key and function. */
3458 format_add_cb(struct format_tree
*ft
, const char *key
, format_cb cb
)
3460 struct format_entry
*fe
;
3461 struct format_entry
*fe_now
;
3463 fe
= xmalloc(sizeof *fe
);
3464 fe
->key
= xstrdup(key
);
3466 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3467 if (fe_now
!= NULL
) {
3470 free(fe_now
->value
);
3480 /* Quote shell special characters in string. */
3482 format_quote_shell(const char *s
)
3487 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3488 for (cp
= s
; *cp
!= '\0'; cp
++) {
3489 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp
) != NULL
)
3497 /* Quote #s in string. */
3499 format_quote_style(const char *s
)
3504 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3505 for (cp
= s
; *cp
!= '\0'; cp
++) {
3514 /* Make a prettier time. */
3516 format_pretty_time(time_t t
, int seconds
)
3518 struct tm now_tm
, tm
;
3527 localtime_r(&now
, &now_tm
);
3528 localtime_r(&t
, &tm
);
3530 /* Last 24 hours. */
3531 if (age
< 24 * 3600) {
3533 strftime(s
, sizeof s
, "%H:%M:%S", &tm
);
3535 strftime(s
, sizeof s
, "%H:%M", &tm
);
3536 return (xstrdup(s
));
3539 /* This month or last 28 days. */
3540 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
== now_tm
.tm_mon
) ||
3541 age
< 28 * 24 * 3600) {
3542 strftime(s
, sizeof s
, "%a%d", &tm
);
3543 return (xstrdup(s
));
3546 /* Last 12 months. */
3547 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
< now_tm
.tm_mon
) ||
3548 (tm
.tm_year
== now_tm
.tm_year
- 1 && tm
.tm_mon
> now_tm
.tm_mon
)) {
3549 strftime(s
, sizeof s
, "%d%b", &tm
);
3550 return (xstrdup(s
));
3553 /* Older than that. */
3554 strftime(s
, sizeof s
, "%h%y", &tm
);
3555 return (xstrdup(s
));
3558 /* Find a format entry. */
3560 format_find(struct format_tree
*ft
, const char *key
, int modifiers
,
3561 const char *time_format
)
3563 struct format_table_entry
*fte
;
3565 struct format_entry
*fe
, fe_find
;
3566 struct environ_entry
*envent
;
3567 struct options_entry
*o
;
3569 char *found
= NULL
, *saved
, s
[512];
3574 o
= options_parse_get(global_options
, key
, &idx
, 0);
3575 if (o
== NULL
&& ft
->wp
!= NULL
)
3576 o
= options_parse_get(ft
->wp
->options
, key
, &idx
, 0);
3577 if (o
== NULL
&& ft
->w
!= NULL
)
3578 o
= options_parse_get(ft
->w
->options
, key
, &idx
, 0);
3580 o
= options_parse_get(global_w_options
, key
, &idx
, 0);
3581 if (o
== NULL
&& ft
->s
!= NULL
)
3582 o
= options_parse_get(ft
->s
->options
, key
, &idx
, 0);
3584 o
= options_parse_get(global_s_options
, key
, &idx
, 0);
3586 found
= options_to_string(o
, idx
, 1);
3590 fte
= format_table_get(key
);
3592 value
= fte
->cb(ft
);
3593 if (fte
->type
== FORMAT_TABLE_TIME
&& value
!= NULL
)
3594 t
= ((struct timeval
*)value
)->tv_sec
;
3599 fe_find
.key
= (char *)key
;
3600 fe
= RB_FIND(format_entry_tree
, &ft
->tree
, &fe_find
);
3602 if (fe
->time
!= 0) {
3606 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3607 fe
->value
= fe
->cb(ft
);
3608 if (fe
->value
== NULL
)
3609 fe
->value
= xstrdup("");
3611 found
= xstrdup(fe
->value
);
3615 if (~modifiers
& FORMAT_TIMESTRING
) {
3618 envent
= environ_find(ft
->s
->environ
, key
);
3620 envent
= environ_find(global_environ
, key
);
3621 if (envent
!= NULL
&& envent
->value
!= NULL
) {
3622 found
= xstrdup(envent
->value
);
3630 if (modifiers
& FORMAT_TIMESTRING
) {
3631 if (t
== 0 && found
!= NULL
) {
3632 t
= strtonum(found
, 0, INT64_MAX
, &errstr
);
3639 if (modifiers
& FORMAT_PRETTY
)
3640 found
= format_pretty_time(t
, 0);
3642 if (time_format
!= NULL
) {
3643 localtime_r(&t
, &tm
);
3644 strftime(s
, sizeof s
, time_format
, &tm
);
3647 s
[strcspn(s
, "\n")] = '\0';
3655 xasprintf(&found
, "%lld", (long long)t
);
3656 else if (found
== NULL
)
3658 if (modifiers
& FORMAT_BASENAME
) {
3660 found
= xstrdup(basename(saved
));
3663 if (modifiers
& FORMAT_DIRNAME
) {
3665 found
= xstrdup(dirname(saved
));
3668 if (modifiers
& FORMAT_QUOTE_SHELL
) {
3670 found
= format_quote_shell(saved
);
3673 if (modifiers
& FORMAT_QUOTE_STYLE
) {
3675 found
= format_quote_style(saved
);
3681 /* Unescape escaped characters. */
3683 format_unescape(const char *s
)
3688 cp
= out
= xmalloc(strlen(s
) + 1);
3689 for (; *s
!= '\0'; s
++) {
3690 if (*s
== '#' && s
[1] == '{')
3692 if (brackets
== 0 &&
3694 strchr(",#{}:", s
[1]) != NULL
) {
3706 /* Remove escaped characters. */
3708 format_strip(const char *s
)
3713 cp
= out
= xmalloc(strlen(s
) + 1);
3714 for (; *s
!= '\0'; s
++) {
3715 if (*s
== '#' && s
[1] == '{')
3717 if (*s
== '#' && strchr(",#{}:", s
[1]) != NULL
) {
3730 /* Skip until end. */
3732 format_skip(const char *s
, const char *end
)
3736 for (; *s
!= '\0'; s
++) {
3737 if (*s
== '#' && s
[1] == '{')
3741 strchr(",#{}:", s
[1]) != NULL
) {
3747 if (strchr(end
, *s
) != NULL
&& brackets
== 0)
3755 /* Return left and right alternatives separated by commas. */
3757 format_choose(struct format_expand_state
*es
, const char *s
, char **left
,
3758 char **right
, int expand
)
3761 char *left0
, *right0
;
3763 cp
= format_skip(s
, ",");
3766 left0
= xstrndup(s
, cp
- s
);
3767 right0
= xstrdup(cp
+ 1);
3770 *left
= format_expand1(es
, left0
);
3772 *right
= format_expand1(es
, right0
);
3783 format_true(const char *s
)
3785 if (s
!= NULL
&& *s
!= '\0' && (s
[0] != '0' || s
[1] != '\0'))
3790 /* Check if modifier end. */
3792 format_is_end(char c
)
3794 return (c
== ';' || c
== ':');
3797 /* Add to modifier list. */
3799 format_add_modifier(struct format_modifier
**list
, u_int
*count
,
3800 const char *c
, size_t n
, char **argv
, int argc
)
3802 struct format_modifier
*fm
;
3804 *list
= xreallocarray(*list
, (*count
) + 1, sizeof **list
);
3805 fm
= &(*list
)[(*count
)++];
3807 memcpy(fm
->modifier
, c
, n
);
3808 fm
->modifier
[n
] = '\0';
3815 /* Free modifier list. */
3817 format_free_modifiers(struct format_modifier
*list
, u_int count
)
3821 for (i
= 0; i
< count
; i
++)
3822 cmd_free_argv(list
[i
].argc
, list
[i
].argv
);
3826 /* Build modifier list. */
3827 static struct format_modifier
*
3828 format_build_modifiers(struct format_expand_state
*es
, const char **s
,
3831 const char *cp
= *s
, *end
;
3832 struct format_modifier
*list
= NULL
;
3833 char c
, last
[] = "X;:", **argv
, *value
;
3837 * Modifiers are a ; separated list of the forms:
3838 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3849 while (*cp
!= '\0' && *cp
!= ':') {
3850 /* Skip any separator character. */
3854 /* Check single character modifiers with no arguments. */
3855 if (strchr("labcdnwETSWPL<>", cp
[0]) != NULL
&&
3856 format_is_end(cp
[1])) {
3857 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3862 /* Then try double character with no arguments. */
3863 if ((memcmp("||", cp
, 2) == 0 ||
3864 memcmp("&&", cp
, 2) == 0 ||
3865 memcmp("!=", cp
, 2) == 0 ||
3866 memcmp("==", cp
, 2) == 0 ||
3867 memcmp("<=", cp
, 2) == 0 ||
3868 memcmp(">=", cp
, 2) == 0) &&
3869 format_is_end(cp
[2])) {
3870 format_add_modifier(&list
, count
, cp
, 2, NULL
, 0);
3875 /* Now try single character with arguments. */
3876 if (strchr("mCNst=peq", cp
[0]) == NULL
)
3880 /* No arguments provided. */
3881 if (format_is_end(cp
[1])) {
3882 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3889 /* Single argument with no wrapper character. */
3890 if (!ispunct((u_char
)cp
[1]) || cp
[1] == '-') {
3891 end
= format_skip(cp
+ 1, ":;");
3895 argv
= xcalloc(1, sizeof *argv
);
3896 value
= xstrndup(cp
+ 1, end
- (cp
+ 1));
3897 argv
[0] = format_expand1(es
, value
);
3901 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3906 /* Multiple arguments with a wrapper character. */
3910 if (cp
[0] == last
[0] && format_is_end(cp
[1])) {
3914 end
= format_skip(cp
+ 1, last
);
3919 argv
= xreallocarray(argv
, argc
+ 1, sizeof *argv
);
3920 value
= xstrndup(cp
, end
- cp
);
3921 argv
[argc
++] = format_expand1(es
, value
);
3925 } while (!format_is_end(cp
[0]));
3926 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3929 format_free_modifiers(list
, *count
);
3937 /* Match against an fnmatch(3) pattern or regular expression. */
3939 format_match(struct format_modifier
*fm
, const char *pattern
, const char *text
)
3947 if (strchr(s
, 'r') == NULL
) {
3948 if (strchr(s
, 'i') != NULL
)
3949 flags
|= FNM_CASEFOLD
;
3950 if (fnmatch(pattern
, text
, flags
) != 0)
3951 return (xstrdup("0"));
3953 flags
= REG_EXTENDED
|REG_NOSUB
;
3954 if (strchr(s
, 'i') != NULL
)
3956 if (regcomp(&r
, pattern
, flags
) != 0)
3957 return (xstrdup("0"));
3958 if (regexec(&r
, text
, 0, NULL
, 0) != 0) {
3960 return (xstrdup("0"));
3964 return (xstrdup("1"));
3967 /* Perform substitution in string. */
3969 format_sub(struct format_modifier
*fm
, const char *text
, const char *pattern
,
3973 int flags
= REG_EXTENDED
;
3975 if (fm
->argc
>= 3 && strchr(fm
->argv
[2], 'i') != NULL
)
3977 value
= regsub(pattern
, with
, text
, flags
);
3979 return (xstrdup(text
));
3983 /* Search inside pane. */
3985 format_search(struct format_modifier
*fm
, struct window_pane
*wp
, const char *s
)
3987 int ignore
= 0, regex
= 0;
3990 if (fm
->argc
>= 1) {
3991 if (strchr(fm
->argv
[0], 'i') != NULL
)
3993 if (strchr(fm
->argv
[0], 'r') != NULL
)
3996 xasprintf(&value
, "%u", window_pane_search(wp
, s
, regex
, ignore
));
4000 /* Does session name exist? */
4002 format_session_name(struct format_expand_state
*es
, const char *fmt
)
4007 name
= format_expand1(es
, fmt
);
4008 RB_FOREACH(s
, sessions
, &sessions
) {
4009 if (strcmp(s
->name
, name
) == 0) {
4011 return (xstrdup("1"));
4015 return (xstrdup("0"));
4018 /* Loop over sessions. */
4020 format_loop_sessions(struct format_expand_state
*es
, const char *fmt
)
4022 struct format_tree
*ft
= es
->ft
;
4023 struct client
*c
= ft
->client
;
4024 struct cmdq_item
*item
= ft
->item
;
4025 struct format_tree
*nft
;
4026 struct format_expand_state next
;
4027 char *expanded
, *value
;
4031 value
= xcalloc(1, 1);
4034 RB_FOREACH(s
, sessions
, &sessions
) {
4035 format_log(es
, "session loop: $%u", s
->id
);
4036 nft
= format_create(c
, item
, FORMAT_NONE
, ft
->flags
);
4037 format_defaults(nft
, ft
->c
, s
, NULL
, NULL
);
4038 format_copy_state(&next
, es
, 0);
4040 expanded
= format_expand1(&next
, fmt
);
4041 format_free(next
.ft
);
4043 valuelen
+= strlen(expanded
);
4044 value
= xrealloc(value
, valuelen
);
4046 strlcat(value
, expanded
, valuelen
);
4053 /* Does window name exist? */
4055 format_window_name(struct format_expand_state
*es
, const char *fmt
)
4057 struct format_tree
*ft
= es
->ft
;
4061 if (ft
->s
== NULL
) {
4062 format_log(es
, "window name but no session");
4066 name
= format_expand1(es
, fmt
);
4067 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
4068 if (strcmp(wl
->window
->name
, name
) == 0) {
4070 return (xstrdup("1"));
4074 return (xstrdup("0"));
4077 /* Loop over windows. */
4079 format_loop_windows(struct format_expand_state
*es
, const char *fmt
)
4081 struct format_tree
*ft
= es
->ft
;
4082 struct client
*c
= ft
->client
;
4083 struct cmdq_item
*item
= ft
->item
;
4084 struct format_tree
*nft
;
4085 struct format_expand_state next
;
4086 char *all
, *active
, *use
, *expanded
, *value
;
4091 if (ft
->s
== NULL
) {
4092 format_log(es
, "window loop but no session");
4096 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4101 value
= xcalloc(1, 1);
4104 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
4106 format_log(es
, "window loop: %u @%u", wl
->idx
, w
->id
);
4107 if (active
!= NULL
&& wl
== ft
->s
->curw
)
4111 nft
= format_create(c
, item
, FORMAT_WINDOW
|w
->id
, ft
->flags
);
4112 format_defaults(nft
, ft
->c
, ft
->s
, wl
, NULL
);
4113 format_copy_state(&next
, es
, 0);
4115 expanded
= format_expand1(&next
, use
);
4118 valuelen
+= strlen(expanded
);
4119 value
= xrealloc(value
, valuelen
);
4121 strlcat(value
, expanded
, valuelen
);
4131 /* Loop over panes. */
4133 format_loop_panes(struct format_expand_state
*es
, const char *fmt
)
4135 struct format_tree
*ft
= es
->ft
;
4136 struct client
*c
= ft
->client
;
4137 struct cmdq_item
*item
= ft
->item
;
4138 struct format_tree
*nft
;
4139 struct format_expand_state next
;
4140 char *all
, *active
, *use
, *expanded
, *value
;
4142 struct window_pane
*wp
;
4144 if (ft
->w
== NULL
) {
4145 format_log(es
, "pane loop but no window");
4149 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4154 value
= xcalloc(1, 1);
4157 TAILQ_FOREACH(wp
, &ft
->w
->panes
, entry
) {
4158 format_log(es
, "pane loop: %%%u", wp
->id
);
4159 if (active
!= NULL
&& wp
== ft
->w
->active
)
4163 nft
= format_create(c
, item
, FORMAT_PANE
|wp
->id
, ft
->flags
);
4164 format_defaults(nft
, ft
->c
, ft
->s
, ft
->wl
, wp
);
4165 format_copy_state(&next
, es
, 0);
4167 expanded
= format_expand1(&next
, use
);
4170 valuelen
+= strlen(expanded
);
4171 value
= xrealloc(value
, valuelen
);
4173 strlcat(value
, expanded
, valuelen
);
4183 /* Loop over clients. */
4185 format_loop_clients(struct format_expand_state
*es
, const char *fmt
)
4187 struct format_tree
*ft
= es
->ft
;
4188 struct client
*c
= ft
->client
;
4189 struct cmdq_item
*item
= ft
->item
;
4190 struct format_tree
*nft
;
4191 struct format_expand_state next
;
4192 char *expanded
, *value
;
4195 value
= xcalloc(1, 1);
4198 TAILQ_FOREACH(c
, &clients
, entry
) {
4199 format_log(es
, "client loop: %s", c
->name
);
4200 nft
= format_create(c
, item
, 0, ft
->flags
);
4201 format_defaults(nft
, c
, ft
->s
, ft
->wl
, ft
->wp
);
4202 format_copy_state(&next
, es
, 0);
4204 expanded
= format_expand1(&next
, fmt
);
4207 valuelen
+= strlen(expanded
);
4208 value
= xrealloc(value
, valuelen
);
4210 strlcat(value
, expanded
, valuelen
);
4218 format_replace_expression(struct format_modifier
*mexp
,
4219 struct format_expand_state
*es
, const char *copy
)
4221 int argc
= mexp
->argc
;
4223 char *endch
, *value
, *left
= NULL
, *right
= NULL
;
4226 double mleft
, mright
, result
;
4237 LESS_THAN_EQUAL
} operator;
4239 if (strcmp(mexp
->argv
[0], "+") == 0)
4241 else if (strcmp(mexp
->argv
[0], "-") == 0)
4242 operator = SUBTRACT
;
4243 else if (strcmp(mexp
->argv
[0], "*") == 0)
4244 operator = MULTIPLY
;
4245 else if (strcmp(mexp
->argv
[0], "/") == 0)
4247 else if (strcmp(mexp
->argv
[0], "%") == 0 ||
4248 strcmp(mexp
->argv
[0], "m") == 0)
4250 else if (strcmp(mexp
->argv
[0], "==") == 0)
4252 else if (strcmp(mexp
->argv
[0], "!=") == 0)
4253 operator = NOT_EQUAL
;
4254 else if (strcmp(mexp
->argv
[0], ">") == 0)
4255 operator = GREATER_THAN
;
4256 else if (strcmp(mexp
->argv
[0], "<") == 0)
4257 operator = LESS_THAN
;
4258 else if (strcmp(mexp
->argv
[0], ">=") == 0)
4259 operator = GREATER_THAN_EQUAL
;
4260 else if (strcmp(mexp
->argv
[0], "<=") == 0)
4261 operator = LESS_THAN_EQUAL
;
4263 format_log(es
, "expression has no valid operator: '%s'",
4268 /* The second argument may be flags. */
4269 if (argc
>= 2 && strchr(mexp
->argv
[1], 'f') != NULL
) {
4274 /* The third argument may be precision. */
4276 prec
= strtonum(mexp
->argv
[2], INT_MIN
, INT_MAX
, &errstr
);
4277 if (errstr
!= NULL
) {
4278 format_log(es
, "expression precision %s: %s", errstr
,
4284 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4285 format_log(es
, "expression syntax error");
4289 mleft
= strtod(left
, &endch
);
4290 if (*endch
!= '\0') {
4291 format_log(es
, "expression left side is invalid: %s", left
);
4295 mright
= strtod(right
, &endch
);
4296 if (*endch
!= '\0') {
4297 format_log(es
, "expression right side is invalid: %s", right
);
4302 mleft
= (long long)mleft
;
4303 mright
= (long long)mright
;
4305 format_log(es
, "expression left side is: %.*f", prec
, mleft
);
4306 format_log(es
, "expression right side is: %.*f", prec
, mright
);
4310 result
= mleft
+ mright
;
4313 result
= mleft
- mright
;
4316 result
= mleft
* mright
;
4319 result
= mleft
/ mright
;
4322 result
= fmod(mleft
, mright
);
4325 result
= fabs(mleft
- mright
) < 1e-9;
4328 result
= fabs(mleft
- mright
) > 1e-9;
4331 result
= (mleft
> mright
);
4333 case GREATER_THAN_EQUAL
:
4334 result
= (mleft
>= mright
);
4337 result
= (mleft
< mright
);
4339 case LESS_THAN_EQUAL
:
4340 result
= (mleft
<= mright
);
4344 xasprintf(&value
, "%.*f", prec
, result
);
4346 xasprintf(&value
, "%.*f", prec
, (double)(long long)result
);
4347 format_log(es
, "expression result is %s", value
);
4359 /* Replace a key. */
4361 format_replace(struct format_expand_state
*es
, const char *key
, size_t keylen
,
4362 char **buf
, size_t *len
, size_t *off
)
4364 struct format_tree
*ft
= es
->ft
;
4365 struct window_pane
*wp
= ft
->wp
;
4366 const char *errstr
, *copy
, *cp
, *marker
= NULL
;
4367 const char *time_format
= NULL
;
4368 char *copy0
, *condition
, *found
, *new;
4369 char *value
, *left
, *right
;
4371 int modifiers
= 0, limit
= 0, width
= 0;
4373 struct format_modifier
*list
, *cmp
= NULL
, *search
= NULL
;
4374 struct format_modifier
**sub
= NULL
, *mexp
= NULL
, *fm
;
4375 u_int i
, count
, nsub
= 0;
4376 struct format_expand_state next
;
4378 /* Make a copy of the key. */
4379 copy
= copy0
= xstrndup(key
, keylen
);
4381 /* Process modifier list. */
4382 list
= format_build_modifiers(es
, ©
, &count
);
4383 for (i
= 0; i
< count
; i
++) {
4385 if (format_logging(ft
)) {
4386 format_log(es
, "modifier %u is %s", i
, fm
->modifier
);
4387 for (j
= 0; j
< fm
->argc
; j
++) {
4388 format_log(es
, "modifier %u argument %d: %s", i
,
4392 if (fm
->size
== 1) {
4393 switch (fm
->modifier
[0]) {
4405 sub
= xreallocarray(sub
, nsub
+ 1, sizeof *sub
);
4411 limit
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4415 if (fm
->argc
>= 2 && fm
->argv
[1] != NULL
)
4416 marker
= fm
->argv
[1];
4421 width
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4427 modifiers
|= FORMAT_WIDTH
;
4430 if (fm
->argc
< 1 || fm
->argc
> 3)
4435 modifiers
|= FORMAT_LITERAL
;
4438 modifiers
|= FORMAT_CHARACTER
;
4441 modifiers
|= FORMAT_BASENAME
;
4444 modifiers
|= FORMAT_COLOUR
;
4447 modifiers
|= FORMAT_DIRNAME
;
4450 modifiers
|= FORMAT_LENGTH
;
4453 modifiers
|= FORMAT_TIMESTRING
;
4456 if (strchr(fm
->argv
[0], 'p') != NULL
)
4457 modifiers
|= FORMAT_PRETTY
;
4458 else if (fm
->argc
>= 2 &&
4459 strchr(fm
->argv
[0], 'f') != NULL
)
4460 time_format
= format_strip(fm
->argv
[1]);
4464 modifiers
|= FORMAT_QUOTE_SHELL
;
4465 else if (strchr(fm
->argv
[0], 'e') != NULL
||
4466 strchr(fm
->argv
[0], 'h') != NULL
)
4467 modifiers
|= FORMAT_QUOTE_STYLE
;
4470 modifiers
|= FORMAT_EXPAND
;
4473 modifiers
|= FORMAT_EXPANDTIME
;
4477 strchr(fm
->argv
[0], 'w') != NULL
)
4478 modifiers
|= FORMAT_WINDOW_NAME
;
4479 else if (strchr(fm
->argv
[0], 's') != NULL
)
4480 modifiers
|= FORMAT_SESSION_NAME
;
4483 modifiers
|= FORMAT_SESSIONS
;
4486 modifiers
|= FORMAT_WINDOWS
;
4489 modifiers
|= FORMAT_PANES
;
4492 modifiers
|= FORMAT_CLIENTS
;
4495 } else if (fm
->size
== 2) {
4496 if (strcmp(fm
->modifier
, "||") == 0 ||
4497 strcmp(fm
->modifier
, "&&") == 0 ||
4498 strcmp(fm
->modifier
, "==") == 0 ||
4499 strcmp(fm
->modifier
, "!=") == 0 ||
4500 strcmp(fm
->modifier
, ">=") == 0 ||
4501 strcmp(fm
->modifier
, "<=") == 0)
4506 /* Is this a literal string? */
4507 if (modifiers
& FORMAT_LITERAL
) {
4508 format_log(es
, "literal string is '%s'", copy
);
4509 value
= format_unescape(copy
);
4513 /* Is this a character? */
4514 if (modifiers
& FORMAT_CHARACTER
) {
4515 new = format_expand1(es
, copy
);
4516 c
= strtonum(new, 32, 126, &errstr
);
4518 value
= xstrdup("");
4520 xasprintf(&value
, "%c", c
);
4525 /* Is this a colour? */
4526 if (modifiers
& FORMAT_COLOUR
) {
4527 new = format_expand1(es
, copy
);
4528 c
= colour_fromstring(new);
4529 if (c
== -1 || (c
= colour_force_rgb(c
)) == -1)
4530 value
= xstrdup("");
4532 xasprintf(&value
, "%06x", c
& 0xffffff);
4537 /* Is this a loop, comparison or condition? */
4538 if (modifiers
& FORMAT_SESSIONS
) {
4539 value
= format_loop_sessions(es
, copy
);
4542 } else if (modifiers
& FORMAT_WINDOWS
) {
4543 value
= format_loop_windows(es
, copy
);
4546 } else if (modifiers
& FORMAT_PANES
) {
4547 value
= format_loop_panes(es
, copy
);
4550 } else if (modifiers
& FORMAT_CLIENTS
) {
4551 value
= format_loop_clients(es
, copy
);
4554 } else if (modifiers
& FORMAT_WINDOW_NAME
) {
4555 value
= format_window_name(es
, copy
);
4558 } else if (modifiers
& FORMAT_SESSION_NAME
) {
4559 value
= format_session_name(es
, copy
);
4562 } else if (search
!= NULL
) {
4563 /* Search in pane. */
4564 new = format_expand1(es
, copy
);
4566 format_log(es
, "search '%s' but no pane", new);
4567 value
= xstrdup("0");
4569 format_log(es
, "search '%s' pane %%%u", new, wp
->id
);
4570 value
= format_search(search
, wp
, new);
4573 } else if (cmp
!= NULL
) {
4574 /* Comparison of left and right. */
4575 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4576 format_log(es
, "compare %s syntax error: %s",
4577 cmp
->modifier
, copy
);
4580 format_log(es
, "compare %s left is: %s", cmp
->modifier
, left
);
4581 format_log(es
, "compare %s right is: %s", cmp
->modifier
, right
);
4583 if (strcmp(cmp
->modifier
, "||") == 0) {
4584 if (format_true(left
) || format_true(right
))
4585 value
= xstrdup("1");
4587 value
= xstrdup("0");
4588 } else if (strcmp(cmp
->modifier
, "&&") == 0) {
4589 if (format_true(left
) && format_true(right
))
4590 value
= xstrdup("1");
4592 value
= xstrdup("0");
4593 } else if (strcmp(cmp
->modifier
, "==") == 0) {
4594 if (strcmp(left
, right
) == 0)
4595 value
= xstrdup("1");
4597 value
= xstrdup("0");
4598 } else if (strcmp(cmp
->modifier
, "!=") == 0) {
4599 if (strcmp(left
, right
) != 0)
4600 value
= xstrdup("1");
4602 value
= xstrdup("0");
4603 } else if (strcmp(cmp
->modifier
, "<") == 0) {
4604 if (strcmp(left
, right
) < 0)
4605 value
= xstrdup("1");
4607 value
= xstrdup("0");
4608 } else if (strcmp(cmp
->modifier
, ">") == 0) {
4609 if (strcmp(left
, right
) > 0)
4610 value
= xstrdup("1");
4612 value
= xstrdup("0");
4613 } else if (strcmp(cmp
->modifier
, "<=") == 0) {
4614 if (strcmp(left
, right
) <= 0)
4615 value
= xstrdup("1");
4617 value
= xstrdup("0");
4618 } else if (strcmp(cmp
->modifier
, ">=") == 0) {
4619 if (strcmp(left
, right
) >= 0)
4620 value
= xstrdup("1");
4622 value
= xstrdup("0");
4623 } else if (strcmp(cmp
->modifier
, "m") == 0)
4624 value
= format_match(cmp
, left
, right
);
4628 } else if (*copy
== '?') {
4629 /* Conditional: check first and choose second or third. */
4630 cp
= format_skip(copy
+ 1, ",");
4632 format_log(es
, "condition syntax error: %s", copy
+ 1);
4635 condition
= xstrndup(copy
+ 1, cp
- (copy
+ 1));
4636 format_log(es
, "condition is: %s", condition
);
4638 found
= format_find(ft
, condition
, modifiers
, time_format
);
4639 if (found
== NULL
) {
4641 * If the condition not found, try to expand it. If
4642 * the expansion doesn't have any effect, then assume
4645 found
= format_expand1(es
, condition
);
4646 if (strcmp(found
, condition
) == 0) {
4648 found
= xstrdup("");
4650 "condition '%s' not found; assuming false",
4654 format_log(es
, "condition '%s' found: %s", condition
,
4658 if (format_choose(es
, cp
+ 1, &left
, &right
, 0) != 0) {
4659 format_log(es
, "condition '%s' syntax error: %s",
4664 if (format_true(found
)) {
4665 format_log(es
, "condition '%s' is true", condition
);
4666 value
= format_expand1(es
, left
);
4668 format_log(es
, "condition '%s' is false", condition
);
4669 value
= format_expand1(es
, right
);
4676 } else if (mexp
!= NULL
) {
4677 value
= format_replace_expression(mexp
, es
, copy
);
4679 value
= xstrdup("");
4681 if (strstr(copy
, "#{") != 0) {
4682 format_log(es
, "expanding inner format '%s'", copy
);
4683 value
= format_expand1(es
, copy
);
4685 value
= format_find(ft
, copy
, modifiers
, time_format
);
4686 if (value
== NULL
) {
4687 format_log(es
, "format '%s' not found", copy
);
4688 value
= xstrdup("");
4690 format_log(es
, "format '%s' found: %s", copy
,
4697 /* Expand again if required. */
4698 if (modifiers
& FORMAT_EXPAND
) {
4699 new = format_expand1(es
, value
);
4702 } else if (modifiers
& FORMAT_EXPANDTIME
) {
4703 format_copy_state(&next
, es
, FORMAT_EXPAND_TIME
);
4704 new = format_expand1(&next
, value
);
4709 /* Perform substitution if any. */
4710 for (i
= 0; i
< nsub
; i
++) {
4711 left
= format_expand1(es
, sub
[i
]->argv
[0]);
4712 right
= format_expand1(es
, sub
[i
]->argv
[1]);
4713 new = format_sub(sub
[i
], value
, left
, right
);
4714 format_log(es
, "substitute '%s' to '%s': %s", left
, right
, new);
4721 /* Truncate the value if needed. */
4723 new = format_trim_left(value
, limit
);
4724 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4726 xasprintf(&value
, "%s%s", new, marker
);
4731 format_log(es
, "applied length limit %d: %s", limit
, value
);
4732 } else if (limit
< 0) {
4733 new = format_trim_right(value
, -limit
);
4734 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4736 xasprintf(&value
, "%s%s", marker
, new);
4741 format_log(es
, "applied length limit %d: %s", limit
, value
);
4744 /* Pad the value if needed. */
4746 new = utf8_padcstr(value
, width
);
4749 format_log(es
, "applied padding width %d: %s", width
, value
);
4750 } else if (width
< 0) {
4751 new = utf8_rpadcstr(value
, -width
);
4754 format_log(es
, "applied padding width %d: %s", width
, value
);
4757 /* Replace with the length or width if needed. */
4758 if (modifiers
& FORMAT_LENGTH
) {
4759 xasprintf(&new, "%zu", strlen(value
));
4762 format_log(es
, "replacing with length: %s", new);
4764 if (modifiers
& FORMAT_WIDTH
) {
4765 xasprintf(&new, "%u", format_width(value
));
4768 format_log(es
, "replacing with width: %s", new);
4771 /* Expand the buffer and copy in the value. */
4772 valuelen
= strlen(value
);
4773 while (*len
- *off
< valuelen
+ 1) {
4774 *buf
= xreallocarray(*buf
, 2, *len
);
4777 memcpy(*buf
+ *off
, value
, valuelen
);
4780 format_log(es
, "replaced '%s' with '%s'", copy0
, value
);
4784 format_free_modifiers(list
, count
);
4789 format_log(es
, "failed %s", copy0
);
4792 format_free_modifiers(list
, count
);
4797 /* Expand keys in a template. */
4799 format_expand1(struct format_expand_state
*es
, const char *fmt
)
4801 struct format_tree
*ft
= es
->ft
;
4802 char *buf
, *out
, *name
;
4803 const char *ptr
, *s
, *style_end
= NULL
;
4804 size_t off
, len
, n
, outlen
;
4806 char expanded
[8192];
4808 if (fmt
== NULL
|| *fmt
== '\0')
4809 return (xstrdup(""));
4811 if (es
->loop
== FORMAT_LOOP_LIMIT
) {
4812 format_log(es
, "reached loop limit (%u)", FORMAT_LOOP_LIMIT
);
4813 return (xstrdup(""));
4817 format_log(es
, "expanding format: %s", fmt
);
4819 if ((es
->flags
& FORMAT_EXPAND_TIME
) && strchr(fmt
, '%') != NULL
) {
4820 if (es
->time
== 0) {
4821 es
->time
= time(NULL
);
4822 localtime_r(&es
->time
, &es
->tm
);
4824 if (strftime(expanded
, sizeof expanded
, fmt
, &es
->tm
) == 0) {
4825 format_log(es
, "format is too long");
4826 return (xstrdup(""));
4828 if (format_logging(ft
) && strcmp(expanded
, fmt
) != 0)
4829 format_log(es
, "after time expanded: %s", expanded
);
4837 while (*fmt
!= '\0') {
4839 while (len
- off
< 2) {
4840 buf
= xreallocarray(buf
, 2, len
);
4843 buf
[off
++] = *fmt
++;
4848 ch
= (u_char
)*fmt
++;
4852 for (ptr
= fmt
; *ptr
!= '\0'; ptr
++) {
4855 if (*ptr
== ')' && --brackets
== 0)
4858 if (*ptr
!= ')' || brackets
!= 0)
4862 name
= xstrndup(fmt
, n
);
4863 format_log(es
, "found #(): %s", name
);
4865 if ((ft
->flags
& FORMAT_NOJOBS
) ||
4866 (es
->flags
& FORMAT_EXPAND_NOJOBS
)) {
4868 format_log(es
, "#() is disabled");
4870 out
= format_job_get(es
, name
);
4871 format_log(es
, "#() result: %s", out
);
4875 outlen
= strlen(out
);
4876 while (len
- off
< outlen
+ 1) {
4877 buf
= xreallocarray(buf
, 2, len
);
4880 memcpy(buf
+ off
, out
, outlen
);
4888 ptr
= format_skip((char *)fmt
- 2, "}");
4893 format_log(es
, "found #{}: %.*s", (int)n
, fmt
);
4894 if (format_replace(es
, fmt
, n
, &buf
, &len
, &off
) != 0)
4901 * If ##[ (with two or more #s), then it is a style and
4902 * can be left for format_draw to handle.
4904 ptr
= fmt
- (ch
== '[');
4905 n
= 2 - (ch
== '[');
4906 while (*ptr
== '#') {
4911 style_end
= format_skip(fmt
- 2, "]");
4912 format_log(es
, "found #*%zu[", n
);
4913 while (len
- off
< n
+ 2) {
4914 buf
= xreallocarray(buf
, 2, len
);
4917 memcpy(buf
+ off
, fmt
- 2, n
+ 1);
4925 format_log(es
, "found #%c", ch
);
4926 while (len
- off
< 2) {
4927 buf
= xreallocarray(buf
, 2, len
);
4934 if (fmt
> style_end
) { /* skip inside #[] */
4935 if (ch
>= 'A' && ch
<= 'Z')
4936 s
= format_upper
[ch
- 'A'];
4937 else if (ch
>= 'a' && ch
<= 'z')
4938 s
= format_lower
[ch
- 'a'];
4941 while (len
- off
< 3) {
4942 buf
= xreallocarray(buf
, 2, len
);
4950 format_log(es
, "found #%c: %s", ch
, s
);
4951 if (format_replace(es
, s
, n
, &buf
, &len
, &off
) != 0)
4960 format_log(es
, "result is: %s", buf
);
4966 /* Expand keys in a template, passing through strftime first. */
4968 format_expand_time(struct format_tree
*ft
, const char *fmt
)
4970 struct format_expand_state es
;
4972 memset(&es
, 0, sizeof es
);
4974 es
.flags
= FORMAT_EXPAND_TIME
;
4975 return (format_expand1(&es
, fmt
));
4978 /* Expand keys in a template. */
4980 format_expand(struct format_tree
*ft
, const char *fmt
)
4982 struct format_expand_state es
;
4984 memset(&es
, 0, sizeof es
);
4987 return (format_expand1(&es
, fmt
));
4990 /* Expand a single string. */
4992 format_single(struct cmdq_item
*item
, const char *fmt
, struct client
*c
,
4993 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
4995 struct format_tree
*ft
;
4998 ft
= format_create_defaults(item
, c
, s
, wl
, wp
);
4999 expanded
= format_expand(ft
, fmt
);
5004 /* Expand a single string using state. */
5006 format_single_from_state(struct cmdq_item
*item
, const char *fmt
,
5007 struct client
*c
, struct cmd_find_state
*fs
)
5009 return (format_single(item
, fmt
, c
, fs
->s
, fs
->wl
, fs
->wp
));
5012 /* Expand a single string using target. */
5014 format_single_from_target(struct cmdq_item
*item
, const char *fmt
)
5016 struct client
*tc
= cmdq_get_target_client(item
);
5018 return (format_single_from_state(item
, fmt
, tc
, cmdq_get_target(item
)));
5021 /* Create and add defaults. */
5022 struct format_tree
*
5023 format_create_defaults(struct cmdq_item
*item
, struct client
*c
,
5024 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
5026 struct format_tree
*ft
;
5029 ft
= format_create(cmdq_get_client(item
), item
, FORMAT_NONE
, 0);
5031 ft
= format_create(NULL
, item
, FORMAT_NONE
, 0);
5032 format_defaults(ft
, c
, s
, wl
, wp
);
5036 /* Create and add defaults using state. */
5037 struct format_tree
*
5038 format_create_from_state(struct cmdq_item
*item
, struct client
*c
,
5039 struct cmd_find_state
*fs
)
5041 return (format_create_defaults(item
, c
, fs
->s
, fs
->wl
, fs
->wp
));
5044 /* Create and add defaults using target. */
5045 struct format_tree
*
5046 format_create_from_target(struct cmdq_item
*item
)
5048 struct client
*tc
= cmdq_get_target_client(item
);
5050 return (format_create_from_state(item
, tc
, cmdq_get_target(item
)));
5053 /* Set defaults for any of arguments that are not NULL. */
5055 format_defaults(struct format_tree
*ft
, struct client
*c
, struct session
*s
,
5056 struct winlink
*wl
, struct window_pane
*wp
)
5058 struct paste_buffer
*pb
;
5060 if (c
!= NULL
&& c
->name
!= NULL
)
5061 log_debug("%s: c=%s", __func__
, c
->name
);
5063 log_debug("%s: c=none", __func__
);
5065 log_debug("%s: s=$%u", __func__
, s
->id
);
5067 log_debug("%s: s=none", __func__
);
5069 log_debug("%s: wl=%u", __func__
, wl
->idx
);
5071 log_debug("%s: wl=none", __func__
);
5073 log_debug("%s: wp=%%%u", __func__
, wp
->id
);
5075 log_debug("%s: wp=none", __func__
);
5077 if (c
!= NULL
&& s
!= NULL
&& c
->session
!= s
)
5078 log_debug("%s: session does not match", __func__
);
5081 ft
->type
= FORMAT_TYPE_PANE
;
5082 else if (wl
!= NULL
)
5083 ft
->type
= FORMAT_TYPE_WINDOW
;
5085 ft
->type
= FORMAT_TYPE_SESSION
;
5087 ft
->type
= FORMAT_TYPE_UNKNOWN
;
5089 if (s
== NULL
&& c
!= NULL
)
5091 if (wl
== NULL
&& s
!= NULL
)
5093 if (wp
== NULL
&& wl
!= NULL
)
5094 wp
= wl
->window
->active
;
5097 format_defaults_client(ft
, c
);
5099 format_defaults_session(ft
, s
);
5101 format_defaults_winlink(ft
, wl
);
5103 format_defaults_pane(ft
, wp
);
5105 pb
= paste_get_top(NULL
);
5107 format_defaults_paste_buffer(ft
, pb
);
5110 /* Set default format keys for a session. */
5112 format_defaults_session(struct format_tree
*ft
, struct session
*s
)
5117 /* Set default format keys for a client. */
5119 format_defaults_client(struct format_tree
*ft
, struct client
*c
)
5126 /* Set default format keys for a window. */
5128 format_defaults_window(struct format_tree
*ft
, struct window
*w
)
5133 /* Set default format keys for a winlink. */
5135 format_defaults_winlink(struct format_tree
*ft
, struct winlink
*wl
)
5138 format_defaults_window(ft
, wl
->window
);
5142 /* Set default format keys for a window pane. */
5144 format_defaults_pane(struct format_tree
*ft
, struct window_pane
*wp
)
5146 struct window_mode_entry
*wme
;
5149 format_defaults_window(ft
, wp
->window
);
5152 wme
= TAILQ_FIRST(&wp
->modes
);
5153 if (wme
!= NULL
&& wme
->mode
->formats
!= NULL
)
5154 wme
->mode
->formats(wme
, ft
);
5157 /* Set default format keys for paste buffer. */
5159 format_defaults_paste_buffer(struct format_tree
*ft
, struct paste_buffer
*pb
)
5164 /* Return word at given coordinates. Caller frees. */
5166 format_grid_word(struct grid
*gd
, u_int x
, u_int y
)
5168 const struct grid_line
*gl
;
5169 struct grid_cell gc
;
5171 struct utf8_data
*ud
= NULL
;
5177 ws
= options_get_string(global_s_options
, "word-separators");
5180 grid_get_cell(gd
, x
, y
, &gc
);
5181 if (gc
.flags
& GRID_FLAG_PADDING
)
5183 if (utf8_cstrhas(ws
, &gc
.data
) ||
5184 (gc
.data
.size
== 1 && *gc
.data
.data
== ' ')) {
5192 gl
= grid_peek_line(gd
, y
- 1);
5193 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5196 x
= grid_line_length(gd
, y
);
5204 end
= grid_line_length(gd
, y
);
5205 if (end
== 0 || x
== end
- 1) {
5206 if (y
== gd
->hsize
+ gd
->sy
- 1)
5208 gl
= grid_peek_line(gd
, y
);
5209 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5218 grid_get_cell(gd
, x
, y
, &gc
);
5219 if (gc
.flags
& GRID_FLAG_PADDING
)
5221 if (utf8_cstrhas(ws
, &gc
.data
) ||
5222 (gc
.data
.size
== 1 && *gc
.data
.data
== ' '))
5225 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5226 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5230 s
= utf8_tocstr(ud
);
5236 /* Return line at given coordinates. Caller frees. */
5238 format_grid_line(struct grid
*gd
, u_int y
)
5240 struct grid_cell gc
;
5241 struct utf8_data
*ud
= NULL
;
5246 for (x
= 0; x
< grid_line_length(gd
, y
); x
++) {
5247 grid_get_cell(gd
, x
, y
, &gc
);
5248 if (gc
.flags
& GRID_FLAG_PADDING
)
5251 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5252 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5256 s
= utf8_tocstr(ud
);
5262 /* Return hyperlink at given coordinates. Caller frees. */
5264 format_grid_hyperlink(struct grid
*gd
, u_int x
, u_int y
, struct screen
* s
)
5267 struct grid_cell gc
;
5269 grid_get_cell(gd
, x
, y
, &gc
);
5270 if (gc
.flags
& GRID_FLAG_PADDING
)
5272 if (s
->hyperlinks
== NULL
|| gc
.link
== 0)
5274 if (!hyperlinks_get(s
->hyperlinks
, gc
.link
, &uri
, NULL
, NULL
))
5276 return (xstrdup(uri
));