Skip to content

Commit 6feae65

Browse files
authoredAug 20, 2018
Merge pull request #267 from ashb/save-restore-hooks
Further Save and restore hooks
2 parents 42f77b3 + 8aa999c commit 6feae65

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed
 

‎README.md

+1
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

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Save & Restore Hooks
2+
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.
6+
7+
Currently the following hooks are supported:
8+
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
34+
35+
Here is an example how to save and restore window geometry for most terminals in X11.
36+
Add this to `.tmux.conf`:
37+
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

+18
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,21 @@ resurrect_history_file() {
148148
local shell_name="$2"
149149
echo "$(resurrect_dir)/${shell_name}_history-${pane_id}"
150150
}
151+
152+
execute_hook() {
153+
local kind="$1"
154+
shift
155+
local args="" hook=""
156+
157+
hook=$(get_tmux_option "$hook_prefix$kind" "")
158+
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
164+
165+
if [ -n "$hook" ]; then
166+
eval "$hook $args"
167+
fi
168+
}

‎scripts/restore.sh

+4
Original file line numberDiff line numberDiff line change
@@ -344,18 +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+
execute_hook "pre-restore-all"
347348
restore_all_panes
348349
restore_pane_layout_for_each_window >/dev/null 2>&1
350+
execute_hook "pre-restore-history"
349351
if save_shell_history_option_on; then
350352
restore_shell_history
351353
fi
354+
execute_hook "pre-restore-pane-processes"
352355
restore_all_pane_processes
353356
# below functions restore exact cursor positions
354357
restore_active_pane_for_each_window
355358
restore_zoomed_windows
356359
restore_grouped_sessions # also restores active and alt windows for grouped sessions
357360
restore_active_and_alternate_windows
358361
restore_active_and_alternate_sessions
362+
execute_hook "post-restore-all"
359363
stop_spinner
360364
display_message "Tmux restore complete!"
361365
fi

‎scripts/save.sh

+2
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() {

‎scripts/variables.sh

+3
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ 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 are set via ${hook_prefix}${name}, i.e. "@resurrect-hook-post-save-all"
47+
hook_prefix="@resurrect-hook-"

0 commit comments

Comments
 (0)
Please sign in to comment.