Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Vim ignore first character in line while indenting

Whenever I have to edit Perl Mason files, I always have problems indenting lines due to Perl code starting with %. For example:

<div>
    <div>
%       if( !$something ) {
            <strong><% $title %></strong>
%       }
    </div>
</div>

Any idea how I can tell Vim to ignore the % at the beginning of the line and indent like it wasn't there?

I'm using https://github.com/aming/vim-mason to support the mixed Perl/HTLM syntax, but I don't think it changes anything.

like image 873
André Gil Avatar asked Oct 17 '22 13:10

André Gil


1 Answers

This is Perl code embedded inside HTML, so the indenting comes from $VIMRUNTIME/indent/html.vim. This defines an 'indentexpr', implemented by HtmlIndent().

You need to modify that implementation to ignore % in the first column; whenever it accesses the buffer (getline(), prevnonblank(), shiftwidth()), you need to intercept, find the previous line that does have such % sigil, and return the value for that instead. (If those special lines can also contain HTML tags, you may have to extract those from the Perl code and return only those.) That gets you the indenting you desire.

Unfortunately, it's not trivial, and you have to fork the original implementation. However, if you manage to implement a clean solution, you could suggest adding integration points to the author of indent/html.vim. If there are other languages apart from Mason that use these prefixes on top of HTML, that would be an additional argument for adding such support (and maybe even your wrapper functions).

like image 134
Ingo Karkat Avatar answered Oct 20 '22 21:10

Ingo Karkat