199e242abSchristos /* $OpenBSD$ */
2698d5317Sjmmv
3698d5317Sjmmv /*
4698d5317Sjmmv * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5698d5317Sjmmv *
6698d5317Sjmmv * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv *
10698d5317Sjmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv */
18698d5317Sjmmv
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv
2161fba46bSchristos #include <stdlib.h>
22698d5317Sjmmv #include <time.h>
23698d5317Sjmmv
24698d5317Sjmmv #include "tmux.h"
25698d5317Sjmmv
26698d5317Sjmmv /*
27698d5317Sjmmv * Displays a message in the status line.
28698d5317Sjmmv */
29698d5317Sjmmv
3099e242abSchristos #define DISPLAY_MESSAGE_TEMPLATE \
3199e242abSchristos "[#{session_name}] #{window_index}:" \
3299e242abSchristos "#{window_name}, current pane #{pane_index} " \
3399e242abSchristos "- (%H:%M %d-%b-%y)"
3499e242abSchristos
35e9a2d6faSchristos static enum cmd_retval cmd_display_message_exec(struct cmd *,
36e9a2d6faSchristos struct cmdq_item *);
37698d5317Sjmmv
38698d5317Sjmmv const struct cmd_entry cmd_display_message_entry = {
39f26e8bc9Schristos .name = "display-message",
40f26e8bc9Schristos .alias = "display",
41f26e8bc9Schristos
42*f844e94eSwiz .args = { "ac:d:lINpt:F:v", 0, 1, NULL },
43*f844e94eSwiz .usage = "[-aIlNpv] [-c target-client] [-d delay] [-F format] "
44f26e8bc9Schristos CMD_TARGET_PANE_USAGE " [message]",
45f26e8bc9Schristos
46e271dbb8Schristos .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
47f26e8bc9Schristos
48e271dbb8Schristos .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG|CMD_CLIENT_CANFAIL,
49f26e8bc9Schristos .exec = cmd_display_message_exec
50698d5317Sjmmv };
51698d5317Sjmmv
520a274e86Schristos static void
cmd_display_message_each(const char * key,const char * value,void * arg)530a274e86Schristos cmd_display_message_each(const char *key, const char *value, void *arg)
540a274e86Schristos {
550a274e86Schristos struct cmdq_item *item = arg;
560a274e86Schristos
570a274e86Schristos cmdq_print(item, "%s=%s", key, value);
580a274e86Schristos }
590a274e86Schristos
60e9a2d6faSchristos static enum cmd_retval
cmd_display_message_exec(struct cmd * self,struct cmdq_item * item)61e9a2d6faSchristos cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
62698d5317Sjmmv {
63e271dbb8Schristos struct args *args = cmd_get_args(self);
64e271dbb8Schristos struct cmd_find_state *target = cmdq_get_target(item);
65e271dbb8Schristos struct client *tc = cmdq_get_target_client(item), *c;
66e271dbb8Schristos struct session *s = target->s;
67e271dbb8Schristos struct winlink *wl = target->wl;
68e271dbb8Schristos struct window_pane *wp = target->wp;
69698d5317Sjmmv const char *template;
7030744affSchristos char *msg, *cause;
71*f844e94eSwiz int delay = -1, flags, Nflag = args_has(args, 'N');
7261fba46bSchristos struct format_tree *ft;
7346548964Swiz u_int count = args_count(args);
74*f844e94eSwiz struct evbuffer *evb;
75d530c4d0Sjmmv
7630744affSchristos if (args_has(args, 'I')) {
77e271dbb8Schristos if (wp == NULL)
78e271dbb8Schristos return (CMD_RETURN_NORMAL);
7946548964Swiz switch (window_pane_start_input(wp, item, &cause)) {
8046548964Swiz case -1:
8130744affSchristos cmdq_error(item, "%s", cause);
8230744affSchristos free(cause);
8330744affSchristos return (CMD_RETURN_ERROR);
8446548964Swiz case 1:
8546548964Swiz return (CMD_RETURN_NORMAL);
8646548964Swiz case 0:
8730744affSchristos return (CMD_RETURN_WAIT);
8830744affSchristos }
8946548964Swiz }
9030744affSchristos
9146548964Swiz if (args_has(args, 'F') && count != 0) {
92e9a2d6faSchristos cmdq_error(item, "only one of -F or argument must be given");
9361fba46bSchristos return (CMD_RETURN_ERROR);
9461fba46bSchristos }
95698d5317Sjmmv
96e271dbb8Schristos if (args_has(args, 'd')) {
97e271dbb8Schristos delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
98e271dbb8Schristos if (cause != NULL) {
99e271dbb8Schristos cmdq_error(item, "delay %s", cause);
100e271dbb8Schristos free(cause);
101e271dbb8Schristos return (CMD_RETURN_ERROR);
102e271dbb8Schristos }
103e271dbb8Schristos }
104e271dbb8Schristos
10546548964Swiz if (count != 0)
10646548964Swiz template = args_string(args, 0);
10746548964Swiz else
10861fba46bSchristos template = args_get(args, 'F');
10961fba46bSchristos if (template == NULL)
11061fba46bSchristos template = DISPLAY_MESSAGE_TEMPLATE;
11161fba46bSchristos
112c7e17de0Schristos /*
113c7e17de0Schristos * -c is intended to be the client where the message should be
114c7e17de0Schristos * displayed if -p is not given. But it makes sense to use it for the
115c7e17de0Schristos * formats too, assuming it matches the session. If it doesn't, use the
116c7e17de0Schristos * best client for the session.
117c7e17de0Schristos */
118e271dbb8Schristos if (tc != NULL && tc->session == s)
119e271dbb8Schristos c = tc;
120e271dbb8Schristos else if (s != NULL)
121e271dbb8Schristos c = cmd_find_best_client(s);
122c7e17de0Schristos else
123e271dbb8Schristos c = NULL;
124e271dbb8Schristos if (args_has(args, 'v'))
1250a274e86Schristos flags = FORMAT_VERBOSE;
1260a274e86Schristos else
1270a274e86Schristos flags = 0;
128e271dbb8Schristos ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, flags);
129e271dbb8Schristos format_defaults(ft, c, s, wl, wp);
13061fba46bSchristos
1310a274e86Schristos if (args_has(args, 'a')) {
1320a274e86Schristos format_each(ft, cmd_display_message_each, item);
1330a274e86Schristos return (CMD_RETURN_NORMAL);
1340a274e86Schristos }
1350a274e86Schristos
136*f844e94eSwiz if (args_has(args, 'l'))
137*f844e94eSwiz msg = xstrdup(template);
138*f844e94eSwiz else
1390a274e86Schristos msg = format_expand_time(ft, template);
140*f844e94eSwiz
141e271dbb8Schristos if (cmdq_get_client(item) == NULL)
142e271dbb8Schristos cmdq_error(item, "%s", msg);
143e271dbb8Schristos else if (args_has(args, 'p'))
144e9a2d6faSchristos cmdq_print(item, "%s", msg);
145*f844e94eSwiz else if (tc != NULL && (tc->flags & CLIENT_CONTROL)) {
146*f844e94eSwiz evb = evbuffer_new();
147*f844e94eSwiz if (evb == NULL)
148*f844e94eSwiz fatalx("out of memory");
149*f844e94eSwiz evbuffer_add_printf(evb, "%%message %s", msg);
150*f844e94eSwiz server_client_print(tc, 0, evb);
151*f844e94eSwiz evbuffer_free(evb);
152*f844e94eSwiz } else if (tc != NULL)
153*f844e94eSwiz status_message_set(tc, delay, 0, Nflag, "%s", msg);
15461fba46bSchristos free(msg);
155e9a2d6faSchristos
15661fba46bSchristos format_free(ft);
157698d5317Sjmmv
15861fba46bSchristos return (CMD_RETURN_NORMAL);
159698d5317Sjmmv }
160