gioele / vim-autoswap

Please Vim, stop with these swap file messages. Just switch to the correct window!
Creative Commons Zero v1.0 Universal
118 stars 25 forks source link

Not switching window on Mac OS X with tmux #12

Closed ddickstein closed 7 years ago

ddickstein commented 7 years ago

I'm using the same vim configuration on both my personal macOS Sierra (10.12.4) laptop and on my CentOS 7 server. I have tmux running on both and let g:autoswap_detect_tmux = 1. On CentOS if I have the file open in one vim session and I try to open it in a new pane the cursor jumps panes correctly. On my laptop if I try the same thing it instead falls back to opening a readonly version. What am I missing?

gioele commented 7 years ago

Hi @ddickstein, could you please install the branch verbose-debug, run a test on macOS and paste here the content of /tmp/vim-autoswap.log?

ddickstein commented 7 years ago

Starting session 1495231585 testing already loaded?... ...no, set variable testing tmux support set?... ...yes, use it

Starting session 1495231588 testing already loaded?... ...no, set variable testing tmux support set?... ...yes, use it testing is $TMUX set?... ...yes, tmux not running detected active window = <> Swapfile detected... opening read-only

thalesmello commented 7 years ago

I'm also facing the same issue, using NVIM v0.2.1-237-gf4fddbfb

Starting session 1496877454
testing already loaded?...
 ...no, set variable
testing tmux support set?...
 ...yes, use it
testing is $TMUX set?...
 ...yes, tmux not running
tmux: pid = >['18521']<
tmux: tty = >[]<
tmux: window = >[]<
detected active window = <[>
Switched to existing session in another window
g:autoswap_detect_tmux = 1
testing is $TMUX set?...
 ...yes, tmux not running
AS_RunningTmux() = 1
has('macunix') = 1
has('unix') = 1
testing is $TMUX set?...
 ...yes, tmux not running

Starting session 1496877458
testing already loaded?...
 ...no, set variable
testing tmux support set?...
 ...yes, use it
thalesmello commented 7 years ago

@gioele @ddickstein I've opened a pull request that should solve this issue. Could you guys test it and see if it works, please?

gioele commented 7 years ago

@ddickstein, @thalesmello: I think I have found a way to support tmux on both Linux and MacOS with a single code path. Could you please test 994c14073b3091fbce2120366d502098e1150e8d or the branch tmux-on-macos?

@lutostag: you contributed the initial code to support tmux on Linux. Could you see if this change broke something for you? Thanks

thalesmello commented 7 years ago

@gioele Not working.

thalesmello commented 7 years ago

@gioele The following modified function makes it work for me on MacOS.

I've removed trailing whitespaces from the tty variable, and changed the grep regex you applied on the tmux list-panes command. See if that works for you.

function! AS_DetectActiveWindow_Tmux (swapname)
    let pid = systemlist('fuser '.a:swapname.' 2>/dev/null | grep -E -o "[0-9]+"')
    if (len(pid) == 0)
        return ''
    endif
    let tty = systemlist('ps -o "tt=" '.pid[0].' 2>/dev/null')
    if (len(tty) == 0)
        return ''
    endif
    " Remove trailing whitespaces when on MacOS
    let tty[0] = substitute(tty[0], '\s\+$', '', '')
    "FIXME: `pts/1` matches `pts/11` as well

    " The output of `list-panes` on MacOS is `/dev/ttys007` and tty is
    " `s007`, so we need the systemlist to match the substring `s007`
    " inside of `/dev/ttys007`.
    let window = systemlist('tmux list-panes -aF "#{pane_tty} #{window_index} #{pane_index}" | grep -F "'.tty[0].'" 2>/dev/null')
    if (len(window) == 0)
        return ''
    endif
    return window[0]
endfunction
gioele commented 7 years ago

Thanks @ddickstein and @thalesmello for the report and the help. I merged the fix for tmux on MacOS.

Some days ago I left a message about about some tiny changes I made and how perverse the MacOS's tty naming scheme was. Shame that it somehow got lost. ;)

Everything should work fine now. Feel free to reopen this issues if this is not the case.