Loading ...
Sorry, an error occurred while loading the content.

Re: abbreviations NOT used in source comments

Expand Messages
  • Hari Krishna Dara
    ... 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 ... inoreabbr
    Message 1 of 7 , Apr 30 2:55 PM
    • 0 Attachment
      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@...> 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
    Your message has been successfully submitted and would be delivered to recipients shortly.