lines folding by pattern ; was Re: hiding lines

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

lines folding by pattern ; was Re: hiding lines

iler.ml
On 5/4/06, Jack Donohue <[hidden email]> wrote:

> > :v (and :g) made my day..!
>
> Yes, I use this a lot if I just want to filter out a set of lines, or see
> only lines containing some text (like the old XEDIT command).  But what I'd
> really like to to is continue editing in this view and not lose all the
> hidden lines, more like folding.  I expect folding can do this, but I've
> never gotten it to work and it seems pretty cumbersome.  I'm looking for a
> simple solution involving maybe a couple of command I can make a map for.
>
> I guess I'm looking for a g or v command that folds the lines instead of
> actually deleting them.

I guess the mappings below are simple enough. If you prefer
to keep it to just couple of lines, you can remove mapping for <F5>
and whole function TogglePatternBasedFolding(). What remains is
just 3 lines (maps for f2, f3, and f3). But I find tri-state toggle
(f5) also useful.

" Simple pattern-based folding.
" How to use: (1) perform searh (so that vim remebers search pattern),
"           (2) press f2, f3 f4, or press f5 repeatedly.
"
" F2 - fold lines that match current-search-pattern (cf :v//p)
" F3 - fold lines that do not match current-search-pattern (cf :g//p)
" F4 - open all folds
" F5 - tri-state toggle between f2-f3-f4

map <silent><F2> :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr
foldenable<cr>zM
map <silent><F3> :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr
foldenable<cr>zM
map <silent><f4> :set nofoldenable<cr>
map <silent><f5> :call TogglePatternBasedFolding()<cr>

function! TogglePatternBasedFolding()
    if &foldenable && &foldmethod=='expr' && &foldexpr[0] == '!'
        :set nofoldenable | redraw | echo "all folds opened"
    elseif &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
        :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr foldenable
        normal zM
        redraw | echo "lines not matching /".@/."/ folded"
    else " &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
        :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr foldenable
        normal zM
        redraw | echo "lines matching /".@/."/ folded"
    endif
endfun


Yakov
Reply | Threaded
Open this post in threaded view
|

Re: lines folding by pattern ; was Re: hiding lines

Hari Krishna Dara

On Thu, 4 May 2006 at 8:54pm, Yakov Lerner wrote:

> On 5/4/06, Jack Donohue <[hidden email]> wrote:
> > > :v (and :g) made my day..!
> >
> > Yes, I use this a lot if I just want to filter out a set of lines, or see
> > only lines containing some text (like the old XEDIT command).  But what I'd
> > really like to to is continue editing in this view and not lose all the
> > hidden lines, more like folding.  I expect folding can do this, but I've
> > never gotten it to work and it seems pretty cumbersome.  I'm looking for a
> > simple solution involving maybe a couple of command I can make a map for.
> >
> > I guess I'm looking for a g or v command that folds the lines instead of
> > actually deleting them.
>
> I guess the mappings below are simple enough. If you prefer
> to keep it to just couple of lines, you can remove mapping for <F5>
> and whole function TogglePatternBasedFolding(). What remains is
> just 3 lines (maps for f2, f3, and f3). But I find tri-state toggle
> (f5) also useful.
>
> " Simple pattern-based folding.
> " How to use: (1) perform searh (so that vim remebers search pattern),
> "           (2) press f2, f3 f4, or press f5 repeatedly.
> "
> " F2 - fold lines that match current-search-pattern (cf :v//p)
> " F3 - fold lines that do not match current-search-pattern (cf :g//p)
> " F4 - open all folds
> " F5 - tri-state toggle between f2-f3-f4
>
> map <silent><F2> :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr
> foldenable<cr>zM
> map <silent><F3> :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr
> foldenable<cr>zM
> map <silent><f4> :set nofoldenable<cr>
> map <silent><f5> :call TogglePatternBasedFolding()<cr>
>
> function! TogglePatternBasedFolding()
>     if &foldenable && &foldmethod=='expr' && &foldexpr[0] == '!'
>         :set nofoldenable | redraw | echo "all folds opened"
>     elseif &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
>         :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr foldenable
>         normal zM
>         redraw | echo "lines not matching /".@/."/ folded"
>     else " &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
>         :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr foldenable
>         normal zM
>         redraw | echo "lines matching /".@/."/ folded"
>     endif
> endfun
>
>
> Yakov

May I suggest that you take a look at my foldutil.vim? It allows folding
by matching lines as well as other criterion, and also gives you the
flexibility of specifying a context.

http://www.vim.org/scripts/script.php?script_id=158

--
Thanks,
Hari

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
Reply | Threaded
Open this post in threaded view
|

Re: lines folding by pattern ; was Re: hiding lines

