Skip to content

Commit 0133c7a

Browse files
noscriptashb
authored andcommitted
Save and restore hooks
1 parent 42f77b3 commit 0133c7a

File tree

6 files changed

+38
-0
lines changed

6 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ You should now be able to use the plugin.
9090
**Configuration**
9191

9292
- [Changing the default key bindings](docs/custom_key_bindings.md).
93+
- [Setting up hooks on save & restore](docs/hooks.md).
9394
- Only a conservative list of programs is restored by default:<br/>
9495
`vi vim nvim emacs man less more tail top htop irssi weechat mutt`.<br/>
9596
[Restoring programs doc](docs/restoring_programs.md) explains how to restore

docs/hooks.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Save & Restore Hooks
2+
3+
Hooks allow to set custom commands that will be executed during session save and restore.
4+
5+
Currently the following hooks are supported:
6+
7+
- `@resurrect-save-hook` - executed after session save
8+
- `@resurrect-restore-hook` - executed before session restore
9+
10+
Here is an example how to save and restore window geometry for most terminals in X11.
11+
Add this to `.tmux.conf`:
12+
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)'

scripts/helpers.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,14 @@ resurrect_history_file() {
148148
local shell_name="$2"
149149
echo "$(resurrect_dir)/${shell_name}_history-${pane_id}"
150150
}
151+
152+
# hook helpers
153+
154+
save_hook() {
155+
get_tmux_option "$save_hook_option" "$save_hook_default"
156+
}
157+
158+
restore_hook() {
159+
get_tmux_option "$restore_hook_option" "$restore_hook_default"
160+
}
161+

scripts/restore.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ 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
347350
restore_all_panes
348351
restore_pane_layout_for_each_window >/dev/null 2>&1
349352
if save_shell_history_option_on; then

scripts/save.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ main() {
314314
stop_spinner
315315
display_message "Tmux environment saved!"
316316
fi
317+
if [ -n "$(save_hook)" ]; then
318+
eval "$(save_hook)"
319+
fi
317320
fi
318321
}
319322
main

scripts/variables.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ shell_history_option="@resurrect-save-shell-history"
4242

4343
# set to 'on' to ensure panes are never ever overwritten
4444
overwrite_option="@resurrect-never-overwrite"
45+
46+
# Hooks
47+
restore_hook_default=""
48+
restore_hook_option="@resurrect-restore-hook"
49+
save_hook_default=""
50+
save_hook_option="@resurrect-save-hook"

0 commit comments

Comments
 (0)