foldtext question

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

foldtext question

Guido Van Hoecke-2
Hi,

I want to write a foldtext function where the folded block would not
have the standard +- header with variable nr of dashes, but the exact
text appearing before the fold marker (in casu one or more '{' possibly
followed by a digit). Here it is:

function! MyFoldText() " show the text before the fold marker
    let nr = v:foldend - v:foldstart + 1
    let line = getline(v:foldstart)
    let txt = substitute(line, '{\+\d\=', '('.nr.' lines)', 'g')
    return txt
endfunction

I am expecting erm... hoping that this would preserve the initial
whitespace: a line '\t\t\tsomeText {' would then be displayed as
a folded block with the text '\t\t\tsomeText (nn lines)'.

It is not. I know that the help for 'foldtext' explicitely states
'...Leading white space, "//" or "/*" and the text from the 'foldmarker'
and 'commentstring' options is removed. ...'

I was hoping that this comment only applied to the default foldtext
function, but it seems that that is not the case. I would have liked to
have full control over the displayed foldtext.

Is this at all possible?
If so, I'd like to see how it's done.
If it is not the case, wouldn't that be a desirable feature?

With my current indented folded blocks are outdented and
completely break the visual indentation view, such a pitty.

Kind regards,


Guido

--
I must get out of these wet clothes and into a dry Martini.
                -- Alexander Woolcott

http://vanhoecke.org ... and go2 places!

--
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
Reply | Threaded
Open this post in threaded view
|

Re: foldtext question

Andrei Kulakov
On 01/15/2012 10:42 AM, Guido Van Hoecke wrote:

> Hi,
>
> I want to write a foldtext function where the folded block would not
> have the standard +- header with variable nr of dashes, but the exact
> text appearing before the fold marker (in casu one or more '{' possibly
> followed by a digit). Here it is:
>
> function! MyFoldText() " show the text before the fold marker
>      let nr = v:foldend - v:foldstart + 1
>      let line = getline(v:foldstart)
>      let txt = substitute(line, '{\+\d\=', '('.nr.' lines)', 'g')
>      return txt
> endfunction
>
> I am expecting erm... hoping that this would preserve the initial
> whitespace: a line '\t\t\tsomeText {' would then be displayed as
> a folded block with the text '\t\t\tsomeText (nn lines)'.
>
> It is not. I know that the help for 'foldtext' explicitely states
> '...Leading white space, "//" or "/*" and the text from the 'foldmarker'
> and 'commentstring' options is removed. ...'
>
> I was hoping that this comment only applied to the default foldtext
> function, but it seems that that is not the case. I would have liked to
> have full control over the displayed foldtext.
>
> Is this at all possible?
> If so, I'd like to see how it's done.
> If it is not the case, wouldn't that be a desirable feature?
>
> With my current indented folded blocks are outdented and
> completely break the visual indentation view, such a pitty.
>
> Kind regards,
>
>
> Guido
>
> --
> I must get out of these wet clothes and into a dry Martini.
> -- Alexander Woolcott
>
> http://vanhoecke.org ... and go2 places!
>


It does work for me but I'm dealing with lines that have
leading spaces and I add leading spaces, here is a relevant
snip:

     let l = getline(v:foldstart)
     let line = substitute(l, '^[ ]*', '', '')
     let prefix = repeat(' ', (strlen(l) - strlen(line)))
     let c = ''
     if l =~ '^\s*class\s' | let c = '(c)--' | endif
     let line = prefix . '+--' . c . '  ' . line


  -ak

--
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
Reply | Threaded
Open this post in threaded view
|

Re: foldtext question

Gary Johnson-4
On 2012-01-15, AK wrote:

> It does work for me but I'm dealing with lines that have
> leading spaces and I add leading spaces, here is a relevant
> snip:
>
>     let l = getline(v:foldstart)
>     let line = substitute(l, '^[ ]*', '', '')
>     let prefix = repeat(' ', (strlen(l) - strlen(line)))
>     let c = ''
>     if l =~ '^\s*class\s' | let c = '(c)--' | endif
>     let line = prefix . '+--' . c . '  ' . line

