functions.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. local fn = vim.fn
  2. local cmd = vim.cmd
  3. -- toggle quickfix list
  4. function ToggleQuickFix()
  5. local isQflistOn = fn.getwininfo(fn.win_getid())[1].quickfix
  6. if isQflistOn == 1 then
  7. cmd("cclose")
  8. else
  9. cmd("copen")
  10. end
  11. end
  12. -- toggle location list
  13. function ToggleLocList()
  14. local isLocListOn = fn.getwininfo(fn.win_getid())[1].loclist
  15. if isLocListOn == 1 then
  16. cmd('lclose')
  17. else
  18. cmd('lopen')
  19. end
  20. end
  21. -- jump to start of line
  22. function JumpToStartOfLine()
  23. local curCol = vim.fn.col(".")
  24. if curCol == 1 then
  25. cmd('normal _')
  26. else
  27. cmd('normal 0')
  28. end
  29. end
  30. -- jump to end of line
  31. function JumpToEndOfLine()
  32. local curCol = vim.fn.col(".")
  33. local endCol = vim.fn.col("$") - 1
  34. if curCol == endCol then
  35. cmd('normal g_')
  36. else
  37. cmd('normal $')
  38. end
  39. end
  40. -- buffer menu
  41. function BuffersMenu()
  42. local actions = {
  43. "enew",
  44. "b#",
  45. "w!",
  46. "bd!",
  47. "",
  48. }
  49. local action = vim.fn.confirm("Buffers?", "&new\n&last\n&save\n&delete\n&all")
  50. if action > 0 then
  51. vim.cmd(actions[action])
  52. end
  53. end
  54. -- tab menu
  55. function TabsMenu()
  56. local actions = {
  57. "tabnew",
  58. "tablast",
  59. "tabclose",
  60. "",
  61. }
  62. local action = vim.fn.confirm("Tabs?", "&new\n&last\n&close\n&all")
  63. if action > 0 then
  64. -- trying to close the last tab returns error
  65. if action == 3 and #vim.api.nvim_list_tabpages() == 1 then return end
  66. vim.cmd(actions[action])
  67. end
  68. end