@@ -8,6 +8,8 @@ source "$CURRENT_DIR/variables.sh"
8
8
source " $CURRENT_DIR /helpers.sh"
9
9
10
10
OPEN_STRATEGY=" $( get_tmux_option " $open_strategy " " :e" ) "
11
+ MENU_STYLE=" $( get_tmux_option " $menu_style " ) "
12
+ MENU_SELECTED_STYLE=" $( get_tmux_option " $menu_selected_style " ) "
11
13
12
14
# Check pipe from stdin or parameter
13
15
FPATH=${STDIN:- $1 }
@@ -30,60 +32,49 @@ LINE=${a[1]:-0}
30
32
COLUMN=${a[2]:- 0}
31
33
32
34
# Get all nvim listening sockets (default location)
33
- IFS=' :' read -r -a LISTEN_SOCKS <<< " $(ls " ${XDG_RUNTIME_DIR:- ${TMPDIR} nvim.${USER} } " /**/nvim.*.0 2>/dev/null | tr '\n' ':')"
34
- IFS=$OIFS
35
-
36
- # Match child process with nvim sock, since process is within
37
- # e.g `/run/user/1000/nvim.1624238.0` -- nvim process is 1624238
38
- IFS=' :' read -r -a panes <<< " $(tmux list-panes -a -F '#{window_index} #{pane_index} #{pane_pid}' | tr '\n' ':')"
39
- IFS=$OIFS
35
+ readarray -t LISTEN_SOCKS < <( ls " ${XDG_RUNTIME_DIR:- ${TMPDIR} nvim.${USER} } " /** /nvim.* .0 2> /dev/null)
40
36
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
- }
37
+ CURRENT_WINDOW_INDEX=$( tmux display-message -p ' #{window_index}' )
53
38
54
- for pane in " ${panes[@]} " ; do
55
- IFS=' ' pane_ids=($pane )
56
- IFS=$OIFS
39
+ MENU_ARGS=()
40
+ SOCK_COUNT=${# LISTEN_SOCKS[@]}
41
+ SOCK_INDEX=1
42
+ for sock in " ${LISTEN_SOCKS[@]} " ; do
43
+ # extract pid from socket path
44
+ server_pid=$( echo " $sock " | cut -d' .' -f2)
45
+ # process id of server's parent, which is the nvim client
46
+ client_pid=$( ps -o ppid= -p " $server_pid " | tr -d ' ' )
47
+ # process id of tmux pane that contains the nvim client
48
+ pane_pid=$( ps -o ppid= -p " $client_pid " | tr -d ' ' )
57
49
58
- win_id=${pane_ids[0]}
59
- pane_id=${pane_ids[1]}
60
- pid=${pane_ids[2]}
50
+ # tmux ids for the nvim client pane
51
+ readarray -d ' ' -t ids < <( tmux list-panes -a -f " #{==:#{pane_pid},$pane_pid }" -F " #{window_index} #{window_name} #{pane_index}" )
52
+ window_index=${ids[0]}
53
+ window_name=${ids[1]}
54
+ pane_index=${ids[2]}
61
55
62
- # Get pid of nvim process
63
- cpid=$( pgrep -P " $pid " nvim)
64
- ppid=0
65
- if [ $cpid ]; then
66
- # Get pid of nvim parent RPC process (--embed)
67
- ppid=$( pgrep -P " $cpid " nvim)
56
+ c1=" nvim --server $sock --remote-send \" <esc>$OPEN_STRATEGY $FILE <cr>\" "
57
+ c2=" nvim --server $sock --remote-send \" <esc>:call cursor($LINE , $COLUMN )<cr>\" "
58
+ c3=" tmux selectw -t $window_index && tmux selectp -t $pane_index "
59
+ remote_open=" $c1 && $c2 && $c3 "
60
+ if [[ $window_index == " $CURRENT_WINDOW_INDEX " ]] && [[ $SOCK_COUNT == 1 ]]; then
61
+ # we found a nvim running in our window, escape hatch
62
+ eval $remote_open
63
+ exit 0
64
+ else
65
+ # store this nvim instance for selection
66
+ MENU_ARGS+=(" $window_name : pane $pane_index " " $SOCK_INDEX " " run '$remote_open '" )
68
67
fi
69
-
70
- for sock in " ${LISTEN_SOCKS[@]} " ; do
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
79
- fi
80
- done
68
+ SOCK_INDEX=$(( $SOCK_INDEX + 1 ))
81
69
done
82
70
83
- if [[ $SOCK ]]; then
84
- remote_open $SOCK $WIN_ID $PANE_ID
71
+ if [[ ${MENU_ARGS[0]} ]]; then
72
+ # open menu for selection
73
+ tmux menu -s " $MENU_STYLE " -H " $MENU_SELECTED_STYLE " -T " tmux-open-nvim: $FILE " " ${MENU_ARGS[@]} "
74
+ exit 0
75
+ else
76
+ # No remote nvim, so just open in current pane
77
+ tmux send-keys " nvim -c \" call cursor($LINE , $COLUMN )\" $FILE "
78
+ tmux send-keys " C-m"
79
+ exit 0
85
80
fi
86
-
87
- # No remote nvim, so just open in current pane
88
- tmux send-keys " nvim -c \" call cursor($LINE , $COLUMN )\" $FILE "
89
- tmux send-keys " C-m"
0 commit comments