Skip to content

Commit

Permalink
RCreateMaps: Create plug mappings unconditionally (fixes #470, fixes #…
Browse files Browse the repository at this point in the history
…494) (#472)

* RCreateMaps: Create plug mappings unconditionally

Previously, Plug mappings were only created if they were already in use
- i.e., if someone had mapped them to something in their .vimrc. The
problem with that is if someone wants to create buffer-local maps to an
nvim-R <Plug> mapping, they cannot do so from .vimrc. They must do so
once the buffer is loaded - ie within vim, after loading .vimrc.
This is usually done with an filetype autocmd.

See #470 for more

* Update mapping documentation to suggest buffer-local mappings

* Link to option to only use custom user settings

* Clarify purpose of RStop

The word 'stop' is a bit misleading, because it suggests killing the R
process totally. That doesn't always happen, it just interrupts.

* Fix filetype autocmd in documentation

* Flip check on user_maps_only

* Restore check to not map default if custom mapping exists

* Fix delay when RSendLine plug mapping is used

Co-authored-by: bluedrink9 <[email protected]>
  • Loading branch information
BlueDrink9 and bluedrink9 authored Oct 28, 2020
1 parent 50497c7 commit fe5edb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
19 changes: 8 additions & 11 deletions R/common_global.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2946,23 +2946,20 @@ function RCreateMaps(type, plug, combo, target)
let il = 'a'
endif
if a:type =~ "n"
if hasmapto('<Plug>' . a:plug, "n")
exec 'noremap <buffer><silent> <Plug>' . a:plug . ' ' . tg
elseif g:R_user_maps_only == 0
exec 'noremap <buffer><silent> <Plug>' . a:plug . ' ' . tg
if g:R_user_maps_only != 1 && !hasmapto('<Plug>' . a:plug, "n")
exec 'noremap <buffer><silent> <LocalLeader>' . a:combo . ' ' . tg
endif
endif
if a:type =~ "v"
if hasmapto('<Plug>' . a:plug, "v")
exec 'vnoremap <buffer><silent> <Plug>' . a:plug . ' <Esc>' . tg
elseif g:R_user_maps_only == 0
exec 'vnoremap <buffer><silent> <Plug>' . a:plug . ' <Esc>' . tg
if g:R_user_maps_only != 1 && !hasmapto('<Plug>' . a:plug, "v")
exec 'vnoremap <buffer><silent> <LocalLeader>' . a:combo . ' <Esc>' . tg
endif
endif
if g:R_insert_mode_cmds == 1 && a:type =~ "i"
if hasmapto('<Plug>' . a:plug, "i")
exec 'inoremap <buffer><silent> <Plug>' . a:plug . ' <Esc>' . tg . il
elseif g:R_user_maps_only == 0
exec 'inoremap <buffer><silent> <Plug>' . a:plug . ' <Esc>' . tg . il
if g:R_user_maps_only != 1 && !hasmapto('<Plug>' . a:plug, "i")
exec 'inoremap <buffer><silent> <LocalLeader>' . a:combo . ' <Esc>' . tg . il
endif
endif
Expand Down Expand Up @@ -3117,8 +3114,8 @@ function RCreateSendMaps()
"-------------------------------------
call RCreateMaps('ni', 'RSendLine', 'l', ':call SendLineToR("stay")')
call RCreateMaps('ni0', 'RDSendLine', 'd', ':call SendLineToR("down")')
call RCreateMaps('ni0', 'RDSendLineAndInsertOutput', 'o', ':call SendLineToRAndInsertOutput()')
call RCreateMaps('v', 'RDSendLineAndInsertOutput', 'o', ':call RWarningMsg("This command does not work over a selection of lines.")')
call RCreateMaps('ni0', '(RDSendLineAndInsertOutput)', 'o', ':call SendLineToRAndInsertOutput()')
call RCreateMaps('v', '(RDSendLineAndInsertOutput)', 'o', ':call RWarningMsg("This command does not work over a selection of lines.")')
call RCreateMaps('i', 'RSendLAndOpenNewOne', 'q', ':call SendLineToR("newline")')
call RCreateMaps('ni.', 'RSendMotion', 'm', ':set opfunc=SendMotionToR<CR>g@')
call RCreateMaps('n', 'RNLeftPart', 'r<left>', ':call RSendPartOfLine("left", 0)')
Expand Down
24 changes: 16 additions & 8 deletions doc/Nvim-R.txt
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ Start/Close
. Start R (custom) \rc
--------------------------------------------------------
. Close R (no save) \rq
. Stop R :RStop
. Stop (interrupt) R :RStop
-----------------------------------------------------------

Send
Expand Down Expand Up @@ -2419,15 +2419,23 @@ maps for most functions because the way the function is called is different in
each Vim mode. Thus, key bindings must be made for Normal mode, Insert mode,
and Visual mode.

To customize a key binding you should put in your |vimrc| something like:
To customize a key binding you could put something like this in your |vimrc|:
>
nmap <LocalLeader>sr <Plug>RStart
imap <LocalLeader>sr <Plug>RStart
vmap <LocalLeader>sr <Plug>RStart
function! s:customNvimRMappings()
nmap <buffer> <Leader>sr <Plug>RStart
imap <buffer> <Leader>sr <Plug>RStart
vmap <buffer> <Leader>sr <Plug>RStart
endfunction
augroup myNvimR
au!
autocmd filetype r call s:customNvimRMappings()
augroup end
<
The above example shows how to change key binding used to start R from
<LocalLeader>rf to <LocalLeader>sr. After changing the maps in your |vimrc|,
you have to restart Vim.
The above example shows how to add a key binding to start R for <Leader>sr
for just r buffers. The default <LocalLeader>rf still exists. After changing
the maps in your |vimrc|, you have to restart Vim. If you want to remove the
default mappings, you can set |R_user_maps_only| .

Only the custom key bindings for Normal mode are shown in Vim's menu, but
you can type |:map| to see the complete list of current mappings, and below is
Expand Down

0 comments on commit fe5edb9

Please sign in to comment.