hiding lines

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

hiding lines

William Pursell

Is it possible to completely hide lines?  Something stronger than merely
folding.  In particular, I'd like to be able to display the buffer with
all lines containing "assert" hidden, or to hide lines between and
including #ifdef/#endif pairs.  Can that be done?
Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

iler.ml
On 5/1/06, Bill Pursell <[hidden email]> wrote:
>
> Is it possible to completely hide lines?  Something stronger than merely
> folding.  In particular, I'd like to be able to display the buffer with
> all lines containing "assert" hidden, or to hide lines between and
> including #ifdef/#endif pairs.  Can that be done?

Piping the buffer text into temp. buffer through some sort
of grep -v.

Yakov
Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

cga2000
Thus spake Yakov Lerner on Mon, May 01, 2006 at 10:06:57PM +0300 or thereabouts: <[hidden email]> [2006-05-01 17:25]:

> On 5/1/06, Bill Pursell <[hidden email]> wrote:
> >
> >Is it possible to completely hide lines?  Something stronger than merely
> >folding.  In particular, I'd like to be able to display the buffer with
> >all lines containing "assert" hidden, or to hide lines between and
> >including #ifdef/#endif pairs.  Can that be done?
>
> Piping the buffer text into temp. buffer through some sort
> of grep -v.
>

could you be a little more specific - or provide a short example.. been
looking for something like this myself for some time..


thanks,

cga

Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

Gary Johnson
On 2006-05-01, cga2000 <[hidden email]> wrote:

> Thus spake Yakov Lerner on Mon, May 01, 2006 at 10:06:57PM +0300 or thereabouts: <[hidden email]> [2006-05-01 17:25]:
> > On 5/1/06, Bill Pursell <[hidden email]> wrote:
> > >
> > >Is it possible to completely hide lines?  Something stronger than merely
> > >folding.  In particular, I'd like to be able to display the buffer with
> > >all lines containing "assert" hidden, or to hide lines between and
> > >including #ifdef/#endif pairs.  Can that be done?
> >
> > Piping the buffer text into temp. buffer through some sort
> > of grep -v.
> >
>
> could you be a little more specific - or provide a short example.. been
> looking for something like this myself for some time..

I can't think of anything 'grep -v <pattern>' can do in this
situation that 'v/<patter>/d' can't, so here's one way to do this:

    gg"aYG
    :new
    "ap
    :1d
    :v/assert/d

HTH,
Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

cga2000
Thus spake Gary Johnson on Mon, May 01, 2006 at 02:52:21PM -0700 or thereabouts: <[hidden email]> [2006-05-01 19:31]:

> On 2006-05-01, cga2000 <[hidden email]> wrote:
> > Thus spake Yakov Lerner on Mon, May 01, 2006 at 10:06:57PM +0300 or thereabouts: <[hidden email]> [2006-05-01 17:25]:
> > > On 5/1/06, Bill Pursell <[hidden email]> wrote:
> > > >
> > > >Is it possible to completely hide lines?  Something stronger than merely
> > > >folding.  In particular, I'd like to be able to display the buffer with
> > > >all lines containing "assert" hidden, or to hide lines between and
> > > >including #ifdef/#endif pairs.  Can that be done?
> > >
> > > Piping the buffer text into temp. buffer through some sort
> > > of grep -v.
> > >
> >
> > could you be a little more specific - or provide a short example.. been
> > looking for something like this myself for some time..
>
> I can't think of anything 'grep -v <pattern>' can do in this
> situation that 'v/<patter>/d' can't, so here's one way to do this:
>
>     gg"aYG
>     :new
>     "ap
>     :1d
>     :v/assert/d
>
> HTH,
> Gary

Thanks, Gary..

:v (and :g) made my day..!

>
> --
> Gary Johnson                 | Agilent Technologies
> [hidden email]     | Wireless Division
>                              | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

iler.ml
In reply to this post by Gary Johnson
On 5/2/06, Gary Johnson <[hidden email]> wrote:

> > Thus spake Yakov Lerner on Mon, May 01, 2006 at 10:06:57PM +0300 or thereabouts: <[hidden email]> [2006-05-01 17:25]:
> > > On 5/1/06, Bill Pursell <[hidden email]> wrote:
> > > >Is it possible to completely hide lines?  Something stronger than merely
> > > >folding.  In particular, I'd like to be able to display the buffer with
> > > >all lines containing "assert" hidden, or to hide lines between and
> > > >including #ifdef/#endif pairs.  Can that be done?
> > >
> > > Piping the buffer text into temp. buffer through some sort
> > > of grep -v.
> I can't think of anything 'grep -v <pattern>' can do in this
> situation that 'v/<patter>/d' can't, so here's one way to do this:

