1

in my ftplugin, I have code:

  if executable('fprettify')
    au BufWritePre <buffer> :silent %!fprettify --silent
  else
    :let choice=confirm("fprettify doesn't exists! Install fprettify?", "&Yes\n&No(use fallback)")
    if choice == 1
      :execute ':!pip3 install fprettify --user -q'
      au BufWritePre <buffer> :silent %!fprettify --silent
    elseif choice == 2
      let g:VimF90Linter = 1
    endif

Now, if users have g:VimF90Linter = 2 in ~.vimrc, but does not have fprettify, he may still choose 1. In that case, the prompt is appearing everytime he opens a fortran file. How can I override this behaviour, i.e. the prompt should disappear and irrespective of value of g:VimF90Linter set in ~/.vimrc, it should be 1 as set in the plugin.

Is this possible?

3
  • 1
    There is nothing in your snippet that depends on g:VimF90Linter. – Maxim Kim Jul 8 '20 at 7:59
  • Er, I wouldnt recommend trying to force your settings on users (though I’m not sure if that’s a vim global or a global unique to your plugin) – D. Ben Knoble Jul 8 '20 at 12:09
  • @D.BenKnoble: it was global unique to my plugin, now modified to b: anyway. And I am not forcing my setting to user. That is why the option is. – BaRud Jul 8 '20 at 19:10
1

I'm sorry I don't understand the specifics of your question.

At best I guess you want to cache the previous user choice, that could have been done in the ftplugin or the user .vimrc.

get(g:, 'varname', defaultvalue) may help here.

May be something like the following?

function! s:register_linter() abort
  augroup FortranLinter
    au!
    au BufWritePre <buffer> :silent %!fprettify --silent
  aug END
  let g:VimF90Linter = get(g:, 'VimF90Linter', 'use')
endfunction

function! s:install_on_the_fly() abort
  :!pip3 install fprettify --user -q
  " Make sure to not ask again if pip fails
  if ! v:shell_error
    let g:VimF90Linter = 'ignore'
  endif
  return ! v:shell_error " returns 1 iff pip succeeds
endfunction

let linter = get(g:, 'VimF90Linter', '')
if executable('fprettify') && linter != 'ignore'
  call s:register_linter()
elseif linter == 'install on the fly' " or may be not empty and != 'ignore'?
  let dummy = s:install_on_the_fly() && s:register_linter()
elseif empty(linter)
  let choice = confirm(....)
  if choice == 1
    let dummy = s:install_on_the_fly() && s:register_linter()
  else
    let g:VimF90Linter = 'ignore'
  endif
endif

IMO, as with the fizz-buzz test, you'll have to be explicit and quite possibly state all the possible cases and subcases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.