William Pursell
In reply to this post by iler.ml
Yakov Lerner wrote:

> On 5/4/06, Jack Donohue <[hidden email]> wrote:
>
>> > :v (and :g) made my day..!
>>
>> Yes, I use this a lot if I just want to filter out a set of lines, or see
>> only lines containing some text (like the old XEDIT command).  But
>> what I'd
>> really like to to is continue editing in this view and not lose all the
>> hidden lines, more like folding.  I expect folding can do this, but I've
>> never gotten it to work and it seems pretty cumbersome.  I'm looking
>> for a
>> simple solution involving maybe a couple of command I can make a map for.
>>
>> I guess I'm looking for a g or v command that folds the lines instead of
>> actually deleting them.
>
>
> I guess the mappings below are simple enough. If you prefer
> to keep it to just couple of lines, you can remove mapping for <F5>
> and whole function TogglePatternBasedFolding(). What remains is
> just 3 lines (maps for f2, f3, and f3). But I find tri-state toggle
> (f5) also useful.
>
> " Simple pattern-based folding.
> " How to use: (1) perform searh (so that vim remebers search pattern),
> "           (2) press f2, f3 f4, or press f5 repeatedly.
> "
> " F2 - fold lines that match current-search-pattern (cf :v//p)
> " F3 - fold lines that do not match current-search-pattern (cf :g//p)
> " F4 - open all folds
> " F5 - tri-state toggle between f2-f3-f4
>
> map <silent><F2> :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr
> foldenable<cr>zM
> map <silent><F3> :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr
> foldenable<cr>zM
> map <silent><f4> :set nofoldenable<cr>
> map <silent><f5> :call TogglePatternBasedFolding()<cr>
>
> function! TogglePatternBasedFolding()
>    if &foldenable && &foldmethod=='expr' && &foldexpr[0] == '!'
>        :set nofoldenable | redraw | echo "all folds opened"
>    elseif &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
>        :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr foldenable
>        normal zM
>        redraw | echo "lines not matching /".@/."/ folded"
>    else " &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
>        :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr foldenable
>        normal zM
>        redraw | echo "lines matching /".@/."/ folded"
>    endif
> endfun
>

That type of thing works nicely, except for the fact that as
soon as you set foldmethod=expr, you lose any other syntax based
folding that's in place.  Is it possible to do something like
foldmethod+=expr, so that the syntax based folding remains in
effect?  I don't believe that can be done, but I'd love to
hear suggestions for a work-around.

I originally was hoping to do  what Jack was asking for, which is
to hide the lines and continue working in the buffer, but in
retrospect, that may not be particularly safe.  Filtering to
a temporary buffer will probably satisfy me.

Thanks for all the suggestions.
Reply | Threaded
Open this post in threaded view
|

Re: lines folding by pattern ; was Re: hiding lines

iler.ml
On 5/6/06, Bill Pursell <[hidden email]> wrote:

> Yakov Lerner wrote:
> > On 5/4/06, Jack Donohue <[hidden email]> wrote:
> >
> >> > :v (and :g) made my day..!
> >>
> >> Yes, I use this a lot if I just want to filter out a set of lines, or see
> >> only lines containing some text (like the old XEDIT command).  But
> >> what I'd
> >> really like to to is continue editing in this view and not lose all the
> >> hidden lines, more like folding.  I expect folding can do this, but I've
> >> never gotten it to work and it seems pretty cumbersome.  I'm looking
> >> for a
> >> simple solution involving maybe a couple of command I can make a map for.
> >>
> >> I guess I'm looking for a g or v command that folds the lines instead of
> >> actually deleting them.
> >
> >
> > I guess the mappings below are simple enough. If you prefer
> > to keep it to just couple of lines, you can remove mapping for <F5>
> > and whole function TogglePatternBasedFolding(). What remains is
> > just 3 lines (maps for f2, f3, and f3). But I find tri-state toggle
> > (f5) also useful.
> >
> > " Simple pattern-based folding.
> > " How to use: (1) perform searh (so that vim remebers search pattern),
> > "           (2) press f2, f3 f4, or press f5 repeatedly.
> > "
> > " F2 - fold lines that match current-search-pattern (cf :v//p)
> > " F3 - fold lines that do not match current-search-pattern (cf :g//p)
> > " F4 - open all folds
> > " F5 - tri-state toggle between f2-f3-f4
> >
> > map <silent><F2> :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr
> > foldenable<cr>zM
> > map <silent><F3> :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr
> > foldenable<cr>zM
> > map <silent><f4> :set nofoldenable<cr>
> > map <silent><f5> :call TogglePatternBasedFolding()<cr>
> >
> > function! TogglePatternBasedFolding()
> >    if &foldenable && &foldmethod=='expr' && &foldexpr[0] == '!'
> >        :set nofoldenable | redraw | echo "all folds opened"
> >    elseif &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
> >        :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr foldenable
> >        normal zM
> >        redraw | echo "lines not matching /".@/."/ folded"
> >    else " &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
> >        :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr foldenable
> >        normal zM
> >        redraw | echo "lines matching /".@/."/ folded"
> >    endif
> > endfun
> >
>
> That type of thing works nicely, except for the fact that as
> soon as you set foldmethod=expr, you lose any other syntax based
> folding that's in place.  Is it possible to do something like
> foldmethod+=expr, so that the syntax based folding remains in
> effect?  I don't believe that can be done, but I'd love to
> hear suggestions for a work-around.
>
> I originally was hoping to do  what Jack was asking for, which is
> to hide the lines and continue working in the buffer, but in
> retrospect, that may not be particularly safe.  Filtering to
> a temporary buffer will probably satisfy me.
>
> Thanks for all the suggestions.

