From 80adb917c1128b9dd701e8f542632dea23b39838 Mon Sep 17 00:00:00 2001 From: A Farzat Date: Mon, 23 Aug 2021 13:55:19 +0900 Subject: [PATCH 1/5] Maintain the value of automatic-rename When the session is restored, the windows are renamed to their original names switching off automatic-rename, which can be undesirable. Therefore the value of automatic-rename is now saved for each window and restored after the renaming. If the value is set, that value is saved and then applied. Otherwise, a placeholder of ':' is placed instead, in which case the local option is unset for that window (as it originally was). --- scripts/restore.sh | 11 ++++++++--- scripts/save.sh | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/scripts/restore.sh b/scripts/restore.sh index 373a210..9b95527 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -292,10 +292,15 @@ handle_session_0() { fi } -restore_pane_layout_for_each_window() { +restore_pane_layout_and_autonaming_for_each_window() { \grep '^window' $(last_resurrect_file) | - while IFS=$d read line_type session_name window_number window_active window_flags window_layout; do + while IFS=$d read line_type session_name window_number window_active window_flags window_layout automatic_rename; do tmux select-layout -t "${session_name}:${window_number}" "$window_layout" + if [ "${automatic_rename}" = ":" ]; then + tmux set-option -u -t "${session_name}:${window_number}" automatic-rename + else + tmux set-option -t "${session_name}:${window_number}" automatic-rename "$automatic_rename" + fi done } @@ -376,7 +381,7 @@ main() { execute_hook "pre-restore-all" restore_all_panes handle_session_0 - restore_pane_layout_for_each_window >/dev/null 2>&1 + restore_pane_layout_and_autonaming_for_each_window >/dev/null 2>&1 execute_hook "pre-restore-history" if save_shell_history_option_on; then restore_shell_history diff --git a/scripts/save.sh b/scripts/save.sh index 08647a7..85b9765 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -245,7 +245,10 @@ dump_windows() { if is_session_grouped "$session_name"; then continue fi - echo "${line_type}${d}${session_name}${d}${window_index}${d}${window_active}${d}${window_flags}${d}${window_layout}" + automatic_rename="$(tmux show-window-options -vt "${session_name}:${window_index}" automatic-rename)" + # If the option was unset, place the ":" placeholder instead. + [ -z "${automatic_rename}" ] && automatic_rename=":" + echo "${line_type}${d}${session_name}${d}${window_index}${d}${window_active}${d}${window_flags}${d}${window_layout}${d}${automatic_rename}" done } From 6c9322aa9955f3407e1e472afdc8f8cdd370caec Mon Sep 17 00:00:00 2001 From: A Farzat Date: Thu, 26 Aug 2021 07:00:49 +0900 Subject: [PATCH 2/5] Leave window name handling to windows Previously, window names were set when creating panes and were therefore saved with pane data. However, saving the names with window data is more intuitive and easier to manage. In addition, one can set the name and automatic-rename options in the same function, so one can make sure that renaming the windows will not overwrite the automatic-rename option. --- scripts/restore.sh | 45 ++++++++++++++++++++------------------------- scripts/save.sh | 12 ++++++------ 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/scripts/restore.sh b/scripts/restore.sh index 9b95527..cb08b35 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -126,31 +126,29 @@ pane_creation_command() { new_window() { local session_name="$1" local window_number="$2" - local window_name="$3" - local dir="$4" - local pane_index="$5" + local dir="$3" + local pane_index="$4" local pane_id="${session_name}:${window_number}.${pane_index}" dir="${dir/#\~/$HOME}" if is_restoring_pane_contents && pane_contents_file_exists "$pane_id"; then local pane_creation_command="$(pane_creation_command "$session_name" "$window_number" "$pane_index")" - tmux new-window -d -t "${session_name}:${window_number}" -n "$window_name" -c "$dir" "$pane_creation_command" + tmux new-window -d -t "${session_name}:${window_number}" -c "$dir" "$pane_creation_command" else - tmux new-window -d -t "${session_name}:${window_number}" -n "$window_name" -c "$dir" + tmux new-window -d -t "${session_name}:${window_number}" -c "$dir" fi } new_session() { local session_name="$1" local window_number="$2" - local window_name="$3" - local dir="$4" - local pane_index="$5" + local dir="$3" + local pane_index="$4" local pane_id="${session_name}:${window_number}.${pane_index}" if is_restoring_pane_contents && pane_contents_file_exists "$pane_id"; then local pane_creation_command="$(pane_creation_command "$session_name" "$window_number" "$pane_index")" - TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -n "$window_name" -c "$dir" "$pane_creation_command" + TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -c "$dir" "$pane_creation_command" else - TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -n "$window_name" -c "$dir" + TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -c "$dir" fi # change first window number if necessary local created_window_num="$(first_window_num)" @@ -162,9 +160,8 @@ new_session() { new_pane() { local session_name="$1" local window_number="$2" - local window_name="$3" - local dir="$4" - local pane_index="$5" + local dir="$3" + local pane_index="$4" local pane_id="${session_name}:${window_number}.${pane_index}" if is_restoring_pane_contents && pane_contents_file_exists "$pane_id"; then local pane_creation_command="$(pane_creation_command "$session_name" "$window_number" "$pane_index")" @@ -172,27 +169,24 @@ new_pane() { else tmux split-window -t "${session_name}:${window_number}" -c "$dir" fi - tmux rename-window -t "${session_name}:${window_number}" "$window_name" # minimize window so more panes can fit tmux resize-pane -t "${session_name}:${window_number}" -U "999" } restore_pane() { local pane="$1" - while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_full_command; do + while IFS=$d read line_type session_name window_number window_active window_flags pane_index dir pane_active pane_command pane_full_command; do dir="$(remove_first_char "$dir")" - window_name="$(remove_first_char "$window_name")" pane_full_command="$(remove_first_char "$pane_full_command")" if [ "$session_name" == "0" ]; then restored_session_0_true fi if pane_exists "$session_name" "$window_number" "$pane_index"; then - tmux rename-window -t "${session_name}:${window_number}" "$window_name" if is_restoring_from_scratch; then # overwrite the pane # happens only for the first pane if it's the only registered pane for the whole tmux server local pane_id="$(tmux display-message -p -F "#{pane_id}" -t "$session_name:$window_number")" - new_pane "$session_name" "$window_number" "$window_name" "$dir" "$pane_index" + new_pane "$session_name" "$window_number" "$dir" "$pane_index" tmux kill-pane -t "$pane_id" else # Pane exists, no need to create it! @@ -200,12 +194,11 @@ restore_pane() { register_existing_pane "$session_name" "$window_number" "$pane_index" fi elif window_exists "$session_name" "$window_number"; then - tmux rename-window -t "${session_name}:${window_number}" "$window_name" - new_pane "$session_name" "$window_number" "$window_name" "$dir" "$pane_index" + new_pane "$session_name" "$window_number" "$dir" "$pane_index" elif session_exists "$session_name"; then - new_window "$session_name" "$window_number" "$window_name" "$dir" "$pane_index" + new_window "$session_name" "$window_number" "$dir" "$pane_index" else - new_session "$session_name" "$window_number" "$window_name" "$dir" "$pane_index" + new_session "$session_name" "$window_number" "$dir" "$pane_index" fi done < <(echo "$pane") } @@ -292,9 +285,11 @@ handle_session_0() { fi } -restore_pane_layout_and_autonaming_for_each_window() { +restore_window_properties() { \grep '^window' $(last_resurrect_file) | - while IFS=$d read line_type session_name window_number window_active window_flags window_layout automatic_rename; do + while IFS=$d read line_type session_name window_number window_name window_active window_flags window_layout automatic_rename; do + window_name="$(remove_first_char "$window_name")" + tmux rename-window -t "${session_name}:${window_number}" "$window_name" tmux select-layout -t "${session_name}:${window_number}" "$window_layout" if [ "${automatic_rename}" = ":" ]; then tmux set-option -u -t "${session_name}:${window_number}" automatic-rename @@ -381,7 +376,7 @@ main() { execute_hook "pre-restore-all" restore_all_panes handle_session_0 - restore_pane_layout_and_autonaming_for_each_window >/dev/null 2>&1 + restore_window_properties >/dev/null 2>&1 execute_hook "pre-restore-history" if save_shell_history_option_on; then restore_shell_history diff --git a/scripts/save.sh b/scripts/save.sh index 85b9765..cee984b 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -33,8 +33,6 @@ pane_format() { format+="${delimiter}" format+="#{window_index}" format+="${delimiter}" - format+=":#{window_name}" - format+="${delimiter}" format+="#{window_active}" format+="${delimiter}" format+=":#{window_flags}" @@ -61,6 +59,8 @@ window_format() { format+="${delimiter}" format+="#{window_index}" format+="${delimiter}" + format+="#{window_name}" + format+="${delimiter}" format+="#{window_active}" format+="${delimiter}" format+=":#{window_flags}" @@ -227,20 +227,20 @@ fetch_and_dump_grouped_sessions(){ dump_panes() { local full_command dump_panes_raw | - while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_pid history_size; do + while IFS=$d read line_type session_name window_number window_active window_flags pane_index dir pane_active pane_command pane_pid history_size; do # not saving panes from grouped sessions if is_session_grouped "$session_name"; then continue fi full_command="$(pane_full_command $pane_pid)" dir=$(echo $dir | sed 's/ /\\ /') # escape all spaces in directory path - echo "${line_type}${d}${session_name}${d}${window_number}${d}${window_name}${d}${window_active}${d}${window_flags}${d}${pane_index}${d}${dir}${d}${pane_active}${d}${pane_command}${d}:${full_command}" + echo "${line_type}${d}${session_name}${d}${window_number}${d}${window_active}${d}${window_flags}${d}${pane_index}${d}${dir}${d}${pane_active}${d}${pane_command}${d}:${full_command}" done } dump_windows() { dump_windows_raw | - while IFS=$d read line_type session_name window_index window_active window_flags window_layout; do + while IFS=$d read line_type session_name window_index window_name window_active window_flags window_layout; do # not saving windows from grouped sessions if is_session_grouped "$session_name"; then continue @@ -248,7 +248,7 @@ dump_windows() { automatic_rename="$(tmux show-window-options -vt "${session_name}:${window_index}" automatic-rename)" # If the option was unset, place the ":" placeholder instead. [ -z "${automatic_rename}" ] && automatic_rename=":" - echo "${line_type}${d}${session_name}${d}${window_index}${d}${window_active}${d}${window_flags}${d}${window_layout}${d}${automatic_rename}" + echo "${line_type}${d}${session_name}${d}${window_index}${d}${window_name}${d}${window_active}${d}${window_flags}${d}${window_layout}${d}${automatic_rename}" done } From 02a7f1f9d644c04a167f027360bf619103b44b0d Mon Sep 17 00:00:00 2001 From: A Farzat Date: Fri, 27 Aug 2021 13:27:00 +0900 Subject: [PATCH 3/5] Fix some of the errors in the last commit First, make sure to include the ":" placeholder in the window_name format. Second, decrement the indices in relevant awk commands to make sure they point to the right items. --- scripts/restore.sh | 8 ++++---- scripts/save.sh | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/restore.sh b/scripts/restore.sh index cb08b35..fb7547e 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -300,7 +300,7 @@ restore_window_properties() { } restore_shell_history() { - awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ { print $2, $3, $7, $10; }' $(last_resurrect_file) | + awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ { print $2, $3, $6, $9; }' $(last_resurrect_file) | while IFS=$d read session_name window_number pane_index pane_command; do if ! is_pane_registered_as_existing "$session_name" "$window_number" "$pane_index"; then local pane_id="$session_name:$window_number.$pane_index" @@ -321,7 +321,7 @@ restore_shell_history() { restore_all_pane_processes() { if restore_pane_processes_enabled; then local pane_full_command - awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $11 !~ "^:$" { print $2, $3, $7, $8, $11; }' $(last_resurrect_file) | + awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $10 !~ "^:$" { print $2, $3, $6, $7, $10; }' $(last_resurrect_file) | while IFS=$d read -r session_name window_number pane_index dir pane_full_command; do dir="$(remove_first_char "$dir")" pane_full_command="$(remove_first_char "$pane_full_command")" @@ -331,7 +331,7 @@ restore_all_pane_processes() { } restore_active_pane_for_each_window() { - awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $9 == 1 { print $2, $3, $7; }' $(last_resurrect_file) | + awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $8 == 1 { print $2, $3, $6; }' $(last_resurrect_file) | while IFS=$d read session_name window_number active_pane; do tmux switch-client -t "${session_name}:${window_number}" tmux select-pane -t "$active_pane" @@ -339,7 +339,7 @@ restore_active_pane_for_each_window() { } restore_zoomed_windows() { - awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $6 ~ /Z/ && $9 == 1 { print $2, $3; }' $(last_resurrect_file) | + awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $5 ~ /Z/ && $8 == 1 { print $2, $3; }' $(last_resurrect_file) | while IFS=$d read session_name window_number; do tmux resize-pane -t "${session_name}:${window_number}" -Z done diff --git a/scripts/save.sh b/scripts/save.sh index cee984b..de5246f 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -59,7 +59,7 @@ window_format() { format+="${delimiter}" format+="#{window_index}" format+="${delimiter}" - format+="#{window_name}" + format+=":#{window_name}" format+="${delimiter}" format+="#{window_active}" format+="${delimiter}" From 1b63a940a03e2370f8a504e57b8a83103d288d89 Mon Sep 17 00:00:00 2001 From: A Farzat Date: Fri, 27 Aug 2021 15:57:26 +0900 Subject: [PATCH 4/5] Fix even more bugs in before last commit First, increment the relevant indices in the awk statements regarding windows, as now the window_name entry exists. Second, remove the window_name entry from the dump_pane_contents and dump_shell_history functions as it no longer exists in the format. --- scripts/restore.sh | 2 +- scripts/save.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/restore.sh b/scripts/restore.sh index fb7547e..cb7a9ae 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -355,7 +355,7 @@ restore_grouped_sessions() { } restore_active_and_alternate_windows() { - awk 'BEGIN { FS="\t"; OFS="\t" } /^window/ && $5 ~ /[*-]/ { print $2, $4, $3; }' $(last_resurrect_file) | + awk 'BEGIN { FS="\t"; OFS="\t" } /^window/ && $6 ~ /[*-]/ { print $2, $4, $3; }' $(last_resurrect_file) | sort -u | while IFS=$d read session_name active_window window_number; do tmux switch-client -t "${session_name}:${window_number}" diff --git a/scripts/save.sh b/scripts/save.sh index de5246f..2c672f4 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -259,14 +259,14 @@ dump_state() { dump_pane_contents() { local pane_contents_area="$(get_tmux_option "$pane_contents_area_option" "$default_pane_contents_area")" dump_panes_raw | - while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_pid history_size; do + while IFS=$d read line_type session_name window_number window_active window_flags pane_index dir pane_active pane_command pane_pid history_size; do capture_pane_contents "${session_name}:${window_number}.${pane_index}" "$history_size" "$pane_contents_area" done } dump_shell_history() { dump_panes | - while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command full_command; do + while IFS=$d read line_type session_name window_number window_active window_flags pane_index dir pane_active pane_command full_command; do save_shell_history "$session_name:$window_number.$pane_index" "$pane_command" "$full_command" done } From 3e8fbdf7aa96799388e9e51bb8bdb20dc6be06fd Mon Sep 17 00:00:00 2001 From: A Farzat Date: Fri, 27 Aug 2021 18:02:00 +0900 Subject: [PATCH 5/5] Make window_name variable local in restoring func In restore_window_properties function, the window_name is set globally at first, but now it is first declared as local to prevent that from happening. --- scripts/restore.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/restore.sh b/scripts/restore.sh index cb7a9ae..45a5a80 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -286,6 +286,7 @@ handle_session_0() { } restore_window_properties() { + local window_name \grep '^window' $(last_resurrect_file) | while IFS=$d read line_type session_name window_number window_name window_active window_flags window_layout automatic_rename; do window_name="$(remove_first_char "$window_name")"