make.lua 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. local Job = require('plenary.job')
  2. function make(opts)
  3. local makeprg = vim.bo.makeprg
  4. local errorformat = vim.bo.errorformat
  5. if makeprg == '' then
  6. return
  7. end
  8. -- escape special characters in args
  9. args = string.gsub(opts.args or '', '%%', '%%%%')
  10. -- substitute $*
  11. makeprg, _ = string.gsub(makeprg, '%$%*', args);
  12. -- expand
  13. makeprg = vim.fn.expandcmd(makeprg)
  14. local function on_exit(job, retval)
  15. local result = {}
  16. vim.list_extend(result, job:result())
  17. vim.list_extend(result, job:stderr_result())
  18. vim.notify(string.format(
  19. ':!%s\n%s\n\nshell returned %d',
  20. makeprg,
  21. table.concat(result, '\n'),
  22. retval
  23. ))
  24. vim.fn.setqflist({}, ' ', {
  25. title = makeprg,
  26. lines = result,
  27. efm = errorformat,
  28. })
  29. vim.api.nvim_exec_autocmds('QuickFixCmdPost', {})
  30. if #result > 0 then
  31. vim.cmd('copen')
  32. end
  33. end
  34. Job:new({
  35. command = 'sh',
  36. args = { '-c', makeprg },
  37. on_exit = vim.schedule_wrap(on_exit),
  38. }):start()
  39. end
  40. vim.api.nvim_create_user_command('Make', make, {
  41. nargs = '*',
  42. complete = 'file',
  43. })