How to make a function toggling

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to make a function toggling

Eric Leenman
Hi,

I got a function to comment a piece of code, depending on the filetype,
see below.
How do I make this function so that when I select a commented piece of
code to be 'uncomment'

So in a cpp file this :
//  Count();
//  Move();

Becomes:
  Count();
  Move();

And in a C file this:
/*  Count(); */
/*  Move(); */

Becomes:
  Count();
  Move();



function Cmt()
  if &filetype == "vhdl"
   let s:c1 = "-- "
   let s:c2 = ""
  elseif &filetype == "cpp"
   let s:c1 = "// "
   let s:c2 = ""
  elseif &filetype == "vim"
   let s:c1 = "\""
   let s:c2 = ""
  elseif &filetype == "ucf"
   let s:c1 = "# "
   let s:c2 = ""
   elseif &filetype == "c"
   let s:c1 = "/* "
   let s:c2 = " */"
  elseif &filetype == "pascal"
   let s:c1 = "{ "
   let s:c2 = " }"
  else
   echoerr "Unknown filetype" &filetype
   \ "in Cmt() in file"
   \ expand("%") "at line"
   \ line(".")
   return 255
  endif
    exec "normal A\<c-r>=s:c2\<cr>\<esc>0i\<c-r>=s:c1\<cr>\<esc>"
 endfunction

Rgds,
Eric


Disclaimer; The information contained in this communication is confidential and may be legally privileged. It is intended solely for the use of the individual
or entity to whom it is addressed and others authorized to recieve it. Use of this information, in whatever way, by others is strictly prohibited
and may be unlawful. GREEFA does not accept legal responsibility for this e-mail message due to the insecure nature of internet communications.
Reply | Threaded
Open this post in threaded view
|

Re: How to make a function toggling

Peter Hodge-2
You probably want something like:

function! Uncomment()
  [ paste large if/else statement to set up s:c1 and s:c2 ]

  " substitute <s:c1> at start of line with an empty string
  " and supress error message when <s:c1> not found
  " NOTE: each '\' in the search pattern must be preceded by a '\'
  " because the search pattern is inside a string!
  " NOTE: the use of \V in the search pattern forces literal matching
  " of '*' when it is part of a comment string.
  " NOTE: the use of @ as separator in the :s command stops comments
  " like '/*' playing funny games with the substitute command.  AFIK
  " no commenting systems include a '@' char ...
  if s:c1
    exe 's@^\\V'.s:c1.'@@e'
  endif
  " substitute <s:c2> at end of line with an empty string
  " and supress error message when <s:c2> not found
  if s:c2
    exe 's@\\V'.s:c2.'\\$@@e'
  endif
endfunction

This function should silently ignore lines which are not commented.  Please
note that I haven't tried this function (although it is similar to something I
use myself), so it may need tweaking and some syntax fixing.


hope this helps,
Peter



> Hi,
>
> I got a function to comment a piece of code, depending on the filetype,
> see below.
> How do I make this function so that when I select a commented piece of
> code to be 'uncomment'
>
> So in a cpp file this :
> //  Count();
> //  Move();
>
> Becomes:
>   Count();
>   Move();
>
> And in a C file this:
> /*  Count(); */
> /*  Move(); */
>
> Becomes:
>   Count();
>   Move();
>
>
>
> function Cmt()
>   if &filetype == "vhdl"
>    let s:c1 = "-- "
>    let s:c2 = ""
>   elseif &filetype == "cpp"
>    let s:c1 = "// "
>    let s:c2 = ""
>   elseif &filetype == "vim"
>    let s:c1 = "\""
>    let s:c2 = ""
>   elseif &filetype == "ucf"
>    let s:c1 = "# "
>    let s:c2 = ""
>    elseif &filetype == "c"
>    let s:c1 = "/* "
>    let s:c2 = " */"
>   elseif &filetype == "pascal"
>    let s:c1 = "{ "
>    let s:c2 = " }"
>   else
>    echoerr "Unknown filetype" &filetype
>    \ "in Cmt() in file"
>    \ expand("%") "at line"
>    \ line(".")
>    return 255
>   endif
>     exec "normal A\<c-r>=s:c2\<cr>\<esc>0i\<c-r>=s:c1\<cr>\<esc>"
>  endfunction
>
> Rgds,
> Eric
>
>
> Disclaimer; The information contained in this communication is confidential
> and may be legally privileged. It is intended solely for the use of the
> individual
> or entity to whom it is addressed and others authorized to recieve it. Use of
> this information, in whatever way, by others is strictly prohibited
> and may be unlawful. GREEFA does not accept legal responsibility for this
> e-mail message due to the insecure nature of internet communications.
>



               
____________________________________________________
Do you Yahoo!?
Messenger 7.0: Free worldwide PC to PC calls
http://au.messenger.yahoo.com
Reply | Threaded
Open this post in threaded view
|

Re: How to make a function toggling

Luc Hermitte
In reply to this post by Eric Leenman
Hello,

* On Thu, Oct 13, 2005 at 04:11:06PM +0200, Eric Leenman <[hidden email]> wrote:
> How do I make this function so that when I select a commented piece of
> code to be 'uncomment'

Check EnhancedCommentify on sourceforge.
There are other plugins dedicated to comments toggling.

HTH,

--
Luc Hermitte
http://hermitte.free.fr/vim/