Guest User

NeovimRunFunctions

a guest
Aug 25th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.20 KB | None | 0 0
  1. lvim.keys.normal_mode["<leader>i"] = ":Hang<cr>"
  2. lvim.keys.normal_mode["<leader>r"] = ":Make<cr>"
  3. lvim.keys.normal_mode["<leader>rr"] = ":CloseTerminal<cr>"
  4. lvim.keys.normal_mode["<leader>ii"] = ":CloseTerminal<cr>"
  5.  
  6. vim.cmd("command! CloseTerminal lua close_terminal_buffer()")
  7. function _G.close_terminal_buffer()
  8.   local terminal_bufnr = nil
  9.   for _, win in ipairs(vim.api.nvim_list_wins()) do
  10.     local bufnr = vim.api.nvim_win_get_buf(win)
  11.     local buftype = vim.api.nvim_buf_get_option(bufnr, "buftype")
  12.     if buftype == "terminal" then
  13.       terminal_bufnr = bufnr
  14.       Already_open = false
  15.       break
  16.     end
  17.   end
  18.   if terminal_bufnr then
  19.     vim.cmd("bdelete! " .. terminal_bufnr)
  20.   end
  21. end
  22.  
  23.  
  24. vim.cmd("command! Hang lua generic_make_hang()")
  25. function _G.generic_make_hang()
  26.   local filename = vim.fn.expand("%");
  27.   local filetype = vim.bo.filetype
  28.   vim.cmd(":w")
  29.   local commands = {
  30.     scala = "sbt --client run",
  31.     java = "./mvnw spring-boot:run",
  32.     c = string.format("gcc -g %s && ./a.out", filename)
  33.   }
  34.   local command = commands[filetype] or "make!"
  35.   _G.close_terminal_buffer() -- close any buffs
  36.   vim.cmd("botright new")  -- create buff
  37.   vim.cmd("resize " .. 10)  -- set buff height
  38.   vim.fn.termopen(command)  -- run cmd
  39.   vim.cmd("visual!")  -- switch to visual
  40.   vim.cmd("wincmd p")  -- switch to main window
  41. end
  42.  
  43.  
  44. vim.cmd("command! Make lua generic_make()")
  45. function _G.generic_make()
  46.   local filename = vim.fn.expand("%");
  47.   local filetype = vim.bo.filetype
  48.   vim.cmd(":w")
  49.   local commands = {
  50.     scala = "sbt --client run",
  51.     java = "./mvnw spring-boot:run",
  52.     c = string.format("gcc %s && ./a.out", filename)
  53.    }
  54.  
  55.     local command = commands[filetype] or "make!"
  56.     local height = 10  -- Set the desired terminal window height
  57.     vim.cmd("botright new")  -- Create a new buffer at the bottom of the screen
  58.     vim.cmd("resize " .. height)  -- Set the height of the new buffer
  59.     vim.fn.termopen(command)
  60.     -- Set up a temporary key mapping to close the terminal buffer when any key is pressed
  61.     vim.cmd("autocmd BufEnter <buffer> tnoremap <buffer> <silent> <nowait> <expr> <Plug>(CloseTerminal) execute('silent! bwipeout!<CR>')")
  62. end
  63.  
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment