@@ -40,45 +40,46 @@ usage() {
4040 EOF
4141}
4242
43- # Prepares the tmux commands to open a popup. After called,
43+ # Prepares the tmux commands to initialize a popup session . After called,
4444#
45- # - `open_cmds ` is used to initialize the popup session
45+ # - `init_cmds ` is used to initialize the popup session
4646# - `on_cleanup` is used to undo temporary changes on the popup server
4747# - `popup_id` is set to the name of the target popup session
48- declare open_cmds =() on_cleanup=() popup_id
49- prepare_open () {
48+ declare init_cmds =() on_cleanup=() popup_id
49+ prepare_init () {
5050 popup_id=${id:- $(interpolate popup_name=" $name " " $id_format " )}
5151 popup_id=$( escape_session_name " $popup_id " )
5252 if [[ -n $open_dir ]]; then
53+ # Interpolate `{popup_caller_path}`, `{popup_caller_pane_path}`.
5354 open_args+=(-c " $( interpolate popup_caller_path=" $caller_path " \
5455 popup_caller_pane_path=" $caller_pane_path " " $open_dir " ) " )
5556 fi
5657
57- open_cmds =()
58+ init_cmds =()
5859 if [[ $1 == " open" ]]; then
59- open_cmds +=(new -As " $popup_id " " ${open_args[@]} " " ${program[@]} " \; )
60+ init_cmds +=(new -As " $popup_id " " ${open_args[@]} " " ${program[@]} " \; )
6061 else
6162 if ! tmux has -t " $popup_id " 2> /dev/null; then
62- open_cmds +=(new -ds " $popup_id " " ${open_args[@]} " " ${program[@]} " \; )
63+ init_cmds +=(new -ds " $popup_id " " ${open_args[@]} " " ${program[@]} " \; )
6364 fi
64- open_cmds +=(switch -t " $popup_id " \; )
65+ init_cmds +=(switch -t " $popup_id " \; )
6566 fi
6667
6768 # Export internal variables
68- open_cmds +=(set @__popup_name " $name " \; )
69- open_cmds +=(set @__popup_id_format " $id_format " \; )
70- open_cmds +=(set @__popup_caller_path " $caller_path " \; )
71- open_cmds +=(set @__popup_caller_pane_path " $caller_pane_path " \; )
69+ init_cmds +=(set @__popup_name " $name " \; )
70+ init_cmds +=(set @__popup_id_format " $id_format " \; )
71+ init_cmds +=(set @__popup_caller_path " $caller_path " \; )
72+ init_cmds +=(set @__popup_caller_pane_path " $caller_pane_path " \; )
7273
7374 # Create temporary toggle keys in the opened popup
7475 # shellcheck disable=SC2206
7576 for k in " ${toggle_keys[@]} " ; do
76- open_cmds +=(bind $k run " #{@popup-toggle} $( escape " ${args[@]} " ) " \; )
77+ init_cmds +=(bind $k run " #{@popup-toggle} $( escape " ${args[@]} " ) " \; )
7778 on_cleanup+=(unbind $k \; )
7879 done
7980
8081 if parse_cmds " $on_init " ; then
81- open_cmds +=(" ${cmds[@]} " )
82+ init_cmds +=(" ${cmds[@]} " )
8283 fi
8384}
8485
@@ -153,8 +154,8 @@ main() {
153154 elif [[ $toggle_mode == " switch" ]]; then
154155 # Inherit the caller's ID format in switch mode
155156 id_format=${caller_id_format}
156- prepare_open " switch"
157- tmux " ${open_cmds [@]} "
157+ prepare_init " switch"
158+ tmux " ${init_cmds [@]} "
158159 return
159160 elif [[ $toggle_mode != " force-open" ]]; then
160161 die " illegal toggle mode: $toggle_mode "
@@ -169,37 +170,47 @@ main() {
169170 popup_server=${socket_name}
170171 fi
171172
172- # Run hook: before-open
173- if parse_cmds " $before_open " ; then tmux " ${cmds[@]} " ; fi
173+ # Command sequence to open the popup window, including hooks.
174+ open_cmds=()
175+
176+ # Handle hook: before-open
177+ if parse_cmds " $before_open " ; then open_cmds+=(" ${cmds[@]} " \; ); fi
174178
175179 # This session is the caller, so use it's path
176180 caller_path=${session_path}
177181 caller_pane_path=${pane_path}
178- prepare_open " open"
182+ prepare_init " open"
179183
184+ # Script to initialize the popup session inside a popup window.
180185 open_script=" "
181- # Revert to the user's default shell.
182- open_script+=" tmux set default-shell '$default_shell '"
183- # Set $TMUX_POPUP_SERVER so as to identify the popup server,
184- # and propagate user's default shell.
185- open_script+=" ; export TMUX_POPUP_SERVER='$popup_server ' SHELL='$default_shell '"
186- # Suppress stdout to hide the `[detached] ...` message
187- open_script+=" ; exec tmux $( escape " ${popup_socket[@]} " " ${open_cmds[@]} " ) >/dev/null"
188186
189187 # Starting from version 3.5, tmux uses the user's `default-shell` to execute
190188 # shell commands. However, our scripts require sh(1) and may not be parsed
191189 # correctly by some incompatible shells. In this case, we change the default
192190 # shell to `/bin/sh` and then revert it immediately.
193- tmux set default-shell " /bin/sh" \; popup " ${display_args[@]} " " $open_script "
191+ open_script+=" tmux set default-shell '$default_shell ' ;"
192+ open_cmds+=(set default-shell " /bin/sh" \; )
193+
194+ # Set $TMUX_POPUP_SERVER to identify the popup server.
195+ # Propagate user's default shell.
196+ open_script+=" export TMUX_POPUP_SERVER='$popup_server ' ;"
197+ open_script+=" export SHELL='$default_shell ' ;"
198+
199+ # Suppress stdout to hide the `[detached] ...` message
200+ open_script+=" exec tmux $( escape " ${popup_socket[@]} " " ${init_cmds[@]} " ) >/dev/null"
201+ open_cmds+=(display-popup " ${display_args[@]} " " $open_script " \; )
202+
203+ # Handle hook: after-close
204+ if parse_cmds " $after_close " ; then open_cmds+=(" ${cmds[@]} " \; ); fi
205+
206+ # Do open the popup window
207+ tmux " ${open_cmds[@]} "
194208
195209 # Undo temporary changes on the popup server
196210 if [[ -z $opened_name && ${# on_cleanup} -gt 0 ]]; then
197211 # Ignore error if the server has already stopped
198212 tmux -N " ${popup_socket[@]} " " ${on_cleanup[@]} " 2> /dev/null || true
199213 fi
200-
201- # Run hook: after-close
202- if parse_cmds " $after_close " ; then tmux " ${cmds[@]} " ; fi
203214}
204215
205216args=(" $@ " )
0 commit comments