Counting ; character

6 views
Skip to first unread message

epanda

unread,
Dec 15, 2009, 6:42:40 AM12/15/09
to vim_use
Hi,


I would like to count numbers of ; in a line before position of my
cursor.

I am doing a search like that : search(';;\{1,30}','w')

;foo;;bar => should return 2

;foo1;foo;;bar => should return 3

;foo1;foo2;foo;;bar => should return 4

;;foo;;bar => should return 0

Thanks

Tim Chase

unread,
Dec 15, 2009, 8:36:44 AM12/15/09
to vim...@googlegroups.com
There are some crucial details missing:

- you don't indicate the "position of [your] cursor" in the above
examples

- you don't indicate what you want to do with the count...store
it in a variable for the current line? put the count at the
beginning/end of the line?

- I'm not sure what your search() call is supposed to do in
relation to the remainder of your email


-tim



Christian Brabandt

unread,
Dec 15, 2009, 8:39:11 AM12/15/09
to vim_use
Hi epanda!
I don't understand. Where is the cursor? And I don't really understand
the count logic either.

regards,
Christian
--
hundred-and-one symptoms of being an internet addict:
20. When looking at a pageful of someone else's links, you notice all of them
are already highlighted in purple.

epanda

unread,
Dec 15, 2009, 8:57:44 AM12/15/09
to vim_use


On 15 déc, 14:39, Christian Brabandt <cbli...@256bit.org> wrote:
> Hi epanda!
>
> On Di, 15 Dez 2009, epanda wrote:
>
> > I am doing a search like that : search(';;\{1,30}','w')
>
> I don't understand. Where is the cursor? And I don't really understand
> the count logic either.
>
> regards,
> Christian
> --
> hundred-and-one symptoms of being an internet addict:
> 20. When looking at a pageful of someone else's links, you notice all of them
>     are already highlighted in purple.

Sorry, the search command send cursor where occurence is found : here
=> ;;

cursor is here
|
V
;foo1;foo;;bar => should return 3


I have done this little func but using while and procedural don't
satisfy me.

function! CountCharBeforeCursor(searchedChar)
let l:c = a:searchedChar
let l:cpt = 0
while col('.') > col('^') + 1
normal h
if getline('.')[col('.')] ==? l:c
let l:cpt += 1
endif
endwhile
return l:cpt
endfunction



Now I know exactly how many ; are between my cursor and the begin of
line.

NB : in fact I have to search the nth ; at the line just before.
How can I jump directly to the third ; at the previous line ?


epanda

unread,
Dec 15, 2009, 9:07:33 AM12/15/09
to vim_use
> NB : in fact I have to search the nth ; at the line just before.
> How can I jump directly to the third ; at the previous line ?


Ok I have forgotten Nf; normal command to go to N'th ; to the right

Thank you very much

Christian Brabandt

unread,
Dec 15, 2009, 9:14:15 AM12/15/09
to vim...@googlegroups.com
On Tue, December 15, 2009 2:57 pm, epanda wrote:
> Sorry, the search command send cursor where occurence is found : here
> => ;;
>
> cursor is here
> |
> V
> ;foo1;foo;;bar => should return 3

Ok, so your cursor in on the first of the 2 consecutive ';;', right?
And from your example above, it should return 2?

I would actually use something like this:

:echo len(split(getline('.')[0:col('.')-1], ';'))

regards,
Christian
--
:wq

Tim Chase

unread,
Dec 15, 2009, 9:22:35 AM12/15/09
to vim...@googlegroups.com
I'd have done something like

echo strlen(substitute(getline('.')[:col('.')-1], '[^;]', '', 'g'))

but either works well.

-tim


epanda

unread,
Dec 15, 2009, 9:36:29 AM12/15/09
to vim_use
> -tim- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -

Yes you've seen my cursor pos Christian.
Is it faster than my func ?

Christian Brabandt

unread,
Dec 15, 2009, 4:15:51 PM12/15/09
to vim_use
Hi epanda!

On Di, 15 Dez 2009, epanda wrote:

> Yes you've seen my cursor pos Christian.
> Is it faster than my func ?

Interesting challenge ;) I changed your function a little bit� and made
a test:

com! Count :call CountCharBeforeCursor(';')
com! Count1 :let a=len(split(getline('.')[0:col('.')-1], ';'))
com! Count2 :let a=strlen(substitute(getline('.')[:col('.')-1], '[^;]', '', 'g'))

Now using timing.vim� on this line (cursor on first of the 2 consecutive
;;):
;foo1;foo2;foo;;bar

Let's see:


for i in ['Count', 'Count1', 'Count2'] | exe "TIME 100 ".i | endfor
Execution took 0.094060 sec. (count=100)
Execution took 0.007555 sec. (count=100)
Execution took 0.009596 sec. (count=100)


�)I changed your function to save and restore cursor position.
�)http://www.vim.org/scripts/script.php?script_id=1530


regards,
Christian
--
hundred-and-one symptoms of being an internet addict:
22. You've already visited all the links at Yahoo and you're halfway through
Lycos.

Christian Brabandt

unread,
Dec 15, 2009, 4:20:05 PM12/15/09
to vim_use
Hi

On Di, 15 Dez 2009, Christian Brabandt wrote:

> đ)I changed your function to save and restore cursor position.
> ē)http://www.vim.org/scripts/script.php?script_id=1530

Grml, thouse were supposed to be footnotes. Don't know, why mutt mangled
this mail back to latin1, it was supposed to be in an utf8 encoding.

