Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Replace term:// with termopen() to resolve wildcard expansion error #153

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ example*.hs
examples/*.docx
examples/*.html
examples/*.pdf
.DS_Store
42 changes: 25 additions & 17 deletions lua/quarto/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ 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

-- Check file extensions
local quarto_extensions = { '.qmd', '.Rmd', '.ipynb', '.md' }
local file_extension = buffer_path:match '^.+(%..+)$'
if mode == 'file' and not file_extension then
Expand All @@ -36,19 +34,29 @@ function M.quartoPreview(opts)
return
end

-- run command in embedded terminal
-- in a new tab and go back to the buffer
vim.cmd('tabedit term://' .. cmd)
-- Store current tabpage
local current_tabpage = vim.api.nvim_get_current_tabpage()

-- Open a new tab for the terminal
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, _)
if exit_code ~= 0 then
vim.notify("Quarto preview exited with code " .. exit_code, vim.log.levels.ERROR)
end
end,
})

-- Store the terminal buffer and return to previous tab
local quartoOutputBuf = vim.api.nvim_get_current_buf()
vim.cmd 'tabprevious'
vim.api.nvim_set_current_tabpage(current_tabpage)
api.nvim_buf_set_var(0, 'quartoOutputBuf', quartoOutputBuf)

if not cfg.config then
return
end

-- 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', {}),
Expand Down
Loading