From 743ab5728da1ce950a3782d5bc08831f61c5d744 Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 21 Jul 2020 05:24:33 +0000 Subject: [PATCH] Fix show-buffer when run from inside tmux, GitHub issue 2314. --- cmd-save-buffer.c | 14 +++++++++++--- screen-write.c | 5 ++++- tmux.h | 1 + utf8.c | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/cmd-save-buffer.c b/cmd-save-buffer.c index a6ad188f..5e45e279 100644 --- a/cmd-save-buffer.c +++ b/cmd-save-buffer.c @@ -74,11 +74,12 @@ static enum cmd_retval cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = cmd_get_args(self); + struct client *c = cmdq_get_client(item); struct paste_buffer *pb; int flags; const char *bufname = args_get(args, 'b'), *bufdata; size_t bufsize; - char *path; + char *path, *tmp; if (bufname == NULL) { if ((pb = paste_get_top(NULL)) == NULL) { @@ -94,9 +95,16 @@ cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item) } bufdata = paste_buffer_data(pb, &bufsize); - if (cmd_get_entry(self) == &cmd_show_buffer_entry) + if (cmd_get_entry(self) == &cmd_show_buffer_entry) { + if (c->session != NULL || (c->flags & CLIENT_CONTROL)) { + utf8_stravisx(&tmp, bufdata, bufsize, + VIS_OCTAL|VIS_CSTYLE|VIS_TAB); + cmdq_print(item, "%s", tmp); + free(tmp); + return (CMD_RETURN_NORMAL); + } path = xstrdup("-"); - else + } else path = format_single_from_target(item, args->argv[0]); if (args_has(args, 'a')) flags = O_APPEND; diff --git a/screen-write.c b/screen-write.c index 16a9c1d7..c9c61086 100644 --- a/screen-write.c +++ b/screen-write.c @@ -514,7 +514,10 @@ screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen, if (*ptr == '\001') gc.attr ^= GRID_ATTR_CHARSET; - else if (*ptr > 0x1f && *ptr < 0x7f) { + else if (*ptr == '\n') { + screen_write_linefeed(ctx, 0, 8); + screen_write_carriagereturn(ctx); + } else if (*ptr > 0x1f && *ptr < 0x7f) { size++; screen_write_putc(ctx, &gc, *ptr); } diff --git a/tmux.h b/tmux.h index 07dfc0ae..a6749e99 100644 --- a/tmux.h +++ b/tmux.h @@ -2940,6 +2940,7 @@ enum utf8_state utf8_append(struct utf8_data *, u_char); int utf8_isvalid(const char *); int utf8_strvis(char *, const char *, size_t, int); int utf8_stravis(char **, const char *, int); +int utf8_stravisx(char **, const char *, size_t, int); char *utf8_sanitize(const char *); size_t utf8_strlen(const struct utf8_data *); u_int utf8_strwidth(const struct utf8_data *, ssize_t); diff --git a/utf8.c b/utf8.c index e3fe570d..36c9daad 100644 --- a/utf8.c +++ b/utf8.c @@ -341,6 +341,20 @@ utf8_stravis(char **dst, const char *src, int flag) return (len); } +/* Same as utf8_strvis but allocate the buffer. */ +int +utf8_stravisx(char **dst, const char *src, size_t srclen, int flag) +{ + char *buf; + int len; + + buf = xreallocarray(NULL, 4, srclen + 1); + len = utf8_strvis(buf, src, srclen, flag); + + *dst = xrealloc(buf, len + 1); + return (len); +} + /* Does this string contain anything that isn't valid UTF-8? */ int utf8_isvalid(const char *s)