How can I define vim syntax folding for Informix 4GL source code?

0

I use vim to edit Informix 4GL source code which has this sort of syntax:

FUNCTION
  FOR ...
  ...
  END FOR
END FUNCTION

I wish to fold the functions using za zM etc

This works

  :set foldmarker=FUNCTION,END\ FUNCTION
  :set foldmethod=marker

However the keywords can also be in lower case and I'd ideally also like to fold MAIN..END MAIN, so markers are not able to do this.

I tried

:syn region myFun start="FUNCTION" end="END FUNCTION" transparent fold
:set foldmethod=syntax

but it had no effect. I already have a syntax file that does color highlighting and :syn showed myFun included in the syntax definitions.

How can I configure case-independent syntax folding of FUNCTION...END FUNCTION and MAIN..END MAIN ?


Update: I have tried http://www.vim.org/scripts/script.php?script_id=2287 but it has some problems:

  • It folds at a level of detail I don't want (IF, FOR, WHILE, ...)
  • It assumes END statements start on a new line (so IF a<b call c() END IF folds to EOF)
  • It thinks SELECT * FROM table \n FOR UPDATE is the start of a FOR statement

So I'd like to try creating something much simpler first.

RedGrittyBrick

Posted 2011-08-17T16:53:35.297

Reputation: 70 632

What language is this? – digitxp – 2011-08-17T17:17:26.853

Informix 4GL. I have found a syntax file for it but it folds more than I want (IF statements), has bugs (e.g. when "END IF" on same line as "IF", or counts "SELECT * FROM table FOR UPDATE" as start of FOR loop) and doesn't fold FUNCTION! (though the file seems to have code for folding functions) – RedGrittyBrick – 2011-08-17T17:21:48.730

Did you try set foldmethod=indent? – romainl – 2011-08-17T20:25:53.267

@romainl: I hadn't but I have now. It's not ideal for me but it is useful. Thanks. – RedGrittyBrick – 2011-08-17T23:08:07.497

Answers

1

I think you should define your own folding function, that way you will have more control over what is folded when. The following fold function folds function/end function case insensitively, and should be fairly easy to adjust for further requirements:

function! InformixFold()
  let line      = getline(v:lnum)
  let prev_line = getline(v:lnum-1)

  if match(line, '^\s*function\s*$') >= 0
    return 1
  elseif match(prev_line, '^\s*end\s*function\s*$') >= 0
    return 0
  else
    return "="
endfunction

Then set foldmethod to expr and foldexpr to the function:

set foldmethod=expr
set foldexpr=InformixFold()

See help fold-expr for more.

"Screenshot":

- FUNCTION
|   FOR ...
|   ...
|   END FOR
| END FUNCTION

+ +--  5 lines: FUNCTION------------------------------------------------------

Thor

Posted 2011-08-17T16:53:35.297

Reputation: 5 178