From: nicm Date: Thu, 12 Aug 2021 08:05:11 +0000 (+0000) Subject: Move hook format setup earlier and add a hook_client, GitHub issue 2809. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=9eee77352704a4b4fee7786d72d82865da232633;p=openbsd Move hook format setup earlier and add a hook_client, GitHub issue 2809. --- diff --git a/usr.bin/tmux/cmd-queue.c b/usr.bin/tmux/cmd-queue.c index fb968e88842..be36a9be8f1 100644 --- a/usr.bin/tmux/cmd-queue.c +++ b/usr.bin/tmux/cmd-queue.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-queue.c,v 1.102 2021/04/12 09:36:12 nicm Exp $ */ +/* $OpenBSD: cmd-queue.c,v 1.103 2021/08/12 08:05:11 nicm Exp $ */ /* * Copyright (c) 2013 Nicholas Marriott @@ -269,6 +269,15 @@ cmdq_add_format(struct cmdq_state *state, const char *key, const char *fmt, ...) free(value); } +/* Add formats to command queue. */ +void +cmdq_add_formats(struct cmdq_state *state, struct format_tree *ft) +{ + if (state->formats == NULL) + state->formats = format_create(NULL, NULL, FORMAT_NONE, 0); + format_merge(state->formats, ft); +} + /* Merge formats from item. */ void cmdq_merge_formats(struct cmdq_item *item, struct format_tree *ft) diff --git a/usr.bin/tmux/format.c b/usr.bin/tmux/format.c index 19ed9326947..9f788687e43 100644 --- a/usr.bin/tmux/format.c +++ b/usr.bin/tmux/format.c @@ -1,4 +1,4 @@ -/* $OpenBSD: format.c,v 1.288 2021/07/13 22:09:29 nicm Exp $ */ +/* $OpenBSD: format.c,v 1.289 2021/08/12 08:05:11 nicm Exp $ */ /* * Copyright (c) 2011 Nicholas Marriott @@ -929,6 +929,9 @@ format_cb_pane_fg(struct format_tree *ft) struct window_pane *wp = ft->wp; struct grid_cell gc; + if (wp == NULL) + return (NULL); + tty_default_colours(&gc, wp); return (xstrdup(colour_tostring(gc.fg))); } @@ -940,6 +943,9 @@ format_cb_pane_bg(struct format_tree *ft) struct window_pane *wp = ft->wp; struct grid_cell gc; + if (wp == NULL) + return (NULL); + tty_default_colours(&gc, wp); return (xstrdup(colour_tostring(gc.bg))); } @@ -3079,6 +3085,22 @@ format_free(struct format_tree *ft) free(ft); } +/* Log each format. */ +static void +format_log_debug_cb(const char *key, const char *value, void *arg) +{ + const char *prefix = arg; + + log_debug("%s: %s=%s", prefix, key, value); +} + +/* Log a format tree. */ +void +format_log_debug(struct format_tree *ft, const char *prefix) +{ + format_each(ft, format_log_debug_cb, prefix); +} + /* Walk each format. */ void format_each(struct format_tree *ft, void (*cb)(const char *, const char *, diff --git a/usr.bin/tmux/notify.c b/usr.bin/tmux/notify.c index 9bb6e4e8b82..69d8ce1ffc5 100644 --- a/usr.bin/tmux/notify.c +++ b/usr.bin/tmux/notify.c @@ -1,4 +1,4 @@ -/* $OpenBSD: notify.c,v 1.37 2021/03/16 09:14:58 nicm Exp $ */ +/* $OpenBSD: notify.c,v 1.38 2021/08/12 08:05:11 nicm Exp $ */ /* * Copyright (c) 2012 George Nachman @@ -26,31 +26,15 @@ struct notify_entry { const char *name; + struct cmd_find_state fs; + struct format_tree *formats; struct client *client; struct session *session; struct window *window; int pane; - - struct cmd_find_state fs; }; -static void -notify_hook_formats(struct cmdq_state *state, struct session *s, - struct window *w, int pane) -{ - if (s != NULL) { - cmdq_add_format(state, "hook_session", "$%u", s->id); - cmdq_add_format(state, "hook_session_name", "%s", s->name); - } - if (w != NULL) { - cmdq_add_format(state, "hook_window", "@%u", w->id); - cmdq_add_format(state, "hook_window_name", "%s", w->name); - } - if (pane != -1) - cmdq_add_format(state, "hook_pane", "%%%d", pane); -} - static void notify_insert_hook(struct cmdq_item *item, struct notify_entry *ne) { @@ -58,8 +42,6 @@ notify_insert_hook(struct cmdq_item *item, struct notify_entry *ne) struct options *oo; struct cmdq_item *new_item; struct cmdq_state *new_state; - struct session *s = ne->session; - struct window *w = ne->window; struct options_entry *o; struct options_array_item *a; struct cmd_list *cmdlist; @@ -89,8 +71,7 @@ notify_insert_hook(struct cmdq_item *item, struct notify_entry *ne) return; new_state = cmdq_new_state(&fs, NULL, CMDQ_STATE_NOHOOKS); - cmdq_add_format(new_state, "hook", "%s", ne->name); - notify_hook_formats(new_state, s, w, ne->pane); + cmdq_add_formats(new_state, ne->formats); a = options_array_first(o); while (a != NULL) { @@ -149,6 +130,7 @@ notify_callback(struct cmdq_item *item, void *data) if (ne->fs.s != NULL) session_remove_ref(ne->fs.s, __func__); + format_free(ne->formats); free((void *)ne->name); free(ne); @@ -172,11 +154,23 @@ notify_add(const char *name, struct cmd_find_state *fs, struct client *c, ne->client = c; ne->session = s; ne->window = w; + ne->pane = (wp != NULL ? wp->id : -1); + ne->formats = format_create(NULL, NULL, 0, FORMAT_NOJOBS); + format_add(ne->formats, "hook", "%s", name); + if (c != NULL) + format_add(ne->formats, "hook_client", "%s", c->name); + if (s != NULL) { + format_add(ne->formats, "hook_session", "$%u", s->id); + format_add(ne->formats, "hook_session_name", "%s", s->name); + } + if (w != NULL) { + format_add(ne->formats, "hook_window", "@%u", w->id); + format_add(ne->formats, "hook_window_name", "%s", w->name); + } if (wp != NULL) - ne->pane = wp->id; - else - ne->pane = -1; + format_add(ne->formats, "hook_pane", "%%%d", wp->id); + format_log_debug(ne->formats, __func__); if (c != NULL) c->references++; @@ -208,7 +202,12 @@ notify_hook(struct cmdq_item *item, const char *name) ne.window = target->w; ne.pane = target->wp->id; + ne.formats = format_create(NULL, NULL, 0, FORMAT_NOJOBS); + format_add(ne.formats, "hook", "%s", name); + format_log_debug(ne.formats, __func__); + notify_insert_hook(item, &ne); + format_free(ne.formats); } void diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1 index d15bde2329c..0c4e4426e54 100644 --- a/usr.bin/tmux/tmux.1 +++ b/usr.bin/tmux/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.850 2021/08/11 20:49:55 nicm Exp $ +.\" $OpenBSD: tmux.1,v 1.851 2021/08/12 08:05:11 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott .\" @@ -14,7 +14,7 @@ .\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING .\" OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: August 11 2021 $ +.Dd $Mdocdate: August 12 2021 $ .Dt TMUX 1 .Os .Sh NAME @@ -4925,6 +4925,7 @@ The following variables are available, where appropriate: .It Li "history_limit" Ta "" Ta "Maximum window history lines" .It Li "history_size" Ta "" Ta "Size of history in lines" .It Li "hook" Ta "" Ta "Name of running hook, if any" +.It Li "hook_client" Ta "" Ta "Name of client where hook was run, if any" .It Li "hook_pane" Ta "" Ta "ID of pane where hook was run, if any" .It Li "hook_session" Ta "" Ta "ID of session where hook was run, if any" .It Li "hook_session_name" Ta "" Ta "Name of session where hook was run, if any" diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index ad96643fab1..755c1c07e98 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.1115 2021/08/11 20:49:55 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.1116 2021/08/12 08:05:11 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -2023,6 +2023,7 @@ void printflike(3, 4) format_add(struct format_tree *, const char *, void format_add_tv(struct format_tree *, const char *, struct timeval *); void format_add_cb(struct format_tree *, const char *, format_cb); +void format_log_debug(struct format_tree *, const char *); void format_each(struct format_tree *, void (*)(const char *, const char *, void *), void *); char *format_expand_time(struct format_tree *, const char *); @@ -2382,6 +2383,7 @@ struct cmdq_state *cmdq_copy_state(struct cmdq_state *); void cmdq_free_state(struct cmdq_state *); void printflike(3, 4) cmdq_add_format(struct cmdq_state *, const char *, const char *, ...); +void cmdq_add_formats(struct cmdq_state *, struct format_tree *); void cmdq_merge_formats(struct cmdq_item *, struct format_tree *); struct cmdq_list *cmdq_new(void); void cmdq_free(struct cmdq_list *);