annotate runtime/macros/editexisting.vim @ 589:bc49ed25543d

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