From 0133c7a96a7570c1295c3ac1816fdd1d545384a9 Mon Sep 17 00:00:00 2001 From: Sergey Vlasov Date: Fri, 19 Jan 2018 18:36:13 +0200 Subject: [PATCH 1/2] Save and restore hooks --- README.md | 1 + docs/hooks.md | 14 ++++++++++++++ scripts/helpers.sh | 11 +++++++++++ scripts/restore.sh | 3 +++ scripts/save.sh | 3 +++ scripts/variables.sh | 6 ++++++ 6 files changed, 38 insertions(+) create mode 100644 docs/hooks.md diff --git a/README.md b/README.md index 63042f5..6564135 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ You should now be able to use the plugin. **Configuration** - [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:
`vi vim nvim emacs man less more tail top htop irssi weechat mutt`.
[Restoring programs doc](docs/restoring_programs.md) explains how to restore diff --git a/docs/hooks.md b/docs/hooks.md new file mode 100644 index 0000000..014039e --- /dev/null +++ b/docs/hooks.md @@ -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)' diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 41fc7ce..5f57ed8 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -148,3 +148,14 @@ resurrect_history_file() { local shell_name="$2" 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" +} + diff --git a/scripts/restore.sh b/scripts/restore.sh index 9fd03a9..e965484 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -344,6 +344,9 @@ restore_active_and_alternate_sessions() { main() { if supported_tmux_version_ok && check_saved_session_exists; then start_spinner "Restoring..." "Tmux restore complete!" + if [ -n "$(restore_hook)" ]; then + eval "$(restore_hook)" + fi restore_all_panes restore_pane_layout_for_each_window >/dev/null 2>&1 if save_shell_history_option_on; then diff --git a/scripts/save.sh b/scripts/save.sh index f7508e8..c1c3f62 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -314,6 +314,9 @@ main() { stop_spinner display_message "Tmux environment saved!" fi + if [ -n "$(save_hook)" ]; then + eval "$(save_hook)" + fi fi } main diff --git a/scripts/variables.sh b/scripts/variables.sh index 9cfda0c..0680076 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -42,3 +42,9 @@ shell_history_option="@resurrect-save-shell-history" # set to 'on' to ensure panes are never ever overwritten overwrite_option="@resurrect-never-overwrite" + +# Hooks +restore_hook_default="" +restore_hook_option="@resurrect-restore-hook" +save_hook_default="" +save_hook_option="@resurrect-save-hook" From 8aa999c591a49460c4e1a854682f1efcfaf70792 Mon Sep 17 00:00:00 2001 From: Ash Berlin-Taylor Date: Fri, 17 Aug 2018 20:38:33 +0100 Subject: [PATCH 2/2] Add more hook points And make the hook calling simpler at the call site. --- docs/hooks.md | 35 ++++++++++++++++++++++++++++++----- scripts/helpers.sh | 23 +++++++++++++++-------- scripts/restore.sh | 7 ++++--- scripts/save.sh | 5 ++--- scripts/variables.sh | 7 ++----- 5 files changed, 53 insertions(+), 24 deletions(-) diff --git a/docs/hooks.md b/docs/hooks.md index 014039e..cb4e7a3 100644 --- a/docs/hooks.md +++ b/docs/hooks.md @@ -1,14 +1,39 @@ # Save & Restore Hooks -Hooks allow to set custom commands that will be executed during session save and restore. +Hooks allow to set custom commands that will be executed during session save +and restore. Most hooks are called with zero arguments, unless explicitly +stated otherwise. Currently the following hooks are supported: -- `@resurrect-save-hook` - executed after session save -- `@resurrect-restore-hook` - executed before session restore +- `@resurrect-hook-post-save-layout` + + Called after all sessions, panes and windows have been saved. + + Passed single argument of the state file. + +- `@resurrect-hook-post-save-all` + + Called at end of save process right before the spinner is turned off. + +- `@resurrect-hook-pre-restore-all` + + Called before any tmux state is altered. + +- `@resurrect-hook-pre-restore-history` + + Called after panes and layout have been restores, but before bash history is + restored (if it is enabled) -- the hook is always called even if history + saving is disabled. + +- `@resurrect-hook-pre-restore-pane-processes` + + Called after history is restored, but before running processes are restored. + +### Examples 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)' + set -g @resurrect-hook-post-save-all 'eval $(xdotool getwindowgeometry --shell $WINDOWID); echo 0,$X,$Y,$WIDTH,$HEIGHT > $HOME/.tmux/resurrect/geometry' + set -g @resurrect-hook-pre-restore-all 'wmctrl -i -r $WINDOWID -e $(cat $HOME/.tmux/resurrect/geometry)' diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 5f57ed8..8c2d6a7 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -149,13 +149,20 @@ resurrect_history_file() { echo "$(resurrect_dir)/${shell_name}_history-${pane_id}" } -# hook helpers +execute_hook() { + local kind="$1" + shift + local args="" hook="" -save_hook() { - get_tmux_option "$save_hook_option" "$save_hook_default" + hook=$(get_tmux_option "$hook_prefix$kind" "") + + # If there are any args, pass them to the hook (in a way that preserves/copes + # with spaces and unusual characters. + if [ "$#" -gt 0 ]; then + printf -v args "%q " "$@" + fi + + if [ -n "$hook" ]; then + eval "$hook $args" + fi } - -restore_hook() { - get_tmux_option "$restore_hook_option" "$restore_hook_default" -} - diff --git a/scripts/restore.sh b/scripts/restore.sh index e965484..6fdfc2f 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -344,14 +344,14 @@ restore_active_and_alternate_sessions() { main() { if supported_tmux_version_ok && check_saved_session_exists; then start_spinner "Restoring..." "Tmux restore complete!" - if [ -n "$(restore_hook)" ]; then - eval "$(restore_hook)" - fi + execute_hook "pre-restore-all" restore_all_panes restore_pane_layout_for_each_window >/dev/null 2>&1 + execute_hook "pre-restore-history" if save_shell_history_option_on; then restore_shell_history fi + execute_hook "pre-restore-pane-processes" restore_all_pane_processes # below functions restore exact cursor positions restore_active_pane_for_each_window @@ -359,6 +359,7 @@ main() { restore_grouped_sessions # also restores active and alt windows for grouped sessions restore_active_and_alternate_windows restore_active_and_alternate_sessions + execute_hook "post-restore-all" stop_spinner display_message "Tmux restore complete!" fi diff --git a/scripts/save.sh b/scripts/save.sh index c1c3f62..ae1141d 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -283,6 +283,7 @@ save_all() { dump_panes >> "$resurrect_file_path" dump_windows >> "$resurrect_file_path" dump_state >> "$resurrect_file_path" + execute_hook "post-save-layout" "$resurrect_file_path" if files_differ "$resurrect_file_path" "$last_resurrect_file"; then ln -fs "$(basename "$resurrect_file_path")" "$last_resurrect_file" else @@ -298,6 +299,7 @@ save_all() { dump_shell_history fi remove_old_backups + execute_hook "post-save-all" } show_output() { @@ -314,9 +316,6 @@ main() { stop_spinner display_message "Tmux environment saved!" fi - if [ -n "$(save_hook)" ]; then - eval "$(save_hook)" - fi fi } main diff --git a/scripts/variables.sh b/scripts/variables.sh index 0680076..9436957 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -43,8 +43,5 @@ shell_history_option="@resurrect-save-shell-history" # set to 'on' to ensure panes are never ever overwritten overwrite_option="@resurrect-never-overwrite" -# Hooks -restore_hook_default="" -restore_hook_option="@resurrect-restore-hook" -save_hook_default="" -save_hook_option="@resurrect-save-hook" +# Hooks are set via ${hook_prefix}${name}, i.e. "@resurrect-hook-post-save-all" +hook_prefix="@resurrect-hook-"