Skip to content

Commit cf2d8de

Browse files
committed
patch 7.4.1674
Problem: The editexisting plugin has to be copied or sourced to be used. Solution: Turn it into a package.
1 parent 2946d02 commit cf2d8de

4 files changed

Lines changed: 128 additions & 11 deletions

File tree

Filelist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ RT_ALL = \
526526
runtime/pack/dist/opt/dvorak/plugin/dvorak.vim \
527527
runtime/pack/dist/opt/dvorak/dvorak/enable.vim \
528528
runtime/pack/dist/opt/dvorak/dvorak/disable.vim \
529+
runtime/pack/dist/opt/editexisting/plugin/editexisting.vim \
529530
runtime/pack/dist/opt/justify/plugin/justify.vim \
530531
runtime/pack/dist/opt/matchit/plugin/matchit.vim \
531532
runtime/pack/dist/opt/matchit/doc/matchit.txt \

runtime/macros/README.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ shellmenu.vim menus for editing shell scripts in the GUI version
1717

1818
swapmous.vim swap left and right mouse buttons
1919

20-
editexisting.vim when editing a file that is already edited with
21-
another Vim instance
20+
editexisting.vim
2221

2322
This one is only for Unix.
2423
file_select.vim macros that make a handy file selector
2524

26-
The Dvorak support has been moved to an optional package. To load it put this
27-
line in your vimrc file:
28-
packadd! dvorak
2925

30-
The support for justifying test has been moved to an optional package. To
31-
load it put this line in your vimrc file:
32-
packadd! justify
26+
The following have been moved to an optional package. Add the command to your
27+
vimrc file to use the package:
3328

34-
The matchit plugin has been moved to an optional package. To load it put this
35-
line in your vimrc file:
36-
packadd! matchit
29+
packadd! dvorak Dvorak keyboard support; adds mappings
30+
31+
packadd! editexisting when editing a file that is already edited with
32+
another Vim instance, go to that Vim instance
33+
34+
packadd! justify justifying text.
35+
36+
packadd! matchit makes the % command work better
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
" Vim Plugin: Edit the file with an existing Vim if possible
2+
" Maintainer: Bram Moolenaar
3+
" Last Change: 2016 Mar 28
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+
if remote_expr(servername, "bufloaded('" . fname_esc . "')")
39+
" Yes, bring it to the foreground.
40+
if has("win32")
41+
call remote_foreground(servername)
42+
endif
43+
call remote_expr(servername, "foreground()")
44+
45+
if remote_expr(servername, "exists('*EditExisting')")
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+
endif
55+
56+
if !(has('vim_starting') && has('gui_running') && has('gui_win32'))
57+
" Tell the user what is happening. Not when the GUI is starting
58+
" though, it would result in a message box.
59+
echomsg "File is being edited by " . servername
60+
sleep 2
61+
endif
62+
return 'q'
63+
endif
64+
endwhile
65+
return ''
66+
endfunc
67+
68+
" When the plugin is loaded and there is one file name argument: Find another
69+
" Vim server that is editing this file right now.
70+
if argc() == 1 && !&modified
71+
if s:EditElsewhere(expand("%:p")) == 'q'
72+
quit
73+
endif
74+
endif
75+
76+
" Setup for handling the situation that an existing swap file is found.
77+
try
78+
au! SwapExists * let v:swapchoice = s:EditElsewhere(expand("<afile>:p"))
79+
catch
80+
" Without SwapExists we don't do anything for ":edit" commands
81+
endtry
82+
83+
" Function used on the server to make the file visible and possibly execute a
84+
" command.
85+
func! EditExisting(fname, command)
86+
" Get the window number of the file in the current tab page.
87+
let winnr = bufwinnr(a:fname)
88+
if winnr <= 0
89+
" Not found, look in other tab pages.
90+
let bufnr = bufnr(a:fname)
91+
for i in range(tabpagenr('$'))
92+
if index(tabpagebuflist(i + 1), bufnr) >= 0
93+
" Make this tab page the current one and find the window number.
94+
exe 'tabnext ' . (i + 1)
95+
let winnr = bufwinnr(a:fname)
96+
break
97+
endif
98+
endfor
99+
endif
100+
101+
if winnr > 0
102+
exe winnr . "wincmd w"
103+
elseif exists('*fnameescape')
104+
exe "split " . fnameescape(a:fname)
105+
else
106+
exe "split " . escape(a:fname, " \t\n*?[{`$\\%#'\"|!<")
107+
endif
108+
109+
if a:command != ''
110+
exe "normal! " . a:command
111+
endif
112+
113+
redraw
114+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,8 @@ static char *(features[]) =
748748

749749
static int included_patches[] =
750750
{ /* Add new patch number below this line */
751+
/**/
752+
1674,
751753
/**/
752754
1673,
753755
/**/

0 commit comments

Comments
 (0)