Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating custom vim syntax: Can I set custom auto-indent rules?

Tags:

vim

I'm creating a custom syntax file for vim, and to make the code easier to read, I want to tell vim how to indent the code automatically. Right now, my usual re-indent gg=G'' in vim just gets rid of all the whitespace at the beginning of each line:

IF (@("X_17") = "X") THEN
*Make sure only one is selected
countX = 0;
IF (@("X_19") = "X") THEN
countX += 1;
END
IF (@("X_20") = "X") THEN
countX += 1;
END
IF (@("X_21") = "X") THEN
countX += 1;
END
...

I would like to set it up so that comments (beginning with *) are not indented at all (no whitespace at the beginning of the line), but the rest are indented based on IF and END. The same code, properly indented (assume even the first IF is within an even larger IF/END block):

    IF (@("X_17") = "X") THEN
*Make sure only one is selected
        countX = 0;
        IF (@("X_19") = "X") THEN
            countX += 1;
        END
        IF (@("X_20") = "X") THEN
            countX += 1;
        END
        IF (@("X_21") = "X") THEN
            countX += 1;
        END
        ...

I have the syntax higlighting set up, and keywords and built-in functions already part of a dal.vim file. Here is the current dal.vim:

http://pastebin.com/PEcSvqM9

Is there a way to change what I have so that code will automatically indent within the IF/END blocks, while keeping all the comments with no whitespace at the beginning?

Bonus question: How can i set up code folding to follow those IF/END blocks? Right now I am doing zf2j to fold this code.

like image 459
maccam912 Avatar asked Jul 31 '12 19:07

maccam912


1 Answers

You have to write an indentation plugin and put it into the indent subdirectory. More is explained in

:h 'indentexpr'

, it is what your plugin should set. Feel free to get examples from a hundred of indentation scripts distributed with vim. I did not write such plugins by myself, so can’t say which one will be most useful as an example.

For code folding check out

:h :syn-fold

Ah, and don’t set indent in a syntax file. It is not impossible, but it is just not right place for this type of job.

like image 190
ZyX Avatar answered Sep 27 '22 20:09

ZyX