On Mon, Sep 25, 2006 at 12:58:51PM +0000, Yakov Lerner wrote:
> On 9/25/06, Marius Roets <[EMAIL PROTECTED]> wrote:
> >I have a in my plsql.vim filetype plugin a lot of abbreviations to the
> >effect of :
> >iabbrev <buffer> then THEN
> >iabbrev <buffer> else ELSE
> >... So I was wondering if there was a
> >way where I could turn a set of abbreviations on and off, on the fly,
> >without reloading the buffer.
> 
> How about this:
> 
> function! MyAbbrevs()
>   iabbrev <buffer> then THEN
>   iabbrev <buffer> else ELSE
>   let g:myabbrev_set=1
> endfunc
> 
> function! UnsetMyAbbrev()
>   iunabbrev <buffer>then
>   iunabbrev <buffer>else
>   let g:myabbrev_set=0
> endfunc
> 
> function! ToggleMyAbbrev()
>   if exists("g:myabbrev_set") && g:myabbrev_set
>        call UnsetMyAbbrev()
>   else
>        call MyAbbrevs()
>   endif
> endfunc
> 
> call MyAbbrevs() " initial setting
> nmap <F5> :call ToggleMyAbbrev()<cr>
> 
> Yakov

     You forgot to set g:myabbrev_set to 0 or 1 in the ToggleMyAbbrev()
function.  Easily fixed.

     Here is another way to do it, using the new Dictionary type
introduced in vim 7.0.  I have not tested this!  I also use a single
function, with an argument, instead of three, and I think it makes more
sense to use a buffer-local variable to keep track of the state.  This
version has the advantage that if you add an abbreviation, you do not have to 
remember to add the unabbreviation in a different place.

let s:abbs = {'then': 'THEN', 'else': 'ELSE'}

" Set action to 's' for set, 'u' for unset, or 't' for toggle.
fun! MyAbbrevs(action)
  if (s:action == 's') ||
        \ (s:action == 't' && exists("b:myabbrev_set") && b:myabbrev_set)
    for [lhs, rhs] in items(s:abbs)
      execute 'iabbrev <buffer>' lhs rhs
    endfor
    let b:myabbrev_set = 1
    return 0
  elseif (s:action == 'u' || s:action == 't')
    for lhs in keys(s:abbs)
      execute 'iunabbrev <buffer>' lhs
    endfor
    let b:myabbrev_set = 0
    return 0
  else  " unrecognized parameter
    return 1
  endif
endfun

HTH                                     --Benji Fisher

Reply via email to