[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [StrongED] Better C folding

In message <CALE3-UAGtOyo43eCu80qotDiy2uSYF0EpXMw0=abmaMomybcKg@xxxxxxxxxxxxxx>
          Richard Griffin <rik.griffin@xxxxxxxxxxxxxxxxxxx> wrote:

> I noticed that the default C ModeFile doesn't fold functions
> perfectly. For example it doesn't match a function that returns
> "unsigned long long int".

The default C ModeFile doesn't fold on functions, only on fold markers
"/*{{{" and "/*}}}", so your copy must have been modified at some point.

> So here's a replacement that I believe works better. This should also
> improve the "list of functions" (Shift-F2).

Shift-F2 doesn't use the Function expression but the ListOfC function
which uses special LoF code designed for C like languages.

If you want to use the Function expression then Shift-click with Select
on the LoF icon.

> I'm not sure what FoldParm1 does but that's the line from the default
> ModeFile:
> FoldParm1 ("/*{{{", "/*}}}", StartSpace, Case)

FoldParm1 will fold on special markers that you place in the text around
a function:

  /*{{{ start of function X */

   <function X goes here>

  /*}}} end of function X */

The advantage of using fold markers is that they can be more reliable
than trying to match function definitions. Trying to catch everything
that a compiler allows isn't easy.

> FoldParm2 (Function, "}", StartOfLine, Case)
> In the Search section I've altered the Function pattern:
>  function_ ("extern" | "volatile" | "static" | "signed" | "unsigned" |
> "long") {" "}
>  Function < {function_} ~"else" ~"return" {AD|"_"}+ {' *'}+ {AD|"_"}+
> {" "} "(" * ")" {\s} "{"

This seems to work reasonably well but I did come a across a couple of
issues. It doesn't work when the function's parameter list is split over
multiple lines or when old K&R style definitions are used.

I've altered it to:

  function_ {("extern" | "volatile" | "static" | "signed" | "unsigned" |
  "long") {" "}} {AD|"_"}+ {' *'}+
  Function < [function_] {AD|"_"}+ {" "} "(" ** ")" {\s} "{"

Which seems to work better but may have issues too. The above fails on
functions that use comments between the function definition and the '{'
at the start of the function body. Perhaps we can get away with omitting
the {\s} "{" part from the Function expression?

Please give it a try and let me know how you've got on.

> Hope this is useful.

Sure is, always good to have another perspective on things. Thanks.


StrongED Developer

To unsubscribe send a mail to StrongED+unsubscribe@xxxxxxxxxxxxxx