You can also use the indent() function to get the indentation of the
line directly instead of calculating string lengths, e.g.,

    let n = v:foldend - v:foldstart + 1
    let i = indent(v:foldstart)
    return repeat(' ', i) . "+-" . v:folddashes . " " . n . " lines "

Regards,
Gary

--
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
Reply | Threaded
Open this post in threaded view
|

Re: foldtext question

Andrei Kulakov
On 01/15/2012 01:56 PM, Gary Johnson wrote:

> On 2012-01-15, AK wrote:
>
>> It does work for me but I'm dealing with lines that have
>> leading spaces and I add leading spaces, here is a relevant
>> snip:
>>
>>      let l = getline(v:foldstart)
>>      let line = substitute(l, '^[ ]*', '', '')
>>      let prefix = repeat(' ', (strlen(l) - strlen(line)))
>>      let c = ''
>>      if l =~ '^\s*class\s' | let c = '(c)--' | endif
>>      let line = prefix . '+--' . c . '  ' . line
>
> You can also use the indent() function to get the indentation of the
> line directly instead of calculating string lengths, e.g.,
>
>      let n = v:foldend - v:foldstart + 1
>      let i = indent(v:foldstart)
>      return repeat(' ', i) . "+-" . v:folddashes . " " . n . " lines "
>
> Regards,
> Gary
>


Thanks! I know that now, but I wrote this function a
long time ago..  -ak

--
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
Reply | Threaded
Open this post in threaded view
|

Re: foldtext question

Gary Johnson-4
On 2012-01-15, AK wrote:

> On 01/15/2012 01:56 PM, Gary Johnson wrote:
> >On 2012-01-15, AK wrote:
> >
> >>It does work for me but I'm dealing with lines that have
> >>leading spaces and I add leading spaces, here is a relevant
> >>snip:
> >>
> >>     let l = getline(v:foldstart)
> >>     let line = substitute(l, '^[ ]*', '', '')
> >>     let prefix = repeat(' ', (strlen(l) - strlen(line)))
> >>     let c = ''
> >>     if l =~ '^\s*class\s' | let c = '(c)--' | endif
> >>     let line = prefix . '+--' . c . '  ' . line
> >
> >You can also use the indent() function to get the indentation of the
> >line directly instead of calculating string lengths, e.g.,
> >
> >     let n = v:foldend - v:foldstart + 1
> >     let i = indent(v:foldstart)
> >     return repeat(' ', i) . "+-" . v:folddashes . " " . n . " lines "

> Thanks! I know that now, but I wrote this function a
> long time ago..  -ak

You're welcome.  I should thank you, too.  I also wrote my function
a long time ago and until now used a while loop instead of repeat()
to create the indent prefix.

Regards,
Gary

--
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
Reply | Threaded
Open this post in threaded view
|

Re: foldtext question

Guido Van Hoecke-2
On Sun, Jan 15, 2012 at 20:11, Gary Johnson <[hidden email]> wrote:

> On 2012-01-15, AK wrote:
>> On 01/15/2012 01:56 PM, Gary Johnson wrote:
>> >On 2012-01-15, AK wrote:
>> >
>> >>It does work for me but I'm dealing with lines that have
>> >>leading spaces and I add leading spaces, here is a relevant
>> >>snip:
>> >>
>> >>     let l = getline(v:foldstart)
>> >>     let line = substitute(l, '^[ ]*', '', '')
>> >>     let prefix = repeat(' ', (strlen(l) - strlen(line)))
>> >>     let c = ''
>> >>     if l =~ '^\s*class\s' | let c = '(c)--' | endif
>> >>     let line = prefix . '+--' . c . '  ' . line
>> >
>> >You can also use the indent() function to get the indentation of the
>> >line directly instead of calculating string lengths, e.g.,
>> >
>> >     let n = v:foldend - v:foldstart + 1
>> >     let i = indent(v:foldstart)
>> >     return repeat(' ', i) . "+-" . v:folddashes . " " . n . " lines "
>
>> Thanks! I know that now, but I wrote this function a
>> long time ago..  -ak
>
> You're welcome.  I should thank you, too.  I also wrote my function
> a long time ago and until now used a while loop instead of repeat()
> to create the indent prefix.
>

