phplint.vim 2.6 KB

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