From 759efe1b3327a7244c03ecc7b90e0e3c49712d06 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 11 Oct 2021 10:55:30 +0000 Subject: Add -e flag to set environment for popup, from Alexis Hildebrandt in GitHub issue 2924. --- format.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'format.c') diff --git a/format.c b/format.c index 37b9123b..f5d2c5f6 100644 --- a/format.c +++ b/format.c @@ -390,7 +390,7 @@ format_job_get(struct format_expand_state *es, const char *cmd) if (force && fj->job != NULL) job_free(fj->job); if (force || (fj->job == NULL && fj->last != t)) { - fj->job = job_run(expanded, 0, NULL, NULL, + fj->job = job_run(expanded, 0, NULL, NULL, NULL, server_client_get_cwd(ft->client, NULL), format_job_update, format_job_complete, NULL, fj, JOB_NOWAIT, -1, -1); if (fj->job == NULL) { -- cgit From 197a116f5a2146309c4c6fecbd9d08d36f2be750 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 25 Oct 2021 21:21:16 +0000 Subject: Add a way to force a colour to RGB and a format to display it. --- format.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'format.c') diff --git a/format.c b/format.c index f5d2c5f6..b34fc341 100644 --- a/format.c +++ b/format.c @@ -101,6 +101,7 @@ format_job_cmp(struct format_job *fj1, struct format_job *fj2) #define FORMAT_WINDOW_NAME 0x4000 #define FORMAT_SESSION_NAME 0x8000 #define FORMAT_CHARACTER 0x10000 +#define FORMAT_COLOUR 0x20000 /* Limit on recursion. */ #define FORMAT_LOOP_LIMIT 100 @@ -3555,7 +3556,7 @@ format_build_modifiers(struct format_expand_state *es, const char **s, /* * Modifiers are a ; separated list of the forms: - * l,m,C,a,b,d,n,t,w,q,E,T,S,W,P,<,> + * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,> * =a * =/a * =/a/ @@ -3572,7 +3573,7 @@ format_build_modifiers(struct format_expand_state *es, const char **s, cp++; /* Check single character modifiers with no arguments. */ - if (strchr("labdnwETSWP<>", cp[0]) != NULL && + if (strchr("labcdnwETSWP<>", cp[0]) != NULL && format_is_end(cp[1])) { format_add_modifier(&list, count, cp, 1, NULL, 0); cp++; @@ -4052,10 +4053,10 @@ format_replace(struct format_expand_state *es, const char *key, size_t keylen, const char *errstr, *copy, *cp, *marker = NULL; const char *time_format = NULL; char *copy0, *condition, *found, *new; - char *value, *left, *right, c; + char *value, *left, *right; size_t valuelen; int modifiers = 0, limit = 0, width = 0; - int j; + int j, c; struct format_modifier *list, *cmp = NULL, *search = NULL; struct format_modifier **sub = NULL, *mexp = NULL, *fm; u_int i, count, nsub = 0; @@ -4126,6 +4127,9 @@ format_replace(struct format_expand_state *es, const char *key, size_t keylen, case 'b': modifiers |= FORMAT_BASENAME; break; + case 'c': + modifiers |= FORMAT_COLOUR; + break; case 'd': modifiers |= FORMAT_DIRNAME; break; @@ -4201,6 +4205,18 @@ format_replace(struct format_expand_state *es, const char *key, size_t keylen, goto done; } + /* Is this a colour? */ + if (modifiers & FORMAT_COLOUR) { + new = format_expand1(es, copy); + c = colour_fromstring(new); + if (c == -1 || (c = colour_force_rgb(c)) == -1) + value = xstrdup(""); + else + xasprintf(&value, "%06x", c & 0xffffff); + free(new); + goto done; + } + /* Is this a loop, comparison or condition? */ if (modifiers & FORMAT_SESSIONS) { value = format_loop_sessions(es, copy); -- cgit From fa71e9a07911715da596d618fe045943337c596b Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 22 Feb 2022 11:10:41 +0000 Subject: Add next_session_id format with the next session ID, GitHub issue 3078. --- format.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'format.c') diff --git a/format.c b/format.c index b34fc341..15f3179c 100644 --- a/format.c +++ b/format.c @@ -1650,6 +1650,13 @@ format_cb_mouse_y(struct format_tree *ft) return (NULL); } +/* Callback for next_session_id. */ +static void * +format_cb_next_session_id(__unused struct format_tree *ft) +{ + return (format_printf("$%u", next_session_id)); +} + /* Callback for origin_flag. */ static void * format_cb_origin_flag(struct format_tree *ft) @@ -2707,6 +2714,9 @@ static const struct format_table_entry format_table[] = { { "mouse_y", FORMAT_TABLE_STRING, format_cb_mouse_y }, + { "next_session_id", FORMAT_TABLE_STRING, + format_cb_next_session_id + }, { "origin_flag", FORMAT_TABLE_STRING, format_cb_origin_flag }, -- cgit From 98cd8e4cad3afbc542de665e9d025d12b560bc6b Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 8 Mar 2022 11:28:40 +0000 Subject: Add formats for client and server UID and user (for multiuser setups). --- format.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'format.c') diff --git a/format.c b/format.c index 15f3179c..f381a0ec 100644 --- a/format.c +++ b/format.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -1387,6 +1388,35 @@ format_cb_client_tty(struct format_tree *ft) return (NULL); } +/* Callback for client_uid. */ +static void * +format_cb_client_uid(struct format_tree *ft) +{ + uid_t uid; + + if (ft->c != NULL) { + uid = proc_get_peer_uid(ft->c->peer); + if (uid != (uid_t)-1) + return (format_printf("%ld", (long)uid)); + } + return (NULL); +} + +/* Callback for client_user. */ +static void * +format_cb_client_user(struct format_tree *ft) +{ + uid_t uid; + struct passwd *pw; + + if (ft->c != NULL) { + uid = proc_get_peer_uid(ft->c->peer); + if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL) + return (xstrdup(pw->pw_name)); + } + return (NULL); +} + /* Callback for client_utf8. */ static void * format_cb_client_utf8(struct format_tree *ft) @@ -2521,6 +2551,24 @@ format_cb_tree_mode_format(__unused struct format_tree *ft) return (xstrdup(window_tree_mode.default_format)); } +/* Callback for uid. */ +static void * +format_cb_uid(__unused struct format_tree *ft) +{ + return (format_printf("%ld", (long)getuid())); +} + +/* Callback for user. */ +static void * +format_cb_user(__unused struct format_tree *ft) +{ + struct passwd *pw; + + if ((pw = getpwuid(getuid())) != NULL) + return (xstrdup(pw->pw_name)); + return NULL; +} + /* Format table type. */ enum format_table_type { FORMAT_TABLE_STRING, @@ -2627,6 +2675,12 @@ static const struct format_table_entry format_table[] = { { "client_tty", FORMAT_TABLE_STRING, format_cb_client_tty }, + { "client_uid", FORMAT_TABLE_STRING, + format_cb_client_uid + }, + { "client_user", FORMAT_TABLE_STRING, + format_cb_client_user + }, { "client_utf8", FORMAT_TABLE_STRING, format_cb_client_utf8 }, @@ -2906,6 +2960,12 @@ static const struct format_table_entry format_table[] = { { "tree_mode_format", FORMAT_TABLE_STRING, format_cb_tree_mode_format }, + { "uid", FORMAT_TABLE_STRING, + format_cb_uid + }, + { "user", FORMAT_TABLE_STRING, + format_cb_user + }, { "version", FORMAT_TABLE_STRING, format_cb_version }, -- cgit From a3d920930bf77cafa5260e25584dd3ba1d26f9cb Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 8 Mar 2022 18:31:46 +0000 Subject: Add remain-on-exit-format to set text shown when pane is dead. --- format.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'format.c') diff --git a/format.c b/format.c index f381a0ec..981161b3 100644 --- a/format.c +++ b/format.c @@ -1756,6 +1756,23 @@ format_cb_pane_dead(struct format_tree *ft) return (NULL); } +/* Callback for pane_dead_signal. */ +static void * +format_cb_pane_dead_signal(struct format_tree *ft) +{ + struct window_pane *wp = ft->wp; + const char *name; + + if (wp != NULL) { + if ((wp->flags & PANE_STATUSREADY) && WIFSIGNALED(wp->status)) { + name = sig2name(WTERMSIG(wp->status)); + return (format_printf("%s", name)); + } + return (NULL); + } + return (NULL); +} + /* Callback for pane_dead_status. */ static void * format_cb_pane_dead_status(struct format_tree *ft) @@ -1770,6 +1787,20 @@ format_cb_pane_dead_status(struct format_tree *ft) return (NULL); } +/* Callback for pane_dead_time. */ +static void * +format_cb_pane_dead_time(struct format_tree *ft) +{ + struct window_pane *wp = ft->wp; + + if (wp != NULL) { + if (wp->flags & PANE_STATUSDRAWN) + return (&wp->dead_time); + return (NULL); + } + return (NULL); +} + /* Callback for pane_format. */ static void * format_cb_pane_format(struct format_tree *ft) @@ -2804,9 +2835,15 @@ static const struct format_table_entry format_table[] = { { "pane_dead", FORMAT_TABLE_STRING, format_cb_pane_dead }, + { "pane_dead_signal", FORMAT_TABLE_STRING, + format_cb_pane_dead_signal + }, { "pane_dead_status", FORMAT_TABLE_STRING, format_cb_pane_dead_status }, + { "pane_dead_time", FORMAT_TABLE_TIME, + format_cb_pane_dead_time + }, { "pane_fg", FORMAT_TABLE_STRING, format_cb_pane_fg }, -- cgit From 3a6d82b7c8d4254fa87959d8cf19b313f5e05480 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Wed, 6 Apr 2022 16:39:46 +0100 Subject: Some style nits. --- format.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'format.c') diff --git a/format.c b/format.c index 2e4bceb5..5e04a6a2 100644 --- a/format.c +++ b/format.c @@ -2597,7 +2597,7 @@ format_cb_user(__unused struct format_tree *ft) if ((pw = getpwuid(getuid())) != NULL) return (xstrdup(pw->pw_name)); - return NULL; + return (NULL); } /* Format table type. */ -- cgit From 1b28b2b51d264544a7c9490087561c9192459ba5 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Fri, 20 May 2022 08:49:05 +0100 Subject: Add pane_start_path to match start_command. --- format.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'format.c') diff --git a/format.c b/format.c index 5e04a6a2..ccd259ec 100644 --- a/format.c +++ b/format.c @@ -801,6 +801,20 @@ format_cb_start_command(struct format_tree *ft) return (cmd_stringify_argv(wp->argc, wp->argv)); } +/* Callback for pane_start_path. */ +static void * +format_cb_start_path(struct format_tree *ft) +{ + struct window_pane *wp = ft->wp; + + if (wp == NULL) + return (NULL); + + if (wp->cwd == NULL) + return (xstrdup("")); + return (xstrdup(wp->cwd)); +} + /* Callback for pane_current_command. */ static void * format_cb_current_command(struct format_tree *ft) @@ -2898,6 +2912,9 @@ static const struct format_table_entry format_table[] = { { "pane_start_command", FORMAT_TABLE_STRING, format_cb_start_command }, + { "pane_start_path", FORMAT_TABLE_STRING, + format_cb_start_path + }, { "pane_synchronized", FORMAT_TABLE_STRING, format_cb_pane_synchronized }, -- cgit From 0a8f356c7278dba34a526adea03561f2063df359 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 30 May 2022 12:55:25 +0000 Subject: Spacing/style nits. --- format.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'format.c') diff --git a/format.c b/format.c index 981161b3..24d03af0 100644 --- a/format.c +++ b/format.c @@ -2597,7 +2597,7 @@ format_cb_user(__unused struct format_tree *ft) if ((pw = getpwuid(getuid())) != NULL) return (xstrdup(pw->pw_name)); - return NULL; + return (NULL); } /* Format table type. */ -- cgit From 2f2bb82f5f9c7ba995e8c21a217926efbbb4c5e5 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 30 May 2022 13:07:06 +0000 Subject: Add pane_start_path to match start_command. --- format.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'format.c') diff --git a/format.c b/format.c index 24d03af0..d085e348 100644 --- a/format.c +++ b/format.c @@ -801,6 +801,20 @@ format_cb_start_command(struct format_tree *ft) return (cmd_stringify_argv(wp->argc, wp->argv)); } +/* Callback for pane_start_path. */ +static void * +format_cb_start_path(struct format_tree *ft) +{ + struct window_pane *wp = ft->wp; + + if (wp == NULL) + return (NULL); + + if (wp->cwd == NULL) + return (xstrdup("")); + return (xstrdup(wp->cwd)); +} + /* Callback for pane_current_command. */ static void * format_cb_current_command(struct format_tree *ft) @@ -2898,6 +2912,9 @@ static const struct format_table_entry format_table[] = { { "pane_start_command", FORMAT_TABLE_STRING, format_cb_start_command }, + { "pane_start_path", FORMAT_TABLE_STRING, + format_cb_start_path + }, { "pane_synchronized", FORMAT_TABLE_STRING, format_cb_pane_synchronized }, -- cgit From 9c89f7c2af748858e784e8c533c548460bd6b10e Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 21 Jun 2022 09:30:01 +0000 Subject: Store time lines are scrolled into history and display in copy mode. --- format.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'format.c') diff --git a/format.c b/format.c index d085e348..69d8ef34 100644 --- a/format.c +++ b/format.c @@ -3387,12 +3387,12 @@ format_quote_style(const char *s) } /* Make a prettier time. */ -static char * -format_pretty_time(time_t t) +char * +format_pretty_time(time_t t, int seconds) { struct tm now_tm, tm; time_t now, age; - char s[6]; + char s[9]; time(&now); if (now < t) @@ -3404,7 +3404,10 @@ format_pretty_time(time_t t) /* Last 24 hours. */ if (age < 24 * 3600) { - strftime(s, sizeof s, "%H:%M", &tm); + if (seconds) + strftime(s, sizeof s, "%H:%M:%S", &tm); + else + strftime(s, sizeof s, "%H:%M", &tm); return (xstrdup(s)); } @@ -3509,7 +3512,7 @@ found: if (t == 0) return (NULL); if (modifiers & FORMAT_PRETTY) - found = format_pretty_time(t); + found = format_pretty_time(t, 0); else { if (time_format != NULL) { localtime_r(&t, &tm); -- cgit From 786cff8db9dd64ec8143a492c63051582ee41288 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 27 Jun 2022 09:14:49 +0000 Subject: Do not expand single character format aliases inside #[] since they interfere with colours. GitHub issue 3239 from Magnus Gross. --- format.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'format.c') diff --git a/format.c b/format.c index 69d8ef34..e10f324f 100644 --- a/format.c +++ b/format.c @@ -4606,7 +4606,7 @@ format_expand1(struct format_expand_state *es, const char *fmt) { struct format_tree *ft = es->ft; char *buf, *out, *name; - const char *ptr, *s; + const char *ptr, *s, *style_end = NULL; size_t off, len, n, outlen; int ch, brackets; char expanded[8192]; @@ -4701,13 +4701,14 @@ format_expand1(struct format_expand_state *es, const char *fmt) break; fmt += n + 1; continue; + case '[': case '#': /* * If ##[ (with two or more #s), then it is a style and * can be left for format_draw to handle. */ - ptr = fmt; - n = 2; + ptr = fmt - (ch == '['); + n = 2 - (ch == '['); while (*ptr == '#') { ptr++; n++; @@ -4721,6 +4722,7 @@ format_expand1(struct format_expand_state *es, const char *fmt) memcpy(buf + off, fmt - 2, n + 1); off += n + 1; fmt = ptr + 1; + style_end = format_skip(fmt - 2, "]"); continue; } /* FALLTHROUGH */ @@ -4735,10 +4737,12 @@ format_expand1(struct format_expand_state *es, const char *fmt) continue; default: s = NULL; - if (ch >= 'A' && ch <= 'Z') - s = format_upper[ch - 'A']; - else if (ch >= 'a' && ch <= 'z') - s = format_lower[ch - 'a']; + if (fmt > style_end) { /* skip inside #[] */ + if (ch >= 'A' && ch <= 'Z') + s = format_upper[ch - 'A']; + else if (ch >= 'a' && ch <= 'z') + s = format_lower[ch - 'a']; + } if (s == NULL) { while (len - off < 3) { buf = xreallocarray(buf, 2, len); -- cgit From b22edcf3a5e6271625141992c1a842d295c8b89f Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 27 Jun 2022 09:16:54 +0000 Subject: Tweak previous - find end of style correctly. --- format.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'format.c') diff --git a/format.c b/format.c index e10f324f..b97c842c 100644 --- a/format.c +++ b/format.c @@ -4714,6 +4714,7 @@ format_expand1(struct format_expand_state *es, const char *fmt) n++; } if (*ptr == '[') { + style_end = format_skip(fmt - 2, "]"); format_log(es, "found #*%zu[", n); while (len - off < n + 2) { buf = xreallocarray(buf, 2, len); @@ -4722,7 +4723,6 @@ format_expand1(struct format_expand_state *es, const char *fmt) memcpy(buf + off, fmt - 2, n + 1); off += n + 1; fmt = ptr + 1; - style_end = format_skip(fmt - 2, "]"); continue; } /* FALLTHROUGH */ -- cgit From d0d2c39decd1c342f2ffdb360e5d6b509b9bb34e Mon Sep 17 00:00:00 2001 From: nicm Date: Wed, 6 Jul 2022 07:36:36 +0000 Subject: Support hyperlinks with capture-pane -e and add a mouse_hyperlink format, GitHub issue 3247 from Jeff Chiang. --- format.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'format.c') diff --git a/format.c b/format.c index b97c842c..edf4217c 100644 --- a/format.c +++ b/format.c @@ -1145,6 +1145,25 @@ format_cb_mouse_word(struct format_tree *ft) return (format_grid_word(gd, x, gd->hsize + y)); } +/* Callback for mouse_hyperlink. */ +static void * +format_cb_mouse_hyperlink(struct format_tree *ft) +{ + struct window_pane *wp; + struct grid *gd; + u_int x, y; + + if (!ft->m.valid) + return (NULL); + wp = cmd_mouse_pane(&ft->m, NULL, NULL); + if (wp == NULL) + return (NULL); + if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0) + return (NULL); + gd = wp->base.grid; + return (format_grid_hyperlink(gd, x, gd->hsize + y, wp->screen)); +} + /* Callback for mouse_line. */ static void * format_cb_mouse_line(struct format_tree *ft) @@ -2789,6 +2808,9 @@ static const struct format_table_entry format_table[] = { { "mouse_button_flag", FORMAT_TABLE_STRING, format_cb_mouse_button_flag }, + { "mouse_hyperlink", FORMAT_TABLE_STRING, + format_cb_mouse_hyperlink + }, { "mouse_line", FORMAT_TABLE_STRING, format_cb_mouse_line }, @@ -5064,3 +5086,20 @@ format_grid_line(struct grid *gd, u_int y) } return (s); } + +/* Return hyperlink at given coordinates. Caller frees. */ +char * +format_grid_hyperlink(struct grid *gd, u_int x, u_int y, struct screen* s) +{ + const char *uri; + struct grid_cell gc; + + grid_get_cell(gd, x, y, &gc); + if (gc.flags & GRID_FLAG_PADDING) + return (NULL); + if (s->hyperlinks == NULL || gc.link == 0) + return (NULL); + if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL)) + return (NULL); + return (xstrdup(uri)); +} -- cgit From 3c65475561b25073c3b7dcbb0b6498a0535ecd59 Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 19 Jul 2022 06:46:57 +0000 Subject: Fix memory leak, from Gabriel Souza Franco. --- format.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'format.c') diff --git a/format.c b/format.c index edf4217c..535af061 100644 --- a/format.c +++ b/format.c @@ -3564,12 +3564,12 @@ found: } if (modifiers & FORMAT_QUOTE_SHELL) { saved = found; - found = xstrdup(format_quote_shell(saved)); + found = format_quote_shell(saved); free(saved); } if (modifiers & FORMAT_QUOTE_STYLE) { saved = found; - found = xstrdup(format_quote_style(saved)); + found = format_quote_style(saved); free(saved); } return (found); -- cgit