Inspired by all received answers, I finally came up with a foldtext
function that positions the fold text exactly right, namely
indent(v:foldstart) - foldlevel(v:foldstart) spaces:

function! VhoFoldText() " show the text before the fold marker {{{2
    let n = v:foldend - v:foldstart + 1
    let l = getline(v:foldstart)
    let i = indent(l)
    let j = foldlevel(l)
    let t = substitute(l, '\*\|\*/\|{\+\d\=', '('.n.' lines)', 'g')
    return repeat(' ', i-j) . t
endfunction

Thank you all, and kind regards,


Guido

--
I have a hobby.  I have the world's largest collection of sea shells.
I keep it scattered on beaches all over the world.  Maybe you've seen
some of it.
                -- Steven Wright

--
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
Reply | Threaded
Open this post in threaded view
|

Re: foldtext question

Guido Van Hoecke-2
On Mon, Jan 16, 2012 at 09:24, Guido Van Hoecke <[hidden email]> wrote:

> On Sun, Jan 15, 2012 at 20:11, Gary Johnson <[hidden email]> wrote:
>> On 2012-01-15, AK wrote:
>>> On 01/15/2012 01:56 PM, Gary Johnson wrote:
>>> >On 2012-01-15, AK wrote:
>>> >
>>> >>It does work for me but I'm dealing with lines that have
>>> >>leading spaces and I add leading spaces, here is a relevant
>>> >>snip:
>>> >>
>>> >>     let l = getline(v:foldstart)
>>> >>     let line = substitute(l, '^[ ]*', '', '')
>>> >>     let prefix = repeat(' ', (strlen(l) - strlen(line)))
>>> >>     let c = ''
>>> >>     if l =~ '^\s*class\s' | let c = '(c)--' | endif
>>> >>     let line = prefix . '+--' . c . '  ' . line
>>> >
>>> >You can also use the indent() function to get the indentation of the
>>> >line directly instead of calculating string lengths, e.g.,
>>> >
>>> >     let n = v:foldend - v:foldstart + 1
>>> >     let i = indent(v:foldstart)
>>> >     return repeat(' ', i) . "+-" . v:folddashes . " " . n . " lines "
>>
>>> Thanks! I know that now, but I wrote this function a
>>> long time ago..  -ak
>>
>> You're welcome.  I should thank you, too.  I also wrote my function
>> a long time ago and until now used a while loop instead of repeat()
>> to create the indent prefix.
>>
>
> Inspired by all received answers, I finally came up with a foldtext
> function that positions the fold text exactly right, namely
> indent(v:foldstart) - foldlevel(v:foldstart) spaces:
>
> function! VhoFoldText() " show the text before the fold marker {{{2
>    let n = v:foldend - v:foldstart + 1
>    let l = getline(v:foldstart)
>    let i = indent(l)
>    let j = foldlevel(l)
>    let t = substitute(l, '\*\|\*/\|{\+\d\=', '('.n.' lines)', 'g')
>    return repeat(' ', i-j) . t
> endfunction

oops, this should be:
function! VhoFoldText() " show the text before the fold marker {{{2
    let n = v:foldend - v:foldstart + 1
    let i = indent(v:foldstart)
    let j = foldlevel(v:foldstart)
    let l = getline(v:foldstart)
    let t = substitute(l, '\*\|\*/\|{\+\d\=', '('.n.' lines)', 'g')
    return repeat(' ', i-j) . t
endfunction

Sorry for the erroneous version,

Kind regards,


Guido

--
Law of the Yukon:
        Only the lead dog gets a change of scenery.

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