Loading ...
Sorry, an error occurred while loading the content.

Auto folding ONLY on blocks starting from column 1

Expand Messages
  • Philip Rhoades
    People, I have been playing around with folding options in .vimrc but can t work out how to do what I want which is: Fold all blocks between the first line of
    Message 1 of 7 , Nov 19, 2012
    • 0 Attachment
      People,

      I have been playing around with folding options in .vimrc but can't
      work out how to do what I want which is:

      Fold all blocks between the first line of the block and the "end"
      line but ONLY only for those blocks whose first line starts at column 1.

      Is this possible?

      Thanks,

      Phil.
      --
      Philip Rhoades

      GPO Box 3411
      Sydney NSW 2001
      Australia
      E-mail: phil@...

      --
      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
    • Ben Fritz
      ... What do you mean by block ? How do you know where it starts and ends? This should be fairly easy to do with a foldexpr. -- You received this message from
      Message 2 of 7 , Nov 19, 2012
      • 0 Attachment
        On Monday, November 19, 2012 2:31:49 PM UTC-6, Philip Rhoades wrote:
        > People,
        >
        >
        >
        > I have been playing around with folding options in .vimrc but can't
        >
        > work out how to do what I want which is:
        >
        >
        >
        > Fold all blocks between the first line of the block and the "end"
        >
        > line but ONLY only for those blocks whose first line starts at column 1.
        >
        >
        >
        > Is this possible?
        >

        What do you mean by "block"? How do you know where it starts and ends? This should be fairly easy to do with a foldexpr.

        --
        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
      • Philip Rhoades
        People, ... Sorry, silly of me - I should have said when editing Ruby blocks eg: if (..) . . end or tst = proc{ tarr . . } Thanks, Phil. -- Philip Rhoades GPO
        Message 3 of 7 , Nov 20, 2012
        • 0 Attachment
          People,


          On Nov 20, 2012; 8:01am, Ben Fritz wrote:
          > On Monday, November 19, 2012 2:31:49 PM UTC-6, Philip Rhoades wrote:

          >> I have been playing around with folding options in .vimrc but can't
          >>
          >> work out how to do what I want which is:
          >>
          >> - Fold all blocks between the first line of the block and the "end"
          >>
          >> line but ONLY only for those blocks whose first line starts at
          >> column 1.
          >>
          >> Is this possible?
          >>
          >
          >
          > What do you mean by "block"? How do you know where it starts and
          > ends? This should be fairly easy to do with a foldexpr.


          Sorry, silly of me - I should have said when editing Ruby blocks eg:

          if (..)
          .
          .
          end

          or

          tst = proc{ tarr
          .
          .
          }


          Thanks,

          Phil.
          --
          Philip Rhoades

          GPO Box 3411
          Sydney NSW 2001
          Australia
          E-mail: phil@...

          --
          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
        • Ben Fritz
          ... First, it looks like this will fold more than you want, but just in case it is acceptable to you, have you tried the Ruby syntax folding? See :help
          Message 4 of 7 , Nov 20, 2012
          • 0 Attachment
            On Tuesday, November 20, 2012 1:48:55 PM UTC-6, Philip Rhoades wrote:
            >
            > On Nov 20, 2012; 8:01am, Ben Fritz wrote:
            > > On Monday, November 19, 2012 2:31:49 PM UTC-6, Philip Rhoades wrote:
            >
            > >> I have been playing around with folding options in .vimrc but can't
            > >>
            > >> work out how to do what I want which is:
            > >>
            > >> - Fold all blocks between the first line of the block and the "end"
            > >>
            > >> line but ONLY only for those blocks whose first line starts at
            > >> column 1.
            > >>
            > >> Is this possible?
            > >>
            > >
            > > What do you mean by "block"? How do you know where it starts and
            > > ends? This should be fairly easy to do with a foldexpr.
            >
            >
            > Sorry, silly of me - I should have said when editing Ruby blocks eg:
            >
            > if (..)
            > .
            > .
            > end
            >
            > or
            >
            > tst = proc{ tarr
            > .
            > .
            > }
            >
            >

            First, it looks like this will fold more than you want, but just in case it
            is acceptable to you, have you tried the Ruby syntax folding?

            See :help ruby.vim for the options on Ruby syntax. Near the end are the
            folding options ruby_fold and ruby_no_comment_fold.

            If all you want is the specific blocks you mention, and if you're not too
            worried about being precisely correct, you could make a foldexpr which
            depends on not only the beginning of the match but also the end being in the
            first column. Something like:

            fun! MyFoldBlocks()
            if getline(v:lnum)=~'\v^(if|(\S&.*\{)|SomeOtherBlockBeginning)'
            return ">1"
            elseif getline(v:lnum)=~'\v^(end|\}|SomeOtherBlockEnding)'
            return "<1"
            else
            return "="
            endif
            endfun
            setl foldexpr=MyFoldBlocks() fdm=expr

            --
            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
          • Philip Rhoades
            Ben, ... Yes, I did look at that and yes it was too much. ... This nearly does what I want but I want the first (and maybe the last line?) of the block visible
            Message 5 of 7 , Nov 20, 2012
            • 0 Attachment
              Ben,


              On 2012-11-21 07:20, Ben Fritz wrote:
              > On Tuesday, November 20, 2012 1:48:55 PM UTC-6, Philip Rhoades wrote:
              >>
              >> On Nov 20, 2012; 8:01am, Ben Fritz wrote:
              >> > On Monday, November 19, 2012 2:31:49 PM UTC-6, Philip Rhoades
              >> wrote:
              >>
              >> >> I have been playing around with folding options in .vimrc but
              >> can't
              >> >>
              >> >> work out how to do what I want which is:
              >> >>
              >> >> - Fold all blocks between the first line of the block and the
              >> "end"
              >> >>
              >> >> line but ONLY only for those blocks whose first line starts at
              >> >> column 1.
              >> >>
              >> >> Is this possible?
              >> >>
              >> >
              >> > What do you mean by "block"? How do you know where it starts and
              >> > ends? This should be fairly easy to do with a foldexpr.
              >>
              >>
              >> Sorry, silly of me - I should have said when editing Ruby blocks eg:
              >>
              >> if (..)
              >> .
              >> .
              >> end
              >>
              >> or
              >>
              >> tst = proc{ tarr
              >> .
              >> .
              >> }
              >>
              >>
              >
              > First, it looks like this will fold more than you want, but just in
              > case it
              > is acceptable to you, have you tried the Ruby syntax folding?


              Yes, I did look at that and yes it was too much.


              > See :help ruby.vim for the options on Ruby syntax. Near the end are
              > the
              > folding options ruby_fold and ruby_no_comment_fold.
              >
              > If all you want is the specific blocks you mention, and if you're not
              > too
              > worried about being precisely correct, you could make a foldexpr
              > which
              > depends on not only the beginning of the match but also the end being
              > in the
              > first column. Something like:
              >
              > fun! MyFoldBlocks()
              > if getline(v:lnum)=~'\v^(if|(\S&.*\{)|SomeOtherBlockBeginning)'
              > return ">1"
              > elseif getline(v:lnum)=~'\v^(end|\}|SomeOtherBlockEnding)'
              > return "<1"
              > else
              > return "="
              > endif
              > endfun
              > setl foldexpr=MyFoldBlocks() fdm=expr


              This nearly does what I want but I want the first (and maybe the last
              line?) of the block visible eg:

              if ..)
              folded
              end

              since the actual blocks can get quite large and I just want to see the
              overall logic. I guess that means doing something with lnum?

              Thanks!

              Phil.
              --
              Philip Rhoades

              GPO Box 3411
              Sydney NSW 2001
              Australia
              E-mail: phil@...

              --
              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
            • Ben Fritz
              ... Sure, you can get the previous line with getline(v:lnum-1) or the next line with getline(v:lnum+1). Or you could mess with the foldtext option to adjust
              Message 6 of 7 , Nov 20, 2012
              • 0 Attachment
                On Tuesday, November 20, 2012 4:31:27 PM UTC-6, Philip Rhoades wrote:
                >
                > > fun! MyFoldBlocks()
                >
                > > if getline(v:lnum)=~'\v^(if|(\S&.*\{)|SomeOtherBlockBeginning)'
                >
                > > return ">1"
                >
                > > elseif getline(v:lnum)=~'\v^(end|\}|SomeOtherBlockEnding)'
                >
                > > return "<1"
                >
                > > else
                >
                > > return "="
                >
                > > endif
                >
                > > endfun
                >
                > > setl foldexpr=MyFoldBlocks() fdm=expr
                >
                >
                >
                >
                > This nearly does what I want but I want the first (and maybe the last
                >
                > line?) of the block visible eg:
                >
                >
                >
                > if ..)
                >
                > folded
                >
                > end
                >
                >
                >
                > since the actual blocks can get quite large and I just want to see the
                >
                > overall logic. I guess that means doing something with lnum?
                >
                >
                >

                Sure, you can get the previous line with getline(v:lnum-1) or the next line with getline(v:lnum+1).

                Or you could mess with the 'foldtext' option to adjust what gets displayed when you fold stuff away.

                --
                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
              • Philip Rhoades
                Ben, ... It took me a while to work out how to use foldtext (I have found this problem in the past - the Vim docs are good but are like man pages - short on
                Message 7 of 7 , Nov 21, 2012
                • 0 Attachment
                  Ben,


                  On 2012-11-21 13:40, Ben Fritz wrote:
                  > On Tuesday, November 20, 2012 4:31:27 PM UTC-6, Philip Rhoades wrote:
                  >>
                  >> > fun! MyFoldBlocks()
                  >> > if getline(v:lnum)=~'\v^(if|(\S&.*\{)|SomeOtherBlockBeginning)'
                  >> > return ">1"
                  >> > elseif getline(v:lnum)=~'\v^(end|\}|SomeOtherBlockEnding)'
                  >> > return "<1"
                  >> > else
                  >> > return "="
                  >> > endif
                  >> > endfun
                  >>
                  >> > setl foldexpr=MyFoldBlocks() fdm=expr
                  >>
                  >>
                  >> This nearly does what I want but I want the first (and maybe the
                  >> last
                  >> line?) of the block visible eg:
                  >>
                  >> if (..)
                  >> folded
                  >> end
                  >>
                  >> since the actual blocks can get quite large and I just want to see
                  >> the
                  >> overall logic. I guess that means doing something with lnum?
                  >
                  >
                  > Sure, you can get the previous line with getline(v:lnum-1) or the
                  > next line with getline(v:lnum+1).
                  >
                  > Or you could mess with the 'foldtext' option to adjust what gets
                  > displayed when you fold stuff away.


                  It took me a while to work out how to use foldtext (I have found this
                  problem in the past - the Vim docs are good but are like "man" pages -
                  short on examples). So I now have:

                  fun! MyFoldBlocks()
                  if getline(v:lnum)=~'\v^(class|for|if|while|([a-z0-9][a-b0-9\
                  _].*\{))' "inc escaped space + tab
                  return ">1"
                  elseif getline(v:lnum)=~'\v^(end|\}|SomeOtherBlockEnding)'
                  return "<1"
                  else
                  return "="
                  endif
                  endfun

                  set foldtext=getline(v:foldstart)
                  setl foldexpr=MyFoldBlocks() fdm=expr

                  - my change to prevent folding on comments with '#' in col 1 worked but
                  is not so elegant?

                  I didn't have much success with getline(v:lnum+1) but
                  foldtextresult({lnum}) also looks like it might offer some
                  possibilities?

                  Anyway, everything is working exactly how I want it to now so thanks
                  very much!

                  Regards,

                  Phil.
                  --
                  Philip Rhoades

                  GPO Box 3411
                  Sydney NSW 2001
                  Australia
                  E-mail: phil@...

                  --
                  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
                Your message has been successfully submitted and would be delivered to recipients shortly.