Skip to content

Commit 32a30cd

Browse files
committed
Save/restore also linked windows
1 parent cff343c commit 32a30cd

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

scripts/helpers.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ is_session_grouped() {
7878
[[ "$GROUPED_SESSIONS" == *"${d}${session_name}${d}"* ]]
7979
}
8080

81+
get_linked_windows() {
82+
local linked_windows_dump="$1"
83+
export LINKED_WINDOWS="${d}$(echo "$linked_windows_dump" | cut -f2-3 -d"$d" | tr "$d\\n" ":$d")"
84+
}
85+
86+
is_window_linked() {
87+
local session_name="$1"
88+
local window_index="$2"
89+
[[ "$LINKED_WINDOWS" = *"${d}${session_name}:${window_index}${d}"* ]]
90+
}
91+
8192
# pane content file helpers
8293

8394
pane_contents_create_archive() {

scripts/restore.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ handle_session_0() {
284284
fi
285285
}
286286

287+
restore_linked_windows() {
288+
\grep '^linked_window' $(last_resurrect_file) |
289+
while IFS=$d read line_type session window_index original_session original_window_index; do
290+
tmux link-window -s "${original_session}:${original_window_index}" -t "${session}:${window_index}"
291+
done
292+
}
293+
287294
restore_window_properties() {
288295
local window_name
289296
\grep '^window' $(last_resurrect_file) |
@@ -369,6 +376,7 @@ main() {
369376
execute_hook "pre-restore-all"
370377
restore_all_panes
371378
handle_session_0
379+
restore_linked_windows
372380
restore_window_properties >/dev/null 2>&1
373381
execute_hook "pre-restore-pane-processes"
374382
restore_all_pane_processes

scripts/save.sh

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ grouped_sessions_format() {
2525
echo "$format"
2626
}
2727

28+
linked_windows_format() {
29+
local format
30+
format+="#{window_linked}"
31+
format+="${delimiter}"
32+
format+="#{window_id}"
33+
format+="${delimiter}"
34+
format+="#{session_name}"
35+
format+="${delimiter}"
36+
format+="#{window_index}"
37+
echo "$format"
38+
}
39+
2840
pane_format() {
2941
local format
3042
format+="pane"
@@ -185,13 +197,45 @@ fetch_and_dump_grouped_sessions(){
185197
fi
186198
}
187199

200+
dump_linked_windows() {
201+
local current_window_id=""
202+
local original_session
203+
local original_window_index
204+
tmux list-windows -a -F "$(linked_windows_format)" |
205+
grep "^1" |
206+
cut -c 3- |
207+
sort |
208+
while IFS=$d read window_id session_name window_index; do
209+
if is_session_grouped "$session_name"; then
210+
continue
211+
fi
212+
if [ "$window_id" != "$current_window_id" ]; then
213+
# this window is the original/first of linked windows
214+
original_session="$session_name"
215+
original_window_index="$window_index"
216+
current_window_id="$window_id"
217+
else
218+
# this window is linked to the original window
219+
echo "linked_window${d}${session_name}${d}${window_index}${d}${original_session}${d}${original_window_index}"
220+
fi
221+
done
222+
}
223+
224+
fetch_and_dump_linked_windows() {
225+
local linked_windows_dump="$(dump_linked_windows)"
226+
get_linked_windows "$linked_windows_dump"
227+
if [ -n "$linked_windows_dump" ]; then
228+
echo "$linked_windows_dump"
229+
fi
230+
}
231+
188232
# translates pane pid to process command running inside a pane
189233
dump_panes() {
190234
local full_command
191235
dump_panes_raw |
192236
while IFS=$d read line_type session_name window_number window_active window_flags pane_index pane_title dir pane_active pane_command pane_pid history_size; do
193237
# not saving panes from grouped sessions
194-
if is_session_grouped "$session_name"; then
238+
if is_session_grouped "$session_name" || is_window_linked "$session_name" "$window_number"; then
195239
continue
196240
fi
197241
full_command="$(pane_full_command $pane_pid)"
@@ -240,6 +284,7 @@ save_all() {
240284
local last_resurrect_file="$(last_resurrect_file)"
241285
mkdir -p "$(resurrect_dir)"
242286
fetch_and_dump_grouped_sessions > "$resurrect_file_path"
287+
fetch_and_dump_linked_windows >> "$resurrect_file_path"
243288
dump_panes >> "$resurrect_file_path"
244289
dump_windows >> "$resurrect_file_path"
245290
dump_state >> "$resurrect_file_path"

0 commit comments

Comments
 (0)