How to get the start line and end line of a folding?

5 views
Skip to first unread message

anhnmncb

unread,
Nov 30, 2008, 3:52:25 AM11/30/08
to vim...@googlegroups.com
As title, I want to make a function for foldtext like this:

function MY_TeX_BiBFoldText()
let line = getline(v:foldstart, v:foldend)
let matchlinea = match(line, '^\s*author.*{.*}.*') + 1
let matchlinet = match(line, '^\s*title.*{.*}.*') + 1
let matcha = substitute(getline(matchlinea), '^.*{\(.*\)}*.*$', '\1', 'g')
let matcht = substitute(getline(matchlinet), '^.*{\(.*\)}*.*$', '\1', 'g')
let matched = "title: " . matcht . " author: " . matcha
return v:folddashes . matched
endfunction

So the folding will show the string "matched", but I find that v:foldstart and
v:foldend have the same value in foldings, so all my foldings show the same
string, why?

--
Regards,
anhnmncb

anhnmncb

unread,
Dec 1, 2008, 6:20:36 AM12/1/08
to vim...@googlegroups.com
Isn't my description clear? If so, let me know please.


--
Regards,
anhnmncb

Tony Mechelynck

unread,
Dec 1, 2008, 6:40:58 AM12/1/08
to vim...@googlegroups.com

Isn't it clear that I don't know how to help (and that Bram, Dr.Chip,
Bill, John and the rest are probably either too busy or equally nonplussed)?

In case you didn't know, if you don't get a reply on this list, it
usually means that none of those who got your mail (and already got to
the point of opening it) knew a satisfatory answer. Lost mail may happen
too but it's rare, and with some mail providers including @gmail.com
it's even possible to get back the mail that "an over-diligent secretary
sent to the wastebasket" (and I don't mean a human secretary).


Best regards,
Tony.
--
One nice thing about egotists: they don't talk about other people.

anhnmncb

unread,
Dec 1, 2008, 6:53:20 AM12/1/08
to vim...@googlegroups.com
On 2008-12-01, Tony Mechelynck wrote:
>
> Isn't it clear that I don't know how to help (and that Bram, Dr.Chip,
> Bill, John and the rest are probably either too busy or equally nonplussed)?
>
> In case you didn't know, if you don't get a reply on this list, it
> usually means that none of those who got your mail (and already got to
> the point of opening it) knew a satisfatory answer. Lost mail may happen
> too but it's rare, and with some mail providers including @gmail.com
> it's even possible to get back the mail that "an over-diligent secretary
> sent to the wastebasket" (and I don't mean a human secretary).
>
Thank you, I'm using usenet proxy to access this list, so if it got lost, I
would not get the reply :)


--
Regards,
anhnmncb

Tony Mechelynck

unread,
Dec 1, 2008, 7:11:37 AM12/1/08
to vim...@googlegroups.com

I'm using POP on my @gmail.com account (to which I have set the vim
lists to send every message) and from time to time I use the webmail
interface to handle spam (recover false positives, report false
negatives, and then clear the trash and spam folders until next time).
Now and then there is a false positive at gmail but at least (thank ***)
there's no firewall or proxy between it and me to delete my mail without
my say-so.

Best regards,
Tony.
--
"I'd love to go out with you, but I've been scheduled for a karma
transplant."

Charles Campbell

unread,
Dec 1, 2008, 3:43:31 PM12/1/08
to vim...@googlegroups.com
Hello!

I've not messed with the foldtext option before; I suspect its one of
the less common things people do, so your target audience is small.
Also, I've moved recently to a new email delivery mechanism (even though
my email address itself hasn't changed), and its flagging your email as
spam:

Content analysis details: (5.9 points, 3.5 required)

pts rule name description
---- ---------------------- --------------------------------------------------
0.1 RDNS_NONE Delivered to trusted network by a host with no rDNS
3.2 FROM_LOCAL_NOVOWEL From: localpart has series of non-vowel letters
2.6 RCVD_NUMERIC_HELO Received: contains an IP address used for HELO

The biggest item on the above list is your false "name". Rather discourages one from looking at your email, you know.

Now, on to your problem -- I instrumented your function with Decho.vim calls (Dfunc, Decho, Dret). Since the foldtext option says its a no-no to change windows, I used DechoMsgOn which sends Decho output via echomsg.

You can get the Decho plugin from:

http://vim.sourceforge.net/scripts/script.php?script_id=120
-or-
http://mysite.verizon.net/astronaut/vim/index.html#DECHO
(this latter one is always the most up-to-date)

Here's the modified function/script:

DechoMsgOn
function! MY_TeX_BiBFoldText()
call Dfunc("My_TeX_BiBFoldText() v:fold[".v:foldstart.",".v:foldend."]")


let line = getline(v:foldstart, v:foldend)
let matchlinea = match(line, '^\s*author.*{.*}.*') + 1
let matchlinet = match(line, '^\s*title.*{.*}.*') + 1

