Saturday, July 17, 2010

Re: [SOVLED] Re: Detecting first characters in a line ?

On 17/07/10 15:57, Aaron Lewis wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 07/17/2010 09:20 PM, Tony Mechelynck wrote:
>> On 17/07/10 14:15, Aaron Lewis wrote:
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>>
>>> On 07/17/2010 07:51 PM, Frank Blendinger wrote:
>>>> Hi.
>>>>
>>>> Aaron Lewis<aaron.lewis1989@gmail.com> (2010-07-17 19:24 +0800):
>>>>> I'm trying to implement a custom script used for commenting and
>>>>> uncommenting , we have two types , like c-style `//' and shell-style
>>>>> `#'
>>>>> , how can i detect the first characters ?
>>>>
>>>> Not really answering your question, but if you aren't writing the script
>>>> just for fun or education I would recommend the existing NERD commenter
>>>> plugin, which does its job pretty well for all kinds of comments:
>>>
>>> OK , i've just got some ideas from that plugin , as i'm doing a custom
>>> plugin , what's wrong with this func , anybody give a try ?
>>>
>>> func! CommentingOut()
>>> if filetype == ""
>>> s/^/#/<CR>
>>> else
>>> s/^/\/\//<CR>
>>> endif
>>> endfunc
>>>
>>> Thanks
>>
>> Hm, the logic looks a little too simple. There are a lot of filetypes,
>
> Doesn't matter for me right now ;-) You've got it , should be regex here.
>
>> detected by Vim, for which C++-like comments won't be acceptable. In
>> addition, since script lines are ex-commands, you don't need additional
>> <CR> characters at the end. And finally, you may want to separate your
>> comment leader from the text by a space.
>
> Right
>
> Ah, and, "filetype" without the
>> quotes is a user-defined variable name. To get the option you need a&
>> prefix, see ":help expr-option".
>>
>
> Yep.
>
>> I haven't tried the following (based on the above function), but what do
>> you think of it?
>>
>> function CommentOut() range
>> if&filetype =~? '^c$\|^cpp$\|^css$\|^javascript$'
>> exe a:firstline . ',' . a:lastline . 's/^/\/\/ /'
>> else
>> exe a:firstline . ',' . a:lastline . 's/^/# /'
>> endif
>> endfunc
>>
>
> Great , works fine.
>
> At first glance , i'm just trying to remove comments by determining the
> first character , maybe by file type is more reliable. I'm just thinking
> about macros in c language.
>
>
> - --
> Best Regards,
> Aaron Lewis - PGP: 0x4A6D32A0
> FingerPrint EA63 26B2 6C52 72EA A4A5 EB6B BDFE 35B0 4A6D 32A0
> irc: A4r0n on freenode
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.14 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkxBts8ACgkQvf41sEptMqDpLgCgw9GwImV5yC03I1vgBY5U80Cr
> 0RQAnjw7JMT4sDQkPHUj8kDx0Eruc8ev
> =4hYk
> -----END PGP SIGNATURE-----
>

Well, this will _add_ comments. _Removing_ comments is easier:

:[range]s/^# \|^\/\/ //

would do it, except that in C-like files you wouldn't want to strip the
initial # in indented preprocessor directives like

#ifdef A
# if defined(B) || defined(C)
# ifndef D
# define D foobar
# endif
# else

etc., so you would probably have to do either the one or the other; but
I suppose it is simple enough that you can still do either

:[range]s/# //
or
:[range]s/\/\/ //

manually.


Best regards,
Tony.
--
The man who said "A bird in the hand's worth two in the bush" has been
putting his bird in the *WRONG* bushes.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: