@@ -8,23 +8,21 @@ function M.quartoPreview(opts)
88 opts = opts or {}
99 local args = opts .args or ' '
1010
11- -- find root directory / check if it is a project
11+ -- Find root directory / check if it is a project
1212 local buffer_path = api .nvim_buf_get_name (0 )
1313 local root_dir = util .root_pattern ' _quarto.yml' (buffer_path )
1414 local cmd
1515 local mode
16+
1617 if root_dir then
1718 mode = ' project'
18- cmd = ' quarto preview' .. ' ' .. args
19+ cmd = ' quarto preview ' .. args
1920 else
2021 mode = ' file'
21- if vim .loop .os_uname ().sysname == ' Windows_NT' then
22- cmd = ' quarto preview \\ "' .. buffer_path .. ' \\ "' .. ' ' .. args
23- else
24- cmd = " quarto preview '" .. buffer_path .. " '" .. ' ' .. args
25- end
22+ cmd = ' quarto preview ' .. vim .fn .shellescape (buffer_path ) .. ' ' .. args
2623 end
2724
25+ -- Check file extensions
2826 local quarto_extensions = { ' .qmd' , ' .Rmd' , ' .ipynb' , ' .md' }
2927 local file_extension = buffer_path :match ' ^.+(%..+)$'
3028 if mode == ' file' and not file_extension then
@@ -36,19 +34,29 @@ function M.quartoPreview(opts)
3634 return
3735 end
3836
39- -- run command in embedded terminal
40- -- in a new tab and go back to the buffer
41- vim .cmd (' tabedit term://' .. cmd )
37+ -- Store current tabpage
38+ local current_tabpage = vim .api .nvim_get_current_tabpage ()
39+
40+ -- Open a new tab for the terminal
41+ vim .cmd (' tabnew' )
42+ local term_buf = vim .api .nvim_create_buf (true , false )
43+ vim .api .nvim_set_current_buf (term_buf )
44+
45+ vim .fn .termopen (cmd , {
46+ on_exit = function (_ , exit_code , _ )
47+ if exit_code ~= 0 then
48+ vim .notify (" Quarto preview exited with code " .. exit_code , vim .log .levels .ERROR )
49+ end
50+ end ,
51+ })
52+
53+ -- Store the terminal buffer and return to previous tab
4254 local quartoOutputBuf = vim .api .nvim_get_current_buf ()
43- vim .cmd ' tabprevious '
55+ vim .api . nvim_set_current_tabpage ( current_tabpage )
4456 api .nvim_buf_set_var (0 , ' quartoOutputBuf' , quartoOutputBuf )
4557
46- if not cfg .config then
47- return
48- end
49-
50- -- close preview terminal on exit of the quarto buffer
51- if cfg .config .closePreviewOnExit then
58+ -- Close preview terminal on exit of the Quarto buffer
59+ if cfg .config and cfg .config .closePreviewOnExit then
5260 api .nvim_create_autocmd ({ ' QuitPre' , ' WinClosed' }, {
5361 buffer = api .nvim_get_current_buf (),
5462 group = api .nvim_create_augroup (' quartoPreview' , {}),
0 commit comments