forked from jalvesaq/Vim-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrbrowser.vim
More file actions
357 lines (315 loc) · 9.86 KB
/
rbrowser.vim
File metadata and controls
357 lines (315 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
" Vim filetype plugin file
" Language: R Browser (generated by the Nvim-R)
" Maintainer: Jakson Alves de Aquino <jalvesaq@gmail.com>
" Only do this when not yet done for this buffer
if exists("b:did_ftplugin") || !has("nvim")
finish
endif
let b:did_ftplugin = 1
let g:rplugin_upobcnt = 0
let s:cpo_save = &cpo
set cpo&vim
" Source scripts common to R, Rnoweb, Rhelp and rdoc files:
runtime R/common_global.vim
" Some buffer variables common to R, Rnoweb, Rhelp and rdoc file need be
" defined after the global ones:
runtime R/common_buffer.vim
setlocal noswapfile
setlocal buftype=nofile
setlocal nowrap
setlocal iskeyword=@,48-57,_,.
setlocal nolist
if !exists("g:rplugin_hasmenu")
let g:rplugin_hasmenu = 0
endif
" Popup menu
if !exists("g:rplugin_hasbrowsermenu")
let g:rplugin_hasbrowsermenu = 0
endif
" Current view of the object browser: .GlobalEnv X loaded libraries
let g:rplugin_curview = "GlobalEnv"
function! UpdateOB(what)
if a:what == "both"
let wht = g:rplugin_curview
else
let wht = a:what
endif
if g:rplugin_curview != wht
return "curview != what"
endif
if g:rplugin_upobcnt
echoerr "OB called twice"
return "OB called twice"
endif
let g:rplugin_upobcnt = 1
let rplugin_switchedbuf = 0
if g:R_tmux_ob == 0
redir => s:bufl
silent buffers
redir END
if s:bufl !~ "Object_Browser"
let g:rplugin_upobcnt = 0
return "Object_Browser not listed"
endif
if exists("g:rplugin_curbuf") && g:rplugin_curbuf != "Object_Browser"
let savesb = &switchbuf
set switchbuf=useopen,usetab
sil noautocmd sb Object_Browser
let rplugin_switchedbuf = 1
endif
endif
setlocal modifiable
let curline = line(".")
let curcol = col(".")
if !exists("curline")
let curline = 3
endif
if !exists("curcol")
let curcol = 1
endif
let save_unnamed_reg = @@
sil normal! ggdG
let @@ = save_unnamed_reg
if wht == "GlobalEnv"
let fcntt = readfile(g:rplugin_tmpdir . "/globenv_" . $NVIMR_ID)
else
let fcntt = readfile(g:rplugin_tmpdir . "/liblist_" . $NVIMR_ID)
endif
call setline(1, fcntt)
call cursor(curline, curcol)
if bufname("%") =~ "Object_Browser" || b:rplugin_extern_ob
setlocal nomodifiable
endif
redraw
if rplugin_switchedbuf
exe "sil noautocmd sb " . g:rplugin_curbuf
exe "set switchbuf=" . savesb
endif
let g:rplugin_upobcnt = 0
return "End of UpdateOB()"
endfunction
function! RBrowserDoubleClick()
" Toggle view: Objects in the workspace X List of libraries
if line(".") == 1
if g:rplugin_curview == "libraries"
let g:rplugin_curview = "GlobalEnv"
call UpdateOB("GlobalEnv")
else
let g:rplugin_curview = "libraries"
call UpdateOB("libraries")
endif
return
endif
" Toggle state of list or data.frame: open X closed
let key = RBrowserGetName(0, 1)
if g:rplugin_curview == "GlobalEnv"
call SendToNvimcom("\006" . key)
else
let key = substitute(key, '`', '', "g")
if key !~ "^package:"
let key = "package:" . RBGetPkgName() . '-' . key
endif
call SendToNvimcom("\006" . key)
endif
endfunction
function! RBrowserRightClick()
if line(".") == 1
return
endif
let key = RBrowserGetName(1, 0)
if key == ""
return
endif
let line = getline(".")
if line =~ "^ ##"
return
endif
let isfunction = 0
if line =~ "(#.*\t"
let isfunction = 1
endif
if g:rplugin_hasbrowsermenu == 1
aunmenu ]RBrowser
endif
let key = substitute(key, '\.', '\\.', "g")
let key = substitute(key, ' ', '\\ ', "g")
exe 'amenu ]RBrowser.summary('. key . ') :call RAction("summary")<CR>'
exe 'amenu ]RBrowser.str('. key . ') :call RAction("str")<CR>'
exe 'amenu ]RBrowser.names('. key . ') :call RAction("names")<CR>'
exe 'amenu ]RBrowser.plot('. key . ') :call RAction("plot")<CR>'
exe 'amenu ]RBrowser.print(' . key . ') :call RAction("print")<CR>'
amenu ]RBrowser.-sep01- <nul>
exe 'amenu ]RBrowser.example('. key . ') :call RAction("example")<CR>'
exe 'amenu ]RBrowser.help('. key . ') :call RAction("help")<CR>'
if isfunction
exe 'amenu ]RBrowser.args('. key . ') :call RAction("args")<CR>'
endif
popup ]RBrowser
let g:rplugin_hasbrowsermenu = 1
endfunction
function! RBGetPkgName()
let lnum = line(".")
while lnum > 0
let line = getline(lnum)
if line =~ '.*##[0-9a-zA-Z\.]*\t'
let line = substitute(line, '.*##\(.*\)\t', '\1', "")
return line
endif
let lnum -= 1
endwhile
return ""
endfunction
function! RBrowserFindParent(word, curline, curpos)
let curline = a:curline
let curpos = a:curpos
while curline > 1 && curpos >= a:curpos
let curline -= 1
let line = substitute(getline(curline), " .*", "", "")
let curpos = stridx(line, '[#')
if curpos == -1
let curpos = stridx(line, '<#')
if curpos == -1
let curpos = a:curpos
endif
endif
endwhile
if g:rplugin_curview == "GlobalEnv"
let spacelimit = 3
else
if s:isutf8
let spacelimit = 10
else
let spacelimit = 6
endif
endif
if curline > 1
let line = substitute(line, '^.\{-}\(.\)#', '\1#', "")
let line = substitute(line, '^ *', '', "")
if line =~ " " || line =~ '^.#[0-9]'
let line = substitute(line, '\(.\)#\(.*\)$', '\1#`\2`', "")
endif
if line =~ '<#'
let word = substitute(line, '.*<#', "", "") . '@' . a:word
else
let word = substitute(line, '.*\[#', "", "") . '$' . a:word
endif
if curpos != spacelimit
let word = RBrowserFindParent(word, line("."), curpos)
endif
return word
else
" Didn't find the parent: should never happen.
let msg = "R-plugin Error: " . a:word . ":" . curline
echoerr msg
endif
return ""
endfunction
function! RBrowserCleanTailTick(word, cleantail, cleantick)
let nword = a:word
if a:cleantick
let nword = substitute(nword, "`", "", "g")
endif
if a:cleantail
let nword = substitute(nword, '[\$@]$', '', '')
let nword = substitute(nword, '[\$@]`$', '`', '')
endif
return nword
endfunction
function! RBrowserGetName(cleantail, cleantick)
let line = getline(".")
if line =~ "^$"
return ""
endif
let curpos = stridx(line, "#")
let word = substitute(line, '.\{-}\(.#\)\(.\{-}\)\t.*', '\2\1', '')
let word = substitute(word, '\[#$', '$', '')
let word = substitute(word, '<#$', '@', '')
let word = substitute(word, '.#$', '', '')
if word =~ ' ' || word =~ '^[0-9]'
let word = '`' . word . '`'
endif
if (g:rplugin_curview == "GlobalEnv" && curpos == 4) || (g:rplugin_curview == "libraries" && curpos == 3)
" top level object
let word = substitute(word, '\$\[\[', '[[', "g")
let word = RBrowserCleanTailTick(word, a:cleantail, a:cleantick)
if g:rplugin_curview == "libraries"
return "package:" . substitute(word, "#", "", "")
else
return word
endif
else
if g:rplugin_curview == "libraries"
if s:isutf8
if curpos == 11
let word = RBrowserCleanTailTick(word, a:cleantail, a:cleantick)
let word = substitute(word, '\$\[\[', '[[', "g")
return word
endif
elseif curpos == 7
let word = RBrowserCleanTailTick(word, a:cleantail, a:cleantick)
let word = substitute(word, '\$\[\[', '[[', "g")
return word
endif
endif
if curpos > 4
" Find the parent data.frame or list
let word = RBrowserFindParent(word, line("."), curpos - 1)
let word = RBrowserCleanTailTick(word, a:cleantail, a:cleantick)
let word = substitute(word, '\$\[\[', '[[', "g")
return word
else
" Wrong object name delimiter: should never happen.
let msg = "R-plugin Error: (curpos = " . curpos . ") " . word
echoerr msg
return ""
endif
endif
endfunction
function! SourceObjBrLines()
exe "source " . substitute(g:rplugin_tmpdir, ' ', '\\ ', 'g') . "/objbrowserInit"
endfunction
function! OnOBBufUnload()
call SendToNvimcom("\004Stop updating info [OB BufUnload].")
if b:rplugin_extern_ob
call SendToOtherNvim("let g:rplugin_ob_port = 0")
endif
endfunction
" Called by Neovim editor in another Tmux pane
function! ExternOBQuit()
call OnOBBufUnload()
quit
endfunction
nmap <buffer><silent> <CR> :call RBrowserDoubleClick()<CR>
nmap <buffer><silent> <2-LeftMouse> :call RBrowserDoubleClick()<CR>
nmap <buffer><silent> <RightMouse> :call RBrowserRightClick()<CR>
call RControlMaps()
setlocal winfixwidth
setlocal bufhidden=wipe
if has("gui_running")
runtime R/gui_running.vim
call RControlMenu()
call RBrowserMenu()
endif
au BufEnter <buffer> stopinsert
au BufUnload <buffer> call OnOBBufUnload()
if g:R_tmux_ob
" Fix problems caused by some plugins
if exists("g:loaded_surround") && mapcheck("ds", "n") != ""
nunmap ds
endif
if exists("g:loaded_showmarks ")
autocmd! ShowMarks
endif
endif
let s:envstring = tolower($LC_MESSAGES . $LC_ALL . $LANG)
if s:envstring =~ "utf-8" || s:envstring =~ "utf8"
let s:isutf8 = 1
else
let s:isutf8 = 0
endif
unlet s:envstring
call setline(1, ".GlobalEnv | Libraries")
call RSourceOtherScripts()
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: sw=4