From 1adaafb706b4493550229fb0e410729ca936ce41 Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 14 May 2024 07:52:19 +0000 Subject: [PATCH] Add a command-error hook when a command fails, from Hugh Davenport in GitHub issue 3973. --- usr.bin/tmux/cmd-queue.c | 15 ++++++++++++--- usr.bin/tmux/options-table.c | 3 ++- usr.bin/tmux/tmux.1 | 12 +++++++++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/usr.bin/tmux/cmd-queue.c b/usr.bin/tmux/cmd-queue.c index 623535a1208..81ed3de30a8 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.116 2024/04/23 13:34:51 jsg Exp $ */ +/* $OpenBSD: cmd-queue.c,v 1.117 2024/05/14 07:52:19 nicm Exp $ */ /* * Copyright (c) 2013 Nicholas Marriott @@ -665,9 +665,18 @@ cmdq_fire_command(struct cmdq_item *item) out: item->client = saved; - if (retval == CMD_RETURN_ERROR) + if (retval == CMD_RETURN_ERROR) { + fsp = NULL; + if (cmd_find_valid_state(&item->target)) + fsp = &item->target; + else if (cmd_find_valid_state(&item->state->current)) + fsp = &item->state->current; + else if (cmd_find_from_client(&fs, item->client, 0) == 0) + fsp = &fs; + cmdq_insert_hook(fsp != NULL ? fsp->s : NULL, item, fsp, + "command-error"); cmdq_guard(item, "error", flags); - else + } else cmdq_guard(item, "end", flags); return (retval); } diff --git a/usr.bin/tmux/options-table.c b/usr.bin/tmux/options-table.c index 998e2764ff8..98c226d26af 100644 --- a/usr.bin/tmux/options-table.c +++ b/usr.bin/tmux/options-table.c @@ -1,4 +1,4 @@ -/* $OpenBSD: options-table.c,v 1.172 2024/05/14 07:40:39 nicm Exp $ */ +/* $OpenBSD: options-table.c,v 1.173 2024/05/14 07:52:19 nicm Exp $ */ /* * Copyright (c) 2011 Nicholas Marriott @@ -1334,6 +1334,7 @@ const struct options_table_entry options_table[] = { OPTIONS_TABLE_HOOK("client-focus-out", ""), OPTIONS_TABLE_HOOK("client-resized", ""), OPTIONS_TABLE_HOOK("client-session-changed", ""), + OPTIONS_TABLE_HOOK("command-error", ""), OPTIONS_TABLE_PANE_HOOK("pane-died", ""), OPTIONS_TABLE_PANE_HOOK("pane-exited", ""), OPTIONS_TABLE_PANE_HOOK("pane-focus-in", ""), diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1 index d29565f39e6..3bd92c46852 100644 --- a/usr.bin/tmux/tmux.1 +++ b/usr.bin/tmux/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.942 2024/05/14 07:40:39 nicm Exp $ +.\" $OpenBSD: tmux.1,v 1.943 2024/05/14 07:52:19 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott .\" @@ -4889,6 +4889,14 @@ layout after every set-hook -g after-split-window "selectl even-vertical" .Ed .Pp +If a command fails, the +.Ql command-error +hook will be fired. +For example, this could be used to write to a log file: +.Bd -literal -offset indent +set-hook -g command-error "run-shell \\"echo 'a tmux command failed' >>/tmp/log\\"" +.Ed +.Pp All the notifications listed in the .Sx CONTROL MODE section are hooks (without any arguments), except @@ -4921,6 +4929,8 @@ Run when focus exits a client Run when a client is resized. .It client-session-changed Run when a client's attached session is changed. +.It command-error +Run when a command fails. .It pane-died Run when the program running in a pane exits, but .Ic remain-on-exit -- 2.20.1