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
107 /* Limit on recursion. */
108 #define FORMAT_LOOP_LIMIT 100
110 /* Format expand flags. */
111 #define FORMAT_EXPAND_TIME 0x1
112 #define FORMAT_EXPAND_NOJOBS 0x2
114 /* Entry in format tree. */
115 struct format_entry
{
120 RB_ENTRY(format_entry
) entry
;
132 enum format_type type
;
138 struct window_pane
*wp
;
139 struct paste_buffer
*pb
;
141 struct cmdq_item
*item
;
142 struct client
*client
;
146 struct mouse_event m
;
148 RB_HEAD(format_entry_tree
, format_entry
) tree
;
150 static int format_entry_cmp(struct format_entry
*, struct format_entry
*);
151 RB_GENERATE_STATIC(format_entry_tree
, format_entry
, entry
, format_entry_cmp
);
153 /* Format expand state. */
154 struct format_expand_state
{
155 struct format_tree
*ft
;
162 /* Format modifier. */
163 struct format_modifier
{
171 /* Format entry tree comparison function. */
173 format_entry_cmp(struct format_entry
*fe1
, struct format_entry
*fe2
)
175 return (strcmp(fe1
->key
, fe2
->key
));
178 /* Single-character uppercase aliases. */
179 static const char *format_upper
[] = {
185 "window_flags", /* F */
188 "window_index", /* I */
195 "pane_index", /* P */
198 "session_name", /* S */
199 "pane_title", /* T */
202 "window_name", /* W */
208 /* Single-character lowercase aliases. */
209 static const char *format_lower
[] = {
217 "host_short", /* h */
238 /* Is logging enabled? */
240 format_logging(struct format_tree
*ft
)
242 return (log_get_level() != 0 || (ft
->flags
& FORMAT_VERBOSE
));
245 /* Log a message if verbose. */
246 static void printflike(3, 4)
247 format_log1(struct format_expand_state
*es
, const char *from
, const char *fmt
,
250 struct format_tree
*ft
= es
->ft
;
253 static const char spaces
[] = " ";
255 if (!format_logging(ft
))
259 xvasprintf(&s
, fmt
, ap
);
262 log_debug("%s: %s", from
, s
);
263 if (ft
->item
!= NULL
&& (ft
->flags
& FORMAT_VERBOSE
))
264 cmdq_print(ft
->item
, "#%.*s%s", es
->loop
, spaces
, s
);
268 #define format_log(es, fmt, ...) format_log1(es, __func__, fmt, ##__VA_ARGS__)
270 /* Copy expand state. */
272 format_copy_state(struct format_expand_state
*to
,
273 struct format_expand_state
*from
, int flags
)
276 to
->loop
= from
->loop
;
277 to
->time
= from
->time
;
278 memcpy(&to
->tm
, &from
->tm
, sizeof to
->tm
);
279 to
->flags
= from
->flags
|flags
;
282 /* Format job update callback. */
284 format_job_update(struct job
*job
)
286 struct format_job
*fj
= job_get_data(job
);
287 struct evbuffer
*evb
= job_get_event(job
)->input
;
288 char *line
= NULL
, *next
;
291 while ((next
= evbuffer_readline(evb
)) != NULL
) {
302 log_debug("%s: %p %s: %s", __func__
, fj
, fj
->cmd
, fj
->out
);
305 if (fj
->status
&& fj
->last
!= t
) {
306 if (fj
->client
!= NULL
)
307 server_status_client(fj
->client
);
312 /* Format job complete callback. */
314 format_job_complete(struct job
*job
)
316 struct format_job
*fj
= job_get_data(job
);
317 struct evbuffer
*evb
= job_get_event(job
)->input
;
324 if ((line
= evbuffer_readline(evb
)) == NULL
) {
325 len
= EVBUFFER_LENGTH(evb
);
326 buf
= xmalloc(len
+ 1);
328 memcpy(buf
, EVBUFFER_DATA(evb
), len
);
333 log_debug("%s: %p %s: %s", __func__
, fj
, fj
->cmd
, buf
);
335 if (*buf
!= '\0' || !fj
->updated
) {
342 if (fj
->client
!= NULL
)
343 server_status_client(fj
->client
);
350 format_job_get(struct format_expand_state
*es
, const char *cmd
)
352 struct format_tree
*ft
= es
->ft
;
353 struct format_job_tree
*jobs
;
354 struct format_job fj0
, *fj
;
358 struct format_expand_state next
;
360 if (ft
->client
== NULL
)
362 else if (ft
->client
->jobs
!= NULL
)
363 jobs
= ft
->client
->jobs
;
365 jobs
= ft
->client
->jobs
= xmalloc(sizeof *ft
->client
->jobs
);
371 if ((fj
= RB_FIND(format_job_tree
, jobs
, &fj0
)) == NULL
) {
372 fj
= xcalloc(1, sizeof *fj
);
373 fj
->client
= ft
->client
;
375 fj
->cmd
= xstrdup(cmd
);
377 RB_INSERT(format_job_tree
, jobs
, fj
);
380 format_copy_state(&next
, es
, FORMAT_EXPAND_NOJOBS
);
381 next
.flags
&= ~FORMAT_EXPAND_TIME
;
383 expanded
= format_expand1(&next
, cmd
);
384 if (fj
->expanded
== NULL
|| strcmp(expanded
, fj
->expanded
) != 0) {
385 free((void *)fj
->expanded
);
386 fj
->expanded
= xstrdup(expanded
);
389 force
= (ft
->flags
& FORMAT_FORCE
);
392 if (force
&& fj
->job
!= NULL
)
394 if (force
|| (fj
->job
== NULL
&& fj
->last
!= t
)) {
395 fj
->job
= job_run(expanded
, 0, NULL
, NULL
, NULL
,
396 server_client_get_cwd(ft
->client
, NULL
), format_job_update
,
397 format_job_complete
, NULL
, fj
, JOB_NOWAIT
, -1, -1);
398 if (fj
->job
== NULL
) {
400 xasprintf(&fj
->out
, "<'%s' didn't start>", fj
->cmd
);
404 } else if (fj
->job
!= NULL
&& (t
- fj
->last
) > 1 && fj
->out
== NULL
)
405 xasprintf(&fj
->out
, "<'%s' not ready>", fj
->cmd
);
408 if (ft
->flags
& FORMAT_STATUS
)
411 return (xstrdup(""));
412 return (format_expand1(&next
, fj
->out
));
415 /* Remove old jobs. */
417 format_job_tidy(struct format_job_tree
*jobs
, int force
)
419 struct format_job
*fj
, *fj1
;
423 RB_FOREACH_SAFE(fj
, format_job_tree
, jobs
, fj1
) {
424 if (!force
&& (fj
->last
> now
|| now
- fj
->last
< 3600))
426 RB_REMOVE(format_job_tree
, jobs
, fj
);
428 log_debug("%s: %s", __func__
, fj
->cmd
);
433 free((void *)fj
->expanded
);
434 free((void *)fj
->cmd
);
441 /* Tidy old jobs for all clients. */
443 format_tidy_jobs(void)
447 format_job_tidy(&format_jobs
, 0);
448 TAILQ_FOREACH(c
, &clients
, entry
) {
450 format_job_tidy(c
->jobs
, 0);
454 /* Remove old jobs for client. */
456 format_lost_client(struct client
*c
)
459 format_job_tidy(c
->jobs
, 1);
463 /* Wrapper for asprintf. */
464 static char * printflike(1, 2)
465 format_printf(const char *fmt
, ...)
471 xvasprintf(&s
, fmt
, ap
);
476 /* Callback for host. */
478 format_cb_host(__unused
struct format_tree
*ft
)
480 char host
[HOST_NAME_MAX
+ 1];
482 if (gethostname(host
, sizeof host
) != 0)
483 return (xstrdup(""));
484 return (xstrdup(host
));
487 /* Callback for host_short. */
489 format_cb_host_short(__unused
struct format_tree
*ft
)
491 char host
[HOST_NAME_MAX
+ 1], *cp
;
493 if (gethostname(host
, sizeof host
) != 0)
494 return (xstrdup(""));
495 if ((cp
= strchr(host
, '.')) != NULL
)
497 return (xstrdup(host
));
500 /* Callback for pid. */
502 format_cb_pid(__unused
struct format_tree
*ft
)
506 xasprintf(&value
, "%ld", (long)getpid());
510 /* Callback for session_attached_list. */
512 format_cb_session_attached_list(struct format_tree
*ft
)
514 struct session
*s
= ft
->s
;
516 struct evbuffer
*buffer
;
523 buffer
= evbuffer_new();
525 fatalx("out of memory");
527 TAILQ_FOREACH(loop
, &clients
, entry
) {
528 if (loop
->session
== s
) {
529 if (EVBUFFER_LENGTH(buffer
) > 0)
530 evbuffer_add(buffer
, ",", 1);
531 evbuffer_add_printf(buffer
, "%s", loop
->name
);
535 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
536 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
537 evbuffer_free(buffer
);
541 /* Callback for session_alerts. */
543 format_cb_session_alerts(struct format_tree
*ft
)
545 struct session
*s
= ft
->s
;
547 char alerts
[1024], tmp
[16];
553 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
554 if ((wl
->flags
& WINLINK_ALERTFLAGS
) == 0)
556 xsnprintf(tmp
, sizeof tmp
, "%u", wl
->idx
);
559 strlcat(alerts
, ",", sizeof alerts
);
560 strlcat(alerts
, tmp
, sizeof alerts
);
561 if (wl
->flags
& WINLINK_ACTIVITY
)
562 strlcat(alerts
, "#", sizeof alerts
);
563 if (wl
->flags
& WINLINK_BELL
)
564 strlcat(alerts
, "!", sizeof alerts
);
565 if (wl
->flags
& WINLINK_SILENCE
)
566 strlcat(alerts
, "~", sizeof alerts
);
568 return (xstrdup(alerts
));
571 /* Callback for session_stack. */
573 format_cb_session_stack(struct format_tree
*ft
)
575 struct session
*s
= ft
->s
;
577 char result
[1024], tmp
[16];
582 xsnprintf(result
, sizeof result
, "%u", s
->curw
->idx
);
583 TAILQ_FOREACH(wl
, &s
->lastw
, sentry
) {
584 xsnprintf(tmp
, sizeof tmp
, "%u", wl
->idx
);
587 strlcat(result
, ",", sizeof result
);
588 strlcat(result
, tmp
, sizeof result
);
590 return (xstrdup(result
));
593 /* Callback for window_stack_index. */
595 format_cb_window_stack_index(struct format_tree
*ft
)
607 TAILQ_FOREACH(wl
, &s
->lastw
, sentry
) {
613 return (xstrdup("0"));
614 xasprintf(&value
, "%u", idx
);
618 /* Callback for window_linked_sessions_list. */
620 format_cb_window_linked_sessions_list(struct format_tree
*ft
)
624 struct evbuffer
*buffer
;
632 buffer
= evbuffer_new();
634 fatalx("out of memory");
636 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
637 if (EVBUFFER_LENGTH(buffer
) > 0)
638 evbuffer_add(buffer
, ",", 1);
639 evbuffer_add_printf(buffer
, "%s", wl
->session
->name
);
642 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
643 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
644 evbuffer_free(buffer
);
648 /* Callback for window_active_sessions. */
650 format_cb_window_active_sessions(struct format_tree
*ft
)
661 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
662 if (wl
->session
->curw
== wl
)
666 xasprintf(&value
, "%u", n
);
670 /* Callback for window_active_sessions_list. */
672 format_cb_window_active_sessions_list(struct format_tree
*ft
)
676 struct evbuffer
*buffer
;
684 buffer
= evbuffer_new();
686 fatalx("out of memory");
688 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
689 if (wl
->session
->curw
== wl
) {
690 if (EVBUFFER_LENGTH(buffer
) > 0)
691 evbuffer_add(buffer
, ",", 1);
692 evbuffer_add_printf(buffer
, "%s", wl
->session
->name
);
696 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
697 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
698 evbuffer_free(buffer
);
702 /* Callback for window_active_clients. */
704 format_cb_window_active_clients(struct format_tree
*ft
)
708 struct session
*client_session
;
716 TAILQ_FOREACH(loop
, &clients
, entry
) {
717 client_session
= loop
->session
;
718 if (client_session
== NULL
)
721 if (w
== client_session
->curw
->window
)
725 xasprintf(&value
, "%u", n
);
729 /* Callback for window_active_clients_list. */
731 format_cb_window_active_clients_list(struct format_tree
*ft
)
735 struct session
*client_session
;
736 struct evbuffer
*buffer
;
744 buffer
= evbuffer_new();
746 fatalx("out of memory");
748 TAILQ_FOREACH(loop
, &clients
, entry
) {
749 client_session
= loop
->session
;
750 if (client_session
== NULL
)
753 if (w
== client_session
->curw
->window
) {
754 if (EVBUFFER_LENGTH(buffer
) > 0)
755 evbuffer_add(buffer
, ",", 1);
756 evbuffer_add_printf(buffer
, "%s", loop
->name
);
760 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
761 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
762 evbuffer_free(buffer
);
766 /* Callback for window_layout. */
768 format_cb_window_layout(struct format_tree
*ft
)
770 struct window
*w
= ft
->w
;
775 if (w
->saved_layout_root
!= NULL
)
776 return (layout_dump(w
->saved_layout_root
));
777 return (layout_dump(w
->layout_root
));
780 /* Callback for window_visible_layout. */
782 format_cb_window_visible_layout(struct format_tree
*ft
)
784 struct window
*w
= ft
->w
;
789 return (layout_dump(w
->layout_root
));
792 /* Callback for pane_start_command. */
794 format_cb_start_command(struct format_tree
*ft
)
796 struct window_pane
*wp
= ft
->wp
;
801 return (cmd_stringify_argv(wp
->argc
, wp
->argv
));
804 /* Callback for pane_start_path. */
806 format_cb_start_path(struct format_tree
*ft
)
808 struct window_pane
*wp
= ft
->wp
;
814 return (xstrdup(""));
815 return (xstrdup(wp
->cwd
));
818 /* Callback for pane_current_command. */
820 format_cb_current_command(struct format_tree
*ft
)
822 struct window_pane
*wp
= ft
->wp
;
825 if (wp
== NULL
|| wp
->shell
== NULL
)
828 cmd
= get_proc_name(wp
->fd
, wp
->tty
);
829 if (cmd
== NULL
|| *cmd
== '\0') {
831 cmd
= cmd_stringify_argv(wp
->argc
, wp
->argv
);
832 if (cmd
== NULL
|| *cmd
== '\0') {
834 cmd
= xstrdup(wp
->shell
);
837 value
= parse_window_name(cmd
);
842 /* Callback for pane_current_path. */
844 format_cb_current_path(struct format_tree
*ft
)
846 struct window_pane
*wp
= ft
->wp
;
852 cwd
= get_proc_cwd(wp
->fd
);
855 return (xstrdup(cwd
));
858 /* Callback for history_bytes. */
860 format_cb_history_bytes(struct format_tree
*ft
)
862 struct window_pane
*wp
= ft
->wp
;
864 struct grid_line
*gl
;
873 for (i
= 0; i
< gd
->hsize
+ gd
->sy
; i
++) {
874 gl
= grid_get_line(gd
, i
);
875 size
+= gl
->cellsize
* sizeof *gl
->celldata
;
876 size
+= gl
->extdsize
* sizeof *gl
->extddata
;
878 size
+= (gd
->hsize
+ gd
->sy
) * sizeof *gl
;
880 xasprintf(&value
, "%zu", size
);
884 /* Callback for history_all_bytes. */
886 format_cb_history_all_bytes(struct format_tree
*ft
)
888 struct window_pane
*wp
= ft
->wp
;
890 struct grid_line
*gl
;
891 u_int i
, lines
, cells
= 0, extended_cells
= 0;
898 lines
= gd
->hsize
+ gd
->sy
;
899 for (i
= 0; i
< lines
; i
++) {
900 gl
= grid_get_line(gd
, i
);
901 cells
+= gl
->cellsize
;
902 extended_cells
+= gl
->extdsize
;
905 xasprintf(&value
, "%u,%zu,%u,%zu,%u,%zu", lines
,
906 lines
* sizeof *gl
, cells
, cells
* sizeof *gl
->celldata
,
907 extended_cells
, extended_cells
* sizeof *gl
->extddata
);
911 /* Callback for pane_tabs. */
913 format_cb_pane_tabs(struct format_tree
*ft
)
915 struct window_pane
*wp
= ft
->wp
;
916 struct evbuffer
*buffer
;
924 buffer
= evbuffer_new();
926 fatalx("out of memory");
927 for (i
= 0; i
< wp
->base
.grid
->sx
; i
++) {
928 if (!bit_test(wp
->base
.tabs
, i
))
931 if (EVBUFFER_LENGTH(buffer
) > 0)
932 evbuffer_add(buffer
, ",", 1);
933 evbuffer_add_printf(buffer
, "%u", i
);
935 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
936 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
937 evbuffer_free(buffer
);
941 /* Callback for pane_fg. */
943 format_cb_pane_fg(struct format_tree
*ft
)
945 struct window_pane
*wp
= ft
->wp
;
951 tty_default_colours(&gc
, wp
);
952 return (xstrdup(colour_tostring(gc
.fg
)));
955 /* Callback for pane_bg. */
957 format_cb_pane_bg(struct format_tree
*ft
)
959 struct window_pane
*wp
= ft
->wp
;
965 tty_default_colours(&gc
, wp
);
966 return (xstrdup(colour_tostring(gc
.bg
)));
969 /* Callback for session_group_list. */
971 format_cb_session_group_list(struct format_tree
*ft
)
973 struct session
*s
= ft
->s
;
974 struct session_group
*sg
;
975 struct session
*loop
;
976 struct evbuffer
*buffer
;
982 sg
= session_group_contains(s
);
986 buffer
= evbuffer_new();
988 fatalx("out of memory");
990 TAILQ_FOREACH(loop
, &sg
->sessions
, gentry
) {
991 if (EVBUFFER_LENGTH(buffer
) > 0)
992 evbuffer_add(buffer
, ",", 1);
993 evbuffer_add_printf(buffer
, "%s", loop
->name
);
996 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
997 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
998 evbuffer_free(buffer
);
1002 /* Callback for session_group_attached_list. */
1004 format_cb_session_group_attached_list(struct format_tree
*ft
)
1006 struct session
*s
= ft
->s
, *client_session
, *session_loop
;
1007 struct session_group
*sg
;
1008 struct client
*loop
;
1009 struct evbuffer
*buffer
;
1015 sg
= session_group_contains(s
);
1019 buffer
= evbuffer_new();
1021 fatalx("out of memory");
1023 TAILQ_FOREACH(loop
, &clients
, entry
) {
1024 client_session
= loop
->session
;
1025 if (client_session
== NULL
)
1027 TAILQ_FOREACH(session_loop
, &sg
->sessions
, gentry
) {
1028 if (session_loop
== client_session
){
1029 if (EVBUFFER_LENGTH(buffer
) > 0)
1030 evbuffer_add(buffer
, ",", 1);
1031 evbuffer_add_printf(buffer
, "%s", loop
->name
);
1036 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
1037 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
1038 evbuffer_free(buffer
);
1042 /* Callback for pane_in_mode. */
1044 format_cb_pane_in_mode(struct format_tree
*ft
)
1046 struct window_pane
*wp
= ft
->wp
;
1048 struct window_mode_entry
*wme
;
1054 TAILQ_FOREACH(wme
, &wp
->modes
, entry
)
1056 xasprintf(&value
, "%u", n
);
1060 /* Callback for pane_at_top. */
1062 format_cb_pane_at_top(struct format_tree
*ft
)
1064 struct window_pane
*wp
= ft
->wp
;
1073 status
= options_get_number(w
->options
, "pane-border-status");
1074 if (status
== PANE_STATUS_TOP
)
1075 flag
= (wp
->yoff
== 1);
1077 flag
= (wp
->yoff
== 0);
1078 xasprintf(&value
, "%d", flag
);
1082 /* Callback for pane_at_bottom. */
1084 format_cb_pane_at_bottom(struct format_tree
*ft
)
1086 struct window_pane
*wp
= ft
->wp
;
1095 status
= options_get_number(w
->options
, "pane-border-status");
1096 if (status
== PANE_STATUS_BOTTOM
)
1097 flag
= (wp
->yoff
+ wp
->sy
== w
->sy
- 1);
1099 flag
= (wp
->yoff
+ wp
->sy
== w
->sy
);
1100 xasprintf(&value
, "%d", flag
);
1104 /* Callback for cursor_character. */
1106 format_cb_cursor_character(struct format_tree
*ft
)
1108 struct window_pane
*wp
= ft
->wp
;
1109 struct grid_cell gc
;
1115 grid_view_get_cell(wp
->base
.grid
, wp
->base
.cx
, wp
->base
.cy
, &gc
);
1116 if (~gc
.flags
& GRID_FLAG_PADDING
)
1117 xasprintf(&value
, "%.*s", (int)gc
.data
.size
, gc
.data
.data
);
1121 /* Callback for mouse_word. */
1123 format_cb_mouse_word(struct format_tree
*ft
)
1125 struct window_pane
*wp
;
1132 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1135 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1138 if (!TAILQ_EMPTY(&wp
->modes
)) {
1139 if (TAILQ_FIRST(&wp
->modes
)->mode
== &window_copy_mode
||
1140 TAILQ_FIRST(&wp
->modes
)->mode
== &window_view_mode
)
1141 return (s
= window_copy_get_word(wp
, x
, y
));
1145 return (format_grid_word(gd
, x
, gd
->hsize
+ y
));
1148 /* Callback for mouse_hyperlink. */
1150 format_cb_mouse_hyperlink(struct format_tree
*ft
)
1152 struct window_pane
*wp
;
1158 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1161 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1164 return (format_grid_hyperlink(gd
, x
, gd
->hsize
+ y
, wp
->screen
));
1167 /* Callback for mouse_line. */
1169 format_cb_mouse_line(struct format_tree
*ft
)
1171 struct window_pane
*wp
;
1177 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1180 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1183 if (!TAILQ_EMPTY(&wp
->modes
)) {
1184 if (TAILQ_FIRST(&wp
->modes
)->mode
== &window_copy_mode
||
1185 TAILQ_FIRST(&wp
->modes
)->mode
== &window_view_mode
)
1186 return (window_copy_get_line(wp
, y
));
1190 return (format_grid_line(gd
, gd
->hsize
+ y
));
1193 /* Callback for alternate_on. */
1195 format_cb_alternate_on(struct format_tree
*ft
)
1197 if (ft
->wp
!= NULL
) {
1198 if (ft
->wp
->base
.saved_grid
!= NULL
)
1199 return (xstrdup("1"));
1200 return (xstrdup("0"));
1205 /* Callback for alternate_saved_x. */
1207 format_cb_alternate_saved_x(struct format_tree
*ft
)
1210 return (format_printf("%u", ft
->wp
->base
.saved_cx
));
1214 /* Callback for alternate_saved_y. */
1216 format_cb_alternate_saved_y(struct format_tree
*ft
)
1219 return (format_printf("%u", ft
->wp
->base
.saved_cy
));
1223 /* Callback for buffer_name. */
1225 format_cb_buffer_name(struct format_tree
*ft
)
1228 return (xstrdup(paste_buffer_name(ft
->pb
)));
1232 /* Callback for buffer_sample. */
1234 format_cb_buffer_sample(struct format_tree
*ft
)
1237 return (paste_make_sample(ft
->pb
));
1241 /* Callback for buffer_size. */
1243 format_cb_buffer_size(struct format_tree
*ft
)
1247 if (ft
->pb
!= NULL
) {
1248 paste_buffer_data(ft
->pb
, &size
);
1249 return (format_printf("%zu", size
));
1254 /* Callback for client_cell_height. */
1256 format_cb_client_cell_height(struct format_tree
*ft
)
1258 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1259 return (format_printf("%u", ft
->c
->tty
.ypixel
));
1263 /* Callback for client_cell_width. */
1265 format_cb_client_cell_width(struct format_tree
*ft
)
1267 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1268 return (format_printf("%u", ft
->c
->tty
.xpixel
));
1272 /* Callback for client_control_mode. */
1274 format_cb_client_control_mode(struct format_tree
*ft
)
1276 if (ft
->c
!= NULL
) {
1277 if (ft
->c
->flags
& CLIENT_CONTROL
)
1278 return (xstrdup("1"));
1279 return (xstrdup("0"));
1284 /* Callback for client_discarded. */
1286 format_cb_client_discarded(struct format_tree
*ft
)
1289 return (format_printf("%zu", ft
->c
->discarded
));
1293 /* Callback for client_flags. */
1295 format_cb_client_flags(struct format_tree
*ft
)
1298 return (xstrdup(server_client_get_flags(ft
->c
)));
1302 /* Callback for client_height. */
1304 format_cb_client_height(struct format_tree
*ft
)
1306 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1307 return (format_printf("%u", ft
->c
->tty
.sy
));
1311 /* Callback for client_key_table. */
1313 format_cb_client_key_table(struct format_tree
*ft
)
1316 return (xstrdup(ft
->c
->keytable
->name
));
1320 /* Callback for client_last_session. */
1322 format_cb_client_last_session(struct format_tree
*ft
)
1324 if (ft
->c
!= NULL
&&
1325 ft
->c
->last_session
!= NULL
&&
1326 session_alive(ft
->c
->last_session
))
1327 return (xstrdup(ft
->c
->last_session
->name
));
1331 /* Callback for client_name. */
1333 format_cb_client_name(struct format_tree
*ft
)
1336 return (xstrdup(ft
->c
->name
));
1340 /* Callback for client_pid. */
1342 format_cb_client_pid(struct format_tree
*ft
)
1345 return (format_printf("%ld", (long)ft
->c
->pid
));
1349 /* Callback for client_prefix. */
1351 format_cb_client_prefix(struct format_tree
*ft
)
1355 if (ft
->c
!= NULL
) {
1356 name
= server_client_get_key_table(ft
->c
);
1357 if (strcmp(ft
->c
->keytable
->name
, name
) == 0)
1358 return (xstrdup("0"));
1359 return (xstrdup("1"));
1364 /* Callback for client_readonly. */
1366 format_cb_client_readonly(struct format_tree
*ft
)
1368 if (ft
->c
!= NULL
) {
1369 if (ft
->c
->flags
& CLIENT_READONLY
)
1370 return (xstrdup("1"));
1371 return (xstrdup("0"));
1376 /* Callback for client_session. */
1378 format_cb_client_session(struct format_tree
*ft
)
1380 if (ft
->c
!= NULL
&& ft
->c
->session
!= NULL
)
1381 return (xstrdup(ft
->c
->session
->name
));
1385 /* Callback for client_termfeatures. */
1387 format_cb_client_termfeatures(struct format_tree
*ft
)
1390 return (xstrdup(tty_get_features(ft
->c
->term_features
)));
1394 /* Callback for client_termname. */
1396 format_cb_client_termname(struct format_tree
*ft
)
1399 return (xstrdup(ft
->c
->term_name
));
1403 /* Callback for client_termtype. */
1405 format_cb_client_termtype(struct format_tree
*ft
)
1407 if (ft
->c
!= NULL
) {
1408 if (ft
->c
->term_type
== NULL
)
1409 return (xstrdup(""));
1410 return (xstrdup(ft
->c
->term_type
));
1415 /* Callback for client_tty. */
1417 format_cb_client_tty(struct format_tree
*ft
)
1420 return (xstrdup(ft
->c
->ttyname
));
1424 /* Callback for client_uid. */
1426 format_cb_client_uid(struct format_tree
*ft
)
1430 if (ft
->c
!= NULL
) {
1431 uid
= proc_get_peer_uid(ft
->c
->peer
);
1432 if (uid
!= (uid_t
)-1)
1433 return (format_printf("%ld", (long)uid
));
1438 /* Callback for client_user. */
1440 format_cb_client_user(struct format_tree
*ft
)
1445 if (ft
->c
!= NULL
) {
1446 uid
= proc_get_peer_uid(ft
->c
->peer
);
1447 if (uid
!= (uid_t
)-1 && (pw
= getpwuid(uid
)) != NULL
)
1448 return (xstrdup(pw
->pw_name
));
1453 /* Callback for client_utf8. */
1455 format_cb_client_utf8(struct format_tree
*ft
)
1457 if (ft
->c
!= NULL
) {
1458 if (ft
->c
->flags
& CLIENT_UTF8
)
1459 return (xstrdup("1"));
1460 return (xstrdup("0"));
1465 /* Callback for client_width. */
1467 format_cb_client_width(struct format_tree
*ft
)
1470 return (format_printf("%u", ft
->c
->tty
.sx
));
1474 /* Callback for client_written. */
1476 format_cb_client_written(struct format_tree
*ft
)
1479 return (format_printf("%zu", ft
->c
->written
));
1483 /* Callback for config_files. */
1485 format_cb_config_files(__unused
struct format_tree
*ft
)
1492 for (i
= 0; i
< cfg_nfiles
; i
++) {
1493 n
= strlen(cfg_files
[i
]) + 1;
1494 s
= xrealloc(s
, slen
+ n
+ 1);
1495 slen
+= xsnprintf(s
+ slen
, n
+ 1, "%s,", cfg_files
[i
]);
1498 return (xstrdup(""));
1503 /* Callback for cursor_flag. */
1505 format_cb_cursor_flag(struct format_tree
*ft
)
1507 if (ft
->wp
!= NULL
) {
1508 if (ft
->wp
->base
.mode
& MODE_CURSOR
)
1509 return (xstrdup("1"));
1510 return (xstrdup("0"));
1515 /* Callback for cursor_x. */
1517 format_cb_cursor_x(struct format_tree
*ft
)
1520 return (format_printf("%u", ft
->wp
->base
.cx
));
1524 /* Callback for cursor_y. */
1526 format_cb_cursor_y(struct format_tree
*ft
)
1529 return (format_printf("%u", ft
->wp
->base
.cy
));
1533 /* Callback for history_limit. */
1535 format_cb_history_limit(struct format_tree
*ft
)
1538 return (format_printf("%u", ft
->wp
->base
.grid
->hlimit
));
1542 /* Callback for history_size. */
1544 format_cb_history_size(struct format_tree
*ft
)
1547 return (format_printf("%u", ft
->wp
->base
.grid
->hsize
));
1551 /* Callback for insert_flag. */
1553 format_cb_insert_flag(struct format_tree
*ft
)
1555 if (ft
->wp
!= NULL
) {
1556 if (ft
->wp
->base
.mode
& MODE_INSERT
)
1557 return (xstrdup("1"));
1558 return (xstrdup("0"));
1563 /* Callback for keypad_cursor_flag. */
1565 format_cb_keypad_cursor_flag(struct format_tree
*ft
)
1567 if (ft
->wp
!= NULL
) {
1568 if (ft
->wp
->base
.mode
& MODE_KCURSOR
)
1569 return (xstrdup("1"));
1570 return (xstrdup("0"));
1575 /* Callback for keypad_flag. */
1577 format_cb_keypad_flag(struct format_tree
*ft
)
1579 if (ft
->wp
!= NULL
) {
1580 if (ft
->wp
->base
.mode
& MODE_KKEYPAD
)
1581 return (xstrdup("1"));
1582 return (xstrdup("0"));
1587 /* Callback for mouse_all_flag. */
1589 format_cb_mouse_all_flag(struct format_tree
*ft
)
1591 if (ft
->wp
!= NULL
) {
1592 if (ft
->wp
->base
.mode
& MODE_MOUSE_ALL
)
1593 return (xstrdup("1"));
1594 return (xstrdup("0"));
1599 /* Callback for mouse_any_flag. */
1601 format_cb_mouse_any_flag(struct format_tree
*ft
)
1603 if (ft
->wp
!= NULL
) {
1604 if (ft
->wp
->base
.mode
& ALL_MOUSE_MODES
)
1605 return (xstrdup("1"));
1606 return (xstrdup("0"));
1611 /* Callback for mouse_button_flag. */
1613 format_cb_mouse_button_flag(struct format_tree
*ft
)
1615 if (ft
->wp
!= NULL
) {
1616 if (ft
->wp
->base
.mode
& MODE_MOUSE_BUTTON
)
1617 return (xstrdup("1"));
1618 return (xstrdup("0"));
1623 /* Callback for mouse_pane. */
1625 format_cb_mouse_pane(struct format_tree
*ft
)
1627 struct window_pane
*wp
;
1630 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1632 return (format_printf("%%%u", wp
->id
));
1638 /* Callback for mouse_sgr_flag. */
1640 format_cb_mouse_sgr_flag(struct format_tree
*ft
)
1642 if (ft
->wp
!= NULL
) {
1643 if (ft
->wp
->base
.mode
& MODE_MOUSE_SGR
)
1644 return (xstrdup("1"));
1645 return (xstrdup("0"));
1650 /* Callback for mouse_standard_flag. */
1652 format_cb_mouse_standard_flag(struct format_tree
*ft
)
1654 if (ft
->wp
!= NULL
) {
1655 if (ft
->wp
->base
.mode
& MODE_MOUSE_STANDARD
)
1656 return (xstrdup("1"));
1657 return (xstrdup("0"));
1662 /* Callback for mouse_utf8_flag. */
1664 format_cb_mouse_utf8_flag(struct format_tree
*ft
)
1666 if (ft
->wp
!= NULL
) {
1667 if (ft
->wp
->base
.mode
& MODE_MOUSE_UTF8
)
1668 return (xstrdup("1"));
1669 return (xstrdup("0"));
1674 /* Callback for mouse_x. */
1676 format_cb_mouse_x(struct format_tree
*ft
)
1678 struct window_pane
*wp
;
1683 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1684 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1685 return (format_printf("%u", x
));
1686 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1687 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1688 return (format_printf("%u", ft
->m
.x
));
1689 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1690 return (format_printf("%u", ft
->m
.x
));
1695 /* Callback for mouse_y. */
1697 format_cb_mouse_y(struct format_tree
*ft
)
1699 struct window_pane
*wp
;
1704 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1705 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1706 return (format_printf("%u", y
));
1707 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1708 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1709 return (format_printf("%u", ft
->m
.y
));
1710 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1711 return (format_printf("%u", ft
->m
.y
- ft
->m
.statusat
));
1716 /* Callback for next_session_id. */
1718 format_cb_next_session_id(__unused
struct format_tree
*ft
)
1720 return (format_printf("$%u", next_session_id
));
1723 /* Callback for origin_flag. */
1725 format_cb_origin_flag(struct format_tree
*ft
)
1727 if (ft
->wp
!= NULL
) {
1728 if (ft
->wp
->base
.mode
& MODE_ORIGIN
)
1729 return (xstrdup("1"));
1730 return (xstrdup("0"));
1735 /* Callback for pane_active. */
1737 format_cb_pane_active(struct format_tree
*ft
)
1739 if (ft
->wp
!= NULL
) {
1740 if (ft
->wp
== ft
->wp
->window
->active
)
1741 return (xstrdup("1"));
1742 return (xstrdup("0"));
1747 /* Callback for pane_at_left. */
1749 format_cb_pane_at_left(struct format_tree
*ft
)
1751 if (ft
->wp
!= NULL
) {
1752 if (ft
->wp
->xoff
== 0)
1753 return (xstrdup("1"));
1754 return (xstrdup("0"));
1759 /* Callback for pane_at_right. */
1761 format_cb_pane_at_right(struct format_tree
*ft
)
1763 if (ft
->wp
!= NULL
) {
1764 if (ft
->wp
->xoff
+ ft
->wp
->sx
== ft
->wp
->window
->sx
)
1765 return (xstrdup("1"));
1766 return (xstrdup("0"));
1771 /* Callback for pane_bottom. */
1773 format_cb_pane_bottom(struct format_tree
*ft
)
1776 return (format_printf("%u", ft
->wp
->yoff
+ ft
->wp
->sy
- 1));
1780 /* Callback for pane_dead. */
1782 format_cb_pane_dead(struct format_tree
*ft
)
1784 if (ft
->wp
!= NULL
) {
1785 if (ft
->wp
->fd
== -1)
1786 return (xstrdup("1"));
1787 return (xstrdup("0"));
1792 /* Callback for pane_dead_signal. */
1794 format_cb_pane_dead_signal(struct format_tree
*ft
)
1796 struct window_pane
*wp
= ft
->wp
;
1800 if ((wp
->flags
& PANE_STATUSREADY
) && WIFSIGNALED(wp
->status
)) {
1801 name
= sig2name(WTERMSIG(wp
->status
));
1802 return (format_printf("%s", name
));
1809 /* Callback for pane_dead_status. */
1811 format_cb_pane_dead_status(struct format_tree
*ft
)
1813 struct window_pane
*wp
= ft
->wp
;
1816 if ((wp
->flags
& PANE_STATUSREADY
) && WIFEXITED(wp
->status
))
1817 return (format_printf("%d", WEXITSTATUS(wp
->status
)));
1823 /* Callback for pane_dead_time. */
1825 format_cb_pane_dead_time(struct format_tree
*ft
)
1827 struct window_pane
*wp
= ft
->wp
;
1830 if (wp
->flags
& PANE_STATUSDRAWN
)
1831 return (&wp
->dead_time
);
1837 /* Callback for pane_format. */
1839 format_cb_pane_format(struct format_tree
*ft
)
1841 if (ft
->type
== FORMAT_TYPE_PANE
)
1842 return (xstrdup("1"));
1843 return (xstrdup("0"));
1846 /* Callback for pane_height. */
1848 format_cb_pane_height(struct format_tree
*ft
)
1851 return (format_printf("%u", ft
->wp
->sy
));
1855 /* Callback for pane_id. */
1857 format_cb_pane_id(struct format_tree
*ft
)
1860 return (format_printf("%%%u", ft
->wp
->id
));
1864 /* Callback for pane_index. */
1866 format_cb_pane_index(struct format_tree
*ft
)
1870 if (ft
->wp
!= NULL
&& window_pane_index(ft
->wp
, &idx
) == 0)
1871 return (format_printf("%u", idx
));
1875 /* Callback for pane_input_off. */
1877 format_cb_pane_input_off(struct format_tree
*ft
)
1879 if (ft
->wp
!= NULL
) {
1880 if (ft
->wp
->flags
& PANE_INPUTOFF
)
1881 return (xstrdup("1"));
1882 return (xstrdup("0"));
1887 /* Callback for pane_last. */
1889 format_cb_pane_last(struct format_tree
*ft
)
1891 if (ft
->wp
!= NULL
) {
1892 if (ft
->wp
== ft
->wp
->window
->last
)
1893 return (xstrdup("1"));
1894 return (xstrdup("0"));
1899 /* Callback for pane_left. */
1901 format_cb_pane_left(struct format_tree
*ft
)
1904 return (format_printf("%u", ft
->wp
->xoff
));
1908 /* Callback for pane_marked. */
1910 format_cb_pane_marked(struct format_tree
*ft
)
1912 if (ft
->wp
!= NULL
) {
1913 if (server_check_marked() && marked_pane
.wp
== ft
->wp
)
1914 return (xstrdup("1"));
1915 return (xstrdup("0"));
1920 /* Callback for pane_marked_set. */
1922 format_cb_pane_marked_set(struct format_tree
*ft
)
1924 if (ft
->wp
!= NULL
) {
1925 if (server_check_marked())
1926 return (xstrdup("1"));
1927 return (xstrdup("0"));
1932 /* Callback for pane_mode. */
1934 format_cb_pane_mode(struct format_tree
*ft
)
1936 struct window_mode_entry
*wme
;
1938 if (ft
->wp
!= NULL
) {
1939 wme
= TAILQ_FIRST(&ft
->wp
->modes
);
1941 return (xstrdup(wme
->mode
->name
));
1947 /* Callback for pane_path. */
1949 format_cb_pane_path(struct format_tree
*ft
)
1951 if (ft
->wp
!= NULL
) {
1952 if (ft
->wp
->base
.path
== NULL
)
1953 return (xstrdup(""));
1954 return (xstrdup(ft
->wp
->base
.path
));
1959 /* Callback for pane_pid. */
1961 format_cb_pane_pid(struct format_tree
*ft
)
1964 return (format_printf("%ld", (long)ft
->wp
->pid
));
1968 /* Callback for pane_pipe. */
1970 format_cb_pane_pipe(struct format_tree
*ft
)
1972 if (ft
->wp
!= NULL
) {
1973 if (ft
->wp
->pipe_fd
!= -1)
1974 return (xstrdup("1"));
1975 return (xstrdup("0"));
1980 /* Callback for pane_right. */
1982 format_cb_pane_right(struct format_tree
*ft
)
1985 return (format_printf("%u", ft
->wp
->xoff
+ ft
->wp
->sx
- 1));
1989 /* Callback for pane_search_string. */
1991 format_cb_pane_search_string(struct format_tree
*ft
)
1993 if (ft
->wp
!= NULL
) {
1994 if (ft
->wp
->searchstr
== NULL
)
1995 return (xstrdup(""));
1996 return (xstrdup(ft
->wp
->searchstr
));
2001 /* Callback for pane_synchronized. */
2003 format_cb_pane_synchronized(struct format_tree
*ft
)
2005 if (ft
->wp
!= NULL
) {
2006 if (options_get_number(ft
->wp
->options
, "synchronize-panes"))
2007 return (xstrdup("1"));
2008 return (xstrdup("0"));
2013 /* Callback for pane_title. */
2015 format_cb_pane_title(struct format_tree
*ft
)
2018 return (xstrdup(ft
->wp
->base
.title
));
2022 /* Callback for pane_top. */
2024 format_cb_pane_top(struct format_tree
*ft
)
2027 return (format_printf("%u", ft
->wp
->yoff
));
2031 /* Callback for pane_tty. */
2033 format_cb_pane_tty(struct format_tree
*ft
)
2036 return (xstrdup(ft
->wp
->tty
));
2040 /* Callback for pane_width. */
2042 format_cb_pane_width(struct format_tree
*ft
)
2045 return (format_printf("%u", ft
->wp
->sx
));
2049 /* Callback for scroll_region_lower. */
2051 format_cb_scroll_region_lower(struct format_tree
*ft
)
2054 return (format_printf("%u", ft
->wp
->base
.rlower
));
2058 /* Callback for scroll_region_upper. */
2060 format_cb_scroll_region_upper(struct format_tree
*ft
)
2063 return (format_printf("%u", ft
->wp
->base
.rupper
));
2067 /* Callback for session_attached. */
2069 format_cb_session_attached(struct format_tree
*ft
)
2072 return (format_printf("%u", ft
->s
->attached
));
2076 /* Callback for session_format. */
2078 format_cb_session_format(struct format_tree
*ft
)
2080 if (ft
->type
== FORMAT_TYPE_SESSION
)
2081 return (xstrdup("1"));
2082 return (xstrdup("0"));
2085 /* Callback for session_group. */
2087 format_cb_session_group(struct format_tree
*ft
)
2089 struct session_group
*sg
;
2091 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2092 return (xstrdup(sg
->name
));
2096 /* Callback for session_group_attached. */
2098 format_cb_session_group_attached(struct format_tree
*ft
)
2100 struct session_group
*sg
;
2102 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2103 return (format_printf("%u", session_group_attached_count (sg
)));
2107 /* Callback for session_group_many_attached. */
2109 format_cb_session_group_many_attached(struct format_tree
*ft
)
2111 struct session_group
*sg
;
2113 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
) {
2114 if (session_group_attached_count (sg
) > 1)
2115 return (xstrdup("1"));
2116 return (xstrdup("0"));
2121 /* Callback for session_group_size. */
2123 format_cb_session_group_size(struct format_tree
*ft
)
2125 struct session_group
*sg
;
2127 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2128 return (format_printf("%u", session_group_count (sg
)));
2132 /* Callback for session_grouped. */
2134 format_cb_session_grouped(struct format_tree
*ft
)
2136 if (ft
->s
!= NULL
) {
2137 if (session_group_contains(ft
->s
) != NULL
)
2138 return (xstrdup("1"));
2139 return (xstrdup("0"));
2144 /* Callback for session_id. */
2146 format_cb_session_id(struct format_tree
*ft
)
2149 return (format_printf("$%u", ft
->s
->id
));
2153 /* Callback for session_many_attached. */
2155 format_cb_session_many_attached(struct format_tree
*ft
)
2157 if (ft
->s
!= NULL
) {
2158 if (ft
->s
->attached
> 1)
2159 return (xstrdup("1"));
2160 return (xstrdup("0"));
2165 /* Callback for session_marked. */
2167 format_cb_session_marked(struct format_tree
*ft
)
2169 if (ft
->s
!= NULL
) {
2170 if (server_check_marked() && marked_pane
.s
== ft
->s
)
2171 return (xstrdup("1"));
2172 return (xstrdup("0"));
2177 /* Callback for session_name. */
2179 format_cb_session_name(struct format_tree
*ft
)
2182 return (xstrdup(ft
->s
->name
));
2186 /* Callback for session_path. */
2188 format_cb_session_path(struct format_tree
*ft
)
2191 return (xstrdup(ft
->s
->cwd
));
2195 /* Callback for session_windows. */
2197 format_cb_session_windows(struct format_tree
*ft
)
2200 return (format_printf("%u", winlink_count(&ft
->s
->windows
)));
2204 /* Callback for socket_path. */
2206 format_cb_socket_path(__unused
struct format_tree
*ft
)
2208 return (xstrdup(socket_path
));
2211 /* Callback for version. */
2213 format_cb_version(__unused
struct format_tree
*ft
)
2215 return (xstrdup(getversion()));
2218 /* Callback for active_window_index. */
2220 format_cb_active_window_index(struct format_tree
*ft
)
2223 return (format_printf("%u", ft
->s
->curw
->idx
));
2227 /* Callback for last_window_index. */
2229 format_cb_last_window_index(struct format_tree
*ft
)
2233 if (ft
->s
!= NULL
) {
2234 wl
= RB_MAX(winlinks
, &ft
->s
->windows
);
2235 return (format_printf("%u", wl
->idx
));
2240 /* Callback for window_active. */
2242 format_cb_window_active(struct format_tree
*ft
)
2244 if (ft
->wl
!= NULL
) {
2245 if (ft
->wl
== ft
->wl
->session
->curw
)
2246 return (xstrdup("1"));
2247 return (xstrdup("0"));
2252 /* Callback for window_activity_flag. */
2254 format_cb_window_activity_flag(struct format_tree
*ft
)
2256 if (ft
->wl
!= NULL
) {
2257 if (ft
->wl
->flags
& WINLINK_ACTIVITY
)
2258 return (xstrdup("1"));
2259 return (xstrdup("0"));
2264 /* Callback for window_bell_flag. */
2266 format_cb_window_bell_flag(struct format_tree
*ft
)
2268 if (ft
->wl
!= NULL
) {
2269 if (ft
->wl
->flags
& WINLINK_BELL
)
2270 return (xstrdup("1"));
2271 return (xstrdup("0"));
2276 /* Callback for window_bigger. */
2278 format_cb_window_bigger(struct format_tree
*ft
)
2280 u_int ox
, oy
, sx
, sy
;
2282 if (ft
->c
!= NULL
) {
2283 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2284 return (xstrdup("1"));
2285 return (xstrdup("0"));
2290 /* Callback for window_cell_height. */
2292 format_cb_window_cell_height(struct format_tree
*ft
)
2295 return (format_printf("%u", ft
->w
->ypixel
));
2299 /* Callback for window_cell_width. */
2301 format_cb_window_cell_width(struct format_tree
*ft
)
2304 return (format_printf("%u", ft
->w
->xpixel
));
2308 /* Callback for window_end_flag. */
2310 format_cb_window_end_flag(struct format_tree
*ft
)
2312 if (ft
->wl
!= NULL
) {
2313 if (ft
->wl
== RB_MAX(winlinks
, &ft
->wl
->session
->windows
))
2314 return (xstrdup("1"));
2315 return (xstrdup("0"));
2320 /* Callback for window_flags. */
2322 format_cb_window_flags(struct format_tree
*ft
)
2325 return (xstrdup(window_printable_flags(ft
->wl
, 1)));
2329 /* Callback for window_format. */
2331 format_cb_window_format(struct format_tree
*ft
)
2333 if (ft
->type
== FORMAT_TYPE_WINDOW
)
2334 return (xstrdup("1"));
2335 return (xstrdup("0"));
2338 /* Callback for window_height. */
2340 format_cb_window_height(struct format_tree
*ft
)
2343 return (format_printf("%u", ft
->w
->sy
));
2347 /* Callback for window_id. */
2349 format_cb_window_id(struct format_tree
*ft
)
2352 return (format_printf("@%u", ft
->w
->id
));
2356 /* Callback for window_index. */
2358 format_cb_window_index(struct format_tree
*ft
)
2361 return (format_printf("%d", ft
->wl
->idx
));
2365 /* Callback for window_last_flag. */
2367 format_cb_window_last_flag(struct format_tree
*ft
)
2369 if (ft
->wl
!= NULL
) {
2370 if (ft
->wl
== TAILQ_FIRST(&ft
->wl
->session
->lastw
))
2371 return (xstrdup("1"));
2372 return (xstrdup("0"));
2377 /* Callback for window_linked. */
2379 format_cb_window_linked(struct format_tree
*ft
)
2381 if (ft
->wl
!= NULL
) {
2382 if (session_is_linked(ft
->wl
->session
, ft
->wl
->window
))
2383 return (xstrdup("1"));
2384 return (xstrdup("0"));
2389 /* Callback for window_linked_sessions. */
2391 format_cb_window_linked_sessions(struct format_tree
*ft
)
2394 return (format_printf("%u", ft
->wl
->window
->references
));
2398 /* Callback for window_marked_flag. */
2400 format_cb_window_marked_flag(struct format_tree
*ft
)
2402 if (ft
->wl
!= NULL
) {
2403 if (server_check_marked() && marked_pane
.wl
== ft
->wl
)
2404 return (xstrdup("1"));
2405 return (xstrdup("0"));
2410 /* Callback for window_name. */
2412 format_cb_window_name(struct format_tree
*ft
)
2415 return (format_printf("%s", ft
->w
->name
));
2419 /* Callback for window_offset_x. */
2421 format_cb_window_offset_x(struct format_tree
*ft
)
2423 u_int ox
, oy
, sx
, sy
;
2425 if (ft
->c
!= NULL
) {
2426 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2427 return (format_printf("%u", ox
));
2433 /* Callback for window_offset_y. */
2435 format_cb_window_offset_y(struct format_tree
*ft
)
2437 u_int ox
, oy
, sx
, sy
;
2439 if (ft
->c
!= NULL
) {
2440 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2441 return (format_printf("%u", oy
));
2447 /* Callback for window_panes. */
2449 format_cb_window_panes(struct format_tree
*ft
)
2452 return (format_printf("%u", window_count_panes(ft
->w
)));
2456 /* Callback for window_raw_flags. */
2458 format_cb_window_raw_flags(struct format_tree
*ft
)
2461 return (xstrdup(window_printable_flags(ft
->wl
, 0)));
2465 /* Callback for window_silence_flag. */
2467 format_cb_window_silence_flag(struct format_tree
*ft
)
2469 if (ft
->wl
!= NULL
) {
2470 if (ft
->wl
->flags
& WINLINK_SILENCE
)
2471 return (xstrdup("1"));
2472 return (xstrdup("0"));
2477 /* Callback for window_start_flag. */
2479 format_cb_window_start_flag(struct format_tree
*ft
)
2481 if (ft
->wl
!= NULL
) {
2482 if (ft
->wl
== RB_MIN(winlinks
, &ft
->wl
->session
->windows
))
2483 return (xstrdup("1"));
2484 return (xstrdup("0"));
2489 /* Callback for window_width. */
2491 format_cb_window_width(struct format_tree
*ft
)
2494 return (format_printf("%u", ft
->w
->sx
));
2498 /* Callback for window_zoomed_flag. */
2500 format_cb_window_zoomed_flag(struct format_tree
*ft
)
2502 if (ft
->w
!= NULL
) {
2503 if (ft
->w
->flags
& WINDOW_ZOOMED
)
2504 return (xstrdup("1"));
2505 return (xstrdup("0"));
2510 /* Callback for wrap_flag. */
2512 format_cb_wrap_flag(struct format_tree
*ft
)
2514 if (ft
->wp
!= NULL
) {
2515 if (ft
->wp
->base
.mode
& MODE_WRAP
)
2516 return (xstrdup("1"));
2517 return (xstrdup("0"));
2522 /* Callback for buffer_created. */
2524 format_cb_buffer_created(struct format_tree
*ft
)
2526 static struct timeval tv
;
2528 if (ft
->pb
!= NULL
) {
2530 tv
.tv_sec
= paste_buffer_created(ft
->pb
);
2536 /* Callback for client_activity. */
2538 format_cb_client_activity(struct format_tree
*ft
)
2541 return (&ft
->c
->activity_time
);
2545 /* Callback for client_created. */
2547 format_cb_client_created(struct format_tree
*ft
)
2550 return (&ft
->c
->creation_time
);
2554 /* Callback for session_activity. */
2556 format_cb_session_activity(struct format_tree
*ft
)
2559 return (&ft
->s
->activity_time
);
2563 /* Callback for session_created. */
2565 format_cb_session_created(struct format_tree
*ft
)
2568 return (&ft
->s
->creation_time
);
2572 /* Callback for session_last_attached. */
2574 format_cb_session_last_attached(struct format_tree
*ft
)
2577 return (&ft
->s
->last_attached_time
);
2581 /* Callback for start_time. */
2583 format_cb_start_time(__unused
struct format_tree
*ft
)
2585 return (&start_time
);
2588 /* Callback for window_activity. */
2590 format_cb_window_activity(struct format_tree
*ft
)
2593 return (&ft
->w
->activity_time
);
2597 /* Callback for buffer_mode_format, */
2599 format_cb_buffer_mode_format(__unused
struct format_tree
*ft
)
2601 return (xstrdup(window_buffer_mode
.default_format
));
2604 /* Callback for client_mode_format, */
2606 format_cb_client_mode_format(__unused
struct format_tree
*ft
)
2608 return (xstrdup(window_client_mode
.default_format
));
2611 /* Callback for tree_mode_format, */
2613 format_cb_tree_mode_format(__unused
struct format_tree
*ft
)
2615 return (xstrdup(window_tree_mode
.default_format
));
2618 /* Callback for uid. */
2620 format_cb_uid(__unused
struct format_tree
*ft
)
2622 return (format_printf("%ld", (long)getuid()));
2625 /* Callback for user. */
2627 format_cb_user(__unused
struct format_tree
*ft
)
2631 if ((pw
= getpwuid(getuid())) != NULL
)
2632 return (xstrdup(pw
->pw_name
));
2636 /* Format table type. */
2637 enum format_table_type
{
2638 FORMAT_TABLE_STRING
,
2642 /* Format table entry. */
2643 struct format_table_entry
{
2645 enum format_table_type type
;
2650 * Format table. Default format variables (that are almost always in the tree
2651 * and where the value is expanded by a callback in this file) are listed here.
2652 * Only variables which are added by the caller go into the tree.
2654 static const struct format_table_entry format_table
[] = {
2655 { "active_window_index", FORMAT_TABLE_STRING
,
2656 format_cb_active_window_index
2658 { "alternate_on", FORMAT_TABLE_STRING
,
2659 format_cb_alternate_on
2661 { "alternate_saved_x", FORMAT_TABLE_STRING
,
2662 format_cb_alternate_saved_x
2664 { "alternate_saved_y", FORMAT_TABLE_STRING
,
2665 format_cb_alternate_saved_y
2667 { "buffer_created", FORMAT_TABLE_TIME
,
2668 format_cb_buffer_created
2670 { "buffer_mode_format", FORMAT_TABLE_STRING
,
2671 format_cb_buffer_mode_format
2673 { "buffer_name", FORMAT_TABLE_STRING
,
2674 format_cb_buffer_name
2676 { "buffer_sample", FORMAT_TABLE_STRING
,
2677 format_cb_buffer_sample
2679 { "buffer_size", FORMAT_TABLE_STRING
,
2680 format_cb_buffer_size
2682 { "client_activity", FORMAT_TABLE_TIME
,
2683 format_cb_client_activity
2685 { "client_cell_height", FORMAT_TABLE_STRING
,
2686 format_cb_client_cell_height
2688 { "client_cell_width", FORMAT_TABLE_STRING
,
2689 format_cb_client_cell_width
2691 { "client_control_mode", FORMAT_TABLE_STRING
,
2692 format_cb_client_control_mode
2694 { "client_created", FORMAT_TABLE_TIME
,
2695 format_cb_client_created
2697 { "client_discarded", FORMAT_TABLE_STRING
,
2698 format_cb_client_discarded
2700 { "client_flags", FORMAT_TABLE_STRING
,
2701 format_cb_client_flags
2703 { "client_height", FORMAT_TABLE_STRING
,
2704 format_cb_client_height
2706 { "client_key_table", FORMAT_TABLE_STRING
,
2707 format_cb_client_key_table
2709 { "client_last_session", FORMAT_TABLE_STRING
,
2710 format_cb_client_last_session
2712 { "client_mode_format", FORMAT_TABLE_STRING
,
2713 format_cb_client_mode_format
2715 { "client_name", FORMAT_TABLE_STRING
,
2716 format_cb_client_name
2718 { "client_pid", FORMAT_TABLE_STRING
,
2719 format_cb_client_pid
2721 { "client_prefix", FORMAT_TABLE_STRING
,
2722 format_cb_client_prefix
2724 { "client_readonly", FORMAT_TABLE_STRING
,
2725 format_cb_client_readonly
2727 { "client_session", FORMAT_TABLE_STRING
,
2728 format_cb_client_session
2730 { "client_termfeatures", FORMAT_TABLE_STRING
,
2731 format_cb_client_termfeatures
2733 { "client_termname", FORMAT_TABLE_STRING
,
2734 format_cb_client_termname
2736 { "client_termtype", FORMAT_TABLE_STRING
,
2737 format_cb_client_termtype
2739 { "client_tty", FORMAT_TABLE_STRING
,
2740 format_cb_client_tty
2742 { "client_uid", FORMAT_TABLE_STRING
,
2743 format_cb_client_uid
2745 { "client_user", FORMAT_TABLE_STRING
,
2746 format_cb_client_user
2748 { "client_utf8", FORMAT_TABLE_STRING
,
2749 format_cb_client_utf8
2751 { "client_width", FORMAT_TABLE_STRING
,
2752 format_cb_client_width
2754 { "client_written", FORMAT_TABLE_STRING
,
2755 format_cb_client_written
2757 { "config_files", FORMAT_TABLE_STRING
,
2758 format_cb_config_files
2760 { "cursor_character", FORMAT_TABLE_STRING
,
2761 format_cb_cursor_character
2763 { "cursor_flag", FORMAT_TABLE_STRING
,
2764 format_cb_cursor_flag
2766 { "cursor_x", FORMAT_TABLE_STRING
,
2769 { "cursor_y", FORMAT_TABLE_STRING
,
2772 { "history_all_bytes", FORMAT_TABLE_STRING
,
2773 format_cb_history_all_bytes
2775 { "history_bytes", FORMAT_TABLE_STRING
,
2776 format_cb_history_bytes
2778 { "history_limit", FORMAT_TABLE_STRING
,
2779 format_cb_history_limit
2781 { "history_size", FORMAT_TABLE_STRING
,
2782 format_cb_history_size
2784 { "host", FORMAT_TABLE_STRING
,
2787 { "host_short", FORMAT_TABLE_STRING
,
2788 format_cb_host_short
2790 { "insert_flag", FORMAT_TABLE_STRING
,
2791 format_cb_insert_flag
2793 { "keypad_cursor_flag", FORMAT_TABLE_STRING
,
2794 format_cb_keypad_cursor_flag
2796 { "keypad_flag", FORMAT_TABLE_STRING
,
2797 format_cb_keypad_flag
2799 { "last_window_index", FORMAT_TABLE_STRING
,
2800 format_cb_last_window_index
2802 { "mouse_all_flag", FORMAT_TABLE_STRING
,
2803 format_cb_mouse_all_flag
2805 { "mouse_any_flag", FORMAT_TABLE_STRING
,
2806 format_cb_mouse_any_flag
2808 { "mouse_button_flag", FORMAT_TABLE_STRING
,
2809 format_cb_mouse_button_flag
2811 { "mouse_hyperlink", FORMAT_TABLE_STRING
,
2812 format_cb_mouse_hyperlink
2814 { "mouse_line", FORMAT_TABLE_STRING
,
2815 format_cb_mouse_line
2817 { "mouse_pane", FORMAT_TABLE_STRING
,
2818 format_cb_mouse_pane
2820 { "mouse_sgr_flag", FORMAT_TABLE_STRING
,
2821 format_cb_mouse_sgr_flag
2823 { "mouse_standard_flag", FORMAT_TABLE_STRING
,
2824 format_cb_mouse_standard_flag
2826 { "mouse_utf8_flag", FORMAT_TABLE_STRING
,
2827 format_cb_mouse_utf8_flag
2829 { "mouse_word", FORMAT_TABLE_STRING
,
2830 format_cb_mouse_word
2832 { "mouse_x", FORMAT_TABLE_STRING
,
2835 { "mouse_y", FORMAT_TABLE_STRING
,
2838 { "next_session_id", FORMAT_TABLE_STRING
,
2839 format_cb_next_session_id
2841 { "origin_flag", FORMAT_TABLE_STRING
,
2842 format_cb_origin_flag
2844 { "pane_active", FORMAT_TABLE_STRING
,
2845 format_cb_pane_active
2847 { "pane_at_bottom", FORMAT_TABLE_STRING
,
2848 format_cb_pane_at_bottom
2850 { "pane_at_left", FORMAT_TABLE_STRING
,
2851 format_cb_pane_at_left
2853 { "pane_at_right", FORMAT_TABLE_STRING
,
2854 format_cb_pane_at_right
2856 { "pane_at_top", FORMAT_TABLE_STRING
,
2857 format_cb_pane_at_top
2859 { "pane_bg", FORMAT_TABLE_STRING
,
2862 { "pane_bottom", FORMAT_TABLE_STRING
,
2863 format_cb_pane_bottom
2865 { "pane_current_command", FORMAT_TABLE_STRING
,
2866 format_cb_current_command
2868 { "pane_current_path", FORMAT_TABLE_STRING
,
2869 format_cb_current_path
2871 { "pane_dead", FORMAT_TABLE_STRING
,
2874 { "pane_dead_signal", FORMAT_TABLE_STRING
,
2875 format_cb_pane_dead_signal
2877 { "pane_dead_status", FORMAT_TABLE_STRING
,
2878 format_cb_pane_dead_status
2880 { "pane_dead_time", FORMAT_TABLE_TIME
,
2881 format_cb_pane_dead_time
2883 { "pane_fg", FORMAT_TABLE_STRING
,
2886 { "pane_format", FORMAT_TABLE_STRING
,
2887 format_cb_pane_format
2889 { "pane_height", FORMAT_TABLE_STRING
,
2890 format_cb_pane_height
2892 { "pane_id", FORMAT_TABLE_STRING
,
2895 { "pane_in_mode", FORMAT_TABLE_STRING
,
2896 format_cb_pane_in_mode
2898 { "pane_index", FORMAT_TABLE_STRING
,
2899 format_cb_pane_index
2901 { "pane_input_off", FORMAT_TABLE_STRING
,
2902 format_cb_pane_input_off
2904 { "pane_last", FORMAT_TABLE_STRING
,
2907 { "pane_left", FORMAT_TABLE_STRING
,
2910 { "pane_marked", FORMAT_TABLE_STRING
,
2911 format_cb_pane_marked
2913 { "pane_marked_set", FORMAT_TABLE_STRING
,
2914 format_cb_pane_marked_set
2916 { "pane_mode", FORMAT_TABLE_STRING
,
2919 { "pane_path", FORMAT_TABLE_STRING
,
2922 { "pane_pid", FORMAT_TABLE_STRING
,
2925 { "pane_pipe", FORMAT_TABLE_STRING
,
2928 { "pane_right", FORMAT_TABLE_STRING
,
2929 format_cb_pane_right
2931 { "pane_search_string", FORMAT_TABLE_STRING
,
2932 format_cb_pane_search_string
2934 { "pane_start_command", FORMAT_TABLE_STRING
,
2935 format_cb_start_command
2937 { "pane_start_path", FORMAT_TABLE_STRING
,
2938 format_cb_start_path
2940 { "pane_synchronized", FORMAT_TABLE_STRING
,
2941 format_cb_pane_synchronized
2943 { "pane_tabs", FORMAT_TABLE_STRING
,
2946 { "pane_title", FORMAT_TABLE_STRING
,
2947 format_cb_pane_title
2949 { "pane_top", FORMAT_TABLE_STRING
,
2952 { "pane_tty", FORMAT_TABLE_STRING
,
2955 { "pane_width", FORMAT_TABLE_STRING
,
2956 format_cb_pane_width
2958 { "pid", FORMAT_TABLE_STRING
,
2961 { "scroll_region_lower", FORMAT_TABLE_STRING
,
2962 format_cb_scroll_region_lower
2964 { "scroll_region_upper", FORMAT_TABLE_STRING
,
2965 format_cb_scroll_region_upper
2967 { "session_activity", FORMAT_TABLE_TIME
,
2968 format_cb_session_activity
2970 { "session_alerts", FORMAT_TABLE_STRING
,
2971 format_cb_session_alerts
2973 { "session_attached", FORMAT_TABLE_STRING
,
2974 format_cb_session_attached
2976 { "session_attached_list", FORMAT_TABLE_STRING
,
2977 format_cb_session_attached_list
2979 { "session_created", FORMAT_TABLE_TIME
,
2980 format_cb_session_created
2982 { "session_format", FORMAT_TABLE_STRING
,
2983 format_cb_session_format
2985 { "session_group", FORMAT_TABLE_STRING
,
2986 format_cb_session_group
2988 { "session_group_attached", FORMAT_TABLE_STRING
,
2989 format_cb_session_group_attached
2991 { "session_group_attached_list", FORMAT_TABLE_STRING
,
2992 format_cb_session_group_attached_list
2994 { "session_group_list", FORMAT_TABLE_STRING
,
2995 format_cb_session_group_list
2997 { "session_group_many_attached", FORMAT_TABLE_STRING
,
2998 format_cb_session_group_many_attached
3000 { "session_group_size", FORMAT_TABLE_STRING
,
3001 format_cb_session_group_size
3003 { "session_grouped", FORMAT_TABLE_STRING
,
3004 format_cb_session_grouped
3006 { "session_id", FORMAT_TABLE_STRING
,
3007 format_cb_session_id
3009 { "session_last_attached", FORMAT_TABLE_TIME
,
3010 format_cb_session_last_attached
3012 { "session_many_attached", FORMAT_TABLE_STRING
,
3013 format_cb_session_many_attached
3015 { "session_marked", FORMAT_TABLE_STRING
,
3016 format_cb_session_marked
,
3018 { "session_name", FORMAT_TABLE_STRING
,
3019 format_cb_session_name
3021 { "session_path", FORMAT_TABLE_STRING
,
3022 format_cb_session_path
3024 { "session_stack", FORMAT_TABLE_STRING
,
3025 format_cb_session_stack
3027 { "session_windows", FORMAT_TABLE_STRING
,
3028 format_cb_session_windows
3030 { "socket_path", FORMAT_TABLE_STRING
,
3031 format_cb_socket_path
3033 { "start_time", FORMAT_TABLE_TIME
,
3034 format_cb_start_time
3036 { "tree_mode_format", FORMAT_TABLE_STRING
,
3037 format_cb_tree_mode_format
3039 { "uid", FORMAT_TABLE_STRING
,
3042 { "user", FORMAT_TABLE_STRING
,
3045 { "version", FORMAT_TABLE_STRING
,
3048 { "window_active", FORMAT_TABLE_STRING
,
3049 format_cb_window_active
3051 { "window_active_clients", FORMAT_TABLE_STRING
,
3052 format_cb_window_active_clients
3054 { "window_active_clients_list", FORMAT_TABLE_STRING
,
3055 format_cb_window_active_clients_list
3057 { "window_active_sessions", FORMAT_TABLE_STRING
,
3058 format_cb_window_active_sessions
3060 { "window_active_sessions_list", FORMAT_TABLE_STRING
,
3061 format_cb_window_active_sessions_list
3063 { "window_activity", FORMAT_TABLE_TIME
,
3064 format_cb_window_activity
3066 { "window_activity_flag", FORMAT_TABLE_STRING
,
3067 format_cb_window_activity_flag
3069 { "window_bell_flag", FORMAT_TABLE_STRING
,
3070 format_cb_window_bell_flag
3072 { "window_bigger", FORMAT_TABLE_STRING
,
3073 format_cb_window_bigger
3075 { "window_cell_height", FORMAT_TABLE_STRING
,
3076 format_cb_window_cell_height
3078 { "window_cell_width", FORMAT_TABLE_STRING
,
3079 format_cb_window_cell_width
3081 { "window_end_flag", FORMAT_TABLE_STRING
,
3082 format_cb_window_end_flag
3084 { "window_flags", FORMAT_TABLE_STRING
,
3085 format_cb_window_flags
3087 { "window_format", FORMAT_TABLE_STRING
,
3088 format_cb_window_format
3090 { "window_height", FORMAT_TABLE_STRING
,
3091 format_cb_window_height
3093 { "window_id", FORMAT_TABLE_STRING
,
3096 { "window_index", FORMAT_TABLE_STRING
,
3097 format_cb_window_index
3099 { "window_last_flag", FORMAT_TABLE_STRING
,
3100 format_cb_window_last_flag
3102 { "window_layout", FORMAT_TABLE_STRING
,
3103 format_cb_window_layout
3105 { "window_linked", FORMAT_TABLE_STRING
,
3106 format_cb_window_linked
3108 { "window_linked_sessions", FORMAT_TABLE_STRING
,
3109 format_cb_window_linked_sessions
3111 { "window_linked_sessions_list", FORMAT_TABLE_STRING
,
3112 format_cb_window_linked_sessions_list
3114 { "window_marked_flag", FORMAT_TABLE_STRING
,
3115 format_cb_window_marked_flag
3117 { "window_name", FORMAT_TABLE_STRING
,
3118 format_cb_window_name
3120 { "window_offset_x", FORMAT_TABLE_STRING
,
3121 format_cb_window_offset_x
3123 { "window_offset_y", FORMAT_TABLE_STRING
,
3124 format_cb_window_offset_y
3126 { "window_panes", FORMAT_TABLE_STRING
,
3127 format_cb_window_panes
3129 { "window_raw_flags", FORMAT_TABLE_STRING
,
3130 format_cb_window_raw_flags
3132 { "window_silence_flag", FORMAT_TABLE_STRING
,
3133 format_cb_window_silence_flag
3135 { "window_stack_index", FORMAT_TABLE_STRING
,
3136 format_cb_window_stack_index
3138 { "window_start_flag", FORMAT_TABLE_STRING
,
3139 format_cb_window_start_flag
3141 { "window_visible_layout", FORMAT_TABLE_STRING
,
3142 format_cb_window_visible_layout
3144 { "window_width", FORMAT_TABLE_STRING
,
3145 format_cb_window_width
3147 { "window_zoomed_flag", FORMAT_TABLE_STRING
,
3148 format_cb_window_zoomed_flag
3150 { "wrap_flag", FORMAT_TABLE_STRING
,
3155 /* Compare format table entries. */
3157 format_table_compare(const void *key0
, const void *entry0
)
3159 const char *key
= key0
;
3160 const struct format_table_entry
*entry
= entry0
;
3162 return (strcmp(key
, entry
->key
));
3165 /* Get a format callback. */
3166 static struct format_table_entry
*
3167 format_table_get(const char *key
)
3169 return (bsearch(key
, format_table
, nitems(format_table
),
3170 sizeof *format_table
, format_table_compare
));
3173 /* Merge one format tree into another. */
3175 format_merge(struct format_tree
*ft
, struct format_tree
*from
)
3177 struct format_entry
*fe
;
3179 RB_FOREACH(fe
, format_entry_tree
, &from
->tree
) {
3180 if (fe
->value
!= NULL
)
3181 format_add(ft
, fe
->key
, "%s", fe
->value
);
3185 /* Get format pane. */
3186 struct window_pane
*
3187 format_get_pane(struct format_tree
*ft
)
3192 /* Add item bits to tree. */
3194 format_create_add_item(struct format_tree
*ft
, struct cmdq_item
*item
)
3196 struct key_event
*event
= cmdq_get_event(item
);
3197 struct mouse_event
*m
= &event
->m
;
3199 cmdq_merge_formats(item
, ft
);
3200 memcpy(&ft
->m
, m
, sizeof ft
->m
);
3203 /* Create a new tree. */
3204 struct format_tree
*
3205 format_create(struct client
*c
, struct cmdq_item
*item
, int tag
, int flags
)
3207 struct format_tree
*ft
;
3209 ft
= xcalloc(1, sizeof *ft
);
3214 ft
->client
->references
++;
3222 format_create_add_item(ft
, item
);
3229 format_free(struct format_tree
*ft
)
3231 struct format_entry
*fe
, *fe1
;
3233 RB_FOREACH_SAFE(fe
, format_entry_tree
, &ft
->tree
, fe1
) {
3234 RB_REMOVE(format_entry_tree
, &ft
->tree
, fe
);
3240 if (ft
->client
!= NULL
)
3241 server_client_unref(ft
->client
);
3245 /* Log each format. */
3247 format_log_debug_cb(const char *key
, const char *value
, void *arg
)
3249 const char *prefix
= arg
;
3251 log_debug("%s: %s=%s", prefix
, key
, value
);
3254 /* Log a format tree. */
3256 format_log_debug(struct format_tree
*ft
, const char *prefix
)
3258 format_each(ft
, format_log_debug_cb
, (void *)prefix
);
3261 /* Walk each format. */
3263 format_each(struct format_tree
*ft
, void (*cb
)(const char *, const char *,
3266 const struct format_table_entry
*fte
;
3267 struct format_entry
*fe
;
3273 for (i
= 0; i
< nitems(format_table
); i
++) {
3274 fte
= &format_table
[i
];
3276 value
= fte
->cb(ft
);
3279 if (fte
->type
== FORMAT_TABLE_TIME
) {
3281 xsnprintf(s
, sizeof s
, "%lld", (long long)tv
->tv_sec
);
3282 cb(fte
->key
, s
, arg
);
3284 cb(fte
->key
, value
, arg
);
3288 RB_FOREACH(fe
, format_entry_tree
, &ft
->tree
) {
3289 if (fe
->time
!= 0) {
3290 xsnprintf(s
, sizeof s
, "%lld", (long long)fe
->time
);
3291 cb(fe
->key
, s
, arg
);
3293 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3294 fe
->value
= fe
->cb(ft
);
3295 if (fe
->value
== NULL
)
3296 fe
->value
= xstrdup("");
3298 cb(fe
->key
, fe
->value
, arg
);
3303 /* Add a key-value pair. */
3305 format_add(struct format_tree
*ft
, const char *key
, const char *fmt
, ...)
3307 struct format_entry
*fe
;
3308 struct format_entry
*fe_now
;
3311 fe
= xmalloc(sizeof *fe
);
3312 fe
->key
= xstrdup(key
);
3314 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3315 if (fe_now
!= NULL
) {
3318 free(fe_now
->value
);
3326 xvasprintf(&fe
->value
, fmt
, ap
);
3330 /* Add a key and time. */
3332 format_add_tv(struct format_tree
*ft
, const char *key
, struct timeval
*tv
)
3334 struct format_entry
*fe
, *fe_now
;
3336 fe
= xmalloc(sizeof *fe
);
3337 fe
->key
= xstrdup(key
);
3339 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3340 if (fe_now
!= NULL
) {
3343 free(fe_now
->value
);
3348 fe
->time
= tv
->tv_sec
;
3353 /* Add a key and function. */
3355 format_add_cb(struct format_tree
*ft
, const char *key
, format_cb cb
)
3357 struct format_entry
*fe
;
3358 struct format_entry
*fe_now
;
3360 fe
= xmalloc(sizeof *fe
);
3361 fe
->key
= xstrdup(key
);
3363 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3364 if (fe_now
!= NULL
) {
3367 free(fe_now
->value
);
3377 /* Quote shell special characters in string. */
3379 format_quote_shell(const char *s
)
3384 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3385 for (cp
= s
; *cp
!= '\0'; cp
++) {
3386 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp
) != NULL
)
3394 /* Quote #s in string. */
3396 format_quote_style(const char *s
)
3401 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3402 for (cp
= s
; *cp
!= '\0'; cp
++) {
3411 /* Make a prettier time. */
3413 format_pretty_time(time_t t
, int seconds
)
3415 struct tm now_tm
, tm
;
3424 localtime_r(&now
, &now_tm
);
3425 localtime_r(&t
, &tm
);
3427 /* Last 24 hours. */
3428 if (age
< 24 * 3600) {
3430 strftime(s
, sizeof s
, "%H:%M:%S", &tm
);
3432 strftime(s
, sizeof s
, "%H:%M", &tm
);
3433 return (xstrdup(s
));
3436 /* This month or last 28 days. */
3437 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
== now_tm
.tm_mon
) ||
3438 age
< 28 * 24 * 3600) {
3439 strftime(s
, sizeof s
, "%a%d", &tm
);
3440 return (xstrdup(s
));
3443 /* Last 12 months. */
3444 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
< now_tm
.tm_mon
) ||
3445 (tm
.tm_year
== now_tm
.tm_year
- 1 && tm
.tm_mon
> now_tm
.tm_mon
)) {
3446 strftime(s
, sizeof s
, "%d%b", &tm
);
3447 return (xstrdup(s
));
3450 /* Older than that. */
3451 strftime(s
, sizeof s
, "%h%y", &tm
);
3452 return (xstrdup(s
));
3455 /* Find a format entry. */
3457 format_find(struct format_tree
*ft
, const char *key
, int modifiers
,
3458 const char *time_format
)
3460 struct format_table_entry
*fte
;
3462 struct format_entry
*fe
, fe_find
;
3463 struct environ_entry
*envent
;
3464 struct options_entry
*o
;
3466 char *found
= NULL
, *saved
, s
[512];
3471 o
= options_parse_get(global_options
, key
, &idx
, 0);
3472 if (o
== NULL
&& ft
->wp
!= NULL
)
3473 o
= options_parse_get(ft
->wp
->options
, key
, &idx
, 0);
3474 if (o
== NULL
&& ft
->w
!= NULL
)
3475 o
= options_parse_get(ft
->w
->options
, key
, &idx
, 0);
3477 o
= options_parse_get(global_w_options
, key
, &idx
, 0);
3478 if (o
== NULL
&& ft
->s
!= NULL
)
3479 o
= options_parse_get(ft
->s
->options
, key
, &idx
, 0);
3481 o
= options_parse_get(global_s_options
, key
, &idx
, 0);
3483 found
= options_to_string(o
, idx
, 1);
3487 fte
= format_table_get(key
);
3489 value
= fte
->cb(ft
);
3490 if (fte
->type
== FORMAT_TABLE_TIME
&& value
!= NULL
)
3491 t
= ((struct timeval
*)value
)->tv_sec
;
3496 fe_find
.key
= (char *)key
;
3497 fe
= RB_FIND(format_entry_tree
, &ft
->tree
, &fe_find
);
3499 if (fe
->time
!= 0) {
3503 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3504 fe
->value
= fe
->cb(ft
);
3505 if (fe
->value
== NULL
)
3506 fe
->value
= xstrdup("");
3508 found
= xstrdup(fe
->value
);
3512 if (~modifiers
& FORMAT_TIMESTRING
) {
3515 envent
= environ_find(ft
->s
->environ
, key
);
3517 envent
= environ_find(global_environ
, key
);
3518 if (envent
!= NULL
&& envent
->value
!= NULL
) {
3519 found
= xstrdup(envent
->value
);
3527 if (modifiers
& FORMAT_TIMESTRING
) {
3528 if (t
== 0 && found
!= NULL
) {
3529 t
= strtonum(found
, 0, INT64_MAX
, &errstr
);
3536 if (modifiers
& FORMAT_PRETTY
)
3537 found
= format_pretty_time(t
, 0);
3539 if (time_format
!= NULL
) {
3540 localtime_r(&t
, &tm
);
3541 strftime(s
, sizeof s
, time_format
, &tm
);
3544 s
[strcspn(s
, "\n")] = '\0';
3552 xasprintf(&found
, "%lld", (long long)t
);
3553 else if (found
== NULL
)
3555 if (modifiers
& FORMAT_BASENAME
) {
3557 found
= xstrdup(basename(saved
));
3560 if (modifiers
& FORMAT_DIRNAME
) {
3562 found
= xstrdup(dirname(saved
));
3565 if (modifiers
& FORMAT_QUOTE_SHELL
) {
3567 found
= format_quote_shell(saved
);
3570 if (modifiers
& FORMAT_QUOTE_STYLE
) {
3572 found
= format_quote_style(saved
);
3578 /* Unescape escaped characters. */
3580 format_unescape(const char *s
)
3585 cp
= out
= xmalloc(strlen(s
) + 1);
3586 for (; *s
!= '\0'; s
++) {
3587 if (*s
== '#' && s
[1] == '{')
3589 if (brackets
== 0 &&
3591 strchr(",#{}:", s
[1]) != NULL
) {
3603 /* Remove escaped characters. */
3605 format_strip(const char *s
)
3610 cp
= out
= xmalloc(strlen(s
) + 1);
3611 for (; *s
!= '\0'; s
++) {
3612 if (*s
== '#' && s
[1] == '{')
3614 if (*s
== '#' && strchr(",#{}:", s
[1]) != NULL
) {
3627 /* Skip until end. */
3629 format_skip(const char *s
, const char *end
)
3633 for (; *s
!= '\0'; s
++) {
3634 if (*s
== '#' && s
[1] == '{')
3636 if (*s
== '#' && strchr(",#{}:", s
[1]) != NULL
) {
3642 if (strchr(end
, *s
) != NULL
&& brackets
== 0)
3650 /* Return left and right alternatives separated by commas. */
3652 format_choose(struct format_expand_state
*es
, const char *s
, char **left
,
3653 char **right
, int expand
)
3656 char *left0
, *right0
;
3658 cp
= format_skip(s
, ",");
3661 left0
= xstrndup(s
, cp
- s
);
3662 right0
= xstrdup(cp
+ 1);
3665 *left
= format_expand1(es
, left0
);
3667 *right
= format_expand1(es
, right0
);
3678 format_true(const char *s
)
3680 if (s
!= NULL
&& *s
!= '\0' && (s
[0] != '0' || s
[1] != '\0'))
3685 /* Check if modifier end. */
3687 format_is_end(char c
)
3689 return (c
== ';' || c
== ':');
3692 /* Add to modifier list. */
3694 format_add_modifier(struct format_modifier
**list
, u_int
*count
,
3695 const char *c
, size_t n
, char **argv
, int argc
)
3697 struct format_modifier
*fm
;
3699 *list
= xreallocarray(*list
, (*count
) + 1, sizeof **list
);
3700 fm
= &(*list
)[(*count
)++];
3702 memcpy(fm
->modifier
, c
, n
);
3703 fm
->modifier
[n
] = '\0';
3710 /* Free modifier list. */
3712 format_free_modifiers(struct format_modifier
*list
, u_int count
)
3716 for (i
= 0; i
< count
; i
++)
3717 cmd_free_argv(list
[i
].argc
, list
[i
].argv
);
3721 /* Build modifier list. */
3722 static struct format_modifier
*
3723 format_build_modifiers(struct format_expand_state
*es
, const char **s
,
3726 const char *cp
= *s
, *end
;
3727 struct format_modifier
*list
= NULL
;
3728 char c
, last
[] = "X;:", **argv
, *value
;
3732 * Modifiers are a ; separated list of the forms:
3733 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3744 while (*cp
!= '\0' && *cp
!= ':') {
3745 /* Skip any separator character. */
3749 /* Check single character modifiers with no arguments. */
3750 if (strchr("labcdnwETSWP<>", cp
[0]) != NULL
&&
3751 format_is_end(cp
[1])) {
3752 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3757 /* Then try double character with no arguments. */
3758 if ((memcmp("||", cp
, 2) == 0 ||
3759 memcmp("&&", cp
, 2) == 0 ||
3760 memcmp("!=", cp
, 2) == 0 ||
3761 memcmp("==", cp
, 2) == 0 ||
3762 memcmp("<=", cp
, 2) == 0 ||
3763 memcmp(">=", cp
, 2) == 0) &&
3764 format_is_end(cp
[2])) {
3765 format_add_modifier(&list
, count
, cp
, 2, NULL
, 0);
3770 /* Now try single character with arguments. */
3771 if (strchr("mCNst=peq", cp
[0]) == NULL
)
3775 /* No arguments provided. */
3776 if (format_is_end(cp
[1])) {
3777 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3784 /* Single argument with no wrapper character. */
3785 if (!ispunct(cp
[1]) || cp
[1] == '-') {
3786 end
= format_skip(cp
+ 1, ":;");
3790 argv
= xcalloc(1, sizeof *argv
);
3791 value
= xstrndup(cp
+ 1, end
- (cp
+ 1));
3792 argv
[0] = format_expand1(es
, value
);
3796 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3801 /* Multiple arguments with a wrapper character. */
3805 if (cp
[0] == last
[0] && format_is_end(cp
[1])) {
3809 end
= format_skip(cp
+ 1, last
);
3814 argv
= xreallocarray(argv
, argc
+ 1, sizeof *argv
);
3815 value
= xstrndup(cp
, end
- cp
);
3816 argv
[argc
++] = format_expand1(es
, value
);
3820 } while (!format_is_end(cp
[0]));
3821 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3824 format_free_modifiers(list
, *count
);
3832 /* Match against an fnmatch(3) pattern or regular expression. */
3834 format_match(struct format_modifier
*fm
, const char *pattern
, const char *text
)
3842 if (strchr(s
, 'r') == NULL
) {
3843 if (strchr(s
, 'i') != NULL
)
3844 flags
|= FNM_CASEFOLD
;
3845 if (fnmatch(pattern
, text
, flags
) != 0)
3846 return (xstrdup("0"));
3848 flags
= REG_EXTENDED
|REG_NOSUB
;
3849 if (strchr(s
, 'i') != NULL
)
3851 if (regcomp(&r
, pattern
, flags
) != 0)
3852 return (xstrdup("0"));
3853 if (regexec(&r
, text
, 0, NULL
, 0) != 0) {
3855 return (xstrdup("0"));
3859 return (xstrdup("1"));
3862 /* Perform substitution in string. */
3864 format_sub(struct format_modifier
*fm
, const char *text
, const char *pattern
,
3868 int flags
= REG_EXTENDED
;
3870 if (fm
->argc
>= 3 && strchr(fm
->argv
[2], 'i') != NULL
)
3872 value
= regsub(pattern
, with
, text
, flags
);
3874 return (xstrdup(text
));
3878 /* Search inside pane. */
3880 format_search(struct format_modifier
*fm
, struct window_pane
*wp
, const char *s
)
3882 int ignore
= 0, regex
= 0;
3885 if (fm
->argc
>= 1) {
3886 if (strchr(fm
->argv
[0], 'i') != NULL
)
3888 if (strchr(fm
->argv
[0], 'r') != NULL
)
3891 xasprintf(&value
, "%u", window_pane_search(wp
, s
, regex
, ignore
));
3895 /* Does session name exist? */
3897 format_session_name(struct format_expand_state
*es
, const char *fmt
)
3902 name
= format_expand1(es
, fmt
);
3903 RB_FOREACH(s
, sessions
, &sessions
) {
3904 if (strcmp(s
->name
, name
) == 0) {
3906 return (xstrdup("1"));
3910 return (xstrdup("0"));
3913 /* Loop over sessions. */
3915 format_loop_sessions(struct format_expand_state
*es
, const char *fmt
)
3917 struct format_tree
*ft
= es
->ft
;
3918 struct client
*c
= ft
->client
;
3919 struct cmdq_item
*item
= ft
->item
;
3920 struct format_tree
*nft
;
3921 struct format_expand_state next
;
3922 char *expanded
, *value
;
3926 value
= xcalloc(1, 1);
3929 RB_FOREACH(s
, sessions
, &sessions
) {
3930 format_log(es
, "session loop: $%u", s
->id
);
3931 nft
= format_create(c
, item
, FORMAT_NONE
, ft
->flags
);
3932 format_defaults(nft
, ft
->c
, s
, NULL
, NULL
);
3933 format_copy_state(&next
, es
, 0);
3935 expanded
= format_expand1(&next
, fmt
);
3936 format_free(next
.ft
);
3938 valuelen
+= strlen(expanded
);
3939 value
= xrealloc(value
, valuelen
);
3941 strlcat(value
, expanded
, valuelen
);
3948 /* Does window name exist? */
3950 format_window_name(struct format_expand_state
*es
, const char *fmt
)
3952 struct format_tree
*ft
= es
->ft
;
3956 if (ft
->s
== NULL
) {
3957 format_log(es
, "window name but no session");
3961 name
= format_expand1(es
, fmt
);
3962 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
3963 if (strcmp(wl
->window
->name
, name
) == 0) {
3965 return (xstrdup("1"));
3969 return (xstrdup("0"));
3972 /* Loop over windows. */
3974 format_loop_windows(struct format_expand_state
*es
, const char *fmt
)
3976 struct format_tree
*ft
= es
->ft
;
3977 struct client
*c
= ft
->client
;
3978 struct cmdq_item
*item
= ft
->item
;
3979 struct format_tree
*nft
;
3980 struct format_expand_state next
;
3981 char *all
, *active
, *use
, *expanded
, *value
;
3986 if (ft
->s
== NULL
) {
3987 format_log(es
, "window loop but no session");
3991 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
3996 value
= xcalloc(1, 1);
3999 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
4001 format_log(es
, "window loop: %u @%u", wl
->idx
, w
->id
);
4002 if (active
!= NULL
&& wl
== ft
->s
->curw
)
4006 nft
= format_create(c
, item
, FORMAT_WINDOW
|w
->id
, ft
->flags
);
4007 format_defaults(nft
, ft
->c
, ft
->s
, wl
, NULL
);
4008 format_copy_state(&next
, es
, 0);
4010 expanded
= format_expand1(&next
, use
);
4013 valuelen
+= strlen(expanded
);
4014 value
= xrealloc(value
, valuelen
);
4016 strlcat(value
, expanded
, valuelen
);
4026 /* Loop over panes. */
4028 format_loop_panes(struct format_expand_state
*es
, const char *fmt
)
4030 struct format_tree
*ft
= es
->ft
;
4031 struct client
*c
= ft
->client
;
4032 struct cmdq_item
*item
= ft
->item
;
4033 struct format_tree
*nft
;
4034 struct format_expand_state next
;
4035 char *all
, *active
, *use
, *expanded
, *value
;
4037 struct window_pane
*wp
;
4039 if (ft
->w
== NULL
) {
4040 format_log(es
, "pane loop but no window");
4044 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
4049 value
= xcalloc(1, 1);
4052 TAILQ_FOREACH(wp
, &ft
->w
->panes
, entry
) {
4053 format_log(es
, "pane loop: %%%u", wp
->id
);
4054 if (active
!= NULL
&& wp
== ft
->w
->active
)
4058 nft
= format_create(c
, item
, FORMAT_PANE
|wp
->id
, ft
->flags
);
4059 format_defaults(nft
, ft
->c
, ft
->s
, ft
->wl
, wp
);
4060 format_copy_state(&next
, es
, 0);
4062 expanded
= format_expand1(&next
, use
);
4065 valuelen
+= strlen(expanded
);
4066 value
= xrealloc(value
, valuelen
);
4068 strlcat(value
, expanded
, valuelen
);
4079 format_replace_expression(struct format_modifier
*mexp
,
4080 struct format_expand_state
*es
, const char *copy
)
4082 int argc
= mexp
->argc
;
4084 char *endch
, *value
, *left
= NULL
, *right
= NULL
;
4087 double mleft
, mright
, result
;
4098 LESS_THAN_EQUAL
} operator;
4100 if (strcmp(mexp
->argv
[0], "+") == 0)
4102 else if (strcmp(mexp
->argv
[0], "-") == 0)
4103 operator = SUBTRACT
;
4104 else if (strcmp(mexp
->argv
[0], "*") == 0)
4105 operator = MULTIPLY
;
4106 else if (strcmp(mexp
->argv
[0], "/") == 0)
4108 else if (strcmp(mexp
->argv
[0], "%") == 0 ||
4109 strcmp(mexp
->argv
[0], "m") == 0)
4111 else if (strcmp(mexp
->argv
[0], "==") == 0)
4113 else if (strcmp(mexp
->argv
[0], "!=") == 0)
4114 operator = NOT_EQUAL
;
4115 else if (strcmp(mexp
->argv
[0], ">") == 0)
4116 operator = GREATER_THAN
;
4117 else if (strcmp(mexp
->argv
[0], "<") == 0)
4118 operator = LESS_THAN
;
4119 else if (strcmp(mexp
->argv
[0], ">=") == 0)
4120 operator = GREATER_THAN_EQUAL
;
4121 else if (strcmp(mexp
->argv
[0], "<=") == 0)
4122 operator = LESS_THAN_EQUAL
;
4124 format_log(es
, "expression has no valid operator: '%s'",
4129 /* The second argument may be flags. */
4130 if (argc
>= 2 && strchr(mexp
->argv
[1], 'f') != NULL
) {
4135 /* The third argument may be precision. */
4137 prec
= strtonum(mexp
->argv
[2], INT_MIN
, INT_MAX
, &errstr
);
4138 if (errstr
!= NULL
) {
4139 format_log(es
, "expression precision %s: %s", errstr
,
4145 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4146 format_log(es
, "expression syntax error");
4150 mleft
= strtod(left
, &endch
);
4151 if (*endch
!= '\0') {
4152 format_log(es
, "expression left side is invalid: %s", left
);
4156 mright
= strtod(right
, &endch
);
4157 if (*endch
!= '\0') {
4158 format_log(es
, "expression right side is invalid: %s", right
);
4163 mleft
= (long long)mleft
;
4164 mright
= (long long)mright
;
4166 format_log(es
, "expression left side is: %.*f", prec
, mleft
);
4167 format_log(es
, "expression right side is: %.*f", prec
, mright
);
4171 result
= mleft
+ mright
;
4174 result
= mleft
- mright
;
4177 result
= mleft
* mright
;
4180 result
= mleft
/ mright
;
4183 result
= fmod(mleft
, mright
);
4186 result
= fabs(mleft
- mright
) < 1e-9;
4189 result
= fabs(mleft
- mright
) > 1e-9;
4192 result
= (mleft
> mright
);
4194 case GREATER_THAN_EQUAL
:
4195 result
= (mleft
>= mright
);
4198 result
= (mleft
< mright
);
4200 case LESS_THAN_EQUAL
:
4201 result
= (mleft
<= mright
);
4205 xasprintf(&value
, "%.*f", prec
, result
);
4207 xasprintf(&value
, "%.*f", prec
, (double)(long long)result
);
4208 format_log(es
, "expression result is %s", value
);
4220 /* Replace a key. */
4222 format_replace(struct format_expand_state
*es
, const char *key
, size_t keylen
,
4223 char **buf
, size_t *len
, size_t *off
)
4225 struct format_tree
*ft
= es
->ft
;
4226 struct window_pane
*wp
= ft
->wp
;
4227 const char *errstr
, *copy
, *cp
, *marker
= NULL
;
4228 const char *time_format
= NULL
;
4229 char *copy0
, *condition
, *found
, *new;
4230 char *value
, *left
, *right
;
4232 int modifiers
= 0, limit
= 0, width
= 0;
4234 struct format_modifier
*list
, *cmp
= NULL
, *search
= NULL
;
4235 struct format_modifier
**sub
= NULL
, *mexp
= NULL
, *fm
;
4236 u_int i
, count
, nsub
= 0;
4237 struct format_expand_state next
;
4239 /* Make a copy of the key. */
4240 copy
= copy0
= xstrndup(key
, keylen
);
4242 /* Process modifier list. */
4243 list
= format_build_modifiers(es
, ©
, &count
);
4244 for (i
= 0; i
< count
; i
++) {
4246 if (format_logging(ft
)) {
4247 format_log(es
, "modifier %u is %s", i
, fm
->modifier
);
4248 for (j
= 0; j
< fm
->argc
; j
++) {
4249 format_log(es
, "modifier %u argument %d: %s", i
,
4253 if (fm
->size
== 1) {
4254 switch (fm
->modifier
[0]) {
4266 sub
= xreallocarray(sub
, nsub
+ 1, sizeof *sub
);
4272 limit
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4276 if (fm
->argc
>= 2 && fm
->argv
[1] != NULL
)
4277 marker
= fm
->argv
[1];
4282 width
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4288 modifiers
|= FORMAT_WIDTH
;
4291 if (fm
->argc
< 1 || fm
->argc
> 3)
4296 modifiers
|= FORMAT_LITERAL
;
4299 modifiers
|= FORMAT_CHARACTER
;
4302 modifiers
|= FORMAT_BASENAME
;
4305 modifiers
|= FORMAT_COLOUR
;
4308 modifiers
|= FORMAT_DIRNAME
;
4311 modifiers
|= FORMAT_LENGTH
;
4314 modifiers
|= FORMAT_TIMESTRING
;
4317 if (strchr(fm
->argv
[0], 'p') != NULL
)
4318 modifiers
|= FORMAT_PRETTY
;
4319 else if (fm
->argc
>= 2 &&
4320 strchr(fm
->argv
[0], 'f') != NULL
)
4321 time_format
= format_strip(fm
->argv
[1]);
4325 modifiers
|= FORMAT_QUOTE_SHELL
;
4326 else if (strchr(fm
->argv
[0], 'e') != NULL
||
4327 strchr(fm
->argv
[0], 'h') != NULL
)
4328 modifiers
|= FORMAT_QUOTE_STYLE
;
4331 modifiers
|= FORMAT_EXPAND
;
4334 modifiers
|= FORMAT_EXPANDTIME
;
4338 strchr(fm
->argv
[0], 'w') != NULL
)
4339 modifiers
|= FORMAT_WINDOW_NAME
;
4340 else if (strchr(fm
->argv
[0], 's') != NULL
)
4341 modifiers
|= FORMAT_SESSION_NAME
;
4344 modifiers
|= FORMAT_SESSIONS
;
4347 modifiers
|= FORMAT_WINDOWS
;
4350 modifiers
|= FORMAT_PANES
;
4353 } else if (fm
->size
== 2) {
4354 if (strcmp(fm
->modifier
, "||") == 0 ||
4355 strcmp(fm
->modifier
, "&&") == 0 ||
4356 strcmp(fm
->modifier
, "==") == 0 ||
4357 strcmp(fm
->modifier
, "!=") == 0 ||
4358 strcmp(fm
->modifier
, ">=") == 0 ||
4359 strcmp(fm
->modifier
, "<=") == 0)
4364 /* Is this a literal string? */
4365 if (modifiers
& FORMAT_LITERAL
) {
4366 format_log(es
, "literal string is '%s'", copy
);
4367 value
= format_unescape(copy
);
4371 /* Is this a character? */
4372 if (modifiers
& FORMAT_CHARACTER
) {
4373 new = format_expand1(es
, copy
);
4374 c
= strtonum(new, 32, 126, &errstr
);
4376 value
= xstrdup("");
4378 xasprintf(&value
, "%c", c
);
4383 /* Is this a colour? */
4384 if (modifiers
& FORMAT_COLOUR
) {
4385 new = format_expand1(es
, copy
);
4386 c
= colour_fromstring(new);
4387 if (c
== -1 || (c
= colour_force_rgb(c
)) == -1)
4388 value
= xstrdup("");
4390 xasprintf(&value
, "%06x", c
& 0xffffff);
4395 /* Is this a loop, comparison or condition? */
4396 if (modifiers
& FORMAT_SESSIONS
) {
4397 value
= format_loop_sessions(es
, copy
);
4400 } else if (modifiers
& FORMAT_WINDOWS
) {
4401 value
= format_loop_windows(es
, copy
);
4404 } else if (modifiers
& FORMAT_PANES
) {
4405 value
= format_loop_panes(es
, copy
);
4408 } else if (modifiers
& FORMAT_WINDOW_NAME
) {
4409 value
= format_window_name(es
, copy
);
4412 } else if (modifiers
& FORMAT_SESSION_NAME
) {
4413 value
= format_session_name(es
, copy
);
4416 } else if (search
!= NULL
) {
4417 /* Search in pane. */
4418 new = format_expand1(es
, copy
);
4420 format_log(es
, "search '%s' but no pane", new);
4421 value
= xstrdup("0");
4423 format_log(es
, "search '%s' pane %%%u", new, wp
->id
);
4424 value
= format_search(search
, wp
, new);
4427 } else if (cmp
!= NULL
) {
4428 /* Comparison of left and right. */
4429 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4430 format_log(es
, "compare %s syntax error: %s",
4431 cmp
->modifier
, copy
);
4434 format_log(es
, "compare %s left is: %s", cmp
->modifier
, left
);
4435 format_log(es
, "compare %s right is: %s", cmp
->modifier
, right
);
4437 if (strcmp(cmp
->modifier
, "||") == 0) {
4438 if (format_true(left
) || format_true(right
))
4439 value
= xstrdup("1");
4441 value
= xstrdup("0");
4442 } else if (strcmp(cmp
->modifier
, "&&") == 0) {
4443 if (format_true(left
) && format_true(right
))
4444 value
= xstrdup("1");
4446 value
= xstrdup("0");
4447 } else if (strcmp(cmp
->modifier
, "==") == 0) {
4448 if (strcmp(left
, right
) == 0)
4449 value
= xstrdup("1");
4451 value
= xstrdup("0");
4452 } else if (strcmp(cmp
->modifier
, "!=") == 0) {
4453 if (strcmp(left
, right
) != 0)
4454 value
= xstrdup("1");
4456 value
= xstrdup("0");
4457 } else if (strcmp(cmp
->modifier
, "<") == 0) {
4458 if (strcmp(left
, right
) < 0)
4459 value
= xstrdup("1");
4461 value
= xstrdup("0");
4462 } else if (strcmp(cmp
->modifier
, ">") == 0) {
4463 if (strcmp(left
, right
) > 0)
4464 value
= xstrdup("1");
4466 value
= xstrdup("0");
4467 } else if (strcmp(cmp
->modifier
, "<=") == 0) {
4468 if (strcmp(left
, right
) <= 0)
4469 value
= xstrdup("1");
4471 value
= xstrdup("0");
4472 } else if (strcmp(cmp
->modifier
, ">=") == 0) {
4473 if (strcmp(left
, right
) >= 0)
4474 value
= xstrdup("1");
4476 value
= xstrdup("0");
4477 } else if (strcmp(cmp
->modifier
, "m") == 0)
4478 value
= format_match(cmp
, left
, right
);
4482 } else if (*copy
== '?') {
4483 /* Conditional: check first and choose second or third. */
4484 cp
= format_skip(copy
+ 1, ",");
4486 format_log(es
, "condition syntax error: %s", copy
+ 1);
4489 condition
= xstrndup(copy
+ 1, cp
- (copy
+ 1));
4490 format_log(es
, "condition is: %s", condition
);
4492 found
= format_find(ft
, condition
, modifiers
, time_format
);
4493 if (found
== NULL
) {
4495 * If the condition not found, try to expand it. If
4496 * the expansion doesn't have any effect, then assume
4499 found
= format_expand1(es
, condition
);
4500 if (strcmp(found
, condition
) == 0) {
4502 found
= xstrdup("");
4504 "condition '%s' not found; assuming false",
4508 format_log(es
, "condition '%s' found: %s", condition
,
4512 if (format_choose(es
, cp
+ 1, &left
, &right
, 0) != 0) {
4513 format_log(es
, "condition '%s' syntax error: %s",
4518 if (format_true(found
)) {
4519 format_log(es
, "condition '%s' is true", condition
);
4520 value
= format_expand1(es
, left
);
4522 format_log(es
, "condition '%s' is false", condition
);
4523 value
= format_expand1(es
, right
);
4530 } else if (mexp
!= NULL
) {
4531 value
= format_replace_expression(mexp
, es
, copy
);
4533 value
= xstrdup("");
4535 if (strstr(copy
, "#{") != 0) {
4536 format_log(es
, "expanding inner format '%s'", copy
);
4537 value
= format_expand1(es
, copy
);
4539 value
= format_find(ft
, copy
, modifiers
, time_format
);
4540 if (value
== NULL
) {
4541 format_log(es
, "format '%s' not found", copy
);
4542 value
= xstrdup("");
4544 format_log(es
, "format '%s' found: %s", copy
,
4551 /* Expand again if required. */
4552 if (modifiers
& FORMAT_EXPAND
) {
4553 new = format_expand1(es
, value
);
4556 } else if (modifiers
& FORMAT_EXPANDTIME
) {
4557 format_copy_state(&next
, es
, FORMAT_EXPAND_TIME
);
4558 new = format_expand1(&next
, value
);
4563 /* Perform substitution if any. */
4564 for (i
= 0; i
< nsub
; i
++) {
4565 left
= format_expand1(es
, sub
[i
]->argv
[0]);
4566 right
= format_expand1(es
, sub
[i
]->argv
[1]);
4567 new = format_sub(sub
[i
], value
, left
, right
);
4568 format_log(es
, "substitute '%s' to '%s': %s", left
, right
, new);
4575 /* Truncate the value if needed. */
4577 new = format_trim_left(value
, limit
);
4578 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4580 xasprintf(&value
, "%s%s", new, marker
);
4585 format_log(es
, "applied length limit %d: %s", limit
, value
);
4586 } else if (limit
< 0) {
4587 new = format_trim_right(value
, -limit
);
4588 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4590 xasprintf(&value
, "%s%s", marker
, new);
4595 format_log(es
, "applied length limit %d: %s", limit
, value
);
4598 /* Pad the value if needed. */
4600 new = utf8_padcstr(value
, width
);
4603 format_log(es
, "applied padding width %d: %s", width
, value
);
4604 } else if (width
< 0) {
4605 new = utf8_rpadcstr(value
, -width
);
4608 format_log(es
, "applied padding width %d: %s", width
, value
);
4611 /* Replace with the length or width if needed. */
4612 if (modifiers
& FORMAT_LENGTH
) {
4613 xasprintf(&new, "%zu", strlen(value
));
4616 format_log(es
, "replacing with length: %s", new);
4618 if (modifiers
& FORMAT_WIDTH
) {
4619 xasprintf(&new, "%u", format_width(value
));
4622 format_log(es
, "replacing with width: %s", new);
4625 /* Expand the buffer and copy in the value. */
4626 valuelen
= strlen(value
);
4627 while (*len
- *off
< valuelen
+ 1) {
4628 *buf
= xreallocarray(*buf
, 2, *len
);
4631 memcpy(*buf
+ *off
, value
, valuelen
);
4634 format_log(es
, "replaced '%s' with '%s'", copy0
, value
);
4638 format_free_modifiers(list
, count
);
4643 format_log(es
, "failed %s", copy0
);
4646 format_free_modifiers(list
, count
);
4651 /* Expand keys in a template. */
4653 format_expand1(struct format_expand_state
*es
, const char *fmt
)
4655 struct format_tree
*ft
= es
->ft
;
4656 char *buf
, *out
, *name
;
4657 const char *ptr
, *s
, *style_end
= NULL
;
4658 size_t off
, len
, n
, outlen
;
4660 char expanded
[8192];
4662 if (fmt
== NULL
|| *fmt
== '\0')
4663 return (xstrdup(""));
4665 if (es
->loop
== FORMAT_LOOP_LIMIT
) {
4666 format_log(es
, "reached loop limit (%u)", FORMAT_LOOP_LIMIT
);
4667 return (xstrdup(""));
4671 format_log(es
, "expanding format: %s", fmt
);
4673 if ((es
->flags
& FORMAT_EXPAND_TIME
) && strchr(fmt
, '%') != NULL
) {
4674 if (es
->time
== 0) {
4675 es
->time
= time(NULL
);
4676 localtime_r(&es
->time
, &es
->tm
);
4678 if (strftime(expanded
, sizeof expanded
, fmt
, &es
->tm
) == 0) {
4679 format_log(es
, "format is too long");
4680 return (xstrdup(""));
4682 if (format_logging(ft
) && strcmp(expanded
, fmt
) != 0)
4683 format_log(es
, "after time expanded: %s", expanded
);
4691 while (*fmt
!= '\0') {
4693 while (len
- off
< 2) {
4694 buf
= xreallocarray(buf
, 2, len
);
4697 buf
[off
++] = *fmt
++;
4702 ch
= (u_char
)*fmt
++;
4706 for (ptr
= fmt
; *ptr
!= '\0'; ptr
++) {
4709 if (*ptr
== ')' && --brackets
== 0)
4712 if (*ptr
!= ')' || brackets
!= 0)
4716 name
= xstrndup(fmt
, n
);
4717 format_log(es
, "found #(): %s", name
);
4719 if ((ft
->flags
& FORMAT_NOJOBS
) ||
4720 (es
->flags
& FORMAT_EXPAND_NOJOBS
)) {
4722 format_log(es
, "#() is disabled");
4724 out
= format_job_get(es
, name
);
4725 format_log(es
, "#() result: %s", out
);
4729 outlen
= strlen(out
);
4730 while (len
- off
< outlen
+ 1) {
4731 buf
= xreallocarray(buf
, 2, len
);
4734 memcpy(buf
+ off
, out
, outlen
);
4742 ptr
= format_skip((char *)fmt
- 2, "}");
4747 format_log(es
, "found #{}: %.*s", (int)n
, fmt
);
4748 if (format_replace(es
, fmt
, n
, &buf
, &len
, &off
) != 0)
4755 * If ##[ (with two or more #s), then it is a style and
4756 * can be left for format_draw to handle.
4758 ptr
= fmt
- (ch
== '[');
4759 n
= 2 - (ch
== '[');
4760 while (*ptr
== '#') {
4765 style_end
= format_skip(fmt
- 2, "]");
4766 format_log(es
, "found #*%zu[", n
);
4767 while (len
- off
< n
+ 2) {
4768 buf
= xreallocarray(buf
, 2, len
);
4771 memcpy(buf
+ off
, fmt
- 2, n
+ 1);
4779 format_log(es
, "found #%c", ch
);
4780 while (len
- off
< 2) {
4781 buf
= xreallocarray(buf
, 2, len
);
4788 if (fmt
> style_end
) { /* skip inside #[] */
4789 if (ch
>= 'A' && ch
<= 'Z')
4790 s
= format_upper
[ch
- 'A'];
4791 else if (ch
>= 'a' && ch
<= 'z')
4792 s
= format_lower
[ch
- 'a'];
4795 while (len
- off
< 3) {
4796 buf
= xreallocarray(buf
, 2, len
);
4804 format_log(es
, "found #%c: %s", ch
, s
);
4805 if (format_replace(es
, s
, n
, &buf
, &len
, &off
) != 0)
4814 format_log(es
, "result is: %s", buf
);
4820 /* Expand keys in a template, passing through strftime first. */
4822 format_expand_time(struct format_tree
*ft
, const char *fmt
)
4824 struct format_expand_state es
;
4826 memset(&es
, 0, sizeof es
);
4828 es
.flags
= FORMAT_EXPAND_TIME
;
4829 return (format_expand1(&es
, fmt
));
4832 /* Expand keys in a template. */
4834 format_expand(struct format_tree
*ft
, const char *fmt
)
4836 struct format_expand_state es
;
4838 memset(&es
, 0, sizeof es
);
4841 return (format_expand1(&es
, fmt
));
4844 /* Expand a single string. */
4846 format_single(struct cmdq_item
*item
, const char *fmt
, struct client
*c
,
4847 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
4849 struct format_tree
*ft
;
4852 ft
= format_create_defaults(item
, c
, s
, wl
, wp
);
4853 expanded
= format_expand(ft
, fmt
);
4858 /* Expand a single string using state. */
4860 format_single_from_state(struct cmdq_item
*item
, const char *fmt
,
4861 struct client
*c
, struct cmd_find_state
*fs
)
4863 return (format_single(item
, fmt
, c
, fs
->s
, fs
->wl
, fs
->wp
));
4866 /* Expand a single string using target. */
4868 format_single_from_target(struct cmdq_item
*item
, const char *fmt
)
4870 struct client
*tc
= cmdq_get_target_client(item
);
4872 return (format_single_from_state(item
, fmt
, tc
, cmdq_get_target(item
)));
4875 /* Create and add defaults. */
4876 struct format_tree
*
4877 format_create_defaults(struct cmdq_item
*item
, struct client
*c
,
4878 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
4880 struct format_tree
*ft
;
4883 ft
= format_create(cmdq_get_client(item
), item
, FORMAT_NONE
, 0);
4885 ft
= format_create(NULL
, item
, FORMAT_NONE
, 0);
4886 format_defaults(ft
, c
, s
, wl
, wp
);
4890 /* Create and add defaults using state. */
4891 struct format_tree
*
4892 format_create_from_state(struct cmdq_item
*item
, struct client
*c
,
4893 struct cmd_find_state
*fs
)
4895 return (format_create_defaults(item
, c
, fs
->s
, fs
->wl
, fs
->wp
));
4898 /* Create and add defaults using target. */
4899 struct format_tree
*
4900 format_create_from_target(struct cmdq_item
*item
)
4902 struct client
*tc
= cmdq_get_target_client(item
);
4904 return (format_create_from_state(item
, tc
, cmdq_get_target(item
)));
4907 /* Set defaults for any of arguments that are not NULL. */
4909 format_defaults(struct format_tree
*ft
, struct client
*c
, struct session
*s
,
4910 struct winlink
*wl
, struct window_pane
*wp
)
4912 struct paste_buffer
*pb
;
4914 if (c
!= NULL
&& c
->name
!= NULL
)
4915 log_debug("%s: c=%s", __func__
, c
->name
);
4917 log_debug("%s: c=none", __func__
);
4919 log_debug("%s: s=$%u", __func__
, s
->id
);
4921 log_debug("%s: s=none", __func__
);
4923 log_debug("%s: wl=%u", __func__
, wl
->idx
);
4925 log_debug("%s: wl=none", __func__
);
4927 log_debug("%s: wp=%%%u", __func__
, wp
->id
);
4929 log_debug("%s: wp=none", __func__
);
4931 if (c
!= NULL
&& s
!= NULL
&& c
->session
!= s
)
4932 log_debug("%s: session does not match", __func__
);
4935 ft
->type
= FORMAT_TYPE_PANE
;
4936 else if (wl
!= NULL
)
4937 ft
->type
= FORMAT_TYPE_WINDOW
;
4939 ft
->type
= FORMAT_TYPE_SESSION
;
4941 ft
->type
= FORMAT_TYPE_UNKNOWN
;
4943 if (s
== NULL
&& c
!= NULL
)
4945 if (wl
== NULL
&& s
!= NULL
)
4947 if (wp
== NULL
&& wl
!= NULL
)
4948 wp
= wl
->window
->active
;
4951 format_defaults_client(ft
, c
);
4953 format_defaults_session(ft
, s
);
4955 format_defaults_winlink(ft
, wl
);
4957 format_defaults_pane(ft
, wp
);
4959 pb
= paste_get_top(NULL
);
4961 format_defaults_paste_buffer(ft
, pb
);
4964 /* Set default format keys for a session. */
4966 format_defaults_session(struct format_tree
*ft
, struct session
*s
)
4971 /* Set default format keys for a client. */
4973 format_defaults_client(struct format_tree
*ft
, struct client
*c
)
4980 /* Set default format keys for a window. */
4982 format_defaults_window(struct format_tree
*ft
, struct window
*w
)
4987 /* Set default format keys for a winlink. */
4989 format_defaults_winlink(struct format_tree
*ft
, struct winlink
*wl
)
4992 format_defaults_window(ft
, wl
->window
);
4996 /* Set default format keys for a window pane. */
4998 format_defaults_pane(struct format_tree
*ft
, struct window_pane
*wp
)
5000 struct window_mode_entry
*wme
;
5003 format_defaults_window(ft
, wp
->window
);
5006 wme
= TAILQ_FIRST(&wp
->modes
);
5007 if (wme
!= NULL
&& wme
->mode
->formats
!= NULL
)
5008 wme
->mode
->formats(wme
, ft
);
5011 /* Set default format keys for paste buffer. */
5013 format_defaults_paste_buffer(struct format_tree
*ft
, struct paste_buffer
*pb
)
5018 /* Return word at given coordinates. Caller frees. */
5020 format_grid_word(struct grid
*gd
, u_int x
, u_int y
)
5022 const struct grid_line
*gl
;
5023 struct grid_cell gc
;
5025 struct utf8_data
*ud
= NULL
;
5031 ws
= options_get_string(global_s_options
, "word-separators");
5034 grid_get_cell(gd
, x
, y
, &gc
);
5035 if (gc
.flags
& GRID_FLAG_PADDING
)
5037 if (utf8_cstrhas(ws
, &gc
.data
) ||
5038 (gc
.data
.size
== 1 && *gc
.data
.data
== ' ')) {
5046 gl
= grid_peek_line(gd
, y
- 1);
5047 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5050 x
= grid_line_length(gd
, y
);
5058 end
= grid_line_length(gd
, y
);
5059 if (end
== 0 || x
== end
- 1) {
5060 if (y
== gd
->hsize
+ gd
->sy
- 1)
5062 gl
= grid_peek_line(gd
, y
);
5063 if (~gl
->flags
& GRID_LINE_WRAPPED
)
5072 grid_get_cell(gd
, x
, y
, &gc
);
5073 if (gc
.flags
& GRID_FLAG_PADDING
)
5075 if (utf8_cstrhas(ws
, &gc
.data
) ||
5076 (gc
.data
.size
== 1 && *gc
.data
.data
== ' '))
5079 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5080 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5084 s
= utf8_tocstr(ud
);
5090 /* Return line at given coordinates. Caller frees. */
5092 format_grid_line(struct grid
*gd
, u_int y
)
5094 struct grid_cell gc
;
5095 struct utf8_data
*ud
= NULL
;
5100 for (x
= 0; x
< grid_line_length(gd
, y
); x
++) {
5101 grid_get_cell(gd
, x
, y
, &gc
);
5102 if (gc
.flags
& GRID_FLAG_PADDING
)
5105 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
5106 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
5110 s
= utf8_tocstr(ud
);
5116 /* Return hyperlink at given coordinates. Caller frees. */
5118 format_grid_hyperlink(struct grid
*gd
, u_int x
, u_int y
, struct screen
* s
)
5121 struct grid_cell gc
;
5123 grid_get_cell(gd
, x
, y
, &gc
);
5124 if (gc
.flags
& GRID_FLAG_PADDING
)
5126 if (s
->hyperlinks
== NULL
|| gc
.link
== 0)
5128 if (!hyperlinks_get(s
->hyperlinks
, gc
.link
, &uri
, NULL
, NULL
))
5130 return (xstrdup(uri
));