foldexpr correct ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

foldexpr correct ?

Stephan Sinz
I want to fold lines starting with similar ip addresses in a log file.

 ":set
foldexpr=getline(v:lnum)=~'^\\([0-9]\\+\\)\\.'&&getline(v:lnum+1)=~'^\\1\\.'"

doesn't work for folding similar ip adresses. what's wrong ?

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: foldexpr correct ?

Christian Brabandt

On Do, 03 Aug 2017, Stephan Sinz wrote:

> I want to fold lines starting with similar ip addresses in a log file.
>
>  ":set
> foldexpr=getline(v:lnum)=~'^\\([0-9]\\+\\)\\.'&&getline(v:lnum+1)=~'^\\1\\.'"
>
> doesn't work for folding similar ip adresses. what's wrong ?

That doesn't work, because the =~ operation does not understand the \1
marked group. BTW: for more complex expressions, it is a alot easier to
write specific functions, instead on trying to squeeze everything into a
single command line and having to double escape all kind of things...

This should work:

function! MyFoldExpr(lnum)
    let content = getline(a:lnum)
    let next_cont = getline(a:lnum+1)
    if content =~ '\.' && next_cont =~ '\.'
        if split(content, '\.')[0] == split(next_cont, '\.')[0]
            return 1
        else
            " does not match, fold should end here
            return '<1'
        endif
    else
        " use foldlevel from previous line, if line contains a '.'
        return content =~ '\.' ? '=' : 0
    endif
endfunction


Best,
Christian
--
Manche wären sehr erstaunt zu erfahren, worauf ihre Achtung vor den
Menschen beruht.
                -- Luc de Clapiers Vauvenargues (Nachgelassene Maximen)

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: foldexpr correct ?

Stephan Sinz
Hello Christian,
Thanks for your sophisticated solution.
Works like a charm.
Thinking I'm a Pro, you showed me the limits.Thx!

Offtopic:
What software do you recommend for daily use
(browser,email-client,development,...) ?
Suckless and simple ?

Best Regards

Stephan
On Thu, Aug 03, 2017 at 09:03:56PM +0200, Christian Brabandt wrote:

>
> On Do, 03 Aug 2017, Stephan Sinz wrote:
>
> > I want to fold lines starting with similar ip addresses in a log file.
> >
> >  ":set
> > foldexpr=getline(v:lnum)=~'^\\([0-9]\\+\\)\\.'&&getline(v:lnum+1)=~'^\\1\\.'"
> >
> > doesn't work for folding similar ip adresses. what's wrong ?
>
> That doesn't work, because the =~ operation does not understand the \1
> marked group. BTW: for more complex expressions, it is a alot easier to
> write specific functions, instead on trying to squeeze everything into a
> single command line and having to double escape all kind of things...
>
> This should work:
>
> function! MyFoldExpr(lnum)
>     let content = getline(a:lnum)
>     let next_cont = getline(a:lnum+1)
>     if content =~ '\.' && next_cont =~ '\.'
>         if split(content, '\.')[0] == split(next_cont, '\.')[0]
>             return 1
>         else
>             " does not match, fold should end here
>             return '<1'
>         endif
>     else
>         " use foldlevel from previous line, if line contains a '.'
>         return content =~ '\.' ? '=' : 0
>     endif
> endfunction
>
>
> Best,
> Christian
> --
> Manche wären sehr erstaunt zu erfahren, worauf ihre Achtung vor den
> Menschen beruht.
> -- Luc de Clapiers Vauvenargues (Nachgelassene Maximen)
>
> --
> --
> 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
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: foldexpr correct ?

Christian Brabandt

On Fr, 04 Aug 2017, Stephan Sinz wrote:

> Offtopic:
> What software do you recommend for daily use
> (browser,email-client,development,...) ?
> Suckless and simple ?

I can't. I use whatever I have access to and to which I am accustomed.

I would say, use whatever works the best for you, don't listen to any
stranger on the Internet.

Best,
Christian
--
Wer glücklich ist, fühlt, wer unglücklich ist, denkt.
                -- Joachim Fernau

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...