tex.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. local M = {}
  2. M.config = function()
  3. O.lang.latex = {
  4. filetypes = { "tex", "bib" },
  5. aux_directory = nil,
  6. bibtex_formatter = "texlab",
  7. diagnostics_delay = 300,
  8. formatter_line_length = 80,
  9. latex_formatter = "latexindent",
  10. lsp = {
  11. path = DATA_PATH .. "/lspinstall/latex/texlab",
  12. },
  13. build = {
  14. executable = "latexmk",
  15. args = { "-pdf", "-interaction=nonstopmode", "-synctex=1", "%f" },
  16. on_save = false,
  17. forward_search_after = false,
  18. },
  19. chktex = {
  20. on_open_and_save = false,
  21. on_edit = false,
  22. },
  23. forward_search = {
  24. executable = nil,
  25. args = {},
  26. },
  27. latexindent = {
  28. ["local"] = nil,
  29. modify_line_breaks = false,
  30. },
  31. diagnostics = {
  32. virtual_text = { spacing = 0, prefix = "" },
  33. signs = true,
  34. underline = true,
  35. },
  36. linters = { "chktex" },
  37. auto_save = false,
  38. ignore_errors = {},
  39. }
  40. end
  41. M.format = function()
  42. -- TODO: implement formatter for language
  43. return "No formatter available!"
  44. end
  45. M.lint = function()
  46. require("lint").linters_by_ft = {
  47. tex = O.lang.latex.linters,
  48. }
  49. end
  50. M.lsp = function()
  51. if require("lv-utils").check_lsp_client_active "texlab" then
  52. return
  53. end
  54. local preview_settings = {}
  55. local sumatrapdf_args = { "-reuse-instance", "%p", "-forward-search", "%f", "%l" }
  56. local evince_args = { "-f", "%l", "%p", '"code -g %f:%l"' }
  57. local okular_args = { "--unique", "file:%p#src:%l%f" }
  58. local zathura_args = { "--synctex-forward", "%l:1:%f", "%p" }
  59. local qpdfview_args = { "--unique", "%p#src:%f:%l:1" }
  60. local skim_args = { "%l", "%p", "%f" }
  61. if O.lang.latex.forward_search.executable == "C:/Users/{User}/AppData/Local/SumatraPDF/SumatraPDF.exe" then
  62. preview_settings = sumatrapdf_args
  63. elseif O.lang.latex.forward_search.executable == "evince-synctex" then
  64. preview_settings = evince_args
  65. elseif O.lang.latex.forward_search.executable == "okular" then
  66. preview_settings = okular_args
  67. elseif O.lang.latex.forward_search.executable == "zathura" then
  68. preview_settings = zathura_args
  69. elseif O.lang.latex.forward_search.executable == "qpdfview" then
  70. preview_settings = qpdfview_args
  71. elseif O.lang.latex.forward_search.executable == "/Applications/Skim.app/Contents/SharedSupport/displayline" then
  72. preview_settings = skim_args
  73. end
  74. require("lspconfig").texlab.setup {
  75. cmd = { O.lang.latex.lsp.path },
  76. on_attach = require("lsp").common_on_attach,
  77. handlers = {
  78. ["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
  79. virtual_text = O.lang.latex.diagnostics.virtual_text,
  80. signs = O.lang.latex.diagnostics.signs,
  81. underline = O.lang.latex.diagnostics.underline,
  82. update_in_insert = true,
  83. }),
  84. },
  85. filetypes = { "tex", "bib" },
  86. settings = {
  87. texlab = {
  88. auxDirectory = O.lang.latex.aux_directory,
  89. bibtexFormatter = O.lang.latex.bibtex_formatter,
  90. build = {
  91. args = O.lang.latex.build.args,
  92. executable = O.lang.latex.build.executable,
  93. forwardSearchAfter = O.lang.latex.build.forward_search_after,
  94. onSave = O.lang.latex.build.on_save,
  95. },
  96. chktex = {
  97. onEdit = O.lang.latex.chktex.on_edit,
  98. onOpenAndSave = O.lang.latex.chktex.on_open_and_save,
  99. },
  100. diagnosticsDelay = O.lang.latex.diagnostics_delay,
  101. formatterLineLength = O.lang.latex.formatter_line_length,
  102. forwardSearch = {
  103. args = preview_settings,
  104. executable = O.lang.latex.forward_search.executable,
  105. },
  106. latexFormatter = O.lang.latex.latex_formatter,
  107. latexindent = {
  108. modifyLineBreaks = O.lang.latex.latexindent.modify_line_breaks,
  109. },
  110. },
  111. },
  112. }
  113. vim.g.vimtex_compiler_method = "latexmk"
  114. vim.g.vimtex_view_method = "zathura"
  115. vim.g.vimtex_fold_enabled = 0
  116. vim.g.vimtex_quickfix_ignore_filters = O.lang.latex.ignore_errors
  117. O.plugin.which_key.mappings["t"] = {
  118. name = "+Latex",
  119. c = { "<cmd>VimtexCompile<cr>", "Toggle Compilation Mode" },
  120. f = { "<cmd>call vimtex#fzf#run()<cr>", "Fzf Find" },
  121. i = { "<cmd>VimtexInfo<cr>", "Project Information" },
  122. s = { "<cmd>VimtexStop<cr>", "Stop Project Compilation" },
  123. t = { "<cmd>VimtexTocToggle<cr>", "Toggle Table Of Content" },
  124. v = { "<cmd>VimtexView<cr>", "View PDF" },
  125. b = { "<cmd>TexlabBuild<cr>", "Build with Texlab" },
  126. p = { "<cmd>TexlabForward<cr>", "Preview with Texlab" },
  127. }
  128. -- Compile on initialization, cleanup on quit
  129. vim.api.nvim_exec(
  130. [[
  131. augroup vimtex_event_1
  132. au!
  133. au User VimtexEventQuit call vimtex#compiler#clean(0)
  134. au User VimtexEventInitPost call vimtex#compiler#compile()
  135. augroup END
  136. ]],
  137. false
  138. )
  139. if O.lang.latex.auto_save then
  140. vim.api.nvim_exec([[au FocusLost * :wa]], false)
  141. end
  142. end
  143. M.dap = function()
  144. -- TODO: implement dap
  145. return "No DAP configured!"
  146. end
  147. return M