editexisting.vim (3670B)
1 " Vim Plugin: Edit the file with an existing Vim if possible 2 " Maintainer: The Vim Project <https://github.com/vim/vim> 3 " Last Change: 2023 Aug 13 4 5 " To use add ":packadd! editexisting" in your vimrc file. 6 7 " This plugin serves two purposes: 8 " 1. On startup, if we were invoked with one file name argument and the file 9 " is not modified then try to find another Vim instance that is editing 10 " this file. If there is one then bring it to the foreground and exit. 11 " 2. When a file is edited and a swap file exists for it, try finding that 12 " other Vim and bring it to the foreground. Requires Vim 7, because it 13 " uses the SwapExists autocommand event. 14 15 " Function that finds the Vim instance that is editing "filename" and brings 16 " it to the foreground. 17 func s:EditElsewhere(filename) 18 let fname_esc = substitute(a:filename, "'", "''", "g") 19 20 let servers = serverlist() 21 while servers != '' 22 " Get next server name in "servername"; remove it from "servers". 23 let i = match(servers, "\n") 24 if i == -1 25 let servername = servers 26 let servers = '' 27 else 28 let servername = strpart(servers, 0, i) 29 let servers = strpart(servers, i + 1) 30 endif 31 32 " Skip ourselves. 33 if servername ==? v:servername 34 continue 35 endif 36 37 " Check if this server is editing our file. 38 try 39 if remote_expr(servername, "bufloaded('" . fname_esc . "')") 40 " Yes, bring it to the foreground. 41 if has("win32") 42 call remote_foreground(servername) 43 endif 44 call remote_expr(servername, "foreground()") 45 46 if remote_expr(servername, "exists('*EditExisting')") 47 " Make sure the file is visible in a window (not hidden). 48 " If v:swapcommand exists and is set, send it to the server. 49 if exists("v:swapcommand") 50 let c = substitute(v:swapcommand, "'", "''", "g") 51 call remote_expr(servername, "EditExisting('" . fname_esc . "', '" . c . "')") 52 else 53 call remote_expr(servername, "EditExisting('" . fname_esc . "', '')") 54 endif 55 endif 56 57 if !(has('vim_starting') && has('gui_running') && has('gui_win32')) 58 " Tell the user what is happening. Not when the GUI is starting 59 " though, it would result in a message box. 60 echomsg "File is being edited by " . servername 61 sleep 2 62 endif 63 return 'q' 64 endif 65 catch /^Vim\%((\a\+)\)\=:E241:/ 66 " Unable to send to this server, ignore it. 67 endtry 68 endwhile 69 return '' 70 endfunc 71 72 " When the plugin is loaded and there is one file name argument: Find another 73 " Vim server that is editing this file right now. 74 if argc() == 1 && !&modified 75 if s:EditElsewhere(expand("%:p")) == 'q' 76 quit 77 endif 78 endif 79 80 " Setup for handling the situation that an existing swap file is found. 81 try 82 au! SwapExists * let v:swapchoice = s:EditElsewhere(expand("<afile>:p")) 83 catch 84 " Without SwapExists we don't do anything for ":edit" commands 85 endtry 86 87 " Function used on the server to make the file visible and possibly execute a 88 " command. 89 func! EditExisting(fname, command) 90 " Get the window number of the file in the current tab page. 91 let winnr = bufwinnr(a:fname) 92 if winnr <= 0 93 " Not found, look in other tab pages. 94 let bufnr = bufnr(a:fname) 95 for i in range(tabpagenr('$')) 96 if index(tabpagebuflist(i + 1), bufnr) >= 0 97 " Make this tab page the current one and find the window number. 98 exe 'tabnext ' . (i + 1) 99 let winnr = bufwinnr(a:fname) 100 break 101 endif 102 endfor 103 endif 104 105 if winnr > 0 106 exe winnr . "wincmd w" 107 elseif exists('*fnameescape') 108 exe "split " . fnameescape(a:fname) 109 else 110 exe "split " . escape(a:fname, " \t\n*?[{`$\\%#'\"|!<") 111 endif 112 113 if a:command != '' 114 exe "normal! " . a:command 115 endif 116 117 redraw 118 endfunc