~spacexplorer/+junk/myenv

« back to all changes in this revision

Viewing changes to vim/vim/plugin/remoteOpen.vim

  • Committer: Kim Allamandola
  • Date: 2011-05-02 05:39:17 UTC
  • Revision ID: spacexplorer@gmail.com-20110502053917-x0yl2lr9ri4yskr2
InitĀ import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
" File: remoteOpen.vim
 
2
" Author: Srinath Avadhanula <srinath AT fastmail DOT fm>
 
3
" $Id: remoteOpen.vim 997 2006-03-20 09:45:45Z srinathava $
 
4
 
5
" Description:
 
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 
 
9
"
 
10
" gvim --remote-silent <filename> 
 
11
"
 
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:
 
15
"
 
16
" gvim -c ":RemoteOpen +<lnum> <filename>"
 
17
"
 
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
 
23
" at <lnum>.
 
24
"
 
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.
 
28
"
 
29
" Ofcourse, this requires vim with +clientserver. If not, then RemoteOpen just
 
30
" opens in the present session.
 
31
 
 
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>')
 
35
 
 
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)
 
40
 
 
41
        " First construct line number and filename from argument. a:arglist is of
 
42
        " the form:
 
43
        "    +10 c:\path\to\file
 
44
        " or just
 
45
        "        c:\path\to\file
 
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')
 
49
        else
 
50
                let linenum = 1
 
51
                let filename = matchstr(a:arglist, '^\s*\zs.*\ze')
 
52
        endif
 
53
        let filename = escape(filename, ' ')
 
54
        call Tex_Debug("linenum = ".linenum.', filename = '.filename, "ropen")
 
55
 
 
56
        " If there is no clientserver functionality, then just open in the present
 
57
        " session and return
 
58
        if !has('clientserver')
 
59
                call Tex_Debug("-clientserver, opening locally and returning", "ropen")
 
60
                exec "e ".filename
 
61
                exec linenum
 
62
                normal! zv
 
63
                return
 
64
        endif
 
65
 
 
66
        " Otherwise, loop through all available servers
 
67
        let servers = serverlist()
 
68
        " If there are no servers, open file locally.
 
69
        if servers == ''
 
70
                call Tex_Debug("no open servers, opening locally", "ropen")
 
71
                exec "e ".filename
 
72
                exec linenum
 
73
                let g:Remote_Server = 1
 
74
                normal! zv
 
75
                return
 
76
        endif
 
77
 
 
78
        let i = 1
 
79
        let server = s:Strntok(servers, "\n", i) 
 
80
        let targetServer = v:servername
 
81
 
 
82
        while server != ''
 
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
 
88
                endif
 
89
 
 
90
                " Ask each server if that file is being edited by them.
 
91
                let bufnum = remote_expr(server, "bufnr('".filename."')")
 
92
                " If it is...
 
93
                if bufnum != -1
 
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
 
98
                        break
 
99
                end
 
100
                
 
101
                let i = i + 1
 
102
                let server = s:Strntok(servers, "\n", i) 
 
103
        endwhile
 
104
 
 
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
 
108
        " server.
 
109
        call remote_send(targetServer, 
 
110
                \ "\<C-\>\<C-n>".
 
111
                \ ":let g:Remote_Server = 1\<CR>".
 
112
                \ ":drop ".filename."\<CR>".
 
113
                \ ":".linenum."\<CR>zv"
 
114
                \ )
 
115
        call remote_foreground(targetServer)
 
116
        " quit this vim session
 
117
        if v:servername != targetServer
 
118
                q
 
119
        endif
 
120
endfunction " }}}
 
121
" RemoteInsert: inserts a \cite'ation remotely (if possible) {{{
 
122
" Description:
 
123
function! RemoteInsert(...)
 
124
 
 
125
        let citation =  matchstr(argv(0), "\\[InsText('.cite{\\zs.\\{-}\\ze}');\\]")
 
126
        if citation == ""
 
127
                q
 
128
        endif
 
129
 
 
130
        " Otherwise, loop through all available servers
 
131
        let servers = serverlist()
 
132
 
 
133
        let i = 1
 
134
        let server = s:Strntok(servers, "\n", i) 
 
135
        let targetServer = v:servername
 
136
 
 
137
        while server != ''
 
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
 
142
                                q
 
143
                        else
 
144
                                return
 
145
                        endif
 
146
                endif
 
147
 
 
148
                let i = i + 1
 
149
                let server = s:Strntok(servers, "\n", i) 
 
150
        endwhile
 
151
 
 
152
        q
 
153
 
 
154
endfunction " }}}
 
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.'}')
 
159
endfun
 
160
 
 
161
" }}}
 
162
 
 
163
" vim:ft=vim:ts=4:sw=4:noet:fdm=marker:commentstring=\"\ %s:nowrap