mirror of
https://github.com/tmux-plugins/tmux-resurrect.git
synced 2024-11-22 04:18:48 +00:00
Merge pull request #267 from ashb/save-restore-hooks
Further Save and restore hooks
This commit is contained in:
commit
6feae65a83
@ -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
|
||||||
|
39
docs/hooks.md
Normal file
39
docs/hooks.md
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
# Save & Restore Hooks
|
||||||
|
|
||||||
|
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-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-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)'
|
@ -148,3 +148,21 @@ 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}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
execute_hook() {
|
||||||
|
local kind="$1"
|
||||||
|
shift
|
||||||
|
local args="" hook=""
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
@ -344,11 +344,14 @@ 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!"
|
||||||
|
execute_hook "pre-restore-all"
|
||||||
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
|
||||||
|
execute_hook "pre-restore-history"
|
||||||
if save_shell_history_option_on; then
|
if save_shell_history_option_on; then
|
||||||
restore_shell_history
|
restore_shell_history
|
||||||
fi
|
fi
|
||||||
|
execute_hook "pre-restore-pane-processes"
|
||||||
restore_all_pane_processes
|
restore_all_pane_processes
|
||||||
# below functions restore exact cursor positions
|
# below functions restore exact cursor positions
|
||||||
restore_active_pane_for_each_window
|
restore_active_pane_for_each_window
|
||||||
@ -356,6 +359,7 @@ main() {
|
|||||||
restore_grouped_sessions # also restores active and alt windows for grouped sessions
|
restore_grouped_sessions # also restores active and alt windows for grouped sessions
|
||||||
restore_active_and_alternate_windows
|
restore_active_and_alternate_windows
|
||||||
restore_active_and_alternate_sessions
|
restore_active_and_alternate_sessions
|
||||||
|
execute_hook "post-restore-all"
|
||||||
stop_spinner
|
stop_spinner
|
||||||
display_message "Tmux restore complete!"
|
display_message "Tmux restore complete!"
|
||||||
fi
|
fi
|
||||||
|
@ -283,6 +283,7 @@ save_all() {
|
|||||||
dump_panes >> "$resurrect_file_path"
|
dump_panes >> "$resurrect_file_path"
|
||||||
dump_windows >> "$resurrect_file_path"
|
dump_windows >> "$resurrect_file_path"
|
||||||
dump_state >> "$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
|
if files_differ "$resurrect_file_path" "$last_resurrect_file"; then
|
||||||
ln -fs "$(basename "$resurrect_file_path")" "$last_resurrect_file"
|
ln -fs "$(basename "$resurrect_file_path")" "$last_resurrect_file"
|
||||||
else
|
else
|
||||||
@ -298,6 +299,7 @@ save_all() {
|
|||||||
dump_shell_history
|
dump_shell_history
|
||||||
fi
|
fi
|
||||||
remove_old_backups
|
remove_old_backups
|
||||||
|
execute_hook "post-save-all"
|
||||||
}
|
}
|
||||||
|
|
||||||
show_output() {
|
show_output() {
|
||||||
|
@ -42,3 +42,6 @@ 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 are set via ${hook_prefix}${name}, i.e. "@resurrect-hook-post-save-all"
|
||||||
|
hook_prefix="@resurrect-hook-"
|
||||||
|
Loading…
Reference in New Issue
Block a user