Search code examples
phpvimsavelintautocmd

Is it possible to have vim prevent the saving of a php file that has a parse error?


I use vim and would like it to prevent me from saving php files that has parse errors. If I would like to use for instance "php -l <file>" to achieve this, how would the autocmd in .vimrc look like?

I know I can hook into BufWritePre with something like

autocmd FileType php autocmd BufWritePre * !php -l %

but I want it to abort the "write" if the php command fails.


Solution

  • You can throw an uncaught exception in the hook and it will abort write: try

    function s:RunPHP()
        execute '!php -l' shellescape(@%, 1)
        if v:shell_error | throw 'PHP failed' | endif
    endfunction
    
    function s:DefineBWPphpevent()
        augroup Run_php
            autocmd! BufWritePre <buffer> :call s:RunPHP()
        augroup END
    endfunction
    
    augroup Run_php
        autocmd!
        autocmd FileType  *  :autocmd! Run_php BufWritePre <buffer>
        autocmd FileType php :call s:DefineBWPphpevent()
    augroup END
    

    Also note

    1. shellescape: you should not use :!shell_command % as here vim does not do proper escaping and you will run into troubles if file name contains special symbols like spaces, quotes, dollar, newlines, ...
    2. <buffer> in place of * in second pattern. Your original command does not define autocommand for php files, it instead defines autocommand for all filetypes if one of the filetypes is php.
    3. autocmd grouping and banged :autocmd!s: they prevent duplicating autocommands.

    By the way, what do you want to achieve by running php on old version of file (it is BufWritePre event, so it happens before file is written)? Maybe you should replace code that runs php with :w !php -l, :w !php -l - or even :w !php -l /dev/stdin depending on what php accepts (I do not have it on my system and do not want to have).