phplint.vim 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. " Check we actually have PHP installed, otherwise you'll never be able to save
  2. if exists("loadedPHPLint") || !executable('php')
  3. finish
  4. endif
  5. let loadedPHPLint = 1
  6. autocmd BufWriteCmd * execute('call LintPHPFile()')
  7. function LintPHPFile()
  8. if &filetype == 'php'
  9. let thisFile = expand("%")
  10. " If the file isn't writable don't do anything, as it'll freak vim out
  11. if filewritable(thisFile)
  12. let testFile = tempname()
  13. " This resets the view to the top, so we need to restore it
  14. let view = winsaveview()
  15. let bufferContents = getbufline(bufnr("%"), 1, "$")
  16. exe writefile(bufferContents, testFile)
  17. call winrestview(view)
  18. " Check the test file got written, this might fail if the disk is
  19. " full and prevent you from saving. Which would be bad.
  20. if filereadable(testFile)
  21. let phpLint = system('php -l ' . testFile)
  22. let phpLint = substitute(phpLint, testFile, thisFile, "g")
  23. call delete(testFile)
  24. let errLine = matchstr(phpLint, 'No syntax errors')
  25. if strlen(errLine) > 0
  26. cclose
  27. redraw " Avoids the annoying 'Press ENTER to BLAH' message
  28. else
  29. let lintLines = split(phpLint, "\n")
  30. let errorLines = []
  31. for line in lintLines
  32. let pos = matchstr(line, 'on line')
  33. if strlen(pos) > 0 && stridx(line, 'PHP') != 0 " Some versions dupe the error
  34. call add(errorLines, line)
  35. endif
  36. endfor
  37. let cFile = tempname()
  38. exe writefile(errorLines, cFile)
  39. let oldCpoptions = &cpoptions
  40. let oldErrorformat = &errorformat
  41. set cpoptions-=F
  42. set errorformat=%m\ in\ %f\ on\ line\ %l
  43. exe "cfile " . cFile
  44. "copen 5
  45. botright cwindow 5
  46. call delete(cFile)
  47. let &cpoptions = oldCpoptions
  48. let &errorformat = oldErrorformat
  49. return
  50. endif
  51. endif
  52. endif
  53. endif
  54. " We have to handle the write op ourselves, as we overrode it
  55. noautocmd w
  56. endf