2
" Author: Srinath Avadhanula <srinath AT fastmail DOT fm>
3
" $Id: remoteOpen.vim 997 2006-03-20 09:45:45Z srinathava $
6
" Often times, an external program needs to open a file in gvim from the
7
" command line. However, it will not know if the file is already opened in a
8
" previous vim session. It is not sufficient to simply specify
10
" gvim --remote-silent <filename>
12
" because this simply opens up <filename> in the first remote gvim session it
13
" sees. This script provides a command RemoteOpen which is meant to be used
14
" from the command line as follows:
16
" gvim -c ":RemoteOpen +<lnum> <filename>"
18
" where <lnum> is the line-number you wish <filename> to open to. What will
19
" happen is that a new gvim will start up and enquire from all previous
20
" sessions if <filename> is already open in any of them. If it is, then it
21
" will edit the file in that session and bring it to the foreground and itself
22
" quit. Otherwise, it will not quit and instead open up the file for editing
25
" This was mainly created to be used with Yap (the dvi previewer in miktex),
26
" so you can specify the program for "inverse search" as specified above.
27
" This ensures that the inverse search uses the correct gvim each time.
29
" Ofcourse, this requires vim with +clientserver. If not, then RemoteOpen just
30
" opens in the present session.
32
" Enclose <args> in single quotes so it can be passed as a function argument.
33
com -nargs=1 RemoteOpen :call RemoteOpen('<args>')
34
com -nargs=? RemoteInsert :call RemoteInsert('<args>')
36
" RemoteOpen: open a file remotely (if possible) {{{
37
" Description: checks all open vim windows to see if this file has been opened
38
" anywhere and if so, opens it there instead of in this session.
39
function! RemoteOpen(arglist)
41
" First construct line number and filename from argument. a:arglist is of
46
if a:arglist =~ '^\s*+\d\+'
47
let linenum = matchstr(a:arglist, '^\s*+\zs\d\+\ze')
48
let filename = matchstr(a:arglist, '^\s*+\d\+\s*\zs.*\ze')
51
let filename = matchstr(a:arglist, '^\s*\zs.*\ze')
53
let filename = escape(filename, ' ')
54
call Tex_Debug("linenum = ".linenum.', filename = '.filename, "ropen")
56
" If there is no clientserver functionality, then just open in the present
58
if !has('clientserver')
59
call Tex_Debug("-clientserver, opening locally and returning", "ropen")
66
" Otherwise, loop through all available servers
67
let servers = serverlist()
68
" If there are no servers, open file locally.
70
call Tex_Debug("no open servers, opening locally", "ropen")
73
let g:Remote_Server = 1
79
let server = s:Strntok(servers, "\n", i)
80
let targetServer = v:servername
83
" Find out if there was any server which was used by remoteOpen before
84
" this. If a new gvim session was ever started via remoteOpen, then
85
" g:Remote_Server will be set.
86
if remote_expr(server, 'exists("g:Remote_Server")')
87
let targetServer = server
90
" Ask each server if that file is being edited by them.
91
let bufnum = remote_expr(server, "bufnr('".filename."')")
94
" ask the server to edit that file and come to the foreground.
95
" set a variable g:Remote_Server to indicate that this server
96
" session has at least one file opened via RemoteOpen
97
let targetServer = server
102
let server = s:Strntok(servers, "\n", i)
105
" If none of the servers have the file open, then open this file in the
106
" first server. This has the advantage if yap tries to make vim open
107
" multiple vims, then at least they will all be opened by the same gvim
109
call remote_send(targetServer,
111
\ ":let g:Remote_Server = 1\<CR>".
112
\ ":drop ".filename."\<CR>".
113
\ ":".linenum."\<CR>zv"
115
call remote_foreground(targetServer)
116
" quit this vim session
117
if v:servername != targetServer
121
" RemoteInsert: inserts a \cite'ation remotely (if possible) {{{
123
function! RemoteInsert(...)
125
let citation = matchstr(argv(0), "\\[InsText('.cite{\\zs.\\{-}\\ze}');\\]")
130
" Otherwise, loop through all available servers
131
let servers = serverlist()
134
let server = s:Strntok(servers, "\n", i)
135
let targetServer = v:servername
138
if remote_expr(server, 'exists("g:Remote_WaitingForCite")')
139
call remote_send(server, citation . "\<CR>")
140
call remote_foreground(server)
141
if v:servername != server
149
let server = s:Strntok(servers, "\n", i)
155
" Strntok: extract the n^th token from a list {{{
156
" example: Strntok('1,23,3', ',', 2) = 23
157
fun! <SID>Strntok(s, tok, n)
158
return matchstr( a:s.a:tok[0], '\v(\zs([^'.a:tok.']*)\ze['.a:tok.']){'.a:n.'}')
163
" vim:ft=vim:ts=4:sw=4:noet:fdm=marker:commentstring=\"\ %s:nowrap