regards,
Christian
--
hundred-and-one symptoms of being an internet addict:
23. You can't call your mother...she doesn't have a modem.

Chris Jones

unread,
Dec 15, 2009, 4:44:07 PM12/15/09
to vim_use
On Tue, Dec 15, 2009 at 04:20:05PM EST, Christian Brabandt wrote:
> Hi
>
> On Di, 15 Dez 2009, Christian Brabandt wrote:
>
> > đ)I changed your function to save and restore cursor position.
> > ē)http://www.vim.org/scripts/script.php?script_id=1530
>
> Grml, thouse were supposed to be footnotes. Don't know, why mutt mangled
> this mail back to latin1, it was supposed to be in an utf8 encoding.

Same thing happened to me on this list, a couple of days ago.

I've only seen this happen on this list, which happens to also be a
google group.

What makes you think mutt is the culprit?

CJ

Christian Brabandt

unread,
Dec 15, 2009, 5:12:40 PM12/15/09
to vim_use
Hi Chris!

On Di, 15 Dez 2009, Chris Jones wrote:

> I've only seen this happen on this list, which happens to also be a
> google group.
>
> What makes you think mutt is the culprit?

First guess. Actually, I think you are right, cause in my sent box, the
mail looks alright. It was sent with charset=iso8859-1 and
content-transfer-encoding: 8bit, but it was received with charset=utf-8
and CTE: quoted-printable

Let's see if this happens again: ��

regards,
Christian

Chris Jones

unread,
Dec 15, 2009, 5:23:14 PM12/15/09
to vim_use, cblists2...@turki.gavron.org
> Let's see if this happens again: 共

With the other message I mentioned¹, I forwarded the saved copy in the
=Sent folder to myself and it came back OK. And since this have never
happened to me one any of the 30-40 lists I am subscribed to.. Not that
I use superscripts in each and every one of my posts, of course :-)

CJ

¹ Title was 'Text replacing question', Dec 12 2009.

Chris Jones

unread,
Dec 23, 2009, 10:24:08 PM12/23/09
to vim_use
On Tue, Dec 15, 2009 at 04:20:05PM EST, Christian Brabandt wrote:
> Hi
>
> On Di, 15 Dez 2009, Christian Brabandt wrote:
>
> > đ)I changed your function to save and restore cursor position.
> > ē)http://www.vim.org/scripts/script.php?script_id=1530

> Grml, thouse were supposed to be footnotes. Don't know, why mutt
> mangled this mail back to latin1, it was supposed to be in an utf8
> encoding.

I think I accidentally reproduced this when posting on another vim_use
thread whose subject is something like 'printing with UTF-8 characters
from Windows'.

What happened was that I replied to the OP in mutt, but before I hit 'y'
to send the message, I thought of something I wanted to correct or add,
hit 'e' to edit, and was confronted by an absolute mess where my
non-ASCII stuff was concerned.

I checked mutt's idea of the encoding and it said something like
'text/plain, 7bit, iso-8859-1'. After twiddling a bit, I managed to
convince Vim to convert my message back to UTF-8, wrote it to a temp
file, started over and copied the temp file back into my reply and this
time when I got to the compose/send screen in mutt, the message was
correctly reported as 'text/plain, 8bit, UTF-8.

Since the thread was about someone experiencing problems with encodings,
what I assume is that I did change something manually, while replying,
possibly I did a 'set fenc=latin1, or possibly cp1252.. don't remember
exactly, and forgot to set it back to UTF-8 before posting.

On the other hand, since I had no reason to do anything like this with
my other garbled message, it looks like there may be some form of
miscommunication between mutt and Vim at some point or other. I assume
that my locale, which is set to UTF-8, is used by default when mutt
decodes the message I am responding to, and that iconv is used to
convert the message before it is passed on to Vim, and that mutt keeps
track of this pending my :wq. When I finish editing, mutt creates the
content header, adjusting it to reflect whatever change I may have made
while editing.

Just wondering if anything in the above speculations might be confirmed
by something you might remember happened at the time you posted the
message where your footnote superscripts got messed up?

At this point, I'm not sure looking at the code would help, unless we
manage to determine a pattern and are able to reproduce.

Thanks,

CJ

Tony Mechelynck

unread,
Feb 7, 2010, 8:24:26 PM2/7/10
to vim_use

Well, the other time too, mutt was somehow involved; this could be a
coincidence however, with the real culprit being for instance (let's
venture a guess) some mail router along the line with out-of-date
non-8bitMIME-capable software. I know my own ISP's routers handle
8bitMIME correctly, or at least acceptably, because I've received time
and again in the past UTF-8 mail (which contained more than only 7-bit
ASCII characters) in 8bit Content-Transfer-Encoding, and it displayed
beautifully; or some rare times, I received it in quoted-printable with
headers saying it had been converted from 8bit to quoted-printable along
the way, and the conversion was correct; however I've been told that the
non-8bit-capable mail routers which once were the majority on the Web
haven't yet been all upgraded or decommissionned.

This mail is in UTF-8, BTW, and I'm composing it using Mozilla
SeaMonkey. The French language uses the following accented or otherwise
non-ASCII characters: âà Çç éêèë îï ô œŒ ûù ŷÿ where I'm grouping them
by collation equivalents. Also Euro € and paragraph §. (As you can see,
French uses the diaeresis on exactly those vowels which can never have
an umlaut -the same diacritic with a different function- in German. ;-) )

Best regards,
Tony.
--
Christ:
A man who was born at least 5,000 years ahead of his time.

Reply all
Reply to author
Forward
0 new messages