[prev in list] [next in list] [prev in thread] [next in thread] 

List:       vim
Subject:    Re: abbreviations NOT used in source comments
From:       Hari Krishna Dara <hari_vim () yahoo ! com>
Date:       2003-04-30 21:55:46
[Download RAW message or body]



On Wed, 30 Apr 2003 at 2:00pm, Luc Hermitte wrote:

> Hello,
>
> * On Wed, Apr 30, 2003 at 10:56:01AM +0200, Martin Metz <martin.metz@onevision.de> wrote:
> > for coding I use a lot of abbreviations and remaps like:
> >
> > " Ease the usage of '{' and '}'
> > inoremap { {<CR>}<ESC>O
> > " java try catch blox for exceptions:
> > iab try try {x<ESC><Down>A catch(Exception e)
> > {e.printStackTrace();<Esc><Up><Up>$xxA
> >
> > So when I code in java and write "try " vim expands it for me to:
> > [...]
> >
> > which I really love. But my problem are comments! Every time I use the
> > word "try" within a comment I have to remove all the coding stuff
> > again.  Is there a simple way to detect if I'm currently in comment
> > mode or not?  Or any other ideas which help?
>
> You can check my C & C++ ftplugins. They do exactly what you are looking
> for -- and much more.
> From the archive file lh-cpp.tar.gz keep the plugins, ftplugin/c/c_set.vim
> and adapt ftplugin/cpp/cpp_set.vim to what you need for java
> programming.
>
> HTH,
>
>

You can also take a look at sql_iabbr.vim on vim.sf.net, which avoids
expanding the words if typed in comments or string constants. Here is
what it essentially does:

>>>>
inoreabbr <silent> <buffer> datetime <C-R>=SqlIab_ReplaceConditionally('datetime', 'DATETIME')<CR>

function! SqlIab_ReplaceConditionally(original, replacement)
  " only replace outside of comments or strings (which map to constant)
  let elesyn = synIDtrans(synID(line("."), col(".") - 1, 0))
  if elesyn != hlID('Comment') && elesyn != hlID('Constant')
    let word = a:replacement
  else
    let word = a:original
  endif

  let g:UndoBuffer = a:original
  return word
endfunction

inoremap <silent> <buffer> <C-X>u <C-W><C-R>=g:UndoBuffer<CR><C-V><Space>
<<<<

The last maping assumes that the expansion results in only one word,
which obviously won't work, but you may be able to extend it.

HTH,
Hari
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic