Home page

564,421 Visitors, Wednesday 08 February 2012 07:47:46 CET, IP: 38.107.179.211

Return to parent page: Miscellaneous

PHP-Correct-Indenting VIM script



Download here (version 1.34 released on 2011-02-14 - a complete changelog is available)

You can subscribe to a newsletter about this script. (Only new releases will be announced there)

Development versions are available on GitHub: http://github.com/2072/PHP-Indenting-for-VIm/downloads

(The version 1.33 of this script is included in VIM 7)

Features:
- Indent "perfectly" PHP code.
- Comment or non-PHP code doesn't break the algorithm.
- Non-PHP code, multi-line /**/ comments and "Here Document" content remain unchanged.
- HTML <Script.*> tags are indented as PHP and like if their content was part of the surrounding PHP code.
- Fast optimized indenting, the script knows when it's indenting large blocks and skips some unnecessary tests...
- Support folding (folds don't break the indenting)
- Auto-formatting for comments (using the Vim option formatoptions=qroc).
- Indent correctly complex "non bracketed blocks" (like in language C):
Exemple:

<?php
if (!isset($History_lst_sel))
    if (!isset($History_lst_sel))
        if (!isset($History_lst_sel)) {
            $History_lst_sel=0;
        } else
            $foo="truc";
$command_hist = TRUE;
?>


- Direct indenting, the text is indented as you type it.
- switch/case are indented correctly.
- Script source code well commented.
- Bug free as far as I know :)

A more complete example of its capabilities:


<?php
if ($foo="thing") // as you see an opened bracket isn't needed to indent correctly :)
    if (foo("something"))
    {
        somethingelse("blabla");
?>


<html>
<head>
<title>SOME HTML CODE</title>
<?
        echo "Something here";
?>
        <script type="text/javascript">
        // Note the indentation of the script as if part of PHP

        var truc=0;
        if (truc) {
            nothing();
        }

        </script>

</head>
<body>

<b>some closing and oppenning bracket here } } {
nothing to indent here :)
</b>

</body>
</html>


<?
        Somethingelse(true);
    }

if (foo)
    /* a useless comment here
    with useless code:
    if (broken code) {
                   blabla();
    */
    if (testHereDocument()) {


        echo <<<HEREDOC
Here is an Here Document style
echo, very useful.
The following code isn't indented
if (test)
while (foo) {
nothing();
}
HEREDOC;

    }

bye();
?>


Available options:

let PHP_autoformatcomment = 0 (default is 1)
To disable autoformating of comment by default.
If set to 0, this script will let the 'formatoptions' setting intact.

let PHP_default_indenting = # of sw (default is 0)
# of sw will be added to the indent of each line of PHP code (non PHP code isn't affected).

let PHP_removeCRwhenUnix = 1
Will make the script automatically remove CR at end of lines (by default this option is unset), NOTE that you MUST remove CR when the fileformat is UNIX else the indentation won't be correct...

let PHP_BracesAtCodeLevel = 1
Will indent the '{' and '}' at the same level than the code they contain.


install details:

Just make sure the name of the file is php.vim and copy it under your vimfiles/indent folder.
PHP syntax coloring must be turned on.

You must also enable filetype indenting (this is activated with the default configuration)

Add this line in your config files:

:filetype indent on

That's all folks!


Enjoy :)
Return to parent page: Miscellaneous



Valid HTML 4.01! Best viewed in sRGB A utility to annoy spammers Valid CSS!
Previous page - Next page
This page has been seen 8,882 times ; last update: Mon 14 Feb 16:51:36 2011
Copyright © 2001 - 2012 John Wellesz '2072'
All rights reserved.
All trademarks and registered trademarks mentioned on this website are the properties of their respective companies.