Folding and indenting for parenthesises, braces and brackets

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

Folding and indenting for parenthesises, braces and brackets

Lukas Ruf

Dear all,

for somewhat cryptic language, I would like to automate the folding
and indentation according to the following rules:

Folding: Anything between a pair of parenthesises, braces and brackets
is folded if the opening and the closing ones are not on the same
line.

Indentation: Any parenthesis, braces and brackets that is the first
non-white-space character of a line defines the basis for shiftwidth
(sorry, do not know the exact term), and automates then the
indentation by shiftwidth, i.e. the p.,b.,b., are the first
non-white-space character of the current line and define the
indentation to which the shiftwidth is either added or subtracted.

Example for Folding and Indentation:

  {              // Fold1
    []           // no fold
    (            // Fold2
      [          // Fold3
        {        // Fold4
          Text
        }        // End Fold4
      ]          // End Fold3
    )            // End Fold2
  }              // End Fold1


My unlucky approach for folding:
- I read
  :help fold.txt
  :help usr_28.txt
- and tried then without success
    set foldmethod=indent
    set foldmethod=marker
- I tried without success creating a fold function
    set foldexpr=myFold(v:lnum)
    fun! DoFold(line)
        if     a:line =~ '{' | return 'a1'
        elseif a:line =~ '}' | return 's1'
        elseif a:line =~ '[' | return 'a1'
        elseif a:line =~ ']' | return 's1'
        elseif a:line =~ '(' | return 'a1'
        elseif a:line =~ ')' | return 's1'
        else
        endif
    endfun
    fun! MyFold(lnum)
        let line = getline(a:lnum)
        let level= DoFold(line)
        if level
            exe 'return ">'.level.'"'
        endif
    endfun
    setlocal foldmethod=expr
  But this has not provided the solution either......

Indentation: I have not really started to play with that.... for this
language, after I spent the time for C/C++ indentation with your help
in the email thread on Sept. 5th.

Thus: If anyone can give me the needed help, I would be very happy and
appreciate your effort.

Thanks in advance.

wbr,
Lukas
--
Lukas Ruf   <http://www.lpr.ch> | Ad Personam
rbacs      <http://wiki.lpr.ch> | Restaurants, Bars and Clubs
Raw IP   <http://www.rawip.org> | Low Level Network Programming
Style  <http://email.rawip.org> | How to write emails
Reply | Threaded
Open this post in threaded view
|

Re: Folding and indenting for parenthesises, braces and brackets

John (Eljay) Love-Jensen
Hi Lukas,

What about { } [ ] ( ) in a string?  In a comment?

What about < > (I have no idea how one would easily discern between paired <
> delimiters, and either < or > used as an operator).

Note:  I gave up trying to do what you are doing, and now just rely on
indentation level.  A cop-out, to be sure.

Food for thought,
--Eljay


Reply | Threaded
Open this post in threaded view
|

Re: Folding and indenting for parenthesises, braces and brackets

Lukas Ruf
Hi Eljay,

> Eljay Love-Jensen <[hidden email]> [2005-09-12 13:42]:
>
thanks!

>
> What about { } [ ] ( ) in a string?  In a comment?
>

neither in a string not in a comment nor in an expression.

> What about < > (I have no idea how one would easily discern between
> paired <
> > delimiters, and either < or > used as an operator).
>

well, those, I do not use.

> Note:  I gave up trying to do what you are doing, and now just rely
> on indentation level.  A cop-out, to be sure.
>

hmmm.  can you give me a hint, how I can achieve the solution for my
problem with the indentation???

TIA.

wbr,
Lukas
--
Lukas Ruf   <http://www.lpr.ch> | Ad Personam
rbacs      <http://wiki.lpr.ch> | Restaurants, Bars and Clubs
Raw IP   <http://www.rawip.org> | Low Level Network Programming
Style  <http://email.rawip.org> | How to write emails
Reply | Threaded
Open this post in threaded view
|

Re: Folding and indenting for parenthesises, braces and brackets

John (Eljay) Love-Jensen
Hi Lukas,

> hmmm.  can you give me a hint, how I can achieve the solution for my
> problem with the indentation???

You can use:
:set fdm=indent

If you *REALLY* want to roll up the delimiters too, you can alter your
coding practices and indent the delimiters as well.

void foo()
  {
    code;
    code;
    if(test)
      {
        ifbody;
        ifbody;
        ifbody;
      }
    else
      {
        elsebody;
        elsebody;
        elsebody;
      }
    code;
    code;
  }

NOTE:  my own coding style is like this:

void foo()
{
  code;
  if(test)
  {
    ifbody;
  }
}

My coding style USED to be an outline format, like this:

void foo() {
  code;
  if(test) {
    ifbody;
    }
  }

My old outline format style dovetailed nicely with using fdm=indent.  Which
was a style that I developed while using Borland Sidekick 1.x's Outline
editor as my code editor.  From way back when.

My current "where the effin' curly braces go (and who the eff cares)" format
complies with my department's coding styles, and I've now grown used to it.
I even use it at home.

My only two complaints with Vim and folds is #1) that the displayed
rolled-up fold-line is fold highlighted instead of being syntax colored
(don't even have the OPTION to syntax color it instead of using highlight
Folded ... :highlight Folded term=NONE ctermfg=NONE ctermbg=NONE does not do
it).  #2) performance for fdm=syntax seems subpar* (hence my fdm=indent
preference).

Note to Bram:  fdm=diff for vim -d is sweet.

HTH,
--Eljay

* for 6.3; I haven't played around with 7.0.  Maybe things are better now.


Reply | Threaded
Open this post in threaded view
|

Re: Folding and indenting for parenthesises, braces and brackets

Lukas Ruf
Hi Eljay,

> Eljay Love-Jensen <[hidden email]> [2005-09-12 15:10]:
>
thsnks.

>
> > hmmm.  can you give me a hint, how I can achieve the solution for my
> > problem with the indentation???
>
> You can use:
> :set fdm=indent
>
> If you *REALLY* want to roll up the delimiters too, you can alter your
> coding practices and indent the delimiters as well.
>
well, it seems that I need to.

>
> NOTE:  my own coding style is like this:
>
> void foo()
> {
>   code;
>   if(test)
>   {
>     ifbody;
>   }
> }
>

this is the style I have been used to for about ten years now.....  I
personally prefer it because, at least to me, it eases reading of
code.

I am even writing conditions the form

    if (condition1 ||
        condition2 &&
        condition3
       )

or -- if there are complex expressions:

    if (condition1
        ||
        condition2
        &&
        condition3
       )

It just helps me....    ;)

Well, thanks!

wbr,
Lukas
--
Lukas Ruf   <http://www.lpr.ch> | Ad Personam
rbacs      <http://wiki.lpr.ch> | Restaurants, Bars and Clubs
Raw IP   <http://www.rawip.org> | Low Level Network Programming
Style  <http://email.rawip.org> | How to write emails