Wednesday, May 30, 2018

Re: conditional mapping of , depending on whether plugin exists

Hi,

----- Mail original -----
> I use https://github.com/terryma/vim-smooth-scroll and so I bind
> <C-d> & <C-u> to ":call smooth_scroll#down(&scroll, 5, 2)<CR>" &
> ":call smooth_scroll#up(&scroll, 5, 2)<CR>" respectively. However I
> would like <C-d> & <C-u> to default to their original behaviour if
> the smooth scroll plugin isn't loaded.
>
> I've tried this:
>
> if exists('g:loaded_smoothscroll')
> noremap <silent> <C-u> :call smooth_scroll#up(&scroll, 5, 2)<CR>
> noremap <silent> <C-d> :call smooth_scroll#down(&scroll, 5, 2)<CR>
> endif
>
> but strangely it ignores the mapping if I load it like that. That
> means the exists('g:loaded_smoothscroll') check failed, but doing an
> :echo exists('g:loaded_smoothscroll') does indeed return 1. I'm
> guessing at the moment of sourcing the vimrc g:loaded_smoothscroll
> was 0, then it got set to 1 after after the mapping got read. How do
> I get around that?
>
> If it helps I actually put the variable let g:loaded_smoothscroll = 1
> into the plugin/smooth_scroll.vim file (which did not exist in the
> author's original version, I had to fork it myself). Is that the
> correct place to put the g:loaded_smoothscroll variable?

No, it's not.
Autoload plugins aren't expected to have an anti-inclusion guard. Beside, plain plugins are loaded **after** the .vimrc file has been sourced.

What you can do instead is to check whether the file exists with

if !empty(globpath(&rtp, 'autoload/smooth_scroll.vim'))
nnoremap ...


This needs to be done **after** your plugin manager has kicked in to fill the `&rtp` option.


HTH,

--
Luc Hermitte

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: