From cceca5a1584881ee5e3a34011efc7178a5e7b7ef Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 3 Jul 2026 14:48:07 +0000 Subject: [PATCH] Add single quote operator. --- format.c | 31 +++++++++++++++++++++++++++++++ tmux.1 | 4 +++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/format.c b/format.c index 4055efa5d..f73a6caa0 100644 --- a/format.c +++ b/format.c @@ -123,6 +123,7 @@ format_job_cmp(struct format_job *fj1, struct format_job *fj2) #define FORMAT_CLIENT_ENVIRON 0x4000000 #define FORMAT_COLOUR_ESC_FG 0x8000000 #define FORMAT_COLOUR_ESC_BG 0x10000000 +#define FORMAT_QUOTE_SHELL_SQ 0x20000000 /* Limit on recursion. */ #define FORMAT_LOOP_LIMIT 100 @@ -4009,6 +4010,29 @@ format_quote_shell(const char *s) return (out); } +/* Quote string with POSIX shell single quotes. */ +static char * +format_quote_shell_single(const char *s) +{ + const char *cp; + char *out, *at; + + at = out = xmalloc(strlen(s) * 4 + 3); + *at++ = '\''; + for (cp = s; *cp != '\0'; cp++) { + if (*cp == '\'') { + *at++ = '\''; + *at++ = '\\'; + *at++ = '\''; + *at++ = '\''; + } else + *at++ = *cp; + } + *at++ = '\''; + *at = '\0'; + return (out); +} + /* Quote #s in string. */ static char * format_quote_style(const char *s) @@ -4227,6 +4251,11 @@ found: found = format_quote_shell(saved); free(saved); } + if (modifiers & FORMAT_QUOTE_SHELL_SQ) { + saved = found; + found = format_quote_shell_single(saved); + free(saved); + } if (modifiers & FORMAT_QUOTE_STYLE) { saved = found; found = format_quote_style(saved); @@ -5297,6 +5326,8 @@ format_replace(struct format_expand_state *es, const char *key, size_t keylen, case 'q': if (fm->argc < 1) modifiers |= FORMAT_QUOTE_SHELL; + else if (strchr(fm->argv[0], 's') != NULL) + modifiers |= FORMAT_QUOTE_SHELL_SQ; else if (strchr(fm->argv[0], 'e') != NULL || strchr(fm->argv[0], 'h') != NULL) modifiers |= FORMAT_QUOTE_STYLE; diff --git a/tmux.1 b/tmux.1 index 6061fe9a0..f016d9db9 100644 --- a/tmux.1 +++ b/tmux.1 @@ -6820,7 +6820,9 @@ or with .Ql a escape .Nm -command arguments. +command arguments; with +.Ql s +use single quotes. .Ql E:\& will expand the format twice, for example .Ql #{E:status\-left}