Skip to content

Commit 4234ba9

Browse files
authored
Merge pull request #401 from Farzat07/automatic-rename2
Maintain the value of automatic-rename
2 parents 003a7cd + 3e8fbdf commit 4234ba9

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

scripts/restore.sh

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,31 +126,29 @@ pane_creation_command() {
126126
new_window() {
127127
local session_name="$1"
128128
local window_number="$2"
129-
local window_name="$3"
130-
local dir="$4"
131-
local pane_index="$5"
129+
local dir="$3"
130+
local pane_index="$4"
132131
local pane_id="${session_name}:${window_number}.${pane_index}"
133132
dir="${dir/#\~/$HOME}"
134133
if is_restoring_pane_contents && pane_contents_file_exists "$pane_id"; then
135134
local pane_creation_command="$(pane_creation_command "$session_name" "$window_number" "$pane_index")"
136-
tmux new-window -d -t "${session_name}:${window_number}" -n "$window_name" -c "$dir" "$pane_creation_command"
135+
tmux new-window -d -t "${session_name}:${window_number}" -c "$dir" "$pane_creation_command"
137136
else
138-
tmux new-window -d -t "${session_name}:${window_number}" -n "$window_name" -c "$dir"
137+
tmux new-window -d -t "${session_name}:${window_number}" -c "$dir"
139138
fi
140139
}
141140

142141
new_session() {
143142
local session_name="$1"
144143
local window_number="$2"
145-
local window_name="$3"
146-
local dir="$4"
147-
local pane_index="$5"
144+
local dir="$3"
145+
local pane_index="$4"
148146
local pane_id="${session_name}:${window_number}.${pane_index}"
149147
if is_restoring_pane_contents && pane_contents_file_exists "$pane_id"; then
150148
local pane_creation_command="$(pane_creation_command "$session_name" "$window_number" "$pane_index")"
151-
TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -n "$window_name" -c "$dir" "$pane_creation_command"
149+
TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -c "$dir" "$pane_creation_command"
152150
else
153-
TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -n "$window_name" -c "$dir"
151+
TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -c "$dir"
154152
fi
155153
# change first window number if necessary
156154
local created_window_num="$(first_window_num)"
@@ -162,50 +160,45 @@ new_session() {
162160
new_pane() {
163161
local session_name="$1"
164162
local window_number="$2"
165-
local window_name="$3"
166-
local dir="$4"
167-
local pane_index="$5"
163+
local dir="$3"
164+
local pane_index="$4"
168165
local pane_id="${session_name}:${window_number}.${pane_index}"
169166
if is_restoring_pane_contents && pane_contents_file_exists "$pane_id"; then
170167
local pane_creation_command="$(pane_creation_command "$session_name" "$window_number" "$pane_index")"
171168
tmux split-window -t "${session_name}:${window_number}" -c "$dir" "$pane_creation_command"
172169
else
173170
tmux split-window -t "${session_name}:${window_number}" -c "$dir"
174171
fi
175-
tmux rename-window -t "${session_name}:${window_number}" "$window_name"
176172
# minimize window so more panes can fit
177173
tmux resize-pane -t "${session_name}:${window_number}" -U "999"
178174
}
179175

180176
restore_pane() {
181177
local pane="$1"
182-
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
178+
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
183179
dir="$(remove_first_char "$dir")"
184-
window_name="$(remove_first_char "$window_name")"
185180
pane_full_command="$(remove_first_char "$pane_full_command")"
186181
if [ "$session_name" == "0" ]; then
187182
restored_session_0_true
188183
fi
189184
if pane_exists "$session_name" "$window_number" "$pane_index"; then
190-
tmux rename-window -t "${session_name}:${window_number}" "$window_name"
191185
if is_restoring_from_scratch; then
192186
# overwrite the pane
193187
# happens only for the first pane if it's the only registered pane for the whole tmux server
194188
local pane_id="$(tmux display-message -p -F "#{pane_id}" -t "$session_name:$window_number")"
195-
new_pane "$session_name" "$window_number" "$window_name" "$dir" "$pane_index"
189+
new_pane "$session_name" "$window_number" "$dir" "$pane_index"
196190
tmux kill-pane -t "$pane_id"
197191
else
198192
# Pane exists, no need to create it!
199193
# Pane existence is registered. Later, its process also won't be restored.
200194
register_existing_pane "$session_name" "$window_number" "$pane_index"
201195
fi
202196
elif window_exists "$session_name" "$window_number"; then
203-
tmux rename-window -t "${session_name}:${window_number}" "$window_name"
204-
new_pane "$session_name" "$window_number" "$window_name" "$dir" "$pane_index"
197+
new_pane "$session_name" "$window_number" "$dir" "$pane_index"
205198
elif session_exists "$session_name"; then
206-
new_window "$session_name" "$window_number" "$window_name" "$dir" "$pane_index"
199+
new_window "$session_name" "$window_number" "$dir" "$pane_index"
207200
else
208-
new_session "$session_name" "$window_number" "$window_name" "$dir" "$pane_index"
201+
new_session "$session_name" "$window_number" "$dir" "$pane_index"
209202
fi
210203
done < <(echo "$pane")
211204
}
@@ -292,15 +285,23 @@ handle_session_0() {
292285
fi
293286
}
294287

295-
restore_pane_layout_for_each_window() {
288+
restore_window_properties() {
289+
local window_name
296290
\grep '^window' $(last_resurrect_file) |
297-
while IFS=$d read line_type session_name window_number window_active window_flags window_layout; do
291+
while IFS=$d read line_type session_name window_number window_name window_active window_flags window_layout automatic_rename; do
292+
window_name="$(remove_first_char "$window_name")"
293+
tmux rename-window -t "${session_name}:${window_number}" "$window_name"
298294
tmux select-layout -t "${session_name}:${window_number}" "$window_layout"
295+
if [ "${automatic_rename}" = ":" ]; then
296+
tmux set-option -u -t "${session_name}:${window_number}" automatic-rename
297+
else
298+
tmux set-option -t "${session_name}:${window_number}" automatic-rename "$automatic_rename"
299+
fi
299300
done
300301
}
301302

302303
restore_shell_history() {
303-
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ { print $2, $3, $7, $10; }' $(last_resurrect_file) |
304+
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ { print $2, $3, $6, $9; }' $(last_resurrect_file) |
304305
while IFS=$d read session_name window_number pane_index pane_command; do
305306
if ! is_pane_registered_as_existing "$session_name" "$window_number" "$pane_index"; then
306307
local pane_id="$session_name:$window_number.$pane_index"
@@ -321,7 +322,7 @@ restore_shell_history() {
321322
restore_all_pane_processes() {
322323
if restore_pane_processes_enabled; then
323324
local pane_full_command
324-
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $11 !~ "^:$" { print $2, $3, $7, $8, $11; }' $(last_resurrect_file) |
325+
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $10 !~ "^:$" { print $2, $3, $6, $7, $10; }' $(last_resurrect_file) |
325326
while IFS=$d read -r session_name window_number pane_index dir pane_full_command; do
326327
dir="$(remove_first_char "$dir")"
327328
pane_full_command="$(remove_first_char "$pane_full_command")"
@@ -331,15 +332,15 @@ restore_all_pane_processes() {
331332
}
332333

333334
restore_active_pane_for_each_window() {
334-
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $9 == 1 { print $2, $3, $7; }' $(last_resurrect_file) |
335+
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $8 == 1 { print $2, $3, $6; }' $(last_resurrect_file) |
335336
while IFS=$d read session_name window_number active_pane; do
336337
tmux switch-client -t "${session_name}:${window_number}"
337338
tmux select-pane -t "$active_pane"
338339
done
339340
}
340341

341342
restore_zoomed_windows() {
342-
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $6 ~ /Z/ && $9 == 1 { print $2, $3; }' $(last_resurrect_file) |
343+
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $5 ~ /Z/ && $8 == 1 { print $2, $3; }' $(last_resurrect_file) |
343344
while IFS=$d read session_name window_number; do
344345
tmux resize-pane -t "${session_name}:${window_number}" -Z
345346
done
@@ -355,7 +356,7 @@ restore_grouped_sessions() {
355356
}
356357

357358
restore_active_and_alternate_windows() {
358-
awk 'BEGIN { FS="\t"; OFS="\t" } /^window/ && $5 ~ /[*-]/ { print $2, $4, $3; }' $(last_resurrect_file) |
359+
awk 'BEGIN { FS="\t"; OFS="\t" } /^window/ && $6 ~ /[*-]/ { print $2, $4, $3; }' $(last_resurrect_file) |
359360
sort -u |
360361
while IFS=$d read session_name active_window window_number; do
361362
tmux switch-client -t "${session_name}:${window_number}"
@@ -376,7 +377,7 @@ main() {
376377
execute_hook "pre-restore-all"
377378
restore_all_panes
378379
handle_session_0
379-
restore_pane_layout_for_each_window >/dev/null 2>&1
380+
restore_window_properties >/dev/null 2>&1
380381
execute_hook "pre-restore-history"
381382
if save_shell_history_option_on; then
382383
restore_shell_history

scripts/save.sh

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ pane_format() {
3333
format+="${delimiter}"
3434
format+="#{window_index}"
3535
format+="${delimiter}"
36-
format+=":#{window_name}"
37-
format+="${delimiter}"
3836
format+="#{window_active}"
3937
format+="${delimiter}"
4038
format+=":#{window_flags}"
@@ -61,6 +59,8 @@ window_format() {
6159
format+="${delimiter}"
6260
format+="#{window_index}"
6361
format+="${delimiter}"
62+
format+=":#{window_name}"
63+
format+="${delimiter}"
6464
format+="#{window_active}"
6565
format+="${delimiter}"
6666
format+=":#{window_flags}"
@@ -227,25 +227,28 @@ fetch_and_dump_grouped_sessions(){
227227
dump_panes() {
228228
local full_command
229229
dump_panes_raw |
230-
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
230+
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
231231
# not saving panes from grouped sessions
232232
if is_session_grouped "$session_name"; then
233233
continue
234234
fi
235235
full_command="$(pane_full_command $pane_pid)"
236236
dir=$(echo $dir | sed 's/ /\\ /') # escape all spaces in directory path
237-
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}"
237+
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}"
238238
done
239239
}
240240

241241
dump_windows() {
242242
dump_windows_raw |
243-
while IFS=$d read line_type session_name window_index window_active window_flags window_layout; do
243+
while IFS=$d read line_type session_name window_index window_name window_active window_flags window_layout; do
244244
# not saving windows from grouped sessions
245245
if is_session_grouped "$session_name"; then
246246
continue
247247
fi
248-
echo "${line_type}${d}${session_name}${d}${window_index}${d}${window_active}${d}${window_flags}${d}${window_layout}"
248+
automatic_rename="$(tmux show-window-options -vt "${session_name}:${window_index}" automatic-rename)"
249+
# If the option was unset, place the ":" placeholder instead.
250+
[ -z "${automatic_rename}" ] && automatic_rename=":"
251+
echo "${line_type}${d}${session_name}${d}${window_index}${d}${window_name}${d}${window_active}${d}${window_flags}${d}${window_layout}${d}${automatic_rename}"
249252
done
250253
}
251254

@@ -256,14 +259,14 @@ dump_state() {
256259
dump_pane_contents() {
257260
local pane_contents_area="$(get_tmux_option "$pane_contents_area_option" "$default_pane_contents_area")"
258261
dump_panes_raw |
259-
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
262+
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
260263
capture_pane_contents "${session_name}:${window_number}.${pane_index}" "$history_size" "$pane_contents_area"
261264
done
262265
}
263266

264267
dump_shell_history() {
265268
dump_panes |
266-
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
269+
while IFS=$d read line_type session_name window_number window_active window_flags pane_index dir pane_active pane_command full_command; do
267270
save_shell_history "$session_name:$window_number.$pane_index" "$pane_command" "$full_command"
268271
done
269272
}

0 commit comments

Comments
 (0)