589
|
1 " Vim Plugin: Edit the file with an existing Vim if possible
|
|
2 " Maintainer: Bram Moolenaar
|
|
3 " Last Change: 2005 Dec 11
|
|
4
|
|
5 " This is a plugin, drop it in your (Unix) ~/.vim/plugin or (Win32)
|
|
6 " $VIM/vimfiles/plugin directory.
|
|
7
|
|
8 " This plugin serves two purposes:
|
|
9 " 1. On startup, if we were invoked with one file name argument and the file
|
|
10 " is not modified then try to find another Vim instance that is editing
|
|
11 " this file. If there is one then bring it to the foreground and exit.
|
|
12 " 2. When a file is edited and a swap file exists for it, try finding that
|
|
13 " other Vim and bring it to the foreground. Requires Vim 7, because it
|
|
14 " uses the SwapExists autocommand event.
|
|
15
|
|
16 " Function that finds the Vim instance that is editing "filename" and brings
|
|
17 " it to the foreground.
|
|
18 func s:EditElsewhere(filename)
|
|
19 let fname_esc = substitute(a:filename, "'", "''", "g")
|
|
20
|
|
21 let servers = serverlist()
|
|
22 while servers != ''
|
|
23 " Get next server name in "servername"; remove it from "servers".
|
|
24 let i = match(servers, "\n")
|
|
25 if i == -1
|
|
26 let servername = servers
|
|
27 let servers = ''
|
|
28 else
|
|
29 let servername = strpart(servers, 0, i)
|
|
30 let servers = strpart(servers, i + 1)
|
|
31 endif
|
|
32
|
|
33 " Skip ourselves.
|
|
34 if servername ==? v:servername
|
|
35 continue
|
|
36 endif
|
|
37
|
|
38 " Check if this server is editing our file.
|
|
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 " Make sure the file is visible in a window (not hidden).
|
|
47 " If v:swapcommand exists and is set, send it to the server.
|
|
48 if exists("v:swapcommand")
|
|
49 let c = substitute(v:swapcommand, "'", "''", "g")
|
|
50 call remote_expr(servername, "EditExisting('" . fname_esc . "', '" . c . "')")
|
|
51 else
|
|
52 call remote_expr(servername, "EditExisting('" . fname_esc . "', '')")
|
|
53 endif
|
|
54
|
|
55 if !(has('vim_starting') && has('gui_running') && has('gui_win32'))
|
|
56 " Tell the user what is happening. Not when the GUI is starting
|
|
57 " though, it would result in a message box.
|
|
58 echomsg "File is being edited by " . servername
|
|
59 sleep 2
|
|
60 endif
|
|
61 return 'q'
|
|
62 endif
|
|
63 endwhile
|
|
64 return ''
|
|
65 endfunc
|
|
66
|
|
67 " When the plugin is loaded and there is one file name argument: Find another
|
|
68 " Vim server that is editing this file right now.
|
|
69 if argc() == 1 && !&modified
|
|
70 if s:EditElsewhere(expand("%:p")) == 'q'
|
|
71 quit
|
|
72 endif
|
|
73 endif
|
|
74
|
|
75 " Setup for handling the situation that an existing swap file is found.
|
|
76 try
|
|
77 au! SwapExists * let v:swapchoice = s:EditElsewhere(expand("<afile>:p"))
|
|
78 catch
|
|
79 " Without SwapExists we don't do anything for ":edit" commands
|
|
80 endtry
|
|
81
|
|
82 " Function used on the server to make the file visible and possibly execute a
|
|
83 " command.
|
|
84 func! EditExisting(fname, command)
|
|
85 let n = bufwinnr(a:fname)
|
|
86 if n > 0
|
|
87 exe n . "wincmd w"
|
|
88 else
|
|
89 exe "split " . escape(a:fname, ' #%"|')
|
|
90 endif
|
|
91
|
|
92 if a:command != ''
|
|
93 exe "normal " . a:command
|
|
94 endif
|
|
95
|
|
96 redraw
|
|
97 endfunc
|