It's easy to add saving of preexisting fold-rules in the
mappings. In the toggle mapping, the 3rd state would
be restoration of old mappings instead of disabled folding.

You can do it yourself.
It's easy. Just assign &foldmethod, &foldexpr &foldenable
etc to global variables, and make a mapping to restore it.

Yakov
Reply | Threaded
Open this post in threaded view
|

Re: lines folding by pattern ; was Re: hiding lines

William Pursell
Yakov Lerner wrote:

> On 5/6/06, Bill Pursell <[hidden email]> wrote:
>
>> Yakov Lerner wrote:
>> > On 5/4/06, Jack Donohue <[hidden email]> wrote:
>> >
>> >> > :v (and :g) made my day..!
>> >>
>> >> Yes, I use this a lot if I just want to filter out a set of lines,
>> or see
>> >> only lines containing some text (like the old XEDIT command).  But
>> >> what I'd
>> >> really like to to is continue editing in this view and not lose all
>> the
>> >> hidden lines, more like folding.  I expect folding can do this, but
>> I've
>> >> never gotten it to work and it seems pretty cumbersome.  I'm looking
>> >> for a
>> >> simple solution involving maybe a couple of command I can make a
>> map for.
>> >>
>> >> I guess I'm looking for a g or v command that folds the lines
>> instead of
>> >> actually deleting them.
>> >
>> >
>> > I guess the mappings below are simple enough. If you prefer
>> > to keep it to just couple of lines, you can remove mapping for <F5>
>> > and whole function TogglePatternBasedFolding(). What remains is
>> > just 3 lines (maps for f2, f3, and f3). But I find tri-state toggle
>> > (f5) also useful.
>> >
>> > " Simple pattern-based folding.
>> > " How to use: (1) perform searh (so that vim remebers search pattern),
>> > "           (2) press f2, f3 f4, or press f5 repeatedly.
>> > "
>> > " F2 - fold lines that match current-search-pattern (cf :v//p)
>> > " F3 - fold lines that do not match current-search-pattern (cf :g//p)
>> > " F4 - open all folds
>> > " F5 - tri-state toggle between f2-f3-f4
>> >
>> > map <silent><F2> :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr
>> > foldenable<cr>zM
>> > map <silent><F3> :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr
>> > foldenable<cr>zM
>> > map <silent><f4> :set nofoldenable<cr>
>> > map <silent><f5> :call TogglePatternBasedFolding()<cr>
>> >
>> > function! TogglePatternBasedFolding()
>> >    if &foldenable && &foldmethod=='expr' && &foldexpr[0] == '!'
>> >        :set nofoldenable | redraw | echo "all folds opened"
>> >    elseif &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
>> >        :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr foldenable
>> >        normal zM
>> >        redraw | echo "lines not matching /".@/."/ folded"
>> >    else " &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
>> >        :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr foldenable
>> >        normal zM
>> >        redraw | echo "lines matching /".@/."/ folded"
>> >    endif
>> > endfun
>> >
>>
>> That type of thing works nicely, except for the fact that as
>> soon as you set foldmethod=expr, you lose any other syntax based
>> folding that's in place.  Is it possible to do something like
>> foldmethod+=expr, so that the syntax based folding remains in
>> effect?  I don't believe that can be done, but I'd love to
>> hear suggestions for a work-around.
>>
>> I originally was hoping to do  what Jack was asking for, which is
>> to hide the lines and continue working in the buffer, but in
>> retrospect, that may not be particularly safe.  Filtering to
>> a temporary buffer will probably satisfy me.
>>
>> Thanks for all the suggestions.
>
>
> It's easy to add saving of preexisting fold-rules in the
> mappings. In the toggle mapping, the 3rd state would
> be restoration of old mappings instead of disabled folding.
>
> You can do it yourself.
> It's easy. Just assign &foldmethod, &foldexpr &foldenable
> etc to global variables, and make a mapping to restore it.
>

