Skip to content

Commit

Permalink
Add a command-error hook when a command fails, from Hugh Davenport in
Browse files Browse the repository at this point in the history
GitHub issue 3973.
  • Loading branch information
nicm committed May 14, 2024
1 parent 4c928dc commit c961670
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cmd-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions options-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""),
Expand Down
10 changes: 10 additions & 0 deletions tmux.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c961670

Please sign in to comment.