Saturday, July 17, 2010

Re: Detecting first characters in a line ?

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,
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. Ah, and, "filetype" without the
quotes is a user-defined variable name. To get the option you need a &
prefix, see ":help expr-option".

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


Best regards,
Tony.
--
A is for Amy who fell down the stairs, B is for Basil assaulted by bears.
C is for Clair who wasted away, D is for Desmond thrown out of the sleigh.
E is for Ernest who choked on a peach, F is for Fanny, sucked dry by a
leech.
G is for George, smothered under a rug, H is for Hector, done in by a thug.
I is for Ida who drowned in the lake, J is for James who took lye, by
mistake.
K is for Kate who was struck with an axe, L is for Leo who swallowed
some tacks.
M is for Maud who was swept out to sea, N is for Nevil who died of enui.
O is for Olive, run through with an awl, P is for Prue, trampled flat in
a brawl
Q is for Quinton who sank in a mire, R is for Rhoda, consumed by a fire.
S is for Susan who parished of fits, T is for Titas who flew into bits.
U is for Una who slipped down a drain, V is for Victor, squashed under
a train.
W is for Winie, embedded in ice, X is for Xercies, devoured by mice.
Y is for Yoric whose head was bashed in, Z is for Zilla who drank too
much gin.
-- Edward Gorey "The Gastly Crumb Tines"

--
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: