Skip to content

Commit 8aa999c

Browse files
committed
Add more hook points
And make the hook calling simpler at the call site.
1 parent 0133c7a commit 8aa999c

File tree

5 files changed

+52
-23
lines changed

5 files changed

+52
-23
lines changed

docs/hooks.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
# Save & Restore Hooks
22

3-
Hooks allow to set custom commands that will be executed during session save and restore.
3+
Hooks allow to set custom commands that will be executed during session save
4+
and restore. Most hooks are called with zero arguments, unless explicitly
5+
stated otherwise.
46

57
Currently the following hooks are supported:
68

7-
- `@resurrect-save-hook` - executed after session save
8-
- `@resurrect-restore-hook` - executed before session restore
9+
- `@resurrect-hook-post-save-layout`
10+
11+
Called after all sessions, panes and windows have been saved.
12+
13+
Passed single argument of the state file.
14+
15+
- `@resurrect-hook-post-save-all`
16+
17+
Called at end of save process right before the spinner is turned off.
18+
19+
- `@resurrect-hook-pre-restore-all`
20+
21+
Called before any tmux state is altered.
22+
23+
- `@resurrect-hook-pre-restore-history`
24+
25+
Called after panes and layout have been restores, but before bash history is
26+
restored (if it is enabled) -- the hook is always called even if history
27+
saving is disabled.
28+
29+
- `@resurrect-hook-pre-restore-pane-processes`
30+
31+
Called after history is restored, but before running processes are restored.
32+
33+
### Examples
934

1035
Here is an example how to save and restore window geometry for most terminals in X11.
1136
Add this to `.tmux.conf`:
1237

13-
set -g @resurrect-save-hook 'eval $(xdotool getwindowgeometry --shell $WINDOWID); echo 0,$X,$Y,$WIDTH,$HEIGHT > $HOME/.tmux/resurrect/geometry'
14-
set -g @resurrect-restore-hook 'wmctrl -i -r $WINDOWID -e $(cat $HOME/.tmux/resurrect/geometry)'
38+
set -g @resurrect-hook-post-save-all 'eval $(xdotool getwindowgeometry --shell $WINDOWID); echo 0,$X,$Y,$WIDTH,$HEIGHT > $HOME/.tmux/resurrect/geometry'
39+
set -g @resurrect-hook-pre-restore-all 'wmctrl -i -r $WINDOWID -e $(cat $HOME/.tmux/resurrect/geometry)'

scripts/helpers.sh

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,20 @@ resurrect_history_file() {
149149
echo "$(resurrect_dir)/${shell_name}_history-${pane_id}"
150150
}
151151

152-
# hook helpers
152+
execute_hook() {
153+
local kind="$1"
154+
shift
155+
local args="" hook=""
153156

154-
save_hook() {
155-
get_tmux_option "$save_hook_option" "$save_hook_default"
156-
}
157+
hook=$(get_tmux_option "$hook_prefix$kind" "")
157158

158-
restore_hook() {
159-
get_tmux_option "$restore_hook_option" "$restore_hook_default"
160-
}
159+
# If there are any args, pass them to the hook (in a way that preserves/copes
160+
# with spaces and unusual characters.
161+
if [ "$#" -gt 0 ]; then
162+
printf -v args "%q " "$@"
163+
fi
161164

165+
if [ -n "$hook" ]; then
166+
eval "$hook $args"
167+
fi
168+
}

scripts/restore.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,22 @@ restore_active_and_alternate_sessions() {
344344
main() {
345345
if supported_tmux_version_ok && check_saved_session_exists; then
346346
start_spinner "Restoring..." "Tmux restore complete!"
347-
if [ -n "$(restore_hook)" ]; then
348-
eval "$(restore_hook)"
349-
fi
347+
execute_hook "pre-restore-all"
350348
restore_all_panes
351349
restore_pane_layout_for_each_window >/dev/null 2>&1
350+
execute_hook "pre-restore-history"
352351
if save_shell_history_option_on; then
353352
restore_shell_history
354353
fi
354+
execute_hook "pre-restore-pane-processes"
355355
restore_all_pane_processes
356356
# below functions restore exact cursor positions
357357
restore_active_pane_for_each_window
358358
restore_zoomed_windows
359359
restore_grouped_sessions # also restores active and alt windows for grouped sessions
360360
restore_active_and_alternate_windows
361361
restore_active_and_alternate_sessions
362+
execute_hook "post-restore-all"
362363
stop_spinner
363364
display_message "Tmux restore complete!"
364365
fi

scripts/save.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ save_all() {
283283
dump_panes >> "$resurrect_file_path"
284284
dump_windows >> "$resurrect_file_path"
285285
dump_state >> "$resurrect_file_path"
286+
execute_hook "post-save-layout" "$resurrect_file_path"
286287
if files_differ "$resurrect_file_path" "$last_resurrect_file"; then
287288
ln -fs "$(basename "$resurrect_file_path")" "$last_resurrect_file"
288289
else
@@ -298,6 +299,7 @@ save_all() {
298299
dump_shell_history
299300
fi
300301
remove_old_backups
302+
execute_hook "post-save-all"
301303
}
302304

303305
show_output() {
@@ -314,9 +316,6 @@ main() {
314316
stop_spinner
315317
display_message "Tmux environment saved!"
316318
fi
317-
if [ -n "$(save_hook)" ]; then
318-
eval "$(save_hook)"
319-
fi
320319
fi
321320
}
322321
main

scripts/variables.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,5 @@ shell_history_option="@resurrect-save-shell-history"
4343
# set to 'on' to ensure panes are never ever overwritten
4444
overwrite_option="@resurrect-never-overwrite"
4545

46-
# Hooks
47-
restore_hook_default=""
48-
restore_hook_option="@resurrect-restore-hook"
49-
save_hook_default=""
50-
save_hook_option="@resurrect-save-hook"
46+
# Hooks are set via ${hook_prefix}${name}, i.e. "@resurrect-hook-post-save-all"
47+
hook_prefix="@resurrect-hook-"

0 commit comments

Comments
 (0)