Add single quote operator.

This commit is contained in:
nicm
2026-07-03 14:48:07 +00:00
parent fa2c9f4551
commit cceca5a158
2 changed files with 34 additions and 1 deletions

View File

@@ -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;

4
tmux.1
View File

@@ -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}