Skip to content

Commit 0007f9e

Browse files
committed
fix: matching socket to nvim parent only
There was an unintentional check to see if the process id of the neovim socket (--embed) is the "client" neovim process, which shouldn't ever be the case (I think). The behavior was a false positive that opens up the file from any pane in a running instance of neovim in a different tmux window. I went ahead and added this functionality, since I think it's useful. Now, it ton will try to open in a nvim instance in the current window, or fallback to the first nvim instance it finds.
1 parent 91bdd48 commit 0007f9e

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

scripts/ton

+30-10
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,55 @@ IFS=$OIFS
3535

3636
# Match child process with nvim sock, since process is within
3737
# e.g `/run/user/1000/nvim.1624238.0` -- nvim process is 1624238
38-
IFS=':' read -r -a panes <<<"$(tmux list-panes -F '#{pane_index} #{pane_pid}' | tr '\n' ':')"
38+
IFS=':' read -r -a panes <<<"$(tmux list-panes -a -F '#{window_index} #{pane_index} #{pane_pid}' | tr '\n' ':')"
3939
IFS=$OIFS
4040

41+
CURRENT_WINDOW_ID=$(tmux display-message -p '#{window_index}')
42+
43+
# args:
44+
# socket
45+
# win_id
46+
# pane_id
47+
remote_open() {
48+
nvim --server "$1" --remote-send "<esc>$OPEN_STRATEGY $FILE<cr>"
49+
nvim --server "$1" --remote-send "<esc>:call cursor($LINE, $COLUMN)<cr>"
50+
tmux selectw -t "$2" && tmux selectp -t "$3"
51+
exit 0
52+
}
53+
4154
for pane in "${panes[@]}"; do
4255
IFS=' ' pane_ids=($pane)
4356
IFS=$OIFS
4457

45-
id=${pane_ids[0]}
46-
pid=${pane_ids[1]}
58+
win_id=${pane_ids[0]}
59+
pane_id=${pane_ids[1]}
60+
pid=${pane_ids[2]}
4761

4862
# Get pid of nvim process
4963
cpid=$(pgrep -P "$pid" nvim)
5064
ppid=0
5165
if [ $cpid ]; then
52-
# Get pid of nvim parent process
66+
# Get pid of nvim parent RPC process (--embed)
5367
ppid=$(pgrep -P "$cpid" nvim)
5468
fi
5569

5670
for sock in "${LISTEN_SOCKS[@]}"; do
57-
if [[ $sock == *$cpid* ]] || [[ $sock == *$ppid* ]]; then
58-
# Open on remote!
59-
nvim --server "$sock" --remote-send "<esc>$OPEN_STRATEGY $FILE<cr>"
60-
nvim --server "$sock" --remote-send "<esc>:call cursor($LINE, $COLUMN)<cr>"
61-
tmux selectp -t "$id"
62-
exit 0
71+
# Check if the nvim process associated with the socket is the parent id
72+
# Prioritize instances running in the current window, but fallback to first found instance
73+
if [[ $sock == *nvim.$ppid.* ]] && [[ $win_id == $CURRENT_WINDOW_ID ]]; then
74+
remote_open $sock $win_id $pane_id
75+
elif [[ $sock == *nvim.$ppid.* ]] && [[ ! $SOCK ]]; then
76+
SOCK=$sock
77+
WIN_ID=$win_id
78+
PANE_ID=$pane_id
6379
fi
6480
done
6581
done
6682

83+
if [[ $SOCK ]]; then
84+
remote_open $SOCK $WIN_ID $PANE_ID
85+
fi
86+
6787
# No remote nvim, so just open in current pane
6888
tmux send-keys "nvim -c \"call cursor($LINE, $COLUMN)\" $FILE"
6989
tmux send-keys "C-m"

0 commit comments

Comments
 (0)