typst.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. local function parse_stderr(data)
  2. local message = table.concat(data, "\n")
  3. local level = "info"
  4. -- Check each line for error or warning
  5. for line in message:gmatch("[^\r\n]+") do
  6. local lower_line = line:lower()
  7. if lower_line:match("^%s*error:") then
  8. level = "error"
  9. break
  10. elseif lower_line:match("^%s*warning:") then
  11. level = "warn"
  12. -- Don't break here, in case an error appears later
  13. end
  14. end
  15. return message, level
  16. end
  17. local function typst_build_and_open_sioyek()
  18. local bufname = vim.fn.expand('%:p')
  19. local pdf_path = vim.fn.fnamemodify(bufname, ':r') .. '.pdf'
  20. local cmd = string.format('typst compile "%s"', bufname)
  21. vim.fn.jobstart(cmd, {
  22. on_exit = function(_, exit_code)
  23. if exit_code == 0 then
  24. vim.notify("Typst build completed successfully", vim.log.levels.INFO, {
  25. title = "Typst Build",
  26. timeout = 3000,
  27. })
  28. -- Now open or reload Sioyek
  29. local sioyek_running = vim.fn.system('pgrep sioyek'):match('%d+')
  30. if sioyek_running then
  31. vim.fn.system('sioyek --execute-command reload_no_flicker')
  32. vim.notify("Sioyek reloaded", vim.log.levels.INFO, {
  33. title = "Sioyek",
  34. timeout = 2000,
  35. })
  36. else
  37. vim.fn.jobstart('sioyek "' .. pdf_path .. '"', {
  38. on_exit = function(_, sioyek_exit_code)
  39. if sioyek_exit_code == 0 then
  40. vim.notify("Sioyek opened", vim.log.levels.INFO, {
  41. title = "Sioyek",
  42. timeout = 2000,
  43. })
  44. else
  45. vim.notify("Failed to open Sioyek", vim.log.levels.ERROR, {
  46. title = "Sioyek",
  47. timeout = 3000,
  48. })
  49. end
  50. end
  51. })
  52. end
  53. else
  54. vim.notify("Typst build failed", vim.log.levels.ERROR, {
  55. title = "Typst Build",
  56. timeout = 5000,
  57. })
  58. end
  59. end,
  60. stderr_buffered = true,
  61. on_stderr = function(_, data)
  62. if data and #data > 0 then
  63. local message, level = parse_stderr(data)
  64. vim.notify(message, vim.log.levels[level:upper()], {
  65. title = "Typst Build",
  66. timeout = 5000,
  67. })
  68. end
  69. end,
  70. })
  71. end
  72. vim.api.nvim_create_user_command('TypstBuild', typst_build_and_open_sioyek, {})
  73. -- Create an autocommand group
  74. local typst_group = vim.api.nvim_create_augroup('TypstBuildGroup', { clear = true })
  75. -- Create a command to toggle the autocommand
  76. local typst_auto_build_enabled = false
  77. -- Function to create the autocommand
  78. local function create_typst_autocmd()
  79. vim.api.nvim_create_autocmd('BufWritePost', {
  80. group = typst_group,
  81. pattern = '*.typ',
  82. callback = function()
  83. vim.cmd('TypstBuild')
  84. end,
  85. })
  86. end
  87. -- Create a command to toggle the autocommand
  88. vim.api.nvim_create_user_command('TypstAutoBuild', function()
  89. typst_auto_build_enabled = not typst_auto_build_enabled
  90. if typst_auto_build_enabled then
  91. create_typst_autocmd()
  92. vim.notify('Typst auto-build enabled', vim.log.levels.INFO)
  93. else
  94. vim.api.nvim_clear_autocmds({ group = typst_group })
  95. vim.notify('Typst auto-build disabled', vim.log.levels.INFO)
  96. end
  97. end, {})
  98. return {
  99. -- { "kaarmu/typst.vim", ft = "typst", lazy = false },
  100. -- {
  101. -- "chomosuke/typst-preview.nvim",
  102. -- lazy = false, -- or ft = 'typst'
  103. -- version = "0.3.*",
  104. -- config = function()
  105. -- require("typst-preview").setup({
  106. -- -- Setting this true will enable printing debug information with print()
  107. -- debug = false,
  108. --
  109. -- -- Custom format string to open the output link provided with %s
  110. -- -- Example: open_cmd = 'firefox %s -P typst-preview --class typst-preview'
  111. -- open_cmd = 'sioyek --execute-command reload_no_flicker %s',
  112. --
  113. -- -- Setting this to 'always' will invert black and white in the preview
  114. -- -- Setting this to 'auto' will invert depending if the browser has enable
  115. -- -- dark mode
  116. -- invert_colors = "never",
  117. --
  118. -- -- Whether the preview will follow the cursor in the source file
  119. -- follow_cursor = true,
  120. --
  121. -- -- Provide the path to binaries for dependencies.
  122. -- -- Setting this will skip the download of the binary by the plugin.
  123. -- -- Warning: Be aware that your version might be older than the one
  124. -- -- required.
  125. -- dependencies_bin = {
  126. -- -- if you are using tinymist, just set ['typst-preview'] = "tinymist".
  127. -- ["typst-preview"] = nil,
  128. -- ["websocat"] = nil,
  129. -- },
  130. --
  131. -- -- This function will be called to determine the root of the typst project
  132. -- get_root = function(path_of_main_file)
  133. -- return vim.fn.fnamemodify(path_of_main_file, ":p:h")
  134. -- end,
  135. --
  136. -- -- This function will be called to determine the main file of the typst
  137. -- -- project.
  138. -- get_main_file = function(path_of_buffer)
  139. -- return path_of_buffer
  140. -- end,
  141. -- })
  142. -- end,
  143. -- build = function()
  144. -- require("typst-preview").update()
  145. -- end,
  146. -- },
  147. }