Yes, 'grep -v' in vim is easy. But how do you remove #ifdef blocks ?
I mentioned piping because there is ready utility, 'unifdef',
that removes some or all of  #if blocks.

Yakov

>     gg"aYG
>     :new
>     "ap
>     :1d
>     :v/assert/d
Reply | Threaded
Open this post in threaded view
|

RE: hiding lines

Suresh Govindachar`

  Yakov Lerner wondered:

  > But how do you remove #ifdef blocks?  I mentioned piping because
  > there is ready utility, 'unifdef', that removes some or all of
  > #if blocks.

  Isn't there a way to do a multi-line substitution:

  :%s/^\s*#ifdef .*^\s*#endif//

  where the *s are multi-line and non-greedy, or maybe I should
  say the *s are non-greedy and the . is multi-line?  (I haven't
  actually tried, but I am confident I can do it in perl.)

  --Suresh



Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

iler.ml
On 5/2/06, Suresh Govindachar <[hidden email]> wrote:

>
>   Yakov Lerner wondered:
>
>   > But how do you remove #ifdef blocks?  I mentioned piping because
>   > there is ready utility, 'unifdef', that removes some or all of
>   > #if blocks.
>
>   Isn't there a way to do a multi-line substitution:
>
>   :%s/^\s*#ifdef .*^\s*#endif//
>
>   where the *s are multi-line and non-greedy, or maybe I should
>   say the *s are non-greedy and the . is multi-line?  (I haven't
>   actually tried, but I am confident I can do it in perl.)

What if #if/#endif blocks are nested ?

Yakov
Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

Tim Chase-2
> What if #if/#endif blocks are nested ?

Yakov is correct, that nested #if/#endif blocks would cause
trouble.

My first thought at a [100% untested] solution would be
something like

:g/^\s*#endif/norm dV%

which would find all of the #endif tokens and delete their
associated blocks, exploiting Vim's ability to use "%" to
match #if/#else/#endif blocks.  However, funky behaviors
emerge if you have an #else block, and you want some of it
in to remain behind:

#if COWS_ROLLERSKATE
        foo = "limbo, limbo";
#else
        foo = "mu"
#endif


With my suggestion, you would delete the whole thing, and it
would look like "foo" never gets set.  It also nukes *all*
the if/endif blocks, not just select ones (such as if you
only wanted to nuke the "#if COWS_ROLLERSKATE" blocks, but
not "#if POPE_CATHOLIC" blocks)

If you don't have #else clauses, you might be able to solve
that 2nd problem with something like

:g/^\s*#if COWS_ROLLERSKATE/norm dV%

Otherwise, you'd have to do some funky testing, along the
lines of (again, 100% untested)

:g/^\s*#if COWS/exec("norm %")| exec('norm
'.(getline(".")=~'^\s*#else'?'%':'').'dV%')

which should check if, after using "%", you landed on an
#else line, in which case it adds an extra "%" to go to the
"#endif" line before deleteing back to the original.  All
sorts of odd cases occur with nesting and #else blocks, so
you'd have to tinker to get the desired behavior.

-tim



Reply | Threaded
Open this post in threaded view
|

RE: hiding lines

Suresh Govindachar`
In reply to this post by iler.ml
 
  On May 01, 2006, Yakov Lerner pointed out:
 
  >On 5/2/06, Suresh Govindachar wrote:
  >>
  >> Yakov Lerner wondered:
  >>
  >>> But how do you remove #ifdef blocks?  I mentioned piping
  >>> because there is ready utility, 'unifdef', that removes some
  >>> or all of #if blocks.
  >>
  >>   Isn't there a way to do a multi-line substitution:
  >>
  >>   :%s/^\s*#ifdef .*^\s*#endif//
  >>
  >>   where the *s are multi-line and non-greedy, or maybe I should
  >>   say the *s are non-greedy and the . is multi-line?  (I
  >>   haven't actually tried, but I am confident I can do it in
  >>   perl.)
  >
  > What if #if/#endif blocks are nested ?
 
  [In the above pseudo :%s expression, replace ifdef by just if.]
  I can think of three approaches, the second and third of which I
  have tested successfully.  While the third is elegant for deleting
  #if/#endif blocks, the second is much more flexible.

  1) Have . not match ^\s*#if -- so that we can get rid of
     inner-most #if/#endif blocks.  Repeat this in a while loop till
     there are no more ^\s*#if in the buffer.

  2) I successfully tested the following all-in-one-line command:

     :perl my $skip=0; my @extract=(); foreach my $line ($curbuf->Get(1 .. VIM::Eval('line("$")'))) { $line =~ /^\s*#if/ and
$skip++; $skip or push @extract, $line; $line =~ /^\s*#endif/ and $skip--;} VIM::DoCommand('new'); $curbuf->Append(1, @extract);
VIM::DoCommand('1d');

     with a file that looked like (note the nested, unaligned #if):

            stay
            stay, next blank too
           
               #if
           
             go away, previous blank too
           
             go away, previous blank too
             go away
            #if
             go away
           
             go away, previous blank too
                       #endif
           
             some more go away
            #endif
           
            stay, previous blank too
            stay
           
            stay, previous blank too
            stay
            very last stay

  3) The following works on the above example

        :%/^\s*#if/normal d%dd

  Of course, the perl solution can be translated to other
  languages, including viml.

  --Suresh

Reply | Threaded
Open this post in threaded view
|

RE: hiding lines

Suresh Govindachar`
In reply to this post by William Pursell

 
[In reporting the third solution below, I forgot the g]

  On May 01, 2006, Yakov Lerner pointed out:
 
  >On 5/2/06, Suresh Govindachar wrote:
  >>
  >> Yakov Lerner wondered:
  >>
  >>> But how do you remove #ifdef blocks?  I mentioned piping
  >>> because there is ready utility, 'unifdef', that removes some
  >>> or all of #if blocks.
  >>
  >>   Isn't there a way to do a multi-line substitution:
  >>
  >>   :%s/^\s*#ifdef .*^\s*#endif//
  >>
  >>   where the *s are multi-line and non-greedy, or maybe I should
  >>   say the *s are non-greedy and the . is multi-line?  (I
  >>   haven't actually tried, but I am confident I can do it in
  >>   perl.)
  >
  > What if #if/#endif blocks are nested ?
 
  [In the above pseudo :%s expression, replace ifdef by just if.]
  I can think of three approaches, the second and third of which I
  have tested successfully.  While the third is elegant for deleting
  #if/#endif blocks, the second is much more flexible.

  1) Have . not match ^\s*#if -- so that we can get rid of
     inner-most #if/#endif blocks.  Repeat this in a while loop till
     there are no more ^\s*#if in the buffer.

  2) I successfully tested the following all-in-one-line command:

     :perl my $skip=0; my @extract=(); foreach my $line ($curbuf->Get(1 .. VIM::Eval('line("$")'))) { $line =~ /^\s*#if/ and
$skip++; $skip or push @extract, $line; $line =~ /^\s*#endif/ and $skip--;} VIM::DoCommand('new'); $curbuf->Append(1, @extract);
VIM::DoCommand('1d');

     with a file that looked like (note the nested, unaligned #if):

            stay
            stay, next blank too
           
               #if
           
             go away, previous blank too
           
             go away, previous blank too
             go away
            #if
             go away
           
             go away, previous blank too
                       #endif
           
             some more go away
            #endif
           
            stay, previous blank too
            stay
           
            stay, previous blank too
            stay
            very last stay

  3) The following works on the above example

        :%/^\s*#if/normal d%dd

  Of course, the perl solution can be translated to other
  languages, including viml.

  --Suresh

Reply | Threaded
Open this post in threaded view
|

RE: hiding lines

Suresh Govindachar`

[Modified the third solution]

  On May 01, 2006, Yakov Lerner pointed out:
 
  >On 5/2/06, Suresh Govindachar wrote:
  >>
  >> Yakov Lerner wondered:
  >>
  >>> But how do you remove #ifdef blocks?  I mentioned piping
  >>> because there is ready utility, 'unifdef', that removes some
  >>> or all of #if blocks.
  >>
  >>   Isn't there a way to do a multi-line substitution:
  >>
  >>   :%s/^\s*#ifdef .*^\s*#endif//
  >>
  >>   where the *s are multi-line and non-greedy, or maybe I should
  >>   say the *s are non-greedy and the . is multi-line?  (I
  >>   haven't actually tried, but I am confident I can do it in
  >>   perl.)
  >
  > What if #if/#endif blocks are nested ?
 
  [In the above pseudo :%s expression, replace ifdef by just if.]
  I can think of three approaches, the second and third of which I
  have tested successfully.  While the third is elegant for deleting
  #if/#endif blocks, the second is much more flexible.

  1) Have . not match ^\s*#if -- so that we can get rid of
     inner-most #if/#endif blocks.  Repeat this in a while
     loop till there are no more ^\s*#if in the buffer.

  2) I successfully tested the following all-in-one-line command
     (although it is written in multiple lines to make it easy
     to read):

     :perl my $skip=0; my @extract=();
           foreach my $line ($curbuf->Get(1 .. VIM::Eval('line("$")')))
           {
             $line =~ /^\s*#if/ and $skip++;
             $skip or push @extract, $line;
             $line =~ /^\s*#endif/ and $skip--;
           }
           VIM::DoCommand('new');
           $curbuf->Append(1, @extract);
           VIM::DoCommand('1d');

     with a file that looked like (note the nested, unaligned #if):

            stay
            stay, next blank too
           
               #if
           
             go away, previous blank too
           
             go away, previous blank too
             go away
            #if
             go away
           
             go away, previous blank too
                       #endif
           
             some more go away
            #endif
           
            stay, previous blank too
            stay
           
            stay, previous blank too
            stay
            very last stay

  3) The following works on the above example

        :%g/^\s*#if/normal d%dd

     After seeing Tim Chase's post, noticed that the  
     preceding doesn't handle elses;  I think the
     following would do the job (only partially tested):

     :exec 'normal G$' | while(search('^\s*#endif\s*', 'bce')) | exec 'normal d%dd' | endwhile

  (Of course, the perl solution can be translated to other
  languages, including viml.)

  --Suresh

Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

