-
-
Save Uroc327/8134169 to your computer and use it in GitHub Desktop.
This is a version of autoswap_mac.vim from Damian Conway's vim talk that works (or at least tries to) with wmctrl. This does not depend on any stupid titlestrings for vim. Instead it fetches the pid for the swap file and brings the associated window up.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Vim global plugin for automatin response to swapfiles (from autoswap_mac.vim from Damian Conway) | |
" Last Change: 2013 Dec 25 | |
" Maintainer: Constantin Runge <[email protected]> | |
" License: other | |
if exists("loaded_autoswap") | |
finish | |
endif | |
let loaded_autoswap = 1 | |
" Preserver external compatibility options, then enably full vim compatibility | |
" Do the same with shell redirection (normally stdout and stderr get 'merged' -> we don't want that) | |
let s:save_cpo = &cpo | |
let s:save_srr = &srr | |
set cpo&vim | |
set srr=">" | |
function! Strip(instring) | |
return substitute(a:instring, '^\s*\(.\{-}\)\s*$', '\1', '') | |
endfunction | |
" Invoke handler when swapfile opened | |
augroup AutoSwap | |
autocmd! | |
autocmd SwapExists * call ASHandleSwapFile(expand('<afile>:p')) | |
augroup END | |
" determine what to do | |
function! ASHandleSwapFile(filename) | |
" let active_window = ASGetWindow(a:filename) | |
let pid = ASGetPid(v:swapname) | |
let pwd = Strip(system('pwdx '.pid.' | cut -d: -f2')) " working directory of the process | |
let relname = system('python3 -c "import os.path; print(os.path.relpath('.shellescape(fnamemodify(a:filename, ":p")).','.shellescape(pwd).'))"') | |
" quit and change to existion session | |
if(pid > 0) | |
call ASDelayedMsg('Switched to existing session in another window') | |
call ASSwitchTo(pid, relname) " Open a new tab | |
let v:swapchoice = 'q' | |
" swapfile is older than file -> delete | |
elseif getftime(v:swapname) < getftime(a:filename) | |
call ASDelayedMsg("Old swapfile detected -> it's dead now") | |
call delete(v:swapname) | |
let v:swapchoice = 'e' | |
" swapfile could contain something useful -> open read-only | |
else | |
call ASDelayedMsg("Swapfile detected... You're in readonly mode") | |
let v:swapchoice = 'o' | |
endif | |
endfunction | |
" print message after autocommand completes | |
" (you can see it, but you don't have to hit <Enter> to continue) | |
function! ASDelayedMsg(msg) | |
augroup ASMsg | |
" Print message when entering the buffer | |
autocmd BufWinEnter * echohl WarningMsg | |
exec 'autocmd BufWinEnter * echon "\r'.printf("%-60s", a:msg).'"' | |
autocmd BufWinEnter * echohl NONE | |
" remove our autocmds, so it only prints once | |
autocmd BufWinEnter * augroup ASMsg | |
autocmd BufWinEnter * autocmd! | |
autocmd BufWinEnter * augroup END | |
augroup END | |
endfunction | |
" return the pid of the window that has the swapfile opened (or -1) | |
function! ASGetPid(filename) | |
let pid = Strip(system('fuser '.shellescape(fnamemodify(a:filename, ":p")))) | |
return (pid =~ '[0-9]\+' ? pid : -1) | |
endfunction | |
" switch to the window, open a new tab and open the file there | |
function! ASSwitchTo(pid, filename) | |
call system("wmctrl -ia $(wmctrl -lp | awk -vpid=".a:pid." '$3==pid { print $1; exit }')") | |
endfunction | |
" restore compatibility an shellredirection | |
let &cpo = s:save_cpo | |
let &srr = s:save_srr |
I installed the "wmctrl", but did not work in Linux Mint 16 KDE. Ideas?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works Perfectly. Thanks. =)