call Decho("matchlinea=".matchlinea." matchlinet=".matchlinet)


let matcha = substitute(getline(matchlinea), '^.*{\(.*\)}*.*$', '\1', 'g')
let matcht = substitute(getline(matchlinet), '^.*{\(.*\)}*.*$', '\1', 'g')
let matched = "title: " . matcht . " author: " . matcha

call Dret("My_TeX_BiBFoldText ".v:folddashes . matched)


return v:folddashes . matched
endfunction

set foldtext=MY_TeX_BiBFoldText()

With it, you'll see that v:foldstart and v:foldend do differ when MY_TeX_BiBFoldText() is called for each fold. An example of one of the calls (gotten by typing :mess):

My_TeX_BiBFoldText() v:fold[30,35] {
|matchlinea=0 matchlinet=0
|return My_TeX_BiBFoldText -title: author: }

The problem here is that your two match() calls which look for author and title don't match. None of my bib entries that I used for this test have {}s for author=... or title=... , so that's the proximate cause for me. You'd need to share some of your bib files for a better test, or better: get Decho.vim and try debugging this one yourself.

Regards,
Chip Campbell

Matt Wozniski

unread,
Dec 1, 2008, 5:10:08 PM12/1/08
to vim...@googlegroups.com
On Sun, Nov 30, 2008 at 3:52 AM, anhnmncb wrote:
>
> As title, I want to make a function for foldtext like this:

Looks to me like your function is confused...

> function MY_TeX_BiBFoldText()

You get a List containing the text of all lines in the fold:

> let line = getline(v:foldstart, v:foldend)

Then you find the offsets in that list of the matching lines:

> let matchlinea = match(line, '^\s*author.*{.*}.*') + 1
> let matchlinet = match(line, '^\s*title.*{.*}.*') + 1

Then you use those offsets as line numbers, instead of offsets into a
list. Why are you using getline(match()+1) here, instead of
line[match()] ?

> let matcha = substitute(getline(matchlinea), '^.*{\(.*\)}*.*$', '\1', 'g')
> let matcht = substitute(getline(matchlinet), '^.*{\(.*\)}*.*$', '\1', 'g')

Seems to me that those lines should be:

let matchlinea = match(line, '^\s*author.*{.*}.*')

let matchlinet = match(line, '^\s*title.*{.*}.*')

let matcha = substitute(line[matchlinea], '^.*{\(.*\)}*.*$', '\1', 'g')
let matcht = substitute(line[matchlinet], '^.*{\(.*\)}*.*$', '\1', 'g')

> let matched = "title: " . matcht . " author: " . matcha
> return v:folddashes . matched
> endfunction

Try that, see if it helps.

~Matt

anhnmncb

unread,
Dec 1, 2008, 8:37:03 PM12/1/08
to vim...@googlegroups.com
I use following lines for bib filetype:

set fdm=syntax
set foldtext=MY_TeX_BiBFoldText()
syntax region bibentry start=/^\s*@.*$/ end=/^\s\?}.*\n\s*$/ contained contains=ALL fold

and the bib example is following:

@ARTICLE{foo1,
author = {author1},
title = {This is a long
title.),
journal = {journal1},
year = 1980,
volume = 18,
pages = {55--67},
TypeofLit = {J}
}

@ARTICLE{foo2,
author = {author2 and author 3},
title = {title 2},
journal = {Throbosis Res},
year = 1982,
volume = 13,
pages = {58--67},
TypeofLit = {J}
}


>
> Regards,
> Chip Campbell
>
>
>
>
>

anhnmncb

unread,
Dec 1, 2008, 8:44:48 PM12/1/08
to vim...@googlegroups.com
Hello, your version works! thanks :) I should use line[], not getline().

>
> ~Matt
>
>

anhnmncb

unread,
Dec 1, 2008, 8:56:15 PM12/1/08
to vim...@googlegroups.com
On 2008-12-02, anhnmncb wrote:
>
>
> I use following lines for bib filetype:
>
> set fdm=syntax
> set foldtext=MY_TeX_BiBFoldText()
> syntax region bibentry start=/^\s*@.*$/ end=/^\s\?}.*\n\s*$/ contained contains=ALL fold

From my function and other stuff, you know I'm not a programmer, and not get
familiar with vimscript and regex, anyone can give me an improve version for
my syntax for bib? My version just can fold the bib entry like this:
@article{foo,
author = {author},
title = {title},
}

it doesn't support this one:
@article{foo,
author = {author},
title = {title},}

And my foldtext can support just one line of title, the splited long line will not
be regornized, so if title = {a long \n line title}, then the function just
returns "a long", not with the following line.

I know maybe there is already some plugins like latexsuit can do that, but I
don't get along well with them.

Any helpful info will be appreciated.

--
Regards,
anhnmncb

Reply all
Reply to author
Forward
0 new messages