I don't just want it restored, though.  I want both types of
folding enabled simultaneously.

Reply | Threaded
Open this post in threaded view
|

Re: lines folding by pattern ; was Re: hiding lines

iler.ml
On 5/7/06, Bill Pursell <[hidden email]> wrote:

> Yakov Lerner wrote:
> > On 5/6/06, Bill Pursell <[hidden email]> wrote:
> >
> >> Yakov Lerner wrote:
> >> > On 5/4/06, Jack Donohue <[hidden email]> wrote:
> >> >
> >> >> > :v (and :g) made my day..!
> >> >>
> >> >> Yes, I use this a lot if I just want to filter out a set of lines,
> >> or see
> >> >> only lines containing some text (like the old XEDIT command).  But
> >> >> what I'd
> >> >> really like to to is continue editing in this view and not lose all
> >> the
> >> >> hidden lines, more like folding.  I expect folding can do this, but
> >> I've
> >> >> never gotten it to work and it seems pretty cumbersome.  I'm looking
> >> >> for a
> >> >> simple solution involving maybe a couple of command I can make a
> >> map for.
> >> >>
> >> >> I guess I'm looking for a g or v command that folds the lines
> >> instead of
> >> >> actually deleting them.
> >> >
> >> >
> >> > I guess the mappings below are simple enough. If you prefer
> >> > to keep it to just couple of lines, you can remove mapping for <F5>
> >> > and whole function TogglePatternBasedFolding(). What remains is
> >> > just 3 lines (maps for f2, f3, and f3). But I find tri-state toggle
> >> > (f5) also useful.
> >> >
> >> > " Simple pattern-based folding.
> >> > " How to use: (1) perform searh (so that vim remebers search pattern),
> >> > "           (2) press f2, f3 f4, or press f5 repeatedly.
> >> > "
> >> > " F2 - fold lines that match current-search-pattern (cf :v//p)
> >> > " F3 - fold lines that do not match current-search-pattern (cf :g//p)
> >> > " F4 - open all folds
> >> > " F5 - tri-state toggle between f2-f3-f4
> >> >
> >> > map <silent><F2> :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr
> >> > foldenable<cr>zM
> >> > map <silent><F3> :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr
> >> > foldenable<cr>zM
> >> > map <silent><f4> :set nofoldenable<cr>
> >> > map <silent><f5> :call TogglePatternBasedFolding()<cr>
> >> >
> >> > function! TogglePatternBasedFolding()
> >> >    if &foldenable && &foldmethod=='expr' && &foldexpr[0] == '!'
> >> >        :set nofoldenable | redraw | echo "all folds opened"
> >> >    elseif &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
> >> >        :set foldexpr=!(getline(v:lnum)=~@/) foldmethod=expr foldenable
> >> >        normal zM
> >> >        redraw | echo "lines not matching /".@/."/ folded"
> >> >    else " &foldenable && &foldmethod=='expr' && &foldexpr[0] == 'g'
> >> >        :set foldexpr=getline(v:lnum)=~@/ foldmethod=expr foldenable
> >> >        normal zM
> >> >        redraw | echo "lines matching /".@/."/ folded"
> >> >    endif
> >> > endfun
> >> >
> >>
> >> That type of thing works nicely, except for the fact that as
> >> soon as you set foldmethod=expr, you lose any other syntax based
> >> folding that's in place.  Is it possible to do something like
> >> foldmethod+=expr, so that the syntax based folding remains in
> >> effect?  I don't believe that can be done, but I'd love to
> >> hear suggestions for a work-around.
> >>
> >> I originally was hoping to do  what Jack was asking for, which is
> >> to hide the lines and continue working in the buffer, but in
> >> retrospect, that may not be particularly safe.  Filtering to
> >> a temporary buffer will probably satisfy me.
> >>
> >> Thanks for all the suggestions.
> >
> >
> > It's easy to add saving of preexisting fold-rules in the
> > mappings. In the toggle mapping, the 3rd state would
> > be restoration of old mappings instead of disabled folding.
> >
> > You can do it yourself.
> > It's easy. Just assign &foldmethod, &foldexpr &foldenable
> > etc to global variables, and make a mapping to restore it.
> >
>
> I don't just want it restored, though.  I want both types of
> folding enabled simultaneously.

I think vim cannot do this.

Otoh, leaving foldmethod=expr,
and playing with expressions (||,&& etc) you can possibly
achieve something like this.

Yakov