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

Re: How to fold text between two regular expressions ?

Expand Messages
  • Yakov Lerner
    ... Does this do what you want (untested): let REGEX1= ^procedure let REGEX2= ^end set foldmethod=expr foldexpr=MyFoldLevel() function! MyFoldLevel() let
    Message 1 of 4 , Mar 1, 2007
    • 0 Attachment
      On 3/1/07, MM <mirzani@...> wrote:
      > How do I create folds such that it starts at matched expression and
      > ends at the line containing part of the matched expression.
      > e.g i'd like to fold
      >
      > procedure xyz ( .....
      > :
      > :
      > :
      > :
      > :
      > end xyz;
      >

      Does this do what you want (untested):

      let REGEX1='^procedure'
      let REGEX2='^end'
      set foldmethod=expr foldexpr=MyFoldLevel()
      function! MyFoldLevel()
      let line=getline(v:lnum)
      if line =~ REGEX1
      return "a1" " increase foldlevel
      elseif line =~ REGEX2
      return "s1" " decrease foldlevel
      else
      return '=' " foldlevel same as previous
      endif
      endfunc

      Yakov
    • Yakov Lerner
      ... correction: this line should have been if line =~ g:REGEX1 ... Correction: elseif line =~ g:REGEX2
      Message 2 of 4 , Mar 1, 2007
      • 0 Attachment
        On 3/1/07, Yakov Lerner <iler.ml@...> wrote:
        > On 3/1/07, MM <mirzani@...> wrote:
        > > How do I create folds such that it starts at matched expression and
        > > ends at the line containing part of the matched expression.
        > > e.g i'd like to fold
        > >
        > > procedure xyz ( .....
        > > :
        > > :
        > > :
        > > :
        > > :
        > > end xyz;
        > >
        >
        > Does this do what you want (untested):
        >
        > let REGEX1='^procedure'
        > let REGEX2='^end'
        > set foldmethod=expr foldexpr=MyFoldLevel()
        > function! MyFoldLevel()
        > let line=getline(v:lnum)
        > if line =~ REGEX1
        correction: this line should have been
        if line =~ g:REGEX1
        > return "a1" " increase foldlevel
        > elseif line =~ REGEX2
        Correction:
        elseif line =~ g:REGEX2
        > return "s1" " decrease foldlevel
        > else
        > return '=' " foldlevel same as previous
        > endif
        > endfunc
      • Tim Chase
        ... You might be able to do something like ... You can tweak that initial regexp to catch what you want. If you can mix and match directives, you can do things
        Message 3 of 4 , Mar 1, 2007
        • 0 Attachment
          > How do I create folds such that it starts at matched
          > expression and ends at the line containing part of the matched
          > expression. e.g i'd like to fold
          >
          > procedure xyz ( .....
          > :
          > :
          > end xyz;
          >
          > function abc (
          > :
          > :
          > end abc;
          >
          > impure function pqr (
          > :
          > :
          > end pqr;

          You might be able to do something like

          :g/^\(procedure\|function\|impure function\)/.,/^end\>/fold

          You can tweak that initial regexp to catch what you want.
          If you can mix and match directives, you can do things like

          /^\(impure \|pure \)\=\(procedure\|function\)/

          which would find "procedure" or "function", optionally preceeded
          ("\=")by "pure" or "impure". A bit contrived, but you can riff
          on this theme to get all sorts of permutations.

          HTH,

          -tim
        Your message has been successfully submitted and would be delivered to recipients shortly.