tex.lua 4.9 KB

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