annotate runtime/macros/editexisting.vim @ 4172:1c819b05529c v7.3.838

updated for version 7.3.838 Problem: Insufficient testing for mksession. Solution: Add tests. (mostly by Roland Eggner)
author Bram Moolenaar <bram@vim.org>
date Tue, 26 Feb 2013 17:21:29 +0100
parents 73fe8baea242
children 7ffc704cb7c1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
856
8cd729851562 updated for version 7.0g
vimboss
parents: 602
diff changeset
1 " Vim Plugin: Edit the file with an existing Vim if possible
589
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
2 " Maintainer: Bram Moolenaar
1620
73fe8baea242 updated for version 7.2a
vimboss
parents: 1093
diff changeset
3 " Last Change: 2008 May 29
589
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)
602
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
6 " $VIM/vimfiles/plugin directory. Or make a symbolic link, so that you
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
7 " automatically use the latest version.
589
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
8
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
9 " This plugin serves two purposes:
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
10 " 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
11 " is not modified then try to find another Vim instance that is editing
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
12 " this file. If there is one then bring it to the foreground and exit.
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
13 " 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
14 " other Vim and bring it to the foreground. Requires Vim 7, because it
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
15 " uses the SwapExists autocommand event.
1620
73fe8baea242 updated for version 7.2a
vimboss
parents: 1093
diff changeset
16 if v:version < 700
73fe8baea242 updated for version 7.2a
vimboss
parents: 1093
diff changeset
17 finish
73fe8baea242 updated for version 7.2a
vimboss
parents: 1093
diff changeset
18 endif
589
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
19
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
20 " Function that finds the Vim instance that is editing "filename" and brings
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
21 " it to the foreground.
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
22 func s:EditElsewhere(filename)
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
23 let fname_esc = substitute(a:filename, "'", "''", "g")
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
24
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
25 let servers = serverlist()
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
26 while servers != ''
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
27 " Get next server name in "servername"; remove it from "servers".
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
28 let i = match(servers, "\n")
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
29 if i == -1
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
30 let servername = servers
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
31 let servers = ''
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
32 else
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
33 let servername = strpart(servers, 0, i)
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
34 let servers = strpart(servers, i + 1)
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
35 endif
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
36
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
37 " Skip ourselves.
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
38 if servername ==? v:servername
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
39 continue
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
40 endif
856
8cd729851562 updated for version 7.0g
vimboss
parents: 602
diff changeset
41
589
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
42 " Check if this server is editing our file.
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
43 if remote_expr(servername, "bufloaded('" . fname_esc . "')")
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
44 " Yes, bring it to the foreground.
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
45 if has("win32")
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
46 call remote_foreground(servername)
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
47 endif
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
48 call remote_expr(servername, "foreground()")
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
49
602
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
50 if remote_expr(servername, "exists('*EditExisting')")
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
51 " Make sure the file is visible in a window (not hidden).
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
52 " If v:swapcommand exists and is set, send it to the server.
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
53 if exists("v:swapcommand")
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
54 let c = substitute(v:swapcommand, "'", "''", "g")
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
55 call remote_expr(servername, "EditExisting('" . fname_esc . "', '" . c . "')")
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
56 else
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
57 call remote_expr(servername, "EditExisting('" . fname_esc . "', '')")
111509d2767a updated for version 7.0171
vimboss
parents: 589
diff changeset
58 endif
589
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
59 endif
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
60
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
61 if !(has('vim_starting') && has('gui_running') && has('gui_win32'))
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
62 " Tell the user what is happening. Not when the GUI is starting
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
63 " though, it would result in a message box.
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
64 echomsg "File is being edited by " . servername
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
65 sleep 2
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
66 endif
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
67 return 'q'
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
68 endif
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
69 endwhile
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
70 return ''
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
71 endfunc
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
72
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
73 " When the plugin is loaded and there is one file name argument: Find another
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
74 " Vim server that is editing this file right now.
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
75 if argc() == 1 && !&modified
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
76 if s:EditElsewhere(expand("%:p")) == 'q'
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
77 quit
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
78 endif
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
79 endif
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
80
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
81 " Setup for handling the situation that an existing swap file is found.
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
82 try
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
83 au! SwapExists * let v:swapchoice = s:EditElsewhere(expand("<afile>:p"))
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
84 catch
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
85 " Without SwapExists we don't do anything for ":edit" commands
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
86 endtry
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
87
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
88 " Function used on the server to make the file visible and possibly execute a
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
89 " command.
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
90 func! EditExisting(fname, command)
1093
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
91 " Get the window number of the file in the current tab page.
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
92 let winnr = bufwinnr(a:fname)
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
93 if winnr <= 0
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
94 " Not found, look in other tab pages.
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
95 let bufnr = bufnr(a:fname)
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
96 for i in range(tabpagenr('$'))
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
97 if index(tabpagebuflist(i + 1), bufnr) >= 0
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
98 " Make this tab page the current one and find the window number.
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
99 exe 'tabnext ' . (i + 1)
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
100 let winnr = bufwinnr(a:fname)
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
101 break;
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
102 endif
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
103 endfor
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
104 endif
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
105
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
106 if winnr > 0
23d23246742e updated for version 7.0-219
vimboss
parents: 856
diff changeset
107 exe winnr . "wincmd w"
1620
73fe8baea242 updated for version 7.2a
vimboss
parents: 1093
diff changeset
108 elseif exists('*fnameescape')
73fe8baea242 updated for version 7.2a
vimboss
parents: 1093
diff changeset
109 exe "split " . fnameescape(a:fname)
589
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
110 else
1620
73fe8baea242 updated for version 7.2a
vimboss
parents: 1093
diff changeset
111 exe "split " . escape(a:fname, " \t\n*?[{`$\\%#'\"|!<")
589
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
112 endif
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
113
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
114 if a:command != ''
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
115 exe "normal " . a:command
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
116 endif
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
117
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
118 redraw
bc49ed25543d updated for version 7.0167
vimboss
parents:
diff changeset
119 endfunc