Save and restore hooks

This commit is contained in:
Sergey Vlasov 2018-01-19 18:36:13 +02:00 committed by Ash Berlin-Taylor
parent 42f77b303c
commit 0133c7a96a
6 changed files with 38 additions and 0 deletions

View File

@ -90,6 +90,7 @@ You should now be able to use the plugin.
**Configuration** **Configuration**
- [Changing the default key bindings](docs/custom_key_bindings.md). - [Changing the default key bindings](docs/custom_key_bindings.md).
- [Setting up hooks on save & restore](docs/hooks.md).
- Only a conservative list of programs is restored by default:<br/> - Only a conservative list of programs is restored by default:<br/>
`vi vim nvim emacs man less more tail top htop irssi weechat mutt`.<br/> `vi vim nvim emacs man less more tail top htop irssi weechat mutt`.<br/>
[Restoring programs doc](docs/restoring_programs.md) explains how to restore [Restoring programs doc](docs/restoring_programs.md) explains how to restore

14
docs/hooks.md Normal file
View File

@ -0,0 +1,14 @@
# Save & Restore Hooks
Hooks allow to set custom commands that will be executed during session save and restore.
Currently the following hooks are supported:
- `@resurrect-save-hook` - executed after session save
- `@resurrect-restore-hook` - executed before session restore
Here is an example how to save and restore window geometry for most terminals in X11.
Add this to `.tmux.conf`:
set -g @resurrect-save-hook 'eval $(xdotool getwindowgeometry --shell $WINDOWID); echo 0,$X,$Y,$WIDTH,$HEIGHT > $HOME/.tmux/resurrect/geometry'
set -g @resurrect-restore-hook 'wmctrl -i -r $WINDOWID -e $(cat $HOME/.tmux/resurrect/geometry)'

View File

@ -148,3 +148,14 @@ resurrect_history_file() {
local shell_name="$2" local shell_name="$2"
echo "$(resurrect_dir)/${shell_name}_history-${pane_id}" echo "$(resurrect_dir)/${shell_name}_history-${pane_id}"
} }
# hook helpers
save_hook() {
get_tmux_option "$save_hook_option" "$save_hook_default"
}
restore_hook() {
get_tmux_option "$restore_hook_option" "$restore_hook_default"
}

View File

@ -344,6 +344,9 @@ restore_active_and_alternate_sessions() {
main() { main() {
if supported_tmux_version_ok && check_saved_session_exists; then if supported_tmux_version_ok && check_saved_session_exists; then
start_spinner "Restoring..." "Tmux restore complete!" start_spinner "Restoring..." "Tmux restore complete!"
if [ -n "$(restore_hook)" ]; then
eval "$(restore_hook)"
fi
restore_all_panes restore_all_panes
restore_pane_layout_for_each_window >/dev/null 2>&1 restore_pane_layout_for_each_window >/dev/null 2>&1
if save_shell_history_option_on; then if save_shell_history_option_on; then

View File

@ -314,6 +314,9 @@ main() {
stop_spinner stop_spinner
display_message "Tmux environment saved!" display_message "Tmux environment saved!"
fi fi
if [ -n "$(save_hook)" ]; then
eval "$(save_hook)"
fi
fi fi
} }
main main

View File

@ -42,3 +42,9 @@ shell_history_option="@resurrect-save-shell-history"
# set to 'on' to ensure panes are never ever overwritten # set to 'on' to ensure panes are never ever overwritten
overwrite_option="@resurrect-never-overwrite" overwrite_option="@resurrect-never-overwrite"
# Hooks
restore_hook_default=""
restore_hook_option="@resurrect-restore-hook"
save_hook_default=""
save_hook_option="@resurrect-save-hook"