Skip to content

Commit

Permalink
Use nvim_ functions
Browse files Browse the repository at this point in the history
- No need of switching buffers for immediately updating the syntax
  highlighting of all buffers. This fix a very old issue:
  jcfaria/Vim-R-plugin#148

- No need of switching to the Object Browser buffer to update it.

- No need of switching to R Console buffer to move the cursor to its
  last line when R_auto_scroll = 1.
  • Loading branch information
jalvesaq committed Apr 9, 2019
1 parent 485308d commit a1257b0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 55 deletions.
2 changes: 1 addition & 1 deletion R/common_buffer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ if !exists("b:rplugin_new_libs")
let b:rplugin_new_libs = 0
endif
" When using as a global plugin for non R files, RCheckLibList will not exist
if exists("*RCheckLibList")
if exists("*RCheckLibList") && !exists("*nvim_buf_set_option")
autocmd BufEnter <buffer> call RCheckLibList()
endif

Expand Down
2 changes: 2 additions & 0 deletions R/common_global.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,8 @@ function StartObjBrowser()
sil exe 'resize ' . g:R_objbr_h
endif
sil set filetype=rbrowser
let g:rplugin.ob_winnr = win_getid()
let g:rplugin.ob_buf = nvim_win_get_buf(g:rplugin.ob_winnr)

" Inheritance of some local variables
let b:objbrtitle = g:tmp_objbrtitle
Expand Down
16 changes: 9 additions & 7 deletions R/functions.vim
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,16 @@ function FillRLibList()
endfor
call delete(g:rplugin.tmpdir . "/libnames_" . $NVIMR_ID)
endif
" Now we need to update the syntax in all R files. There should be a
" better solution than setting a flag to let other buffers know that they
" also need to update the syntax on CursorMoved event:
" https://github.com/neovim/neovim/issues/901
if !exists("g:R_hi_fun") || g:R_hi_fun != 0
let s:new_libs = len(g:rplugin.loaded_libs)
silent exe 'set syntax=' . &syntax
redraw
if exists("*nvim_buf_set_option")
for bId in nvim_list_bufs()
call nvim_buf_set_option(bId, "syntax", nvim_buf_get_option(bId, "syntax"))
endfor
else
let s:new_libs = len(g:rplugin.loaded_libs)
silent exe 'set syntax=' . &syntax
redraw
endif
endif
let b:rplugin_new_libs = s:new_libs
call CheckRGlobalEnv()
Expand Down
36 changes: 21 additions & 15 deletions R/nvimbuffer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,32 @@ function SendCmdToR_Buffer(...)
call SendToNvimcom("\x08" . $NVIMR_ID . "options(width=" . Rwidth . ")")
sleep 10m
endif
" Scroll issue in Neovim after R Console window is resized...
endif
endif

if g:R_auto_scroll && cmd !~ '^quit('
let sbopt = &switchbuf
set switchbuf=useopen,usetab
let curtab = tabpagenr()
let isnormal = mode() ==# 'n'
let curwin = winnr()
exe 'sb ' . g:rplugin.R_bufname
call cursor('$', 1)
if tabpagenr() != curtab
exe 'normal! ' . curtab . 'gt'
endif
exe curwin . 'wincmd w'
if isnormal
stopinsert
if exists("*nvim_win_set_cursor")
" These functions exist only in Neovim >= 0.4.0:
call nvim_win_set_cursor(g:rplugin.R_winnr, [nvim_buf_line_count(nvim_win_get_buf(g:rplugin.R_winnr)), 0])
else
" TODO: Delete this code and update the documentation when
" everyone is using Neovim >= 0.4.0 (released on 2019-04-08)
let sbopt = &switchbuf
set switchbuf=useopen,usetab
let curtab = tabpagenr()
let isnormal = mode() ==# 'n'
let curwin = winnr()
exe 'sb ' . g:rplugin.R_bufname
call cursor('$', 1)
if tabpagenr() != curtab
exe 'normal! ' . curtab . 'gt'
endif
exe curwin . 'wincmd w'
if isnormal
stopinsert
endif
exe 'set switchbuf=' . sbopt
endif
exe 'set switchbuf=' . sbopt
endif

if !(a:0 == 2 && a:2 == 0)
Expand Down
14 changes: 8 additions & 6 deletions doc/Nvim-R.txt
Original file line number Diff line number Diff line change
Expand Up @@ -978,12 +978,14 @@ Neovim stops automatically scrolling the R Console if you move the cursor to
any line that is not the last one. This emulates the behavior of other
terminal emulator, but considering that (1) R only outputs something after we
send a command to it, and (2) when we send a command to R, we want to see its
output, Nvim-R will enter the R Console and move the cursor to its last line
before sending commands to it, forcing the auto scrolling of the terminal.
This will trigger an auto command that relies on the cursor moving to the
terminal window. If you have defined such auto command, you will may want to
put the line below in your |init.vim| to disable the auto scrolling of the
terminal:
output, Nvim-R will move the cursor to the last line of R Console before
sending a command to it, forcing the auto scrolling of the terminal.

If you are using Neovim < 0.4.0, Nvim-R will enter the R Console to be able to
move the cursor to its last line. This will trigger an auto command that
relies on the cursor moving to the terminal window. If you have defined such
auto command, you will may want to put the line below in your |init.vim| to
disable the auto scrolling of the terminal:
>
let R_auto_scroll = 0
<
Expand Down
61 changes: 35 additions & 26 deletions ftplugin/rbrowser.vim
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,47 @@ function! UpdateOB(what)
let s: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

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"
setlocal nomodifiable
endif
if rplugin_switchedbuf
exe "sil noautocmd sb " . g:rplugin.curbuf
exe "set switchbuf=" . savesb
if exists("*nvim_buf_set_lines")
let obcur = nvim_win_get_cursor(g:rplugin.ob_winnr)
call nvim_buf_set_option(g:rplugin.ob_buf, "modifiable", v:true)
call nvim_buf_set_lines(g:rplugin.ob_buf, 0, nvim_buf_line_count(g:rplugin.ob_buf), 0, fcntt)
call nvim_win_set_cursor(g:rplugin.ob_winnr, obcur)
call nvim_buf_set_option(g:rplugin.ob_buf, "modifiable", v:false)
else
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

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
return "End of UpdateOB()"
Expand Down

0 comments on commit a1257b0

Please sign in to comment.