make.lua 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. end
  31. Job:new({
  32. command = 'sh',
  33. args = { '-c', makeprg },
  34. on_exit = vim.schedule_wrap(on_exit),
  35. }):start()
  36. end
  37. vim.api.nvim_create_user_command('Make', make, {
  38. nargs = '*',
  39. complete = 'file',
  40. })