Jack Donohue
In reply to this post by cga2000
> :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.

Thanks,


Jack

Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

Gary Johnson
On 2006-05-04, 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.

The basic technique of using folding for this is this:

    set foldmethod=expr
    set foldexpr=getline(v:lnum)=~'text to be folded'

To close all the folds, you can

    set foldlevel=0

but I usually use zM to close all folds and zR to open all folds.

You could wrap that in a mapping and maybe a function to make it
easier to use.  I usually type it out once, then use the command
line history and editing features to modify the pattern as needed.

See

    :help fold-expr

HTH,
Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|

RE: hiding lines

Suresh Govindachar`
In reply to this post by Jack Donohue

Jack Donohue 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.

  The original post specifically asked for a non-fold based solution
  to "hiding lines".

  The first step toward finding a non-"cumbersome" solution that can
  be mapped and which results in folding away certain lines is to
  define what needs to be hidden (with everything else displayed or
  what needs to be displayed with everything else hidden).
 
  --Suresh

Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

Gerald Lai-2
In reply to this post by Jack Donohue
On Thu, 4 May 2006, Jack Donohue 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.

Perhaps this would do:

   :set foldenable foldexpr=getline(v:lnum)=~@/

To fold consecutive lines that start with the word "fold", first search

   /^fold

or

   :let @/ = "^fold"

Then do

   :set foldmethod=expr foldlevel=0

to fold every search match.

HTH.
--
Gerald
Reply | Threaded
Open this post in threaded view
|

Re: hiding lines

Charles E Campbell Jr
In reply to this post by Suresh Govindachar`
Suresh Govindachar wrote:

>Jack Donohue wrote:
>
>  >> :v (and :g) made my day..!
>  >
>  > I guess I'm looking for a g or v command that folds the lines
>  > instead of actually deleting them.
>
>  The original post specifically asked for a non-fold based solution
>  to "hiding lines".
>  
>
May I suggest looking into Vince Negri's conceal patch, available at

  http://vim.sourceforge.net/scripts/script.php?script_id=302

It will not conceal the current line, so one can always see and edit the
current line.  Currently its
a vim 6.0 patch; I don't think VN has made one to vim 7.0 available
yet.  It wasn't rated highly
enough for Bram to consider including it in vim 7.0, apparently.

Regards,
Chip Campbell