Skip to content
Navigation Menu
{{ message }}
forked from jalvesaq/Vim-R
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrbrowser.vim
More file actions
356 lines (316 loc) · 10.3 KB
/
Copy pathrbrowser.vim
File metadata and controls
356 lines (316 loc) · 10.3 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
" 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")
finish
endif
let b:did_ftplugin = 1
let s:upobcnt = 0
let s:cpo_save = &cpo
set cpo&vim
" Define some buffer variables common to R, Rnoweb, Rmd, Rrst, Rhelp and rdoc:
exe "source " . substitute(expand("<sfile>:h:h"), ' ', '\ ', 'g') . "/R/common_buffer.vim"
setlocal noswapfile
setlocal buftype=nofile
setlocal nowrap
setlocal iskeyword=@,48-57,_,.
setlocal nolist
setlocal nonumber
setlocal norelativenumber
setlocal nocursorline
setlocal nocursorcolumn
setlocal nospell
if !has_key(g:rplugin, "hasmenu")
let g:rplugin.hasmenu = 0
endif
" Popup menu
if !exists("s:hasbrowsermenu")
let s:hasbrowsermenu = 0
endif
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 s:upobcnt
echoerr "OB called twice"
return "OB called twice"
endif
let s:upobcnt = 1
let rplugin_switchedbuf = 0
let s:bufl = execute("buffers")
if s:bufl !~ "Object_Browser"
let s:upobcnt = 0
return "Object_Browser not listed"
endif
if wht == "GlobalEnv"
let fcntt = readfile(g:rplugin.localtmpdir . "/globenv_" . $NVIMR_ID)
else
let fcntt = readfile(g:rplugin.localtmpdir . "/liblist_" . $NVIMR_ID)
endif
if has("nvim")
if nvim_win_is_valid(g:rplugin.ob_winnr)
let obcur = nvim_win_get_cursor(g:rplugin.ob_winnr)
endif
if has("nvim-0.8.0")
call nvim_set_option_value("modifiable", v:true, {'buf': g:rplugin.ob_buf})
else
call nvim_buf_set_option(g:rplugin.ob_buf, "modifiable", v:true)
endif
call nvim_buf_set_lines(g:rplugin.ob_buf, 0, nvim_buf_line_count(g:rplugin.ob_buf), 0, fcntt)
if nvim_win_is_valid(g:rplugin.ob_winnr) && obcur[0] <= len(fcntt)
call nvim_win_set_cursor(g:rplugin.ob_winnr, obcur)
endif
if has("nvim-0.8.0")
call nvim_set_option_value("modifiable", v:false, {'buf': g:rplugin.ob_buf})
else
call nvim_buf_set_option(g:rplugin.ob_buf, "modifiable", v:false)
endif
else
if has_key(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
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
call setline(1, fcntt)
call cursor(curline, curcol)
if bufname("%") =~ "Object_Browser"
setlocal nomodifiable
endif
if rplugin_switchedbuf
exe "sil noautocmd sb " . g:rplugin.curbuf
exe "set switchbuf=" . savesb
endif
endif
let s:upobcnt = 0
endfunction
function! RBrowserDoubleClick()
if line(".") == 2
return
endif
" Toggle view: Objects in the workspace X List of libraries
if line(".") == 1
if g:rplugin.curview == "libraries"
let g:rplugin.curview = "GlobalEnv"
call JobStdin(g:rplugin.jobs["Server"], "31\n")
else
let g:rplugin.curview = "libraries"
call JobStdin(g:rplugin.jobs["Server"], "321\n")
endif
return
endif
" Toggle state of list or data.frame: open X closed
let key = RBrowserGetName()
let curline = getline(".")
if g:rplugin.curview == "GlobalEnv"
if curline =~ "&#.*\t"
call SendToNvimcom("L", key)
elseif curline =~ "\[#.*\t" || curline =~ "\$#.*\t" || curline =~ "<#.*\t" || curline =~ ":#.*\t"
let key = substitute(key, '`', '', 'g')
call JobStdin(g:rplugin.jobs["Server"], "33G" . key . "\n")
else
call g:SendCmdToR("str(" . key . ")")
endif
else
if curline =~ "(#.*\t"
let key = substitute(key, '`', '', 'g')
call AskRDoc(key, RBGetPkgName(), 0)
else
if key =~ ":$" || curline =~ "\[#.*\t" || curline =~ "\$#.*\t" || curline =~ "<#.*\t" || curline =~ ":#.*\t"
call JobStdin(g:rplugin.jobs["Server"], "33L" . key . "\n")
else
call g:SendCmdToR("str(" . key . ")")
endif
endif
endif
endfunction
function! RBrowserRightClick()
if line(".") == 1
return
endif
let key = RBrowserGetName()
if key == ""
return
endif
let line = getline(".")
if line =~ "^ ##"
return
endif
let isfunction = 0
if line =~ "(#.*\t"
let isfunction = 1
endif
if s: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 s: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), "\x09.*", "", "")
let curpos = stridx(line, '[#')
if curpos == -1
let curpos = stridx(line, '$#')
if curpos == -1
let curpos = stridx(line, '<#')
if curpos == -1
let curpos = a:curpos
endif
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
if line =~ ' <#'
let suffix = '@'
else
let suffix = '$'
endif
let thisword = substitute(line, '^.\{-}#', '', '')
if thisword =~ " " || thisword =~ '^[0-9_]' || thisword =~ s:punct
let thisword = '`' . thisword . '`'
endif
let word = thisword . suffix . a:word
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! RBrowserGetName()
let line = getline(".")
if line =~ "^$" || line(".") < 3
return ""
endif
let curpos = stridx(line, "#")
let word = substitute(line, '.\{-}\(.#\)\(.\{-}\)\t.*', '\2', '')
if word =~ ' ' || word =~ '^[0-9]' || word =~ s:punct || word =~ '^' . s:reserved . '$'
let word = '`' . word . '`'
endif
if curpos == 4
" top level object
let word = substitute(word, '\$\[\[', '[[', "g")
if g:rplugin.curview == "libraries"
return word . ':'
else
return word
endif
else
if g:rplugin.curview == "libraries"
if s:isutf8
if curpos == 11
let word = substitute(word, '\$\[\[', '[[', "g")
return word
endif
elseif curpos == 7
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 = 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! OnOBBufUnload()
if g:rplugin.update_glbenv == 0
call SendToNvimcom("N", "OnOBBufUnload")
endif
endfunction
function! PrintListTree()
call JobStdin(g:rplugin.jobs["Server"], "37\n")
endfunction
nnoremap <buffer><silent> <CR> :call RBrowserDoubleClick()<CR>
nnoremap <buffer><silent> <2-LeftMouse> :call RBrowserDoubleClick()<CR>
nnoremap <buffer><silent> <RightMouse> :call RBrowserRightClick()<CR>
call RControlMaps()
setlocal winfixwidth
setlocal bufhidden=wipe
if has("gui_running")
exe "source " . substitute(expand("<sfile>:h:h"), ' ', '\ ', 'g') . "/R/gui_running.vim"
call RControlMenu()
call RBrowserMenu()
endif
au BufEnter <buffer> stopinsert
au BufUnload <buffer> call OnOBBufUnload()
let s:reserved = '\(if\|else\|repeat\|while\|function\|for\|in\|next\|break\|TRUE\|FALSE\|NULL\|Inf\|NaN\|NA\|NA_integer_\|NA_real_\|NA_complex_\|NA_character_\)'
let s:punct = '\(!\|''\|"\|#\|%\|&\|(\|)\|\*\|+\|,\|-\|/\|\\\|:\|;\|<\|=\|>\|?\|@\|[\|/\|]\|\^\|\$\|{\||\|}\|\~\)'
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")
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: sw=4
You can’t perform that action at this time.
