From 93b1b781504f8de30f1858a4d0cf013396143c53 Mon Sep 17 00:00:00 2001 From: nicm Date: Sun, 5 Feb 2023 21:15:32 +0000 Subject: [PATCH] Extend display-message to work for control clients. GitHub issue 3449. --- cmd-display-message.c | 16 ++++++++---- cmd-queue.c | 63 +----------------------------------------------- server-client.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ tmux.1 | 4 +++ tmux.h | 1 + 5 files changed, 84 insertions(+), 67 deletions(-) diff --git a/cmd-display-message.c b/cmd-display-message.c index f5e91020..512509f0 100644 --- a/cmd-display-message.c +++ b/cmd-display-message.c @@ -68,9 +68,10 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item) struct window_pane *wp = target->wp; const char *template; char *msg, *cause; - int delay = -1, flags; + int delay = -1, flags, Nflag = args_has(args, 'N'); struct format_tree *ft; u_int count = args_count(args); + struct evbuffer *evb; if (args_has(args, 'I')) { if (wp == NULL) @@ -141,10 +142,15 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item) cmdq_error(item, "%s", msg); else if (args_has(args, 'p')) cmdq_print(item, "%s", msg); - else if (tc != NULL) { - status_message_set(tc, delay, 0, args_has(args, 'N'), "%s", - msg); - } + else if (tc != NULL && (tc->flags & CLIENT_CONTROL)) { + evb = evbuffer_new(); + if (evb == NULL) + fatalx("out of memory"); + evbuffer_add_printf(evb, "%%message %s", msg); + server_client_print(tc, 0, evb); + evbuffer_free(evb); + } else if (tc != NULL) + status_message_set(tc, delay, 0, Nflag, "%s", msg); free(msg); format_free(ft); diff --git a/cmd-queue.c b/cmd-queue.c index bf1dbdaf..ce6cab38 100644 --- a/cmd-queue.c +++ b/cmd-queue.c @@ -826,68 +826,7 @@ cmdq_guard(struct cmdq_item *item, const char *guard, int flags) void cmdq_print_data(struct cmdq_item *item, int parse, struct evbuffer *evb) { - struct client *c = item->client; - void *data = EVBUFFER_DATA(evb); - size_t size = EVBUFFER_LENGTH(evb); - struct window_pane *wp; - struct window_mode_entry *wme; - char *sanitized, *msg, *line; - - if (!parse) { - utf8_stravisx(&msg, data, size, - VIS_OCTAL|VIS_CSTYLE|VIS_NOSLASH); - log_debug("%s: %s", __func__, msg); - } else { - msg = EVBUFFER_DATA(evb); - if (msg[size - 1] != '\0') - evbuffer_add(evb, "", 1); - } - - if (c == NULL) - goto out; - - if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { - if (~c->flags & CLIENT_UTF8) { - sanitized = utf8_sanitize(msg); - if (c->flags & CLIENT_CONTROL) - control_write(c, "%s", sanitized); - else - file_print(c, "%s\n", sanitized); - free(sanitized); - } else { - if (c->flags & CLIENT_CONTROL) - control_write(c, "%s", msg); - else - file_print(c, "%s\n", msg); - } - goto out; - } - - wp = server_client_get_pane(c); - wme = TAILQ_FIRST(&wp->modes); - if (wme == NULL || wme->mode != &window_view_mode) - window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL); - if (parse) { - do { - line = evbuffer_readln(evb, NULL, EVBUFFER_EOL_LF); - if (line != NULL) { - window_copy_add(wp, 1, "%s", line); - free(line); - } - } while (line != NULL); - - size = EVBUFFER_LENGTH(evb); - if (size != 0) { - line = EVBUFFER_DATA(evb); - window_copy_add(wp, 1, "%.*s", (int)size, line); - } - } else - window_copy_add(wp, 0, "%s", msg); - -out: - if (!parse) - free(msg); - + server_client_print(item->client, parse, evb); } /* Show message from command. */ diff --git a/server-client.c b/server-client.c index f0d1d538..1c927bb5 100644 --- a/server-client.c +++ b/server-client.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "tmux.h" @@ -3239,3 +3240,69 @@ server_client_remove_pane(struct window_pane *wp) } } } + +/* Print to a client. */ +void +server_client_print(struct client *c, int parse, struct evbuffer *evb) +{ + void *data = EVBUFFER_DATA(evb); + size_t size = EVBUFFER_LENGTH(evb); + struct window_pane *wp; + struct window_mode_entry *wme; + char *sanitized, *msg, *line; + + if (!parse) { + utf8_stravisx(&msg, data, size, + VIS_OCTAL|VIS_CSTYLE|VIS_NOSLASH); + log_debug("%s: %s", __func__, msg); + } else { + msg = EVBUFFER_DATA(evb); + if (msg[size - 1] != '\0') + evbuffer_add(evb, "", 1); + } + + if (c == NULL) + goto out; + + if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { + if (~c->flags & CLIENT_UTF8) { + sanitized = utf8_sanitize(msg); + if (c->flags & CLIENT_CONTROL) + control_write(c, "%s", sanitized); + else + file_print(c, "%s\n", sanitized); + free(sanitized); + } else { + if (c->flags & CLIENT_CONTROL) + control_write(c, "%s", msg); + else + file_print(c, "%s\n", msg); + } + goto out; + } + + wp = server_client_get_pane(c); + wme = TAILQ_FIRST(&wp->modes); + if (wme == NULL || wme->mode != &window_view_mode) + window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL); + if (parse) { + do { + line = evbuffer_readln(evb, NULL, EVBUFFER_EOL_LF); + if (line != NULL) { + window_copy_add(wp, 1, "%s", line); + free(line); + } + } while (line != NULL); + + size = EVBUFFER_LENGTH(evb); + if (size != 0) { + line = EVBUFFER_DATA(evb); + window_copy_add(wp, 1, "%.*s", (int)size, line); + } + } else + window_copy_add(wp, 0, "%s", msg); + +out: + if (!parse) + free(msg); +} diff --git a/tmux.1 b/tmux.1 index fdbc97cc..20dce121 100644 --- a/tmux.1 +++ b/tmux.1 @@ -6627,6 +6627,10 @@ The window's visible layout is .Ar window-visible-layout and the window flags are .Ar window-flags . +.It Ic %message Ar message +A message sent with the +.Ic display-message +command. .It Ic %output Ar pane-id Ar value A window pane produced output. .Ar value diff --git a/tmux.h b/tmux.h index 3b1a4fc9..3a389948 100644 --- a/tmux.h +++ b/tmux.h @@ -2678,6 +2678,7 @@ struct client_window *server_client_add_client_window(struct client *, u_int); struct window_pane *server_client_get_pane(struct client *); void server_client_set_pane(struct client *, struct window_pane *); void server_client_remove_pane(struct window_pane *); +void server_client_print(struct client *, int, struct evbuffer *); /* server-fn.c */ void server_redraw_client(struct client *); -- 2.11.4.GIT