autocmds.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. local augroup = vim.api.nvim_create_augroup
  2. local autocmd = vim.api.nvim_create_autocmd
  3. -- Fix latex commenting
  4. augroup("fixLatexComments", { clear = true })
  5. autocmd("FileType", {
  6. group = "fixLatexComments",
  7. pattern = { "plaintex", "tex", "latex" },
  8. command = "syntax spell toplevel",
  9. })
  10. -- Wrap Java at 100 lines
  11. augroup("wrapJavaAt100LinesException", { clear = true })
  12. autocmd("FileType", {
  13. group = "wrapJavaAt100LinesException",
  14. pattern = "java",
  15. callback = function()
  16. vim.opt_local.colorcolumn = "100"
  17. end,
  18. })
  19. -- Markdown Math and Liquid highlighting
  20. local function math_and_liquid()
  21. vim.fn.matchadd("math", [[\$\$]])
  22. vim.fn.matchadd("math_block", [[\$[^$].\{-}\$]])
  23. vim.fn.matchadd("liquid", [[{%.*%}]])
  24. vim.fn.matchadd("highlight_block", [[{% highlight .*%}]])
  25. vim.fn.matchadd("highlight_block", [[```]])
  26. vim.cmd([[
  27. hi link math Statement
  28. hi link liquid Statement
  29. hi link highlight_block Function
  30. hi link math_block Function
  31. ]])
  32. end
  33. augroup("callMathFunction", { clear = true })
  34. autocmd({ "BufRead", "BufNewFile", "BufEnter" }, {
  35. group = "callMathFunction",
  36. pattern = { "*.md", "*.markdown" },
  37. callback = math_and_liquid,
  38. })
  39. -- Disable automatic comment
  40. augroup("disableAutoComment", { clear = true })
  41. autocmd("FileType", {
  42. group = "disableAutoComment",
  43. pattern = "*",
  44. callback = function()
  45. vim.opt_local.formatoptions:remove({ "c", "r", "o" })
  46. end,
  47. })
  48. -- Clear TeX build after exiting
  49. augroup("texclear", { clear = true })
  50. autocmd("VimLeavePre", {
  51. group = "texclear",
  52. pattern = { "*.tex", "*.md" },
  53. callback = function()
  54. vim.fn.system('texclear "' .. vim.fn.expand("%") .. '"')
  55. end,
  56. })
  57. -- TI-Basic filetype
  58. augroup("tibasic", { clear = true })
  59. autocmd({ "BufNewFile", "BufRead" }, {
  60. group = "tibasic",
  61. pattern = { "*.tibasic", "*.tib" },
  62. callback = function()
  63. vim.bo.filetype = "python"
  64. end,
  65. })
  66. -- Emmet Enabler
  67. augroup("EmmetEnabler", { clear = true })
  68. autocmd("FileType", {
  69. group = "EmmetEnabler",
  70. pattern = { "html", "css" },
  71. command = "EmmetInstall",
  72. })
  73. -- Relative line numbers
  74. augroup("numbertoggle", { clear = true })
  75. autocmd({ "BufEnter", "FocusGained", "InsertLeave" }, {
  76. group = "numbertoggle",
  77. pattern = "*",
  78. callback = function()
  79. if vim.opt.number:get() then
  80. vim.opt.relativenumber = true
  81. end
  82. end,
  83. })
  84. autocmd({ "BufLeave", "FocusLost", "InsertEnter" }, {
  85. group = "numbertoggle",
  86. pattern = "*",
  87. callback = function()
  88. if vim.opt.number:get() then
  89. vim.opt.relativenumber = false
  90. end
  91. end,
  92. })
  93. -- Vimspector log file fix
  94. augroup("fixVimSpectorLogFile", { clear = true })
  95. autocmd("User", {
  96. group = "fixVimSpectorLogFile",
  97. pattern = "VimspectorDebugEnded",
  98. callback = function()
  99. vim.fn.system("mv ~/.vimspector.log " .. vim.fn.expand("$XDG_CACHE_HOME") .. "/vim/")
  100. end,
  101. })
  102. -- LSP Highlight
  103. augroup("LspHighlight", { clear = true })
  104. autocmd("CursorHold", {
  105. group = "LspHighlight",
  106. callback = function()
  107. vim.lsp.buf.document_highlight()
  108. end,
  109. })
  110. autocmd("CursorMoved", {
  111. group = "LspHighlight",
  112. callback = function()
  113. vim.lsp.buf.clear_references()
  114. end,
  115. })
  116. -- TeX Macros
  117. augroup("texMacros", { clear = true })
  118. local function map(mode, lhs, rhs, opts)
  119. opts = opts or {}
  120. opts.buffer = true
  121. vim.keymap.set(mode, lhs, rhs, opts)
  122. end
  123. autocmd("FileType", {
  124. group = "texMacros",
  125. pattern = "tex",
  126. callback = function()
  127. map("i", ";beg", "\\begin{<Esc>yypkI\\begin{<Esc>A}<Esc>o<Esc>0i<Esc>jI\\end{<Esc>A}<CR><Esc>0i<CR><Esc>0i<++><Esc>3ki")
  128. map("i", ";ig", "\\includegraphics[]{<++>}<Esc>6hi")
  129. map("i", ";tw", "width=\\textwidth<Esc>9hi")
  130. map("i", ";th", "height=\\textheight<Esc>10hi")
  131. map("i", ";ni", "\\setlength{\\parindent}{0em}<Esc>")
  132. map("i", ";ger", "\\usepackage[ngerman]{babel}<Esc>")
  133. map("i", ";bf", "\\textbf{}<++><Esc>T{i")
  134. map("i", ";it", "\\textit{}<++><Esc>T{i")
  135. map("i", ";tt", "\\texttt{}<++><Esc>T{i")
  136. map("i", ";fr", "\\begin{frame}<CR>\\frametitle{}<CR><++><CR>\\end{frame}<Esc>kklli")
  137. map("i", ";sw", "\\switchcolumn[]<++><Esc>4hi")
  138. map("i", ";up", "\\usepackage{}<Esc>i")
  139. map("i", ";hy", "\\hyphenation{}<Esc>i")
  140. map("i", ";s1", "\\section{}<Esc>i")
  141. map("i", ";s2", "\\subsection{}<Esc>i")
  142. map("i", ";s3", "\\subsubsection{}<Esc>i")
  143. map("i", ";ap", "\\bibliographystyle{apacite}<CR>\\bibliography{}<Esc>i")
  144. end,
  145. })