autocmds.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. -- Fern settings
  67. augroup("OnEnteringFernWindow", { clear = true })
  68. autocmd("FileType", {
  69. group = "OnEnteringFernWindow",
  70. pattern = "fern",
  71. callback = function()
  72. vim.api.nvim_buf_set_keymap(0, "n", "<CR>", "<Plug>(fern-action-open:select)", { silent = true })
  73. -- Add more fern local mappings here if needed
  74. end,
  75. })
  76. -- Emmet Enabler
  77. augroup("EmmetEnabler", { clear = true })
  78. autocmd("FileType", {
  79. group = "EmmetEnabler",
  80. pattern = { "html", "css" },
  81. command = "EmmetInstall",
  82. })
  83. -- Glyph palette
  84. augroup("my-glyph-palette", { clear = true })
  85. autocmd("FileType", {
  86. group = "my-glyph-palette",
  87. pattern = { "fern", "startify" },
  88. callback = function()
  89. vim.fn["glyph_palette#apply"]()
  90. end,
  91. })
  92. -- Relative line numbers
  93. augroup("numbertoggle", { clear = true })
  94. autocmd({ "BufEnter", "FocusGained", "InsertLeave" }, {
  95. group = "numbertoggle",
  96. pattern = "*",
  97. callback = function()
  98. if vim.opt.number:get() then
  99. vim.opt.relativenumber = true
  100. end
  101. end,
  102. })
  103. autocmd({ "BufLeave", "FocusLost", "InsertEnter" }, {
  104. group = "numbertoggle",
  105. pattern = "*",
  106. callback = function()
  107. if vim.opt.number:get() then
  108. vim.opt.relativenumber = false
  109. end
  110. end,
  111. })
  112. -- Vimspector log file fix
  113. augroup("fixVimSpectorLogFile", { clear = true })
  114. autocmd("User", {
  115. group = "fixVimSpectorLogFile",
  116. pattern = "VimspectorDebugEnded",
  117. callback = function()
  118. vim.fn.system("mv ~/.vimspector.log " .. vim.fn.expand("$XDG_CACHE_HOME") .. "/vim/")
  119. end,
  120. })
  121. -- LSP Highlight
  122. augroup("LspHighlight", { clear = true })
  123. autocmd("CursorHold", {
  124. group = "LspHighlight",
  125. callback = function()
  126. vim.lsp.buf.document_highlight()
  127. end,
  128. })
  129. autocmd("CursorMoved", {
  130. group = "LspHighlight",
  131. callback = function()
  132. vim.lsp.buf.clear_references()
  133. end,
  134. })
  135. -- TeX Macros
  136. augroup("texMacros", { clear = true })
  137. local function map(mode, lhs, rhs, opts)
  138. opts = opts or {}
  139. opts.buffer = true
  140. vim.keymap.set(mode, lhs, rhs, opts)
  141. end
  142. autocmd("FileType", {
  143. group = "texMacros",
  144. pattern = "tex",
  145. callback = function()
  146. 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")
  147. map("i", ";ig", "\\includegraphics[]{<++>}<Esc>6hi")
  148. map("i", ";tw", "width=\\textwidth<Esc>9hi")
  149. map("i", ";th", "height=\\textheight<Esc>10hi")
  150. map("i", ";ni", "\\setlength{\\parindent}{0em}<Esc>")
  151. map("i", ";ger", "\\usepackage[ngerman]{babel}<Esc>")
  152. map("i", ";bf", "\\textbf{}<++><Esc>T{i")
  153. map("i", ";it", "\\textit{}<++><Esc>T{i")
  154. map("i", ";tt", "\\texttt{}<++><Esc>T{i")
  155. map("i", ";fr", "\\begin{frame}<CR>\\frametitle{}<CR><++><CR>\\end{frame}<Esc>kklli")
  156. map("i", ";sw", "\\switchcolumn[]<++><Esc>4hi")
  157. map("i", ";up", "\\usepackage{}<Esc>i")
  158. map("i", ";hy", "\\hyphenation{}<Esc>i")
  159. map("i", ";s1", "\\section{}<Esc>i")
  160. map("i", ";s2", "\\subsection{}<Esc>i")
  161. map("i", ";s3", "\\subsubsection{}<Esc>i")
  162. map("i", ";ap", "\\bibliographystyle{apacite}<CR>\\bibliography{}<Esc>i")
  163. end,
  164. })