From 8cc6ace7a852003c601bb396bb3f6aaa8fb43fbc Mon Sep 17 00:00:00 2001 From: oddish3 Date: Wed, 4 Dec 2024 10:19:08 +0000 Subject: [PATCH 1/2] use termopen() instead of term:// --- .gitignore | 1 + lua/quarto/init.lua | 53 +++++++++++++++++---------------------------- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index ea0657d..5d9db26 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ example*.hs examples/*.docx examples/*.html examples/*.pdf +.DS_Store diff --git a/lua/quarto/init.lua b/lua/quarto/init.lua index a16ad37..1f48ee4 100644 --- a/lua/quarto/init.lua +++ b/lua/quarto/init.lua @@ -8,53 +8,40 @@ function M.quartoPreview(opts) opts = opts or {} local args = opts.args or '' - -- find root directory / check if it is a project + -- Find root directory / check if it is a project local buffer_path = api.nvim_buf_get_name(0) local root_dir = util.root_pattern '_quarto.yml'(buffer_path) local cmd - local mode + if root_dir then - mode = 'project' - cmd = 'quarto preview' .. ' ' .. args + cmd = 'quarto preview ' .. args else - mode = 'file' - if vim.loop.os_uname().sysname == 'Windows_NT' then - cmd = 'quarto preview \\"' .. buffer_path .. '\\"' .. ' ' .. args - else - cmd = "quarto preview '" .. buffer_path .. "'" .. ' ' .. args - end + cmd = 'quarto preview ' .. vim.fn.shellescape(buffer_path) .. ' ' .. args end - local quarto_extensions = { '.qmd', '.Rmd', '.ipynb', '.md' } - local file_extension = buffer_path:match '^.+(%..+)$' - if mode == 'file' and not file_extension then - vim.notify 'Not in a file. exiting.' - return - end - if mode == 'file' and not tools.contains(quarto_extensions, file_extension) then - vim.notify('Not a quarto file, ends in ' .. file_extension .. ' exiting.') - return - end + -- Open a new tab for the terminal + vim.cmd('tabedit') + local term_buf = vim.api.nvim_get_current_buf() - -- run command in embedded terminal - -- in a new tab and go back to the buffer - vim.cmd('tabedit term://' .. cmd) - local quartoOutputBuf = vim.api.nvim_get_current_buf() - vim.cmd 'tabprevious' - api.nvim_buf_set_var(0, 'quartoOutputBuf', quartoOutputBuf) + vim.fn.termopen(cmd, { + on_exit = function(_, exit_code, _) + if exit_code ~= 0 then + vim.notify("Quarto preview exited with code " .. exit_code, vim.log.levels.ERROR) + end + end, + }) - if not cfg.config then - return - end + -- Store the terminal buffer + api.nvim_buf_set_var(0, 'quartoOutputBuf', term_buf) - -- close preview terminal on exit of the quarto buffer - if cfg.config.closePreviewOnExit then + -- Close preview terminal on exit of the Quarto buffer + if cfg.config and cfg.config.closePreviewOnExit then api.nvim_create_autocmd({ 'QuitPre', 'WinClosed' }, { buffer = api.nvim_get_current_buf(), group = api.nvim_create_augroup('quartoPreview', {}), callback = function(_, _) - if api.nvim_buf_is_loaded(quartoOutputBuf) then - api.nvim_buf_delete(quartoOutputBuf, { force = true }) + if api.nvim_buf_is_loaded(term_buf) then + api.nvim_buf_delete(term_buf, { force = true }) end end, }) From e7cf71b89009472c18e022d50321407f0fb2ee71 Mon Sep 17 00:00:00 2001 From: oddish3 Date: Wed, 4 Dec 2024 12:26:23 +0000 Subject: [PATCH 2/2] fixed quarto preview function with original functionality, included on_exit callback for termopen --- lua/quarto/init.lua | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/lua/quarto/init.lua b/lua/quarto/init.lua index 1f48ee4..9e0fe99 100644 --- a/lua/quarto/init.lua +++ b/lua/quarto/init.lua @@ -12,16 +12,35 @@ function M.quartoPreview(opts) local buffer_path = api.nvim_buf_get_name(0) local root_dir = util.root_pattern '_quarto.yml'(buffer_path) local cmd + local mode if root_dir then + mode = 'project' cmd = 'quarto preview ' .. args else + mode = 'file' cmd = 'quarto preview ' .. vim.fn.shellescape(buffer_path) .. ' ' .. args end + -- Check file extensions + local quarto_extensions = { '.qmd', '.Rmd', '.ipynb', '.md' } + local file_extension = buffer_path:match '^.+(%..+)$' + if mode == 'file' and not file_extension then + vim.notify 'Not in a file. exiting.' + return + end + if mode == 'file' and not tools.contains(quarto_extensions, file_extension) then + vim.notify('Not a quarto file, ends in ' .. file_extension .. ' exiting.') + return + end + + -- Store current tabpage + local current_tabpage = vim.api.nvim_get_current_tabpage() + -- Open a new tab for the terminal - vim.cmd('tabedit') - local term_buf = vim.api.nvim_get_current_buf() + vim.cmd('tabnew') + local term_buf = vim.api.nvim_create_buf(true, false) + vim.api.nvim_set_current_buf(term_buf) vim.fn.termopen(cmd, { on_exit = function(_, exit_code, _) @@ -31,8 +50,10 @@ function M.quartoPreview(opts) end, }) - -- Store the terminal buffer - api.nvim_buf_set_var(0, 'quartoOutputBuf', term_buf) + -- Store the terminal buffer and return to previous tab + local quartoOutputBuf = vim.api.nvim_get_current_buf() + vim.api.nvim_set_current_tabpage(current_tabpage) + api.nvim_buf_set_var(0, 'quartoOutputBuf', quartoOutputBuf) -- Close preview terminal on exit of the Quarto buffer if cfg.config and cfg.config.closePreviewOnExit then @@ -40,8 +61,8 @@ function M.quartoPreview(opts) buffer = api.nvim_get_current_buf(), group = api.nvim_create_augroup('quartoPreview', {}), callback = function(_, _) - if api.nvim_buf_is_loaded(term_buf) then - api.nvim_buf_delete(term_buf, { force = true }) + if api.nvim_buf_is_loaded(quartoOutputBuf) then + api.nvim_buf_delete(quartoOutputBuf, { force = true }) end end, })